Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions src/analysis/GlobalReadBeforeWriteAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<unnamed-global>");
issue.readInst = readEvent->inst;
issue.readInst = readEvent.inst;
issue.firstWriteInst = selectedWrites.lookup(global);
issue.kind = selectedKinds.lookup(global);
issue.hasNonLocalWrite = hasAnyKnownWriteByGlobal.lookup(global);
Expand Down
Loading