From ab178da336824b4ef41224657f0b4c3ef185b431 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 19:17:02 +0100 Subject: [PATCH 1/3] fix(analysis): emit global read-before-write issues in instruction order The issues of one function came out in the iteration order of a DenseMap keyed by GlobalVariable*, so their order followed the globals' addresses. It changed from one run to the next: a function reading six such globals gave 5 orders in 8 runs at --jobs=1. Emit them in the order of the reads, which is the instruction order the function already numbers, the arguments of one call in their order. Sorting the selected reads by that number alone would leave two globals read by the same call in address order. The issues themselves do not change. --- src/analysis/GlobalReadBeforeWriteAnalysis.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/analysis/GlobalReadBeforeWriteAnalysis.cpp b/src/analysis/GlobalReadBeforeWriteAnalysis.cpp index 6c04f5fd..01ca442e 100644 --- a/src/analysis/GlobalReadBeforeWriteAnalysis.cpp +++ b/src/analysis/GlobalReadBeforeWriteAnalysis.cpp @@ -477,18 +477,20 @@ namespace ctrace::stack::analysis } } - for (const auto& selected : selectedReads) + // In the order of the reads, which is the instruction order numbered above (the + // arguments of one call in their order). selectedReads' own order follows the + // globals' addresses, which change from one run to the next. + for (const ReadEvent& readEvent : reads) { - const auto* global = selected.first; - const ReadEvent* readEvent = selected.second; - if (!readEvent || !readEvent->inst || !global) + const auto* global = readEvent.global; + if (selectedReads.lookup(global) != &readEvent) continue; GlobalReadBeforeWriteIssue issue; issue.funcName = function.getName().str(); issue.globalName = global->hasName() ? global->getName().str() : std::string(""); - issue.readInst = readEvent->inst; + issue.readInst = readEvent.inst; issue.firstWriteInst = selectedWrites.lookup(global); issue.kind = selectedKinds.lookup(global); issue.hasNonLocalWrite = hasAnyKnownWriteByGlobal.lookup(global); From 41b91f2939555726d2eb3b201c225cedb8a228a8 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sun, 15 Nov 2026 23:47:36 +0100 Subject: [PATCH 2/3] test(analysis): check the order of global read-before-write issues in a function A function's GlobalReadBeforeWrite issues used to come out in an order that followed the globals' addresses. Check that a function reading eight zero-initialized global arrays, six one statement after the other and two as the arguments of one call, gets its eight diagnostics in the order of its reads, at --jobs=1 and at --jobs=4. On one Linux machine, the check failed in each of 16 runs without the previous commit, with the eight diagnostics present every time but in another order, and passed in each of 16 with it. --- run_test.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/run_test.py b/run_test.py index a14c5f68..1784d6bc 100755 --- a/run_test.py +++ b/run_test.py @@ -3012,6 +3012,49 @@ def check_global_read_before_write_identity_across_files() -> bool: return ok +def check_global_read_before_write_order() -> bool: + """ + A function's GlobalReadBeforeWrite diagnostics come out in the order of its reads, two globals + read by one call in the order of its arguments, at any --jobs. Their order used to follow the + globals' addresses, which change from one run to the next. + """ + print("=== Testing GlobalReadBeforeWrite order within a function ===") + names = [f"g_{n}" for n in ("alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel")] + sources = { + "reads.c": "".join(f"int {n}[4];\n" for n in names) + + "void use2(const int *a, const int *b);\n" + + "int reader(int i)\n{\n int s = 0;\n" + + "".join(f" s += {n}[i & 3];\n" for n in names[:6]) + + f" use2({names[6]}, {names[7]});\n return s;\n}}\n", + "other.c": "int other(int i) { return i; }\n", + } + ok = True + with tempfile.TemporaryDirectory(prefix="ct_global_read_order_") as tmp: + root = Path(tmp) + for name, text in sources.items(): + (root / name).write_text(text) + for jobs in ("--jobs=1", "--jobs=4"): + result = run_analyzer([str(root / "reads.c"), str(root / "other.c"), "--format=json", jobs]) + try: + payload = json.loads(result.stdout or "") + except json.JSONDecodeError: + ok = fail_check(f"{jobs}: no JSON output", (result.stdout or "") + (result.stderr or "")) + continue + observed = [ + match.group(1) + for d in payload.get("diagnostics", []) + if d.get("ruleId") == "GlobalReadBeforeWrite" and d.get("location", {}).get("function") == "reader" + for match in [re.search(r"global buffer '([^']+)'", d.get("details", {}).get("message", ""))] + if match + ] + if observed != names: + ok = fail_check(f"{jobs}: expected {names}, got {observed}") + continue + print(f" ✅ {jobs}") + print() + return ok + + def check_size_minus_one_cross_tu() -> bool: """ #157: the length that a function defined in another file passes to a bounded write reaches @@ -5879,6 +5922,7 @@ def record_ok(ok: bool): check_const_param_contract_message_across_files, check_duplicate_if_cross_tu, check_global_read_before_write_identity_across_files, + check_global_read_before_write_order, check_size_minus_one_cross_tu, check_stack_buffer_callee_cross_tu, check_cross_tu_call_graph, From ee256419b6129ee038de00344afe7c84e0d7505b Mon Sep 17 00:00:00 2001 From: Hugo Date: Mon, 16 Nov 2026 05:11:47 +0100 Subject: [PATCH 3/3] test(analysis): tell the order of the reads from alphabetical and module orders The check's eight globals were read in the alphabetical order of their names, which was also their order of declaration and, as Clang creates a global where it is first used, their order in the module; the call passed its two arguments in alphabetical order too. A regression that emitted the issues in one of those orders would have passed it. reader() now reads six of the globals in another order and passes the call's two arguments in reverse alphabetical order, and first(), defined before reader(), uses all eight in alphabetical order, which keeps the module's order apart from the reads'. On one Linux machine, the check failed in each of 16 runs without the order fix, with the eight diagnostics of reader() present every time, and passed in each of 16 with it; the module the analyzer compiled kept the globals in alphabetical order. --- run_test.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/run_test.py b/run_test.py index 1d77e9a1..fd3ae5b9 100755 --- a/run_test.py +++ b/run_test.py @@ -3087,16 +3087,22 @@ def check_global_read_before_write_order() -> bool: """ A function's GlobalReadBeforeWrite diagnostics come out in the order of its reads, two globals read by one call in the order of its arguments, at any --jobs. Their order used to follow the - globals' addresses, which change from one run to the next. + globals' addresses, which change from one run to the next. The reads follow neither the + alphabetical order of the names, nor their order of declaration, nor their order in the + module, which first() sets by using them before reader() does. """ print("=== Testing GlobalReadBeforeWrite order within a function ===") - names = [f"g_{n}" for n in ("alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel")] + declared = [f"g_{n}" for n in ("alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel")] + reads = ["g_delta", "g_alpha", "g_foxtrot", "g_charlie", "g_echo", "g_bravo"] + call = ["g_hotel", "g_golf"] + names = reads + call sources = { - "reads.c": "".join(f"int {n}[4];\n" for n in names) + "reads.c": "".join(f"int {n}[4];\n" for n in declared) + "void use2(const int *a, const int *b);\n" + + "int first(void)\n{\n return " + " + ".join(f"{n}[0]" for n in declared) + ";\n}\n" + "int reader(int i)\n{\n int s = 0;\n" - + "".join(f" s += {n}[i & 3];\n" for n in names[:6]) - + f" use2({names[6]}, {names[7]});\n return s;\n}}\n", + + "".join(f" s += {n}[i & 3];\n" for n in reads) + + f" use2({call[0]}, {call[1]});\n return s;\n}}\n", "other.c": "int other(int i) { return i; }\n", } ok = True