Skip to content

Commit 13f4a6a

Browse files
committed
Rust: Handle path attributes in path resolution
1 parent edb7aaa commit 13f4a6a

File tree

5 files changed

+74
-9
lines changed

5 files changed

+74
-9
lines changed

rust/ql/lib/codeql/files/FileSystem.qll

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Container = Impl::Container;
3434

3535
class Folder = Impl::Folder;
3636

37+
module Folder = Impl::Folder;
38+
3739
/** A file. */
3840
class File extends Container, Impl::File {
3941
/** Holds if this file was extracted from ordinary source code. */

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

+21-6
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,11 @@ private predicate fileModule(SourceFile f, string name, Folder folder) {
655655
)
656656
}
657657

658+
private Meta getPathAttrMeta(Module m) {
659+
result = m.getAnAttr().getMeta() and
660+
result.getPath().getText() = "path"
661+
}
662+
658663
/**
659664
* Holds if `m` is a `mod name;` module declaration, where the corresponding
660665
* module file needs to be looked up in `lookup` or one of its descandants.
@@ -663,12 +668,7 @@ private predicate modImport0(Module m, string name, Folder lookup) {
663668
exists(File f, Folder parent, string fileName |
664669
f = m.getFile() and
665670
not m.hasItemList() and
666-
// TODO: handle
667-
// ```
668-
// #[path = "foo.rs"]
669-
// mod bar;
670-
// ```
671-
not m.getAnAttr().getMeta().getPath().getText() = "path" and
671+
not exists(getPathAttrMeta(m)) and
672672
name = m.getName().getText() and
673673
parent = f.getParentContainer() and
674674
fileName = f.getStem()
@@ -717,6 +717,16 @@ private predicate modImportNestedLookup(Module m, ModuleItemNode ancestor, Folde
717717
)
718718
}
719719

720+
private predicate pathAttrImport(Folder f, Module m, string relativePath) {
721+
exists(Meta meta |
722+
f = m.getFile().getParentContainer() and
723+
meta = getPathAttrMeta(m) and
724+
relativePath = meta.getExpr().(LiteralExpr).getTextValue().regexpCapture("\"(.+)\"", 1)
725+
)
726+
}
727+
728+
private predicate append(Folder f, string relativePath) { pathAttrImport(f, _, relativePath) }
729+
720730
/** Holds if `m` is a `mod name;` item importing file `f`. */
721731
private predicate fileImport(Module m, SourceFile f) {
722732
exists(string name, Folder parent |
@@ -730,6 +740,11 @@ private predicate fileImport(Module m, SourceFile f) {
730740
// `m` is inside a nested module
731741
modImportNestedLookup(m, m, parent)
732742
)
743+
or
744+
exists(Folder folder, string relativePath |
745+
pathAttrImport(folder, m, relativePath) and
746+
f.getFile() = Folder::Append<append/2>::append(folder, relativePath)
747+
)
733748
}
734749

735750
/**

rust/ql/test/library-tests/path-resolution/my2/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ pub mod my3;
1414
#[path = "renamed.rs"]
1515
mod mymod;
1616

17-
use mymod::f; // $ MISSING: item=I1001
17+
use mymod::f; // $ item=I1001

rust/ql/test/library-tests/path-resolution/path-resolution.expected

+3-2
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,13 @@ resolvePath
308308
| my2/mod.rs:10:9:10:33 | ...::nested8 | my2/nested2.rs:22:5:26:5 | mod nested8 |
309309
| my2/mod.rs:10:37:10:40 | self | my2/nested2.rs:22:5:26:5 | mod nested8 |
310310
| my2/mod.rs:17:5:17:9 | mymod | my2/mod.rs:14:1:15:10 | mod mymod |
311+
| my2/mod.rs:17:5:17:12 | ...::f | my2/renamed.rs:1:1:1:13 | fn f |
311312
| my2/my3/mod.rs:3:5:3:5 | g | my2/mod.rs:3:1:6:1 | fn g |
312313
| my2/my3/mod.rs:4:5:4:5 | h | main.rs:50:1:69:1 | fn h |
313-
| my2/my3/mod.rs:7:5:7:9 | super | my2/mod.rs:1:1:17:39 | SourceFile |
314+
| my2/my3/mod.rs:7:5:7:9 | super | my2/mod.rs:1:1:17:30 | SourceFile |
314315
| my2/my3/mod.rs:7:5:7:16 | ...::super | main.rs:1:1:578:2 | SourceFile |
315316
| my2/my3/mod.rs:7:5:7:19 | ...::h | main.rs:50:1:69:1 | fn h |
316-
| my2/my3/mod.rs:8:5:8:9 | super | my2/mod.rs:1:1:17:39 | SourceFile |
317+
| my2/my3/mod.rs:8:5:8:9 | super | my2/mod.rs:1:1:17:30 | SourceFile |
317318
| my2/my3/mod.rs:8:5:8:12 | ...::g | my2/mod.rs:3:1:6:1 | fn g |
318319
| my.rs:3:5:3:10 | nested | my.rs:1:1:1:15 | mod nested |
319320
| my.rs:3:5:3:13 | ...::g | my/nested.rs:19:1:22:1 | fn g |

shared/util/codeql/util/FileSystem.qll

+47
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,53 @@ module Make<InputSig Input> {
218218
/** Gets the URL of this file. */
219219
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
220220
}
221+
222+
/** Provides logic related to `Folder`s. */
223+
module Folder {
224+
/** Holds if `relativePath` needs to be appended to `f`. */
225+
signature predicate appendSig(Folder f, string relativePath);
226+
227+
/** Provides the `append` predicate for appending a relative path onto a folder. */
228+
module Append<appendSig/2 app> {
229+
pragma[nomagic]
230+
private string getComponent(string relativePath, int i) {
231+
app(_, relativePath) and
232+
result = relativePath.replaceAll("\\", "/").regexpFind("[^/]+", i, _)
233+
}
234+
235+
pragma[nomagic]
236+
private Container appendStep(Folder f, string relativePath, int i) {
237+
i = -1 and
238+
app(f, relativePath) and
239+
result = f
240+
or
241+
exists(Container mid, string comp |
242+
mid = appendStep(f, relativePath, i - 1) and
243+
comp = getComponent(relativePath, i) and
244+
if comp = ".."
245+
then result = mid.getParentContainer()
246+
else
247+
if comp = "."
248+
then result = mid
249+
else (
250+
result = mid.getAChildContainer() and
251+
result.getBaseName() = comp
252+
)
253+
)
254+
}
255+
256+
/**
257+
* Gets the file or folder obtained by appending `relativePath` onto `f`.
258+
*/
259+
pragma[nomagic]
260+
Container append(Folder f, string relativePath) {
261+
exists(int components |
262+
components = (-1).maximum(max(int comp | exists(getComponent(relativePath, comp)) | comp)) and
263+
result = appendStep(f, relativePath, components)
264+
)
265+
}
266+
}
267+
}
221268
}
222269

223270
/** A file. */

0 commit comments

Comments
 (0)