wasm_runtime_common.c 124 KB

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