• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 Google Inc.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7       http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15 
16 #include "sysdep.h"
17 #include "bfd.h"
18 #include "dis-asm.h"
19 #include "disassemble.h"
20 
21 #include <stdint.h>
22 
23 #define MAX_TEXT_SIZE 256
24 
25 typedef struct
26 {
27     char *buffer;
28     size_t pos;
29 } SFILE;
30 
objdump_sprintf(void * vf,const char * format,...)31 static int objdump_sprintf (void *vf, const char *format, ...)
32 {
33     SFILE *f = (SFILE *) vf;
34     size_t n;
35     va_list args;
36 
37     va_start (args, format);
38     if (f->pos >= MAX_TEXT_SIZE){
39         printf("buffer needs more space\n");
40         //reset
41         f->pos=0;
42         return 0;
43     }
44     n = vsnprintf (f->buffer + f->pos, MAX_TEXT_SIZE - f->pos, format, args);
45     //vfprintf(stdout, format, args);
46     va_end (args);
47     f->pos += n;
48     return n;
49 }
50 
51 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)52 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
53     char AssemblyText[MAX_TEXT_SIZE];
54     struct disassemble_info disasm_info;
55     SFILE s;
56 
57     if (Size < 10 || Size > 16394) {
58         // 10 bytes for options
59         // 16394 limit code to prevent timeouts
60         return 0;
61     }
62 
63     init_disassemble_info (&disasm_info, stdout, (fprintf_ftype) fprintf);
64     disasm_info.fprintf_func = objdump_sprintf;
65     disasm_info.print_address_func = generic_print_address;
66     disasm_info.display_endian = disasm_info.endian = BFD_ENDIAN_LITTLE;
67     disasm_info.buffer = (bfd_byte *) Data;
68     disasm_info.buffer_vma = 0x1000;
69     disasm_info.buffer_length = Size-10;
70     disasm_info.insn_info_valid = 0;
71     s.buffer = AssemblyText;
72     s.pos = 0;
73     disasm_info.stream = &s;
74     disasm_info.bytes_per_line = 0;
75 
76     disasm_info.arch = Data[Size-1];
77     disasm_info.mach = bfd_getl64(&Data[Size-9]);
78     disasm_info.flavour = Data[Size-10];
79 
80     if (bfd_lookup_arch (disasm_info.arch, disasm_info.mach) != NULL) {
81         disassembler_ftype disasfunc = disassembler(disasm_info.arch, 0, disasm_info.mach, NULL);
82         if (disasfunc != NULL) {
83             disassemble_init_for_target(&disasm_info);
84             while (1) {
85                 s.pos = 0;
86                 int octets = disasfunc(disasm_info.buffer_vma, &disasm_info);
87                 if (octets < (int) disasm_info.octets_per_byte)
88                     break;
89                 if (disasm_info.buffer_length <= (size_t) octets)
90                     break;
91                 disasm_info.buffer += octets;
92                 disasm_info.buffer_vma += octets / disasm_info.octets_per_byte;
93                 disasm_info.buffer_length -= octets;
94             }
95             disassemble_free_target(&disasm_info);
96         }
97     }
98 
99     return 0;
100 }
101