wasm_c_api.c 153 KB

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