Skip to content

Commit ebf207a

Browse files
committed
fix: make sure ConsumeIntegralInRange is always in range [min; max]
For cases where FuzzedDataProvider.consume<Byte,Short,Char,Integer,Long>(from, to) sets the range [from; to] to be exactly the maximum value for the type, this methods could return values outside the requested range. This PR makes sure that the returned values are always in range. In addition, this PR boost the performance of these methods by making less use of the modulo operator to reduce fuzzer values into the range. Keeping the number "as is" allows the fuzzer to use the table of recent compares more efficiently. At the same time, this change might invalidate existing crash files and some corpus entries. However, given overall performance boost this change brings, the potential corpus invalidation is considered less important.
1 parent c733606 commit ebf207a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/native/com/code_intelligence/jazzer/driver/fuzzed_data_provider.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ T JNICALL ConsumeIntegralInRange(JNIEnv &env, jobject self, T min, T max) {
111111
env.SetIntField(self, gRemainingBytesField, remainingBytes);
112112
// dataPtr hasn't been modified, so we don't need to update gDataPtrField.
113113

114-
if (range != std::numeric_limits<T>::max())
115-
// We accept modulo bias in favor of reading a dynamic number of bytes as
116-
// this would make it harder for the fuzzer to mutate towards values from
117-
// the table of recent compares.
118-
result = result % (range + 1);
119-
120-
return static_cast<T>(min + result);
114+
// The number is already within the requested range. Keeping it "as is"
115+
// makes great use of the table of recent compares.
116+
if (static_cast<T>(result) >= min && static_cast<T>(result) <= max) {
117+
return static_cast<T>(result);
118+
}
119+
// We accept modulo bias in favor of reading a dynamic number of bytes as
120+
// this would make it harder for the fuzzer to mutate towards values from
121+
// the table of recent compares.
122+
return static_cast<T>(min + (result % (range + 1)));
121123
}
122124

123125
template <typename T>

0 commit comments

Comments
 (0)