wasm_c_api.c 147 KB

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