Skip to content

Commit

Permalink
Replace PROTOBUF_ENABLE_FUZZ_MESSAGE_SPACE_USED_LONG macro with const…
Browse files Browse the repository at this point in the history
…expr function for easier maintenance.

No semantic change expected.

PiperOrigin-RevId: 675264102
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Sep 16, 2024
1 parent 1a83839 commit c666aef
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
26 changes: 13 additions & 13 deletions src/google/protobuf/generated_message_reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 0 additions & 4 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/google/protobuf/port_undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c666aef

Please sign in to comment.