| 1 |
/* Copyright 2021 Google LLC |
| 2 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
you may not use this file except in compliance with the License. |
| 4 |
You may obtain a copy of the License at |
| 5 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
Unless required by applicable law or agreed to in writing, software |
| 7 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 8 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 |
See the License for the specific language governing permissions and |
| 10 |
limitations under the License. |
| 11 |
*/ |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
#include <sys/types.h> |
| 17 |
#include <unistd.h> |
| 18 |
|
| 19 |
/* |
| 20 |
* Libdwarf library callers can only use these headers. |
| 21 |
*/ |
| 22 |
#include "dwarf.h" |
| 23 |
#include "libdwarf.h" |
| 24 |
|
| 25 |
/* |
| 26 |
* A fuzzer that simulates a small part of the simplereader.c example. |
| 27 |
*/ |
| 28 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 29 |
char filename[256]; |
| 30 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 31 |
|
| 32 |
FILE *fp = fopen(filename, "wb"); |
| 33 |
if (!fp) { |
| 34 |
return 0; |
| 35 |
} |
| 36 |
fwrite(data, size, 1, fp); |
| 37 |
fclose(fp); |
| 38 |
Dwarf_Ptr errarg = 0; |
| 39 |
Dwarf_Handler errhand = 0; |
| 40 |
Dwarf_Debug dbg = 0; |
| 41 |
Dwarf_Error *errp = NULL; |
| 42 |
#define MACHO_PATH_LEN 2000 |
| 43 |
char macho_real_path[2000]; |
| 44 |
dwarf_init_path(filename, macho_real_path, MACHO_PATH_LEN, DW_GROUPNUMBER_ANY, |
| 45 |
errhand, errarg, &dbg, errp); |
| 46 |
|
| 47 |
dwarf_finish(dbg); |
| 48 |
static const char *glpath[1] = {"/usr/include/c++/9/debug"}; |
| 49 |
unsigned char path_source = 0; |
| 50 |
dwarf_init_path_dl(filename, NULL, 256, DW_GROUPNUMBER_ANY, errhand, errarg, |
| 51 |
&dbg, (char **)glpath, 1, &path_source, errp); |
| 52 |
dwarf_finish(dbg); |
| 53 |
unlink(filename); |
| 54 |
return 0; |
| 55 |
} |