From 3076795ed96cd61d1cf1e1a32ff64a8e4de1ed68 Mon Sep 17 00:00:00 2001 From: Chris Kennelly Date: Wed, 6 Nov 2024 09:24:46 -0800 Subject: [PATCH] Touch initial arena blocks in debug builds. This can be used to detect mismatched block allocation sizes with the provided size. PiperOrigin-RevId: 693754254 --- src/google/protobuf/arena.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index d250b24ac015..12ac6880d644 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -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(buf)[0] = 0; + static_cast(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}; @@ -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(buf)[0] = 0; + static_cast(buf)[size - 1] = 0; + } +#endif ABSL_ANNOTATE_MEMORY_IS_UNINITIALIZED(buf, size); alloc_policy_.set_is_user_owned_initial_block(true); }