Skip to content

fix(analysis): emit global read-before-write issues in instruction order - #284

Merged
SizzleUnrlsd merged 4 commits into
mainfrom
fix/global-read-before-write-order
Nov 16, 2026
Merged

SizzleUnrlsd merged 4 commits into
mainfrom
fix/global-read-before-write-order

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

GlobalReadBeforeWrite emitted a function's issues by walking selectedReads, an llvm::DenseMap keyed by the global's address (const GlobalVariable*). In a function with two or more of these issues, their order, and so their ids, followed the globals' addresses, which change from one run to the next and with --jobs. The issues themselves did not change.

This pull request emits them in the order of the reads, and adds a check of that order to run_test.py.

Change

  • fix(analysis): emit global read-before-write issues in instruction order (src/analysis/GlobalReadBeforeWriteAnalysis.cpp, +7 −5): the loop walks reads and emits, for each global, the read that the analysis selected (selectedReads.lookup(global) == &readEvent).
    • reads is filled in the loop that numbers the instructions (instructionOrder), in that order. Two globals read by one call come in the order of its arguments; a sort of the selected reads on instructionOrder alone would leave those two tied, in address order again.
    • The selection (selectedReads, selectedWrites, selectedKinds) is not touched, and each selected read is in reads once: the same issues come out, once each. Only their order within a function changes. A function with one issue comes out as before.
  • test(analysis): check the order of global read-before-write issues in a function (run_test.py, +44): check_global_read_before_write_order analyzes two files, at --jobs=1 and at --jobs=4. In reads.c, reader reads eight zero-initialized global arrays, six one statement after the other and two as the arguments of one call. The check expects the eight diagnostics of reader in the order of its reads.
  • test(analysis): tell the order of the reads from alphabetical and module orders (run_test.py, +11 −5): in the first version, the reads came in the alphabetical order of the names, which was also their order of declaration and their order in the module (Clang creates a global where it is first used), and the call's two arguments came in alphabetical order. A regression to one of those orders would have passed. Now:
    • the eight arrays are still declared in alphabetical order, and first(), defined before reader(), uses them in that order, which keeps the module's order alphabetical;
    • reader() reads six of them in another order, g_delta, g_alpha, g_foxtrot, g_charlie, g_echo, g_bravo, then calls use2(g_hotel, g_golf);
    • the order the check expects is then none of the alphabetical order, the order of declaration and the module's order, and the call's arguments come in reverse alphabetical order.

The fix and the first test applied to main as they were, the test with other neighbours in main()'s list of checks. The branch was then updated with main at 9393ef3 by a merge commit, before the stronger test.

Where outputs change

Only in functions with two or more GlobalReadBeforeWrite issues. In the corpora that #239 checks, only two functions have more than one: match_patterns (pcre2grep.c) and my_free (pcre2test.c), in PCRE2. zlib has 19 of these issues, at most one per function; tinyxml2 has none.

Evidence

Collected on Linux x86_64 (LLVM 20, glibc), in a container of 4 CPUs and 5 GiB, with Release builds of the change on #239's head (57aa98a). GlobalReadBeforeWriteAnalysis.cpp is the same there as on main at 1fadba2. Four binaries, built from one tree in one run: 57aa98a; 57aa98a with #285's first commit (21f7483 there), which loads the shared modules one at a time; 57aa98a with this fix; 57aa98a with both.

The first version of the check, without and with the fix

check_global_read_before_write_order ran as run_test.py runs it: the function itself, with RUN_CONFIG set as for CI's --analyzer-timeout=120 and run_test.py's cache of outputs off. Each binary ran it 8 times without extra analyzer arguments, and 8 times with CI's CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir, from an empty cache each time:

Binary Check passed
57aa98a 0 of 16
57aa98a, shared modules loaded one at a time 0 of 16
57aa98a with this fix 16 of 16
57aa98a with both 16 of 16
  • The eight diagnostics are there without and with the fix: each of the 128 outputs holds the eight names, once each.
  • Without the fix, the order fails: no output is in the order of the reads.
    • 57aa98a: at --jobs=1, 5 orders in 8 runs without extra arguments, and 6 with CI's; at --jobs=4, one order per variant, the same in its 8 runs.
    • With the shared modules loaded one at a time: 5 or 6 orders in 8 runs, at either --jobs.
  • With the fix, the 64 outputs are in the order of the reads: g_alpha to g_foxtrot, then g_golf and g_hotel, in the order of the call's arguments.
  • The findings do not change: the 128 outputs hold one and the same set of 14 diagnostics, every rule, compared without their id (numbered in the order of emission) and with the temporary directory normalized.

