• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <iostream>
16 
17 #include "libfuzzer_driver.h"
18 
19 namespace {
20 bool is_asan_active = false;
21 }
22 
23 extern "C" {
__asan_default_options()24 const char *__asan_default_options() {
25   is_asan_active = true;
26   // LeakSanitizer is not yet supported as it reports too many false positives
27   // due to how the JVM GC works.
28   // We use a distinguished exit code to recognize ASan crashes in tests.
29   // Also specify abort_on_error=0 explicitly since ASan aborts rather than
30   // exits on macOS by default, which would cause our exit code to be ignored.
31   return "abort_on_error=0,detect_leaks=0,exitcode=76";
32 }
33 
__ubsan_default_options()34 const char *__ubsan_default_options() {
35   // We use a distinguished exit code to recognize UBSan crashes in tests.
36   // Also specify abort_on_error=0 explicitly since UBSan aborts rather than
37   // exits on macOS by default, which would cause our exit code to be ignored.
38   return "abort_on_error=0,exitcode=76";
39 }
40 }
41 
42 namespace {
43 using Driver = jazzer::LibfuzzerDriver;
44 
45 std::unique_ptr<Driver> gLibfuzzerDriver;
46 }  // namespace
47 
driver_cleanup()48 extern "C" void driver_cleanup() {
49   // Free the libfuzzer driver which triggers a clean JVM shutdown.
50   gLibfuzzerDriver.reset(nullptr);
51 }
52 
53 // Entry point called by libfuzzer before any LLVMFuzzerTestOneInput(...)
54 // invocations.
LLVMFuzzerInitialize(int * argc,char *** argv)55 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
56   if (is_asan_active) {
57     std::cerr << "WARN: Jazzer is not compatible with LeakSanitizer yet. Leaks "
58                  "are not reported."
59               << std::endl;
60   }
61   gLibfuzzerDriver = std::make_unique<Driver>(argc, argv);
62   std::atexit(&driver_cleanup);
63   return 0;
64 }
65 
66 // Called by the fuzzer for every fuzzing input.
LLVMFuzzerTestOneInput(const uint8_t * data,const size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, const size_t size) {
68   auto result = gLibfuzzerDriver->TestOneInput(data, size);
69   if (result != jazzer::RunResult::kOk) {
70     // Fuzzer triggered an exception or assertion in Java code. Skip the
71     // uninformative libFuzzer stack trace.
72     std::cerr << "== libFuzzer crashing input ==\n";
73     Driver::libfuzzer_print_crashing_input_();
74     // DumpReproducer needs to be called after libFuzzer printed its final
75     // stats as otherwise it would report incorrect coverage.
76     gLibfuzzerDriver->DumpReproducer(data, size);
77     if (result == jazzer::RunResult::kDumpAndContinue) {
78       // Continue fuzzing after printing the crashing input.
79       return 0;
80     }
81     // Exit directly without invoking libFuzzer's atexit hook.
82     driver_cleanup();
83     _Exit(Driver::kErrorExitCode);
84   }
85   return 0;
86 }
87