wasm_runtime_common.c 60 KB

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