MCPcopy Create free account
hub / github.com/capstone-engine/capstone / LLVMFuzzerTestOneInput

Function LLVMFuzzerTestOneInput

suite/fuzz/fuzz_diff.c:180–237  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

178int LLVMFuzzerReturnOneInput(const uint8_t *Data, size_t Size, char * AssemblyText);
179
180int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
181 csh handle;
182 cs_insn *insn;
183 cs_err err;
184 const uint8_t **Datap = &Data;
185 size_t * Sizep = &Size;
186 uint64_t address = 0x1000;
187 char LLVMAssemblyText[80];
188 char CapstoneAssemblyText[80];
189
190 if (Size < 1) {
191 // 1 byte for arch choice
192 return 0;
193 } else if (Size > 0x1000) {
194 //limit input to 4kb
195 Size = 0x1000;
196 }
197 if (outfile == NULL) {
198 // we compute the output
199 outfile = fopen("/dev/null", "w");
200 if (outfile == NULL) {
201 return 0;
202 }
203 LLVMFuzzerInit();
204 }
205
206 if (Data[0] >= sizeof(platforms)/sizeof(platforms[0])) {
207 return 0;
208 }
209
210 if (LLVMFuzzerReturnOneInput(Data, Size, LLVMAssemblyText) == 1) {
211 return 0;
212 }
213
214 err = cs_open(platforms[Data[0]].arch, platforms[Data[0]].mode, &handle);
215 if (err) {
216 return 0;
217 }
218
219 insn = cs_malloc(handle);
220 Data++;
221 Size--;
222 assert(insn);
223 if (cs_disasm_iter(handle, Datap, Sizep, &address, insn)) {
224 snprintf(CapstoneAssemblyText, 80, "\t%s\t%s", insn->mnemonic, insn->op_str);
225 if (strcmp(CapstoneAssemblyText, LLVMAssemblyText) != 0) {
226 printf("capstone %s != llvm %s", CapstoneAssemblyText, LLVMAssemblyText);
227 abort();
228 }
229 } else {
230 printf("capstone failed with llvm %s", LLVMAssemblyText);
231 abort();
232 }
233 cs_free(insn, 1);
234 cs_close(&handle);
235
236 return 0;
237}

Callers 3

mainFunction · 0.70
mainFunction · 0.70
mainFunction · 0.70

Calls 8

LLVMFuzzerInitFunction · 0.85
LLVMFuzzerReturnOneInputFunction · 0.85
cs_openFunction · 0.85
cs_mallocFunction · 0.85
cs_disasm_iterFunction · 0.85
cs_freeFunction · 0.85
cs_closeFunction · 0.85
printfFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…