Repository navigation
fix(analysis): take the bytes a counted loop writes as initialized after it - #287
Draft
SizzleUnrlsd wants to merge 2 commits into
Draft
SizzleUnrlsd wants to merge 2 commits into
SizzleUnrlsd wants to merge 2 commits into
Conversation
…ter it An array written element by element by a counted loop was reported as read before initialization after the loop (#224). Each iteration stores at an offset that is not constant in the analyzed IR, so the dataflow adds no write. PromotedValues gets a loopWrites query: on the promoted copy, where the loop counter lives in a register, the bytes a store writes over the whole loop around it. It answers only when the loop leaves from one block after a constant number of iterations, the store runs on each of them, and its address moves from a constant offset by a constant step no larger than the bytes written, so that they are contiguous. The uninitialized-read analysis adds those bytes on the edges that leave the loop, nowhere inside it. A loop that stops before the element read, starts after it, skips elements, writes under a condition, may leave early or has a bound that is not constant keeps its report.
…at leave gaps A new fixture covers #224. A read after a counted loop that writes every element of the array is not reported: increasing, decreasing and do-while loops, and an array passed to a function after the loop. A read of an element the loop may leave unwritten stays reported: a loop that stops before it, starts after it, steps over it, writes under a condition, may break out early or has a bound that is not constant. The strict count admits no other report. In the stb-like fixture, the loop at line 20 writes all 17 elements of 'sizes' before any read of it: its three expected reports were false positives. A negative expectation replaces them. The two reports on 'next_code' stay.
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.
Fixes #224.
Problem
An array that a counted loop writes in full was reported as read before initialization after the
loop:
In the analyzed IR, the loop counter lives in a stack slot, so each iteration stores at an offset
that is not constant. The must-init dataflow records only stores at a constant offset from a
tracked object. It added no write for the loop, so the read after it was reported.
Change
PromotedValues::loopWrites(store, base)is a new query. On the promoted copy, where the loopcounter lives in a register and ScalarEvolution sees it, it gives the bytes a store writes from
baseover the whole loop around it. It answers only when all of these hold:baseand moves by a constant step no largerthan the bytes stored, so the bytes written are contiguous.
UninitializedLocalReadadds those bytes on the edges that leave the loop, never inside it.has a candidate store, that is a store to a tracked object at a non-constant offset.
PromotedValuesis the copy the stack-buffer analysis already uses. Each module has its ownLLVMContext, and summaries are built in parallel across modules. So the copy adds no stateshared between threads.
There is no name heuristic and no size cutoff.
The second commit adds a fixture. It also updates
stb-like-next-code-uninitialized.c, whoseexpectations recorded three of these false positives.
Evidence
Everything below ran on one macOS arm64 machine (Apple clang 17, LLVM 20.1.2), with a Release
build in the CI's configuration. "Before" is
588e800, the base of this branch.Inputs written for the issue
22 small inputs were each analyzed with both binaries:
UninitializedLocalReadeachdo … while, decreasing,while,<=andsizeofloops.break, acontinue;Every other diagnostic of these inputs is identical before and after.
Fixtures
Every fixture under
test/(523.c/.cc/.cpp/.llfiles) was analyzed one at a time withboth binaries. Exit codes are identical on every file, and diagnostics are identical on 521 of
them. Three sources under
test/unit/produce no report with either binary, because they do notcompile on their own.
The other two files differ only by five
UninitializedLocalReadthat are gone, all of them falsepositives. The total goes from 49 to 44.
test/false-positive-repro/stb-like-next-code-uninitialized.c: its three reports onsizesare gone. The loop at line 20 zeroessizesin full (17 iterations, 17 elements). Itstwo reports on
next_codestay, because that loop writes onlynext_code[1..15]and the indexread is unknown.
test/unit/int_range_point_input.c: two reports are gone.a[17]is filled by a17-iteration loop, and
b[3]is written by the loop overi = 1..15. This file is the inputof a unit test on integer ranges, which passes.
Both
stack_usage_analyzer_unit_testsandownership_engine_unit_testspass.The new tests
test/uninitialized-variable/uninitialized-local-counted-loop-array.c:cover the issue's loop, a decreasing loop, a
do … while, and the array passed to a functionafter the loop.
a loop that stops before it, one that starts after it, a step of two, a conditional write, a
break, and a bound that is not constant.stb-like-next-code-uninitialized.c: a negative expectation forsizesreplaces the threeexpectations on it. The two expectations on
next_codeare unchanged.Both fixtures ran through
run_test.py's owncheck_file, with each binary. Each ran without andwith the CI's
--compile-ir-cache-dir, and the result was the same in both modes:stb-like, updatedstb-like, as onmainBefore the fix, the failures are exactly the false positives: the reads that must not be reported
are reported, and the strict count differs in each pass. Both binaries report the six true reads.
Full suite
The full suite ran with the CI's configuration:
run_test.py --jobs=8 --analyzer-timeout=120, where--jobsismin(8, CPUs)as in the CI;CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir;All 2934 tests passed (2934/2934) in 49 s.
The same run on the fix commit alone gave 2908/2916. All eight failures came from
stb-like-next-code-uninitialized.c: its three expectations onsizesand its strict count, inboth passes.
Limits
reported, as before:
is larger than what it writes;
outer loop runs at least once;
*p++ = …);the function's summary does not include loop writes.
break,return) or a bound that is not constantkeeps its report, since it may leave elements unwritten.
PromotedValuesqueries.only, not on external code bases.
GCC 13.3, AppleClang 16 and AppleClang 21 were covered by the CI only (see the CI results
below).
so the figure carries unquantified noise.
Interaction with #286
#286 changes
InitRangeStatein the same file. Git merges the two branches without a conflict,but the merged file does not compile. In #286,
InitRangeState::operator[]returns a read-onlyrange set, and this PR writes through it in
computeInState. In the merged code, that call mustbecome:
addRange(leaving, access.objectIdx, access.begin, access.end);With that one-line change, the merged file passes a syntax-only compile. The merged branches have
not been built or tested together. Whichever PR is merged second needs the adaptation, and a full
CI run on the combination before it is merged.
CI results
The CI ran on
1914585(Build run).All 14 checks pass. "Attach binaries to GitHub Release" is skipped, because it only runs for
tags.
run_test.pypasses 2934/2934 on each build job. On each of them, the new fixture passes its 22checks and
stb-like-next-code-uninitialized.cits 8:run_test.pyjobsrun_test.py--fail-on error): passes, and the SARIF upload succeeds.mainhave the same 65 open alerts.One of them is in
PromotedValues.cpp(Recursion.Detected, note) and was already openbefore this PR.
format-check,license-complianceandCheck conventional commits;Concurrency analysis;Test Consumer Project,Test Docker ImageandTest GitHub Action.