wasm_c_api.c 151 KB

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