wasm_c_api.c 152 KB

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