diff --git a/run_test.py b/run_test.py index fd3ae5b..d67c7bf 100755 --- a/run_test.py +++ b/run_test.py @@ -2041,6 +2041,72 @@ def check_uninitialized_optional_receiver_index_repro() -> bool: return True +def check_uninitialized_large_function_memory() -> bool: + """ + Regression for #218: the uninitialized-read analysis gave every block a state holding one + range vector per tracked local, so its memory grew with blocks times locals and killed the + run on large functions. In a function of 2000 locals, each written in one case of a first + switch and read in one case of a second, every read must still be reported, under a memory + bound those states exceeded (about 680 MiB on macOS for this function, under 70 MiB without + them). + """ + print("=== Testing uninitialized-read memory on a large function (#218) ===") + count = 2000 + bound_mib = 384 + source = ["void use(int);", "void f(int c, int d) {"] + source += [f" int v{i};" for i in range(count)] + source += [" switch (c) {"] + [f" case {i}: v{i} = {i}; break;" for i in range(count)] + source += [" }", " switch (d) {"] + [f" case {i}: use(v{i}); break;" for i in range(count)] + source += [" }", "}"] + + with tempfile.TemporaryDirectory(prefix="ct_uninit_memory_") as tmp: + sample = Path(tmp) / "large_function.c" + sample.write_text("\n".join(source) + "\n") + report_path = Path(tmp) / "report.json" + errors_path = Path(tmp) / "stderr.txt" + cmd = [str(RUN_CONFIG.analyzer)] + _effective_analyzer_args([str(sample), "--format=json"]) + # os.wait4 gives the peak memory of this analyzer alone: getrusage(RUSAGE_CHILDREN) would + # mix in the analyzers that the other checks run in parallel. + with report_path.open("w") as report, errors_path.open("w") as errors: + proc = subprocess.Popen(cmd, stdout=report, stderr=errors) + timer = None + if RUN_CONFIG.analyzer_timeout: + timer = threading.Timer(RUN_CONFIG.analyzer_timeout, proc.kill) + timer.start() + _, status, usage = os.wait4(proc.pid, 0) + if timer: + timer.cancel() + proc.returncode = os.waitstatus_to_exitcode(status) + output = report_path.read_text() + errors_text = errors_path.read_text() + + if proc.returncode != 0: + return fail_check(f"analyzer failed (code {proc.returncode})", errors_text[-2000:]) + try: + diagnostics = json.loads(output)["diagnostics"] + except (json.JSONDecodeError, KeyError): + return fail_check("analyzer output is not a JSON report", output[-2000:]) + + names = [] + for diag in diagnostics: + if diag.get("ruleId") != "UninitializedLocalRead": + continue + match = re.search(r"variable '([^']+)'", diag.get("details", {}).get("message", "")) + names.append(match.group(1) if match else "?") + if sorted(names) != sorted(f"v{i}" for i in range(count)): + return fail_check( + f"expected one UninitializedLocalRead per local v0..v{count - 1}, got {len(names)}" + ) + + peak_mib = usage.ru_maxrss / ((1 << 20) if sys.platform == "darwin" else (1 << 10)) + if peak_mib > bound_mib: + return fail_check(f"analyzer peak memory {peak_mib:.0f} MiB exceeds {bound_mib} MiB") + + print(f" ✅ {count} uninitialized reads reported, peak memory {peak_mib:.0f} MiB " + f"(bound {bound_mib} MiB)\n") + return True + + def check_unknown_alloca_virtual_callback_escape() -> bool: """ Regression: unknown-origin unnamed allocas must not be silently treated as @@ -5985,6 +6051,7 @@ def record_ok(ok: bool): check_uninitialized_verbose_ctor_trace, check_uninitialized_unsummarized_defined_bool_out_param, check_uninitialized_optional_receiver_index_repro, + check_uninitialized_large_function_memory, check_unknown_alloca_virtual_callback_escape, check_compdb_as_default_input_source, check_exclude_dir_filter, diff --git a/src/analysis/UninitializedVarAnalysis.cpp b/src/analysis/UninitializedVarAnalysis.cpp index 42738a4..4f25f99 100644 --- a/src/analysis/UninitializedVarAnalysis.cpp +++ b/src/analysis/UninitializedVarAnalysis.cpp @@ -86,6 +86,8 @@ namespace ctrace::stack::analysis std::vector objects; llvm::DenseMap allocaIndex; llvm::DenseMap paramIndex; + // The range set of each object once fully initialized, shared by every state. + std::vector fullRanges; }; struct MemoryAccess @@ -96,8 +98,6 @@ namespace ctrace::stack::analysis unsigned reserved = 0; }; - using InitRangeState = std::vector; - struct PointerSlotWriteEffect { std::uint64_t slotOffset = 0; @@ -1020,6 +1020,7 @@ namespace ctrace::stack::analysis tracked.objects.clear(); tracked.allocaIndex.clear(); tracked.paramIndex.clear(); + tracked.fullRanges.clear(); for (const llvm::BasicBlock& BB : F) { @@ -1062,31 +1063,166 @@ namespace ctrace::stack::analysis .hasNonPaddingLayout = false}); tracked.paramIndex[&arg] = idx; } + + tracked.fullRanges.reserve(tracked.objects.size()); + for (const TrackedMemoryObject& obj : tracked.objects) + { + addRange(tracked.fullRanges.emplace_back(), 0, getObjectFullRangeEnd(obj)); + } + } + + // The initialized byte ranges of every tracked object at one program point. Most range + // sets are empty or cover their whole object: those take one bit each, and only the + // others are stored. A state holding one vector per object made the memory of a function + // grow with its blocks times its objects (#218). + class InitRangeState + { + public: + InitRangeState() = default; + + // Every object uninitialized, or every object initialized over its full range. + InitRangeState(const std::vector& fullRanges, bool fullyInitialized) + : fullRanges_(&fullRanges), full_(static_cast(fullRanges.size())) + { + if (!fullyInitialized) + return; + for (unsigned idx = 0; idx < full_.size(); ++idx) + { + if (!fullRanges[idx].empty()) + full_.set(idx); + } + } + + std::size_t size() const + { + return full_.size(); + } + + const RangeSet& operator[](std::size_t idx) const + { + static const RangeSet empty; + if (full_.test(static_cast(idx))) + return (*fullRanges_)[idx]; + const std::size_t pos = partialPosition(idx); + if (pos < partial_.size() && partial_[pos].first == idx) + return partial_[pos].second; + return empty; + } + + // Replaces the range set of one object. The encoding stays canonical (a full or + // empty set is never stored), so that equal states compare equal. + void set(std::size_t idx, RangeSet ranges) + { + const bool isFull = !ranges.empty() && ranges == (*fullRanges_)[idx]; + if (isFull) + full_.set(static_cast(idx)); + else + full_.reset(static_cast(idx)); + + const std::size_t pos = partialPosition(idx); + const bool stored = pos < partial_.size() && partial_[pos].first == idx; + if (isFull || ranges.empty()) + { + if (stored) + partial_.erase(partial_.begin() + pos); + return; + } + if (stored) + partial_[pos].second = std::move(ranges); + else + partial_.insert(partial_.begin() + pos, + PartialEntry{static_cast(idx), std::move(ranges)}); + } + + // Intersects each object's range set with the incoming one. Only objects stored on + // either side need a range intersection; the others are full on both sides or not. + void meet(const InitRangeState& incoming) + { + std::vector partial; + llvm::SmallVector becameFull; + auto lhs = partial_.begin(); + auto rhs = incoming.partial_.begin(); + while (lhs != partial_.end() || rhs != incoming.partial_.end()) + { + unsigned idx = 0; + if (rhs == incoming.partial_.end() || + (lhs != partial_.end() && lhs->first < rhs->first)) + { + idx = (lhs++)->first; + } + else if (lhs == partial_.end() || rhs->first < lhs->first) + { + idx = (rhs++)->first; + } + else + { + idx = lhs->first; + ++lhs; + ++rhs; + } + + RangeSet ranges = intersectRanges((*this)[idx], incoming[idx]); + if (ranges.empty()) + continue; + if (ranges == (*fullRanges_)[idx]) + becameFull.push_back(idx); + else + partial.emplace_back(idx, std::move(ranges)); + } + + full_ &= incoming.full_; + for (unsigned idx : becameFull) + full_.set(idx); + partial_ = std::move(partial); + } + + bool operator==(const InitRangeState& other) const + { + return full_ == other.full_ && partial_ == other.partial_; + } + + private: + using PartialEntry = std::pair; + + std::size_t partialPosition(std::size_t idx) const + { + auto it = std::lower_bound(partial_.begin(), partial_.end(), idx, + [](const PartialEntry& entry, std::size_t value) + { return entry.first < value; }); + return static_cast(it - partial_.begin()); + } + + const std::vector* fullRanges_ = nullptr; + llvm::BitVector full_; // objects initialized over their full range + std::vector partial_; // other non-empty range sets, by object index + }; + + static void addRange(InitRangeState& state, std::size_t objectIdx, std::uint64_t begin, + std::uint64_t end) + { + const RangeSet& current = state[objectIdx]; + if (isRangeCovered(current, begin, end)) + return; + RangeSet updated = current; + addRange(updated, begin, end); + state.set(objectIdx, std::move(updated)); } - static InitRangeState makeBottomState(std::size_t trackedCount) + static InitRangeState makeBottomState(const TrackedObjectContext& tracked) { - return InitRangeState(trackedCount); + return InitRangeState(tracked.fullRanges, false); } static InitRangeState makeTopState(const TrackedObjectContext& tracked) { - InitRangeState top(tracked.objects.size()); - for (std::size_t i = 0; i < tracked.objects.size(); ++i) - { - addRange(top[i], 0, getObjectFullRangeEnd(tracked.objects[i])); - } - return top; + return InitRangeState(tracked.fullRanges, true); } static void meetMustState(InitRangeState& accum, const InitRangeState& incoming) { if (accum.size() != incoming.size()) return; - for (std::size_t idx = 0; idx < accum.size(); ++idx) - { - accum[idx] = intersectRanges(accum[idx], incoming[idx]); - } + accum.meet(incoming); } static bool statesEqual(const InitRangeState& lhs, const InitRangeState& rhs) @@ -1131,7 +1267,7 @@ namespace ctrace::stack::analysis const llvm::DenseMap& outState, const TrackedObjectContext& tracked) { - InitRangeState in = makeBottomState(tracked.objects.size()); + InitRangeState in = makeBottomState(tracked); if (&BB == entryBlock) return in; @@ -1147,7 +1283,7 @@ namespace ctrace::stack::analysis auto itOut = outState.find(pred); if (itOut == outState.end()) { - merged = makeBottomState(tracked.objects.size()); + merged = makeBottomState(tracked); break; } meetMustState(merged, itOut->second); @@ -1729,7 +1865,7 @@ namespace ctrace::stack::analysis std::uint64_t clippedEnd = 0; if (clipRangeToObject(obj, mappedBegin, mappedEnd, clippedBegin, clippedEnd)) { - addRange(initialized[objectIdx], clippedBegin, clippedEnd); + addRange(initialized, objectIdx, clippedBegin, clippedEnd); } } @@ -2768,7 +2904,7 @@ namespace ctrace::stack::analysis { continue; } - addRange(initialized[objectIdx], clippedBegin, clippedEnd); + addRange(initialized, objectIdx, clippedBegin, clippedEnd); wroteSomething = true; if (currentSummary && isParamObject(obj) && obj.param) @@ -2912,7 +3048,7 @@ namespace ctrace::stack::analysis access)) { const TrackedMemoryObject& obj = tracked.objects[access.objectIdx]; - addRange(initialized[access.objectIdx], access.begin, access.end); + addRange(initialized, access.objectIdx, access.begin, access.end); if (isAllocaObject(obj)) { if (writeSeen && access.objectIdx < writeSeen->size()) @@ -3092,7 +3228,7 @@ namespace ctrace::stack::analysis return; } - addRange(initialized[access.objectIdx], access.begin, access.end); + addRange(initialized, access.objectIdx, access.begin, access.end); if (isAllocaObject(obj)) { if (writeSeen && access.objectIdx < writeSeen->size()) @@ -3235,8 +3371,8 @@ namespace ctrace::stack::analysis if (!reachable.lookup(&BB)) continue; const bool isEntry = (&BB == &F.getEntryBlock()); - inState[&BB] = isEntry ? makeBottomState(trackedCount) : makeTopState(tracked); - outState[&BB] = isEntry ? makeBottomState(trackedCount) : makeTopState(tracked); + inState[&BB] = isEntry ? makeBottomState(tracked) : makeTopState(tracked); + outState[&BB] = isEntry ? makeBottomState(tracked) : makeTopState(tracked); } const unsigned reachableBlocks = static_cast(outState.size());