Skip to content

Commit

Permalink
Replace uses of dlsym and weak symbols
Browse files Browse the repository at this point in the history
Neither is directly supported on Windows, but also not required: We can
simply not add these symbols if we are linking against a sanitizer.
  • Loading branch information
fmeum committed Oct 15, 2021
1 parent 50d1f37 commit 554bfe4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions driver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ cc_17_library(

cc_binary(
name = "jazzer_driver",
srcs = [
# Defines symbols otherwise defined by sanitizers to prevent linker
# errors and print JVM stack traces.
# Windows-compatible replacement for __attribute__((weak)).
"sanitizer_symbols.cpp",
],
data = [
"//agent:jazzer_agent_deploy.jar",
],
Expand Down
22 changes: 5 additions & 17 deletions driver/libfuzzer_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "libfuzzer_driver.h"

#include <dlfcn.h>

#include <algorithm>
#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -44,26 +42,16 @@ DECLARE_string(id_sync_file);
// Defined in fuzz_target_runner.cpp
DECLARE_string(coverage_report);

// This symbol is defined by sanitizers if linked into Jazzer or in
// sanitizer_symbols.cpp if no sanitizer is used.
extern "C" void __sanitizer_set_death_callback(void (*)());

// We apply a patch to libFuzzer to make it call this function instead of
// __sanitizer_set_death_callback to pass us the death callback.
extern "C" [[maybe_unused]] void __jazzer_set_death_callback(
void (*callback)()) {
jazzer::AbstractLibfuzzerDriver::libfuzzer_print_crashing_input_ = callback;
void *sanitizer_set_death_callback =
dlsym(RTLD_DEFAULT, "__sanitizer_set_death_callback");
if (sanitizer_set_death_callback != nullptr)
reinterpret_cast<void (*)(void (*)())>(sanitizer_set_death_callback)(
callback);
}

// Suppress libFuzzer warnings about missing sanitizer methods in non-ASan
// builds.
extern "C" __attribute__((weak)) int __sanitizer_acquire_crash_state() {
return true;
}

extern "C" __attribute__((weak)) void __sanitizer_print_stack_trace() {
jazzer::DumpJvmStackTraces();
__sanitizer_set_death_callback(callback);
}

namespace {
Expand Down
29 changes: 29 additions & 0 deletions driver/sanitizer_symbols.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 Code Intelligence GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Called in libfuzzer_driver.cpp.
extern "C" void __sanitizer_set_death_callback(void (*)()) {}

// Suppress libFuzzer warnings about missing sanitizer methods in non-sanitizer
// builds.
extern "C" int __sanitizer_acquire_crash_state() { return 1; }

namespace jazzer {
void DumpJvmStackTraces();
}

// Dump a JVM stack trace on timeouts.
extern "C" void __sanitizer_print_stack_trace() {
jazzer::DumpJvmStackTraces();
}

0 comments on commit 554bfe4

Please sign in to comment.