Fix the helper function which returns random (exprand-style) file
offsets for fuzzing.
It didn't do proper job (as per so it's been replaced by
something simpler. Now it's still prefering smaller values, but it's
a step function-like distribution now.
@@ -46,7 +46,7 @@ static inline size_t mangle_LenLeft(run_t* run, size_t off) {
return (run->dynfile->size - off - 1);
}
-/* Get a random value between <1:max> with x^2 distribution */
+/* Get a random value <1:max>, but prefer smaller ones - up to 4KiB */
static inline size_t mangle_getLen(size_t max) {
if (max > _HF_INPUT_MAX_SIZE) {
LOG_F("max (%zu) > _HF_INPUT_MAX_SIZE (%zu)", max, (size_t)_HF_INPUT_MAX_SIZE);
@@ -58,22 +58,23 @@ static inline size_t mangle_getLen(size_t max) {
return 1;
}
- const uint64_t max2 = (uint64_t)max * max;
- const uint64_t max3 = (uint64_t)max * max * max;
- const uint64_t rnd = util_rndGet(1, max2 - 1);
-
- uint64_t ret = rnd * rnd;
- ret /= max3;
- ret += 1;
-
- if (ret < 1) {
- LOG_F("ret (%" PRIu64 ") < 1, max:%zu, rnd:%" PRIu64, ret, max, rnd);
- }
- if (ret > max) {
- LOG_F("ret (%" PRIu64 ") > max (%zu), rnd:%" PRIu64, ret, max, rnd);
+ /* Give 50% chance the the uniform distribution */
+ switch (util_rndGet(0, 9)) {
+ case 0:
+ return util_rndGet(1, HF_MIN(16, max));
+ case 1:
+ return util_rndGet(1, HF_MIN(64, max));
+ case 2:
+ return util_rndGet(1, HF_MIN(256, max));
+ case 3:
+ return util_rndGet(1, HF_MIN(1024, max));
+ case 4:
+ return util_rndGet(1, HF_MIN(4096, max));
+ default:
+ break;
}
- return (size_t)ret;
+ return util_rndGet(1, max);
}
/* Prefer smaller values here, so use mangle_getLen() */
