aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/binutils/fuzz_objcopy.c
blob: 6e43bde87203082ac0fca79df2dadaa4b8d876ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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.
*/

/*
 * We convert objcopy.c into a header file to make convenient for fuzzing.
 * We do this for several of the binutils applications when creating
 * the binutils fuzzers.
 */
#include "fuzz_objcopy.h"

int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);

static int initialized = 0;

void
init_objcopy_global_state() {
  // status is a global variable that is set to 0 initially,
  // and we should ensure the state is not maintained for each iteration.
  status = 0;
  pe_file_alignment = (bfd_vma) -1;
  pe_heap_commit = (bfd_vma) -1;
  pe_heap_reserve = (bfd_vma) -1;
  pe_image_base = (bfd_vma) -1;
  pe_section_alignment = (bfd_vma) -1;
  pe_stack_commit = (bfd_vma) -1;
  pe_stack_reserve = (bfd_vma) -1;
  pe_subsystem = -1;
  pe_major_subsystem_version = -1;
  pe_minor_subsystem_version = -1;
  section_rename_list = NULL;
  isympp = NULL;
  osympp = NULL;
  copy_byte = -1;
  interleave = 0;
  copy_width = 1;
  keep_section_symbols = false;
  deterministic = -1;
  status = 0;
  merge_notes = false;
  strip_symbols = STRIP_UNDEF;
  change_sections = NULL;
  change_start = 0;
  set_start_set = false;
  change_section_address = 0;
  gap_fill_set = false;
  gap_fill = 0;
  pad_to_set = false;
  use_alt_mach_code = 0;
  add_sections = NULL;
  update_sections = NULL;
  dump_sections = NULL;
  gnu_debuglink_filename = NULL;
  convert_debugging = false;
  change_leading_char = false;
  remove_leading_char = false;
  wildcard = false;
  localize_hidden = false;
  strip_specific_htab = NULL;
  strip_unneeded_htab = NULL;
  keep_specific_htab = NULL;
  localize_specific_htab = NULL;
  globalize_specific_htab = NULL;
  keepglobal_specific_htab = NULL;
  weaken_specific_htab = NULL;
  redefine_specific_htab = NULL;
  redefine_specific_reverse_htab = NULL;
  add_sym_tail = &add_sym_list;
  add_symbols = 0;
  strip_specific_buffer = NULL;
  strip_unneeded_buffer = NULL;
  keep_specific_buffer = NULL;
  localize_specific_buffer = NULL;
  globalize_specific_buffer = NULL;
  keepglobal_specific_buffer = NULL;
  weaken_specific_buffer = NULL;
  weaken = false;
  keep_file_symbols = false;
  prefix_symbols_string = 0;
  prefix_sections_string = 0;
  prefix_alloc_sections_string = 0;
  extract_symbol = false;

  create_symbol_htabs();
}

int
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
  char filename[256];
  sprintf(filename, "/tmp/libfuzzer.%d", getpid());
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    return 0;
  }
  fwrite(data, size, 1, fp);
  fclose(fp);

  program_name = filename;

  if (initialized == 0) {
    if (bfd_init () != BFD_INIT_MAGIC) {
      abort();
    }
    set_default_bfd_target();
    initialized = 1;
  }

  init_objcopy_global_state();

  char *fakeArgv[12];
  fakeArgv[0] = "fuzz_objdump";
  fakeArgv[1] = "-S";
  fakeArgv[2] = "--decompress-debug-sections";
  fakeArgv[3] = "--extract-dwo";
  fakeArgv[4] = "--merge-notes";
  fakeArgv[5] = "--pure";
  fakeArgv[6] = "--debugging";
  fakeArgv[7] = "--compress-debug-sections";
  fakeArgv[8] = "--extract-symbol";
  fakeArgv[9] = filename;
  fakeArgv[10] = "/tmp/random.out";
  fakeArgv[11] = NULL;
  copy_main(11, fakeArgv);

  // Cleanup
  free (strip_specific_buffer);
  strip_specific_buffer = NULL;
  free (strip_unneeded_buffer);
  strip_unneeded_buffer = NULL;
  free (keep_specific_buffer);
  keep_specific_buffer = NULL;
  free (localize_specific_buffer);
  localize_specific_buffer = NULL;
  free (globalize_specific_buffer);
  globalize_specific_buffer = NULL;
  free (keepglobal_specific_buffer);
  keepglobal_specific_buffer = NULL;
  free (weaken_specific_buffer);
  weaken_specific_buffer = NULL;

  unlink(filename);
  remove("/tmp/random.out");
  return 0;
}