Repository navigation
Conversation
…#31) AcnDyn's scratch buffers were function-local thread_local std::vectors. C++ destroys a thread's thread_local objects before any static object, and in reverse order of construction, so AcnDyn arithmetic in the destructor of a static object, or of a thread_local object created before the thread's first AcnDyn operation, wrote into freed memory (ASan: heap-use-after-free; with AcnDyn<BigInt>, a crash). Each slot's state (buffer, capacity, a "released" flag) is now a constant-initialized, trivially destructible thread_local, which is never destroyed. A thread_local Releaser, created with the thread's first buffer, frees the buffer when the thread exits and marks the slot released. After that, scratch_span() allocates a buffer per borrow, and the ScratchSpan it now returns (instead of a std::span; detail API only) frees it when it goes out of scope, so nothing leaks either. A temporary ScratchSpan does not convert to a span, so a borrow cannot be dropped while its span is still in use. The call sites already use auto and are unchanged. The normal path is one capacity compare with no initialization guard on the thread_local: instructions per AcnDyn add/mul/fma/scale are equal or lower than before, norm2 +1%. The new test scratch_exit does AcnDyn<CheckedI64> and AcnDyn<BigInt> arithmetic in a static object's destructor and, during a thread's exit, in the destructor of a thread_local created before that thread's first AcnDyn operation. AddressSanitizer reports a use after free in the sanitizer build. The other builds replace operator new/delete with a quarantine that fills freed blocks with a pattern; the test checks that the patterns are intact and that the exiting thread freed everything it allocated. It fails before the fix in every build, and also fails for a fallback that re-creates a per-thread buffer (a leak). Fixes #31. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LX2x8TzXgZCE8xUyBZSW92
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 17 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. |
Not up to standards ⛔
|
This was referenced Dec 2, 2026
This was referenced Dec 2, 2026
ondrik
force-pushed
the
review/fix-divide
branch
from
December 2, 2026 13:16
812555e to
5004711
Compare
ondrik
force-pushed
the
review/fix-scratch
branch
from
December 2, 2026 13:16
41e9811 to
411590a
Compare
ondrik
marked this pull request as ready for review
December 2, 2026 13:58
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.
This is part 9 of 13 of the split of #56, which fixes the findings of the code review tracked in #5. It is based on the previous PR in the stack, branch
review/fix-divide, so its diff shows only the one commit of this part. Please review and merge the PRs in stack order. After the previous PR merges, this one is retargeted tomain.mainThe performance PRs for #43–#55 are stacked on top of #70: #71, #72, #73, #74, #75, #76, #77, #78, #79.
How to merge this stack: see the merge guide (one at a time, in order, with "Create a merge commit", then delete the merged branch so the next PR retargets to
main).Issues
AcnDyn's scratch buffers were function-localthread_local std::vectors. C++ destroys a thread'sthread_localobjects before any static object, soAcnDynarithmetic in the destructor of a static object, or of athread_localcreated before the thread's firstAcnDynoperation, wrote into freed memory (ASan heap-use-after-free; a crash withAcnDyn<BigInt>). Each slot's state is now a constant-initialized, trivially destructiblethread_localthat is never destroyed, and athread_localreleaser frees the buffer at thread exit and marks the slot released; after that, each borrow gets a buffer of its own, freed when the borrow ends (commit411590a).The MWE is in the issue. The issue offered two fixes: document "no
AcnDynarithmetic during static/TLS destruction", or make the slots safe during destruction. This PR does the second.Behaviour changes
AcnDynarithmetic now works in static destructors and inthread_localdestructors that run during thread exit. Once a thread's buffers have been released, every scratch borrow on that thread allocates and frees its own buffer, so this late path is slower but does not leak.detail::scratch_spanreturns adetail::ScratchSpan<Int>instead of astd::span<Int>. This is detail API only. AScratchSpanis not copyable, and a temporary one does not convert to a span, sostd::span<Int> s = scratch_span<Int, Tag>(n);no longer compiles: the borrow would end with that statement. All call sites in the library already useautoand are unchanged.thread_local. Instructions perAcnDynadd/mul/fma/scale are equal to or lower than before;norm2is +1%.Testing
The first CI run failed in the g++ Release jobs: GCC 13.3, the
g++of the ubuntu-24.04 runners, reports a false-Wstringop-overflowinmatrix.hppthat GCC 13.4 (used locally) does not. Commit 3d9ece0 in #58 fixes it by makingAcn(),zero(),one()andcoeff_span()constexpr; the later #42 commit in #69 made themconstexpranyway, so the tree after #69 is unchanged. The local results below were taken before that change. After it, this tip was rebuilt and tested again with g++-13 Release-DACN_WERROR=ON: 272/272 passed, 0 warnings, and CI runs the full matrix.The tested tree is this branch's tip tree (
6e53c59). Every build uses-G Ninja -DACN_WERROR=ON. Each log was checked forNo test cases matched,in 0 test casesandNo tests ran, and none of them appears.ACN_WITH_GMP=OFF-O1)The previous PR in the stack runs 271 tests (230 with
ACN_WITH_GMP=OFF) in the same configurations, so this PR adds one:scratch_exit. clang++ was not run locally; it runs in CI.New test:
scratch_exit(new plain executable, [L5] thread_local scratch is destroyed before static objects #31). It doesAcnDyn<CheckedI64>andAcnDyn<BigInt>(only with GMP) arithmetic in a static object's destructor and, during a thread's exit, in the destructor of athread_localcreated before that thread's firstAcnDynoperation. In the sanitizer build, AddressSanitizer reports the use after free. The other builds replaceoperator new/deletewith a quarantine that fills freed blocks with a pattern; the test checks that the patterns are intact and that the exiting thread freed everything it allocated. ctest requires both "thread exit ok" and "static destruction ok" lines (the second is printed aftermainreturns) and fails onFAILED,Sanitizerorruntime error. According to the commit message, the test fails before the fix in every build, and also fails for a fallback that re-creates a per-thread buffer (a leak).A Codacy finding on #56 (CWE-120 on
memcpyintests/scratch_exit.cpp) is addressed by a follow-up folded into this commit (#56's444b718): the test stores the block size in its header with placement new, reads it back throughstd::launder, and has astatic_assertthat it fits. Codacy's two "always true" findings in the same file were left as they are, becausefreed_blocks_intact()andlive_blocks()are stubs in the ASan build on purpose: ASan and LeakSanitizer do those checks there.Earlier review discussion (CodeRabbit, Codacy) is on #56.
🤖 Generated with Claude Code
https://claude.ai/code/session_01LX2x8TzXgZCE8xUyBZSW92