Skip to content

Commit 6947ccb

Browse files
authored
Merge pull request #123 from fitzgen/update-libfuzzer-vendoring
Update how we do libfuzzer vendoring
2 parents c8275d1 + 1a23bcd commit 6947ccb

10 files changed

+72
-26
lines changed

.github/workflows/rust.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ name: Rust
33
on: [push, pull_request]
44

55
jobs:
6+
check_libfuzzer_checkout:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Re-vendor libfuzzer
12+
run: ./update-libfuzzer.sh
13+
- name: Check that nothing changed
14+
run: git diff --exit-code
15+
616
build:
717

818
runs-on: ubuntu-latest
919

1020
steps:
11-
- uses: actions/checkout@v1
21+
- uses: actions/checkout@v4
1222

1323
- name: Install nightly Rust
1424
run: |

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,18 @@ Then link to your own runtime in your `build.rs`.
8686

8787
## Updating libfuzzer from upstream
8888

89-
```
90-
./update-libfuzzer.sh <github.com/llvm-mirror/llvm-project SHA1>
91-
```
89+
* Update the `COMMIT=...` variable in `./update-libfuzzer.sh` with the new
90+
commit hash from [llvm-mirror/llvm-project](github.com/llvm-mirror/llvm-project)
91+
that you are vendoring.
92+
93+
* Re-run the script:
94+
95+
```
96+
$ ./update-libfuzzer.sh <github.com/llvm-mirror/llvm-project SHA1>
97+
```
9298

9399
## License
94100

95-
All files in `libfuzzer` directory are licensed NCSA.
101+
All files in the `libfuzzer` directory are licensed NCSA.
96102

97103
Everything else is dual-licensed Apache 2.0 and MIT.

