memory.c 6.3 KB

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