Repository navigation
fix(analysis): emit global read-before-write issues in instruction order - #284
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GlobalReadBeforeWriteemitted a function's issues by walkingselectedReads, anllvm::DenseMapkeyed 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 walksreadsand emits, for each global, the read that the analysis selected (selectedReads.lookup(global) == &readEvent).readsis 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 oninstructionOrderalone would leave those two tied, in address order again.selectedReads,selectedWrites,selectedKinds) is not touched, and each selected read is inreadsonce: 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_orderanalyzes two files, at--jobs=1and at--jobs=4. Inreads.c,readerreads 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 ofreaderin 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:first(), defined beforereader(), 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 callsuse2(g_hotel, g_golf);The fix and the first test applied to
mainas they were, the test with other neighbours inmain()'s list of checks. The branch was then updated withmainat9393ef3by a merge commit, before the stronger test.Where outputs change
Only in functions with two or more
GlobalReadBeforeWriteissues. In the corpora that #239 checks, only two functions have more than one:match_patterns(pcre2grep.c) andmy_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.cppis the same there as onmainat1fadba2. Four binaries, built from one tree in one run:57aa98a;57aa98awith #285's first commit (21f7483there), which loads the shared modules one at a time;57aa98awith this fix;57aa98awith both.The first version of the check, without and with the fix
check_global_read_before_write_orderran asrun_test.pyruns it: the function itself, withRUN_CONFIGset as for CI's--analyzer-timeout=120andrun_test.py's cache of outputs off. Each binary ran it 8 times without extra analyzer arguments, and 8 times with CI'sCORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir, from an empty cache each time:57aa98a57aa98a, shared modules loaded one at a time57aa98awith this fix57aa98awith both57aa98a: 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.--jobs.g_alphatog_foxtrot, theng_golfandg_hotel, in the order of the call's arguments.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.pywith the stronger test (ee25641, byte for byte the file that ran):57aa98a57aa98a, shared modules loaded one at a time57aa98awith this fix57aa98awith bothreader()are there without and with the fix: each of the 128 outputs holds the eight names, once each.57aa98agives 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.g_delta g_alpha g_foxtrot g_charlie g_echo g_bravo g_hotel g_golf:g_hotelbeforeg_golf, as the call passes them.first()'s included, andreader()'s eight are one and the same set with every binary.--dump-ir, the module the analyzer compiles from the check'sreads.cholds the globals fromg_alphatog_hotel, at--jobs=1and at--jobs=4, with57aa98aand 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,
--jobs1, 4 and the automatic setting, 8 runs each, with address randomization on:Suites and controls
run_test.py --jobs=4 --analyzer-timeout=120with the compile IR cache), for57aa98awith this change, then with both changes: unit and ownership tests pass, and the suite passes 2743/2743 (feat(const-param): experimental Clang check of const suggestions, version 2 #239's suite, its own checks included). The new check, in its first version, passes at--jobs=1and at--jobs=4in both.CI and code scanning
ee25641, with the stronger test (merge ref99c3bb4, intomainat9393ef3): every check passes; the release job is skipped, as outside tags.ubuntu-24.04,macos-15andmacos-latest, wheremainat9393ef3passes 2915/2915. The one more is the new check, which passes at--jobs=1and at--jobs=4on the three runners.99c3bb4: coretrace-stack-analyzer reported 73 results and coretrace-concurrency-analyzer none, as onmainat9393ef3. New alerts: none. The 66 alerts open onrefs/pull/284/mergeare the 66 open onmain, alert for alert.d905ff9, the branch updated withmainat9393ef3(merge refa8e2fcb): every check passed, and the suite passed 2916/2916 on the three runners;41b91f2(merge ref26c25c2, intomainat1fadba2): every check passed; the suite passed 2761/2761 on the three runners, against 2760/2760 formainthen; code scanning matchedmain's.Reservations
main. Onmain, this pull request's CI is the check.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--jobsabove 1, as seen on PCRE2's two functions. This change removes that dependence on addresses.