Skip to content

Commit

Permalink
Move the libfuzzer linkopts to the stub cc_library, such that only th…
Browse files Browse the repository at this point in the history
…e top-level fuzz test is instrumented. (#89)

* Move the libfuzzer linkopts to the stub cc_library, such that only the top-level fuzz test is instrumented.

Without this change, any other binary that may be built on the transitive dependency chain will also get linked with libFuzzer, resulting in a linker error (duplicate main functions).

* Fix libFuzzer's copts.

* Fix typo in stub output.
  • Loading branch information
stefanbucur committed Dec 8, 2020
1 parent 15b5ccc commit 501f20f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions fuzzing/engines/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ cc_fuzzing_engine(
cc_library(
name = "libfuzzer_stub",
srcs = ["libfuzzer_stub.cc"],
linkopts = [
# We add the linker options to the library, so only the fuzz test binary
# is linked with libfuzzer.
"-fsanitize=fuzzer",
],
)

# Honggfuzz specification.
Expand Down
15 changes: 13 additions & 2 deletions fuzzing/engines/libfuzzer_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// TODO(sbucur): Implement here weak main() function that prints an error
// signaling that the real libFuzzer engine was not linked correctly.
#include <cstdio>
#include <cstdlib>

extern "C" int main(int argc, char** argv) __attribute__((weak));

int main(int argc, char** argv) {
fprintf(stderr, "*** ERROR *** This is a stub *** ERROR ***\n");
fprintf(stderr,
" * You have attempted to build a libFuzzer fuzz test, but did not "
"link in the fuzzing engine.\n");
fprintf(stderr, " * Use the '-fsanitize=fuzzer' linker option instead.\n");
abort();
}
4 changes: 1 addition & 3 deletions fuzzing/instrum_opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ def instrumentation_opts(copts = [], linkopts = []):
# Base instrumentation applied to all fuzz test executables.
base_opts = instrumentation_opts(
copts = ["-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION"],
linkopts = [],
)

# Engine-specific instrumentation.
fuzzing_engine_opts = {
"none": instrumentation_opts(),
"libfuzzer": instrumentation_opts(
copts = ["-fsanitize=fuzzer"],
linkopts = ["-fsanitize=fuzzer"],
copts = ["-fsanitize=fuzzer-no-link"],
),
# Reflects the set of options at
# https://github.com/google/honggfuzz/blob/master/hfuzz_cc/hfuzz-cc.c
Expand Down

0 comments on commit 501f20f

Please sign in to comment.