Skip to content

Add ASan redzones between zone allocations (C) - #1195

Open
0xalpharush wants to merge 1 commit into
msgpack:c_masterfrom
0xalpharush:c-zone-asan-annotations
Open

0xalpharush wants to merge 1 commit into
msgpack:c_masterfrom
0xalpharush:c-zone-asan-annotations

Conversation

@0xalpharush

@0xalpharush 0xalpharush commented Oct 27, 2026 •

Copy link
Copy Markdown

msgpack_zone carves the unpacker's array/map element buffers out of one malloc()ed chunk. An overflow from one into its neighbour stays inside a live heap block, so AddressSanitizer stays silent. This is detector hardening, not a bug fix, and the technique isn't new (manual allocator poisoning; see Trail of Bits, https://blog.trailofbits.com/2024/09/10/sanitize-your-c-containers-asan-annotations-step-by-step/).

  • It is active only under ASan; non-ASan objects are byte-identical (gcc/clang). -DMSGPACK_ZONE_NO_ASAN_ANNOTATIONS opts out. Because msgpack_zone_malloc* are inline in zone.h, build the library and its users the same way.
  • msgpack_zone_malloc/_no_align place each object on an 8-byte granule with a poisoned redzone after it. Chunks of the configured size are poisoned when created and re-poisoned by msgpack_zone_clear(). Oversized chunks aren't pre-poisoned. msgpack_zone_malloc_expand() keeps its contract: the bytes it hands out are usable.

Validation: unpacking [[1],[2]] and writing one element past the first inner array is silent on stock and reports use-after-poison with the patch. All 6 tests pass under ASan with annotations on and off. The OSS-Fuzz public corpus plus about 16M executions of an unpack/pack harness show no reports. ASan overhead is about +4% time with unchanged peak memory on a corpus benchmark.

Companion PR for the C++ zone on cpp_master: #1194.

🤖 Generated with Claude Code

The zone carves many objects out of one malloc()ed chunk, so an
overflow from one zone-allocated object into its neighbour stays inside
a live heap block and AddressSanitizer cannot see it.

When building with ASan, follow every allocation with a poisoned
redzone, keep chunks of the configured size poisoned until handed out,
and re-poison on msgpack_zone_clear(). Oversized chunks are not
pre-poisoned, so a huge allocation costs no shadow updates proportional
to its size. Builds without ASan are unchanged. Define
MSGPACK_ZONE_NO_ASAN_ANNOTATIONS to opt out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.19%. Comparing base (eabfe29) to head (def52f2).
⚠️ Report is 4 commits behind head on c_master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##           c_master    #1195      +/-   ##
============================================
+ Coverage     57.47%   58.19%   +0.72%     
============================================
  Files            10       10              
  Lines          1124     1141      +17     
  Branches        129      139      +10     
============================================
+ Hits            646      664      +18     
+ Misses          478      477       -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants