Skip to content

fix(analysis): keep uninitialized-read states compact on large functions - #286

Draft
SizzleUnrlsd wants to merge 2 commits into
mainfrom
218-uninitialized-state-memory
Draft

SizzleUnrlsd wants to merge 2 commits into
mainfrom
218-uninitialized-state-memory

Conversation

@SizzleUnrlsd

@SizzleUnrlsd SizzleUnrlsd commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #218.

Problem

UninitializedLocalRead gave every reachable block an in-state and an out-state. Each was a
std::vector<RangeSet> holding one heap-allocated range vector per tracked object, and a full
state was built at each block visit. Memory grew with blocks × tracked objects. On the issue's
generated function, it reached 6.8 GB at n = 4000, and the run was killed above 12 GB at n = 8000.

Change

InitRangeState becomes a class in src/analysis/UninitializedVarAnalysis.cpp:

  • One bit per object covers the two common range sets: empty, and the object's full range.
  • Only the other range sets are stored, sorted by object index.
  • state[idx] returns the same range set as before: the object's full range (shared by every
    state), the stored set, or an empty set.
  • The meet computes the exact intersection for objects stored on either side, and ANDs the bits
    for the others.
  • The encoding is canonical: a full or empty set is never stored. So two states compare equal
    exactly when their range sets do, and the fixpoint runs the same iterations.

There is no heuristic and no size cutoff, so the reported diagnostics are meant to stay the same.

The second commit adds check_uninitialized_large_function_memory to run_test.py.

Evidence

Everything below ran on one macOS arm64 machine (Apple clang 17, LLVM 20.1.2), with a Release
build. Each measurement is a single run.

Memory and time on the issue's generator

The generator writes if (c == i) v_i = i; then reads with if (c > i) s += v_i;.

n before: time, peak RSS after: time, peak RSS UninitializedLocalRead step
1000 3.15 s, 584 MiB 2.47 s, 63 MiB 822 → 39 ms
2000 10.84 s, 1142 MiB 7.53 s, 80 MiB 3490 → 163 ms
4000 not run (6.8 GB in the issue) 33.37 s, 103 MiB 587 ms
8000 not run (killed above 12 GB in the issue) 183.82 s, 191 MiB 1880 ms

At n = 4000 and n = 8000, one UninitializedLocalRead is reported per variable.

Diagnostics

The diagnostics are unchanged on every input compared:

  • The issue's generator, n = 1000 and n = 2000: the same diagnostics before and after,
    compared as multisets without their id.
  • Every fixture under test/: 523 .c/.cc/.cpp/.ll files, analyzed one at a time
    with both binaries. Exit codes and diagnostics are identical, with 714 diagnostics on each side,
    49 of them UninitializedLocalRead. Three sources under test/unit/ produce no report with
    either binary, because they do not compile on their own.
  • stack_usage_analyzer_unit_tests: passes.

The new check

check_uninitialized_large_function_memory analyzes a function of 2000 locals. Each local is
written in one case of a first switch and read in one case of a second. The check requires
two things:

  • one UninitializedLocalRead per local;
  • an analyzer peak memory of at most 384 MiB, measured with os.wait4 on that process alone,
    since other checks run in parallel.

It ran 8 times with each binary, both with and without the CI's --compile-ir-cache-dir:

  • Before the fix: fails 8/8 on memory (680–694 MiB), with every read still reported.
  • After the fix: passes 8/8 (67–69 MiB), and again in the full suite below (65 MiB).

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 2917 tests passed (2917/2917) in 51 s, the new check included (65 MiB).

Limits

  • Noise: each measurement is a single run on one machine, so the timings carry unquantified
    noise. The memory figures are peak RSS (ru_maxrss).
  • Linux: the new check passes on Linux at 108 MiB with the fix (see the CI results below).
    Its failing side, though, was shown on macOS only: the peak before the fix was not measured
    on Linux, where ru_maxrss counts in KiB and the allocator differs.
  • Other compilers: GCC 13.3, AppleClang 16 and AppleClang 21 were covered by the CI only.
    The local build used Apple clang 17.
  • Diagnostics coverage: diagnostics were compared on the repository's fixtures and the issue's
    generator only, not on external code bases.
  • The bound's reach: 384 MiB is about 5.5× the fixed peak and 0.55× the unfixed one on macOS.
    It catches a regression the size of UninitializedLocalRead memory grows with blocks times locals and kills the run on large functions #218, not a small growth.
  • Remaining time: on the issue's function, most of the remaining time is spent in other
    analyses, which this PR does not address. At n = 8000, the integer-overflow analysis took 147 s
    and the duplicate-if-condition analysis 34 s.

CI results

The CI ran on fdaca3c (Build run).
All 14 checks pass. "Attach binaries to GitHub Release" is skipped, because it only runs for
tags.

run_test.py passes 2917/2917 on each build job, and the new check reports all 2000 reads:

Job Compiler run_test.py jobs New check's peak
ubuntu-24.04 GCC 13.3.0 4 108 MiB
macos-latest AppleClang 21.0.0 3 63 MiB
macos-15 AppleClang 16.0.0 3 69 MiB
  • Self-analysis (ubuntu-24.04, --fail-on error): passes, and the SARIF upload succeeds.
  • Code scanning: no new alert. The four open alerts in UninitializedVarAnalysis.cpp
    (Recursion.Detected, Info) were already open on main.
  • 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.

Every block of a function kept an in-state and an out-state holding one
heap vector of byte ranges per tracked object, so the uninitialized-read
analysis needed memory in proportion to blocks times objects, and a large
function killed the whole run (#218).

A state now keeps one bit per object for the two common range sets, empty
and covering the whole object, and stores only the other ones, sorted by
object. The meet intersects only the objects stored on either side and
ANDs the bits. The encoding stays canonical, so states compare equal
exactly when their range sets do, and every range set read through a
state is the one the old state held: the diagnostics do not change.

On the issue's generated function (macOS, Release), n = 2000 peaks at
80 MiB instead of 1142 MiB, and n = 8000, killed above 12 GB in the
issue, peaks at 191 MiB with all 8000 reads reported.
… locals

Regression for #218. In a function of 2000 locals, each written in one case
of a first switch and read in one case of a second, every local must still
get its UninitializedLocalRead, and the analyzer's peak memory, measured by
os.wait4 on that process alone, must stay within 384 MiB.

On macOS the check fails before the fix (685 to 694 MiB, every read still
reported) and passes with it (67 to 68 MiB).
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.

UninitializedLocalRead memory grows with blocks times locals and kills the run on large functions

1 participant