wasm_c_api.c 154 KB

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