Coverage Report

Created: 2025-06-24 06:45

/src/fuzz_as.c
Line
Count
Source (jump to first uncovered line)
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
13
/*
14
 * We convert as.c into a header file to make convenient for fuzzing.
15
 * We do this for several of the binutils applications when creating
16
 * the binutils fuzzers.
17
 */
18
#include <fuzz_as.h>
19
20
/* Don't allow cleanups.  libiberty's function of the same name adds
21
   cleanups to a list without any means of clearing the list.  The
22
   list must be clear at the start if LLVMFuzzerTestOneInput is to run
23
   more than once, otherwise we will get multiple copies of the same
24
   cleanup on the list which leads to double frees if xexit is called.
25
   Also a cleanup from the first run can result in use-after-free
26
   errors when as_fatal is hit as in issue 56429.  */
27
int
28
xatexit (void (*fn) (void) ATTRIBUTE_UNUSED)
29
56
{
30
56
  return 0;
31
56
}
32
33
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
34
117k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
117k
  char filename[256];
36
117k
  sprintf(filename, "/tmp/libfuzzer-%d.s", getpid());
37
117k
  FILE *fp = fopen(filename, "wb");
38
117k
  if (!fp) {
39
0
    return 0;
40
0
  }
41
117k
  fwrite(data, size, 1, fp);
42
117k
  fclose(fp);
43
44
117k
  reg_section = NULL;
45
46
117k
  char *fakeArgv[3];
47
117k
  fakeArgv[0] = "fuzz_as";
48
117k
  fakeArgv[1] = filename; // Assemble our fake source file.
49
117k
  fakeArgv[2] = NULL;
50
51
117k
  int argc = 2;
52
117k
  char **argv = fakeArgv;
53
117k
  gas_early_init (&argc, &argv);
54
55
117k
  out_file_name = "/tmp/tmp-out";
56
57
117k
  gas_init ();
58
59
  // Main fuzzer target. Assemble our random data.
60
117k
  perform_an_assembly_pass (argc, argv);
61
62
  // Cleanup
63
117k
  cond_finish_check (-1);
64
117k
  codeview_finish ();
65
117k
  dwarf2_finish ();
66
117k
  cfi_finish ();
67
117k
  input_scrub_end ();
68
69
117k
  keep_it = 0;
70
117k
  output_file_close ();
71
117k
  free_notes ();
72
117k
  unlink(filename);
73
74
117k
  return 0;
75
117k
}