wasm_runtime_common.c 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797
  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. static void
  48. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  49. {
  50. if (error_buf != NULL)
  51. snprintf(error_buf, error_buf_size, "%s", string);
  52. }
  53. static void *
  54. runtime_malloc(uint64 size, WASMModuleInstanceCommon *module_inst,
  55. char *error_buf, uint32 error_buf_size)
  56. {
  57. void *mem;
  58. if (size >= UINT32_MAX
  59. || !(mem = wasm_runtime_malloc((uint32)size))) {
  60. if (module_inst != NULL) {
  61. wasm_runtime_set_exception(module_inst,
  62. "allocate memory failed");
  63. }
  64. else if (error_buf != NULL) {
  65. set_error_buf(error_buf, error_buf_size,
  66. "allocate memory failed");
  67. }
  68. return NULL;
  69. }
  70. memset(mem, 0, (uint32)size);
  71. return mem;
  72. }
  73. static bool
  74. wasm_runtime_env_init()
  75. {
  76. if (bh_platform_init() != 0)
  77. return false;
  78. if (wasm_native_init() == false) {
  79. goto fail1;
  80. }
  81. #if WASM_ENABLE_MULTI_MODULE
  82. if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
  83. goto fail2;
  84. }
  85. if (BHT_OK != os_mutex_init(&loading_module_list_lock)) {
  86. goto fail3;
  87. }
  88. #endif
  89. #if WASM_ENABLE_SHARED_MEMORY
  90. if (!wasm_shared_memory_init()) {
  91. goto fail4;
  92. }
  93. #endif
  94. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  95. if (!thread_manager_init()) {
  96. goto fail5;
  97. }
  98. #endif
  99. #if WASM_ENABLE_AOT != 0
  100. #ifdef OS_ENABLE_HW_BOUND_CHECK
  101. if (!aot_signal_init()) {
  102. goto fail6;
  103. }
  104. #endif
  105. #endif
  106. return true;
  107. #if WASM_ENABLE_AOT != 0
  108. #ifdef OS_ENABLE_HW_BOUND_CHECK
  109. fail6:
  110. #endif
  111. #endif
  112. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  113. thread_manager_destroy();
  114. fail5:
  115. #endif
  116. #if WASM_ENABLE_SHARED_MEMORY
  117. wasm_shared_memory_destroy();
  118. fail4:
  119. #endif
  120. #if WASM_ENABLE_MULTI_MODULE
  121. os_mutex_destroy(&loading_module_list_lock);
  122. fail3:
  123. os_mutex_destroy(&registered_module_list_lock);
  124. fail2:
  125. #endif
  126. wasm_native_destroy();
  127. fail1:
  128. bh_platform_destroy();
  129. return false;
  130. }
  131. static bool
  132. wasm_runtime_exec_env_check(WASMExecEnv *exec_env)
  133. {
  134. return exec_env
  135. && exec_env->module_inst
  136. && exec_env->wasm_stack_size > 0
  137. && exec_env->wasm_stack.s.top_boundary ==
  138. exec_env->wasm_stack.s.bottom + exec_env->wasm_stack_size
  139. && exec_env->wasm_stack.s.top <= exec_env->wasm_stack.s.top_boundary;
  140. }
  141. bool
  142. wasm_runtime_init()
  143. {
  144. if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
  145. return false;
  146. if (!wasm_runtime_env_init()) {
  147. wasm_runtime_memory_destroy();
  148. return false;
  149. }
  150. return true;
  151. }
  152. void
  153. wasm_runtime_destroy()
  154. {
  155. #if WASM_ENABLE_AOT != 0
  156. #ifdef OS_ENABLE_HW_BOUND_CHECK
  157. aot_signal_destroy();
  158. #endif
  159. #endif
  160. /* runtime env destroy */
  161. #if WASM_ENABLE_MULTI_MODULE
  162. wasm_runtime_destroy_loading_module_list();
  163. os_mutex_destroy(&loading_module_list_lock);
  164. wasm_runtime_destroy_registered_module_list();
  165. os_mutex_destroy(&registered_module_list_lock);
  166. #endif
  167. #if WASM_ENABLE_SHARED_MEMORY
  168. wasm_shared_memory_destroy();
  169. #endif
  170. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_THREAD_MGR != 0)
  171. thread_manager_destroy();
  172. #endif
  173. wasm_native_destroy();
  174. bh_platform_destroy();
  175. wasm_runtime_memory_destroy();
  176. }
  177. bool
  178. wasm_runtime_full_init(RuntimeInitArgs *init_args)
  179. {
  180. if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
  181. &init_args->mem_alloc_option))
  182. return false;
  183. if (!wasm_runtime_env_init()) {
  184. wasm_runtime_memory_destroy();
  185. return false;
  186. }
  187. if (init_args->n_native_symbols > 0
  188. && !wasm_runtime_register_natives(init_args->native_module_name,
  189. init_args->native_symbols,
  190. init_args->n_native_symbols)) {
  191. wasm_runtime_destroy();
  192. return false;
  193. }
  194. #if WASM_ENABLE_THREAD_MGR != 0
  195. wasm_cluster_set_max_thread_num(init_args->max_thread_num);
  196. #endif
  197. return true;
  198. }
  199. PackageType
  200. get_package_type(const uint8 *buf, uint32 size)
  201. {
  202. if (buf && size >= 4) {
  203. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 's' && buf[3] == 'm')
  204. return Wasm_Module_Bytecode;
  205. if (buf[0] == '\0' && buf[1] == 'a' && buf[2] == 'o' && buf[3] == 't')
  206. return Wasm_Module_AoT;
  207. }
  208. return Package_Type_Unknown;
  209. }
  210. #if WASM_ENABLE_MULTI_MODULE != 0
  211. static module_reader reader;
  212. static module_destroyer destroyer;
  213. void
  214. wasm_runtime_set_module_reader(const module_reader reader_cb,
  215. const module_destroyer destroyer_cb)
  216. {
  217. reader = reader_cb;
  218. destroyer = destroyer_cb;
  219. }
  220. module_reader
  221. wasm_runtime_get_module_reader()
  222. {
  223. return reader;
  224. }
  225. module_destroyer
  226. wasm_runtime_get_module_destroyer()
  227. {
  228. return destroyer;
  229. }
  230. static WASMRegisteredModule *
  231. wasm_runtime_find_module_registered_by_reference(WASMModuleCommon *module)
  232. {
  233. WASMRegisteredModule *reg_module = NULL;
  234. os_mutex_lock(&registered_module_list_lock);
  235. reg_module = bh_list_first_elem(registered_module_list);
  236. while (reg_module && module != reg_module->module) {
  237. reg_module = bh_list_elem_next(reg_module);
  238. }
  239. os_mutex_unlock(&registered_module_list_lock);
  240. return reg_module;
  241. }
  242. bool
  243. wasm_runtime_register_module_internal(const char *module_name,
  244. WASMModuleCommon *module,
  245. uint8 *orig_file_buf,
  246. uint32 orig_file_buf_size,
  247. char *error_buf,
  248. uint32_t error_buf_size)
  249. {
  250. WASMRegisteredModule *node = NULL;
  251. node = wasm_runtime_find_module_registered_by_reference(module);
  252. if (node) { /* module has been registered */
  253. if (node->module_name) { /* module has name */
  254. if (!module_name || strcmp(node->module_name, module_name)) {
  255. /* module has different name */
  256. LOG_DEBUG("module(%p) has been registered with name %s",
  257. module, node->module_name);
  258. set_error_buf(error_buf, error_buf_size,
  259. "Register module failed: "
  260. "failed to rename the module");
  261. return false;
  262. }
  263. else {
  264. /* module has the same name */
  265. LOG_DEBUG("module(%p) has been registered with the same name %s",
  266. module, node->module_name);
  267. return true;
  268. }
  269. }
  270. else {
  271. /* module has empyt name, reset it */
  272. node->module_name = module_name;
  273. return true;
  274. }
  275. }
  276. /* module hasn't been registered */
  277. node = runtime_malloc(sizeof(WASMRegisteredModule), NULL, NULL, 0);
  278. if (!node) {
  279. LOG_DEBUG("malloc WASMRegisteredModule failed. SZ=%d",
  280. sizeof(WASMRegisteredModule));
  281. return false;
  282. }
  283. /* share the string and the module */
  284. node->module_name = module_name;
  285. node->module = module;
  286. node->orig_file_buf = orig_file_buf;
  287. node->orig_file_buf_size = orig_file_buf_size;
  288. os_mutex_lock(&registered_module_list_lock);
  289. bh_list_status ret = bh_list_insert(registered_module_list, node);
  290. bh_assert(BH_LIST_SUCCESS == ret);
  291. (void)ret;
  292. os_mutex_unlock(&registered_module_list_lock);
  293. return true;
  294. }
  295. bool
  296. wasm_runtime_register_module(const char *module_name, WASMModuleCommon *module,
  297. char *error_buf, uint32_t error_buf_size)
  298. {
  299. if (!error_buf || !error_buf_size) {
  300. LOG_ERROR("error buffer is required");
  301. return false;
  302. }
  303. if (!module_name || !module) {
  304. LOG_DEBUG("module_name and module are required");
  305. set_error_buf(error_buf, error_buf_size,
  306. "Register module failed: "
  307. "module_name and module are required");
  308. return false;
  309. }
  310. if (wasm_runtime_is_built_in_module(module_name)) {
  311. LOG_DEBUG("%s is a built-in module name", module_name);
  312. set_error_buf(error_buf, error_buf_size,
  313. "Register module failed: "
  314. "can not register as a built-in module");
  315. return false;
  316. }
  317. return wasm_runtime_register_module_internal(
  318. module_name, module, NULL, 0,
  319. error_buf, error_buf_size);
  320. }
  321. void
  322. wasm_runtime_unregister_module(const WASMModuleCommon *module)
  323. {
  324. WASMRegisteredModule *registered_module = NULL;
  325. os_mutex_lock(&registered_module_list_lock);
  326. registered_module = bh_list_first_elem(registered_module_list);
  327. while (registered_module && module != registered_module->module) {
  328. registered_module = bh_list_elem_next(registered_module);
  329. }
  330. /* it does not matter if it is not exist. after all, it is gone */
  331. if (registered_module) {
  332. bh_list_remove(registered_module_list, registered_module);
  333. wasm_runtime_free(registered_module);
  334. }
  335. os_mutex_unlock(&registered_module_list_lock);
  336. }
  337. WASMModuleCommon *
  338. wasm_runtime_find_module_registered(const char *module_name)
  339. {
  340. WASMRegisteredModule *module = NULL, *module_next;
  341. os_mutex_lock(&registered_module_list_lock);
  342. module = bh_list_first_elem(registered_module_list);
  343. while (module) {
  344. module_next = bh_list_elem_next(module);
  345. if (module->module_name
  346. && !strcmp(module_name, module->module_name)) {
  347. break;
  348. }
  349. module = module_next;
  350. }
  351. os_mutex_unlock(&registered_module_list_lock);
  352. return module ? module->module : NULL;
  353. }
  354. bool
  355. wasm_runtime_is_module_registered(const char *module_name)
  356. {
  357. return NULL != wasm_runtime_find_module_registered(module_name);
  358. }
  359. /*
  360. * simply destroy all
  361. */
  362. static void
  363. wasm_runtime_destroy_registered_module_list()
  364. {
  365. WASMRegisteredModule *reg_module = NULL;
  366. os_mutex_lock(&registered_module_list_lock);
  367. reg_module = bh_list_first_elem(registered_module_list);
  368. while (reg_module) {
  369. WASMRegisteredModule *next_reg_module = bh_list_elem_next(reg_module);
  370. bh_list_remove(registered_module_list, reg_module);
  371. /* now, it is time to release every module in the runtime */
  372. #if WASM_ENABLE_INTERP != 0
  373. if (reg_module->module->module_type == Wasm_Module_Bytecode)
  374. wasm_unload((WASMModule *)reg_module->module);
  375. #endif
  376. #if WASM_ENABLE_AOT != 0
  377. if (reg_module->module->module_type == Wasm_Module_AoT)
  378. aot_unload((AOTModule *)reg_module->module);
  379. #endif
  380. /* destroy the file buffer */
  381. if (destroyer && reg_module->orig_file_buf) {
  382. destroyer(reg_module->orig_file_buf,
  383. reg_module->orig_file_buf_size);
  384. reg_module->orig_file_buf = NULL;
  385. reg_module->orig_file_buf_size = 0;
  386. }
  387. wasm_runtime_free(reg_module);
  388. reg_module = next_reg_module;
  389. }
  390. os_mutex_unlock(&registered_module_list_lock);
  391. }
  392. bool
  393. wasm_runtime_add_loading_module(const char *module_name,
  394. char *error_buf, uint32 error_buf_size)
  395. {
  396. LOG_DEBUG("add %s into a loading list", module_name);
  397. LoadingModule *loadingModule =
  398. runtime_malloc(sizeof(LoadingModule), NULL,
  399. error_buf, error_buf_size);
  400. if (!loadingModule) {
  401. return false;
  402. }
  403. /* share the incoming string */
  404. loadingModule->module_name = module_name;
  405. os_mutex_lock(&loading_module_list_lock);
  406. bh_list_status ret = bh_list_insert(loading_module_list, loadingModule);
  407. bh_assert(BH_LIST_SUCCESS == ret);
  408. (void)ret;
  409. os_mutex_unlock(&loading_module_list_lock);
  410. return true;
  411. }
  412. void
  413. wasm_runtime_delete_loading_module(const char *module_name)
  414. {
  415. LOG_DEBUG("delete %s from a loading list", module_name);
  416. LoadingModule *module = NULL;
  417. os_mutex_lock(&loading_module_list_lock);
  418. module = bh_list_first_elem(loading_module_list);
  419. while (module && strcmp(module->module_name, module_name)) {
  420. module = bh_list_elem_next(module);
  421. }
  422. /* it does not matter if it is not exist. after all, it is gone */
  423. if (module) {
  424. bh_list_remove(loading_module_list, module);
  425. wasm_runtime_free(module);
  426. }
  427. os_mutex_unlock(&loading_module_list_lock);
  428. }
  429. bool
  430. wasm_runtime_is_loading_module(const char *module_name)
  431. {
  432. LOG_DEBUG("find %s in a loading list", module_name);
  433. LoadingModule *module = NULL;
  434. os_mutex_lock(&loading_module_list_lock);
  435. module = bh_list_first_elem(loading_module_list);
  436. while (module && strcmp(module_name, module->module_name)) {
  437. module = bh_list_elem_next(module);
  438. }
  439. os_mutex_unlock(&loading_module_list_lock);
  440. return module != NULL;
  441. }
  442. void
  443. wasm_runtime_destroy_loading_module_list()
  444. {
  445. LoadingModule *module = NULL;
  446. os_mutex_lock(&loading_module_list_lock);
  447. module = bh_list_first_elem(loading_module_list);
  448. while (module) {
  449. LoadingModule *next_module = bh_list_elem_next(module);
  450. bh_list_remove(loading_module_list, module);
  451. /*
  452. * will not free the module_name since it is
  453. * shared one of the const string pool
  454. */
  455. wasm_runtime_free(module);
  456. module = next_module;
  457. }
  458. os_mutex_unlock(&loading_module_list_lock);
  459. }
  460. #endif /* WASM_ENABLE_MULTI_MODULE */
  461. bool
  462. wasm_runtime_is_built_in_module(const char *module_name)
  463. {
  464. return (!strcmp("env", module_name)
  465. || !strcmp("wasi_unstable", module_name)
  466. || !strcmp("wasi_snapshot_preview1", module_name)
  467. #if WASM_ENABLE_SPEC_TEST != 0
  468. || !strcmp("spectest", module_name)
  469. #endif
  470. || !strcmp("", module_name));
  471. }
  472. #if WASM_ENABLE_THREAD_MGR != 0
  473. bool
  474. wasm_exec_env_set_aux_stack(WASMExecEnv *exec_env,
  475. uint32 start_offset, uint32 size)
  476. {
  477. WASMModuleInstanceCommon *module_inst
  478. = wasm_exec_env_get_module_inst(exec_env);
  479. #if WASM_ENABLE_INTERP != 0
  480. if (module_inst->module_type == Wasm_Module_Bytecode) {
  481. return wasm_set_aux_stack(exec_env, start_offset, size);
  482. }
  483. #endif
  484. #if WASM_ENABLE_AOT != 0
  485. if (module_inst->module_type == Wasm_Module_AoT) {
  486. return aot_set_aux_stack(exec_env, start_offset, size);
  487. }
  488. #endif
  489. return false;
  490. }
  491. bool
  492. wasm_exec_env_get_aux_stack(WASMExecEnv *exec_env,
  493. uint32 *start_offset, uint32 *size)
  494. {
  495. WASMModuleInstanceCommon *module_inst
  496. = wasm_exec_env_get_module_inst(exec_env);
  497. #if WASM_ENABLE_INTERP != 0
  498. if (module_inst->module_type == Wasm_Module_Bytecode) {
  499. return wasm_get_aux_stack(exec_env, start_offset, size);
  500. }
  501. #endif
  502. #if WASM_ENABLE_AOT != 0
  503. if (module_inst->module_type == Wasm_Module_AoT) {
  504. return aot_get_aux_stack(exec_env, start_offset, size);
  505. }
  506. #endif
  507. return false;
  508. }
  509. void
  510. wasm_runtime_set_max_thread_num(uint32 num)
  511. {
  512. wasm_cluster_set_max_thread_num(num);
  513. }
  514. #endif /* end of WASM_ENABLE_THREAD_MGR */
  515. static WASMModuleCommon *
  516. register_module_with_null_name(WASMModuleCommon *module_common,
  517. char *error_buf, uint32 error_buf_size)
  518. {
  519. #if WASM_ENABLE_MULTI_MODULE != 0
  520. if (module_common) {
  521. if (!wasm_runtime_register_module_internal(NULL, module_common,
  522. NULL, 0,
  523. error_buf,
  524. error_buf_size)) {
  525. wasm_runtime_unload(module_common);
  526. return NULL;
  527. }
  528. return module_common;
  529. }
  530. else
  531. return NULL;
  532. #else
  533. return module_common;
  534. #endif
  535. }
  536. WASMModuleCommon *
  537. wasm_runtime_load(const uint8 *buf, uint32 size,
  538. char *error_buf, uint32 error_buf_size)
  539. {
  540. WASMModuleCommon *module_common = NULL;
  541. if (get_package_type(buf, size) == Wasm_Module_Bytecode) {
  542. #if WASM_ENABLE_AOT != 0 && WASM_ENABLE_JIT != 0
  543. AOTModule *aot_module;
  544. WASMModule *module = wasm_load(buf, size, error_buf, error_buf_size);
  545. if (!module)
  546. return NULL;
  547. if (!(aot_module = aot_convert_wasm_module(module,
  548. error_buf, error_buf_size))) {
  549. wasm_unload(module);
  550. return NULL;
  551. }
  552. module_common = (WASMModuleCommon*)aot_module;
  553. return register_module_with_null_name(module_common,
  554. error_buf, error_buf_size);
  555. #elif WASM_ENABLE_INTERP != 0
  556. module_common = (WASMModuleCommon*)
  557. wasm_load(buf, size, error_buf, error_buf_size);
  558. return register_module_with_null_name(module_common,
  559. error_buf, error_buf_size);
  560. #endif
  561. }
  562. else if (get_package_type(buf, size) == Wasm_Module_AoT) {
  563. #if WASM_ENABLE_AOT != 0
  564. module_common = (WASMModuleCommon*)
  565. aot_load_from_aot_file(buf, size, error_buf, error_buf_size);
  566. return register_module_with_null_name(module_common,
  567. error_buf, error_buf_size);
  568. #endif
  569. }
  570. if (size < 4)
  571. set_error_buf(error_buf, error_buf_size,
  572. "WASM module load failed: unexpected end");
  573. else
  574. set_error_buf(error_buf, error_buf_size,
  575. "WASM module load failed: magic header not detected");
  576. return NULL;
  577. }
  578. WASMModuleCommon *
  579. wasm_runtime_load_from_sections(WASMSection *section_list, bool is_aot,
  580. char *error_buf, uint32_t error_buf_size)
  581. {
  582. WASMModuleCommon *module_common;
  583. #if WASM_ENABLE_INTERP != 0
  584. if (!is_aot) {
  585. module_common = (WASMModuleCommon*)
  586. wasm_load_from_sections(section_list,
  587. error_buf, error_buf_size);
  588. return register_module_with_null_name(module_common,
  589. error_buf, error_buf_size);
  590. }
  591. #endif
  592. #if WASM_ENABLE_AOT != 0
  593. if (is_aot) {
  594. module_common = (WASMModuleCommon*)
  595. aot_load_from_sections(section_list,
  596. error_buf, error_buf_size);
  597. return register_module_with_null_name(module_common,
  598. error_buf, error_buf_size);
  599. }
  600. #endif
  601. set_error_buf(error_buf, error_buf_size,
  602. "WASM module load failed: invalid section list type");
  603. return NULL;
  604. }
  605. void
  606. wasm_runtime_unload(WASMModuleCommon *module)
  607. {
  608. #if WASM_ENABLE_MULTI_MODULE != 0
  609. /**
  610. * since we will unload and free all module when runtime_destroy()
  611. * we don't want users to unwillingly disrupt it
  612. */
  613. return;
  614. #endif
  615. #if WASM_ENABLE_INTERP != 0
  616. if (module->module_type == Wasm_Module_Bytecode) {
  617. wasm_unload((WASMModule*)module);
  618. return;
  619. }
  620. #endif
  621. #if WASM_ENABLE_AOT != 0
  622. if (module->module_type == Wasm_Module_AoT) {
  623. aot_unload((AOTModule*)module);
  624. return;
  625. }
  626. #endif
  627. }
  628. WASMModuleInstanceCommon *
  629. wasm_runtime_instantiate_internal(WASMModuleCommon *module, bool is_sub_inst,
  630. uint32 stack_size, uint32 heap_size,
  631. char *error_buf, uint32 error_buf_size)
  632. {
  633. #if WASM_ENABLE_INTERP != 0
  634. if (module->module_type == Wasm_Module_Bytecode)
  635. return (WASMModuleInstanceCommon*)
  636. wasm_instantiate((WASMModule*)module, is_sub_inst,
  637. stack_size, heap_size,
  638. error_buf, error_buf_size);
  639. #endif
  640. #if WASM_ENABLE_AOT != 0
  641. if (module->module_type == Wasm_Module_AoT)
  642. return (WASMModuleInstanceCommon*)
  643. aot_instantiate((AOTModule*)module, is_sub_inst,
  644. stack_size, heap_size,
  645. error_buf, error_buf_size);
  646. #endif
  647. set_error_buf(error_buf, error_buf_size,
  648. "Instantiate module failed, invalid module type");
  649. return NULL;
  650. }
  651. WASMModuleInstanceCommon *
  652. wasm_runtime_instantiate(WASMModuleCommon *module,
  653. uint32 stack_size, uint32 heap_size,
  654. char *error_buf, uint32 error_buf_size)
  655. {
  656. return wasm_runtime_instantiate_internal(module, false,
  657. stack_size, heap_size,
  658. error_buf, error_buf_size);
  659. }
  660. void
  661. wasm_runtime_deinstantiate_internal(WASMModuleInstanceCommon *module_inst,
  662. bool is_sub_inst)
  663. {
  664. #if WASM_ENABLE_INTERP != 0
  665. if (module_inst->module_type == Wasm_Module_Bytecode) {
  666. wasm_deinstantiate((WASMModuleInstance*)module_inst, is_sub_inst);
  667. return;
  668. }
  669. #endif
  670. #if WASM_ENABLE_AOT != 0
  671. if (module_inst->module_type == Wasm_Module_AoT) {
  672. aot_deinstantiate((AOTModuleInstance*)module_inst, is_sub_inst);
  673. return;
  674. }
  675. #endif
  676. }
  677. void
  678. wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst)
  679. {
  680. return wasm_runtime_deinstantiate_internal(module_inst, false);
  681. }
  682. WASMExecEnv *
  683. wasm_runtime_create_exec_env(WASMModuleInstanceCommon *module_inst,
  684. uint32 stack_size)
  685. {
  686. return wasm_exec_env_create(module_inst, stack_size);
  687. }
  688. void
  689. wasm_runtime_destroy_exec_env(WASMExecEnv *exec_env)
  690. {
  691. wasm_exec_env_destroy(exec_env);
  692. }
  693. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  694. void
  695. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  696. {
  697. WASMModuleMemConsumption mem_conspn = { 0 };
  698. #if WASM_ENABLE_INTERP != 0
  699. if (module->module_type == Wasm_Module_Bytecode) {
  700. wasm_get_module_mem_consumption((WASMModule*)module, &mem_conspn);
  701. }
  702. #endif
  703. #if WASM_ENABLE_AOT != 0
  704. if (module->module_type == Wasm_Module_AoT) {
  705. aot_get_module_mem_consumption((AOTModule*)module, &mem_conspn);
  706. }
  707. #endif
  708. os_printf("WASM module memory consumption, total size: %u\n",
  709. mem_conspn.total_size);
  710. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  711. os_printf(" types size: %u\n", mem_conspn.types_size);
  712. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  713. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  714. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  715. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  716. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  717. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  718. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  719. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  720. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  721. #if WASM_ENABLE_AOT != 0
  722. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  723. #endif
  724. }
  725. void
  726. wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
  727. *module_inst)
  728. {
  729. WASMModuleInstMemConsumption mem_conspn = { 0 };
  730. #if WASM_ENABLE_INTERP != 0
  731. if (module_inst->module_type == Wasm_Module_Bytecode) {
  732. wasm_get_module_inst_mem_consumption((WASMModuleInstance*)module_inst,
  733. &mem_conspn);
  734. }
  735. #endif
  736. #if WASM_ENABLE_AOT != 0
  737. if (module_inst->module_type == Wasm_Module_AoT) {
  738. aot_get_module_inst_mem_consumption((AOTModuleInstance*)module_inst,
  739. &mem_conspn);
  740. }
  741. #endif
  742. os_printf("WASM module inst memory consumption, total size: %u\n",
  743. mem_conspn.total_size);
  744. os_printf(" module inst struct size: %u\n",
  745. mem_conspn.module_inst_struct_size);
  746. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  747. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  748. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  749. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  750. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  751. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  752. }
  753. void
  754. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  755. {
  756. uint32 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  757. + exec_env->wasm_stack_size;
  758. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  759. os_printf(" exec env struct size: %u\n",
  760. offsetof(WASMExecEnv, wasm_stack.s.bottom));
  761. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  762. os_printf(" block addr cache size: %u\n",
  763. sizeof(exec_env->block_addr_cache));
  764. #endif
  765. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  766. }
  767. uint32
  768. gc_get_heap_highmark_size(void *heap);
  769. void
  770. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  771. {
  772. WASMModuleInstMemConsumption module_inst_mem_consps;
  773. WASMModuleMemConsumption module_mem_consps;
  774. WASMModuleInstanceCommon *module_inst_common;
  775. WASMModuleCommon *module_common = NULL;
  776. void *heap_handle = NULL;
  777. uint32 total_size = 0, app_heap_peak_size = 0;
  778. uint32 max_aux_stack_used = -1;
  779. module_inst_common = exec_env->module_inst;
  780. #if WASM_ENABLE_INTERP != 0
  781. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  782. WASMModuleInstance *wasm_module_inst =
  783. (WASMModuleInstance*)module_inst_common;
  784. WASMModule *wasm_module = wasm_module_inst->module;
  785. module_common = (WASMModuleCommon*)wasm_module;
  786. if (wasm_module_inst->memories) {
  787. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  788. }
  789. wasm_get_module_inst_mem_consumption
  790. (wasm_module_inst, &module_inst_mem_consps);
  791. wasm_get_module_mem_consumption
  792. (wasm_module, &module_mem_consps);
  793. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  794. max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
  795. }
  796. #endif
  797. #if WASM_ENABLE_AOT != 0
  798. if (module_inst_common->module_type == Wasm_Module_AoT) {
  799. AOTModuleInstance *aot_module_inst =
  800. (AOTModuleInstance*)module_inst_common;
  801. AOTModule *aot_module =
  802. (AOTModule*)aot_module_inst->aot_module.ptr;
  803. module_common = (WASMModuleCommon*)aot_module;
  804. if (aot_module_inst->memories.ptr) {
  805. AOTMemoryInstance **memories =
  806. (AOTMemoryInstance **)aot_module_inst->memories.ptr;
  807. heap_handle = memories[0]->heap_handle.ptr;
  808. }
  809. aot_get_module_inst_mem_consumption
  810. (aot_module_inst, &module_inst_mem_consps);
  811. aot_get_module_mem_consumption
  812. (aot_module, &module_mem_consps);
  813. }
  814. #endif
  815. bh_assert(module_common != NULL);
  816. if (heap_handle) {
  817. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  818. }
  819. total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  820. + exec_env->wasm_stack_size
  821. + module_mem_consps.total_size
  822. + module_inst_mem_consps.total_size;
  823. os_printf("\nMemory consumption summary (bytes):\n");
  824. wasm_runtime_dump_module_mem_consumption(module_common);
  825. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  826. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  827. os_printf("\nTotal memory consumption of module, module inst and "
  828. "exec env: %u\n", total_size);
  829. os_printf("Total interpreter stack used: %u\n",
  830. exec_env->max_wasm_stack_used);
  831. if (max_aux_stack_used != (uint32)-1)
  832. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  833. else
  834. os_printf("Total aux stack used: no enough info to profile\n");
  835. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  836. }
  837. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  838. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  839. #if WASM_ENABLE_PERF_PROFILING != 0
  840. void
  841. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  842. {
  843. #if WASM_ENABLE_INTERP != 0
  844. if (module_inst->module_type == Wasm_Module_Bytecode) {
  845. wasm_dump_perf_profiling((WASMModuleInstance*)module_inst);
  846. }
  847. #endif
  848. #if WASM_ENABLE_AOT != 0
  849. if (module_inst->module_type == Wasm_Module_AoT) {
  850. aot_dump_perf_profiling((AOTModuleInstance*)module_inst);
  851. }
  852. #endif
  853. }
  854. #endif
  855. WASMModuleInstanceCommon *
  856. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  857. {
  858. return wasm_exec_env_get_module_inst(exec_env);
  859. }
  860. void *
  861. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  862. {
  863. return exec_env->attachment;
  864. }
  865. void
  866. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  867. {
  868. exec_env->user_data = user_data;
  869. }
  870. void *
  871. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  872. {
  873. return exec_env->user_data;
  874. }
  875. WASMFunctionInstanceCommon *
  876. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  877. const char *name,
  878. const char *signature)
  879. {
  880. #if WASM_ENABLE_INTERP != 0
  881. if (module_inst->module_type == Wasm_Module_Bytecode)
  882. return (WASMFunctionInstanceCommon*)
  883. wasm_lookup_function((const WASMModuleInstance*)module_inst,
  884. name, signature);
  885. #endif
  886. #if WASM_ENABLE_AOT != 0
  887. if (module_inst->module_type == Wasm_Module_AoT)
  888. return (WASMFunctionInstanceCommon*)
  889. aot_lookup_function((const AOTModuleInstance*)module_inst,
  890. name, signature);
  891. #endif
  892. return NULL;
  893. }
  894. bool
  895. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  896. WASMFunctionInstanceCommon *function,
  897. uint32 argc, uint32 argv[])
  898. {
  899. if (!wasm_runtime_exec_env_check(exec_env)) {
  900. LOG_ERROR("Invalid exec env stack info.");
  901. return false;
  902. }
  903. /* set thread handle and stack boundary */
  904. wasm_exec_env_set_thread_info(exec_env);
  905. #if WASM_ENABLE_INTERP != 0
  906. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  907. return wasm_call_function(exec_env,
  908. (WASMFunctionInstance*)function,
  909. argc, argv);
  910. #endif
  911. #if WASM_ENABLE_AOT != 0
  912. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  913. return aot_call_function(exec_env,
  914. (AOTFunctionInstance*)function,
  915. argc, argv);
  916. #endif
  917. return false;
  918. }
  919. static uint32
  920. parse_args_to_uint32_array(WASMType *type,
  921. uint32 num_args, wasm_val_t *args,
  922. uint32 *out_argv)
  923. {
  924. uint32 i, p;
  925. for (i = 0, p = 0; i < num_args; i++) {
  926. switch (args[i].kind) {
  927. case WASM_I32:
  928. out_argv[p++] = args[i].of.i32;
  929. break;
  930. case WASM_I64:
  931. {
  932. union { uint64 val; uint32 parts[2]; } u;
  933. u.val = args[i].of.i64;
  934. out_argv[p++] = u.parts[0];
  935. out_argv[p++] = u.parts[1];
  936. break;
  937. }
  938. case WASM_F32:
  939. {
  940. union { float32 val; uint32 part; } u;
  941. u.val = args[i].of.f32;
  942. out_argv[p++] = u.part;
  943. break;
  944. }
  945. case WASM_F64:
  946. {
  947. union { float64 val; uint32 parts[2]; } u;
  948. u.val = args[i].of.f64;
  949. out_argv[p++] = u.parts[0];
  950. out_argv[p++] = u.parts[1];
  951. break;
  952. }
  953. default:
  954. bh_assert(0);
  955. break;
  956. }
  957. }
  958. return p;
  959. }
  960. static uint32
  961. parse_uint32_array_to_results(WASMType *type,
  962. uint32 argc, uint32 *argv,
  963. wasm_val_t *out_results)
  964. {
  965. uint32 i, p;
  966. for (i = 0, p = 0; i < type->result_count; i++) {
  967. switch (type->types[type->param_count + i]) {
  968. case VALUE_TYPE_I32:
  969. out_results[i].kind = WASM_I32;
  970. out_results[i].of.i32 = (int32)argv[p++];
  971. break;
  972. case VALUE_TYPE_I64:
  973. {
  974. union { uint64 val; uint32 parts[2]; } u;
  975. u.parts[0] = argv[p++];
  976. u.parts[1] = argv[p++];
  977. out_results[i].kind = WASM_I64;
  978. out_results[i].of.i64 = u.val;
  979. break;
  980. }
  981. case VALUE_TYPE_F32:
  982. {
  983. union { float32 val; uint32 part; } u;
  984. u.part = argv[p++];
  985. out_results[i].kind = WASM_F32;
  986. out_results[i].of.f32 = u.val;
  987. break;
  988. }
  989. case VALUE_TYPE_F64:
  990. {
  991. union { float64 val; uint32 parts[2]; } u;
  992. u.parts[0] = argv[p++];
  993. u.parts[1] = argv[p++];
  994. out_results[i].kind = WASM_F64;
  995. out_results[i].of.f64 = u.val;
  996. break;
  997. }
  998. default:
  999. bh_assert(0);
  1000. break;
  1001. }
  1002. }
  1003. bh_assert(argc == p);
  1004. return type->result_count;
  1005. }
  1006. bool
  1007. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  1008. WASMFunctionInstanceCommon *function,
  1009. uint32 num_results, wasm_val_t results[],
  1010. uint32 num_args, wasm_val_t args[])
  1011. {
  1012. uint32 argc, *argv, ret_num, cell_num, total_size;
  1013. bool ret = false;
  1014. WASMType *type = NULL;
  1015. #if WASM_ENABLE_INTERP != 0
  1016. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  1017. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)function;
  1018. type = wasm_func->u.func->func_type;
  1019. argc = wasm_func->param_cell_num;
  1020. cell_num = argc > wasm_func->ret_cell_num ?
  1021. argc : wasm_func->ret_cell_num;
  1022. }
  1023. #endif
  1024. #if WASM_ENABLE_AOT != 0
  1025. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  1026. type = ((AOTFunctionInstance*)function)->u.func.func_type;
  1027. argc = type->param_cell_num;
  1028. cell_num = argc > type->ret_cell_num ?
  1029. argc : type->ret_cell_num;
  1030. }
  1031. #endif
  1032. if (!type) {
  1033. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be enabled at least one.");
  1034. goto fail1;
  1035. }
  1036. if (num_results != type->result_count) {
  1037. LOG_ERROR("The result value number does not match the function declaration.");
  1038. goto fail1;
  1039. }
  1040. if (num_args != type->param_count) {
  1041. LOG_ERROR("The argument value number does not match the function declaration.");
  1042. goto fail1;
  1043. }
  1044. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1045. if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL, 0))) {
  1046. wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
  1047. goto fail1;
  1048. }
  1049. argc = parse_args_to_uint32_array(type, num_args, args, argv);
  1050. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  1051. goto fail2;
  1052. ret_num = parse_uint32_array_to_results(type, type->ret_cell_num, argv, results);
  1053. bh_assert(ret_num == num_results);
  1054. (void)ret_num;
  1055. fail2:
  1056. wasm_runtime_free(argv);
  1057. fail1:
  1058. return ret;
  1059. }
  1060. bool
  1061. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  1062. WASMFunctionInstanceCommon *function,
  1063. uint32 num_results, wasm_val_t results[],
  1064. uint32 num_args, ...)
  1065. {
  1066. wasm_val_t *args = NULL;
  1067. WASMType *type = NULL;
  1068. bool ret = false;
  1069. uint32 i = 0;
  1070. va_list vargs;
  1071. #if WASM_ENABLE_INTERP != 0
  1072. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode) {
  1073. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)function;
  1074. type = wasm_func->u.func->func_type;
  1075. }
  1076. #endif
  1077. #if WASM_ENABLE_AOT != 0
  1078. if (exec_env->module_inst->module_type == Wasm_Module_AoT) {
  1079. type = ((AOTFunctionInstance*)function)->u.func.func_type;
  1080. }
  1081. #endif
  1082. if (!type) {
  1083. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be enabled at least one.");
  1084. goto fail1;
  1085. }
  1086. if (num_args != type->param_count) {
  1087. LOG_ERROR("The argument value number does not match the function declaration.");
  1088. goto fail1;
  1089. }
  1090. if (!(args = runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
  1091. wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
  1092. goto fail1;
  1093. }
  1094. va_start(vargs, num_args);
  1095. for (i = 0; i < num_args; i++) {
  1096. switch (type->types[i]) {
  1097. case VALUE_TYPE_I32:
  1098. args[i].kind = WASM_I32;
  1099. args[i].of.i32 = va_arg(vargs, uint32);
  1100. break;
  1101. case VALUE_TYPE_I64:
  1102. args[i].kind = WASM_I64;
  1103. args[i].of.i64 = va_arg(vargs, uint64);
  1104. break;
  1105. case VALUE_TYPE_F32:
  1106. args[i].kind = WASM_F32;
  1107. args[i].of.f32 = (float32)va_arg(vargs, float64);
  1108. break;
  1109. case VALUE_TYPE_F64:
  1110. args[i].kind = WASM_F64;
  1111. args[i].of.f64 = va_arg(vargs, float64);;
  1112. break;
  1113. default:
  1114. bh_assert(0);
  1115. break;
  1116. }
  1117. }
  1118. va_end(vargs);
  1119. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results, num_args, args);
  1120. wasm_runtime_free(args);
  1121. fail1:
  1122. return ret;
  1123. }
  1124. bool
  1125. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  1126. WASMFunctionInstanceCommon *function,
  1127. uint32 argc, uint32 argv[])
  1128. {
  1129. #if WASM_ENABLE_INTERP != 0
  1130. if (module_inst->module_type == Wasm_Module_Bytecode)
  1131. return wasm_create_exec_env_and_call_function(
  1132. (WASMModuleInstance*)module_inst,
  1133. (WASMFunctionInstance*)function,
  1134. argc, argv);
  1135. #endif
  1136. #if WASM_ENABLE_AOT != 0
  1137. if (module_inst->module_type == Wasm_Module_AoT)
  1138. return aot_create_exec_env_and_call_function(
  1139. (AOTModuleInstance*)module_inst,
  1140. (AOTFunctionInstance*)function,
  1141. argc, argv);
  1142. #endif
  1143. return false;
  1144. }
  1145. void
  1146. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  1147. const char *exception)
  1148. {
  1149. #if WASM_ENABLE_INTERP != 0
  1150. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1151. wasm_set_exception((WASMModuleInstance*)module_inst, exception);
  1152. return;
  1153. }
  1154. #endif
  1155. #if WASM_ENABLE_AOT != 0
  1156. if (module_inst->module_type == Wasm_Module_AoT) {
  1157. aot_set_exception((AOTModuleInstance*)module_inst, exception);
  1158. return;
  1159. }
  1160. #endif
  1161. }
  1162. const char*
  1163. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  1164. {
  1165. #if WASM_ENABLE_INTERP != 0
  1166. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1167. return wasm_get_exception((WASMModuleInstance*)module_inst);
  1168. }
  1169. #endif
  1170. #if WASM_ENABLE_AOT != 0
  1171. if (module_inst->module_type == Wasm_Module_AoT) {
  1172. return aot_get_exception((AOTModuleInstance*)module_inst);
  1173. }
  1174. #endif
  1175. return NULL;
  1176. }
  1177. void
  1178. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  1179. {
  1180. wasm_runtime_set_exception(module_inst, NULL);
  1181. }
  1182. void
  1183. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  1184. void *custom_data)
  1185. {
  1186. #if WASM_ENABLE_INTERP != 0
  1187. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1188. ((WASMModuleInstance*)module_inst)->custom_data = custom_data;
  1189. return;
  1190. }
  1191. #endif
  1192. #if WASM_ENABLE_AOT != 0
  1193. if (module_inst->module_type == Wasm_Module_AoT) {
  1194. ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data;
  1195. return;
  1196. }
  1197. #endif
  1198. }
  1199. void*
  1200. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  1201. {
  1202. #if WASM_ENABLE_INTERP != 0
  1203. if (module_inst->module_type == Wasm_Module_Bytecode)
  1204. return ((WASMModuleInstance*)module_inst)->custom_data;
  1205. #endif
  1206. #if WASM_ENABLE_AOT != 0
  1207. if (module_inst->module_type == Wasm_Module_AoT)
  1208. return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
  1209. #endif
  1210. return NULL;
  1211. }
  1212. uint32
  1213. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  1214. void **p_native_addr)
  1215. {
  1216. #if WASM_ENABLE_INTERP != 0
  1217. if (module_inst->module_type == Wasm_Module_Bytecode)
  1218. return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
  1219. p_native_addr);
  1220. #endif
  1221. #if WASM_ENABLE_AOT != 0
  1222. if (module_inst->module_type == Wasm_Module_AoT)
  1223. return aot_module_malloc((AOTModuleInstance*)module_inst, size,
  1224. p_native_addr);
  1225. #endif
  1226. return 0;
  1227. }
  1228. void
  1229. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  1230. {
  1231. #if WASM_ENABLE_INTERP != 0
  1232. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1233. wasm_module_free((WASMModuleInstance*)module_inst, ptr);
  1234. return;
  1235. }
  1236. #endif
  1237. #if WASM_ENABLE_AOT != 0
  1238. if (module_inst->module_type == Wasm_Module_AoT) {
  1239. aot_module_free((AOTModuleInstance*)module_inst, ptr);
  1240. return;
  1241. }
  1242. #endif
  1243. }
  1244. uint32
  1245. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  1246. const char *src, uint32 size)
  1247. {
  1248. #if WASM_ENABLE_INTERP != 0
  1249. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1250. return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size);
  1251. }
  1252. #endif
  1253. #if WASM_ENABLE_AOT != 0
  1254. if (module_inst->module_type == Wasm_Module_AoT) {
  1255. return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size);
  1256. }
  1257. #endif
  1258. return 0;
  1259. }
  1260. bool
  1261. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  1262. uint32 app_offset, uint32 size)
  1263. {
  1264. #if WASM_ENABLE_INTERP != 0
  1265. if (module_inst->module_type == Wasm_Module_Bytecode)
  1266. return wasm_validate_app_addr((WASMModuleInstance*)module_inst,
  1267. app_offset, size);
  1268. #endif
  1269. #if WASM_ENABLE_AOT != 0
  1270. if (module_inst->module_type == Wasm_Module_AoT)
  1271. return aot_validate_app_addr((AOTModuleInstance*)module_inst,
  1272. app_offset, size);
  1273. #endif
  1274. return false;
  1275. }
  1276. bool
  1277. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  1278. uint32 app_str_offset)
  1279. {
  1280. uint32 app_end_offset;
  1281. char *str, *str_end;
  1282. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
  1283. NULL, &app_end_offset))
  1284. goto fail;
  1285. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1286. str_end = str + (app_end_offset - app_str_offset);
  1287. while (str < str_end && *str != '\0')
  1288. str++;
  1289. if (str == str_end)
  1290. goto fail;
  1291. return true;
  1292. fail:
  1293. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1294. return false;
  1295. }
  1296. bool
  1297. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  1298. void *native_ptr, uint32 size)
  1299. {
  1300. #if WASM_ENABLE_INTERP != 0
  1301. if (module_inst->module_type == Wasm_Module_Bytecode)
  1302. return wasm_validate_native_addr((WASMModuleInstance*)module_inst,
  1303. native_ptr, size);
  1304. #endif
  1305. #if WASM_ENABLE_AOT != 0
  1306. if (module_inst->module_type == Wasm_Module_AoT)
  1307. return aot_validate_native_addr((AOTModuleInstance*)module_inst,
  1308. native_ptr, size);
  1309. #endif
  1310. return false;
  1311. }
  1312. void *
  1313. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  1314. uint32 app_offset)
  1315. {
  1316. #if WASM_ENABLE_INTERP != 0
  1317. if (module_inst->module_type == Wasm_Module_Bytecode)
  1318. return wasm_addr_app_to_native((WASMModuleInstance*)module_inst,
  1319. app_offset);
  1320. #endif
  1321. #if WASM_ENABLE_AOT != 0
  1322. if (module_inst->module_type == Wasm_Module_AoT)
  1323. return aot_addr_app_to_native((AOTModuleInstance*)module_inst,
  1324. app_offset);
  1325. #endif
  1326. return NULL;
  1327. }
  1328. uint32
  1329. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  1330. void *native_ptr)
  1331. {
  1332. #if WASM_ENABLE_INTERP != 0
  1333. if (module_inst->module_type == Wasm_Module_Bytecode)
  1334. return wasm_addr_native_to_app((WASMModuleInstance*)module_inst,
  1335. native_ptr);
  1336. #endif
  1337. #if WASM_ENABLE_AOT != 0
  1338. if (module_inst->module_type == Wasm_Module_AoT)
  1339. return aot_addr_native_to_app((AOTModuleInstance*)module_inst,
  1340. native_ptr);
  1341. #endif
  1342. return 0;
  1343. }
  1344. bool
  1345. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  1346. uint32 app_offset,
  1347. uint32 *p_app_start_offset,
  1348. uint32 *p_app_end_offset)
  1349. {
  1350. #if WASM_ENABLE_INTERP != 0
  1351. if (module_inst->module_type == Wasm_Module_Bytecode)
  1352. return wasm_get_app_addr_range((WASMModuleInstance*)module_inst,
  1353. app_offset, p_app_start_offset,
  1354. p_app_end_offset);
  1355. #endif
  1356. #if WASM_ENABLE_AOT != 0
  1357. if (module_inst->module_type == Wasm_Module_AoT)
  1358. return aot_get_app_addr_range((AOTModuleInstance*)module_inst,
  1359. app_offset, p_app_start_offset,
  1360. p_app_end_offset);
  1361. #endif
  1362. return false;
  1363. }
  1364. bool
  1365. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  1366. uint8 *native_ptr,
  1367. uint8 **p_native_start_addr,
  1368. uint8 **p_native_end_addr)
  1369. {
  1370. #if WASM_ENABLE_INTERP != 0
  1371. if (module_inst->module_type == Wasm_Module_Bytecode)
  1372. return wasm_get_native_addr_range((WASMModuleInstance*)module_inst,
  1373. native_ptr, p_native_start_addr,
  1374. p_native_end_addr);
  1375. #endif
  1376. #if WASM_ENABLE_AOT != 0
  1377. if (module_inst->module_type == Wasm_Module_AoT)
  1378. return aot_get_native_addr_range((AOTModuleInstance*)module_inst,
  1379. native_ptr, p_native_start_addr,
  1380. p_native_end_addr);
  1381. #endif
  1382. return false;
  1383. }
  1384. uint32
  1385. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1386. {
  1387. #if WASM_ENABLE_INTERP != 0
  1388. if (module_inst->module_type == Wasm_Module_Bytecode)
  1389. return ((WASMModuleInstance*)module_inst)->temp_ret;
  1390. #endif
  1391. #if WASM_ENABLE_AOT != 0
  1392. if (module_inst->module_type == Wasm_Module_AoT)
  1393. return ((AOTModuleInstance*)module_inst)->temp_ret;
  1394. #endif
  1395. return 0;
  1396. }
  1397. void
  1398. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1399. uint32 temp_ret)
  1400. {
  1401. #if WASM_ENABLE_INTERP != 0
  1402. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1403. ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret;
  1404. return;
  1405. }
  1406. #endif
  1407. #if WASM_ENABLE_AOT != 0
  1408. if (module_inst->module_type == Wasm_Module_AoT) {
  1409. ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret;
  1410. return;
  1411. }
  1412. #endif
  1413. }
  1414. uint32
  1415. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1416. {
  1417. #if WASM_ENABLE_INTERP != 0
  1418. if (module_inst->module_type == Wasm_Module_Bytecode)
  1419. return ((WASMModuleInstance*)module_inst)->llvm_stack;
  1420. #endif
  1421. #if WASM_ENABLE_AOT != 0
  1422. if (module_inst->module_type == Wasm_Module_AoT)
  1423. return ((AOTModuleInstance*)module_inst)->llvm_stack;
  1424. #endif
  1425. return 0;
  1426. }
  1427. void
  1428. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1429. uint32 llvm_stack)
  1430. {
  1431. #if WASM_ENABLE_INTERP != 0
  1432. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1433. ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1434. return;
  1435. }
  1436. #endif
  1437. #if WASM_ENABLE_AOT != 0
  1438. if (module_inst->module_type == Wasm_Module_AoT) {
  1439. ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1440. return;
  1441. }
  1442. #endif
  1443. }
  1444. bool
  1445. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  1446. uint32 inc_page_count)
  1447. {
  1448. #if WASM_ENABLE_INTERP != 0
  1449. if (module->module_type == Wasm_Module_Bytecode)
  1450. return wasm_enlarge_memory((WASMModuleInstance*)module,
  1451. inc_page_count);
  1452. #endif
  1453. #if WASM_ENABLE_AOT != 0
  1454. if (module->module_type == Wasm_Module_AoT)
  1455. return aot_enlarge_memory((AOTModuleInstance*)module,
  1456. inc_page_count);
  1457. #endif
  1458. return false;
  1459. }
  1460. #if WASM_ENABLE_LIBC_WASI != 0
  1461. void
  1462. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  1463. const char *dir_list[], uint32 dir_count,
  1464. const char *map_dir_list[], uint32 map_dir_count,
  1465. const char *env_list[], uint32 env_count,
  1466. char *argv[], int argc)
  1467. {
  1468. WASIArguments *wasi_args = NULL;
  1469. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1470. if (module->module_type == Wasm_Module_Bytecode)
  1471. wasi_args = &((WASMModule*)module)->wasi_args;
  1472. #endif
  1473. #if WASM_ENABLE_AOT != 0
  1474. if (module->module_type == Wasm_Module_AoT)
  1475. wasi_args = &((AOTModule*)module)->wasi_args;
  1476. #endif
  1477. if (wasi_args) {
  1478. wasi_args->dir_list = dir_list;
  1479. wasi_args->dir_count = dir_count;
  1480. wasi_args->map_dir_list = map_dir_list;
  1481. wasi_args->map_dir_count = map_dir_count;
  1482. wasi_args->env = env_list;
  1483. wasi_args->env_count = env_count;
  1484. wasi_args->argv = argv;
  1485. wasi_args->argc = (uint32)argc;
  1486. }
  1487. }
  1488. #if WASM_ENABLE_UVWASI == 0
  1489. bool
  1490. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1491. const char *dir_list[], uint32 dir_count,
  1492. const char *map_dir_list[], uint32 map_dir_count,
  1493. const char *env[], uint32 env_count,
  1494. char *argv[], uint32 argc,
  1495. char *error_buf, uint32 error_buf_size)
  1496. {
  1497. WASIContext *wasi_ctx;
  1498. char *argv_buf = NULL;
  1499. char **argv_list = NULL;
  1500. char *env_buf = NULL;
  1501. char **env_list = NULL;
  1502. uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
  1503. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  1504. struct fd_table *curfds = NULL;
  1505. struct fd_prestats *prestats = NULL;
  1506. struct argv_environ_values *argv_environ = NULL;
  1507. bool fd_table_inited = false, fd_prestats_inited = false;
  1508. bool argv_environ_inited = false;
  1509. __wasi_fd_t wasm_fd = 3;
  1510. int32 raw_fd;
  1511. char *path, resolved_path[PATH_MAX];
  1512. uint32 i;
  1513. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL,
  1514. error_buf, error_buf_size))) {
  1515. return false;
  1516. }
  1517. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  1518. #if WASM_ENABLE_INTERP != 0
  1519. if (module_inst->module_type == Wasm_Module_Bytecode
  1520. && !((WASMModuleInstance*)module_inst)->default_memory)
  1521. return true;
  1522. #endif
  1523. #if WASM_ENABLE_AOT != 0
  1524. if (module_inst->module_type == Wasm_Module_AoT
  1525. && !((AOTModuleInstance*)module_inst)->
  1526. global_table_data.memory_instances[0].memory_data.ptr)
  1527. return true;
  1528. #endif
  1529. /* process argv[0], trip the path and suffix, only keep the program name */
  1530. for (i = 0; i < argc; i++)
  1531. argv_buf_size += strlen(argv[i]) + 1;
  1532. total_size = sizeof(char *) * (uint64)argc;
  1533. if (total_size >= UINT32_MAX
  1534. || (total_size > 0 &&
  1535. !(argv_list = wasm_runtime_malloc((uint32)total_size)))
  1536. || argv_buf_size >= UINT32_MAX
  1537. || (argv_buf_size > 0 &&
  1538. !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
  1539. set_error_buf(error_buf, error_buf_size,
  1540. "Init wasi environment failed: allocate memory failed");
  1541. goto fail;
  1542. }
  1543. for (i = 0; i < argc; i++) {
  1544. argv_list[i] = argv_buf + argv_buf_offset;
  1545. bh_strcpy_s(argv_buf + argv_buf_offset,
  1546. (uint32)argv_buf_size - argv_buf_offset, argv[i]);
  1547. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  1548. }
  1549. for (i = 0; i < env_count; i++)
  1550. env_buf_size += strlen(env[i]) + 1;
  1551. total_size = sizeof(char *) * (uint64)env_count;
  1552. if (total_size >= UINT32_MAX
  1553. || (total_size > 0
  1554. && !(env_list = wasm_runtime_malloc((uint32)total_size)))
  1555. || env_buf_size >= UINT32_MAX
  1556. || (env_buf_size > 0
  1557. && !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
  1558. set_error_buf(error_buf, error_buf_size,
  1559. "Init wasi environment failed: allocate memory failed");
  1560. goto fail;
  1561. }
  1562. for (i = 0; i < env_count; i++) {
  1563. env_list[i] = env_buf + env_buf_offset;
  1564. bh_strcpy_s(env_buf + env_buf_offset,
  1565. (uint32)env_buf_size - env_buf_offset, env[i]);
  1566. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  1567. }
  1568. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  1569. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  1570. || !(argv_environ =
  1571. wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
  1572. set_error_buf(error_buf, error_buf_size,
  1573. "Init wasi environment failed: allocate memory failed");
  1574. goto fail;
  1575. }
  1576. if (!fd_table_init(curfds)) {
  1577. set_error_buf(error_buf, error_buf_size,
  1578. "Init wasi environment failed: "
  1579. "init fd table failed");
  1580. goto fail;
  1581. }
  1582. fd_table_inited = true;
  1583. if (!fd_prestats_init(prestats)) {
  1584. set_error_buf(error_buf, error_buf_size,
  1585. "Init wasi environment failed: "
  1586. "init fd prestats failed");
  1587. goto fail;
  1588. }
  1589. fd_prestats_inited = true;
  1590. if (!argv_environ_init(argv_environ,
  1591. argv_buf, argv_buf_size,
  1592. argv_list, argc,
  1593. env_buf, env_buf_size,
  1594. env_list, env_count)) {
  1595. set_error_buf(error_buf, error_buf_size,
  1596. "Init wasi environment failed: "
  1597. "init argument environment failed");
  1598. goto fail;
  1599. }
  1600. argv_environ_inited = true;
  1601. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  1602. if (!fd_table_insert_existing(curfds, 0, 0)
  1603. || !fd_table_insert_existing(curfds, 1, 1)
  1604. || !fd_table_insert_existing(curfds, 2, 2)) {
  1605. set_error_buf(error_buf, error_buf_size,
  1606. "Init wasi environment failed: init fd table failed");
  1607. goto fail;
  1608. }
  1609. wasm_fd = 3;
  1610. for (i = 0; i < dir_count; i++, wasm_fd++) {
  1611. path = realpath(dir_list[i], resolved_path);
  1612. if (!path) {
  1613. if (error_buf)
  1614. snprintf(error_buf, error_buf_size,
  1615. "error while pre-opening directory %s: %d\n",
  1616. dir_list[i], errno);
  1617. goto fail;
  1618. }
  1619. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  1620. if (raw_fd == -1) {
  1621. if (error_buf)
  1622. snprintf(error_buf, error_buf_size,
  1623. "error while pre-opening directory %s: %d\n",
  1624. dir_list[i], errno);
  1625. goto fail;
  1626. }
  1627. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  1628. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  1629. }
  1630. wasi_ctx->curfds = curfds;
  1631. wasi_ctx->prestats = prestats;
  1632. wasi_ctx->argv_environ = argv_environ;
  1633. wasi_ctx->argv_buf = argv_buf;
  1634. wasi_ctx->argv_list = argv_list;
  1635. wasi_ctx->env_buf = env_buf;
  1636. wasi_ctx->env_list = env_list;
  1637. return true;
  1638. fail:
  1639. if (argv_environ_inited)
  1640. argv_environ_destroy(argv_environ);
  1641. if (fd_prestats_inited)
  1642. fd_prestats_destroy(prestats);
  1643. if (fd_table_inited)
  1644. fd_table_destroy(curfds);
  1645. if (curfds)
  1646. wasm_runtime_free(curfds);
  1647. if (prestats)
  1648. wasm_runtime_free(prestats);
  1649. if (argv_environ)
  1650. wasm_runtime_free(argv_environ);
  1651. if (argv_buf)
  1652. wasm_runtime_free(argv_buf);
  1653. if (argv_list)
  1654. wasm_runtime_free(argv_list);
  1655. if (env_buf)
  1656. wasm_runtime_free(env_buf);
  1657. if (env_list)
  1658. wasm_runtime_free(env_list);
  1659. return false;
  1660. }
  1661. #else /* else of WASM_ENABLE_UVWASI == 0 */
  1662. static void *
  1663. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  1664. {
  1665. return runtime_malloc(size, NULL, NULL, 0);
  1666. (void)mem_user_data;
  1667. }
  1668. static void
  1669. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  1670. {
  1671. if (ptr)
  1672. wasm_runtime_free(ptr);
  1673. (void)mem_user_data;
  1674. }
  1675. static void *
  1676. wasm_uvwasi_calloc(size_t nmemb, size_t size,
  1677. void *mem_user_data)
  1678. {
  1679. uint64 total_size = (uint64)nmemb * size;
  1680. return runtime_malloc(total_size, NULL, NULL, 0);
  1681. (void)mem_user_data;
  1682. }
  1683. static void *
  1684. wasm_uvwasi_realloc(void *ptr, size_t size,
  1685. void *mem_user_data)
  1686. {
  1687. if (size >= UINT32_MAX) {
  1688. return NULL;
  1689. }
  1690. return wasm_runtime_realloc(ptr, (uint32)size);
  1691. }
  1692. static uvwasi_mem_t uvwasi_allocator = {
  1693. .mem_user_data = 0,
  1694. .malloc = wasm_uvwasi_malloc,
  1695. .free = wasm_uvwasi_free,
  1696. .calloc = wasm_uvwasi_calloc,
  1697. .realloc = wasm_uvwasi_realloc
  1698. };
  1699. bool
  1700. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1701. const char *dir_list[], uint32 dir_count,
  1702. const char *map_dir_list[], uint32 map_dir_count,
  1703. const char *env[], uint32 env_count,
  1704. char *argv[], uint32 argc,
  1705. char *error_buf, uint32 error_buf_size)
  1706. {
  1707. uvwasi_t *uvwasi = NULL;
  1708. uvwasi_options_t init_options;
  1709. const char **envp = NULL;
  1710. uint64 total_size;
  1711. uint32 i;
  1712. bool ret = false;
  1713. uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst,
  1714. error_buf, error_buf_size);
  1715. if (!uvwasi)
  1716. return false;
  1717. /* Setup the initialization options */
  1718. uvwasi_options_init(&init_options);
  1719. init_options.allocator = &uvwasi_allocator;
  1720. init_options.argc = argc;
  1721. init_options.argv = (const char **)argv;
  1722. if (dir_count > 0) {
  1723. init_options.preopenc = dir_count;
  1724. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  1725. init_options.preopens =
  1726. (uvwasi_preopen_t *)runtime_malloc(total_size, module_inst,
  1727. error_buf, error_buf_size);
  1728. if (init_options.preopens == NULL)
  1729. goto fail;
  1730. for (i = 0; i < init_options.preopenc; i++) {
  1731. init_options.preopens[i].real_path = dir_list[i];
  1732. init_options.preopens[i].mapped_path =
  1733. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  1734. }
  1735. }
  1736. if (env_count > 0) {
  1737. total_size = sizeof(char *) * (uint64)(env_count + 1);
  1738. envp = runtime_malloc(total_size, module_inst,
  1739. error_buf, error_buf_size);
  1740. if (envp == NULL)
  1741. goto fail;
  1742. for (i = 0; i < env_count; i++) {
  1743. envp[i] = env[i];
  1744. }
  1745. envp[env_count] = NULL;
  1746. init_options.envp = envp;
  1747. }
  1748. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  1749. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  1750. goto fail;
  1751. }
  1752. wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
  1753. ret = true;
  1754. fail:
  1755. if (envp)
  1756. wasm_runtime_free(envp);
  1757. if (init_options.preopens)
  1758. wasm_runtime_free(init_options.preopens);
  1759. if (!ret && uvwasi)
  1760. wasm_runtime_free(uvwasi);
  1761. return ret;
  1762. }
  1763. #endif /* end of WASM_ENABLE_UVWASI */
  1764. bool
  1765. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  1766. {
  1767. #if WASM_ENABLE_INTERP != 0
  1768. if (module_inst->module_type == Wasm_Module_Bytecode
  1769. && ((WASMModuleInstance*)module_inst)->module->is_wasi_module)
  1770. return true;
  1771. #endif
  1772. #if WASM_ENABLE_AOT != 0
  1773. if (module_inst->module_type == Wasm_Module_AoT
  1774. && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr)
  1775. ->is_wasi_module)
  1776. return true;
  1777. #endif
  1778. return false;
  1779. }
  1780. WASMFunctionInstanceCommon *
  1781. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  1782. {
  1783. uint32 i;
  1784. #if WASM_ENABLE_INTERP != 0
  1785. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1786. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1787. WASMFunctionInstance *func;
  1788. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1789. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  1790. func = wasm_inst->export_functions[i].function;
  1791. if (func->u.func->func_type->param_count != 0
  1792. || func->u.func->func_type->result_count != 0) {
  1793. LOG_ERROR("Lookup wasi _start function failed: "
  1794. "invalid function type.\n");
  1795. return NULL;
  1796. }
  1797. return (WASMFunctionInstanceCommon*)func;
  1798. }
  1799. }
  1800. return NULL;
  1801. }
  1802. #endif
  1803. #if WASM_ENABLE_AOT != 0
  1804. if (module_inst->module_type == Wasm_Module_AoT) {
  1805. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1806. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1807. aot_inst->export_funcs.ptr;
  1808. for (i = 0; i < aot_inst->export_func_count; i++) {
  1809. if (!strcmp(export_funcs[i].func_name, "_start")) {
  1810. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  1811. if (func_type->param_count != 0
  1812. || func_type->result_count != 0) {
  1813. LOG_ERROR("Lookup wasi _start function failed: "
  1814. "invalid function type.\n");
  1815. return NULL;
  1816. }
  1817. return (WASMFunctionInstanceCommon*)&export_funcs[i];
  1818. }
  1819. }
  1820. return NULL;
  1821. }
  1822. #endif /* end of WASM_ENABLE_AOT */
  1823. return NULL;
  1824. }
  1825. #if WASM_ENABLE_UVWASI == 0
  1826. void
  1827. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  1828. {
  1829. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  1830. if (wasi_ctx) {
  1831. if (wasi_ctx->argv_environ) {
  1832. argv_environ_destroy(wasi_ctx->argv_environ);
  1833. wasm_runtime_free(wasi_ctx->argv_environ);
  1834. }
  1835. if (wasi_ctx->curfds) {
  1836. fd_table_destroy(wasi_ctx->curfds);
  1837. wasm_runtime_free(wasi_ctx->curfds);
  1838. }
  1839. if (wasi_ctx->prestats) {
  1840. fd_prestats_destroy(wasi_ctx->prestats);
  1841. wasm_runtime_free(wasi_ctx->prestats);
  1842. }
  1843. if (wasi_ctx->argv_buf)
  1844. wasm_runtime_free(wasi_ctx->argv_buf);
  1845. if (wasi_ctx->argv_list)
  1846. wasm_runtime_free(wasi_ctx->argv_list);
  1847. if (wasi_ctx->env_buf)
  1848. wasm_runtime_free(wasi_ctx->env_buf);
  1849. if (wasi_ctx->env_list)
  1850. wasm_runtime_free(wasi_ctx->env_list);
  1851. wasm_runtime_free(wasi_ctx);
  1852. }
  1853. }
  1854. #else
  1855. void
  1856. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  1857. {
  1858. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  1859. if (wasi_ctx) {
  1860. uvwasi_destroy(wasi_ctx);
  1861. wasm_runtime_free(wasi_ctx);
  1862. }
  1863. }
  1864. #endif
  1865. WASIContext *
  1866. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  1867. {
  1868. #if WASM_ENABLE_INTERP != 0
  1869. if (module_inst->module_type == Wasm_Module_Bytecode)
  1870. return ((WASMModuleInstance*)module_inst)->wasi_ctx;
  1871. #endif
  1872. #if WASM_ENABLE_AOT != 0
  1873. if (module_inst->module_type == Wasm_Module_AoT)
  1874. return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr;
  1875. #endif
  1876. return NULL;
  1877. }
  1878. void
  1879. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  1880. WASIContext *wasi_ctx)
  1881. {
  1882. #if WASM_ENABLE_INTERP != 0
  1883. if (module_inst->module_type == Wasm_Module_Bytecode)
  1884. ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx;
  1885. #endif
  1886. #if WASM_ENABLE_AOT != 0
  1887. if (module_inst->module_type == Wasm_Module_AoT)
  1888. ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx;
  1889. #endif
  1890. }
  1891. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1892. WASMModuleCommon*
  1893. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  1894. {
  1895. WASMModuleInstanceCommon *module_inst =
  1896. wasm_runtime_get_module_inst(exec_env);
  1897. #if WASM_ENABLE_INTERP != 0
  1898. if (module_inst->module_type == Wasm_Module_Bytecode)
  1899. return (WASMModuleCommon*)
  1900. ((WASMModuleInstance*)module_inst)->module;
  1901. #endif
  1902. #if WASM_ENABLE_AOT != 0
  1903. if (module_inst->module_type == Wasm_Module_AoT)
  1904. return (WASMModuleCommon*)
  1905. ((AOTModuleInstance*)module_inst)->aot_module.ptr;
  1906. #endif
  1907. return NULL;
  1908. }
  1909. /**
  1910. * Implementation of wasm_application_execute_main()
  1911. */
  1912. static WASMFunctionInstanceCommon*
  1913. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1914. const char *name);
  1915. static bool
  1916. check_main_func_type(const WASMType *type)
  1917. {
  1918. if (!(type->param_count == 0 || type->param_count == 2)
  1919. ||type->result_count > 1) {
  1920. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1921. return false;
  1922. }
  1923. if (type->param_count == 2
  1924. && !(type->types[0] == VALUE_TYPE_I32
  1925. && type->types[1] == VALUE_TYPE_I32)) {
  1926. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1927. return false;
  1928. }
  1929. if (type->result_count
  1930. && type->types[type->param_count] != VALUE_TYPE_I32) {
  1931. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  1932. return false;
  1933. }
  1934. return true;
  1935. }
  1936. bool
  1937. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  1938. int32 argc, char *argv[])
  1939. {
  1940. WASMFunctionInstanceCommon *func;
  1941. WASMType *func_type = NULL;
  1942. uint32 argc1 = 0, argv1[2] = { 0 };
  1943. uint32 total_argv_size = 0;
  1944. uint64 total_size;
  1945. uint32 argv_buf_offset = 0;
  1946. int32 i;
  1947. char *argv_buf, *p, *p_end;
  1948. uint32 *argv_offsets;
  1949. bool ret;
  1950. #if WASM_ENABLE_LIBC_WASI != 0
  1951. if (wasm_runtime_is_wasi_mode(module_inst)) {
  1952. /* In wasi mode, we should call function named "_start"
  1953. which initializes the wasi envrionment and then calls
  1954. the actual main function. Directly call main function
  1955. may cause exception thrown. */
  1956. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
  1957. return wasm_runtime_create_exec_env_and_call_wasm(
  1958. module_inst, func, 0, NULL);
  1959. /* if no start function is found, we execute
  1960. the main function as normal */
  1961. }
  1962. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1963. if (!(func = resolve_function(module_inst, "main"))
  1964. && !(func = resolve_function(module_inst, "__main_argc_argv"))
  1965. && !(func = resolve_function(module_inst, "_main"))) {
  1966. wasm_runtime_set_exception(module_inst,
  1967. "lookup main function failed");
  1968. return false;
  1969. }
  1970. #if WASM_ENABLE_INTERP != 0
  1971. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1972. if (((WASMFunctionInstance*)func)->is_import_func) {
  1973. wasm_runtime_set_exception(module_inst,
  1974. "lookup main function failed");
  1975. return false;
  1976. }
  1977. func_type = ((WASMFunctionInstance*)func)->u.func->func_type;
  1978. }
  1979. #endif
  1980. #if WASM_ENABLE_AOT != 0
  1981. if (module_inst->module_type == Wasm_Module_AoT)
  1982. func_type = ((AOTFunctionInstance*)func)->u.func.func_type;
  1983. #endif
  1984. if (!func_type) {
  1985. LOG_ERROR("invalid module instance type");
  1986. return false;
  1987. }
  1988. if (!check_main_func_type(func_type)) {
  1989. wasm_runtime_set_exception(module_inst,
  1990. "invalid function type of main function");
  1991. return false;
  1992. }
  1993. if (func_type->param_count) {
  1994. for (i = 0; i < argc; i++)
  1995. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  1996. total_argv_size = align_uint(total_argv_size, 4);
  1997. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  1998. if (total_size >= UINT32_MAX
  1999. || !(argv_buf_offset =
  2000. wasm_runtime_module_malloc(module_inst, (uint32)total_size,
  2001. (void**)&argv_buf))) {
  2002. wasm_runtime_set_exception(module_inst,
  2003. "allocate memory failed");
  2004. return false;
  2005. }
  2006. p = argv_buf;
  2007. argv_offsets = (uint32*)(p + total_argv_size);
  2008. p_end = p + total_size;
  2009. for (i = 0; i < argc; i++) {
  2010. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  2011. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  2012. p += strlen(argv[i]) + 1;
  2013. }
  2014. argc1 = 2;
  2015. argv1[0] = (uint32)argc;
  2016. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  2017. }
  2018. ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  2019. argc1, argv1);
  2020. if (argv_buf_offset)
  2021. wasm_runtime_module_free(module_inst, argv_buf_offset);
  2022. return ret;
  2023. }
  2024. #if WASM_ENABLE_MULTI_MODULE != 0
  2025. static WASMModuleInstance *
  2026. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  2027. const char *sub_module_name)
  2028. {
  2029. WASMSubModInstNode *node =
  2030. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  2031. while (node && strcmp(node->module_name, sub_module_name)) {
  2032. node = bh_list_elem_next(node);
  2033. }
  2034. return node ? node->module_inst : NULL;
  2035. }
  2036. static bool
  2037. parse_function_name(char *orig_function_name, char **p_module_name,
  2038. char **p_function_name)
  2039. {
  2040. if (orig_function_name[0] != '$') {
  2041. *p_module_name = NULL;
  2042. *p_function_name = orig_function_name;
  2043. return true;
  2044. }
  2045. /**
  2046. * $module_name$function_name\0
  2047. * ===>
  2048. * module_name\0function_name\0
  2049. * ===>
  2050. * module_name
  2051. * function_name
  2052. */
  2053. char *p1 = orig_function_name;
  2054. char *p2 = strchr(p1 + 1, '$');
  2055. if (!p2) {
  2056. LOG_DEBUG("can not parse the incoming function name");
  2057. return false;
  2058. }
  2059. *p_module_name = p1 + 1;
  2060. *p2 = '\0';
  2061. *p_function_name = p2 + 1;
  2062. return strlen(*p_module_name) && strlen(*p_function_name);
  2063. }
  2064. #endif
  2065. /**
  2066. * Implementation of wasm_application_execute_func()
  2067. */
  2068. static WASMFunctionInstanceCommon*
  2069. resolve_function(const WASMModuleInstanceCommon *module_inst,
  2070. const char *name)
  2071. {
  2072. uint32 i = 0;
  2073. WASMFunctionInstanceCommon *ret = NULL;
  2074. #if WASM_ENABLE_MULTI_MODULE != 0
  2075. WASMModuleInstance *sub_module_inst = NULL;
  2076. char *orig_name = NULL;
  2077. char *sub_module_name = NULL;
  2078. char *function_name = NULL;
  2079. uint32 length = strlen(name) + 1;
  2080. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  2081. if (!orig_name) {
  2082. return NULL;
  2083. }
  2084. strncpy(orig_name, name, length);
  2085. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  2086. goto LEAVE;
  2087. }
  2088. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  2089. if (sub_module_name) {
  2090. sub_module_inst = get_sub_module_inst(
  2091. (WASMModuleInstance *)module_inst, sub_module_name);
  2092. if (!sub_module_inst) {
  2093. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  2094. goto LEAVE;
  2095. }
  2096. }
  2097. #else
  2098. const char *function_name = name;
  2099. #endif
  2100. #if WASM_ENABLE_INTERP != 0
  2101. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2102. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  2103. #if WASM_ENABLE_MULTI_MODULE != 0
  2104. wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
  2105. #endif /* WASM_ENABLE_MULTI_MODULE */
  2106. for (i = 0; i < wasm_inst->export_func_count; i++) {
  2107. if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
  2108. ret = wasm_inst->export_functions[i].function;
  2109. break;
  2110. }
  2111. }
  2112. }
  2113. #endif /* WASM_ENABLE_INTERP */
  2114. #if WASM_ENABLE_AOT != 0
  2115. if (module_inst->module_type == Wasm_Module_AoT) {
  2116. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  2117. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  2118. aot_inst->export_funcs.ptr;
  2119. for (i = 0; i < aot_inst->export_func_count; i++) {
  2120. if (!strcmp(export_funcs[i].func_name, function_name)) {
  2121. ret = &export_funcs[i];
  2122. break;
  2123. }
  2124. }
  2125. }
  2126. #endif
  2127. #if WASM_ENABLE_MULTI_MODULE != 0
  2128. LEAVE:
  2129. wasm_runtime_free(orig_name);
  2130. #endif
  2131. return ret;
  2132. }
  2133. union ieee754_float {
  2134. float f;
  2135. /* This is the IEEE 754 single-precision format. */
  2136. union {
  2137. struct {
  2138. unsigned int negative:1;
  2139. unsigned int exponent:8;
  2140. unsigned int mantissa:23;
  2141. } ieee_big_endian;
  2142. struct {
  2143. unsigned int mantissa:23;
  2144. unsigned int exponent:8;
  2145. unsigned int negative:1;
  2146. } ieee_little_endian;
  2147. } ieee;
  2148. };
  2149. union ieee754_double {
  2150. double d;
  2151. /* This is the IEEE 754 double-precision format. */
  2152. union {
  2153. struct {
  2154. unsigned int negative:1;
  2155. unsigned int exponent:11;
  2156. /* Together these comprise the mantissa. */
  2157. unsigned int mantissa0:20;
  2158. unsigned int mantissa1:32;
  2159. } ieee_big_endian;
  2160. struct {
  2161. /* Together these comprise the mantissa. */
  2162. unsigned int mantissa1:32;
  2163. unsigned int mantissa0:20;
  2164. unsigned int exponent:11;
  2165. unsigned int negative:1;
  2166. } ieee_little_endian;
  2167. } ieee;
  2168. };
  2169. static union {
  2170. int a;
  2171. char b;
  2172. } __ue = { .a = 1 };
  2173. #define is_little_endian() (__ue.b == 1)
  2174. bool
  2175. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  2176. const char *name, int32 argc, char *argv[])
  2177. {
  2178. WASMFunctionInstanceCommon *func;
  2179. WASMType *type = NULL;
  2180. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  2181. int32 i, p;
  2182. uint64 total_size;
  2183. const char *exception;
  2184. char buf[128];
  2185. bh_assert(argc >= 0);
  2186. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  2187. func = resolve_function(module_inst, name);
  2188. if (!func) {
  2189. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  2190. wasm_runtime_set_exception(module_inst, buf);
  2191. goto fail;
  2192. }
  2193. #if WASM_ENABLE_INTERP != 0
  2194. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2195. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func;
  2196. if (wasm_func->is_import_func
  2197. #if WASM_ENABLE_MULTI_MODULE != 0
  2198. && !wasm_func->import_func_inst
  2199. #endif
  2200. ) {
  2201. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  2202. wasm_runtime_set_exception(module_inst, buf);
  2203. goto fail;
  2204. }
  2205. type = wasm_func->is_import_func ? wasm_func->u.func_import->func_type
  2206. : wasm_func->u.func->func_type;
  2207. argc1 = wasm_func->param_cell_num;
  2208. cell_num = argc1 > wasm_func->ret_cell_num ?
  2209. argc1 : wasm_func->ret_cell_num;
  2210. }
  2211. #endif
  2212. #if WASM_ENABLE_AOT != 0
  2213. if (module_inst->module_type == Wasm_Module_AoT) {
  2214. type = ((AOTFunctionInstance*)func)->u.func.func_type;
  2215. argc1 = type->param_cell_num;
  2216. cell_num = argc1 > type->ret_cell_num ?
  2217. argc1 : type->ret_cell_num;
  2218. }
  2219. #endif
  2220. if (!type) {
  2221. LOG_ERROR("invalid module instance type");
  2222. return false;
  2223. }
  2224. if (type->param_count != (uint32)argc) {
  2225. wasm_runtime_set_exception(module_inst,
  2226. "invalid input argument count");
  2227. goto fail;
  2228. }
  2229. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2230. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst,
  2231. NULL, 0)))) {
  2232. goto fail;
  2233. }
  2234. /* Parse arguments */
  2235. for (i = 0, p = 0; i < argc; i++) {
  2236. char *endptr = NULL;
  2237. bh_assert(argv[i] != NULL);
  2238. if (argv[i][0] == '\0') {
  2239. snprintf(buf, sizeof(buf), "invalid input argument %d", i);
  2240. wasm_runtime_set_exception(module_inst, buf);
  2241. goto fail;
  2242. }
  2243. switch (type->types[i]) {
  2244. case VALUE_TYPE_I32:
  2245. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  2246. break;
  2247. case VALUE_TYPE_I64:
  2248. {
  2249. union { uint64 val; uint32 parts[2]; } u;
  2250. u.val = strtoull(argv[i], &endptr, 0);
  2251. argv1[p++] = u.parts[0];
  2252. argv1[p++] = u.parts[1];
  2253. break;
  2254. }
  2255. case VALUE_TYPE_F32:
  2256. {
  2257. float32 f32 = strtof(argv[i], &endptr);
  2258. if (isnan(f32)) {
  2259. if (argv[i][0] == '-') {
  2260. union ieee754_float u;
  2261. u.f = f32;
  2262. if (is_little_endian())
  2263. u.ieee.ieee_little_endian.negative = 1;
  2264. else
  2265. u.ieee.ieee_big_endian.negative = 1;
  2266. memcpy(&f32, &u.f, sizeof(float));
  2267. }
  2268. if (endptr[0] == ':') {
  2269. uint32 sig;
  2270. union ieee754_float u;
  2271. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  2272. u.f = f32;
  2273. if (is_little_endian())
  2274. u.ieee.ieee_little_endian.mantissa = sig;
  2275. else
  2276. u.ieee.ieee_big_endian.mantissa = sig;
  2277. memcpy(&f32, &u.f, sizeof(float));
  2278. }
  2279. }
  2280. memcpy(&argv1[p++], &f32, sizeof(float));
  2281. break;
  2282. }
  2283. case VALUE_TYPE_F64:
  2284. {
  2285. union { float64 val; uint32 parts[2]; } u;
  2286. u.val = strtod(argv[i], &endptr);
  2287. if (isnan(u.val)) {
  2288. if (argv[i][0] == '-') {
  2289. union ieee754_double ud;
  2290. ud.d = u.val;
  2291. if (is_little_endian())
  2292. ud.ieee.ieee_little_endian.negative = 1;
  2293. else
  2294. ud.ieee.ieee_big_endian.negative = 1;
  2295. memcpy(&u.val, &ud.d, sizeof(double));
  2296. }
  2297. if (endptr[0] == ':') {
  2298. uint64 sig;
  2299. union ieee754_double ud;
  2300. sig = strtoull(endptr + 1, &endptr, 0);
  2301. ud.d = u.val;
  2302. if (is_little_endian()) {
  2303. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  2304. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  2305. }
  2306. else {
  2307. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  2308. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  2309. }
  2310. memcpy(&u.val, &ud.d, sizeof(double));
  2311. }
  2312. }
  2313. argv1[p++] = u.parts[0];
  2314. argv1[p++] = u.parts[1];
  2315. break;
  2316. }
  2317. #if WASM_ENABLE_SIMD != 0
  2318. case VALUE_TYPE_V128:
  2319. {
  2320. /* it likes 0x123\0x234 or 123\234 */
  2321. /* retrive first i64 */
  2322. *(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  2323. /* skip \ */
  2324. endptr++;
  2325. /* retrive second i64 */
  2326. *(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  2327. p += 4;
  2328. break;
  2329. }
  2330. #endif /* WASM_ENABLE_SIMD != 0 */
  2331. default:
  2332. bh_assert(0);
  2333. break;
  2334. }
  2335. if (endptr && *endptr != '\0' && *endptr != '_') {
  2336. snprintf(buf, sizeof(buf), "invalid input argument %d: %s",
  2337. i, argv[i]);
  2338. wasm_runtime_set_exception(module_inst, buf);
  2339. goto fail;
  2340. }
  2341. }
  2342. bh_assert(p == (int32)argc1);
  2343. wasm_runtime_set_exception(module_inst, NULL);
  2344. if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  2345. argc1, argv1)) {
  2346. goto fail;
  2347. }
  2348. /* print return value */
  2349. for (j = 0; j < type->result_count; j++) {
  2350. switch (type->types[type->param_count + j]) {
  2351. case VALUE_TYPE_I32:
  2352. {
  2353. os_printf("0x%x:i32", argv1[k]);
  2354. k++;
  2355. break;
  2356. }
  2357. case VALUE_TYPE_I64:
  2358. {
  2359. union { uint64 val; uint32 parts[2]; } u;
  2360. u.parts[0] = argv1[k];
  2361. u.parts[1] = argv1[k + 1];
  2362. k += 2;
  2363. #ifdef PRIx64
  2364. os_printf("0x%"PRIx64":i64", u.val);
  2365. #else
  2366. char buf[16];
  2367. if (sizeof(long) == 4)
  2368. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  2369. else
  2370. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  2371. os_printf(buf, u.val);
  2372. #endif
  2373. break;
  2374. }
  2375. case VALUE_TYPE_F32:
  2376. os_printf("%.7g:f32", *(float32*)(argv1 + k));
  2377. k++;
  2378. break;
  2379. case VALUE_TYPE_F64:
  2380. {
  2381. union { float64 val; uint32 parts[2]; } u;
  2382. u.parts[0] = argv1[k];
  2383. u.parts[1] = argv1[k + 1];
  2384. k += 2;
  2385. os_printf("%.7g:f64", u.val);
  2386. break;
  2387. }
  2388. #if WASM_ENABLE_SIMD != 0
  2389. case VALUE_TYPE_V128:
  2390. {
  2391. uint64 *v = (uint64*)(argv1 + k);
  2392. #if defined(PRIx64)
  2393. os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1));
  2394. #else
  2395. if (4 == sizeof(long)) {
  2396. os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1));
  2397. }
  2398. else {
  2399. os_printf("<0x%016lx 0x%016lx>:v128", *v, *(v + 1));
  2400. }
  2401. #endif /* PRIx64 */
  2402. k += 4;
  2403. break;
  2404. }
  2405. #endif /* WASM_ENABLE_SIMD != 0 */
  2406. default:
  2407. bh_assert(0);
  2408. break;
  2409. }
  2410. if (j < (uint32)(type->result_count - 1))
  2411. os_printf(",");
  2412. }
  2413. os_printf("\n");
  2414. wasm_runtime_free(argv1);
  2415. return true;
  2416. fail:
  2417. if (argv1)
  2418. wasm_runtime_free(argv1);
  2419. exception = wasm_runtime_get_exception(module_inst);
  2420. bh_assert(exception);
  2421. os_printf("%s\n", exception);
  2422. return false;
  2423. }
  2424. bool
  2425. wasm_runtime_register_natives(const char *module_name,
  2426. NativeSymbol *native_symbols,
  2427. uint32 n_native_symbols)
  2428. {
  2429. return wasm_native_register_natives(module_name,
  2430. native_symbols, n_native_symbols);
  2431. }
  2432. bool
  2433. wasm_runtime_register_natives_raw(const char *module_name,
  2434. NativeSymbol *native_symbols,
  2435. uint32 n_native_symbols)
  2436. {
  2437. return wasm_native_register_natives_raw(module_name,
  2438. native_symbols, n_native_symbols);
  2439. }
  2440. bool
  2441. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  2442. const WASMType *func_type, const char *signature,
  2443. void *attachment,
  2444. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2445. {
  2446. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2447. typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*);
  2448. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  2449. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  2450. uint32 *argv_src = argv, i, argc1, ptr_len;
  2451. uint32 arg_i32;
  2452. bool ret = false;
  2453. argc1 = func_type->param_count;
  2454. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2455. size = sizeof(uint64) * (uint64)argc1;
  2456. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2457. NULL, 0))) {
  2458. return false;
  2459. }
  2460. }
  2461. argv_dst = argv1;
  2462. /* Traverse secondly to fill in each argument */
  2463. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  2464. switch (func_type->types[i]) {
  2465. case VALUE_TYPE_I32:
  2466. {
  2467. *(uint32*)argv_dst = arg_i32 = *argv_src++;
  2468. if (signature) {
  2469. if (signature[i + 1] == '*') {
  2470. /* param is a pointer */
  2471. if (signature[i + 2] == '~')
  2472. /* pointer with length followed */
  2473. ptr_len = *argv_src;
  2474. else
  2475. /* pointer without length followed */
  2476. ptr_len = 1;
  2477. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2478. goto fail;
  2479. *(uintptr_t*)argv_dst = (uintptr_t)
  2480. wasm_runtime_addr_app_to_native(module, arg_i32);
  2481. }
  2482. else if (signature[i + 1] == '$') {
  2483. /* param is a string */
  2484. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2485. goto fail;
  2486. *(uintptr_t*)argv_dst = (uintptr_t)
  2487. wasm_runtime_addr_app_to_native(module, arg_i32);
  2488. }
  2489. }
  2490. break;
  2491. }
  2492. case VALUE_TYPE_I64:
  2493. case VALUE_TYPE_F64:
  2494. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2);
  2495. argv_src += 2;
  2496. break;
  2497. case VALUE_TYPE_F32:
  2498. *(float32*)argv_dst = *(float32*)argv_src++;
  2499. break;
  2500. default:
  2501. bh_assert(0);
  2502. break;
  2503. }
  2504. }
  2505. exec_env->attachment = attachment;
  2506. invokeNativeRaw(exec_env, argv1);
  2507. exec_env->attachment = NULL;
  2508. if (func_type->result_count > 0) {
  2509. switch (func_type->types[func_type->param_count]) {
  2510. case VALUE_TYPE_I32:
  2511. argv_ret[0] = *(uint32*)argv1;
  2512. break;
  2513. case VALUE_TYPE_F32:
  2514. *(float32*)argv_ret = *(float32*)argv1;
  2515. break;
  2516. case VALUE_TYPE_I64:
  2517. case VALUE_TYPE_F64:
  2518. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64));
  2519. break;
  2520. default:
  2521. bh_assert(0);
  2522. break;
  2523. }
  2524. }
  2525. ret = !wasm_runtime_get_exception(module) ? true : false;
  2526. fail:
  2527. if (argv1 != argv_buf)
  2528. wasm_runtime_free(argv1);
  2529. return ret;
  2530. }
  2531. /**
  2532. * Implementation of wasm_runtime_invoke_native()
  2533. */
  2534. #define PUT_I64_TO_ADDR(addr, value) do { \
  2535. union { int64 val; uint32 parts[2]; } u; \
  2536. u.val = (value); \
  2537. (addr)[0] = u.parts[0]; \
  2538. (addr)[1] = u.parts[1]; \
  2539. } while (0)
  2540. #define PUT_F64_TO_ADDR(addr, value) do { \
  2541. union { float64 val; uint32 parts[2]; } u; \
  2542. u.val = (value); \
  2543. (addr)[0] = u.parts[0]; \
  2544. (addr)[1] = u.parts[1]; \
  2545. } while (0)
  2546. /* The invoke native implementation on ARM platform with VFP co-processor */
  2547. #if defined(BUILD_TARGET_ARM_VFP) \
  2548. || defined(BUILD_TARGET_THUMB_VFP) \
  2549. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  2550. || defined(BUILD_TARGET_RISCV32_ILP32)
  2551. typedef void (*GenericFunctionPointer)();
  2552. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2553. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2554. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2555. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32);
  2556. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2557. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2558. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  2559. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  2560. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2561. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2562. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2563. #if !defined(BUILD_TARGET_RISCV32_ILP32D) \
  2564. && !defined(BUILD_TARGET_RISCV32_ILP32)
  2565. #define MAX_REG_INTS 4
  2566. #define MAX_REG_FLOATS 16
  2567. #else
  2568. #define MAX_REG_INTS 8
  2569. #define MAX_REG_FLOATS 8
  2570. #endif
  2571. bool
  2572. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2573. const WASMType *func_type, const char *signature,
  2574. void *attachment,
  2575. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2576. {
  2577. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2578. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2579. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  2580. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2581. uint32 arg_i32, ptr_len;
  2582. uint32 result_count = func_type->result_count;
  2583. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2584. bool ret = false;
  2585. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2586. uint32 *fps;
  2587. int n_fps = 0;
  2588. #else
  2589. #define fps ints
  2590. #define n_fps n_ints
  2591. #endif
  2592. n_ints++; /* exec env */
  2593. /* Traverse firstly to calculate stack args count */
  2594. for (i = 0; i < func_type->param_count; i++) {
  2595. switch (func_type->types[i]) {
  2596. case VALUE_TYPE_I32:
  2597. if (n_ints < MAX_REG_INTS)
  2598. n_ints++;
  2599. else
  2600. n_stacks++;
  2601. break;
  2602. case VALUE_TYPE_I64:
  2603. if (n_ints < MAX_REG_INTS - 1) {
  2604. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2605. /* 64-bit data must be 8 bytes aligned in arm */
  2606. if (n_ints & 1)
  2607. n_ints++;
  2608. #endif
  2609. n_ints += 2;
  2610. }
  2611. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
  2612. /* part in register, part in stack */
  2613. else if (n_ints == MAX_REG_INTS - 1) {
  2614. n_ints++;
  2615. n_stacks++;
  2616. }
  2617. #endif
  2618. else {
  2619. /* 64-bit data in stack must be 8 bytes aligned
  2620. in arm and riscv32 */
  2621. if (n_stacks & 1)
  2622. n_stacks++;
  2623. n_stacks += 2;
  2624. }
  2625. break;
  2626. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2627. case VALUE_TYPE_F32:
  2628. if (n_fps < MAX_REG_FLOATS)
  2629. n_fps++;
  2630. else
  2631. n_stacks++;
  2632. break;
  2633. case VALUE_TYPE_F64:
  2634. if (n_fps < MAX_REG_FLOATS - 1) {
  2635. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2636. /* 64-bit data must be 8 bytes aligned in arm */
  2637. if (n_fps & 1)
  2638. n_fps++;
  2639. #endif
  2640. n_fps += 2;
  2641. }
  2642. #if defined(BUILD_TARGET_RISCV32_ILP32)
  2643. else if (n_fps == MAX_REG_FLOATS - 1) {
  2644. n_fps++;
  2645. n_stacks++;
  2646. }
  2647. #endif
  2648. else {
  2649. /* 64-bit data must be 8 bytes aligned in arm */
  2650. if (n_stacks & 1)
  2651. n_stacks++;
  2652. n_stacks += 2;
  2653. }
  2654. break;
  2655. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2656. case VALUE_TYPE_F32:
  2657. case VALUE_TYPE_F64:
  2658. if (n_fps < MAX_REG_FLOATS) {
  2659. n_fps++;
  2660. }
  2661. else if (func_type->types[i] == VALUE_TYPE_F32
  2662. && n_ints < MAX_REG_INTS) {
  2663. /* use int reg firstly if available */
  2664. n_ints++;
  2665. }
  2666. else if (func_type->types[i] == VALUE_TYPE_F64
  2667. && n_ints < MAX_REG_INTS - 1) {
  2668. /* use int regs firstly if available */
  2669. if (n_ints & 1)
  2670. n_ints++;
  2671. ints += 2;
  2672. }
  2673. else {
  2674. /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
  2675. if (n_stacks & 1)
  2676. n_stacks++;
  2677. n_stacks += 2;
  2678. }
  2679. break;
  2680. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2681. default:
  2682. bh_assert(0);
  2683. break;
  2684. }
  2685. }
  2686. for (i = 0; i < ext_ret_count; i++) {
  2687. if (n_ints < MAX_REG_INTS)
  2688. n_ints++;
  2689. else
  2690. n_stacks++;
  2691. }
  2692. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2693. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2694. #elif defined(BUILD_TARGET_RISCV32_ILP32)
  2695. argc1 = MAX_REG_INTS + n_stacks;
  2696. #else
  2697. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  2698. #endif
  2699. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2700. size = sizeof(uint32) * (uint32)argc1;
  2701. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2702. NULL, 0))) {
  2703. return false;
  2704. }
  2705. }
  2706. ints = argv1;
  2707. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2708. fps = ints + MAX_REG_INTS;
  2709. stacks = fps + MAX_REG_FLOATS;
  2710. #elif defined(BUILD_TARGET_RISCV32_ILP32)
  2711. stacks = ints + MAX_REG_INTS;
  2712. #else
  2713. fps = ints + MAX_REG_INTS;
  2714. stacks = fps + MAX_REG_FLOATS * 2;
  2715. #endif
  2716. n_ints = 0;
  2717. n_fps = 0;
  2718. n_stacks = 0;
  2719. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2720. /* Traverse secondly to fill in each argument */
  2721. for (i = 0; i < func_type->param_count; i++) {
  2722. switch (func_type->types[i]) {
  2723. case VALUE_TYPE_I32:
  2724. {
  2725. arg_i32 = *argv_src++;
  2726. if (signature) {
  2727. if (signature[i + 1] == '*') {
  2728. /* param is a pointer */
  2729. if (signature[i + 2] == '~')
  2730. /* pointer with length followed */
  2731. ptr_len = *argv_src;
  2732. else
  2733. /* pointer without length followed */
  2734. ptr_len = 1;
  2735. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2736. goto fail;
  2737. arg_i32 = (uintptr_t)
  2738. wasm_runtime_addr_app_to_native(module, arg_i32);
  2739. }
  2740. else if (signature[i + 1] == '$') {
  2741. /* param is a string */
  2742. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2743. goto fail;
  2744. arg_i32 = (uintptr_t)
  2745. wasm_runtime_addr_app_to_native(module, arg_i32);
  2746. }
  2747. }
  2748. if (n_ints < MAX_REG_INTS)
  2749. ints[n_ints++] = arg_i32;
  2750. else
  2751. stacks[n_stacks++] = arg_i32;
  2752. break;
  2753. }
  2754. case VALUE_TYPE_I64:
  2755. {
  2756. if (n_ints < MAX_REG_INTS - 1) {
  2757. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2758. /* 64-bit data must be 8 bytes aligned in arm */
  2759. if (n_ints & 1)
  2760. n_ints++;
  2761. #endif
  2762. *(uint64*)&ints[n_ints] = *(uint64*)argv_src;
  2763. n_ints += 2;
  2764. argv_src += 2;
  2765. }
  2766. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
  2767. else if (n_ints == MAX_REG_INTS - 1) {
  2768. ints[n_ints++] = *argv_src++;
  2769. stacks[n_stacks++] = *argv_src++;
  2770. }
  2771. #endif
  2772. else {
  2773. /* 64-bit data in stack must be 8 bytes aligned
  2774. in arm and riscv32 */
  2775. if (n_stacks & 1)
  2776. n_stacks++;
  2777. *(uint64*)&stacks[n_stacks] = *(uint64*)argv_src;
  2778. n_stacks += 2;
  2779. argv_src += 2;
  2780. }
  2781. break;
  2782. }
  2783. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2784. case VALUE_TYPE_F32:
  2785. {
  2786. if (n_fps < MAX_REG_FLOATS)
  2787. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2788. else
  2789. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2790. break;
  2791. }
  2792. case VALUE_TYPE_F64:
  2793. {
  2794. if (n_fps < MAX_REG_FLOATS - 1) {
  2795. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2796. /* 64-bit data must be 8 bytes aligned in arm */
  2797. if (n_fps & 1)
  2798. n_fps++;
  2799. #endif
  2800. *(float64*)&fps[n_fps] = *(float64*)argv_src;
  2801. n_fps += 2;
  2802. argv_src += 2;
  2803. }
  2804. #if defined(BUILD_TARGET_RISCV32_ILP32)
  2805. else if (n_fps == MAX_REG_FLOATS - 1) {
  2806. fps[n_fps++] = *argv_src++;
  2807. stacks[n_stacks++] = *argv_src++;
  2808. }
  2809. #endif
  2810. else {
  2811. /* 64-bit data must be 8 bytes aligned in arm */
  2812. if (n_stacks & 1)
  2813. n_stacks++;
  2814. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  2815. n_stacks += 2;
  2816. argv_src += 2;
  2817. }
  2818. break;
  2819. }
  2820. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2821. case VALUE_TYPE_F32:
  2822. case VALUE_TYPE_F64:
  2823. {
  2824. if (n_fps < MAX_REG_FLOATS) {
  2825. if (func_type->types[i] == VALUE_TYPE_F32) {
  2826. *(float32*)&fps[n_fps * 2] = *(float32*)argv_src++;
  2827. /* NaN boxing, the upper bits of a valid NaN-boxed
  2828. value must be all 1s. */
  2829. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  2830. }
  2831. else {
  2832. *(float64*)&fps[n_fps * 2] = *(float64*)argv_src;
  2833. argv_src += 2;
  2834. }
  2835. n_fps++;
  2836. }
  2837. else if (func_type->types[i] == VALUE_TYPE_F32
  2838. && n_ints < MAX_REG_INTS) {
  2839. /* use int reg firstly if available */
  2840. *(float32*)&ints[n_ints++] = *(float32*)argv_src++;
  2841. }
  2842. else if (func_type->types[i] == VALUE_TYPE_F64
  2843. && n_ints < MAX_REG_INTS - 1) {
  2844. /* use int regs firstly if available */
  2845. if (n_ints & 1)
  2846. n_ints++;
  2847. *(float64*)&ints[n_ints] = *(float64*)argv_src;
  2848. n_ints += 2;
  2849. argv_src += 2;
  2850. }
  2851. else {
  2852. /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
  2853. if (n_stacks & 1)
  2854. n_stacks++;
  2855. if (func_type->types[i] == VALUE_TYPE_F32) {
  2856. *(float32*)&stacks[n_stacks] = *(float32*)argv_src++;
  2857. /* NaN boxing, the upper bits of a valid NaN-boxed
  2858. value must be all 1s. */
  2859. stacks[n_stacks + 1] = 0xFFFFFFFF;
  2860. }
  2861. else {
  2862. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  2863. argv_src += 2;
  2864. }
  2865. n_stacks += 2;
  2866. }
  2867. break;
  2868. }
  2869. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2870. default:
  2871. bh_assert(0);
  2872. break;
  2873. }
  2874. }
  2875. /* Save extra result values' address to argv1 */
  2876. for (i = 0; i < ext_ret_count; i++) {
  2877. if (n_ints < MAX_REG_INTS)
  2878. ints[n_ints++] = *(uint32*)argv_src++;
  2879. else
  2880. stacks[n_stacks++] = *(uint32*)argv_src++;
  2881. }
  2882. exec_env->attachment = attachment;
  2883. if (func_type->result_count == 0) {
  2884. invokeNative_Void(func_ptr, argv1, n_stacks);
  2885. }
  2886. else {
  2887. switch (func_type->types[func_type->param_count]) {
  2888. case VALUE_TYPE_I32:
  2889. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  2890. break;
  2891. case VALUE_TYPE_I64:
  2892. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  2893. break;
  2894. case VALUE_TYPE_F32:
  2895. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  2896. break;
  2897. case VALUE_TYPE_F64:
  2898. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  2899. break;
  2900. default:
  2901. bh_assert(0);
  2902. break;
  2903. }
  2904. }
  2905. exec_env->attachment = NULL;
  2906. ret = !wasm_runtime_get_exception(module) ? true : false;
  2907. fail:
  2908. if (argv1 != argv_buf)
  2909. wasm_runtime_free(argv1);
  2910. return ret;
  2911. }
  2912. #endif /* end of defined(BUILD_TARGET_ARM_VFP)
  2913. || defined(BUILD_TARGET_THUMB_VFP) \
  2914. || defined(BUILD_TARGET_RISCV32_ILP32D)
  2915. || defined(BUILD_TARGET_RISCV32_ILP32) */
  2916. #if defined(BUILD_TARGET_X86_32) \
  2917. || defined(BUILD_TARGET_ARM) \
  2918. || defined(BUILD_TARGET_THUMB) \
  2919. || defined(BUILD_TARGET_MIPS) \
  2920. || defined(BUILD_TARGET_XTENSA)
  2921. typedef void (*GenericFunctionPointer)();
  2922. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  2923. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2924. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2925. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2926. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2927. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  2928. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  2929. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  2930. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  2931. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  2932. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  2933. static inline void
  2934. word_copy(uint32 *dest, uint32 *src, unsigned num)
  2935. {
  2936. for (; num > 0; num--)
  2937. *dest++ = *src++;
  2938. }
  2939. bool
  2940. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2941. const WASMType *func_type, const char *signature,
  2942. void *attachment,
  2943. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2944. {
  2945. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2946. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  2947. uint32 arg_i32, ptr_len;
  2948. uint32 result_count = func_type->result_count;
  2949. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2950. uint64 size;
  2951. bool ret = false;
  2952. #if defined(BUILD_TARGET_X86_32)
  2953. argc1 = argc + ext_ret_count + 2;
  2954. #else
  2955. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  2956. so we need to allocate more memory. */
  2957. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  2958. #endif
  2959. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2960. size = sizeof(uint32) * (uint64)argc1;
  2961. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2962. NULL, 0))) {
  2963. return false;
  2964. }
  2965. }
  2966. for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
  2967. argv1[j++] = ((uint32*)&exec_env)[i];
  2968. for (i = 0; i < func_type->param_count; i++) {
  2969. switch (func_type->types[i]) {
  2970. case VALUE_TYPE_I32:
  2971. {
  2972. arg_i32 = *argv++;
  2973. if (signature) {
  2974. if (signature[i + 1] == '*') {
  2975. /* param is a pointer */
  2976. if (signature[i + 2] == '~')
  2977. /* pointer with length followed */
  2978. ptr_len = *argv;
  2979. else
  2980. /* pointer without length followed */
  2981. ptr_len = 1;
  2982. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2983. goto fail;
  2984. arg_i32 = (uintptr_t)
  2985. wasm_runtime_addr_app_to_native(module, arg_i32);
  2986. }
  2987. else if (signature[i + 1] == '$') {
  2988. /* param is a string */
  2989. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2990. goto fail;
  2991. arg_i32 = (uintptr_t)
  2992. wasm_runtime_addr_app_to_native(module, arg_i32);
  2993. }
  2994. }
  2995. argv1[j++] = arg_i32;
  2996. break;
  2997. }
  2998. case VALUE_TYPE_I64:
  2999. case VALUE_TYPE_F64:
  3000. #if !defined(BUILD_TARGET_X86_32)
  3001. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3002. and xtensa */
  3003. if (j & 1)
  3004. j++;
  3005. #endif
  3006. argv1[j++] = *argv++;
  3007. argv1[j++] = *argv++;
  3008. break;
  3009. case VALUE_TYPE_F32:
  3010. argv1[j++] = *argv++;
  3011. break;
  3012. default:
  3013. bh_assert(0);
  3014. break;
  3015. }
  3016. }
  3017. /* Save extra result values' address to argv1 */
  3018. word_copy(argv1 + j, argv, ext_ret_count);
  3019. argc1 = j + ext_ret_count;
  3020. exec_env->attachment = attachment;
  3021. if (func_type->result_count == 0) {
  3022. invokeNative_Void(func_ptr, argv1, argc1);
  3023. }
  3024. else {
  3025. switch (func_type->types[func_type->param_count]) {
  3026. case VALUE_TYPE_I32:
  3027. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3028. break;
  3029. case VALUE_TYPE_I64:
  3030. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
  3031. break;
  3032. case VALUE_TYPE_F32:
  3033. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
  3034. break;
  3035. case VALUE_TYPE_F64:
  3036. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
  3037. break;
  3038. default:
  3039. bh_assert(0);
  3040. break;
  3041. }
  3042. }
  3043. exec_env->attachment = NULL;
  3044. ret = !wasm_runtime_get_exception(module) ? true : false;
  3045. fail:
  3046. if (argv1 != argv_buf)
  3047. wasm_runtime_free(argv1);
  3048. return ret;
  3049. }
  3050. #endif /* end of defined(BUILD_TARGET_X86_32) \
  3051. || defined(BUILD_TARGET_ARM) \
  3052. || defined(BUILD_TARGET_THUMB) \
  3053. || defined(BUILD_TARGET_MIPS) \
  3054. || defined(BUILD_TARGET_XTENSA) */
  3055. #if defined(BUILD_TARGET_X86_64) \
  3056. || defined(BUILD_TARGET_AMD_64) \
  3057. || defined(BUILD_TARGET_AARCH64) \
  3058. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3059. || defined(BUILD_TARGET_RISCV64_LP64)
  3060. #if WASM_ENABLE_SIMD != 0
  3061. #ifdef v128
  3062. #undef v128
  3063. #endif
  3064. typedef long long v128 __attribute__ ((__vector_size__ (16),
  3065. __may_alias__, __aligned__ (1)));
  3066. #endif /* end of WASM_ENABLE_SIMD != 0 */
  3067. typedef void (*GenericFunctionPointer)();
  3068. int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3069. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3070. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3071. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3072. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3073. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3074. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  3075. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  3076. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  3077. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3078. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3079. #if WASM_ENABLE_SIMD != 0
  3080. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3081. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  3082. #endif
  3083. #if defined(_WIN32) || defined(_WIN32_)
  3084. #define MAX_REG_FLOATS 4
  3085. #define MAX_REG_INTS 4
  3086. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  3087. #define MAX_REG_FLOATS 8
  3088. #if defined(BUILD_TARGET_AARCH64) \
  3089. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3090. || defined(BUILD_TARGET_RISCV64_LP64)
  3091. #define MAX_REG_INTS 8
  3092. #else
  3093. #define MAX_REG_INTS 6
  3094. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  3095. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3096. || defined(BUILD_TARGET_RISCV64_LP64) */
  3097. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3098. bool
  3099. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3100. const WASMType *func_type, const char *signature,
  3101. void *attachment,
  3102. uint32 *argv, uint32 argc, uint32 *argv_ret)
  3103. {
  3104. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3105. uint64 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size, arg_i64;
  3106. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3107. uint32 arg_i32, ptr_len;
  3108. uint32 result_count = func_type->result_count;
  3109. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3110. bool ret = false;
  3111. #ifndef BUILD_TARGET_RISCV64_LP64
  3112. #if WASM_ENABLE_SIMD == 0
  3113. uint64 *fps;
  3114. #else
  3115. v128 *fps;
  3116. #endif
  3117. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3118. #define fps ints
  3119. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3120. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  3121. /* important difference in calling conventions */
  3122. #define n_fps n_ints
  3123. #else
  3124. int n_fps = 0;
  3125. #endif
  3126. #if WASM_ENABLE_SIMD == 0
  3127. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count
  3128. + ext_ret_count;
  3129. #else
  3130. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  3131. + ext_ret_count;
  3132. #endif
  3133. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3134. size = sizeof(uint64) * (uint64)argc1;
  3135. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  3136. NULL, 0))) {
  3137. return false;
  3138. }
  3139. }
  3140. #ifndef BUILD_TARGET_RISCV64_LP64
  3141. #if WASM_ENABLE_SIMD == 0
  3142. fps = argv1;
  3143. ints = fps + MAX_REG_FLOATS;
  3144. #else
  3145. fps = (v128 *)argv1;
  3146. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  3147. #endif
  3148. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3149. ints = argv1;
  3150. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3151. stacks = ints + MAX_REG_INTS;
  3152. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  3153. for (i = 0; i < func_type->param_count; i++) {
  3154. switch (func_type->types[i]) {
  3155. case VALUE_TYPE_I32:
  3156. {
  3157. arg_i32 = *argv_src++;
  3158. arg_i64 = arg_i32;
  3159. if (signature) {
  3160. if (signature[i + 1] == '*') {
  3161. /* param is a pointer */
  3162. if (signature[i + 2] == '~')
  3163. /* pointer with length followed */
  3164. ptr_len = *argv_src;
  3165. else
  3166. /* pointer without length followed */
  3167. ptr_len = 1;
  3168. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  3169. goto fail;
  3170. arg_i64 = (uintptr_t)
  3171. wasm_runtime_addr_app_to_native(module, arg_i32);
  3172. }
  3173. else if (signature[i + 1] == '$') {
  3174. /* param is a string */
  3175. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  3176. goto fail;
  3177. arg_i64 = (uintptr_t)
  3178. wasm_runtime_addr_app_to_native(module, arg_i32);
  3179. }
  3180. }
  3181. if (n_ints < MAX_REG_INTS)
  3182. ints[n_ints++] = arg_i64;
  3183. else
  3184. stacks[n_stacks++] = arg_i64;
  3185. break;
  3186. }
  3187. case VALUE_TYPE_I64:
  3188. if (n_ints < MAX_REG_INTS)
  3189. ints[n_ints++] = *(uint64*)argv_src;
  3190. else
  3191. stacks[n_stacks++] = *(uint64*)argv_src;
  3192. argv_src += 2;
  3193. break;
  3194. case VALUE_TYPE_F32:
  3195. if (n_fps < MAX_REG_FLOATS) {
  3196. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  3197. }
  3198. else {
  3199. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  3200. }
  3201. break;
  3202. case VALUE_TYPE_F64:
  3203. if (n_fps < MAX_REG_FLOATS) {
  3204. *(float64*)&fps[n_fps++] = *(float64*)argv_src;
  3205. }
  3206. else {
  3207. *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
  3208. }
  3209. argv_src += 2;
  3210. break;
  3211. #if WASM_ENABLE_SIMD != 0
  3212. case VALUE_TYPE_V128:
  3213. if (n_fps < MAX_REG_FLOATS) {
  3214. *(v128*)&fps[n_fps++] = *(v128*)argv_src;
  3215. }
  3216. else {
  3217. *(v128*)&stacks[n_stacks++] = *(v128*)argv_src;
  3218. n_stacks++;
  3219. }
  3220. argv_src += 4;
  3221. break;
  3222. #endif
  3223. default:
  3224. bh_assert(0);
  3225. break;
  3226. }
  3227. }
  3228. /* Save extra result values' address to argv1 */
  3229. for (i = 0; i < ext_ret_count; i++) {
  3230. if (n_ints < MAX_REG_INTS)
  3231. ints[n_ints++] = *(uint64*)argv_src;
  3232. else
  3233. stacks[n_stacks++] = *(uint64*)argv_src;
  3234. argv_src += 2;
  3235. }
  3236. exec_env->attachment = attachment;
  3237. if (result_count == 0) {
  3238. invokeNative_Void(func_ptr, argv1, n_stacks);
  3239. }
  3240. else {
  3241. /* Invoke the native function and get the first result value */
  3242. switch (func_type->types[func_type->param_count]) {
  3243. case VALUE_TYPE_I32:
  3244. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3245. break;
  3246. case VALUE_TYPE_I64:
  3247. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  3248. break;
  3249. case VALUE_TYPE_F32:
  3250. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  3251. break;
  3252. case VALUE_TYPE_F64:
  3253. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3254. break;
  3255. #if WASM_ENABLE_SIMD != 0
  3256. case VALUE_TYPE_V128:
  3257. *(v128*)argv_ret = invokeNative_V128(func_ptr, argv1, n_stacks);
  3258. break;
  3259. #endif
  3260. default:
  3261. bh_assert(0);
  3262. break;
  3263. }
  3264. }
  3265. exec_env->attachment = NULL;
  3266. ret = !wasm_runtime_get_exception(module) ? true : false;
  3267. fail:
  3268. if (argv1 != argv_buf)
  3269. wasm_runtime_free(argv1);
  3270. return ret;
  3271. }
  3272. #endif /* end of defined(BUILD_TARGET_X86_64) \
  3273. || defined(BUILD_TARGET_AMD_64) \
  3274. || defined(BUILD_TARGET_AARCH64) \
  3275. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3276. || defined(BUILD_TARGET_RISCV64_LP64) */
  3277. bool
  3278. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  3279. uint32_t element_indices,
  3280. uint32_t argc, uint32_t argv[])
  3281. {
  3282. if (!wasm_runtime_exec_env_check(exec_env)) {
  3283. LOG_ERROR("Invalid exec env stack info.");
  3284. return false;
  3285. }
  3286. /* this function is called from native code, so exec_env->handle and
  3287. exec_env->native_stack_boundary must have been set, we don't set
  3288. it again */
  3289. #if WASM_ENABLE_INTERP != 0
  3290. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  3291. return wasm_call_indirect(exec_env, element_indices, argc, argv);
  3292. #endif
  3293. #if WASM_ENABLE_AOT != 0
  3294. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  3295. return aot_call_indirect(exec_env, element_indices, argc, argv);
  3296. #endif
  3297. return false;
  3298. }
  3299. static void
  3300. exchange_uint32(uint8 *p_data)
  3301. {
  3302. uint8 value = *p_data;
  3303. *p_data = *(p_data + 3);
  3304. *(p_data + 3) = value;
  3305. value = *(p_data + 1);
  3306. *(p_data + 1) = *(p_data + 2);
  3307. *(p_data + 2) = value;
  3308. }
  3309. static void
  3310. exchange_uint64(uint8 *p_data)
  3311. {
  3312. uint32 value;
  3313. value = *(uint32 *)p_data;
  3314. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  3315. *(uint32 *)(p_data + 4) = value;
  3316. exchange_uint32(p_data);
  3317. exchange_uint32(p_data + 4);
  3318. }
  3319. void
  3320. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  3321. {
  3322. uint64 u1, u2;
  3323. bh_memcpy_s(&u1, 8, bytes, 8);
  3324. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  3325. if (!is_little_endian()) {
  3326. exchange_uint64((uint8*)&u1);
  3327. exchange_uint64((uint8*)&u2);
  3328. *ret1 = u2;
  3329. *ret2 = u1;
  3330. }
  3331. else {
  3332. *ret1 = u1;
  3333. *ret2 = u2;
  3334. }
  3335. }
  3336. #if WASM_ENABLE_THREAD_MGR != 0
  3337. typedef struct WASMThreadArg {
  3338. WASMExecEnv *new_exec_env;
  3339. wasm_thread_callback_t callback;
  3340. void *arg;
  3341. } WASMThreadArg;
  3342. WASMExecEnv *
  3343. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  3344. {
  3345. return wasm_cluster_spawn_exec_env(exec_env);
  3346. }
  3347. void
  3348. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  3349. {
  3350. wasm_cluster_destroy_spawned_exec_env(exec_env);
  3351. }
  3352. static void*
  3353. wasm_runtime_thread_routine(void *arg)
  3354. {
  3355. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  3356. void *ret;
  3357. bh_assert(thread_arg->new_exec_env);
  3358. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  3359. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  3360. wasm_runtime_free(thread_arg);
  3361. os_thread_exit(ret);
  3362. return ret;
  3363. }
  3364. int32
  3365. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  3366. wasm_thread_callback_t callback, void *arg)
  3367. {
  3368. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  3369. WASMThreadArg *thread_arg;
  3370. int32 ret;
  3371. if (!new_exec_env)
  3372. return -1;
  3373. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  3374. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3375. return -1;
  3376. }
  3377. thread_arg->new_exec_env = new_exec_env;
  3378. thread_arg->callback = callback;
  3379. thread_arg->arg = arg;
  3380. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  3381. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  3382. if (ret != 0) {
  3383. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3384. wasm_runtime_free(thread_arg);
  3385. }
  3386. return ret;
  3387. }
  3388. int32
  3389. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  3390. {
  3391. return os_thread_join((korp_tid)tid, retval);
  3392. }
  3393. #endif
  3394. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3395. void
  3396. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  3397. {
  3398. WASMModuleInstanceCommon *module_inst
  3399. = wasm_exec_env_get_module_inst(exec_env);
  3400. #if WASM_ENABLE_INTERP != 0
  3401. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3402. wasm_interp_dump_call_stack(exec_env);
  3403. }
  3404. #endif
  3405. #if WASM_ENABLE_AOT != 0
  3406. if (module_inst->module_type == Wasm_Module_AoT) {
  3407. aot_dump_call_stack(exec_env);
  3408. }
  3409. #endif
  3410. }
  3411. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */