wasm_mutator_fuzz.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (C) 2019 Intel Corporation. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. #include "wasm_runtime_common.h"
  4. #include "wasm_export.h"
  5. #include "bh_read_file.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <iostream>
  11. #include <vector>
  12. using namespace std;
  13. static bool
  14. is_supported_val_kind(wasm_valkind_t kind)
  15. {
  16. return kind == WASM_I32 || kind == WASM_I64 || kind == WASM_F32
  17. || kind == WASM_F64 || kind == WASM_EXTERNREF
  18. || kind == WASM_FUNCREF;
  19. }
  20. static wasm_val_t
  21. pre_defined_val(wasm_valkind_t kind)
  22. {
  23. if (kind == WASM_I32) {
  24. return wasm_val_t{ .kind = WASM_I32, .of = { .i32 = 2025 } };
  25. }
  26. else if (kind == WASM_I64) {
  27. return wasm_val_t{ .kind = WASM_I64, .of = { .i64 = 168 } };
  28. }
  29. else if (kind == WASM_F32) {
  30. return wasm_val_t{ .kind = WASM_F32, .of = { .f32 = 3.14159f } };
  31. }
  32. else if (kind == WASM_F64) {
  33. return wasm_val_t{ .kind = WASM_F64, .of = { .f64 = 2.71828 } };
  34. }
  35. else if (kind == WASM_EXTERNREF) {
  36. return wasm_val_t{ .kind = WASM_EXTERNREF,
  37. .of = { .foreign = 0xabcddead } };
  38. }
  39. // because aft is_supported_val_kind() check, so we can safely return as
  40. // WASM_FUNCREF
  41. else {
  42. return wasm_val_t{ .kind = WASM_FUNCREF, .of = { .ref = nullptr } };
  43. }
  44. }
  45. void
  46. print_execution_args(const wasm_export_t &export_type,
  47. const std::vector<wasm_val_t> &args, unsigned param_count)
  48. {
  49. std::cout << "[EXECUTION] " << export_type.name << "(";
  50. for (unsigned p_i = 0; p_i < param_count; p_i++) {
  51. if (p_i != 0) {
  52. std::cout << ", ";
  53. }
  54. switch (args[p_i].kind) {
  55. case WASM_I32:
  56. std::cout << "i32:" << args[p_i].of.i32;
  57. break;
  58. case WASM_I64:
  59. std::cout << "i64:" << args[p_i].of.i64;
  60. break;
  61. case WASM_F32:
  62. std::cout << "f32:" << args[p_i].of.f32;
  63. break;
  64. case WASM_F64:
  65. std::cout << "f64:" << args[p_i].of.f64;
  66. break;
  67. case WASM_EXTERNREF:
  68. std::cout << "externref:" << args[p_i].of.foreign;
  69. break;
  70. default:
  71. // because aft is_supported_val_kind() check, so we can safely
  72. // return as WASM_FUNCREF
  73. std::cout << "funcref:" << args[p_i].of.ref;
  74. break;
  75. }
  76. }
  77. std::cout << ")" << std::endl;
  78. }
  79. static bool
  80. execute_export_functions(wasm_module_t module, wasm_module_inst_t inst)
  81. {
  82. int32_t export_count = wasm_runtime_get_export_count(module);
  83. for (int e_i = 0; e_i < export_count; e_i++) {
  84. wasm_export_t export_type = { 0 };
  85. wasm_runtime_get_export_type(module, e_i, &export_type);
  86. if (export_type.kind != WASM_IMPORT_EXPORT_KIND_FUNC) {
  87. continue;
  88. }
  89. wasm_function_inst_t func =
  90. wasm_runtime_lookup_function(inst, export_type.name);
  91. if (!func) {
  92. std::cout << "Failed to lookup function: " << export_type.name
  93. << std::endl;
  94. continue;
  95. }
  96. wasm_func_type_t func_type = export_type.u.func_type;
  97. uint32_t param_count = wasm_func_type_get_param_count(func_type);
  98. /* build arguments */
  99. std::vector<wasm_val_t> args;
  100. for (unsigned p_i = 0; p_i < param_count; p_i++) {
  101. wasm_valkind_t param_type =
  102. wasm_func_type_get_param_valkind(func_type, p_i);
  103. if (!is_supported_val_kind(param_type)) {
  104. std::cout
  105. << "Bypass execution because of unsupported value kind: "
  106. << param_type << std::endl;
  107. return true;
  108. }
  109. wasm_val_t arg = pre_defined_val(param_type);
  110. args.push_back(arg);
  111. }
  112. /* build results storage */
  113. uint32_t result_count = wasm_func_type_get_result_count(func_type);
  114. std::vector<wasm_val_t> results = std::vector<wasm_val_t>(result_count);
  115. print_execution_args(export_type, args, param_count);
  116. /* execute the function */
  117. wasm_exec_env_t exec_env = wasm_runtime_get_exec_env_singleton(inst);
  118. if (!exec_env) {
  119. std::cout << "Failed to get exec env" << std::endl;
  120. return false;
  121. }
  122. bool ret =
  123. wasm_runtime_call_wasm_a(exec_env, func, result_count,
  124. results.data(), param_count, args.data());
  125. if (!ret) {
  126. const char *exception = wasm_runtime_get_exception(inst);
  127. if (!exception) {
  128. std::cout << "[EXECUTION] " << export_type.name
  129. << "() failed. No exception info." << std::endl;
  130. }
  131. else {
  132. std::cout << "[EXECUTION] " << export_type.name << "() failed. "
  133. << exception << std::endl;
  134. }
  135. }
  136. wasm_runtime_clear_exception(inst);
  137. }
  138. return true;
  139. }
  140. extern "C" int
  141. LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
  142. {
  143. /* libfuzzer don't allow us to modify the given Data, so we copy the data
  144. * here */
  145. std::vector<uint8_t> myData(Data, Data + Size);
  146. /* init runtime environment */
  147. wasm_runtime_init();
  148. char error_buf[128] = { 0 };
  149. wasm_module_t module =
  150. wasm_runtime_load((uint8_t *)myData.data(), Size, error_buf, 120);
  151. if (!module) {
  152. std::cout << "[LOADING] " << error_buf << std::endl;
  153. wasm_runtime_destroy();
  154. /* return SUCCESS because the failure has been handled */
  155. return 0;
  156. }
  157. wasm_module_inst_t inst = wasm_runtime_instantiate(
  158. module, 8 * 1024 * 1024, 16 * 1024 * 1024, error_buf, 120);
  159. if (!inst) {
  160. std::cout << "[INSTANTIATE] " << error_buf << std::endl;
  161. wasm_runtime_unload(module);
  162. wasm_runtime_destroy();
  163. /* return SUCCESS because the failure has been handled */
  164. return 0;
  165. }
  166. execute_export_functions(module, inst);
  167. wasm_runtime_deinstantiate(inst);
  168. wasm_runtime_unload(module);
  169. wasm_runtime_destroy();
  170. return 0; /* Values other than 0 and -1 are reserved for future use. */
  171. }
  172. /* Forward-declare the libFuzzer's mutator callback. */
  173. extern "C" size_t
  174. LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
  175. /* The custom mutator: */
  176. #ifdef CUSTOM_MUTATOR
  177. extern "C" size_t
  178. LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize,
  179. unsigned int Seed)
  180. {
  181. if ((NULL != Data) && (Size > 10)) {
  182. int mutate_ret = -1;
  183. /* delete */
  184. if (access("./cur.wasm", 0) == 0) {
  185. remove("./cur.wasm");
  186. }
  187. /* 1.write data to cur.wasm */
  188. FILE *fwrite_fp = fopen("./cur.wasm", "wb");
  189. if (NULL == fwrite_fp) {
  190. printf("Faild to open cur.wasm file!\n");
  191. return 0;
  192. }
  193. fwrite(Data, sizeof(uint8_t), Size, fwrite_fp);
  194. fclose(fwrite_fp);
  195. fwrite_fp = NULL;
  196. /* 2.wasm-tools mutate modify cur.wasm */
  197. char cmd_tmp[150] = { 0 };
  198. /* clang-format off */
  199. const char *preserve_semantic = (Seed % 2) ? "--preserve-semantics" : "";
  200. sprintf(cmd_tmp, "wasm-tools mutate cur.wasm --seed %d -o modified.wasm %s > /dev/null 2>&1", Seed, preserve_semantic);
  201. /* clang-format on */
  202. mutate_ret = system(cmd_tmp);
  203. memset(cmd_tmp, 0, sizeof(cmd_tmp));
  204. if (mutate_ret != 0) {
  205. /* If source file not valid, use libfuzzer's own modifier */
  206. return LLVMFuzzerMutate(Data, Size, MaxSize);
  207. }
  208. /* 3.read modified file */
  209. int read_len = 0;
  210. int file_len = 0;
  211. int res = 0;
  212. uint8_t *buf = NULL;
  213. FILE *fread_fp = fopen("./modified.wasm", "rb");
  214. if (NULL == fread_fp) {
  215. printf("Faild to open modified.wasm file!\n");
  216. exit(0);
  217. }
  218. fseek(fread_fp, 0, SEEK_END); /* location to file end */
  219. file_len = ftell(fread_fp); /* get file size */
  220. buf = (uint8_t *)malloc(file_len);
  221. if (NULL != buf) {
  222. fseek(fread_fp, 0, SEEK_SET); /* location to file start */
  223. read_len = fread(buf, 1, file_len, fread_fp);
  224. if ((read_len == file_len) && (read_len < MaxSize)) {
  225. /* 4.fill Data buffer */
  226. memcpy(Data, buf, read_len);
  227. res = read_len;
  228. }
  229. else {
  230. res = 0;
  231. }
  232. }
  233. else {
  234. res = 0;
  235. }
  236. memset(buf, 0, file_len);
  237. free(buf);
  238. fclose(fread_fp);
  239. fread_fp = NULL;
  240. return res;
  241. }
  242. else {
  243. if (access("./modified.wasm", 0) == 0) {
  244. remove("./modified.wasm");
  245. }
  246. memset(Data, 0, Size);
  247. Size = 0;
  248. return 0;
  249. }
  250. }
  251. #endif // CUSTOM_MUTATOR