Skip to content

Commit b059956

Browse files
committed
wip
1 parent 1754f0e commit b059956

File tree

7 files changed

+39
-42
lines changed

7 files changed

+39
-42
lines changed

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ abstract class ItemNode extends Locatable {
275275
)
276276
}
277277

278-
ItemNode getASuccessorExternal(string name) {
278+
ItemNode getASuccessor(string name) {
279279
exists(SuccessorKind kind | result = this.getASuccessor(name, kind) |
280280
kind.isExternal()
281281
or
@@ -602,7 +602,7 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
602602

603603
Path getTraitPath() { result = super.getTrait().(PathTypeRepr).getPath() }
604604

605-
ItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }
605+
TypeItemNode resolveSelfTy() { result = resolvePath(this.getSelfPath()) }
606606

607607
TraitItemNode resolveTraitTy() { result = resolvePath(this.getTraitPath()) }
608608

@@ -691,7 +691,7 @@ class ImplItemNode extends ImplOrTraitItemNode instanceof Impl {
691691
}
692692
}
693693

694-
private class ImplTraitTypeReprItemNode extends ItemNode instanceof ImplTraitTypeRepr {
694+
private class ImplTraitTypeReprItemNode extends TypeItemNode instanceof ImplTraitTypeRepr {
695695
pragma[nomagic]
696696
Path getABoundPath() {
697697
result = super.getTypeBoundList().getABound().getTypeRepr().(PathTypeRepr).getPath()
@@ -1138,11 +1138,6 @@ private class BuiltinSourceFile extends SourceFileItemNode {
11381138
pragma[nomagic]
11391139
private predicate crateDependencyEdge(SourceFileItemNode file, string name, CrateItemNode dep) {
11401140
exists(CrateItemNode c | dep = c.(Crate).getDependency(name) | file = c.getASourceFile())
1141-
or
1142-
// Give builtin files access to `std`
1143-
file instanceof BuiltinSourceFile and
1144-
dep.getName() = name and
1145-
name = "std"
11461141
}
11471142

11481143
private predicate useTreeDeclares(UseTree tree, string name) {
@@ -1350,7 +1345,17 @@ private predicate pathUsesNamespace(Path p, Namespace n) {
13501345
/** Gets the item that `path` resolves to, if any. */
13511346
cached
13521347
ItemNode resolvePath(RelevantPath path) {
1353-
exists(Namespace ns | result = resolvePath0(path, ns, _) |
1348+
exists(Namespace ns |
1349+
result = resolvePath0(path, ns, _) and
1350+
if path = any(ImplItemNode i).getSelfPath()
1351+
then
1352+
result instanceof TypeItemNode and
1353+
not result instanceof TraitItemNode
1354+
else
1355+
if path = any(ImplItemNode i).getTraitPath()
1356+
then result instanceof TraitItemNode
1357+
else any()
1358+
|
13541359
pathUsesNamespace(path, ns)
13551360
or
13561361
not pathUsesNamespace(path, _) and
@@ -1462,12 +1467,20 @@ private predicate externCrateEdge(ExternCrateItemNode ec, string name, CrateItem
14621467

14631468
pragma[nomagic]
14641469
private predicate preludeItem(string name, ItemNode i) {
1465-
exists(Crate stdOrCore, ModuleLikeNode mod, ModuleItemNode prelude, ModuleItemNode rust |
1466-
stdOrCore.getName() = ["std", "core"] and
1470+
exists(
1471+
Crate stdOrCore, string stdOrCoreName, ModuleLikeNode mod, ModuleItemNode prelude,
1472+
ModuleItemNode rust
1473+
|
1474+
stdOrCore.getName() = stdOrCoreName and
1475+
stdOrCoreName = ["std", "core"]
1476+
|
1477+
name = stdOrCoreName and
1478+
i = stdOrCore
1479+
or
14671480
mod = stdOrCore.getSourceFile() and
1468-
prelude = mod.getASuccessorExternal("prelude") and
1469-
rust = prelude.getASuccessorExternal(["rust_2015", "rust_2018", "rust_2021", "rust_2024"]) and
1470-
i = rust.getASuccessorExternal(name) and
1481+
prelude = mod.getASuccessor("prelude") and
1482+
rust = prelude.getASuccessor(["rust_2015", "rust_2018", "rust_2021", "rust_2024"]) and
1483+
i = rust.getASuccessor(name, _) and
14711484
not name = ["super", "self"]
14721485
)
14731486
}
@@ -1492,7 +1505,7 @@ pragma[nomagic]
14921505
private predicate builtin(string name, ItemNode i) {
14931506
exists(BuiltinSourceFile builtins |
14941507
builtins.getFile().getBaseName() = "types.rs" and
1495-
i = builtins.getASuccessorExternal(name)
1508+
i = builtins.getASuccessor(name, _)
14961509
)
14971510
}
14981511

rust/ql/lib/codeql/rust/internal/TypeInference.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ private import M2
224224
module Consistency {
225225
import M2::Consistency
226226

227-
query predicate nonUniqueCertainType(AstNode n, TypePath path) {
228-
strictcount(CertainTypeInference::inferCertainType(n, path)) > 1
227+
predicate nonUniqueCertainType(AstNode n, TypePath path, Type t) {
228+
strictcount(CertainTypeInference::inferCertainType(n, path)) > 1 and
229+
t = CertainTypeInference::inferCertainType(n, path)
229230
}
230231
}
231232

@@ -1846,7 +1847,7 @@ pragma[nomagic]
18461847
private predicate methodCandidate(Type type, string name, int arity, Impl impl) {
18471848
type = impl.getSelfTy().(TypeMention).resolveType() and
18481849
exists(Function f |
1849-
f = impl.(ImplItemNode).getASuccessorExternal(name) and
1850+
f = impl.(ImplItemNode).getASuccessor(name) and
18501851
f.getParamList().hasSelfParam() and
18511852
arity = f.getParamList().getNumberOfParams()
18521853
)
@@ -1891,7 +1892,7 @@ private module IsInstantiationOfInput implements IsInstantiationOfInputSig<Metho
18911892
bindingset[item, name]
18921893
pragma[inline_late]
18931894
private Function getMethodSuccessor(ItemNode item, string name) {
1894-
result = item.getASuccessorExternal(name)
1895+
result = item.getASuccessor(name)
18951896
}
18961897

18971898
bindingset[tp, name]
@@ -2513,7 +2514,6 @@ private module Debug {
25132514

25142515
Type debugInferCertainNonUniqueType(AstNode n, TypePath path) {
25152516
n = getRelevantLocatable() and
2516-
Consistency::nonUniqueCertainType(n, path) and
2517-
result = CertainTypeInference::inferCertainType(n, path)
2517+
Consistency::nonUniqueCertainType(n, path, result)
25182518
}
25192519
}

rust/ql/lib/codeql/rust/internal/TypeInferenceConsistency.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ query predicate illFormedTypeMention(TypeMention tm) {
1717
tm.fromSource()
1818
}
1919

20+
query predicate nonUniqueCertainType(AstNode n, TypePath path) {
21+
Consistency::nonUniqueCertainType(n, path, _)
22+
}
23+
2024
int getTypeInferenceInconsistencyCounts(string type) {
2125
type = "Missing type parameter ID" and
2226
result = count(TypeParameter tp | missingTypeParameterId(tp) | tp)

rust/ql/lib/codeql/rust/internal/TypeMention.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class NonAliasPathTypeMention extends PathTypeMention {
207207
this = impl.getTraitPath() and
208208
param.getTrait() = resolved and
209209
name = param.getTypeAlias().getName().getText() and
210-
alias = impl.getASuccessorExternal(pragma[only_bind_into](name)) and
210+
alias = impl.getASuccessor(pragma[only_bind_into](name)) and
211211
result = alias.getTypeRepr() and
212212
tp =
213213
TAssociatedTypeTypeParameter(resolved
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
nonUniqueCertainType
2-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:46:13:46:36 | SelfParam | Ptr.&T.Fut2 |
3-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:84:18:84:41 | SelfParam | Ptr.&T.Fut2 |
4-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:112:19:112:42 | SelfParam | Ptr.&T.Fut2 |
5-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:127:19:127:38 | SelfParam | Ptr.&T.Fut2 |
6-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:135:19:135:38 | SelfParam | Ptr.&T.Fut2 |
7-
| file:///home/runner/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/futures-util-0.3.31/src/future/future/flatten.rs:143:19:143:42 | SelfParam | Ptr.&T.Fut2 |
82
| web_frameworks.rs:139:30:139:39 | ...::get(...) | |
93
| web_frameworks.rs:140:34:140:43 | ...::get(...) | |
104
| web_frameworks.rs:141:30:141:39 | ...::get(...) | |

rust/ql/test/query-tests/security/CWE-089/CONSISTENCY/TypeInferenceConsistency.expected

Lines changed: 0 additions & 7 deletions
This file was deleted.

rust/ql/test/query-tests/security/CWE-311/CONSISTENCY/TypeInferenceConsistency.expected

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)