wasm_runtime_common.c 152 KB

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