memory.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include "wasm_c_api.h"
  6. #define own
  7. wasm_memory_t* get_export_memory(const wasm_extern_vec_t* exports, size_t i) {
  8. if (exports->size <= i || !wasm_extern_as_memory(exports->data[i])) {
  9. printf("> Error accessing memory export %zu!\n", i);
  10. exit(1);
  11. }
  12. return wasm_extern_as_memory(exports->data[i]);
  13. }
  14. wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
  15. if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
  16. printf("> Error accessing function export %zu!\n", i);
  17. exit(1);
  18. }
  19. return wasm_extern_as_func(exports->data[i]);
  20. }
  21. void check(bool success) {
  22. if (!success) {
  23. printf("> Error, expected success\n");
  24. exit(1);
  25. }
  26. }
  27. void check_call(wasm_func_t* func, int i, wasm_val_t args[], int32_t expected) {
  28. wasm_val_vec_t args_vec;
  29. wasm_val_vec_t results_vec;
  30. if (args)
  31. wasm_val_vec_new(&args_vec, i, args);
  32. wasm_val_vec_new(&results_vec, 1, (wasm_val_t []){ WASM_INIT_VAL });
  33. if (wasm_func_call(func, args ? &args_vec : NULL, &results_vec)
  34. || results_vec.data[0].of.i32 != expected) {
  35. printf("> Error on result\n");
  36. exit(1);
  37. }
  38. }
  39. void check_call0(wasm_func_t* func, int32_t expected) {
  40. check_call(func, 0, NULL, expected);
  41. }
  42. void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
  43. wasm_val_t args[] = { WASM_I32_VAL(arg) };
  44. check_call(func, 1, args, expected);
  45. }
  46. void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
  47. wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  48. check_call(func, 2, args, expected);
  49. }
  50. void check_ok(wasm_func_t* func, int i, wasm_val_t args[]) {
  51. wasm_val_vec_t args_vec;
  52. wasm_val_vec_new(&args_vec, i, args);
  53. if (wasm_func_call(func, &args_vec, NULL)) {
  54. printf("> Error on result, expected empty\n");
  55. exit(1);
  56. }
  57. }
  58. void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
  59. wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  60. check_ok(func, 2, args);
  61. }
  62. void check_trap(wasm_func_t* func, int i, wasm_val_t args[]) {
  63. wasm_val_vec_t args_vec, results_vec;
  64. wasm_val_vec_new(&args_vec, i, args);
  65. wasm_val_vec_new(&results_vec, 1, (wasm_val_t []){ WASM_INIT_VAL });
  66. own wasm_trap_t* trap = wasm_func_call(func, &args_vec, &results_vec);
  67. if (! trap) {
  68. printf("> Error on result, expected trap\n");
  69. exit(1);
  70. }
  71. wasm_trap_delete(trap);
  72. }
  73. void check_trap1(wasm_func_t* func, int32_t arg) {
  74. wasm_val_t args[] = { WASM_I32_VAL(arg) };
  75. check_trap(func, 1, args);
  76. }
  77. void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
  78. wasm_val_t args[] = { WASM_I32_VAL(arg1), WASM_I32_VAL(arg2) };
  79. check_trap(func, 2, args);
  80. }
  81. int main(int argc, const char* argv[]) {
  82. // Initialize.
  83. printf("Initializing...\n");
  84. wasm_engine_t* engine = wasm_engine_new();
  85. wasm_store_t* store = wasm_store_new(engine);
  86. // Load binary.
  87. printf("Loading binary...\n");
  88. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_INTERP == 0
  89. FILE* file = fopen("memory.aot", "rb");
  90. #else
  91. FILE* file = fopen("memory.wasm", "rb");
  92. #endif
  93. if (!file) {
  94. printf("> Error loading module!\n");
  95. return 1;
  96. }
  97. fseek(file, 0L, SEEK_END);
  98. size_t file_size = ftell(file);
  99. fseek(file, 0L, SEEK_SET);
  100. wasm_byte_vec_t binary;
  101. wasm_byte_vec_new_uninitialized(&binary, file_size);
  102. if (fread(binary.data, file_size, 1, file) != 1) {
  103. printf("> Error loading module!\n");
  104. fclose(file);
  105. return 1;
  106. }
  107. fclose(file);
  108. // Compile.
  109. printf("Compiling module...\n");
  110. own wasm_module_t* module = wasm_module_new(store, &binary);
  111. if (!module) {
  112. printf("> Error compiling module!\n");
  113. return 1;
  114. }
  115. wasm_byte_vec_delete(&binary);
  116. // Instantiate.
  117. printf("Instantiating module...\n");
  118. own wasm_instance_t* instance =
  119. wasm_instance_new_with_args(store, module, NULL, NULL, KILOBYTE(8), 0);
  120. if (!instance) {
  121. printf("> Error instantiating module!\n");
  122. return 1;
  123. }
  124. // Extract export.
  125. printf("Extracting exports...\n");
  126. own wasm_extern_vec_t exports;
  127. wasm_instance_exports(instance, &exports);
  128. size_t i = 0;
  129. wasm_memory_t* memory = get_export_memory(&exports, i++);
  130. wasm_func_t* size_func = get_export_func(&exports, i++);
  131. wasm_func_t* load_func = get_export_func(&exports, i++);
  132. wasm_func_t* store_func = get_export_func(&exports, i++);
  133. wasm_module_delete(module);
  134. if (!memory || !wasm_memory_data(memory)) {
  135. printf("> Error getting memory!\n");
  136. wasm_extern_vec_delete(&exports);
  137. wasm_instance_delete(instance);
  138. wasm_store_delete(store);
  139. wasm_engine_delete(engine);
  140. return 1;
  141. }
  142. // Try cloning.
  143. own wasm_memory_t* copy = wasm_memory_copy(memory);
  144. assert(wasm_memory_same(memory, copy));
  145. wasm_memory_delete(copy);
  146. // Check initial memory.
  147. printf("Checking memory...\n");
  148. check(wasm_memory_size(memory) >= 2);
  149. check(wasm_memory_data_size(memory) >= 0x20000);
  150. check(wasm_memory_data(memory)[0] == 0);
  151. check(wasm_memory_data(memory)[0x1000] == 1);
  152. check(wasm_memory_data(memory)[0x1003] == 4);
  153. (void)size_func;
  154. check_call1(load_func, 0, 0);
  155. check_call1(load_func, 0x1000, 1);
  156. check_call1(load_func, 0x1003, 4);
  157. check_call1(load_func, 0x1ffff, 0);
  158. check_trap1(load_func, 0x20000);
  159. // Mutate memory.
  160. printf("Mutating memory...\n");
  161. wasm_memory_data(memory)[0x1003] = 5;
  162. check_ok2(store_func, 0x1002, 6);
  163. check_trap2(store_func, 0x20000, 0);
  164. check(wasm_memory_data(memory)[0x1002] == 6);
  165. check(wasm_memory_data(memory)[0x1003] == 5);
  166. check_call1(load_func, 0x1002, 6);
  167. check_call1(load_func, 0x1003, 5);
  168. // Grow memory.
  169. // DO NOT SUPPORT
  170. printf("Bypass Growing memory...\n");
  171. wasm_extern_vec_delete(&exports);
  172. wasm_instance_delete(instance);
  173. // Create stand-alone memory.
  174. // DO NOT SUPPORT
  175. // TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
  176. printf("Bypass Creating stand-alone memory...\n");
  177. // Shut down.
  178. printf("Shutting down...\n");
  179. wasm_store_delete(store);
  180. wasm_engine_delete(engine);
  181. // All done.
  182. printf("Done.\n");
  183. return 0;
  184. }