wasm_runtime.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_runtime.h"
  6. #include "wasm_thread.h"
  7. #include "wasm_loader.h"
  8. #include "wasm_native.h"
  9. #include "wasm_interp.h"
  10. #include "wasm_log.h"
  11. #include "wasm_platform_log.h"
  12. #include "wasm_memory.h"
  13. #include "mem_alloc.h"
  14. #include "bh_common.h"
  15. static void
  16. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  17. {
  18. if (error_buf != NULL)
  19. snprintf(error_buf, error_buf_size, "%s", string);
  20. }
  21. bool
  22. wasm_runtime_init()
  23. {
  24. if (bh_platform_init() != 0)
  25. return false;
  26. if (wasm_log_init() != 0)
  27. return false;
  28. if (ws_thread_sys_init() != 0)
  29. return false;
  30. wasm_native_init();
  31. return true;
  32. }
  33. void
  34. wasm_runtime_destroy()
  35. {
  36. ws_thread_sys_destroy();
  37. }
  38. static void
  39. init_wasm_stack(WASMStack *wasm_stack, uint8 *stack, uint32 stack_size)
  40. {
  41. wasm_stack->top = wasm_stack->bottom = stack;
  42. wasm_stack->top_boundary = stack + stack_size;
  43. }
  44. bool
  45. wasm_runtime_call_wasm(WASMModuleInstance *module_inst,
  46. WASMExecEnv *exec_env,
  47. WASMFunctionInstance *function,
  48. unsigned argc, uint32 argv[])
  49. {
  50. /* Only init stack when no application is running. */
  51. if (!module_inst->main_tlr.cur_frame) {
  52. if (!exec_env) {
  53. if (!module_inst->wasm_stack) {
  54. if (!(module_inst->wasm_stack =
  55. wasm_malloc(module_inst->wasm_stack_size))) {
  56. wasm_runtime_set_exception(module_inst,
  57. "allocate memory failed.");
  58. return false;
  59. }
  60. init_wasm_stack(&module_inst->main_tlr.wasm_stack,
  61. module_inst->wasm_stack,
  62. module_inst->wasm_stack_size);
  63. }
  64. }
  65. else {
  66. uintptr_t stack = (uintptr_t)exec_env->stack;
  67. uint32 stack_size;
  68. /* Set to 8 bytes align */
  69. stack = (stack + 7) & (uintptr_t)~7;
  70. stack_size = (uint32)(exec_env->stack_size
  71. - (stack - (uintptr_t)exec_env->stack));
  72. if (!exec_env->stack || exec_env->stack_size <= 0
  73. || exec_env->stack_size < stack - (uintptr_t)exec_env->stack) {
  74. wasm_runtime_set_exception(module_inst,
  75. "Invalid execution stack info.");
  76. return false;
  77. }
  78. init_wasm_stack(&module_inst->main_tlr.wasm_stack,
  79. (uint8*)stack, stack_size);
  80. }
  81. }
  82. wasm_interp_call_wasm(module_inst, function, argc, argv);
  83. return !wasm_runtime_get_exception(module_inst) ? true : false;
  84. }
  85. void
  86. wasm_runtime_set_exception(WASMModuleInstance *module_inst,
  87. const char *exception)
  88. {
  89. if (exception)
  90. snprintf(module_inst->cur_exception,
  91. sizeof(module_inst->cur_exception),
  92. "Exception: %s", exception);
  93. else
  94. module_inst->cur_exception[0] = '\0';
  95. }
  96. const char*
  97. wasm_runtime_get_exception(WASMModuleInstance *module_inst)
  98. {
  99. if (module_inst->cur_exception[0] == '\0')
  100. return NULL;
  101. else
  102. return module_inst->cur_exception;
  103. }
  104. void
  105. wasm_runtime_clear_exception(WASMModuleInstance *module_inst)
  106. {
  107. wasm_runtime_set_exception(module_inst, NULL);
  108. }
  109. WASMModule*
  110. wasm_runtime_load(const uint8 *buf, uint32 size,
  111. char *error_buf, uint32 error_buf_size)
  112. {
  113. return wasm_loader_load(buf, size, error_buf, error_buf_size);
  114. }
  115. WASMModule*
  116. wasm_runtime_load_from_sections(WASMSection *section_list,
  117. char *error_buf, uint32_t error_buf_size)
  118. {
  119. return wasm_loader_load_from_sections(section_list,
  120. error_buf, error_buf_size);
  121. }
  122. void
  123. wasm_runtime_unload(WASMModule *module)
  124. {
  125. wasm_loader_unload(module);
  126. }
  127. /**
  128. * Destroy memory instances.
  129. */
  130. static void
  131. memories_deinstantiate(WASMMemoryInstance **memories, uint32 count)
  132. {
  133. uint32 i;
  134. if (memories) {
  135. for (i = 0; i < count; i++)
  136. if (memories[i]) {
  137. if (memories[i]->heap_handle)
  138. mem_allocator_destroy(memories[i]->heap_handle);
  139. wasm_free(memories[i]->heap_data);
  140. wasm_free(memories[i]);
  141. }
  142. wasm_free(memories);
  143. }
  144. }
  145. static WASMMemoryInstance*
  146. memory_instantiate(uint32 init_page_count, uint32 max_page_count,
  147. uint32 addr_data_size, uint32 global_data_size,
  148. uint32 heap_size,
  149. char *error_buf, uint32 error_buf_size)
  150. {
  151. WASMMemoryInstance *memory;
  152. uint64 total_size = offsetof(WASMMemoryInstance, base_addr) +
  153. NumBytesPerPage * (uint64)init_page_count +
  154. addr_data_size + global_data_size;
  155. /* Allocate memory space, addr data and global data */
  156. if (total_size >= UINT32_MAX
  157. || !(memory = wasm_malloc((uint32)total_size))) {
  158. set_error_buf(error_buf, error_buf_size,
  159. "Instantiate memory failed: allocate memory failed.");
  160. return NULL;
  161. }
  162. memset(memory, 0, (uint32)total_size);
  163. memory->cur_page_count = init_page_count;
  164. memory->max_page_count = max_page_count;
  165. memory->addr_data = memory->base_addr;
  166. memory->addr_data_size = addr_data_size;
  167. memory->memory_data = memory->addr_data + addr_data_size;
  168. memory->global_data = memory->memory_data +
  169. NumBytesPerPage * memory->cur_page_count;;
  170. memory->global_data_size = global_data_size;
  171. memory->end_addr = memory->global_data + global_data_size;
  172. /* Allocate heap space */
  173. if (!(memory->heap_data = wasm_malloc(heap_size))) {
  174. set_error_buf(error_buf, error_buf_size,
  175. "Instantiate memory failed: allocate memory failed.");
  176. goto fail1;
  177. }
  178. memory->heap_data_end = memory->heap_data + heap_size;
  179. /* Initialize heap */
  180. if (!(memory->heap_handle = mem_allocator_create
  181. (memory->heap_data, heap_size))) {
  182. goto fail2;
  183. }
  184. #if WASM_ENABLE_MEMORY_GROW != 0
  185. memory->heap_base_offset = DEFAULT_APP_HEAP_BASE_OFFSET;
  186. #else
  187. memory->heap_base_offset = memory->end_addr - memory->memory_data;
  188. #endif
  189. return memory;
  190. fail2:
  191. wasm_free(memory->heap_data);
  192. fail1:
  193. wasm_free(memory);
  194. return NULL;
  195. }
  196. /**
  197. * Instantiate memories in a module.
  198. */
  199. static WASMMemoryInstance**
  200. memories_instantiate(const WASMModule *module, uint32 addr_data_size,
  201. uint32 global_data_size, uint32 heap_size,
  202. char *error_buf, uint32 error_buf_size)
  203. {
  204. WASMImport *import;
  205. uint32 mem_index = 0, i, memory_count =
  206. module->import_memory_count + module->memory_count;
  207. uint64 total_size;
  208. WASMMemoryInstance **memories, *memory;
  209. if (memory_count == 0 && global_data_size > 0)
  210. memory_count = 1;
  211. total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
  212. if (total_size >= UINT32_MAX
  213. || !(memories = wasm_malloc((uint32)total_size))) {
  214. set_error_buf(error_buf, error_buf_size,
  215. "Instantiate memory failed: "
  216. "allocate memory failed.");
  217. return NULL;
  218. }
  219. memset(memories, 0, (uint32)total_size);
  220. /* instantiate memories from import section */
  221. import = module->import_memories;
  222. for (i = 0; i < module->import_memory_count; i++, import++) {
  223. if (!(memory = memories[mem_index++] =
  224. memory_instantiate(import->u.memory.init_page_count,
  225. import->u.memory. max_page_count,
  226. addr_data_size, global_data_size,
  227. heap_size, error_buf, error_buf_size))) {
  228. set_error_buf(error_buf, error_buf_size,
  229. "Instantiate memory failed: "
  230. "allocate memory failed.");
  231. memories_deinstantiate(memories, memory_count);
  232. return NULL;
  233. }
  234. }
  235. /* instantiate memories from memory section */
  236. for (i = 0; i < module->memory_count; i++) {
  237. if (!(memory = memories[mem_index++] =
  238. memory_instantiate(module->memories[i].init_page_count,
  239. module->memories[i].max_page_count,
  240. addr_data_size, global_data_size,
  241. heap_size, error_buf, error_buf_size))) {
  242. set_error_buf(error_buf, error_buf_size,
  243. "Instantiate memory failed: "
  244. "allocate memory failed.");
  245. memories_deinstantiate(memories, memory_count);
  246. return NULL;
  247. }
  248. }
  249. if (mem_index == 0) {
  250. /* no import memory and define memory, but has global variables */
  251. if (!(memory = memories[mem_index++] =
  252. memory_instantiate(0, 0, addr_data_size, global_data_size,
  253. heap_size, error_buf, error_buf_size))) {
  254. set_error_buf(error_buf, error_buf_size,
  255. "Instantiate memory failed: "
  256. "allocate memory failed.\n");
  257. memories_deinstantiate(memories, memory_count);
  258. return NULL;
  259. }
  260. }
  261. wasm_assert(mem_index == memory_count);
  262. return memories;
  263. }
  264. /**
  265. * Destroy table instances.
  266. */
  267. static void
  268. tables_deinstantiate(WASMTableInstance **tables, uint32 count)
  269. {
  270. uint32 i;
  271. if (tables) {
  272. for (i = 0; i < count; i++)
  273. if (tables[i])
  274. wasm_free(tables[i]);
  275. wasm_free(tables);
  276. }
  277. }
  278. /**
  279. * Instantiate tables in a module.
  280. */
  281. static WASMTableInstance**
  282. tables_instantiate(const WASMModule *module,
  283. char *error_buf, uint32 error_buf_size)
  284. {
  285. WASMImport *import;
  286. uint32 table_index = 0, i, table_count =
  287. module->import_table_count + module->table_count;
  288. uint64 total_size = sizeof(WASMTableInstance*) * (uint64)table_count;
  289. WASMTableInstance **tables, *table;
  290. if (total_size >= UINT32_MAX
  291. || !(tables = wasm_malloc((uint32)total_size))) {
  292. set_error_buf(error_buf, error_buf_size,
  293. "Instantiate table failed: "
  294. "allocate memory failed.");
  295. return NULL;
  296. }
  297. memset(tables, 0, (uint32)total_size);
  298. /* instantiate tables from import section */
  299. import = module->import_tables;
  300. for (i = 0; i < module->import_table_count; i++, import++) {
  301. total_size = offsetof(WASMTableInstance, base_addr) +
  302. sizeof(uint32) * (uint64)import->u.table.init_size;
  303. if (total_size >= UINT32_MAX
  304. || !(table = tables[table_index++] = wasm_malloc((uint32)total_size))) {
  305. set_error_buf(error_buf, error_buf_size,
  306. "Instantiate table failed: "
  307. "allocate memory failed.");
  308. tables_deinstantiate(tables, table_count);
  309. return NULL;
  310. }
  311. memset(table, 0, (uint32)total_size);
  312. table->cur_size = import->u.table.init_size;
  313. table->max_size = import->u.table.max_size;
  314. }
  315. /* instantiate tables from table section */
  316. for (i = 0; i < module->table_count; i++) {
  317. total_size = offsetof(WASMTableInstance, base_addr) +
  318. sizeof(uint32) * (uint64)module->tables[i].init_size;
  319. if (total_size >= UINT32_MAX
  320. || !(table = tables[table_index++] = wasm_malloc((uint32)total_size))) {
  321. set_error_buf(error_buf, error_buf_size,
  322. "Instantiate table failed: "
  323. "allocate memory failed.");
  324. tables_deinstantiate(tables, table_count);
  325. return NULL;
  326. }
  327. memset(table, 0, (uint32)total_size);
  328. table->cur_size = module->tables[i].init_size;
  329. table->max_size = module->tables[i].max_size;
  330. }
  331. wasm_assert(table_index == table_count);
  332. return tables;
  333. }
  334. /**
  335. * Destroy function instances.
  336. */
  337. static void
  338. functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
  339. {
  340. if (functions) {
  341. uint32 i;
  342. for (i = 0; i < count; i++)
  343. if (functions[i].local_offsets)
  344. wasm_free(functions[i].local_offsets);
  345. wasm_free(functions);
  346. }
  347. }
  348. static bool
  349. function_init_local_offsets(WASMFunctionInstance *func)
  350. {
  351. uint32 local_offset = 0;
  352. WASMType *param_type = func->u.func->func_type;
  353. uint32 param_count = param_type->param_count;
  354. uint8 *param_types = param_type->types;
  355. uint32 local_count = func->u.func->local_count;
  356. uint8 *local_types = func->u.func->local_types;
  357. uint32 i;
  358. uint64 total_size = sizeof(uint16) * (uint64)(param_count + local_count);
  359. if (total_size >= UINT32_MAX
  360. || !(func->local_offsets = wasm_malloc((uint32)total_size)))
  361. return false;
  362. for (i = 0; i < param_count; i++) {
  363. func->local_offsets[i] = (uint16)local_offset;
  364. local_offset += wasm_value_type_cell_num(param_types[i]);
  365. }
  366. for (i = 0; i < local_count; i++) {
  367. func->local_offsets[param_count + i] = (uint16)local_offset;
  368. local_offset += wasm_value_type_cell_num(local_types[i]);
  369. }
  370. wasm_assert(local_offset == func->param_cell_num + func->local_cell_num);
  371. return true;
  372. }
  373. /**
  374. * Instantiate functions in a module.
  375. */
  376. static WASMFunctionInstance*
  377. functions_instantiate(const WASMModule *module,
  378. char *error_buf, uint32 error_buf_size)
  379. {
  380. WASMImport *import;
  381. uint32 i, function_count =
  382. module->import_function_count + module->function_count;
  383. uint64 total_size = sizeof(WASMFunctionInstance) * (uint64)function_count;
  384. WASMFunctionInstance *functions, *function;
  385. if (total_size >= UINT32_MAX
  386. || !(functions = wasm_malloc((uint32)total_size))) {
  387. set_error_buf(error_buf, error_buf_size,
  388. "Instantiate function failed: "
  389. "allocate memory failed.");
  390. return NULL;
  391. }
  392. memset(functions, 0, (uint32)total_size);
  393. /* instantiate functions from import section */
  394. function = functions;
  395. import = module->import_functions;
  396. for (i = 0; i < module->import_function_count; i++, import++) {
  397. function->is_import_func = true;
  398. function->u.func_import = &import->u.function;
  399. function->param_cell_num =
  400. wasm_type_param_cell_num(import->u.function.func_type);
  401. function->ret_cell_num =
  402. wasm_type_return_cell_num(import->u.function.func_type);
  403. function->local_cell_num = 0;
  404. function->param_count =
  405. (uint16)function->u.func_import->func_type->param_count;
  406. function->local_count = 0;
  407. function->param_types = function->u.func_import->func_type->types;
  408. function->local_types = NULL;
  409. function++;
  410. }
  411. /* instantiate functions from function section */
  412. for (i = 0; i < module->function_count; i++) {
  413. function->is_import_func = false;
  414. function->u.func = module->functions[i];
  415. function->param_cell_num =
  416. wasm_type_param_cell_num(function->u.func->func_type);
  417. function->ret_cell_num =
  418. wasm_type_return_cell_num(function->u.func->func_type);
  419. function->local_cell_num =
  420. wasm_get_cell_num(function->u.func->local_types,
  421. function->u.func->local_count);
  422. function->param_count = (uint16)function->u.func->func_type->param_count;
  423. function->local_count = (uint16)function->u.func->local_count;
  424. function->param_types = function->u.func->func_type->types;
  425. function->local_types = function->u.func->local_types;
  426. if (!function_init_local_offsets(function)) {
  427. functions_deinstantiate(functions, function_count);
  428. return NULL;
  429. }
  430. function++;
  431. }
  432. wasm_assert((uint32)(function - functions) == function_count);
  433. return functions;
  434. }
  435. /**
  436. * Destroy global instances.
  437. */
  438. static void
  439. globals_deinstantiate(WASMGlobalInstance *globals)
  440. {
  441. if (globals)
  442. wasm_free(globals);
  443. }
  444. /**
  445. * Instantiate globals in a module.
  446. */
  447. static WASMGlobalInstance*
  448. globals_instantiate(const WASMModule *module,
  449. uint32 *p_addr_data_size,
  450. uint32 *p_global_data_size,
  451. char *error_buf, uint32 error_buf_size)
  452. {
  453. WASMImport *import;
  454. uint32 addr_data_offset = 0, global_data_offset = 0;
  455. uint32 i, global_count =
  456. module->import_global_count + module->global_count;
  457. uint64 total_size = sizeof(WASMGlobalInstance) * (uint64)global_count;
  458. WASMGlobalInstance *globals, *global;
  459. if (total_size >= UINT32_MAX
  460. || !(globals = wasm_malloc((uint32)total_size))) {
  461. set_error_buf(error_buf, error_buf_size,
  462. "Instantiate global failed: "
  463. "allocate memory failed.");
  464. return NULL;
  465. }
  466. memset(globals, 0, (uint32)total_size);
  467. /* instantiate globals from import section */
  468. global = globals;
  469. import = module->import_globals;
  470. for (i = 0; i < module->import_global_count; i++, import++) {
  471. WASMGlobalImport *global_import = &import->u.global;
  472. global->type = global_import->type;
  473. global->is_mutable = global_import->is_mutable;
  474. global->is_addr = global_import->is_addr;
  475. global->initial_value = global_import->global_data_linked;
  476. global->data_offset = global_data_offset;
  477. global_data_offset += wasm_value_type_size(global->type);
  478. if (global->is_addr)
  479. addr_data_offset += (uint32)sizeof(uint32);
  480. global++;
  481. }
  482. /* instantiate globals from global section */
  483. for (i = 0; i < module->global_count; i++) {
  484. global->type = module->globals[i].type;
  485. global->is_mutable = module->globals[i].is_mutable;
  486. global->is_addr = module->globals[i].is_addr;
  487. global->data_offset = global_data_offset;
  488. global_data_offset += wasm_value_type_size(global->type);
  489. if (global->is_addr)
  490. addr_data_offset += (uint32)sizeof(uint32);
  491. global++;
  492. }
  493. wasm_assert((uint32)(global - globals) == global_count);
  494. *p_addr_data_size = addr_data_offset;
  495. *p_global_data_size = global_data_offset;
  496. return globals;
  497. }
  498. static void
  499. globals_instantiate_fix(WASMGlobalInstance *globals,
  500. const WASMModule *module,
  501. WASMModuleInstance *module_inst)
  502. {
  503. WASMGlobalInstance *global = globals;
  504. WASMImport *import = module->import_globals;
  505. uint32 i;
  506. /* Fix globals from import section */
  507. for (i = 0; i < module->import_global_count; i++, import++, global++) {
  508. if (!strcmp(import->u.names.module_name, "env")) {
  509. if (!strcmp(import->u.names.field_name, "memoryBase")
  510. || !strcmp(import->u.names.field_name, "__memory_base")) {
  511. global->initial_value.addr = 0;
  512. }
  513. else if (!strcmp(import->u.names.field_name, "tableBase")
  514. || !strcmp(import->u.names.field_name, "__table_base")) {
  515. global->initial_value.addr = 0;
  516. }
  517. else if (!strcmp(import->u.names.field_name, "DYNAMICTOP_PTR")) {
  518. global->initial_value.i32 =
  519. (int32)(NumBytesPerPage * module_inst->default_memory->cur_page_count);
  520. module_inst->DYNAMICTOP_PTR_offset = global->data_offset;
  521. }
  522. else if (!strcmp(import->u.names.field_name, "STACKTOP")) {
  523. global->initial_value.i32 = 0;
  524. }
  525. else if (!strcmp(import->u.names.field_name, "STACK_MAX")) {
  526. /* Unused in emcc wasm bin actually. */
  527. global->initial_value.i32 = 0;
  528. }
  529. }
  530. }
  531. for (i = 0; i < module->global_count; i++) {
  532. InitializerExpression *init_expr = &module->globals[i].init_expr;
  533. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  534. wasm_assert(init_expr->u.global_index < module->import_global_count);
  535. global->initial_value = globals[init_expr->u.global_index].initial_value;
  536. }
  537. else {
  538. bh_memcpy_s(&global->initial_value, sizeof(WASMValue),
  539. &init_expr->u, sizeof(init_expr->u));
  540. }
  541. global++;
  542. }
  543. }
  544. /**
  545. * Return export function count in module export section.
  546. */
  547. static uint32
  548. get_export_function_count(const WASMModule *module)
  549. {
  550. WASMExport *export = module->exports;
  551. uint32 count = 0, i;
  552. for (i = 0; i < module->export_count; i++, export++)
  553. if (export->kind == EXPORT_KIND_FUNC)
  554. count++;
  555. return count;
  556. }
  557. /**
  558. * Destroy export function instances.
  559. */
  560. static void
  561. export_functions_deinstantiate(WASMExportFuncInstance *functions)
  562. {
  563. if (functions)
  564. wasm_free(functions);
  565. }
  566. /**
  567. * Instantiate export functions in a module.
  568. */
  569. static WASMExportFuncInstance*
  570. export_functions_instantiate(const WASMModule *module,
  571. WASMModuleInstance *module_inst,
  572. uint32 export_func_count,
  573. char *error_buf, uint32 error_buf_size)
  574. {
  575. WASMExportFuncInstance *export_funcs, *export_func;
  576. WASMExport *export = module->exports;
  577. uint32 i;
  578. uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
  579. if (total_size >= UINT32_MAX
  580. || !(export_func = export_funcs = wasm_malloc((uint32)total_size))) {
  581. set_error_buf(error_buf, error_buf_size,
  582. "Instantiate export function failed: "
  583. "allocate memory failed.");
  584. return NULL;
  585. }
  586. memset(export_funcs, 0, (uint32)total_size);
  587. for (i = 0; i < module->export_count; i++, export++)
  588. if (export->kind == EXPORT_KIND_FUNC) {
  589. wasm_assert(export->index >= module->import_function_count
  590. && export->index < module->import_function_count
  591. + module->function_count);
  592. export_func->name = export->name;
  593. export_func->function = &module_inst->functions[export->index];
  594. export_func++;
  595. }
  596. wasm_assert((uint32)(export_func - export_funcs) == export_func_count);
  597. return export_funcs;
  598. }
  599. void
  600. wasm_runtime_deinstantiate(WASMModuleInstance *module_inst);
  601. static bool
  602. execute_post_inst_function(WASMModuleInstance *module_inst)
  603. {
  604. WASMFunctionInstance *post_inst_func = NULL;
  605. WASMType *post_inst_func_type;
  606. uint32 i;
  607. for (i = 0; i < module_inst->export_func_count; i++)
  608. if (!strcmp(module_inst->export_functions[i].name, "__post_instantiate")) {
  609. post_inst_func = module_inst->export_functions[i].function;
  610. break;
  611. }
  612. if (!post_inst_func)
  613. /* Not found */
  614. return true;
  615. post_inst_func_type = post_inst_func->u.func->func_type;
  616. if (post_inst_func_type->param_count != 0
  617. || post_inst_func_type->result_count != 0)
  618. /* Not a valid function type, ignore it */
  619. return true;
  620. return wasm_runtime_call_wasm(module_inst, NULL, post_inst_func, 0, NULL);
  621. }
  622. static bool
  623. execute_start_function(WASMModuleInstance *module_inst)
  624. {
  625. WASMFunctionInstance *func = module_inst->start_function;
  626. if (!func)
  627. return true;
  628. wasm_assert(!func->is_import_func && func->param_cell_num == 0
  629. && func->ret_cell_num == 0);
  630. return wasm_runtime_call_wasm(module_inst, NULL, func, 0, NULL);
  631. }
  632. #if WASM_ENABLE_WASI != 0
  633. static bool
  634. wasm_runtime_init_wasi(WASMModuleInstance *module_inst,
  635. const char *dir_list[], uint32 dir_count,
  636. const char *map_dir_list[], uint32 map_dir_count,
  637. const char *env[], uint32 env_count,
  638. const char *argv[], uint32 argc,
  639. char *error_buf, uint32 error_buf_size)
  640. {
  641. size_t *argv_offsets = NULL;
  642. char *argv_buf = NULL;
  643. size_t *env_offsets = NULL;
  644. char *env_buf = NULL;
  645. uint64 argv_buf_len = 0, env_buf_len = 0;
  646. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  647. struct fd_table *curfds;
  648. struct fd_prestats *prestats;
  649. struct argv_environ_values *argv_environ;
  650. int32 offset_argv_offsets = 0, offset_env_offsets = 0;
  651. int32 offset_argv_buf = 0, offset_env_buf = 0;
  652. int32 offset_curfds = 0;
  653. int32 offset_prestats = 0;
  654. int32 offset_argv_environ = 0;
  655. __wasi_fd_t wasm_fd = 3;
  656. int32 raw_fd;
  657. char *path, resolved_path[PATH_MAX];
  658. uint64 total_size;
  659. uint32 i;
  660. if (!module_inst->default_memory) {
  661. argv_environ = module_inst->wasi_ctx.argv_environ = NULL;
  662. prestats = module_inst->wasi_ctx.prestats = NULL;
  663. curfds = module_inst->wasi_ctx.curfds = NULL;
  664. return true;
  665. }
  666. /* process argv[0], trip the path and suffix, only keep the program name */
  667. for (i = 0; i < argc; i++)
  668. argv_buf_len += strlen(argv[i]) + 1;
  669. total_size = sizeof(size_t) * (uint64)argc;
  670. if (total_size >= UINT32_MAX
  671. || !(offset_argv_offsets = wasm_runtime_module_malloc
  672. (module_inst, (uint32)total_size))
  673. || argv_buf_len >= UINT32_MAX
  674. || !(offset_argv_buf = wasm_runtime_module_malloc
  675. (module_inst, (uint32)argv_buf_len))) {
  676. set_error_buf(error_buf, error_buf_size,
  677. "Init wasi environment failed: allocate memory failed.");
  678. goto fail;
  679. }
  680. argv_offsets = (size_t*)
  681. wasm_runtime_addr_app_to_native(module_inst, offset_argv_offsets);
  682. argv_buf = (char*)
  683. wasm_runtime_addr_app_to_native(module_inst, offset_argv_buf);
  684. for (i = 0; i < argc; i++) {
  685. argv_offsets[i] = argv_buf_offset;
  686. bh_strcpy_s(argv_buf + argv_buf_offset,
  687. (uint32)argv_buf_len - argv_buf_offset, argv[i]);
  688. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  689. }
  690. for (i = 0; i < env_count; i++)
  691. env_buf_len += strlen(env[i]) + 1;
  692. total_size = sizeof(size_t) * (uint64)argc;
  693. if (total_size >= UINT32_MAX
  694. || !(offset_env_offsets = wasm_runtime_module_malloc
  695. (module_inst, (uint32)total_size))
  696. || env_buf_len >= UINT32_MAX
  697. || !(offset_env_buf = wasm_runtime_module_malloc
  698. (module_inst, (uint32)env_buf_len))) {
  699. set_error_buf(error_buf, error_buf_size,
  700. "Init wasi environment failed: allocate memory failed.");
  701. goto fail;
  702. }
  703. env_offsets = (size_t*)
  704. wasm_runtime_addr_app_to_native(module_inst, offset_env_offsets);
  705. env_buf = (char*)
  706. wasm_runtime_addr_app_to_native(module_inst, offset_env_buf);
  707. for (i = 0; i < env_count; i++) {
  708. env_offsets[i] = env_buf_offset;
  709. bh_strcpy_s(env_buf + env_buf_offset,
  710. (uint32)env_buf_len - env_buf_offset, env[i]);
  711. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  712. }
  713. if (!(offset_curfds = wasm_runtime_module_malloc
  714. (module_inst, sizeof(struct fd_table)))
  715. || !(offset_prestats = wasm_runtime_module_malloc
  716. (module_inst, sizeof(struct fd_prestats)))
  717. || !(offset_argv_environ = wasm_runtime_module_malloc
  718. (module_inst, sizeof(struct argv_environ_values)))) {
  719. set_error_buf(error_buf, error_buf_size,
  720. "Init wasi environment failed: allocate memory failed.");
  721. goto fail;
  722. }
  723. curfds = module_inst->wasi_ctx.curfds = (struct fd_table*)
  724. wasm_runtime_addr_app_to_native(module_inst, offset_curfds);
  725. prestats = module_inst->wasi_ctx.prestats = (struct fd_prestats*)
  726. wasm_runtime_addr_app_to_native(module_inst, offset_prestats);
  727. argv_environ = module_inst->wasi_ctx.argv_environ =
  728. (struct argv_environ_values*)wasm_runtime_addr_app_to_native
  729. (module_inst, offset_argv_environ);
  730. fd_table_init(curfds);
  731. fd_prestats_init(prestats);
  732. if (!argv_environ_init(argv_environ,
  733. argv_offsets, argc,
  734. argv_buf, argv_buf_len,
  735. env_offsets, env_count,
  736. env_buf, env_buf_len)) {
  737. set_error_buf(error_buf, error_buf_size,
  738. "Init wasi environment failed: "
  739. "init argument environment failed.");
  740. goto fail;
  741. }
  742. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  743. if (!fd_table_insert_existing(curfds, 0, 0)
  744. || !fd_table_insert_existing(curfds, 1, 1)
  745. || !fd_table_insert_existing(curfds, 2, 2)) {
  746. set_error_buf(error_buf, error_buf_size,
  747. "Init wasi environment failed: init fd table failed.");
  748. goto fail;
  749. }
  750. wasm_fd = 3;
  751. for (i = 0; i < dir_count; i++, wasm_fd++) {
  752. path = realpath(dir_list[i], resolved_path);
  753. if (!path) {
  754. if (error_buf)
  755. snprintf(error_buf, error_buf_size,
  756. "error while pre-opening directory %s: %d\n",
  757. dir_list[i], errno);
  758. goto fail;
  759. }
  760. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  761. if (raw_fd == -1) {
  762. if (error_buf)
  763. snprintf(error_buf, error_buf_size,
  764. "error while pre-opening directory %s: %d\n",
  765. dir_list[i], errno);
  766. goto fail;
  767. }
  768. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  769. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  770. }
  771. return true;
  772. fail:
  773. if (offset_curfds != 0)
  774. wasm_runtime_module_free(module_inst, offset_curfds);
  775. if (offset_prestats != 0)
  776. wasm_runtime_module_free(module_inst, offset_prestats);
  777. if (offset_argv_environ != 0)
  778. wasm_runtime_module_free(module_inst, offset_argv_environ);
  779. if (offset_argv_buf)
  780. wasm_runtime_module_free(module_inst, offset_argv_buf);
  781. if (offset_argv_offsets)
  782. wasm_runtime_module_free(module_inst, offset_argv_offsets);
  783. if (offset_env_buf)
  784. wasm_runtime_module_free(module_inst, offset_env_buf);
  785. if (offset_env_offsets)
  786. wasm_runtime_module_free(module_inst, offset_env_offsets);
  787. return false;
  788. }
  789. static void
  790. wasm_runtime_destroy_wasi(WASMModuleInstance *module_inst)
  791. {
  792. WASIContext *wasi_ctx = &module_inst->wasi_ctx;
  793. if (wasi_ctx->argv_environ)
  794. argv_environ_destroy(wasi_ctx->argv_environ);
  795. if (wasi_ctx->curfds)
  796. fd_table_destroy(wasi_ctx->curfds);
  797. if (wasi_ctx->prestats)
  798. fd_prestats_destroy(wasi_ctx->prestats);
  799. }
  800. WASIContext *
  801. wasm_runtime_get_wasi_ctx(WASMModuleInstance *module_inst)
  802. {
  803. return &module_inst->wasi_ctx;
  804. }
  805. #endif
  806. /**
  807. * Instantiate module
  808. */
  809. WASMModuleInstance*
  810. wasm_runtime_instantiate(WASMModule *module,
  811. uint32 stack_size, uint32 heap_size,
  812. char *error_buf, uint32 error_buf_size)
  813. {
  814. WASMModuleInstance *module_inst;
  815. WASMTableSeg *table_seg;
  816. WASMDataSeg *data_seg;
  817. WASMGlobalInstance *globals = NULL, *global;
  818. uint32 global_count, addr_data_size = 0, global_data_size = 0, i, j;
  819. uint32 base_offset, length, memory_size;
  820. uint8 *global_data, *global_data_end, *addr_data, *addr_data_end;
  821. uint8 *memory_data;
  822. uint32 *table_data;
  823. if (!module)
  824. return NULL;
  825. /* Check heap size */
  826. heap_size = align_uint(heap_size, 8);
  827. if (heap_size == 0)
  828. heap_size = APP_HEAP_SIZE_DEFAULT;
  829. if (heap_size < APP_HEAP_SIZE_MIN)
  830. heap_size = APP_HEAP_SIZE_MIN;
  831. if (heap_size > APP_HEAP_SIZE_MAX)
  832. heap_size = APP_HEAP_SIZE_MAX;
  833. /* Instantiate global firstly to get the mutable data size */
  834. global_count = module->import_global_count + module->global_count;
  835. if (global_count &&
  836. !(globals = globals_instantiate(module, &addr_data_size,
  837. &global_data_size,
  838. error_buf, error_buf_size)))
  839. return NULL;
  840. /* Allocate the memory */
  841. if (!(module_inst = wasm_malloc((uint32)sizeof(WASMModuleInstance)))) {
  842. set_error_buf(error_buf, error_buf_size,
  843. "Instantiate module failed: allocate memory failed.");
  844. globals_deinstantiate(globals);
  845. return NULL;
  846. }
  847. memset(module_inst, 0, (uint32)sizeof(WASMModuleInstance));
  848. module_inst->global_count = global_count;
  849. module_inst->globals = globals;
  850. module_inst->memory_count =
  851. module->import_memory_count + module->memory_count;
  852. module_inst->table_count =
  853. module->import_table_count + module->table_count;
  854. module_inst->function_count =
  855. module->import_function_count + module->function_count;
  856. module_inst->export_func_count = get_export_function_count(module);
  857. /* Instantiate memories/tables/functions */
  858. if (((module_inst->memory_count > 0 || global_count > 0)
  859. && !(module_inst->memories =
  860. memories_instantiate(module, addr_data_size, global_data_size,
  861. heap_size, error_buf, error_buf_size)))
  862. || (module_inst->table_count > 0
  863. && !(module_inst->tables = tables_instantiate(module,
  864. error_buf,
  865. error_buf_size)))
  866. || (module_inst->function_count > 0
  867. && !(module_inst->functions = functions_instantiate(module,
  868. error_buf,
  869. error_buf_size)))
  870. || (module_inst->export_func_count > 0
  871. && !(module_inst->export_functions = export_functions_instantiate(
  872. module, module_inst, module_inst->export_func_count,
  873. error_buf, error_buf_size)))) {
  874. wasm_runtime_deinstantiate(module_inst);
  875. return NULL;
  876. }
  877. if (module_inst->memory_count || global_count > 0) {
  878. WASMMemoryInstance *memory;
  879. memory = module_inst->default_memory = module_inst->memories[0];
  880. memory_data = module_inst->default_memory->memory_data;
  881. /* fix import memoryBase */
  882. globals_instantiate_fix(globals, module, module_inst);
  883. /* Initialize the global data */
  884. addr_data = memory->addr_data;
  885. addr_data_end = addr_data + addr_data_size;
  886. global_data = memory->global_data;
  887. global_data_end = global_data + global_data_size;
  888. global = globals;
  889. for (i = 0; i < global_count; i++, global++) {
  890. switch (global->type) {
  891. case VALUE_TYPE_I32:
  892. case VALUE_TYPE_F32:
  893. if (!global->is_addr)
  894. *(int32*)global_data = global->initial_value.i32;
  895. else {
  896. *(int32*)addr_data = global->initial_value.i32;
  897. /* Store the offset to memory data for global of addr */
  898. *(int32*)global_data = (int32)(addr_data - memory_data);
  899. addr_data += sizeof(int32);
  900. }
  901. global_data += sizeof(int32);
  902. break;
  903. case VALUE_TYPE_I64:
  904. case VALUE_TYPE_F64:
  905. wasm_assert(!global->is_addr);
  906. bh_memcpy_s(global_data, (uint32)(global_data_end - global_data),
  907. &global->initial_value.i64, sizeof(int64));
  908. global_data += sizeof(int64);
  909. break;
  910. default:
  911. wasm_assert(0);
  912. }
  913. }
  914. wasm_assert(addr_data == addr_data_end);
  915. wasm_assert(global_data == global_data_end);
  916. global = globals + module->import_global_count;
  917. for (i = 0; i < module->global_count; i++, global++) {
  918. InitializerExpression *init_expr = &module->globals[i].init_expr;
  919. if (init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL
  920. && globals[init_expr->u.global_index].is_addr) {
  921. uint8 *global_data_dst = memory->global_data + global->data_offset;
  922. uint8 *global_data_src =
  923. memory->global_data + globals[init_expr->u.global_index].data_offset;
  924. *(uintptr_t*)global_data_dst = *(uintptr_t*)global_data_src;
  925. }
  926. }
  927. /* Initialize the memory data with data segment section */
  928. if (module_inst->default_memory->cur_page_count > 0) {
  929. for (i = 0; i < module->data_seg_count; i++) {
  930. data_seg = module->data_segments[i];
  931. wasm_assert(data_seg->memory_index == 0);
  932. wasm_assert(data_seg->base_offset.init_expr_type ==
  933. INIT_EXPR_TYPE_I32_CONST
  934. || data_seg->base_offset.init_expr_type ==
  935. INIT_EXPR_TYPE_GET_GLOBAL);
  936. if (data_seg->base_offset.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  937. wasm_assert(data_seg->base_offset.u.global_index < global_count
  938. && globals[data_seg->base_offset.u.global_index].type ==
  939. VALUE_TYPE_I32);
  940. data_seg->base_offset.u.i32 =
  941. globals[data_seg->base_offset.u.global_index].initial_value.i32;
  942. }
  943. base_offset = (uint32)data_seg->base_offset.u.i32;
  944. length = data_seg->data_length;
  945. memory_size = NumBytesPerPage * module_inst->default_memory->cur_page_count;
  946. if (length > 0
  947. && (base_offset >= memory_size
  948. || base_offset + length > memory_size)) {
  949. set_error_buf(error_buf, error_buf_size,
  950. "Instantiate module failed: data segment out of range.");
  951. wasm_runtime_deinstantiate(module_inst);
  952. return NULL;
  953. }
  954. bh_memcpy_s(memory_data + base_offset, memory_size - base_offset,
  955. data_seg->data, length);
  956. }
  957. }
  958. }
  959. if (module_inst->table_count) {
  960. module_inst->default_table = module_inst->tables[0];
  961. /* Initialize the table data with table segment section */
  962. table_data = (uint32*)module_inst->default_table->base_addr;
  963. table_seg = module->table_segments;
  964. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  965. wasm_assert(table_seg->table_index == 0);
  966. wasm_assert(table_seg->base_offset.init_expr_type ==
  967. INIT_EXPR_TYPE_I32_CONST
  968. || table_seg->base_offset.init_expr_type ==
  969. INIT_EXPR_TYPE_GET_GLOBAL);
  970. if (table_seg->base_offset.init_expr_type ==
  971. INIT_EXPR_TYPE_GET_GLOBAL) {
  972. wasm_assert(table_seg->base_offset.u.global_index < global_count
  973. && globals[table_seg->base_offset.u.global_index].type ==
  974. VALUE_TYPE_I32);
  975. table_seg->base_offset.u.i32 =
  976. globals[table_seg->base_offset.u.global_index].initial_value.i32;
  977. }
  978. if ((uint32)table_seg->base_offset.u.i32 <
  979. module_inst->default_table->cur_size) {
  980. length = table_seg->function_count;
  981. if ((uint32)table_seg->base_offset.u.i32 + length >
  982. module_inst->default_table->cur_size)
  983. length = module_inst->default_table->cur_size
  984. - (uint32)table_seg->base_offset.u.i32;
  985. /* Check function index */
  986. for (j = 0; j < length; j++) {
  987. if (table_seg->func_indexes[j] >= module_inst->function_count) {
  988. set_error_buf(error_buf, error_buf_size,
  989. "function index is overflow");
  990. wasm_runtime_deinstantiate(module_inst);
  991. return NULL;
  992. }
  993. }
  994. bh_memcpy_s(table_data + table_seg->base_offset.u.i32,
  995. (uint32)((module_inst->default_table->cur_size
  996. - (uint32)table_seg->base_offset.u.i32)
  997. * sizeof(uint32)),
  998. table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
  999. }
  1000. }
  1001. }
  1002. #if WASM_ENABLE_WASI != 0
  1003. if (!wasm_runtime_init_wasi(module_inst,
  1004. module->wasi_args.dir_list,
  1005. module->wasi_args.dir_count,
  1006. module->wasi_args.map_dir_list,
  1007. module->wasi_args.map_dir_count,
  1008. module->wasi_args.env,
  1009. module->wasi_args.env_count,
  1010. module->wasi_args.argv,
  1011. module->wasi_args.argc,
  1012. error_buf, error_buf_size)) {
  1013. wasm_runtime_deinstantiate(module_inst);
  1014. return NULL;
  1015. }
  1016. #endif
  1017. if (module->start_function != (uint32)-1) {
  1018. wasm_assert(module->start_function >= module->import_function_count);
  1019. module_inst->start_function =
  1020. &module_inst->functions[module->start_function];
  1021. }
  1022. module_inst->module = module;
  1023. /* module instance type */
  1024. module_inst->module_type = Wasm_Module_Bytecode;
  1025. /* Initialize the thread related data */
  1026. if (stack_size == 0)
  1027. stack_size = DEFAULT_WASM_STACK_SIZE;
  1028. module_inst->wasm_stack_size = stack_size;
  1029. module_inst->main_tlr.module_inst = module_inst;
  1030. /* The native thread handle may be used in future, e.g multiple threads. */
  1031. module_inst->main_tlr.handle = ws_self_thread();
  1032. /* Execute __post_instantiate and start function */
  1033. if (!execute_post_inst_function(module_inst)
  1034. || !execute_start_function(module_inst)) {
  1035. set_error_buf(error_buf, error_buf_size,
  1036. module_inst->cur_exception);
  1037. wasm_runtime_deinstantiate(module_inst);
  1038. return NULL;
  1039. }
  1040. (void)addr_data_end;
  1041. (void)global_data_end;
  1042. return module_inst;
  1043. }
  1044. void
  1045. wasm_runtime_deinstantiate(WASMModuleInstance *module_inst)
  1046. {
  1047. if (!module_inst)
  1048. return;
  1049. #if WASM_ENABLE_WASI != 0
  1050. wasm_runtime_destroy_wasi(module_inst);
  1051. #endif
  1052. if (module_inst->memory_count > 0)
  1053. memories_deinstantiate(module_inst->memories, module_inst->memory_count);
  1054. else if (module_inst->memories != NULL && module_inst->global_count > 0)
  1055. /* No imported memory and defined memory, the memory is created when
  1056. global count > 0. */
  1057. memories_deinstantiate(module_inst->memories, 1);
  1058. tables_deinstantiate(module_inst->tables, module_inst->table_count);
  1059. functions_deinstantiate(module_inst->functions, module_inst->function_count);
  1060. globals_deinstantiate(module_inst->globals);
  1061. export_functions_deinstantiate(module_inst->export_functions);
  1062. if (module_inst->wasm_stack)
  1063. wasm_free(module_inst->wasm_stack);
  1064. wasm_free(module_inst);
  1065. }
  1066. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1067. bool
  1068. wasm_runtime_set_ext_memory(WASMModuleInstance *module_inst,
  1069. uint8 *ext_mem_data, uint32 ext_mem_size,
  1070. char *error_buf, uint32 error_buf_size)
  1071. {
  1072. if (module_inst->ext_mem_data) {
  1073. set_error_buf(error_buf, error_buf_size,
  1074. "Set external memory failed: "
  1075. "an external memory has been set.");
  1076. return false;
  1077. }
  1078. if (!ext_mem_data
  1079. || ext_mem_size > 1 * BH_GB
  1080. || ext_mem_data + ext_mem_size < ext_mem_data) {
  1081. set_error_buf(error_buf, error_buf_size,
  1082. "Set external memory failed: "
  1083. "invalid input.");
  1084. return false;
  1085. }
  1086. module_inst->ext_mem_data = ext_mem_data;
  1087. module_inst->ext_mem_data_end = ext_mem_data + ext_mem_size;
  1088. module_inst->ext_mem_size = ext_mem_size;
  1089. module_inst->ext_mem_base_offset = DEFAULT_EXT_MEM_BASE_OFFSET;
  1090. return true;
  1091. }
  1092. #endif
  1093. bool
  1094. wasm_runtime_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
  1095. {
  1096. #if WASM_ENABLE_MEMORY_GROW != 0
  1097. WASMMemoryInstance *memory = module->default_memory;
  1098. WASMMemoryInstance *new_memory;
  1099. uint32 total_page_count = inc_page_count + memory->cur_page_count;
  1100. uint64 total_size = offsetof(WASMMemoryInstance, base_addr) +
  1101. memory->addr_data_size +
  1102. NumBytesPerPage * (uint64)total_page_count +
  1103. memory->global_data_size;
  1104. if (inc_page_count <= 0)
  1105. /* No need to enlarge memory */
  1106. return true;
  1107. if (total_page_count < memory->cur_page_count /* integer overflow */
  1108. || total_page_count > memory->max_page_count) {
  1109. wasm_runtime_set_exception(module, "fail to enlarge memory.");
  1110. return false;
  1111. }
  1112. if (total_size >= UINT32_MAX
  1113. || !(new_memory = wasm_malloc((uint32)total_size))) {
  1114. wasm_runtime_set_exception(module, "fail to enlarge memory.");
  1115. return false;
  1116. }
  1117. new_memory->cur_page_count = total_page_count;
  1118. new_memory->max_page_count = memory->max_page_count;
  1119. new_memory->addr_data = new_memory->base_addr;
  1120. new_memory->addr_data_size = memory->addr_data_size;
  1121. new_memory->memory_data = new_memory->addr_data + new_memory->addr_data_size;
  1122. new_memory->global_data = new_memory->memory_data +
  1123. NumBytesPerPage * total_page_count;
  1124. new_memory->global_data_size = memory->global_data_size;
  1125. new_memory->end_addr = new_memory->global_data + memory->global_data_size;
  1126. /* Copy addr data and memory data */
  1127. bh_memcpy_s(new_memory->addr_data,
  1128. (uint32)(memory->global_data - memory->addr_data),
  1129. memory->addr_data,
  1130. (uint32)(memory->global_data - memory->addr_data));
  1131. /* Copy global data */
  1132. bh_memcpy_s(new_memory->global_data, new_memory->global_data_size,
  1133. memory->global_data, memory->global_data_size);
  1134. /* Init free space of new memory */
  1135. memset(new_memory->memory_data + NumBytesPerPage * memory->cur_page_count,
  1136. 0, NumBytesPerPage * (total_page_count - memory->cur_page_count));
  1137. new_memory->heap_data = memory->heap_data;
  1138. new_memory->heap_data_end = memory->heap_data_end;
  1139. new_memory->heap_handle = memory->heap_handle;
  1140. new_memory->heap_base_offset = memory->heap_base_offset;
  1141. module->memories[0] = module->default_memory = new_memory;
  1142. wasm_free(memory);
  1143. return true;
  1144. #else
  1145. wasm_runtime_set_exception(module, "unsupported operation: enlarge memory.");
  1146. return false;
  1147. #endif
  1148. }
  1149. PackageType
  1150. get_package_type(const uint8 *buf, uint32 size)
  1151. {
  1152. if (buf && size > 4) {
  1153. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  1154. return Wasm_Module_Bytecode;
  1155. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  1156. return Wasm_Module_AoT;
  1157. }
  1158. return Package_Type_Unknown;
  1159. }
  1160. WASMExecEnv*
  1161. wasm_runtime_create_exec_env(uint32 stack_size)
  1162. {
  1163. WASMExecEnv *exec_env = wasm_malloc((uint32)sizeof(WASMExecEnv));
  1164. if (exec_env) {
  1165. if (!(exec_env->stack = wasm_malloc(stack_size))) {
  1166. wasm_free(exec_env);
  1167. return NULL;
  1168. }
  1169. exec_env->stack_size = stack_size;
  1170. }
  1171. return exec_env;
  1172. }
  1173. void
  1174. wasm_runtime_destroy_exec_env(WASMExecEnv *env)
  1175. {
  1176. if (env) {
  1177. wasm_free(env->stack);
  1178. wasm_free(env);
  1179. }
  1180. }
  1181. void
  1182. wasm_runtime_set_custom_data(WASMModuleInstance *module_inst,
  1183. void *custom_data)
  1184. {
  1185. module_inst->custom_data = custom_data;
  1186. }
  1187. void*
  1188. wasm_runtime_get_custom_data(WASMModuleInstance *module_inst)
  1189. {
  1190. return module_inst->custom_data;
  1191. }
  1192. int32
  1193. wasm_runtime_module_malloc(WASMModuleInstance *module_inst, uint32 size)
  1194. {
  1195. WASMMemoryInstance *memory = module_inst->default_memory;
  1196. uint8 *addr = mem_allocator_malloc(memory->heap_handle, size);
  1197. if (!addr) {
  1198. wasm_runtime_set_exception(module_inst, "out of memory");
  1199. return 0;
  1200. }
  1201. return memory->heap_base_offset + (int32)(addr - memory->heap_data);
  1202. }
  1203. void
  1204. wasm_runtime_module_free(WASMModuleInstance *module_inst, int32 ptr)
  1205. {
  1206. if (ptr) {
  1207. WASMMemoryInstance *memory = module_inst->default_memory;
  1208. uint8 *addr = memory->heap_data + (ptr - memory->heap_base_offset);
  1209. if (memory->heap_data < addr && addr < memory->heap_data_end)
  1210. mem_allocator_free(memory->heap_handle, addr);
  1211. }
  1212. }
  1213. int32
  1214. wasm_runtime_module_dup_data(WASMModuleInstance *module_inst,
  1215. const char *src, uint32 size)
  1216. {
  1217. int32 buffer_offset = wasm_runtime_module_malloc(module_inst, size);
  1218. if (buffer_offset != 0) {
  1219. char *buffer;
  1220. buffer = wasm_runtime_addr_app_to_native(module_inst, buffer_offset);
  1221. bh_memcpy_s(buffer, size, src, size);
  1222. }
  1223. return buffer_offset;
  1224. }
  1225. bool
  1226. wasm_runtime_validate_app_addr(WASMModuleInstance *module_inst,
  1227. int32 app_offset, uint32 size)
  1228. {
  1229. WASMMemoryInstance *memory;
  1230. uint8 *addr;
  1231. /* integer overflow check */
  1232. if(app_offset + (int32)size < app_offset) {
  1233. goto fail;
  1234. }
  1235. memory = module_inst->default_memory;
  1236. if (0 <= app_offset
  1237. && app_offset < memory->heap_base_offset) {
  1238. addr = memory->memory_data + app_offset;
  1239. if (!(memory->base_addr <= addr && addr + size <= memory->end_addr))
  1240. goto fail;
  1241. return true;
  1242. }
  1243. else if (memory->heap_base_offset < app_offset
  1244. && app_offset < memory->heap_base_offset
  1245. + (memory->heap_data_end - memory->heap_data)) {
  1246. addr = memory->heap_data + (app_offset - memory->heap_base_offset);
  1247. if (!(memory->heap_data <= addr && addr + size <= memory->heap_data_end))
  1248. goto fail;
  1249. return true;
  1250. }
  1251. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1252. else if (module_inst->ext_mem_data
  1253. && module_inst->ext_mem_base_offset <= app_offset
  1254. && app_offset < module_inst->ext_mem_base_offset
  1255. + module_inst->ext_mem_size) {
  1256. addr = module_inst->ext_mem_data
  1257. + (app_offset - module_inst->ext_mem_base_offset);
  1258. if (!(module_inst->ext_mem_data <= addr
  1259. && addr + size <= module_inst->ext_mem_data_end))
  1260. goto fail;
  1261. return true;
  1262. }
  1263. #endif
  1264. fail:
  1265. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1266. return false;
  1267. }
  1268. bool
  1269. wasm_runtime_validate_app_str_addr(WASMModuleInstance *module_inst,
  1270. int32 app_str_offset)
  1271. {
  1272. int32 app_end_offset;
  1273. char *str, *str_end;
  1274. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
  1275. NULL, &app_end_offset))
  1276. goto fail;
  1277. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1278. str_end = str + (app_end_offset - app_str_offset);
  1279. while (str < str_end && *str != '\0')
  1280. str++;
  1281. if (str == str_end)
  1282. goto fail;
  1283. return true;
  1284. fail:
  1285. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1286. return false;
  1287. }
  1288. bool
  1289. wasm_runtime_validate_native_addr(WASMModuleInstance *module_inst,
  1290. void *native_ptr, uint32 size)
  1291. {
  1292. uint8 *addr = native_ptr;
  1293. WASMMemoryInstance *memory = module_inst->default_memory;
  1294. if (addr + size < addr) {
  1295. goto fail;
  1296. }
  1297. if ((memory->base_addr <= addr && addr + size <= memory->end_addr)
  1298. || (memory->heap_data <= addr && addr + size <= memory->heap_data_end)
  1299. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1300. || (module_inst->ext_mem_data
  1301. && module_inst->ext_mem_data <= addr
  1302. && addr + size <= module_inst->ext_mem_data_end)
  1303. #endif
  1304. )
  1305. return true;
  1306. fail:
  1307. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1308. return false;
  1309. }
  1310. void *
  1311. wasm_runtime_addr_app_to_native(WASMModuleInstance *module_inst,
  1312. int32 app_offset)
  1313. {
  1314. WASMMemoryInstance *memory = module_inst->default_memory;
  1315. if (0 <= app_offset && app_offset < memory->heap_base_offset)
  1316. return memory->memory_data + app_offset;
  1317. else if (memory->heap_base_offset < app_offset
  1318. && app_offset < memory->heap_base_offset
  1319. + (memory->heap_data_end - memory->heap_data))
  1320. return memory->heap_data + (app_offset - memory->heap_base_offset);
  1321. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1322. else if (module_inst->ext_mem_data
  1323. && module_inst->ext_mem_base_offset <= app_offset
  1324. && app_offset < module_inst->ext_mem_base_offset
  1325. + module_inst->ext_mem_size)
  1326. return module_inst->ext_mem_data
  1327. + (app_offset - module_inst->ext_mem_base_offset);
  1328. #endif
  1329. else
  1330. return NULL;
  1331. }
  1332. int32
  1333. wasm_runtime_addr_native_to_app(WASMModuleInstance *module_inst,
  1334. void *native_ptr)
  1335. {
  1336. WASMMemoryInstance *memory = module_inst->default_memory;
  1337. if (memory->base_addr <= (uint8*)native_ptr
  1338. && (uint8*)native_ptr < memory->end_addr)
  1339. return (int32)((uint8*)native_ptr - memory->memory_data);
  1340. else if (memory->heap_data <= (uint8*)native_ptr
  1341. && (uint8*)native_ptr < memory->heap_data_end)
  1342. return memory->heap_base_offset
  1343. + (int32)((uint8*)native_ptr - memory->heap_data);
  1344. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1345. else if (module_inst->ext_mem_data
  1346. && module_inst->ext_mem_data <= (uint8*)native_ptr
  1347. && (uint8*)native_ptr < module_inst->ext_mem_data_end)
  1348. return module_inst->ext_mem_base_offset
  1349. + ((uint8*)native_ptr - module_inst->ext_mem_data);
  1350. #endif
  1351. else
  1352. return 0;
  1353. }
  1354. bool
  1355. wasm_runtime_get_app_addr_range(WASMModuleInstance *module_inst,
  1356. int32 app_offset,
  1357. int32 *p_app_start_offset,
  1358. int32 *p_app_end_offset)
  1359. {
  1360. int32 app_start_offset, app_end_offset;
  1361. WASMMemoryInstance *memory = module_inst->default_memory;
  1362. if (0 <= app_offset && app_offset < memory->heap_base_offset) {
  1363. app_start_offset = 0;
  1364. app_end_offset = (int32)(NumBytesPerPage * memory->cur_page_count);
  1365. }
  1366. else if (memory->heap_base_offset < app_offset
  1367. && app_offset < memory->heap_base_offset
  1368. + (memory->heap_data_end - memory->heap_data)) {
  1369. app_start_offset = memory->heap_base_offset;
  1370. app_end_offset = memory->heap_base_offset
  1371. + (int32)(memory->heap_data_end - memory->heap_data);
  1372. }
  1373. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1374. else if (module_inst->ext_mem_data
  1375. && module_inst->ext_mem_base_offset <= app_offset
  1376. && app_offset < module_inst->ext_mem_base_offset
  1377. + module_inst->ext_mem_size) {
  1378. app_start_offset = module_inst->ext_mem_base_offset;
  1379. app_end_offset = app_start_offset + module_inst->ext_mem_size;
  1380. }
  1381. #endif
  1382. else
  1383. return false;
  1384. if (p_app_start_offset)
  1385. *p_app_start_offset = app_start_offset;
  1386. if (p_app_end_offset)
  1387. *p_app_end_offset = app_end_offset;
  1388. return true;
  1389. }
  1390. bool
  1391. wasm_runtime_get_native_addr_range(WASMModuleInstance *module_inst,
  1392. uint8 *native_ptr,
  1393. uint8 **p_native_start_addr,
  1394. uint8 **p_native_end_addr)
  1395. {
  1396. uint8 *native_start_addr, *native_end_addr;
  1397. WASMMemoryInstance *memory = module_inst->default_memory;
  1398. if (memory->base_addr <= (uint8*)native_ptr
  1399. && (uint8*)native_ptr < memory->end_addr) {
  1400. native_start_addr = memory->memory_data;
  1401. native_end_addr = memory->memory_data
  1402. + NumBytesPerPage * memory->cur_page_count;
  1403. }
  1404. else if (memory->heap_data <= (uint8*)native_ptr
  1405. && (uint8*)native_ptr < memory->heap_data_end) {
  1406. native_start_addr = memory->heap_data;
  1407. native_end_addr = memory->heap_data_end;
  1408. }
  1409. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  1410. else if (module_inst->ext_mem_data
  1411. && module_inst->ext_mem_data <= (uint8*)native_ptr
  1412. && (uint8*)native_ptr < module_inst->ext_mem_data_end) {
  1413. native_start_addr = module_inst->ext_mem_data;
  1414. native_end_addr = module_inst->ext_mem_data_end;
  1415. }
  1416. #endif
  1417. else
  1418. return false;
  1419. if (p_native_start_addr)
  1420. *p_native_start_addr = native_start_addr;
  1421. if (p_native_end_addr)
  1422. *p_native_end_addr = native_end_addr;
  1423. return true;
  1424. }
  1425. uint32
  1426. wasm_runtime_get_temp_ret(WASMModuleInstance *module_inst)
  1427. {
  1428. return module_inst->temp_ret;
  1429. }
  1430. void
  1431. wasm_runtime_set_temp_ret(WASMModuleInstance *module_inst,
  1432. uint32 temp_ret)
  1433. {
  1434. module_inst->temp_ret = temp_ret;
  1435. }
  1436. uint32
  1437. wasm_runtime_get_llvm_stack(WASMModuleInstance *module_inst)
  1438. {
  1439. return module_inst->llvm_stack;
  1440. }
  1441. void
  1442. wasm_runtime_set_llvm_stack(WASMModuleInstance *module_inst,
  1443. uint32 llvm_stack)
  1444. {
  1445. module_inst->llvm_stack = llvm_stack;
  1446. }
  1447. WASMModuleInstance*
  1448. wasm_runtime_load_aot(uint8 *aot_file, uint32 aot_file_size,
  1449. uint32 heap_size,
  1450. char *error_buf, uint32 error_buf_size)
  1451. {
  1452. (void)aot_file;
  1453. (void)aot_file_size;
  1454. (void)heap_size;
  1455. (void)error_buf;
  1456. (void)error_buf_size;
  1457. return NULL;
  1458. }
  1459. static inline void
  1460. word_copy(uint32 *dest, uint32 *src, unsigned num)
  1461. {
  1462. for (; num > 0; num--)
  1463. *dest++ = *src++;
  1464. }
  1465. #define PUT_I64_TO_ADDR(addr, value) do { \
  1466. union { int64 val; uint32 parts[2]; } u; \
  1467. u.val = (value); \
  1468. (addr)[0] = u.parts[0]; \
  1469. (addr)[1] = u.parts[1]; \
  1470. } while (0)
  1471. #define PUT_F64_TO_ADDR(addr, value) do { \
  1472. union { float64 val; uint32 parts[2]; } u; \
  1473. u.val = (value); \
  1474. (addr)[0] = u.parts[0]; \
  1475. (addr)[1] = u.parts[1]; \
  1476. } while (0)
  1477. #if !defined(BUILD_TARGET_X86_64) && !defined(BUILD_TARGET_AMD_64)
  1478. typedef void (*GenericFunctionPointer)();
  1479. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  1480. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  1481. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  1482. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  1483. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  1484. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  1485. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  1486. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  1487. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  1488. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  1489. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  1490. bool
  1491. wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
  1492. WASMModuleInstance *module_inst,
  1493. uint32 *argv, uint32 argc, uint32 *ret)
  1494. {
  1495. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  1496. uint64 size;
  1497. #if !defined(BUILD_TARGET_ARM_32) && !defined(BUILD_TARGET_MIPS_32)
  1498. argc1 = argc + 2;
  1499. #else
  1500. argc1 = func_type->param_count * 2 + 2;
  1501. #endif
  1502. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  1503. size = sizeof(uint32) * (uint64)argc1;
  1504. if (size >= UINT_MAX
  1505. || !(argv1 = wasm_malloc((uint32)size))) {
  1506. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  1507. return false;
  1508. }
  1509. }
  1510. for (i = 0; i < sizeof(WASMModuleInstance*) / sizeof(uint32); i++)
  1511. argv1[j++] = ((uint32*)&module_inst)[i];
  1512. #if !defined(BUILD_TARGET_ARM_32) && !defined(BUILD_TARGET_MIPS_32)
  1513. word_copy(argv1 + j, argv, argc);
  1514. j += argc;
  1515. #else
  1516. for (i = 0; i < func_type->param_count; i++) {
  1517. switch (func_type->types[i]) {
  1518. case VALUE_TYPE_I32:
  1519. argv1[j++] = *argv++;
  1520. break;
  1521. case VALUE_TYPE_I64:
  1522. case VALUE_TYPE_F64:
  1523. /* 64-bit data must be 8 bytes alined in arm and mips */
  1524. if (j & 1)
  1525. j++;
  1526. argv1[j++] = *argv++;
  1527. argv1[j++] = *argv++;
  1528. break;
  1529. case VALUE_TYPE_F32:
  1530. argv1[j++] = *argv++;
  1531. break;
  1532. default:
  1533. wasm_assert(0);
  1534. break;
  1535. }
  1536. }
  1537. #endif /* end of !defined(BUILD_TARGET_ARM_32) && !defined(BUILD_TARGET_MIPS_32) */
  1538. argc1 = j;
  1539. if (func_type->result_count == 0) {
  1540. invokeNative_Void(func_ptr, argv1, argc1);
  1541. }
  1542. else {
  1543. switch (func_type->types[func_type->param_count]) {
  1544. case VALUE_TYPE_I32:
  1545. ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  1546. break;
  1547. case VALUE_TYPE_I64:
  1548. PUT_I64_TO_ADDR(ret, invokeNative_Int64(func_ptr, argv1, argc1));
  1549. break;
  1550. case VALUE_TYPE_F32:
  1551. *(float32*)ret = invokeNative_Float32(func_ptr, argv1, argc1);
  1552. break;
  1553. case VALUE_TYPE_F64:
  1554. PUT_F64_TO_ADDR(ret, invokeNative_Float64(func_ptr, argv1, argc1));
  1555. break;
  1556. default:
  1557. wasm_assert(0);
  1558. break;
  1559. }
  1560. }
  1561. if (argv1 != argv_buf)
  1562. wasm_free(argv1);
  1563. return true;
  1564. }
  1565. #else /* else of !defined(BUILD_TARGET_X86_64) && !defined(BUILD_TARGET_AMD_64) */
  1566. typedef void (*GenericFunctionPointer)();
  1567. int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  1568. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  1569. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  1570. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*,uint64);
  1571. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  1572. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
  1573. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  1574. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  1575. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  1576. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  1577. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  1578. #if defined(_WIN32) || defined(_WIN32_)
  1579. #define MAX_REG_FLOATS 4
  1580. #define MAX_REG_INTS 4
  1581. #else
  1582. #define MAX_REG_FLOATS 8
  1583. #define MAX_REG_INTS 6
  1584. #endif
  1585. bool
  1586. wasm_runtime_invoke_native(void *func_ptr, WASMType *func_type,
  1587. WASMModuleInstance *module_inst,
  1588. uint32 *argv, uint32 argc, uint32 *ret)
  1589. {
  1590. uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
  1591. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  1592. #if defined(_WIN32) || defined(_WIN32_)
  1593. /* important difference in calling conventions */
  1594. #define n_fps n_ints
  1595. #else
  1596. int n_fps = 0;
  1597. #endif
  1598. argc1 = 1 + MAX_REG_FLOATS + func_type->param_count + 2;
  1599. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  1600. size = sizeof(uint64) * (uint64)argc1;
  1601. if (size >= UINT32_MAX
  1602. || !(argv1 = wasm_malloc((uint32)size))) {
  1603. wasm_runtime_set_exception(module_inst, "allocate memory failed.");
  1604. return false;
  1605. }
  1606. }
  1607. fps = argv1;
  1608. ints = fps + MAX_REG_FLOATS;
  1609. stacks = ints + MAX_REG_INTS;
  1610. ints[n_ints++] = (uint64)(uintptr_t)module_inst;
  1611. for (i = 0; i < func_type->param_count; i++) {
  1612. switch (func_type->types[i]) {
  1613. case VALUE_TYPE_I32:
  1614. if (n_ints < MAX_REG_INTS)
  1615. ints[n_ints++] = *argv_src++;
  1616. else
  1617. stacks[n_stacks++] = *argv_src++;
  1618. break;
  1619. case VALUE_TYPE_I64:
  1620. if (n_ints < MAX_REG_INTS)
  1621. ints[n_ints++] = *(uint64*)argv_src;
  1622. else
  1623. stacks[n_stacks++] = *(uint64*)argv_src;
  1624. argv_src += 2;
  1625. break;
  1626. case VALUE_TYPE_F32:
  1627. if (n_fps < MAX_REG_FLOATS)
  1628. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  1629. else
  1630. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  1631. break;
  1632. case VALUE_TYPE_F64:
  1633. if (n_fps < MAX_REG_FLOATS)
  1634. *(float64*)&fps[n_fps++] = *(float64*)argv_src;
  1635. else
  1636. *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
  1637. argv_src += 2;
  1638. break;
  1639. default:
  1640. wasm_assert(0);
  1641. break;
  1642. }
  1643. }
  1644. if (func_type->result_count == 0) {
  1645. invokeNative_Void(func_ptr, argv1, n_stacks);
  1646. }
  1647. else {
  1648. switch (func_type->types[func_type->param_count]) {
  1649. case VALUE_TYPE_I32:
  1650. ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  1651. break;
  1652. case VALUE_TYPE_I64:
  1653. PUT_I64_TO_ADDR(ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  1654. break;
  1655. case VALUE_TYPE_F32:
  1656. *(float32*)ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  1657. break;
  1658. case VALUE_TYPE_F64:
  1659. PUT_F64_TO_ADDR(ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  1660. break;
  1661. default:
  1662. wasm_assert(0);
  1663. break;
  1664. }
  1665. }
  1666. if (argv1 != argv_buf)
  1667. wasm_free(argv1);
  1668. return true;
  1669. }
  1670. #endif /* end of !defined(BUILD_TARGET_X86_64) && !defined(BUILD_TARGET_AMD_64) */