wasm_runtime_common.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964
  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. #if WASM_ENABLE_THREAD_MGR != 0
  18. #include "../libraries/thread-mgr/thread_manager.h"
  19. #endif
  20. #if WASM_ENABLE_SHARED_MEMORY != 0
  21. #include "wasm_shared_memory.h"
  22. #endif
  23. #if WASM_ENABLE_MULTI_MODULE != 0
  24. /*
  25. * a safety insurance to prevent
  26. * circular depencies leading a stack overflow
  27. * try break early
  28. */
  29. typedef struct LoadingModule {
  30. bh_list_link l;
  31. /* point to a string pool */
  32. const char *module_name;
  33. } LoadingModule;
  34. static bh_list loading_module_list_head;
  35. static bh_list *const loading_module_list = &loading_module_list_head;
  36. static korp_mutex loading_module_list_lock;
  37. /*
  38. * a list about all exported functions, globals, memories, tables of every
  39. * fully loaded module
  40. */
  41. static bh_list registered_module_list_head;
  42. static bh_list *const registered_module_list = &registered_module_list_head;
  43. static korp_mutex registered_module_list_lock;
  44. static void
  45. wasm_runtime_destroy_registered_module_list();
  46. #endif /* WASM_ENABLE_MULTI_MODULE */
  47. void
  48. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format,
  49. ...)
  50. {
  51. va_list args;
  52. va_start(args, format);
  53. vsnprintf(error_buf, error_buf_size, format, args);
  54. va_end(args);
  55. }
  56. static void
  57. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  58. {
  59. if (error_buf != NULL)
  60. snprintf(error_buf, error_buf_size, "%s", string);
  61. }
  62. static void *
  63. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  64. char *error_buf, uint32 error_buf_size)
  65. {
  66. void *mem;
  67. if (size >= UINT32_MAX
  68. || !(mem = wasm_runtime_malloc((uint32)size))) {
  69. if (module_inst != NULL) {
  70. wasm_runtime_set_exception(module_inst,
  71. "allocate memory failed.");
  72. }
  73. else if (error_buf != NULL) {
  74. set_error_buf(error_buf, error_buf_size,
  75. "allocate memory failed.");
  76. }
  77. return NULL;
  78. }
  79. memset(mem, 0, (uint32)size);
  80. return mem;
  81. }
  82. static bool
  83. wasm_runtime_env_init()
  84. {
  85. if (bh_platform_init() != 0)
  86. return false;
  87. if (wasm_native_init() == false) {
  88. goto fail1;
  89. }
  90. #if WASM_ENABLE_MULTI_MODULE
  91. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  92. goto fail2;
  93. }
  94. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  95. goto fail3;
  96. }
  97. #endif
  98. #if WASM_ENABLE_SHARED_MEMORY
  99. if (!wasm_shared_memory_init()) {
  100. goto fail4;
  101. }
  102. #endif
  103. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  104. if (!thread_manager_init()) {
  105. goto fail5;
  106. }
  107. #endif
  108. #if WASM_ENABLE_AOT != 0
  109. #ifdef OS_ENABLE_HW_BOUND_CHECK
  110. if (!aot_signal_init()) {
  111. goto fail6;
  112. }
  113. #endif
  114. #endif
  115. return true;
  116. #if WASM_ENABLE_AOT != 0
  117. #ifdef OS_ENABLE_HW_BOUND_CHECK
  118. fail6:
  119. #endif
  120. #endif
  121. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  122. thread_manager_destroy();
  123. fail5:
  124. #endif
  125. #if WASM_ENABLE_SHARED_MEMORY
  126. wasm_shared_memory_destroy();
  127. fail4:
  128. #endif
  129. #if WASM_ENABLE_MULTI_MODULE
  130. os_mutex_destroy(&loading_module_list_lock);
  131. fail3:
  132. os_mutex_destroy(&registered_module_list_lock);
  133. fail2:
  134. #endif
  135. wasm_native_destroy();
  136. fail1:
  137. bh_platform_destroy();
  138. return false;
  139. }
  140. static bool
  141. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  142. {
  143. return exec_env
  144. && exec_env->module_inst
  145. && exec_env->wasm_stack_size > 0
  146. && exec_env->wasm_stack.s.top_boundary ==
  147. exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size
  148. && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary;
  149. }
  150. bool
  151. wasm_runtime_init()
  152. {
  153. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  154. return false;
  155. if (!wasm_runtime_env_init()) {
  156. wasm_runtime_memory_destroy();
  157. return false;
  158. }
  159. return true;
  160. }
  161. void
  162. wasm_runtime_destroy()
  163. {
  164. #if WASM_ENABLE_AOT != 0
  165. #ifdef OS_ENABLE_HW_BOUND_CHECK
  166. aot_signal_destroy();
  167. #endif
  168. #endif
  169. /* runtime env destroy */
  170. #if WASM_ENABLE_MULTI_MODULE
  171. wasm_runtime_destroy_loading_module_list();
  172. os_mutex_destroy(&loading_module_list_lock);
  173. wasm_runtime_destroy_registered_module_list();
  174. os_mutex_destroy(&registered_module_list_lock);
  175. #endif
  176. #if WASM_ENABLE_SHARED_MEMORY
  177. wasm_shared_memory_destroy();
  178. #endif
  179. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  180. thread_manager_destroy();
  181. #endif
  182. wasm_native_destroy();
  183. bh_platform_destroy();
  184. wasm_runtime_memory_destroy();
  185. }
  186. bool
  187. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  188. {
  189. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  190. &init_args->mem_alloc_option))
  191. return false;
  192. if (!wasm_runtime_env_init()) {
  193. wasm_runtime_memory_destroy();
  194. return false;
  195. }
  196. if (init_args->n_native_symbols > 0
  197. && !wasm_runtime_register_natives(init_args->native_module_name,
  198. init_args->native_symbols,
  199. init_args->n_native_symbols)) {
  200. wasm_runtime_destroy();
  201. return false;
  202. }
  203. #if WASM_ENABLE_THREAD_MGR != 0
  204. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  205. #endif
  206. return true;
  207. }
  208. PackageType
  209. get_package_type(const uint8 *buf, uint32 size)
  210. {
  211. if (buf && size >= 4) {
  212. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  213. return Wasm_Module_Bytecode;
  214. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  215. return Wasm_Module_AoT;
  216. }
  217. return Package_Type_Unknown;
  218. }
  219. #if WASM_ENABLE_MULTI_MODULE != 0
  220. static module_reader reader;
  221. static module_destroyer destroyer;
  222. void
  223. wasm_runtime_set_module_reader(const module_reader reader_cb,
  224. const module_destroyer destroyer_cb)
  225. {
  226. reader = reader_cb;
  227. destroyer = destroyer_cb;
  228. }
  229. module_reader
  230. wasm_runtime_get_module_reader()
  231. {
  232. return reader;
  233. }
  234. module_destroyer
  235. wasm_runtime_get_module_destroyer()
  236. {
  237. return destroyer;
  238. }
  239. static WASMRegisteredModule *
  240. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  241. {
  242. WASMRegisteredModule *reg_module = NULL;
  243. os_mutex_lock(&registered_module_list_lock);
  244. reg_module = bh_list_first_elem(registered_module_list);
  245. while (reg_module && module != reg_module->module) {
  246. reg_module = bh_list_elem_next(reg_module);
  247. }
  248. os_mutex_unlock(&registered_module_list_lock);
  249. return reg_module;
  250. }
  251. bool
  252. wasm_runtime_register_module_internal(const char *module_name,
  253. WASMModuleCommon *module,
  254. uint8 *orig_file_buf,
  255. uint32 orig_file_buf_size,
  256. char *error_buf,
  257. uint32_t error_buf_size)
  258. {
  259. WASMRegisteredModule *node = NULL;
  260. node = wasm_runtime_find_module_registered_by_reference(module);
  261. if (node) { /* module has been registered */
  262. if (node->module_name) { /* module has name */
  263. if (strcmp(node->module_name, module_name)) {
  264. /* module has different name */
  265. LOG_DEBUG("module(%p) has been registered with name %s",
  266. module, node->module_name);
  267. set_error_buf_v(error_buf, error_buf_size,
  268. "can not rename the module");
  269. return false;
  270. }
  271. else {
  272. /* module has the same name */
  273. LOG_DEBUG("module(%p) has been registered with the same name %s",
  274. module, node->module_name);
  275. return true;
  276. }
  277. }
  278. else {
  279. /* module has empyt name, reset it */
  280. node->module_name = module_name;
  281. return true;
  282. }
  283. }
  284. /* module hasn't been registered */
  285. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  286. if (!node) {
  287. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  288. sizeof(WASMRegisteredModule));
  289. return false;
  290. }
  291. /* share the string and the module */
  292. node->module_name = module_name;
  293. node->module = module;
  294. node->orig_file_buf = orig_file_buf;
  295. node->orig_file_buf_size = orig_file_buf_size;
  296. os_mutex_lock(&registered_module_list_lock);
  297. bh_list_status ret = bh_list_insert(registered_module_list, node);
  298. bh_assert(BH_LIST_SUCCESS == ret);
  299. (void)ret;
  300. os_mutex_unlock(&registered_module_list_lock);
  301. return true;
  302. }
  303. bool
  304. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  305. char *error_buf, uint32_t error_buf_size)
  306. {
  307. if (!error_buf || !error_buf_size) {
  308. LOG_ERROR("error buffer is required");
  309. return false;
  310. }
  311. if (!module_name || !module) {
  312. LOG_DEBUG("module_name and module are required");
  313. set_error_buf_v(error_buf, error_buf_size,
  314. "module_name and module are required");
  315. return false;
  316. }
  317. if (wasm_runtime_is_built_in_module(module_name)) {
  318. LOG_DEBUG("%s is a built-in module name", module_name);
  319. set_error_buf(error_buf, error_buf_size,
  320. "can not register as a built-in module");
  321. return false;
  322. }
  323. return wasm_runtime_register_module_internal(
  324. module_name, module, NULL, 0,
  325. error_buf, error_buf_size);
  326. }
  327. void
  328. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  329. {
  330. WASMRegisteredModule *registered_module = NULL;
  331. os_mutex_lock(&registered_module_list_lock);
  332. registered_module = bh_list_first_elem(registered_module_list);
  333. while (registered_module && module != registered_module->module) {
  334. registered_module = bh_list_elem_next(registered_module);
  335. }
  336. /* it does not matter if it is not exist. after all, it is gone */
  337. if (registered_module) {
  338. bh_list_remove(registered_module_list, registered_module);
  339. wasm_runtime_free(registered_module);
  340. }
  341. os_mutex_unlock(&registered_module_list_lock);
  342. }
  343. WASMModuleCommon *
  344. wasm_runtime_find_module_registered(const char *module_name)
  345. {
  346. WASMRegisteredModule *module = NULL, *module_next;
  347. os_mutex_lock(&registered_module_list_lock);
  348. module = bh_list_first_elem(registered_module_list);
  349. while (module) {
  350. module_next = bh_list_elem_next(module);
  351. if (module->module_name
  352. && !strcmp(module_name, module->module_name)) {
  353. break;
  354. }
  355. module = module_next;
  356. }
  357. os_mutex_unlock(&registered_module_list_lock);
  358. return module ? module->module : NULL;
  359. }
  360. bool
  361. wasm_runtime_is_module_registered(const char *module_name)
  362. {
  363. return NULL != wasm_runtime_find_module_registered(module_name);
  364. }
  365. /*
  366. * simply destroy all
  367. */
  368. static void
  369. wasm_runtime_destroy_registered_module_list()
  370. {
  371. WASMRegisteredModule *reg_module = NULL;
  372. os_mutex_lock(&registered_module_list_lock);
  373. reg_module = bh_list_first_elem(registered_module_list);
  374. while (reg_module) {
  375. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  376. bh_list_remove(registered_module_list, reg_module);
  377. /* now, it is time to release every module in the runtime */
  378. #if WASM_ENABLE_INTERP != 0
  379. if (reg_module->module->module_type == Wasm_Module_Bytecode)
  380. wasm_unload((WASMModule *)reg_module->module);
  381. #endif
  382. #if WASM_ENABLE_AOT != 0
  383. if (reg_module->module->module_type == Wasm_Module_AoT)
  384. aot_unload((AOTModule *)reg_module->module);
  385. #endif
  386. /* destroy the file buffer */
  387. if (destroyer && reg_module->orig_file_buf) {
  388. destroyer(reg_module->orig_file_buf,
  389. reg_module->orig_file_buf_size);
  390. reg_module->orig_file_buf = NULL;
  391. reg_module->orig_file_buf_size = 0;
  392. }
  393. wasm_runtime_free(reg_module);
  394. reg_module = next_reg_module;
  395. }
  396. os_mutex_unlock(&registered_module_list_lock);
  397. }
  398. bool
  399. wasm_runtime_add_loading_module(const char *module_name,
  400. char *error_buf, uint32 error_buf_size)
  401. {
  402. LOG_DEBUG("add %s into a loading list", module_name);
  403. LoadingModule *loadingModule =
  404. runtime_malloc(sizeof(LoadingModule), NULL,
  405. error_buf, error_buf_size);
  406. if (!loadingModule) {
  407. return false;
  408. }
  409. /* share the incoming string */
  410. loadingModule->module_name = module_name;
  411. os_mutex_lock(&loading_module_list_lock);
  412. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  413. bh_assert(BH_LIST_SUCCESS == ret);
  414. (void)ret;
  415. os_mutex_unlock(&loading_module_list_lock);
  416. return true;
  417. }
  418. void
  419. wasm_runtime_delete_loading_module(const char *module_name)
  420. {
  421. LOG_DEBUG("delete %s from a loading list", module_name);
  422. LoadingModule *module = NULL;
  423. os_mutex_lock(&loading_module_list_lock);
  424. module = bh_list_first_elem(loading_module_list);
  425. while (module && strcmp(module->module_name, module_name)) {
  426. module = bh_list_elem_next(module);
  427. }
  428. /* it does not matter if it is not exist. after all, it is gone */
  429. if (module) {
  430. bh_list_remove(loading_module_list, module);
  431. wasm_runtime_free(module);
  432. }
  433. os_mutex_unlock(&loading_module_list_lock);
  434. }
  435. bool
  436. wasm_runtime_is_loading_module(const char *module_name)
  437. {
  438. LOG_DEBUG("find %s in a loading list", module_name);
  439. LoadingModule *module = NULL;
  440. os_mutex_lock(&loading_module_list_lock);
  441. module = bh_list_first_elem(loading_module_list);
  442. while (module && strcmp(module_name, module->module_name)) {
  443. module = bh_list_elem_next(module);
  444. }
  445. os_mutex_unlock(&loading_module_list_lock);
  446. return module != NULL;
  447. }
  448. void
  449. wasm_runtime_destroy_loading_module_list()
  450. {
  451. LoadingModule *module = NULL;
  452. os_mutex_lock(&loading_module_list_lock);
  453. module = bh_list_first_elem(loading_module_list);
  454. while (module) {
  455. LoadingModule *next_module = bh_list_elem_next(module);
  456. bh_list_remove(loading_module_list, module);
  457. /*
  458. * will not free the module_name since it is
  459. * shared one of the const string pool
  460. */
  461. wasm_runtime_free(module);
  462. module = next_module;
  463. }
  464. os_mutex_unlock(&loading_module_list_lock);
  465. }
  466. #endif /* WASM_ENABLE_MULTI_MODULE */
  467. bool
  468. wasm_runtime_is_built_in_module(const char *module_name)
  469. {
  470. return (!strcmp("env", module_name)
  471. || !strcmp("wasi_unstable", module_name)
  472. || !strcmp("wasi_snapshot_preview1", module_name)
  473. || !strcmp("spectest", module_name)
  474. );
  475. }
  476. #if WASM_ENABLE_THREAD_MGR != 0
  477. bool
  478. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  479. uint32 start_offset, uint32 size)
  480. {
  481. WASMModuleInstanceCommon *module_inst
  482. = wasm_exec_env_get_module_inst(exec_env);
  483. #if WASM_ENABLE_INTERP != 0
  484. if (module_inst->module_type == Wasm_Module_Bytecode) {
  485. return wasm_set_aux_stack(exec_env, start_offset, size);
  486. }
  487. #endif
  488. #if WASM_ENABLE_AOT != 0
  489. if (module_inst->module_type == Wasm_Module_AoT) {
  490. return aot_set_aux_stack(exec_env, start_offset, size);
  491. }
  492. #endif
  493. return false;
  494. }
  495. bool
  496. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  497. uint32 *start_offset, uint32 *size)
  498. {
  499. WASMModuleInstanceCommon *module_inst
  500. = wasm_exec_env_get_module_inst(exec_env);
  501. #if WASM_ENABLE_INTERP != 0
  502. if (module_inst->module_type == Wasm_Module_Bytecode) {
  503. return wasm_get_aux_stack(exec_env, start_offset, size);
  504. }
  505. #endif
  506. #if WASM_ENABLE_AOT != 0
  507. if (module_inst->module_type == Wasm_Module_AoT) {
  508. return aot_get_aux_stack(exec_env, start_offset, size);
  509. }
  510. #endif
  511. return false;
  512. }
  513. void
  514. wasm_runtime_set_max_thread_num(uint32 num)
  515. {
  516. wasm_cluster_set_max_thread_num(num);
  517. }
  518. #endif /* end of WASM_ENABLE_THREAD_MGR */
  519. static WASMModuleCommon *
  520. register_module_with_null_name(WASMModuleCommon *module_common,
  521. char *error_buf, uint32 error_buf_size)
  522. {
  523. #if WASM_ENABLE_MULTI_MODULE != 0
  524. if (module_common) {
  525. if (!wasm_runtime_register_module_internal(NULL, module_common,
  526. NULL, 0,
  527. error_buf,
  528. error_buf_size)) {
  529. wasm_runtime_unload(module_common);
  530. return NULL;
  531. }
  532. return module_common;
  533. }
  534. else
  535. return NULL;
  536. #else
  537. return module_common;
  538. #endif
  539. }
  540. WASMModuleCommon *
  541. wasm_runtime_load(const uint8 *buf, uint32 size,
  542. char *error_buf, uint32 error_buf_size)
  543. {
  544. WASMModuleCommon *module_common = NULL;
  545. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  546. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  547. AOTModule *aot_module;
  548. WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
  549. if (!module)
  550. return NULL;
  551. if (!(aot_module = aot_convert_wasm_module(module,
  552. error_buf, error_buf_size))) {
  553. wasm_unload(module);
  554. return NULL;
  555. }
  556. module_common = (WASMModuleCommon*)aot_module;
  557. return register_module_with_null_name(module_common,
  558. error_buf, error_buf_size);
  559. #elif WASM_ENABLE_INTERP != 0
  560. module_common = (WASMModuleCommon*)
  561. wasm_load(buf, size, error_buf, error_buf_size);
  562. return register_module_with_null_name(module_common,
  563. error_buf, error_buf_size);
  564. #endif
  565. }
  566. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  567. #if WASM_ENABLE_AOT != 0
  568. module_common = (WASMModuleCommon*)
  569. aot_load_from_aot_file(buf, size, error_buf, error_buf_size);
  570. return register_module_with_null_name(module_common,
  571. error_buf, error_buf_size);
  572. #endif
  573. }
  574. if (size < 4)
  575. set_error_buf(error_buf, error_buf_size,
  576. "WASM module load failed: unexpected end");
  577. else
  578. set_error_buf(error_buf, error_buf_size,
  579. "WASM module load failed: magic header not detected");
  580. return NULL;
  581. }
  582. WASMModuleCommon *
  583. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  584. char *error_buf, uint32_t error_buf_size)
  585. {
  586. WASMModuleCommon *module_common;
  587. #if WASM_ENABLE_INTERP != 0
  588. if (!is_aot) {
  589. module_common = (WASMModuleCommon*)
  590. wasm_load_from_sections(section_list,
  591. error_buf, error_buf_size);
  592. return register_module_with_null_name(module_common,
  593. error_buf, error_buf_size);
  594. }
  595. #endif
  596. #if WASM_ENABLE_AOT != 0
  597. if (is_aot) {
  598. module_common = (WASMModuleCommon*)
  599. aot_load_from_sections(section_list,
  600. error_buf, error_buf_size);
  601. return register_module_with_null_name(module_common,
  602. error_buf, error_buf_size);
  603. }
  604. #endif
  605. set_error_buf(error_buf, error_buf_size,
  606. "WASM module load failed: invalid section list type");
  607. return NULL;
  608. }
  609. void
  610. wasm_runtime_unload(WASMModuleCommon *module)
  611. {
  612. #if WASM_ENABLE_MULTI_MODULE != 0
  613. /**
  614. * since we will unload and free all module when runtime_destroy()
  615. * we don't want users to unwillingly disrupt it
  616. */
  617. return;
  618. #endif
  619. #if WASM_ENABLE_INTERP != 0
  620. if (module->module_type == Wasm_Module_Bytecode) {
  621. wasm_unload((WASMModule*)module);
  622. return;
  623. }
  624. #endif
  625. #if WASM_ENABLE_AOT != 0
  626. if (module->module_type == Wasm_Module_AoT) {
  627. aot_unload((AOTModule*)module);
  628. return;
  629. }
  630. #endif
  631. }
  632. WASMModuleInstanceCommon *
  633. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  634. uint32 stack_size, uint32 heap_size,
  635. char *error_buf, uint32 error_buf_size)
  636. {
  637. #if WASM_ENABLE_INTERP != 0
  638. if (module->module_type == Wasm_Module_Bytecode)
  639. return (WASMModuleInstanceCommon*)
  640. wasm_instantiate((WASMModule*)module, is_sub_inst,
  641. stack_size, heap_size,
  642. error_buf, error_buf_size);
  643. #endif
  644. #if WASM_ENABLE_AOT != 0
  645. if (module->module_type == Wasm_Module_AoT)
  646. return (WASMModuleInstanceCommon*)
  647. aot_instantiate((AOTModule*)module, is_sub_inst,
  648. stack_size, heap_size,
  649. error_buf, error_buf_size);
  650. #endif
  651. set_error_buf(error_buf, error_buf_size,
  652. "Instantiate module failed, invalid module type");
  653. return NULL;
  654. }
  655. WASMModuleInstanceCommon *
  656. wasm_runtime_instantiate(WASMModuleCommon *module,
  657. uint32 stack_size, uint32 heap_size,
  658. char *error_buf, uint32 error_buf_size)
  659. {
  660. return wasm_runtime_instantiate_internal(module, false,
  661. stack_size, heap_size,
  662. error_buf, error_buf_size);
  663. }
  664. void
  665. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  666. bool is_sub_inst)
  667. {
  668. #if WASM_ENABLE_INTERP != 0
  669. if (module_inst->module_type == Wasm_Module_Bytecode) {
  670. wasm_deinstantiate((WASMModuleInstance*)module_inst, is_sub_inst);
  671. return;
  672. }
  673. #endif
  674. #if WASM_ENABLE_AOT != 0
  675. if (module_inst->module_type == Wasm_Module_AoT) {
  676. aot_deinstantiate((AOTModuleInstance*)module_inst, is_sub_inst);
  677. return;
  678. }
  679. #endif
  680. }
  681. void
  682. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  683. {
  684. return wasm_runtime_deinstantiate_internal(module_inst, false);
  685. }
  686. WASMExecEnv *
  687. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  688. uint32 stack_size)
  689. {
  690. return wasm_exec_env_create(module_inst, stack_size);
  691. }
  692. void
  693. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  694. {
  695. wasm_exec_env_destroy(exec_env);
  696. }
  697. WASMModuleInstanceCommon *
  698. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  699. {
  700. return wasm_exec_env_get_module_inst(exec_env);
  701. }
  702. void *
  703. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  704. {
  705. return exec_env->attachment;
  706. }
  707. void
  708. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  709. {
  710. exec_env->user_data = user_data;
  711. }
  712. void *
  713. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  714. {
  715. return exec_env->user_data;
  716. }
  717. WASMFunctionInstanceCommon *
  718. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  719. const char *name,
  720. const char *signature)
  721. {
  722. #if WASM_ENABLE_INTERP != 0
  723. if (module_inst->module_type == Wasm_Module_Bytecode)
  724. return (WASMFunctionInstanceCommon*)
  725. wasm_lookup_function((const WASMModuleInstance*)module_inst,
  726. name, signature);
  727. #endif
  728. #if WASM_ENABLE_AOT != 0
  729. if (module_inst->module_type == Wasm_Module_AoT)
  730. return (WASMFunctionInstanceCommon*)
  731. aot_lookup_function((const AOTModuleInstance*)module_inst,
  732. name, signature);
  733. #endif
  734. return NULL;
  735. }
  736. bool
  737. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  738. WASMFunctionInstanceCommon *function,
  739. uint32 argc, uint32 argv[])
  740. {
  741. if (!wasm_runtime_exec_env_check(exec_env)) {
  742. LOG_ERROR("Invalid exec env stack info.");
  743. return false;
  744. }
  745. /* set thread handle and stack boundary */
  746. wasm_exec_env_set_thread_info(exec_env);
  747. #if WASM_ENABLE_INTERP != 0
  748. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  749. return wasm_call_function(exec_env,
  750. (WASMFunctionInstance*)function,
  751. argc, argv);
  752. #endif
  753. #if WASM_ENABLE_AOT != 0
  754. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  755. return aot_call_function(exec_env,
  756. (AOTFunctionInstance*)function,
  757. argc, argv);
  758. #endif
  759. return false;
  760. }
  761. bool
  762. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  763. WASMFunctionInstanceCommon *function,
  764. uint32 argc, uint32 argv[])
  765. {
  766. #if WASM_ENABLE_INTERP != 0
  767. if (module_inst->module_type == Wasm_Module_Bytecode)
  768. return wasm_create_exec_env_and_call_function(
  769. (WASMModuleInstance*)module_inst,
  770. (WASMFunctionInstance*)function,
  771. argc, argv);
  772. #endif
  773. #if WASM_ENABLE_AOT != 0
  774. if (module_inst->module_type == Wasm_Module_AoT)
  775. return aot_create_exec_env_and_call_function(
  776. (AOTModuleInstance*)module_inst,
  777. (AOTFunctionInstance*)function,
  778. argc, argv);
  779. #endif
  780. return false;
  781. }
  782. void
  783. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  784. const char *exception)
  785. {
  786. #if WASM_ENABLE_INTERP != 0
  787. if (module_inst->module_type == Wasm_Module_Bytecode) {
  788. wasm_set_exception((WASMModuleInstance*)module_inst, exception);
  789. return;
  790. }
  791. #endif
  792. #if WASM_ENABLE_AOT != 0
  793. if (module_inst->module_type == Wasm_Module_AoT) {
  794. aot_set_exception((AOTModuleInstance*)module_inst, exception);
  795. return;
  796. }
  797. #endif
  798. }
  799. const char*
  800. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  801. {
  802. #if WASM_ENABLE_INTERP != 0
  803. if (module_inst->module_type == Wasm_Module_Bytecode) {
  804. return wasm_get_exception((WASMModuleInstance*)module_inst);
  805. }
  806. #endif
  807. #if WASM_ENABLE_AOT != 0
  808. if (module_inst->module_type == Wasm_Module_AoT) {
  809. return aot_get_exception((AOTModuleInstance*)module_inst);
  810. }
  811. #endif
  812. return NULL;
  813. }
  814. void
  815. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  816. {
  817. wasm_runtime_set_exception(module_inst, NULL);
  818. }
  819. void
  820. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  821. void *custom_data)
  822. {
  823. #if WASM_ENABLE_INTERP != 0
  824. if (module_inst->module_type == Wasm_Module_Bytecode) {
  825. ((WASMModuleInstance*)module_inst)->custom_data = custom_data;
  826. return;
  827. }
  828. #endif
  829. #if WASM_ENABLE_AOT != 0
  830. if (module_inst->module_type == Wasm_Module_AoT) {
  831. ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data;
  832. return;
  833. }
  834. #endif
  835. }
  836. void*
  837. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  838. {
  839. #if WASM_ENABLE_INTERP != 0
  840. if (module_inst->module_type == Wasm_Module_Bytecode)
  841. return ((WASMModuleInstance*)module_inst)->custom_data;
  842. #endif
  843. #if WASM_ENABLE_AOT != 0
  844. if (module_inst->module_type == Wasm_Module_AoT)
  845. return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
  846. #endif
  847. return NULL;
  848. }
  849. int32
  850. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  851. void **p_native_addr)
  852. {
  853. #if WASM_ENABLE_INTERP != 0
  854. if (module_inst->module_type == Wasm_Module_Bytecode)
  855. return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
  856. p_native_addr);
  857. #endif
  858. #if WASM_ENABLE_AOT != 0
  859. if (module_inst->module_type == Wasm_Module_AoT)
  860. return aot_module_malloc((AOTModuleInstance*)module_inst, size,
  861. p_native_addr);
  862. #endif
  863. return 0;
  864. }
  865. void
  866. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, int32 ptr)
  867. {
  868. #if WASM_ENABLE_INTERP != 0
  869. if (module_inst->module_type == Wasm_Module_Bytecode) {
  870. wasm_module_free((WASMModuleInstance*)module_inst, ptr);
  871. return;
  872. }
  873. #endif
  874. #if WASM_ENABLE_AOT != 0
  875. if (module_inst->module_type == Wasm_Module_AoT) {
  876. aot_module_free((AOTModuleInstance*)module_inst, ptr);
  877. return;
  878. }
  879. #endif
  880. }
  881. int32
  882. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  883. const char *src, uint32 size)
  884. {
  885. #if WASM_ENABLE_INTERP != 0
  886. if (module_inst->module_type == Wasm_Module_Bytecode) {
  887. return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size);
  888. }
  889. #endif
  890. #if WASM_ENABLE_AOT != 0
  891. if (module_inst->module_type == Wasm_Module_AoT) {
  892. return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size);
  893. }
  894. #endif
  895. return 0;
  896. }
  897. bool
  898. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  899. int32 app_offset, uint32 size)
  900. {
  901. #if WASM_ENABLE_INTERP != 0
  902. if (module_inst->module_type == Wasm_Module_Bytecode)
  903. return wasm_validate_app_addr((WASMModuleInstance*)module_inst,
  904. app_offset, size);
  905. #endif
  906. #if WASM_ENABLE_AOT != 0
  907. if (module_inst->module_type == Wasm_Module_AoT)
  908. return aot_validate_app_addr((AOTModuleInstance*)module_inst,
  909. app_offset, size);
  910. #endif
  911. return false;
  912. }
  913. bool
  914. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  915. int32 app_str_offset)
  916. {
  917. int32 app_end_offset;
  918. char *str, *str_end;
  919. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
  920. NULL, &app_end_offset))
  921. goto fail;
  922. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  923. str_end = str + (app_end_offset - app_str_offset);
  924. while (str < str_end && *str != '\0')
  925. str++;
  926. if (str == str_end)
  927. goto fail;
  928. return true;
  929. fail:
  930. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  931. return false;
  932. }
  933. bool
  934. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  935. void *native_ptr, uint32 size)
  936. {
  937. #if WASM_ENABLE_INTERP != 0
  938. if (module_inst->module_type == Wasm_Module_Bytecode)
  939. return wasm_validate_native_addr((WASMModuleInstance*)module_inst,
  940. native_ptr, size);
  941. #endif
  942. #if WASM_ENABLE_AOT != 0
  943. if (module_inst->module_type == Wasm_Module_AoT)
  944. return aot_validate_native_addr((AOTModuleInstance*)module_inst,
  945. native_ptr, size);
  946. #endif
  947. return false;
  948. }
  949. void *
  950. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  951. int32 app_offset)
  952. {
  953. #if WASM_ENABLE_INTERP != 0
  954. if (module_inst->module_type == Wasm_Module_Bytecode)
  955. return wasm_addr_app_to_native((WASMModuleInstance*)module_inst,
  956. app_offset);
  957. #endif
  958. #if WASM_ENABLE_AOT != 0
  959. if (module_inst->module_type == Wasm_Module_AoT)
  960. return aot_addr_app_to_native((AOTModuleInstance*)module_inst,
  961. app_offset);
  962. #endif
  963. return NULL;
  964. }
  965. int32
  966. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  967. void *native_ptr)
  968. {
  969. #if WASM_ENABLE_INTERP != 0
  970. if (module_inst->module_type == Wasm_Module_Bytecode)
  971. return wasm_addr_native_to_app((WASMModuleInstance*)module_inst,
  972. native_ptr);
  973. #endif
  974. #if WASM_ENABLE_AOT != 0
  975. if (module_inst->module_type == Wasm_Module_AoT)
  976. return aot_addr_native_to_app((AOTModuleInstance*)module_inst,
  977. native_ptr);
  978. #endif
  979. return 0;
  980. }
  981. bool
  982. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  983. int32 app_offset,
  984. int32 *p_app_start_offset,
  985. int32 *p_app_end_offset)
  986. {
  987. #if WASM_ENABLE_INTERP != 0
  988. if (module_inst->module_type == Wasm_Module_Bytecode)
  989. return wasm_get_app_addr_range((WASMModuleInstance*)module_inst,
  990. app_offset, p_app_start_offset,
  991. p_app_end_offset);
  992. #endif
  993. #if WASM_ENABLE_AOT != 0
  994. if (module_inst->module_type == Wasm_Module_AoT)
  995. return aot_get_app_addr_range((AOTModuleInstance*)module_inst,
  996. app_offset, p_app_start_offset,
  997. p_app_end_offset);
  998. #endif
  999. return false;
  1000. }
  1001. bool
  1002. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  1003. uint8_t *native_ptr,
  1004. uint8_t **p_native_start_addr,
  1005. uint8_t **p_native_end_addr)
  1006. {
  1007. #if WASM_ENABLE_INTERP != 0
  1008. if (module_inst->module_type == Wasm_Module_Bytecode)
  1009. return wasm_get_native_addr_range((WASMModuleInstance*)module_inst,
  1010. native_ptr, p_native_start_addr,
  1011. p_native_end_addr);
  1012. #endif
  1013. #if WASM_ENABLE_AOT != 0
  1014. if (module_inst->module_type == Wasm_Module_AoT)
  1015. return aot_get_native_addr_range((AOTModuleInstance*)module_inst,
  1016. native_ptr, p_native_start_addr,
  1017. p_native_end_addr);
  1018. #endif
  1019. return false;
  1020. }
  1021. uint32
  1022. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1023. {
  1024. #if WASM_ENABLE_INTERP != 0
  1025. if (module_inst->module_type == Wasm_Module_Bytecode)
  1026. return ((WASMModuleInstance*)module_inst)->temp_ret;
  1027. #endif
  1028. #if WASM_ENABLE_AOT != 0
  1029. if (module_inst->module_type == Wasm_Module_AoT)
  1030. return ((AOTModuleInstance*)module_inst)->temp_ret;
  1031. #endif
  1032. return 0;
  1033. }
  1034. void
  1035. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1036. uint32 temp_ret)
  1037. {
  1038. #if WASM_ENABLE_INTERP != 0
  1039. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1040. ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret;
  1041. return;
  1042. }
  1043. #endif
  1044. #if WASM_ENABLE_AOT != 0
  1045. if (module_inst->module_type == Wasm_Module_AoT) {
  1046. ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret;
  1047. return;
  1048. }
  1049. #endif
  1050. }
  1051. uint32
  1052. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1053. {
  1054. #if WASM_ENABLE_INTERP != 0
  1055. if (module_inst->module_type == Wasm_Module_Bytecode)
  1056. return ((WASMModuleInstance*)module_inst)->llvm_stack;
  1057. #endif
  1058. #if WASM_ENABLE_AOT != 0
  1059. if (module_inst->module_type == Wasm_Module_AoT)
  1060. return ((AOTModuleInstance*)module_inst)->llvm_stack;
  1061. #endif
  1062. return 0;
  1063. }
  1064. void
  1065. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1066. uint32 llvm_stack)
  1067. {
  1068. #if WASM_ENABLE_INTERP != 0
  1069. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1070. ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1071. return;
  1072. }
  1073. #endif
  1074. #if WASM_ENABLE_AOT != 0
  1075. if (module_inst->module_type == Wasm_Module_AoT) {
  1076. ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1077. return;
  1078. }
  1079. #endif
  1080. }
  1081. bool
  1082. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  1083. uint32 inc_page_count)
  1084. {
  1085. #if WASM_ENABLE_INTERP != 0
  1086. if (module->module_type == Wasm_Module_Bytecode)
  1087. return wasm_enlarge_memory((WASMModuleInstance*)module,
  1088. inc_page_count);
  1089. #endif
  1090. #if WASM_ENABLE_AOT != 0
  1091. if (module->module_type == Wasm_Module_AoT)
  1092. return aot_enlarge_memory((AOTModuleInstance*)module,
  1093. inc_page_count);
  1094. #endif
  1095. return false;
  1096. }
  1097. #if WASM_ENABLE_LIBC_WASI != 0
  1098. void
  1099. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  1100. const char *dir_list[], uint32 dir_count,
  1101. const char *map_dir_list[], uint32 map_dir_count,
  1102. const char *env_list[], uint32 env_count,
  1103. char *argv[], int argc)
  1104. {
  1105. WASIArguments *wasi_args = NULL;
  1106. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1107. if (module->module_type == Wasm_Module_Bytecode)
  1108. wasi_args = &((WASMModule*)module)->wasi_args;
  1109. #endif
  1110. #if WASM_ENABLE_AOT != 0
  1111. if (module->module_type == Wasm_Module_AoT)
  1112. wasi_args = &((AOTModule*)module)->wasi_args;
  1113. #endif
  1114. if (wasi_args) {
  1115. wasi_args->dir_list = dir_list;
  1116. wasi_args->dir_count = dir_count;
  1117. wasi_args->map_dir_list = map_dir_list;
  1118. wasi_args->map_dir_count = map_dir_count;
  1119. wasi_args->env = env_list;
  1120. wasi_args->env_count = env_count;
  1121. wasi_args->argv = argv;
  1122. wasi_args->argc = argc;
  1123. }
  1124. }
  1125. bool
  1126. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1127. const char *dir_list[], uint32 dir_count,
  1128. const char *map_dir_list[], uint32 map_dir_count,
  1129. const char *env[], uint32 env_count,
  1130. char *argv[], uint32 argc,
  1131. char *error_buf, uint32 error_buf_size)
  1132. {
  1133. WASIContext *wasi_ctx;
  1134. size_t *argv_offsets = NULL;
  1135. char *argv_buf = NULL;
  1136. size_t *env_offsets = NULL;
  1137. char *env_buf = NULL;
  1138. uint64 argv_buf_len = 0, env_buf_len = 0;
  1139. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  1140. struct fd_table *curfds;
  1141. struct fd_prestats *prestats;
  1142. struct argv_environ_values *argv_environ;
  1143. bool fd_table_inited = false, fd_prestats_inited = false;
  1144. bool argv_environ_inited = false;
  1145. int32 offset_argv_offsets = 0, offset_env_offsets = 0;
  1146. int32 offset_argv_buf = 0, offset_env_buf = 0;
  1147. int32 offset_curfds = 0;
  1148. int32 offset_prestats = 0;
  1149. int32 offset_argv_environ = 0;
  1150. __wasi_fd_t wasm_fd = 3;
  1151. int32 raw_fd;
  1152. char *path, resolved_path[PATH_MAX];
  1153. uint64 total_size;
  1154. uint32 i;
  1155. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL,
  1156. error_buf, error_buf_size))) {
  1157. return false;
  1158. }
  1159. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  1160. #if WASM_ENABLE_INTERP != 0
  1161. if (module_inst->module_type == Wasm_Module_Bytecode
  1162. && !((WASMModuleInstance*)module_inst)->default_memory)
  1163. return true;
  1164. #endif
  1165. #if WASM_ENABLE_AOT != 0
  1166. if (module_inst->module_type == Wasm_Module_AoT
  1167. && !((AOTModuleInstance*)module_inst)->
  1168. global_table_data.memory_instances[0].memory_data.ptr)
  1169. return true;
  1170. #endif
  1171. /* process argv[0], trip the path and suffix, only keep the program name */
  1172. for (i = 0; i < argc; i++)
  1173. argv_buf_len += strlen(argv[i]) + 1;
  1174. total_size = sizeof(size_t) * (uint64)argc;
  1175. if (total_size >= UINT32_MAX
  1176. || !(offset_argv_offsets = wasm_runtime_module_malloc
  1177. (module_inst, (uint32)total_size,
  1178. (void**)&argv_offsets))
  1179. || argv_buf_len >= UINT32_MAX
  1180. || !(offset_argv_buf = wasm_runtime_module_malloc
  1181. (module_inst, (uint32)argv_buf_len,
  1182. (void**)&argv_buf))) {
  1183. set_error_buf(error_buf, error_buf_size,
  1184. "Init wasi environment failed: allocate memory failed.");
  1185. goto fail;
  1186. }
  1187. for (i = 0; i < argc; i++) {
  1188. argv_offsets[i] = argv_buf_offset;
  1189. bh_strcpy_s(argv_buf + argv_buf_offset,
  1190. (uint32)argv_buf_len - argv_buf_offset, argv[i]);
  1191. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  1192. }
  1193. for (i = 0; i < env_count; i++)
  1194. env_buf_len += strlen(env[i]) + 1;
  1195. total_size = sizeof(size_t) * (uint64)argc;
  1196. if (total_size >= UINT32_MAX
  1197. || !(offset_env_offsets = wasm_runtime_module_malloc
  1198. (module_inst, (uint32)total_size,
  1199. (void**)&env_offsets))
  1200. || env_buf_len >= UINT32_MAX
  1201. || !(offset_env_buf = wasm_runtime_module_malloc
  1202. (module_inst, (uint32)env_buf_len,
  1203. (void**)&env_buf))) {
  1204. set_error_buf(error_buf, error_buf_size,
  1205. "Init wasi environment failed: allocate memory failed.");
  1206. goto fail;
  1207. }
  1208. for (i = 0; i < env_count; i++) {
  1209. env_offsets[i] = env_buf_offset;
  1210. bh_strcpy_s(env_buf + env_buf_offset,
  1211. (uint32)env_buf_len - env_buf_offset, env[i]);
  1212. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  1213. }
  1214. if (!(offset_curfds = wasm_runtime_module_malloc
  1215. (module_inst, sizeof(struct fd_table), (void**)&curfds))
  1216. || !(offset_prestats = wasm_runtime_module_malloc
  1217. (module_inst, sizeof(struct fd_prestats), (void**)&prestats))
  1218. || !(offset_argv_environ = wasm_runtime_module_malloc
  1219. (module_inst, sizeof(struct argv_environ_values),
  1220. (void**)&argv_environ))) {
  1221. set_error_buf(error_buf, error_buf_size,
  1222. "Init wasi environment failed: allocate memory failed.");
  1223. goto fail;
  1224. }
  1225. wasi_ctx->curfds_offset = offset_curfds;
  1226. wasi_ctx->prestats_offset = offset_prestats;
  1227. wasi_ctx->argv_environ_offset = offset_argv_environ;
  1228. wasi_ctx->argv_buf_offset = offset_argv_buf;
  1229. wasi_ctx->argv_offsets_offset = offset_argv_offsets;
  1230. wasi_ctx->env_buf_offset = offset_env_buf;
  1231. wasi_ctx->env_offsets_offset = offset_env_offsets;
  1232. if (!fd_table_init(curfds)) {
  1233. set_error_buf(error_buf, error_buf_size,
  1234. "Init wasi environment failed: "
  1235. "init fd table failed.");
  1236. goto fail;
  1237. }
  1238. fd_table_inited = true;
  1239. if (!fd_prestats_init(prestats)) {
  1240. set_error_buf(error_buf, error_buf_size,
  1241. "Init wasi environment failed: "
  1242. "init fd prestats failed.");
  1243. goto fail;
  1244. }
  1245. fd_prestats_inited = true;
  1246. if (!argv_environ_init(argv_environ,
  1247. argv_offsets, argc,
  1248. argv_buf, argv_buf_len,
  1249. env_offsets, env_count,
  1250. env_buf, env_buf_len)) {
  1251. set_error_buf(error_buf, error_buf_size,
  1252. "Init wasi environment failed: "
  1253. "init argument environment failed.");
  1254. goto fail;
  1255. }
  1256. argv_environ_inited = true;
  1257. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  1258. if (!fd_table_insert_existing(curfds, 0, 0)
  1259. || !fd_table_insert_existing(curfds, 1, 1)
  1260. || !fd_table_insert_existing(curfds, 2, 2)) {
  1261. set_error_buf(error_buf, error_buf_size,
  1262. "Init wasi environment failed: init fd table failed.");
  1263. goto fail;
  1264. }
  1265. wasm_fd = 3;
  1266. for (i = 0; i < dir_count; i++, wasm_fd++) {
  1267. path = realpath(dir_list[i], resolved_path);
  1268. if (!path) {
  1269. if (error_buf)
  1270. snprintf(error_buf, error_buf_size,
  1271. "error while pre-opening directory %s: %d\n",
  1272. dir_list[i], errno);
  1273. goto fail;
  1274. }
  1275. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  1276. if (raw_fd == -1) {
  1277. if (error_buf)
  1278. snprintf(error_buf, error_buf_size,
  1279. "error while pre-opening directory %s: %d\n",
  1280. dir_list[i], errno);
  1281. goto fail;
  1282. }
  1283. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  1284. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  1285. }
  1286. return true;
  1287. fail:
  1288. if (argv_environ_inited)
  1289. argv_environ_destroy(argv_environ);
  1290. if (fd_prestats_inited)
  1291. fd_prestats_destroy(prestats);
  1292. if (fd_table_inited)
  1293. fd_table_destroy(curfds);
  1294. if (offset_curfds != 0)
  1295. wasm_runtime_module_free(module_inst, offset_curfds);
  1296. if (offset_prestats != 0)
  1297. wasm_runtime_module_free(module_inst, offset_prestats);
  1298. if (offset_argv_environ != 0)
  1299. wasm_runtime_module_free(module_inst, offset_argv_environ);
  1300. if (offset_argv_buf)
  1301. wasm_runtime_module_free(module_inst, offset_argv_buf);
  1302. if (offset_argv_offsets)
  1303. wasm_runtime_module_free(module_inst, offset_argv_offsets);
  1304. if (offset_env_buf)
  1305. wasm_runtime_module_free(module_inst, offset_env_buf);
  1306. if (offset_env_offsets)
  1307. wasm_runtime_module_free(module_inst, offset_env_offsets);
  1308. return false;
  1309. }
  1310. bool
  1311. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  1312. {
  1313. #if WASM_ENABLE_INTERP != 0
  1314. if (module_inst->module_type == Wasm_Module_Bytecode
  1315. && ((WASMModuleInstance*)module_inst)->module->is_wasi_module)
  1316. return true;
  1317. #endif
  1318. #if WASM_ENABLE_AOT != 0
  1319. if (module_inst->module_type == Wasm_Module_AoT
  1320. && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr)
  1321. ->is_wasi_module)
  1322. return true;
  1323. #endif
  1324. return false;
  1325. }
  1326. WASMFunctionInstanceCommon *
  1327. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  1328. {
  1329. uint32 i;
  1330. #if WASM_ENABLE_INTERP != 0
  1331. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1332. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1333. WASMFunctionInstance *func;
  1334. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1335. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  1336. func = wasm_inst->export_functions[i].function;
  1337. if (func->u.func->func_type->param_count != 0
  1338. || func->u.func->func_type->result_count != 0) {
  1339. LOG_ERROR("Lookup wasi _start function failed: "
  1340. "invalid function type.\n");
  1341. return NULL;
  1342. }
  1343. return (WASMFunctionInstanceCommon*)func;
  1344. }
  1345. }
  1346. return NULL;
  1347. }
  1348. #endif
  1349. #if WASM_ENABLE_AOT != 0
  1350. if (module_inst->module_type == Wasm_Module_AoT) {
  1351. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1352. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1353. aot_inst->export_funcs.ptr;
  1354. for (i = 0; i < aot_inst->export_func_count; i++) {
  1355. if (!strcmp(export_funcs[i].func_name, "_start")) {
  1356. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  1357. if (func_type->param_count != 0
  1358. || func_type->result_count != 0) {
  1359. LOG_ERROR("Lookup wasi _start function failed: "
  1360. "invalid function type.\n");
  1361. return NULL;
  1362. }
  1363. return (WASMFunctionInstanceCommon*)&export_funcs[i];
  1364. }
  1365. }
  1366. return NULL;
  1367. }
  1368. #endif /* end of WASM_ENABLE_AOT */
  1369. return NULL;
  1370. }
  1371. void
  1372. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  1373. {
  1374. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  1375. struct argv_environ_values *argv_environ;
  1376. struct fd_table *curfds;
  1377. struct fd_prestats *prestats;
  1378. if (wasi_ctx) {
  1379. if (wasi_ctx->argv_environ_offset) {
  1380. argv_environ = (struct argv_environ_values *)
  1381. wasm_runtime_addr_app_to_native(module_inst,
  1382. wasi_ctx->argv_environ_offset);
  1383. argv_environ_destroy(argv_environ);
  1384. wasm_runtime_module_free(module_inst, wasi_ctx->argv_environ_offset);
  1385. }
  1386. if (wasi_ctx->curfds_offset) {
  1387. curfds = (struct fd_table *)
  1388. wasm_runtime_addr_app_to_native(module_inst,
  1389. wasi_ctx->curfds_offset);
  1390. fd_table_destroy(curfds);
  1391. wasm_runtime_module_free(module_inst, wasi_ctx->curfds_offset);
  1392. }
  1393. if (wasi_ctx->prestats_offset) {
  1394. prestats = (struct fd_prestats *)
  1395. wasm_runtime_addr_app_to_native(module_inst,
  1396. wasi_ctx->prestats_offset);
  1397. fd_prestats_destroy(prestats);
  1398. wasm_runtime_module_free(module_inst, wasi_ctx->prestats_offset);
  1399. }
  1400. if (wasi_ctx->argv_buf_offset)
  1401. wasm_runtime_module_free(module_inst, wasi_ctx->argv_buf_offset);
  1402. if (wasi_ctx->argv_offsets_offset)
  1403. wasm_runtime_module_free(module_inst, wasi_ctx->argv_offsets_offset);
  1404. if (wasi_ctx->env_buf_offset)
  1405. wasm_runtime_module_free(module_inst, wasi_ctx->env_buf_offset);
  1406. if (wasi_ctx->env_offsets_offset)
  1407. wasm_runtime_module_free(module_inst, wasi_ctx->env_offsets_offset);
  1408. wasm_runtime_free(wasi_ctx);
  1409. }
  1410. }
  1411. WASIContext *
  1412. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  1413. {
  1414. #if WASM_ENABLE_INTERP != 0
  1415. if (module_inst->module_type == Wasm_Module_Bytecode)
  1416. return ((WASMModuleInstance*)module_inst)->wasi_ctx;
  1417. #endif
  1418. #if WASM_ENABLE_AOT != 0
  1419. if (module_inst->module_type == Wasm_Module_AoT)
  1420. return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr;
  1421. #endif
  1422. return NULL;
  1423. }
  1424. void
  1425. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  1426. WASIContext *wasi_ctx)
  1427. {
  1428. #if WASM_ENABLE_INTERP != 0
  1429. if (module_inst->module_type == Wasm_Module_Bytecode)
  1430. ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx;
  1431. #endif
  1432. #if WASM_ENABLE_AOT != 0
  1433. if (module_inst->module_type == Wasm_Module_AoT)
  1434. ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx;
  1435. #endif
  1436. }
  1437. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1438. WASMModuleCommon*
  1439. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  1440. {
  1441. WASMModuleInstanceCommon *module_inst =
  1442. wasm_runtime_get_module_inst(exec_env);
  1443. #if WASM_ENABLE_INTERP != 0
  1444. if (module_inst->module_type == Wasm_Module_Bytecode)
  1445. return (WASMModuleCommon*)
  1446. ((WASMModuleInstance*)module_inst)->module;
  1447. #endif
  1448. #if WASM_ENABLE_AOT != 0
  1449. if (module_inst->module_type == Wasm_Module_AoT)
  1450. return (WASMModuleCommon*)
  1451. ((AOTModuleInstance*)module_inst)->aot_module.ptr;
  1452. #endif
  1453. return NULL;
  1454. }
  1455. /**
  1456. * Implementation of wasm_application_execute_main()
  1457. */
  1458. static WASMFunctionInstanceCommon*
  1459. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1460. const char *name);
  1461. static bool
  1462. check_main_func_type(const WASMType *type)
  1463. {
  1464. if (!(type->param_count == 0 || type->param_count == 2)
  1465. ||type->result_count > 1) {
  1466. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1467. return false;
  1468. }
  1469. if (type->param_count == 2
  1470. && !(type->types[0] == VALUE_TYPE_I32
  1471. && type->types[1] == VALUE_TYPE_I32)) {
  1472. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1473. return false;
  1474. }
  1475. if (type->result_count
  1476. && type->types[type->param_count] != VALUE_TYPE_I32) {
  1477. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1478. return false;
  1479. }
  1480. return true;
  1481. }
  1482. bool
  1483. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  1484. int32 argc, char *argv[])
  1485. {
  1486. WASMFunctionInstanceCommon *func;
  1487. WASMType *func_type = NULL;
  1488. uint32 argc1 = 0, argv1[2] = { 0 };
  1489. uint32 total_argv_size = 0;
  1490. uint64 total_size;
  1491. int32 argv_buf_offset, i;
  1492. char *argv_buf, *p, *p_end;
  1493. int32 *argv_offsets;
  1494. #if WASM_ENABLE_LIBC_WASI != 0
  1495. if (wasm_runtime_is_wasi_mode(module_inst)) {
  1496. /* In wasi mode, we should call function named "_start"
  1497. which initializes the wasi envrionment and then calls
  1498. the actual main function. Directly call main function
  1499. may cause exception thrown. */
  1500. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
  1501. return wasm_runtime_create_exec_env_and_call_wasm(
  1502. module_inst, func, 0, NULL);
  1503. /* if no start function is found, we execute
  1504. the main function as normal */
  1505. }
  1506. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1507. func = resolve_function(module_inst, "_main");
  1508. if (!func) {
  1509. func = resolve_function(module_inst, "main");
  1510. }
  1511. if (!func) {
  1512. wasm_runtime_set_exception(module_inst,
  1513. "lookup main function failed.");
  1514. return false;
  1515. }
  1516. #if WASM_ENABLE_INTERP != 0
  1517. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1518. if (((WASMFunctionInstance*)func)->is_import_func) {
  1519. wasm_runtime_set_exception(module_inst,
  1520. "lookup main function failed.");
  1521. return false;
  1522. }
  1523. func_type = ((WASMFunctionInstance*)func)->u.func->func_type;
  1524. }
  1525. #endif
  1526. #if WASM_ENABLE_AOT != 0
  1527. if (module_inst->module_type == Wasm_Module_AoT)
  1528. func_type = ((AOTFunctionInstance*)func)->u.func.func_type;
  1529. #endif
  1530. if (!check_main_func_type(func_type)) {
  1531. wasm_runtime_set_exception(module_inst,
  1532. "invalid function type of main function.");
  1533. return false;
  1534. }
  1535. if (func_type->param_count) {
  1536. for (i = 0; i < argc; i++)
  1537. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  1538. total_argv_size = align_uint(total_argv_size, 4);
  1539. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  1540. if (total_size >= UINT32_MAX
  1541. || !(argv_buf_offset =
  1542. wasm_runtime_module_malloc(module_inst, (uint32)total_size,
  1543. (void**)&argv_buf))) {
  1544. wasm_runtime_set_exception(module_inst,
  1545. "allocate memory failed.");
  1546. return false;
  1547. }
  1548. p = argv_buf;
  1549. argv_offsets = (int32*)(p + total_argv_size);
  1550. p_end = p + total_size;
  1551. for (i = 0; i < argc; i++) {
  1552. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  1553. argv_offsets[i] = argv_buf_offset + (int32)(p - argv_buf);
  1554. p += strlen(argv[i]) + 1;
  1555. }
  1556. argc1 = 2;
  1557. argv1[0] = (uint32)argc;
  1558. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  1559. }
  1560. return wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  1561. argc1, argv1);
  1562. }
  1563. #if WASM_ENABLE_MULTI_MODULE != 0
  1564. static WASMModuleInstance *
  1565. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  1566. const char *sub_module_name)
  1567. {
  1568. WASMSubModInstNode *node =
  1569. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  1570. while (node && strcmp(node->module_name, sub_module_name)) {
  1571. node = bh_list_elem_next(node);
  1572. }
  1573. return node ? node->module_inst : NULL;
  1574. }
  1575. static bool
  1576. parse_function_name(char *orig_function_name, char **p_module_name,
  1577. char **p_function_name)
  1578. {
  1579. if (orig_function_name[0] != '$') {
  1580. *p_module_name = NULL;
  1581. *p_function_name = orig_function_name;
  1582. return true;
  1583. }
  1584. /**
  1585. * $module_name$function_name\0
  1586. * ===>
  1587. * module_name\0function_name\0
  1588. * ===>
  1589. * module_name
  1590. * function_name
  1591. */
  1592. char *p1 = orig_function_name;
  1593. char *p2 = strchr(p1 + 1, '$');
  1594. if (!p2) {
  1595. LOG_DEBUG("can not parse the incoming function name");
  1596. return false;
  1597. }
  1598. *p_module_name = p1 + 1;
  1599. *p2 = '\0';
  1600. *p_function_name = p2 + 1;
  1601. return strlen(*p_module_name) && strlen(*p_function_name);
  1602. }
  1603. #endif
  1604. /**
  1605. * Implementation of wasm_application_execute_func()
  1606. */
  1607. static WASMFunctionInstanceCommon*
  1608. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1609. const char *name)
  1610. {
  1611. uint32 i = 0;
  1612. WASMFunctionInstanceCommon *ret = NULL;
  1613. #if WASM_ENABLE_MULTI_MODULE != 0
  1614. WASMModuleInstance *sub_module_inst = NULL;
  1615. char *orig_name = NULL;
  1616. char *sub_module_name = NULL;
  1617. char *function_name = NULL;
  1618. uint32 length = strlen(name) + 1;
  1619. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  1620. if (!orig_name) {
  1621. return NULL;
  1622. }
  1623. strncpy(orig_name, name, length);
  1624. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  1625. goto LEAVE;
  1626. }
  1627. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  1628. if (sub_module_name) {
  1629. sub_module_inst = get_sub_module_inst(
  1630. (WASMModuleInstance *)module_inst, sub_module_name);
  1631. if (!sub_module_inst) {
  1632. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  1633. goto LEAVE;
  1634. }
  1635. }
  1636. #else
  1637. const char *function_name = name;
  1638. #endif
  1639. #if WASM_ENABLE_INTERP != 0
  1640. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1641. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1642. #if WASM_ENABLE_MULTI_MODULE != 0
  1643. wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
  1644. #endif /* WASM_ENABLE_MULTI_MODULE */
  1645. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1646. if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
  1647. ret = wasm_inst->export_functions[i].function;
  1648. break;
  1649. }
  1650. }
  1651. }
  1652. #endif /* WASM_ENABLE_INTERP */
  1653. #if WASM_ENABLE_AOT != 0
  1654. if (module_inst->module_type == Wasm_Module_AoT) {
  1655. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1656. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1657. aot_inst->export_funcs.ptr;
  1658. for (i = 0; i < aot_inst->export_func_count; i++) {
  1659. if (!strcmp(export_funcs[i].func_name, function_name)) {
  1660. ret = &export_funcs[i];
  1661. break;
  1662. }
  1663. }
  1664. }
  1665. #endif
  1666. #if WASM_ENABLE_MULTI_MODULE != 0
  1667. LEAVE:
  1668. wasm_runtime_free(orig_name);
  1669. #endif
  1670. return ret;
  1671. }
  1672. union ieee754_float {
  1673. float f;
  1674. /* This is the IEEE 754 single-precision format. */
  1675. union {
  1676. struct {
  1677. unsigned int negative:1;
  1678. unsigned int exponent:8;
  1679. unsigned int mantissa:23;
  1680. } ieee_big_endian;
  1681. struct {
  1682. unsigned int mantissa:23;
  1683. unsigned int exponent:8;
  1684. unsigned int negative:1;
  1685. } ieee_little_endian;
  1686. } ieee;
  1687. };
  1688. union ieee754_double {
  1689. double d;
  1690. /* This is the IEEE 754 double-precision format. */
  1691. union {
  1692. struct {
  1693. unsigned int negative:1;
  1694. unsigned int exponent:11;
  1695. /* Together these comprise the mantissa. */
  1696. unsigned int mantissa0:20;
  1697. unsigned int mantissa1:32;
  1698. } ieee_big_endian;
  1699. struct {
  1700. /* Together these comprise the mantissa. */
  1701. unsigned int mantissa1:32;
  1702. unsigned int mantissa0:20;
  1703. unsigned int exponent:11;
  1704. unsigned int negative:1;
  1705. } ieee_little_endian;
  1706. } ieee;
  1707. };
  1708. static union {
  1709. int a;
  1710. char b;
  1711. } __ue = { .a = 1 };
  1712. #define is_little_endian() (__ue.b == 1)
  1713. bool
  1714. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  1715. const char *name, int32 argc, char *argv[])
  1716. {
  1717. WASMFunctionInstanceCommon *func;
  1718. WASMType *type = NULL;
  1719. uint32 argc1, *argv1 = NULL, cell_num, j, k = 0;
  1720. int32 i, p;
  1721. uint64 total_size;
  1722. const char *exception;
  1723. char buf[128];
  1724. bh_assert(argc >= 0);
  1725. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  1726. func = resolve_function(module_inst, name);
  1727. if (!func) {
  1728. snprintf(buf, sizeof(buf), "lookup function %s failed.", name);
  1729. wasm_runtime_set_exception(module_inst, buf);
  1730. goto fail;
  1731. }
  1732. #if WASM_ENABLE_INTERP != 0
  1733. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1734. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func;
  1735. if (wasm_func->is_import_func
  1736. #if WASM_ENABLE_MULTI_MODULE != 0
  1737. && !wasm_func->import_func_inst
  1738. #endif
  1739. ) {
  1740. snprintf(buf, sizeof(buf), "lookup function %s failed.", name);
  1741. wasm_runtime_set_exception(module_inst, buf);
  1742. goto fail;
  1743. }
  1744. type = wasm_func->u.func->func_type;
  1745. argc1 = wasm_func->param_cell_num;
  1746. cell_num = argc1 > wasm_func->ret_cell_num ?
  1747. argc1 : wasm_func->ret_cell_num;
  1748. }
  1749. #endif
  1750. #if WASM_ENABLE_AOT != 0
  1751. if (module_inst->module_type == Wasm_Module_AoT) {
  1752. type = ((AOTFunctionInstance*)func)->u.func.func_type;
  1753. argc1 = type->param_cell_num;
  1754. cell_num = argc1 > type->ret_cell_num ?
  1755. argc1 : type->ret_cell_num;
  1756. }
  1757. #endif
  1758. if (type->param_count != (uint32)argc) {
  1759. wasm_runtime_set_exception(module_inst,
  1760. "invalid input argument count.");
  1761. goto fail;
  1762. }
  1763. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1764. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst,
  1765. NULL, 0)))) {
  1766. goto fail;
  1767. }
  1768. /* Clear errno before parsing arguments */
  1769. errno = 0;
  1770. /* Parse arguments */
  1771. for (i = 0, p = 0; i < argc; i++) {
  1772. char *endptr = NULL;
  1773. bh_assert(argv[i] != NULL);
  1774. if (argv[i][0] == '\0') {
  1775. snprintf(buf, sizeof(buf), "invalid input argument %d.", i);
  1776. wasm_runtime_set_exception(module_inst, buf);
  1777. goto fail;
  1778. }
  1779. switch (type->types[i]) {
  1780. case VALUE_TYPE_I32:
  1781. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  1782. break;
  1783. case VALUE_TYPE_I64:
  1784. {
  1785. union { uint64 val; uint32 parts[2]; } u;
  1786. u.val = strtoull(argv[i], &endptr, 0);
  1787. argv1[p++] = u.parts[0];
  1788. argv1[p++] = u.parts[1];
  1789. break;
  1790. }
  1791. case VALUE_TYPE_F32:
  1792. {
  1793. float32 f32 = strtof(argv[i], &endptr);
  1794. if (isnan(f32)) {
  1795. if (argv[i][0] == '-') {
  1796. union ieee754_float u;
  1797. u.f = f32;
  1798. if (is_little_endian())
  1799. u.ieee.ieee_little_endian.negative = 1;
  1800. else
  1801. u.ieee.ieee_big_endian.negative = 1;
  1802. memcpy(&f32, &u.f, sizeof(float));
  1803. }
  1804. if (endptr[0] == ':') {
  1805. uint32 sig;
  1806. union ieee754_float u;
  1807. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  1808. u.f = f32;
  1809. if (is_little_endian())
  1810. u.ieee.ieee_little_endian.mantissa = sig;
  1811. else
  1812. u.ieee.ieee_big_endian.mantissa = sig;
  1813. memcpy(&f32, &u.f, sizeof(float));
  1814. }
  1815. }
  1816. memcpy(&argv1[p++], &f32, sizeof(float));
  1817. break;
  1818. }
  1819. case VALUE_TYPE_F64:
  1820. {
  1821. union { float64 val; uint32 parts[2]; } u;
  1822. u.val = strtod(argv[i], &endptr);
  1823. if (isnan(u.val)) {
  1824. if (argv[i][0] == '-') {
  1825. union ieee754_double ud;
  1826. ud.d = u.val;
  1827. if (is_little_endian())
  1828. ud.ieee.ieee_little_endian.negative = 1;
  1829. else
  1830. ud.ieee.ieee_big_endian.negative = 1;
  1831. memcpy(&u.val, &ud.d, sizeof(double));
  1832. }
  1833. if (endptr[0] == ':') {
  1834. uint64 sig;
  1835. union ieee754_double ud;
  1836. sig = strtoull(endptr + 1, &endptr, 0);
  1837. ud.d = u.val;
  1838. if (is_little_endian()) {
  1839. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  1840. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  1841. }
  1842. else {
  1843. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  1844. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  1845. }
  1846. memcpy(&u.val, &ud.d, sizeof(double));
  1847. }
  1848. }
  1849. argv1[p++] = u.parts[0];
  1850. argv1[p++] = u.parts[1];
  1851. break;
  1852. }
  1853. }
  1854. if (endptr && *endptr != '\0' && *endptr != '_') {
  1855. snprintf(buf, sizeof(buf), "invalid input argument %d: %s.",
  1856. i, argv[i]);
  1857. wasm_runtime_set_exception(module_inst, buf);
  1858. goto fail;
  1859. }
  1860. if (errno != 0) {
  1861. snprintf(buf, sizeof(buf),
  1862. "prepare function argument error, errno: %d.", errno);
  1863. wasm_runtime_set_exception(module_inst, buf);
  1864. goto fail;
  1865. }
  1866. }
  1867. bh_assert(p == (int32)argc1);
  1868. wasm_runtime_set_exception(module_inst, NULL);
  1869. if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  1870. argc1, argv1)) {
  1871. goto fail;
  1872. }
  1873. /* print return value */
  1874. for (j = 0; j < type->result_count; j++) {
  1875. switch (type->types[type->param_count + j]) {
  1876. case VALUE_TYPE_I32:
  1877. os_printf("0x%x:i32", argv1[k]);
  1878. k++;
  1879. break;
  1880. case VALUE_TYPE_I64:
  1881. {
  1882. union { uint64 val; uint32 parts[2]; } u;
  1883. u.parts[0] = argv1[k];
  1884. u.parts[1] = argv1[k + 1];
  1885. k += 2;
  1886. #ifdef PRIx64
  1887. os_printf("0x%"PRIx64":i64", u.val);
  1888. #else
  1889. char buf[16];
  1890. if (sizeof(long) == 4)
  1891. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  1892. else
  1893. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  1894. os_printf(buf, u.val);
  1895. #endif
  1896. break;
  1897. }
  1898. case VALUE_TYPE_F32:
  1899. os_printf("%.7g:f32", *(float32*)(argv1 + k));
  1900. k++;
  1901. break;
  1902. case VALUE_TYPE_F64:
  1903. {
  1904. union { float64 val; uint32 parts[2]; } u;
  1905. u.parts[0] = argv1[k];
  1906. u.parts[1] = argv1[k + 1];
  1907. k += 2;
  1908. os_printf("%.7g:f64", u.val);
  1909. break;
  1910. }
  1911. }
  1912. if (j < (uint32)(type->result_count - 1))
  1913. os_printf(",");
  1914. }
  1915. os_printf("\n");
  1916. wasm_runtime_free(argv1);
  1917. return true;
  1918. fail:
  1919. if (argv1)
  1920. wasm_runtime_free(argv1);
  1921. exception = wasm_runtime_get_exception(module_inst);
  1922. bh_assert(exception);
  1923. os_printf("%s\n", exception);
  1924. return false;
  1925. }
  1926. bool
  1927. wasm_runtime_register_natives(const char *module_name,
  1928. NativeSymbol *native_symbols,
  1929. uint32 n_native_symbols)
  1930. {
  1931. return wasm_native_register_natives(module_name,
  1932. native_symbols, n_native_symbols);
  1933. }
  1934. bool
  1935. wasm_runtime_register_natives_raw(const char *module_name,
  1936. NativeSymbol *native_symbols,
  1937. uint32 n_native_symbols)
  1938. {
  1939. return wasm_native_register_natives_raw(module_name,
  1940. native_symbols, n_native_symbols);
  1941. }
  1942. bool
  1943. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  1944. const WASMType *func_type, const char *signature,
  1945. void *attachment,
  1946. uint32 *argv, uint32 argc, uint32 *argv_ret)
  1947. {
  1948. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  1949. typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*);
  1950. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  1951. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  1952. uint32 *argv_src = argv, i, argc1, ptr_len;
  1953. int32 arg_i32;
  1954. bool ret = false;
  1955. argc1 = func_type->param_count;
  1956. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  1957. size = sizeof(uint64) * (uint64)argc1;
  1958. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  1959. NULL, 0))) {
  1960. return false;
  1961. }
  1962. }
  1963. argv_dst = argv1;
  1964. /* Traverse secondly to fill in each argument */
  1965. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  1966. switch (func_type->types[i]) {
  1967. case VALUE_TYPE_I32:
  1968. {
  1969. *(int32*)argv_dst = arg_i32 = (int32)*argv_src++;
  1970. if (signature) {
  1971. if (signature[i + 1] == '*') {
  1972. /* param is a pointer */
  1973. if (signature[i + 2] == '~')
  1974. /* pointer with length followed */
  1975. ptr_len = *argv_src;
  1976. else
  1977. /* pointer without length followed */
  1978. ptr_len = 1;
  1979. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  1980. goto fail;
  1981. *(uintptr_t*)argv_dst = (uintptr_t)
  1982. wasm_runtime_addr_app_to_native(module, arg_i32);
  1983. }
  1984. else if (signature[i + 1] == '$') {
  1985. /* param is a string */
  1986. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  1987. goto fail;
  1988. *(uintptr_t*)argv_dst = (uintptr_t)
  1989. wasm_runtime_addr_app_to_native(module, arg_i32);
  1990. }
  1991. }
  1992. break;
  1993. }
  1994. case VALUE_TYPE_I64:
  1995. case VALUE_TYPE_F64:
  1996. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2);
  1997. argv_src += 2;
  1998. break;
  1999. case VALUE_TYPE_F32:
  2000. *(float32*)argv_dst = *(float32*)argv_src++;
  2001. break;
  2002. default:
  2003. bh_assert(0);
  2004. break;
  2005. }
  2006. }
  2007. exec_env->attachment = attachment;
  2008. invokeNativeRaw(exec_env, argv1);
  2009. exec_env->attachment = NULL;
  2010. if (func_type->result_count > 0) {
  2011. switch (func_type->types[func_type->param_count]) {
  2012. case VALUE_TYPE_I32:
  2013. argv_ret[0] = *(uint32*)argv1;
  2014. break;
  2015. case VALUE_TYPE_F32:
  2016. *(float32*)argv_ret = *(float32*)argv1;
  2017. break;
  2018. case VALUE_TYPE_I64:
  2019. case VALUE_TYPE_F64:
  2020. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64));
  2021. break;
  2022. default:
  2023. bh_assert(0);
  2024. break;
  2025. }
  2026. }
  2027. ret = true;
  2028. fail:
  2029. if (argv1 != argv_buf)
  2030. wasm_runtime_free(argv1);
  2031. return ret;
  2032. }
  2033. /**
  2034. * Implementation of wasm_runtime_invoke_native()
  2035. */
  2036. static inline void
  2037. word_copy(uint32 *dest, uint32 *src, unsigned num)
  2038. {
  2039. for (; num > 0; num--)
  2040. *dest++ = *src++;
  2041. }
  2042. #define PUT_I64_TO_ADDR(addr, value) do { \
  2043. union { int64 val; uint32 parts[2]; } u; \
  2044. u.val = (value); \
  2045. (addr)[0] = u.parts[0]; \
  2046. (addr)[1] = u.parts[1]; \
  2047. } while (0)
  2048. #define PUT_F64_TO_ADDR(addr, value) do { \
  2049. union { float64 val; uint32 parts[2]; } u; \
  2050. u.val = (value); \
  2051. (addr)[0] = u.parts[0]; \
  2052. (addr)[1] = u.parts[1]; \
  2053. } while (0)
  2054. /* The invoke native implementation on ARM platform with VFP co-processor */
  2055. #if defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP)
  2056. typedef void (*GenericFunctionPointer)();
  2057. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2058. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2059. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2060. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32);
  2061. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2062. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2063. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  2064. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  2065. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  2066. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  2067. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  2068. #define MAX_REG_INTS 4
  2069. #define MAX_REG_FLOATS 16
  2070. bool
  2071. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2072. const WASMType *func_type, const char *signature,
  2073. void *attachment,
  2074. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2075. {
  2076. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2077. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2078. uint32 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size;
  2079. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_fps = 0, n_stacks = 0;
  2080. uint32 arg_i32, ptr_len;
  2081. uint32 result_count = func_type->result_count;
  2082. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2083. bool ret = false;
  2084. n_ints++; /* exec env */
  2085. /* Traverse firstly to calculate stack args count */
  2086. for (i = 0; i < func_type->param_count; i++) {
  2087. switch (func_type->types[i]) {
  2088. case VALUE_TYPE_I32:
  2089. if (n_ints < MAX_REG_INTS)
  2090. n_ints++;
  2091. else
  2092. n_stacks++;
  2093. break;
  2094. case VALUE_TYPE_I64:
  2095. if (n_ints < MAX_REG_INTS - 1) {
  2096. /* 64-bit data must be 8 bytes aligned in arm */
  2097. if (n_ints & 1)
  2098. n_ints++;
  2099. n_ints += 2;
  2100. }
  2101. else {
  2102. /* 64-bit data must be 8 bytes aligned in arm */
  2103. if (n_stacks & 1)
  2104. n_stacks++;
  2105. n_stacks += 2;
  2106. }
  2107. break;
  2108. case VALUE_TYPE_F32:
  2109. if (n_fps < MAX_REG_FLOATS)
  2110. n_fps++;
  2111. else
  2112. n_stacks++;
  2113. break;
  2114. case VALUE_TYPE_F64:
  2115. if (n_fps < MAX_REG_FLOATS - 1) {
  2116. /* 64-bit data must be 8 bytes aligned in arm */
  2117. if (n_fps & 1)
  2118. n_fps++;
  2119. n_fps += 2;
  2120. }
  2121. else {
  2122. /* 64-bit data must be 8 bytes aligned in arm */
  2123. if (n_stacks & 1)
  2124. n_stacks++;
  2125. n_stacks += 2;
  2126. }
  2127. break;
  2128. default:
  2129. bh_assert(0);
  2130. break;
  2131. }
  2132. }
  2133. for (i = 0; i < ext_ret_count; i++) {
  2134. if (n_ints < MAX_REG_INTS)
  2135. n_ints++;
  2136. else
  2137. n_stacks++;
  2138. }
  2139. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2140. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2141. size = sizeof(uint32) * (uint32)argc1;
  2142. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2143. NULL, 0))) {
  2144. return false;
  2145. }
  2146. }
  2147. ints = argv1;
  2148. fps = ints + MAX_REG_INTS;
  2149. stacks = fps + MAX_REG_FLOATS;
  2150. n_ints = 0;
  2151. n_fps = 0;
  2152. n_stacks = 0;
  2153. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2154. /* Traverse secondly to fill in each argument */
  2155. for (i = 0; i < func_type->param_count; i++) {
  2156. switch (func_type->types[i]) {
  2157. case VALUE_TYPE_I32:
  2158. {
  2159. arg_i32 = *argv_src++;
  2160. if (signature) {
  2161. if (signature[i + 1] == '*') {
  2162. /* param is a pointer */
  2163. if (signature[i + 2] == '~')
  2164. /* pointer with length followed */
  2165. ptr_len = *argv_src;
  2166. else
  2167. /* pointer without length followed */
  2168. ptr_len = 1;
  2169. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2170. goto fail;
  2171. arg_i32 = (uintptr_t)
  2172. wasm_runtime_addr_app_to_native(module, arg_i32);
  2173. }
  2174. else if (signature[i + 1] == '$') {
  2175. /* param is a string */
  2176. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2177. goto fail;
  2178. arg_i32 = (uintptr_t)
  2179. wasm_runtime_addr_app_to_native(module, arg_i32);
  2180. }
  2181. }
  2182. if (n_ints < MAX_REG_INTS)
  2183. ints[n_ints++] = arg_i32;
  2184. else
  2185. stacks[n_stacks++] = arg_i32;
  2186. break;
  2187. }
  2188. case VALUE_TYPE_I64:
  2189. if (n_ints < MAX_REG_INTS - 1) {
  2190. /* 64-bit data must be 8 bytes aligned in arm */
  2191. if (n_ints & 1)
  2192. n_ints++;
  2193. *(uint64*)&ints[n_ints] = *(uint64*)argv_src;
  2194. n_ints += 2;
  2195. }
  2196. else {
  2197. /* 64-bit data must be 8 bytes aligned in arm */
  2198. if (n_stacks & 1)
  2199. n_stacks++;
  2200. *(uint64*)&stacks[n_stacks] = *(uint64*)argv_src;
  2201. n_stacks += 2;
  2202. }
  2203. argv_src += 2;
  2204. break;
  2205. case VALUE_TYPE_F32:
  2206. if (n_fps < MAX_REG_FLOATS)
  2207. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2208. else
  2209. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2210. break;
  2211. case VALUE_TYPE_F64:
  2212. if (n_fps < MAX_REG_FLOATS - 1) {
  2213. /* 64-bit data must be 8 bytes aligned in arm */
  2214. if (n_fps & 1)
  2215. n_fps++;
  2216. *(float64*)&fps[n_fps] = *(float64*)argv_src;
  2217. n_fps += 2;
  2218. }
  2219. else {
  2220. /* 64-bit data must be 8 bytes aligned in arm */
  2221. if (n_stacks & 1)
  2222. n_stacks++;
  2223. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  2224. n_stacks += 2;
  2225. }
  2226. argv_src += 2;
  2227. break;
  2228. default:
  2229. bh_assert(0);
  2230. break;
  2231. }
  2232. }
  2233. /* Save extra result values' address to argv1 */
  2234. for (i = 0; i < ext_ret_count; i++) {
  2235. if (n_ints < MAX_REG_INTS)
  2236. ints[n_ints++] = *(uint32*)argv_src++;
  2237. else
  2238. stacks[n_stacks++] = *(uint32*)argv_src++;
  2239. }
  2240. exec_env->attachment = attachment;
  2241. if (func_type->result_count == 0) {
  2242. invokeNative_Void(func_ptr, argv1, n_stacks);
  2243. }
  2244. else {
  2245. switch (func_type->types[func_type->param_count]) {
  2246. case VALUE_TYPE_I32:
  2247. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  2248. break;
  2249. case VALUE_TYPE_I64:
  2250. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  2251. break;
  2252. case VALUE_TYPE_F32:
  2253. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  2254. break;
  2255. case VALUE_TYPE_F64:
  2256. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  2257. break;
  2258. default:
  2259. bh_assert(0);
  2260. break;
  2261. }
  2262. }
  2263. exec_env->attachment = NULL;
  2264. ret = true;
  2265. fail:
  2266. if (argv1 != argv_buf)
  2267. wasm_runtime_free(argv1);
  2268. return ret;
  2269. }
  2270. #endif /* end of defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) */
  2271. #if defined(BUILD_TARGET_X86_32) \
  2272. || defined(BUILD_TARGET_ARM) \
  2273. || defined(BUILD_TARGET_THUMB) \
  2274. || defined(BUILD_TARGET_MIPS) \
  2275. || defined(BUILD_TARGET_XTENSA)
  2276. typedef void (*GenericFunctionPointer)();
  2277. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  2278. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2279. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2280. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2281. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2282. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2283. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  2284. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  2285. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  2286. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  2287. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  2288. bool
  2289. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2290. const WASMType *func_type, const char *signature,
  2291. void *attachment,
  2292. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2293. {
  2294. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2295. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  2296. uint32 arg_i32, ptr_len;
  2297. uint32 result_count = func_type->result_count;
  2298. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2299. uint64 size;
  2300. bool ret = false;
  2301. #if defined(BUILD_TARGET_X86_32)
  2302. argc1 = argc + ext_ret_count + 2;
  2303. #else
  2304. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  2305. so we need to allocate more memory. */
  2306. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  2307. #endif
  2308. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2309. size = sizeof(uint32) * (uint64)argc1;
  2310. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2311. NULL, 0))) {
  2312. return false;
  2313. }
  2314. }
  2315. for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
  2316. argv1[j++] = ((uint32*)&exec_env)[i];
  2317. for (i = 0; i < func_type->param_count; i++) {
  2318. switch (func_type->types[i]) {
  2319. case VALUE_TYPE_I32:
  2320. {
  2321. arg_i32 = *argv++;
  2322. if (signature) {
  2323. if (signature[i + 1] == '*') {
  2324. /* param is a pointer */
  2325. if (signature[i + 2] == '~')
  2326. /* pointer with length followed */
  2327. ptr_len = *argv;
  2328. else
  2329. /* pointer without length followed */
  2330. ptr_len = 1;
  2331. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2332. goto fail;
  2333. arg_i32 = (uintptr_t)
  2334. wasm_runtime_addr_app_to_native(module, arg_i32);
  2335. }
  2336. else if (signature[i + 1] == '$') {
  2337. /* param is a string */
  2338. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2339. goto fail;
  2340. arg_i32 = (uintptr_t)
  2341. wasm_runtime_addr_app_to_native(module, arg_i32);
  2342. }
  2343. }
  2344. argv1[j++] = arg_i32;
  2345. break;
  2346. }
  2347. case VALUE_TYPE_I64:
  2348. case VALUE_TYPE_F64:
  2349. #if !defined(BUILD_TARGET_X86_32)
  2350. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  2351. and xtensa */
  2352. if (j & 1)
  2353. j++;
  2354. #endif
  2355. argv1[j++] = *argv++;
  2356. argv1[j++] = *argv++;
  2357. break;
  2358. case VALUE_TYPE_F32:
  2359. argv1[j++] = *argv++;
  2360. break;
  2361. default:
  2362. bh_assert(0);
  2363. break;
  2364. }
  2365. }
  2366. /* Save extra result values' address to argv1 */
  2367. word_copy(argv1 + j, argv, ext_ret_count);
  2368. argc1 = j + ext_ret_count;
  2369. exec_env->attachment = attachment;
  2370. if (func_type->result_count == 0) {
  2371. invokeNative_Void(func_ptr, argv1, argc1);
  2372. }
  2373. else {
  2374. switch (func_type->types[func_type->param_count]) {
  2375. case VALUE_TYPE_I32:
  2376. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  2377. break;
  2378. case VALUE_TYPE_I64:
  2379. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
  2380. break;
  2381. case VALUE_TYPE_F32:
  2382. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
  2383. break;
  2384. case VALUE_TYPE_F64:
  2385. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
  2386. break;
  2387. default:
  2388. bh_assert(0);
  2389. break;
  2390. }
  2391. }
  2392. exec_env->attachment = NULL;
  2393. ret = true;
  2394. fail:
  2395. if (argv1 != argv_buf)
  2396. wasm_runtime_free(argv1);
  2397. return ret;
  2398. }
  2399. #endif /* end of defined(BUILD_TARGET_X86_32) \
  2400. || defined(BUILD_TARGET_ARM) \
  2401. || defined(BUILD_TARGET_THUMB) \
  2402. || defined(BUILD_TARGET_MIPS) \
  2403. || defined(BUILD_TARGET_XTENSA) */
  2404. #if defined(BUILD_TARGET_X86_64) \
  2405. || defined(BUILD_TARGET_AMD_64) \
  2406. || defined(BUILD_TARGET_AARCH64)
  2407. typedef void (*GenericFunctionPointer)();
  2408. int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  2409. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2410. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2411. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*,uint64);
  2412. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2413. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
  2414. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  2415. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  2416. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2417. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2418. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2419. #if defined(_WIN32) || defined(_WIN32_)
  2420. #define MAX_REG_FLOATS 4
  2421. #define MAX_REG_INTS 4
  2422. #else
  2423. #define MAX_REG_FLOATS 8
  2424. #if defined(BUILD_TARGET_AARCH64)
  2425. #define MAX_REG_INTS 8
  2426. #else
  2427. #define MAX_REG_INTS 6
  2428. #endif /* end of defined(BUILD_TARGET_AARCH64 */
  2429. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  2430. bool
  2431. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2432. const WASMType *func_type, const char *signature,
  2433. void *attachment,
  2434. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2435. {
  2436. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2437. uint64 argv_buf[32], *argv1 = argv_buf, *fps, *ints, *stacks, size, arg_i64;
  2438. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2439. uint32 arg_i32, ptr_len;
  2440. uint32 result_count = func_type->result_count;
  2441. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2442. bool ret = false;
  2443. #if defined(_WIN32) || defined(_WIN32_)
  2444. /* important difference in calling conventions */
  2445. #define n_fps n_ints
  2446. #else
  2447. int n_fps = 0;
  2448. #endif
  2449. argc1 = 1 + MAX_REG_FLOATS + func_type->param_count + ext_ret_count;
  2450. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2451. size = sizeof(uint64) * (uint64)argc1;
  2452. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2453. NULL, 0))) {
  2454. return false;
  2455. }
  2456. }
  2457. fps = argv1;
  2458. ints = fps + MAX_REG_FLOATS;
  2459. stacks = ints + MAX_REG_INTS;
  2460. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  2461. for (i = 0; i < func_type->param_count; i++) {
  2462. switch (func_type->types[i]) {
  2463. case VALUE_TYPE_I32:
  2464. {
  2465. arg_i32 = *argv_src++;
  2466. arg_i64 = arg_i32;
  2467. if (signature) {
  2468. if (signature[i + 1] == '*') {
  2469. /* param is a pointer */
  2470. if (signature[i + 2] == '~')
  2471. /* pointer with length followed */
  2472. ptr_len = *argv_src;
  2473. else
  2474. /* pointer without length followed */
  2475. ptr_len = 1;
  2476. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2477. goto fail;
  2478. arg_i64 = (uintptr_t)
  2479. wasm_runtime_addr_app_to_native(module, arg_i32);
  2480. }
  2481. else if (signature[i + 1] == '$') {
  2482. /* param is a string */
  2483. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2484. goto fail;
  2485. arg_i64 = (uintptr_t)
  2486. wasm_runtime_addr_app_to_native(module, arg_i32);
  2487. }
  2488. }
  2489. if (n_ints < MAX_REG_INTS)
  2490. ints[n_ints++] = arg_i64;
  2491. else
  2492. stacks[n_stacks++] = arg_i64;
  2493. break;
  2494. }
  2495. case VALUE_TYPE_I64:
  2496. if (n_ints < MAX_REG_INTS)
  2497. ints[n_ints++] = *(uint64*)argv_src;
  2498. else
  2499. stacks[n_stacks++] = *(uint64*)argv_src;
  2500. argv_src += 2;
  2501. break;
  2502. case VALUE_TYPE_F32:
  2503. if (n_fps < MAX_REG_FLOATS)
  2504. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2505. else
  2506. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2507. break;
  2508. case VALUE_TYPE_F64:
  2509. if (n_fps < MAX_REG_FLOATS)
  2510. *(float64*)&fps[n_fps++] = *(float64*)argv_src;
  2511. else
  2512. *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
  2513. argv_src += 2;
  2514. break;
  2515. default:
  2516. bh_assert(0);
  2517. break;
  2518. }
  2519. }
  2520. /* Save extra result values' address to argv1 */
  2521. for (i = 0; i < ext_ret_count; i++) {
  2522. if (n_ints < MAX_REG_INTS)
  2523. ints[n_ints++] = *(uint64*)argv_src;
  2524. else
  2525. stacks[n_stacks++] = *(uint64*)argv_src;
  2526. argv_src += 2;
  2527. }
  2528. exec_env->attachment = attachment;
  2529. if (result_count == 0) {
  2530. invokeNative_Void(func_ptr, argv1, n_stacks);
  2531. }
  2532. else {
  2533. /* Invoke the native function and get the first result value */
  2534. switch (func_type->types[func_type->param_count]) {
  2535. case VALUE_TYPE_I32:
  2536. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  2537. break;
  2538. case VALUE_TYPE_I64:
  2539. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  2540. break;
  2541. case VALUE_TYPE_F32:
  2542. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  2543. break;
  2544. case VALUE_TYPE_F64:
  2545. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  2546. break;
  2547. default:
  2548. bh_assert(0);
  2549. break;
  2550. }
  2551. }
  2552. exec_env->attachment = NULL;
  2553. ret = true;
  2554. fail:
  2555. if (argv1 != argv_buf)
  2556. wasm_runtime_free(argv1);
  2557. return ret;
  2558. }
  2559. #endif /* end of defined(BUILD_TARGET_X86_64) \
  2560. || defined(BUILD_TARGET_AMD_64) \
  2561. || defined(BUILD_TARGET_AARCH64) */
  2562. bool
  2563. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  2564. uint32_t element_indices,
  2565. uint32_t argc, uint32_t argv[])
  2566. {
  2567. if (!wasm_runtime_exec_env_check(exec_env)) {
  2568. LOG_ERROR("Invalid exec env stack info.");
  2569. return false;
  2570. }
  2571. /* this function is called from native code, so exec_env->handle and
  2572. exec_env->native_stack_boundary must have been set, we don't set
  2573. it again */
  2574. #if WASM_ENABLE_INTERP != 0
  2575. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  2576. return wasm_call_indirect(exec_env,
  2577. element_indices,
  2578. argc, argv);
  2579. #endif
  2580. #if WASM_ENABLE_AOT != 0
  2581. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  2582. return aot_call_indirect(exec_env, false, 0,
  2583. element_indices, argc, argv);
  2584. #endif
  2585. return false;
  2586. }
  2587. #if WASM_ENABLE_THREAD_MGR != 0
  2588. typedef struct WASMThreadArg {
  2589. WASMExecEnv *new_exec_env;
  2590. wasm_thread_callback_t callback;
  2591. void *arg;
  2592. } WASMThreadArg;
  2593. WASMExecEnv *
  2594. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  2595. {
  2596. return wasm_cluster_spawn_exec_env(exec_env);
  2597. }
  2598. void
  2599. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  2600. {
  2601. wasm_cluster_destroy_spawned_exec_env(exec_env);
  2602. }
  2603. static void*
  2604. wasm_runtime_thread_routine(void *arg)
  2605. {
  2606. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  2607. void *ret;
  2608. bh_assert(thread_arg->new_exec_env);
  2609. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  2610. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  2611. wasm_runtime_free(thread_arg);
  2612. os_thread_exit(ret);
  2613. return ret;
  2614. }
  2615. int32
  2616. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  2617. wasm_thread_callback_t callback, void *arg)
  2618. {
  2619. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  2620. WASMThreadArg *thread_arg;
  2621. int32 ret;
  2622. if (!new_exec_env)
  2623. return -1;
  2624. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  2625. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  2626. return -1;
  2627. }
  2628. thread_arg->new_exec_env = new_exec_env;
  2629. thread_arg->callback = callback;
  2630. thread_arg->arg = arg;
  2631. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  2632. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  2633. if (ret != 0) {
  2634. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  2635. wasm_runtime_free(thread_arg);
  2636. }
  2637. return ret;
  2638. }
  2639. int32
  2640. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  2641. {
  2642. return os_thread_join((korp_tid)tid, retval);
  2643. }
  2644. #endif