Skip to content

fix(analysis): take the bytes a counted loop writes as initialized after it - #287

Draft
SizzleUnrlsd wants to merge 2 commits into
mainfrom
224-uninitialized-loop-written-array
Draft

SizzleUnrlsd wants to merge 2 commits into
mainfrom
224-uninitialized-loop-written-array

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #224.

Problem

An array that a counted loop writes in full was reported as read before initialization after the
loop:

int squares[4];
for (int i = 0; i < 4; i++)
    squares[i] = i * i;
return squares[3]; // UninitializedLocalRead

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 loop
counter lives in a register and ScalarEvolution sees it, it gives the bytes a store writes from
base over the whole loop around it. It answers only when all of these hold:

  • the loop leaves from a single block and has a single latch;
  • its backedge-taken count is a constant;
  • the store runs on every iteration: its block dominates the latch;
  • the address starts at a constant offset from base and moves by a constant step no larger
    than the bytes stored, so the bytes written are contiguous.

UninitializedLocalRead adds those bytes on the edges that leave the loop, never inside it.

  • The writes are collected once per function. The promoted copy is built only when a function
    has a candidate store, that is a store to a tracked object at a non-constant offset.
  • PromotedValues is the copy the stack-buffer analysis already uses. Each module has its own
    LLVMContext, and summaries are built in parallel across modules. So the copy adds no state
    shared 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, whose
expectations 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:

Group Inputs Before After
The array is written in full before the read 8 one UninitializedLocalRead each none
The element read is never written, or not on every path 9 one each the same one, at the same line
Forms this PR does not handle (see Limits) 5 one each the same one, at the same line
  • The first group covers:
    • the issue's example;
    • the array passed to a function that reads it;
    • a function that fills its own parameter, then reads it;
    • do … while, decreasing, while, <= and sizeof loops.
  • The second group covers:
    • a loop that stops before the element read, or starts after it;
    • a step of two;
    • a conditional write, a break, a continue;
    • a bound that is not constant;
    • a read before the write inside the loop;
    • a function that reads its parameter before filling it.

Every other diagnostic of these inputs is identical before and after.

Fixtures

Every fixture under test/ (523 .c/.cc/.cpp/.ll files) was analyzed one at a time with
both 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 not
compile on their own.

The other two files differ only by five UninitializedLocalRead that are gone, all of them false
positives. The total goes from 49 to 44.

  • test/false-positive-repro/stb-like-next-code-uninitialized.c: its three reports on
    sizes are gone. The loop at line 20 zeroes sizes in full (17 iterations, 17 elements). Its
    two reports on next_code stay, because that loop writes only next_code[1..15] and the index
    read is unknown.
  • test/unit/int_range_point_input.c: two reports are gone. a[17] is filled by a
    17-iteration loop, and b[3] is written by the loop over i = 1..15. This file is the input
    of a unit test on integer ranges, which passes.

Both stack_usage_analyzer_unit_tests and ownership_engine_unit_tests pass.

The new tests

  • New fixture test/uninitialized-variable/uninitialized-local-counted-loop-array.c:
    • Four reads come after a loop that writes the whole array, and must not be reported. They
      cover the issue's loop, a decreasing loop, a do … while, and the array passed to a function
      after the loop.
    • Six reads are of an element the loop may leave unwritten, and must stay reported. They cover
      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.
    • The strict diagnostic count, on by default, admits no other report.
  • stb-like-next-code-uninitialized.c: a negative expectation for sizes replaces the three
    expectations on it. The two expectations on next_code are unchanged.

Both fixtures ran through run_test.py's own check_file, with each binary. Each ran without and
with the CI's --compile-ir-cache-dir, and the result was the same in both modes:

Fixture Before the fix After the fix
New fixture fails (12/22) passes (22/22)
stb-like, updated fails (4/8) passes (8/8)
stb-like, as on main passes (12/12) fails (4/12)

Before 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 --jobs is min(8, CPUs) as in the CI;
  • CORETRACE_RUN_TEST_EXTRA_ANALYZER_ARGS=--compile-ir-cache-dir=.cache/compile-ir;
  • empty caches at the start.

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 on sizes and its strict count, in
both passes.

Limits

  • Forms not handled: in these forms, a read after a loop that writes the whole array is still
    reported, as before:
    • a 2D array filled by two nested loops;
    • an array of structures with several fields written per iteration, because each store's step
      is larger than what it writes;
    • an outer loop that repeats a complete inner loop, because the dataflow does not know that the
      outer loop runs at least once;
    • a loop that advances a pointer (*p++ = …);
    • a function that fills its parameter in a loop, when the caller reads after the call, because
      the function's summary does not include loop writes.
  • By design: a loop with several exits (break, return) or a bound that is not constant
    keeps its report, since it may leave elements unwritten.
  • Lifetime markers: functions with lifetime markers are left out, as for the other
    PromotedValues queries.
  • Coverage: diagnostics were compared on the repository's fixtures and the 22 inputs above
    only, not on external code bases.
  • Platforms and compilers: everything above ran on macOS arm64 with Apple clang 17. Linux,
    GCC 13.3, AppleClang 16 and AppleClang 21 were covered by the CI only (see the CI results
    below).
  • Timing: analyzing the 523 fixtures took 32 s with each binary. That is a single run each,
    so the figure carries unquantified noise.

Interaction with #286

#286 changes InitRangeState in 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-only
range set, and this PR writes through it in computeInState. In the merged code, that call must
become:

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.py passes 2934/2934 on each build job. On each of them, the new fixture passes its 22
checks and stb-like-next-code-uninitialized.c its 8:

Job Compiler run_test.py jobs run_test.py
ubuntu-24.04 GCC 13.3.0 4 2934/2934
macos-latest AppleClang 21.0.0 3 2934/2934
macos-15 AppleClang 16.0.0 3 2934/2934
  • Unit tests: both unit test binaries pass on each build job.
  • Self-analysis (ubuntu-24.04, --fail-on error): passes, and the SARIF upload succeeds.
  • Code scanning: no new alert. The PR's merge ref and main have the same 65 open alerts.
    One of them is in PromotedValues.cpp (Recursion.Detected, note) and was already open
    before this PR.
  • Other workflows: these all pass:
    • format-check, license-compliance and Check conventional commits;
    • Concurrency analysis;
    • both Linux release binaries (amd64 and arm64);
    • Test Consumer Project, Test Docker Image and Test GitHub Action.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working FP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: UninitializedLocalRead on an array a counted loop writes in full

1 participant