wasm_c_api.c 133 KB

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