[compiler-rt] [libFuzzer] Pass the current size to Mutate in MinimizeCrashLoop (PR #223470)
Stan Ulbrych via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 11:14:34 PDT 2026
https://github.com/StanFromIreland updated https://github.com/llvm/llvm-project/pull/223470
>From 12dcf0fcdc1e95e12d4b648855c3d1df7692ff52 Mon Sep 17 00:00:00 2001
From: Stan Ulbrych <stan at python.org>
Date: Mon, 14 Sep 2026 18:33:22 +0100
Subject: [PATCH 1/4] [libFuzzer] Pass the current size to Mutate in
MinimizeCrashLoop
Fixes #149942. MinimizeCrashLoop passed U.size() to every MD.Mutate call in the mutate_depth loop, so following mutations operated on a stale size. Track the size across mutations, like MutateAndTestOne already does.
---
compiler-rt/lib/fuzzer/FuzzerLoop.cpp | 12 ++++----
.../fuzzer/MinimizeCrashCustomMutatorTest.cpp | 29 +++++++++++++++++++
.../fuzzer/minimize_crash_custom_mutator.test | 6 ++++
3 files changed, 42 insertions(+), 5 deletions(-)
create mode 100644 compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
create mode 100644 compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
diff --git a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
index 39ccf50c40069..47c9b92981238 100644
--- a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
@@ -921,13 +921,15 @@ void Fuzzer::MinimizeCrashLoop(const Unit &U) {
return;
while (!TimedOut() && TotalNumberOfRuns < Options.MaxNumberOfRuns) {
MD.StartMutationSequence();
- memcpy(CurrentUnitData, U.data(), U.size());
+ size_t Size = U.size();
+ memcpy(CurrentUnitData, U.data(), Size);
for (int i = 0; i < Options.MutateDepth; i++) {
- size_t NewSize = MD.Mutate(CurrentUnitData, U.size(), MaxMutationLen);
+ size_t NewSize = MD.Mutate(CurrentUnitData, Size, MaxMutationLen);
assert(NewSize > 0 && NewSize <= MaxMutationLen);
- ExecuteCallback(CurrentUnitData, NewSize);
- PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
- TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
+ Size = NewSize;
+ ExecuteCallback(CurrentUnitData, Size);
+ PrintPulseAndReportSlowInput(CurrentUnitData, Size);
+ TryDetectingAMemoryLeak(CurrentUnitData, Size,
/*DuringInitialCorpusExecution*/ false);
}
}
diff --git a/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp b/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
new file mode 100644
index 0000000000000..d7f18acdb0dd6
--- /dev/null
+++ b/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
@@ -0,0 +1,29 @@
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+// Test that -minimize_crash passes the size returned by the previous mutation
+// to the next one when mutate_depth > 1.
+#include <cstddef>
+#include <cstdint>
+#include <cstdio>
+
+#include "FuzzerInterface.h"
+
+static volatile int *Null = 0;
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ if (Size > 0 && Data[0] == '!')
+ *Null = 1;
+ return 0;
+}
+
+// Maintains the invariant Data[0] == '0' + Size, so a stale Size passed by the
+// minimizer is detected.
+extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
+ size_t MaxSize, unsigned int Seed) {
+ if (Data[0] != '!' && Data[0] != '0' + Size)
+ fprintf(stderr, "MUTATOR: size mismatch");
+ Data[0] = '0' + MaxSize;
+ return MaxSize;
+}
diff --git a/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test b/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
new file mode 100644
index 0000000000000..d8ce78b10c0a9
--- /dev/null
+++ b/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
@@ -0,0 +1,6 @@
+RUN: %cpp_compiler %S/MinimizeCrashCustomMutatorTest.cpp -o %t-MinimizeCrashCustomMutatorTest
+RUN: mkdir -p %t.dir
+RUN: echo -n '!2345678' > %t.dir/crash
+RUN: %run %t-MinimizeCrashCustomMutatorTest -minimize_crash=1 -mutate_depth=5 -runs=10 %t.dir/crash 2>&1 | FileCheck %s
+CHECK-NOT: MUTATOR: size mismatch
+CHECK: CRASH_MIN: failed to minimize beyond {{.*}}crash (8 bytes), exiting
>From 55a25308f86676f4d06d12e304ff1298983ac6de Mon Sep 17 00:00:00 2001
From: Stan Ulbrych <stan at python.org>
Date: Mon, 14 Sep 2026 20:22:36 +0100
Subject: [PATCH 2/4] Move tests to a separate PR
---
.../fuzzer/MinimizeCrashCustomMutatorTest.cpp | 29 -------------------
.../fuzzer/minimize_crash_custom_mutator.test | 6 ----
2 files changed, 35 deletions(-)
delete mode 100644 compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
delete mode 100644 compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
diff --git a/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp b/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
deleted file mode 100644
index d7f18acdb0dd6..0000000000000
--- a/compiler-rt/test/fuzzer/MinimizeCrashCustomMutatorTest.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-// Test that -minimize_crash passes the size returned by the previous mutation
-// to the next one when mutate_depth > 1.
-#include <cstddef>
-#include <cstdint>
-#include <cstdio>
-
-#include "FuzzerInterface.h"
-
-static volatile int *Null = 0;
-
-extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
- if (Size > 0 && Data[0] == '!')
- *Null = 1;
- return 0;
-}
-
-// Maintains the invariant Data[0] == '0' + Size, so a stale Size passed by the
-// minimizer is detected.
-extern "C" size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size,
- size_t MaxSize, unsigned int Seed) {
- if (Data[0] != '!' && Data[0] != '0' + Size)
- fprintf(stderr, "MUTATOR: size mismatch");
- Data[0] = '0' + MaxSize;
- return MaxSize;
-}
diff --git a/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test b/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
deleted file mode 100644
index d8ce78b10c0a9..0000000000000
--- a/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
+++ /dev/null
@@ -1,6 +0,0 @@
-RUN: %cpp_compiler %S/MinimizeCrashCustomMutatorTest.cpp -o %t-MinimizeCrashCustomMutatorTest
-RUN: mkdir -p %t.dir
-RUN: echo -n '!2345678' > %t.dir/crash
-RUN: %run %t-MinimizeCrashCustomMutatorTest -minimize_crash=1 -mutate_depth=5 -runs=10 %t.dir/crash 2>&1 | FileCheck %s
-CHECK-NOT: MUTATOR: size mismatch
-CHECK: CRASH_MIN: failed to minimize beyond {{.*}}crash (8 bytes), exiting
>From 62ad9703c15d271c94fb6f43747ad721d6c8b434 Mon Sep 17 00:00:00 2001
From: Stan Ulbrych <stan at python.org>
Date: Tue, 15 Sep 2026 17:19:04 +0100
Subject: [PATCH 3/4] Remove `XFAIL` from test.
---
compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test | 1 -
1 file changed, 1 deletion(-)
diff --git a/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test b/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
index 4450639a8e197..d8ce78b10c0a9 100644
--- a/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
+++ b/compiler-rt/test/fuzzer/minimize_crash_custom_mutator.test
@@ -1,4 +1,3 @@
-XFAIL: *
RUN: %cpp_compiler %S/MinimizeCrashCustomMutatorTest.cpp -o %t-MinimizeCrashCustomMutatorTest
RUN: mkdir -p %t.dir
RUN: echo -n '!2345678' > %t.dir/crash
>From a7cbb84a0420f993c6c64370ac492f3ec9708c17 Mon Sep 17 00:00:00 2001
From: Stan Ulbrych <stan at python.org>
Date: Tue, 15 Sep 2026 19:14:19 +0100
Subject: [PATCH 4/4] Refactor to update Size at the end of the loop
Co-authored-by: Vitaly Buka <vitalybuka at google.com>
---
compiler-rt/lib/fuzzer/FuzzerLoop.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
index 47c9b92981238..5511dfa8cf268 100644
--- a/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerLoop.cpp
@@ -926,11 +926,11 @@ void Fuzzer::MinimizeCrashLoop(const Unit &U) {
for (int i = 0; i < Options.MutateDepth; i++) {
size_t NewSize = MD.Mutate(CurrentUnitData, Size, MaxMutationLen);
assert(NewSize > 0 && NewSize <= MaxMutationLen);
- Size = NewSize;
- ExecuteCallback(CurrentUnitData, Size);
- PrintPulseAndReportSlowInput(CurrentUnitData, Size);
- TryDetectingAMemoryLeak(CurrentUnitData, Size,
+ ExecuteCallback(CurrentUnitData, NewSize);
+ PrintPulseAndReportSlowInput(CurrentUnitData, NewSize);
+ TryDetectingAMemoryLeak(CurrentUnitData, NewSize,
/*DuringInitialCorpusExecution*/ false);
+ Size = NewSize;
}
}
}
More information about the llvm-commits
mailing list