mirror of
https://github.com/davea42/libdwarf-code.git
synced 2025-10-18 18:22:36 +08:00

of the temp file to start with junk to better match what is routine in the regressiontests. And to something that works just fine with Win msys2. (in msys2 "/tmp/anything" will not work for open( "wb") whereas "anything" will work.) modified: fuzz/fuzz_aranges.c modified: fuzz/fuzz_crc.c modified: fuzz/fuzz_crc_32.c modified: fuzz/fuzz_debug_addr_access.c modified: fuzz/fuzz_debug_str.c modified: fuzz/fuzz_debuglink.c modified: fuzz/fuzz_die_cu.c modified: fuzz/fuzz_die_cu_attrs.c modified: fuzz/fuzz_die_cu_attrs_loclist.c modified: fuzz/fuzz_die_cu_e.c modified: fuzz/fuzz_die_cu_e_print.c modified: fuzz/fuzz_die_cu_info1.c modified: fuzz/fuzz_die_cu_offset.c modified: fuzz/fuzz_die_cu_print.c modified: fuzz/fuzz_dnames.c modified: fuzz/fuzz_findfuncbypc.c modified: fuzz/fuzz_gdbindex.c modified: fuzz/fuzz_globals.c modified: fuzz/fuzz_gnu_index.c modified: fuzz/fuzz_init_b.c modified: fuzz/fuzz_init_binary.c modified: fuzz/fuzz_init_path.c modified: fuzz/fuzz_macro_dwarf4.c modified: fuzz/fuzz_macro_dwarf5.c modified: fuzz/fuzz_rng.c modified: fuzz/fuzz_set_frame_all.c modified: fuzz/fuzz_showsectgrp.c modified: fuzz/fuzz_simplereader_tu.c modified: fuzz/fuzz_srcfiles.c modified: fuzz/fuzz_stack_frame_access.c modified: fuzz/fuzz_str_offsets.c modified: fuzz/fuzz_tie.c modified: fuzz/fuzz_xuindex.c modified: src/lib/libdwarf/libdwarf.h
212 lines
6.6 KiB
C
212 lines
6.6 KiB
C
/* Copyright 2021 Google LLC
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
#include <fcntl.h> /* open() O_RDONLY O_BINARY */
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
|
|
#ifndef O_BINARY
|
|
#define O_BINARY 0 /* So it does nothing in Linux/Unix */
|
|
#endif
|
|
|
|
|
|
/*
|
|
* Libdwarf library callers can only use these headers.
|
|
*/
|
|
#include "dwarf.h"
|
|
#include "libdwarf.h"
|
|
|
|
/* This now initializes local variables to zero
|
|
rather than leaving them uninitialized.
|
|
When uninitialized consistent behavior is
|
|
unlikely, run-to-run. And
|
|
crashes are likely.
|
|
David Anderson 30 May 2023.
|
|
*/
|
|
/*
|
|
* A fuzzer that simulates a small part of the simplereader.c example.
|
|
*/
|
|
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|
char filename[256];
|
|
#ifdef DWREGRESSIONTEMP
|
|
/* Under msys2, the /tmp/ results in an open fail */
|
|
sprintf(filename, "junklibfuzzer.%d", getpid());
|
|
#else
|
|
sprintf(filename, "/tmp/libfuzzer.%d", getpid());
|
|
#endif
|
|
FILE *fp = fopen(filename, "wb");
|
|
if (!fp) {
|
|
printf("FAIL libfuzzer cannot open temp as writeable %s\n",
|
|
filename);
|
|
return 0;
|
|
}
|
|
fwrite(data, size, 1, fp);
|
|
fclose(fp);
|
|
|
|
Dwarf_Debug dbg = 0;
|
|
int res = DW_DLV_ERROR;
|
|
Dwarf_Error error = 0;
|
|
Dwarf_Handler errhand = 0;
|
|
Dwarf_Ptr errarg = 0;
|
|
int regtabrulecount = 0;
|
|
int curopt = 0;
|
|
|
|
int fd = open(filename, O_RDONLY | O_BINARY);
|
|
if (fd < 0) {
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
res = dwarf_init_b(fd, DW_GROUPNUMBER_ANY, errhand, errarg, &dbg, &error);
|
|
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_error(dbg, error);
|
|
} else {
|
|
|
|
Dwarf_Dnames_Head dnames_h = 0;
|
|
Dwarf_Off dw_offset_of_next_table = 0;
|
|
res = dwarf_dnames_header(dbg, 0, &dnames_h, &dw_offset_of_next_table,
|
|
&error);
|
|
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
Dwarf_Unsigned dw_index = 1;
|
|
Dwarf_Unsigned dw_abbrev_offset = 0;
|
|
Dwarf_Unsigned dw_abbrev_code = 0;
|
|
Dwarf_Unsigned dw_abbrev_tag = 0;
|
|
Dwarf_Unsigned dw_array_size = 256;
|
|
/* This test code originally passed in uninitialized
|
|
pointers dw_idxattr_array and dw_form_array, which
|
|
we cannot protect against. But we can check for NULL
|
|
so now the variables are initialilized.
|
|
In any case this code does not call the function correctly,
|
|
but we leave that as written. David Anderson 30 May 2023 */
|
|
Dwarf_Half *dw_idxattr_array = 0;
|
|
Dwarf_Half *dw_form_array = 0;
|
|
Dwarf_Unsigned dw_idxattr_count = 0;
|
|
|
|
res = dwarf_dnames_abbrevtable(
|
|
dnames_h, dw_index, &dw_abbrev_offset, &dw_abbrev_code, &dw_abbrev_tag,
|
|
dw_array_size, dw_idxattr_array, dw_form_array, &dw_idxattr_count);
|
|
if (res == DW_DLV_NO_ENTRY) {
|
|
}
|
|
|
|
Dwarf_Unsigned dw_comp_unit_count = 0;
|
|
Dwarf_Unsigned dw_local_type_unit_count = 0;
|
|
Dwarf_Unsigned dw_foreign_type_unit_count = 0;
|
|
Dwarf_Unsigned dw_bucket_count = 0;
|
|
Dwarf_Unsigned dw_name_count = 0;
|
|
Dwarf_Unsigned dw_abbrev_table_size = 0;
|
|
Dwarf_Unsigned dw_entry_pool_size = 0;
|
|
Dwarf_Unsigned dw_augmentation_string_size = 0;
|
|
char *dw_augmentation_string = 0;
|
|
Dwarf_Unsigned dw_section_size = 0;
|
|
Dwarf_Half dw_table_version = 0;
|
|
Dwarf_Half dw_offset_size = 0;
|
|
res = dwarf_dnames_sizes(
|
|
dnames_h, &dw_comp_unit_count, &dw_local_type_unit_count,
|
|
&dw_foreign_type_unit_count, &dw_bucket_count, &dw_name_count,
|
|
&dw_abbrev_table_size, &dw_entry_pool_size,
|
|
&dw_augmentation_string_size, &dw_augmentation_string, &dw_section_size,
|
|
&dw_table_version, &dw_offset_size, &error);
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
Dwarf_Unsigned dw_header_offset = 0;
|
|
Dwarf_Unsigned dw_cu_table_offset = 0;
|
|
Dwarf_Unsigned dw_tu_local_offset = 0;
|
|
Dwarf_Unsigned dw_foreign_tu_offset = 0;
|
|
Dwarf_Unsigned dw_bucket_offset = 0;
|
|
Dwarf_Unsigned dw_hashes_offset = 0;
|
|
Dwarf_Unsigned dw_stringoffsets_offset = 0;
|
|
Dwarf_Unsigned dw_entryoffsets_offset = 0;
|
|
Dwarf_Unsigned dw_abbrev_table_offset = 0;
|
|
Dwarf_Unsigned dw_entry_pool_offset = 0;
|
|
res = dwarf_dnames_offsets(
|
|
dnames_h, &dw_header_offset, &dw_cu_table_offset, &dw_tu_local_offset,
|
|
&dw_foreign_tu_offset, &dw_bucket_offset, &dw_hashes_offset,
|
|
&dw_stringoffsets_offset, &dw_entryoffsets_offset,
|
|
&dw_abbrev_table_offset, &dw_entry_pool_offset, &error);
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
Dwarf_Unsigned dw_offset = 0;
|
|
Dwarf_Sig8 dw_sig;
|
|
res = dwarf_dnames_cu_table(dnames_h, "cu", 0, &dw_offset, &dw_sig, &error);
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
dw_index = 0;
|
|
Dwarf_Unsigned dw_indexcount;
|
|
res = dwarf_dnames_bucket(dnames_h, 0, &dw_index, &dw_indexcount, &error);
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
Dwarf_Unsigned dw_bucket_number = 0;
|
|
Dwarf_Unsigned dw_hash_value = 0;
|
|
Dwarf_Unsigned dw_offset_to_debug_str = 0;
|
|
char *dw_ptrtostr = 0;
|
|
Dwarf_Unsigned dw_offset_in_entrypool = 0;
|
|
Dwarf_Unsigned dw_abbrev_number = 0;
|
|
Dwarf_Half abbrev_tg = 0;
|
|
dw_array_size = 10;
|
|
Dwarf_Half idxattr_array[10];
|
|
Dwarf_Half form_array[10];
|
|
res = dwarf_dnames_name(
|
|
dnames_h, 1, &dw_bucket_number, &dw_hash_value, &dw_offset_to_debug_str,
|
|
&dw_ptrtostr, &dw_offset_in_entrypool, &dw_abbrev_number, &abbrev_tg,
|
|
dw_array_size, idxattr_array, form_array, &dw_idxattr_count, &error);
|
|
if (res != DW_DLV_OK) {
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|
|
|
|
dwarf_dealloc_dnames(dnames_h);
|
|
}
|
|
|
|
dwarf_finish(dbg);
|
|
close(fd);
|
|
unlink(filename);
|
|
return 0;
|
|
}
|