diff --git a/run_test.py b/run_test.py index a841933..fd3ae5b 100755 --- a/run_test.py +++ b/run_test.py @@ -3083,6 +3083,55 @@ 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. 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 ===") + 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 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 reads) + + f" use2({call[0]}, {call[1]});\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 @@ -5951,6 +6000,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, diff --git a/src/analysis/GlobalReadBeforeWriteAnalysis.cpp b/src/analysis/GlobalReadBeforeWriteAnalysis.cpp index 6c04f5f..01ca442 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);