Fix integer-range bugs in absl::log_uniform_int_distribution. Previously, floating-point `log`/`pow` bucket math and signed `result_type` casts caused misbehavior on large or signed ranges: * For `base > 2`, `double` rounding could drop exact-power endpoints (e.g. `10^15` in base 10), create spurious singleton top buckets (e.g. `5^26 - 1` in base 5), or trigger `[conv.fpint]` float-to-int overflow UB at `UINT64_MAX`. * Casting unsigned bucket offsets `[lo, hi]` to signed `result_type` before sampling inverted `lo > hi` whenever `min < 0` and `max - min > INT_MAX`. Output sequences change for affected `base > 2` and wide signed ranges; `absl::log_uniform_int_distribution` does not guarantee cross-release stability. PiperOrigin-RevId: 1022527247
diff --git a/absl/random/BUILD.bazel b/absl/random/BUILD.bazel index 162e328..e709215 100644 --- a/absl/random/BUILD.bazel +++ b/absl/random/BUILD.bazel
@@ -288,6 +288,7 @@ ":random", "//absl/base:core_headers", "//absl/log", + "//absl/numeric:int128", "//absl/random/internal:distribution_test_util", "//absl/random/internal:pcg_engine", "//absl/random/internal:sequence_urbg",
diff --git a/absl/random/CMakeLists.txt b/absl/random/CMakeLists.txt index cef4ab5..578883b 100644 --- a/absl/random/CMakeLists.txt +++ b/absl/random/CMakeLists.txt
@@ -349,6 +349,7 @@ LINKOPTS ${ABSL_DEFAULT_LINKOPTS} DEPS + absl::int128 absl::log absl::random_distributions absl::random_internal_distribution_test_util
diff --git a/absl/random/log_uniform_int_distribution.h b/absl/random/log_uniform_int_distribution.h index 8bcb60e..67d4257 100644 --- a/absl/random/log_uniform_int_distribution.h +++ b/absl/random/log_uniform_int_distribution.h
@@ -16,7 +16,6 @@ #define ABSL_RANDOM_LOG_UNIFORM_INT_DISTRIBUTION_H_ #include <algorithm> -#include <cmath> #include <ios> #include <istream> #include <limits> @@ -34,9 +33,10 @@ // log_uniform_int_distribution: // // Returns a random variate R in range [min, max] such that -// floor(log(R-min, base)) is uniformly distributed. -// We ensure uniformity by discretization using the -// boundary sets [0, 1, base, base * base, ... min(base*n, max)] +// floor(log(R - min, base)) is uniformly distributed. +// Uniformity is ensured by partitioning [min, max] into buckets starting at +// min, min + 1, min + base, min + base^2, ... (up to max), choosing a bucket +// uniformly, and then sampling uniformly within it. // template <typename IntType = int> class log_uniform_int_distribution { @@ -69,22 +69,18 @@ // value which can be used to construct bounds. log_range_ = (std::min)(random_internal::BitWidth(range()), std::numeric_limits<unsigned_type>::digits); - } else if (base_ > 2) { - // An out-of-contract base_ (<= 1) skips this branch entirely so that - // no floating-point undefined behavior is reached. - // - // NOTE: Computing the logN(x) introduces error from 2 sources: - // 1. Conversion of int to double loses precision for values >= - // 2^53, which may cause some log() computations to operate on - // different values. - // 2. The error introduced by the division will cause the result - // to differ from the expected value. - // - // Thus a result which should equal K may equal K +/- epsilon, - // which can eliminate some values depending on where the bounds fall. - const double inv_log_base = 1.0 / std::log(static_cast<double>(base_)); - const double log_range = std::log(static_cast<double>(range()) + 0.5); - log_range_ = static_cast<int>(std::ceil(inv_log_base * log_range)); + } else if (base_ > 2 && range() > 0) { + // An out-of-contract base_ (<= 1) skips this branch entirely to avoid + // division by 0 and guarantee termination. + const unsigned_type ubase = static_cast<unsigned_type>(base_); + const unsigned_type limit = range() / ubase; + unsigned_type pow = 1; + int log_range = 1; + while (pow <= limit) { + pow *= ubase; + ++log_range; + } + log_range_ = log_range; } } @@ -110,7 +106,7 @@ result_type max_; result_type base_; unsigned_type range_; // max - min - int log_range_; // ceil(logN(range_)) + int log_range_; // range_ != 0 ? floor(log(range_, base_)) + 1 : 0 static_assert(random_internal::IsIntegral<IntType>::value, "Class-template absl::log_uniform_int_distribution<> must be " @@ -138,7 +134,8 @@ template <typename URBG> result_type operator()(URBG& g, // NOLINT(runtime/references) const param_type& p) { - return static_cast<result_type>((p.min)() + Generate(g, p)); + return static_cast<result_type>(static_cast<unsigned_type>((p.min)()) + + Generate(g, p)); } result_type(min)() const { return (param_.min)(); } @@ -175,43 +172,42 @@ const param_type& p) { // sample e over [0, log_range]. Map the results of e to this: // 0 => 0 - // 1 => [1, b-1] - // 2 => [b, (b^2)-1] - // n => [b^(n-1)..(b^n)-1] + // 1 => [1, base-1] + // 2 => [base, (base^2)-1] + // n => [base^(n-1)..(base^n)-1] const int e = absl::uniform_int_distribution<int>(0, p.log_range())(g); if (e == 0) { return 0; } const int d = e - 1; - unsigned_type base_e, top_e; - if (p.base() == 2) { - base_e = static_cast<unsigned_type>(1) << d; - - top_e = (e >= std::numeric_limits<unsigned_type>::digits) - ? (std::numeric_limits<unsigned_type>::max)() - : (static_cast<unsigned_type>(1) << e) - 1; + // param_type establishes base^(log_range - 1) <= range < base^log_range + // whenever log_range > 0. Thus base^d <= range for every e in [1, log_range], + // and base^e <= range (so base^e fits in unsigned_type) if e < log_range. + const unsigned_type ubase = static_cast<unsigned_type>(p.base()); + unsigned_type lo; + if (ubase == 2) { + lo = static_cast<unsigned_type>(unsigned_type{1} << d); } else { - const double r = std::pow(static_cast<double>(p.base()), d); - const double s = (r * static_cast<double>(p.base())) - 1.0; - - base_e = - (r > static_cast<double>((std::numeric_limits<unsigned_type>::max)())) - ? (std::numeric_limits<unsigned_type>::max)() - : static_cast<unsigned_type>(r); - - top_e = - (s > static_cast<double>((std::numeric_limits<unsigned_type>::max)())) - ? (std::numeric_limits<unsigned_type>::max)() - : static_cast<unsigned_type>(s); + lo = 1; + unsigned_type factor = ubase; + for (int exp = d; exp > 0; exp >>= 1) { + if ((exp & 1) != 0) { + lo *= factor; + } + // Skip the unused final squaring: for uint16_t, squaring a large factor + // (e.g. 50000) promotes to signed int and overflows. + if (exp > 1) { + factor *= factor; + } + } } - - const unsigned_type lo = (base_e >= p.range()) ? p.range() : base_e; - const unsigned_type hi = (top_e >= p.range()) ? p.range() : top_e; + const unsigned_type hi = (e == p.log_range()) + ? p.range() + : static_cast<unsigned_type>((lo * ubase) - 1); // choose uniformly over [lo, hi] - return absl::uniform_int_distribution<result_type>( - static_cast<result_type>(lo), static_cast<result_type>(hi))(g); + return absl::uniform_int_distribution<unsigned_type>(lo, hi)(g); } template <typename CharT, typename Traits, typename IntType>
diff --git a/absl/random/log_uniform_int_distribution_test.cc b/absl/random/log_uniform_int_distribution_test.cc index 403aaea..992f842 100644 --- a/absl/random/log_uniform_int_distribution_test.cc +++ b/absl/random/log_uniform_int_distribution_test.cc
@@ -20,15 +20,18 @@ #include <cstdint> #include <iterator> #include <limits> +#include <ostream> #include <random> #include <sstream> #include <string> +#include <type_traits> #include <vector> #include "gmock/gmock.h" #include "gtest/gtest.h" #include "absl/base/macros.h" #include "absl/log/log.h" +#include "absl/numeric/int128.h" #include "absl/random/internal/chi_square.h" #include "absl/random/internal/distribution_test_util.h" #include "absl/random/internal/pcg_engine.h" @@ -41,6 +44,27 @@ namespace { +using ::testing::AllOf; +using ::testing::Each; +using ::testing::ElementsAre; +using ::testing::Field; +using ::testing::Ge; +using ::testing::Gt; +using ::testing::IsTrue; +using ::testing::Le; +using ::testing::SizeIs; + +// Fixed nothing-up-my-sleeve seed for deterministic sampling tests (hex digits +// of Euler's number e, used across Abseil random tests). +constexpr uint64_t kTestSeed = 0x2B7E151628AED2A6ULL; +// ~5 sigma for binomial(n, p) with expected bucket count 1000. +constexpr int kExpectedBucketHits = 1000; +constexpr int kBucketHitTolerance = 150; + +constexpr uint64_t PowU64(uint64_t base, int exp) { + return exp == 0 ? 1 : base * PowU64(base, exp - 1); +} + template <typename IntType> class LogUniformIntDistributionTypeTest : public ::testing::Test {}; @@ -148,7 +172,7 @@ // We use a fixed bit generator for distribution accuracy tests. This allows // these tests to be deterministic, while still testing the qualify of the // implementation. - absl::random_internal::pcg64_2018_engine rng_{0x2B7E151628AED2A6}; + absl::random_internal::pcg64_2018_engine rng_{kTestSeed}; }; double LogUniformIntChiSquaredTest::ChiSquaredTestImpl() { @@ -272,9 +296,9 @@ // NOTE: absl::log_uniform_int_distribution is not guaranteed to be stable. TEST(LogUniformIntDistributionTest, StabilityTest) { - using testing::ElementsAre; - // absl::uniform_int_distribution stability relies on - // absl::random_internal::LeadingSetBit, std::log, std::pow. + // absl::log_uniform_int_distribution stability relies on + // absl::uniform_int_distribution, absl::random_internal::BitWidth, and + // integer exponentiation. absl::random_internal::sequence_urbg urbg( {0x0003eb76f6f7f755ull, 0xFFCEA50FDB2F953Bull, 0xC332DDEFBE6C5AA5ull, 0x6558218568AB9702ull, 0x2AEF7DAD5B6E2F84ull, 0x1521B62829076170ull, @@ -417,4 +441,196 @@ EXPECT_THAT(ParseFromStream(kInitial, "0 10"), FailsLeavingUnchanged()); } +template <typename IntType> +struct SampleSummary { + bool all_in_bounds = true; + IntType min_sample = (std::numeric_limits<IntType>::max)(); + IntType max_sample = (std::numeric_limits<IntType>::min)(); + int max_hits = 0; + std::vector<int> bucket_counts; +}; + +template <typename IntType> +void PrintTo(const SampleSummary<IntType>& summary, std::ostream* os) { + // Unary '+' promotes int8_t/uint8_t so they print numerically, not as chars. + *os << "{all_in_bounds: " << (summary.all_in_bounds ? "true" : "false") + << ", min_sample: " << +summary.min_sample + << ", max_sample: " << +summary.max_sample + << ", max_hits: " << summary.max_hits + << ", bucket_counts: " << ::testing::PrintToString(summary.bucket_counts) + << "}"; +} + +MATCHER(AllInBounds, "") { + return ::testing::ExplainMatchResult( + Field("all_in_bounds", &std::decay_t<arg_type>::all_in_bounds, IsTrue()), + arg, result_listener); +} + +MATCHER_P(MinSampleIs, matcher, "") { + return ::testing::ExplainMatchResult( + Field("min_sample", &std::decay_t<arg_type>::min_sample, matcher), arg, + result_listener); +} + +MATCHER_P(MaxSampleIs, matcher, "") { + return ::testing::ExplainMatchResult( + Field("max_sample", &std::decay_t<arg_type>::max_sample, matcher), arg, + result_listener); +} + +MATCHER_P(MaxHitsIs, matcher, "") { + return ::testing::ExplainMatchResult( + Field("max_hits", &std::decay_t<arg_type>::max_hits, matcher), arg, + result_listener); +} + +MATCHER_P(BucketCountsAre, matcher, "") { + return ::testing::ExplainMatchResult( + Field("bucket_counts", &std::decay_t<arg_type>::bucket_counts, matcher), + arg, result_listener); +} + +MATCHER_P(HasNonEmptyBuckets, count, "") { + return ::testing::ExplainMatchResult( + Field("bucket_counts", &std::decay_t<arg_type>::bucket_counts, + AllOf(SizeIs(count), Each(Gt(0)))), + arg, result_listener); +} + +// Runs `trials` draws using fixed seed `kTestSeed`, so runtime flake rate is 0. +// For an arbitrary seed, false-failure probability across the tests below is +// < 2e-5 (dominated by the base-3 top-tail check at ~1.8e-5; bucket-count +// bounds are ~1e-6 and HasNonEmptyBuckets union bounds are < 1e-65). +template <typename IntType> +SampleSummary<IntType> SampleLogUniform(IntType min, IntType max, IntType base, + int trials) { + using UIntType = std::make_unsigned_t<IntType>; + const UIntType umin = static_cast<UIntType>(min); + const UIntType umax = static_cast<UIntType>(max); + const UIntType ubase = static_cast<UIntType>(base); + // Oracle to determine bucket for each offset. + const auto exact_bucket = [ubase](UIntType offset) -> size_t { + if (offset == 0) return 0; + size_t bucket = 1; + // Use uint128 so `pow * ubase` cannot overflow for uint64_t offsets up to + // UINT64_MAX. + absl::uint128 pow = 1; + while (pow * ubase <= offset) { + pow *= ubase; + ++bucket; + } + return bucket; + }; + + SampleSummary<IntType> summary; + summary.bucket_counts.assign(exact_bucket(umax - umin) + 1, 0); + absl::random_internal::pcg64_2018_engine rng(kTestSeed); + absl::log_uniform_int_distribution<IntType> dist(min, max, base); + for (int i = 0; i < trials; ++i) { + const IntType v = dist(rng); + if (v < min || v > max) { + summary.all_in_bounds = false; + } else { + ++summary.bucket_counts[exact_bucket(static_cast<UIntType>(v) - umin)]; + } + summary.min_sample = (std::min)(summary.min_sample, v); + summary.max_sample = (std::max)(summary.max_sample, v); + if (v == max) { + ++summary.max_hits; + } + } + return summary; +} + +TEST(LogUniformIntDistributionTest, NegativeToMaxInt32RangeCoversAllBuckets) { + // Width (max - min) exceeds INT32_MAX, exercising unsigned offset arithmetic. + EXPECT_THAT(SampleLogUniform<int32_t>(-10, INT32_MAX, 10, /*trials=*/5000), + AllOf(AllInBounds(), MinSampleIs(-10), + MaxSampleIs(Gt(INT32_MAX / 2)), HasNonEmptyBuckets(11))); +} + +TEST(LogUniformIntDistributionTest, FullInt64RangeCoversAllBuckets) { + EXPECT_THAT( + SampleLogUniform<int64_t>(INT64_MIN, INT64_MAX, 10, /*trials=*/5000), + AllOf(AllInBounds(), MinSampleIs(INT64_MIN), + MaxSampleIs(Gt(INT64_MAX / 2)), HasNonEmptyBuckets(21))); +} + +TEST(LogUniformIntDistributionTest, FullUint64RangeBase2Covers65FullBuckets) { + EXPECT_THAT( + SampleLogUniform<uint64_t>(0, UINT64_MAX, 2, /*trials=*/10000), + AllOf(AllInBounds(), MinSampleIs(0u), MaxSampleIs(Ge(uint64_t{1} << 63)), + MaxHitsIs(0), HasNonEmptyBuckets(65))); +} + +TEST(LogUniformIntDistributionTest, FullUint64RangeBase4Covers33FullBuckets) { + // UINT64_MAX == 4^32 - 1, so the top bucket is full (no singleton at max). + EXPECT_THAT( + SampleLogUniform<uint64_t>(0, UINT64_MAX, 4, /*trials=*/5000), + AllOf(AllInBounds(), MinSampleIs(0u), MaxSampleIs(Ge(uint64_t{1} << 62)), + MaxHitsIs(0), HasNonEmptyBuckets(33))); +} + +TEST(LogUniformIntDistributionTest, FullUint64RangeBase3ReachesTopOfRange) { + // 3^40 < UINT64_MAX < 3^41, so the 42nd bucket spans [3^40, UINT64_MAX]. + EXPECT_THAT(SampleLogUniform<uint64_t>(0, UINT64_MAX, 3, /*trials=*/10000), + AllOf(AllInBounds(), MinSampleIs(0u), + MaxSampleIs(Gt(UINT64_MAX - (UINT64_MAX >> 6))), + HasNonEmptyBuckets(42))); +} + +TEST(LogUniformIntDistributionTest, MaxBaseFormsThreeEqualBuckets) { + // Base == UINT64_MAX on [0, UINT64_MAX] forms 3 buckets ({0}, + // [1, UINT64_MAX - 1], and {UINT64_MAX}), each drawn with probability 1/3. + const auto approx_expected_hits = + AllOf(Ge(kExpectedBucketHits - kBucketHitTolerance), + Le(kExpectedBucketHits + kBucketHitTolerance)); + EXPECT_THAT( + SampleLogUniform<uint64_t>(0, UINT64_MAX, UINT64_MAX, /*trials=*/3000), + AllOf(AllInBounds(), MinSampleIs(0u), MaxSampleIs(UINT64_MAX), + MaxHitsIs(approx_expected_hits), + BucketCountsAre(ElementsAre(approx_expected_hits, + approx_expected_hits, + approx_expected_hits)))); +} + +TEST(LogUniformIntDistributionTest, SingleValueRangeReturnsMin) { + EXPECT_THAT(SampleLogUniform<uint64_t>(5, 5, 3, /*trials=*/100), + AllOf(AllInBounds(), MinSampleIs(5u), MaxSampleIs(5u), + BucketCountsAre(ElementsAre(100)))); +} + +TEST(LogUniformIntDistributionTest, ExactPowerMaxFormsSingletonTopBucket) { + // 10^15 is the sole element of the 17th bucket (weight 1/17, ~1000 of 17000). + constexpr int64_t kPow10_15 = static_cast<int64_t>(PowU64(10, 15)); + EXPECT_THAT( + SampleLogUniform<int64_t>(0, kPow10_15, 10, /*trials=*/17000), + AllOf(AllInBounds(), + MaxHitsIs(AllOf(Ge(kExpectedBucketHits - kBucketHitTolerance), + Le(kExpectedBucketHits + kBucketHitTolerance))), + HasNonEmptyBuckets(17))); +} + +TEST(LogUniformIntDistributionTest, PowerMinusOneMaxHasNoSingletonTopBucket) { + // 5^26 - 1 > 2^53 fills the top bucket [5^25, 5^26 - 1] without a singleton. + constexpr uint64_t kPow5_26Minus1 = PowU64(5, 26) - 1; + EXPECT_THAT( + SampleLogUniform<uint64_t>(0, kPow5_26Minus1, 5, /*trials=*/10000), + AllOf(AllInBounds(), MaxHitsIs(0), HasNonEmptyBuckets(27))); +} + +TEST(LogUniformIntDistributionTest, + LargeUint16BaseAvoidsSignedPromotionOverflow) { + // Squaring uint16_t(50000) would promote to signed int and overflow INT_MAX. + const auto approx_expected_hits = + AllOf(Ge(kExpectedBucketHits - kBucketHitTolerance), + Le(kExpectedBucketHits + kBucketHitTolerance)); + EXPECT_THAT(SampleLogUniform<uint16_t>(0, UINT16_MAX, 50000, /*trials=*/3000), + AllOf(AllInBounds(), MinSampleIs(0u), MaxSampleIs(Ge(50000u)), + BucketCountsAre(ElementsAre(approx_expected_hits, + approx_expected_hits, + approx_expected_hits)))); +} + } // namespace