• 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 <jni.h>
16 
17 #include <atomic>
18 #include <csignal>
19 
20 #include "com_code_intelligence_jazzer_driver_SignalHandler.h"
21 
22 #ifdef _WIN32
23 // Windows does not have SIGUSR1, which triggers a graceful exit of libFuzzer.
24 // Instead, trigger a hard exit.
25 #define SIGUSR1 SIGTERM
26 #endif
27 
28 // Handles SIGINT raised while running Java code.
29 [[maybe_unused]] void
Java_com_code_1intelligence_jazzer_driver_SignalHandler_handleInterrupt(JNIEnv *,jclass)30 Java_com_code_1intelligence_jazzer_driver_SignalHandler_handleInterrupt(
31     JNIEnv *, jclass) {
32   static std::atomic<bool> already_exiting{false};
33   if (!already_exiting.exchange(true)) {
34     // Let libFuzzer exit gracefully when the JVM received SIGINT.
35     raise(SIGUSR1);
36   } else {
37     // Exit libFuzzer forcefully on repeated SIGINTs.
38     raise(SIGTERM);
39   }
40 }
41