Skip to content

Commit 8564b4e

Browse files
committed
Go: Use shared modelCoverage.
1 parent 4b2e8c0 commit 8564b4e

File tree

2 files changed

+37
-86
lines changed

2 files changed

+37
-86
lines changed

go/ql/lib/semmle/go/dataflow/ExternalFlow.qll

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,21 @@ private import internal.FlowSummaryImpl::Private::External
9696
private import codeql.mad.ModelValidation as SharedModelVal
9797
private import codeql.mad.static.MaD as SharedMaD
9898

99-
private module MadInput implements SharedMaD::InputSig { }
99+
private module MadInput implements SharedMaD::InputSig {
100+
string namespaceSegmentSeparator() { result = "/" }
101+
102+
bindingset[p]
103+
string cleanNamespace(string p) {
104+
exists(string noPrefix |
105+
p = fixedVersionPrefix() + noPrefix
106+
or
107+
not p = fixedVersionPrefix() + any(string s) and
108+
noPrefix = p
109+
|
110+
result = noPrefix.regexpReplaceAll(majorVersionSuffixRegex(), "")
111+
)
112+
}
113+
}
100114

101115
private module MaD = SharedMaD::ModelsAsData<Extensions, MadInput>;
102116

@@ -107,78 +121,6 @@ module FlowExtensions = Extensions;
107121
/** Gets the prefix for a group of packages. */
108122
private string groupPrefix() { result = "group:" }
109123

110-
bindingset[p]
111-
private string cleanPackage(string p) {
112-
exists(string noPrefix |
113-
p = fixedVersionPrefix() + noPrefix
114-
or
115-
not p = fixedVersionPrefix() + any(string s) and
116-
noPrefix = p
117-
|
118-
result = noPrefix.regexpReplaceAll(majorVersionSuffixRegex(), "")
119-
)
120-
}
121-
122-
private predicate relevantPackage(string package) {
123-
exists(string p | package = cleanPackage(p) |
124-
sourceModel(p, _, _, _, _, _, _, _, _, _) or
125-
sinkModel(p, _, _, _, _, _, _, _, _, _) or
126-
summaryModel(p, _, _, _, _, _, _, _, _, _, _)
127-
)
128-
}
129-
130-
private predicate packageLink(string shortpkg, string longpkg) {
131-
relevantPackage(shortpkg) and
132-
relevantPackage(longpkg) and
133-
longpkg.prefix(longpkg.indexOf("/")) = shortpkg
134-
}
135-
136-
private predicate canonicalPackage(string package) {
137-
relevantPackage(package) and not packageLink(_, package)
138-
}
139-
140-
private predicate canonicalPkgLink(string package, string subpkg) {
141-
canonicalPackage(package) and
142-
(subpkg = package or packageLink(package, subpkg))
143-
}
144-
145-
/**
146-
* Holds if MaD framework coverage of `package` is `n` api endpoints of the
147-
* kind `(kind, part)`, and `pkgs` is the number of subpackages of `package`
148-
* which have MaD framework coverage (including `package` itself).
149-
*/
150-
predicate modelCoverage(string package, int pkgs, string kind, string part, int n) {
151-
pkgs = strictcount(string subpkg | canonicalPkgLink(package, subpkg)) and
152-
(
153-
part = "source" and
154-
n =
155-
strictcount(string subpkg, string type, boolean subtypes, string name, string signature,
156-
string ext, string output, string provenance, string x |
157-
canonicalPkgLink(package, subpkg) and
158-
subpkg = cleanPackage(x) and
159-
sourceModel(x, type, subtypes, name, signature, ext, output, kind, provenance, _)
160-
)
161-
or
162-
part = "sink" and
163-
n =
164-
strictcount(string subpkg, string type, boolean subtypes, string name, string signature,
165-
string ext, string input, string provenance, string x |
166-
canonicalPkgLink(package, subpkg) and
167-
subpkg = cleanPackage(x) and
168-
sinkModel(x, type, subtypes, name, signature, ext, input, kind, provenance, _)
169-
)
170-
or
171-
part = "summary" and
172-
n =
173-
strictcount(string subpkg, string type, boolean subtypes, string name, string signature,
174-
string ext, string input, string output, string provenance, string x |
175-
canonicalPkgLink(package, subpkg) and
176-
subpkg = cleanPackage(x) and
177-
summaryModel(x, type, subtypes, name, signature, ext, input, output, kind, provenance, _)
178-
)
179-
)
180-
}
181-
182124
/** Provides a query predicate to check the MaD models for validation errors. */
183125
module ModelValidation {
184126
private import codeql.dataflow.internal.AccessPathSyntax as AccessPathSyntax

shared/mad/codeql/mad/static/MaD.qll

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ signature module InputSig {
8787
none()
8888
}
8989

90-
/** Get the separator used between namespace segments. */
90+
/** Gets the separator used between namespace segments. */
9191
default string namespaceSegmentSeparator() { result = "." }
92+
93+
/** Gets a cleaned-up version of the namespace for presentation in model coverage. */
94+
bindingset[ns]
95+
default string cleanNamespace(string ns) { result = ns }
9296
}
9397

