Replace PROTOBUF_ENABLE_FUZZ_MESSAGE_SPACE_USED_LONG macro with constexpr function for easier maintenance.

No semantic change expected.

PiperOrigin-RevId: 675264102
pull/18342/head
Protobuf Team Bot 5 months ago committed by Copybara-Service
parent 1a83839831
commit c666aef7e6
  1. 26
      src/google/protobuf/generated_message_reflection.cc
  2. 4
      src/google/protobuf/port.h
  3. 4
      src/google/protobuf/port_def.inc
  4. 1
      src/google/protobuf/port_undef.inc

@ -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<uintptr_t>(&dummy) ^ reinterpret_cast<uintptr_t>(this);
// Fuzz the size by +/- 50%.
double scale = (static_cast<double>(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<uintptr_t>(&dummy) ^ reinterpret_cast<uintptr_t>(this);
// Fuzz the size by +/- 50%.
double scale = (static_cast<double>(seed % 10000) / 10000) + 0.5;
return total_size * scale;
} else {
return total_size;
}
}
namespace {

@ -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; }

@ -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

@ -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

Loading…
Cancel
Save