wasm_runtime_common.c 157 KB

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