Skip to content

Commit

Permalink
Touch initial arena blocks in debug builds.
Browse files Browse the repository at this point in the history
This can be used to detect mismatched block allocation sizes with the provided
size.

PiperOrigin-RevId: 693754254
  • Loading branch information
ckennelly authored and copybara-github committed Dec 24, 2024
1 parent 1223341 commit 3076795
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/google/protobuf/arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,13 @@ ArenaBlock* ThreadSafeArena::FirstBlock(void* buf, size_t size) {
return SentryArenaBlock();
}
// Record user-owned block.
#ifndef NDEBUG
// Touch block to verify it is addressable.
if (size > 0) {
static_cast<char*>(buf)[0] = 0;
static_cast<char*>(buf)[size - 1] = 0;
}
#endif
ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(buf, size);
alloc_policy_.set_is_user_owned_initial_block(true);
return new (buf) ArenaBlock{nullptr, size};
Expand All @@ -603,6 +610,13 @@ ArenaBlock* ThreadSafeArena::FirstBlock(void* buf, size_t size,
} else {
mem = {buf, size};
// Record user-owned block.
#ifndef NDEBUG
// Touch block to verify it is addressable.
if (size > 0) {
static_cast<char*>(buf)[0] = 0;
static_cast<char*>(buf)[size - 1] = 0;
}
#endif
ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(buf, size);
alloc_policy_.set_is_user_owned_initial_block(true);
}
Expand Down

0 comments on commit 3076795

Please sign in to comment.