test_helper.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #pragma once
  6. #include "wasm_export.h"
  7. #include "gtest/gtest.h"
  8. #include <iostream>
  9. #include <memory>
  10. #include <fstream>
  11. template<int Size = 512 * 1024>
  12. class WAMRRuntimeRAII
  13. {
  14. private:
  15. char global_heap_buf[Size];
  16. RuntimeInitArgs init_args;
  17. public:
  18. WAMRRuntimeRAII()
  19. {
  20. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  21. init_args.mem_alloc_type = Alloc_With_Pool;
  22. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  23. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  24. wasm_runtime_full_init(&init_args);
  25. }
  26. ~WAMRRuntimeRAII() { wasm_runtime_destroy(); }
  27. };
  28. class WAMRModule
  29. {
  30. private:
  31. wasm_module_t module_;
  32. public:
  33. WAMRModule(uint8_t *buffer, uint32_t size)
  34. {
  35. module_ = wasm_runtime_load(buffer, size, NULL, 0);
  36. }
  37. ~WAMRModule() { wasm_runtime_unload(module_); }
  38. wasm_module_t get() const { return module_; }
  39. };
  40. class WAMRInstance
  41. {
  42. private:
  43. wasm_module_inst_t module_inst_;
  44. public:
  45. WAMRInstance(WAMRModule &module, uint32_t stack_size = 8192,
  46. uint32_t heap_size = 8192)
  47. {
  48. module_inst_ = wasm_runtime_instantiate(module.get(), stack_size,
  49. heap_size, NULL, 0);
  50. }
  51. ~WAMRInstance() { wasm_runtime_deinstantiate(module_inst_); }
  52. wasm_module_inst_t get() const { return module_inst_; }
  53. };
  54. class WAMRExecEnv
  55. {
  56. private:
  57. wasm_exec_env_t exec_env_;
  58. public:
  59. WAMRExecEnv(WAMRInstance &instance, uint32_t stack_size = 8192)
  60. {
  61. exec_env_ = wasm_runtime_create_exec_env(instance.get(), stack_size);
  62. }
  63. ~WAMRExecEnv() { wasm_runtime_destroy_exec_env(exec_env_); }
  64. wasm_exec_env_t get() const { return exec_env_; }
  65. wasm_module_inst_t get_inst() const
  66. {
  67. return wasm_runtime_get_module_inst(exec_env_);
  68. }
  69. };
  70. static uint8_t dummy_wasm_buffer[] = {
  71. 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
  72. 0x02, 0x06, 0x08, 0x01, 0x7F, 0x01, 0x41, 0x80, 0x88, 0x04, 0x0B, 0x07,
  73. 0x0A, 0x01, 0x06, 0x6D, 0x65, 0x6D, 0x6F, 0x72, 0x79, 0x02, 0x00, 0x00,
  74. 0x19, 0x04, 0x6E, 0x61, 0x6D, 0x65, 0x07, 0x12, 0x01, 0x00, 0x0F, 0x5F,
  75. 0x5F, 0x73, 0x74, 0x61, 0x63, 0x6B, 0x5F, 0x70, 0x6F, 0x69, 0x6E, 0x74,
  76. 0x65, 0x72, 0x00, 0x76, 0x09, 0x70, 0x72, 0x6F, 0x64, 0x75, 0x63, 0x65,
  77. 0x72, 0x73, 0x01, 0x0C, 0x70, 0x72, 0x6F, 0x63, 0x65, 0x73, 0x73, 0x65,
  78. 0x64, 0x2D, 0x62, 0x79, 0x01, 0x05, 0x63, 0x6C, 0x61, 0x6E, 0x67, 0x56,
  79. 0x31, 0x33, 0x2E, 0x30, 0x2E, 0x30, 0x20, 0x28, 0x68, 0x74, 0x74, 0x70,
  80. 0x73, 0x3A, 0x2F, 0x2F, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2E, 0x63,
  81. 0x6F, 0x6D, 0x2F, 0x6C, 0x6C, 0x76, 0x6D, 0x2F, 0x6C, 0x6C, 0x76, 0x6D,
  82. 0x2D, 0x70, 0x72, 0x6F, 0x6A, 0x65, 0x63, 0x74, 0x20, 0x66, 0x64, 0x31,
  83. 0x64, 0x38, 0x63, 0x32, 0x66, 0x30, 0x34, 0x64, 0x64, 0x65, 0x32, 0x33,
  84. 0x62, 0x65, 0x65, 0x30, 0x66, 0x62, 0x33, 0x61, 0x37, 0x64, 0x30, 0x36,
  85. 0x39, 0x61, 0x39, 0x62, 0x31, 0x30, 0x34, 0x36, 0x64, 0x61, 0x39, 0x37,
  86. 0x39, 0x29
  87. };
  88. class DummyExecEnv
  89. {
  90. private:
  91. std::shared_ptr<WAMRExecEnv> dummy_exec_env_;
  92. std::shared_ptr<WAMRInstance> inst_;
  93. std::shared_ptr<WAMRModule> mod_;
  94. std::vector<uint8_t> my_wasm_buffer;
  95. private:
  96. void construct(uint8_t *buf, uint32_t len)
  97. {
  98. std::vector<uint8_t> buffer(buf, buf + len);
  99. my_wasm_buffer = buffer;
  100. mod_ = std::make_shared<WAMRModule>(my_wasm_buffer.data(),
  101. my_wasm_buffer.size());
  102. EXPECT_NE(mod_.get(), nullptr);
  103. inst_ = std::make_shared<WAMRInstance>(*mod_);
  104. EXPECT_NE(inst_.get(), nullptr);
  105. dummy_exec_env_ = std::make_shared<WAMRExecEnv>(*inst_);
  106. EXPECT_NE(dummy_exec_env_.get(), nullptr);
  107. }
  108. public:
  109. DummyExecEnv() { construct(dummy_wasm_buffer, sizeof(dummy_wasm_buffer)); }
  110. DummyExecEnv(uint8_t *buf, uint32_t len) { construct(buf, len); }
  111. DummyExecEnv(std::string filename)
  112. {
  113. std::ifstream wasm_file(filename, std::ios::binary);
  114. std::vector<uint8_t> buffer(std::istreambuf_iterator<char>(wasm_file),
  115. {});
  116. construct(buffer.data(), buffer.size());
  117. }
  118. ~DummyExecEnv() {}
  119. wasm_exec_env_t get() const { return dummy_exec_env_->get(); }
  120. void *app_to_native(uint32_t app_addr) const
  121. {
  122. return wasm_runtime_addr_app_to_native(inst_->get(), app_addr);
  123. }
  124. uint32_t native_to_app(void *ptr) const
  125. {
  126. return wasm_runtime_addr_native_to_app(inst_->get(), ptr);
  127. }
  128. const char *get_exception() const
  129. {
  130. return wasm_runtime_get_exception(inst_->get());
  131. }
  132. void set_exception(std::string str) const
  133. {
  134. wasm_runtime_set_exception(inst_->get(), str.c_str());
  135. }
  136. void clear_exception() const { wasm_runtime_clear_exception(inst_->get()); }
  137. bool execute(const char *func_name, uint32_t argc, uint32_t argv[])
  138. {
  139. wasm_function_inst_t func;
  140. if (!(func = wasm_runtime_lookup_function(inst_->get(), func_name))) {
  141. return false;
  142. }
  143. return wasm_runtime_call_wasm(dummy_exec_env_->get(), func, argc, argv);
  144. }
  145. };
  146. class WAMRVaList
  147. {
  148. private:
  149. void *buffer_;
  150. uint32_t current_loc_;
  151. uint32_t capacity_;
  152. wasm_exec_env_t exec_env_;
  153. void _append(void *ptr, uint32_t size)
  154. {
  155. if (current_loc_ + size >= capacity_) {
  156. capacity_ *= 2;
  157. buffer_ = realloc(buffer_, capacity_);
  158. ASSERT_NE(buffer_, nullptr);
  159. }
  160. memcpy((void *)((uintptr_t)buffer_ + current_loc_), ptr, size);
  161. current_loc_ += size;
  162. }
  163. public:
  164. explicit WAMRVaList(wasm_exec_env_t exec_env)
  165. : exec_env_(exec_env)
  166. {
  167. capacity_ = 64;
  168. buffer_ = malloc(capacity_);
  169. EXPECT_NE(buffer_, nullptr);
  170. current_loc_ = 0;
  171. }
  172. ~WAMRVaList()
  173. {
  174. current_loc_ = 0;
  175. free(buffer_);
  176. }
  177. template<typename T>
  178. void add(T arg)
  179. {
  180. if (std::is_floating_point<T>::value) {
  181. /* float data should be 8 bytes aligned */
  182. current_loc_ = ((current_loc_ + 7) & ~7);
  183. _append(&arg, sizeof(T));
  184. }
  185. else if (std::is_integral<T>::value) {
  186. if (sizeof(T) > 4) {
  187. current_loc_ = ((current_loc_ + 7) & ~7);
  188. }
  189. _append(&arg, sizeof(T));
  190. }
  191. }
  192. void add(std::string arg)
  193. {
  194. void *native_addr;
  195. auto inst = wasm_runtime_get_module_inst(exec_env_);
  196. uint32_t addr =
  197. wasm_runtime_module_malloc(inst, arg.size() + 1, &native_addr);
  198. ASSERT_NE(addr, 0);
  199. memcpy(native_addr, arg.data(), arg.size());
  200. *(char *)((uintptr_t)native_addr + arg.size()) = 0;
  201. _append(&addr, sizeof(uint32_t));
  202. }
  203. void add(const char *arg) { add(std::string(arg)); }
  204. char *get() const
  205. {
  206. auto inst = wasm_runtime_get_module_inst(exec_env_);
  207. uint32_t addr = wasm_runtime_module_dup_data(
  208. inst, (const char *)buffer_, current_loc_);
  209. EXPECT_NE(addr, 0);
  210. return (char *)wasm_runtime_addr_app_to_native(inst, addr);
  211. }
  212. };
  213. /* Get memory space in app */
  214. class AppMemory
  215. {
  216. private:
  217. wasm_exec_env_t exec_env_;
  218. void *native_addr_;
  219. uint32_t app_addr_;
  220. public:
  221. AppMemory(wasm_exec_env_t exec_env, uint32_t size)
  222. : exec_env_(exec_env)
  223. {
  224. app_addr_ = wasm_runtime_module_malloc(get_module_inst(exec_env_), size,
  225. &native_addr_);
  226. }
  227. ~AppMemory()
  228. {
  229. wasm_runtime_module_free(get_module_inst(exec_env_), app_addr_);
  230. }
  231. void *get_native_addr() const
  232. {
  233. return wasm_runtime_addr_app_to_native(get_module_inst(exec_env_),
  234. app_addr_);
  235. }
  236. uint32_t get_app_addr() const { return app_addr_; }
  237. };
  238. /* Put the data to app */
  239. class AppData
  240. {
  241. private:
  242. wasm_exec_env_t exec_env_;
  243. void *native_addr_;
  244. uint32_t app_addr_;
  245. public:
  246. AppData(wasm_exec_env_t exec_env, void *data, uint32_t size)
  247. : exec_env_(exec_env)
  248. {
  249. app_addr_ = wasm_runtime_module_dup_data(get_module_inst(exec_env_),
  250. (const char *)data, size);
  251. }
  252. AppData(wasm_exec_env_t exec_env, std::string str)
  253. : exec_env_(exec_env)
  254. {
  255. app_addr_ = wasm_runtime_module_dup_data(get_module_inst(exec_env_),
  256. (const char *)str.c_str(),
  257. str.size() + 1);
  258. }
  259. ~AppData()
  260. {
  261. wasm_runtime_module_free(get_module_inst(exec_env_), app_addr_);
  262. }
  263. void *get_native_addr() const
  264. {
  265. return wasm_runtime_addr_app_to_native(get_module_inst(exec_env_),
  266. app_addr_);
  267. }
  268. uint32_t get_app_addr() const { return app_addr_; }
  269. };