Skip to content

Commit f128213

Browse files
committed
Update vendored LibFuzzer to LLVM 20.x release
Specifically: * Tag: `llvmorg-20.1.7` * Commit: `6146a88f60492b520a36f8f8f3231e15f3cc6082` Major changes: * Clarified behavior of `-max_len` option * Avoid UBSan faults in LibFuzzer by guarding `memcpy()` calls to check for valid size argument * Fix building LibFuzzer with `clang-cl` * Thread name setting logic for Fuchsia and Windows Full commit log: `git log --format=ref release/19.x..release/20.x -- 'compiler-rt/lib/fuzzer/'` ``` 091741a880c2 ([libfuzzer] Clarify -max_len behavior on bigger files (#123095), 2025-01-23) f9125ddc1faa (Revert "[libfuzzer] use timer_create() instead of setitimer() for linux" (#115811), 2024-11-11) 3b29a8a00809 ([libfuzzer] use timer_create() instead of setitimer() for linux (#110274), 2024-11-12) 5082acce4fd3 ([compiler-rt] Add custom libc++ workaround for CMake < 3.26, 2024-11-10) 87f4bc0acad6 ([compiler-rt] [fuzzer] Skip trying to set the thread name on MinGW (#115167), 2024-11-07) e7bad34475e2 ([compiler-rt] Use installed libc++(abi) for tests instead of build tree, 2024-11-06) a6fdfefbd04d ([compiler-rt] Include stdlib.h for exit() (#115025), 2024-11-05) d54953ef472b ([fuzzer] fix clang-cl build fuzzer lit test failure (#112339), 2024-10-17) b4130bee6bfd (Fix libFuzzer not building with pthreads on Windows (#109525), 2024-09-24) b32dc677325c (Revert "[compiler-rt][fuzzer] SetThreadName build fix for Mingwin attempt (#106902)", 2024-09-02) 7c4cffd9d8be ([compiler-rt][fuzzer] SetThreadName build fix for Mingwin attempt (#106902), 2024-09-01) f47966b1de45 ([compiler-rt] Reland "SetThreadName implementation for Fuchsia" (#105179), 2024-08-21) ddaa8284f5b4 (Revert "[compiler-rt][fuzzer] implements SetThreadName for fuchsia." (#105162), 2024-08-20) 31cc4ccdea92 ([compiler-rt][fuzzer] implements SetThreadName for fuchsia. (#99953), 2024-08-20) bde4ffe75214 (Don't pass null pointers to memcmp and memcpy in libFuzzer (#96775), 2024-08-13) 7202fe582931 ([compiler-rt] Silence warnings, 2024-08-11) ```
1 parent bcfa5f8 commit f128213

10 files changed

+45
-31
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Released YYYY-MM-DD.
88

99
### Changed
1010

11-
* TODO (or remove section if none)
11+
* Updated to `libFuzzer` commit `6146a88f6049` (`release/20.x`).
1212

1313
### Deprecated
1414

