memory.c 6.1 KB

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