1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stddef.h>
6 #include <stdint.h>
7 #include <string.h>
8 
9 #include <signal.h>
10 #include <unistd.h>
11 
12 #include "zlib.h"
13 
14 static Bytef buffer[256 * 1024] = { 0 };
15 
16 
17 #ifdef INTENTIONAL_STARTUP_CRASH
bad_term_handler(int signum)18 void bad_term_handler(int signum) {
19   _exit(0);
20 }
21 #endif
22 
23 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
25 #ifdef INTENTIONAL_STARTUP_CRASH
26   // Simulates the worst case, fuzz target silently dies without any error.
27   struct sigaction action = { 0 };
28   action.sa_handler = bad_term_handler;
29   sigaction(SIGTERM, &action, NULL);
30 
31   // Cannot call _exit(0) directly, as it's even worse -- sancov does not print
32   // any coverage information in that case.
33   kill(getpid(), SIGTERM);
34 #endif
35 
36   uLongf buffer_length = static_cast<uLongf>(sizeof(buffer));
37   if (Z_OK != uncompress(buffer, &buffer_length, data,
38                          static_cast<uLong>(size))) {
39     return 0;
40   }
41   return 0;
42 }
43