wasm_runtime_common.c 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565
  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. #if (WASM_ENABLE_MEMORY_PROFILING != 0) || (WASM_ENABLE_MEMORY_TRACING != 0)
  716. void
  717. wasm_runtime_dump_module_mem_consumption(const WASMModuleCommon *module)
  718. {
  719. WASMModuleMemConsumption mem_conspn = { 0 };
  720. #if WASM_ENABLE_INTERP != 0
  721. if (module->module_type == Wasm_Module_Bytecode) {
  722. wasm_get_module_mem_consumption((WASMModule*)module, &mem_conspn);
  723. }
  724. #endif
  725. #if WASM_ENABLE_AOT != 0
  726. if (module->module_type == Wasm_Module_AoT) {
  727. aot_get_module_mem_consumption((AOTModule*)module, &mem_conspn);
  728. }
  729. #endif
  730. os_printf("WASM module memory consumption, total size: %u\n",
  731. mem_conspn.total_size);
  732. os_printf(" module struct size: %u\n", mem_conspn.module_struct_size);
  733. os_printf(" types size: %u\n", mem_conspn.types_size);
  734. os_printf(" imports size: %u\n", mem_conspn.imports_size);
  735. os_printf(" funcs size: %u\n", mem_conspn.functions_size);
  736. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  737. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  738. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  739. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  740. os_printf(" table segs size: %u\n", mem_conspn.table_segs_size);
  741. os_printf(" data segs size: %u\n", mem_conspn.data_segs_size);
  742. os_printf(" const strings size: %u\n", mem_conspn.const_strs_size);
  743. #if WASM_ENABLE_AOT != 0
  744. os_printf(" aot code size: %u\n", mem_conspn.aot_code_size);
  745. #endif
  746. }
  747. void
  748. wasm_runtime_dump_module_inst_mem_consumption(const WASMModuleInstanceCommon
  749. *module_inst)
  750. {
  751. WASMModuleInstMemConsumption mem_conspn = { 0 };
  752. #if WASM_ENABLE_INTERP != 0
  753. if (module_inst->module_type == Wasm_Module_Bytecode) {
  754. wasm_get_module_inst_mem_consumption((WASMModuleInstance*)module_inst,
  755. &mem_conspn);
  756. }
  757. #endif
  758. #if WASM_ENABLE_AOT != 0
  759. if (module_inst->module_type == Wasm_Module_AoT) {
  760. aot_get_module_inst_mem_consumption((AOTModuleInstance*)module_inst,
  761. &mem_conspn);
  762. }
  763. #endif
  764. os_printf("WASM module inst memory consumption, total size: %u\n",
  765. mem_conspn.total_size);
  766. os_printf(" module inst struct size: %u\n",
  767. mem_conspn.module_inst_struct_size);
  768. os_printf(" memories size: %u\n", mem_conspn.memories_size);
  769. os_printf(" app heap size: %u\n", mem_conspn.app_heap_size);
  770. os_printf(" tables size: %u\n", mem_conspn.tables_size);
  771. os_printf(" functions size: %u\n", mem_conspn.functions_size);
  772. os_printf(" globals size: %u\n", mem_conspn.globals_size);
  773. os_printf(" exports size: %u\n", mem_conspn.exports_size);
  774. }
  775. void
  776. wasm_runtime_dump_exec_env_mem_consumption(const WASMExecEnv *exec_env)
  777. {
  778. uint32 total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  779. + exec_env->wasm_stack_size;
  780. os_printf("Exec env memory consumption, total size: %u\n", total_size);
  781. os_printf(" exec env struct size: %u\n",
  782. offsetof(WASMExecEnv, wasm_stack.s.bottom));
  783. #if WASM_ENABLE_INTERP != 0 && WASM_ENABLE_FAST_INTERP == 0
  784. os_printf(" block addr cache size: %u\n",
  785. sizeof(exec_env->block_addr_cache));
  786. #endif
  787. os_printf(" stack size: %u\n", exec_env->wasm_stack_size);
  788. }
  789. uint32
  790. gc_get_heap_highmark_size(void *heap);
  791. void
  792. wasm_runtime_dump_mem_consumption(WASMExecEnv *exec_env)
  793. {
  794. WASMModuleInstMemConsumption module_inst_mem_consps;
  795. WASMModuleMemConsumption module_mem_consps;
  796. WASMModuleInstanceCommon *module_inst_common;
  797. WASMModuleCommon *module_common = NULL;
  798. void *heap_handle = NULL;
  799. uint32 total_size = 0, app_heap_peak_size = 0;
  800. uint32 max_aux_stack_used = -1;
  801. module_inst_common = exec_env->module_inst;
  802. #if WASM_ENABLE_INTERP != 0
  803. if (module_inst_common->module_type == Wasm_Module_Bytecode) {
  804. WASMModuleInstance *wasm_module_inst =
  805. (WASMModuleInstance*)module_inst_common;
  806. WASMModule *wasm_module = wasm_module_inst->module;
  807. module_common = (WASMModuleCommon*)wasm_module;
  808. if (wasm_module_inst->memories) {
  809. heap_handle = wasm_module_inst->memories[0]->heap_handle;
  810. }
  811. wasm_get_module_inst_mem_consumption
  812. (wasm_module_inst, &module_inst_mem_consps);
  813. wasm_get_module_mem_consumption
  814. (wasm_module, &module_mem_consps);
  815. if (wasm_module_inst->module->aux_stack_top_global_index != (uint32)-1)
  816. max_aux_stack_used = wasm_module_inst->max_aux_stack_used;
  817. }
  818. #endif
  819. #if WASM_ENABLE_AOT != 0
  820. if (module_inst_common->module_type == Wasm_Module_AoT) {
  821. AOTModuleInstance *aot_module_inst =
  822. (AOTModuleInstance*)module_inst_common;
  823. AOTModule *aot_module =
  824. (AOTModule*)aot_module_inst->aot_module.ptr;
  825. module_common = (WASMModuleCommon*)aot_module;
  826. if (aot_module_inst->memories.ptr) {
  827. AOTMemoryInstance **memories =
  828. (AOTMemoryInstance **)aot_module_inst->memories.ptr;
  829. heap_handle = memories[0]->heap_handle.ptr;
  830. }
  831. aot_get_module_inst_mem_consumption
  832. (aot_module_inst, &module_inst_mem_consps);
  833. aot_get_module_mem_consumption
  834. (aot_module, &module_mem_consps);
  835. }
  836. #endif
  837. bh_assert(module_common != NULL);
  838. if (heap_handle) {
  839. app_heap_peak_size = gc_get_heap_highmark_size(heap_handle);
  840. }
  841. total_size = offsetof(WASMExecEnv, wasm_stack.s.bottom)
  842. + exec_env->wasm_stack_size
  843. + module_mem_consps.total_size
  844. + module_inst_mem_consps.total_size;
  845. os_printf("\nMemory consumption summary (bytes):\n");
  846. wasm_runtime_dump_module_mem_consumption(module_common);
  847. wasm_runtime_dump_module_inst_mem_consumption(module_inst_common);
  848. wasm_runtime_dump_exec_env_mem_consumption(exec_env);
  849. os_printf("\nTotal memory consumption of module, module inst and "
  850. "exec env: %u\n", total_size);
  851. os_printf("Total interpreter stack used: %u\n",
  852. exec_env->max_wasm_stack_used);
  853. if (max_aux_stack_used != (uint32)-1)
  854. os_printf("Total auxiliary stack used: %u\n", max_aux_stack_used);
  855. else
  856. os_printf("Total aux stack used: no enough info to profile\n");
  857. os_printf("Total app heap used: %u\n", app_heap_peak_size);
  858. }
  859. #endif /* end of (WASM_ENABLE_MEMORY_PROFILING != 0)
  860. || (WASM_ENABLE_MEMORY_TRACING != 0) */
  861. #if WASM_ENABLE_PERF_PROFILING != 0
  862. void
  863. wasm_runtime_dump_perf_profiling(WASMModuleInstanceCommon *module_inst)
  864. {
  865. #if WASM_ENABLE_INTERP != 0
  866. if (module_inst->module_type == Wasm_Module_Bytecode) {
  867. wasm_dump_perf_profiling((WASMModuleInstance*)module_inst);
  868. }
  869. #endif
  870. #if WASM_ENABLE_AOT != 0
  871. if (module_inst->module_type == Wasm_Module_AoT) {
  872. aot_dump_perf_profiling((AOTModuleInstance*)module_inst);
  873. }
  874. #endif
  875. }
  876. #endif
  877. WASMModuleInstanceCommon *
  878. wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
  879. {
  880. return wasm_exec_env_get_module_inst(exec_env);
  881. }
  882. void *
  883. wasm_runtime_get_function_attachment(WASMExecEnv *exec_env)
  884. {
  885. return exec_env->attachment;
  886. }
  887. void
  888. wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data)
  889. {
  890. exec_env->user_data = user_data;
  891. }
  892. void *
  893. wasm_runtime_get_user_data(WASMExecEnv *exec_env)
  894. {
  895. return exec_env->user_data;
  896. }
  897. WASMType *
  898. wasm_runtime_get_function_type(const WASMFunctionInstanceCommon *function,
  899. uint32 module_type)
  900. {
  901. WASMType *type = NULL;
  902. #if WASM_ENABLE_INTERP != 0
  903. if (module_type == Wasm_Module_Bytecode) {
  904. WASMFunctionInstance *wasm_func = (WASMFunctionInstance *)function;
  905. type = wasm_func->is_import_func
  906. ? wasm_func->u.func_import->func_type
  907. : wasm_func->u.func->func_type;
  908. }
  909. #endif
  910. #if WASM_ENABLE_AOT != 0
  911. if (module_type == Wasm_Module_AoT) {
  912. AOTFunctionInstance *aot_func = (AOTFunctionInstance *)function;
  913. type = aot_func->is_import_func
  914. ? aot_func->u.func_import->func_type
  915. : aot_func->u.func.func_type;
  916. }
  917. #endif
  918. return type;
  919. }
  920. WASMFunctionInstanceCommon *
  921. wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
  922. const char *name, const char *signature)
  923. {
  924. #if WASM_ENABLE_INTERP != 0
  925. if (module_inst->module_type == Wasm_Module_Bytecode)
  926. return (WASMFunctionInstanceCommon*)
  927. wasm_lookup_function((const WASMModuleInstance*)module_inst,
  928. name, signature);
  929. #endif
  930. #if WASM_ENABLE_AOT != 0
  931. if (module_inst->module_type == Wasm_Module_AoT)
  932. return (WASMFunctionInstanceCommon*)
  933. aot_lookup_function((const AOTModuleInstance*)module_inst,
  934. name, signature);
  935. #endif
  936. return NULL;
  937. }
  938. #if WASM_ENABLE_REF_TYPES != 0
  939. static void
  940. wasm_runtime_reclaim_externref(WASMExecEnv *exec_env,
  941. WASMFunctionInstanceCommon *function,
  942. uint32 *argv)
  943. {
  944. uint32 i = 0, cell_num = 0;
  945. WASMType *func_type = wasm_runtime_get_function_type(
  946. function, exec_env->module_inst->module_type);
  947. bh_assert(func_type);
  948. while (i < func_type->result_count) {
  949. uint8 result_type = func_type->types[func_type->param_count + i];
  950. if (result_type == VALUE_TYPE_EXTERNREF && argv[i] != NULL_REF) {
  951. /* Retain the externref returned to runtime embedder */
  952. (void)wasm_externref_retain(argv[i]);
  953. }
  954. cell_num += wasm_value_type_cell_num(result_type);
  955. i++;
  956. }
  957. wasm_externref_reclaim(exec_env->module_inst);
  958. }
  959. void
  960. wasm_runtime_prepare_call_function(WASMExecEnv *exec_env,
  961. WASMFunctionInstanceCommon *function)
  962. {
  963. exec_env->nested_calling_depth++;
  964. }
  965. void
  966. wasm_runtime_finalize_call_function(WASMExecEnv *exec_env,
  967. WASMFunctionInstanceCommon *function,
  968. bool ret, uint32 *argv)
  969. {
  970. exec_env->nested_calling_depth--;
  971. if (!exec_env->nested_calling_depth && ret) {
  972. wasm_runtime_reclaim_externref(exec_env, function, argv);
  973. }
  974. }
  975. #endif
  976. bool
  977. wasm_runtime_call_wasm(WASMExecEnv *exec_env,
  978. WASMFunctionInstanceCommon *function,
  979. uint32 argc, uint32 argv[])
  980. {
  981. bool ret = false;
  982. if (!wasm_runtime_exec_env_check(exec_env)) {
  983. LOG_ERROR("Invalid exec env stack info.");
  984. return false;
  985. }
  986. #if WASM_ENABLE_REF_TYPES != 0
  987. wasm_runtime_prepare_call_function(exec_env, function);
  988. #endif
  989. #if WASM_ENABLE_INTERP != 0
  990. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  991. ret = wasm_call_function(exec_env,
  992. (WASMFunctionInstance*)function,
  993. argc, argv);
  994. #endif
  995. #if WASM_ENABLE_AOT != 0
  996. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  997. ret = aot_call_function(exec_env,
  998. (AOTFunctionInstance*)function,
  999. argc, argv);
  1000. #endif
  1001. #if WASM_ENABLE_REF_TYPES != 0
  1002. wasm_runtime_finalize_call_function(exec_env, function, ret, argv);
  1003. #endif
  1004. return ret;
  1005. }
  1006. static uint32
  1007. parse_args_to_uint32_array(WASMType *type,
  1008. uint32 num_args, wasm_val_t *args,
  1009. uint32 *out_argv)
  1010. {
  1011. uint32 i, p;
  1012. for (i = 0, p = 0; i < num_args; i++) {
  1013. switch (args[i].kind) {
  1014. case WASM_I32:
  1015. out_argv[p++] = args[i].of.i32;
  1016. break;
  1017. case WASM_I64:
  1018. {
  1019. union { uint64 val; uint32 parts[2]; } u;
  1020. u.val = args[i].of.i64;
  1021. out_argv[p++] = u.parts[0];
  1022. out_argv[p++] = u.parts[1];
  1023. break;
  1024. }
  1025. case WASM_F32:
  1026. {
  1027. union { float32 val; uint32 part; } u;
  1028. u.val = args[i].of.f32;
  1029. out_argv[p++] = u.part;
  1030. break;
  1031. }
  1032. case WASM_F64:
  1033. {
  1034. union { float64 val; uint32 parts[2]; } u;
  1035. u.val = args[i].of.f64;
  1036. out_argv[p++] = u.parts[0];
  1037. out_argv[p++] = u.parts[1];
  1038. break;
  1039. }
  1040. default:
  1041. bh_assert(0);
  1042. break;
  1043. }
  1044. }
  1045. return p;
  1046. }
  1047. static uint32
  1048. parse_uint32_array_to_results(WASMType *type,
  1049. uint32 argc, uint32 *argv,
  1050. wasm_val_t *out_results)
  1051. {
  1052. uint32 i, p;
  1053. for (i = 0, p = 0; i < type->result_count; i++) {
  1054. switch (type->types[type->param_count + i]) {
  1055. case VALUE_TYPE_I32:
  1056. out_results[i].kind = WASM_I32;
  1057. out_results[i].of.i32 = (int32)argv[p++];
  1058. break;
  1059. case VALUE_TYPE_I64:
  1060. {
  1061. union { uint64 val; uint32 parts[2]; } u;
  1062. u.parts[0] = argv[p++];
  1063. u.parts[1] = argv[p++];
  1064. out_results[i].kind = WASM_I64;
  1065. out_results[i].of.i64 = u.val;
  1066. break;
  1067. }
  1068. case VALUE_TYPE_F32:
  1069. {
  1070. union { float32 val; uint32 part; } u;
  1071. u.part = argv[p++];
  1072. out_results[i].kind = WASM_F32;
  1073. out_results[i].of.f32 = u.val;
  1074. break;
  1075. }
  1076. case VALUE_TYPE_F64:
  1077. {
  1078. union { float64 val; uint32 parts[2]; } u;
  1079. u.parts[0] = argv[p++];
  1080. u.parts[1] = argv[p++];
  1081. out_results[i].kind = WASM_F64;
  1082. out_results[i].of.f64 = u.val;
  1083. break;
  1084. }
  1085. default:
  1086. bh_assert(0);
  1087. break;
  1088. }
  1089. }
  1090. bh_assert(argc == p);
  1091. return type->result_count;
  1092. }
  1093. bool
  1094. wasm_runtime_call_wasm_a(WASMExecEnv *exec_env,
  1095. WASMFunctionInstanceCommon *function,
  1096. uint32 num_results, wasm_val_t results[],
  1097. uint32 num_args, wasm_val_t args[])
  1098. {
  1099. uint32 argc, *argv, ret_num, cell_num, total_size, module_type;
  1100. WASMType *type;
  1101. bool ret = false;
  1102. module_type = exec_env->module_inst->module_type;
  1103. type = wasm_runtime_get_function_type(function, module_type);
  1104. if (!type) {
  1105. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT must be enabled at least one.");
  1106. goto fail1;
  1107. }
  1108. argc = type->param_cell_num;
  1109. cell_num = (argc > type->ret_cell_num) ? argc : type->ret_cell_num;
  1110. if (num_results != type->result_count) {
  1111. LOG_ERROR("The result value number does not match the function declaration.");
  1112. goto fail1;
  1113. }
  1114. if (num_args != type->param_count) {
  1115. LOG_ERROR("The argument value number does not match the function declaration.");
  1116. goto fail1;
  1117. }
  1118. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  1119. if (!(argv = runtime_malloc((uint32)total_size, exec_env->module_inst, NULL, 0))) {
  1120. wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
  1121. goto fail1;
  1122. }
  1123. argc = parse_args_to_uint32_array(type, num_args, args, argv);
  1124. if (!(ret = wasm_runtime_call_wasm(exec_env, function, argc, argv)))
  1125. goto fail2;
  1126. ret_num = parse_uint32_array_to_results(type, type->ret_cell_num, argv, results);
  1127. bh_assert(ret_num == num_results);
  1128. (void)ret_num;
  1129. fail2:
  1130. wasm_runtime_free(argv);
  1131. fail1:
  1132. return ret;
  1133. }
  1134. bool
  1135. wasm_runtime_call_wasm_v(WASMExecEnv *exec_env,
  1136. WASMFunctionInstanceCommon *function,
  1137. uint32 num_results, wasm_val_t results[],
  1138. uint32 num_args, ...)
  1139. {
  1140. wasm_val_t *args = NULL;
  1141. WASMType *type = NULL;
  1142. bool ret = false;
  1143. uint32 i = 0, module_type;
  1144. va_list vargs;
  1145. module_type = exec_env->module_inst->module_type;
  1146. type = wasm_runtime_get_function_type(function, module_type);
  1147. if (!type) {
  1148. LOG_ERROR("Function type get failed, WAMR Interpreter and AOT "
  1149. "must be enabled at least one.");
  1150. goto fail1;
  1151. }
  1152. if (num_args != type->param_count) {
  1153. LOG_ERROR("The argument value number does not match the "
  1154. "function declaration.");
  1155. goto fail1;
  1156. }
  1157. if (!(args = runtime_malloc(sizeof(wasm_val_t) * num_args, NULL, NULL, 0))) {
  1158. wasm_runtime_set_exception(exec_env->module_inst, "allocate memory failed");
  1159. goto fail1;
  1160. }
  1161. va_start(vargs, num_args);
  1162. for (i = 0; i < num_args; i++) {
  1163. switch (type->types[i]) {
  1164. case VALUE_TYPE_I32:
  1165. args[i].kind = WASM_I32;
  1166. args[i].of.i32 = va_arg(vargs, uint32);
  1167. break;
  1168. case VALUE_TYPE_I64:
  1169. args[i].kind = WASM_I64;
  1170. args[i].of.i64 = va_arg(vargs, uint64);
  1171. break;
  1172. case VALUE_TYPE_F32:
  1173. args[i].kind = WASM_F32;
  1174. args[i].of.f32 = (float32)va_arg(vargs, float64);
  1175. break;
  1176. case VALUE_TYPE_F64:
  1177. args[i].kind = WASM_F64;
  1178. args[i].of.f64 = va_arg(vargs, float64);;
  1179. break;
  1180. default:
  1181. bh_assert(0);
  1182. break;
  1183. }
  1184. }
  1185. va_end(vargs);
  1186. ret = wasm_runtime_call_wasm_a(exec_env, function, num_results, results,
  1187. num_args, args);
  1188. wasm_runtime_free(args);
  1189. fail1:
  1190. return ret;
  1191. }
  1192. bool
  1193. wasm_runtime_create_exec_env_and_call_wasm(WASMModuleInstanceCommon *module_inst,
  1194. WASMFunctionInstanceCommon *function,
  1195. uint32 argc, uint32 argv[])
  1196. {
  1197. bool ret = false;
  1198. #if WASM_ENABLE_INTERP != 0
  1199. if (module_inst->module_type == Wasm_Module_Bytecode)
  1200. ret = wasm_create_exec_env_and_call_function(
  1201. (WASMModuleInstance *)module_inst, (WASMFunctionInstance *)function,
  1202. argc, argv);
  1203. #endif
  1204. #if WASM_ENABLE_AOT != 0
  1205. if (module_inst->module_type == Wasm_Module_AoT)
  1206. ret = aot_create_exec_env_and_call_function(
  1207. (AOTModuleInstance *)module_inst, (AOTFunctionInstance *)function,
  1208. argc, argv);
  1209. #endif
  1210. return ret;
  1211. }
  1212. void
  1213. wasm_runtime_set_exception(WASMModuleInstanceCommon *module_inst,
  1214. const char *exception)
  1215. {
  1216. #if WASM_ENABLE_INTERP != 0
  1217. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1218. wasm_set_exception((WASMModuleInstance*)module_inst, exception);
  1219. return;
  1220. }
  1221. #endif
  1222. #if WASM_ENABLE_AOT != 0
  1223. if (module_inst->module_type == Wasm_Module_AoT) {
  1224. aot_set_exception((AOTModuleInstance*)module_inst, exception);
  1225. return;
  1226. }
  1227. #endif
  1228. }
  1229. const char*
  1230. wasm_runtime_get_exception(WASMModuleInstanceCommon *module_inst)
  1231. {
  1232. #if WASM_ENABLE_INTERP != 0
  1233. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1234. return wasm_get_exception((WASMModuleInstance*)module_inst);
  1235. }
  1236. #endif
  1237. #if WASM_ENABLE_AOT != 0
  1238. if (module_inst->module_type == Wasm_Module_AoT) {
  1239. return aot_get_exception((AOTModuleInstance*)module_inst);
  1240. }
  1241. #endif
  1242. return NULL;
  1243. }
  1244. void
  1245. wasm_runtime_clear_exception(WASMModuleInstanceCommon *module_inst)
  1246. {
  1247. wasm_runtime_set_exception(module_inst, NULL);
  1248. }
  1249. void
  1250. wasm_runtime_set_custom_data(WASMModuleInstanceCommon *module_inst,
  1251. void *custom_data)
  1252. {
  1253. #if WASM_ENABLE_INTERP != 0
  1254. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1255. ((WASMModuleInstance*)module_inst)->custom_data = custom_data;
  1256. return;
  1257. }
  1258. #endif
  1259. #if WASM_ENABLE_AOT != 0
  1260. if (module_inst->module_type == Wasm_Module_AoT) {
  1261. ((AOTModuleInstance*)module_inst)->custom_data.ptr = custom_data;
  1262. return;
  1263. }
  1264. #endif
  1265. }
  1266. void*
  1267. wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
  1268. {
  1269. #if WASM_ENABLE_INTERP != 0
  1270. if (module_inst->module_type == Wasm_Module_Bytecode)
  1271. return ((WASMModuleInstance*)module_inst)->custom_data;
  1272. #endif
  1273. #if WASM_ENABLE_AOT != 0
  1274. if (module_inst->module_type == Wasm_Module_AoT)
  1275. return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
  1276. #endif
  1277. return NULL;
  1278. }
  1279. uint32
  1280. wasm_runtime_module_malloc(WASMModuleInstanceCommon *module_inst, uint32 size,
  1281. void **p_native_addr)
  1282. {
  1283. #if WASM_ENABLE_INTERP != 0
  1284. if (module_inst->module_type == Wasm_Module_Bytecode)
  1285. return wasm_module_malloc((WASMModuleInstance*)module_inst, size,
  1286. p_native_addr);
  1287. #endif
  1288. #if WASM_ENABLE_AOT != 0
  1289. if (module_inst->module_type == Wasm_Module_AoT)
  1290. return aot_module_malloc((AOTModuleInstance*)module_inst, size,
  1291. p_native_addr);
  1292. #endif
  1293. return 0;
  1294. }
  1295. uint32
  1296. wasm_runtime_module_realloc(WASMModuleInstanceCommon *module_inst, uint32 ptr,
  1297. uint32 size, void **p_native_addr)
  1298. {
  1299. #if WASM_ENABLE_INTERP != 0
  1300. if (module_inst->module_type == Wasm_Module_Bytecode)
  1301. return wasm_module_realloc((WASMModuleInstance*)module_inst, ptr,
  1302. size, p_native_addr);
  1303. #endif
  1304. #if WASM_ENABLE_AOT != 0
  1305. if (module_inst->module_type == Wasm_Module_AoT)
  1306. return aot_module_realloc((AOTModuleInstance*)module_inst, ptr,
  1307. size, p_native_addr);
  1308. #endif
  1309. return 0;
  1310. }
  1311. void
  1312. wasm_runtime_module_free(WASMModuleInstanceCommon *module_inst, uint32 ptr)
  1313. {
  1314. #if WASM_ENABLE_INTERP != 0
  1315. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1316. wasm_module_free((WASMModuleInstance*)module_inst, ptr);
  1317. return;
  1318. }
  1319. #endif
  1320. #if WASM_ENABLE_AOT != 0
  1321. if (module_inst->module_type == Wasm_Module_AoT) {
  1322. aot_module_free((AOTModuleInstance*)module_inst, ptr);
  1323. return;
  1324. }
  1325. #endif
  1326. }
  1327. uint32
  1328. wasm_runtime_module_dup_data(WASMModuleInstanceCommon *module_inst,
  1329. const char *src, uint32 size)
  1330. {
  1331. #if WASM_ENABLE_INTERP != 0
  1332. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1333. return wasm_module_dup_data((WASMModuleInstance*)module_inst, src, size);
  1334. }
  1335. #endif
  1336. #if WASM_ENABLE_AOT != 0
  1337. if (module_inst->module_type == Wasm_Module_AoT) {
  1338. return aot_module_dup_data((AOTModuleInstance*)module_inst, src, size);
  1339. }
  1340. #endif
  1341. return 0;
  1342. }
  1343. bool
  1344. wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst,
  1345. uint32 app_offset, uint32 size)
  1346. {
  1347. #if WASM_ENABLE_INTERP != 0
  1348. if (module_inst->module_type == Wasm_Module_Bytecode)
  1349. return wasm_validate_app_addr((WASMModuleInstance*)module_inst,
  1350. app_offset, size);
  1351. #endif
  1352. #if WASM_ENABLE_AOT != 0
  1353. if (module_inst->module_type == Wasm_Module_AoT)
  1354. return aot_validate_app_addr((AOTModuleInstance*)module_inst,
  1355. app_offset, size);
  1356. #endif
  1357. return false;
  1358. }
  1359. bool
  1360. wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst,
  1361. uint32 app_str_offset)
  1362. {
  1363. uint32 app_end_offset;
  1364. char *str, *str_end;
  1365. if (!wasm_runtime_get_app_addr_range(module_inst, app_str_offset,
  1366. NULL, &app_end_offset))
  1367. goto fail;
  1368. str = wasm_runtime_addr_app_to_native(module_inst, app_str_offset);
  1369. str_end = str + (app_end_offset - app_str_offset);
  1370. while (str < str_end && *str != '\0')
  1371. str++;
  1372. if (str == str_end)
  1373. goto fail;
  1374. return true;
  1375. fail:
  1376. wasm_runtime_set_exception(module_inst, "out of bounds memory access");
  1377. return false;
  1378. }
  1379. bool
  1380. wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst,
  1381. void *native_ptr, uint32 size)
  1382. {
  1383. #if WASM_ENABLE_INTERP != 0
  1384. if (module_inst->module_type == Wasm_Module_Bytecode)
  1385. return wasm_validate_native_addr((WASMModuleInstance*)module_inst,
  1386. native_ptr, size);
  1387. #endif
  1388. #if WASM_ENABLE_AOT != 0
  1389. if (module_inst->module_type == Wasm_Module_AoT)
  1390. return aot_validate_native_addr((AOTModuleInstance*)module_inst,
  1391. native_ptr, size);
  1392. #endif
  1393. return false;
  1394. }
  1395. void *
  1396. wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst,
  1397. uint32 app_offset)
  1398. {
  1399. #if WASM_ENABLE_INTERP != 0
  1400. if (module_inst->module_type == Wasm_Module_Bytecode)
  1401. return wasm_addr_app_to_native((WASMModuleInstance*)module_inst,
  1402. app_offset);
  1403. #endif
  1404. #if WASM_ENABLE_AOT != 0
  1405. if (module_inst->module_type == Wasm_Module_AoT)
  1406. return aot_addr_app_to_native((AOTModuleInstance*)module_inst,
  1407. app_offset);
  1408. #endif
  1409. return NULL;
  1410. }
  1411. uint32
  1412. wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst,
  1413. void *native_ptr)
  1414. {
  1415. #if WASM_ENABLE_INTERP != 0
  1416. if (module_inst->module_type == Wasm_Module_Bytecode)
  1417. return wasm_addr_native_to_app((WASMModuleInstance*)module_inst,
  1418. native_ptr);
  1419. #endif
  1420. #if WASM_ENABLE_AOT != 0
  1421. if (module_inst->module_type == Wasm_Module_AoT)
  1422. return aot_addr_native_to_app((AOTModuleInstance*)module_inst,
  1423. native_ptr);
  1424. #endif
  1425. return 0;
  1426. }
  1427. bool
  1428. wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst,
  1429. uint32 app_offset,
  1430. uint32 *p_app_start_offset,
  1431. uint32 *p_app_end_offset)
  1432. {
  1433. #if WASM_ENABLE_INTERP != 0
  1434. if (module_inst->module_type == Wasm_Module_Bytecode)
  1435. return wasm_get_app_addr_range((WASMModuleInstance*)module_inst,
  1436. app_offset, p_app_start_offset,
  1437. p_app_end_offset);
  1438. #endif
  1439. #if WASM_ENABLE_AOT != 0
  1440. if (module_inst->module_type == Wasm_Module_AoT)
  1441. return aot_get_app_addr_range((AOTModuleInstance*)module_inst,
  1442. app_offset, p_app_start_offset,
  1443. p_app_end_offset);
  1444. #endif
  1445. return false;
  1446. }
  1447. bool
  1448. wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst,
  1449. uint8 *native_ptr,
  1450. uint8 **p_native_start_addr,
  1451. uint8 **p_native_end_addr)
  1452. {
  1453. #if WASM_ENABLE_INTERP != 0
  1454. if (module_inst->module_type == Wasm_Module_Bytecode)
  1455. return wasm_get_native_addr_range((WASMModuleInstance*)module_inst,
  1456. native_ptr, p_native_start_addr,
  1457. p_native_end_addr);
  1458. #endif
  1459. #if WASM_ENABLE_AOT != 0
  1460. if (module_inst->module_type == Wasm_Module_AoT)
  1461. return aot_get_native_addr_range((AOTModuleInstance*)module_inst,
  1462. native_ptr, p_native_start_addr,
  1463. p_native_end_addr);
  1464. #endif
  1465. return false;
  1466. }
  1467. uint32
  1468. wasm_runtime_get_temp_ret(WASMModuleInstanceCommon *module_inst)
  1469. {
  1470. #if WASM_ENABLE_INTERP != 0
  1471. if (module_inst->module_type == Wasm_Module_Bytecode)
  1472. return ((WASMModuleInstance*)module_inst)->temp_ret;
  1473. #endif
  1474. #if WASM_ENABLE_AOT != 0
  1475. if (module_inst->module_type == Wasm_Module_AoT)
  1476. return ((AOTModuleInstance*)module_inst)->temp_ret;
  1477. #endif
  1478. return 0;
  1479. }
  1480. void
  1481. wasm_runtime_set_temp_ret(WASMModuleInstanceCommon *module_inst,
  1482. uint32 temp_ret)
  1483. {
  1484. #if WASM_ENABLE_INTERP != 0
  1485. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1486. ((WASMModuleInstance*)module_inst)->temp_ret = temp_ret;
  1487. return;
  1488. }
  1489. #endif
  1490. #if WASM_ENABLE_AOT != 0
  1491. if (module_inst->module_type == Wasm_Module_AoT) {
  1492. ((AOTModuleInstance*)module_inst)->temp_ret = temp_ret;
  1493. return;
  1494. }
  1495. #endif
  1496. }
  1497. uint32
  1498. wasm_runtime_get_llvm_stack(WASMModuleInstanceCommon *module_inst)
  1499. {
  1500. #if WASM_ENABLE_INTERP != 0
  1501. if (module_inst->module_type == Wasm_Module_Bytecode)
  1502. return ((WASMModuleInstance*)module_inst)->llvm_stack;
  1503. #endif
  1504. #if WASM_ENABLE_AOT != 0
  1505. if (module_inst->module_type == Wasm_Module_AoT)
  1506. return ((AOTModuleInstance*)module_inst)->llvm_stack;
  1507. #endif
  1508. return 0;
  1509. }
  1510. void
  1511. wasm_runtime_set_llvm_stack(WASMModuleInstanceCommon *module_inst,
  1512. uint32 llvm_stack)
  1513. {
  1514. #if WASM_ENABLE_INTERP != 0
  1515. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1516. ((WASMModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1517. return;
  1518. }
  1519. #endif
  1520. #if WASM_ENABLE_AOT != 0
  1521. if (module_inst->module_type == Wasm_Module_AoT) {
  1522. ((AOTModuleInstance*)module_inst)->llvm_stack = llvm_stack;
  1523. return;
  1524. }
  1525. #endif
  1526. }
  1527. bool
  1528. wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module,
  1529. uint32 inc_page_count)
  1530. {
  1531. #if WASM_ENABLE_INTERP != 0
  1532. if (module->module_type == Wasm_Module_Bytecode)
  1533. return wasm_enlarge_memory((WASMModuleInstance*)module,
  1534. inc_page_count);
  1535. #endif
  1536. #if WASM_ENABLE_AOT != 0
  1537. if (module->module_type == Wasm_Module_AoT)
  1538. return aot_enlarge_memory((AOTModuleInstance*)module,
  1539. inc_page_count);
  1540. #endif
  1541. return false;
  1542. }
  1543. #if WASM_ENABLE_LIBC_WASI != 0
  1544. void
  1545. wasm_runtime_set_wasi_args(WASMModuleCommon *module,
  1546. const char *dir_list[], uint32 dir_count,
  1547. const char *map_dir_list[], uint32 map_dir_count,
  1548. const char *env_list[], uint32 env_count,
  1549. char *argv[], int argc)
  1550. {
  1551. WASIArguments *wasi_args = NULL;
  1552. #if WASM_ENABLE_INTERP != 0 || WASM_ENABLE_JIT != 0
  1553. if (module->module_type == Wasm_Module_Bytecode)
  1554. wasi_args = &((WASMModule*)module)->wasi_args;
  1555. #endif
  1556. #if WASM_ENABLE_AOT != 0
  1557. if (module->module_type == Wasm_Module_AoT)
  1558. wasi_args = &((AOTModule*)module)->wasi_args;
  1559. #endif
  1560. if (wasi_args) {
  1561. wasi_args->dir_list = dir_list;
  1562. wasi_args->dir_count = dir_count;
  1563. wasi_args->map_dir_list = map_dir_list;
  1564. wasi_args->map_dir_count = map_dir_count;
  1565. wasi_args->env = env_list;
  1566. wasi_args->env_count = env_count;
  1567. wasi_args->argv = argv;
  1568. wasi_args->argc = (uint32)argc;
  1569. }
  1570. }
  1571. #if WASM_ENABLE_UVWASI == 0
  1572. bool
  1573. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1574. const char *dir_list[], uint32 dir_count,
  1575. const char *map_dir_list[], uint32 map_dir_count,
  1576. const char *env[], uint32 env_count,
  1577. char *argv[], uint32 argc,
  1578. char *error_buf, uint32 error_buf_size)
  1579. {
  1580. WASIContext *wasi_ctx;
  1581. char *argv_buf = NULL;
  1582. char **argv_list = NULL;
  1583. char *env_buf = NULL;
  1584. char **env_list = NULL;
  1585. uint64 argv_buf_size = 0, env_buf_size = 0, total_size;
  1586. uint32 argv_buf_offset = 0, env_buf_offset = 0;
  1587. struct fd_table *curfds = NULL;
  1588. struct fd_prestats *prestats = NULL;
  1589. struct argv_environ_values *argv_environ = NULL;
  1590. bool fd_table_inited = false, fd_prestats_inited = false;
  1591. bool argv_environ_inited = false;
  1592. __wasi_fd_t wasm_fd = 3;
  1593. int32 raw_fd;
  1594. char *path, resolved_path[PATH_MAX];
  1595. uint32 i;
  1596. if (!(wasi_ctx = runtime_malloc(sizeof(WASIContext), NULL,
  1597. error_buf, error_buf_size))) {
  1598. return false;
  1599. }
  1600. wasm_runtime_set_wasi_ctx(module_inst, wasi_ctx);
  1601. #if WASM_ENABLE_INTERP != 0
  1602. if (module_inst->module_type == Wasm_Module_Bytecode
  1603. && !((WASMModuleInstance*)module_inst)->default_memory)
  1604. return true;
  1605. #endif
  1606. #if WASM_ENABLE_AOT != 0
  1607. if (module_inst->module_type == Wasm_Module_AoT
  1608. && !((AOTModuleInstance*)module_inst)->
  1609. global_table_data.memory_instances[0].memory_data.ptr)
  1610. return true;
  1611. #endif
  1612. /* process argv[0], trip the path and suffix, only keep the program name */
  1613. for (i = 0; i < argc; i++)
  1614. argv_buf_size += strlen(argv[i]) + 1;
  1615. total_size = sizeof(char *) * (uint64)argc;
  1616. if (total_size >= UINT32_MAX
  1617. || (total_size > 0 &&
  1618. !(argv_list = wasm_runtime_malloc((uint32)total_size)))
  1619. || argv_buf_size >= UINT32_MAX
  1620. || (argv_buf_size > 0 &&
  1621. !(argv_buf = wasm_runtime_malloc((uint32)argv_buf_size)))) {
  1622. set_error_buf(error_buf, error_buf_size,
  1623. "Init wasi environment failed: allocate memory failed");
  1624. goto fail;
  1625. }
  1626. for (i = 0; i < argc; i++) {
  1627. argv_list[i] = argv_buf + argv_buf_offset;
  1628. bh_strcpy_s(argv_buf + argv_buf_offset,
  1629. (uint32)argv_buf_size - argv_buf_offset, argv[i]);
  1630. argv_buf_offset += (uint32)(strlen(argv[i]) + 1);
  1631. }
  1632. for (i = 0; i < env_count; i++)
  1633. env_buf_size += strlen(env[i]) + 1;
  1634. total_size = sizeof(char *) * (uint64)env_count;
  1635. if (total_size >= UINT32_MAX
  1636. || (total_size > 0
  1637. && !(env_list = wasm_runtime_malloc((uint32)total_size)))
  1638. || env_buf_size >= UINT32_MAX
  1639. || (env_buf_size > 0
  1640. && !(env_buf = wasm_runtime_malloc((uint32)env_buf_size)))) {
  1641. set_error_buf(error_buf, error_buf_size,
  1642. "Init wasi environment failed: allocate memory failed");
  1643. goto fail;
  1644. }
  1645. for (i = 0; i < env_count; i++) {
  1646. env_list[i] = env_buf + env_buf_offset;
  1647. bh_strcpy_s(env_buf + env_buf_offset,
  1648. (uint32)env_buf_size - env_buf_offset, env[i]);
  1649. env_buf_offset += (uint32)(strlen(env[i]) + 1);
  1650. }
  1651. if (!(curfds = wasm_runtime_malloc(sizeof(struct fd_table)))
  1652. || !(prestats = wasm_runtime_malloc(sizeof(struct fd_prestats)))
  1653. || !(argv_environ =
  1654. wasm_runtime_malloc(sizeof(struct argv_environ_values)))) {
  1655. set_error_buf(error_buf, error_buf_size,
  1656. "Init wasi environment failed: allocate memory failed");
  1657. goto fail;
  1658. }
  1659. if (!fd_table_init(curfds)) {
  1660. set_error_buf(error_buf, error_buf_size,
  1661. "Init wasi environment failed: "
  1662. "init fd table failed");
  1663. goto fail;
  1664. }
  1665. fd_table_inited = true;
  1666. if (!fd_prestats_init(prestats)) {
  1667. set_error_buf(error_buf, error_buf_size,
  1668. "Init wasi environment failed: "
  1669. "init fd prestats failed");
  1670. goto fail;
  1671. }
  1672. fd_prestats_inited = true;
  1673. if (!argv_environ_init(argv_environ,
  1674. argv_buf, argv_buf_size,
  1675. argv_list, argc,
  1676. env_buf, env_buf_size,
  1677. env_list, env_count)) {
  1678. set_error_buf(error_buf, error_buf_size,
  1679. "Init wasi environment failed: "
  1680. "init argument environment failed");
  1681. goto fail;
  1682. }
  1683. argv_environ_inited = true;
  1684. /* Prepopulate curfds with stdin, stdout, and stderr file descriptors. */
  1685. if (!fd_table_insert_existing(curfds, 0, 0)
  1686. || !fd_table_insert_existing(curfds, 1, 1)
  1687. || !fd_table_insert_existing(curfds, 2, 2)) {
  1688. set_error_buf(error_buf, error_buf_size,
  1689. "Init wasi environment failed: init fd table failed");
  1690. goto fail;
  1691. }
  1692. wasm_fd = 3;
  1693. for (i = 0; i < dir_count; i++, wasm_fd++) {
  1694. path = realpath(dir_list[i], resolved_path);
  1695. if (!path) {
  1696. if (error_buf)
  1697. snprintf(error_buf, error_buf_size,
  1698. "error while pre-opening directory %s: %d\n",
  1699. dir_list[i], errno);
  1700. goto fail;
  1701. }
  1702. raw_fd = open(path, O_RDONLY | O_DIRECTORY, 0);
  1703. if (raw_fd == -1) {
  1704. if (error_buf)
  1705. snprintf(error_buf, error_buf_size,
  1706. "error while pre-opening directory %s: %d\n",
  1707. dir_list[i], errno);
  1708. goto fail;
  1709. }
  1710. fd_table_insert_existing(curfds, wasm_fd, raw_fd);
  1711. fd_prestats_insert(prestats, dir_list[i], wasm_fd);
  1712. }
  1713. wasi_ctx->curfds = curfds;
  1714. wasi_ctx->prestats = prestats;
  1715. wasi_ctx->argv_environ = argv_environ;
  1716. wasi_ctx->argv_buf = argv_buf;
  1717. wasi_ctx->argv_list = argv_list;
  1718. wasi_ctx->env_buf = env_buf;
  1719. wasi_ctx->env_list = env_list;
  1720. return true;
  1721. fail:
  1722. if (argv_environ_inited)
  1723. argv_environ_destroy(argv_environ);
  1724. if (fd_prestats_inited)
  1725. fd_prestats_destroy(prestats);
  1726. if (fd_table_inited)
  1727. fd_table_destroy(curfds);
  1728. if (curfds)
  1729. wasm_runtime_free(curfds);
  1730. if (prestats)
  1731. wasm_runtime_free(prestats);
  1732. if (argv_environ)
  1733. wasm_runtime_free(argv_environ);
  1734. if (argv_buf)
  1735. wasm_runtime_free(argv_buf);
  1736. if (argv_list)
  1737. wasm_runtime_free(argv_list);
  1738. if (env_buf)
  1739. wasm_runtime_free(env_buf);
  1740. if (env_list)
  1741. wasm_runtime_free(env_list);
  1742. return false;
  1743. }
  1744. #else /* else of WASM_ENABLE_UVWASI == 0 */
  1745. static void *
  1746. wasm_uvwasi_malloc(size_t size, void *mem_user_data)
  1747. {
  1748. return runtime_malloc(size, NULL, NULL, 0);
  1749. (void)mem_user_data;
  1750. }
  1751. static void
  1752. wasm_uvwasi_free(void *ptr, void *mem_user_data)
  1753. {
  1754. if (ptr)
  1755. wasm_runtime_free(ptr);
  1756. (void)mem_user_data;
  1757. }
  1758. static void *
  1759. wasm_uvwasi_calloc(size_t nmemb, size_t size,
  1760. void *mem_user_data)
  1761. {
  1762. uint64 total_size = (uint64)nmemb * size;
  1763. return runtime_malloc(total_size, NULL, NULL, 0);
  1764. (void)mem_user_data;
  1765. }
  1766. static void *
  1767. wasm_uvwasi_realloc(void *ptr, size_t size,
  1768. void *mem_user_data)
  1769. {
  1770. if (size >= UINT32_MAX) {
  1771. return NULL;
  1772. }
  1773. return wasm_runtime_realloc(ptr, (uint32)size);
  1774. }
  1775. static uvwasi_mem_t uvwasi_allocator = {
  1776. .mem_user_data = 0,
  1777. .malloc = wasm_uvwasi_malloc,
  1778. .free = wasm_uvwasi_free,
  1779. .calloc = wasm_uvwasi_calloc,
  1780. .realloc = wasm_uvwasi_realloc
  1781. };
  1782. bool
  1783. wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
  1784. const char *dir_list[], uint32 dir_count,
  1785. const char *map_dir_list[], uint32 map_dir_count,
  1786. const char *env[], uint32 env_count,
  1787. char *argv[], uint32 argc,
  1788. char *error_buf, uint32 error_buf_size)
  1789. {
  1790. uvwasi_t *uvwasi = NULL;
  1791. uvwasi_options_t init_options;
  1792. const char **envp = NULL;
  1793. uint64 total_size;
  1794. uint32 i;
  1795. bool ret = false;
  1796. uvwasi = runtime_malloc(sizeof(uvwasi_t), module_inst,
  1797. error_buf, error_buf_size);
  1798. if (!uvwasi)
  1799. return false;
  1800. /* Setup the initialization options */
  1801. uvwasi_options_init(&init_options);
  1802. init_options.allocator = &uvwasi_allocator;
  1803. init_options.argc = argc;
  1804. init_options.argv = (const char **)argv;
  1805. if (dir_count > 0) {
  1806. init_options.preopenc = dir_count;
  1807. total_size = sizeof(uvwasi_preopen_t) * (uint64)init_options.preopenc;
  1808. init_options.preopens =
  1809. (uvwasi_preopen_t *)runtime_malloc(total_size, module_inst,
  1810. error_buf, error_buf_size);
  1811. if (init_options.preopens == NULL)
  1812. goto fail;
  1813. for (i = 0; i < init_options.preopenc; i++) {
  1814. init_options.preopens[i].real_path = dir_list[i];
  1815. init_options.preopens[i].mapped_path =
  1816. (i < map_dir_count) ? map_dir_list[i] : dir_list[i];
  1817. }
  1818. }
  1819. if (env_count > 0) {
  1820. total_size = sizeof(char *) * (uint64)(env_count + 1);
  1821. envp = runtime_malloc(total_size, module_inst,
  1822. error_buf, error_buf_size);
  1823. if (envp == NULL)
  1824. goto fail;
  1825. for (i = 0; i < env_count; i++) {
  1826. envp[i] = env[i];
  1827. }
  1828. envp[env_count] = NULL;
  1829. init_options.envp = envp;
  1830. }
  1831. if (UVWASI_ESUCCESS != uvwasi_init(uvwasi, &init_options)) {
  1832. set_error_buf(error_buf, error_buf_size, "uvwasi init failed");
  1833. goto fail;
  1834. }
  1835. wasm_runtime_set_wasi_ctx(module_inst, uvwasi);
  1836. ret = true;
  1837. fail:
  1838. if (envp)
  1839. wasm_runtime_free((void*)envp);
  1840. if (init_options.preopens)
  1841. wasm_runtime_free(init_options.preopens);
  1842. if (!ret && uvwasi)
  1843. wasm_runtime_free(uvwasi);
  1844. return ret;
  1845. }
  1846. #endif /* end of WASM_ENABLE_UVWASI */
  1847. bool
  1848. wasm_runtime_is_wasi_mode(WASMModuleInstanceCommon *module_inst)
  1849. {
  1850. #if WASM_ENABLE_INTERP != 0
  1851. if (module_inst->module_type == Wasm_Module_Bytecode
  1852. && ((WASMModuleInstance*)module_inst)->module->is_wasi_module)
  1853. return true;
  1854. #endif
  1855. #if WASM_ENABLE_AOT != 0
  1856. if (module_inst->module_type == Wasm_Module_AoT
  1857. && ((AOTModule*)((AOTModuleInstance*)module_inst)->aot_module.ptr)
  1858. ->is_wasi_module)
  1859. return true;
  1860. #endif
  1861. return false;
  1862. }
  1863. WASMFunctionInstanceCommon *
  1864. wasm_runtime_lookup_wasi_start_function(WASMModuleInstanceCommon *module_inst)
  1865. {
  1866. uint32 i;
  1867. #if WASM_ENABLE_INTERP != 0
  1868. if (module_inst->module_type == Wasm_Module_Bytecode) {
  1869. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  1870. WASMFunctionInstance *func;
  1871. for (i = 0; i < wasm_inst->export_func_count; i++) {
  1872. if (!strcmp(wasm_inst->export_functions[i].name, "_start")) {
  1873. func = wasm_inst->export_functions[i].function;
  1874. if (func->u.func->func_type->param_count != 0
  1875. || func->u.func->func_type->result_count != 0) {
  1876. LOG_ERROR("Lookup wasi _start function failed: "
  1877. "invalid function type.\n");
  1878. return NULL;
  1879. }
  1880. return (WASMFunctionInstanceCommon*)func;
  1881. }
  1882. }
  1883. return NULL;
  1884. }
  1885. #endif
  1886. #if WASM_ENABLE_AOT != 0
  1887. if (module_inst->module_type == Wasm_Module_AoT) {
  1888. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  1889. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  1890. aot_inst->export_funcs.ptr;
  1891. for (i = 0; i < aot_inst->export_func_count; i++) {
  1892. if (!strcmp(export_funcs[i].func_name, "_start")) {
  1893. AOTFuncType *func_type = export_funcs[i].u.func.func_type;
  1894. if (func_type->param_count != 0
  1895. || func_type->result_count != 0) {
  1896. LOG_ERROR("Lookup wasi _start function failed: "
  1897. "invalid function type.\n");
  1898. return NULL;
  1899. }
  1900. return (WASMFunctionInstanceCommon*)&export_funcs[i];
  1901. }
  1902. }
  1903. return NULL;
  1904. }
  1905. #endif /* end of WASM_ENABLE_AOT */
  1906. return NULL;
  1907. }
  1908. #if WASM_ENABLE_UVWASI == 0
  1909. void
  1910. wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
  1911. {
  1912. WASIContext *wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  1913. if (wasi_ctx) {
  1914. if (wasi_ctx->argv_environ) {
  1915. argv_environ_destroy(wasi_ctx->argv_environ);
  1916. wasm_runtime_free(wasi_ctx->argv_environ);
  1917. }
  1918. if (wasi_ctx->curfds) {
  1919. fd_table_destroy(wasi_ctx->curfds);
  1920. wasm_runtime_free(wasi_ctx->curfds);
  1921. }
  1922. if (wasi_ctx->prestats) {
  1923. fd_prestats_destroy(wasi_ctx->prestats);
  1924. wasm_runtime_free(wasi_ctx->prestats);
  1925. }
  1926. if (wasi_ctx->argv_buf)
  1927. wasm_runtime_free(wasi_ctx->argv_buf);
  1928. if (wasi_ctx->argv_list)
  1929. wasm_runtime_free(wasi_ctx->argv_list);
  1930. if (wasi_ctx->env_buf)
  1931. wasm_runtime_free(wasi_ctx->env_buf);
  1932. if (wasi_ctx->env_list)
  1933. wasm_runtime_free(wasi_ctx->env_list);
  1934. wasm_runtime_free(wasi_ctx);
  1935. }
  1936. }
  1937. #else
  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. uvwasi_destroy(wasi_ctx);
  1944. wasm_runtime_free(wasi_ctx);
  1945. }
  1946. }
  1947. #endif
  1948. WASIContext *
  1949. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst)
  1950. {
  1951. #if WASM_ENABLE_INTERP != 0
  1952. if (module_inst->module_type == Wasm_Module_Bytecode)
  1953. return ((WASMModuleInstance*)module_inst)->wasi_ctx;
  1954. #endif
  1955. #if WASM_ENABLE_AOT != 0
  1956. if (module_inst->module_type == Wasm_Module_AoT)
  1957. return ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr;
  1958. #endif
  1959. return NULL;
  1960. }
  1961. void
  1962. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst,
  1963. WASIContext *wasi_ctx)
  1964. {
  1965. #if WASM_ENABLE_INTERP != 0
  1966. if (module_inst->module_type == Wasm_Module_Bytecode)
  1967. ((WASMModuleInstance*)module_inst)->wasi_ctx = wasi_ctx;
  1968. #endif
  1969. #if WASM_ENABLE_AOT != 0
  1970. if (module_inst->module_type == Wasm_Module_AoT)
  1971. ((AOTModuleInstance*)module_inst)->wasi_ctx.ptr = wasi_ctx;
  1972. #endif
  1973. }
  1974. #endif /* end of WASM_ENABLE_LIBC_WASI */
  1975. WASMModuleCommon*
  1976. wasm_exec_env_get_module(WASMExecEnv *exec_env)
  1977. {
  1978. WASMModuleInstanceCommon *module_inst =
  1979. wasm_runtime_get_module_inst(exec_env);
  1980. #if WASM_ENABLE_INTERP != 0
  1981. if (module_inst->module_type == Wasm_Module_Bytecode)
  1982. return (WASMModuleCommon*)
  1983. ((WASMModuleInstance*)module_inst)->module;
  1984. #endif
  1985. #if WASM_ENABLE_AOT != 0
  1986. if (module_inst->module_type == Wasm_Module_AoT)
  1987. return (WASMModuleCommon*)
  1988. ((AOTModuleInstance*)module_inst)->aot_module.ptr;
  1989. #endif
  1990. return NULL;
  1991. }
  1992. /**
  1993. * Implementation of wasm_application_execute_main()
  1994. */
  1995. static WASMFunctionInstanceCommon*
  1996. resolve_function(const WASMModuleInstanceCommon *module_inst,
  1997. const char *name);
  1998. static bool
  1999. check_main_func_type(const WASMType *type)
  2000. {
  2001. if (!(type->param_count == 0 || type->param_count == 2)
  2002. ||type->result_count > 1) {
  2003. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  2004. return false;
  2005. }
  2006. if (type->param_count == 2
  2007. && !(type->types[0] == VALUE_TYPE_I32
  2008. && type->types[1] == VALUE_TYPE_I32)) {
  2009. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  2010. return false;
  2011. }
  2012. if (type->result_count
  2013. && type->types[type->param_count] != VALUE_TYPE_I32) {
  2014. LOG_ERROR("WASM execute application failed: invalid main function type.\n");
  2015. return false;
  2016. }
  2017. return true;
  2018. }
  2019. bool
  2020. wasm_application_execute_main(WASMModuleInstanceCommon *module_inst,
  2021. int32 argc, char *argv[])
  2022. {
  2023. WASMFunctionInstanceCommon *func;
  2024. WASMType *func_type = NULL;
  2025. uint32 argc1 = 0, argv1[2] = { 0 };
  2026. uint32 total_argv_size = 0;
  2027. uint64 total_size;
  2028. uint32 argv_buf_offset = 0;
  2029. int32 i;
  2030. char *argv_buf, *p, *p_end;
  2031. uint32 *argv_offsets, module_type;
  2032. bool ret, is_import_func = true;
  2033. #if WASM_ENABLE_LIBC_WASI != 0
  2034. if (wasm_runtime_is_wasi_mode(module_inst)) {
  2035. /* In wasi mode, we should call function named "_start"
  2036. which initializes the wasi envrionment and then calls
  2037. the actual main function. Directly call main function
  2038. may cause exception thrown. */
  2039. if ((func = wasm_runtime_lookup_wasi_start_function(module_inst)))
  2040. return wasm_runtime_create_exec_env_and_call_wasm(
  2041. module_inst, func, 0, NULL);
  2042. /* if no start function is found, we execute
  2043. the main function as normal */
  2044. }
  2045. #endif /* end of WASM_ENABLE_LIBC_WASI */
  2046. if (!(func = resolve_function(module_inst, "main"))
  2047. && !(func = resolve_function(module_inst, "__main_argc_argv"))
  2048. && !(func = resolve_function(module_inst, "_main"))) {
  2049. wasm_runtime_set_exception(module_inst,
  2050. "lookup main function failed");
  2051. return false;
  2052. }
  2053. #if WASM_ENABLE_INTERP != 0
  2054. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2055. is_import_func = ((WASMFunctionInstance*)func)->is_import_func;
  2056. }
  2057. #endif
  2058. #if WASM_ENABLE_AOT != 0
  2059. if (module_inst->module_type == Wasm_Module_AoT) {
  2060. is_import_func = ((AOTFunctionInstance*)func)->is_import_func;
  2061. }
  2062. #endif
  2063. if (is_import_func) {
  2064. wasm_runtime_set_exception(module_inst,
  2065. "lookup main function failed");
  2066. return false;
  2067. }
  2068. module_type = module_inst->module_type;
  2069. func_type = wasm_runtime_get_function_type(func, module_type);
  2070. if (!func_type) {
  2071. LOG_ERROR("invalid module instance type");
  2072. return false;
  2073. }
  2074. if (!check_main_func_type(func_type)) {
  2075. wasm_runtime_set_exception(module_inst,
  2076. "invalid function type of main function");
  2077. return false;
  2078. }
  2079. if (func_type->param_count) {
  2080. for (i = 0; i < argc; i++)
  2081. total_argv_size += (uint32)(strlen(argv[i]) + 1);
  2082. total_argv_size = align_uint(total_argv_size, 4);
  2083. total_size = (uint64)total_argv_size + sizeof(int32) * (uint64)argc;
  2084. if (total_size >= UINT32_MAX
  2085. || !(argv_buf_offset =
  2086. wasm_runtime_module_malloc(module_inst, (uint32)total_size,
  2087. (void**)&argv_buf))) {
  2088. wasm_runtime_set_exception(module_inst,
  2089. "allocate memory failed");
  2090. return false;
  2091. }
  2092. p = argv_buf;
  2093. argv_offsets = (uint32*)(p + total_argv_size);
  2094. p_end = p + total_size;
  2095. for (i = 0; i < argc; i++) {
  2096. bh_memcpy_s(p, (uint32)(p_end - p), argv[i], (uint32)(strlen(argv[i]) + 1));
  2097. argv_offsets[i] = argv_buf_offset + (uint32)(p - argv_buf);
  2098. p += strlen(argv[i]) + 1;
  2099. }
  2100. argc1 = 2;
  2101. argv1[0] = (uint32)argc;
  2102. argv1[1] = (uint32)wasm_runtime_addr_native_to_app(module_inst, argv_offsets);
  2103. }
  2104. ret = wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  2105. argc1, argv1);
  2106. if (argv_buf_offset)
  2107. wasm_runtime_module_free(module_inst, argv_buf_offset);
  2108. return ret;
  2109. }
  2110. #if WASM_ENABLE_MULTI_MODULE != 0
  2111. static WASMModuleInstance *
  2112. get_sub_module_inst(const WASMModuleInstance *parent_module_inst,
  2113. const char *sub_module_name)
  2114. {
  2115. WASMSubModInstNode *node =
  2116. bh_list_first_elem(parent_module_inst->sub_module_inst_list);
  2117. while (node && strcmp(node->module_name, sub_module_name)) {
  2118. node = bh_list_elem_next(node);
  2119. }
  2120. return node ? node->module_inst : NULL;
  2121. }
  2122. static bool
  2123. parse_function_name(char *orig_function_name, char **p_module_name,
  2124. char **p_function_name)
  2125. {
  2126. if (orig_function_name[0] != '$') {
  2127. *p_module_name = NULL;
  2128. *p_function_name = orig_function_name;
  2129. return true;
  2130. }
  2131. /**
  2132. * $module_name$function_name\0
  2133. * ===>
  2134. * module_name\0function_name\0
  2135. * ===>
  2136. * module_name
  2137. * function_name
  2138. */
  2139. char *p1 = orig_function_name;
  2140. char *p2 = strchr(p1 + 1, '$');
  2141. if (!p2) {
  2142. LOG_DEBUG("can not parse the incoming function name");
  2143. return false;
  2144. }
  2145. *p_module_name = p1 + 1;
  2146. *p2 = '\0';
  2147. *p_function_name = p2 + 1;
  2148. return strlen(*p_module_name) && strlen(*p_function_name);
  2149. }
  2150. #endif
  2151. /**
  2152. * Implementation of wasm_application_execute_func()
  2153. */
  2154. static WASMFunctionInstanceCommon*
  2155. resolve_function(const WASMModuleInstanceCommon *module_inst,
  2156. const char *name)
  2157. {
  2158. uint32 i = 0;
  2159. WASMFunctionInstanceCommon *ret = NULL;
  2160. #if WASM_ENABLE_MULTI_MODULE != 0
  2161. WASMModuleInstance *sub_module_inst = NULL;
  2162. char *orig_name = NULL;
  2163. char *sub_module_name = NULL;
  2164. char *function_name = NULL;
  2165. uint32 length = (uint32)(strlen(name) + 1);
  2166. orig_name = runtime_malloc(sizeof(char) * length, NULL, NULL, 0);
  2167. if (!orig_name) {
  2168. return NULL;
  2169. }
  2170. strncpy(orig_name, name, length);
  2171. if (!parse_function_name(orig_name, &sub_module_name, &function_name)) {
  2172. goto LEAVE;
  2173. }
  2174. LOG_DEBUG("%s -> %s and %s", name, sub_module_name, function_name);
  2175. if (sub_module_name) {
  2176. sub_module_inst = get_sub_module_inst(
  2177. (WASMModuleInstance *)module_inst, sub_module_name);
  2178. if (!sub_module_inst) {
  2179. LOG_DEBUG("can not find a sub module named %s", sub_module_name);
  2180. goto LEAVE;
  2181. }
  2182. }
  2183. #else
  2184. const char *function_name = name;
  2185. #endif
  2186. #if WASM_ENABLE_INTERP != 0
  2187. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2188. WASMModuleInstance *wasm_inst = (WASMModuleInstance*)module_inst;
  2189. #if WASM_ENABLE_MULTI_MODULE != 0
  2190. wasm_inst = sub_module_inst ? sub_module_inst : wasm_inst;
  2191. #endif /* WASM_ENABLE_MULTI_MODULE */
  2192. for (i = 0; i < wasm_inst->export_func_count; i++) {
  2193. if (!strcmp(wasm_inst->export_functions[i].name, function_name)) {
  2194. ret = wasm_inst->export_functions[i].function;
  2195. break;
  2196. }
  2197. }
  2198. }
  2199. #endif /* WASM_ENABLE_INTERP */
  2200. #if WASM_ENABLE_AOT != 0
  2201. if (module_inst->module_type == Wasm_Module_AoT) {
  2202. AOTModuleInstance *aot_inst = (AOTModuleInstance*)module_inst;
  2203. AOTFunctionInstance *export_funcs = (AOTFunctionInstance *)
  2204. aot_inst->export_funcs.ptr;
  2205. for (i = 0; i < aot_inst->export_func_count; i++) {
  2206. if (!strcmp(export_funcs[i].func_name, function_name)) {
  2207. ret = &export_funcs[i];
  2208. break;
  2209. }
  2210. }
  2211. }
  2212. #endif
  2213. #if WASM_ENABLE_MULTI_MODULE != 0
  2214. LEAVE:
  2215. wasm_runtime_free(orig_name);
  2216. #endif
  2217. return ret;
  2218. }
  2219. union ieee754_float {
  2220. float f;
  2221. /* This is the IEEE 754 single-precision format. */
  2222. union {
  2223. struct {
  2224. unsigned int negative:1;
  2225. unsigned int exponent:8;
  2226. unsigned int mantissa:23;
  2227. } ieee_big_endian;
  2228. struct {
  2229. unsigned int mantissa:23;
  2230. unsigned int exponent:8;
  2231. unsigned int negative:1;
  2232. } ieee_little_endian;
  2233. } ieee;
  2234. };
  2235. union ieee754_double {
  2236. double d;
  2237. /* This is the IEEE 754 double-precision format. */
  2238. union {
  2239. struct {
  2240. unsigned int negative:1;
  2241. unsigned int exponent:11;
  2242. /* Together these comprise the mantissa. */
  2243. unsigned int mantissa0:20;
  2244. unsigned int mantissa1:32;
  2245. } ieee_big_endian;
  2246. struct {
  2247. /* Together these comprise the mantissa. */
  2248. unsigned int mantissa1:32;
  2249. unsigned int mantissa0:20;
  2250. unsigned int exponent:11;
  2251. unsigned int negative:1;
  2252. } ieee_little_endian;
  2253. } ieee;
  2254. };
  2255. static union {
  2256. int a;
  2257. char b;
  2258. } __ue = { .a = 1 };
  2259. #define is_little_endian() (__ue.b == 1)
  2260. bool
  2261. wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
  2262. const char *name, int32 argc, char *argv[])
  2263. {
  2264. WASMFunctionInstanceCommon *func;
  2265. WASMType *type = NULL;
  2266. uint32 argc1, *argv1 = NULL, cell_num = 0, j, k = 0;
  2267. int32 i, p, module_type;
  2268. uint64 total_size;
  2269. const char *exception;
  2270. char buf[128];
  2271. bh_assert(argc >= 0);
  2272. LOG_DEBUG("call a function \"%s\" with %d arguments", name, argc);
  2273. func = resolve_function(module_inst, name);
  2274. if (!func) {
  2275. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  2276. wasm_runtime_set_exception(module_inst, buf);
  2277. goto fail;
  2278. }
  2279. #if WASM_ENABLE_INTERP != 0
  2280. if (module_inst->module_type == Wasm_Module_Bytecode) {
  2281. WASMFunctionInstance *wasm_func = (WASMFunctionInstance*)func;
  2282. if (wasm_func->is_import_func
  2283. #if WASM_ENABLE_MULTI_MODULE != 0
  2284. && !wasm_func->import_func_inst
  2285. #endif
  2286. ) {
  2287. snprintf(buf, sizeof(buf), "lookup function %s failed", name);
  2288. wasm_runtime_set_exception(module_inst, buf);
  2289. goto fail;
  2290. }
  2291. }
  2292. #endif
  2293. module_type = module_inst->module_type;
  2294. type = wasm_runtime_get_function_type(func, module_type);
  2295. if (!type) {
  2296. LOG_ERROR("invalid module instance type");
  2297. return false;
  2298. }
  2299. if (type->param_count != (uint32)argc) {
  2300. wasm_runtime_set_exception(module_inst,
  2301. "invalid input argument count");
  2302. goto fail;
  2303. }
  2304. argc1 = type->param_cell_num;
  2305. cell_num = (argc1 > type->ret_cell_num) ? argc1 : type->ret_cell_num;
  2306. total_size = sizeof(uint32) * (uint64)(cell_num > 2 ? cell_num : 2);
  2307. if ((!(argv1 = runtime_malloc((uint32)total_size, module_inst,
  2308. NULL, 0)))) {
  2309. goto fail;
  2310. }
  2311. /* Parse arguments */
  2312. for (i = 0, p = 0; i < argc; i++) {
  2313. char *endptr = NULL;
  2314. bh_assert(argv[i] != NULL);
  2315. if (argv[i][0] == '\0') {
  2316. snprintf(buf, sizeof(buf), "invalid input argument %d", i);
  2317. wasm_runtime_set_exception(module_inst, buf);
  2318. goto fail;
  2319. }
  2320. switch (type->types[i]) {
  2321. case VALUE_TYPE_I32:
  2322. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  2323. break;
  2324. case VALUE_TYPE_I64:
  2325. {
  2326. union { uint64 val; uint32 parts[2]; } u;
  2327. u.val = strtoull(argv[i], &endptr, 0);
  2328. argv1[p++] = u.parts[0];
  2329. argv1[p++] = u.parts[1];
  2330. break;
  2331. }
  2332. case VALUE_TYPE_F32:
  2333. {
  2334. float32 f32 = strtof(argv[i], &endptr);
  2335. if (isnan(f32)) {
  2336. if (argv[i][0] == '-') {
  2337. union ieee754_float u;
  2338. u.f = f32;
  2339. if (is_little_endian())
  2340. u.ieee.ieee_little_endian.negative = 1;
  2341. else
  2342. u.ieee.ieee_big_endian.negative = 1;
  2343. memcpy(&f32, &u.f, sizeof(float));
  2344. }
  2345. if (endptr[0] == ':') {
  2346. uint32 sig;
  2347. union ieee754_float u;
  2348. sig = (uint32)strtoul(endptr + 1, &endptr, 0);
  2349. u.f = f32;
  2350. if (is_little_endian())
  2351. u.ieee.ieee_little_endian.mantissa = sig;
  2352. else
  2353. u.ieee.ieee_big_endian.mantissa = sig;
  2354. memcpy(&f32, &u.f, sizeof(float));
  2355. }
  2356. }
  2357. memcpy(&argv1[p++], &f32, sizeof(float));
  2358. break;
  2359. }
  2360. case VALUE_TYPE_F64:
  2361. {
  2362. union { float64 val; uint32 parts[2]; } u;
  2363. u.val = strtod(argv[i], &endptr);
  2364. if (isnan(u.val)) {
  2365. if (argv[i][0] == '-') {
  2366. union ieee754_double ud;
  2367. ud.d = u.val;
  2368. if (is_little_endian())
  2369. ud.ieee.ieee_little_endian.negative = 1;
  2370. else
  2371. ud.ieee.ieee_big_endian.negative = 1;
  2372. memcpy(&u.val, &ud.d, sizeof(double));
  2373. }
  2374. if (endptr[0] == ':') {
  2375. uint64 sig;
  2376. union ieee754_double ud;
  2377. sig = strtoull(endptr + 1, &endptr, 0);
  2378. ud.d = u.val;
  2379. if (is_little_endian()) {
  2380. ud.ieee.ieee_little_endian.mantissa0 = sig >> 32;
  2381. ud.ieee.ieee_little_endian.mantissa1 = (uint32)sig;
  2382. }
  2383. else {
  2384. ud.ieee.ieee_big_endian.mantissa0 = sig >> 32;
  2385. ud.ieee.ieee_big_endian.mantissa1 = (uint32)sig;
  2386. }
  2387. memcpy(&u.val, &ud.d, sizeof(double));
  2388. }
  2389. }
  2390. argv1[p++] = u.parts[0];
  2391. argv1[p++] = u.parts[1];
  2392. break;
  2393. }
  2394. #if WASM_ENABLE_SIMD != 0
  2395. case VALUE_TYPE_V128:
  2396. {
  2397. /* it likes 0x123\0x234 or 123\234 */
  2398. /* retrive first i64 */
  2399. *(uint64*)(argv1 + p) = strtoull(argv[i], &endptr, 0);
  2400. /* skip \ */
  2401. endptr++;
  2402. /* retrive second i64 */
  2403. *(uint64*)(argv1 + p + 2) = strtoull(endptr, &endptr, 0);
  2404. p += 4;
  2405. break;
  2406. }
  2407. #endif /* WASM_ENABLE_SIMD != 0 */
  2408. #if WASM_ENABLE_REF_TYPES != 0
  2409. case VALUE_TYPE_FUNCREF:
  2410. {
  2411. if (strncmp(argv[i], "null", 4) == 0
  2412. || strncmp(argv[i], "NULL", 4) == 0) {
  2413. argv1[p++] = NULL_REF;
  2414. }
  2415. else {
  2416. argv1[p++] = (uint32)strtoul(argv[i], &endptr, 0);
  2417. }
  2418. break;
  2419. }
  2420. case VALUE_TYPE_EXTERNREF:
  2421. {
  2422. if (strncmp(argv[i], "null", 4) == 0
  2423. || strncmp(argv[i], "NULL", 4) == 0) {
  2424. argv1[p++] = NULL_REF;
  2425. }
  2426. else {
  2427. uint64 val = strtoull(argv[i], &endptr, 0);
  2428. void *extern_obj = (void *)(uintptr_t)val;
  2429. uint32 externref_idx;
  2430. if (!wasm_externref_obj2ref(module_inst, extern_obj,
  2431. &externref_idx)) {
  2432. wasm_runtime_set_exception(
  2433. module_inst, "map extern object to ref failed");
  2434. goto fail;
  2435. }
  2436. argv1[p++] = externref_idx;
  2437. }
  2438. break;
  2439. }
  2440. #endif /* WASM_ENABLE_REF_TYPES */
  2441. default:
  2442. bh_assert(0);
  2443. break;
  2444. }
  2445. if (endptr && *endptr != '\0' && *endptr != '_') {
  2446. snprintf(buf, sizeof(buf), "invalid input argument %d: %s",
  2447. i, argv[i]);
  2448. wasm_runtime_set_exception(module_inst, buf);
  2449. goto fail;
  2450. }
  2451. }
  2452. bh_assert(p == (int32)argc1);
  2453. wasm_runtime_set_exception(module_inst, NULL);
  2454. if (!wasm_runtime_create_exec_env_and_call_wasm(module_inst, func,
  2455. argc1, argv1)) {
  2456. goto fail;
  2457. }
  2458. /* print return value */
  2459. for (j = 0; j < type->result_count; j++) {
  2460. switch (type->types[type->param_count + j]) {
  2461. case VALUE_TYPE_I32:
  2462. {
  2463. os_printf("0x%x:i32", argv1[k]);
  2464. k++;
  2465. break;
  2466. }
  2467. case VALUE_TYPE_I64:
  2468. {
  2469. union { uint64 val; uint32 parts[2]; } u;
  2470. u.parts[0] = argv1[k];
  2471. u.parts[1] = argv1[k + 1];
  2472. k += 2;
  2473. #ifdef PRIx64
  2474. os_printf("0x%"PRIx64":i64", u.val);
  2475. #else
  2476. char buf[16];
  2477. if (sizeof(long) == 4)
  2478. snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
  2479. else
  2480. snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
  2481. os_printf(buf, u.val);
  2482. #endif
  2483. break;
  2484. }
  2485. case VALUE_TYPE_F32:
  2486. {
  2487. os_printf("%.7g:f32", *(float32*)(argv1 + k));
  2488. k++;
  2489. break;
  2490. }
  2491. case VALUE_TYPE_F64:
  2492. {
  2493. union { float64 val; uint32 parts[2]; } u;
  2494. u.parts[0] = argv1[k];
  2495. u.parts[1] = argv1[k + 1];
  2496. k += 2;
  2497. os_printf("%.7g:f64", u.val);
  2498. break;
  2499. }
  2500. #if WASM_ENABLE_REF_TYPES
  2501. case VALUE_TYPE_FUNCREF:
  2502. {
  2503. if (argv1[k] != NULL_REF)
  2504. os_printf("%u:ref.func", argv1[k]);
  2505. else
  2506. os_printf("func:ref.null");
  2507. k++;
  2508. break;
  2509. }
  2510. case VALUE_TYPE_EXTERNREF:
  2511. {
  2512. if (argv1[k] != NULL_REF) {
  2513. void *extern_obj = NULL;
  2514. bool ret = wasm_externref_ref2obj(argv1[k], &extern_obj);
  2515. bh_assert(ret);
  2516. (void)ret;
  2517. os_printf("%p:ref.extern", extern_obj);
  2518. }
  2519. else
  2520. os_printf("extern:ref.null");
  2521. k++;
  2522. break;
  2523. }
  2524. #endif
  2525. #if WASM_ENABLE_SIMD != 0
  2526. case VALUE_TYPE_V128:
  2527. {
  2528. uint64 *v = (uint64*)(argv1 + k);
  2529. #if defined(PRIx64)
  2530. os_printf("<0x%016"PRIx64" 0x%016"PRIx64">:v128", *v, *(v + 1));
  2531. #else
  2532. if (4 == sizeof(long)) {
  2533. os_printf("<0x%016llx 0x%016llx>:v128", *v, *(v + 1));
  2534. }
  2535. else {
  2536. os_printf("<0x%016lx 0x%016lx>:v128", *v, *(v + 1));
  2537. }
  2538. #endif /* PRIx64 */
  2539. k += 4;
  2540. break;
  2541. }
  2542. #endif /* WASM_ENABLE_SIMD != 0 */
  2543. default:
  2544. bh_assert(0);
  2545. break;
  2546. }
  2547. if (j < (uint32)(type->result_count - 1))
  2548. os_printf(",");
  2549. }
  2550. os_printf("\n");
  2551. wasm_runtime_free(argv1);
  2552. return true;
  2553. fail:
  2554. if (argv1)
  2555. wasm_runtime_free(argv1);
  2556. exception = wasm_runtime_get_exception(module_inst);
  2557. bh_assert(exception);
  2558. os_printf("%s\n", exception);
  2559. return false;
  2560. }
  2561. bool
  2562. wasm_runtime_register_natives(const char *module_name,
  2563. NativeSymbol *native_symbols,
  2564. uint32 n_native_symbols)
  2565. {
  2566. return wasm_native_register_natives(module_name,
  2567. native_symbols, n_native_symbols);
  2568. }
  2569. bool
  2570. wasm_runtime_register_natives_raw(const char *module_name,
  2571. NativeSymbol *native_symbols,
  2572. uint32 n_native_symbols)
  2573. {
  2574. return wasm_native_register_natives_raw(module_name,
  2575. native_symbols, n_native_symbols);
  2576. }
  2577. bool
  2578. wasm_runtime_invoke_native_raw(WASMExecEnv *exec_env, void *func_ptr,
  2579. const WASMType *func_type, const char *signature,
  2580. void *attachment,
  2581. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2582. {
  2583. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2584. typedef void (*NativeRawFuncPtr)(WASMExecEnv*, uint64*);
  2585. NativeRawFuncPtr invokeNativeRaw = (NativeRawFuncPtr)func_ptr;
  2586. uint64 argv_buf[16] = { 0 }, *argv1 = argv_buf, *argv_dst, size;
  2587. uint32 *argv_src = argv, i, argc1, ptr_len;
  2588. uint32 arg_i32;
  2589. bool ret = false;
  2590. argc1 = func_type->param_count;
  2591. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  2592. size = sizeof(uint64) * (uint64)argc1;
  2593. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2594. NULL, 0))) {
  2595. return false;
  2596. }
  2597. }
  2598. argv_dst = argv1;
  2599. /* Traverse secondly to fill in each argument */
  2600. for (i = 0; i < func_type->param_count; i++, argv_dst++) {
  2601. switch (func_type->types[i]) {
  2602. case VALUE_TYPE_I32:
  2603. {
  2604. *(uint32*)argv_dst = arg_i32 = *argv_src++;
  2605. if (signature) {
  2606. if (signature[i + 1] == '*') {
  2607. /* param is a pointer */
  2608. if (signature[i + 2] == '~')
  2609. /* pointer with length followed */
  2610. ptr_len = *argv_src;
  2611. else
  2612. /* pointer without length followed */
  2613. ptr_len = 1;
  2614. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2615. goto fail;
  2616. *(uintptr_t*)argv_dst = (uintptr_t)
  2617. wasm_runtime_addr_app_to_native(module, arg_i32);
  2618. }
  2619. else if (signature[i + 1] == '$') {
  2620. /* param is a string */
  2621. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2622. goto fail;
  2623. *(uintptr_t*)argv_dst = (uintptr_t)
  2624. wasm_runtime_addr_app_to_native(module, arg_i32);
  2625. }
  2626. }
  2627. break;
  2628. }
  2629. case VALUE_TYPE_I64:
  2630. case VALUE_TYPE_F64:
  2631. bh_memcpy_s(argv_dst, sizeof(uint64), argv_src, sizeof(uint32) * 2);
  2632. argv_src += 2;
  2633. break;
  2634. case VALUE_TYPE_F32:
  2635. *(float32*)argv_dst = *(float32*)argv_src++;
  2636. break;
  2637. #if WASM_ENABLE_REF_TYPES != 0
  2638. case VALUE_TYPE_FUNCREF:
  2639. case VALUE_TYPE_EXTERNREF:
  2640. *(uint32*)argv_dst = *argv_src++;
  2641. break;
  2642. #endif
  2643. default:
  2644. bh_assert(0);
  2645. break;
  2646. }
  2647. }
  2648. exec_env->attachment = attachment;
  2649. invokeNativeRaw(exec_env, argv1);
  2650. exec_env->attachment = NULL;
  2651. if (func_type->result_count > 0) {
  2652. switch (func_type->types[func_type->param_count]) {
  2653. case VALUE_TYPE_I32:
  2654. #if WASM_ENABLE_REF_TYPES != 0
  2655. case VALUE_TYPE_FUNCREF:
  2656. case VALUE_TYPE_EXTERNREF:
  2657. #endif
  2658. argv_ret[0] = *(uint32*)argv1;
  2659. break;
  2660. case VALUE_TYPE_F32:
  2661. *(float32*)argv_ret = *(float32*)argv1;
  2662. break;
  2663. case VALUE_TYPE_I64:
  2664. case VALUE_TYPE_F64:
  2665. bh_memcpy_s(argv_ret, sizeof(uint32) * 2, argv1, sizeof(uint64));
  2666. break;
  2667. default:
  2668. bh_assert(0);
  2669. break;
  2670. }
  2671. }
  2672. ret = !wasm_runtime_get_exception(module) ? true : false;
  2673. fail:
  2674. if (argv1 != argv_buf)
  2675. wasm_runtime_free(argv1);
  2676. return ret;
  2677. }
  2678. /**
  2679. * Implementation of wasm_runtime_invoke_native()
  2680. */
  2681. /* The invoke native implementation on ARM platform with VFP co-processor */
  2682. #if defined(BUILD_TARGET_ARM_VFP) \
  2683. || defined(BUILD_TARGET_THUMB_VFP) \
  2684. || defined(BUILD_TARGET_RISCV32_ILP32D) \
  2685. || defined(BUILD_TARGET_RISCV32_ILP32)
  2686. typedef void (*GenericFunctionPointer)();
  2687. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 n_stacks);
  2688. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2689. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2690. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint32*,uint32);
  2691. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2692. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint32*, uint32);
  2693. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  2694. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  2695. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  2696. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  2697. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  2698. #if !defined(BUILD_TARGET_RISCV32_ILP32D) \
  2699. && !defined(BUILD_TARGET_RISCV32_ILP32)
  2700. #define MAX_REG_INTS 4
  2701. #define MAX_REG_FLOATS 16
  2702. #else
  2703. #define MAX_REG_INTS 8
  2704. #define MAX_REG_FLOATS 8
  2705. #endif
  2706. bool
  2707. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  2708. const WASMType *func_type, const char *signature,
  2709. void *attachment,
  2710. uint32 *argv, uint32 argc, uint32 *argv_ret)
  2711. {
  2712. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  2713. /* argv buf layout: int args(fix cnt) + float args(fix cnt) + stack args */
  2714. uint32 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size;
  2715. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  2716. uint32 arg_i32, ptr_len;
  2717. uint32 result_count = func_type->result_count;
  2718. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  2719. bool ret = false;
  2720. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2721. uint32 *fps;
  2722. int n_fps = 0;
  2723. #else
  2724. #define fps ints
  2725. #define n_fps n_ints
  2726. #endif
  2727. n_ints++; /* exec env */
  2728. /* Traverse firstly to calculate stack args count */
  2729. for (i = 0; i < func_type->param_count; i++) {
  2730. switch (func_type->types[i]) {
  2731. case VALUE_TYPE_I32:
  2732. #if WASM_ENABLE_REF_TYPES != 0
  2733. case VALUE_TYPE_FUNCREF:
  2734. case VALUE_TYPE_EXTERNREF:
  2735. #endif
  2736. if (n_ints < MAX_REG_INTS)
  2737. n_ints++;
  2738. else
  2739. n_stacks++;
  2740. break;
  2741. case VALUE_TYPE_I64:
  2742. if (n_ints < MAX_REG_INTS - 1) {
  2743. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2744. /* 64-bit data must be 8 bytes aligned in arm */
  2745. if (n_ints & 1)
  2746. n_ints++;
  2747. #endif
  2748. n_ints += 2;
  2749. }
  2750. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
  2751. /* part in register, part in stack */
  2752. else if (n_ints == MAX_REG_INTS - 1) {
  2753. n_ints++;
  2754. n_stacks++;
  2755. }
  2756. #endif
  2757. else {
  2758. /* 64-bit data in stack must be 8 bytes aligned
  2759. in arm and riscv32 */
  2760. if (n_stacks & 1)
  2761. n_stacks++;
  2762. n_stacks += 2;
  2763. }
  2764. break;
  2765. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2766. case VALUE_TYPE_F32:
  2767. if (n_fps < MAX_REG_FLOATS)
  2768. n_fps++;
  2769. else
  2770. n_stacks++;
  2771. break;
  2772. case VALUE_TYPE_F64:
  2773. if (n_fps < MAX_REG_FLOATS - 1) {
  2774. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2775. /* 64-bit data must be 8 bytes aligned in arm */
  2776. if (n_fps & 1)
  2777. n_fps++;
  2778. #endif
  2779. n_fps += 2;
  2780. }
  2781. #if defined(BUILD_TARGET_RISCV32_ILP32)
  2782. else if (n_fps == MAX_REG_FLOATS - 1) {
  2783. n_fps++;
  2784. n_stacks++;
  2785. }
  2786. #endif
  2787. else {
  2788. /* 64-bit data must be 8 bytes aligned in arm */
  2789. if (n_stacks & 1)
  2790. n_stacks++;
  2791. n_stacks += 2;
  2792. }
  2793. break;
  2794. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2795. case VALUE_TYPE_F32:
  2796. case VALUE_TYPE_F64:
  2797. if (n_fps < MAX_REG_FLOATS) {
  2798. n_fps++;
  2799. }
  2800. else if (func_type->types[i] == VALUE_TYPE_F32
  2801. && n_ints < MAX_REG_INTS) {
  2802. /* use int reg firstly if available */
  2803. n_ints++;
  2804. }
  2805. else if (func_type->types[i] == VALUE_TYPE_F64
  2806. && n_ints < MAX_REG_INTS - 1) {
  2807. /* use int regs firstly if available */
  2808. if (n_ints & 1)
  2809. n_ints++;
  2810. ints += 2;
  2811. }
  2812. else {
  2813. /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
  2814. if (n_stacks & 1)
  2815. n_stacks++;
  2816. n_stacks += 2;
  2817. }
  2818. break;
  2819. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  2820. default:
  2821. bh_assert(0);
  2822. break;
  2823. }
  2824. }
  2825. for (i = 0; i < ext_ret_count; i++) {
  2826. if (n_ints < MAX_REG_INTS)
  2827. n_ints++;
  2828. else
  2829. n_stacks++;
  2830. }
  2831. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2832. argc1 = MAX_REG_INTS + MAX_REG_FLOATS + n_stacks;
  2833. #elif defined(BUILD_TARGET_RISCV32_ILP32)
  2834. argc1 = MAX_REG_INTS + n_stacks;
  2835. #else
  2836. argc1 = MAX_REG_INTS + MAX_REG_FLOATS * 2 + n_stacks;
  2837. #endif
  2838. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  2839. size = sizeof(uint32) * (uint32)argc1;
  2840. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  2841. NULL, 0))) {
  2842. return false;
  2843. }
  2844. }
  2845. ints = argv1;
  2846. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2847. fps = ints + MAX_REG_INTS;
  2848. stacks = fps + MAX_REG_FLOATS;
  2849. #elif defined(BUILD_TARGET_RISCV32_ILP32)
  2850. stacks = ints + MAX_REG_INTS;
  2851. #else
  2852. fps = ints + MAX_REG_INTS;
  2853. stacks = fps + MAX_REG_FLOATS * 2;
  2854. #endif
  2855. n_ints = 0;
  2856. n_fps = 0;
  2857. n_stacks = 0;
  2858. ints[n_ints++] = (uint32)(uintptr_t)exec_env;
  2859. /* Traverse secondly to fill in each argument */
  2860. for (i = 0; i < func_type->param_count; i++) {
  2861. switch (func_type->types[i]) {
  2862. case VALUE_TYPE_I32:
  2863. {
  2864. arg_i32 = *argv_src++;
  2865. if (signature) {
  2866. if (signature[i + 1] == '*') {
  2867. /* param is a pointer */
  2868. if (signature[i + 2] == '~')
  2869. /* pointer with length followed */
  2870. ptr_len = *argv_src;
  2871. else
  2872. /* pointer without length followed */
  2873. ptr_len = 1;
  2874. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  2875. goto fail;
  2876. arg_i32 = (uintptr_t)
  2877. wasm_runtime_addr_app_to_native(module, arg_i32);
  2878. }
  2879. else if (signature[i + 1] == '$') {
  2880. /* param is a string */
  2881. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  2882. goto fail;
  2883. arg_i32 = (uintptr_t)
  2884. wasm_runtime_addr_app_to_native(module, arg_i32);
  2885. }
  2886. }
  2887. if (n_ints < MAX_REG_INTS)
  2888. ints[n_ints++] = arg_i32;
  2889. else
  2890. stacks[n_stacks++] = arg_i32;
  2891. break;
  2892. }
  2893. #if WASM_ENABLE_REF_TYPES != 0
  2894. case VALUE_TYPE_FUNCREF:
  2895. case VALUE_TYPE_EXTERNREF:
  2896. {
  2897. if (n_ints < MAX_REG_INTS)
  2898. ints[n_ints++] = *argv_src++;
  2899. else
  2900. stacks[n_stacks++] = *argv_src++;
  2901. break;
  2902. }
  2903. #endif
  2904. case VALUE_TYPE_I64:
  2905. {
  2906. if (n_ints < MAX_REG_INTS - 1) {
  2907. #if !defined(BUILD_TARGET_RISCV32_ILP32) && !defined(BUILD_TARGET_RISCV32_ILP32D)
  2908. /* 64-bit data must be 8 bytes aligned in arm */
  2909. if (n_ints & 1)
  2910. n_ints++;
  2911. #endif
  2912. *(uint64*)&ints[n_ints] = *(uint64*)argv_src;
  2913. n_ints += 2;
  2914. argv_src += 2;
  2915. }
  2916. #if defined(BUILD_TARGET_RISCV32_ILP32) || defined(BUILD_TARGET_RISCV32_ILP32D)
  2917. else if (n_ints == MAX_REG_INTS - 1) {
  2918. ints[n_ints++] = *argv_src++;
  2919. stacks[n_stacks++] = *argv_src++;
  2920. }
  2921. #endif
  2922. else {
  2923. /* 64-bit data in stack must be 8 bytes aligned
  2924. in arm and riscv32 */
  2925. if (n_stacks & 1)
  2926. n_stacks++;
  2927. *(uint64*)&stacks[n_stacks] = *(uint64*)argv_src;
  2928. n_stacks += 2;
  2929. argv_src += 2;
  2930. }
  2931. break;
  2932. }
  2933. #if !defined(BUILD_TARGET_RISCV32_ILP32D)
  2934. case VALUE_TYPE_F32:
  2935. {
  2936. if (n_fps < MAX_REG_FLOATS)
  2937. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  2938. else
  2939. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  2940. break;
  2941. }
  2942. case VALUE_TYPE_F64:
  2943. {
  2944. if (n_fps < MAX_REG_FLOATS - 1) {
  2945. #if !defined(BUILD_TARGET_RISCV32_ILP32)
  2946. /* 64-bit data must be 8 bytes aligned in arm */
  2947. if (n_fps & 1)
  2948. n_fps++;
  2949. #endif
  2950. *(float64*)&fps[n_fps] = *(float64*)argv_src;
  2951. n_fps += 2;
  2952. argv_src += 2;
  2953. }
  2954. #if defined(BUILD_TARGET_RISCV32_ILP32)
  2955. else if (n_fps == MAX_REG_FLOATS - 1) {
  2956. fps[n_fps++] = *argv_src++;
  2957. stacks[n_stacks++] = *argv_src++;
  2958. }
  2959. #endif
  2960. else {
  2961. /* 64-bit data must be 8 bytes aligned in arm */
  2962. if (n_stacks & 1)
  2963. n_stacks++;
  2964. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  2965. n_stacks += 2;
  2966. argv_src += 2;
  2967. }
  2968. break;
  2969. }
  2970. #else /* BUILD_TARGET_RISCV32_ILP32D */
  2971. case VALUE_TYPE_F32:
  2972. case VALUE_TYPE_F64:
  2973. {
  2974. if (n_fps < MAX_REG_FLOATS) {
  2975. if (func_type->types[i] == VALUE_TYPE_F32) {
  2976. *(float32*)&fps[n_fps * 2] = *(float32*)argv_src++;
  2977. /* NaN boxing, the upper bits of a valid NaN-boxed
  2978. value must be all 1s. */
  2979. fps[n_fps * 2 + 1] = 0xFFFFFFFF;
  2980. }
  2981. else {
  2982. *(float64*)&fps[n_fps * 2] = *(float64*)argv_src;
  2983. argv_src += 2;
  2984. }
  2985. n_fps++;
  2986. }
  2987. else if (func_type->types[i] == VALUE_TYPE_F32
  2988. && n_ints < MAX_REG_INTS) {
  2989. /* use int reg firstly if available */
  2990. *(float32*)&ints[n_ints++] = *(float32*)argv_src++;
  2991. }
  2992. else if (func_type->types[i] == VALUE_TYPE_F64
  2993. && n_ints < MAX_REG_INTS - 1) {
  2994. /* use int regs firstly if available */
  2995. if (n_ints & 1)
  2996. n_ints++;
  2997. *(float64*)&ints[n_ints] = *(float64*)argv_src;
  2998. n_ints += 2;
  2999. argv_src += 2;
  3000. }
  3001. else {
  3002. /* 64-bit data in stack must be 8 bytes aligned in riscv32 */
  3003. if (n_stacks & 1)
  3004. n_stacks++;
  3005. if (func_type->types[i] == VALUE_TYPE_F32) {
  3006. *(float32*)&stacks[n_stacks] = *(float32*)argv_src++;
  3007. /* NaN boxing, the upper bits of a valid NaN-boxed
  3008. value must be all 1s. */
  3009. stacks[n_stacks + 1] = 0xFFFFFFFF;
  3010. }
  3011. else {
  3012. *(float64*)&stacks[n_stacks] = *(float64*)argv_src;
  3013. argv_src += 2;
  3014. }
  3015. n_stacks += 2;
  3016. }
  3017. break;
  3018. }
  3019. #endif /* BUILD_TARGET_RISCV32_ILP32D */
  3020. default:
  3021. bh_assert(0);
  3022. break;
  3023. }
  3024. }
  3025. /* Save extra result values' address to argv1 */
  3026. for (i = 0; i < ext_ret_count; i++) {
  3027. if (n_ints < MAX_REG_INTS)
  3028. ints[n_ints++] = *(uint32*)argv_src++;
  3029. else
  3030. stacks[n_stacks++] = *(uint32*)argv_src++;
  3031. }
  3032. exec_env->attachment = attachment;
  3033. if (func_type->result_count == 0) {
  3034. invokeNative_Void(func_ptr, argv1, n_stacks);
  3035. }
  3036. else {
  3037. switch (func_type->types[func_type->param_count]) {
  3038. case VALUE_TYPE_I32:
  3039. #if WASM_ENABLE_REF_TYPES != 0
  3040. case VALUE_TYPE_FUNCREF:
  3041. case VALUE_TYPE_EXTERNREF:
  3042. #endif
  3043. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3044. break;
  3045. case VALUE_TYPE_I64:
  3046. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  3047. break;
  3048. case VALUE_TYPE_F32:
  3049. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  3050. break;
  3051. case VALUE_TYPE_F64:
  3052. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3053. break;
  3054. default:
  3055. bh_assert(0);
  3056. break;
  3057. }
  3058. }
  3059. exec_env->attachment = NULL;
  3060. ret = !wasm_runtime_get_exception(module) ? true : false;
  3061. fail:
  3062. if (argv1 != argv_buf)
  3063. wasm_runtime_free(argv1);
  3064. return ret;
  3065. }
  3066. #endif /* end of defined(BUILD_TARGET_ARM_VFP)
  3067. || defined(BUILD_TARGET_THUMB_VFP) \
  3068. || defined(BUILD_TARGET_RISCV32_ILP32D)
  3069. || defined(BUILD_TARGET_RISCV32_ILP32) */
  3070. #if defined(BUILD_TARGET_X86_32) \
  3071. || defined(BUILD_TARGET_ARM) \
  3072. || defined(BUILD_TARGET_THUMB) \
  3073. || defined(BUILD_TARGET_MIPS) \
  3074. || defined(BUILD_TARGET_XTENSA)
  3075. typedef void (*GenericFunctionPointer)();
  3076. int64 invokeNative(GenericFunctionPointer f, uint32 *args, uint32 sz);
  3077. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  3078. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  3079. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  3080. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  3081. typedef void (*VoidFuncPtr)(GenericFunctionPointer f, uint32*, uint32);
  3082. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)invokeNative;
  3083. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)invokeNative;
  3084. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)invokeNative;
  3085. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)invokeNative;
  3086. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)invokeNative;
  3087. static inline void
  3088. word_copy(uint32 *dest, uint32 *src, unsigned num)
  3089. {
  3090. for (; num > 0; num--)
  3091. *dest++ = *src++;
  3092. }
  3093. bool
  3094. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3095. const WASMType *func_type, const char *signature,
  3096. void *attachment,
  3097. uint32 *argv, uint32 argc, uint32 *argv_ret)
  3098. {
  3099. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3100. uint32 argv_buf[32], *argv1 = argv_buf, argc1, i, j = 0;
  3101. uint32 arg_i32, ptr_len;
  3102. uint32 result_count = func_type->result_count;
  3103. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3104. uint64 size;
  3105. bool ret = false;
  3106. #if defined(BUILD_TARGET_X86_32)
  3107. argc1 = argc + ext_ret_count + 2;
  3108. #else
  3109. /* arm/thumb/mips/xtensa, 64-bit data must be 8 bytes aligned,
  3110. so we need to allocate more memory. */
  3111. argc1 = func_type->param_count * 2 + ext_ret_count + 2;
  3112. #endif
  3113. if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
  3114. size = sizeof(uint32) * (uint64)argc1;
  3115. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  3116. NULL, 0))) {
  3117. return false;
  3118. }
  3119. }
  3120. for (i = 0; i < sizeof(WASMExecEnv*) / sizeof(uint32); i++)
  3121. argv1[j++] = ((uint32*)&exec_env)[i];
  3122. for (i = 0; i < func_type->param_count; i++) {
  3123. switch (func_type->types[i]) {
  3124. case VALUE_TYPE_I32:
  3125. {
  3126. arg_i32 = *argv++;
  3127. if (signature) {
  3128. if (signature[i + 1] == '*') {
  3129. /* param is a pointer */
  3130. if (signature[i + 2] == '~')
  3131. /* pointer with length followed */
  3132. ptr_len = *argv;
  3133. else
  3134. /* pointer without length followed */
  3135. ptr_len = 1;
  3136. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  3137. goto fail;
  3138. arg_i32 = (uintptr_t)
  3139. wasm_runtime_addr_app_to_native(module, arg_i32);
  3140. }
  3141. else if (signature[i + 1] == '$') {
  3142. /* param is a string */
  3143. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  3144. goto fail;
  3145. arg_i32 = (uintptr_t)
  3146. wasm_runtime_addr_app_to_native(module, arg_i32);
  3147. }
  3148. }
  3149. argv1[j++] = arg_i32;
  3150. break;
  3151. }
  3152. case VALUE_TYPE_I64:
  3153. case VALUE_TYPE_F64:
  3154. #if !defined(BUILD_TARGET_X86_32)
  3155. /* 64-bit data must be 8 bytes aligned in arm, thumb, mips
  3156. and xtensa */
  3157. if (j & 1)
  3158. j++;
  3159. #endif
  3160. argv1[j++] = *argv++;
  3161. argv1[j++] = *argv++;
  3162. break;
  3163. case VALUE_TYPE_F32:
  3164. #if WASM_ENABLE_REF_TYPES != 0
  3165. case VALUE_TYPE_FUNCREF:
  3166. case VALUE_TYPE_EXTERNREF:
  3167. #endif
  3168. argv1[j++] = *argv++;
  3169. break;
  3170. default:
  3171. bh_assert(0);
  3172. break;
  3173. }
  3174. }
  3175. /* Save extra result values' address to argv1 */
  3176. word_copy(argv1 + j, argv, ext_ret_count);
  3177. argc1 = j + ext_ret_count;
  3178. exec_env->attachment = attachment;
  3179. if (func_type->result_count == 0) {
  3180. invokeNative_Void(func_ptr, argv1, argc1);
  3181. }
  3182. else {
  3183. switch (func_type->types[func_type->param_count]) {
  3184. case VALUE_TYPE_I32:
  3185. #if WASM_ENABLE_REF_TYPES != 0
  3186. case VALUE_TYPE_FUNCREF:
  3187. case VALUE_TYPE_EXTERNREF:
  3188. #endif
  3189. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, argc1);
  3190. break;
  3191. case VALUE_TYPE_I64:
  3192. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, argc1));
  3193. break;
  3194. case VALUE_TYPE_F32:
  3195. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, argc1);
  3196. break;
  3197. case VALUE_TYPE_F64:
  3198. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, argc1));
  3199. break;
  3200. default:
  3201. bh_assert(0);
  3202. break;
  3203. }
  3204. }
  3205. exec_env->attachment = NULL;
  3206. ret = !wasm_runtime_get_exception(module) ? true : false;
  3207. fail:
  3208. if (argv1 != argv_buf)
  3209. wasm_runtime_free(argv1);
  3210. return ret;
  3211. }
  3212. #endif /* end of defined(BUILD_TARGET_X86_32) \
  3213. || defined(BUILD_TARGET_ARM) \
  3214. || defined(BUILD_TARGET_THUMB) \
  3215. || defined(BUILD_TARGET_MIPS) \
  3216. || defined(BUILD_TARGET_XTENSA) */
  3217. #if defined(BUILD_TARGET_X86_64) \
  3218. || defined(BUILD_TARGET_AMD_64) \
  3219. || defined(BUILD_TARGET_AARCH64) \
  3220. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3221. || defined(BUILD_TARGET_RISCV64_LP64)
  3222. #if WASM_ENABLE_SIMD != 0
  3223. #ifdef v128
  3224. #undef v128
  3225. #endif
  3226. #if defined(_WIN32) || defined(_WIN32_)
  3227. typedef union __declspec(intrin_type) __declspec(align(8)) v128 {
  3228. __int8 m128i_i8[16];
  3229. __int16 m128i_i16[8];
  3230. __int32 m128i_i32[4];
  3231. __int64 m128i_i64[2];
  3232. unsigned __int8 m128i_u8[16];
  3233. unsigned __int16 m128i_u16[8];
  3234. unsigned __int32 m128i_u32[4];
  3235. unsigned __int64 m128i_u64[2];
  3236. } v128;
  3237. #elif defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
  3238. typedef long long v128 __attribute__ ((__vector_size__ (16),
  3239. __may_alias__, __aligned__ (1)));
  3240. #elif defined(BUILD_TARGET_AARCH64)
  3241. #include <arm_neon.h>
  3242. typedef uint32x4_t __m128i;
  3243. #define v128 __m128i
  3244. #endif
  3245. #endif /* end of WASM_ENABLE_SIMD != 0 */
  3246. typedef void (*GenericFunctionPointer)();
  3247. int64 invokeNative(GenericFunctionPointer f, uint64 *args, uint64 n_stacks);
  3248. typedef float64 (*Float64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3249. typedef float32 (*Float32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3250. typedef int64 (*Int64FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3251. typedef int32 (*Int32FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3252. typedef void (*VoidFuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3253. static Float64FuncPtr invokeNative_Float64 = (Float64FuncPtr)(uintptr_t)invokeNative;
  3254. static Float32FuncPtr invokeNative_Float32 = (Float32FuncPtr)(uintptr_t)invokeNative;
  3255. static Int64FuncPtr invokeNative_Int64 = (Int64FuncPtr)(uintptr_t)invokeNative;
  3256. static Int32FuncPtr invokeNative_Int32 = (Int32FuncPtr)(uintptr_t)invokeNative;
  3257. static VoidFuncPtr invokeNative_Void = (VoidFuncPtr)(uintptr_t)invokeNative;
  3258. #if WASM_ENABLE_SIMD != 0
  3259. typedef v128 (*V128FuncPtr)(GenericFunctionPointer, uint64*, uint64);
  3260. static V128FuncPtr invokeNative_V128 = (V128FuncPtr)(uintptr_t)invokeNative;
  3261. #endif
  3262. #if defined(_WIN32) || defined(_WIN32_)
  3263. #define MAX_REG_FLOATS 4
  3264. #define MAX_REG_INTS 4
  3265. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  3266. #define MAX_REG_FLOATS 8
  3267. #if defined(BUILD_TARGET_AARCH64) \
  3268. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3269. || defined(BUILD_TARGET_RISCV64_LP64)
  3270. #define MAX_REG_INTS 8
  3271. #else
  3272. #define MAX_REG_INTS 6
  3273. #endif /* end of defined(BUILD_TARGET_AARCH64) \
  3274. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3275. || defined(BUILD_TARGET_RISCV64_LP64) */
  3276. #endif /* end of defined(_WIN32) || defined(_WIN32_) */
  3277. bool
  3278. wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
  3279. const WASMType *func_type, const char *signature,
  3280. void *attachment,
  3281. uint32 *argv, uint32 argc, uint32 *argv_ret)
  3282. {
  3283. WASMModuleInstanceCommon *module = wasm_runtime_get_module_inst(exec_env);
  3284. uint64 argv_buf[32], *argv1 = argv_buf, *ints, *stacks, size, arg_i64;
  3285. uint32 *argv_src = argv, i, argc1, n_ints = 0, n_stacks = 0;
  3286. uint32 arg_i32, ptr_len;
  3287. uint32 result_count = func_type->result_count;
  3288. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  3289. bool ret = false;
  3290. #ifndef BUILD_TARGET_RISCV64_LP64
  3291. #if WASM_ENABLE_SIMD == 0
  3292. uint64 *fps;
  3293. #else
  3294. v128 *fps;
  3295. #endif
  3296. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3297. #define fps ints
  3298. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3299. #if defined(_WIN32) || defined(_WIN32_) || defined(BUILD_TARGET_RISCV64_LP64)
  3300. /* important difference in calling conventions */
  3301. #define n_fps n_ints
  3302. #else
  3303. int n_fps = 0;
  3304. #endif
  3305. #if WASM_ENABLE_SIMD == 0
  3306. argc1 = 1 + MAX_REG_FLOATS + (uint32)func_type->param_count
  3307. + ext_ret_count;
  3308. #else
  3309. argc1 = 1 + MAX_REG_FLOATS * 2 + (uint32)func_type->param_count * 2
  3310. + ext_ret_count;
  3311. #endif
  3312. if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
  3313. size = sizeof(uint64) * (uint64)argc1;
  3314. if (!(argv1 = runtime_malloc((uint32)size, exec_env->module_inst,
  3315. NULL, 0))) {
  3316. return false;
  3317. }
  3318. }
  3319. #ifndef BUILD_TARGET_RISCV64_LP64
  3320. #if WASM_ENABLE_SIMD == 0
  3321. fps = argv1;
  3322. ints = fps + MAX_REG_FLOATS;
  3323. #else
  3324. fps = (v128 *)argv1;
  3325. ints = (uint64 *)(fps + MAX_REG_FLOATS);
  3326. #endif
  3327. #else /* else of BUILD_TARGET_RISCV64_LP64 */
  3328. ints = argv1;
  3329. #endif /* end of BUILD_TARGET_RISCV64_LP64 */
  3330. stacks = ints + MAX_REG_INTS;
  3331. ints[n_ints++] = (uint64)(uintptr_t)exec_env;
  3332. for (i = 0; i < func_type->param_count; i++) {
  3333. switch (func_type->types[i]) {
  3334. case VALUE_TYPE_I32:
  3335. {
  3336. arg_i32 = *argv_src++;
  3337. arg_i64 = arg_i32;
  3338. if (signature) {
  3339. if (signature[i + 1] == '*') {
  3340. /* param is a pointer */
  3341. if (signature[i + 2] == '~')
  3342. /* pointer with length followed */
  3343. ptr_len = *argv_src;
  3344. else
  3345. /* pointer without length followed */
  3346. ptr_len = 1;
  3347. if (!wasm_runtime_validate_app_addr(module, arg_i32, ptr_len))
  3348. goto fail;
  3349. arg_i64 = (uintptr_t)
  3350. wasm_runtime_addr_app_to_native(module, arg_i32);
  3351. }
  3352. else if (signature[i + 1] == '$') {
  3353. /* param is a string */
  3354. if (!wasm_runtime_validate_app_str_addr(module, arg_i32))
  3355. goto fail;
  3356. arg_i64 = (uintptr_t)
  3357. wasm_runtime_addr_app_to_native(module, arg_i32);
  3358. }
  3359. }
  3360. if (n_ints < MAX_REG_INTS)
  3361. ints[n_ints++] = arg_i64;
  3362. else
  3363. stacks[n_stacks++] = arg_i64;
  3364. break;
  3365. }
  3366. case VALUE_TYPE_I64:
  3367. if (n_ints < MAX_REG_INTS)
  3368. ints[n_ints++] = *(uint64*)argv_src;
  3369. else
  3370. stacks[n_stacks++] = *(uint64*)argv_src;
  3371. argv_src += 2;
  3372. break;
  3373. case VALUE_TYPE_F32:
  3374. if (n_fps < MAX_REG_FLOATS) {
  3375. *(float32*)&fps[n_fps++] = *(float32*)argv_src++;
  3376. }
  3377. else {
  3378. *(float32*)&stacks[n_stacks++] = *(float32*)argv_src++;
  3379. }
  3380. break;
  3381. case VALUE_TYPE_F64:
  3382. if (n_fps < MAX_REG_FLOATS) {
  3383. *(float64*)&fps[n_fps++] = *(float64*)argv_src;
  3384. }
  3385. else {
  3386. *(float64*)&stacks[n_stacks++] = *(float64*)argv_src;
  3387. }
  3388. argv_src += 2;
  3389. break;
  3390. #if WASM_ENABLE_REF_TYPES != 0
  3391. case VALUE_TYPE_FUNCREF:
  3392. case VALUE_TYPE_EXTERNREF:
  3393. if (n_ints < MAX_REG_INTS)
  3394. ints[n_ints++] = *argv_src++;
  3395. else
  3396. stacks[n_stacks++] = *argv_src++;
  3397. break;
  3398. #endif
  3399. #if WASM_ENABLE_SIMD != 0
  3400. case VALUE_TYPE_V128:
  3401. if (n_fps < MAX_REG_FLOATS) {
  3402. *(v128*)&fps[n_fps++] = *(v128*)argv_src;
  3403. }
  3404. else {
  3405. *(v128*)&stacks[n_stacks++] = *(v128*)argv_src;
  3406. n_stacks++;
  3407. }
  3408. argv_src += 4;
  3409. break;
  3410. #endif
  3411. default:
  3412. bh_assert(0);
  3413. break;
  3414. }
  3415. }
  3416. /* Save extra result values' address to argv1 */
  3417. for (i = 0; i < ext_ret_count; i++) {
  3418. if (n_ints < MAX_REG_INTS)
  3419. ints[n_ints++] = *(uint64*)argv_src;
  3420. else
  3421. stacks[n_stacks++] = *(uint64*)argv_src;
  3422. argv_src += 2;
  3423. }
  3424. exec_env->attachment = attachment;
  3425. if (result_count == 0) {
  3426. invokeNative_Void(func_ptr, argv1, n_stacks);
  3427. }
  3428. else {
  3429. /* Invoke the native function and get the first result value */
  3430. switch (func_type->types[func_type->param_count]) {
  3431. case VALUE_TYPE_I32:
  3432. #if WASM_ENABLE_REF_TYPES != 0
  3433. case VALUE_TYPE_FUNCREF:
  3434. case VALUE_TYPE_EXTERNREF:
  3435. #endif
  3436. argv_ret[0] = (uint32)invokeNative_Int32(func_ptr, argv1, n_stacks);
  3437. break;
  3438. case VALUE_TYPE_I64:
  3439. PUT_I64_TO_ADDR(argv_ret, invokeNative_Int64(func_ptr, argv1, n_stacks));
  3440. break;
  3441. case VALUE_TYPE_F32:
  3442. *(float32*)argv_ret = invokeNative_Float32(func_ptr, argv1, n_stacks);
  3443. break;
  3444. case VALUE_TYPE_F64:
  3445. PUT_F64_TO_ADDR(argv_ret, invokeNative_Float64(func_ptr, argv1, n_stacks));
  3446. break;
  3447. #if WASM_ENABLE_SIMD != 0
  3448. case VALUE_TYPE_V128:
  3449. *(v128*)argv_ret = invokeNative_V128(func_ptr, argv1, n_stacks);
  3450. break;
  3451. #endif
  3452. default:
  3453. bh_assert(0);
  3454. break;
  3455. }
  3456. }
  3457. exec_env->attachment = NULL;
  3458. ret = !wasm_runtime_get_exception(module) ? true : false;
  3459. fail:
  3460. if (argv1 != argv_buf)
  3461. wasm_runtime_free(argv1);
  3462. return ret;
  3463. }
  3464. #endif /* end of defined(BUILD_TARGET_X86_64) \
  3465. || defined(BUILD_TARGET_AMD_64) \
  3466. || defined(BUILD_TARGET_AARCH64) \
  3467. || defined(BUILD_TARGET_RISCV64_LP64D) \
  3468. || defined(BUILD_TARGET_RISCV64_LP64) */
  3469. bool
  3470. wasm_runtime_call_indirect(WASMExecEnv *exec_env,
  3471. uint32_t element_indices,
  3472. uint32_t argc, uint32_t argv[])
  3473. {
  3474. if (!wasm_runtime_exec_env_check(exec_env)) {
  3475. LOG_ERROR("Invalid exec env stack info.");
  3476. return false;
  3477. }
  3478. /* this function is called from native code, so exec_env->handle and
  3479. exec_env->native_stack_boundary must have been set, we don't set
  3480. it again */
  3481. #if WASM_ENABLE_INTERP != 0
  3482. if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
  3483. return wasm_call_indirect(exec_env, 0, element_indices, argc, argv);
  3484. #endif
  3485. #if WASM_ENABLE_AOT != 0
  3486. if (exec_env->module_inst->module_type == Wasm_Module_AoT)
  3487. return aot_call_indirect(exec_env, 0, element_indices, argc, argv);
  3488. #endif
  3489. return false;
  3490. }
  3491. static void
  3492. exchange_uint32(uint8 *p_data)
  3493. {
  3494. uint8 value = *p_data;
  3495. *p_data = *(p_data + 3);
  3496. *(p_data + 3) = value;
  3497. value = *(p_data + 1);
  3498. *(p_data + 1) = *(p_data + 2);
  3499. *(p_data + 2) = value;
  3500. }
  3501. static void
  3502. exchange_uint64(uint8 *p_data)
  3503. {
  3504. uint32 value;
  3505. value = *(uint32 *)p_data;
  3506. *(uint32 *)p_data = *(uint32 *)(p_data + 4);
  3507. *(uint32 *)(p_data + 4) = value;
  3508. exchange_uint32(p_data);
  3509. exchange_uint32(p_data + 4);
  3510. }
  3511. void
  3512. wasm_runtime_read_v128(const uint8 *bytes, uint64 *ret1, uint64 *ret2)
  3513. {
  3514. uint64 u1, u2;
  3515. bh_memcpy_s(&u1, 8, bytes, 8);
  3516. bh_memcpy_s(&u2, 8, bytes + 8, 8);
  3517. if (!is_little_endian()) {
  3518. exchange_uint64((uint8*)&u1);
  3519. exchange_uint64((uint8*)&u2);
  3520. *ret1 = u2;
  3521. *ret2 = u1;
  3522. }
  3523. else {
  3524. *ret1 = u1;
  3525. *ret2 = u2;
  3526. }
  3527. }
  3528. #if WASM_ENABLE_THREAD_MGR != 0
  3529. typedef struct WASMThreadArg {
  3530. WASMExecEnv *new_exec_env;
  3531. wasm_thread_callback_t callback;
  3532. void *arg;
  3533. } WASMThreadArg;
  3534. WASMExecEnv *
  3535. wasm_runtime_spawn_exec_env(WASMExecEnv *exec_env)
  3536. {
  3537. return wasm_cluster_spawn_exec_env(exec_env);
  3538. }
  3539. void
  3540. wasm_runtime_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  3541. {
  3542. wasm_cluster_destroy_spawned_exec_env(exec_env);
  3543. }
  3544. static void*
  3545. wasm_runtime_thread_routine(void *arg)
  3546. {
  3547. WASMThreadArg *thread_arg = (WASMThreadArg *)arg;
  3548. void *ret;
  3549. bh_assert(thread_arg->new_exec_env);
  3550. ret = thread_arg->callback(thread_arg->new_exec_env, thread_arg->arg);
  3551. wasm_runtime_destroy_spawned_exec_env(thread_arg->new_exec_env);
  3552. wasm_runtime_free(thread_arg);
  3553. os_thread_exit(ret);
  3554. return ret;
  3555. }
  3556. int32
  3557. wasm_runtime_spawn_thread(WASMExecEnv *exec_env, wasm_thread_t *tid,
  3558. wasm_thread_callback_t callback, void *arg)
  3559. {
  3560. WASMExecEnv *new_exec_env = wasm_runtime_spawn_exec_env(exec_env);
  3561. WASMThreadArg *thread_arg;
  3562. int32 ret;
  3563. if (!new_exec_env)
  3564. return -1;
  3565. if (!(thread_arg = wasm_runtime_malloc(sizeof(WASMThreadArg)))) {
  3566. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3567. return -1;
  3568. }
  3569. thread_arg->new_exec_env = new_exec_env;
  3570. thread_arg->callback = callback;
  3571. thread_arg->arg = arg;
  3572. ret = os_thread_create((korp_tid *)tid, wasm_runtime_thread_routine,
  3573. thread_arg, APP_THREAD_STACK_SIZE_DEFAULT);
  3574. if (ret != 0) {
  3575. wasm_runtime_destroy_spawned_exec_env(new_exec_env);
  3576. wasm_runtime_free(thread_arg);
  3577. }
  3578. return ret;
  3579. }
  3580. int32
  3581. wasm_runtime_join_thread(wasm_thread_t tid, void **retval)
  3582. {
  3583. return os_thread_join((korp_tid)tid, retval);
  3584. }
  3585. #endif /* end of WASM_ENABLE_THREAD_MGR */
  3586. #if WASM_ENABLE_REF_TYPES != 0
  3587. static korp_mutex externref_lock;
  3588. static uint32 externref_global_id = 1;
  3589. static HashMap *externref_map;
  3590. typedef struct ExternRefMapNode {
  3591. /* The extern object from runtime embedder */
  3592. void *extern_obj;
  3593. /* The module instance it belongs to */
  3594. WASMModuleInstanceCommon *module_inst;
  3595. /* Whether it is retained */
  3596. bool retained;
  3597. /* Whether it is marked by runtime */
  3598. bool marked;
  3599. } ExternRefMapNode;
  3600. static uint32
  3601. wasm_externref_hash(const void *key)
  3602. {
  3603. uint32 externref_idx = (uint32)(uintptr_t)key;
  3604. return externref_idx;
  3605. }
  3606. static bool
  3607. wasm_externref_equal(void *key1, void *key2)
  3608. {
  3609. uint32 externref_idx1 = (uint32)(uintptr_t)key1;
  3610. uint32 externref_idx2 = (uint32)(uintptr_t)key2;
  3611. return externref_idx1 == externref_idx2 ? true : false;
  3612. }
  3613. static bool
  3614. wasm_externref_map_init()
  3615. {
  3616. if (os_mutex_init(&externref_lock) != 0)
  3617. return false;
  3618. if (!(externref_map = bh_hash_map_create(32, false,
  3619. wasm_externref_hash,
  3620. wasm_externref_equal,
  3621. NULL,
  3622. wasm_runtime_free))) {
  3623. os_mutex_destroy(&externref_lock);
  3624. return false;
  3625. }
  3626. externref_global_id = 1;
  3627. return true;
  3628. }
  3629. static void
  3630. wasm_externref_map_destroy()
  3631. {
  3632. bh_hash_map_destroy(externref_map);
  3633. os_mutex_destroy(&externref_lock);
  3634. }
  3635. typedef struct LookupExtObj_UserData {
  3636. ExternRefMapNode node;
  3637. bool found;
  3638. uint32 externref_idx;
  3639. } LookupExtObj_UserData;
  3640. static void
  3641. lookup_extobj_callback(void *key, void *value, void *user_data)
  3642. {
  3643. uint32 externref_idx = (uint32)(uintptr_t)key;
  3644. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3645. LookupExtObj_UserData *user_data_lookup = (LookupExtObj_UserData *)
  3646. user_data;
  3647. if (node->extern_obj == user_data_lookup->node.extern_obj
  3648. && node->module_inst == user_data_lookup->node.module_inst) {
  3649. user_data_lookup->found = true;
  3650. user_data_lookup->externref_idx = externref_idx;
  3651. }
  3652. }
  3653. bool
  3654. wasm_externref_obj2ref(WASMModuleInstanceCommon *module_inst,
  3655. void *extern_obj, uint32 *p_externref_idx)
  3656. {
  3657. LookupExtObj_UserData lookup_user_data;
  3658. ExternRefMapNode *node;
  3659. uint32 externref_idx;
  3660. lookup_user_data.node.extern_obj = extern_obj;
  3661. lookup_user_data.node.module_inst = module_inst;
  3662. lookup_user_data.found = false;
  3663. os_mutex_lock(&externref_lock);
  3664. /* Lookup hashmap firstly */
  3665. bh_hash_map_traverse(externref_map, lookup_extobj_callback,
  3666. (void*)&lookup_user_data);
  3667. if (lookup_user_data.found) {
  3668. *p_externref_idx = lookup_user_data.externref_idx;
  3669. os_mutex_unlock(&externref_lock);
  3670. return true;
  3671. }
  3672. /* Not found in hashmap */
  3673. if (externref_global_id == NULL_REF
  3674. || externref_global_id == 0) {
  3675. goto fail1;
  3676. }
  3677. if (!(node = wasm_runtime_malloc(sizeof(ExternRefMapNode)))) {
  3678. goto fail1;
  3679. }
  3680. memset(node, 0, sizeof(ExternRefMapNode));
  3681. node->extern_obj = extern_obj;
  3682. node->module_inst = module_inst;
  3683. externref_idx = externref_global_id;
  3684. if (!bh_hash_map_insert(externref_map,
  3685. (void*)(uintptr_t)externref_idx,
  3686. (void*)node)) {
  3687. goto fail2;
  3688. }
  3689. externref_global_id++;
  3690. *p_externref_idx = externref_idx;
  3691. os_mutex_unlock(&externref_lock);
  3692. return true;
  3693. fail2:
  3694. wasm_runtime_free(node);
  3695. fail1:
  3696. os_mutex_unlock(&externref_lock);
  3697. return false;
  3698. }
  3699. bool
  3700. wasm_externref_ref2obj(uint32 externref_idx, void **p_extern_obj)
  3701. {
  3702. ExternRefMapNode *node;
  3703. if (externref_idx == NULL_REF) {
  3704. return false;
  3705. }
  3706. os_mutex_lock(&externref_lock);
  3707. node = bh_hash_map_find(externref_map,
  3708. (void*)(uintptr_t)externref_idx);
  3709. os_mutex_unlock(&externref_lock);
  3710. if (!node)
  3711. return false;
  3712. *p_extern_obj = node->extern_obj;
  3713. return true;
  3714. }
  3715. static void
  3716. reclaim_extobj_callback(void *key, void *value, void *user_data)
  3717. {
  3718. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3719. WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *)
  3720. user_data;
  3721. if (node->module_inst == module_inst) {
  3722. if (!node->marked && !node->retained) {
  3723. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3724. wasm_runtime_free(value);
  3725. }
  3726. else {
  3727. node->marked = false;
  3728. }
  3729. }
  3730. }
  3731. static void
  3732. mark_externref(uint32 externref_idx)
  3733. {
  3734. ExternRefMapNode *node;
  3735. if (externref_idx != NULL_REF) {
  3736. node = bh_hash_map_find(externref_map,
  3737. (void*)(uintptr_t)externref_idx);
  3738. if (node) {
  3739. node->marked = true;
  3740. }
  3741. }
  3742. }
  3743. #if WASM_ENABLE_INTERP != 0
  3744. static void
  3745. interp_mark_all_externrefs(WASMModuleInstance *module_inst)
  3746. {
  3747. uint32 i, j, externref_idx, *table_data;
  3748. uint8 *global_data = module_inst->global_data;
  3749. WASMGlobalInstance *global;
  3750. WASMTableInstance *table;
  3751. global = module_inst->globals;
  3752. for (i = 0; i < module_inst->global_count; i++, global++) {
  3753. if (global->type == VALUE_TYPE_EXTERNREF) {
  3754. externref_idx = *(uint32*)(global_data + global->data_offset);
  3755. mark_externref(externref_idx);
  3756. }
  3757. }
  3758. for (i = 0; i < module_inst->table_count; i++) {
  3759. table = wasm_get_table_inst(module_inst, i);
  3760. if (table->elem_type == VALUE_TYPE_EXTERNREF) {
  3761. table_data = (uint32 *)table->base_addr;
  3762. for (j = 0; j < table->cur_size; j++) {
  3763. externref_idx = table_data[j];
  3764. mark_externref(externref_idx);
  3765. }
  3766. }
  3767. }
  3768. }
  3769. #endif
  3770. #if WASM_ENABLE_AOT != 0
  3771. static void
  3772. aot_mark_all_externrefs(AOTModuleInstance *module_inst)
  3773. {
  3774. uint32 i = 0, j = 0;
  3775. const AOTModule *module = (AOTModule *)(module_inst->aot_module.ptr);
  3776. const AOTTable *table = module->tables;
  3777. const AOTGlobal *global = module->globals;
  3778. const AOTTableInstance *table_inst =
  3779. (AOTTableInstance *)module_inst->tables.ptr;
  3780. for (i = 0; i < module->global_count; i++, global++) {
  3781. if (global->type == VALUE_TYPE_EXTERNREF) {
  3782. mark_externref(*(uint32 *)((uint8 *)module_inst->global_data.ptr
  3783. + global->data_offset));
  3784. }
  3785. }
  3786. for (i = 0; i < module->table_count;
  3787. i++, table_inst = aot_next_tbl_inst(table_inst)) {
  3788. if ((table + i)->elem_type == VALUE_TYPE_EXTERNREF) {
  3789. while (j < table_inst->cur_size) {
  3790. mark_externref(table_inst->data[j++]);
  3791. }
  3792. }
  3793. }
  3794. }
  3795. #endif
  3796. void
  3797. wasm_externref_reclaim(WASMModuleInstanceCommon *module_inst)
  3798. {
  3799. os_mutex_lock(&externref_lock);
  3800. #if WASM_ENABLE_INTERP != 0
  3801. if (module_inst->module_type == Wasm_Module_Bytecode)
  3802. interp_mark_all_externrefs((WASMModuleInstance*)module_inst);
  3803. #endif
  3804. #if WASM_ENABLE_AOT != 0
  3805. if (module_inst->module_type == Wasm_Module_AoT)
  3806. aot_mark_all_externrefs((AOTModuleInstance*)module_inst);
  3807. #endif
  3808. bh_hash_map_traverse(externref_map, reclaim_extobj_callback,
  3809. (void*)module_inst);
  3810. os_mutex_unlock(&externref_lock);
  3811. }
  3812. static void
  3813. cleanup_extobj_callback(void *key, void *value, void *user_data)
  3814. {
  3815. ExternRefMapNode *node = (ExternRefMapNode *)value;
  3816. WASMModuleInstanceCommon *module_inst = (WASMModuleInstanceCommon *)
  3817. user_data;
  3818. if (node->module_inst == module_inst) {
  3819. bh_hash_map_remove(externref_map, key, NULL, NULL);
  3820. wasm_runtime_free(value);
  3821. }
  3822. }
  3823. void
  3824. wasm_externref_cleanup(WASMModuleInstanceCommon *module_inst)
  3825. {
  3826. os_mutex_lock(&externref_lock);
  3827. bh_hash_map_traverse(externref_map, cleanup_extobj_callback,
  3828. (void*)module_inst);
  3829. os_mutex_unlock(&externref_lock);
  3830. }
  3831. bool
  3832. wasm_externref_retain(uint32 externref_idx)
  3833. {
  3834. ExternRefMapNode *node;
  3835. os_mutex_lock(&externref_lock);
  3836. if (externref_idx != NULL_REF) {
  3837. node = bh_hash_map_find(externref_map,
  3838. (void*)(uintptr_t)externref_idx);
  3839. if (node) {
  3840. node->retained = true;
  3841. os_mutex_unlock(&externref_lock);
  3842. return true;
  3843. }
  3844. }
  3845. os_mutex_unlock(&externref_lock);
  3846. return false;
  3847. }
  3848. #endif /* end of WASM_ENABLE_REF_TYPES */
  3849. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3850. void
  3851. wasm_runtime_dump_call_stack(WASMExecEnv *exec_env)
  3852. {
  3853. WASMModuleInstanceCommon *module_inst
  3854. = wasm_exec_env_get_module_inst(exec_env);
  3855. #if WASM_ENABLE_INTERP != 0
  3856. if (module_inst->module_type == Wasm_Module_Bytecode) {
  3857. wasm_interp_dump_call_stack(exec_env);
  3858. }
  3859. #endif
  3860. #if WASM_ENABLE_AOT != 0
  3861. if (module_inst->module_type == Wasm_Module_AoT) {
  3862. aot_dump_call_stack(exec_env);
  3863. }
  3864. #endif
  3865. }
  3866. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK */
  3867. bool
  3868. wasm_runtime_get_export_func_type(const WASMModuleCommon *module_comm,
  3869. const WASMExport *export,
  3870. WASMType **out)
  3871. {
  3872. #if WASM_ENABLE_INTERP != 0
  3873. if (module_comm->module_type == Wasm_Module_Bytecode) {
  3874. WASMModule *module = (WASMModule *)module_comm;
  3875. if (export->index < module->import_function_count) {
  3876. *out =
  3877. module->import_functions[export->index].u.function.func_type;
  3878. }
  3879. else {
  3880. *out =
  3881. module->functions[export->index - module->import_function_count]
  3882. ->func_type;
  3883. }
  3884. return true;
  3885. }
  3886. #endif
  3887. #if WASM_ENABLE_AOT != 0
  3888. if (module_comm->module_type == Wasm_Module_AoT) {
  3889. AOTModule *module = (AOTModule *)module_comm;
  3890. if (export->index < module->import_func_count) {
  3891. *out = module->func_types[module->import_funcs[export->index]
  3892. .func_type_index];
  3893. }
  3894. else {
  3895. *out =
  3896. module->func_types[module->func_type_indexes
  3897. [export->index - module->import_func_count]];
  3898. }
  3899. return true;
  3900. }
  3901. #endif
  3902. return false;
  3903. }
  3904. bool
  3905. wasm_runtime_get_export_global_type(const WASMModuleCommon *module_comm,
  3906. const WASMExport *export,
  3907. uint8 *out_val_type,
  3908. bool *out_mutability)
  3909. {
  3910. #if WASM_ENABLE_INTERP != 0
  3911. if (module_comm->module_type == Wasm_Module_Bytecode) {
  3912. WASMModule *module = (WASMModule *)module_comm;
  3913. if (export->index < module->import_global_count) {
  3914. WASMGlobalImport *import_global =
  3915. &((module->import_globals + export->index)->u.global);
  3916. *out_val_type = import_global->type;
  3917. *out_mutability = import_global->is_mutable;
  3918. }
  3919. else {
  3920. WASMGlobal *global =
  3921. module->globals + (export->index - module->import_global_count);
  3922. *out_val_type = global->type;
  3923. *out_mutability = global->is_mutable;
  3924. }
  3925. return true;
  3926. }
  3927. #endif
  3928. #if WASM_ENABLE_AOT != 0
  3929. if (module_comm->module_type == Wasm_Module_AoT) {
  3930. AOTModule *module = (AOTModule *)module_comm;
  3931. if (export->index < module->import_global_count) {
  3932. AOTImportGlobal *import_global =
  3933. module->import_globals + export->index;
  3934. *out_val_type = import_global->type;
  3935. *out_mutability = import_global->is_mutable;
  3936. }
  3937. else {
  3938. AOTGlobal *global =
  3939. module->globals + (export->index - module->import_global_count);
  3940. *out_val_type = global->type;
  3941. *out_mutability = global->is_mutable;
  3942. }
  3943. return true;
  3944. }
  3945. #endif
  3946. return false;
  3947. }
  3948. bool
  3949. wasm_runtime_get_export_memory_type(const WASMModuleCommon *module_comm,
  3950. const WASMExport *export,
  3951. uint32 *out_min_page,
  3952. uint32 *out_max_page)
  3953. {
  3954. #if WASM_ENABLE_INTERP != 0
  3955. if (module_comm->module_type == Wasm_Module_Bytecode) {
  3956. WASMModule *module = (WASMModule *)module_comm;
  3957. if (export->index < module->import_memory_count) {
  3958. WASMMemoryImport *import_memory =
  3959. &((module->import_memories + export->index)->u.memory);
  3960. *out_min_page = import_memory->init_page_count;
  3961. *out_max_page = import_memory->max_page_count;
  3962. }
  3963. else {
  3964. WASMMemory *memory =
  3965. module->memories + (export->index - module->import_memory_count);
  3966. *out_min_page = memory->init_page_count;
  3967. *out_max_page = memory->max_page_count;
  3968. }
  3969. return true;
  3970. }
  3971. #endif
  3972. #if WASM_ENABLE_AOT != 0
  3973. if (module_comm->module_type == Wasm_Module_AoT) {
  3974. AOTModule *module = (AOTModule *)module_comm;
  3975. if (export->index < module->import_memory_count) {
  3976. AOTImportMemory *import_memory =
  3977. module->import_memories + export->index;
  3978. *out_min_page = import_memory->mem_init_page_count;
  3979. *out_max_page = import_memory->mem_max_page_count;
  3980. }
  3981. else {
  3982. AOTMemory *memory =
  3983. module->memories + (export->index - module->import_memory_count);
  3984. *out_min_page = memory->mem_init_page_count;
  3985. *out_max_page = memory->mem_max_page_count;
  3986. }
  3987. return true;
  3988. }
  3989. #endif
  3990. return false;
  3991. }
  3992. bool
  3993. wasm_runtime_get_export_table_type(const WASMModuleCommon *module_comm,
  3994. const WASMExport *export,
  3995. uint8 *out_elem_type,
  3996. uint32 *out_min_size,
  3997. uint32 *out_max_size)
  3998. {
  3999. #if WASM_ENABLE_INTERP != 0
  4000. if (module_comm->module_type == Wasm_Module_Bytecode) {
  4001. WASMModule *module = (WASMModule *)module_comm;
  4002. if (export->index < module->import_table_count) {
  4003. WASMTableImport *import_table =
  4004. &((module->import_tables + export->index)->u.table);
  4005. *out_elem_type = import_table->elem_type;
  4006. *out_min_size = import_table->init_size;
  4007. *out_max_size = import_table->max_size;
  4008. }
  4009. else {
  4010. WASMTable *table =
  4011. module->tables + (export->index - module->import_table_count);
  4012. *out_elem_type = table->elem_type;
  4013. *out_min_size = table->init_size;
  4014. *out_max_size = table->max_size;
  4015. }
  4016. return true;
  4017. }
  4018. #endif
  4019. #if WASM_ENABLE_AOT != 0
  4020. if (module_comm->module_type == Wasm_Module_AoT) {
  4021. AOTModule *module = (AOTModule *)module_comm;
  4022. if (export->index < module->import_table_count) {
  4023. AOTImportTable *import_table =
  4024. module->import_tables + export->index;
  4025. *out_elem_type = VALUE_TYPE_FUNCREF;
  4026. *out_min_size = import_table->table_init_size;
  4027. *out_max_size = import_table->table_max_size;
  4028. }
  4029. else {
  4030. AOTTable *table =
  4031. module->tables + (export->index - module->import_table_count);
  4032. *out_elem_type = table->elem_type;
  4033. *out_min_size = table->table_init_size;
  4034. *out_max_size = table->table_max_size;
  4035. }
  4036. return true;
  4037. }
  4038. #endif
  4039. return false;
  4040. }
  4041. uint8 *
  4042. wasm_runtime_get_memory_data(const WASMModuleInstanceCommon *module_inst_comm,
  4043. uint32 memory_inst_idx)
  4044. {
  4045. #if WASM_ENABLE_INTERP != 0
  4046. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  4047. WASMModuleInstance *module_inst =
  4048. (WASMModuleInstance *)module_inst_comm;
  4049. WASMMemoryInstance *memory_inst =
  4050. module_inst->memories[memory_inst_idx];
  4051. return memory_inst->memory_data;
  4052. }
  4053. #endif
  4054. #if WASM_ENABLE_AOT != 0
  4055. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  4056. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  4057. AOTMemoryInstance *memory_inst =
  4058. (AOTMemoryInstance*)module_inst->memories.ptr + memory_inst_idx;
  4059. return memory_inst->memory_data.ptr;
  4060. }
  4061. #endif
  4062. return NULL;
  4063. }
  4064. uint32
  4065. wasm_runtime_get_memory_data_size(
  4066. const WASMModuleInstanceCommon *module_inst_comm,
  4067. uint32 memory_inst_idx)
  4068. {
  4069. #if WASM_ENABLE_INTERP != 0
  4070. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  4071. WASMModuleInstance *module_inst =
  4072. (WASMModuleInstance *)module_inst_comm;
  4073. WASMMemoryInstance *memory_inst =
  4074. module_inst->memories[memory_inst_idx];
  4075. return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
  4076. }
  4077. #endif
  4078. #if WASM_ENABLE_AOT != 0
  4079. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  4080. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  4081. AOTMemoryInstance *memory_inst =
  4082. (AOTMemoryInstance*)module_inst->memories.ptr + memory_inst_idx;
  4083. return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
  4084. }
  4085. #endif
  4086. return 0;
  4087. }