wasm_c_api.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591
  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. 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 = import->u.names.module_name;
  1683. field_name_rt = import->u.names.field_name;
  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;
  1691. field_name_rt = import->func_name;
  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 = import->u.names.module_name;
  1720. field_name_rt = import->u.names.field_name;
  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 = MODULE_AOT(module)->import_globals
  1728. + (i - import_func_count);
  1729. module_name_rt = import->module_name;
  1730. field_name_rt = import->global_name;
  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 = import->u.names.module_name;
  1754. field_name_rt = import->u.names.field_name;
  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 = import->u.names.module_name;
  1797. field_name_rt = import->u.names.field_name;
  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. (AOTFunctionInstance *)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. #else
  2481. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2482. #endif
  2483. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_interp,
  2484. val_type_rt, v, data);
  2485. }
  2486. static bool
  2487. interp_global_get(const WASMModuleInstance *inst_interp, uint16 global_idx_rt,
  2488. wasm_val_t *out)
  2489. {
  2490. WASMGlobalInstance *global_interp = inst_interp->globals + global_idx_rt;
  2491. uint8 val_type_rt = global_interp->type;
  2492. #if WASM_ENABLE_MULTI_MODULE != 0
  2493. uint8 *data = global_interp->import_global_inst
  2494. ? global_interp->import_module_inst->global_data
  2495. + global_interp->import_global_inst->data_offset
  2496. : inst_interp->global_data + global_interp->data_offset;
  2497. #else
  2498. uint8 *data = inst_interp->global_data + global_interp->data_offset;
  2499. #endif
  2500. return rt_val_to_wasm_val(data, val_type_rt, out);
  2501. }
  2502. #endif
  2503. #if WASM_ENABLE_AOT != 0
  2504. static bool
  2505. aot_global_set(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  2506. const wasm_val_t *v)
  2507. {
  2508. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2509. uint8 val_type_rt = 0;
  2510. uint32 data_offset = 0;
  2511. void *data = NULL;
  2512. if (global_idx_rt < module_aot->import_global_count) {
  2513. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2514. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2515. }
  2516. else {
  2517. data_offset =
  2518. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2519. .data_offset;
  2520. val_type_rt =
  2521. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2522. .type;
  2523. }
  2524. data = (void *)((uint8 *)inst_aot->global_data.ptr + data_offset);
  2525. return wasm_val_to_rt_val((WASMModuleInstanceCommon *)inst_aot, val_type_rt,
  2526. v, data);
  2527. }
  2528. static bool
  2529. aot_global_get(const AOTModuleInstance *inst_aot, uint16 global_idx_rt,
  2530. wasm_val_t *out)
  2531. {
  2532. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2533. uint8 val_type_rt = 0;
  2534. uint32 data_offset = 0;
  2535. uint8 *data = NULL;
  2536. if (global_idx_rt < module_aot->import_global_count) {
  2537. data_offset = module_aot->import_globals[global_idx_rt].data_offset;
  2538. val_type_rt = module_aot->import_globals[global_idx_rt].type;
  2539. }
  2540. else {
  2541. data_offset =
  2542. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2543. .data_offset;
  2544. val_type_rt =
  2545. module_aot->globals[global_idx_rt - module_aot->import_global_count]
  2546. .type;
  2547. }
  2548. data = (uint8 *)inst_aot->global_data.ptr + data_offset;
  2549. return rt_val_to_wasm_val(data, val_type_rt, out);
  2550. }
  2551. #endif
  2552. void
  2553. wasm_global_set(wasm_global_t *global, const wasm_val_t *v)
  2554. {
  2555. if (!global || !v) {
  2556. return;
  2557. }
  2558. #if WASM_ENABLE_INTERP != 0
  2559. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2560. (void)interp_global_set((WASMModuleInstance *)global->inst_comm_rt,
  2561. global->global_idx_rt, v);
  2562. return;
  2563. }
  2564. #endif
  2565. #if WASM_ENABLE_AOT != 0
  2566. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2567. (void)aot_global_set((AOTModuleInstance *)global->inst_comm_rt,
  2568. global->global_idx_rt, v);
  2569. return;
  2570. }
  2571. #endif
  2572. /*
  2573. * a wrong combination of module filetype and compilation flags
  2574. * leads to below branch
  2575. */
  2576. UNREACHABLE();
  2577. }
  2578. void
  2579. wasm_global_get(const wasm_global_t *global, wasm_val_t *out)
  2580. {
  2581. if (!global || !out) {
  2582. return;
  2583. }
  2584. memset(out, 0, sizeof(wasm_val_t));
  2585. #if WASM_ENABLE_INTERP != 0
  2586. if (global->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2587. (void)interp_global_get((WASMModuleInstance *)global->inst_comm_rt,
  2588. global->global_idx_rt, out);
  2589. return;
  2590. }
  2591. #endif
  2592. #if WASM_ENABLE_AOT != 0
  2593. if (global->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2594. (void)aot_global_get((AOTModuleInstance *)global->inst_comm_rt,
  2595. global->global_idx_rt, out);
  2596. return;
  2597. }
  2598. #endif
  2599. /*
  2600. * a wrong combination of module filetype and compilation flags
  2601. * leads to below branch
  2602. */
  2603. UNREACHABLE();
  2604. }
  2605. wasm_global_t *
  2606. wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
  2607. WASMModuleInstanceCommon *inst_comm_rt)
  2608. {
  2609. wasm_global_t *global = NULL;
  2610. uint8 val_type_rt = 0;
  2611. bool is_mutable = 0;
  2612. bool init = false;
  2613. bh_assert(singleton_engine);
  2614. if (!inst_comm_rt) {
  2615. return NULL;
  2616. }
  2617. global = malloc_internal(sizeof(wasm_global_t));
  2618. if (!global) {
  2619. goto failed;
  2620. }
  2621. global->store = store;
  2622. global->kind = WASM_EXTERN_GLOBAL;
  2623. #if WASM_ENABLE_INTERP != 0
  2624. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2625. WASMGlobalInstance *global_interp =
  2626. ((WASMModuleInstance *)inst_comm_rt)->globals + global_idx_rt;
  2627. val_type_rt = global_interp->type;
  2628. is_mutable = global_interp->is_mutable;
  2629. init = true;
  2630. }
  2631. #endif
  2632. #if WASM_ENABLE_AOT != 0
  2633. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2634. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2635. AOTModule *module_aot = inst_aot->aot_module.ptr;
  2636. init = true;
  2637. if (global_idx_rt < module_aot->import_global_count) {
  2638. AOTImportGlobal *global_import_aot =
  2639. module_aot->import_globals + global_idx_rt;
  2640. val_type_rt = global_import_aot->type;
  2641. is_mutable = global_import_aot->is_mutable;
  2642. }
  2643. else {
  2644. AOTGlobal *global_aot =
  2645. module_aot->globals
  2646. + (global_idx_rt - module_aot->import_global_count);
  2647. val_type_rt = global_aot->type;
  2648. is_mutable = global_aot->is_mutable;
  2649. }
  2650. }
  2651. #endif
  2652. /*
  2653. * a wrong combination of module filetype and compilation flags
  2654. * leads to below branch
  2655. */
  2656. if (!init) {
  2657. goto failed;
  2658. }
  2659. global->type = wasm_globaltype_new_internal(val_type_rt, is_mutable);
  2660. if (!global->type) {
  2661. goto failed;
  2662. }
  2663. global->init = malloc_internal(sizeof(wasm_val_t));
  2664. if (!global->init) {
  2665. goto failed;
  2666. }
  2667. #if WASM_ENABLE_INTERP != 0
  2668. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2669. interp_global_get((WASMModuleInstance *)inst_comm_rt, global_idx_rt,
  2670. global->init);
  2671. }
  2672. #endif
  2673. #if WASM_ENABLE_AOT != 0
  2674. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2675. aot_global_get((AOTModuleInstance *)inst_comm_rt, global_idx_rt,
  2676. global->init);
  2677. }
  2678. #endif
  2679. global->inst_comm_rt = inst_comm_rt;
  2680. global->global_idx_rt = global_idx_rt;
  2681. return global;
  2682. failed:
  2683. LOG_DEBUG("%s failed", __FUNCTION__);
  2684. wasm_global_delete(global);
  2685. return NULL;
  2686. }
  2687. wasm_globaltype_t *
  2688. wasm_global_type(const wasm_global_t *global)
  2689. {
  2690. if (!global) {
  2691. return NULL;
  2692. }
  2693. return wasm_globaltype_copy(global->type);
  2694. }
  2695. static wasm_table_t *
  2696. wasm_table_new_basic(wasm_store_t *store, const wasm_tabletype_t *type)
  2697. {
  2698. wasm_table_t *table = NULL;
  2699. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2700. goto failed;
  2701. }
  2702. table->store = store;
  2703. table->kind = WASM_EXTERN_TABLE;
  2704. if (!(table->type = wasm_tabletype_copy(type))) {
  2705. goto failed;
  2706. }
  2707. RETURN_OBJ(table, wasm_table_delete);
  2708. }
  2709. wasm_table_t *
  2710. wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
  2711. WASMModuleInstanceCommon *inst_comm_rt)
  2712. {
  2713. wasm_table_t *table = NULL;
  2714. uint8 val_type_rt = 0;
  2715. uint32 init_size = 0, max_size = 0;
  2716. bool init_flag = false;
  2717. bh_assert(singleton_engine);
  2718. if (!inst_comm_rt) {
  2719. return NULL;
  2720. }
  2721. if (!(table = malloc_internal(sizeof(wasm_table_t)))) {
  2722. goto failed;
  2723. }
  2724. table->store = store;
  2725. table->kind = WASM_EXTERN_TABLE;
  2726. #if WASM_ENABLE_INTERP != 0
  2727. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2728. WASMTableInstance *table_interp =
  2729. ((WASMModuleInstance *)inst_comm_rt)->tables[table_idx_rt];
  2730. val_type_rt = table_interp->elem_type;
  2731. init_size = table_interp->cur_size;
  2732. max_size = table_interp->max_size;
  2733. init_flag = true;
  2734. }
  2735. #endif
  2736. #if WASM_ENABLE_AOT != 0
  2737. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  2738. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  2739. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2740. if (table_idx_rt < module_aot->import_table_count) {
  2741. AOTImportTable *table_aot =
  2742. module_aot->import_tables + table_idx_rt;
  2743. val_type_rt = VALUE_TYPE_FUNCREF;
  2744. init_size = table_aot->table_init_size;
  2745. max_size = table_aot->table_max_size;
  2746. }
  2747. else {
  2748. AOTTable *table_aot =
  2749. module_aot->tables
  2750. + (table_idx_rt - module_aot->import_table_count);
  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. init_flag = true;
  2756. }
  2757. #endif
  2758. /*
  2759. * a wrong combination of module filetype and compilation flags
  2760. * leads to below branch
  2761. */
  2762. if (!init_flag) {
  2763. goto failed;
  2764. }
  2765. if (!(table->type =
  2766. wasm_tabletype_new_internal(val_type_rt, init_size, max_size))) {
  2767. goto failed;
  2768. }
  2769. table->inst_comm_rt = inst_comm_rt;
  2770. table->table_idx_rt = table_idx_rt;
  2771. RETURN_OBJ(table, wasm_table_delete);
  2772. }
  2773. /* will not actually apply this new table into the runtime */
  2774. wasm_table_t *
  2775. wasm_table_new(wasm_store_t *store, const wasm_tabletype_t *table_type,
  2776. wasm_ref_t *init)
  2777. {
  2778. wasm_table_t *table;
  2779. (void)init;
  2780. bh_assert(singleton_engine);
  2781. if ((table = wasm_table_new_basic(store, table_type))) {
  2782. table->store = store;
  2783. }
  2784. return table;
  2785. }
  2786. wasm_table_t *
  2787. wasm_table_copy(const wasm_table_t *src)
  2788. {
  2789. wasm_table_t *table;
  2790. if (!(table = wasm_table_new_basic(src->store, src->type))) {
  2791. return NULL;
  2792. }
  2793. table->table_idx_rt = src->table_idx_rt;
  2794. table->inst_comm_rt = src->inst_comm_rt;
  2795. return table;
  2796. }
  2797. void
  2798. wasm_table_delete(wasm_table_t *table)
  2799. {
  2800. if (!table) {
  2801. return;
  2802. }
  2803. if (table->type) {
  2804. wasm_tabletype_delete(table->type);
  2805. table->type = NULL;
  2806. }
  2807. DELETE_HOST_INFO(table)
  2808. wasm_runtime_free(table);
  2809. }
  2810. wasm_tabletype_t *
  2811. wasm_table_type(const wasm_table_t *table)
  2812. {
  2813. if (!table) {
  2814. return NULL;
  2815. }
  2816. return wasm_tabletype_copy(table->type);
  2817. }
  2818. own wasm_ref_t *
  2819. wasm_table_get(const wasm_table_t *table, wasm_table_size_t index)
  2820. {
  2821. uint32 func_idx_rt = NULL_REF;
  2822. if (!table) {
  2823. return NULL;
  2824. }
  2825. /* index -> func_idx_rt */
  2826. #if WASM_ENABLE_INTERP != 0
  2827. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2828. WASMTableInstance *table_interp =
  2829. ((WASMModuleInstance *)table->inst_comm_rt)
  2830. ->tables[table->table_idx_rt];
  2831. if (index >= table_interp->cur_size) {
  2832. return NULL;
  2833. }
  2834. func_idx_rt = ((uint32 *)table_interp->base_addr)[index];
  2835. }
  2836. #endif
  2837. #if WASM_ENABLE_AOT != 0
  2838. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2839. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  2840. AOTTableInstance *table_aot =
  2841. (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt;
  2842. if (index >= table_aot->cur_size) {
  2843. return NULL;
  2844. }
  2845. func_idx_rt = table_aot->data[index];
  2846. }
  2847. #endif
  2848. /*
  2849. * a wrong combination of module filetype and compilation flags
  2850. * also leads to below branch
  2851. */
  2852. if (func_idx_rt == NULL_REF) {
  2853. return NULL;
  2854. }
  2855. return wasm_ref_new_internal(table->store, WASM_REF_func, func_idx_rt,
  2856. table->inst_comm_rt);
  2857. }
  2858. bool
  2859. wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
  2860. own wasm_ref_t *func_ref)
  2861. {
  2862. uint32 *p_func_idx_rt = NULL;
  2863. uint32 function_count = 0, ref_idx_rt = NULL_REF;
  2864. if (!table) {
  2865. return false;
  2866. }
  2867. if (func_ref && func_ref->kind != WASM_REF_func) {
  2868. return false;
  2869. }
  2870. if (func_ref) {
  2871. ref_idx_rt = func_ref->ref_idx_rt;
  2872. wasm_ref_delete(func_ref);
  2873. }
  2874. /* index -> *p_func_idx_rt */
  2875. #if WASM_ENABLE_INTERP != 0
  2876. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2877. WASMTableInstance *table_interp =
  2878. ((WASMModuleInstance *)table->inst_comm_rt)
  2879. ->tables[table->table_idx_rt];
  2880. if (index >= table_interp->cur_size) {
  2881. return false;
  2882. }
  2883. p_func_idx_rt = ((uint32 *)table_interp->base_addr) + index;
  2884. function_count =
  2885. ((WASMModuleInstance *)table->inst_comm_rt)->function_count;
  2886. }
  2887. #endif
  2888. #if WASM_ENABLE_AOT != 0
  2889. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2890. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  2891. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2892. AOTTableInstance *table_aot =
  2893. (AOTTableInstance *)inst_aot->tables.ptr + table->table_idx_rt;
  2894. if (index >= table_aot->cur_size) {
  2895. return false;
  2896. }
  2897. p_func_idx_rt = table_aot->data + index;
  2898. function_count = module_aot->func_count;
  2899. }
  2900. #endif
  2901. /*
  2902. * a wrong combination of module filetype and compilation flags
  2903. * leads to below branch
  2904. */
  2905. if (!p_func_idx_rt) {
  2906. return false;
  2907. }
  2908. if (NULL_REF != ref_idx_rt) {
  2909. if (ref_idx_rt >= function_count) {
  2910. return false;
  2911. }
  2912. }
  2913. *p_func_idx_rt = ref_idx_rt;
  2914. return true;
  2915. }
  2916. wasm_table_size_t
  2917. wasm_table_size(const wasm_table_t *table)
  2918. {
  2919. if (!table) {
  2920. return 0;
  2921. }
  2922. #if WASM_ENABLE_INTERP != 0
  2923. if (table->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  2924. WASMTableInstance *table_interp =
  2925. ((WASMModuleInstance *)table->inst_comm_rt)
  2926. ->tables[table->table_idx_rt];
  2927. return table_interp->cur_size;
  2928. }
  2929. #endif
  2930. #if WASM_ENABLE_AOT != 0
  2931. if (table->inst_comm_rt->module_type == Wasm_Module_AoT) {
  2932. AOTModuleInstance *inst_aot = (AOTModuleInstance *)table->inst_comm_rt;
  2933. AOTModule *module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  2934. if (table->table_idx_rt < module_aot->import_table_count) {
  2935. AOTImportTable *table_aot =
  2936. module_aot->import_tables + table->table_idx_rt;
  2937. return table_aot->table_init_size;
  2938. }
  2939. else {
  2940. AOTTable *table_aot =
  2941. module_aot->tables
  2942. + (table->table_idx_rt - module_aot->import_table_count);
  2943. return table_aot->table_init_size;
  2944. }
  2945. }
  2946. #endif
  2947. /*
  2948. * a wrong combination of module filetype and compilation flags
  2949. * leads to below branch
  2950. */
  2951. return 0;
  2952. }
  2953. bool
  2954. wasm_table_grow(wasm_table_t *table, wasm_table_size_t delta,
  2955. own wasm_ref_t *init)
  2956. {
  2957. (void)table;
  2958. (void)delta;
  2959. (void)init;
  2960. LOG_WARNING("Calling wasm_table_grow() by host is not supported."
  2961. "Only allow growing a table via the opcode table.grow");
  2962. return false;
  2963. }
  2964. static wasm_memory_t *
  2965. wasm_memory_new_basic(wasm_store_t *store, const wasm_memorytype_t *type)
  2966. {
  2967. wasm_memory_t *memory = NULL;
  2968. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  2969. goto failed;
  2970. }
  2971. memory->store = store;
  2972. memory->kind = WASM_EXTERN_MEMORY;
  2973. memory->type = wasm_memorytype_copy(type);
  2974. RETURN_OBJ(memory, wasm_memory_delete)
  2975. }
  2976. wasm_memory_t *
  2977. wasm_memory_new(wasm_store_t *store, const wasm_memorytype_t *type)
  2978. {
  2979. bh_assert(singleton_engine);
  2980. return wasm_memory_new_basic(store, type);
  2981. }
  2982. wasm_memory_t *
  2983. wasm_memory_copy(const wasm_memory_t *src)
  2984. {
  2985. wasm_memory_t *dst = NULL;
  2986. if (!src) {
  2987. return NULL;
  2988. }
  2989. if (!(dst = wasm_memory_new_basic(src->store, src->type))) {
  2990. goto failed;
  2991. }
  2992. dst->memory_idx_rt = src->memory_idx_rt;
  2993. dst->inst_comm_rt = src->inst_comm_rt;
  2994. RETURN_OBJ(dst, wasm_memory_delete)
  2995. }
  2996. wasm_memory_t *
  2997. wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
  2998. WASMModuleInstanceCommon *inst_comm_rt)
  2999. {
  3000. wasm_memory_t *memory = NULL;
  3001. uint32 min_pages = 0, max_pages = 0;
  3002. bool init_flag = false;
  3003. bh_assert(singleton_engine);
  3004. if (!inst_comm_rt) {
  3005. return NULL;
  3006. }
  3007. if (!(memory = malloc_internal(sizeof(wasm_memory_t)))) {
  3008. goto failed;
  3009. }
  3010. memory->store = store;
  3011. memory->kind = WASM_EXTERN_MEMORY;
  3012. #if WASM_ENABLE_INTERP != 0
  3013. if (inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3014. WASMMemoryInstance *memory_interp =
  3015. ((WASMModuleInstance *)inst_comm_rt)->memories[memory_idx_rt];
  3016. min_pages = memory_interp->cur_page_count;
  3017. max_pages = memory_interp->max_page_count;
  3018. init_flag = true;
  3019. }
  3020. #endif
  3021. #if WASM_ENABLE_AOT != 0
  3022. if (inst_comm_rt->module_type == Wasm_Module_AoT) {
  3023. AOTModuleInstance *inst_aot = (AOTModuleInstance *)inst_comm_rt;
  3024. AOTModule *module_aot = (AOTModule *)(inst_aot->aot_module.ptr);
  3025. if (memory_idx_rt < module_aot->import_memory_count) {
  3026. min_pages = module_aot->import_memories->mem_init_page_count;
  3027. max_pages = module_aot->import_memories->mem_max_page_count;
  3028. }
  3029. else {
  3030. min_pages = module_aot->memories->mem_init_page_count;
  3031. max_pages = module_aot->memories->mem_max_page_count;
  3032. }
  3033. init_flag = true;
  3034. }
  3035. #endif
  3036. /*
  3037. * a wrong combination of module filetype and compilation flags
  3038. * leads to below branch
  3039. */
  3040. if (!init_flag) {
  3041. goto failed;
  3042. }
  3043. if (!(memory->type = wasm_memorytype_new_internal(min_pages, max_pages))) {
  3044. goto failed;
  3045. }
  3046. memory->inst_comm_rt = inst_comm_rt;
  3047. memory->memory_idx_rt = memory_idx_rt;
  3048. RETURN_OBJ(memory, wasm_memory_delete);
  3049. }
  3050. void
  3051. wasm_memory_delete(wasm_memory_t *memory)
  3052. {
  3053. if (!memory) {
  3054. return;
  3055. }
  3056. if (memory->type) {
  3057. wasm_memorytype_delete(memory->type);
  3058. memory->type = NULL;
  3059. }
  3060. DELETE_HOST_INFO(memory)
  3061. wasm_runtime_free(memory);
  3062. }
  3063. wasm_memorytype_t *
  3064. wasm_memory_type(const wasm_memory_t *memory)
  3065. {
  3066. if (!memory) {
  3067. return NULL;
  3068. }
  3069. return wasm_memorytype_copy(memory->type);
  3070. }
  3071. byte_t *
  3072. wasm_memory_data(wasm_memory_t *memory)
  3073. {
  3074. WASMModuleInstanceCommon *module_inst_comm = memory->inst_comm_rt;
  3075. #if WASM_ENABLE_INTERP != 0
  3076. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3077. WASMModuleInstance *module_inst =
  3078. (WASMModuleInstance *)module_inst_comm;
  3079. WASMMemoryInstance *memory_inst =
  3080. module_inst->memories[memory->memory_idx_rt];
  3081. return (byte_t *)memory_inst->memory_data;
  3082. }
  3083. #endif
  3084. #if WASM_ENABLE_AOT != 0
  3085. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3086. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3087. AOTMemoryInstance *memory_inst =
  3088. ((AOTMemoryInstance **)
  3089. module_inst->memories.ptr)[memory->memory_idx_rt];
  3090. return (byte_t *)memory_inst->memory_data.ptr;
  3091. }
  3092. #endif
  3093. /*
  3094. * a wrong combination of module filetype and compilation flags
  3095. * leads to below branch
  3096. */
  3097. return NULL;
  3098. }
  3099. size_t
  3100. wasm_memory_data_size(const wasm_memory_t *memory)
  3101. {
  3102. WASMModuleInstanceCommon *module_inst_comm = memory->inst_comm_rt;
  3103. #if WASM_ENABLE_INTERP != 0
  3104. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3105. WASMModuleInstance *module_inst =
  3106. (WASMModuleInstance *)module_inst_comm;
  3107. WASMMemoryInstance *memory_inst =
  3108. module_inst->memories[memory->memory_idx_rt];
  3109. return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
  3110. }
  3111. #endif
  3112. #if WASM_ENABLE_AOT != 0
  3113. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3114. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3115. AOTMemoryInstance *memory_inst =
  3116. ((AOTMemoryInstance **)
  3117. module_inst->memories.ptr)[memory->memory_idx_rt];
  3118. return memory_inst->cur_page_count * memory_inst->num_bytes_per_page;
  3119. }
  3120. #endif
  3121. /*
  3122. * a wrong combination of module filetype and compilation flags
  3123. * leads to below branch
  3124. */
  3125. return 0;
  3126. }
  3127. wasm_memory_pages_t
  3128. wasm_memory_size(const wasm_memory_t *memory)
  3129. {
  3130. WASMModuleInstanceCommon *module_inst_comm = memory->inst_comm_rt;
  3131. #if WASM_ENABLE_INTERP != 0
  3132. if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
  3133. WASMModuleInstance *module_inst =
  3134. (WASMModuleInstance *)module_inst_comm;
  3135. WASMMemoryInstance *memory_inst =
  3136. module_inst->memories[memory->memory_idx_rt];
  3137. return memory_inst->cur_page_count;
  3138. }
  3139. #endif
  3140. #if WASM_ENABLE_AOT != 0
  3141. if (module_inst_comm->module_type == Wasm_Module_AoT) {
  3142. AOTModuleInstance *module_inst = (AOTModuleInstance *)module_inst_comm;
  3143. AOTMemoryInstance *memory_inst =
  3144. ((AOTMemoryInstance **)
  3145. module_inst->memories.ptr)[memory->memory_idx_rt];
  3146. return memory_inst->cur_page_count;
  3147. }
  3148. #endif
  3149. /*
  3150. * a wrong combination of module filetype and compilation flags
  3151. * leads to below branch
  3152. */
  3153. return 0;
  3154. }
  3155. bool
  3156. wasm_memory_grow(wasm_memory_t *memory, wasm_memory_pages_t delta)
  3157. {
  3158. (void)memory;
  3159. (void)delta;
  3160. LOG_WARNING("Calling wasm_memory_grow() by host is not supported."
  3161. "Only allow growing a memory via the opcode memory.grow");
  3162. return false;
  3163. }
  3164. #if WASM_ENABLE_INTERP != 0
  3165. static bool
  3166. interp_link_func(const wasm_instance_t *inst, const WASMModule *module_interp,
  3167. uint16 func_idx_rt, wasm_func_t *import)
  3168. {
  3169. WASMImport *imported_func_interp = NULL;
  3170. wasm_func_t *cloned = NULL;
  3171. bh_assert(inst && module_interp && import);
  3172. bh_assert(func_idx_rt < module_interp->import_function_count);
  3173. bh_assert(WASM_EXTERN_FUNC == import->kind);
  3174. imported_func_interp = module_interp->import_functions + func_idx_rt;
  3175. bh_assert(imported_func_interp);
  3176. if (!(cloned = wasm_func_copy(import))) {
  3177. return false;
  3178. }
  3179. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  3180. wasm_func_delete(cloned);
  3181. return false;
  3182. }
  3183. imported_func_interp->u.function.call_conv_wasm_c_api = true;
  3184. imported_func_interp->u.function.wasm_c_api_with_env = import->with_env;
  3185. if (import->with_env) {
  3186. imported_func_interp->u.function.func_ptr_linked = import->u.cb_env.cb;
  3187. imported_func_interp->u.function.attachment = import->u.cb_env.env;
  3188. }
  3189. else {
  3190. imported_func_interp->u.function.func_ptr_linked = import->u.cb;
  3191. imported_func_interp->u.function.attachment = NULL;
  3192. }
  3193. import->func_idx_rt = func_idx_rt;
  3194. return true;
  3195. }
  3196. static bool
  3197. interp_link_global(const WASMModule *module_interp, uint16 global_idx_rt,
  3198. wasm_global_t *import)
  3199. {
  3200. WASMImport *imported_global_interp = NULL;
  3201. bh_assert(module_interp && import);
  3202. bh_assert(global_idx_rt < module_interp->import_global_count);
  3203. bh_assert(WASM_EXTERN_GLOBAL == import->kind);
  3204. imported_global_interp = module_interp->import_globals + global_idx_rt;
  3205. bh_assert(imported_global_interp);
  3206. /* set init value */
  3207. switch (wasm_valtype_kind(import->type->val_type)) {
  3208. case WASM_I32:
  3209. bh_assert(VALUE_TYPE_I32 == imported_global_interp->u.global.type);
  3210. imported_global_interp->u.global.global_data_linked.i32 =
  3211. import->init->of.i32;
  3212. break;
  3213. case WASM_I64:
  3214. bh_assert(VALUE_TYPE_I64 == imported_global_interp->u.global.type);
  3215. imported_global_interp->u.global.global_data_linked.i64 =
  3216. import->init->of.i64;
  3217. break;
  3218. case WASM_F32:
  3219. bh_assert(VALUE_TYPE_F32 == imported_global_interp->u.global.type);
  3220. imported_global_interp->u.global.global_data_linked.f32 =
  3221. import->init->of.f32;
  3222. break;
  3223. case WASM_F64:
  3224. bh_assert(VALUE_TYPE_F64 == imported_global_interp->u.global.type);
  3225. imported_global_interp->u.global.global_data_linked.f64 =
  3226. import->init->of.f64;
  3227. break;
  3228. default:
  3229. return false;
  3230. }
  3231. import->global_idx_rt = global_idx_rt;
  3232. imported_global_interp->u.global.is_linked = true;
  3233. return true;
  3234. }
  3235. static uint32
  3236. interp_link(const wasm_instance_t *inst, const WASMModule *module_interp,
  3237. wasm_extern_t *imports[])
  3238. {
  3239. uint32 i = 0;
  3240. uint32 import_func_i = 0;
  3241. uint32 import_global_i = 0;
  3242. bh_assert(inst && module_interp && imports);
  3243. for (i = 0; i < module_interp->import_count; ++i) {
  3244. wasm_extern_t *import = imports[i];
  3245. WASMImport *import_rt = module_interp->imports + i;
  3246. switch (import_rt->kind) {
  3247. case IMPORT_KIND_FUNC:
  3248. {
  3249. if (!interp_link_func(inst, module_interp, import_func_i++,
  3250. wasm_extern_as_func(import))) {
  3251. goto failed;
  3252. }
  3253. break;
  3254. }
  3255. case IMPORT_KIND_GLOBAL:
  3256. {
  3257. if (!interp_link_global(module_interp, import_global_i++,
  3258. wasm_extern_as_global(import))) {
  3259. goto failed;
  3260. }
  3261. break;
  3262. }
  3263. case IMPORT_KIND_MEMORY:
  3264. case IMPORT_KIND_TABLE:
  3265. ASSERT_NOT_IMPLEMENTED();
  3266. break;
  3267. default:
  3268. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3269. import_rt->kind);
  3270. goto failed;
  3271. }
  3272. }
  3273. return i;
  3274. failed:
  3275. LOG_DEBUG("%s failed", __FUNCTION__);
  3276. return (uint32)-1;
  3277. }
  3278. static bool
  3279. interp_process_export(wasm_store_t *store,
  3280. const WASMModuleInstance *inst_interp,
  3281. wasm_extern_vec_t *externals)
  3282. {
  3283. WASMExport *exports = NULL;
  3284. WASMExport *export = NULL;
  3285. wasm_extern_t *external = NULL;
  3286. uint32 export_cnt = 0;
  3287. uint32 i = 0;
  3288. bh_assert(store && inst_interp && inst_interp->module && externals);
  3289. exports = inst_interp->module->exports;
  3290. export_cnt = inst_interp->module->export_count;
  3291. for (i = 0; i < export_cnt; ++i) {
  3292. export = exports + i;
  3293. switch (export->kind) {
  3294. case EXPORT_KIND_FUNC:
  3295. {
  3296. wasm_func_t *func;
  3297. if (!(func = wasm_func_new_internal(
  3298. store, export->index,
  3299. (WASMModuleInstanceCommon *)inst_interp))) {
  3300. goto failed;
  3301. }
  3302. external = wasm_func_as_extern(func);
  3303. break;
  3304. }
  3305. case EXPORT_KIND_GLOBAL:
  3306. {
  3307. wasm_global_t *global;
  3308. if (!(global = wasm_global_new_internal(
  3309. store, export->index,
  3310. (WASMModuleInstanceCommon *)inst_interp))) {
  3311. goto failed;
  3312. }
  3313. external = wasm_global_as_extern(global);
  3314. break;
  3315. }
  3316. case EXPORT_KIND_TABLE:
  3317. {
  3318. wasm_table_t *table;
  3319. if (!(table = wasm_table_new_internal(
  3320. store, export->index,
  3321. (WASMModuleInstanceCommon *)inst_interp))) {
  3322. goto failed;
  3323. }
  3324. external = wasm_table_as_extern(table);
  3325. break;
  3326. }
  3327. case EXPORT_KIND_MEMORY:
  3328. {
  3329. wasm_memory_t *memory;
  3330. if (!(memory = wasm_memory_new_internal(
  3331. store, export->index,
  3332. (WASMModuleInstanceCommon *)inst_interp))) {
  3333. goto failed;
  3334. }
  3335. external = wasm_memory_as_extern(memory);
  3336. break;
  3337. }
  3338. default:
  3339. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3340. export->kind);
  3341. goto failed;
  3342. }
  3343. if (!bh_vector_append((Vector *)externals, &external)) {
  3344. goto failed;
  3345. }
  3346. }
  3347. return true;
  3348. failed:
  3349. wasm_extern_delete(external);
  3350. return false;
  3351. }
  3352. #endif /* WASM_ENABLE_INTERP */
  3353. #if WASM_ENABLE_AOT != 0
  3354. static bool
  3355. aot_link_func(const wasm_instance_t *inst, const AOTModule *module_aot,
  3356. uint32 import_func_idx_rt, wasm_func_t *import)
  3357. {
  3358. AOTImportFunc *import_aot_func = NULL;
  3359. wasm_func_t *cloned = NULL;
  3360. bh_assert(inst && module_aot && import);
  3361. import_aot_func = module_aot->import_funcs + import_func_idx_rt;
  3362. bh_assert(import_aot_func);
  3363. if (!(cloned = wasm_func_copy(import))) {
  3364. return false;
  3365. }
  3366. if (!bh_vector_append((Vector *)inst->imports, &cloned)) {
  3367. wasm_func_delete(cloned);
  3368. return false;
  3369. }
  3370. import_aot_func->call_conv_wasm_c_api = true;
  3371. import_aot_func->wasm_c_api_with_env = import->with_env;
  3372. if (import->with_env) {
  3373. import_aot_func->func_ptr_linked = import->u.cb_env.cb;
  3374. import_aot_func->attachment = import->u.cb_env.env;
  3375. }
  3376. else
  3377. import_aot_func->func_ptr_linked = import->u.cb;
  3378. import->func_idx_rt = import_func_idx_rt;
  3379. return true;
  3380. }
  3381. static bool
  3382. aot_link_global(const AOTModule *module_aot, uint16 global_idx_rt,
  3383. wasm_global_t *import)
  3384. {
  3385. AOTImportGlobal *import_aot_global = NULL;
  3386. const wasm_valtype_t *val_type = NULL;
  3387. bh_assert(module_aot && import);
  3388. import_aot_global = module_aot->import_globals + global_idx_rt;
  3389. bh_assert(import_aot_global);
  3390. val_type = wasm_globaltype_content(import->type);
  3391. bh_assert(val_type);
  3392. switch (wasm_valtype_kind(val_type)) {
  3393. case WASM_I32:
  3394. bh_assert(VALUE_TYPE_I32 == import_aot_global->type);
  3395. import_aot_global->global_data_linked.i32 = import->init->of.i32;
  3396. break;
  3397. case WASM_I64:
  3398. bh_assert(VALUE_TYPE_I64 == import_aot_global->type);
  3399. import_aot_global->global_data_linked.i64 = import->init->of.i64;
  3400. break;
  3401. case WASM_F32:
  3402. bh_assert(VALUE_TYPE_F32 == import_aot_global->type);
  3403. import_aot_global->global_data_linked.f32 = import->init->of.f32;
  3404. break;
  3405. case WASM_F64:
  3406. bh_assert(VALUE_TYPE_F64 == import_aot_global->type);
  3407. import_aot_global->global_data_linked.f64 = import->init->of.f64;
  3408. break;
  3409. default:
  3410. goto failed;
  3411. }
  3412. import->global_idx_rt = global_idx_rt;
  3413. return true;
  3414. failed:
  3415. LOG_DEBUG("%s failed", __FUNCTION__);
  3416. return false;
  3417. }
  3418. static uint32
  3419. aot_link(const wasm_instance_t *inst, const AOTModule *module_aot,
  3420. wasm_extern_t *imports[])
  3421. {
  3422. uint32 i = 0;
  3423. uint32 import_func_i = 0;
  3424. uint32 import_global_i = 0;
  3425. wasm_extern_t *import = NULL;
  3426. wasm_func_t *func = NULL;
  3427. wasm_global_t *global = NULL;
  3428. bh_assert(inst && module_aot && imports);
  3429. while (import_func_i < module_aot->import_func_count
  3430. || import_global_i < module_aot->import_global_count) {
  3431. import = imports[i++];
  3432. bh_assert(import);
  3433. switch (wasm_extern_kind(import)) {
  3434. case WASM_EXTERN_FUNC:
  3435. bh_assert(import_func_i < module_aot->import_func_count);
  3436. func = wasm_extern_as_func((wasm_extern_t *)import);
  3437. if (!aot_link_func(inst, module_aot, import_func_i++, func)) {
  3438. goto failed;
  3439. }
  3440. break;
  3441. case WASM_EXTERN_GLOBAL:
  3442. bh_assert(import_global_i < module_aot->import_global_count);
  3443. global = wasm_extern_as_global((wasm_extern_t *)import);
  3444. if (!aot_link_global(module_aot, import_global_i++, global)) {
  3445. goto failed;
  3446. }
  3447. break;
  3448. case WASM_EXTERN_MEMORY:
  3449. case WASM_EXTERN_TABLE:
  3450. ASSERT_NOT_IMPLEMENTED();
  3451. break;
  3452. default:
  3453. goto failed;
  3454. }
  3455. }
  3456. return i;
  3457. failed:
  3458. LOG_DEBUG("%s failed", __FUNCTION__);
  3459. return (uint32)-1;
  3460. }
  3461. static bool
  3462. aot_process_export(wasm_store_t *store, const AOTModuleInstance *inst_aot,
  3463. wasm_extern_vec_t *externals)
  3464. {
  3465. uint32 i;
  3466. wasm_extern_t *external = NULL;
  3467. AOTModule *module_aot = NULL;
  3468. bh_assert(store && inst_aot && externals);
  3469. module_aot = (AOTModule *)inst_aot->aot_module.ptr;
  3470. bh_assert(module_aot);
  3471. for (i = 0; i < module_aot->export_count; ++i) {
  3472. AOTExport *export = module_aot->exports + i;
  3473. switch (export->kind) {
  3474. case EXPORT_KIND_FUNC:
  3475. {
  3476. wasm_func_t *func = NULL;
  3477. if (!(func = wasm_func_new_internal(
  3478. store, export->index,
  3479. (WASMModuleInstanceCommon *)inst_aot))) {
  3480. goto failed;
  3481. }
  3482. external = wasm_func_as_extern(func);
  3483. break;
  3484. }
  3485. case EXPORT_KIND_GLOBAL:
  3486. {
  3487. wasm_global_t *global = NULL;
  3488. if (!(global = wasm_global_new_internal(
  3489. store, export->index,
  3490. (WASMModuleInstanceCommon *)inst_aot))) {
  3491. goto failed;
  3492. }
  3493. external = wasm_global_as_extern(global);
  3494. break;
  3495. }
  3496. case EXPORT_KIND_TABLE:
  3497. {
  3498. wasm_table_t *table;
  3499. if (!(table = wasm_table_new_internal(
  3500. store, export->index,
  3501. (WASMModuleInstanceCommon *)inst_aot))) {
  3502. goto failed;
  3503. }
  3504. external = wasm_table_as_extern(table);
  3505. break;
  3506. }
  3507. case EXPORT_KIND_MEMORY:
  3508. {
  3509. wasm_memory_t *memory;
  3510. if (!(memory = wasm_memory_new_internal(
  3511. store, export->index,
  3512. (WASMModuleInstanceCommon *)inst_aot))) {
  3513. goto failed;
  3514. }
  3515. external = wasm_memory_as_extern(memory);
  3516. break;
  3517. }
  3518. default:
  3519. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3520. export->kind);
  3521. goto failed;
  3522. }
  3523. if (!(external->name = malloc_internal(sizeof(wasm_byte_vec_t)))) {
  3524. goto failed;
  3525. }
  3526. wasm_name_new_from_string(external->name, export->name);
  3527. if (strlen(export->name) && !external->name->data) {
  3528. goto failed;
  3529. }
  3530. if (!bh_vector_append((Vector *)externals, &external)) {
  3531. goto failed;
  3532. }
  3533. }
  3534. return true;
  3535. failed:
  3536. wasm_extern_delete(external);
  3537. return false;
  3538. }
  3539. #endif /* WASM_ENABLE_AOT */
  3540. wasm_instance_t *
  3541. wasm_instance_new(wasm_store_t *store, const wasm_module_t *module,
  3542. const wasm_extern_vec_t *imports, own wasm_trap_t **traps)
  3543. {
  3544. return wasm_instance_new_with_args(store, module, imports, traps,
  3545. KILOBYTE(32), KILOBYTE(32));
  3546. }
  3547. wasm_instance_t *
  3548. wasm_instance_new_with_args(wasm_store_t *store, const wasm_module_t *module,
  3549. const wasm_extern_vec_t *imports,
  3550. own wasm_trap_t **traps, const uint32 stack_size,
  3551. const uint32 heap_size)
  3552. {
  3553. char error_buf[128] = { 0 };
  3554. uint32 import_count = 0;
  3555. bool import_count_verified = false;
  3556. wasm_instance_t *instance = NULL;
  3557. uint32 i = 0;
  3558. bool processed = false;
  3559. (void)traps;
  3560. bh_assert(singleton_engine);
  3561. if (!module) {
  3562. return NULL;
  3563. }
  3564. instance = malloc_internal(sizeof(wasm_instance_t));
  3565. if (!instance) {
  3566. goto failed;
  3567. }
  3568. /* link module and imports */
  3569. if (imports) {
  3570. #if WASM_ENABLE_INTERP != 0
  3571. if ((*module)->module_type == Wasm_Module_Bytecode) {
  3572. import_count = MODULE_INTERP(module)->import_count;
  3573. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3574. import_count);
  3575. if (import_count) {
  3576. uint32 actual_link_import_count =
  3577. interp_link(instance, MODULE_INTERP(module),
  3578. (wasm_extern_t **)imports->data);
  3579. /* make sure a complete import list */
  3580. if ((int32)import_count < 0
  3581. || import_count != actual_link_import_count) {
  3582. goto failed;
  3583. }
  3584. }
  3585. import_count_verified = true;
  3586. }
  3587. #endif
  3588. #if WASM_ENABLE_AOT != 0
  3589. if ((*module)->module_type == Wasm_Module_AoT) {
  3590. import_count = MODULE_AOT(module)->import_func_count
  3591. + MODULE_AOT(module)->import_global_count
  3592. + MODULE_AOT(module)->import_memory_count
  3593. + MODULE_AOT(module)->import_table_count;
  3594. INIT_VEC(instance->imports, wasm_extern_vec_new_uninitialized,
  3595. import_count);
  3596. if (import_count) {
  3597. import_count = aot_link(instance, MODULE_AOT(module),
  3598. (wasm_extern_t **)imports->data);
  3599. if ((int32)import_count < 0) {
  3600. goto failed;
  3601. }
  3602. }
  3603. import_count_verified = true;
  3604. }
  3605. #endif
  3606. /*
  3607. * a wrong combination of module filetype and compilation flags
  3608. * also leads to below branch
  3609. */
  3610. if (!import_count_verified) {
  3611. goto failed;
  3612. }
  3613. }
  3614. instance->inst_comm_rt = wasm_runtime_instantiate(
  3615. *module, stack_size, heap_size, error_buf, sizeof(error_buf));
  3616. if (!instance->inst_comm_rt) {
  3617. LOG_ERROR(error_buf);
  3618. goto failed;
  3619. }
  3620. if (!wasm_runtime_create_exec_env_singleton(instance->inst_comm_rt)) {
  3621. goto failed;
  3622. }
  3623. /* fill with inst */
  3624. for (i = 0; imports && imports->data && i < (uint32)import_count; ++i) {
  3625. wasm_extern_t *import = imports->data[i];
  3626. switch (import->kind) {
  3627. case WASM_EXTERN_FUNC:
  3628. wasm_extern_as_func(import)->inst_comm_rt =
  3629. instance->inst_comm_rt;
  3630. break;
  3631. case WASM_EXTERN_GLOBAL:
  3632. wasm_extern_as_global(import)->inst_comm_rt =
  3633. instance->inst_comm_rt;
  3634. break;
  3635. case WASM_EXTERN_MEMORY:
  3636. wasm_extern_as_memory(import)->inst_comm_rt =
  3637. instance->inst_comm_rt;
  3638. break;
  3639. case WASM_EXTERN_TABLE:
  3640. wasm_extern_as_table(import)->inst_comm_rt =
  3641. instance->inst_comm_rt;
  3642. break;
  3643. default:
  3644. goto failed;
  3645. }
  3646. }
  3647. /* build the exports list */
  3648. #if WASM_ENABLE_INTERP != 0
  3649. if (instance->inst_comm_rt->module_type == Wasm_Module_Bytecode) {
  3650. uint32 export_cnt = ((WASMModuleInstance *)instance->inst_comm_rt)
  3651. ->module->export_count;
  3652. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3653. export_cnt);
  3654. if (!interp_process_export(store,
  3655. (WASMModuleInstance *)instance->inst_comm_rt,
  3656. instance->exports)) {
  3657. goto failed;
  3658. }
  3659. processed = true;
  3660. }
  3661. #endif
  3662. #if WASM_ENABLE_AOT != 0
  3663. if (instance->inst_comm_rt->module_type == Wasm_Module_AoT) {
  3664. uint32 export_cnt =
  3665. ((AOTModuleInstance *)instance->inst_comm_rt)->export_func_count
  3666. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_global_count
  3667. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_tab_count
  3668. + ((AOTModuleInstance *)instance->inst_comm_rt)->export_mem_count;
  3669. INIT_VEC(instance->exports, wasm_extern_vec_new_uninitialized,
  3670. export_cnt);
  3671. if (!aot_process_export(store,
  3672. (AOTModuleInstance *)instance->inst_comm_rt,
  3673. instance->exports)) {
  3674. goto failed;
  3675. }
  3676. processed = true;
  3677. }
  3678. #endif
  3679. /*
  3680. * a wrong combination of module filetype and compilation flags
  3681. * leads to below branch
  3682. */
  3683. if (!processed) {
  3684. goto failed;
  3685. }
  3686. /* add it to a watching list in store */
  3687. if (!bh_vector_append((Vector *)store->instances, &instance)) {
  3688. goto failed;
  3689. }
  3690. return instance;
  3691. failed:
  3692. LOG_DEBUG("%s failed", __FUNCTION__);
  3693. wasm_instance_delete_internal(instance);
  3694. return NULL;
  3695. }
  3696. static void
  3697. wasm_instance_delete_internal(wasm_instance_t *instance)
  3698. {
  3699. if (!instance) {
  3700. return;
  3701. }
  3702. DEINIT_VEC(instance->imports, wasm_extern_vec_delete);
  3703. DEINIT_VEC(instance->exports, wasm_extern_vec_delete);
  3704. if (instance->inst_comm_rt) {
  3705. wasm_runtime_deinstantiate(instance->inst_comm_rt);
  3706. instance->inst_comm_rt = NULL;
  3707. }
  3708. wasm_runtime_free(instance);
  3709. }
  3710. void
  3711. wasm_instance_delete(wasm_instance_t *inst)
  3712. {
  3713. DELETE_HOST_INFO(inst)
  3714. /* will release instance when releasing the store */
  3715. }
  3716. void
  3717. wasm_instance_exports(const wasm_instance_t *instance,
  3718. own wasm_extern_vec_t *out)
  3719. {
  3720. if (!instance || !out) {
  3721. return;
  3722. }
  3723. wasm_extern_vec_copy(out, instance->exports);
  3724. }
  3725. wasm_extern_t *
  3726. wasm_extern_copy(const wasm_extern_t *src)
  3727. {
  3728. wasm_extern_t *dst = NULL;
  3729. if (!src) {
  3730. return NULL;
  3731. }
  3732. switch (wasm_extern_kind(src)) {
  3733. case WASM_EXTERN_FUNC:
  3734. dst = wasm_func_as_extern(
  3735. wasm_func_copy(wasm_extern_as_func_const(src)));
  3736. break;
  3737. case WASM_EXTERN_GLOBAL:
  3738. dst = wasm_global_as_extern(
  3739. wasm_global_copy(wasm_extern_as_global_const(src)));
  3740. break;
  3741. case WASM_EXTERN_MEMORY:
  3742. dst = wasm_memory_as_extern(
  3743. wasm_memory_copy(wasm_extern_as_memory_const(src)));
  3744. break;
  3745. case WASM_EXTERN_TABLE:
  3746. dst = wasm_table_as_extern(
  3747. wasm_table_copy(wasm_extern_as_table_const(src)));
  3748. break;
  3749. default:
  3750. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3751. src->kind);
  3752. break;
  3753. }
  3754. if (!dst) {
  3755. goto failed;
  3756. }
  3757. return dst;
  3758. failed:
  3759. LOG_DEBUG("%s failed", __FUNCTION__);
  3760. wasm_extern_delete(dst);
  3761. return NULL;
  3762. }
  3763. void
  3764. wasm_extern_delete(wasm_extern_t *external)
  3765. {
  3766. if (!external) {
  3767. return;
  3768. }
  3769. if (external->name) {
  3770. wasm_byte_vec_delete(external->name);
  3771. wasm_runtime_free(external->name);
  3772. external->name = NULL;
  3773. }
  3774. switch (wasm_extern_kind(external)) {
  3775. case WASM_EXTERN_FUNC:
  3776. wasm_func_delete(wasm_extern_as_func(external));
  3777. break;
  3778. case WASM_EXTERN_GLOBAL:
  3779. wasm_global_delete(wasm_extern_as_global(external));
  3780. break;
  3781. case WASM_EXTERN_MEMORY:
  3782. wasm_memory_delete(wasm_extern_as_memory(external));
  3783. break;
  3784. case WASM_EXTERN_TABLE:
  3785. wasm_table_delete(wasm_extern_as_table(external));
  3786. break;
  3787. default:
  3788. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3789. external->kind);
  3790. break;
  3791. }
  3792. }
  3793. wasm_externkind_t
  3794. wasm_extern_kind(const wasm_extern_t *external)
  3795. {
  3796. if (!external) {
  3797. return WASM_ANYREF;
  3798. }
  3799. return external->kind;
  3800. }
  3801. own wasm_externtype_t *
  3802. wasm_extern_type(const wasm_extern_t *external)
  3803. {
  3804. if (!external) {
  3805. return NULL;
  3806. }
  3807. switch (wasm_extern_kind(external)) {
  3808. case WASM_EXTERN_FUNC:
  3809. return wasm_functype_as_externtype(
  3810. wasm_func_type(wasm_extern_as_func_const(external)));
  3811. case WASM_EXTERN_GLOBAL:
  3812. return wasm_globaltype_as_externtype(
  3813. wasm_global_type(wasm_extern_as_global_const(external)));
  3814. case WASM_EXTERN_MEMORY:
  3815. return wasm_memorytype_as_externtype(
  3816. wasm_memory_type(wasm_extern_as_memory_const(external)));
  3817. case WASM_EXTERN_TABLE:
  3818. return wasm_tabletype_as_externtype(
  3819. wasm_table_type(wasm_extern_as_table_const(external)));
  3820. default:
  3821. LOG_WARNING("%s meets unsupported kind: %d", __FUNCTION__,
  3822. external->kind);
  3823. break;
  3824. }
  3825. return NULL;
  3826. }
  3827. #define BASIC_FOUR_LIST(V) \
  3828. V(func) \
  3829. V(global) \
  3830. V(memory) \
  3831. V(table)
  3832. #define WASM_EXTERN_AS_OTHER(name) \
  3833. wasm_##name##_t *wasm_extern_as_##name(wasm_extern_t *external) \
  3834. { \
  3835. return (wasm_##name##_t *)external; \
  3836. }
  3837. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER)
  3838. #undef WASM_EXTERN_AS_OTHER
  3839. #define WASM_OTHER_AS_EXTERN(name) \
  3840. wasm_extern_t *wasm_##name##_as_extern(wasm_##name##_t *other) \
  3841. { \
  3842. return (wasm_extern_t *)other; \
  3843. }
  3844. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN)
  3845. #undef WASM_OTHER_AS_EXTERN
  3846. #define WASM_EXTERN_AS_OTHER_CONST(name) \
  3847. const wasm_##name##_t *wasm_extern_as_##name##_const( \
  3848. const wasm_extern_t *external) \
  3849. { \
  3850. return (const wasm_##name##_t *)external; \
  3851. }
  3852. BASIC_FOUR_LIST(WASM_EXTERN_AS_OTHER_CONST)
  3853. #undef WASM_EXTERN_AS_OTHER_CONST
  3854. #define WASM_OTHER_AS_EXTERN_CONST(name) \
  3855. const wasm_extern_t *wasm_##name##_as_extern_const( \
  3856. const wasm_##name##_t *other) \
  3857. { \
  3858. return (const wasm_extern_t *)other; \
  3859. }
  3860. BASIC_FOUR_LIST(WASM_OTHER_AS_EXTERN_CONST)
  3861. #undef WASM_OTHER_AS_EXTERN_CONST