wasm_c_api.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_log.h"
  6. #include "wasm_c_api_internal.h"
  7. #include "bh_assert.h"
  8. #include "wasm_export.h"
  9. #include "wasm_memory.h"
  10. #if WASM_ENABLE_INTERP != 0
  11. #include "wasm_runtime.h"
  12. #endif
  13. #if WASM_ENABLE_AOT != 0
  14. #include "aot_runtime.h"
  15. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT == 0
  16. #include "aot.h"
  17. #include "aot_llvm.h"
  18. #endif /*WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT == 0*/
  19. #endif /*WASM_ENABLE_AOT != 0*/
  20. #if WASM_ENABLE_WASM_CACHE != 0
  21. #include <openssl/sha.h>
  22. #endif
  23. #if WASM_ENABLE_THREAD_MGR != 0
  24. #include "thread_manager.h"
  25. #endif
  26. /*
  27. * Thread Model:
  28. * - Only one wasm_engine_t in one process
  29. * - One wasm_store_t is only accessed by one thread. wasm_store_t can't be
  30. * shared in threads
  31. * - wasm_module_t can be shared in threads
  32. * - wasm_instance_t can not be shared in threads
  33. */
  34. #define ASSERT_NOT_IMPLEMENTED() bh_assert(!"not implemented")
  35. #define UNREACHABLE() bh_assert(!"unreachable")
  36. typedef struct wasm_module_ex_t {
  37. struct WASMModuleCommon *module_comm_rt;
  38. wasm_byte_vec_t *binary;
  39. korp_mutex lock;
  40. uint32 ref_count;
  41. #if WASM_ENABLE_WASM_CACHE != 0
  42. char hash[SHA256_DIGEST_LENGTH];
  43. #endif
  44. } wasm_module_ex_t;
  45. #ifndef os_thread_local_attribute
  46. typedef struct thread_local_stores {
  47. korp_tid tid;
  48. unsigned stores_num;
  49. } thread_local_stores;
  50. #endif
  51. static void
  52. wasm_module_delete_internal(wasm_module_t *);
  53. static void
  54. wasm_instance_delete_internal(wasm_instance_t *);
  55. /* temporarily put stubs here */
  56. static wasm_store_t *
  57. wasm_store_copy(const wasm_store_t *src)
  58. {
  59. (void)src;
  60. LOG_WARNING("in the stub of %s", __FUNCTION__);
  61. return NULL;
  62. }
  63. wasm_module_t *
  64. wasm_module_copy(const wasm_module_t *src)
  65. {
  66. (void)src;
  67. LOG_WARNING("in the stub of %s", __FUNCTION__);
  68. return NULL;
  69. }
  70. wasm_instance_t *
  71. wasm_instance_copy(const wasm_instance_t *src)
  72. {
  73. (void)src;
  74. LOG_WARNING("in the stub of %s", __FUNCTION__);
  75. return NULL;
  76. }
  77. /* ---------------------------------------------------------------------- */
  78. static inline void *
  79. malloc_internal(uint64 size)
  80. {
  81. void *mem = NULL;
  82. if (size < UINT32_MAX && (mem = wasm_runtime_malloc((uint32)size))) {
  83. memset(mem, 0, size);
  84. }
  85. return mem;
  86. }
  87. /* clang-format off */
  88. #define RETURN_OBJ(obj, obj_del_func) \
  89. return obj; \
  90. failed: \
  91. obj_del_func(obj); \
  92. return NULL;
  93. #define RETURN_VOID(obj, obj_del_func) \
  94. return; \
  95. failed: \
  96. obj_del_func(obj); \
  97. return;
  98. /* clang-format on */
  99. /* Vectors */
  100. #define INIT_VEC(vector_p, init_func, ...) \
  101. do { \
  102. if (!(vector_p = malloc_internal(sizeof(*(vector_p))))) { \
  103. goto failed; \
  104. } \
  105. \
  106. init_func(vector_p, ##__VA_ARGS__); \
  107. if (vector_p->size && !vector_p->data) { \
  108. LOG_DEBUG("%s failed", #init_func); \
  109. goto failed; \
  110. } \
  111. } while (false)
  112. #define DEINIT_VEC(vector_p, deinit_func) \
  113. if ((vector_p)) { \
  114. deinit_func(vector_p); \
  115. wasm_runtime_free(vector_p); \
  116. vector_p = NULL; \
  117. }
  118. #define WASM_DEFINE_VEC(name) \
  119. void wasm_##name##_vec_new_empty(own wasm_##name##_vec_t *out) \
  120. { \
  121. wasm_##name##_vec_new_uninitialized(out, 0); \
  122. } \
  123. void wasm_##name##_vec_new_uninitialized(own wasm_##name##_vec_t *out, \
  124. size_t size) \
  125. { \
  126. wasm_##name##_vec_new(out, size, NULL); \
  127. }
  128. /* vectors with no ownership management of elements */
  129. #define WASM_DEFINE_VEC_PLAIN(name) \
  130. WASM_DEFINE_VEC(name) \
  131. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  132. own wasm_##name##_t const data[]) \
  133. { \
  134. if (!out) { \
  135. return; \
  136. } \
  137. \
  138. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  139. \
  140. if (!size) { \
  141. return; \
  142. } \
  143. \
  144. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t), \
  145. true)) { \
  146. LOG_DEBUG("bh_vector_init failed"); \
  147. goto failed; \
  148. } \
  149. \
  150. if (data) { \
  151. uint32 size_in_bytes = 0; \
  152. size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t)); \
  153. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  154. out->num_elems = size; \
  155. } \
  156. \
  157. RETURN_VOID(out, wasm_##name##_vec_delete) \
  158. } \
  159. void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \
  160. const wasm_##name##_vec_t *src) \
  161. { \
  162. if (!src) { \
  163. return; \
  164. } \
  165. wasm_##name##_vec_new(out, src->size, src->data); \
  166. } \
  167. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  168. { \
  169. if (v) { \
  170. bh_vector_destroy((Vector *)v); \
  171. } \
  172. }
  173. /* vectors that own their elements */
  174. #define WASM_DEFINE_VEC_OWN(name, elem_destroy_func) \
  175. WASM_DEFINE_VEC(name) \
  176. void wasm_##name##_vec_new(own wasm_##name##_vec_t *out, size_t size, \
  177. own wasm_##name##_t *const data[]) \
  178. { \
  179. if (!out) { \
  180. return; \
  181. } \
  182. \
  183. memset(out, 0, sizeof(wasm_##name##_vec_t)); \
  184. \
  185. if (!size) { \
  186. return; \
  187. } \
  188. \
  189. if (!bh_vector_init((Vector *)out, size, sizeof(wasm_##name##_t *), \
  190. true)) { \
  191. LOG_DEBUG("bh_vector_init failed"); \
  192. goto failed; \
  193. } \
  194. \
  195. if (data) { \
  196. uint32 size_in_bytes = 0; \
  197. size_in_bytes = (uint32)(size * sizeof(wasm_##name##_t *)); \
  198. bh_memcpy_s(out->data, size_in_bytes, data, size_in_bytes); \
  199. out->num_elems = size; \
  200. } \
  201. \
  202. RETURN_VOID(out, wasm_##name##_vec_delete) \
  203. } \
  204. void wasm_##name##_vec_copy(own wasm_##name##_vec_t *out, \
  205. const wasm_##name##_vec_t *src) \
  206. { \
  207. size_t i = 0; \
  208. \
  209. if (!out) { \
  210. return; \
  211. } \
  212. memset(out, 0, sizeof(Vector)); \
  213. \
  214. if (!src || !src->size) { \
  215. return; \
  216. } \
  217. \
  218. if (!bh_vector_init((Vector *)out, src->size, \
  219. sizeof(wasm_##name##_t *), true)) { \
  220. LOG_DEBUG("bh_vector_init failed"); \
  221. goto failed; \
  222. } \
  223. \
  224. for (i = 0; i != src->num_elems; ++i) { \
  225. if (!(out->data[i] = wasm_##name##_copy(src->data[i]))) { \
  226. LOG_DEBUG("wasm_%s_copy failed", #name); \
  227. goto failed; \
  228. } \
  229. } \
  230. out->num_elems = src->num_elems; \
  231. \
  232. RETURN_VOID(out, wasm_##name##_vec_delete) \
  233. } \
  234. void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \
  235. { \
  236. size_t i = 0; \
  237. if (!v) { \
  238. return; \
  239. } \
  240. for (i = 0; i != v->num_elems && v->data; ++i) { \
  241. elem_destroy_func(*(v->data + i)); \
  242. } \
  243. bh_vector_destroy((Vector *)v); \
  244. }
  245. WASM_DEFINE_VEC_PLAIN(byte)
  246. WASM_DEFINE_VEC_PLAIN(val)
  247. WASM_DEFINE_VEC_OWN(exporttype, wasm_exporttype_delete)
  248. WASM_DEFINE_VEC_OWN(extern, wasm_extern_delete)
  249. WASM_DEFINE_VEC_OWN(frame, wasm_frame_delete)
  250. WASM_DEFINE_VEC_OWN(functype, wasm_functype_delete)
  251. WASM_DEFINE_VEC_OWN(importtype, wasm_importtype_delete)
  252. WASM_DEFINE_VEC_OWN(instance, wasm_instance_delete_internal)
  253. WASM_DEFINE_VEC_OWN(module, wasm_module_delete_internal)
  254. WASM_DEFINE_VEC_OWN(store, wasm_store_delete)
  255. WASM_DEFINE_VEC_OWN(valtype, wasm_valtype_delete)
  256. #ifndef NDEBUG
  257. #if WASM_ENABLE_MEMORY_PROFILING != 0
  258. #define WASM_C_DUMP_PROC_MEM() LOG_PROC_MEM()
  259. #else
  260. #define WASM_C_DUMP_PROC_MEM() (void)0
  261. #endif
  262. #else
  263. #define WASM_C_DUMP_PROC_MEM() (void)0
  264. #endif
  265. /* Runtime Environment */
  266. own wasm_config_t *
  267. wasm_config_new(void)
  268. {
  269. return NULL;
  270. }
  271. void
  272. wasm_config_delete(own wasm_config_t *config)
  273. {
  274. (void)config;
  275. }
  276. static void
  277. wasm_engine_delete_internal(wasm_engine_t *engine)
  278. {
  279. if (engine) {
  280. /* clean all created wasm_module_t and their locks */
  281. unsigned i;
  282. for (i = 0; i < engine->modules.num_elems; i++) {
  283. wasm_module_ex_t *module;
  284. if (bh_vector_get(&engine->modules, i, &module)) {
  285. os_mutex_destroy(&module->lock);
  286. wasm_runtime_free(module);
  287. }
  288. }
  289. bh_vector_destroy(&engine->modules);
  290. #ifndef os_thread_local_attribute
  291. bh_vector_destroy(&engine->stores_by_tid);
  292. #endif
  293. wasm_runtime_free(engine);
  294. }
  295. wasm_runtime_destroy();
  296. }
  297. static wasm_engine_t *
  298. wasm_engine_new_internal(mem_alloc_type_t type, const MemAllocOption *opts)
  299. {
  300. wasm_engine_t *engine = NULL;
  301. /* init runtime */
  302. RuntimeInitArgs init_args = { 0 };
  303. init_args.mem_alloc_type = type;
  304. #ifndef NDEBUG
  305. bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
  306. #else
  307. bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
  308. #endif
  309. WASM_C_DUMP_PROC_MEM();
  310. if (type == Alloc_With_Pool) {
  311. if (!opts) {
  312. return NULL;
  313. }
  314. init_args.mem_alloc_option.pool.heap_buf = opts->pool.heap_buf;
  315. init_args.mem_alloc_option.pool.heap_size = opts->pool.heap_size;
  316. }
  317. else if (type == Alloc_With_Allocator) {
  318. if (!opts) {
  319. return NULL;
  320. }
  321. init_args.mem_alloc_option.allocator.malloc_func =
  322. opts->allocator.malloc_func;
  323. init_args.mem_alloc_option.allocator.free_func =
  324. opts->allocator.free_func;
  325. init_args.mem_alloc_option.allocator.realloc_func =
  326. opts->allocator.realloc_func;
  327. #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
  328. init_args.mem_alloc_option.allocator.user_data =
  329. opts->allocator.user_data;
  330. #endif
  331. }
  332. else {
  333. init_args.mem_alloc_option.pool.heap_buf = NULL;
  334. init_args.mem_alloc_option.pool.heap_size = 0;
  335. }
  336. if (!wasm_runtime_full_init(&init_args)) {
  337. LOG_DEBUG("wasm_runtime_full_init failed");
  338. goto failed;
  339. }
  340. /* create wasm_engine_t */
  341. if (!(engine = malloc_internal(sizeof(wasm_engine_t)))) {
  342. goto failed;
  343. }
  344. if (!bh_vector_init(&engine->modules, DEFAULT_VECTOR_INIT_SIZE,
  345. sizeof(wasm_module_ex_t *), true))
  346. goto failed;
  347. #ifndef os_thread_local_attribute
  348. if (!bh_vector_init(&engine->stores_by_tid, DEFAULT_VECTOR_INIT_SIZE,
  349. sizeof(thread_local_stores), true))
  350. goto failed;
  351. #endif
  352. engine->ref_count = 1;
  353. WASM_C_DUMP_PROC_MEM();
  354. RETURN_OBJ(engine, wasm_engine_delete_internal)
  355. }
  356. /* global engine instance */
  357. static wasm_engine_t *singleton_engine;
  358. #ifdef os_thread_local_attribute
  359. /* categorize wasm_store_t as threads*/
  360. static os_thread_local_attribute unsigned thread_local_stores_num = 0;
  361. #endif
  362. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  363. /**
  364. * lock for the singleton_engine
  365. * Note: if the platform has mutex initializer, we use a global lock to
  366. * lock the operations of the singleton_engine, otherwise when there are
  367. * operations happening simultaneously in multiple threads, developer
  368. * must create the lock by himself, and use it to lock the operations
  369. */
  370. static korp_mutex engine_lock = OS_THREAD_MUTEX_INITIALIZER;
  371. #endif
  372. own wasm_engine_t *
  373. wasm_engine_new_with_args(mem_alloc_type_t type, const MemAllocOption *opts)
  374. {
  375. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  376. os_mutex_lock(&engine_lock);
  377. #endif
  378. if (!singleton_engine)
  379. singleton_engine = wasm_engine_new_internal(type, opts);
  380. else
  381. singleton_engine->ref_count++;
  382. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  383. os_mutex_unlock(&engine_lock);
  384. #endif
  385. return singleton_engine;
  386. }
  387. own wasm_engine_t *
  388. wasm_engine_new()
  389. {
  390. return wasm_engine_new_with_args(Alloc_With_System_Allocator, NULL);
  391. }
  392. own wasm_engine_t *
  393. wasm_engine_new_with_config(own wasm_config_t *config)
  394. {
  395. (void)config;
  396. return wasm_engine_new_with_args(Alloc_With_System_Allocator, NULL);
  397. }
  398. void
  399. wasm_engine_delete(wasm_engine_t *engine)
  400. {
  401. if (!engine)
  402. return;
  403. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  404. os_mutex_lock(&engine_lock);
  405. #endif
  406. if (!singleton_engine) {
  407. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  408. os_mutex_unlock(&engine_lock);
  409. #endif
  410. return;
  411. }
  412. bh_assert(engine == singleton_engine);
  413. bh_assert(singleton_engine->ref_count > 0);
  414. singleton_engine->ref_count--;
  415. if (singleton_engine->ref_count == 0) {
  416. wasm_engine_delete_internal(engine);
  417. singleton_engine = NULL;
  418. }
  419. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  420. os_mutex_unlock(&engine_lock);
  421. #endif
  422. }
  423. #ifndef os_thread_local_attribute
  424. static bool
  425. search_thread_local_store_num(Vector *stores_by_tid, korp_tid tid,
  426. thread_local_stores *out_ts, unsigned *out_i)
  427. {
  428. unsigned i;
  429. for (i = 0; i < stores_by_tid->num_elems; i++) {
  430. bool ret = bh_vector_get(stores_by_tid, i, out_ts);
  431. bh_assert(ret);
  432. (void)ret;
  433. if (out_ts->tid == tid) {
  434. *out_i = i;
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. #endif
  441. static unsigned
  442. retrive_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  443. {
  444. #ifndef os_thread_local_attribute
  445. unsigned i = 0;
  446. thread_local_stores ts = { 0 };
  447. unsigned ret = 0;
  448. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  449. os_mutex_lock(&engine_lock);
  450. #endif
  451. if (search_thread_local_store_num(stores_by_tid, tid, &ts, &i))
  452. ret = ts.stores_num;
  453. else
  454. ret = 0;
  455. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  456. os_mutex_unlock(&engine_lock);
  457. #endif
  458. return ret;
  459. #else
  460. (void)stores_by_tid;
  461. (void)tid;
  462. return thread_local_stores_num;
  463. #endif
  464. }
  465. static bool
  466. increase_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  467. {
  468. #ifndef os_thread_local_attribute
  469. unsigned i = 0;
  470. thread_local_stores ts = { 0 };
  471. bool ret = false;
  472. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  473. os_mutex_lock(&engine_lock);
  474. #endif
  475. if (search_thread_local_store_num(stores_by_tid, tid, &ts, &i)) {
  476. /* just in case if integer overflow */
  477. if (ts.stores_num + 1 < ts.stores_num) {
  478. ret = false;
  479. }
  480. else {
  481. ts.stores_num++;
  482. ret = bh_vector_set(stores_by_tid, i, &ts);
  483. bh_assert(ret);
  484. }
  485. }
  486. else {
  487. ts.tid = tid;
  488. ts.stores_num = 1;
  489. ret = bh_vector_append(stores_by_tid, &ts);
  490. }
  491. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  492. os_mutex_unlock(&engine_lock);
  493. #endif
  494. return ret;
  495. #else
  496. (void)stores_by_tid;
  497. (void)tid;
  498. /* just in case if integer overflow */
  499. if (thread_local_stores_num + 1 < thread_local_stores_num)
  500. return false;
  501. thread_local_stores_num++;
  502. return true;
  503. #endif
  504. }
  505. static bool
  506. decrease_thread_local_store_num(Vector *stores_by_tid, korp_tid tid)
  507. {
  508. #ifndef os_thread_local_attribute
  509. unsigned i = 0;
  510. thread_local_stores ts = { 0 };
  511. bool ret = false;
  512. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  513. os_mutex_lock(&engine_lock);
  514. #endif
  515. ret = search_thread_local_store_num(stores_by_tid, tid, &ts, &i);
  516. bh_assert(ret);
  517. /* just in case if integer overflow */
  518. if (ts.stores_num - 1 > ts.stores_num) {
  519. ret = false;
  520. }
  521. else {
  522. ts.stores_num--;
  523. ret = bh_vector_set(stores_by_tid, i, &ts);
  524. bh_assert(ret);
  525. }
  526. #if defined(OS_THREAD_MUTEX_INITIALIZER)
  527. os_mutex_unlock(&engine_lock);
  528. #endif
  529. return ret;
  530. #else
  531. (void)stores_by_tid;
  532. (void)tid;
  533. /* just in case if integer overflow */
  534. if (thread_local_stores_num - 1 > thread_local_stores_num)
  535. return false;
  536. thread_local_stores_num--;
  537. return true;
  538. #endif
  539. }
  540. wasm_store_t *
  541. wasm_store_new(wasm_engine_t *engine)
  542. {
  543. wasm_store_t *store = NULL;
  544. WASM_C_DUMP_PROC_MEM();
  545. if (!engine || singleton_engine != engine)
  546. return NULL;
  547. if (!retrive_thread_local_store_num(&engine->stores_by_tid,
  548. os_self_thread())) {
  549. if (!wasm_runtime_init_thread_env()) {
  550. LOG_ERROR("init thread environment failed");
  551. return NULL;
  552. }
  553. if (!increase_thread_local_store_num(&engine->stores_by_tid,
  554. os_self_thread())) {
  555. wasm_runtime_destroy_thread_env();
  556. return NULL;
  557. }
  558. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  559. decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  560. os_self_thread());
  561. wasm_runtime_destroy_thread_env();
  562. return NULL;
  563. }
  564. }
  565. else {
  566. if (!increase_thread_local_store_num(&engine->stores_by_tid,
  567. os_self_thread()))
  568. return NULL;
  569. if (!(store = malloc_internal(sizeof(wasm_store_t)))) {
  570. decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  571. os_self_thread());
  572. return NULL;
  573. }
  574. }
  575. /* new a vector, and new its data */
  576. INIT_VEC(store->modules, wasm_module_vec_new_uninitialized,
  577. DEFAULT_VECTOR_INIT_LENGTH);
  578. INIT_VEC(store->instances, wasm_instance_vec_new_uninitialized,
  579. DEFAULT_VECTOR_INIT_LENGTH);
  580. if (!(store->foreigns = malloc_internal(sizeof(Vector)))
  581. || !(bh_vector_init(store->foreigns, 24, sizeof(wasm_foreign_t *),
  582. true))) {
  583. goto failed;
  584. }
  585. WASM_C_DUMP_PROC_MEM();
  586. return store;
  587. failed:
  588. wasm_store_delete(store);
  589. return NULL;
  590. }
  591. void
  592. wasm_store_delete(wasm_store_t *store)
  593. {
  594. if (!store) {
  595. return;
  596. }
  597. DEINIT_VEC(store->instances, wasm_instance_vec_delete);
  598. DEINIT_VEC(store->modules, wasm_module_vec_delete);
  599. if (store->foreigns) {
  600. bh_vector_destroy(store->foreigns);
  601. wasm_runtime_free(store->foreigns);
  602. }
  603. wasm_runtime_free(store);
  604. if (decrease_thread_local_store_num(&singleton_engine->stores_by_tid,
  605. os_self_thread())) {
  606. if (!retrive_thread_local_store_num(&singleton_engine->stores_by_tid,
  607. os_self_thread())) {
  608. wasm_runtime_destroy_thread_env();
  609. }
  610. }
  611. }
  612. /* Type Representations */
  613. static inline wasm_valkind_t
  614. val_type_rt_2_valkind(uint8 val_type_rt)
  615. {
  616. switch (val_type_rt) {
  617. #define WAMR_VAL_TYPE_2_WASM_VAL_KIND(name) \
  618. case VALUE_TYPE_##name: \
  619. return WASM_##name;
  620. WAMR_VAL_TYPE_2_WASM_VAL_KIND(I32)
  621. WAMR_VAL_TYPE_2_WASM_VAL_KIND(I64)
  622. WAMR_VAL_TYPE_2_WASM_VAL_KIND(F32)
  623. WAMR_VAL_TYPE_2_WASM_VAL_KIND(F64)
  624. WAMR_VAL_TYPE_2_WASM_VAL_KIND(FUNCREF)
  625. #undef WAMR_VAL_TYPE_2_WASM_VAL_KIND
  626. default:
  627. return WASM_ANYREF;
  628. }
  629. }
  630. static wasm_valtype_t *
  631. wasm_valtype_new_internal(uint8 val_type_rt)
  632. {
  633. return wasm_valtype_new(val_type_rt_2_valkind(val_type_rt));
  634. }
  635. wasm_valtype_t *
  636. wasm_valtype_new(wasm_valkind_t kind)
  637. {
  638. wasm_valtype_t *val_type;
  639. if (kind > WASM_F64 && WASM_FUNCREF != kind
  640. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  641. && WASM_ANYREF != kind
  642. #endif
  643. ) {
  644. return NULL;
  645. }
  646. if (!(val_type = malloc_internal(sizeof(wasm_valtype_t)))) {
  647. return NULL;
  648. }
  649. val_type->kind = kind;
  650. return val_type;
  651. }
  652. void
  653. wasm_valtype_delete(wasm_valtype_t *val_type)
  654. {
  655. if (val_type) {
  656. wasm_runtime_free(val_type);
  657. }
  658. }
  659. wasm_valtype_t *
  660. wasm_valtype_copy(const wasm_valtype_t *src)
  661. {
  662. return src ? wasm_valtype_new(src->kind) : NULL;
  663. }
  664. wasm_valkind_t
  665. wasm_valtype_kind(const wasm_valtype_t *val_type)
  666. {
  667. return val_type ? val_type->kind : WASM_ANYREF;
  668. }
  669. static wasm_functype_t *
  670. wasm_functype_new_internal(WASMFuncType *type_rt)
  671. {
  672. wasm_functype_t *type = NULL;
  673. wasm_valtype_t *param_type = NULL, *result_type = NULL;
  674. uint32 i = 0;
  675. if (!type_rt) {
  676. return NULL;
  677. }
  678. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  679. return NULL;
  680. }
  681. type->extern_kind = WASM_EXTERN_FUNC;
  682. /* WASMFuncType->types[0 : type_rt->param_count) -> type->params */
  683. INIT_VEC(type->params, wasm_valtype_vec_new_uninitialized,
  684. type_rt->param_count);
  685. for (i = 0; i < type_rt->param_count; ++i) {
  686. if (!(param_type = wasm_valtype_new_internal(*(type_rt->types + i)))) {
  687. goto failed;
  688. }
  689. if (!bh_vector_append((Vector *)type->params, &param_type)) {
  690. LOG_DEBUG("bh_vector_append failed");
  691. goto failed;
  692. }
  693. }
  694. /* WASMFuncType->types[type_rt->param_count : type_rt->result_count) ->
  695. * type->results */
  696. INIT_VEC(type->results, wasm_valtype_vec_new_uninitialized,
  697. type_rt->result_count);
  698. for (i = 0; i < type_rt->result_count; ++i) {
  699. if (!(result_type = wasm_valtype_new_internal(
  700. *(type_rt->types + type_rt->param_count + i)))) {
  701. goto failed;
  702. }
  703. if (!bh_vector_append((Vector *)type->results, &result_type)) {
  704. LOG_DEBUG("bh_vector_append failed");
  705. goto failed;
  706. }
  707. }
  708. return type;
  709. failed:
  710. wasm_valtype_delete(param_type);
  711. wasm_valtype_delete(result_type);
  712. wasm_functype_delete(type);
  713. return NULL;
  714. }
  715. wasm_functype_t *
  716. wasm_functype_new(own wasm_valtype_vec_t *params,
  717. own wasm_valtype_vec_t *results)
  718. {
  719. wasm_functype_t *type = NULL;
  720. if (!(type = malloc_internal(sizeof(wasm_functype_t)))) {
  721. goto failed;
  722. }
  723. type->extern_kind = WASM_EXTERN_FUNC;
  724. /* take ownership */
  725. if (!(type->params = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  726. goto failed;
  727. }
  728. if (params) {
  729. bh_memcpy_s(type->params, sizeof(wasm_valtype_vec_t), params,
  730. sizeof(wasm_valtype_vec_t));
  731. }
  732. if (!(type->results = malloc_internal(sizeof(wasm_valtype_vec_t)))) {
  733. goto failed;
  734. }
  735. if (results) {
  736. bh_memcpy_s(type->results, sizeof(wasm_valtype_vec_t), results,
  737. sizeof(wasm_valtype_vec_t));
  738. }
  739. return type;
  740. failed:
  741. wasm_functype_delete(type);
  742. return NULL;
  743. }
  744. wasm_functype_t *
  745. wasm_functype_copy(const wasm_functype_t *src)
  746. {
  747. wasm_functype_t *functype;
  748. wasm_valtype_vec_t params = { 0 }, results = { 0 };
  749. if (!src) {
  750. return NULL;
  751. }
  752. wasm_valtype_vec_copy(&params, src->params);
  753. if (src->params->size && !params.data) {
  754. goto failed;
  755. }
  756. wasm_valtype_vec_copy(&results, src->results);
  757. if (src->results->size && !results.data) {
  758. goto failed;
  759. }
  760. if (!(functype = wasm_functype_new(&params, &results))) {
  761. goto failed;
  762. }
  763. return functype;
  764. failed:
  765. wasm_valtype_vec_delete(&params);
  766. wasm_valtype_vec_delete(&results);
  767. return NULL;
  768. }
  769. void
  770. wasm_functype_delete(wasm_functype_t *func_type)
  771. {
  772. if (!func_type) {
  773. return;
  774. }
  775. DEINIT_VEC(func_type->params, wasm_valtype_vec_delete);
  776. DEINIT_VEC(func_type->results, wasm_valtype_vec_delete);
  777. wasm_runtime_free(func_type);
  778. }
  779. const wasm_valtype_vec_t *
  780. wasm_functype_params(const wasm_functype_t *func_type)
  781. {
  782. if (!func_type) {
  783. return NULL;
  784. }
  785. return func_type->params;
  786. }
  787. const wasm_valtype_vec_t *
  788. wasm_functype_results(const wasm_functype_t *func_type)
  789. {
  790. if (!func_type) {
  791. return NULL;
  792. }
  793. return func_type->results;
  794. }
  795. static bool
  796. cmp_val_kind_with_val_type(wasm_valkind_t v_k, uint8 v_t)
  797. {
  798. return (v_k == WASM_I32 && v_t == VALUE_TYPE_I32)
  799. || (v_k == WASM_I64 && v_t == VALUE_TYPE_I64)
  800. || (v_k == WASM_F32 && v_t == VALUE_TYPE_F32)
  801. || (v_k == WASM_F64 && v_t == VALUE_TYPE_F64)
  802. || (v_k == WASM_ANYREF && v_t == VALUE_TYPE_EXTERNREF)
  803. || (v_k == WASM_FUNCREF && v_t == VALUE_TYPE_FUNCREF);
  804. }
  805. /*
  806. *to compare a function type of wasm-c-api with a function type of wasm_runtime
  807. */
  808. static bool
  809. wasm_functype_same_internal(const wasm_functype_t *type,
  810. const WASMFuncType *type_intl)
  811. {
  812. uint32 i = 0;
  813. if (!type || !type_intl || type->params->num_elems != type_intl->param_count
  814. || type->results->num_elems != type_intl->result_count)
  815. return false;
  816. for (i = 0; i < type->params->num_elems; i++) {
  817. wasm_valtype_t *v_t = type->params->data[i];
  818. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(v_t),
  819. type_intl->types[i]))
  820. return false;
  821. }
  822. for (i = 0; i < type->results->num_elems; i++) {
  823. wasm_valtype_t *v_t = type->results->data[i];
  824. if (!cmp_val_kind_with_val_type(
  825. wasm_valtype_kind(v_t),
  826. type_intl->types[i + type->params->num_elems]))
  827. return false;
  828. }
  829. return true;
  830. }
  831. wasm_globaltype_t *
  832. wasm_globaltype_new(own wasm_valtype_t *val_type, wasm_mutability_t mut)
  833. {
  834. wasm_globaltype_t *global_type = NULL;
  835. if (!val_type) {
  836. return NULL;
  837. }
  838. if (!(global_type = malloc_internal(sizeof(wasm_globaltype_t)))) {
  839. return NULL;
  840. }
  841. global_type->extern_kind = WASM_EXTERN_GLOBAL;
  842. global_type->val_type = val_type;
  843. global_type->mutability = mut;
  844. return global_type;
  845. }
  846. wasm_globaltype_t *
  847. wasm_globaltype_new_internal(uint8 val_type_rt, bool is_mutable)
  848. {
  849. wasm_globaltype_t *globaltype;
  850. wasm_valtype_t *val_type;
  851. if (!(val_type = wasm_valtype_new(val_type_rt_2_valkind(val_type_rt)))) {
  852. return NULL;
  853. }
  854. if (!(globaltype = wasm_globaltype_new(
  855. val_type, is_mutable ? WASM_VAR : WASM_CONST))) {
  856. wasm_valtype_delete(val_type);
  857. }
  858. return globaltype;
  859. }
  860. void
  861. wasm_globaltype_delete(wasm_globaltype_t *global_type)
  862. {
  863. if (!global_type) {
  864. return;
  865. }
  866. if (global_type->val_type) {
  867. wasm_valtype_delete(global_type->val_type);
  868. global_type->val_type = NULL;
  869. }
  870. wasm_runtime_free(global_type);
  871. }
  872. wasm_globaltype_t *
  873. wasm_globaltype_copy(const wasm_globaltype_t *src)
  874. {
  875. wasm_globaltype_t *global_type;
  876. wasm_valtype_t *val_type;
  877. if (!src) {
  878. return NULL;
  879. }
  880. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  881. return NULL;
  882. }
  883. if (!(global_type = wasm_globaltype_new(val_type, src->mutability))) {
  884. wasm_valtype_delete(val_type);
  885. }
  886. return global_type;
  887. }
  888. const wasm_valtype_t *
  889. wasm_globaltype_content(const wasm_globaltype_t *global_type)
  890. {
  891. if (!global_type) {
  892. return NULL;
  893. }
  894. return global_type->val_type;
  895. }
  896. wasm_mutability_t
  897. wasm_globaltype_mutability(const wasm_globaltype_t *global_type)
  898. {
  899. if (!global_type) {
  900. return false;
  901. }
  902. return global_type->mutability;
  903. }
  904. static wasm_tabletype_t *
  905. wasm_tabletype_new_internal(uint8 val_type_rt, uint32 init_size,
  906. uint32 max_size)
  907. {
  908. wasm_tabletype_t *table_type;
  909. wasm_limits_t limits = { init_size, max_size };
  910. wasm_valtype_t *val_type;
  911. if (!(val_type = wasm_valtype_new_internal(val_type_rt))) {
  912. return NULL;
  913. }
  914. if (!(table_type = wasm_tabletype_new(val_type, &limits))) {
  915. wasm_valtype_delete(val_type);
  916. }
  917. return table_type;
  918. }
  919. wasm_tabletype_t *
  920. wasm_tabletype_new(own wasm_valtype_t *val_type, const wasm_limits_t *limits)
  921. {
  922. wasm_tabletype_t *table_type = NULL;
  923. if (!val_type || !limits) {
  924. return NULL;
  925. }
  926. if (wasm_valtype_kind(val_type) != WASM_FUNCREF
  927. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  928. && wasm_valtype_kind(val_type) != WASM_ANYREF
  929. #endif
  930. ) {
  931. return NULL;
  932. }
  933. if (!(table_type = malloc_internal(sizeof(wasm_tabletype_t)))) {
  934. return NULL;
  935. }
  936. table_type->extern_kind = WASM_EXTERN_TABLE;
  937. table_type->val_type = val_type;
  938. table_type->limits.min = limits->min;
  939. table_type->limits.max = limits->max;
  940. return table_type;
  941. }
  942. wasm_tabletype_t *
  943. wasm_tabletype_copy(const wasm_tabletype_t *src)
  944. {
  945. wasm_tabletype_t *table_type;
  946. wasm_valtype_t *val_type;
  947. if (!src) {
  948. return NULL;
  949. }
  950. if (!(val_type = wasm_valtype_copy(src->val_type))) {
  951. return NULL;
  952. }
  953. if (!(table_type = wasm_tabletype_new(val_type, &src->limits))) {
  954. wasm_valtype_delete(val_type);
  955. }
  956. return table_type;
  957. }
  958. void
  959. wasm_tabletype_delete(wasm_tabletype_t *table_type)
  960. {
  961. if (!table_type) {
  962. return;
  963. }
  964. if (table_type->val_type) {
  965. wasm_valtype_delete(table_type->val_type);
  966. table_type->val_type = NULL;
  967. }
  968. wasm_runtime_free(table_type);
  969. }
  970. const wasm_valtype_t *
  971. wasm_tabletype_element(const wasm_tabletype_t *table_type)
  972. {
  973. if (!table_type) {
  974. return NULL;
  975. }
  976. return table_type->val_type;
  977. }
  978. const wasm_limits_t *
  979. wasm_tabletype_limits(const wasm_tabletype_t *table_type)
  980. {
  981. if (!table_type) {
  982. return NULL;
  983. }
  984. return &(table_type->limits);
  985. }
  986. static wasm_memorytype_t *
  987. wasm_memorytype_new_internal(uint32 min_pages, uint32 max_pages)
  988. {
  989. wasm_limits_t limits = { min_pages, max_pages };
  990. return wasm_memorytype_new(&limits);
  991. }
  992. wasm_memorytype_t *
  993. wasm_memorytype_new(const wasm_limits_t *limits)
  994. {
  995. wasm_memorytype_t *memory_type = NULL;
  996. if (!limits) {
  997. return NULL;
  998. }
  999. if (!(memory_type = malloc_internal(sizeof(wasm_memorytype_t)))) {
  1000. return NULL;
  1001. }
  1002. memory_type->extern_kind = WASM_EXTERN_MEMORY;
  1003. memory_type->limits.min = limits->min;
  1004. memory_type->limits.max = limits->max;
  1005. return memory_type;
  1006. }
  1007. wasm_memorytype_t *
  1008. wasm_memorytype_copy(const wasm_memorytype_t *src)
  1009. {
  1010. if (!src) {
  1011. return NULL;
  1012. }
  1013. return wasm_memorytype_new(&src->limits);
  1014. }
  1015. void
  1016. wasm_memorytype_delete(wasm_memorytype_t *memory_type)
  1017. {
  1018. if (memory_type) {
  1019. wasm_runtime_free(memory_type);
  1020. }
  1021. }
  1022. const wasm_limits_t *
  1023. wasm_memorytype_limits(const wasm_memorytype_t *memory_type)
  1024. {
  1025. if (!memory_type) {
  1026. return NULL;
  1027. }
  1028. return &(memory_type->limits);
  1029. }
  1030. wasm_externkind_t
  1031. wasm_externtype_kind(const wasm_externtype_t *extern_type)
  1032. {
  1033. if (!extern_type) {
  1034. return WASM_EXTERN_FUNC;
  1035. }
  1036. return extern_type->extern_kind;
  1037. }
  1038. #define BASIC_FOUR_TYPE_LIST(V) \
  1039. V(functype) \
  1040. V(globaltype) \
  1041. V(memorytype) \
  1042. V(tabletype)
  1043. #define WASM_EXTERNTYPE_AS_OTHERTYPE(name) \
  1044. wasm_##name##_t *wasm_externtype_as_##name(wasm_externtype_t *extern_type) \
  1045. { \
  1046. return (wasm_##name##_t *)extern_type; \
  1047. }
  1048. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE)
  1049. #undef WASM_EXTERNTYPE_AS_OTHERTYPE
  1050. #define WASM_OTHERTYPE_AS_EXTERNTYPE(name) \
  1051. wasm_externtype_t *wasm_##name##_as_externtype(wasm_##name##_t *other) \
  1052. { \
  1053. return (wasm_externtype_t *)other; \
  1054. }
  1055. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE)
  1056. #undef WASM_OTHERTYPE_AS_EXTERNTYPE
  1057. #define WASM_EXTERNTYPE_AS_OTHERTYPE_CONST(name) \
  1058. const wasm_##name##_t *wasm_externtype_as_##name##_const( \
  1059. const wasm_externtype_t *extern_type) \
  1060. { \
  1061. return (const wasm_##name##_t *)extern_type; \
  1062. }
  1063. BASIC_FOUR_TYPE_LIST(WASM_EXTERNTYPE_AS_OTHERTYPE_CONST)
  1064. #undef WASM_EXTERNTYPE_AS_OTHERTYPE_CONST
  1065. #define WASM_OTHERTYPE_AS_EXTERNTYPE_CONST(name) \
  1066. const wasm_externtype_t *wasm_##name##_as_externtype_const( \
  1067. const wasm_##name##_t *other) \
  1068. { \
  1069. return (const wasm_externtype_t *)other; \
  1070. }
  1071. BASIC_FOUR_TYPE_LIST(WASM_OTHERTYPE_AS_EXTERNTYPE_CONST)
  1072. #undef WASM_OTHERTYPE_AS_EXTERNTYPE_CONST
  1073. wasm_externtype_t *
  1074. wasm_externtype_copy(const wasm_externtype_t *src)
  1075. {
  1076. wasm_externtype_t *extern_type = NULL;
  1077. if (!src) {
  1078. return NULL;
  1079. }
  1080. switch (src->extern_kind) {
  1081. #define COPY_EXTERNTYPE(NAME, name) \
  1082. case WASM_EXTERN_##NAME: \
  1083. { \
  1084. extern_type = wasm_##name##_as_externtype( \
  1085. wasm_##name##_copy(wasm_externtype_as_##name##_const(src))); \
  1086. break; \
  1087. }
  1088. COPY_EXTERNTYPE(FUNC, functype)
  1089. COPY_EXTERNTYPE(GLOBAL, globaltype)
  1090. COPY_EXTERNTYPE(MEMORY, memorytype)
  1091. COPY_EXTERNTYPE(TABLE, tabletype)
  1092. #undef COPY_EXTERNTYPE
  1093. default:
  1094. LOG_WARNING("%s meets unsupported kind %u", __FUNCTION__,
  1095. src->extern_kind);
  1096. break;
  1097. }
  1098. return extern_type;
  1099. }
  1100. void
  1101. wasm_externtype_delete(wasm_externtype_t *extern_type)
  1102. {
  1103. if (!extern_type) {
  1104. return;
  1105. }
  1106. switch (wasm_externtype_kind(extern_type)) {
  1107. case WASM_EXTERN_FUNC:
  1108. wasm_functype_delete(wasm_externtype_as_functype(extern_type));
  1109. break;
  1110. case WASM_EXTERN_GLOBAL:
  1111. wasm_globaltype_delete(wasm_externtype_as_globaltype(extern_type));
  1112. break;
  1113. case WASM_EXTERN_MEMORY:
  1114. wasm_memorytype_delete(wasm_externtype_as_memorytype(extern_type));
  1115. break;
  1116. case WASM_EXTERN_TABLE:
  1117. wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type));
  1118. break;
  1119. default:
  1120. LOG_WARNING("%s meets unsupported type %u", __FUNCTION__,
  1121. wasm_externtype_kind(extern_type));
  1122. break;
  1123. }
  1124. }
  1125. own wasm_importtype_t *
  1126. wasm_importtype_new(own wasm_byte_vec_t *module_name,
  1127. own wasm_byte_vec_t *field_name,
  1128. own wasm_externtype_t *extern_type)
  1129. {
  1130. wasm_importtype_t *import_type = NULL;
  1131. if (!module_name || !field_name || !extern_type) {
  1132. return NULL;
  1133. }
  1134. if (!(import_type = malloc_internal(sizeof(wasm_importtype_t)))) {
  1135. return NULL;
  1136. }
  1137. /* take ownership */
  1138. if (!(import_type->module_name =
  1139. malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1140. goto failed;
  1141. }
  1142. bh_memcpy_s(import_type->module_name, sizeof(wasm_byte_vec_t), module_name,
  1143. sizeof(wasm_byte_vec_t));
  1144. if (!(import_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1145. goto failed;
  1146. }
  1147. bh_memcpy_s(import_type->name, sizeof(wasm_byte_vec_t), field_name,
  1148. sizeof(wasm_byte_vec_t));
  1149. import_type->extern_type = extern_type;
  1150. return import_type;
  1151. failed:
  1152. wasm_importtype_delete(import_type);
  1153. return NULL;
  1154. }
  1155. void
  1156. wasm_importtype_delete(own wasm_importtype_t *import_type)
  1157. {
  1158. if (!import_type) {
  1159. return;
  1160. }
  1161. DEINIT_VEC(import_type->module_name, wasm_byte_vec_delete);
  1162. DEINIT_VEC(import_type->name, wasm_byte_vec_delete);
  1163. wasm_externtype_delete(import_type->extern_type);
  1164. import_type->extern_type = NULL;
  1165. wasm_runtime_free(import_type);
  1166. }
  1167. own wasm_importtype_t *
  1168. wasm_importtype_copy(const wasm_importtype_t *src)
  1169. {
  1170. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1171. wasm_externtype_t *extern_type = NULL;
  1172. wasm_importtype_t *import_type = NULL;
  1173. if (!src) {
  1174. return NULL;
  1175. }
  1176. wasm_byte_vec_copy(&module_name, src->module_name);
  1177. if (src->module_name->size && !module_name.data) {
  1178. goto failed;
  1179. }
  1180. wasm_byte_vec_copy(&name, src->name);
  1181. if (src->name->size && !name.data) {
  1182. goto failed;
  1183. }
  1184. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1185. goto failed;
  1186. }
  1187. if (!(import_type =
  1188. wasm_importtype_new(&module_name, &name, extern_type))) {
  1189. goto failed;
  1190. }
  1191. return import_type;
  1192. failed:
  1193. wasm_byte_vec_delete(&module_name);
  1194. wasm_byte_vec_delete(&name);
  1195. wasm_externtype_delete(extern_type);
  1196. wasm_importtype_delete(import_type);
  1197. return NULL;
  1198. }
  1199. const wasm_byte_vec_t *
  1200. wasm_importtype_module(const wasm_importtype_t *import_type)
  1201. {
  1202. if (!import_type) {
  1203. return NULL;
  1204. }
  1205. return import_type->module_name;
  1206. }
  1207. const wasm_byte_vec_t *
  1208. wasm_importtype_name(const wasm_importtype_t *import_type)
  1209. {
  1210. if (!import_type) {
  1211. return NULL;
  1212. }
  1213. return import_type->name;
  1214. }
  1215. const wasm_externtype_t *
  1216. wasm_importtype_type(const wasm_importtype_t *import_type)
  1217. {
  1218. if (!import_type) {
  1219. return NULL;
  1220. }
  1221. return import_type->extern_type;
  1222. }
  1223. bool
  1224. wasm_importtype_is_linked(const wasm_importtype_t *import_type)
  1225. {
  1226. if (!import_type)
  1227. return false;
  1228. const wasm_name_t *module_name = wasm_importtype_module(import_type);
  1229. const wasm_name_t *field_name = wasm_importtype_name(import_type);
  1230. switch (wasm_externtype_kind(wasm_importtype_type(import_type))) {
  1231. case WASM_EXTERN_FUNC:
  1232. return wasm_runtime_is_import_func_linked(module_name->data,
  1233. field_name->data);
  1234. case WASM_EXTERN_GLOBAL:
  1235. return wasm_runtime_is_import_global_linked(module_name->data,
  1236. field_name->data);
  1237. case WASM_EXTERN_MEMORY:
  1238. case WASM_EXTERN_TABLE:
  1239. default:
  1240. break;
  1241. }
  1242. return false;
  1243. }
  1244. own wasm_exporttype_t *
  1245. wasm_exporttype_new(own wasm_byte_vec_t *name,
  1246. own wasm_externtype_t *extern_type)
  1247. {
  1248. wasm_exporttype_t *export_type = NULL;
  1249. if (!name || !extern_type) {
  1250. return NULL;
  1251. }
  1252. if (!(export_type = malloc_internal(sizeof(wasm_exporttype_t)))) {
  1253. return NULL;
  1254. }
  1255. if (!(export_type->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1256. wasm_exporttype_delete(export_type);
  1257. return NULL;
  1258. }
  1259. bh_memcpy_s(export_type->name, sizeof(wasm_byte_vec_t), name,
  1260. sizeof(wasm_byte_vec_t));
  1261. export_type->extern_type = extern_type;
  1262. return export_type;
  1263. }
  1264. wasm_exporttype_t *
  1265. wasm_exporttype_copy(const wasm_exporttype_t *src)
  1266. {
  1267. wasm_exporttype_t *export_type;
  1268. wasm_byte_vec_t name = { 0 };
  1269. wasm_externtype_t *extern_type = NULL;
  1270. if (!src) {
  1271. return NULL;
  1272. }
  1273. wasm_byte_vec_copy(&name, src->name);
  1274. if (src->name->size && !name.data) {
  1275. goto failed;
  1276. }
  1277. if (!(extern_type = wasm_externtype_copy(src->extern_type))) {
  1278. goto failed;
  1279. }
  1280. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  1281. goto failed;
  1282. }
  1283. return export_type;
  1284. failed:
  1285. wasm_byte_vec_delete(&name);
  1286. wasm_externtype_delete(extern_type);
  1287. return NULL;
  1288. }
  1289. void
  1290. wasm_exporttype_delete(wasm_exporttype_t *export_type)
  1291. {
  1292. if (!export_type) {
  1293. return;
  1294. }
  1295. DEINIT_VEC(export_type->name, wasm_byte_vec_delete);
  1296. wasm_externtype_delete(export_type->extern_type);
  1297. wasm_runtime_free(export_type);
  1298. }
  1299. const wasm_byte_vec_t *
  1300. wasm_exporttype_name(const wasm_exporttype_t *export_type)
  1301. {
  1302. if (!export_type) {
  1303. return NULL;
  1304. }
  1305. return export_type->name;
  1306. }
  1307. const wasm_externtype_t *
  1308. wasm_exporttype_type(const wasm_exporttype_t *export_type)
  1309. {
  1310. if (!export_type) {
  1311. return NULL;
  1312. }
  1313. return export_type->extern_type;
  1314. }
  1315. /* Runtime Objects */
  1316. void
  1317. wasm_val_delete(wasm_val_t *v)
  1318. {
  1319. if (v)
  1320. wasm_runtime_free(v);
  1321. }
  1322. void
  1323. wasm_val_copy(wasm_val_t *out, const wasm_val_t *src)
  1324. {
  1325. if (!out || !src) {
  1326. return;
  1327. }
  1328. bh_memcpy_s(out, sizeof(wasm_val_t), src, sizeof(wasm_val_t));
  1329. }
  1330. bool
  1331. rt_val_to_wasm_val(const uint8 *data, uint8 val_type_rt, wasm_val_t *out)
  1332. {
  1333. bool ret = true;
  1334. switch (val_type_rt) {
  1335. case VALUE_TYPE_I32:
  1336. out->kind = WASM_I32;
  1337. out->of.i32 = *((int32 *)data);
  1338. break;
  1339. case VALUE_TYPE_F32:
  1340. out->kind = WASM_F32;
  1341. out->of.f32 = *((float32 *)data);
  1342. break;
  1343. case VALUE_TYPE_I64:
  1344. out->kind = WASM_I64;
  1345. out->of.i64 = *((int64 *)data);
  1346. break;
  1347. case VALUE_TYPE_F64:
  1348. out->kind = WASM_F64;
  1349. out->of.f64 = *((float64 *)data);
  1350. break;
  1351. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1352. case VALUE_TYPE_EXTERNREF:
  1353. out->kind = WASM_ANYREF;
  1354. if (NULL_REF == *(uint32 *)data) {
  1355. out->of.ref = NULL;
  1356. }
  1357. else {
  1358. ret = wasm_externref_ref2obj(*(uint32 *)data,
  1359. (void **)&out->of.ref);
  1360. }
  1361. break;
  1362. #endif
  1363. default:
  1364. LOG_WARNING("unexpected value type %d", val_type_rt);
  1365. ret = false;
  1366. }
  1367. return ret;
  1368. }
  1369. bool
  1370. wasm_val_to_rt_val(WASMModuleInstanceCommon *inst_comm_rt, uint8 val_type_rt,
  1371. const wasm_val_t *v, uint8 *data)
  1372. {
  1373. bool ret = true;
  1374. switch (val_type_rt) {
  1375. case VALUE_TYPE_I32:
  1376. bh_assert(WASM_I32 == v->kind);
  1377. *((int32 *)data) = v->of.i32;
  1378. break;
  1379. case VALUE_TYPE_F32:
  1380. bh_assert(WASM_F32 == v->kind);
  1381. *((float32 *)data) = v->of.f32;
  1382. break;
  1383. case VALUE_TYPE_I64:
  1384. bh_assert(WASM_I64 == v->kind);
  1385. *((int64 *)data) = v->of.i64;
  1386. break;
  1387. case VALUE_TYPE_F64:
  1388. bh_assert(WASM_F64 == v->kind);
  1389. *((float64 *)data) = v->of.f64;
  1390. break;
  1391. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  1392. case VALUE_TYPE_EXTERNREF:
  1393. bh_assert(WASM_ANYREF == v->kind);
  1394. ret =
  1395. wasm_externref_obj2ref(inst_comm_rt, v->of.ref, (uint32 *)data);
  1396. break;
  1397. #endif
  1398. default:
  1399. LOG_WARNING("unexpected value type %d", val_type_rt);
  1400. ret = false;
  1401. break;
  1402. }
  1403. (void)inst_comm_rt;
  1404. return ret;
  1405. }
  1406. wasm_ref_t *
  1407. wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind,
  1408. uint32 ref_idx_rt, WASMModuleInstanceCommon *inst_comm_rt)
  1409. {
  1410. wasm_ref_t *ref;
  1411. if (!store) {
  1412. return NULL;
  1413. }
  1414. if (!(ref = malloc_internal(sizeof(wasm_ref_t)))) {
  1415. return NULL;
  1416. }
  1417. ref->store = store;
  1418. ref->kind = kind;
  1419. ref->ref_idx_rt = ref_idx_rt;
  1420. ref->inst_comm_rt = inst_comm_rt;
  1421. /* workaround */
  1422. if (WASM_REF_foreign == kind) {
  1423. wasm_foreign_t *foreign;
  1424. if (!(bh_vector_get(ref->store->foreigns, ref->ref_idx_rt, &foreign))
  1425. || !foreign) {
  1426. wasm_runtime_free(ref);
  1427. return NULL;
  1428. }
  1429. foreign->ref_cnt++;
  1430. }
  1431. /* others doesn't include ref counters */
  1432. return ref;
  1433. }
  1434. own wasm_ref_t *
  1435. wasm_ref_copy(const wasm_ref_t *src)
  1436. {
  1437. if (!src)
  1438. return NULL;
  1439. /* host_info are different in wasm_ref_t(s) */
  1440. return wasm_ref_new_internal(src->store, src->kind, src->ref_idx_rt,
  1441. src->inst_comm_rt);
  1442. }
  1443. #define DELETE_HOST_INFO(obj) \
  1444. if (obj->host_info.info) { \
  1445. if (obj->host_info.finalizer) { \
  1446. obj->host_info.finalizer(obj->host_info.info); \
  1447. } \
  1448. }
  1449. void
  1450. wasm_ref_delete(own wasm_ref_t *ref)
  1451. {
  1452. if (!ref || !ref->store)
  1453. return;
  1454. DELETE_HOST_INFO(ref);
  1455. if (WASM_REF_foreign == ref->kind) {
  1456. wasm_foreign_t *foreign = NULL;
  1457. if (bh_vector_get(ref->store->foreigns, ref->ref_idx_rt, &foreign)
  1458. && foreign) {
  1459. wasm_foreign_delete(foreign);
  1460. }
  1461. }
  1462. wasm_runtime_free(ref);
  1463. }
  1464. #define WASM_DEFINE_REF_BASE(name) \
  1465. bool wasm_##name##_same(const wasm_##name##_t *o1, \
  1466. const wasm_##name##_t *o2) \
  1467. { \
  1468. return (!o1 && !o2) ? true \
  1469. : (!o1 || !o2) ? false \
  1470. : (o1->kind != o2->kind) \
  1471. ? false \
  1472. : o1->name##_idx_rt == o2->name##_idx_rt; \
  1473. } \
  1474. \
  1475. void *wasm_##name##_get_host_info(const wasm_##name##_t *obj) \
  1476. { \
  1477. return obj ? obj->host_info.info : NULL; \
  1478. } \
  1479. \
  1480. void wasm_##name##_set_host_info(wasm_##name##_t *obj, void *host_info) \
  1481. { \
  1482. if (obj) { \
  1483. obj->host_info.info = host_info; \
  1484. obj->host_info.finalizer = NULL; \
  1485. } \
  1486. } \
  1487. \
  1488. void wasm_##name##_set_host_info_with_finalizer( \
  1489. wasm_##name##_t *obj, void *host_info, void (*finalizer)(void *)) \
  1490. { \
  1491. if (obj) { \
  1492. obj->host_info.info = host_info; \
  1493. obj->host_info.finalizer = finalizer; \
  1494. } \
  1495. }
  1496. #define WASM_DEFINE_REF(name) \
  1497. WASM_DEFINE_REF_BASE(name) \
  1498. \
  1499. wasm_ref_t *wasm_##name##_as_ref(wasm_##name##_t *name) \
  1500. { \
  1501. if (!name) { \
  1502. return NULL; \
  1503. } \
  1504. \
  1505. return wasm_ref_new_internal(name->store, WASM_REF_##name, \
  1506. name->name##_idx_rt, name->inst_comm_rt); \
  1507. } \
  1508. \
  1509. const wasm_ref_t *wasm_##name##_as_ref_const(const wasm_##name##_t *name) \
  1510. { \
  1511. if (!name) { \
  1512. return NULL; \
  1513. } \
  1514. \
  1515. return wasm_ref_new_internal(name->store, WASM_REF_##name, \
  1516. name->name##_idx_rt, name->inst_comm_rt); \
  1517. } \
  1518. \
  1519. wasm_##name##_t *wasm_ref_as_##name(wasm_ref_t *ref) \
  1520. { \
  1521. if (!ref || WASM_REF_##name != ref->kind) { \
  1522. return NULL; \
  1523. } \
  1524. \
  1525. return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \
  1526. ref->inst_comm_rt); \
  1527. } \
  1528. \
  1529. const wasm_##name##_t *wasm_ref_as_##name##_const(const wasm_ref_t *ref) \
  1530. { \
  1531. if (!ref || WASM_REF_##name != ref->kind) { \
  1532. return NULL; \
  1533. } \
  1534. \
  1535. return wasm_##name##_new_internal(ref->store, ref->ref_idx_rt, \
  1536. ref->inst_comm_rt); \
  1537. }
  1538. WASM_DEFINE_REF_BASE(ref)
  1539. WASM_DEFINE_REF(foreign)
  1540. WASM_DEFINE_REF(func)
  1541. WASM_DEFINE_REF(global)
  1542. WASM_DEFINE_REF(memory)
  1543. WASM_DEFINE_REF(table)
  1544. static wasm_frame_t *
  1545. wasm_frame_new(wasm_instance_t *instance, size_t module_offset,
  1546. uint32 func_index, size_t func_offset)
  1547. {
  1548. wasm_frame_t *frame;
  1549. if (!(frame = malloc_internal(sizeof(wasm_frame_t)))) {
  1550. return NULL;
  1551. }
  1552. frame->instance = instance;
  1553. frame->module_offset = (uint32)module_offset;
  1554. frame->func_index = func_index;
  1555. frame->func_offset = (uint32)func_offset;
  1556. return frame;
  1557. }
  1558. own wasm_frame_t *
  1559. wasm_frame_copy(const wasm_frame_t *src)
  1560. {
  1561. if (!src) {
  1562. return NULL;
  1563. }
  1564. return wasm_frame_new(src->instance, src->module_offset, src->func_index,
  1565. src->func_offset);
  1566. }
  1567. void
  1568. wasm_frame_delete(own wasm_frame_t *frame)
  1569. {
  1570. if (frame) {
  1571. wasm_runtime_free(frame);
  1572. }
  1573. }
  1574. struct wasm_instance_t *
  1575. wasm_frame_instance(const wasm_frame_t *frame)
  1576. {
  1577. return frame ? frame->instance : NULL;
  1578. }
  1579. size_t
  1580. wasm_frame_module_offset(const wasm_frame_t *frame)
  1581. {
  1582. return frame ? frame->module_offset : 0;
  1583. }
  1584. uint32_t
  1585. wasm_frame_func_index(const wasm_frame_t *frame)
  1586. {
  1587. return frame ? frame->func_index : 0;
  1588. }
  1589. size_t
  1590. wasm_frame_func_offset(const wasm_frame_t *frame)
  1591. {
  1592. return frame ? frame->func_offset : 0;
  1593. }
  1594. static wasm_trap_t *
  1595. wasm_trap_new_internal(wasm_store_t *store,
  1596. WASMModuleInstanceCommon *inst_comm_rt,
  1597. const char *error_info)
  1598. {
  1599. wasm_trap_t *trap;
  1600. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1601. wasm_instance_vec_t *instances;
  1602. wasm_instance_t *frame_instance = NULL;
  1603. uint32 i;
  1604. #endif
  1605. if (!singleton_engine)
  1606. return NULL;
  1607. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1608. return NULL;
  1609. }
  1610. /* fill in message */
  1611. if (error_info && strlen(error_info) > 0) {
  1612. if (!(trap->message = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  1613. goto failed;
  1614. }
  1615. wasm_name_new_from_string_nt(trap->message, error_info);
  1616. if (!trap->message->data) {
  1617. goto failed;
  1618. }
  1619. }
  1620. /* fill in frames */
  1621. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1622. trap->frames = ((WASMModuleInstance *)inst_comm_rt)->frames;
  1623. if (trap->frames) {
  1624. /* fill in instances */
  1625. instances = store->instances;
  1626. bh_assert(instances != NULL);
  1627. for (i = 0; i < instances->num_elems; i++) {
  1628. if (instances->data[i]->inst_comm_rt == inst_comm_rt) {
  1629. frame_instance = instances->data[i];
  1630. break;
  1631. }
  1632. }
  1633. for (i = 0; i < trap->frames->num_elems; i++) {
  1634. (((wasm_frame_t *)trap->frames->data) + i)->instance =
  1635. frame_instance;
  1636. }
  1637. }
  1638. #else
  1639. (void)store;
  1640. (void)inst_comm_rt;
  1641. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1642. return trap;
  1643. failed:
  1644. wasm_trap_delete(trap);
  1645. return NULL;
  1646. }
  1647. wasm_trap_t *
  1648. wasm_trap_new(wasm_store_t *store, const wasm_message_t *message)
  1649. {
  1650. wasm_trap_t *trap;
  1651. if (!store) {
  1652. return NULL;
  1653. }
  1654. if (!(trap = malloc_internal(sizeof(wasm_trap_t)))) {
  1655. return NULL;
  1656. }
  1657. if (message) {
  1658. INIT_VEC(trap->message, wasm_byte_vec_new, message->size,
  1659. message->data);
  1660. }
  1661. return trap;
  1662. failed:
  1663. wasm_trap_delete(trap);
  1664. return NULL;
  1665. }
  1666. void
  1667. wasm_trap_delete(wasm_trap_t *trap)
  1668. {
  1669. if (!trap) {
  1670. return;
  1671. }
  1672. DEINIT_VEC(trap->message, wasm_byte_vec_delete);
  1673. /* reuse frames of WASMModuleInstance, do not free it here */
  1674. wasm_runtime_free(trap);
  1675. }
  1676. void
  1677. wasm_trap_message(const wasm_trap_t *trap, own wasm_message_t *out)
  1678. {
  1679. if (!trap || !out) {
  1680. return;
  1681. }
  1682. wasm_byte_vec_copy(out, trap->message);
  1683. }
  1684. own wasm_frame_t *
  1685. wasm_trap_origin(const wasm_trap_t *trap)
  1686. {
  1687. wasm_frame_t *latest_frame;
  1688. if (!trap || !trap->frames || !trap->frames->num_elems) {
  1689. return NULL;
  1690. }
  1691. /* first frame is the latest frame */
  1692. latest_frame = (wasm_frame_t *)trap->frames->data;
  1693. return wasm_frame_copy(latest_frame);
  1694. }
  1695. void
  1696. wasm_trap_trace(const wasm_trap_t *trap, own wasm_frame_vec_t *out)
  1697. {
  1698. uint32 i;
  1699. if (!trap || !out) {
  1700. return;
  1701. }
  1702. if (!trap->frames || !trap->frames->num_elems) {
  1703. wasm_frame_vec_new_empty(out);
  1704. return;
  1705. }
  1706. wasm_frame_vec_new_uninitialized(out, trap->frames->num_elems);
  1707. if (out->size == 0 || !out->data) {
  1708. return;
  1709. }
  1710. for (i = 0; i < trap->frames->num_elems; i++) {
  1711. wasm_frame_t *frame;
  1712. frame = ((wasm_frame_t *)trap->frames->data) + i;
  1713. if (!(out->data[i] =
  1714. wasm_frame_new(frame->instance, frame->module_offset,
  1715. frame->func_index, frame->func_offset))) {
  1716. goto failed;
  1717. }
  1718. out->num_elems++;
  1719. }
  1720. return;
  1721. failed:
  1722. for (i = 0; i < out->num_elems; i++) {
  1723. if (out->data[i]) {
  1724. wasm_runtime_free(out->data[i]);
  1725. }
  1726. }
  1727. wasm_runtime_free(out->data);
  1728. }
  1729. wasm_foreign_t *
  1730. wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
  1731. WASMModuleInstanceCommon *inst_comm_rt)
  1732. {
  1733. wasm_foreign_t *foreign = NULL;
  1734. if (!store || !store->foreigns)
  1735. return NULL;
  1736. if (!(bh_vector_get(store->foreigns, foreign_idx_rt, &foreign))
  1737. || !foreign) {
  1738. return NULL;
  1739. }
  1740. foreign->ref_cnt++;
  1741. (void)inst_comm_rt;
  1742. return foreign;
  1743. }
  1744. own wasm_foreign_t *
  1745. wasm_foreign_new(wasm_store_t *store)
  1746. {
  1747. wasm_foreign_t *foreign;
  1748. if (!store)
  1749. return NULL;
  1750. if (!(foreign = malloc_internal(sizeof(wasm_foreign_t))))
  1751. return NULL;
  1752. foreign->store = store;
  1753. foreign->kind = WASM_REF_foreign;
  1754. foreign->foreign_idx_rt = (uint32)bh_vector_size(store->foreigns);
  1755. if (!(bh_vector_append(store->foreigns, &foreign))) {
  1756. wasm_runtime_free(foreign);
  1757. return NULL;
  1758. }
  1759. return foreign;
  1760. }
  1761. void
  1762. wasm_foreign_delete(wasm_foreign_t *foreign)
  1763. {
  1764. if (!foreign)
  1765. return;
  1766. if (foreign->ref_cnt < 1) {
  1767. return;
  1768. }
  1769. foreign->ref_cnt--;
  1770. if (!foreign->ref_cnt) {
  1771. wasm_runtime_free(foreign);
  1772. }
  1773. }
  1774. static inline wasm_module_t *
  1775. module_ext_to_module(wasm_module_ex_t *module_ex)
  1776. {
  1777. return (wasm_module_t *)module_ex;
  1778. }
  1779. static inline wasm_module_ex_t *
  1780. module_to_module_ext(wasm_module_t *module)
  1781. {
  1782. return (wasm_module_ex_t *)module;
  1783. }
  1784. #if WASM_ENABLE_INTERP != 0
  1785. #define MODULE_INTERP(module_comm) ((WASMModule *)(*module_comm))
  1786. #endif
  1787. #if WASM_ENABLE_AOT != 0
  1788. #define MODULE_AOT(module_comm) ((AOTModule *)(*module_comm))
  1789. #endif
  1790. #if WASM_ENABLE_WASM_CACHE != 0
  1791. static wasm_module_ex_t *
  1792. check_loaded_module(Vector *modules, char *binary_hash)
  1793. {
  1794. unsigned i;
  1795. wasm_module_ex_t *module = NULL;
  1796. for (i = 0; i < modules->num_elems; i++) {
  1797. bh_vector_get(modules, i, &module);
  1798. if (!module) {
  1799. LOG_ERROR("Unexpected failure at %d\n", __LINE__);
  1800. return NULL;
  1801. }
  1802. if (!module->ref_count)
  1803. /* deleted */
  1804. continue;
  1805. if (memcmp(module->hash, binary_hash, SHA256_DIGEST_LENGTH) == 0)
  1806. return module;
  1807. }
  1808. return NULL;
  1809. }
  1810. static wasm_module_ex_t *
  1811. try_reuse_loaded_module(wasm_store_t *store, char *binary_hash)
  1812. {
  1813. wasm_module_ex_t *cached = NULL;
  1814. wasm_module_ex_t *ret = NULL;
  1815. cached = check_loaded_module(&singleton_engine->modules, binary_hash);
  1816. if (!cached)
  1817. goto quit;
  1818. os_mutex_lock(&cached->lock);
  1819. if (!cached->ref_count)
  1820. goto unlock;
  1821. if (!bh_vector_append((Vector *)store->modules, &cached))
  1822. goto unlock;
  1823. cached->ref_count += 1;
  1824. ret = cached;
  1825. unlock:
  1826. os_mutex_unlock(&cached->lock);
  1827. quit:
  1828. return ret;
  1829. }
  1830. #endif /* WASM_ENABLE_WASM_CACHE != 0 */
  1831. wasm_module_t *
  1832. wasm_module_new(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1833. {
  1834. char error_buf[128] = { 0 };
  1835. wasm_module_ex_t *module_ex = NULL;
  1836. #if WASM_ENABLE_WASM_CACHE != 0
  1837. char binary_hash[SHA256_DIGEST_LENGTH] = { 0 };
  1838. #endif
  1839. bh_assert(singleton_engine);
  1840. if (!store || !binary || binary->size == 0 || binary->size > UINT32_MAX)
  1841. goto quit;
  1842. /* whether the combination of compilation flags are compatable with the
  1843. * package type */
  1844. {
  1845. PackageType pkg_type;
  1846. pkg_type =
  1847. get_package_type((uint8 *)binary->data, (uint32)binary->size);
  1848. bool result = false;
  1849. #if WASM_ENABLE_INTERP != 0
  1850. result = (pkg_type == Wasm_Module_Bytecode);
  1851. #endif
  1852. #if WASM_ENABLE_AOT != 0
  1853. result = result || (pkg_type == Wasm_Module_AoT);
  1854. #endif
  1855. if (!result) {
  1856. LOG_VERBOSE("current building isn't compatiable with the module,"
  1857. "may need recompile");
  1858. goto quit;
  1859. }
  1860. }
  1861. #if WASM_ENABLE_WASM_CACHE != 0
  1862. /* if cached */
  1863. SHA256((void *)binary->data, binary->num_elems, (uint8_t *)binary_hash);
  1864. module_ex = try_reuse_loaded_module(store, binary_hash);
  1865. if (module_ex)
  1866. return module_ext_to_module(module_ex);
  1867. #endif
  1868. WASM_C_DUMP_PROC_MEM();
  1869. module_ex = malloc_internal(sizeof(wasm_module_ex_t));
  1870. if (!module_ex)
  1871. goto quit;
  1872. module_ex->binary = malloc_internal(sizeof(wasm_byte_vec_t));
  1873. if (!module_ex->binary)
  1874. goto free_module;
  1875. wasm_byte_vec_copy(module_ex->binary, binary);
  1876. if (!module_ex->binary->data)
  1877. goto free_binary;
  1878. module_ex->module_comm_rt = wasm_runtime_load(
  1879. (uint8 *)module_ex->binary->data, (uint32)module_ex->binary->size,
  1880. error_buf, (uint32)sizeof(error_buf));
  1881. if (!(module_ex->module_comm_rt)) {
  1882. LOG_ERROR(error_buf);
  1883. goto free_vec;
  1884. }
  1885. /* append it to a watching list in store */
  1886. if (!bh_vector_append((Vector *)store->modules, &module_ex))
  1887. goto unload;
  1888. if (os_mutex_init(&module_ex->lock) != BHT_OK)
  1889. goto remove_last;
  1890. if (!bh_vector_append(&singleton_engine->modules, &module_ex))
  1891. goto destroy_lock;
  1892. #if WASM_ENABLE_WASM_CACHE != 0
  1893. bh_memcpy_s(module_ex->hash, sizeof(module_ex->hash), binary_hash,
  1894. sizeof(binary_hash));
  1895. #endif
  1896. module_ex->ref_count = 1;
  1897. WASM_C_DUMP_PROC_MEM();
  1898. return module_ext_to_module(module_ex);
  1899. destroy_lock:
  1900. os_mutex_destroy(&module_ex->lock);
  1901. remove_last:
  1902. bh_vector_remove((Vector *)store->modules,
  1903. (uint32)(store->modules->num_elems - 1), NULL);
  1904. unload:
  1905. wasm_runtime_unload(module_ex->module_comm_rt);
  1906. free_vec:
  1907. wasm_byte_vec_delete(module_ex->binary);
  1908. free_binary:
  1909. wasm_runtime_free(module_ex->binary);
  1910. free_module:
  1911. wasm_runtime_free(module_ex);
  1912. quit:
  1913. LOG_ERROR("%s failed", __FUNCTION__);
  1914. return NULL;
  1915. }
  1916. bool
  1917. wasm_module_validate(wasm_store_t *store, const wasm_byte_vec_t *binary)
  1918. {
  1919. wasm_byte_vec_t local_binary = { 0 };
  1920. struct WASMModuleCommon *module_rt;
  1921. char error_buf[128] = { 0 };
  1922. bool ret;
  1923. bh_assert(singleton_engine);
  1924. if (!store || !binary || binary->size > UINT32_MAX) {
  1925. LOG_ERROR("%s failed", __FUNCTION__);
  1926. return false;
  1927. }
  1928. /* make a copy of binary */
  1929. wasm_byte_vec_copy(&local_binary, binary);
  1930. if (binary->size && !local_binary.data)
  1931. return false;
  1932. module_rt = wasm_runtime_load((uint8 *)local_binary.data,
  1933. (uint32)local_binary.size, error_buf, 128);
  1934. wasm_byte_vec_delete(&local_binary);
  1935. if (module_rt) {
  1936. wasm_runtime_unload(module_rt);
  1937. ret = true;
  1938. }
  1939. else {
  1940. ret = false;
  1941. LOG_VERBOSE(error_buf);
  1942. }
  1943. return ret;
  1944. }
  1945. static void
  1946. wasm_module_delete_internal(wasm_module_t *module)
  1947. {
  1948. wasm_module_ex_t *module_ex;
  1949. if (!module) {
  1950. return;
  1951. }
  1952. module_ex = module_to_module_ext(module);
  1953. os_mutex_lock(&module_ex->lock);
  1954. /* N -> N-1 -> 0 -> UINT32_MAX */
  1955. module_ex->ref_count--;
  1956. if (module_ex->ref_count > 0) {
  1957. os_mutex_unlock(&module_ex->lock);
  1958. return;
  1959. }
  1960. DEINIT_VEC(module_ex->binary, wasm_byte_vec_delete);
  1961. if (module_ex->module_comm_rt) {
  1962. wasm_runtime_unload(module_ex->module_comm_rt);
  1963. module_ex->module_comm_rt = NULL;
  1964. }
  1965. #if WASM_ENABLE_WASM_CACHE != 0
  1966. memset(module_ex->hash, 0, sizeof(module_ex->hash));
  1967. #endif
  1968. os_mutex_unlock(&module_ex->lock);
  1969. }
  1970. void
  1971. wasm_module_delete(wasm_module_t *module)
  1972. {
  1973. /* the module will be released when releasing the store */
  1974. (void)module;
  1975. }
  1976. void
  1977. wasm_module_imports(const wasm_module_t *module, own wasm_importtype_vec_t *out)
  1978. {
  1979. uint32 i, import_func_count = 0, import_memory_count = 0,
  1980. import_global_count = 0, import_table_count = 0, import_count = 0;
  1981. wasm_byte_vec_t module_name = { 0 }, name = { 0 };
  1982. wasm_externtype_t *extern_type = NULL;
  1983. wasm_importtype_t *import_type = NULL;
  1984. if (!module || !out) {
  1985. return;
  1986. }
  1987. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  1988. return;
  1989. #if WASM_ENABLE_INTERP != 0
  1990. if ((*module)->module_type == Wasm_Module_Bytecode) {
  1991. import_func_count = MODULE_INTERP(module)->import_function_count;
  1992. import_global_count = MODULE_INTERP(module)->import_global_count;
  1993. import_memory_count = MODULE_INTERP(module)->import_memory_count;
  1994. import_table_count = MODULE_INTERP(module)->import_table_count;
  1995. }
  1996. #endif
  1997. #if WASM_ENABLE_AOT != 0
  1998. if ((*module)->module_type == Wasm_Module_AoT) {
  1999. import_func_count = MODULE_AOT(module)->import_func_count;
  2000. import_global_count = MODULE_AOT(module)->import_global_count;
  2001. import_memory_count = MODULE_AOT(module)->import_memory_count;
  2002. import_table_count = MODULE_AOT(module)->import_table_count;
  2003. }
  2004. #endif
  2005. import_count = import_func_count + import_global_count + import_table_count
  2006. + import_memory_count;
  2007. wasm_importtype_vec_new_uninitialized(out, import_count);
  2008. /*
  2009. * a wrong combination of module filetype and compilation flags
  2010. * also leads to below branch
  2011. */
  2012. if (!out->data) {
  2013. return;
  2014. }
  2015. for (i = 0; i != import_count; ++i) {
  2016. char *module_name_rt = NULL, *field_name_rt = NULL;
  2017. memset(&module_name, 0, sizeof(wasm_val_vec_t));
  2018. memset(&name, 0, sizeof(wasm_val_vec_t));
  2019. extern_type = NULL;
  2020. import_type = NULL;
  2021. if (i < import_func_count) {
  2022. wasm_functype_t *type = NULL;
  2023. WASMFuncType *type_rt = NULL;
  2024. #if WASM_ENABLE_INTERP != 0
  2025. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2026. WASMImport *import =
  2027. MODULE_INTERP(module)->import_functions + i;
  2028. module_name_rt = import->u.names.module_name;
  2029. field_name_rt = import->u.names.field_name;
  2030. type_rt = import->u.function.func_type;
  2031. }
  2032. #endif
  2033. #if WASM_ENABLE_AOT != 0
  2034. if ((*module)->module_type == Wasm_Module_AoT) {
  2035. AOTImportFunc *import = MODULE_AOT(module)->import_funcs + i;
  2036. module_name_rt = import->module_name;
  2037. field_name_rt = import->func_name;
  2038. type_rt = import->func_type;
  2039. }
  2040. #endif
  2041. if (!module_name_rt || !field_name_rt || !type_rt) {
  2042. continue;
  2043. }
  2044. if (!(type = wasm_functype_new_internal(type_rt))) {
  2045. goto failed;
  2046. }
  2047. extern_type = wasm_functype_as_externtype(type);
  2048. }
  2049. else if (i < import_func_count + import_global_count) {
  2050. wasm_globaltype_t *type = NULL;
  2051. uint8 val_type_rt = 0;
  2052. bool mutability_rt = 0;
  2053. #if WASM_ENABLE_INTERP != 0
  2054. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2055. WASMImport *import = MODULE_INTERP(module)->import_globals
  2056. + (i - import_func_count);
  2057. module_name_rt = import->u.names.module_name;
  2058. field_name_rt = import->u.names.field_name;
  2059. val_type_rt = import->u.global.type;
  2060. mutability_rt = import->u.global.is_mutable;
  2061. }
  2062. #endif
  2063. #if WASM_ENABLE_AOT != 0
  2064. if ((*module)->module_type == Wasm_Module_AoT) {
  2065. AOTImportGlobal *import = MODULE_AOT(module)->import_globals
  2066. + (i - import_func_count);
  2067. module_name_rt = import->module_name;
  2068. field_name_rt = import->global_name;
  2069. val_type_rt = import->type;
  2070. mutability_rt = import->is_mutable;
  2071. }
  2072. #endif
  2073. if (!module_name_rt || !field_name_rt) {
  2074. continue;
  2075. }
  2076. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2077. mutability_rt))) {
  2078. goto failed;
  2079. }
  2080. extern_type = wasm_globaltype_as_externtype(type);
  2081. }
  2082. else if (i < import_func_count + import_global_count
  2083. + import_memory_count) {
  2084. wasm_memorytype_t *type = NULL;
  2085. uint32 min_page = 0, max_page = 0;
  2086. #if WASM_ENABLE_INTERP != 0
  2087. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2088. WASMImport *import =
  2089. MODULE_INTERP(module)->import_memories
  2090. + (i - import_func_count - import_global_count);
  2091. module_name_rt = import->u.names.module_name;
  2092. field_name_rt = import->u.names.field_name;
  2093. min_page = import->u.memory.init_page_count;
  2094. max_page = import->u.memory.max_page_count;
  2095. }
  2096. #endif
  2097. #if WASM_ENABLE_AOT != 0
  2098. if ((*module)->module_type == Wasm_Module_AoT) {
  2099. AOTImportMemory *import =
  2100. MODULE_AOT(module)->import_memories
  2101. + (i - import_func_count - import_global_count);
  2102. module_name_rt = import->module_name;
  2103. field_name_rt = import->memory_name;
  2104. min_page = import->mem_init_page_count;
  2105. max_page = import->mem_max_page_count;
  2106. }
  2107. #endif
  2108. if (!module_name_rt || !field_name_rt) {
  2109. continue;
  2110. }
  2111. if (!(type = wasm_memorytype_new_internal(min_page, max_page))) {
  2112. goto failed;
  2113. }
  2114. extern_type = wasm_memorytype_as_externtype(type);
  2115. }
  2116. else {
  2117. wasm_tabletype_t *type = NULL;
  2118. uint8 elem_type_rt = 0;
  2119. uint32 min_size = 0, max_size = 0;
  2120. #if WASM_ENABLE_INTERP != 0
  2121. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2122. WASMImport *import =
  2123. MODULE_INTERP(module)->import_tables
  2124. + (i - import_func_count - import_global_count
  2125. - import_memory_count);
  2126. module_name_rt = import->u.names.module_name;
  2127. field_name_rt = import->u.names.field_name;
  2128. elem_type_rt = import->u.table.elem_type;
  2129. min_size = import->u.table.init_size;
  2130. max_size = import->u.table.max_size;
  2131. }
  2132. #endif
  2133. #if WASM_ENABLE_AOT != 0
  2134. if ((*module)->module_type == Wasm_Module_AoT) {
  2135. AOTImportTable *import =
  2136. MODULE_AOT(module)->import_tables
  2137. + (i - import_func_count - import_global_count
  2138. - import_memory_count);
  2139. module_name_rt = import->module_name;
  2140. field_name_rt = import->table_name;
  2141. elem_type_rt = import->elem_type;
  2142. min_size = import->table_init_size;
  2143. max_size = import->table_max_size;
  2144. }
  2145. #endif
  2146. if (!module_name_rt || !field_name_rt) {
  2147. continue;
  2148. }
  2149. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2150. max_size))) {
  2151. goto failed;
  2152. }
  2153. extern_type = wasm_tabletype_as_externtype(type);
  2154. }
  2155. bh_assert(extern_type);
  2156. wasm_name_new_from_string_nt(&module_name, module_name_rt);
  2157. if (strlen(module_name_rt) && !module_name.data) {
  2158. goto failed;
  2159. }
  2160. wasm_name_new_from_string_nt(&name, field_name_rt);
  2161. if (strlen(field_name_rt) && !name.data) {
  2162. goto failed;
  2163. }
  2164. if (!(import_type =
  2165. wasm_importtype_new(&module_name, &name, extern_type))) {
  2166. goto failed;
  2167. }
  2168. if (!bh_vector_append((Vector *)out, &import_type)) {
  2169. goto failed_importtype_new;
  2170. }
  2171. continue;
  2172. failed:
  2173. wasm_byte_vec_delete(&module_name);
  2174. wasm_byte_vec_delete(&name);
  2175. wasm_externtype_delete(extern_type);
  2176. failed_importtype_new:
  2177. wasm_importtype_delete(import_type);
  2178. }
  2179. }
  2180. void
  2181. wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out)
  2182. {
  2183. uint32 i, export_count = 0;
  2184. wasm_byte_vec_t name = { 0 };
  2185. wasm_externtype_t *extern_type = NULL;
  2186. wasm_exporttype_t *export_type = NULL;
  2187. if (!module || !out) {
  2188. return;
  2189. }
  2190. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2191. return;
  2192. #if WASM_ENABLE_INTERP != 0
  2193. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2194. export_count = MODULE_INTERP(module)->export_count;
  2195. }
  2196. #endif
  2197. #if WASM_ENABLE_AOT != 0
  2198. if ((*module)->module_type == Wasm_Module_AoT) {
  2199. export_count = MODULE_AOT(module)->export_count;
  2200. }
  2201. #endif
  2202. wasm_exporttype_vec_new_uninitialized(out, export_count);
  2203. /*
  2204. * a wrong combination of module filetype and compilation flags
  2205. * also leads to below branch
  2206. */
  2207. if (!out->data) {
  2208. return;
  2209. }
  2210. for (i = 0; i != export_count; i++) {
  2211. WASMExport *export = NULL;
  2212. #if WASM_ENABLE_INTERP != 0
  2213. if ((*module)->module_type == Wasm_Module_Bytecode) {
  2214. export = MODULE_INTERP(module)->exports + i;
  2215. }
  2216. #endif
  2217. #if WASM_ENABLE_AOT != 0
  2218. if ((*module)->module_type == Wasm_Module_AoT) {
  2219. export = MODULE_AOT(module)->exports + i;
  2220. }
  2221. #endif
  2222. if (!export) {
  2223. continue;
  2224. }
  2225. /* byte* -> wasm_byte_vec_t */
  2226. wasm_name_new_from_string_nt(&name, export->name);
  2227. if (strlen(export->name) && !name.data) {
  2228. goto failed;
  2229. }
  2230. /* WASMExport -> (WASMFuncType, (uint8, bool)) -> (wasm_functype_t,
  2231. * wasm_globaltype_t) -> wasm_externtype_t*/
  2232. switch (export->kind) {
  2233. case EXPORT_KIND_FUNC:
  2234. {
  2235. wasm_functype_t *type = NULL;
  2236. WASMFuncType *type_rt;
  2237. if (!wasm_runtime_get_export_func_type(*module, export,
  2238. &type_rt)) {
  2239. goto failed;
  2240. }
  2241. if (!(type = wasm_functype_new_internal(type_rt))) {
  2242. goto failed;
  2243. }
  2244. extern_type = wasm_functype_as_externtype(type);
  2245. break;
  2246. }
  2247. case EXPORT_KIND_GLOBAL:
  2248. {
  2249. wasm_globaltype_t *type = NULL;
  2250. uint8 val_type_rt = 0;
  2251. bool mutability_rt = 0;
  2252. if (!wasm_runtime_get_export_global_type(
  2253. *module, export, &val_type_rt, &mutability_rt)) {
  2254. goto failed;
  2255. }
  2256. if (!(type = wasm_globaltype_new_internal(val_type_rt,
  2257. mutability_rt))) {
  2258. goto failed;
  2259. }
  2260. extern_type = wasm_globaltype_as_externtype(type);
  2261. break;
  2262. }
  2263. case EXPORT_KIND_MEMORY:
  2264. {
  2265. wasm_memorytype_t *type = NULL;
  2266. uint32 min_page = 0, max_page = 0;
  2267. if (!wasm_runtime_get_export_memory_type(
  2268. *module, export, &min_page, &max_page)) {
  2269. goto failed;
  2270. }
  2271. if (!(type =
  2272. wasm_memorytype_new_internal(min_page, max_page))) {
  2273. goto failed;
  2274. }
  2275. extern_type = wasm_memorytype_as_externtype(type);
  2276. break;
  2277. }
  2278. case EXPORT_KIND_TABLE:
  2279. {
  2280. wasm_tabletype_t *type = NULL;
  2281. uint8 elem_type_rt = 0;
  2282. #if WASM_ENABLE_GC != 0
  2283. WASMRefType *elem_ref_type_rt;
  2284. #endif
  2285. uint32 min_size = 0, max_size = 0;
  2286. if (!wasm_runtime_get_export_table_type(*module, export,
  2287. &elem_type_rt,
  2288. #if WASM_ENABLE_GC != 0
  2289. &elem_ref_type_rt,
  2290. #endif
  2291. &min_size, &max_size)) {
  2292. goto failed;
  2293. }
  2294. #if WASM_ENABLE_GC != 0
  2295. (void)elem_ref_type_rt; /* TODO */
  2296. #endif
  2297. if (!(type = wasm_tabletype_new_internal(elem_type_rt, min_size,
  2298. max_size))) {
  2299. goto failed;
  2300. }
  2301. extern_type = wasm_tabletype_as_externtype(type);
  2302. break;
  2303. }
  2304. default:
  2305. {
  2306. LOG_WARNING("%s meets unsupported type %u", __FUNCTION__,
  2307. export->kind);
  2308. break;
  2309. }
  2310. }
  2311. if (!(export_type = wasm_exporttype_new(&name, extern_type))) {
  2312. goto failed;
  2313. }
  2314. if (!(bh_vector_append((Vector *)out, &export_type))) {
  2315. goto failed_exporttype_new;
  2316. }
  2317. }
  2318. return;
  2319. failed:
  2320. wasm_byte_vec_delete(&name);
  2321. wasm_externtype_delete(extern_type);
  2322. failed_exporttype_new:
  2323. wasm_exporttype_delete(export_type);
  2324. wasm_exporttype_vec_delete(out);
  2325. }
  2326. #if WASM_ENABLE_JIT == 0 || WASM_ENABLE_LAZY_JIT != 0
  2327. void
  2328. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2329. {
  2330. (void)module;
  2331. (void)out;
  2332. LOG_ERROR("only supported serialization in JIT with eager compilation");
  2333. }
  2334. own wasm_module_t *
  2335. wasm_module_deserialize(wasm_store_t *module, const wasm_byte_vec_t *binary)
  2336. {
  2337. (void)module;
  2338. (void)binary;
  2339. LOG_ERROR("only supported deserialization in JIT with eager compilation");
  2340. return NULL;
  2341. }
  2342. #else
  2343. extern uint8 *
  2344. aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
  2345. uint32 *p_aot_file_size);
  2346. void
  2347. wasm_module_serialize(wasm_module_t *module, own wasm_byte_vec_t *out)
  2348. {
  2349. wasm_module_ex_t *module_ex;
  2350. AOTCompContext *comp_ctx;
  2351. AOTCompData *comp_data;
  2352. uint8 *aot_file_buf = NULL;
  2353. uint32 aot_file_size = 0;
  2354. if (!module || !out)
  2355. return;
  2356. if (((const wasm_module_ex_t *)(module))->ref_count == 0)
  2357. return;
  2358. module_ex = module_to_module_ext(module);
  2359. comp_ctx = ((WASMModule *)(module_ex->module_comm_rt))->comp_ctx;
  2360. comp_data = ((WASMModule *)(module_ex->module_comm_rt))->comp_data;
  2361. bh_assert(comp_ctx != NULL && comp_data != NULL);
  2362. aot_file_buf = aot_emit_aot_file_buf(comp_ctx, comp_data, &aot_file_size);
  2363. if (!aot_file_buf)
  2364. return;
  2365. wasm_byte_vec_new(out, aot_file_size, (wasm_byte_t *)aot_file_buf);
  2366. wasm_runtime_free(aot_file_buf);
  2367. return;
  2368. }
  2369. own wasm_module_t *
  2370. wasm_module_deserialize(wasm_store_t *store, const wasm_byte_vec_t *binary)
  2371. {
  2372. return wasm_module_new(store, binary);
  2373. }
  2374. #endif
  2375. wasm_module_t *
  2376. wasm_module_obtain(wasm_store_t *store, wasm_shared_module_t *shared_module)
  2377. {
  2378. wasm_module_ex_t *module_ex = NULL;
  2379. if (!store || !shared_module)
  2380. return NULL;
  2381. module_ex = (wasm_module_ex_t *)shared_module;
  2382. os_mutex_lock(&module_ex->lock);
  2383. /* deleting the module... */
  2384. if (module_ex->ref_count == 0) {
  2385. LOG_WARNING("wasm_module_obtain re-enter a module under deleting.");
  2386. os_mutex_unlock(&module_ex->lock);
  2387. return NULL;
  2388. }
  2389. /* add it to a watching list in store */
  2390. if (!bh_vector_append((Vector *)store->modules, &module_ex)) {
  2391. os_mutex_unlock(&module_ex->lock);
  2392. return NULL;
  2393. }
  2394. module_ex->ref_count++;
  2395. os_mutex_unlock(&module_ex->lock);
  2396. return (wasm_module_t *)shared_module;
  2397. }
  2398. wasm_shared_module_t *
  2399. wasm_module_share(wasm_module_t *module)
  2400. {
  2401. wasm_module_ex_t *module_ex = NULL;
  2402. if (!module)
  2403. return NULL;
  2404. module_ex = (wasm_module_ex_t *)module;
  2405. os_mutex_lock(&module_ex->lock);
  2406. /* deleting the module... */
  2407. if (module_ex->ref_count == 0) {
  2408. LOG_WARNING("wasm_module_share re-enter a module under deleting.");
  2409. os_mutex_unlock(&module_ex->lock);
  2410. return NULL;
  2411. }
  2412. module_ex->ref_count++;
  2413. os_mutex_unlock(&module_ex->lock);
  2414. return (wasm_shared_module_t *)module;
  2415. }
  2416. void
  2417. wasm_shared_module_delete(own wasm_shared_module_t *shared_module)
  2418. {
  2419. wasm_module_delete_internal((wasm_module_t *)shared_module);
  2420. }
  2421. static wasm_func_t *
  2422. wasm_func_new_basic(wasm_store_t *store, const wasm_functype_t *type,
  2423. wasm_func_callback_t func_callback)
  2424. {
  2425. wasm_func_t *func = NULL;
  2426. if (!type) {
  2427. goto failed;
  2428. }
  2429. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2430. goto failed;
  2431. }
  2432. func->store = store;
  2433. func->kind = WASM_EXTERN_FUNC;
  2434. func->func_idx_rt = (uint16)-1;
  2435. func->with_env = false;
  2436. func->u.cb = func_callback;
  2437. if (!(func->type = wasm_functype_copy(type))) {
  2438. goto failed;
  2439. }
  2440. RETURN_OBJ(func, wasm_func_delete)
  2441. }
  2442. static wasm_func_t *
  2443. wasm_func_new_with_env_basic(wasm_store_t *store, const wasm_functype_t *type,
  2444. wasm_func_callback_with_env_t callback, void *env,
  2445. void (*finalizer)(void *))
  2446. {
  2447. wasm_func_t *func = NULL;
  2448. if (!type) {
  2449. goto failed;
  2450. }
  2451. if (!(func = malloc_internal(sizeof(wasm_func_t)))) {
  2452. goto failed;
  2453. }
  2454. func->store = store;
  2455. func->kind = WASM_EXTERN_FUNC;
  2456. func->func_idx_rt = (uint16)-1;
  2457. func->with_env = true;
  2458. func->u.cb_env.cb = callback;
  2459. func->u.cb_env.env = env;
  2460. func->u.cb_env.finalizer = finalizer;
  2461. if (!(func->type = wasm_functype_copy(type))) {
  2462. goto failed;
  2463. }
  2464. RETURN_OBJ(func, wasm_func_delete)
  2465. }
  2466. wasm_func_t *
  2467. wasm_func_new(wasm_store_t *store, const wasm_functype_t *type,
  2468. wasm_func_callback_t callback)
  2469. {
  2470. bh_assert(singleton_engine);
  2471. if (!callback) {
  2472. return NULL;
  2473. }
  2474. return wasm_func_new_basic(store, type, callback);
  2475. }
  2476. wasm_func_t *
  2477. wasm_func_new_with_env(wasm_store_t *store, const wasm_functype_t *type,
  2478. wasm_func_callback_with_env_t callback, void *env,
  2479. void (*finalizer)(void *))
  2480. {
  2481. bh_assert(singleton_engine);
  2482. if (!callback) {
  2483. return NULL;
  2484. }
  2485. return wasm_func_new_with_env_basic(store, type, callback, env, finalizer);
  2486. }
  2487. wasm_func_t *
  2488. wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
  2489. WASMModuleInstanceCommon *inst_comm_rt)
  2490. {
  2491. wasm_func_t *func = NULL;
  2492. WASMFuncType *type_rt = NULL;
  2493. bh_assert(singleton_engine);
  2494. if (!inst_comm_rt) {
  2495. return NULL;
  2496. }
  2497. func = malloc_internal(sizeof(wasm_func_t));
  2498. if (!func) {
  2499. goto failed;
  2500. }
  2501. func->kind = WASM_EXTERN_FUNC;
  2502. #if WASM_ENABLE_INTERP != 0
  2503. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2504. bh_assert(func_idx_rt
  2505. < ((WASMModuleInstance *)inst_comm_rt)->e->function_count);
  2506. WASMFunctionInstance *func_interp =
  2507. ((WASMModuleInstance *)inst_comm_rt)->e->functions + func_idx_rt;
  2508. type_rt = func_interp->is_import_func
  2509. ? func_interp->u.func_import->func_type
  2510. : func_interp->u.func->func_type;
  2511. }
  2512. #endif
  2513. #if WASM_ENABLE_AOT != 0
  2514. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2515. /* use same index to trace the function type in AOTFuncType **func_types
  2516. */
  2517. AOTModule *module_aot =
  2518. (AOTModule *)((AOTModuleInstance *)inst_comm_rt)->module;
  2519. if (func_idx_rt < module_aot->import_func_count) {
  2520. type_rt = (module_aot->import_funcs + func_idx_rt)->func_type;
  2521. }
  2522. else {
  2523. type_rt =
  2524. (AOTFuncType *)module_aot
  2525. ->types[module_aot->func_type_indexes
  2526. [func_idx_rt - module_aot->import_func_count]];
  2527. }
  2528. }
  2529. #endif
  2530. /*
  2531. * a wrong combination of module filetype and compilation flags
  2532. * also leads to below branch
  2533. */
  2534. if (!type_rt) {
  2535. goto failed;
  2536. }
  2537. func->type = wasm_functype_new_internal(type_rt);
  2538. if (!func->type) {
  2539. goto failed;
  2540. }
  2541. /* will add name information when processing "exports" */
  2542. func->store = store;
  2543. func->module_name = NULL;
  2544. func->name = NULL;
  2545. func->func_idx_rt = func_idx_rt;
  2546. func->inst_comm_rt = inst_comm_rt;
  2547. return func;
  2548. failed:
  2549. LOG_DEBUG("%s failed", __FUNCTION__);
  2550. wasm_func_delete(func);
  2551. return NULL;
  2552. }
  2553. static wasm_func_t *
  2554. wasm_func_new_empty(wasm_store_t *store)
  2555. {
  2556. wasm_func_t *func = NULL;
  2557. if (!(func = malloc_internal(sizeof(wasm_func_t))))
  2558. goto failed;
  2559. func->store = store;
  2560. func->kind = WASM_EXTERN_FUNC;
  2561. RETURN_OBJ(func, wasm_func_delete)
  2562. }
  2563. void
  2564. wasm_func_delete(wasm_func_t *func)
  2565. {
  2566. if (!func) {
  2567. return;
  2568. }
  2569. if (func->type) {
  2570. wasm_functype_delete(func->type);
  2571. func->type = NULL;
  2572. }
  2573. if (func->with_env) {
  2574. if (func->u.cb_env.finalizer) {
  2575. func->u.cb_env.finalizer(func->u.cb_env.env);
  2576. func->u.cb_env.finalizer = NULL;
  2577. func->u.cb_env.env = NULL;
  2578. }
  2579. }
  2580. DELETE_HOST_INFO(func)
  2581. wasm_runtime_free(func);
  2582. }
  2583. own wasm_func_t *
  2584. wasm_func_copy(const wasm_func_t *func)
  2585. {
  2586. wasm_func_t *cloned = NULL;
  2587. if (!func) {
  2588. return NULL;
  2589. }
  2590. if (!(cloned = func->with_env ? wasm_func_new_with_env_basic(
  2591. func->store, func->type, func->u.cb_env.cb,
  2592. func->u.cb_env.env, func->u.cb_env.finalizer)
  2593. : wasm_func_new_basic(func->store, func->type,
  2594. func->u.cb))) {
  2595. goto failed;
  2596. }
  2597. cloned->func_idx_rt = func->func_idx_rt;
  2598. cloned->inst_comm_rt = func->inst_comm_rt;
  2599. RETURN_OBJ(cloned, wasm_func_delete)
  2600. }
  2601. own wasm_functype_t *
  2602. wasm_func_type(const wasm_func_t *func)
  2603. {
  2604. if (!func) {
  2605. return NULL;
  2606. }
  2607. return wasm_functype_copy(func->type);
  2608. }
  2609. static bool
  2610. params_to_argv(const wasm_val_vec_t *params,
  2611. const wasm_valtype_vec_t *param_defs, uint32 *argv,
  2612. uint32 *ptr_argc)
  2613. {
  2614. size_t i = 0;
  2615. if (!param_defs->num_elems) {
  2616. return true;
  2617. }
  2618. if (!params || !params->num_elems || !params->size || !params->data) {
  2619. LOG_ERROR("the parameter params is invalid");
  2620. return false;
  2621. }
  2622. *ptr_argc = 0;
  2623. for (i = 0; i < param_defs->num_elems; ++i) {
  2624. const wasm_val_t *param = params->data + i;
  2625. bh_assert((*(param_defs->data + i))->kind == param->kind);
  2626. switch (param->kind) {
  2627. case WASM_I32:
  2628. *(int32 *)argv = param->of.i32;
  2629. argv += 1;
  2630. *ptr_argc += 1;
  2631. break;
  2632. case WASM_I64:
  2633. *(int64 *)argv = param->of.i64;
  2634. argv += 2;
  2635. *ptr_argc += 2;
  2636. break;
  2637. case WASM_F32:
  2638. *(float32 *)argv = param->of.f32;
  2639. argv += 1;
  2640. *ptr_argc += 1;
  2641. break;
  2642. case WASM_F64:
  2643. *(float64 *)argv = param->of.f64;
  2644. argv += 2;
  2645. *ptr_argc += 2;
  2646. break;
  2647. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2648. case WASM_ANYREF:
  2649. *(uintptr_t *)argv = (uintptr_t)param->of.ref;
  2650. argv += sizeof(uintptr_t) / sizeof(uint32);
  2651. *ptr_argc += 1;
  2652. break;
  2653. #endif
  2654. default:
  2655. LOG_WARNING("unexpected parameter val type %d", param->kind);
  2656. return false;
  2657. }
  2658. }
  2659. return true;
  2660. }
  2661. static bool
  2662. argv_to_results(const uint32 *argv, const wasm_valtype_vec_t *result_defs,
  2663. wasm_val_vec_t *results)
  2664. {
  2665. size_t i = 0, argv_i = 0;
  2666. wasm_val_t *result;
  2667. if (!result_defs->num_elems) {
  2668. return true;
  2669. }
  2670. if (!results || !results->size || !results->data) {
  2671. LOG_ERROR("the parameter results is invalid");
  2672. return false;
  2673. }
  2674. for (i = 0, result = results->data, argv_i = 0; i < result_defs->num_elems;
  2675. i++, result++) {
  2676. switch (result_defs->data[i]->kind) {
  2677. case WASM_I32:
  2678. {
  2679. result->kind = WASM_I32;
  2680. result->of.i32 = *(int32 *)(argv + argv_i);
  2681. argv_i += 1;
  2682. break;
  2683. }
  2684. case WASM_I64:
  2685. {
  2686. result->kind = WASM_I64;
  2687. result->of.i64 = *(int64 *)(argv + argv_i);
  2688. argv_i += 2;
  2689. break;
  2690. }
  2691. case WASM_F32:
  2692. {
  2693. result->kind = WASM_F32;
  2694. result->of.f32 = *(float32 *)(argv + argv_i);
  2695. argv_i += 1;
  2696. break;
  2697. }
  2698. case WASM_F64:
  2699. {
  2700. result->kind = WASM_F64;
  2701. result->of.f64 = *(float64 *)(argv + argv_i);
  2702. argv_i += 2;
  2703. break;
  2704. }
  2705. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  2706. case WASM_ANYREF:
  2707. {
  2708. result->kind = WASM_ANYREF;
  2709. result->of.ref =
  2710. (struct wasm_ref_t *)(*(uintptr_t *)(argv + argv_i));
  2711. argv_i += sizeof(uintptr_t) / sizeof(uint32);
  2712. break;
  2713. }
  2714. #endif
  2715. default:
  2716. LOG_WARNING("%s meets unsupported type: %d", __FUNCTION__,
  2717. result_defs->data[i]->kind);
  2718. return false;
  2719. }
  2720. }
  2721. return true;
  2722. }
  2723. wasm_trap_t *
  2724. wasm_func_call(const wasm_func_t *func, const wasm_val_vec_t *params,
  2725. wasm_val_vec_t *results)
  2726. {
  2727. /* parameters count as if all are uint32 */
  2728. /* a int64 or float64 parameter means 2 */
  2729. uint32 argc = 0;
  2730. /* a parameter list and a return value list */
  2731. uint32 argv_buf[32] = { 0 }, *argv = argv_buf;
  2732. WASMFunctionInstanceCommon *func_comm_rt = NULL;
  2733. WASMExecEnv *exec_env = NULL;
  2734. size_t param_count, result_count, alloc_count;
  2735. if (!func) {
  2736. return NULL;
  2737. }
  2738. if (!func->inst_comm_rt) {
  2739. wasm_name_t message = { 0 };
  2740. wasm_trap_t *trap;
  2741. wasm_name_new_from_string_nt(&message,
  2742. "failed to call unlinked function");
  2743. trap = wasm_trap_new(func->store, &message);
  2744. wasm_byte_vec_delete(&message);
  2745. return trap;
  2746. }
  2747. bh_assert(func->type);
  2748. #if WASM_ENABLE_INTERP != 0
  2749. if (func->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2750. func_comm_rt = ((WASMModuleInstance *)func->inst_comm_rt)->e->functions
  2751. + func->func_idx_rt;
  2752. }
  2753. #endif
  2754. #if WASM_ENABLE_AOT != 0
  2755. if (func->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2756. if (!(func_comm_rt = func->func_comm_rt)) {
  2757. AOTModuleInstance *inst_aot =
  2758. (AOTModuleInstance *)func->inst_comm_rt;
  2759. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2760. uint32 export_i = 0, export_func_j = 0;
  2761. for (; export_i < module_aot->export_count; ++export_i) {
  2762. AOTExport *export = module_aot->exports + export_i;
  2763. if (export->kind == EXPORT_KIND_FUNC) {
  2764. if (export->index == func->func_idx_rt) {
  2765. func_comm_rt =
  2766. (AOTFunctionInstance *)inst_aot->export_functions
  2767. + export_func_j;
  2768. ((wasm_func_t *)func)->func_comm_rt = func_comm_rt;
  2769. break;
  2770. }
  2771. export_func_j++;
  2772. }
  2773. }
  2774. }
  2775. }
  2776. #endif
  2777. /*
  2778. * a wrong combination of module filetype and compilation flags
  2779. * also leads to below branch
  2780. */
  2781. if (!func_comm_rt) {
  2782. goto failed;
  2783. }
  2784. param_count = wasm_func_param_arity(func);
  2785. result_count = wasm_func_result_arity(func);
  2786. alloc_count = (param_count > result_count) ? param_count : result_count;
  2787. if (alloc_count > (size_t)sizeof(argv_buf) / sizeof(uint64)) {
  2788. if (!(argv = malloc_internal(sizeof(uint64) * alloc_count))) {
  2789. goto failed;
  2790. }
  2791. }
  2792. /* copy parametes */
  2793. if (param_count
  2794. && !params_to_argv(params, wasm_functype_params(func->type), argv,
  2795. &argc)) {
  2796. goto failed;
  2797. }
  2798. #ifdef OS_ENABLE_HW_BOUND_CHECK
  2799. exec_env = wasm_runtime_get_exec_env_tls();
  2800. #endif
  2801. #if WASM_ENABLE_THREAD_MGR != 0
  2802. if (!exec_env) {
  2803. exec_env = wasm_clusters_search_exec_env(func->inst_comm_rt);
  2804. }
  2805. #endif
  2806. if (!exec_env) {
  2807. exec_env = wasm_runtime_get_exec_env_singleton(func->inst_comm_rt);
  2808. }
  2809. if (!exec_env) {
  2810. goto failed;
  2811. }
  2812. wasm_runtime_set_exception(func->inst_comm_rt, NULL);
  2813. if (!wasm_runtime_call_wasm(exec_env, func_comm_rt, argc, argv)) {
  2814. if (wasm_runtime_get_exception(func->inst_comm_rt)) {
  2815. LOG_DEBUG(wasm_runtime_get_exception(func->inst_comm_rt));
  2816. goto failed;
  2817. }
  2818. }
  2819. /* copy results */
  2820. if (result_count) {
  2821. if (!argv_to_results(argv, wasm_functype_results(func->type),
  2822. results)) {
  2823. goto failed;
  2824. }
  2825. results->num_elems = result_count;
  2826. results->size = result_count;
  2827. }
  2828. if (argv != argv_buf)
  2829. wasm_runtime_free(argv);
  2830. return NULL;
  2831. failed:
  2832. if (argv != argv_buf)
  2833. wasm_runtime_free(argv);
  2834. return wasm_trap_new_internal(
  2835. func->store, func->inst_comm_rt,
  2836. wasm_runtime_get_exception(func->inst_comm_rt));
  2837. }
  2838. size_t
  2839. wasm_func_param_arity(const wasm_func_t *func)
  2840. {
  2841. if (!func || !func->type || !func->type->params) {
  2842. return 0;
  2843. }
  2844. return func->type->params->num_elems;
  2845. }
  2846. size_t
  2847. wasm_func_result_arity(const wasm_func_t *func)
  2848. {
  2849. if (!func || !func->type || !func->type->results) {
  2850. return 0;
  2851. }
  2852. return func->type->results->num_elems;
  2853. }
  2854. wasm_global_t *
  2855. wasm_global_new(wasm_store_t *store, const wasm_globaltype_t *global_type,
  2856. const wasm_val_t *init)
  2857. {
  2858. wasm_global_t *global = NULL;
  2859. bh_assert(singleton_engine);
  2860. if (!global_type || !init) {
  2861. goto failed;
  2862. }
  2863. global = malloc_internal(sizeof(wasm_global_t));
  2864. if (!global) {
  2865. goto failed;
  2866. }
  2867. global->store = store;
  2868. global->kind = WASM_EXTERN_GLOBAL;
  2869. global->type = wasm_globaltype_copy(global_type);
  2870. if (!global->type) {
  2871. goto failed;
  2872. }
  2873. global->init = malloc_internal(sizeof(wasm_val_t));
  2874. if (!global->init) {
  2875. goto failed;
  2876. }
  2877. wasm_val_copy(global->init, init);
  2878. /* TODO: how to check if above is failed */
  2879. return global;
  2880. failed:
  2881. LOG_DEBUG("%s failed", __FUNCTION__);
  2882. wasm_global_delete(global);
  2883. return NULL;
  2884. }
  2885. static wasm_global_t *
  2886. wasm_global_new_empty(wasm_store_t *store)
  2887. {
  2888. wasm_global_t *global = NULL;
  2889. global = malloc_internal(sizeof(wasm_global_t));
  2890. if (!global)
  2891. goto failed;
  2892. global->store = store;
  2893. global->kind = WASM_EXTERN_GLOBAL;
  2894. return global;
  2895. failed:
  2896. LOG_DEBUG("%s failed", __FUNCTION__);
  2897. wasm_global_delete(global);
  2898. return NULL;
  2899. }
  2900. /* almost same with wasm_global_new */
  2901. wasm_global_t *
  2902. wasm_global_copy(const wasm_global_t *src)
  2903. {
  2904. wasm_global_t *global = NULL;
  2905. if (!src) {
  2906. return NULL;
  2907. }
  2908. global = malloc_internal(sizeof(wasm_global_t));
  2909. if (!global) {
  2910. goto failed;
  2911. }
  2912. global->kind = WASM_EXTERN_GLOBAL;
  2913. global->type = wasm_globaltype_copy(src->type);
  2914. if (!global->type) {
  2915. goto failed;
  2916. }
  2917. global->init = malloc_internal(sizeof(wasm_val_t));
  2918. if (!global->init) {
  2919. goto failed;
  2920. }
  2921. wasm_val_copy(global->init, src->init);
  2922. global->global_idx_rt = src->global_idx_rt;
  2923. global->inst_comm_rt = src->inst_comm_rt;
  2924. return global;
  2925. failed:
  2926. LOG_DEBUG("%s failed", __FUNCTION__);
  2927. wasm_global_delete(global);
  2928. return NULL;
  2929. }
  2930. void
  2931. wasm_global_delete(wasm_global_t *global)
  2932. {
  2933. if (!global) {
  2934. return;
  2935. }
  2936. if (global->init) {
  2937. wasm_val_delete(global->init);
  2938. global->init = NULL;
  2939. }
  2940. if (global->type) {
  2941. wasm_globaltype_delete(global->type);
  2942. global->type = NULL;
  2943. }
  2944. DELETE_HOST_INFO(global)
  2945. wasm_runtime_free(global);
  2946. }
  2947. #if WASM_ENABLE_INTERP != 0
  2948. static bool
  2949. interp_global_set(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2950. const wasm_val_t *v)
  2951. {
  2952. const WASMGlobalInstance *global_interp =
  2953. inst_interp->e->globals + global_idx_rt;
  2954. uint8 val_type_rt = global_interp->type;
  2955. #if WASM_ENABLE_MULTI_MODULE != 0
  2956. uint8 *data = global_interp->import_global_inst
  2957. ? global_interp->import_module_inst->global_data
  2958. + global_interp->import_global_inst->data_offset
  2959. : inst_interp->global_data + global_interp->data_offset;
  2960. #else
  2961. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2962. #endif
  2963. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_interp,
  2964. val_type_rt, v, data);
  2965. }
  2966. static bool
  2967. interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2968. wasm_val_t *out)
  2969. {
  2970. WASMGlobalInstance *global_interp = inst_interp->e->globals + global_idx_rt;
  2971. uint8 val_type_rt = global_interp->type;
  2972. #if WASM_ENABLE_MULTI_MODULE != 0
  2973. uint8 *data = global_interp->import_global_inst
  2974. ? global_interp->import_module_inst->global_data
  2975. + global_interp->import_global_inst->data_offset
  2976. : inst_interp->global_data + global_interp->data_offset;
  2977. #else
  2978. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2979. #endif
  2980. return rt_val_to_wasm_val(data, val_type_rt, out);
  2981. }
  2982. #endif
  2983. #if WASM_ENABLE_AOT != 0
  2984. static bool
  2985. aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  2986. const wasm_val_t *v)
  2987. {
  2988. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  2989. uint8 val_type_rt = 0;
  2990. uint32 data_offset = 0;
  2991. void *data = NULL;
  2992. if (global_idx_rt < module_aot->import_global_count) {
  2993. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2994. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2995. }
  2996. else {
  2997. data_offset =
  2998. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2999. .data_offset;
  3000. val_type_rt =
  3001. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3002. .type;
  3003. }
  3004. data = (void *)(inst_aot->global_data + data_offset);
  3005. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt,
  3006. v, data);
  3007. }
  3008. static bool
  3009. aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  3010. wasm_val_t *out)
  3011. {
  3012. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3013. uint8 val_type_rt = 0;
  3014. uint32 data_offset = 0;
  3015. uint8 *data = NULL;
  3016. if (global_idx_rt < module_aot->import_global_count) {
  3017. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  3018. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  3019. }
  3020. else {
  3021. data_offset =
  3022. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3023. .data_offset;
  3024. val_type_rt =
  3025. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  3026. .type;
  3027. }
  3028. data = inst_aot->global_data + data_offset;
  3029. return rt_val_to_wasm_val(data, val_type_rt, out);
  3030. }
  3031. #endif
  3032. void
  3033. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  3034. {
  3035. if (!global || !v || !global->inst_comm_rt) {
  3036. return;
  3037. }
  3038. #if WASM_ENABLE_INTERP != 0
  3039. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3040. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  3041. global->global_idx_rt, v);
  3042. return;
  3043. }
  3044. #endif
  3045. #if WASM_ENABLE_AOT != 0
  3046. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3047. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  3048. global->global_idx_rt, v);
  3049. return;
  3050. }
  3051. #endif
  3052. /*
  3053. * a wrong combination of module filetype and compilation flags
  3054. * leads to below branch
  3055. */
  3056. UNREACHABLE();
  3057. }
  3058. void
  3059. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  3060. {
  3061. if (!global || !out) {
  3062. return;
  3063. }
  3064. if (!global->inst_comm_rt) {
  3065. return;
  3066. }
  3067. memset(out, 0, sizeof(wasm_val_t));
  3068. #if WASM_ENABLE_INTERP != 0
  3069. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3070. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  3071. global->global_idx_rt, out);
  3072. return;
  3073. }
  3074. #endif
  3075. #if WASM_ENABLE_AOT != 0
  3076. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3077. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  3078. global->global_idx_rt, out);
  3079. return;
  3080. }
  3081. #endif
  3082. /*
  3083. * a wrong combination of module filetype and compilation flags
  3084. * leads to below branch
  3085. */
  3086. UNREACHABLE();
  3087. }
  3088. wasm_global_t *
  3089. wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
  3090. WASMModuleInstanceCommon *inst_comm_rt)
  3091. {
  3092. wasm_global_t *global = NULL;
  3093. uint8 val_type_rt = 0;
  3094. bool is_mutable = 0;
  3095. bool init = false;
  3096. bh_assert(singleton_engine);
  3097. if (!inst_comm_rt) {
  3098. return NULL;
  3099. }
  3100. global = malloc_internal(sizeof(wasm_global_t));
  3101. if (!global) {
  3102. goto failed;
  3103. }
  3104. global->store = store;
  3105. global->kind = WASM_EXTERN_GLOBAL;
  3106. #if WASM_ENABLE_INTERP != 0
  3107. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3108. WASMGlobalInstance *global_interp =
  3109. ((WASMModuleInstance *)inst_comm_rt)->e->globals + global_idx_rt;
  3110. val_type_rt = global_interp->type;
  3111. is_mutable = global_interp->is_mutable;
  3112. init = true;
  3113. }
  3114. #endif
  3115. #if WASM_ENABLE_AOT != 0
  3116. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3117. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3118. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3119. init = true;
  3120. if (global_idx_rt < module_aot->import_global_count) {
  3121. AOTImportGlobal *global_import_aot =
  3122. module_aot->import_globals + global_idx_rt;
  3123. val_type_rt = global_import_aot->type;
  3124. is_mutable = global_import_aot->is_mutable;
  3125. }
  3126. else {
  3127. AOTGlobal *global_aot =
  3128. module_aot->globals
  3129. + (global_idx_rt - module_aot->import_global_count);
  3130. val_type_rt = global_aot->type;
  3131. is_mutable = global_aot->is_mutable;
  3132. }
  3133. }
  3134. #endif
  3135. /*
  3136. * a wrong combination of module filetype and compilation flags
  3137. * leads to below branch
  3138. */
  3139. if (!init) {
  3140. goto failed;
  3141. }
  3142. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  3143. if (!global->type) {
  3144. goto failed;
  3145. }
  3146. global->init = malloc_internal(sizeof(wasm_val_t));
  3147. if (!global->init) {
  3148. goto failed;
  3149. }
  3150. #if WASM_ENABLE_INTERP != 0
  3151. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3152. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  3153. global->init);
  3154. }
  3155. #endif
  3156. #if WASM_ENABLE_AOT != 0
  3157. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3158. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  3159. global->init);
  3160. }
  3161. #endif
  3162. global->inst_comm_rt = inst_comm_rt;
  3163. global->global_idx_rt = global_idx_rt;
  3164. return global;
  3165. failed:
  3166. LOG_DEBUG("%s failed", __FUNCTION__);
  3167. wasm_global_delete(global);
  3168. return NULL;
  3169. }
  3170. wasm_globaltype_t *
  3171. wasm_global_type(const wasm_global_t *global)
  3172. {
  3173. if (!global) {
  3174. return NULL;
  3175. }
  3176. return wasm_globaltype_copy(global->type);
  3177. }
  3178. static wasm_table_t *
  3179. wasm_table_new_basic(wasm_store_t *store, const wasm_tabletype_t *type)
  3180. {
  3181. wasm_table_t *table = NULL;
  3182. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3183. goto failed;
  3184. }
  3185. table->store = store;
  3186. table->kind = WASM_EXTERN_TABLE;
  3187. if (!(table->type = wasm_tabletype_copy(type))) {
  3188. goto failed;
  3189. }
  3190. RETURN_OBJ(table, wasm_table_delete);
  3191. }
  3192. wasm_table_t *
  3193. wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
  3194. WASMModuleInstanceCommon *inst_comm_rt)
  3195. {
  3196. wasm_table_t *table = NULL;
  3197. uint8 val_type_rt = 0;
  3198. #if WASM_ENABLE_GC != 0
  3199. WASMRefType *val_ref_type_rt;
  3200. #endif
  3201. uint32 init_size = 0, max_size = 0;
  3202. bh_assert(singleton_engine);
  3203. if (!inst_comm_rt) {
  3204. return NULL;
  3205. }
  3206. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  3207. goto failed;
  3208. }
  3209. table->store = store;
  3210. table->kind = WASM_EXTERN_TABLE;
  3211. if (!wasm_runtime_get_table_inst_elem_type(inst_comm_rt, table_idx_rt,
  3212. &val_type_rt,
  3213. #if WASM_ENABLE_GC != 0
  3214. &val_ref_type_rt,
  3215. #endif
  3216. &init_size, &max_size)) {
  3217. /*
  3218. * a wrong combination of module filetype and compilation flags
  3219. * leads to below branch
  3220. */
  3221. goto failed;
  3222. }
  3223. #if WASM_ENABLE_GC != 0
  3224. (void)val_ref_type_rt; /* TODO */
  3225. #endif
  3226. if (!(table->type =
  3227. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  3228. goto failed;
  3229. }
  3230. table->inst_comm_rt = inst_comm_rt;
  3231. table->table_idx_rt = table_idx_rt;
  3232. RETURN_OBJ(table, wasm_table_delete);
  3233. }
  3234. /* will not actually apply this new table into the runtime */
  3235. wasm_table_t *
  3236. wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *table_type,
  3237. wasm_ref_t *init)
  3238. {
  3239. wasm_table_t *table;
  3240. (void)init;
  3241. bh_assert(singleton_engine);
  3242. if ((table = wasm_table_new_basic(store, table_type))) {
  3243. table->store = store;
  3244. }
  3245. return table;
  3246. }
  3247. wasm_table_t *
  3248. wasm_table_copy(const wasm_table_t *src)
  3249. {
  3250. wasm_table_t *table;
  3251. if (!(table = wasm_table_new_basic(src->store, src->type))) {
  3252. return NULL;
  3253. }
  3254. table->table_idx_rt = src->table_idx_rt;
  3255. table->inst_comm_rt = src->inst_comm_rt;
  3256. return table;
  3257. }
  3258. void
  3259. wasm_table_delete(wasm_table_t *table)
  3260. {
  3261. if (!table) {
  3262. return;
  3263. }
  3264. if (table->type) {
  3265. wasm_tabletype_delete(table->type);
  3266. table->type = NULL;
  3267. }
  3268. DELETE_HOST_INFO(table)
  3269. wasm_runtime_free(table);
  3270. }
  3271. wasm_tabletype_t *
  3272. wasm_table_type(const wasm_table_t *table)
  3273. {
  3274. if (!table) {
  3275. return NULL;
  3276. }
  3277. return wasm_tabletype_copy(table->type);
  3278. }
  3279. #if WASM_ENABLE_GC == 0
  3280. own wasm_ref_t *
  3281. wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
  3282. {
  3283. uint32 ref_idx = NULL_REF;
  3284. if (!table || !table->inst_comm_rt) {
  3285. return NULL;
  3286. }
  3287. #if WASM_ENABLE_INTERP != 0
  3288. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3289. WASMTableInstance *table_interp =
  3290. ((WASMModuleInstance *)table->inst_comm_rt)
  3291. ->tables[table->table_idx_rt];
  3292. if (index >= table_interp->cur_size) {
  3293. return NULL;
  3294. }
  3295. ref_idx = table_interp->elems[index];
  3296. }
  3297. #endif
  3298. #if WASM_ENABLE_AOT != 0
  3299. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3300. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3301. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3302. if (index >= table_aot->cur_size) {
  3303. return NULL;
  3304. }
  3305. ref_idx = table_aot->elems[index];
  3306. }
  3307. #endif
  3308. /*
  3309. * a wrong combination of module filetype and compilation flags
  3310. * also leads to below branch
  3311. */
  3312. if (ref_idx == NULL_REF) {
  3313. return NULL;
  3314. }
  3315. #if WASM_ENABLE_REF_TYPES != 0
  3316. if (table->type->val_type->kind == WASM_ANYREF) {
  3317. void *externref_obj;
  3318. if (!wasm_externref_ref2obj(ref_idx, &externref_obj)) {
  3319. return NULL;
  3320. }
  3321. return externref_obj;
  3322. }
  3323. else
  3324. #endif
  3325. {
  3326. return wasm_ref_new_internal(table->store, WASM_REF_func, ref_idx,
  3327. table->inst_comm_rt);
  3328. }
  3329. }
  3330. bool
  3331. wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
  3332. own wasm_ref_t *ref)
  3333. {
  3334. uint32 *p_ref_idx = NULL;
  3335. uint32 function_count = 0;
  3336. if (!table || !table->inst_comm_rt) {
  3337. return false;
  3338. }
  3339. if (ref
  3340. #if WASM_ENABLE_REF_TYPES != 0
  3341. && !(WASM_REF_foreign == ref->kind
  3342. && WASM_ANYREF == table->type->val_type->kind)
  3343. #endif
  3344. && !(WASM_REF_func == ref->kind
  3345. && WASM_FUNCREF == table->type->val_type->kind)) {
  3346. return false;
  3347. }
  3348. #if WASM_ENABLE_INTERP != 0
  3349. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3350. WASMTableInstance *table_interp =
  3351. ((WASMModuleInstance *)table->inst_comm_rt)
  3352. ->tables[table->table_idx_rt];
  3353. if (index >= table_interp->cur_size) {
  3354. return false;
  3355. }
  3356. p_ref_idx = (uint32 *)(table_interp->elems + index);
  3357. function_count =
  3358. ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
  3359. }
  3360. #endif
  3361. #if WASM_ENABLE_AOT != 0
  3362. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3363. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3364. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3365. AOTTableInstance *table_aot = inst_aot->tables[table->table_idx_rt];
  3366. if (index >= table_aot->cur_size) {
  3367. return false;
  3368. }
  3369. p_ref_idx = (uint32 *)(table_aot->elems + index);
  3370. function_count = module_aot->func_count;
  3371. }
  3372. #endif
  3373. /*
  3374. * a wrong combination of module filetype and compilation flags
  3375. * leads to below branch
  3376. */
  3377. if (!p_ref_idx) {
  3378. return false;
  3379. }
  3380. #if WASM_ENABLE_REF_TYPES != 0
  3381. if (table->type->val_type->kind == WASM_ANYREF) {
  3382. return wasm_externref_obj2ref(table->inst_comm_rt, ref, p_ref_idx);
  3383. }
  3384. else
  3385. #endif
  3386. {
  3387. if (ref) {
  3388. if (NULL_REF != ref->ref_idx_rt) {
  3389. if (ref->ref_idx_rt >= function_count) {
  3390. return false;
  3391. }
  3392. }
  3393. *p_ref_idx = ref->ref_idx_rt;
  3394. wasm_ref_delete(ref);
  3395. }
  3396. else {
  3397. *p_ref_idx = NULL_REF;
  3398. }
  3399. }
  3400. return true;
  3401. }
  3402. #else /* else of WASM_ENABLE_GC == 0 */
  3403. own wasm_ref_t *
  3404. wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
  3405. {
  3406. /* TODO */
  3407. return NULL;
  3408. }
  3409. bool
  3410. wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
  3411. own wasm_ref_t *ref)
  3412. {
  3413. /* TODO */
  3414. return false;
  3415. }
  3416. #endif /* end of WASM_ENABLE_GC == 0 */
  3417. wasm_table_size_t
  3418. wasm_table_size(const wasm_table_t *table)
  3419. {
  3420. if (!table || !table->inst_comm_rt) {
  3421. return 0;
  3422. }
  3423. #if WASM_ENABLE_INTERP != 0
  3424. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3425. WASMTableInstance *table_interp =
  3426. ((WASMModuleInstance *)table->inst_comm_rt)
  3427. ->tables[table->table_idx_rt];
  3428. return table_interp->cur_size;
  3429. }
  3430. #endif
  3431. #if WASM_ENABLE_AOT != 0
  3432. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3433. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  3434. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3435. if (table->table_idx_rt < module_aot->import_table_count) {
  3436. AOTImportTable *table_aot =
  3437. module_aot->import_tables + table->table_idx_rt;
  3438. return table_aot->table_init_size;
  3439. }
  3440. else {
  3441. AOTTable *table_aot =
  3442. module_aot->tables
  3443. + (table->table_idx_rt - module_aot->import_table_count);
  3444. return table_aot->table_init_size;
  3445. }
  3446. }
  3447. #endif
  3448. /*
  3449. * a wrong combination of module filetype and compilation flags
  3450. * leads to below branch
  3451. */
  3452. return 0;
  3453. }
  3454. bool
  3455. wasm_table_grow(wasm_table_t *table, wasm_table_size_t delta,
  3456. own wasm_ref_t *init)
  3457. {
  3458. (void)table;
  3459. (void)delta;
  3460. (void)init;
  3461. LOG_WARNING("Calling wasm_table_grow() by host is not supported."
  3462. "Only allow growing a table via the opcode table.grow");
  3463. return false;
  3464. }
  3465. static wasm_memory_t *
  3466. wasm_memory_new_basic(wasm_store_t *store, const wasm_memorytype_t *type)
  3467. {
  3468. wasm_memory_t *memory = NULL;
  3469. if (!type) {
  3470. goto failed;
  3471. }
  3472. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3473. goto failed;
  3474. }
  3475. memory->store = store;
  3476. memory->kind = WASM_EXTERN_MEMORY;
  3477. memory->type = wasm_memorytype_copy(type);
  3478. RETURN_OBJ(memory, wasm_memory_delete)
  3479. }
  3480. wasm_memory_t *
  3481. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  3482. {
  3483. bh_assert(singleton_engine);
  3484. return wasm_memory_new_basic(store, type);
  3485. }
  3486. wasm_memory_t *
  3487. wasm_memory_copy(const wasm_memory_t *src)
  3488. {
  3489. wasm_memory_t *dst = NULL;
  3490. if (!src) {
  3491. return NULL;
  3492. }
  3493. if (!(dst = wasm_memory_new_basic(src->store, src->type))) {
  3494. goto failed;
  3495. }
  3496. dst->memory_idx_rt = src->memory_idx_rt;
  3497. dst->inst_comm_rt = src->inst_comm_rt;
  3498. RETURN_OBJ(dst, wasm_memory_delete)
  3499. }
  3500. wasm_memory_t *
  3501. wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
  3502. WASMModuleInstanceCommon *inst_comm_rt)
  3503. {
  3504. wasm_memory_t *memory = NULL;
  3505. uint32 min_pages = 0, max_pages = 0;
  3506. bool init_flag = false;
  3507. bh_assert(singleton_engine);
  3508. if (!inst_comm_rt) {
  3509. return NULL;
  3510. }
  3511. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3512. goto failed;
  3513. }
  3514. memory->store = store;
  3515. memory->kind = WASM_EXTERN_MEMORY;
  3516. #if WASM_ENABLE_INTERP != 0
  3517. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3518. WASMMemoryInstance *memory_interp =
  3519. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  3520. min_pages = memory_interp->cur_page_count;
  3521. max_pages = memory_interp->max_page_count;
  3522. init_flag = true;
  3523. }
  3524. #endif
  3525. #if WASM_ENABLE_AOT != 0
  3526. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3527. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3528. AOTModule *module_aot = (AOTModule *)inst_aot->module;
  3529. if (memory_idx_rt < module_aot->import_memory_count) {
  3530. min_pages = module_aot->import_memories->mem_init_page_count;
  3531. max_pages = module_aot->import_memories->mem_max_page_count;
  3532. }
  3533. else {
  3534. min_pages = module_aot->memories->mem_init_page_count;
  3535. max_pages = module_aot->memories->mem_max_page_count;
  3536. }
  3537. init_flag = true;
  3538. }
  3539. #endif
  3540. /*
  3541. * a wrong combination of module filetype and compilation flags
  3542. * leads to below branch
  3543. */
  3544. if (!init_flag) {
  3545. goto failed;
  3546. }
  3547. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  3548. goto failed;
  3549. }
  3550. memory->inst_comm_rt = inst_comm_rt;
  3551. memory->memory_idx_rt = memory_idx_rt;
  3552. RETURN_OBJ(memory, wasm_memory_delete);
  3553. }
  3554. void
  3555. wasm_memory_delete(wasm_memory_t *memory)
  3556. {
  3557. if (!memory) {
  3558. return;
  3559. }
  3560. if (memory->type) {
  3561. wasm_memorytype_delete(memory->type);
  3562. memory->type = NULL;
  3563. }
  3564. DELETE_HOST_INFO(memory)
  3565. wasm_runtime_free(memory);
  3566. }
  3567. wasm_memorytype_t *
  3568. wasm_memory_type(const wasm_memory_t *memory)
  3569. {
  3570. if (!memory) {
  3571. return NULL;
  3572. }
  3573. return wasm_memorytype_copy(memory->type);
  3574. }
  3575. byte_t *
  3576. wasm_memory_data(wasm_memory_t *memory)
  3577. {
  3578. WASMModuleInstanceCommon *module_inst_comm;
  3579. if (!memory || !memory->inst_comm_rt) {
  3580. return NULL;
  3581. }
  3582. module_inst_comm = memory->inst_comm_rt;
  3583. #if WASM_ENABLE_INTERP != 0
  3584. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3585. WASMModuleInstance *module_inst =
  3586. (WASMModuleInstance *)module_inst_comm;
  3587. WASMMemoryInstance *memory_inst =
  3588. module_inst->memories[memory->memory_idx_rt];
  3589. return (byte_t *)memory_inst->memory_data;
  3590. }
  3591. #endif
  3592. #if WASM_ENABLE_AOT != 0
  3593. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3594. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3595. AOTMemoryInstance *memory_inst =
  3596. ((AOTMemoryInstance **)
  3597. module_inst->memories)[memory->memory_idx_rt];
  3598. return (byte_t *)memory_inst->memory_data;
  3599. }
  3600. #endif
  3601. /*
  3602. * a wrong combination of module filetype and compilation flags
  3603. * leads to below branch
  3604. */
  3605. return NULL;
  3606. }
  3607. size_t
  3608. wasm_memory_data_size(const wasm_memory_t *memory)
  3609. {
  3610. WASMModuleInstanceCommon *module_inst_comm;
  3611. if (!memory || !memory->inst_comm_rt) {
  3612. return 0;
  3613. }
  3614. module_inst_comm = memory->inst_comm_rt;
  3615. #if WASM_ENABLE_INTERP != 0
  3616. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3617. WASMModuleInstance *module_inst =
  3618. (WASMModuleInstance *)module_inst_comm;
  3619. WASMMemoryInstance *memory_inst =
  3620. module_inst->memories[memory->memory_idx_rt];
  3621. return (size_t)memory_inst->cur_page_count
  3622. * memory_inst->num_bytes_per_page;
  3623. }
  3624. #endif
  3625. #if WASM_ENABLE_AOT != 0
  3626. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3627. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3628. AOTMemoryInstance *memory_inst =
  3629. ((AOTMemoryInstance **)
  3630. module_inst->memories)[memory->memory_idx_rt];
  3631. return (size_t)memory_inst->cur_page_count
  3632. * memory_inst->num_bytes_per_page;
  3633. }
  3634. #endif
  3635. /*
  3636. * a wrong combination of module filetype and compilation flags
  3637. * leads to below branch
  3638. */
  3639. return 0;
  3640. }
  3641. wasm_memory_pages_t
  3642. wasm_memory_size(const wasm_memory_t *memory)
  3643. {
  3644. WASMModuleInstanceCommon *module_inst_comm;
  3645. if (!memory || !memory->inst_comm_rt) {
  3646. return 0;
  3647. }
  3648. module_inst_comm = memory->inst_comm_rt;
  3649. #if WASM_ENABLE_INTERP != 0
  3650. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3651. WASMModuleInstance *module_inst =
  3652. (WASMModuleInstance *)module_inst_comm;
  3653. WASMMemoryInstance *memory_inst =
  3654. module_inst->memories[memory->memory_idx_rt];
  3655. return memory_inst->cur_page_count;
  3656. }
  3657. #endif
  3658. #if WASM_ENABLE_AOT != 0
  3659. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3660. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3661. AOTMemoryInstance *memory_inst =
  3662. ((AOTMemoryInstance **)
  3663. module_inst->memories)[memory->memory_idx_rt];
  3664. return memory_inst->cur_page_count;
  3665. }
  3666. #endif
  3667. /*
  3668. * a wrong combination of module filetype and compilation flags
  3669. * leads to below branch
  3670. */
  3671. return 0;
  3672. }
  3673. bool
  3674. wasm_memory_grow(wasm_memory_t *memory, wasm_memory_pages_t delta)
  3675. {
  3676. (void)memory;
  3677. (void)delta;
  3678. LOG_WARNING("Calling wasm_memory_grow() by host is not supported."
  3679. "Only allow growing a memory via the opcode memory.grow");
  3680. return false;
  3681. }
  3682. #if WASM_ENABLE_INTERP != 0
  3683. static bool
  3684. interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
  3685. uint16 func_idx_rt, wasm_func_t *import)
  3686. {
  3687. WASMImport *imported_func_interp = NULL;
  3688. bh_assert(inst && module_interp && import);
  3689. bh_assert(func_idx_rt < module_interp->import_function_count);
  3690. bh_assert(WASM_EXTERN_FUNC == import->kind);
  3691. imported_func_interp = module_interp->import_functions + func_idx_rt;
  3692. bh_assert(imported_func_interp);
  3693. bh_assert(imported_func_interp->kind == IMPORT_KIND_FUNC);
  3694. /* it is a placeholder and let's skip it*/
  3695. if (!import->type)
  3696. return true;
  3697. /* type comparison */
  3698. if (!wasm_functype_same_internal(
  3699. import->type, imported_func_interp->u.function.func_type))
  3700. return false;
  3701. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  3702. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3703. func_ptr_linked, with_env and env will be stored in module instance's
  3704. c_api_func_imports later and used when calling import function */
  3705. if (import->with_env)
  3706. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  3707. else
  3708. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  3709. bh_assert(imported_func_interp->u.function.func_ptr_linked);
  3710. import->func_idx_rt = func_idx_rt;
  3711. (void)inst;
  3712. return true;
  3713. }
  3714. static bool
  3715. interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
  3716. wasm_global_t *import)
  3717. {
  3718. WASMImport *imported_global_interp = NULL;
  3719. bh_assert(module_interp && import);
  3720. bh_assert(global_idx_rt < module_interp->import_global_count);
  3721. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  3722. imported_global_interp = module_interp->import_globals + global_idx_rt;
  3723. bh_assert(imported_global_interp);
  3724. bh_assert(imported_global_interp->kind == IMPORT_KIND_GLOBAL);
  3725. /* it is a placeholder and let's skip it*/
  3726. if (!import->type)
  3727. return true;
  3728. /* type comparison */
  3729. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(import->type->val_type),
  3730. imported_global_interp->u.global.type))
  3731. return false;
  3732. /* set init value */
  3733. bh_assert(import->init);
  3734. switch (wasm_valtype_kind(import->type->val_type)) {
  3735. case WASM_I32:
  3736. imported_global_interp->u.global.global_data_linked.i32 =
  3737. import->init->of.i32;
  3738. break;
  3739. case WASM_I64:
  3740. imported_global_interp->u.global.global_data_linked.i64 =
  3741. import->init->of.i64;
  3742. break;
  3743. case WASM_F32:
  3744. imported_global_interp->u.global.global_data_linked.f32 =
  3745. import->init->of.f32;
  3746. break;
  3747. case WASM_F64:
  3748. imported_global_interp->u.global.global_data_linked.f64 =
  3749. import->init->of.f64;
  3750. break;
  3751. default:
  3752. return false;
  3753. }
  3754. import->global_idx_rt = global_idx_rt;
  3755. imported_global_interp->u.global.is_linked = true;
  3756. return true;
  3757. }
  3758. static bool
  3759. interp_process_export(wasm_store_t *store,
  3760. const WASMModuleInstance *inst_interp,
  3761. wasm_extern_vec_t *externals)
  3762. {
  3763. WASMExport *exports = NULL;
  3764. WASMExport *export = NULL;
  3765. wasm_extern_t *external = NULL;
  3766. uint32 export_cnt = 0;
  3767. uint32 i = 0;
  3768. bh_assert(store && inst_interp && inst_interp->module && externals);
  3769. exports = inst_interp->module->exports;
  3770. export_cnt = inst_interp->module->export_count;
  3771. for (i = 0; i < export_cnt; ++i) {
  3772. export = exports + i;
  3773. switch (export->kind) {
  3774. case EXPORT_KIND_FUNC:
  3775. {
  3776. wasm_func_t *func;
  3777. if (!(func = wasm_func_new_internal(
  3778. store, export->index,
  3779. (WASMModuleInstanceCommon *)inst_interp))) {
  3780. goto failed;
  3781. }
  3782. external = wasm_func_as_extern(func);
  3783. break;
  3784. }
  3785. case EXPORT_KIND_GLOBAL:
  3786. {
  3787. wasm_global_t *global;
  3788. if (!(global = wasm_global_new_internal(
  3789. store, export->index,
  3790. (WASMModuleInstanceCommon *)inst_interp))) {
  3791. goto failed;
  3792. }
  3793. external = wasm_global_as_extern(global);
  3794. break;
  3795. }
  3796. case EXPORT_KIND_TABLE:
  3797. {
  3798. wasm_table_t *table;
  3799. if (!(table = wasm_table_new_internal(
  3800. store, export->index,
  3801. (WASMModuleInstanceCommon *)inst_interp))) {
  3802. goto failed;
  3803. }
  3804. external = wasm_table_as_extern(table);
  3805. break;
  3806. }
  3807. case EXPORT_KIND_MEMORY:
  3808. {
  3809. wasm_memory_t *memory;
  3810. if (!(memory = wasm_memory_new_internal(
  3811. store, export->index,
  3812. (WASMModuleInstanceCommon *)inst_interp))) {
  3813. goto failed;
  3814. }
  3815. external = wasm_memory_as_extern(memory);
  3816. break;
  3817. }
  3818. default:
  3819. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3820. export->kind);
  3821. goto failed;
  3822. }
  3823. if (!bh_vector_append((Vector *)externals, &external)) {
  3824. goto failed;
  3825. }
  3826. }
  3827. return true;
  3828. failed:
  3829. wasm_extern_delete(external);
  3830. return false;
  3831. }
  3832. #endif /* WASM_ENABLE_INTERP */
  3833. #if WASM_ENABLE_AOT != 0
  3834. static bool
  3835. aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
  3836. uint32 import_func_idx_rt, wasm_func_t *import)
  3837. {
  3838. AOTImportFunc *import_aot_func = NULL;
  3839. bh_assert(inst && module_aot && import);
  3840. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  3841. bh_assert(import_aot_func);
  3842. /* it is a placeholder and let's skip it*/
  3843. if (!import->type)
  3844. return true;
  3845. /* type comparison */
  3846. if (!wasm_functype_same_internal(import->type, import_aot_func->func_type))
  3847. return false;
  3848. import_aot_func->call_conv_wasm_c_api = true;
  3849. /* only set func_ptr_linked to avoid unlink warning during instantiation,
  3850. func_ptr_linked, with_env and env will be stored in module instance's
  3851. c_api_func_imports later and used when calling import function */
  3852. if (import->with_env)
  3853. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  3854. else
  3855. import_aot_func->func_ptr_linked = import->u.cb;
  3856. bh_assert(import_aot_func->func_ptr_linked);
  3857. import->func_idx_rt = import_func_idx_rt;
  3858. return true;
  3859. }
  3860. static bool
  3861. aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
  3862. wasm_global_t *import)
  3863. {
  3864. AOTImportGlobal *import_aot_global = NULL;
  3865. const wasm_valtype_t *val_type = NULL;
  3866. bh_assert(module_aot && import);
  3867. import_aot_global = module_aot->import_globals + global_idx_rt;
  3868. bh_assert(import_aot_global);
  3869. /* it is a placeholder and let's skip it*/
  3870. if (!import->type)
  3871. return true;
  3872. val_type = wasm_globaltype_content(import->type);
  3873. bh_assert(val_type);
  3874. if (!cmp_val_kind_with_val_type(wasm_valtype_kind(val_type),
  3875. import_aot_global->type))
  3876. return false;
  3877. bh_assert(import->init);
  3878. switch (wasm_valtype_kind(val_type)) {
  3879. case WASM_I32:
  3880. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  3881. break;
  3882. case WASM_I64:
  3883. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  3884. break;
  3885. case WASM_F32:
  3886. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  3887. break;
  3888. case WASM_F64:
  3889. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  3890. break;
  3891. default:
  3892. goto failed;
  3893. }
  3894. import->global_idx_rt = global_idx_rt;
  3895. import_aot_global->is_linked = true;
  3896. return true;
  3897. failed:
  3898. LOG_DEBUG("%s failed", __FUNCTION__);
  3899. return false;
  3900. }
  3901. static bool
  3902. aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
  3903. wasm_extern_vec_t *externals)
  3904. {
  3905. uint32 i;
  3906. wasm_extern_t *external = NULL;
  3907. AOTModule *module_aot = NULL;
  3908. bh_assert(store && inst_aot && externals);
  3909. module_aot = (AOTModule *)inst_aot->module;
  3910. bh_assert(module_aot);
  3911. for (i = 0; i < module_aot->export_count; ++i) {
  3912. AOTExport *export = module_aot->exports + i;
  3913. switch (export->kind) {
  3914. case EXPORT_KIND_FUNC:
  3915. {
  3916. wasm_func_t *func = NULL;
  3917. if (!(func = wasm_func_new_internal(
  3918. store, export->index,
  3919. (WASMModuleInstanceCommon *)inst_aot))) {
  3920. goto failed;
  3921. }
  3922. external = wasm_func_as_extern(func);
  3923. break;
  3924. }
  3925. case EXPORT_KIND_GLOBAL:
  3926. {
  3927. wasm_global_t *global = NULL;
  3928. if (!(global = wasm_global_new_internal(
  3929. store, export->index,
  3930. (WASMModuleInstanceCommon *)inst_aot))) {
  3931. goto failed;
  3932. }
  3933. external = wasm_global_as_extern(global);
  3934. break;
  3935. }
  3936. case EXPORT_KIND_TABLE:
  3937. {
  3938. wasm_table_t *table;
  3939. if (!(table = wasm_table_new_internal(
  3940. store, export->index,
  3941. (WASMModuleInstanceCommon *)inst_aot))) {
  3942. goto failed;
  3943. }
  3944. external = wasm_table_as_extern(table);
  3945. break;
  3946. }
  3947. case EXPORT_KIND_MEMORY:
  3948. {
  3949. wasm_memory_t *memory;
  3950. if (!(memory = wasm_memory_new_internal(
  3951. store, export->index,
  3952. (WASMModuleInstanceCommon *)inst_aot))) {
  3953. goto failed;
  3954. }
  3955. external = wasm_memory_as_extern(memory);
  3956. break;
  3957. }
  3958. default:
  3959. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3960. export->kind);
  3961. goto failed;
  3962. }
  3963. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3964. goto failed;
  3965. }
  3966. wasm_name_new_from_string_nt(external->name, export->name);
  3967. if (strlen(export->name) && !external->name->data) {
  3968. goto failed;
  3969. }
  3970. if (!bh_vector_append((Vector *)externals, &external)) {
  3971. goto failed;
  3972. }
  3973. }
  3974. return true;
  3975. failed:
  3976. wasm_extern_delete(external);
  3977. return false;
  3978. }
  3979. #endif /* WASM_ENABLE_AOT */
  3980. static bool
  3981. do_link(const wasm_instance_t *inst, const wasm_module_t *module,
  3982. const wasm_extern_vec_t *imports)
  3983. {
  3984. uint32 i, import_func_i, import_global_i;
  3985. bh_assert(inst && module);
  3986. /* we have run a module_type check before. */
  3987. for (i = 0, import_func_i = 0, import_global_i = 0; i < imports->num_elems;
  3988. i++) {
  3989. wasm_extern_t *import = imports->data[i];
  3990. if (!import) {
  3991. LOG_ERROR("imports[%d] is NULL and it is fatal\n", i);
  3992. goto failed;
  3993. }
  3994. switch (wasm_extern_kind(import)) {
  3995. case WASM_EXTERN_FUNC:
  3996. {
  3997. bool ret = false;
  3998. #if WASM_ENABLE_INTERP != 0
  3999. if ((*module)->module_type == Wasm_Module_Bytecode) {
  4000. ret = interp_link_func(inst, MODULE_INTERP(module),
  4001. import_func_i,
  4002. wasm_extern_as_func(import));
  4003. }
  4004. #endif
  4005. #if WASM_ENABLE_AOT != 0
  4006. if ((*module)->module_type == Wasm_Module_AoT) {
  4007. ret = aot_link_func(inst, MODULE_AOT(module), import_func_i,
  4008. wasm_extern_as_func(import));
  4009. }
  4010. #endif
  4011. if (!ret) {
  4012. LOG_WARNING("link function #%d failed", import_func_i);
  4013. goto failed;
  4014. }
  4015. import_func_i++;
  4016. break;
  4017. }
  4018. case WASM_EXTERN_GLOBAL:
  4019. {
  4020. bool ret = false;
  4021. #if WASM_ENABLE_INTERP != 0
  4022. if ((*module)->module_type == Wasm_Module_Bytecode) {
  4023. ret = interp_link_global(MODULE_INTERP(module),
  4024. import_global_i,
  4025. wasm_extern_as_global(import));
  4026. }
  4027. #endif
  4028. #if WASM_ENABLE_AOT != 0
  4029. if ((*module)->module_type == Wasm_Module_AoT) {
  4030. ret = aot_link_global(MODULE_AOT(module), import_global_i,
  4031. wasm_extern_as_global(import));
  4032. }
  4033. #endif
  4034. if (!ret) {
  4035. LOG_WARNING("link global #%d failed", import_global_i);
  4036. goto failed;
  4037. }
  4038. import_global_i++;
  4039. break;
  4040. }
  4041. case WASM_EXTERN_MEMORY:
  4042. case WASM_EXTERN_TABLE:
  4043. {
  4044. LOG_WARNING("doesn't support import memories and tables for "
  4045. "now, ignore them");
  4046. break;
  4047. }
  4048. default:
  4049. {
  4050. UNREACHABLE();
  4051. break;
  4052. }
  4053. }
  4054. }
  4055. return true;
  4056. failed:
  4057. LOG_DEBUG("%s failed", __FUNCTION__);
  4058. return false;
  4059. }
  4060. wasm_instance_t *
  4061. wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
  4062. const wasm_extern_vec_t *imports, own wasm_trap_t **trap)
  4063. {
  4064. return wasm_instance_new_with_args(store, module, imports, trap,
  4065. KILOBYTE(32), KILOBYTE(32));
  4066. }
  4067. wasm_instance_t *
  4068. wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
  4069. const wasm_extern_vec_t *imports,
  4070. own wasm_trap_t **trap, const uint32 stack_size,
  4071. const uint32 heap_size)
  4072. {
  4073. char sub_error_buf[128] = { 0 };
  4074. char error_buf[256] = { 0 };
  4075. wasm_instance_t *instance = NULL;
  4076. CApiFuncImport *func_import = NULL, **p_func_imports = NULL;
  4077. uint32 i = 0, import_func_count = 0;
  4078. uint64 total_size;
  4079. bool build_exported = false;
  4080. bh_assert(singleton_engine);
  4081. if (!module)
  4082. return NULL;
  4083. /*
  4084. * will do the check at the end of wasm_runtime_instantiate
  4085. */
  4086. WASM_C_DUMP_PROC_MEM();
  4087. instance = malloc_internal(sizeof(wasm_instance_t));
  4088. if (!instance) {
  4089. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4090. "Failed to malloc instance");
  4091. goto failed;
  4092. }
  4093. /* executes the instantiate-time linking if provided */
  4094. if (imports) {
  4095. if (!do_link(instance, module, imports)) {
  4096. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4097. "Failed to validate imports");
  4098. goto failed;
  4099. }
  4100. }
  4101. /*
  4102. * will do the linking result check at the end of wasm_runtime_instantiate
  4103. */
  4104. instance->inst_comm_rt = wasm_runtime_instantiate(
  4105. *module, stack_size, heap_size, sub_error_buf, sizeof(sub_error_buf));
  4106. if (!instance->inst_comm_rt) {
  4107. goto failed;
  4108. }
  4109. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  4110. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4111. "Failed to create exec env singleton");
  4112. goto failed;
  4113. }
  4114. /* create the c-api func import list */
  4115. #if WASM_ENABLE_INTERP != 0
  4116. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4117. WASMModuleInstanceExtra *e =
  4118. ((WASMModuleInstance *)instance->inst_comm_rt)->e;
  4119. p_func_imports = &(e->common.c_api_func_imports);
  4120. import_func_count = MODULE_INTERP(module)->import_function_count;
  4121. }
  4122. #endif
  4123. #if WASM_ENABLE_AOT != 0
  4124. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4125. AOTModuleInstanceExtra *e =
  4126. (AOTModuleInstanceExtra *)((AOTModuleInstance *)
  4127. instance->inst_comm_rt)
  4128. ->e;
  4129. p_func_imports = &(e->common.c_api_func_imports);
  4130. import_func_count = MODULE_AOT(module)->import_func_count;
  4131. }
  4132. #endif
  4133. bh_assert(p_func_imports);
  4134. total_size = (uint64)sizeof(CApiFuncImport) * import_func_count;
  4135. if (total_size > 0
  4136. && !(*p_func_imports = func_import = malloc_internal(total_size))) {
  4137. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4138. "Failed to create wasm-c-api func imports");
  4139. goto failed;
  4140. }
  4141. /* fill in module_inst->e->c_api_func_imports */
  4142. for (i = 0; imports && i < imports->num_elems; i++) {
  4143. wasm_func_t *func_host = NULL;
  4144. wasm_extern_t *in = imports->data[i];
  4145. bh_assert(in);
  4146. if (wasm_extern_kind(in) != WASM_EXTERN_FUNC)
  4147. continue;
  4148. func_host = wasm_extern_as_func(in);
  4149. /* it is a placeholder and let's skip it*/
  4150. if (!func_host->type) {
  4151. func_import++;
  4152. continue;
  4153. }
  4154. func_import->with_env_arg = func_host->with_env;
  4155. if (func_host->with_env) {
  4156. func_import->func_ptr_linked = func_host->u.cb_env.cb;
  4157. func_import->env_arg = func_host->u.cb_env.env;
  4158. }
  4159. else {
  4160. func_import->func_ptr_linked = func_host->u.cb;
  4161. func_import->env_arg = NULL;
  4162. }
  4163. bh_assert(func_import->func_ptr_linked);
  4164. func_import++;
  4165. }
  4166. /* fill with inst */
  4167. for (i = 0; imports && imports->data && i < imports->num_elems; ++i) {
  4168. wasm_extern_t *import = imports->data[i];
  4169. bh_assert(import);
  4170. switch (import->kind) {
  4171. case WASM_EXTERN_FUNC:
  4172. wasm_extern_as_func(import)->inst_comm_rt =
  4173. instance->inst_comm_rt;
  4174. break;
  4175. case WASM_EXTERN_GLOBAL:
  4176. wasm_extern_as_global(import)->inst_comm_rt =
  4177. instance->inst_comm_rt;
  4178. break;
  4179. case WASM_EXTERN_MEMORY:
  4180. wasm_extern_as_memory(import)->inst_comm_rt =
  4181. instance->inst_comm_rt;
  4182. break;
  4183. case WASM_EXTERN_TABLE:
  4184. wasm_extern_as_table(import)->inst_comm_rt =
  4185. instance->inst_comm_rt;
  4186. break;
  4187. default:
  4188. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4189. "Unknown import kind");
  4190. goto failed;
  4191. }
  4192. }
  4193. /* build the exports list */
  4194. #if WASM_ENABLE_INTERP != 0
  4195. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  4196. uint32 export_cnt = ((WASMModuleInstance *)instance->inst_comm_rt)
  4197. ->module->export_count;
  4198. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4199. export_cnt);
  4200. if (!interp_process_export(store,
  4201. (WASMModuleInstance *)instance->inst_comm_rt,
  4202. instance->exports)) {
  4203. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4204. "Interpreter failed to process exports");
  4205. goto failed;
  4206. }
  4207. build_exported = true;
  4208. }
  4209. #endif
  4210. #if WASM_ENABLE_AOT != 0
  4211. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  4212. uint32 export_cnt =
  4213. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  4214. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  4215. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_table_count
  4216. + ((AOTModuleInstance *)instance->inst_comm_rt)
  4217. ->export_memory_count;
  4218. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  4219. export_cnt);
  4220. if (!aot_process_export(store,
  4221. (AOTModuleInstance *)instance->inst_comm_rt,
  4222. instance->exports)) {
  4223. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4224. "AOT failed to process exports");
  4225. goto failed;
  4226. }
  4227. build_exported = true;
  4228. }
  4229. #endif
  4230. /*
  4231. * a wrong combination of module filetype and compilation flags
  4232. * leads to below branch
  4233. */
  4234. if (!build_exported) {
  4235. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4236. "Incorrect filetype and compilation flags");
  4237. goto failed;
  4238. }
  4239. /* add it to a watching list in store */
  4240. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  4241. snprintf(sub_error_buf, sizeof(sub_error_buf),
  4242. "Failed to add to store instances");
  4243. goto failed;
  4244. }
  4245. WASM_C_DUMP_PROC_MEM();
  4246. return instance;
  4247. failed:
  4248. snprintf(error_buf, sizeof(error_buf), "%s failed: %s", __FUNCTION__,
  4249. sub_error_buf);
  4250. if (trap != NULL) {
  4251. wasm_message_t message = { 0 };
  4252. wasm_name_new_from_string_nt(&message, error_buf);
  4253. *trap = wasm_trap_new(store, &message);
  4254. wasm_byte_vec_delete(&message);
  4255. }
  4256. LOG_DEBUG(error_buf);
  4257. wasm_instance_delete_internal(instance);
  4258. return NULL;
  4259. }
  4260. static void
  4261. wasm_instance_delete_internal(wasm_instance_t *instance)
  4262. {
  4263. if (!instance) {
  4264. return;
  4265. }
  4266. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  4267. if (instance->inst_comm_rt) {
  4268. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  4269. instance->inst_comm_rt = NULL;
  4270. }
  4271. wasm_runtime_free(instance);
  4272. }
  4273. void
  4274. wasm_instance_delete(wasm_instance_t *inst)
  4275. {
  4276. DELETE_HOST_INFO(inst)
  4277. /* will release instance when releasing the store */
  4278. }
  4279. void
  4280. wasm_instance_exports(const wasm_instance_t *instance,
  4281. own wasm_extern_vec_t *out)
  4282. {
  4283. if (!instance || !out) {
  4284. return;
  4285. }
  4286. wasm_extern_vec_copy(out, instance->exports);
  4287. }
  4288. wasm_extern_t *
  4289. wasm_extern_copy(const wasm_extern_t *src)
  4290. {
  4291. wasm_extern_t *dst = NULL;
  4292. if (!src) {
  4293. return NULL;
  4294. }
  4295. switch (wasm_extern_kind(src)) {
  4296. case WASM_EXTERN_FUNC:
  4297. dst = wasm_func_as_extern(
  4298. wasm_func_copy(wasm_extern_as_func_const(src)));
  4299. break;
  4300. case WASM_EXTERN_GLOBAL:
  4301. dst = wasm_global_as_extern(
  4302. wasm_global_copy(wasm_extern_as_global_const(src)));
  4303. break;
  4304. case WASM_EXTERN_MEMORY:
  4305. dst = wasm_memory_as_extern(
  4306. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  4307. break;
  4308. case WASM_EXTERN_TABLE:
  4309. dst = wasm_table_as_extern(
  4310. wasm_table_copy(wasm_extern_as_table_const(src)));
  4311. break;
  4312. default:
  4313. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4314. src->kind);
  4315. break;
  4316. }
  4317. if (!dst) {
  4318. goto failed;
  4319. }
  4320. return dst;
  4321. failed:
  4322. LOG_DEBUG("%s failed", __FUNCTION__);
  4323. wasm_extern_delete(dst);
  4324. return NULL;
  4325. }
  4326. void
  4327. wasm_extern_delete(wasm_extern_t *external)
  4328. {
  4329. if (!external) {
  4330. return;
  4331. }
  4332. if (external->name) {
  4333. wasm_byte_vec_delete(external->name);
  4334. wasm_runtime_free(external->name);
  4335. external->name = NULL;
  4336. }
  4337. switch (wasm_extern_kind(external)) {
  4338. case WASM_EXTERN_FUNC:
  4339. wasm_func_delete(wasm_extern_as_func(external));
  4340. break;
  4341. case WASM_EXTERN_GLOBAL:
  4342. wasm_global_delete(wasm_extern_as_global(external));
  4343. break;
  4344. case WASM_EXTERN_MEMORY:
  4345. wasm_memory_delete(wasm_extern_as_memory(external));
  4346. break;
  4347. case WASM_EXTERN_TABLE:
  4348. wasm_table_delete(wasm_extern_as_table(external));
  4349. break;
  4350. default:
  4351. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4352. external->kind);
  4353. break;
  4354. }
  4355. }
  4356. wasm_externkind_t
  4357. wasm_extern_kind(const wasm_extern_t *external)
  4358. {
  4359. if (!external) {
  4360. return WASM_ANYREF;
  4361. }
  4362. return external->kind;
  4363. }
  4364. own wasm_externtype_t *
  4365. wasm_extern_type(const wasm_extern_t *external)
  4366. {
  4367. if (!external) {
  4368. return NULL;
  4369. }
  4370. switch (wasm_extern_kind(external)) {
  4371. case WASM_EXTERN_FUNC:
  4372. return wasm_functype_as_externtype(
  4373. wasm_func_type(wasm_extern_as_func_const(external)));
  4374. case WASM_EXTERN_GLOBAL:
  4375. return wasm_globaltype_as_externtype(
  4376. wasm_global_type(wasm_extern_as_global_const(external)));
  4377. case WASM_EXTERN_MEMORY:
  4378. return wasm_memorytype_as_externtype(
  4379. wasm_memory_type(wasm_extern_as_memory_const(external)));
  4380. case WASM_EXTERN_TABLE:
  4381. return wasm_tabletype_as_externtype(
  4382. wasm_table_type(wasm_extern_as_table_const(external)));
  4383. default:
  4384. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  4385. external->kind);
  4386. break;
  4387. }
  4388. return NULL;
  4389. }
  4390. #define BASIC_FOUR_LIST(V) \
  4391. V(func) \
  4392. V(global) \
  4393. V(memory) \
  4394. V(table)
  4395. #define WASM_EXTERN_AS_OTHER(name) \
  4396. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  4397. { \
  4398. return (wasm_##name##_t *)external; \
  4399. }
  4400. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  4401. #undef WASM_EXTERN_AS_OTHER
  4402. #define WASM_OTHER_AS_EXTERN(name) \
  4403. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  4404. { \
  4405. return (wasm_extern_t *)other; \
  4406. }
  4407. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  4408. #undef WASM_OTHER_AS_EXTERN
  4409. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  4410. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  4411. const wasm_extern_t *external) \
  4412. { \
  4413. return (const wasm_##name##_t *)external; \
  4414. }
  4415. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  4416. #undef WASM_EXTERN_AS_OTHER_CONST
  4417. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  4418. const wasm_extern_t *wasm_##name##_as_extern_const( \
  4419. const wasm_##name##_t *other) \
  4420. { \
  4421. return (const wasm_extern_t *)other; \
  4422. }
  4423. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  4424. #undef WASM_OTHER_AS_EXTERN_CONST
  4425. wasm_extern_t *
  4426. wasm_extern_new_empty(wasm_store_t *store, wasm_externkind_t extern_kind)
  4427. {
  4428. if (extern_kind == WASM_EXTERN_FUNC)
  4429. return wasm_func_as_extern(wasm_func_new_empty(store));
  4430. if (extern_kind == WASM_EXTERN_GLOBAL)
  4431. return wasm_global_as_extern(wasm_global_new_empty(store));
  4432. LOG_ERROR("Don't support linking table and memory for now");
  4433. return NULL;
  4434. }