wasm_c_api.c 152 KB

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