libfuzzer/FuzzerDriver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static void PulseThread() {
229229

230230
static void WorkerThread(const Command &BaseCmd, std::atomic<unsigned> *Counter,
231231
unsigned NumJobs, std::atomic<bool> *HasErrors) {
232+
ScopedDisableMsanInterceptorChecks S;
232233
while (true) {
233234
unsigned C = (*Counter)++;
234235
if (C >= NumJobs) break;

libfuzzer/FuzzerFork.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ void FuzzWithFork(Random &Rand, const FuzzingOptions &Options,
349349
&NewFeatures, Env.Cov, &NewCov, CFPath,
350350
/*Verbose=*/false, /*IsSetCoverMerge=*/false);
351351
Env.Features.insert(NewFeatures.begin(), NewFeatures.end());
352-
Env.Cov.insert(NewFeatures.begin(), NewFeatures.end());
352+
Env.Cov.insert(NewCov.begin(), NewCov.end());
353353
RemoveFile(CFPath);
354354
}
355355

libfuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void CrashHandler() {
292292
zx_wait_item_t WaitItems[] = {
293293
{
294294
.handle = SignalHandlerEvent,
295-
.waitfor = ZX_SIGNAL_HANDLE_CLOSED,
295+
.waitfor = ZX_USER_SIGNAL_1,
296296
.pending = 0,
297297
},
298298
{
@@ -378,10 +378,11 @@ void CrashHandler() {
378378
}
379379

380380
void StopSignalHandler() {
381-
_zx_handle_close(SignalHandlerEvent);
381+
_zx_object_signal(SignalHandlerEvent, 0, ZX_USER_SIGNAL_1);
382382
if (SignalHandler.joinable()) {
383383
SignalHandler.join();
384384
}
385+
_zx_handle_close(SignalHandlerEvent);
385386
}
386387

387388
} // namespace

libfuzzer/FuzzerUtilLinux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void SetThreadName(std::thread &thread, const std::string &name) {
4444
#if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
4545
(void)pthread_setname_np(thread.native_handle(), name.c_str());
4646
#elif LIBFUZZER_NETBSD
47-
(void)pthread_set_name_np(thread.native_handle(), "%s", name.c_str());
47+
(void)pthread_setname_np(thread.native_handle(), "%s", const_cast<char *>(name.c_str()));
4848
#endif
4949
}
5050

libfuzzer/FuzzerUtilWindows.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@
2121
#include <signal.h>
2222
#include <stdio.h>
2323
#include <sys/types.h>
24+
// clang-format off
2425
#include <windows.h>
25-
26-
// This must be included after windows.h.
26+
// These must be included after windows.h.
27+
// archicture need to be set before including
28+
// libloaderapi
29+
#include <libloaderapi.h>
30+
#include <stringapiset.h>
2731
#include <psapi.h>
32+
// clang-format on
2833

2934
namespace fuzzer {
3035

@@ -234,8 +239,25 @@ size_t PageSize() {
234239
}
235240

236241
void SetThreadName(std::thread &thread, const std::string &name) {
237-
// TODO ?
238-
// to UTF-8 then SetThreadDescription ?
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
246+
typedef HRESULT(WINAPI * proc)(HANDLE, PCWSTR);
247+
HMODULE kbase = GetModuleHandleA("KernelBase.dll");
248+
proc ThreadNameProc =
249+
reinterpret_cast<proc>(GetProcAddress(kbase, "SetThreadDescription"));
250+
if (ThreadNameProc) {
251+
std::wstring buf;
252+
auto sz = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
253+
if (sz > 0) {
254+
buf.resize(sz);
255+
if (MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, &buf[0], sz) > 0) {
256+
(void)ThreadNameProc(thread.native_handle(), buf.c_str());
257+
}
258+
}
259+
}
260+
#endif
239261
}
240262

241263
} // namespace fuzzer

libfuzzer/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
LIBFUZZER_SRC_DIR=$(dirname $0)
33
CXX="${CXX:-clang}"
44
for f in $LIBFUZZER_SRC_DIR/*.cpp; do
5-
$CXX -g -O2 -fno-omit-frame-pointer -std=c++14 $f -c &
5+
$CXX -g -O2 -fno-omit-frame-pointer -std=c++17 $f -c &
66
done
77
wait
88
rm -f libFuzzer.a

libfuzzer/tests/CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ if (APPLE)
1212
endif()
1313

1414
add_custom_target(FuzzerUnitTests)
15-
set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
15+
set_target_properties(FuzzerUnitTests PROPERTIES FOLDER "Compiler-RT/Tests")
1616

1717
add_custom_target(FuzzedDataProviderUnitTests)
18-
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES FOLDER "Compiler-RT Tests")
18+
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES FOLDER "Compiler-RT/Tests")
1919

2020
set(LIBFUZZER_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_LINK_FLAGS})
2121
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS --driver-mode=g++)
@@ -58,7 +58,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
5858
${LIBFUZZER_TEST_RUNTIME_OBJECTS})
5959
set_target_properties(${LIBFUZZER_TEST_RUNTIME} PROPERTIES
6060
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
61-
FOLDER "Compiler-RT Runtime tests")
61+
FOLDER "Compiler-RT/Tests/Runtime")
6262

6363
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND
6464
COMPILER_RT_LIBCXX_PATH AND
@@ -74,7 +74,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
7474
FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
7575
SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
7676
RUNTIME ${LIBFUZZER_TEST_RUNTIME}
77-
DEPS llvm_gtest ${LIBFUZZER_TEST_RUNTIME_DEPS}
77+
DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS}
7878
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
7979
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
8080
set_target_properties(FuzzerUnitTests PROPERTIES
@@ -84,7 +84,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST FUZZER_SUPPORTED_ARCH)
8484
generate_compiler_rt_tests(FuzzedDataProviderTestObjects
8585
FuzzedDataProviderUnitTests "FuzzerUtils-${arch}-Test" ${arch}
8686
SOURCES FuzzedDataProviderUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
87-
DEPS llvm_gtest ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
87+
DEPS ${LIBFUZZER_TEST_RUNTIME_DEPS} ${COMPILER_RT_SOURCE_DIR}/include/fuzzer/FuzzedDataProvider.h
8888
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
8989
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
9090
set_target_properties(FuzzedDataProviderUnitTests PROPERTIES

update-libfuzzer.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
# Usage:
44
#
5-
# ./update-libfuzzer $commit_hash
6-
#
7-
# Where `$commit_hash` is a commit hash from
8-
# https://github.com/llvm-mirror/llvm-project
5+
# $ ./update-libfuzzer.sh
96

107
set -ex
118

9+
# The LLVM commit from which we are vendoring libfuzzer. This must be a commit
10+
# hash from https://github.com/llvm/llvm-project
11+
COMMIT=ab51eccf88f5321e7c60591c5546b254b6afab99
12+
1213
cd "$(dirname $0)"
1314
project_dir="$(pwd)"
1415

1516
tmp_dir="$(mktemp -d)"
16-
17-
git clone https://github.com/llvm/llvm-project.git "$tmp_dir"
1817
cd "$tmp_dir"
19-
git checkout "$1"
18+
19+
git init
20+
git remote add llvm https://github.com/llvm/llvm-project.git
21+
git sparse-checkout set compiler-rt/lib/fuzzer
22+
23+
git fetch --depth 1 llvm "$COMMIT" --filter=blob:none
24+
git checkout "$COMMIT"
25+
2026
rm -rf "$project_dir/libfuzzer/"
2127
mv "$tmp_dir/compiler-rt/lib/fuzzer/" "$project_dir/libfuzzer/"

0 commit comments

Comments
 (0)