Rust: modules declared in a file brought in by `include!` are not resolved (no canonical paths, unresolved calls), e.g. in `windows-sys` and `windows`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 47/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- rust
- 領域
- compilers, testing-qa
調査の方向性
Start with text_range_for_node in rust/extractor/src/translate/base.rs and modImport0 in codeql/rust/internal/PathResolution.qll; compare how the included tree/mod.rs and its inner module are extracted with the normally declared direct module. Reproduce with the supplied two-crate workspace and query, then check that hidden and hidden_extern receive canonical paths and their calls resolve like the control module.
索引モデルが issue の本文から書いたものです。
説明
Description of the issue
When a crate's lib.rs brings in part of its module tree with include!("tree/mod.rs"), and that file declares pub mod inner;, CodeQL looks for inner next to lib.rs instead of next to tree/mod.rs, where rustc finds it. As a result, nothing in inner gets a canonical path, calls into it have no getStaticTarget(), and models-as-data rows for its functions never apply.
The public windows-sys and windows crates are built this way (lib.rs contains include!("Windows/mod.rs")), so Windows API calls made through them are never resolved:
windows-sys0.61.2: 0 of the 10,661 functions undersrc/Windows/Win32have a canonical path.windows0.62.2: 777 of 130,113 do (withcodeql/rust-all0.2.25);CreateFileWis not among them.- Calls to
CreateFileW,ReadFile,RegQueryValueExW,CreateProcessW,ShellExecuteWandrecvthroughwindows-sys, and to the first four throughwindows, are unresolved.
Seen with CodeQL 2.27.2 (codeql/rust-all 0.2.23) and 2.27.4 (codeql/rust-all 0.2.25), Rust 1.99.0, Windows x64. Setting extract_dependencies_as_source=true does not change it.
Reproduction
A two-crate workspace. toy-include brings in module inner through include!; module direct, declared normally, has the same contents and serves as a control.
Cargo.toml
[workspace]
members = ["toy-include", "toy-user"]
resolver = "2"
toy-include/Cargo.toml
[package]
name = "toy-include"
version = "0.1.0"
edition = "2021"
toy-include/src/lib.rs
macro_rules! declare {
($name:ident) => {
extern "C" {
pub fn $name() -> u32;
}
};
}
include!("tree/mod.rs");
pub mod direct;
toy-include/src/tree/mod.rs
pub mod inner;
toy-include/src/tree/inner/mod.rs
pub fn hidden() -> u32 {
1
}
declare!(hidden_extern);
toy-include/src/direct.rs
pub fn visible() -> u32 {
2
}
declare!(visible_extern);
toy-user/Cargo.toml
[package]
name = "toy-user"
version = "0.1.0"
edition = "2021"
[dependencies]
toy-include = { path = "../toy-include" }
toy-user/src/main.rs
fn main() {
let a = toy_include::inner::hidden();
let b = toy_include::direct::visible();
let c = unsafe { toy_include::inner::hidden_extern() };
let d = unsafe { toy_include::direct::visible_extern() };
println!("{}", a + b + c + d);
}
The workspace builds with cargo check. Query (query/qlpack.yml depends on codeql/rust-all: "*"):
query/include-paths.ql
import rust
/** Each function of the `toy-include` crate, with its canonical path (none if CodeQL gives none). */
query predicate functions(string file, string name, string canonicalPath) {
exists(Function f |
f.getFile().getAbsolutePath().matches("%toy-include%") and
file = f.getFile().getAbsolutePath().regexpCapture(".*toy-include[/\\\\]src[/\\\\](.*)", 1) and
name = f.getName().getText() and
if exists(f.getCanonicalPath()) then canonicalPath = f.getCanonicalPath() else canonicalPath = "(none)"
)
}
/** Each call in the program, with the function it resolves to (none if unresolved). */
query predicate calls(int line, string call, string target) {
exists(Call c | c.getFile().getBaseName() = "main.rs" |
line = c.getLocation().getStartLine() and
call = c.toString() and
if exists(c.getStaticTarget())
then target = c.getStaticTarget().getCanonicalPath()
else target = "(unresolved)"
)
}
select 1
Run:
codeql database create db --language=rust --source-root=.
codeql query run query/include-paths.ql --database=db --output=out.bqrs
codeql bqrs decode out.bqrs --format=csv --result-set=functions
codeql bqrs decode out.bqrs --format=csv --result-set=calls
Expected: hidden and hidden_extern have the canonical paths toy_include::inner::hidden and toy_include::inner::hidden_extern, and the calls on lines 2 and 4 resolve to them, like the control module's.
Actual:
"file","name","canonicalPath"
"direct.rs","visible","toy_include::direct::visible"
"direct.rs","visible_extern","toy_include::direct::visible_extern"
"tree/inner/mod.rs","hidden","(none)"
"tree/inner/mod.rs","hidden_extern","(none)"
"line","call","target"
2,"...::hidden(...)","(unresolved)"
3,"...::visible(...)","toy_include::direct::visible"
4,"...::hidden_extern(...)","(unresolved)"
5,"...::visible_extern(...)","toy_include::direct::visible_extern"
Likely cause
- In the extractor,
text_range_for_node(rust/extractor/src/translate/base.rs) keeps a node's location only if it maps into the file being extracted. Nodes produced by aninclude!map into the included file, so theinnermodule in the expansion reportslib.rsas its file. modImport0incodeql/rust/internal/PathResolution.qllthen looks formod inner;relative tolib.rs(src/inner.rs,src/inner/mod.rs) instead ofsrc/tree/.tree/mod.rsis also extracted on its own, and theremod inner;does resolve, but that copy is not part of the crate's module tree.
A possible fix: have the extractor give include!d nodes their locations in the included file (or record which file each include! includes), so that mod files are looked up next to the file that declares them.
- 主要言語
- CodeQL
- スター
- 10.3k
- フォーク
- 2.1k
- 平均マージ
- 2日 6時間
- マージ済み PR(30日)
- 137
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
github/codeql のほかの issue
-
JavaScript externs model StorageEvent() with no type argument対応中かも @asgerf が 14 日前に担当しました。 オープンfalse-positive javascript
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
github/codeql#22632 · コメント 3 件 · リアクション 1 件 · 担当者 2 名 ·
メンテナーはふだん 1 日以内に返信
-
Add AlertSuppression.ql for Rust (inline // codeql[...] suppression)対応中かも @cnuss が 215 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
github/codeql#21637 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
false-positive
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
github/codeql#21076 · コメント 3 件 · リアクション 3 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 47/100
メンテナーはふだん 1 日以内に返信
-
False positive java/local-variable-is-never-read for Kotlin data object-generated equals temporariesオープンfalse-positive
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
github/codeql#22987 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
0.kind: enhancement 9.needs: package (update)
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
S3: friction & debt ws:tooling
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
MedakaLang/medaka#4865 ·
メンテナーはふだん 1 日以内に返信
-
Bug: the Atlas reports the five injected `$` helpers as unknown identifiers, including in the default scaffold対応中かも @ShahadatTest が今日担当しました。 オープンbug Compiler difficulty:easy good first issue help wanted Tooling
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Avenx-JS/avenx-js#1615 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
メンテナーはふだん 1 日以内に返信
-
x/arch/x86/x86asm: disp16/disp32 memory displacements are not sign-extended対応中かも @mhr3 が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信