9498
module ModelsAsData<ExtensionsSig Extensions, InputSig Input> {
@@ -278,9 +282,11 @@ module ModelsAsData<ExtensionsSig Extensions, InputSig Input> {
278282
}
279283

280284
private predicate relevantNamespace(string namespace) {
281-
sourceModel(namespace, _, _, _, _, _, _, _, _, _) or
282-
sinkModel(namespace, _, _, _, _, _, _, _, _, _) or
283-
summaryModel(namespace, _, _, _, _, _, _, _, _, _, _)
285+
exists(string ns | namespace = Input::cleanNamespace(ns) |
286+
sourceModel(ns, _, _, _, _, _, _, _, _, _) or
287+
sinkModel(ns, _, _, _, _, _, _, _, _, _) or
288+
summaryModel(ns, _, _, _, _, _, _, _, _, _, _)
289+
)
284290
}
285291

286292
private predicate namespaceLink(string shortns, string longns) {
@@ -309,25 +315,28 @@ module ModelsAsData<ExtensionsSig Extensions, InputSig Input> {
309315
(
310316
part = "source" and
311317
n =
312-
strictcount(string subns, string type, boolean subtypes, string name, string signature,
313-
string ext, string output, string provenance |
314-
canonicalNamespaceLink(namespace, subns) and
318+
strictcount(string subns, string subnsClean, string type, boolean subtypes, string name,
319+
string signature, string ext, string output, string provenance |
320+
canonicalNamespaceLink(namespace, subnsClean) and
321+
subnsClean = Input::cleanNamespace(subns) and
315322
sourceModel(subns, type, subtypes, name, signature, ext, output, kind, provenance, _)
316323
)
317324
or
318325
part = "sink" and
319326
n =
320-
strictcount(string subns, string type, boolean subtypes, string name, string signature,
321-
string ext, string input, string provenance |
322-
canonicalNamespaceLink(namespace, subns) and
327+
strictcount(string subns, string subnsClean, string type, boolean subtypes, string name,
328+
string signature, string ext, string input, string provenance |
329+
canonicalNamespaceLink(namespace, subnsClean) and
330+
subnsClean = Input::cleanNamespace(subns) and
323331
sinkModel(subns, type, subtypes, name, signature, ext, input, kind, provenance, _)
324332
)
325333
or
326334
part = "summary" and
327335
n =
328-
strictcount(string subns, string type, boolean subtypes, string name, string signature,
329-
string ext, string input, string output, string provenance |
330-
canonicalNamespaceLink(namespace, subns) and
336+
strictcount(string subns, string subnsClean, string type, boolean subtypes, string name,
337+
string signature, string ext, string input, string output, string provenance |
338+
canonicalNamespaceLink(namespace, subnsClean) and
339+
subnsClean = Input::cleanNamespace(subns) and
331340
summaryModel(subns, type, subtypes, name, signature, ext, input, output, kind, provenance,
332341
_)
333342
)

0 commit comments

Comments
 (0)