The stronger check, without and with the fix

The same procedure, on the same machine and with the same four binaries, on this branch's run_test.py with the stronger test (ee25641, byte for byte the file that ran):

Binary Check passed
57aa98a 0 of 16
57aa98a, shared modules loaded one at a time 0 of 16
57aa98a with this fix 16 of 16
57aa98a with both 16 of 16
  • The eight diagnostics of reader() are there without and with the fix: each of the 128 outputs holds the eight names, once each.
  • Without the fix, the order fails: no output is in the order of the reads. 57aa98a gives 4 orders in 8 runs at --jobs=1, without and with CI's argument, and one order at --jobs=4; with the shared modules loaded one at a time, 4 to 6 orders in 8 runs.
  • With the fix, the 64 outputs are in the order of the reads, g_delta g_alpha g_foxtrot g_charlie g_echo g_bravo g_hotel g_golf: g_hotel before g_golf, as the call passes them.
  • The findings do not change: the 128 outputs hold one and the same set of 22 diagnostics, every rule, first()'s included, and reader()'s eight are one and the same set with every binary.
  • The module's order differs from the expected one: with --dump-ir, the module the analyzer compiles from the check's reads.c holds the globals from g_alpha to g_hotel, at --jobs=1 and at --jobs=4, with 57aa98a and with this fix.

Order experiment

With the first version of the check's files. Two pairs of files (the check's, and an earlier pair whose function reads six arrays in one expression), the four binaries, --jobs 1, 4 and the automatic setting, 8 runs each, with address randomization on:

  • with the fix, 96 of 96 outputs in the order of the reads;
  • without it, 0 of 96, with 1 to 8 different orders per case;
  • the findings are the same from one binary to another.

Suites and controls

CI and code scanning

  • CI on ee25641, with the stronger test (merge ref 99c3bb4, into main at 9393ef3): every check passes; the release job is skipped, as outside tags.
  • The suite passes 2916/2916 on ubuntu-24.04, macos-15 and macos-latest, where main at 9393ef3 passes 2915/2915. The one more is the new check, which passes at --jobs=1 and at --jobs=4 on the three runners.
  • What this CI does not show: the check failing without the fix on these runners. It runs with the fix only (see Reservations).
  • Code scanning on 99c3bb4: coretrace-stack-analyzer reported 73 results and coretrace-concurrency-analyzer none, as on main at 9393ef3. New alerts: none. The 66 alerts open on refs/pull/284/merge are the 66 open on main, alert for alert.
  • Earlier:
    • on d905ff9, the branch updated with main at 9393ef3 (merge ref a8e2fcb): every check passed, and the suite passed 2916/2916 on the three runners;
    • on 41b91f2 (merge ref 26c25c2, into main at 1fadba2): every check passed; the suite passed 2761/2761 on the three runners, against 2760/2760 for main then; code scanning matched main's.

Reservations

  • The failure without the fix depends on addresses. It was observed on that one machine (Linux, glibc). Where the address order happens to match the order of the reads, the check passes without the fix; macOS was not measured.
  • PCRE2 was not run with this change. It is the only corpus checked where the order can change. That its issues stay the same rests on the selection being untouched, and on the experiments above.
  • Measured on feat(const-param): experimental Clang check of const suggestions, version 2 #239's head, not on main. On main, this pull request's CI is the check.
  • The compile IR cache: the analyzer's default log does not say whether the second run of the CI variant read the cache; this is not claimed.

Relation to #285

The plan is to merge this pull request first, then #285. This change is independent of #285, and merges with it without conflict. That change loads the shared modules on the calling thread, in the main arena, where this order already varied from run to run at --jobs=1: with it alone, the order also varies at --jobs above 1, as seen on PCRE2's two functions. This change removes that dependence on addresses.

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.
… 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.
…ule 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.
@SizzleUnrlsd
SizzleUnrlsd marked this pull request as ready for review November 16, 2026 04:18
@SizzleUnrlsd
SizzleUnrlsd merged commit b7afcfb into main Nov 16, 2026
15 checks passed
@SizzleUnrlsd
SizzleUnrlsd deleted the fix/global-read-before-write-order branch November 16, 2026 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant