Skip to content

Commit

Permalink
Update libfuzzer code to compiler-rt 19
Browse files Browse the repository at this point in the history
Update the LLVM compiler-rt C++ code to the top of the 19.x branch

The summary of the changes from compiler-rt 18 (the last time this code
was updated) are:

1. Preventing MSan false positives in fork mode
2. Fix incorrect coverage number in fork mode
3. C++ code is now built with C++17
4. Worker thread name setting imporvements (on NetBSD and Windows)
5. Improvements to signal handling in Fuchsia

The LLVM commits which make up the changes in this commit come from the
squash of the following LLVM commits:

```sh
git log --format=reference origin/release/18.x..origin/release/19.x -- compiler-rt/lib/fuzzer/
```

```
83251a22f623 ([libFuzzer] Fix incorrect coverage number in fork mode (#82335), 2024-07-18)
b4b17d97637b (Revert "[compiler-rt] Silence function cast warning when building with Clang ToT targetting Windows", 2024-07-06)
10e1b935e5d9 ([compiler-rt] Silence function cast warning when building with Clang ToT targetting Windows, 2024-07-05)
a35ac42fac88 ([compiler-rt] Revise IDE folder structure (#89753), 2024-06-04)
d9ce33a0eea7 ([libfuzzer] Prevent MSan false positive when printing log with -jobs (#91679), 2024-05-10)
791161516f48 ([compiler-rt] Update libFuzzer build script to use C++17. (#89604), 2024-04-24)
55b90b5140a2 ([compiler-rt] Remove llvm_gtest dependency from unit tests, 2024-03-13)
e371ada409b2 ([compiler-rt] reimplements GetMemoryProfile for netbsd. (#84841), 2024-03-13)
e932fe880b69 ([compiler-rt][Fuzzer] fix windows typo (#84407), 2024-03-08)
8bf8d36f8e82 ([compiler-rt][fuzzer] Reland "SetThreadName windows implementation" (#83562), 2024-03-07)
d1538c15f9c6 (Revert fuzzer windows changes (#83551), 2024-03-01)
062d78ef58ac ([compiler-rt][fuzzer] windows build unbreak proposal. (#83538), 2024-03-01)
2cdf611c0239 ([compiler-rt][Fuzzer] SetThreadName windows implementation new try. (#76761), 2024-03-01)
7f3980a7b2c9 ([Fuzzer] Use user signal to coordinate handler shutdown (#82067), 2024-02-20)
```
  • Loading branch information
rchildre3 committed Sep 22, 2024
1 parent c8275d1 commit 9a30557
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Released YYYY-MM-DD.
### Changed

* Updated to `libFuzzer` commit `3747cde5e84f` (`release/18.x`).
* Updated to `libFuzzer` commit `83251a22f623` (`release/19.x`).

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions libfuzzer/FuzzerDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ static void PulseThread() {

static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter,
unsigned NumJobs, std::atomic<bool> *HasErrors) {
ScopedDisableMsanInterceptorChecks S;
while (true) {
unsigned C = (*Counter)++;
if (C >= NumJobs) break;
Expand Down
2 changes: 1 addition & 1 deletion libfuzzer/FuzzerFork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
&NewFeatures, Env.Cov, &NewCov, CFPath,
/*Verbose=*/false, /*IsSetCoverMerge=*/false);
Env.Features.insert(NewFeatures.begin(), NewFeatures.end());
Env.Cov.insert(NewFeatures.begin(), NewFeatures.end());
Env.Cov.insert(NewCov.begin(), NewCov.end());
RemoveFile(CFPath);
}

Expand Down
5 changes: 3 additions & 2 deletions libfuzzer/FuzzerUtilFuchsia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void CrashHandler() {
zx_wait_item_t WaitItems[] = {
{
.handle = SignalHandlerEvent,
.waitfor = ZX_SIGNAL_HANDLE_CLOSED,
.waitfor = ZX_USER_SIGNAL_1,
.pending = 0,
},
{
Expand Down Expand Up @@ -378,10 +378,11 @@ void CrashHandler() {
}

void StopSignalHandler() {
_zx_handle_close(SignalHandlerEvent);
_zx_object_signal(SignalHandlerEvent, 0, ZX_USER_SIGNAL_1);
if (SignalHandler.joinable()) {
SignalHandler.join();
}
_zx_handle_close(SignalHandlerEvent);
}

} // namespace
Expand Down
2 changes: 1 addition & 1 deletion libfuzzer/FuzzerUtilLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void SetThreadName(std::thread &thread, const std::string &name) {
#if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
(void)pthread_setname_np(thread.native_handle(), name.c_str());
#elif LIBFUZZER_NETBSD
(void)pthread_set_name_np(thread.native_handle(), "%s", name.c_str());
(void)pthread_setname_np(thread.native_handle(), "%s", const_cast<char *>(name.c_str()));
#endif
}

Expand Down
25 changes: 21 additions & 4 deletions libfuzzer/FuzzerUtilWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
// clang-format off
#include <windows.h>

// This must be included after windows.h.
// These must be included after windows.h.
// archicture need to be set before including
// libloaderapi
#include <libloaderapi.h>
#include <stringapiset.h>
#include <psapi.h>
// clang-format on

namespace fuzzer {

Expand Down Expand Up @@ -234,8 +239,20 @@ size_t PageSize() {
}

void SetThreadName(std::thread &thread, const std::string &name) {
// TODO ?
// to UTF-8 then SetThreadDescription ?
typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
HMODULE kbase = GetModuleHandleA("KernelBase.dll");
proc ThreadNameProc =
reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
if (ThreadNameProc) {
std::wstring buf;
auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
if (sz > 0) {
buf.resize(sz);
if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
(void)ThreadNameProc(thread.native_handle(), buf.c_str());
}
}
}
}

} // namespace fuzzer
Expand Down
2 changes: 1 addition & 1 deletion libfuzzer/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
LIBFUZZER_SRC_DIR=$(dirname $0)
CXX="${CXX:-clang}"
for f in $LIBFUZZER_SRC_DIR/*.cpp; do
$CXX -g -O2 -fno-omit-frame-pointer -std=c++14 $f -c &
$CXX -g -O2 -fno-omit-frame-pointer -std=c++17 $f -c &
done
wait
rm -f libFuzzer.a
Expand Down
10 changes: 5 additions & 5 deletions libfuzzer/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ if (APPLE)
endif()

add_custom_target(FuzzerUnitTests)
set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT/Tests")

add_custom_target(FuzzedDataProviderUnitTests)
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES FOLDER "Compiler-RT/Tests")

set(LIBFUZZER_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS})
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS --driver-mode=g++)
Expand Down Expand Up @@ -58,7 +58,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
${LIBFUZZER_TEST_RUNTIME_OBJECTS})
set_target_properties(${LIBFUZZER_TEST_RUNTIME} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
FOLDER "Compiler-RT Runtime tests")
FOLDER "Compiler-RT/Tests/Runtime")

if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
COMPILER_RT_LIBCXX_PATH AND
Expand All @@ -74,7 +74,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
RUNTIME ${LIBFUZZER_TEST_RUNTIME}
DEPS llvm_gtest ${LIBFUZZER_TEST_RUNTIME_DEPS}
DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS}
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
set_target_properties(FuzzerUnitTests PROPERTIES
Expand All @@ -84,7 +84,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
generate_compiler_rt_tests(FuzzedDataProviderTestObjects
FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
DEPS llvm_gtest ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES
Expand Down

0 comments on commit 9a30557

Please sign in to comment.