wasm_c_api.c 153 KB

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