diff --git a/src/google/protobuf/generated_message_reflection.cc b/src/google/protobuf/generated_message_reflection.cc index e79e7ca919c8..ea5132129ba5 100644 --- a/src/google/protobuf/generated_message_reflection.cc +++ b/src/google/protobuf/generated_message_reflection.cc @@ -496,19 +496,19 @@ size_t Reflection::SpaceUsedLong(const Message& message) const { } } } -#ifndef PROTOBUF_FUZZ_MESSAGE_SPACE_USED_LONG - return total_size; -#else - // Use both `this` and `dummy` to generate the seed so that the scale factor - // is both per-object and non-predictable, but consistent across multiple - // calls in the same binary. - static bool dummy; - uintptr_t seed = - reinterpret_cast(&dummy) ^ reinterpret_cast(this); - // Fuzz the size by +/- 50%. - double scale = (static_cast(seed % 10000) / 10000) + 0.5; - return total_size * scale; -#endif + if (internal::DebugHardenFuzzMessageSpaceUsedLong()) { + // Use both `this` and `dummy` to generate the seed so that the scale factor + // is both per-object and non-predictable, but consistent across multiple + // calls in the same binary. + static bool dummy; + uintptr_t seed = + reinterpret_cast(&dummy) ^ reinterpret_cast(this); + // Fuzz the size by +/- 50%. + double scale = (static_cast(seed % 10000) / 10000) + 0.5; + return total_size * scale; + } else { + return total_size; + } } namespace { diff --git a/src/google/protobuf/port.h b/src/google/protobuf/port.h index d02a17f6609f..b8f01a2d8766 100644 --- a/src/google/protobuf/port.h +++ b/src/google/protobuf/port.h @@ -281,6 +281,10 @@ constexpr bool DebugHardenForceCopyInMove() { return false; } +constexpr bool DebugHardenFuzzMessageSpaceUsedLong() { + return false; +} + // Returns true if pointers are 8B aligned, leaving least significant 3 bits // available. inline constexpr bool PtrIsAtLeast8BAligned() { return alignof(void*) >= 8; } diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index 3fb34d9e8543..80d6df57e6a5 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -398,10 +398,6 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3), #define PROTOBUF_RESTRICT #endif -#ifdef PROTOBUF_FUZZ_MESSAGE_SPACE_USED_LONG -#error PROTOBUF_FUZZ_MESSAGE_SPACE_USED_LONG was previously defined -#endif - #ifdef PROTOBUF_FORCE_ALLOCATION_ON_CONSTRUCTION #error PROTOBUF_FORCE_ALLOCATION_ON_CONSTRUCTION was previously defined #endif diff --git a/src/google/protobuf/port_undef.inc b/src/google/protobuf/port_undef.inc index 8aa31265582f..da4e272f75e8 100644 --- a/src/google/protobuf/port_undef.inc +++ b/src/google/protobuf/port_undef.inc @@ -41,7 +41,6 @@ #undef PROTOC_EXPORT #undef PROTOBUF_NODISCARD #undef PROTOBUF_RESTRICT -#undef PROTOBUF_FUZZ_MESSAGE_SPACE_USED_LONG #undef PROTOBUF_FORCE_ALLOCATION_ON_CONSTRUCTION #undef PROTOBUF_UNUSED #undef PROTOBUF_ASSUME