wasm_c_api.c 133 KB

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