| 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 <fcntl.h> /* open() O_RDONLY O_BINARY */ |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include "dwarf.h" |
| 21 |
#include "libdwarf.h" |
| 22 |
|
| 23 |
#ifndef O_BINARY |
| 24 |
#define O_BINARY 0 |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
/* Every return from this after dwarf_init_b() |
| 29 |
has to call |
| 30 |
dwarf_finish(dbg); |
| 31 |
close(fuzz_fd); |
| 32 |
unlink(filename); |
| 33 |
to avoid memory leaks (and close the fd, of course). */ |
| 34 |
|
| 35 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 36 |
char filename[256]; |
| 37 |
sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
| 38 |
|
| 39 |
FILE *fp = fopen(filename, "wb"); |
| 40 |
if (!fp) { |
| 41 |
return 0; |
| 42 |
} |
| 43 |
fwrite(data, size, 1, fp); |
| 44 |
fclose(fp); |
| 45 |
|
| 46 |
int fuzz_fd = 0; |
| 47 |
Dwarf_Ptr errarg = 0; |
| 48 |
Dwarf_Handler errhand = 0; |
| 49 |
Dwarf_Error *errp = NULL; |
| 50 |
Dwarf_Debug dbg = 0; |
| 51 |
|
| 52 |
fuzz_fd = open(filename, O_RDONLY|O_BINARY); |
| 53 |
if (fuzz_fd != -1) { |
| 54 |
dwarf_init_b(fuzz_fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, errp); |
| 55 |
Dwarf_Unsigned count = 0; |
| 56 |
int res = 0; |
| 57 |
Dwarf_Unsigned i = 0; |
| 58 |
|
| 59 |
res = dwarf_load_rnglists(dbg, &count, errp); |
| 60 |
if (res == DW_DLV_OK) { |
| 61 |
for (i = 0; i < count; ++i) { |
| 62 |
Dwarf_Unsigned header_offset = 0; |
| 63 |
Dwarf_Small offset_size = 0; |
| 64 |
Dwarf_Small extension_size = 0; |
| 65 |
unsigned version = 0; |
| 66 |
Dwarf_Small address_size = 0; |
| 67 |
Dwarf_Small segment_selector_size = 0; |
| 68 |
Dwarf_Unsigned offset_entry_count = 0; |
| 69 |
Dwarf_Unsigned offset_of_offset_array = 0; |
| 70 |
Dwarf_Unsigned offset_of_first_rangeentry = 0; |
| 71 |
Dwarf_Unsigned offset_past_last_rangeentry = 0; |
| 72 |
|
| 73 |
res = dwarf_get_rnglist_context_basics( |
| 74 |
dbg, i, &header_offset, &offset_size, &extension_size, &version, |
| 75 |
&address_size, &segment_selector_size, &offset_entry_count, |
| 76 |
&offset_of_offset_array, &offset_of_first_rangeentry, |
| 77 |
&offset_past_last_rangeentry, errp); |
| 78 |
|
| 79 |
Dwarf_Unsigned e = 0; |
| 80 |
unsigned colmax = 4; |
| 81 |
unsigned col = 0; |
| 82 |
Dwarf_Unsigned global_offset_of_value = 0; |
| 83 |
|
| 84 |
for (; e < offset_entry_count; ++e) { |
| 85 |
Dwarf_Unsigned value = 0; |
| 86 |
int resc = 0; |
| 87 |
|
| 88 |
resc = dwarf_get_rnglist_offset_index_value( |
| 89 |
dbg, i, e, &value, &global_offset_of_value, errp); |
| 90 |
if (resc != DW_DLV_OK) { |
| 91 |
dwarf_finish(dbg); |
| 92 |
close(fuzz_fd); |
| 93 |
unlink(filename); |
| 94 |
return resc; |
| 95 |
} |
| 96 |
col++; |
| 97 |
if (col == colmax) { |
| 98 |
col = 0; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
Dwarf_Unsigned curoffset = offset_of_first_rangeentry; |
| 103 |
Dwarf_Unsigned endoffset = offset_past_last_rangeentry; |
| 104 |
int rese = 0; |
| 105 |
Dwarf_Unsigned ct = 0; |
| 106 |
|
| 107 |
for (; curoffset < endoffset; ++ct) { |
| 108 |
unsigned entrylen = 0; |
| 109 |
unsigned code = 0; |
| 110 |
Dwarf_Unsigned v1 = 0; |
| 111 |
Dwarf_Unsigned v2 = 0; |
| 112 |
rese = dwarf_get_rnglist_rle(dbg, i, curoffset, endoffset, &entrylen, |
| 113 |
&code, &v1, &v2, errp); |
| 114 |
if (rese != DW_DLV_OK) { |
| 115 |
dwarf_finish(dbg); |
| 116 |
close(fuzz_fd); |
| 117 |
unlink(filename); |
| 118 |
return rese; |
| 119 |
} |
| 120 |
curoffset += entrylen; |
| 121 |
if (curoffset > endoffset) { |
| 122 |
dwarf_finish(dbg); |
| 123 |
close(fuzz_fd); |
| 124 |
unlink(filename); |
| 125 |
return DW_DLV_ERROR; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
dwarf_finish(dbg); |
| 131 |
close(fuzz_fd); |
| 132 |
} |
| 133 |
unlink(filename); |
| 134 |
return 0; |
| 135 |
} |