libfuzzer/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ if(OS_NAME MATCHES "Android|Linux|Fuchsia" AND
166166
-DLIBCXX_ABI_NAMESPACE=__Fuzzer
167167
-DLIBCXX_ENABLE_EXCEPTIONS=OFF)
168168
target_compile_options(RTfuzzer.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
169-
add_dependencies(RTfuzzer.${arch} libcxx_fuzzer_${arch}-build)
169+
add_dependencies(RTfuzzer.${arch} libcxx_fuzzer_${arch}-install-cmake326-workaround)
170170
target_compile_options(RTfuzzer_main.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
171-
add_dependencies(RTfuzzer_main.${arch} libcxx_fuzzer_${arch}-build)
171+
add_dependencies(RTfuzzer_main.${arch} libcxx_fuzzer_${arch}-install-cmake326-workaround)
172172
target_compile_options(RTfuzzer_interceptors.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
173-
add_dependencies(RTfuzzer_interceptors.${arch} libcxx_fuzzer_${arch}-build)
173+
add_dependencies(RTfuzzer_interceptors.${arch} libcxx_fuzzer_${arch}-install-cmake326-workaround)
174174
partially_link_libcxx(fuzzer_no_main ${LIBCXX_${arch}_PREFIX} ${arch})
175175
partially_link_libcxx(fuzzer_interceptors ${LIBCXX_${arch}_PREFIX} ${arch})
176176
partially_link_libcxx(fuzzer ${LIBCXX_${arch}_PREFIX} ${arch})

libfuzzer/FuzzerDictionary.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ template <size_t kMaxSizeT> class FixedWord {
2929
static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
3030
"FixedWord::kMaxSizeT cannot fit in a uint8_t.");
3131
assert(S <= kMaxSize);
32-
memcpy(Data, B, S);
32+
// memcpy cannot take null pointer arguments even if Size is 0.
33+
if (S)
34+
memcpy(Data, B, S);
3335
Size = static_cast<uint8_t>(S);
3436
}
3537

libfuzzer/FuzzerExtFunctionsWindows.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "FuzzerExtFunctions.h"
1616
#include "FuzzerIO.h"
17+
#include <stdlib.h>
1718

1819
using namespace fuzzer;
1920

@@ -22,6 +23,11 @@ using namespace fuzzer;
2223
#define STRINGIFY(A) STRINGIFY_(A)
2324

2425
#if LIBFUZZER_MSVC
26+
#define GET_FUNCTION_ADDRESS(fn) &fn
27+
#else
28+
#define GET_FUNCTION_ADDRESS(fn) __builtin_function_start(fn)
29+
#endif // LIBFUZER_MSVC
30+
2531
// Copied from compiler-rt/lib/sanitizer_common/sanitizer_win_defs.h
2632
#if defined(_M_IX86) || defined(__i386__)
2733
#define WIN_SYM_PREFIX "_"
@@ -31,17 +37,9 @@ using namespace fuzzer;
3137

3238
// Declare external functions as having alternativenames, so that we can
3339
// determine if they are not defined.
34-
#define EXTERNAL_FUNC(Name, Default) \
35-
__pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \
40+
#define EXTERNAL_FUNC(Name, Default) \
41+
__pragma(comment(linker, "/alternatename:" WIN_SYM_PREFIX STRINGIFY( \
3642
Name) "=" WIN_SYM_PREFIX STRINGIFY(Default)))
37-
#else
38-
// Declare external functions as weak to allow them to default to a specified
39-
// function if not defined explicitly. We must use weak symbols because clang's
40-
// support for alternatename is not 100%, see
41-
// https://bugs.llvm.org/show_bug.cgi?id=40218 for more details.
42-
#define EXTERNAL_FUNC(Name, Default) \
43-
__attribute__((weak, alias(STRINGIFY(Default))))
44-
#endif // LIBFUZZER_MSVC
4543

4644
extern "C" {
4745
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
@@ -57,20 +55,23 @@ extern "C" {
5755
}
5856

5957
template <typename T>
60-
static T *GetFnPtr(T *Fun, T *FunDef, const char *FnName, bool WarnIfMissing) {
58+
static T *GetFnPtr(void *Fun, void *FunDef, const char *FnName,
59+
bool WarnIfMissing) {
6160
if (Fun == FunDef) {
6261
if (WarnIfMissing)
6362
Printf("WARNING: Failed to find function \"%s\".\n", FnName);
6463
return nullptr;
6564
}
66-
return Fun;
65+
return (T *)Fun;
6766
}
6867

6968
namespace fuzzer {
7069

7170
ExternalFunctions::ExternalFunctions() {
72-
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
73-
this->NAME = GetFnPtr<decltype(::NAME)>(::NAME, ::NAME##Def, #NAME, WARN);
71+
#define EXT_FUNC(NAME, RETURN_TYPE, FUNC_SIG, WARN) \
72+
this->NAME = GetFnPtr<decltype(::NAME)>(GET_FUNCTION_ADDRESS(::NAME), \
73+
GET_FUNCTION_ADDRESS(::NAME##Def), \
74+
#NAME, WARN);
7475

7576
#include "FuzzerExtFunctions.def"
7677

libfuzzer/FuzzerFlags.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ FUZZER_FLAG_UNSIGNED(seed, 0, "Random seed. If 0, seed is generated.")
1414
FUZZER_FLAG_INT(runs, -1,
1515
"Number of individual test runs (-1 for infinite runs).")
1616
FUZZER_FLAG_INT(max_len, 0, "Maximum length of the test input. "
17-
"If 0, libFuzzer tries to guess a good value based on the corpus "
18-
"and reports it. ")
17+
"Contents of corpus files are going to be truncated to this value. "
18+
"If 0, libFuzzer tries to guess a good value based on the corpus "
19+
"and reports it.")
1920
FUZZER_FLAG_INT(len_control, 100, "Try generating small inputs first, "
2021
"then try larger inputs over time. Specifies the rate at which the length "
2122
"limit is increased (smaller == faster). If 0, immediately try inputs with "

libfuzzer/FuzzerLoop.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,9 @@ void Fuzzer::CrashOnOverwrittenData() {
579579
// Compare two arrays, but not all bytes if the arrays are large.
580580
static bool LooseMemeq(const uint8_t *A, const uint8_t *B, size_t Size) {
581581
const size_t Limit = 64;
582+
// memcmp cannot take null pointer arguments even if Size is 0.
583+
if (!Size)
584+
return true;
582585
if (Size <= 64)
583586
return !memcmp(A, B, Size);
584587
// Compare first and last Limit/2 bytes.
@@ -596,7 +599,9 @@ ATTRIBUTE_NOINLINE bool Fuzzer::ExecuteCallback(const uint8_t *Data,
596599
// We copy the contents of Unit into a separate heap buffer
597600
// so that we reliably find buffer overflows in it.
598601
uint8_t *DataCopy = new uint8_t[Size];
599-
memcpy(DataCopy, Data, Size);
602+
// memcpy cannot take null pointer arguments even if Size is 0.
603+
if (Size)
604+
memcpy(DataCopy, Data, Size);
600605
if (EF->__msan_unpoison)
601606
EF->__msan_unpoison(DataCopy, Size);
602607
if (EF->__msan_unpoison_param)

libfuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,11 @@ size_t PageSize() {
607607
}
608608

609609
void SetThreadName(std::thread &thread, const std::string &name) {
610-
// TODO ?
610+
if (zx_status_t s = zx_object_set_property(
611+
thread.native_handle(), ZX_PROP_NAME, name.data(), name.size());
612+
s != ZX_OK)
613+
Printf("SetThreadName for name %s failed: %s", name.c_str(),
614+
zx_status_get_string(s));
611615
}
612616

613617
} // namespace fuzzer

libfuzzer/FuzzerUtilWindows.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,15 @@ size_t PageSize() {
239239
}
240240

241241
void SetThreadName(std::thread &thread, const std::string &name) {
242-
#if defined(_LIBCPP_HAS_THREAD_API_PTHREAD) || \
243-
defined(_GLIBCXX_GCC_GTHR_POSIX_H)
244-
(void)pthread_setname_np(thread.native_handle(), name.c_str());
245-
#else
242+
#ifndef __MINGW32__
243+
// Not setting the thread name in MinGW environments. MinGW C++ standard
244+
// libraries can either use native Windows threads or pthreads, so we
245+
// don't know with certainty what kind of thread handle we're getting
246+
// from thread.native_handle() here.
246247
typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
247248
HMODULE kbase = GetModuleHandleA("KernelBase.dll");
248-
proc ThreadNameProc =
249-
reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
249+
proc ThreadNameProc = reinterpret_cast<proc>(
250+
(void *)GetProcAddress(kbase, "SetThreadDescription"));
250251
if (ThreadNameProc) {
251252
std::wstring buf;
252253
auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);

libfuzzer/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
6464
COMPILER_RT_LIBCXX_PATH AND
6565
COMPILER_RT_LIBCXXABI_PATH)
6666
file(GLOB libfuzzer_headers ../*.h)
67-
set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch}-build ${libfuzzer_headers})
67+
set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch}-install-cmake326-workaround ${libfuzzer_headers})
6868
set(LIBFUZZER_TEST_RUNTIME_CFLAGS -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
6969
set(LIBFUZZER_TEST_RUNTIME_LINK_FLAGS ${LIBCXX_${arch}_PREFIX}/lib/libc++.a)
7070
endif()

update-libfuzzer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -ex
88

99
# The LLVM commit from which we are vendoring libfuzzer. This must be a commit
1010
# hash from https://github.com/llvm/llvm-project
11-
COMMIT=ab51eccf88f5321e7c60591c5546b254b6afab99
11+
COMMIT=6146a88f60492b520a36f8f8f3231e15f3cc6082
1212

1313
cd "$(dirname $0)"
1414
project_dir="$(pwd)"

0 commit comments

Comments
 (0)