Skip to content

align caller-supplied buffer in value_stack::stack - #1193

Open
Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:value-stack-align-buffer
Open

Ramya-9353 wants to merge 1 commit into
boostorg:developfrom
Ramya-9353:value-stack-align-buffer

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Repro: hand any of the buffer-taking parser / stream_parser / value_stack constructors a buffer that is not aligned for value (buf + 1, a heap block, a sub-buffer) and parse a small array. UBSan reports constructor call on misaligned address ... for type boost::json::value, which requires 8 byte alignment at detail/value.hpp:229, via value_stack::stack::push.

Cause: value_stack::stack stores value objects in the caller-owned buffer but reinterprets it as value* without aligning the pointer. static_resource::do_allocate and monotonic_resource both align a caller buffer through std::align; this constructor was the one that skipped it, and the buffer overloads document the parameter only as "a pointer to valid storage".

Fix: align the buffer up to alignof(value) with std::align, keeping min_size_ * sizeof(value) usable bytes, and fall back to the memory resource when the aligned region is too small.

Regression test walks every misalignment of a value-sized buffer and parses [1,2,3]; the odd offsets trip UBSan before the change and pass after.

@cppalliance-bot

cppalliance-bot commented Aug 28, 2026 •

Copy link
Copy Markdown

An automated preview of the documentation is available at https://1193.json.prtest2.cppalliance.org/libs/json/doc/html/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-09-27 14:58:43 UTC

@cppalliance-bot

cppalliance-bot commented Aug 28, 2026 •

Copy link
Copy Markdown

GCOVR code coverage report https://1193.json.prtest2.cppalliance.org/gcovr/index.html
LCOV code coverage report https://1193.json.prtest2.cppalliance.org/genhtml/index.html
Coverage Diff Report https://1193.json.prtest2.cppalliance.org/diff-report/index.html

Build time: 2026-09-27 15:16:52 UTC

@cppalliance-bot

Copy link
Copy Markdown

@Ramya-9353
Ramya-9353 force-pushed the value-stack-align-buffer branch from 86dfe9a to 37912e9 Compare September 8, 2026 16:18
@Ramya-9353

Copy link
Copy Markdown
Contributor Author

Rebased onto develop to pick up the GCC 16 CI fix (82398f9); the failing Drone stages were the null_resource.cpp -Wfree-nonheap-object error, not this change. No code changes.

@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.72%. Comparing base (82398f9) to head (37912e9).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1193   +/-   ##
========================================
  Coverage    93.72%   93.72%           
========================================
  Files           85       85           
  Lines         8981     8982    +1     
========================================
+ Hits          8417     8418    +1     
  Misses         564      564           
Files with missing lines Coverage Δ
include/boost/json/impl/value_stack.ipp 99.50% <100.00%> (+<0.01%) ⬆️

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 82398f9...37912e9. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cppalliance-bot

Copy link
Copy Markdown

Comment thread test/value_stack.cpp Outdated
// must cope with a buffer that is not aligned for one. Walk every
// misalignment; before the fix the odd offsets construct a `value`
// at a misaligned address (UBSan: misaligned constructor call).
alignas(value) unsigned char buf[4096 + alignof(value)];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a large buffer is not needed. 1024 is more than enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, buffer is 1024 now.

Comment thread test/value_stack.cpp Outdated
// misalignment; before the fix the odd offsets construct a `value`
// at a misaligned address (UBSan: misaligned constructor call).
alignas(value) unsigned char buf[4096 + alignof(value)];
for(std::size_t off = 0; off < alignof(value); ++off)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a loop. The buffer is aligned for value. And offset of 1 will lead to an unalligned allocation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, dropped the loop; it's a single value-aligned buffer offset by 1, which is enough to make the allocation misaligned.

Comment thread test/value_stack.cpp Outdated
storage_ptr(), buf + off, sizeof(buf) - off);
st.reset();
st.push_int64(1);
st.push_int64(2);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to push more values and then turn them into an array. Pushing just a single number will already lead to a value construction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, it just pushes one int64 now, which is enough to trip the misaligned construction.

Comment thread include/boost/json/impl/value_stack.ipp Outdated
sizeof(value))
// the buffer stores `value`s, so it has to be aligned for one;
// align it up the same way static_resource does for its buffer
if(std::align(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the call to std::align to a separate line, it will make the condition more readable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, pulled the std::align call out into its own line before the condition.

@Ramya-9353
Ramya-9353 force-pushed the value-stack-align-buffer branch from 37912e9 to 7a32d8f Compare September 27, 2026 14:48
@cppalliance-bot

Copy link
Copy Markdown

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.

3 participants