FuzzilliJs.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Function.h>
  7. #include <AK/String.h>
  8. #include <AK/StringView.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Interpreter.h>
  11. #include <LibJS/Lexer.h>
  12. #include <LibJS/Parser.h>
  13. #include <LibJS/Runtime/GlobalObject.h>
  14. #include <errno.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <sys/mman.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. //
  23. // BEGIN FUZZING CODE
  24. //
  25. #define REPRL_CRFD 100
  26. #define REPRL_CWFD 101
  27. #define REPRL_DRFD 102
  28. #define REPRL_DWFD 103
  29. #define REPRL_MAX_DATA_SIZE (16 * 1024 * 1024)
  30. #define SHM_SIZE 0x100000
  31. #define MAX_EDGES ((SHM_SIZE - 4) * 8)
  32. #define CHECK(cond) \
  33. if (!(cond)) { \
  34. fprintf(stderr, "\"" #cond "\" failed\n"); \
  35. _exit(-1); \
  36. }
  37. struct shmem_data {
  38. uint32_t num_edges;
  39. unsigned char edges[];
  40. };
  41. struct shmem_data* __shmem;
  42. uint32_t *__edges_start, *__edges_stop;
  43. void __sanitizer_cov_reset_edgeguards()
  44. {
  45. uint64_t N = 0;
  46. for (uint32_t* x = __edges_start; x < __edges_stop && N < MAX_EDGES; x++)
  47. *x = ++N;
  48. }
  49. extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t* start, uint32_t* stop)
  50. {
  51. // Avoid duplicate initialization
  52. if (start == stop || *start)
  53. return;
  54. if (__edges_start != NULL || __edges_stop != NULL) {
  55. fprintf(stderr, "Coverage instrumentation is only supported for a single module\n");
  56. _exit(-1);
  57. }
  58. __edges_start = start;
  59. __edges_stop = stop;
  60. // Map the shared memory region
  61. const char* shm_key = getenv("SHM_ID");
  62. if (!shm_key) {
  63. puts("[COV] no shared memory bitmap available, skipping");
  64. __shmem = (struct shmem_data*)malloc(SHM_SIZE);
  65. } else {
  66. int fd = shm_open(shm_key, O_RDWR, S_IREAD | S_IWRITE);
  67. if (fd <= -1) {
  68. fprintf(stderr, "Failed to open shared memory region: %s\n", strerror(errno));
  69. _exit(-1);
  70. }
  71. __shmem = (struct shmem_data*)mmap(0, SHM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  72. if (__shmem == MAP_FAILED) {
  73. fprintf(stderr, "Failed to mmap shared memory region\n");
  74. _exit(-1);
  75. }
  76. }
  77. __sanitizer_cov_reset_edgeguards();
  78. __shmem->num_edges = stop - start;
  79. printf("[COV] edge counters initialized. Shared memory: %s with %u edges\n", shm_key, __shmem->num_edges);
  80. }
  81. extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t* guard)
  82. {
  83. // There's a small race condition here: if this function executes in two threads for the same
  84. // edge at the same time, the first thread might disable the edge (by setting the guard to zero)
  85. // before the second thread fetches the guard value (and thus the index). However, our
  86. // instrumentation ignores the first edge (see libcoverage.c) and so the race is unproblematic.
  87. uint32_t index = *guard;
  88. // If this function is called before coverage instrumentation is properly initialized we want to return early.
  89. if (!index)
  90. return;
  91. __shmem->edges[index / 8] |= 1 << (index % 8);
  92. *guard = 0;
  93. }
  94. //
  95. // END FUZZING CODE
  96. //
  97. class TestRunnerGlobalObject final : public JS::GlobalObject {
  98. JS_OBJECT(TestRunnerGlobalObject, JS::GlobalObject);
  99. public:
  100. TestRunnerGlobalObject();
  101. virtual ~TestRunnerGlobalObject() override;
  102. virtual void initialize_global_object() override;
  103. private:
  104. JS_DECLARE_OLD_NATIVE_FUNCTION(fuzzilli);
  105. };
  106. TestRunnerGlobalObject::TestRunnerGlobalObject()
  107. {
  108. }
  109. TestRunnerGlobalObject::~TestRunnerGlobalObject()
  110. {
  111. }
  112. JS_DEFINE_OLD_NATIVE_FUNCTION(TestRunnerGlobalObject::fuzzilli)
  113. {
  114. if (!vm.argument_count())
  115. return JS::js_undefined();
  116. auto operation = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
  117. if (operation == "FUZZILLI_CRASH") {
  118. auto type = TRY_OR_DISCARD(vm.argument(1).to_i32(global_object));
  119. switch (type) {
  120. case 0:
  121. *((int*)0x41414141) = 0x1337;
  122. break;
  123. default:
  124. VERIFY_NOT_REACHED();
  125. break;
  126. }
  127. } else if (operation == "FUZZILLI_PRINT") {
  128. static FILE* fzliout = fdopen(REPRL_DWFD, "w");
  129. if (!fzliout) {
  130. dbgln("Fuzzer output not available");
  131. fzliout = stdout;
  132. }
  133. auto string = TRY_OR_DISCARD(vm.argument(1).to_string(global_object));
  134. fprintf(fzliout, "%s\n", string.characters());
  135. fflush(fzliout);
  136. }
  137. return JS::js_undefined();
  138. }
  139. void TestRunnerGlobalObject::initialize_global_object()
  140. {
  141. Base::initialize_global_object();
  142. define_direct_property("global", this, JS::Attribute::Enumerable);
  143. define_native_function("fuzzilli", fuzzilli, 2, JS::default_attributes);
  144. }
  145. int main(int, char**)
  146. {
  147. char* reprl_input = nullptr;
  148. char helo[] = "HELO";
  149. if (write(REPRL_CWFD, helo, 4) != 4 || read(REPRL_CRFD, helo, 4) != 4) {
  150. VERIFY_NOT_REACHED();
  151. }
  152. VERIFY(memcmp(helo, "HELO", 4) == 0);
  153. reprl_input = (char*)mmap(0, REPRL_MAX_DATA_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, REPRL_DRFD, 0);
  154. VERIFY(reprl_input != MAP_FAILED);
  155. auto vm = JS::VM::create();
  156. auto interpreter = JS::Interpreter::create<TestRunnerGlobalObject>(*vm);
  157. while (true) {
  158. unsigned action;
  159. VERIFY(read(REPRL_CRFD, &action, 4) == 4);
  160. VERIFY(action == 'cexe');
  161. size_t script_size;
  162. VERIFY(read(REPRL_CRFD, &script_size, 8) == 8);
  163. VERIFY(script_size < REPRL_MAX_DATA_SIZE);
  164. ByteBuffer data_buffer;
  165. data_buffer.resize(script_size);
  166. VERIFY(data_buffer.size() >= script_size);
  167. memcpy(data_buffer.data(), reprl_input, script_size);
  168. int result = 0;
  169. auto js = StringView(static_cast<const unsigned char*>(data_buffer.data()), script_size);
  170. auto lexer = JS::Lexer(js);
  171. auto parser = JS::Parser(lexer);
  172. auto program = parser.parse_program();
  173. if (parser.has_errors()) {
  174. result = 1;
  175. } else {
  176. interpreter->run(interpreter->global_object(), *program);
  177. if (interpreter->exception()) {
  178. result = 1;
  179. vm->clear_exception();
  180. }
  181. }
  182. fflush(stdout);
  183. fflush(stderr);
  184. int status = (result & 0xff) << 8;
  185. VERIFY(write(REPRL_CWFD, &status, 4) == 4);
  186. __sanitizer_cov_reset_edgeguards();
  187. }
  188. return 0;
  189. }