wasm_c_api.c 148 KB

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