wasm_c_api.c 132 KB

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