Skip to content

Commit cb77273

Browse files
Dominik KlembaV8-internal LUCI CQ
authored andcommitted
Add support for random number generation for small integer ranges
This also fixes a bug where `randomNonNegativeIndex` could return a value greater than `max` when `max` is less than 10. Change-Id: Idf656a0cdd31467b20c47dd912338d22a5d60a31 Reviewed-on: https://chrome-internal-review.googlesource.com/c/v8/fuzzilli/+/8446898 Reviewed-by: Matthias Liedtke <[email protected]> Commit-Queue: Dominik Klemba <[email protected]>
1 parent 377cbe5 commit cb77273

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Sources/Fuzzilli/Base/ProgramBuilder.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,10 @@ public class ProgramBuilder {
272272
/// Returns a random integer value suitable as size of for example an array.
273273
/// The returned value is guaranteed to be positive.
274274
public func randomSize(upTo maximum: Int64 = 0x100000000) -> Int64 {
275-
assert(maximum >= 0x1000)
276-
if probability(0.5) {
275+
assert(maximum >= 0)
276+
if maximum < 0x1000 {
277+
return Int64.random(in: 0...maximum)
278+
} else if probability(0.5) {
277279
return chooseUniform(from: fuzzer.environment.interestingIntegers.filter({ $0 >= 0 && $0 <= maximum }))
278280
} else {
279281
return withEqualProbability({
@@ -291,7 +293,7 @@ public class ProgramBuilder {
291293
/// Returns a random non-negative integer value suitable as index.
292294
public func randomNonNegativeIndex(upTo max: Int64 = 0x100000000) -> Int64 {
293295
// Prefer small indices.
294-
if probability(0.33) {
296+
if max > 10 && probability(0.33) {
295297
return Int64.random(in: 0...10)
296298
} else {
297299
return randomSize(upTo: max)

0 commit comments

Comments
 (0)