afl-as.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. american fuzzy lop - injectable parts
  3. -------------------------------------
  4. Written and maintained by Michal Zalewski <lcamtuf@google.com>
  5. Forkserver design by Jann Horn <jannhorn@googlemail.com>
  6. Copyright 2013, 2014, 2015 Google Inc. All rights reserved.
  7. Licensed under the Apache License, Version 2.0 (the "License");
  8. you may not use this file except in compliance with the License.
  9. You may obtain a copy of the License at:
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. This file houses the assembly-level instrumentation injected into fuzzed
  12. programs. The instrumentation stores XORed pairs of data: identifiers of the
  13. currently executing branch and the one that executed immediately before.
  14. TL;DR: the instrumentation does shm_trace_map[cur_loc ^ prev_loc]++
  15. The code is designed for 32-bit and 64-bit x86 systems. Both modes should
  16. work everywhere except for Apple systems. Apple does relocations differently
  17. from everybody else, so since their OSes have been 64-bit for a longer while,
  18. I didn't go through the mental effort of porting the 32-bit code.
  19. In principle, similar code should be easy to inject into any well-behaved
  20. binary-only code (e.g., using DynamoRIO). Conditional jumps offer natural
  21. targets for instrumentation, and should offer comparable probe density.
  22. */
  23. #ifndef _HAVE_AFL_AS_H
  24. #define _HAVE_AFL_AS_H
  25. #include "config.h"
  26. #include "types.h"
  27. /*
  28. ------------------
  29. Performances notes
  30. ------------------
  31. Contributions to make this code faster are appreciated! Here are some
  32. rough notes that may help with the task:
  33. - Only the trampoline_fmt and the non-setup __afl_maybe_log code paths are
  34. really worth optimizing; the setup / fork server stuff matters a lot less
  35. and should be mostly just kept readable.
  36. - We're aiming for modern CPUs with out-of-order execution and large
  37. pipelines; the code is mostly follows intuitive, human-readable
  38. instruction ordering, because "textbook" manual reorderings make no
  39. substantial difference.
  40. - Interestingly, instrumented execution isn't a lot faster if we store a
  41. variable pointer to the setup, log, or return routine and then do a reg
  42. call from within trampoline_fmt. It does speed up non-instrumented
  43. execution quite a bit, though, since that path just becomes
  44. push-call-ret-pop.
  45. - There is also not a whole lot to be gained by doing SHM attach at a
  46. fixed address instead of retrieving __afl_area_ptr. Although it allows us
  47. to have a shorter log routine inserted for conditional jumps and jump
  48. labels (for a ~10% perf gain), there is a risk of bumping into other
  49. allocations created by the program or by tools such as ASAN.
  50. - popf is *awfully* slow, which is why we're doing the lahf / sahf +
  51. overflow test trick. Unfortunately, this forces us to taint eax / rax, but
  52. this dependency on a commonly-used register still beats the alternative of
  53. using pushf / popf.
  54. One possible optimization is to avoid touching flags by using a circular
  55. buffer that stores just a sequence of current locations, with the XOR stuff
  56. happening offline. Alas, this doesn't seem to have a huge impact:
  57. https://groups.google.com/d/msg/afl-users/MsajVf4fRLo/2u6t88ntUBIJ
  58. - Preforking one child a bit sooner, and then waiting for the "go" command
  59. from within the child, doesn't offer major performance gains; fork() seems
  60. to be relatively inexpensive these days. Preforking multiple children does
  61. help, but badly breaks the "~1 core per fuzzer" design, making it harder to
  62. scale up. Maybe there is some middle ground.
  63. Perhaps of note: in the 64-bit version for all platforms except for Apple,
  64. the instrumentation is done slightly differently than on 32-bit, with
  65. __afl_prev_loc and __afl_area_ptr being local to the object file (.lcomm),
  66. rather than global (.comm). This is to avoid GOTRELPC lookups in the critical
  67. code path, which AFAICT, are otherwise unavoidable if we want gcc -shared to
  68. work; simple relocations between .bss and .text won't work on most 64-bit
  69. platforms in such a case.
  70. (Fun fact: on Apple systems, .lcomm can segfault the linker.)
  71. The side effect is that state transitions are measured in a somewhat
  72. different way, with previous tuple being recorded separately within the scope
  73. of every .c file. This should have no impact in any practical sense.
  74. Another side effect of this design is that getenv() will be called once per
  75. every .o file when running in non-instrumented mode; and since getenv() tends
  76. to be optimized in funny ways, we need to be very careful to save every
  77. oddball register it may touch.
  78. */
  79. static const u8* trampoline_fmt_32 =
  80. "\n"
  81. "/* --- AFL TRAMPOLINE (32-BIT) --- */\n"
  82. "\n"
  83. ".align 4\n"
  84. "\n"
  85. "leal -16(%%esp), %%esp\n"
  86. "movl %%edi, 0(%%esp)\n"
  87. "movl %%edx, 4(%%esp)\n"
  88. "movl %%ecx, 8(%%esp)\n"
  89. "movl %%eax, 12(%%esp)\n"
  90. "movl $0x%08x, %%ecx\n"
  91. "call __afl_maybe_log\n"
  92. "movl 12(%%esp), %%eax\n"
  93. "movl 8(%%esp), %%ecx\n"
  94. "movl 4(%%esp), %%edx\n"
  95. "movl 0(%%esp), %%edi\n"
  96. "leal 16(%%esp), %%esp\n"
  97. "\n"
  98. "/* --- END --- */\n"
  99. "\n";
  100. static const u8* trampoline_fmt_64 =
  101. "\n"
  102. "/* --- AFL TRAMPOLINE (64-BIT) --- */\n"
  103. "\n"
  104. ".align 4\n"
  105. "\n"
  106. "leaq -(128+24)(%%rsp), %%rsp\n"
  107. "movq %%rdx, 0(%%rsp)\n"
  108. "movq %%rcx, 8(%%rsp)\n"
  109. "movq %%rax, 16(%%rsp)\n"
  110. "movq $0x%08x, %%rcx\n"
  111. "call __afl_maybe_log\n"
  112. "movq 16(%%rsp), %%rax\n"
  113. "movq 8(%%rsp), %%rcx\n"
  114. "movq 0(%%rsp), %%rdx\n"
  115. "leaq (128+24)(%%rsp), %%rsp\n"
  116. "\n"
  117. "/* --- END --- */\n"
  118. "\n";
  119. static const u8* main_payload_32 =
  120. "\n"
  121. "/* --- AFL MAIN PAYLOAD (32-BIT) --- */\n"
  122. "\n"
  123. ".text\n"
  124. ".att_syntax\n"
  125. ".code32\n"
  126. ".align 8\n"
  127. "\n"
  128. "__afl_maybe_log:\n"
  129. "\n"
  130. " lahf\n"
  131. " seto %al\n"
  132. "\n"
  133. " /* Check if SHM region is already mapped. */\n"
  134. "\n"
  135. " movl __afl_area_ptr, %edx\n"
  136. " testl %edx, %edx\n"
  137. " je __afl_setup\n"
  138. "\n"
  139. "__afl_store:\n"
  140. "\n"
  141. " /* Calculate and store hit for the code location specified in ecx. There\n"
  142. " is a double-XOR way of doing this without tainting another register,\n"
  143. " and we use it on 64-bit systems; but it's slower for 32-bit ones. */\n"
  144. "\n"
  145. #ifndef COVERAGE_ONLY
  146. " movl __afl_prev_loc, %edi\n"
  147. " xorl %ecx, %edi\n"
  148. " shrl $1, %ecx\n"
  149. " movl %ecx, __afl_prev_loc\n"
  150. #else
  151. " movl %ecx, %edi\n"
  152. #endif /* ^!COVERAGE_ONLY */
  153. "\n"
  154. #ifdef SKIP_COUNTS
  155. " orb $1, (%edx, %edi, 1)\n"
  156. #else
  157. " incb (%edx, %edi, 1)\n"
  158. #endif /* ^SKIP_COUNTS */
  159. "\n"
  160. "__afl_return:\n"
  161. "\n"
  162. " addb $127, %al\n"
  163. " sahf\n"
  164. " ret\n"
  165. "\n"
  166. ".align 8\n"
  167. "\n"
  168. "__afl_setup:\n"
  169. "\n"
  170. " /* Do not retry setup if we had previous failures. */\n"
  171. "\n"
  172. " cmpb $0, __afl_setup_failure\n"
  173. " jne __afl_return\n"
  174. "\n"
  175. " /* Map SHM, jumping to __afl_setup_abort if something goes wrong.\n"
  176. " We do not save FPU/MMX/SSE registers here, but hopefully, nobody\n"
  177. " will notice this early in the game. */\n"
  178. "\n"
  179. " pushl %eax\n"
  180. " pushl %ecx\n"
  181. "\n"
  182. " pushl $.AFL_SHM_ENV\n"
  183. " call getenv\n"
  184. " addl $4, %esp\n"
  185. "\n"
  186. " testl %eax, %eax\n"
  187. " je __afl_setup_abort\n"
  188. "\n"
  189. " pushl %eax\n"
  190. " call atoi\n"
  191. " addl $4, %esp\n"
  192. "\n"
  193. " pushl $0 /* shmat flags */\n"
  194. " pushl $0 /* requested addr */\n"
  195. " pushl %eax /* SHM ID */\n"
  196. " call shmat\n"
  197. " addl $12, %esp\n"
  198. "\n"
  199. " cmpl $-1, %eax\n"
  200. " je __afl_setup_abort\n"
  201. "\n"
  202. " /* Store the address of the SHM region. */\n"
  203. "\n"
  204. " movl %eax, __afl_area_ptr\n"
  205. " movl %eax, %edx\n"
  206. "\n"
  207. " popl %ecx\n"
  208. " popl %eax\n"
  209. "\n"
  210. "__afl_forkserver:\n"
  211. "\n"
  212. " /* Enter the fork server mode to avoid the overhead of execve() calls. */\n"
  213. "\n"
  214. " pushl %eax\n"
  215. " pushl %ecx\n"
  216. " pushl %edx\n"
  217. "\n"
  218. " /* Phone home and tell the parent that we're OK. (Note that signals with\n"
  219. " no SA_RESTART will mess it up). If this fails, assume that the fd is\n"
  220. " closed because we were execve()d from an instrumented binary, or because\n"
  221. " the parent doesn't want to use the fork server. */\n"
  222. "\n"
  223. " pushl $4 /* length */\n"
  224. " pushl $__afl_temp /* data */\n"
  225. " pushl $" STRINGIFY((FORKSRV_FD + 1)) " /* file desc */\n"
  226. " call write\n"
  227. " addl $12, %esp\n"
  228. "\n"
  229. " cmpl $4, %eax\n"
  230. " jne __afl_fork_resume\n"
  231. "\n"
  232. "__afl_fork_wait_loop:\n"
  233. "\n"
  234. " /* Wait for parent by reading from the pipe. Abort if read fails. */\n"
  235. "\n"
  236. " pushl $4 /* length */\n"
  237. " pushl $__afl_temp /* data */\n"
  238. " pushl $" STRINGIFY(FORKSRV_FD) " /* file desc */\n"
  239. " call read\n"
  240. " addl $12, %esp\n"
  241. "\n"
  242. " cmpl $4, %eax\n"
  243. " jne __afl_die\n"
  244. "\n"
  245. " /* Once woken up, create a clone of our process. This is an excellent use\n"
  246. " case for syscall(__NR_clone, 0, CLONE_PARENT), but glibc boneheadedly\n"
  247. " caches getpid() results and offers no way to update the value, breaking\n"
  248. " abort(), raise(), and a bunch of other things :-( */\n"
  249. "\n"
  250. " call fork\n"
  251. "\n"
  252. " cmpl $0, %eax\n"
  253. " jl __afl_die\n"
  254. " je __afl_fork_resume\n"
  255. "\n"
  256. " /* In parent process: write PID to pipe, then wait for child. */\n"
  257. "\n"
  258. " movl %eax, __afl_fork_pid\n"
  259. "\n"
  260. " pushl $4 /* length */\n"
  261. " pushl $__afl_fork_pid /* data */\n"
  262. " pushl $" STRINGIFY((FORKSRV_FD + 1)) " /* file desc */\n"
  263. " call write\n"
  264. " addl $12, %esp\n"
  265. "\n"
  266. " pushl $0 /* no flags */\n"
  267. " pushl $__afl_temp /* status */\n"
  268. " pushl __afl_fork_pid /* PID */\n"
  269. " call waitpid\n"
  270. " addl $12, %esp\n"
  271. "\n"
  272. " cmpl $0, %eax\n"
  273. " jle __afl_die\n"
  274. "\n"
  275. " /* Relay wait status to pipe, then loop back. */\n"
  276. "\n"
  277. " pushl $4 /* length */\n"
  278. " pushl $__afl_temp /* data */\n"
  279. " pushl $" STRINGIFY((FORKSRV_FD + 1)) " /* file desc */\n"
  280. " call write\n"
  281. " addl $12, %esp\n"
  282. "\n"
  283. " jmp __afl_fork_wait_loop\n"
  284. "\n"
  285. "__afl_fork_resume:\n"
  286. "\n"
  287. " /* In child process: close fds, resume execution. */\n"
  288. "\n"
  289. " pushl $" STRINGIFY(FORKSRV_FD) "\n"
  290. " call close\n"
  291. "\n"
  292. " pushl $" STRINGIFY((FORKSRV_FD + 1)) "\n"
  293. " call close\n"
  294. "\n"
  295. " addl $8, %esp\n"
  296. "\n"
  297. " popl %edx\n"
  298. " popl %ecx\n"
  299. " popl %eax\n"
  300. " jmp __afl_store\n"
  301. "\n"
  302. "__afl_die:\n"
  303. "\n"
  304. " xorl %eax, %eax\n"
  305. " call _exit\n"
  306. "\n"
  307. "__afl_setup_abort:\n"
  308. "\n"
  309. " /* Record setup failure so that we don't keep calling\n"
  310. " shmget() / shmat() over and over again. */\n"
  311. "\n"
  312. " incb __afl_setup_failure\n"
  313. " popl %ecx\n"
  314. " popl %eax\n"
  315. " jmp __afl_return\n"
  316. "\n"
  317. ".AFL_VARS:\n"
  318. "\n"
  319. " .comm __afl_area_ptr, 4, 32\n"
  320. " .comm __afl_setup_failure, 1, 32\n"
  321. #ifndef COVERAGE_ONLY
  322. " .comm __afl_prev_loc, 4, 32\n"
  323. #endif /* !COVERAGE_ONLY */
  324. " .comm __afl_fork_pid, 4, 32\n"
  325. " .comm __afl_temp, 4, 32\n"
  326. "\n"
  327. ".AFL_SHM_ENV:\n"
  328. " .asciz \"" SHM_ENV_VAR "\"\n"
  329. "\n"
  330. "/* --- END --- */\n"
  331. "\n";
  332. /* The OpenBSD hack is due to lahf and sahf not being recognized by some
  333. versions of binutils: http://marc.info/?l=openbsd-cvs&m=141636589924400
  334. The Apple code is a bit different when calling libc functions because
  335. they are doing relocations differently from everybody else. We also need
  336. to work around the crash issue with .lcomm and the fact that they don't
  337. recognize .string. */
  338. #ifdef __APPLE__
  339. # define CALL_L64(str) "call _" str "\n"
  340. #else
  341. # define CALL_L64(str) "call " str "@PLT\n"
  342. #endif /* ^__APPLE__ */
  343. static const u8* main_payload_64 =
  344. "\n"
  345. "/* --- AFL MAIN PAYLOAD (64-BIT) --- */\n"
  346. "\n"
  347. ".text\n"
  348. ".att_syntax\n"
  349. ".code64\n"
  350. ".align 8\n"
  351. "\n"
  352. "__afl_maybe_log:\n"
  353. "\n"
  354. #if defined(__OpenBSD__) || (defined(__FreeBSD__) && (__FreeBSD__ < 9))
  355. " .byte 0x9f /* lahf */\n"
  356. #else
  357. " lahf\n"
  358. #endif /* ^__OpenBSD__, etc */
  359. " seto %al\n"
  360. "\n"
  361. " /* Check if SHM region is already mapped. */\n"
  362. "\n"
  363. " movq __afl_area_ptr(%rip), %rdx\n"
  364. " testq %rdx, %rdx\n"
  365. " je __afl_setup\n"
  366. "\n"
  367. "__afl_store:\n"
  368. "\n"
  369. " /* Calculate and store hit for the code location specified in rcx. */\n"
  370. "\n"
  371. #ifndef COVERAGE_ONLY
  372. " xorq __afl_prev_loc(%rip), %rcx\n"
  373. " xorq %rcx, __afl_prev_loc(%rip)\n"
  374. " shrq $1, __afl_prev_loc(%rip)\n"
  375. #endif /* ^!COVERAGE_ONLY */
  376. "\n"
  377. #ifdef SKIP_COUNTS
  378. " orb $1, (%rdx, %rcx, 1)\n"
  379. #else
  380. " incb (%rdx, %rcx, 1)\n"
  381. #endif /* ^SKIP_COUNTS */
  382. "\n"
  383. "__afl_return:\n"
  384. "\n"
  385. " addb $127, %al\n"
  386. #if defined(__OpenBSD__) || (defined(__FreeBSD__) && (__FreeBSD__ < 9))
  387. " .byte 0x9e /* sahf */\n"
  388. #else
  389. " sahf\n"
  390. #endif /* ^__OpenBSD__, etc */
  391. " ret\n"
  392. "\n"
  393. ".align 8\n"
  394. "\n"
  395. "__afl_setup:\n"
  396. "\n"
  397. " /* Do not retry setup if we had previous failures. */\n"
  398. "\n"
  399. " cmpb $0, __afl_setup_failure(%rip)\n"
  400. " jne __afl_return\n"
  401. "\n"
  402. " /* Check out if we have a global pointer on file. */\n"
  403. "\n"
  404. #ifndef __APPLE__
  405. " movq __afl_global_area_ptr@GOTPCREL(%rip), %rdx\n"
  406. " movq (%rdx), %rdx\n"
  407. #else
  408. " movq __afl_global_area_ptr(%rip), %rdx\n"
  409. #endif /* !^__APPLE__ */
  410. " testq %rdx, %rdx\n"
  411. " je __afl_setup_first\n"
  412. "\n"
  413. " movq %rdx, __afl_area_ptr(%rip)\n"
  414. " jmp __afl_store\n"
  415. "\n"
  416. "__afl_setup_first:\n"
  417. "\n"
  418. " /* Save everything that is not yet saved and that may be touched by\n"
  419. " getenv() and several other libcalls we'll be relying on. */\n"
  420. "\n"
  421. " leaq -352(%rsp), %rsp\n"
  422. "\n"
  423. " movq %rax, 0(%rsp)\n"
  424. " movq %rcx, 8(%rsp)\n"
  425. " movq %rdi, 16(%rsp)\n"
  426. " movq %rsi, 32(%rsp)\n"
  427. " movq %r8, 40(%rsp)\n"
  428. " movq %r9, 48(%rsp)\n"
  429. " movq %r10, 56(%rsp)\n"
  430. " movq %r11, 64(%rsp)\n"
  431. "\n"
  432. " movq %xmm0, 96(%rsp)\n"
  433. " movq %xmm1, 112(%rsp)\n"
  434. " movq %xmm2, 128(%rsp)\n"
  435. " movq %xmm3, 144(%rsp)\n"
  436. " movq %xmm4, 160(%rsp)\n"
  437. " movq %xmm5, 176(%rsp)\n"
  438. " movq %xmm6, 192(%rsp)\n"
  439. " movq %xmm7, 208(%rsp)\n"
  440. " movq %xmm8, 224(%rsp)\n"
  441. " movq %xmm9, 240(%rsp)\n"
  442. " movq %xmm10, 256(%rsp)\n"
  443. " movq %xmm11, 272(%rsp)\n"
  444. " movq %xmm12, 288(%rsp)\n"
  445. " movq %xmm13, 304(%rsp)\n"
  446. " movq %xmm14, 320(%rsp)\n"
  447. " movq %xmm15, 336(%rsp)\n"
  448. "\n"
  449. " /* Map SHM, jumping to __afl_setup_abort if something goes wrong. */\n"
  450. "\n"
  451. " /* The 64-bit ABI requires 16-byte stack alignment. We'll keep the\n"
  452. " original stack ptr in the callee-saved r12. */\n"
  453. "\n"
  454. " pushq %r12\n"
  455. " movq %rsp, %r12\n"
  456. " subq $16, %rsp\n"
  457. " andq $0xfffffffffffffff0, %rsp\n"
  458. "\n"
  459. " leaq .AFL_SHM_ENV(%rip), %rdi\n"
  460. CALL_L64("getenv")
  461. "\n"
  462. " testq %rax, %rax\n"
  463. " je __afl_setup_abort\n"
  464. "\n"
  465. " movq %rax, %rdi\n"
  466. CALL_L64("atoi")
  467. "\n"
  468. " xorq %rdx, %rdx /* shmat flags */\n"
  469. " xorq %rsi, %rsi /* requested addr */\n"
  470. " movq %rax, %rdi /* SHM ID */\n"
  471. CALL_L64("shmat")
  472. "\n"
  473. " cmpq $-1, %rax\n"
  474. " je __afl_setup_abort\n"
  475. "\n"
  476. " /* Store the address of the SHM region. */\n"
  477. "\n"
  478. " movq %rax, %rdx\n"
  479. " movq %rax, __afl_area_ptr(%rip)\n"
  480. "\n"
  481. #ifdef __APPLE__
  482. " movq %rax, __afl_global_area_ptr(%rip)\n"
  483. #else
  484. " movq __afl_global_area_ptr@GOTPCREL(%rip), %rdx\n"
  485. " movq %rax, (%rdx)\n"
  486. #endif /* ^__APPLE__ */
  487. " movq %rax, %rdx\n"
  488. "\n"
  489. "__afl_forkserver:\n"
  490. "\n"
  491. " /* Enter the fork server mode to avoid the overhead of execve() calls. We\n"
  492. " push rdx (area ptr) twice to keep stack alignment neat. */\n"
  493. "\n"
  494. " pushq %rdx\n"
  495. " pushq %rdx\n"
  496. "\n"
  497. " /* Phone home and tell the parent that we're OK. (Note that signals with\n"
  498. " no SA_RESTART will mess it up). If this fails, assume that the fd is\n"
  499. " closed because we were execve()d from an instrumented binary, or because\n"
  500. " the parent doesn't want to use the fork server. */\n"
  501. "\n"
  502. " movq $4, %rdx /* length */\n"
  503. " leaq __afl_temp(%rip), %rsi /* data */\n"
  504. " movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi /* file desc */\n"
  505. CALL_L64("write")
  506. "\n"
  507. " cmpq $4, %rax\n"
  508. " jne __afl_fork_resume\n"
  509. "\n"
  510. "__afl_fork_wait_loop:\n"
  511. "\n"
  512. " /* Wait for parent by reading from the pipe. Abort if read fails. */\n"
  513. "\n"
  514. " movq $4, %rdx /* length */\n"
  515. " leaq __afl_temp(%rip), %rsi /* data */\n"
  516. " movq $" STRINGIFY(FORKSRV_FD) ", %rdi /* file desc */\n"
  517. CALL_L64("read")
  518. " cmpq $4, %rax\n"
  519. " jne __afl_die\n"
  520. "\n"
  521. " /* Once woken up, create a clone of our process. This is an excellent use\n"
  522. " case for syscall(__NR_clone, 0, CLONE_PARENT), but glibc boneheadedly\n"
  523. " caches getpid() results and offers no way to update the value, breaking\n"
  524. " abort(), raise(), and a bunch of other things :-( */\n"
  525. "\n"
  526. CALL_L64("fork")
  527. " cmpq $0, %rax\n"
  528. " jl __afl_die\n"
  529. " je __afl_fork_resume\n"
  530. "\n"
  531. " /* In parent process: write PID to pipe, then wait for child. */\n"
  532. "\n"
  533. " movl %eax, __afl_fork_pid(%rip)\n"
  534. "\n"
  535. " movq $4, %rdx /* length */\n"
  536. " leaq __afl_fork_pid(%rip), %rsi /* data */\n"
  537. " movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi /* file desc */\n"
  538. CALL_L64("write")
  539. "\n"
  540. " movq $0, %rdx /* no flags */\n"
  541. " leaq __afl_temp(%rip), %rsi /* status */\n"
  542. " movq __afl_fork_pid(%rip), %rdi /* PID */\n"
  543. CALL_L64("waitpid")
  544. " cmpq $0, %rax\n"
  545. " jle __afl_die\n"
  546. "\n"
  547. " /* Relay wait status to pipe, then loop back. */\n"
  548. "\n"
  549. " movq $4, %rdx /* length */\n"
  550. " leaq __afl_temp(%rip), %rsi /* data */\n"
  551. " movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi /* file desc */\n"
  552. CALL_L64("write")
  553. "\n"
  554. " jmp __afl_fork_wait_loop\n"
  555. "\n"
  556. "__afl_fork_resume:\n"
  557. "\n"
  558. " /* In child process: close fds, resume execution. */\n"
  559. "\n"
  560. " movq $" STRINGIFY(FORKSRV_FD) ", %rdi\n"
  561. CALL_L64("close")
  562. "\n"
  563. " movq $" STRINGIFY((FORKSRV_FD + 1)) ", %rdi\n"
  564. CALL_L64("close")
  565. "\n"
  566. " popq %rdx\n"
  567. " popq %rdx\n"
  568. "\n"
  569. " movq %r12, %rsp\n"
  570. " popq %r12\n"
  571. "\n"
  572. " movq 0(%rsp), %rax\n"
  573. " movq 8(%rsp), %rcx\n"
  574. " movq 16(%rsp), %rdi\n"
  575. " movq 32(%rsp), %rsi\n"
  576. " movq 40(%rsp), %r8\n"
  577. " movq 48(%rsp), %r9\n"
  578. " movq 56(%rsp), %r10\n"
  579. " movq 64(%rsp), %r11\n"
  580. "\n"
  581. " movq 96(%rsp), %xmm0\n"
  582. " movq 112(%rsp), %xmm1\n"
  583. " movq 128(%rsp), %xmm2\n"
  584. " movq 144(%rsp), %xmm3\n"
  585. " movq 160(%rsp), %xmm4\n"
  586. " movq 176(%rsp), %xmm5\n"
  587. " movq 192(%rsp), %xmm6\n"
  588. " movq 208(%rsp), %xmm7\n"
  589. " movq 224(%rsp), %xmm8\n"
  590. " movq 240(%rsp), %xmm9\n"
  591. " movq 256(%rsp), %xmm10\n"
  592. " movq 272(%rsp), %xmm11\n"
  593. " movq 288(%rsp), %xmm12\n"
  594. " movq 304(%rsp), %xmm13\n"
  595. " movq 320(%rsp), %xmm14\n"
  596. " movq 336(%rsp), %xmm15\n"
  597. "\n"
  598. " leaq 352(%rsp), %rsp\n"
  599. "\n"
  600. " jmp __afl_store\n"
  601. "\n"
  602. "__afl_die:\n"
  603. "\n"
  604. " xorq %rax, %rax\n"
  605. CALL_L64("_exit")
  606. "\n"
  607. "__afl_setup_abort:\n"
  608. "\n"
  609. " /* Record setup failure so that we don't keep calling\n"
  610. " shmget() / shmat() over and over again. */\n"
  611. "\n"
  612. " incb __afl_setup_failure(%rip)\n"
  613. "\n"
  614. " movq %r12, %rsp\n"
  615. " popq %r12\n"
  616. "\n"
  617. " movq 0(%rsp), %rax\n"
  618. " movq 8(%rsp), %rcx\n"
  619. " movq 16(%rsp), %rdi\n"
  620. " movq 32(%rsp), %rsi\n"
  621. " movq 40(%rsp), %r8\n"
  622. " movq 48(%rsp), %r9\n"
  623. " movq 56(%rsp), %r10\n"
  624. " movq 64(%rsp), %r11\n"
  625. "\n"
  626. " movq 96(%rsp), %xmm0\n"
  627. " movq 112(%rsp), %xmm1\n"
  628. " movq 128(%rsp), %xmm2\n"
  629. " movq 144(%rsp), %xmm3\n"
  630. " movq 160(%rsp), %xmm4\n"
  631. " movq 176(%rsp), %xmm5\n"
  632. " movq 192(%rsp), %xmm6\n"
  633. " movq 208(%rsp), %xmm7\n"
  634. " movq 224(%rsp), %xmm8\n"
  635. " movq 240(%rsp), %xmm9\n"
  636. " movq 256(%rsp), %xmm10\n"
  637. " movq 272(%rsp), %xmm11\n"
  638. " movq 288(%rsp), %xmm12\n"
  639. " movq 304(%rsp), %xmm13\n"
  640. " movq 320(%rsp), %xmm14\n"
  641. " movq 336(%rsp), %xmm15\n"
  642. "\n"
  643. " leaq 352(%rsp), %rsp\n"
  644. "\n"
  645. " jmp __afl_return\n"
  646. "\n"
  647. ".AFL_VARS:\n"
  648. "\n"
  649. #ifdef __APPLE__
  650. " .comm __afl_area_ptr, 8\n"
  651. #ifndef COVERAGE_ONLY
  652. " .comm __afl_prev_loc, 8\n"
  653. #endif /* !COVERAGE_ONLY */
  654. " .comm __afl_fork_pid, 4\n"
  655. " .comm __afl_temp, 4\n"
  656. " .comm __afl_setup_failure, 1\n"
  657. #else
  658. " .lcomm __afl_area_ptr, 8\n"
  659. #ifndef COVERAGE_ONLY
  660. " .lcomm __afl_prev_loc, 8\n"
  661. #endif /* !COVERAGE_ONLY */
  662. " .lcomm __afl_fork_pid, 4\n"
  663. " .lcomm __afl_temp, 4\n"
  664. " .lcomm __afl_setup_failure, 1\n"
  665. #endif /* ^__APPLE__ */
  666. " .comm __afl_global_area_ptr, 8, 8\n"
  667. "\n"
  668. ".AFL_SHM_ENV:\n"
  669. " .asciz \"" SHM_ENV_VAR "\"\n"
  670. "\n"
  671. "/* --- END --- */\n"
  672. "\n";
  673. #endif /* !_HAVE_AFL_AS_H */