wasm_c_api.c 153 KB

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