wasm_runtime_common.c 72 KB

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