wasm_interp_classic.c 284 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "wasm_memory.h"
  11. #include "../common/wasm_exec_env.h"
  12. #if WASM_ENABLE_GC != 0
  13. #include "../common/gc/gc_object.h"
  14. #include "mem_alloc.h"
  15. #if WASM_ENABLE_STRINGREF != 0
  16. #include "string_object.h"
  17. #endif
  18. #endif
  19. #if WASM_ENABLE_SHARED_MEMORY != 0
  20. #include "../common/wasm_shared_memory.h"
  21. #endif
  22. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  23. #include "../libraries/thread-mgr/thread_manager.h"
  24. #include "../libraries/debug-engine/debug_engine.h"
  25. #endif
  26. #if WASM_ENABLE_FAST_JIT != 0
  27. #include "../fast-jit/jit_compiler.h"
  28. #endif
  29. typedef int32 CellType_I32;
  30. typedef int64 CellType_I64;
  31. typedef float32 CellType_F32;
  32. typedef float64 CellType_F64;
  33. #define BR_TABLE_TMP_BUF_LEN 32
  34. #if WASM_ENABLE_THREAD_MGR == 0
  35. #define get_linear_mem_size() linear_mem_size
  36. #else
  37. /**
  38. * Load memory data size in each time boundary check in
  39. * multi-threading mode since it may be changed by other
  40. * threads in memory.grow
  41. */
  42. #define get_linear_mem_size() GET_LINEAR_MEMORY_SIZE(memory)
  43. #endif
  44. #if WASM_ENABLE_SHARED_HEAP != 0
  45. #if WASM_ENABLE_MULTI_MEMORY != 0
  46. /* Only enable shared heap for the default memory */
  47. #define is_default_memory (memidx == 0)
  48. #else
  49. #define is_default_memory true
  50. #endif
  51. #define app_addr_in_shared_heap(app_addr, bytes) \
  52. (shared_heap && is_default_memory && (app_addr) >= shared_heap_start_off \
  53. && (app_addr) <= shared_heap_end_off - bytes + 1)
  54. #define shared_heap_addr_app_to_native(app_addr, native_addr) \
  55. native_addr = shared_heap_base_addr + ((app_addr)-shared_heap_start_off)
  56. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr) \
  57. if (app_addr_in_shared_heap(app_addr, bytes)) \
  58. shared_heap_addr_app_to_native(app_addr, native_addr); \
  59. else
  60. #else
  61. #define CHECK_SHARED_HEAP_OVERFLOW(app_addr, bytes, native_addr)
  62. #endif
  63. #if WASM_ENABLE_MEMORY64 == 0
  64. #if (!defined(OS_ENABLE_HW_BOUND_CHECK) \
  65. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0)
  66. #define CHECK_MEMORY_OVERFLOW(bytes) \
  67. do { \
  68. uint64 offset1 = (uint64)offset + (uint64)addr; \
  69. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  70. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  71. /* If offset1 is in valid range, maddr must also \
  72. be in valid range, no need to check it again. */ \
  73. maddr = memory->memory_data + offset1; \
  74. else \
  75. goto out_of_bounds; \
  76. } while (0)
  77. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  78. do { \
  79. uint64 offset1 = (uint32)(start); \
  80. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  81. if (disable_bounds_checks || offset1 + bytes <= get_linear_mem_size()) \
  82. /* App heap space is not valid space for \
  83. bulk memory operation */ \
  84. maddr = memory->memory_data + offset1; \
  85. else \
  86. goto out_of_bounds; \
  87. } while (0)
  88. #else /* else of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  89. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  90. #define CHECK_MEMORY_OVERFLOW(bytes) \
  91. do { \
  92. uint64 offset1 = (uint64)offset + (uint64)addr; \
  93. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  94. maddr = memory->memory_data + offset1; \
  95. } while (0)
  96. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  97. do { \
  98. uint64 offset1 = (uint32)(start); \
  99. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  100. maddr = memory->memory_data + offset1; \
  101. } while (0)
  102. #endif /* end of !defined(OS_ENABLE_HW_BOUND_CHECK) || \
  103. WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
  104. #else /* else of WASM_ENABLE_MEMORY64 == 0 */
  105. #define CHECK_MEMORY_OVERFLOW(bytes) \
  106. do { \
  107. uint64 offset1 = (uint64)offset + (uint64)addr; \
  108. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  109. /* If memory64 is enabled, offset1, offset1 + bytes can overflow */ \
  110. if (disable_bounds_checks \
  111. || (offset1 >= offset && offset1 + bytes >= offset1 \
  112. && offset1 + bytes <= get_linear_mem_size())) \
  113. maddr = memory->memory_data + offset1; \
  114. else \
  115. goto out_of_bounds; \
  116. } while (0)
  117. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  118. do { \
  119. uint64 offset1 = (uint64)(start); \
  120. CHECK_SHARED_HEAP_OVERFLOW(offset1, bytes, maddr) \
  121. /* If memory64 is enabled, offset1 + bytes can overflow */ \
  122. if (disable_bounds_checks \
  123. || (offset1 + bytes >= offset1 \
  124. && offset1 + bytes <= get_linear_mem_size())) \
  125. /* App heap space is not valid space for \
  126. bulk memory operation */ \
  127. maddr = memory->memory_data + offset1; \
  128. else \
  129. goto out_of_bounds; \
  130. } while (0)
  131. #endif /* end of WASM_ENABLE_MEMORY64 == 0 */
  132. #define CHECK_ATOMIC_MEMORY_ACCESS() \
  133. do { \
  134. if (((uintptr_t)maddr & (((uintptr_t)1 << align) - 1)) != 0) \
  135. goto unaligned_atomic; \
  136. } while (0)
  137. #if WASM_ENABLE_DEBUG_INTERP != 0
  138. #define TRIGGER_WATCHPOINT_SIGTRAP() \
  139. do { \
  140. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP); \
  141. CHECK_SUSPEND_FLAGS(); \
  142. } while (0)
  143. #define CHECK_WATCHPOINT(list, current_addr) \
  144. do { \
  145. WASMDebugWatchPoint *watchpoint = bh_list_first_elem(list); \
  146. while (watchpoint) { \
  147. WASMDebugWatchPoint *next = bh_list_elem_next(watchpoint); \
  148. if (watchpoint->addr <= current_addr \
  149. && watchpoint->addr + watchpoint->length > current_addr) { \
  150. TRIGGER_WATCHPOINT_SIGTRAP(); \
  151. } \
  152. watchpoint = next; \
  153. } \
  154. } while (0)
  155. #define CHECK_READ_WATCHPOINT(addr, offset) \
  156. CHECK_WATCHPOINT(watch_point_list_read, WASM_ADDR_OFFSET(addr + offset))
  157. #define CHECK_WRITE_WATCHPOINT(addr, offset) \
  158. CHECK_WATCHPOINT(watch_point_list_write, WASM_ADDR_OFFSET(addr + offset))
  159. #else
  160. #define CHECK_READ_WATCHPOINT(addr, offset) (void)0
  161. #define CHECK_WRITE_WATCHPOINT(addr, offset) (void)0
  162. #endif
  163. static inline uint32
  164. rotl32(uint32 n, uint32 c)
  165. {
  166. const uint32 mask = (31);
  167. c = c % 32;
  168. c &= mask;
  169. return (n << c) | (n >> ((0 - c) & mask));
  170. }
  171. static inline uint32
  172. rotr32(uint32 n, uint32 c)
  173. {
  174. const uint32 mask = (31);
  175. c = c % 32;
  176. c &= mask;
  177. return (n >> c) | (n << ((0 - c) & mask));
  178. }
  179. static inline uint64
  180. rotl64(uint64 n, uint64 c)
  181. {
  182. const uint64 mask = (63);
  183. c = c % 64;
  184. c &= mask;
  185. return (n << c) | (n >> ((0 - c) & mask));
  186. }
  187. static inline uint64
  188. rotr64(uint64 n, uint64 c)
  189. {
  190. const uint64 mask = (63);
  191. c = c % 64;
  192. c &= mask;
  193. return (n >> c) | (n << ((0 - c) & mask));
  194. }
  195. static inline float32
  196. f32_min(float32 a, float32 b)
  197. {
  198. if (isnan(a) || isnan(b))
  199. return NAN;
  200. else if (a == 0 && a == b)
  201. return signbit(a) ? a : b;
  202. else
  203. return a > b ? b : a;
  204. }
  205. static inline float32
  206. f32_max(float32 a, float32 b)
  207. {
  208. if (isnan(a) || isnan(b))
  209. return NAN;
  210. else if (a == 0 && a == b)
  211. return signbit(a) ? b : a;
  212. else
  213. return a > b ? a : b;
  214. }
  215. static inline float64
  216. f64_min(float64 a, float64 b)
  217. {
  218. if (isnan(a) || isnan(b))
  219. return NAN;
  220. else if (a == 0 && a == b)
  221. return signbit(a) ? a : b;
  222. else
  223. return a > b ? b : a;
  224. }
  225. static inline float64
  226. f64_max(float64 a, float64 b)
  227. {
  228. if (isnan(a) || isnan(b))
  229. return NAN;
  230. else if (a == 0 && a == b)
  231. return signbit(a) ? b : a;
  232. else
  233. return a > b ? a : b;
  234. }
  235. static inline uint32
  236. clz32(uint32 type)
  237. {
  238. uint32 num = 0;
  239. if (type == 0)
  240. return 32;
  241. while (!(type & 0x80000000)) {
  242. num++;
  243. type <<= 1;
  244. }
  245. return num;
  246. }
  247. static inline uint32
  248. clz64(uint64 type)
  249. {
  250. uint32 num = 0;
  251. if (type == 0)
  252. return 64;
  253. while (!(type & 0x8000000000000000LL)) {
  254. num++;
  255. type <<= 1;
  256. }
  257. return num;
  258. }
  259. static inline uint32
  260. ctz32(uint32 type)
  261. {
  262. uint32 num = 0;
  263. if (type == 0)
  264. return 32;
  265. while (!(type & 1)) {
  266. num++;
  267. type >>= 1;
  268. }
  269. return num;
  270. }
  271. static inline uint32
  272. ctz64(uint64 type)
  273. {
  274. uint32 num = 0;
  275. if (type == 0)
  276. return 64;
  277. while (!(type & 1)) {
  278. num++;
  279. type >>= 1;
  280. }
  281. return num;
  282. }
  283. static inline uint32
  284. popcount32(uint32 u)
  285. {
  286. uint32 ret = 0;
  287. while (u) {
  288. u = (u & (u - 1));
  289. ret++;
  290. }
  291. return ret;
  292. }
  293. static inline uint32
  294. popcount64(uint64 u)
  295. {
  296. uint32 ret = 0;
  297. while (u) {
  298. u = (u & (u - 1));
  299. ret++;
  300. }
  301. return ret;
  302. }
  303. static float
  304. local_copysignf(float x, float y)
  305. {
  306. union {
  307. float f;
  308. uint32 i;
  309. } ux = { x }, uy = { y };
  310. ux.i &= 0x7fffffff;
  311. ux.i |= uy.i & 0x80000000;
  312. return ux.f;
  313. }
  314. static double
  315. local_copysign(double x, double y)
  316. {
  317. union {
  318. double f;
  319. uint64 i;
  320. } ux = { x }, uy = { y };
  321. ux.i &= UINT64_MAX / 2;
  322. ux.i |= uy.i & 1ULL << 63;
  323. return ux.f;
  324. }
  325. static uint64
  326. read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
  327. {
  328. uint64 result = 0, byte;
  329. uint32 offset = *p_offset;
  330. uint32 shift = 0;
  331. while (true) {
  332. byte = buf[offset++];
  333. result |= ((byte & 0x7f) << shift);
  334. shift += 7;
  335. if ((byte & 0x80) == 0) {
  336. break;
  337. }
  338. }
  339. if (sign && (shift < maxbits) && (byte & 0x40)) {
  340. /* Sign extend */
  341. result |= (~((uint64)0)) << shift;
  342. }
  343. *p_offset = offset;
  344. return result;
  345. }
  346. #if WASM_ENABLE_GC != 0
  347. static uint8 *
  348. get_frame_ref(WASMInterpFrame *frame)
  349. {
  350. WASMFunctionInstance *cur_func = frame->function;
  351. uint32 all_cell_num;
  352. if (!cur_func) {
  353. /* it's a glue frame created in wasm_interp_call_wasm,
  354. no GC object will be traversed */
  355. return (uint8 *)frame->lp;
  356. }
  357. else if (!frame->ip) {
  358. /* it's a native method frame created in
  359. wasm_interp_call_func_native */
  360. all_cell_num =
  361. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  362. return (uint8 *)(frame->lp + all_cell_num);
  363. }
  364. else {
  365. #if WASM_ENABLE_JIT == 0
  366. /* it's a wasm bytecode function frame */
  367. return (uint8 *)frame->csp_boundary;
  368. #else
  369. return (uint8 *)(frame->lp + cur_func->param_cell_num
  370. + cur_func->local_cell_num
  371. + cur_func->u.func->max_stack_cell_num);
  372. #endif
  373. }
  374. }
  375. static void
  376. init_frame_refs(uint8 *frame_ref, uint32 cell_num, WASMFunctionInstance *func)
  377. {
  378. uint32 i, j;
  379. memset(frame_ref, 0, cell_num);
  380. for (i = 0, j = 0; i < func->param_count; i++) {
  381. if (wasm_is_type_reftype(func->param_types[i])
  382. && !wasm_is_reftype_i31ref(func->param_types[i])) {
  383. frame_ref[j++] = 1;
  384. #if UINTPTR_MAX == UINT64_MAX
  385. frame_ref[j++] = 1;
  386. #endif
  387. }
  388. else {
  389. j += wasm_value_type_cell_num(func->param_types[i]);
  390. }
  391. }
  392. for (i = 0; i < func->local_count; i++) {
  393. if (wasm_is_type_reftype(func->local_types[i])
  394. && !wasm_is_reftype_i31ref(func->local_types[i])) {
  395. frame_ref[j++] = 1;
  396. #if UINTPTR_MAX == UINT64_MAX
  397. frame_ref[j++] = 1;
  398. #endif
  399. }
  400. else {
  401. j += wasm_value_type_cell_num(func->local_types[i]);
  402. }
  403. }
  404. }
  405. uint8 *
  406. wasm_interp_get_frame_ref(WASMInterpFrame *frame)
  407. {
  408. return get_frame_ref(frame);
  409. }
  410. /* Return the corresponding ref slot of the given address of local
  411. variable or stack pointer. */
  412. #define COMPUTE_FRAME_REF(ref, lp, p) (ref + (unsigned)((uint32 *)p - lp))
  413. #define FRAME_REF(p) COMPUTE_FRAME_REF(frame_ref, frame_lp, p)
  414. #define FRAME_REF_FOR(frame, p) \
  415. COMPUTE_FRAME_REF(get_frame_ref(frame), frame->lp, p)
  416. #define CLEAR_FRAME_REF(p, n) \
  417. do { \
  418. int32 ref_i, ref_n = (int32)(n); \
  419. uint8 *ref = FRAME_REF(p); \
  420. for (ref_i = 0; ref_i < ref_n; ref_i++) \
  421. ref[ref_i] = 0; \
  422. } while (0)
  423. #else
  424. #define CLEAR_FRAME_REF(p, n) (void)0
  425. #endif /* end of WASM_ENABLE_GC != 0 */
  426. #define skip_leb(p) while (*p++ & 0x80)
  427. #define PUSH_I32(value) \
  428. do { \
  429. *(int32 *)frame_sp++ = (int32)(value); \
  430. } while (0)
  431. #define PUSH_F32(value) \
  432. do { \
  433. *(float32 *)frame_sp++ = (float32)(value); \
  434. } while (0)
  435. #define PUSH_I64(value) \
  436. do { \
  437. PUT_I64_TO_ADDR(frame_sp, value); \
  438. frame_sp += 2; \
  439. } while (0)
  440. #define PUSH_F64(value) \
  441. do { \
  442. PUT_F64_TO_ADDR(frame_sp, value); \
  443. frame_sp += 2; \
  444. } while (0)
  445. #if UINTPTR_MAX == UINT64_MAX
  446. #define PUSH_REF(value) \
  447. do { \
  448. PUT_REF_TO_ADDR(frame_sp, value); \
  449. frame_ref_tmp = FRAME_REF(frame_sp); \
  450. *frame_ref_tmp = *(frame_ref_tmp + 1) = 1; \
  451. frame_sp += 2; \
  452. } while (0)
  453. #define PUSH_I31REF(value) \
  454. do { \
  455. PUT_REF_TO_ADDR(frame_sp, value); \
  456. frame_sp += 2; \
  457. } while (0)
  458. #else
  459. #define PUSH_REF(value) \
  460. do { \
  461. PUT_REF_TO_ADDR(frame_sp, value); \
  462. frame_ref_tmp = FRAME_REF(frame_sp); \
  463. *frame_ref_tmp = 1; \
  464. frame_sp++; \
  465. } while (0)
  466. #define PUSH_I31REF(value) \
  467. do { \
  468. PUT_REF_TO_ADDR(frame_sp, value); \
  469. frame_sp++; \
  470. } while (0)
  471. #endif
  472. #if UINTPTR_MAX == UINT64_MAX
  473. #define PUSH_PTR(value) PUSH_I64(value)
  474. #else
  475. #define PUSH_PTR(value) PUSH_I32(value)
  476. #endif
  477. /* in exception handling, label_type needs to be stored to lookup exception
  478. * handlers */
  479. #if WASM_ENABLE_EXCE_HANDLING != 0
  480. #define SET_LABEL_TYPE(_label_type) frame_csp->label_type = _label_type
  481. #else
  482. #define SET_LABEL_TYPE(_label_type) (void)0
  483. #endif
  484. #if WASM_ENABLE_MEMORY64 != 0
  485. #define COND_PUSH_TEMPLATE(cond, value) \
  486. do { \
  487. if (cond) { \
  488. PUT_I64_TO_ADDR(frame_sp, value); \
  489. frame_sp += 2; \
  490. } \
  491. else { \
  492. *(int32 *)frame_sp++ = (int32)(value); \
  493. } \
  494. } while (0)
  495. #define PUSH_MEM_OFFSET(value) COND_PUSH_TEMPLATE(is_memory64, value)
  496. #define PUSH_TBL_ELEM_IDX(value) COND_PUSH_TEMPLATE(is_table64, value)
  497. #else
  498. #define PUSH_MEM_OFFSET(value) PUSH_I32(value)
  499. #define PUSH_TBL_ELEM_IDX(value) PUSH_I32(value)
  500. #endif
  501. #define PUSH_PAGE_COUNT(value) PUSH_MEM_OFFSET(value)
  502. #define PUSH_CSP(_label_type, param_cell_num, cell_num, _target_addr) \
  503. do { \
  504. bh_assert(frame_csp < frame->csp_boundary); \
  505. SET_LABEL_TYPE(_label_type); \
  506. frame_csp->cell_num = cell_num; \
  507. frame_csp->begin_addr = frame_ip; \
  508. frame_csp->target_addr = _target_addr; \
  509. frame_csp->frame_sp = frame_sp - param_cell_num; \
  510. frame_csp++; \
  511. } while (0)
  512. #define POP_I32() (--frame_sp, *(int32 *)frame_sp)
  513. #define POP_F32() (--frame_sp, *(float32 *)frame_sp)
  514. #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp))
  515. #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp))
  516. #if UINTPTR_MAX == UINT64_MAX
  517. #define POP_REF() \
  518. (frame_sp -= 2, frame_ref_tmp = FRAME_REF(frame_sp), \
  519. *frame_ref_tmp = *(frame_ref_tmp + 1) = 0, GET_REF_FROM_ADDR(frame_sp))
  520. #else
  521. #define POP_REF() \
  522. (frame_sp--, frame_ref_tmp = FRAME_REF(frame_sp), *frame_ref_tmp = 0, \
  523. GET_REF_FROM_ADDR(frame_sp))
  524. #endif
  525. #if WASM_ENABLE_MEMORY64 != 0
  526. #define POP_MEM_OFFSET() (is_memory64 ? POP_I64() : POP_I32())
  527. #define POP_TBL_ELEM_IDX() (is_table64 ? POP_I64() : POP_I32())
  528. #else
  529. #define POP_MEM_OFFSET() POP_I32()
  530. #define POP_TBL_ELEM_IDX() POP_I32()
  531. #endif
  532. #define POP_PAGE_COUNT() POP_MEM_OFFSET()
  533. #define POP_CSP_CHECK_OVERFLOW(n) \
  534. do { \
  535. bh_assert(frame_csp - n >= frame->csp_bottom); \
  536. } while (0)
  537. #define POP_CSP() \
  538. do { \
  539. POP_CSP_CHECK_OVERFLOW(1); \
  540. --frame_csp; \
  541. } while (0)
  542. #define POP_CSP_N(n) \
  543. do { \
  544. uint32 *frame_sp_old = frame_sp; \
  545. uint32 cell_num_to_copy; \
  546. POP_CSP_CHECK_OVERFLOW(n + 1); \
  547. frame_csp -= n; \
  548. frame_ip = (frame_csp - 1)->target_addr; \
  549. /* copy arity values of block */ \
  550. frame_sp = (frame_csp - 1)->frame_sp; \
  551. cell_num_to_copy = (frame_csp - 1)->cell_num; \
  552. if (cell_num_to_copy > 0) { \
  553. word_copy(frame_sp, frame_sp_old - cell_num_to_copy, \
  554. cell_num_to_copy); \
  555. frame_ref_copy(FRAME_REF(frame_sp), \
  556. FRAME_REF(frame_sp_old - cell_num_to_copy), \
  557. cell_num_to_copy); \
  558. } \
  559. frame_sp += cell_num_to_copy; \
  560. CLEAR_FRAME_REF(frame_sp, frame_sp_old - frame_sp); \
  561. } while (0)
  562. /* Pop the given number of elements from the given frame's stack. */
  563. #define POP(N) \
  564. do { \
  565. int n = (N); \
  566. frame_sp -= n; \
  567. CLEAR_FRAME_REF(frame_sp, n); \
  568. } while (0)
  569. #if WASM_ENABLE_EXCE_HANDLING != 0
  570. /* unwind the CSP to a given label and optionally modify the labeltype */
  571. #define UNWIND_CSP(N, T) \
  572. do { \
  573. /* unwind to function frame */ \
  574. frame_csp -= N; \
  575. /* drop handlers and values pushd in try block */ \
  576. frame_sp = (frame_csp - 1)->frame_sp; \
  577. (frame_csp - 1)->label_type = T ? T : (frame_csp - 1)->label_type; \
  578. } while (0)
  579. #endif
  580. #define SYNC_ALL_TO_FRAME() \
  581. do { \
  582. frame->sp = frame_sp; \
  583. frame->ip = frame_ip; \
  584. frame->csp = frame_csp; \
  585. } while (0)
  586. #define UPDATE_ALL_FROM_FRAME() \
  587. do { \
  588. frame_sp = frame->sp; \
  589. frame_ip = frame->ip; \
  590. frame_csp = frame->csp; \
  591. } while (0)
  592. #define read_leb_int64(p, p_end, res) \
  593. do { \
  594. uint8 _val = *p; \
  595. if (!(_val & 0x80)) { \
  596. res = (int64)_val; \
  597. if (_val & 0x40) \
  598. /* sign extend */ \
  599. res |= 0xFFFFFFFFFFFFFF80LL; \
  600. p++; \
  601. } \
  602. else { \
  603. uint32 _off = 0; \
  604. res = (int64)read_leb(p, &_off, 64, true); \
  605. p += _off; \
  606. } \
  607. } while (0)
  608. #define read_leb_uint32(p, p_end, res) \
  609. do { \
  610. uint8 _val = *p; \
  611. if (!(_val & 0x80)) { \
  612. res = _val; \
  613. p++; \
  614. } \
  615. else { \
  616. uint32 _off = 0; \
  617. res = (uint32)read_leb(p, &_off, 32, false); \
  618. p += _off; \
  619. } \
  620. } while (0)
  621. #define read_leb_int32(p, p_end, res) \
  622. do { \
  623. uint8 _val = *p; \
  624. if (!(_val & 0x80)) { \
  625. res = (int32)_val; \
  626. if (_val & 0x40) \
  627. /* sign extend */ \
  628. res |= 0xFFFFFF80; \
  629. p++; \
  630. } \
  631. else { \
  632. uint32 _off = 0; \
  633. res = (int32)read_leb(p, &_off, 32, true); \
  634. p += _off; \
  635. } \
  636. } while (0)
  637. #if WASM_ENABLE_MEMORY64 != 0
  638. #define read_leb_mem_offset(p, p_end, res) \
  639. do { \
  640. uint8 _val = *p; \
  641. if (!(_val & 0x80)) { \
  642. res = (mem_offset_t)_val; \
  643. p++; \
  644. } \
  645. else { \
  646. uint32 _off = 0; \
  647. res = (mem_offset_t)read_leb(p, &_off, is_memory64 ? 64 : 32, \
  648. false); \
  649. p += _off; \
  650. } \
  651. } while (0)
  652. #else
  653. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  654. #endif
  655. #if WASM_ENABLE_MULTI_MEMORY != 0
  656. /* If the current memidx differs than the last cached one,
  657. * update memory related information */
  658. #define read_leb_memidx(p, p_end, res) \
  659. do { \
  660. read_leb_uint32(p, p_end, res); \
  661. if (res != memidx_cached) { \
  662. memory = wasm_get_memory_with_idx(module, res); \
  663. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory); \
  664. memidx_cached = res; \
  665. } \
  666. } while (0)
  667. /* First read the alignment, then if it has flag indicating following memidx,
  668. * read and update memory related information, if it differs than the
  669. * last(cached) one. If it doesn't have flag reset the
  670. * memory instance to the default memories[0] */
  671. #define read_leb_memarg(p, p_end, res) \
  672. do { \
  673. read_leb_uint32(p, p_end, res); \
  674. if (!(res & OPT_MEMIDX_FLAG)) \
  675. memidx = 0; \
  676. else \
  677. read_leb_uint32(p, p_end, memidx); \
  678. if (memidx != memidx_cached) { \
  679. memory = wasm_get_memory_with_idx(module, memidx); \
  680. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory); \
  681. memidx_cached = memidx; \
  682. } \
  683. } while (0)
  684. #else
  685. #define read_leb_memarg(p, p_end, res) \
  686. do { \
  687. read_leb_uint32(p, p_end, res); \
  688. (void)res; \
  689. } while (0)
  690. #define read_leb_memidx(p, p_end, res) read_leb_memarg(p, p_end, res)
  691. #endif
  692. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  693. #define RECOVER_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  694. #else
  695. #define RECOVER_FRAME_IP_END() (void)0
  696. #endif
  697. #if WASM_ENABLE_GC != 0
  698. #define RECOVER_FRAME_REF() frame_ref = (uint8 *)frame->csp_boundary
  699. #else
  700. #define RECOVER_FRAME_REF() (void)0
  701. #endif
  702. #define RECOVER_CONTEXT(new_frame) \
  703. do { \
  704. frame = (new_frame); \
  705. cur_func = frame->function; \
  706. prev_frame = frame->prev_frame; \
  707. frame_ip = frame->ip; \
  708. RECOVER_FRAME_IP_END(); \
  709. frame_lp = frame->lp; \
  710. frame_sp = frame->sp; \
  711. frame_csp = frame->csp; \
  712. RECOVER_FRAME_REF(); \
  713. } while (0)
  714. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  715. #define GET_OPCODE() opcode = *(frame_ip - 1);
  716. #else
  717. #define GET_OPCODE() (void)0
  718. #endif
  719. #define DEF_OP_I_CONST(ctype, src_op_type) \
  720. do { \
  721. ctype cval; \
  722. read_leb_##ctype(frame_ip, frame_ip_end, cval); \
  723. PUSH_##src_op_type(cval); \
  724. } while (0)
  725. #define DEF_OP_EQZ(src_op_type) \
  726. do { \
  727. int32 pop_val; \
  728. pop_val = POP_##src_op_type() == 0; \
  729. PUSH_I32(pop_val); \
  730. } while (0)
  731. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  732. do { \
  733. uint32 res; \
  734. src_type val1, val2; \
  735. val2 = (src_type)POP_##src_op_type(); \
  736. val1 = (src_type)POP_##src_op_type(); \
  737. res = val1 cond val2; \
  738. PUSH_I32(res); \
  739. } while (0)
  740. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  741. do { \
  742. src_type val1, val2; \
  743. val1 = (src_type)POP_##src_op_type(); \
  744. val2 = (src_type)operation(val1); \
  745. PUSH_##src_op_type(val2); \
  746. } while (0)
  747. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  748. do { \
  749. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  750. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  751. operation## = *(src_type2 *)(frame_sp); \
  752. } while (0)
  753. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  754. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  755. #else
  756. #define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) \
  757. do { \
  758. src_type1 val1; \
  759. src_type2 val2; \
  760. frame_sp -= 2; \
  761. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  762. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  763. val1 operation## = val2; \
  764. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  765. } while (0)
  766. #endif
  767. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  768. do { \
  769. frame_sp -= sizeof(src_type2) / sizeof(uint32); \
  770. *(src_type1 *)(frame_sp - sizeof(src_type1) / sizeof(uint32)) \
  771. operation## = (*(src_type2 *)(frame_sp) % 32); \
  772. } while (0)
  773. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  774. do { \
  775. src_type1 val1; \
  776. src_type2 val2; \
  777. frame_sp -= 2; \
  778. val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
  779. val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
  780. val1 operation## = (val2 % 64); \
  781. PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
  782. } while (0)
  783. #define DEF_OP_MATH(src_type, src_op_type, method) \
  784. do { \
  785. src_type src_val; \
  786. src_val = POP_##src_op_type(); \
  787. PUSH_##src_op_type(method(src_val)); \
  788. } while (0)
  789. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  790. static dst_type func_name(src_type src_value, src_type src_min, \
  791. src_type src_max, dst_type dst_min, \
  792. dst_type dst_max, bool is_sign) \
  793. { \
  794. dst_type dst_value = 0; \
  795. if (!isnan(src_value)) { \
  796. if (src_value <= src_min) \
  797. dst_value = dst_min; \
  798. else if (src_value >= src_max) \
  799. dst_value = dst_max; \
  800. else { \
  801. if (is_sign) \
  802. dst_value = (dst_type)(signed_type)src_value; \
  803. else \
  804. dst_value = (dst_type)src_value; \
  805. } \
  806. } \
  807. return dst_value; \
  808. }
  809. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  810. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  811. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  812. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  813. static bool
  814. trunc_f32_to_int(WASMModuleInstance *module, uint32 *frame_sp, float32 src_min,
  815. float32 src_max, bool saturating, bool is_i32, bool is_sign)
  816. {
  817. float32 src_value = POP_F32();
  818. uint64 dst_value_i64;
  819. uint32 dst_value_i32;
  820. if (!saturating) {
  821. if (isnan(src_value)) {
  822. wasm_set_exception(module, "invalid conversion to integer");
  823. return false;
  824. }
  825. else if (src_value <= src_min || src_value >= src_max) {
  826. wasm_set_exception(module, "integer overflow");
  827. return false;
  828. }
  829. }
  830. if (is_i32) {
  831. uint32 dst_min = is_sign ? INT32_MIN : 0;
  832. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  833. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  834. dst_max, is_sign);
  835. PUSH_I32(dst_value_i32);
  836. }
  837. else {
  838. uint64 dst_min = is_sign ? INT64_MIN : 0;
  839. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  840. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  841. dst_max, is_sign);
  842. PUSH_I64(dst_value_i64);
  843. }
  844. return true;
  845. }
  846. static bool
  847. trunc_f64_to_int(WASMModuleInstance *module, uint32 *frame_sp, float64 src_min,
  848. float64 src_max, bool saturating, bool is_i32, bool is_sign)
  849. {
  850. float64 src_value = POP_F64();
  851. uint64 dst_value_i64;
  852. uint32 dst_value_i32;
  853. if (!saturating) {
  854. if (isnan(src_value)) {
  855. wasm_set_exception(module, "invalid conversion to integer");
  856. return false;
  857. }
  858. else if (src_value <= src_min || src_value >= src_max) {
  859. wasm_set_exception(module, "integer overflow");
  860. return false;
  861. }
  862. }
  863. if (is_i32) {
  864. uint32 dst_min = is_sign ? INT32_MIN : 0;
  865. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  866. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  867. dst_max, is_sign);
  868. PUSH_I32(dst_value_i32);
  869. }
  870. else {
  871. uint64 dst_min = is_sign ? INT64_MIN : 0;
  872. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  873. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  874. dst_max, is_sign);
  875. PUSH_I64(dst_value_i64);
  876. }
  877. return true;
  878. }
  879. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  880. do { \
  881. if (!trunc_f32_to_int(module, frame_sp, min, max, false, is_i32, \
  882. is_sign)) \
  883. goto got_exception; \
  884. } while (0)
  885. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  886. do { \
  887. if (!trunc_f64_to_int(module, frame_sp, min, max, false, is_i32, \
  888. is_sign)) \
  889. goto got_exception; \
  890. } while (0)
  891. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  892. do { \
  893. (void)trunc_f32_to_int(module, frame_sp, min, max, true, is_i32, \
  894. is_sign); \
  895. } while (0)
  896. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  897. do { \
  898. (void)trunc_f64_to_int(module, frame_sp, min, max, true, is_i32, \
  899. is_sign); \
  900. } while (0)
  901. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  902. do { \
  903. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  904. PUSH_##dst_op_type(value); \
  905. } while (0)
  906. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  907. do { \
  908. uint32 param_count = cur_func->param_count; \
  909. read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
  910. bh_assert(local_idx < param_count + cur_func->local_count); \
  911. local_offset = cur_func->local_offsets[local_idx]; \
  912. if (local_idx < param_count) \
  913. local_type = cur_func->param_types[local_idx]; \
  914. else \
  915. local_type = cur_func->local_types[local_idx - param_count]; \
  916. } while (0)
  917. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  918. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  919. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  920. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  921. { \
  922. uint32 readv, sval; \
  923. \
  924. sval = POP_I32(); \
  925. addr = POP_MEM_OFFSET(); \
  926. \
  927. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  928. CHECK_MEMORY_OVERFLOW(1); \
  929. CHECK_ATOMIC_MEMORY_ACCESS(); \
  930. \
  931. shared_memory_lock(memory); \
  932. readv = (uint32)(*(uint8 *)maddr); \
  933. *(uint8 *)maddr = (uint8)(readv op sval); \
  934. shared_memory_unlock(memory); \
  935. } \
  936. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  937. CHECK_MEMORY_OVERFLOW(2); \
  938. CHECK_ATOMIC_MEMORY_ACCESS(); \
  939. \
  940. shared_memory_lock(memory); \
  941. readv = (uint32)LOAD_U16(maddr); \
  942. STORE_U16(maddr, (uint16)(readv op sval)); \
  943. shared_memory_unlock(memory); \
  944. } \
  945. else { \
  946. CHECK_MEMORY_OVERFLOW(4); \
  947. CHECK_ATOMIC_MEMORY_ACCESS(); \
  948. \
  949. shared_memory_lock(memory); \
  950. readv = LOAD_I32(maddr); \
  951. STORE_U32(maddr, readv op sval); \
  952. shared_memory_unlock(memory); \
  953. } \
  954. PUSH_I32(readv); \
  955. break; \
  956. } \
  957. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  958. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  959. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  960. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  961. { \
  962. uint64 readv, sval; \
  963. \
  964. sval = (uint64)POP_I64(); \
  965. addr = POP_MEM_OFFSET(); \
  966. \
  967. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  968. CHECK_MEMORY_OVERFLOW(1); \
  969. CHECK_ATOMIC_MEMORY_ACCESS(); \
  970. \
  971. shared_memory_lock(memory); \
  972. readv = (uint64)(*(uint8 *)maddr); \
  973. *(uint8 *)maddr = (uint8)(readv op sval); \
  974. shared_memory_unlock(memory); \
  975. } \
  976. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  977. CHECK_MEMORY_OVERFLOW(2); \
  978. CHECK_ATOMIC_MEMORY_ACCESS(); \
  979. \
  980. shared_memory_lock(memory); \
  981. readv = (uint64)LOAD_U16(maddr); \
  982. STORE_U16(maddr, (uint16)(readv op sval)); \
  983. shared_memory_unlock(memory); \
  984. } \
  985. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  986. CHECK_MEMORY_OVERFLOW(4); \
  987. CHECK_ATOMIC_MEMORY_ACCESS(); \
  988. \
  989. shared_memory_lock(memory); \
  990. readv = (uint64)LOAD_U32(maddr); \
  991. STORE_U32(maddr, (uint32)(readv op sval)); \
  992. shared_memory_unlock(memory); \
  993. } \
  994. else { \
  995. uint64 op_result; \
  996. CHECK_MEMORY_OVERFLOW(8); \
  997. CHECK_ATOMIC_MEMORY_ACCESS(); \
  998. \
  999. shared_memory_lock(memory); \
  1000. readv = (uint64)LOAD_I64(maddr); \
  1001. op_result = readv op sval; \
  1002. STORE_I64(maddr, op_result); \
  1003. shared_memory_unlock(memory); \
  1004. } \
  1005. PUSH_I64(readv); \
  1006. break; \
  1007. }
  1008. static inline int32
  1009. sign_ext_8_32(int8 val)
  1010. {
  1011. if (val & 0x80)
  1012. return (int32)val | (int32)0xffffff00;
  1013. return val;
  1014. }
  1015. static inline int32
  1016. sign_ext_16_32(int16 val)
  1017. {
  1018. if (val & 0x8000)
  1019. return (int32)val | (int32)0xffff0000;
  1020. return val;
  1021. }
  1022. static inline int64
  1023. sign_ext_8_64(int8 val)
  1024. {
  1025. if (val & 0x80)
  1026. return (int64)val | (int64)0xffffffffffffff00LL;
  1027. return val;
  1028. }
  1029. static inline int64
  1030. sign_ext_16_64(int16 val)
  1031. {
  1032. if (val & 0x8000)
  1033. return (int64)val | (int64)0xffffffffffff0000LL;
  1034. return val;
  1035. }
  1036. static inline int64
  1037. sign_ext_32_64(int32 val)
  1038. {
  1039. if (val & (int32)0x80000000)
  1040. return (int64)val | (int64)0xffffffff00000000LL;
  1041. return val;
  1042. }
  1043. static inline void
  1044. word_copy(uint32 *dest, uint32 *src, unsigned num)
  1045. {
  1046. bh_assert(dest != NULL);
  1047. bh_assert(src != NULL);
  1048. bh_assert(num > 0);
  1049. if (dest != src) {
  1050. /* No overlap buffer */
  1051. bh_assert(!((src < dest) && (dest < src + num)));
  1052. for (; num > 0; num--)
  1053. *dest++ = *src++;
  1054. }
  1055. }
  1056. #if WASM_ENABLE_GC != 0
  1057. static inline void
  1058. frame_ref_copy(uint8 *frame_ref_dest, uint8 *frame_ref_src, unsigned num)
  1059. {
  1060. if (frame_ref_dest != frame_ref_src)
  1061. for (; num > 0; num--)
  1062. *frame_ref_dest++ = *frame_ref_src++;
  1063. }
  1064. #else
  1065. #define frame_ref_copy(frame_ref_dst, frame_ref_src, num) (void)0
  1066. #endif
  1067. static inline WASMInterpFrame *
  1068. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  1069. {
  1070. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  1071. if (frame) {
  1072. frame->prev_frame = prev_frame;
  1073. #if WASM_ENABLE_PERF_PROFILING != 0
  1074. frame->time_started = os_time_thread_cputime_us();
  1075. #endif
  1076. }
  1077. else {
  1078. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  1079. "wasm operand stack overflow");
  1080. }
  1081. return frame;
  1082. }
  1083. static inline void
  1084. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  1085. {
  1086. #if WASM_ENABLE_PERF_PROFILING != 0
  1087. if (frame->function) {
  1088. WASMInterpFrame *prev_frame = frame->prev_frame;
  1089. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  1090. frame->function->total_exec_time += time_elapsed;
  1091. frame->function->total_exec_cnt++;
  1092. if (prev_frame && prev_frame->function)
  1093. prev_frame->function->children_exec_time += time_elapsed;
  1094. }
  1095. #endif
  1096. wasm_exec_env_free_wasm_frame(exec_env, frame);
  1097. }
  1098. static void
  1099. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  1100. WASMExecEnv *exec_env,
  1101. WASMFunctionInstance *cur_func,
  1102. WASMInterpFrame *prev_frame)
  1103. {
  1104. WASMFunctionImport *func_import = cur_func->u.func_import;
  1105. CApiFuncImport *c_api_func_import = NULL;
  1106. unsigned local_cell_num =
  1107. cur_func->param_cell_num > 2 ? cur_func->param_cell_num : 2;
  1108. unsigned all_cell_num;
  1109. WASMInterpFrame *frame;
  1110. uint32 argv_ret[2], cur_func_index;
  1111. void *native_func_pointer = NULL;
  1112. char buf[128];
  1113. bool ret;
  1114. #if WASM_ENABLE_GC != 0
  1115. WASMFuncType *func_type;
  1116. uint8 *frame_ref;
  1117. #endif
  1118. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1119. return;
  1120. }
  1121. all_cell_num = local_cell_num;
  1122. #if WASM_ENABLE_GC != 0
  1123. all_cell_num += (local_cell_num + 3) / 4;
  1124. #endif
  1125. if (!(frame =
  1126. ALLOC_FRAME(exec_env, wasm_interp_interp_frame_size(all_cell_num),
  1127. prev_frame)))
  1128. return;
  1129. frame->function = cur_func;
  1130. frame->ip = NULL;
  1131. frame->sp = frame->lp + local_cell_num;
  1132. #if WASM_ENABLE_GC != 0
  1133. /* native function doesn't have operand stack and label stack */
  1134. frame_ref = (uint8 *)frame->sp;
  1135. init_frame_refs(frame_ref, local_cell_num, cur_func);
  1136. #endif
  1137. wasm_exec_env_set_cur_frame(exec_env, frame);
  1138. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  1139. bh_assert(cur_func_index < module_inst->module->import_function_count);
  1140. if (!func_import->call_conv_wasm_c_api) {
  1141. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  1142. }
  1143. else if (module_inst->c_api_func_imports) {
  1144. c_api_func_import = module_inst->c_api_func_imports + cur_func_index;
  1145. native_func_pointer = c_api_func_import->func_ptr_linked;
  1146. }
  1147. if (!native_func_pointer) {
  1148. snprintf(buf, sizeof(buf),
  1149. "failed to call unlinked import function (%s, %s)",
  1150. func_import->module_name, func_import->field_name);
  1151. wasm_set_exception(module_inst, buf);
  1152. return;
  1153. }
  1154. if (func_import->call_conv_wasm_c_api) {
  1155. ret = wasm_runtime_invoke_c_api_native(
  1156. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  1157. func_import->func_type, cur_func->param_cell_num, frame->lp,
  1158. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  1159. if (ret) {
  1160. argv_ret[0] = frame->lp[0];
  1161. argv_ret[1] = frame->lp[1];
  1162. }
  1163. }
  1164. else if (!func_import->call_conv_raw) {
  1165. ret = wasm_runtime_invoke_native(
  1166. exec_env, native_func_pointer, func_import->func_type,
  1167. func_import->signature, func_import->attachment, frame->lp,
  1168. cur_func->param_cell_num, argv_ret);
  1169. }
  1170. else {
  1171. ret = wasm_runtime_invoke_native_raw(
  1172. exec_env, native_func_pointer, func_import->func_type,
  1173. func_import->signature, func_import->attachment, frame->lp,
  1174. cur_func->param_cell_num, argv_ret);
  1175. }
  1176. if (!ret)
  1177. return;
  1178. #if WASM_ENABLE_GC != 0
  1179. func_type = cur_func->u.func_import->func_type;
  1180. if (func_type->result_count
  1181. && wasm_is_type_reftype(func_type->types[cur_func->param_count])) {
  1182. frame_ref = (uint8 *)prev_frame->csp_boundary
  1183. + (unsigned)(uintptr_t)(prev_frame->sp - prev_frame->lp);
  1184. if (!wasm_is_reftype_i31ref(func_type->types[cur_func->param_count])) {
  1185. #if UINTPTR_MAX == UINT64_MAX
  1186. *frame_ref = *(frame_ref + 1) = 1;
  1187. #else
  1188. *frame_ref = 1;
  1189. #endif
  1190. }
  1191. }
  1192. #endif
  1193. if (cur_func->ret_cell_num == 1) {
  1194. prev_frame->sp[0] = argv_ret[0];
  1195. prev_frame->sp++;
  1196. }
  1197. else if (cur_func->ret_cell_num == 2) {
  1198. prev_frame->sp[0] = argv_ret[0];
  1199. prev_frame->sp[1] = argv_ret[1];
  1200. prev_frame->sp += 2;
  1201. }
  1202. FREE_FRAME(exec_env, frame);
  1203. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  1204. }
  1205. #if WASM_ENABLE_FAST_JIT != 0
  1206. bool
  1207. fast_jit_invoke_native(WASMExecEnv *exec_env, uint32 func_idx,
  1208. WASMInterpFrame *prev_frame)
  1209. {
  1210. WASMModuleInstance *module_inst =
  1211. (WASMModuleInstance *)exec_env->module_inst;
  1212. WASMFunctionInstance *cur_func = module_inst->e->functions + func_idx;
  1213. wasm_interp_call_func_native(module_inst, exec_env, cur_func, prev_frame);
  1214. return wasm_copy_exception(module_inst, NULL) ? false : true;
  1215. }
  1216. #endif
  1217. #if WASM_ENABLE_MULTI_MODULE != 0
  1218. static void
  1219. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1220. WASMExecEnv *exec_env,
  1221. WASMFunctionInstance *cur_func,
  1222. WASMInterpFrame *prev_frame);
  1223. static void
  1224. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  1225. WASMExecEnv *exec_env,
  1226. WASMFunctionInstance *cur_func,
  1227. WASMInterpFrame *prev_frame)
  1228. {
  1229. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  1230. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  1231. WASMFunctionImport *func_import = cur_func->u.func_import;
  1232. uint8 *ip = prev_frame->ip;
  1233. char buf[128];
  1234. WASMExecEnv *sub_module_exec_env = NULL;
  1235. uintptr_t aux_stack_origin_boundary = 0;
  1236. uintptr_t aux_stack_origin_bottom = 0;
  1237. /*
  1238. * perform stack overflow check before calling
  1239. * wasm_interp_call_func_bytecode recursively.
  1240. */
  1241. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  1242. return;
  1243. }
  1244. if (!sub_func_inst) {
  1245. snprintf(buf, sizeof(buf),
  1246. "failed to call unlinked import function (%s, %s)",
  1247. func_import->module_name, func_import->field_name);
  1248. wasm_set_exception(module_inst, buf);
  1249. return;
  1250. }
  1251. /* Switch exec_env but keep using the same one by replacing necessary
  1252. * variables */
  1253. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  1254. (WASMModuleInstanceCommon *)sub_module_inst);
  1255. if (!sub_module_exec_env) {
  1256. wasm_set_exception(module_inst, "create singleton exec_env failed");
  1257. return;
  1258. }
  1259. /* - module_inst */
  1260. wasm_exec_env_set_module_inst(exec_env,
  1261. (WASMModuleInstanceCommon *)sub_module_inst);
  1262. /* - aux_stack_boundary */
  1263. aux_stack_origin_boundary = exec_env->aux_stack_boundary;
  1264. exec_env->aux_stack_boundary = sub_module_exec_env->aux_stack_boundary;
  1265. /* - aux_stack_bottom */
  1266. aux_stack_origin_bottom = exec_env->aux_stack_bottom;
  1267. exec_env->aux_stack_bottom = sub_module_exec_env->aux_stack_bottom;
  1268. /* set ip NULL to make call_func_bytecode return after executing
  1269. this function */
  1270. prev_frame->ip = NULL;
  1271. /* call function of sub-module*/
  1272. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  1273. prev_frame);
  1274. /* restore ip and other replaced */
  1275. prev_frame->ip = ip;
  1276. exec_env->aux_stack_boundary = aux_stack_origin_boundary;
  1277. exec_env->aux_stack_bottom = aux_stack_origin_bottom;
  1278. wasm_exec_env_restore_module_inst(exec_env,
  1279. (WASMModuleInstanceCommon *)module_inst);
  1280. }
  1281. #endif
  1282. #if WASM_ENABLE_THREAD_MGR != 0
  1283. #if WASM_ENABLE_DEBUG_INTERP != 0
  1284. #define CHECK_SUSPEND_FLAGS() \
  1285. do { \
  1286. os_mutex_lock(&exec_env->wait_lock); \
  1287. if (IS_WAMR_TERM_SIG(exec_env->current_status->signal_flag)) { \
  1288. os_mutex_unlock(&exec_env->wait_lock); \
  1289. return; \
  1290. } \
  1291. if (IS_WAMR_STOP_SIG(exec_env->current_status->signal_flag)) { \
  1292. SYNC_ALL_TO_FRAME(); \
  1293. wasm_cluster_thread_waiting_run(exec_env); \
  1294. } \
  1295. os_mutex_unlock(&exec_env->wait_lock); \
  1296. } while (0)
  1297. #else
  1298. #if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0
  1299. /* The lock is only needed when the suspend_flags is atomic; otherwise
  1300. the lock is already taken at the time when SUSPENSION_LOCK() is called. */
  1301. #define SUSPENSION_LOCK() os_mutex_lock(&exec_env->wait_lock);
  1302. #define SUSPENSION_UNLOCK() os_mutex_unlock(&exec_env->wait_lock);
  1303. #else
  1304. #define SUSPENSION_LOCK()
  1305. #define SUSPENSION_UNLOCK()
  1306. #endif
  1307. #define CHECK_SUSPEND_FLAGS() \
  1308. do { \
  1309. WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
  1310. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1311. & WASM_SUSPEND_FLAG_TERMINATE) { \
  1312. /* terminate current thread */ \
  1313. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1314. return; \
  1315. } \
  1316. while (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  1317. & WASM_SUSPEND_FLAG_SUSPEND) { \
  1318. /* suspend current thread */ \
  1319. SUSPENSION_LOCK() \
  1320. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock); \
  1321. SUSPENSION_UNLOCK() \
  1322. } \
  1323. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  1324. } while (0)
  1325. #endif /* WASM_ENABLE_DEBUG_INTERP */
  1326. #endif /* WASM_ENABLE_THREAD_MGR */
  1327. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1328. #if BH_ATOMIC_32_IS_ATOMIC != 0
  1329. #define GET_SIGNAL_FLAG() \
  1330. do { \
  1331. signal_flag = \
  1332. BH_ATOMIC_32_LOAD(exec_env->current_status->signal_flag); \
  1333. } while (0)
  1334. #else
  1335. #define GET_SIGNAL_FLAG() \
  1336. do { \
  1337. os_mutex_lock(&exec_env->wait_lock); \
  1338. signal_flag = exec_env->current_status->signal_flag; \
  1339. os_mutex_unlock(&exec_env->wait_lock); \
  1340. } while (0)
  1341. #endif
  1342. #endif
  1343. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1344. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1345. #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++]
  1346. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1347. #define HANDLE_OP_END() \
  1348. do { \
  1349. /* Record the current frame_ip, so when exception occurs, \
  1350. debugger can know the exact opcode who caused the exception */ \
  1351. frame_ip_orig = frame_ip; \
  1352. /* Atomic load the exec_env's signal_flag first, and then handle \
  1353. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1354. GET_SIGNAL_FLAG(); \
  1355. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1356. os_mutex_lock(&exec_env->wait_lock); \
  1357. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1358. && exec_env->current_status->step_count++ == 1) { \
  1359. exec_env->current_status->step_count = 0; \
  1360. SYNC_ALL_TO_FRAME(); \
  1361. wasm_cluster_thread_waiting_run(exec_env); \
  1362. } \
  1363. os_mutex_unlock(&exec_env->wait_lock); \
  1364. } \
  1365. goto *handle_table[*frame_ip++]; \
  1366. } while (0)
  1367. #else
  1368. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1369. #endif
  1370. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1371. #define HANDLE_OP(opcode) case opcode:
  1372. #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
  1373. #define HANDLE_OP_END() \
  1374. /* Record the current frame_ip, so when exception occurs, \
  1375. debugger can know the exact opcode who caused the exception */ \
  1376. frame_ip_orig = frame_ip; \
  1377. /* Atomic load the exec_env's signal_flag first, and then handle \
  1378. more with lock if it is WAMR_SIG_SINGSTEP */ \
  1379. GET_SIGNAL_FLAG(); \
  1380. if (signal_flag == WAMR_SIG_SINGSTEP) { \
  1381. os_mutex_lock(&exec_env->wait_lock); \
  1382. while (exec_env->current_status->signal_flag == WAMR_SIG_SINGSTEP \
  1383. && exec_env->current_status->step_count++ == 1) { \
  1384. exec_env->current_status->step_count = 0; \
  1385. SYNC_ALL_TO_FRAME(); \
  1386. wasm_cluster_thread_waiting_run(exec_env); \
  1387. } \
  1388. os_mutex_unlock(&exec_env->wait_lock); \
  1389. } \
  1390. continue;
  1391. #else
  1392. #define HANDLE_OP_END() continue
  1393. #endif
  1394. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1395. static inline uint8 *
  1396. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1397. {
  1398. #if WASM_ENABLE_MULTI_MODULE == 0
  1399. return global_data + global->data_offset;
  1400. #else
  1401. return global->import_global_inst
  1402. ? global->import_module_inst->global_data
  1403. + global->import_global_inst->data_offset
  1404. : global_data + global->data_offset;
  1405. #endif
  1406. }
  1407. static void
  1408. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1409. WASMExecEnv *exec_env,
  1410. WASMFunctionInstance *cur_func,
  1411. WASMInterpFrame *prev_frame)
  1412. {
  1413. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1414. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1415. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1416. || WASM_ENABLE_BULK_MEMORY != 0
  1417. uint64 linear_mem_size = 0;
  1418. if (memory)
  1419. #if WASM_ENABLE_THREAD_MGR == 0
  1420. linear_mem_size = memory->memory_data_size;
  1421. #else
  1422. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1423. #endif
  1424. #endif
  1425. WASMFuncType **wasm_types = (WASMFuncType **)module->module->types;
  1426. WASMGlobalInstance *globals = module->e->globals, *global;
  1427. uint8 *global_data = module->global_data;
  1428. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1429. WASMInterpFrame *frame = NULL;
  1430. /* Points to this special opcode so as to jump to the
  1431. * call_method_from_entry. */
  1432. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1433. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1434. register uint32 *frame_sp = NULL; /* cache of frame->sp */
  1435. #if WASM_ENABLE_GC != 0
  1436. register uint8 *frame_ref = NULL; /* cache of frame->ref */
  1437. uint8 *frame_ref_tmp;
  1438. #endif
  1439. WASMBranchBlock *frame_csp = NULL;
  1440. BlockAddr *cache_items;
  1441. uint8 *frame_ip_end = frame_ip + 1;
  1442. uint8 opcode;
  1443. uint32 i, depth, cond, count, fidx, tidx, lidx, frame_size = 0;
  1444. uint32 all_cell_num = 0;
  1445. tbl_elem_idx_t val;
  1446. uint8 *else_addr, *end_addr, *maddr = NULL;
  1447. uint32 local_idx, local_offset, global_idx;
  1448. uint8 local_type, *global_addr;
  1449. uint32 cache_index, type_index, param_cell_num, cell_num;
  1450. #if WASM_ENABLE_EXCE_HANDLING != 0
  1451. int32_t exception_tag_index;
  1452. #endif
  1453. uint8 value_type;
  1454. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1455. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1456. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  1457. bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
  1458. (WASMModuleInstanceCommon *)module);
  1459. #else
  1460. bool disable_bounds_checks = false;
  1461. #endif
  1462. #endif
  1463. #if WASM_ENABLE_GC != 0
  1464. WASMObjectRef gc_obj;
  1465. WASMStructObjectRef struct_obj;
  1466. WASMArrayObjectRef array_obj;
  1467. WASMFuncObjectRef func_obj;
  1468. WASMI31ObjectRef i31_obj;
  1469. WASMExternrefObjectRef externref_obj;
  1470. #if WASM_ENABLE_STRINGREF != 0
  1471. WASMString str_obj = NULL;
  1472. WASMStringrefObjectRef stringref_obj;
  1473. WASMStringviewWTF8ObjectRef stringview_wtf8_obj;
  1474. WASMStringviewWTF16ObjectRef stringview_wtf16_obj;
  1475. WASMStringviewIterObjectRef stringview_iter_obj;
  1476. #endif
  1477. #endif
  1478. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  1479. bool is_return_call = false;
  1480. #endif
  1481. #if WASM_ENABLE_MEMORY64 != 0
  1482. /* TODO: multi-memories for now assuming the memory idx type is consistent
  1483. * across multi-memories */
  1484. bool is_memory64 = false;
  1485. bool is_table64 = false;
  1486. if (memory)
  1487. is_memory64 = memory->is_memory64;
  1488. #endif
  1489. #if WASM_ENABLE_SHARED_HEAP != 0
  1490. WASMSharedHeap *shared_heap = module->e->shared_heap;
  1491. uint8 *shared_heap_base_addr = shared_heap ? shared_heap->base_addr : NULL;
  1492. #if WASM_ENABLE_MEMORY64 != 0
  1493. uint64 shared_heap_start_off =
  1494. shared_heap ? (is_memory64 ? shared_heap->start_off_mem64
  1495. : shared_heap->start_off_mem32)
  1496. : 0;
  1497. uint64 shared_heap_end_off =
  1498. shared_heap ? (is_memory64 ? UINT64_MAX : UINT32_MAX) : 0;
  1499. #else
  1500. uint64 shared_heap_start_off =
  1501. shared_heap ? shared_heap->start_off_mem32 : 0;
  1502. uint64 shared_heap_end_off = shared_heap ? UINT32_MAX : 0;
  1503. #endif
  1504. #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
  1505. #if WASM_ENABLE_MULTI_MEMORY != 0
  1506. uint32 memidx = 0;
  1507. uint32 memidx_cached = (uint32)-1;
  1508. #endif
  1509. #if WASM_ENABLE_DEBUG_INTERP != 0
  1510. uint8 *frame_ip_orig = NULL;
  1511. WASMDebugInstance *debug_instance = wasm_exec_env_get_instance(exec_env);
  1512. bh_list *watch_point_list_read =
  1513. debug_instance ? &debug_instance->watch_point_list_read : NULL;
  1514. bh_list *watch_point_list_write =
  1515. debug_instance ? &debug_instance->watch_point_list_write : NULL;
  1516. #if WASM_ENABLE_THREAD_MGR != 0
  1517. uint32 signal_flag;
  1518. #endif
  1519. #endif
  1520. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1521. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1522. DEFINE_GOTO_TABLE(const void *, handle_table);
  1523. #undef HANDLE_OPCODE
  1524. #endif
  1525. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1526. while (frame_ip < frame_ip_end) {
  1527. opcode = *frame_ip++;
  1528. switch (opcode) {
  1529. #else
  1530. FETCH_OPCODE_AND_DISPATCH();
  1531. #endif
  1532. /* control instructions */
  1533. HANDLE_OP(WASM_OP_UNREACHABLE)
  1534. {
  1535. wasm_set_exception(module, "unreachable");
  1536. goto got_exception;
  1537. }
  1538. HANDLE_OP(WASM_OP_NOP) { HANDLE_OP_END(); }
  1539. #if WASM_ENABLE_EXCE_HANDLING != 0
  1540. HANDLE_OP(WASM_OP_RETHROW)
  1541. {
  1542. int32_t relative_depth;
  1543. read_leb_int32(frame_ip, frame_ip_end, relative_depth);
  1544. /* No frame found with exception handler; validation should
  1545. * catch it */
  1546. bh_assert(frame_csp >= frame->csp_bottom + relative_depth);
  1547. /* go up the frame stack */
  1548. WASMBranchBlock *tgtframe = (frame_csp - 1) - relative_depth;
  1549. bh_assert(tgtframe->label_type == LABEL_TYPE_CATCH
  1550. || tgtframe->label_type == LABEL_TYPE_CATCH_ALL);
  1551. /* tgtframe points to the frame containing a thrown
  1552. * exception */
  1553. uint32 *tgtframe_sp = tgtframe->frame_sp;
  1554. /* frame sp of tgtframe points to caught exception */
  1555. exception_tag_index = *((uint32 *)tgtframe_sp);
  1556. tgtframe_sp++;
  1557. /* get tag type */
  1558. uint8 tag_type_index =
  1559. module->module->tags[exception_tag_index]->type;
  1560. uint32 cell_num_to_copy =
  1561. wasm_types[tag_type_index]->param_cell_num;
  1562. /* move exception parameters (if there are any) onto top
  1563. * of stack */
  1564. if (cell_num_to_copy > 0) {
  1565. word_copy(frame_sp, tgtframe_sp - cell_num_to_copy,
  1566. cell_num_to_copy);
  1567. }
  1568. frame_sp += cell_num_to_copy;
  1569. goto find_a_catch_handler;
  1570. }
  1571. HANDLE_OP(WASM_OP_THROW)
  1572. {
  1573. read_leb_int32(frame_ip, frame_ip_end, exception_tag_index);
  1574. /* landing pad for the rethrow ? */
  1575. find_a_catch_handler:
  1576. {
  1577. WASMFuncType *tag_type = NULL;
  1578. uint32 cell_num_to_copy = 0;
  1579. if (IS_INVALID_TAGINDEX(exception_tag_index)) {
  1580. /*
  1581. * invalid exception index,
  1582. * generated if a submodule throws an exception
  1583. * that has not been imported here
  1584. *
  1585. * This should result in a branch to the CATCH_ALL block,
  1586. * if there is one
  1587. */
  1588. tag_type = NULL;
  1589. cell_num_to_copy = 0;
  1590. }
  1591. else {
  1592. if (module->e->tags[exception_tag_index].is_import_tag) {
  1593. tag_type = module->e->tags[exception_tag_index]
  1594. .u.tag_import->tag_type;
  1595. }
  1596. else {
  1597. tag_type = module->e->tags[exception_tag_index]
  1598. .u.tag->tag_type;
  1599. }
  1600. cell_num_to_copy = tag_type->param_cell_num;
  1601. }
  1602. /* browse through frame stack */
  1603. uint32 relative_depth = 0;
  1604. do {
  1605. POP_CSP_CHECK_OVERFLOW(relative_depth - 1);
  1606. WASMBranchBlock *tgtframe = frame_csp - relative_depth - 1;
  1607. switch (tgtframe->label_type) {
  1608. case LABEL_TYPE_BLOCK:
  1609. case LABEL_TYPE_IF:
  1610. case LABEL_TYPE_LOOP:
  1611. case LABEL_TYPE_CATCH:
  1612. case LABEL_TYPE_CATCH_ALL:
  1613. /*
  1614. * skip that blocks in search
  1615. * BLOCK, IF and LOOP do not contain handlers and
  1616. * cannot catch exceptions.
  1617. * blocks marked as CATCH or
  1618. * CATCH_ALL did already caught an exception and can
  1619. * only be a target for RETHROW, but cannot catch an
  1620. * exception again
  1621. */
  1622. break;
  1623. case LABEL_TYPE_TRY:
  1624. {
  1625. uint32 handler_number = 0;
  1626. uint8 **handlers = (uint8 **)tgtframe->frame_sp;
  1627. uint8 *handler = NULL;
  1628. while ((handler = handlers[handler_number]) != 0) {
  1629. uint8 handler_opcode = *handler;
  1630. uint8 *target_addr =
  1631. handler
  1632. + 1; /* first instruction or leb-immediate
  1633. behind the handler opcode */
  1634. switch (handler_opcode) {
  1635. case WASM_OP_CATCH:
  1636. {
  1637. int32 lookup_index = 0;
  1638. /* read the tag_index and advance
  1639. * target_addr to the first instruction
  1640. * in the block */
  1641. read_leb_int32(target_addr, 0,
  1642. lookup_index);
  1643. if (exception_tag_index
  1644. == lookup_index) {
  1645. /* set ip */
  1646. frame_ip = target_addr;
  1647. /* save frame_sp (points to
  1648. * exception values) */
  1649. uint32 *frame_sp_old = frame_sp;
  1650. UNWIND_CSP(relative_depth,
  1651. LABEL_TYPE_CATCH);
  1652. /* push exception_tag_index and
  1653. * exception values for rethrow */
  1654. PUSH_I32(exception_tag_index);
  1655. if (cell_num_to_copy > 0) {
  1656. word_copy(
  1657. frame_sp,
  1658. frame_sp_old
  1659. - cell_num_to_copy,
  1660. cell_num_to_copy);
  1661. frame_sp += cell_num_to_copy;
  1662. /* push exception values for
  1663. * catch
  1664. */
  1665. word_copy(
  1666. frame_sp,
  1667. frame_sp_old
  1668. - cell_num_to_copy,
  1669. cell_num_to_copy);
  1670. frame_sp += cell_num_to_copy;
  1671. }
  1672. /* advance to handler */
  1673. HANDLE_OP_END();
  1674. }
  1675. break;
  1676. }
  1677. case WASM_OP_DELEGATE:
  1678. {
  1679. int32 lookup_depth = 0;
  1680. /* read the depth */
  1681. read_leb_int32(target_addr, 0,
  1682. lookup_depth);
  1683. /* save frame_sp (points to exception
  1684. * values) */
  1685. uint32 *frame_sp_old = frame_sp;
  1686. UNWIND_CSP(relative_depth,
  1687. LABEL_TYPE_CATCH);
  1688. /* leave the block (the delegate is
  1689. * technically not inside the frame) */
  1690. frame_csp--;
  1691. /* unwind to delegated frame */
  1692. frame_csp -= lookup_depth;
  1693. /* push exception values for catch */
  1694. if (cell_num_to_copy > 0) {
  1695. word_copy(frame_sp,
  1696. frame_sp_old
  1697. - cell_num_to_copy,
  1698. cell_num_to_copy);
  1699. frame_sp += cell_num_to_copy;
  1700. }
  1701. /* tag_index is already stored in
  1702. * exception_tag_index */
  1703. goto find_a_catch_handler;
  1704. }
  1705. case WASM_OP_CATCH_ALL:
  1706. {
  1707. /* no immediate */
  1708. /* save frame_sp (points to exception
  1709. * values) */
  1710. uint32 *frame_sp_old = frame_sp;
  1711. /* set ip */
  1712. frame_ip = target_addr;
  1713. UNWIND_CSP(relative_depth,
  1714. LABEL_TYPE_CATCH_ALL);
  1715. /* push exception_tag_index and
  1716. * exception values for rethrow */
  1717. PUSH_I32(exception_tag_index);
  1718. if (cell_num_to_copy > 0) {
  1719. word_copy(frame_sp,
  1720. frame_sp_old
  1721. - cell_num_to_copy,
  1722. cell_num_to_copy);
  1723. frame_sp += cell_num_to_copy;
  1724. }
  1725. /* catch_all has no exception values */
  1726. /* advance to handler */
  1727. HANDLE_OP_END();
  1728. }
  1729. default:
  1730. wasm_set_exception(
  1731. module, "WASM_OP_THROW found "
  1732. "unexpected handler type");
  1733. goto got_exception;
  1734. }
  1735. handler_number++;
  1736. }
  1737. /* exception not caught in this frame */
  1738. break;
  1739. }
  1740. case LABEL_TYPE_FUNCTION:
  1741. {
  1742. /* save frame_sp (points to exception values) */
  1743. uint32 *frame_sp_old = frame_sp;
  1744. UNWIND_CSP(relative_depth, LABEL_TYPE_FUNCTION);
  1745. /* push exception values for catch
  1746. * The values are copied to the CALLER FRAME
  1747. * (prev_frame->sp) same behvior ad WASM_OP_RETURN
  1748. */
  1749. if (cell_num_to_copy > 0) {
  1750. word_copy(prev_frame->sp,
  1751. frame_sp_old - cell_num_to_copy,
  1752. cell_num_to_copy);
  1753. prev_frame->sp += cell_num_to_copy;
  1754. }
  1755. *((int32 *)(prev_frame->sp)) = exception_tag_index;
  1756. prev_frame->sp++;
  1757. /* mark frame as raised exception */
  1758. wasm_set_exception(module,
  1759. "uncaught wasm exception");
  1760. /* end of function, treat as WASM_OP_RETURN */
  1761. goto return_func;
  1762. }
  1763. default:
  1764. wasm_set_exception(
  1765. module,
  1766. "unexpected or invalid label in THROW or "
  1767. "RETHROW when searching a catch handler");
  1768. goto got_exception;
  1769. }
  1770. relative_depth++;
  1771. } while (1);
  1772. }
  1773. /* something went wrong. normally, we should always find the
  1774. * func label. if not, stop the interpreter */
  1775. wasm_set_exception(
  1776. module, "WASM_OP_THROW hit the bottom of the frame stack");
  1777. goto got_exception;
  1778. }
  1779. HANDLE_OP(EXT_OP_TRY)
  1780. {
  1781. /* read the blocktype */
  1782. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1783. param_cell_num = wasm_types[type_index]->param_cell_num;
  1784. cell_num = wasm_types[type_index]->ret_cell_num;
  1785. goto handle_op_try;
  1786. }
  1787. HANDLE_OP(WASM_OP_TRY)
  1788. {
  1789. value_type = *frame_ip++;
  1790. param_cell_num = 0;
  1791. cell_num = wasm_value_type_cell_num(value_type);
  1792. handle_op_try:
  1793. cache_index = ((uintptr_t)frame_ip)
  1794. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1795. cache_items = exec_env->block_addr_cache[cache_index];
  1796. if (cache_items[0].start_addr == frame_ip) {
  1797. cache_items[0].start_addr = 0;
  1798. }
  1799. if (cache_items[1].start_addr == frame_ip) {
  1800. cache_items[1].start_addr = 0;
  1801. }
  1802. /* start at the first opcode following the try and its blocktype
  1803. */
  1804. uint8 *lookup_cursor = frame_ip;
  1805. uint8 handler_opcode = WASM_OP_UNREACHABLE;
  1806. /* target_addr filled in when END or DELEGATE is found */
  1807. PUSH_CSP(LABEL_TYPE_TRY, param_cell_num, cell_num, 0);
  1808. /* reset to begin of block */
  1809. lookup_cursor = frame_ip;
  1810. do {
  1811. /* lookup the next CATCH, CATCH_ALL or END for this TRY */
  1812. if (!wasm_loader_find_block_addr(
  1813. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1814. lookup_cursor, (uint8 *)-1, LABEL_TYPE_TRY,
  1815. &else_addr, &end_addr)) {
  1816. /* something went wrong */
  1817. wasm_set_exception(module, "find block address failed");
  1818. goto got_exception;
  1819. }
  1820. /* place cursor for continuation past opcode */
  1821. lookup_cursor = end_addr + 1;
  1822. /* end_addr points to CATCH, CATCH_ALL, DELEGATE or END */
  1823. handler_opcode = *end_addr;
  1824. switch (handler_opcode) {
  1825. case WASM_OP_CATCH:
  1826. skip_leb(lookup_cursor); /* skip tag_index */
  1827. PUSH_PTR(end_addr);
  1828. break;
  1829. case WASM_OP_CATCH_ALL:
  1830. PUSH_PTR(end_addr);
  1831. break;
  1832. case WASM_OP_DELEGATE:
  1833. skip_leb(lookup_cursor); /* skip depth */
  1834. PUSH_PTR(end_addr);
  1835. /* patch target_addr */
  1836. (frame_csp - 1)->target_addr = lookup_cursor;
  1837. break;
  1838. case WASM_OP_END:
  1839. PUSH_PTR(0);
  1840. /* patch target_addr */
  1841. (frame_csp - 1)->target_addr = end_addr;
  1842. break;
  1843. default:
  1844. /* something went wrong */
  1845. wasm_set_exception(module,
  1846. "find block address returned an "
  1847. "unexpected opcode");
  1848. goto got_exception;
  1849. }
  1850. /* ... search until the returned address is the END of the
  1851. * TRY block */
  1852. } while (handler_opcode != WASM_OP_END
  1853. && handler_opcode != WASM_OP_DELEGATE);
  1854. /* handler setup on stack complete */
  1855. HANDLE_OP_END();
  1856. }
  1857. HANDLE_OP(WASM_OP_CATCH)
  1858. {
  1859. /* skip the tag_index */
  1860. skip_leb(frame_ip);
  1861. /* leave the frame */
  1862. POP_CSP_N(0);
  1863. HANDLE_OP_END();
  1864. }
  1865. HANDLE_OP(WASM_OP_CATCH_ALL)
  1866. {
  1867. /* leave the frame */
  1868. POP_CSP_N(0);
  1869. HANDLE_OP_END();
  1870. }
  1871. HANDLE_OP(WASM_OP_DELEGATE)
  1872. {
  1873. /* skip the delegate depth */
  1874. skip_leb(frame_ip);
  1875. /* leave the frame like WASM_OP_END */
  1876. POP_CSP();
  1877. HANDLE_OP_END();
  1878. }
  1879. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  1880. HANDLE_OP(EXT_OP_BLOCK)
  1881. {
  1882. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1883. param_cell_num =
  1884. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1885. cell_num =
  1886. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1887. goto handle_op_block;
  1888. }
  1889. HANDLE_OP(WASM_OP_BLOCK)
  1890. {
  1891. value_type = *frame_ip++;
  1892. param_cell_num = 0;
  1893. cell_num = wasm_value_type_cell_num(value_type);
  1894. handle_op_block:
  1895. cache_index = ((uintptr_t)frame_ip)
  1896. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1897. cache_items = exec_env->block_addr_cache[cache_index];
  1898. if (cache_items[0].start_addr == frame_ip) {
  1899. end_addr = cache_items[0].end_addr;
  1900. }
  1901. else if (cache_items[1].start_addr == frame_ip) {
  1902. end_addr = cache_items[1].end_addr;
  1903. }
  1904. #if WASM_ENABLE_DEBUG_INTERP != 0
  1905. else if (!wasm_loader_find_block_addr(
  1906. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1907. frame_ip, (uint8 *)-1, LABEL_TYPE_BLOCK,
  1908. &else_addr, &end_addr)) {
  1909. wasm_set_exception(module, "find block address failed");
  1910. goto got_exception;
  1911. }
  1912. #endif
  1913. else {
  1914. end_addr = NULL;
  1915. }
  1916. PUSH_CSP(LABEL_TYPE_BLOCK, param_cell_num, cell_num, end_addr);
  1917. HANDLE_OP_END();
  1918. }
  1919. HANDLE_OP(EXT_OP_LOOP)
  1920. {
  1921. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1922. param_cell_num =
  1923. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1924. cell_num =
  1925. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1926. goto handle_op_loop;
  1927. }
  1928. HANDLE_OP(WASM_OP_LOOP)
  1929. {
  1930. value_type = *frame_ip++;
  1931. param_cell_num = 0;
  1932. cell_num = 0;
  1933. handle_op_loop:
  1934. PUSH_CSP(LABEL_TYPE_LOOP, param_cell_num, cell_num, frame_ip);
  1935. HANDLE_OP_END();
  1936. }
  1937. HANDLE_OP(EXT_OP_IF)
  1938. {
  1939. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  1940. param_cell_num =
  1941. ((WASMFuncType *)wasm_types[type_index])->param_cell_num;
  1942. cell_num =
  1943. ((WASMFuncType *)wasm_types[type_index])->ret_cell_num;
  1944. goto handle_op_if;
  1945. }
  1946. HANDLE_OP(WASM_OP_IF)
  1947. {
  1948. value_type = *frame_ip++;
  1949. param_cell_num = 0;
  1950. cell_num = wasm_value_type_cell_num(value_type);
  1951. handle_op_if:
  1952. cache_index = ((uintptr_t)frame_ip)
  1953. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  1954. cache_items = exec_env->block_addr_cache[cache_index];
  1955. if (cache_items[0].start_addr == frame_ip) {
  1956. else_addr = cache_items[0].else_addr;
  1957. end_addr = cache_items[0].end_addr;
  1958. }
  1959. else if (cache_items[1].start_addr == frame_ip) {
  1960. else_addr = cache_items[1].else_addr;
  1961. end_addr = cache_items[1].end_addr;
  1962. }
  1963. else if (!wasm_loader_find_block_addr(
  1964. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  1965. frame_ip, (uint8 *)-1, LABEL_TYPE_IF, &else_addr,
  1966. &end_addr)) {
  1967. wasm_set_exception(module, "find block address failed");
  1968. goto got_exception;
  1969. }
  1970. cond = (uint32)POP_I32();
  1971. if (cond) { /* if branch is met */
  1972. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num, end_addr);
  1973. }
  1974. else { /* if branch is not met */
  1975. /* if there is no else branch, go to the end addr */
  1976. if (else_addr == NULL) {
  1977. frame_ip = end_addr + 1;
  1978. }
  1979. /* if there is an else branch, go to the else addr */
  1980. else {
  1981. PUSH_CSP(LABEL_TYPE_IF, param_cell_num, cell_num,
  1982. end_addr);
  1983. frame_ip = else_addr + 1;
  1984. }
  1985. }
  1986. HANDLE_OP_END();
  1987. }
  1988. HANDLE_OP(WASM_OP_ELSE)
  1989. {
  1990. /* comes from the if branch in WASM_OP_IF */
  1991. frame_ip = (frame_csp - 1)->target_addr;
  1992. HANDLE_OP_END();
  1993. }
  1994. HANDLE_OP(WASM_OP_END)
  1995. {
  1996. if (frame_csp > frame->csp_bottom + 1) {
  1997. POP_CSP();
  1998. }
  1999. else { /* end of function, treat as WASM_OP_RETURN */
  2000. frame_sp -= cur_func->ret_cell_num;
  2001. for (i = 0; i < cur_func->ret_cell_num; i++) {
  2002. #if WASM_ENABLE_GC != 0
  2003. if (prev_frame->ip) {
  2004. /* prev frame is not a glue frame and has
  2005. the frame ref area */
  2006. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  2007. *FRAME_REF(frame_sp + i);
  2008. }
  2009. #endif
  2010. *prev_frame->sp++ = frame_sp[i];
  2011. }
  2012. goto return_func;
  2013. }
  2014. HANDLE_OP_END();
  2015. }
  2016. HANDLE_OP(WASM_OP_BR)
  2017. {
  2018. #if WASM_ENABLE_THREAD_MGR != 0
  2019. CHECK_SUSPEND_FLAGS();
  2020. #endif
  2021. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2022. label_pop_csp_n:
  2023. POP_CSP_N(depth);
  2024. if (!frame_ip) { /* must be label pushed by WASM_OP_BLOCK */
  2025. if (!wasm_loader_find_block_addr(
  2026. exec_env, (BlockAddr *)exec_env->block_addr_cache,
  2027. (frame_csp - 1)->begin_addr, (uint8 *)-1,
  2028. LABEL_TYPE_BLOCK, &else_addr, &end_addr)) {
  2029. wasm_set_exception(module, "find block address failed");
  2030. goto got_exception;
  2031. }
  2032. frame_ip = end_addr;
  2033. }
  2034. HANDLE_OP_END();
  2035. }
  2036. HANDLE_OP(WASM_OP_BR_IF)
  2037. {
  2038. #if WASM_ENABLE_THREAD_MGR != 0
  2039. CHECK_SUSPEND_FLAGS();
  2040. #endif
  2041. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2042. cond = (uint32)POP_I32();
  2043. if (cond)
  2044. goto label_pop_csp_n;
  2045. HANDLE_OP_END();
  2046. }
  2047. HANDLE_OP(WASM_OP_BR_TABLE)
  2048. {
  2049. #if WASM_ENABLE_THREAD_MGR != 0
  2050. CHECK_SUSPEND_FLAGS();
  2051. #endif
  2052. read_leb_uint32(frame_ip, frame_ip_end, count);
  2053. lidx = POP_I32();
  2054. if (lidx > count)
  2055. lidx = count;
  2056. depth = frame_ip[lidx];
  2057. goto label_pop_csp_n;
  2058. }
  2059. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  2060. {
  2061. BrTableCache *node_cache =
  2062. bh_list_first_elem(module->module->br_table_cache_list);
  2063. BrTableCache *node_next;
  2064. #if WASM_ENABLE_THREAD_MGR != 0
  2065. CHECK_SUSPEND_FLAGS();
  2066. #endif
  2067. lidx = POP_I32();
  2068. while (node_cache) {
  2069. node_next = bh_list_elem_next(node_cache);
  2070. if (node_cache->br_table_op_addr == frame_ip - 1) {
  2071. if (lidx > node_cache->br_count)
  2072. lidx = node_cache->br_count;
  2073. depth = node_cache->br_depths[lidx];
  2074. goto label_pop_csp_n;
  2075. }
  2076. node_cache = node_next;
  2077. }
  2078. bh_assert(0);
  2079. HANDLE_OP_END();
  2080. }
  2081. HANDLE_OP(WASM_OP_RETURN)
  2082. {
  2083. frame_sp -= cur_func->ret_cell_num;
  2084. for (i = 0; i < cur_func->ret_cell_num; i++) {
  2085. #if WASM_ENABLE_GC != 0
  2086. if (prev_frame->ip) {
  2087. /* prev frame is not a glue frame and has
  2088. the frame ref area */
  2089. *FRAME_REF_FOR(prev_frame, prev_frame->sp) =
  2090. *FRAME_REF(frame_sp + i);
  2091. }
  2092. #endif
  2093. *prev_frame->sp++ = frame_sp[i];
  2094. }
  2095. goto return_func;
  2096. }
  2097. HANDLE_OP(WASM_OP_CALL)
  2098. {
  2099. #if WASM_ENABLE_THREAD_MGR != 0
  2100. CHECK_SUSPEND_FLAGS();
  2101. #endif
  2102. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2103. #if WASM_ENABLE_MULTI_MODULE != 0
  2104. if (fidx >= module->e->function_count) {
  2105. wasm_set_exception(module, "unknown function");
  2106. goto got_exception;
  2107. }
  2108. #endif
  2109. cur_func = module->e->functions + fidx;
  2110. goto call_func_from_interp;
  2111. }
  2112. #if WASM_ENABLE_TAIL_CALL != 0
  2113. HANDLE_OP(WASM_OP_RETURN_CALL)
  2114. {
  2115. #if WASM_ENABLE_THREAD_MGR != 0
  2116. CHECK_SUSPEND_FLAGS();
  2117. #endif
  2118. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  2119. #if WASM_ENABLE_MULTI_MODULE != 0
  2120. if (fidx >= module->e->function_count) {
  2121. wasm_set_exception(module, "unknown function");
  2122. goto got_exception;
  2123. }
  2124. #endif
  2125. cur_func = module->e->functions + fidx;
  2126. goto call_func_from_return_call;
  2127. }
  2128. #endif /* WASM_ENABLE_TAIL_CALL */
  2129. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  2130. #if WASM_ENABLE_TAIL_CALL != 0
  2131. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  2132. #endif
  2133. {
  2134. WASMFuncType *cur_type, *cur_func_type;
  2135. WASMTableInstance *tbl_inst;
  2136. uint32 tbl_idx;
  2137. #if WASM_ENABLE_TAIL_CALL != 0
  2138. opcode = *(frame_ip - 1);
  2139. #endif
  2140. #if WASM_ENABLE_THREAD_MGR != 0
  2141. CHECK_SUSPEND_FLAGS();
  2142. #endif
  2143. /**
  2144. * type check. compiler will make sure all like
  2145. * (call_indirect (type $x) (it.const 1))
  2146. * the function type has to be defined in the module also
  2147. * no matter it is used or not
  2148. */
  2149. read_leb_uint32(frame_ip, frame_ip_end, tidx);
  2150. bh_assert(tidx < module->module->type_count);
  2151. cur_type = wasm_types[tidx];
  2152. /* clang-format off */
  2153. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2154. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2155. #else
  2156. frame_ip++;
  2157. tbl_idx = 0;
  2158. #endif
  2159. bh_assert(tbl_idx < module->table_count);
  2160. /* clang-format on */
  2161. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2162. #if WASM_ENABLE_MEMORY64 != 0
  2163. is_table64 = tbl_inst->is_table64;
  2164. #endif
  2165. val = POP_TBL_ELEM_IDX();
  2166. if (val >= tbl_inst->cur_size) {
  2167. wasm_set_exception(module, "undefined element");
  2168. goto got_exception;
  2169. }
  2170. /* clang-format off */
  2171. #if WASM_ENABLE_GC == 0
  2172. fidx = tbl_inst->elems[val];
  2173. if (fidx == (uint32)-1) {
  2174. wasm_set_exception(module, "uninitialized element");
  2175. goto got_exception;
  2176. }
  2177. #else
  2178. func_obj = (WASMFuncObjectRef)tbl_inst->elems[val];
  2179. if (!func_obj) {
  2180. wasm_set_exception(module, "uninitialized element");
  2181. goto got_exception;
  2182. }
  2183. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2184. #endif
  2185. /* clang-format on */
  2186. /*
  2187. * we might be using a table injected by host or
  2188. * another module. In that case, we don't validate
  2189. * the elem value while loading
  2190. */
  2191. if (fidx >= module->e->function_count) {
  2192. wasm_set_exception(module, "unknown function");
  2193. goto got_exception;
  2194. }
  2195. /* always call module own functions */
  2196. cur_func = module->e->functions + fidx;
  2197. if (cur_func->is_import_func)
  2198. cur_func_type = cur_func->u.func_import->func_type;
  2199. else
  2200. cur_func_type = cur_func->u.func->func_type;
  2201. /* clang-format off */
  2202. #if WASM_ENABLE_GC == 0
  2203. if (cur_type != cur_func_type) {
  2204. wasm_set_exception(module, "indirect call type mismatch");
  2205. goto got_exception;
  2206. }
  2207. #else
  2208. if (!wasm_func_type_is_super_of(cur_type, cur_func_type)) {
  2209. wasm_set_exception(module, "indirect call type mismatch");
  2210. goto got_exception;
  2211. }
  2212. #endif
  2213. /* clang-format on */
  2214. #if WASM_ENABLE_TAIL_CALL != 0
  2215. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  2216. goto call_func_from_return_call;
  2217. #endif
  2218. goto call_func_from_interp;
  2219. }
  2220. /* parametric instructions */
  2221. HANDLE_OP(WASM_OP_DROP)
  2222. {
  2223. frame_sp--;
  2224. #if WASM_ENABLE_GC != 0
  2225. frame_ref_tmp = FRAME_REF(frame_sp);
  2226. *frame_ref_tmp = 0;
  2227. #endif
  2228. HANDLE_OP_END();
  2229. }
  2230. HANDLE_OP(WASM_OP_DROP_64)
  2231. {
  2232. frame_sp -= 2;
  2233. #if WASM_ENABLE_GC != 0
  2234. frame_ref_tmp = FRAME_REF(frame_sp);
  2235. *frame_ref_tmp = 0;
  2236. *(frame_ref_tmp + 1) = 0;
  2237. #endif
  2238. HANDLE_OP_END();
  2239. }
  2240. HANDLE_OP(WASM_OP_SELECT)
  2241. {
  2242. cond = (uint32)POP_I32();
  2243. frame_sp--;
  2244. if (!cond)
  2245. *(frame_sp - 1) = *frame_sp;
  2246. HANDLE_OP_END();
  2247. }
  2248. HANDLE_OP(WASM_OP_SELECT_64)
  2249. {
  2250. cond = (uint32)POP_I32();
  2251. frame_sp -= 2;
  2252. if (!cond) {
  2253. *(frame_sp - 2) = *frame_sp;
  2254. *(frame_sp - 1) = *(frame_sp + 1);
  2255. }
  2256. HANDLE_OP_END();
  2257. }
  2258. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  2259. HANDLE_OP(WASM_OP_SELECT_T)
  2260. {
  2261. uint32 vec_len;
  2262. uint8 type;
  2263. read_leb_uint32(frame_ip, frame_ip_end, vec_len);
  2264. type = *frame_ip++;
  2265. cond = (uint32)POP_I32();
  2266. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  2267. #if WASM_ENABLE_GC != 0 && UINTPTR_MAX == UINT64_MAX
  2268. || wasm_is_type_reftype(type)
  2269. #endif
  2270. ) {
  2271. frame_sp -= 2;
  2272. if (!cond) {
  2273. *(frame_sp - 2) = *frame_sp;
  2274. *(frame_sp - 1) = *(frame_sp + 1);
  2275. }
  2276. }
  2277. else {
  2278. frame_sp--;
  2279. if (!cond)
  2280. *(frame_sp - 1) = *frame_sp;
  2281. }
  2282. #if WASM_ENABLE_GC != 0
  2283. frame_ref_tmp = FRAME_REF(frame_sp);
  2284. *frame_ref_tmp = 0;
  2285. #if UINTPTR_MAX == UINT64_MAX
  2286. *(frame_ref_tmp + 1) = 0;
  2287. #endif
  2288. #endif
  2289. (void)vec_len;
  2290. HANDLE_OP_END();
  2291. }
  2292. HANDLE_OP(WASM_OP_TABLE_GET)
  2293. {
  2294. uint32 tbl_idx;
  2295. tbl_elem_idx_t elem_idx;
  2296. WASMTableInstance *tbl_inst;
  2297. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2298. bh_assert(tbl_idx < module->table_count);
  2299. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2300. #if WASM_ENABLE_MEMORY64 != 0
  2301. is_table64 = tbl_inst->is_table64;
  2302. #endif
  2303. elem_idx = POP_TBL_ELEM_IDX();
  2304. if (elem_idx >= tbl_inst->cur_size) {
  2305. wasm_set_exception(module, "out of bounds table access");
  2306. goto got_exception;
  2307. }
  2308. #if WASM_ENABLE_GC == 0
  2309. PUSH_I32(tbl_inst->elems[elem_idx]);
  2310. #else
  2311. PUSH_REF(tbl_inst->elems[elem_idx]);
  2312. #endif
  2313. HANDLE_OP_END();
  2314. }
  2315. HANDLE_OP(WASM_OP_TABLE_SET)
  2316. {
  2317. WASMTableInstance *tbl_inst;
  2318. uint32 tbl_idx;
  2319. tbl_elem_idx_t elem_idx;
  2320. table_elem_type_t elem_val;
  2321. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  2322. bh_assert(tbl_idx < module->table_count);
  2323. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2324. #if WASM_ENABLE_MEMORY64 != 0
  2325. is_table64 = tbl_inst->is_table64;
  2326. #endif
  2327. #if WASM_ENABLE_GC == 0
  2328. elem_val = POP_I32();
  2329. #else
  2330. elem_val = POP_REF();
  2331. #endif
  2332. elem_idx = POP_TBL_ELEM_IDX();
  2333. if (elem_idx >= tbl_inst->cur_size) {
  2334. wasm_set_exception(module, "out of bounds table access");
  2335. goto got_exception;
  2336. }
  2337. tbl_inst->elems[elem_idx] = elem_val;
  2338. HANDLE_OP_END();
  2339. }
  2340. HANDLE_OP(WASM_OP_REF_NULL)
  2341. {
  2342. uint32 ref_type;
  2343. read_leb_uint32(frame_ip, frame_ip_end, ref_type);
  2344. #if WASM_ENABLE_GC == 0
  2345. PUSH_I32(NULL_REF);
  2346. #else
  2347. PUSH_REF(NULL_REF);
  2348. #endif
  2349. (void)ref_type;
  2350. HANDLE_OP_END();
  2351. }
  2352. HANDLE_OP(WASM_OP_REF_IS_NULL)
  2353. {
  2354. #if WASM_ENABLE_GC == 0
  2355. uint32 ref_val;
  2356. ref_val = POP_I32();
  2357. #else
  2358. void *ref_val;
  2359. ref_val = POP_REF();
  2360. #endif
  2361. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  2362. HANDLE_OP_END();
  2363. }
  2364. HANDLE_OP(WASM_OP_REF_FUNC)
  2365. {
  2366. uint32 func_idx;
  2367. read_leb_uint32(frame_ip, frame_ip_end, func_idx);
  2368. #if WASM_ENABLE_GC == 0
  2369. PUSH_I32(func_idx);
  2370. #else
  2371. SYNC_ALL_TO_FRAME();
  2372. if (!(gc_obj = wasm_create_func_obj(module, func_idx, true,
  2373. NULL, 0))) {
  2374. goto got_exception;
  2375. }
  2376. PUSH_REF(gc_obj);
  2377. #endif
  2378. HANDLE_OP_END();
  2379. }
  2380. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  2381. #if WASM_ENABLE_GC != 0
  2382. HANDLE_OP(WASM_OP_CALL_REF)
  2383. {
  2384. #if WASM_ENABLE_THREAD_MGR != 0
  2385. CHECK_SUSPEND_FLAGS();
  2386. #endif
  2387. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2388. func_obj = POP_REF();
  2389. if (!func_obj) {
  2390. wasm_set_exception(module, "null function object");
  2391. goto got_exception;
  2392. }
  2393. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2394. cur_func = module->e->functions + fidx;
  2395. goto call_func_from_interp;
  2396. }
  2397. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  2398. {
  2399. #if WASM_ENABLE_THREAD_MGR != 0
  2400. CHECK_SUSPEND_FLAGS();
  2401. #endif
  2402. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2403. func_obj = POP_REF();
  2404. if (!func_obj) {
  2405. wasm_set_exception(module, "null function object");
  2406. goto got_exception;
  2407. }
  2408. fidx = wasm_func_obj_get_func_idx_bound(func_obj);
  2409. cur_func = module->e->functions + fidx;
  2410. goto call_func_from_return_call;
  2411. }
  2412. HANDLE_OP(WASM_OP_REF_EQ)
  2413. {
  2414. WASMObjectRef gc_obj1, gc_obj2;
  2415. gc_obj2 = POP_REF();
  2416. gc_obj1 = POP_REF();
  2417. val = wasm_obj_equal(gc_obj1, gc_obj2);
  2418. PUSH_I32(val);
  2419. HANDLE_OP_END();
  2420. }
  2421. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  2422. {
  2423. gc_obj = POP_REF();
  2424. if (gc_obj == NULL_REF) {
  2425. wasm_set_exception(module, "null reference");
  2426. goto got_exception;
  2427. }
  2428. PUSH_REF(gc_obj);
  2429. HANDLE_OP_END();
  2430. }
  2431. HANDLE_OP(WASM_OP_BR_ON_NULL)
  2432. {
  2433. #if WASM_ENABLE_THREAD_MGR != 0
  2434. CHECK_SUSPEND_FLAGS();
  2435. #endif
  2436. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2437. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2438. if (gc_obj == NULL_REF) {
  2439. frame_sp -= REF_CELL_NUM;
  2440. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2441. goto label_pop_csp_n;
  2442. }
  2443. HANDLE_OP_END();
  2444. }
  2445. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  2446. {
  2447. #if WASM_ENABLE_THREAD_MGR != 0
  2448. CHECK_SUSPEND_FLAGS();
  2449. #endif
  2450. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2451. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2452. if (gc_obj != NULL_REF) {
  2453. goto label_pop_csp_n;
  2454. }
  2455. else {
  2456. frame_sp -= REF_CELL_NUM;
  2457. CLEAR_FRAME_REF(frame_sp, REF_CELL_NUM);
  2458. }
  2459. HANDLE_OP_END();
  2460. }
  2461. HANDLE_OP(WASM_OP_GC_PREFIX)
  2462. {
  2463. uint32 opcode1;
  2464. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  2465. /* opcode1 was checked in loader and is no larger than
  2466. UINT8_MAX */
  2467. opcode = (uint8)opcode1;
  2468. switch (opcode) {
  2469. case WASM_OP_STRUCT_NEW:
  2470. case WASM_OP_STRUCT_NEW_DEFAULT:
  2471. {
  2472. WASMModule *wasm_module = module->module;
  2473. WASMStructType *struct_type;
  2474. WASMRttType *rtt_type;
  2475. WASMValue field_value = { 0 };
  2476. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2477. struct_type =
  2478. (WASMStructType *)module->module->types[type_index];
  2479. if (!(rtt_type = wasm_rtt_type_new(
  2480. (WASMType *)struct_type, type_index,
  2481. wasm_module->rtt_types,
  2482. wasm_module->type_count,
  2483. &wasm_module->rtt_type_lock))) {
  2484. wasm_set_exception(module,
  2485. "create rtt type failed");
  2486. goto got_exception;
  2487. }
  2488. SYNC_ALL_TO_FRAME();
  2489. struct_obj = wasm_struct_obj_new(exec_env, rtt_type);
  2490. if (!struct_obj) {
  2491. wasm_set_exception(module,
  2492. "create struct object failed");
  2493. goto got_exception;
  2494. }
  2495. if (opcode == WASM_OP_STRUCT_NEW) {
  2496. WASMStructFieldType *fields = struct_type->fields;
  2497. int32 field_count = (int32)struct_type->field_count;
  2498. int32 field_idx;
  2499. uint8 field_type;
  2500. for (field_idx = field_count - 1; field_idx >= 0;
  2501. field_idx--) {
  2502. field_type = fields[field_idx].field_type;
  2503. if (wasm_is_type_reftype(field_type)) {
  2504. field_value.gc_obj = POP_REF();
  2505. }
  2506. else if (field_type == VALUE_TYPE_I32
  2507. || field_type == VALUE_TYPE_F32
  2508. || field_type == PACKED_TYPE_I8
  2509. || field_type == PACKED_TYPE_I16) {
  2510. field_value.i32 = POP_I32();
  2511. }
  2512. else {
  2513. field_value.i64 = POP_I64();
  2514. }
  2515. wasm_struct_obj_set_field(struct_obj, field_idx,
  2516. &field_value);
  2517. }
  2518. }
  2519. PUSH_REF(struct_obj);
  2520. HANDLE_OP_END();
  2521. }
  2522. case WASM_OP_STRUCT_GET:
  2523. case WASM_OP_STRUCT_GET_S:
  2524. case WASM_OP_STRUCT_GET_U:
  2525. {
  2526. WASMStructType *struct_type;
  2527. WASMValue field_value = { 0 };
  2528. uint32 field_idx;
  2529. uint8 field_type;
  2530. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2531. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2532. struct_type =
  2533. (WASMStructType *)module->module->types[type_index];
  2534. struct_obj = POP_REF();
  2535. if (!struct_obj) {
  2536. wasm_set_exception(module, "null structure object");
  2537. goto got_exception;
  2538. }
  2539. wasm_struct_obj_get_field(
  2540. struct_obj, field_idx,
  2541. opcode == WASM_OP_STRUCT_GET_S ? true : false,
  2542. &field_value);
  2543. field_type = struct_type->fields[field_idx].field_type;
  2544. if (wasm_is_reftype_i31ref(field_type)) {
  2545. PUSH_I31REF(field_value.gc_obj);
  2546. }
  2547. else if (wasm_is_type_reftype(field_type)) {
  2548. PUSH_REF(field_value.gc_obj);
  2549. }
  2550. else if (field_type == VALUE_TYPE_I32
  2551. || field_type == VALUE_TYPE_F32
  2552. || field_type == PACKED_TYPE_I8
  2553. || field_type == PACKED_TYPE_I16) {
  2554. PUSH_I32(field_value.i32);
  2555. }
  2556. else {
  2557. PUSH_I64(field_value.i64);
  2558. }
  2559. HANDLE_OP_END();
  2560. }
  2561. case WASM_OP_STRUCT_SET:
  2562. {
  2563. WASMStructType *struct_type;
  2564. WASMValue field_value = { 0 };
  2565. uint32 field_idx;
  2566. uint8 field_type;
  2567. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2568. read_leb_uint32(frame_ip, frame_ip_end, field_idx);
  2569. struct_type =
  2570. (WASMStructType *)module->module->types[type_index];
  2571. field_type = struct_type->fields[field_idx].field_type;
  2572. if (wasm_is_type_reftype(field_type)) {
  2573. field_value.gc_obj = POP_REF();
  2574. }
  2575. else if (field_type == VALUE_TYPE_I32
  2576. || field_type == VALUE_TYPE_F32
  2577. || field_type == PACKED_TYPE_I8
  2578. || field_type == PACKED_TYPE_I16) {
  2579. field_value.i32 = POP_I32();
  2580. }
  2581. else {
  2582. field_value.i64 = POP_I64();
  2583. }
  2584. struct_obj = POP_REF();
  2585. if (!struct_obj) {
  2586. wasm_set_exception(module, "null structure object");
  2587. goto got_exception;
  2588. }
  2589. wasm_struct_obj_set_field(struct_obj, field_idx,
  2590. &field_value);
  2591. HANDLE_OP_END();
  2592. }
  2593. case WASM_OP_ARRAY_NEW:
  2594. case WASM_OP_ARRAY_NEW_DEFAULT:
  2595. case WASM_OP_ARRAY_NEW_FIXED:
  2596. {
  2597. WASMModule *wasm_module = module->module;
  2598. WASMArrayType *array_type;
  2599. WASMRttType *rtt_type;
  2600. WASMValue array_elem = { 0 };
  2601. uint32 array_len;
  2602. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2603. array_type =
  2604. (WASMArrayType *)wasm_module->types[type_index];
  2605. if (!(rtt_type = wasm_rtt_type_new(
  2606. (WASMType *)array_type, type_index,
  2607. wasm_module->rtt_types,
  2608. wasm_module->type_count,
  2609. &wasm_module->rtt_type_lock))) {
  2610. wasm_set_exception(module,
  2611. "create rtt type failed");
  2612. goto got_exception;
  2613. }
  2614. if (opcode != WASM_OP_ARRAY_NEW_FIXED)
  2615. array_len = POP_I32();
  2616. else
  2617. read_leb_uint32(frame_ip, frame_ip_end, array_len);
  2618. if (opcode == WASM_OP_ARRAY_NEW) {
  2619. if (wasm_is_type_reftype(array_type->elem_type)) {
  2620. array_elem.gc_obj = POP_REF();
  2621. }
  2622. else if (array_type->elem_type == VALUE_TYPE_I32
  2623. || array_type->elem_type == VALUE_TYPE_F32
  2624. || array_type->elem_type == PACKED_TYPE_I8
  2625. || array_type->elem_type
  2626. == PACKED_TYPE_I16) {
  2627. array_elem.i32 = POP_I32();
  2628. }
  2629. else {
  2630. array_elem.i64 = POP_I64();
  2631. }
  2632. }
  2633. SYNC_ALL_TO_FRAME();
  2634. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2635. array_len, &array_elem);
  2636. if (!array_obj) {
  2637. wasm_set_exception(module,
  2638. "create array object failed");
  2639. goto got_exception;
  2640. }
  2641. if (opcode == WASM_OP_ARRAY_NEW_FIXED) {
  2642. for (i = 0; i < array_len; i++) {
  2643. if (wasm_is_type_reftype(
  2644. array_type->elem_type)) {
  2645. array_elem.gc_obj = POP_REF();
  2646. }
  2647. else if (array_type->elem_type == VALUE_TYPE_I32
  2648. || array_type->elem_type
  2649. == VALUE_TYPE_F32
  2650. || array_type->elem_type
  2651. == PACKED_TYPE_I8
  2652. || array_type->elem_type
  2653. == PACKED_TYPE_I16) {
  2654. array_elem.i32 = POP_I32();
  2655. }
  2656. else {
  2657. array_elem.i64 = POP_I64();
  2658. }
  2659. wasm_array_obj_set_elem(
  2660. array_obj, array_len - 1 - i, &array_elem);
  2661. }
  2662. }
  2663. PUSH_REF(array_obj);
  2664. HANDLE_OP_END();
  2665. }
  2666. case WASM_OP_ARRAY_NEW_DATA:
  2667. {
  2668. WASMModule *wasm_module = module->module;
  2669. WASMArrayType *array_type;
  2670. WASMRttType *rtt_type;
  2671. WASMValue array_elem = { 0 };
  2672. WASMDataSeg *data_seg;
  2673. uint8 *array_elem_base;
  2674. uint32 array_len, data_seg_idx, data_seg_offset;
  2675. uint32 elem_size = 0;
  2676. uint64 total_size;
  2677. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2678. read_leb_uint32(frame_ip, frame_ip_end, data_seg_idx);
  2679. data_seg = wasm_module->data_segments[data_seg_idx];
  2680. array_type =
  2681. (WASMArrayType *)wasm_module->types[type_index];
  2682. if (!(rtt_type = wasm_rtt_type_new(
  2683. (WASMType *)array_type, type_index,
  2684. wasm_module->rtt_types,
  2685. wasm_module->type_count,
  2686. &wasm_module->rtt_type_lock))) {
  2687. wasm_set_exception(module,
  2688. "create rtt type failed");
  2689. goto got_exception;
  2690. }
  2691. array_len = POP_I32();
  2692. data_seg_offset = POP_I32();
  2693. switch (array_type->elem_type) {
  2694. case PACKED_TYPE_I8:
  2695. elem_size = 1;
  2696. break;
  2697. case PACKED_TYPE_I16:
  2698. elem_size = 2;
  2699. break;
  2700. case VALUE_TYPE_I32:
  2701. case VALUE_TYPE_F32:
  2702. elem_size = 4;
  2703. break;
  2704. case VALUE_TYPE_I64:
  2705. case VALUE_TYPE_F64:
  2706. elem_size = 8;
  2707. break;
  2708. default:
  2709. bh_assert(0);
  2710. }
  2711. total_size = (uint64)elem_size * array_len;
  2712. if (data_seg_offset >= data_seg->data_length
  2713. || total_size
  2714. > data_seg->data_length - data_seg_offset) {
  2715. wasm_set_exception(module,
  2716. "data segment out of bounds");
  2717. goto got_exception;
  2718. }
  2719. SYNC_ALL_TO_FRAME();
  2720. array_obj = wasm_array_obj_new(exec_env, rtt_type,
  2721. array_len, &array_elem);
  2722. if (!array_obj) {
  2723. wasm_set_exception(module,
  2724. "create array object failed");
  2725. goto got_exception;
  2726. }
  2727. array_elem_base =
  2728. (uint8 *)wasm_array_obj_first_elem_addr(array_obj);
  2729. bh_memcpy_s(array_elem_base, (uint32)total_size,
  2730. data_seg->data + data_seg_offset,
  2731. (uint32)total_size);
  2732. PUSH_REF(array_obj);
  2733. HANDLE_OP_END();
  2734. }
  2735. case WASM_OP_ARRAY_NEW_ELEM:
  2736. {
  2737. /* TODO */
  2738. wasm_set_exception(module, "unsupported opcode");
  2739. goto got_exception;
  2740. }
  2741. case WASM_OP_ARRAY_GET:
  2742. case WASM_OP_ARRAY_GET_S:
  2743. case WASM_OP_ARRAY_GET_U:
  2744. {
  2745. WASMArrayType *array_type;
  2746. WASMValue array_elem = { 0 };
  2747. uint32 elem_idx, elem_size_log;
  2748. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2749. array_type =
  2750. (WASMArrayType *)module->module->types[type_index];
  2751. elem_idx = POP_I32();
  2752. array_obj = POP_REF();
  2753. if (!array_obj) {
  2754. wasm_set_exception(module, "null array reference");
  2755. goto got_exception;
  2756. }
  2757. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2758. wasm_set_exception(module,
  2759. "out of bounds array access");
  2760. goto got_exception;
  2761. }
  2762. wasm_array_obj_get_elem(
  2763. array_obj, elem_idx,
  2764. opcode == WASM_OP_ARRAY_GET_S ? true : false,
  2765. &array_elem);
  2766. elem_size_log = wasm_array_obj_elem_size_log(array_obj);
  2767. if (wasm_is_reftype_i31ref(array_type->elem_type)) {
  2768. PUSH_I31REF(array_elem.gc_obj);
  2769. }
  2770. else if (wasm_is_type_reftype(array_type->elem_type)) {
  2771. PUSH_REF(array_elem.gc_obj);
  2772. }
  2773. else if (elem_size_log < 3) {
  2774. PUSH_I32(array_elem.i32);
  2775. }
  2776. else {
  2777. PUSH_I64(array_elem.i64);
  2778. }
  2779. HANDLE_OP_END();
  2780. }
  2781. case WASM_OP_ARRAY_SET:
  2782. {
  2783. WASMArrayType *array_type;
  2784. WASMValue array_elem = { 0 };
  2785. uint32 elem_idx;
  2786. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2787. array_type =
  2788. (WASMArrayType *)module->module->types[type_index];
  2789. if (wasm_is_type_reftype(array_type->elem_type)) {
  2790. array_elem.gc_obj = POP_REF();
  2791. }
  2792. else if (array_type->elem_type == VALUE_TYPE_I32
  2793. || array_type->elem_type == VALUE_TYPE_F32
  2794. || array_type->elem_type == PACKED_TYPE_I8
  2795. || array_type->elem_type == PACKED_TYPE_I16) {
  2796. array_elem.i32 = POP_I32();
  2797. }
  2798. else {
  2799. array_elem.i64 = POP_I64();
  2800. }
  2801. elem_idx = POP_I32();
  2802. array_obj = POP_REF();
  2803. if (!array_obj) {
  2804. wasm_set_exception(module, "null array reference");
  2805. goto got_exception;
  2806. }
  2807. if (elem_idx >= wasm_array_obj_length(array_obj)) {
  2808. wasm_set_exception(module,
  2809. "out of bounds array access");
  2810. goto got_exception;
  2811. }
  2812. wasm_array_obj_set_elem(array_obj, elem_idx,
  2813. &array_elem);
  2814. HANDLE_OP_END();
  2815. }
  2816. case WASM_OP_ARRAY_LEN:
  2817. {
  2818. uint32 array_len;
  2819. array_obj = POP_REF();
  2820. if (!array_obj) {
  2821. wasm_set_exception(module, "null array reference");
  2822. goto got_exception;
  2823. }
  2824. array_len = wasm_array_obj_length(array_obj);
  2825. PUSH_I32(array_len);
  2826. HANDLE_OP_END();
  2827. }
  2828. case WASM_OP_ARRAY_FILL:
  2829. {
  2830. WASMArrayType *array_type;
  2831. WASMValue fill_value = { 0 };
  2832. uint32 start_offset, len;
  2833. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2834. array_type =
  2835. (WASMArrayType *)module->module->types[type_index];
  2836. len = POP_I32();
  2837. if (wasm_is_type_reftype(array_type->elem_type)) {
  2838. fill_value.gc_obj = POP_REF();
  2839. }
  2840. else if (array_type->elem_type == VALUE_TYPE_I32
  2841. || array_type->elem_type == VALUE_TYPE_F32
  2842. || array_type->elem_type == PACKED_TYPE_I8
  2843. || array_type->elem_type == PACKED_TYPE_I16) {
  2844. fill_value.i32 = POP_I32();
  2845. }
  2846. else {
  2847. fill_value.i64 = POP_I64();
  2848. }
  2849. start_offset = POP_I32();
  2850. array_obj = POP_REF();
  2851. if (!array_obj) {
  2852. wasm_set_exception(module, "null array reference");
  2853. goto got_exception;
  2854. }
  2855. if (len > 0) {
  2856. if ((uint64)start_offset + len
  2857. >= wasm_array_obj_length(array_obj)) {
  2858. wasm_set_exception(
  2859. module, "out of bounds array access");
  2860. goto got_exception;
  2861. }
  2862. wasm_array_obj_fill(array_obj, start_offset, len,
  2863. &fill_value);
  2864. }
  2865. HANDLE_OP_END();
  2866. }
  2867. case WASM_OP_ARRAY_COPY:
  2868. {
  2869. uint32 dst_offset, src_offset, len, src_type_index;
  2870. WASMArrayObjectRef src_obj, dst_obj;
  2871. read_leb_uint32(frame_ip, frame_ip_end, type_index);
  2872. read_leb_uint32(frame_ip, frame_ip_end, src_type_index);
  2873. len = POP_I32();
  2874. src_offset = POP_I32();
  2875. src_obj = POP_REF();
  2876. dst_offset = POP_I32();
  2877. dst_obj = POP_REF();
  2878. if (!src_obj || !dst_obj) {
  2879. wasm_set_exception(module, "null array reference");
  2880. goto got_exception;
  2881. }
  2882. if (len > 0) {
  2883. if ((dst_offset > UINT32_MAX - len)
  2884. || (dst_offset + len
  2885. > wasm_array_obj_length(dst_obj))
  2886. || (src_offset > UINT32_MAX - len)
  2887. || (src_offset + len
  2888. > wasm_array_obj_length(src_obj))) {
  2889. wasm_set_exception(
  2890. module, "out of bounds array access");
  2891. goto got_exception;
  2892. }
  2893. wasm_array_obj_copy(dst_obj, dst_offset, src_obj,
  2894. src_offset, len);
  2895. }
  2896. (void)src_type_index;
  2897. HANDLE_OP_END();
  2898. }
  2899. case WASM_OP_REF_I31:
  2900. {
  2901. uint32 i31_val;
  2902. i31_val = POP_I32();
  2903. i31_obj = wasm_i31_obj_new(i31_val);
  2904. PUSH_I31REF(i31_obj);
  2905. HANDLE_OP_END();
  2906. }
  2907. case WASM_OP_I31_GET_S:
  2908. case WASM_OP_I31_GET_U:
  2909. {
  2910. uint32 i31_val;
  2911. i31_obj = (WASMI31ObjectRef)POP_REF();
  2912. if (!i31_obj) {
  2913. wasm_set_exception(module, "null i31 reference");
  2914. goto got_exception;
  2915. }
  2916. i31_val = (uint32)(((uintptr_t)i31_obj) >> 1);
  2917. if (opcode == WASM_OP_I31_GET_S
  2918. && (i31_val & 0x40000000) /* bit 30 is 1 */)
  2919. /* set bit 31 to 1 */
  2920. i31_val |= 0x80000000;
  2921. PUSH_I32(i31_val);
  2922. HANDLE_OP_END();
  2923. }
  2924. case WASM_OP_REF_TEST:
  2925. case WASM_OP_REF_CAST:
  2926. case WASM_OP_REF_TEST_NULLABLE:
  2927. case WASM_OP_REF_CAST_NULLABLE:
  2928. {
  2929. int32 heap_type;
  2930. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2931. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2932. if (!gc_obj) {
  2933. if (opcode == WASM_OP_REF_TEST
  2934. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2935. (void)POP_REF();
  2936. if (opcode == WASM_OP_REF_TEST)
  2937. PUSH_I32(0);
  2938. else
  2939. PUSH_I32(1);
  2940. }
  2941. else if (opcode == WASM_OP_REF_CAST) {
  2942. wasm_set_exception(module, "cast failure");
  2943. goto got_exception;
  2944. }
  2945. else {
  2946. /* Do nothing for WASM_OP_REF_CAST_NULLABLE */
  2947. }
  2948. }
  2949. else {
  2950. bool castable = false;
  2951. if (heap_type >= 0) {
  2952. WASMModule *wasm_module = module->module;
  2953. castable = wasm_obj_is_instance_of(
  2954. gc_obj, (uint32)heap_type,
  2955. wasm_module->types,
  2956. wasm_module->type_count);
  2957. }
  2958. else {
  2959. castable =
  2960. wasm_obj_is_type_of(gc_obj, heap_type);
  2961. }
  2962. if (opcode == WASM_OP_REF_TEST
  2963. || opcode == WASM_OP_REF_TEST_NULLABLE) {
  2964. (void)POP_REF();
  2965. if (castable)
  2966. PUSH_I32(1);
  2967. else
  2968. PUSH_I32(0);
  2969. }
  2970. else if (!castable) {
  2971. wasm_set_exception(module, "cast failure");
  2972. goto got_exception;
  2973. }
  2974. }
  2975. HANDLE_OP_END();
  2976. }
  2977. case WASM_OP_BR_ON_CAST:
  2978. case WASM_OP_BR_ON_CAST_FAIL:
  2979. {
  2980. int32 heap_type, heap_type_dst;
  2981. uint8 castflags;
  2982. #if WASM_ENABLE_THREAD_MGR != 0
  2983. CHECK_SUSPEND_FLAGS();
  2984. #endif
  2985. castflags = *frame_ip++;
  2986. read_leb_uint32(frame_ip, frame_ip_end, depth);
  2987. read_leb_int32(frame_ip, frame_ip_end, heap_type);
  2988. read_leb_int32(frame_ip, frame_ip_end, heap_type_dst);
  2989. gc_obj = GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM);
  2990. if (!gc_obj) {
  2991. /*
  2992. * castflags should be 0~3:
  2993. * 0: (non-null, non-null)
  2994. * 1: (null, non-null)
  2995. * 2: (non-null, null)
  2996. * 3: (null, null)
  2997. */
  2998. if (
  2999. /* op is BR_ON_CAST and dst reftype is nullable
  3000. */
  3001. ((opcode1 == WASM_OP_BR_ON_CAST)
  3002. && ((castflags == 2) || (castflags == 3)))
  3003. /* op is BR_ON_CAST_FAIL and dst reftype is
  3004. non-nullable */
  3005. || ((opcode1 == WASM_OP_BR_ON_CAST_FAIL)
  3006. && ((castflags == 0) || (castflags == 1))))
  3007. goto label_pop_csp_n;
  3008. }
  3009. else {
  3010. bool castable = false;
  3011. if (heap_type_dst >= 0) {
  3012. WASMModule *wasm_module = module->module;
  3013. castable = wasm_obj_is_instance_of(
  3014. gc_obj, (uint32)heap_type_dst,
  3015. wasm_module->types,
  3016. wasm_module->type_count);
  3017. }
  3018. else {
  3019. castable =
  3020. wasm_obj_is_type_of(gc_obj, heap_type_dst);
  3021. }
  3022. if ((castable && (opcode == WASM_OP_BR_ON_CAST))
  3023. || (!castable
  3024. && (opcode == WASM_OP_BR_ON_CAST_FAIL))) {
  3025. goto label_pop_csp_n;
  3026. }
  3027. }
  3028. (void)heap_type;
  3029. HANDLE_OP_END();
  3030. }
  3031. case WASM_OP_ANY_CONVERT_EXTERN:
  3032. {
  3033. externref_obj = POP_REF();
  3034. if (externref_obj == NULL_REF)
  3035. PUSH_REF(NULL_REF);
  3036. else {
  3037. gc_obj = wasm_externref_obj_to_internal_obj(
  3038. externref_obj);
  3039. PUSH_REF(gc_obj);
  3040. }
  3041. HANDLE_OP_END();
  3042. }
  3043. case WASM_OP_EXTERN_CONVERT_ANY:
  3044. {
  3045. gc_obj = POP_REF();
  3046. if (gc_obj == NULL_REF)
  3047. PUSH_REF(NULL_REF);
  3048. else {
  3049. if (!(externref_obj =
  3050. wasm_internal_obj_to_externref_obj(
  3051. exec_env, gc_obj))) {
  3052. wasm_set_exception(
  3053. module, "create externref object failed");
  3054. goto got_exception;
  3055. }
  3056. PUSH_REF(externref_obj);
  3057. }
  3058. HANDLE_OP_END();
  3059. }
  3060. #if WASM_ENABLE_STRINGREF != 0
  3061. case WASM_OP_STRING_NEW_UTF8:
  3062. case WASM_OP_STRING_NEW_WTF16:
  3063. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  3064. case WASM_OP_STRING_NEW_WTF8:
  3065. {
  3066. uint32 mem_idx, addr, bytes_length, offset = 0;
  3067. EncodingFlag flag = WTF8;
  3068. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3069. bytes_length = POP_I32();
  3070. addr = POP_I32();
  3071. CHECK_MEMORY_OVERFLOW(bytes_length);
  3072. if (opcode == WASM_OP_STRING_NEW_WTF16) {
  3073. flag = WTF16;
  3074. }
  3075. else if (opcode == WASM_OP_STRING_NEW_UTF8) {
  3076. flag = UTF8;
  3077. }
  3078. else if (opcode == WASM_OP_STRING_NEW_LOSSY_UTF8) {
  3079. flag = LOSSY_UTF8;
  3080. }
  3081. else if (opcode == WASM_OP_STRING_NEW_WTF8) {
  3082. flag = WTF8;
  3083. }
  3084. str_obj = wasm_string_new_with_encoding(
  3085. maddr, bytes_length, flag);
  3086. if (!str_obj) {
  3087. wasm_set_exception(module,
  3088. "create string object failed");
  3089. goto got_exception;
  3090. }
  3091. SYNC_ALL_TO_FRAME();
  3092. stringref_obj =
  3093. wasm_stringref_obj_new(exec_env, str_obj);
  3094. if (!stringref_obj) {
  3095. wasm_set_exception(module,
  3096. "create stringref failed");
  3097. goto got_exception;
  3098. }
  3099. PUSH_REF(stringref_obj);
  3100. (void)mem_idx;
  3101. HANDLE_OP_END();
  3102. }
  3103. case WASM_OP_STRING_CONST:
  3104. {
  3105. WASMModule *wasm_module = module->module;
  3106. uint32 contents;
  3107. read_leb_uint32(frame_ip, frame_ip_end, contents);
  3108. str_obj = wasm_string_new_const(
  3109. (const char *)
  3110. wasm_module->string_literal_ptrs[contents],
  3111. wasm_module->string_literal_lengths[contents]);
  3112. if (!str_obj) {
  3113. wasm_set_exception(module,
  3114. "create string object failed");
  3115. goto got_exception;
  3116. }
  3117. SYNC_ALL_TO_FRAME();
  3118. stringref_obj =
  3119. wasm_stringref_obj_new(exec_env, str_obj);
  3120. if (!stringref_obj) {
  3121. wasm_set_exception(module,
  3122. "create stringref failed");
  3123. goto got_exception;
  3124. }
  3125. PUSH_REF(stringref_obj);
  3126. HANDLE_OP_END();
  3127. }
  3128. case WASM_OP_STRING_MEASURE_UTF8:
  3129. case WASM_OP_STRING_MEASURE_WTF8:
  3130. case WASM_OP_STRING_MEASURE_WTF16:
  3131. {
  3132. int32 target_bytes_length;
  3133. EncodingFlag flag = WTF8;
  3134. stringref_obj = POP_REF();
  3135. if (opcode == WASM_OP_STRING_MEASURE_WTF16) {
  3136. flag = WTF16;
  3137. }
  3138. else if (opcode == WASM_OP_STRING_MEASURE_UTF8) {
  3139. flag = UTF8;
  3140. }
  3141. else if (opcode == WASM_OP_STRING_MEASURE_WTF8) {
  3142. flag = LOSSY_UTF8;
  3143. }
  3144. target_bytes_length = wasm_string_measure(
  3145. (WASMString)wasm_stringref_obj_get_value(
  3146. stringref_obj),
  3147. flag);
  3148. PUSH_I32(target_bytes_length);
  3149. HANDLE_OP_END();
  3150. }
  3151. case WASM_OP_STRING_ENCODE_UTF8:
  3152. case WASM_OP_STRING_ENCODE_WTF16:
  3153. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  3154. case WASM_OP_STRING_ENCODE_WTF8:
  3155. {
  3156. uint32 mem_idx, addr;
  3157. int32 target_bytes_length;
  3158. WASMMemoryInstance *memory_inst;
  3159. EncodingFlag flag = WTF8;
  3160. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3161. addr = POP_I32();
  3162. stringref_obj = POP_REF();
  3163. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3164. stringref_obj);
  3165. #if WASM_ENABLE_SHARED_HEAP != 0
  3166. if (app_addr_in_shared_heap((uint64)addr, 1))
  3167. shared_heap_addr_app_to_native((uint64)addr, maddr);
  3168. else
  3169. #endif
  3170. {
  3171. memory_inst = module->memories[mem_idx];
  3172. maddr = memory_inst->memory_data + addr;
  3173. }
  3174. if (opcode == WASM_OP_STRING_ENCODE_WTF16) {
  3175. flag = WTF16;
  3176. count = wasm_string_measure(str_obj, flag);
  3177. target_bytes_length = wasm_string_encode(
  3178. str_obj, 0, count, maddr, NULL, flag);
  3179. }
  3180. else {
  3181. if (opcode == WASM_OP_STRING_ENCODE_UTF8) {
  3182. flag = UTF8;
  3183. }
  3184. else if (opcode
  3185. == WASM_OP_STRING_ENCODE_LOSSY_UTF8) {
  3186. flag = LOSSY_UTF8;
  3187. }
  3188. else if (opcode == WASM_OP_STRING_ENCODE_WTF8) {
  3189. flag = WTF8;
  3190. }
  3191. count = wasm_string_measure(str_obj, flag);
  3192. target_bytes_length = wasm_string_encode(
  3193. str_obj, 0, count, maddr, NULL, flag);
  3194. if (target_bytes_length == -1) {
  3195. wasm_set_exception(
  3196. module, "isolated surrogate is seen");
  3197. goto got_exception;
  3198. }
  3199. }
  3200. if (target_bytes_length < 0) {
  3201. wasm_set_exception(module,
  3202. "stringref encode failed");
  3203. goto got_exception;
  3204. }
  3205. PUSH_I32(target_bytes_length);
  3206. HANDLE_OP_END();
  3207. }
  3208. case WASM_OP_STRING_CONCAT:
  3209. {
  3210. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3211. stringref_obj2 = POP_REF();
  3212. stringref_obj1 = POP_REF();
  3213. str_obj = wasm_string_concat(
  3214. (WASMString)wasm_stringref_obj_get_value(
  3215. stringref_obj1),
  3216. (WASMString)wasm_stringref_obj_get_value(
  3217. stringref_obj2));
  3218. if (!str_obj) {
  3219. wasm_set_exception(module,
  3220. "create string object failed");
  3221. goto got_exception;
  3222. }
  3223. SYNC_ALL_TO_FRAME();
  3224. stringref_obj =
  3225. wasm_stringref_obj_new(exec_env, str_obj);
  3226. if (!stringref_obj) {
  3227. wasm_set_exception(module,
  3228. "create stringref failed");
  3229. goto got_exception;
  3230. }
  3231. PUSH_REF(stringref_obj);
  3232. HANDLE_OP_END();
  3233. }
  3234. case WASM_OP_STRING_EQ:
  3235. {
  3236. WASMStringrefObjectRef stringref_obj1, stringref_obj2;
  3237. int32 is_eq;
  3238. stringref_obj2 = POP_REF();
  3239. stringref_obj1 = POP_REF();
  3240. is_eq = wasm_string_eq(
  3241. (WASMString)wasm_stringref_obj_get_value(
  3242. stringref_obj1),
  3243. (WASMString)wasm_stringref_obj_get_value(
  3244. stringref_obj2));
  3245. PUSH_I32(is_eq);
  3246. HANDLE_OP_END();
  3247. }
  3248. case WASM_OP_STRING_IS_USV_SEQUENCE:
  3249. {
  3250. int32 is_usv_sequence;
  3251. stringref_obj = POP_REF();
  3252. is_usv_sequence = wasm_string_is_usv_sequence(
  3253. (WASMString)wasm_stringref_obj_get_value(
  3254. stringref_obj));
  3255. PUSH_I32(is_usv_sequence);
  3256. HANDLE_OP_END();
  3257. }
  3258. case WASM_OP_STRING_AS_WTF8:
  3259. {
  3260. stringref_obj = POP_REF();
  3261. str_obj = wasm_string_create_view(
  3262. (WASMString)wasm_stringref_obj_get_value(
  3263. stringref_obj),
  3264. STRING_VIEW_WTF8);
  3265. if (!str_obj) {
  3266. wasm_set_exception(module,
  3267. "create string object failed");
  3268. goto got_exception;
  3269. }
  3270. SYNC_ALL_TO_FRAME();
  3271. stringview_wtf8_obj =
  3272. wasm_stringview_wtf8_obj_new(exec_env, str_obj);
  3273. if (!stringview_wtf8_obj) {
  3274. wasm_set_exception(module,
  3275. "create stringview wtf8 failed");
  3276. goto got_exception;
  3277. }
  3278. PUSH_REF(stringview_wtf8_obj);
  3279. HANDLE_OP_END();
  3280. }
  3281. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  3282. {
  3283. uint32 next_pos, bytes, pos;
  3284. bytes = POP_I32();
  3285. pos = POP_I32();
  3286. stringview_wtf8_obj = POP_REF();
  3287. next_pos = wasm_string_advance(
  3288. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3289. stringview_wtf8_obj),
  3290. pos, bytes, NULL);
  3291. PUSH_I32(next_pos);
  3292. HANDLE_OP_END();
  3293. }
  3294. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  3295. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  3296. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  3297. {
  3298. uint32 mem_idx, addr, pos, bytes, next_pos;
  3299. int32 bytes_written;
  3300. WASMMemoryInstance *memory_inst;
  3301. EncodingFlag flag = WTF8;
  3302. if (opcode == WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8) {
  3303. flag = UTF8;
  3304. }
  3305. else if (opcode
  3306. == WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8) {
  3307. flag = LOSSY_UTF8;
  3308. }
  3309. else if (opcode
  3310. == WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8) {
  3311. flag = WTF8;
  3312. }
  3313. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3314. bytes = POP_I32();
  3315. pos = POP_I32();
  3316. addr = POP_I32();
  3317. stringview_wtf8_obj = POP_REF();
  3318. #if WASM_ENABLE_SHARED_HEAP != 0
  3319. if (app_addr_in_shared_heap((uint64)addr, 1))
  3320. shared_heap_addr_app_to_native((uint64)addr, maddr);
  3321. else
  3322. #endif
  3323. {
  3324. memory_inst = module->memories[mem_idx];
  3325. maddr = memory_inst->memory_data + addr;
  3326. }
  3327. bytes_written = wasm_string_encode(
  3328. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3329. stringview_wtf8_obj),
  3330. pos, bytes, maddr, &next_pos, flag);
  3331. if (bytes_written < 0) {
  3332. if (bytes_written == Isolated_Surrogate) {
  3333. wasm_set_exception(
  3334. module, "isolated surrogate is seen");
  3335. }
  3336. else {
  3337. wasm_set_exception(module, "encode failed");
  3338. }
  3339. goto got_exception;
  3340. }
  3341. PUSH_I32(next_pos);
  3342. PUSH_I32(bytes_written);
  3343. HANDLE_OP_END();
  3344. }
  3345. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  3346. {
  3347. uint32 start, end;
  3348. end = POP_I32();
  3349. start = POP_I32();
  3350. stringview_wtf8_obj = POP_REF();
  3351. str_obj = wasm_string_slice(
  3352. (WASMString)wasm_stringview_wtf8_obj_get_value(
  3353. stringview_wtf8_obj),
  3354. start, end, STRING_VIEW_WTF8);
  3355. if (!str_obj) {
  3356. wasm_set_exception(module,
  3357. "create string object failed");
  3358. goto got_exception;
  3359. }
  3360. SYNC_ALL_TO_FRAME();
  3361. stringref_obj =
  3362. wasm_stringref_obj_new(exec_env, str_obj);
  3363. if (!stringref_obj) {
  3364. wasm_set_exception(module,
  3365. "create stringref failed");
  3366. goto got_exception;
  3367. }
  3368. PUSH_REF(stringref_obj);
  3369. HANDLE_OP_END();
  3370. }
  3371. case WASM_OP_STRING_AS_WTF16:
  3372. {
  3373. stringref_obj = POP_REF();
  3374. str_obj = wasm_string_create_view(
  3375. (WASMString)wasm_stringref_obj_get_value(
  3376. stringref_obj),
  3377. STRING_VIEW_WTF16);
  3378. if (!str_obj) {
  3379. wasm_set_exception(module,
  3380. "create string object failed");
  3381. goto got_exception;
  3382. }
  3383. SYNC_ALL_TO_FRAME();
  3384. stringview_wtf16_obj =
  3385. wasm_stringview_wtf16_obj_new(exec_env, str_obj);
  3386. if (!stringview_wtf16_obj) {
  3387. wasm_set_exception(
  3388. module, "create stringview wtf16 failed");
  3389. goto got_exception;
  3390. }
  3391. PUSH_REF(stringview_wtf16_obj);
  3392. HANDLE_OP_END();
  3393. }
  3394. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  3395. {
  3396. int32 code_units_length;
  3397. stringview_wtf16_obj = POP_REF();
  3398. code_units_length = wasm_string_wtf16_get_length(
  3399. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3400. stringview_wtf16_obj));
  3401. PUSH_I32(code_units_length);
  3402. HANDLE_OP_END();
  3403. }
  3404. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  3405. {
  3406. int32 pos;
  3407. uint32 code_unit;
  3408. pos = POP_I32();
  3409. stringview_wtf16_obj = POP_REF();
  3410. code_unit = (uint32)wasm_string_get_wtf16_codeunit(
  3411. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3412. stringview_wtf16_obj),
  3413. pos);
  3414. PUSH_I32(code_unit);
  3415. HANDLE_OP_END();
  3416. }
  3417. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  3418. {
  3419. uint32 mem_idx, addr, pos, len, offset = 0;
  3420. int32 written_code_units = 0;
  3421. read_leb_uint32(frame_ip, frame_ip_end, mem_idx);
  3422. len = POP_I32();
  3423. pos = POP_I32();
  3424. addr = POP_I32();
  3425. stringview_wtf16_obj = POP_REF();
  3426. CHECK_MEMORY_OVERFLOW(len * sizeof(uint16));
  3427. /* check 2-byte alignment */
  3428. if (((uintptr_t)maddr & (((uintptr_t)1 << 2) - 1))
  3429. != 0) {
  3430. wasm_set_exception(module,
  3431. "unaligned memory access");
  3432. goto got_exception;
  3433. }
  3434. written_code_units = wasm_string_encode(
  3435. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3436. stringview_wtf16_obj),
  3437. pos, len, maddr, NULL, WTF16);
  3438. if (written_code_units < 0) {
  3439. wasm_set_exception(module, "encode failed");
  3440. goto got_exception;
  3441. }
  3442. PUSH_I32(written_code_units);
  3443. (void)mem_idx;
  3444. HANDLE_OP_END();
  3445. }
  3446. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  3447. {
  3448. uint32 start, end;
  3449. end = POP_I32();
  3450. start = POP_I32();
  3451. stringview_wtf16_obj = POP_REF();
  3452. str_obj = wasm_string_slice(
  3453. (WASMString)wasm_stringview_wtf16_obj_get_value(
  3454. stringview_wtf16_obj),
  3455. start, end, STRING_VIEW_WTF16);
  3456. if (!str_obj) {
  3457. wasm_set_exception(module,
  3458. "create string object failed");
  3459. goto got_exception;
  3460. }
  3461. SYNC_ALL_TO_FRAME();
  3462. stringref_obj =
  3463. wasm_stringref_obj_new(exec_env, str_obj);
  3464. if (!stringref_obj) {
  3465. wasm_set_exception(module,
  3466. "create stringref failed");
  3467. goto got_exception;
  3468. }
  3469. PUSH_REF(stringref_obj);
  3470. HANDLE_OP_END();
  3471. }
  3472. case WASM_OP_STRING_AS_ITER:
  3473. {
  3474. stringref_obj = POP_REF();
  3475. str_obj = wasm_string_create_view(
  3476. (WASMString)wasm_stringref_obj_get_value(
  3477. stringref_obj),
  3478. STRING_VIEW_ITER);
  3479. if (!str_obj) {
  3480. wasm_set_exception(module,
  3481. "create string object failed");
  3482. goto got_exception;
  3483. }
  3484. SYNC_ALL_TO_FRAME();
  3485. stringview_iter_obj =
  3486. wasm_stringview_iter_obj_new(exec_env, str_obj, 0);
  3487. if (!stringview_iter_obj) {
  3488. wasm_set_exception(module,
  3489. "create stringview iter failed");
  3490. goto got_exception;
  3491. }
  3492. PUSH_REF(stringview_iter_obj);
  3493. HANDLE_OP_END();
  3494. }
  3495. case WASM_OP_STRINGVIEW_ITER_NEXT:
  3496. {
  3497. uint32 code_point;
  3498. stringview_iter_obj = POP_REF();
  3499. code_point = wasm_string_next_codepoint(
  3500. (WASMString)wasm_stringview_iter_obj_get_value(
  3501. stringview_iter_obj),
  3502. wasm_stringview_iter_obj_get_pos(
  3503. stringview_iter_obj));
  3504. PUSH_I32(code_point);
  3505. HANDLE_OP_END();
  3506. }
  3507. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  3508. case WASM_OP_STRINGVIEW_ITER_REWIND:
  3509. {
  3510. uint32 code_points_count, code_points_consumed = 0,
  3511. cur_pos, next_pos = 0;
  3512. code_points_count = POP_I32();
  3513. stringview_iter_obj = POP_REF();
  3514. str_obj =
  3515. (WASMString)wasm_stringview_iter_obj_get_value(
  3516. stringview_iter_obj);
  3517. cur_pos = wasm_stringview_iter_obj_get_pos(
  3518. stringview_iter_obj);
  3519. if (opcode == WASM_OP_STRINGVIEW_ITER_ADVANCE) {
  3520. next_pos = wasm_string_advance(
  3521. str_obj, cur_pos, code_points_count,
  3522. &code_points_consumed);
  3523. }
  3524. else if (opcode == WASM_OP_STRINGVIEW_ITER_REWIND) {
  3525. next_pos = wasm_string_rewind(
  3526. str_obj, cur_pos, code_points_count,
  3527. &code_points_consumed);
  3528. }
  3529. wasm_stringview_iter_obj_update_pos(stringview_iter_obj,
  3530. next_pos);
  3531. PUSH_I32(code_points_consumed);
  3532. HANDLE_OP_END();
  3533. }
  3534. case WASM_OP_STRINGVIEW_ITER_SLICE:
  3535. {
  3536. uint32 code_points_count, cur_pos;
  3537. code_points_count = POP_I32();
  3538. stringview_iter_obj = POP_REF();
  3539. cur_pos = wasm_stringview_iter_obj_get_pos(
  3540. stringview_iter_obj);
  3541. str_obj = wasm_string_slice(
  3542. (WASMString)wasm_stringview_iter_obj_get_value(
  3543. stringview_iter_obj),
  3544. cur_pos, cur_pos + code_points_count,
  3545. STRING_VIEW_ITER);
  3546. if (!str_obj) {
  3547. wasm_set_exception(module,
  3548. "create string object failed");
  3549. goto got_exception;
  3550. }
  3551. SYNC_ALL_TO_FRAME();
  3552. stringref_obj =
  3553. wasm_stringref_obj_new(exec_env, str_obj);
  3554. if (!stringref_obj) {
  3555. wasm_set_exception(module,
  3556. "create stringref failed");
  3557. goto got_exception;
  3558. }
  3559. PUSH_REF(stringref_obj);
  3560. HANDLE_OP_END();
  3561. }
  3562. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  3563. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  3564. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  3565. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  3566. {
  3567. uint32 start, end, array_len;
  3568. EncodingFlag flag = WTF8;
  3569. WASMArrayType *array_type;
  3570. void *arr_start_addr;
  3571. end = POP_I32();
  3572. start = POP_I32();
  3573. array_obj = POP_REF();
  3574. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3575. (WASMObjectRef)array_obj);
  3576. arr_start_addr =
  3577. wasm_array_obj_elem_addr(array_obj, start);
  3578. array_len = wasm_array_obj_length(array_obj);
  3579. if (start > end || end > array_len) {
  3580. wasm_set_exception(module,
  3581. "out of bounds array access");
  3582. goto got_exception;
  3583. }
  3584. if (opcode == WASM_OP_STRING_NEW_WTF16_ARRAY) {
  3585. if (array_type->elem_type != VALUE_TYPE_I16) {
  3586. wasm_set_exception(module,
  3587. "array type mismatch");
  3588. goto got_exception;
  3589. }
  3590. flag = WTF16;
  3591. }
  3592. else {
  3593. if (array_type->elem_type != VALUE_TYPE_I8) {
  3594. wasm_set_exception(module,
  3595. "array type mismatch");
  3596. goto got_exception;
  3597. }
  3598. if (opcode == WASM_OP_STRING_NEW_UTF8_ARRAY) {
  3599. flag = UTF8;
  3600. }
  3601. else if (opcode == WASM_OP_STRING_NEW_WTF8_ARRAY) {
  3602. flag = WTF8;
  3603. }
  3604. else if (opcode
  3605. == WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY) {
  3606. flag = LOSSY_UTF8;
  3607. }
  3608. }
  3609. str_obj = wasm_string_new_with_encoding(
  3610. arr_start_addr, (end - start), flag);
  3611. if (!str_obj) {
  3612. wasm_set_exception(module,
  3613. "create string object failed");
  3614. goto got_exception;
  3615. }
  3616. SYNC_ALL_TO_FRAME();
  3617. stringref_obj =
  3618. wasm_stringref_obj_new(exec_env, str_obj);
  3619. if (!stringref_obj) {
  3620. wasm_set_exception(module,
  3621. "create stringref failed");
  3622. goto got_exception;
  3623. }
  3624. PUSH_REF(stringref_obj);
  3625. HANDLE_OP_END();
  3626. }
  3627. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  3628. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  3629. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  3630. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  3631. {
  3632. uint32 start, array_len, count;
  3633. int32 bytes_written;
  3634. EncodingFlag flag = WTF8;
  3635. WASMArrayType *array_type;
  3636. void *arr_start_addr;
  3637. start = POP_I32();
  3638. array_obj = POP_REF();
  3639. stringref_obj = POP_REF();
  3640. str_obj = (WASMString)wasm_stringref_obj_get_value(
  3641. stringref_obj);
  3642. array_type = (WASMArrayType *)wasm_obj_get_defined_type(
  3643. (WASMObjectRef)array_obj);
  3644. arr_start_addr =
  3645. wasm_array_obj_elem_addr(array_obj, start);
  3646. array_len = wasm_array_obj_length(array_obj);
  3647. if (start > array_len) {
  3648. wasm_set_exception(module,
  3649. "out of bounds array access");
  3650. goto got_exception;
  3651. }
  3652. if (opcode == WASM_OP_STRING_ENCODE_WTF16_ARRAY) {
  3653. if (array_type->elem_type != VALUE_TYPE_I16) {
  3654. wasm_set_exception(module,
  3655. "array type mismatch");
  3656. goto got_exception;
  3657. }
  3658. flag = WTF16;
  3659. }
  3660. else {
  3661. if (array_type->elem_type != VALUE_TYPE_I8) {
  3662. wasm_set_exception(module,
  3663. "array type mismatch");
  3664. goto got_exception;
  3665. }
  3666. if (opcode == WASM_OP_STRING_ENCODE_UTF8_ARRAY) {
  3667. flag = UTF8;
  3668. }
  3669. else if (opcode
  3670. == WASM_OP_STRING_ENCODE_WTF8_ARRAY) {
  3671. flag = WTF8;
  3672. }
  3673. else if (
  3674. opcode
  3675. == WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY) {
  3676. flag = LOSSY_UTF8;
  3677. }
  3678. }
  3679. count = wasm_string_measure(str_obj, flag);
  3680. bytes_written = wasm_string_encode(
  3681. str_obj, 0, count, arr_start_addr, NULL, flag);
  3682. if (bytes_written < 0) {
  3683. if (bytes_written == Isolated_Surrogate) {
  3684. wasm_set_exception(
  3685. module, "isolated surrogate is seen");
  3686. }
  3687. else if (bytes_written == Insufficient_Space) {
  3688. wasm_set_exception(
  3689. module, "array space is insufficient");
  3690. }
  3691. else {
  3692. wasm_set_exception(module, "encode failed");
  3693. }
  3694. goto got_exception;
  3695. }
  3696. PUSH_I32(bytes_written);
  3697. HANDLE_OP_END();
  3698. }
  3699. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  3700. default:
  3701. {
  3702. wasm_set_exception(module, "unsupported opcode");
  3703. goto got_exception;
  3704. }
  3705. }
  3706. }
  3707. #endif /* end of WASM_ENABLE_GC != 0 */
  3708. /* variable instructions */
  3709. HANDLE_OP(WASM_OP_GET_LOCAL)
  3710. {
  3711. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3712. switch (local_type) {
  3713. case VALUE_TYPE_I32:
  3714. case VALUE_TYPE_F32:
  3715. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3716. case VALUE_TYPE_FUNCREF:
  3717. case VALUE_TYPE_EXTERNREF:
  3718. #endif
  3719. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3720. break;
  3721. case VALUE_TYPE_I64:
  3722. case VALUE_TYPE_F64:
  3723. PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset));
  3724. break;
  3725. default:
  3726. #if WASM_ENABLE_GC != 0
  3727. if (wasm_is_type_reftype(local_type)) {
  3728. if (wasm_is_reftype_i31ref(local_type)) {
  3729. PUSH_I31REF(
  3730. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3731. }
  3732. else {
  3733. PUSH_REF(
  3734. GET_REF_FROM_ADDR(frame_lp + local_offset));
  3735. }
  3736. }
  3737. else
  3738. #endif
  3739. {
  3740. wasm_set_exception(module, "invalid local type");
  3741. goto got_exception;
  3742. }
  3743. }
  3744. HANDLE_OP_END();
  3745. }
  3746. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3747. {
  3748. local_offset = *frame_ip++;
  3749. if (local_offset & 0x80)
  3750. PUSH_I64(
  3751. GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F)));
  3752. else
  3753. PUSH_I32(*(int32 *)(frame_lp + local_offset));
  3754. HANDLE_OP_END();
  3755. }
  3756. HANDLE_OP(WASM_OP_SET_LOCAL)
  3757. {
  3758. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3759. switch (local_type) {
  3760. case VALUE_TYPE_I32:
  3761. case VALUE_TYPE_F32:
  3762. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3763. case VALUE_TYPE_FUNCREF:
  3764. case VALUE_TYPE_EXTERNREF:
  3765. #endif
  3766. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3767. break;
  3768. case VALUE_TYPE_I64:
  3769. case VALUE_TYPE_F64:
  3770. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3771. POP_I64());
  3772. break;
  3773. default:
  3774. #if WASM_ENABLE_GC != 0
  3775. if (wasm_is_type_reftype(local_type)) {
  3776. PUT_REF_TO_ADDR(frame_lp + local_offset, POP_REF());
  3777. }
  3778. else
  3779. #endif
  3780. {
  3781. wasm_set_exception(module, "invalid local type");
  3782. goto got_exception;
  3783. }
  3784. }
  3785. HANDLE_OP_END();
  3786. }
  3787. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  3788. {
  3789. local_offset = *frame_ip++;
  3790. if (local_offset & 0x80)
  3791. PUT_I64_TO_ADDR(
  3792. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3793. POP_I64());
  3794. else
  3795. *(int32 *)(frame_lp + local_offset) = POP_I32();
  3796. HANDLE_OP_END();
  3797. }
  3798. HANDLE_OP(WASM_OP_TEE_LOCAL)
  3799. {
  3800. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3801. switch (local_type) {
  3802. case VALUE_TYPE_I32:
  3803. case VALUE_TYPE_F32:
  3804. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  3805. case VALUE_TYPE_FUNCREF:
  3806. case VALUE_TYPE_EXTERNREF:
  3807. #endif
  3808. *(int32 *)(frame_lp + local_offset) =
  3809. *(int32 *)(frame_sp - 1);
  3810. break;
  3811. case VALUE_TYPE_I64:
  3812. case VALUE_TYPE_F64:
  3813. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  3814. GET_I64_FROM_ADDR(frame_sp - 2));
  3815. break;
  3816. default:
  3817. #if WASM_ENABLE_GC != 0
  3818. if (wasm_is_type_reftype(local_type)) {
  3819. PUT_REF_TO_ADDR(
  3820. frame_lp + local_offset,
  3821. GET_REF_FROM_ADDR(frame_sp - REF_CELL_NUM));
  3822. }
  3823. else
  3824. #endif
  3825. {
  3826. wasm_set_exception(module, "invalid local type");
  3827. goto got_exception;
  3828. }
  3829. }
  3830. HANDLE_OP_END();
  3831. }
  3832. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  3833. {
  3834. local_offset = *frame_ip++;
  3835. if (local_offset & 0x80)
  3836. PUT_I64_TO_ADDR(
  3837. (uint32 *)(frame_lp + (local_offset & 0x7F)),
  3838. GET_I64_FROM_ADDR(frame_sp - 2));
  3839. else
  3840. *(int32 *)(frame_lp + local_offset) =
  3841. *(int32 *)(frame_sp - 1);
  3842. HANDLE_OP_END();
  3843. }
  3844. HANDLE_OP(WASM_OP_GET_GLOBAL)
  3845. {
  3846. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3847. bh_assert(global_idx < module->e->global_count);
  3848. global = globals + global_idx;
  3849. global_addr = get_global_addr(global_data, global);
  3850. /* clang-format off */
  3851. #if WASM_ENABLE_GC == 0
  3852. PUSH_I32(*(uint32 *)global_addr);
  3853. #else
  3854. if (!wasm_is_type_reftype(global->type)) {
  3855. PUSH_I32(*(uint32 *)global_addr);
  3856. }
  3857. else if (wasm_is_reftype_i31ref(global->type)) {
  3858. PUSH_I31REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3859. }
  3860. else {
  3861. PUSH_REF(GET_REF_FROM_ADDR((uint32 *)global_addr));
  3862. }
  3863. #endif
  3864. /* clang-format on */
  3865. HANDLE_OP_END();
  3866. }
  3867. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  3868. {
  3869. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3870. bh_assert(global_idx < module->e->global_count);
  3871. global = globals + global_idx;
  3872. global_addr = get_global_addr(global_data, global);
  3873. PUSH_I64(GET_I64_FROM_ADDR((uint32 *)global_addr));
  3874. HANDLE_OP_END();
  3875. }
  3876. HANDLE_OP(WASM_OP_SET_GLOBAL)
  3877. {
  3878. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3879. bh_assert(global_idx < module->e->global_count);
  3880. global = globals + global_idx;
  3881. global_addr = get_global_addr(global_data, global);
  3882. /* clang-format off */
  3883. #if WASM_ENABLE_GC == 0
  3884. *(int32 *)global_addr = POP_I32();
  3885. #else
  3886. if (!wasm_is_type_reftype(global->type))
  3887. *(int32 *)global_addr = POP_I32();
  3888. else
  3889. PUT_REF_TO_ADDR((uint32 *)global_addr, POP_REF());
  3890. #endif
  3891. /* clang-format on */
  3892. HANDLE_OP_END();
  3893. }
  3894. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  3895. {
  3896. uint64 aux_stack_top;
  3897. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3898. bh_assert(global_idx < module->e->global_count);
  3899. global = globals + global_idx;
  3900. global_addr = get_global_addr(global_data, global);
  3901. #if WASM_ENABLE_MEMORY64 != 0
  3902. if (is_memory64) {
  3903. aux_stack_top = *(uint64 *)(frame_sp - 2);
  3904. }
  3905. else
  3906. #endif
  3907. {
  3908. aux_stack_top = (uint64)(*(uint32 *)(frame_sp - 1));
  3909. }
  3910. if (aux_stack_top <= (uint64)exec_env->aux_stack_boundary) {
  3911. wasm_set_exception(module, "wasm auxiliary stack overflow");
  3912. goto got_exception;
  3913. }
  3914. if (aux_stack_top > (uint64)exec_env->aux_stack_bottom) {
  3915. wasm_set_exception(module,
  3916. "wasm auxiliary stack underflow");
  3917. goto got_exception;
  3918. }
  3919. #if WASM_ENABLE_MEMORY64 != 0
  3920. if (is_memory64) {
  3921. *(uint64 *)global_addr = aux_stack_top;
  3922. frame_sp -= 2;
  3923. }
  3924. else
  3925. #endif
  3926. {
  3927. *(uint32 *)global_addr = (uint32)aux_stack_top;
  3928. frame_sp--;
  3929. }
  3930. #if WASM_ENABLE_MEMORY_PROFILING != 0
  3931. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  3932. uint32 aux_stack_used =
  3933. (uint32)(module->module->aux_stack_bottom
  3934. - *(uint32 *)global_addr);
  3935. if (aux_stack_used > module->e->max_aux_stack_used)
  3936. module->e->max_aux_stack_used = aux_stack_used;
  3937. }
  3938. #endif
  3939. HANDLE_OP_END();
  3940. }
  3941. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  3942. {
  3943. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  3944. bh_assert(global_idx < module->e->global_count);
  3945. global = globals + global_idx;
  3946. global_addr = get_global_addr(global_data, global);
  3947. PUT_I64_TO_ADDR((uint32 *)global_addr, POP_I64());
  3948. HANDLE_OP_END();
  3949. }
  3950. /* memory load instructions */
  3951. HANDLE_OP(WASM_OP_I32_LOAD)
  3952. HANDLE_OP(WASM_OP_F32_LOAD)
  3953. {
  3954. uint32 flags;
  3955. mem_offset_t offset, addr;
  3956. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3957. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3958. addr = POP_MEM_OFFSET();
  3959. CHECK_MEMORY_OVERFLOW(4);
  3960. PUSH_I32(LOAD_I32(maddr));
  3961. CHECK_READ_WATCHPOINT(addr, offset);
  3962. HANDLE_OP_END();
  3963. }
  3964. HANDLE_OP(WASM_OP_I64_LOAD)
  3965. HANDLE_OP(WASM_OP_F64_LOAD)
  3966. {
  3967. uint32 flags;
  3968. mem_offset_t offset, addr;
  3969. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3970. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3971. addr = POP_MEM_OFFSET();
  3972. CHECK_MEMORY_OVERFLOW(8);
  3973. PUSH_I64(LOAD_I64(maddr));
  3974. CHECK_READ_WATCHPOINT(addr, offset);
  3975. HANDLE_OP_END();
  3976. }
  3977. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  3978. {
  3979. uint32 flags;
  3980. mem_offset_t offset, addr;
  3981. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3982. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3983. addr = POP_MEM_OFFSET();
  3984. CHECK_MEMORY_OVERFLOW(1);
  3985. PUSH_I32(sign_ext_8_32(*(int8 *)maddr));
  3986. CHECK_READ_WATCHPOINT(addr, offset);
  3987. HANDLE_OP_END();
  3988. }
  3989. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  3990. {
  3991. uint32 flags;
  3992. mem_offset_t offset, addr;
  3993. read_leb_memarg(frame_ip, frame_ip_end, flags);
  3994. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  3995. addr = POP_MEM_OFFSET();
  3996. CHECK_MEMORY_OVERFLOW(1);
  3997. PUSH_I32((uint32)(*(uint8 *)maddr));
  3998. CHECK_READ_WATCHPOINT(addr, offset);
  3999. HANDLE_OP_END();
  4000. }
  4001. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  4002. {
  4003. uint32 flags;
  4004. mem_offset_t offset, addr;
  4005. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4006. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4007. addr = POP_MEM_OFFSET();
  4008. CHECK_MEMORY_OVERFLOW(2);
  4009. PUSH_I32(sign_ext_16_32(LOAD_I16(maddr)));
  4010. CHECK_READ_WATCHPOINT(addr, offset);
  4011. HANDLE_OP_END();
  4012. }
  4013. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  4014. {
  4015. uint32 flags;
  4016. mem_offset_t offset, addr;
  4017. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4018. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4019. addr = POP_MEM_OFFSET();
  4020. CHECK_MEMORY_OVERFLOW(2);
  4021. PUSH_I32((uint32)(LOAD_U16(maddr)));
  4022. CHECK_READ_WATCHPOINT(addr, offset);
  4023. HANDLE_OP_END();
  4024. }
  4025. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  4026. {
  4027. uint32 flags;
  4028. mem_offset_t offset, addr;
  4029. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4030. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4031. addr = POP_MEM_OFFSET();
  4032. CHECK_MEMORY_OVERFLOW(1);
  4033. PUSH_I64(sign_ext_8_64(*(int8 *)maddr));
  4034. CHECK_READ_WATCHPOINT(addr, offset);
  4035. HANDLE_OP_END();
  4036. }
  4037. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  4038. {
  4039. uint32 flags;
  4040. mem_offset_t offset, addr;
  4041. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4042. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4043. addr = POP_MEM_OFFSET();
  4044. CHECK_MEMORY_OVERFLOW(1);
  4045. PUSH_I64((uint64)(*(uint8 *)maddr));
  4046. CHECK_READ_WATCHPOINT(addr, offset);
  4047. HANDLE_OP_END();
  4048. }
  4049. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  4050. {
  4051. uint32 flags;
  4052. mem_offset_t offset, addr;
  4053. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4054. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4055. addr = POP_MEM_OFFSET();
  4056. CHECK_MEMORY_OVERFLOW(2);
  4057. PUSH_I64(sign_ext_16_64(LOAD_I16(maddr)));
  4058. CHECK_READ_WATCHPOINT(addr, offset);
  4059. HANDLE_OP_END();
  4060. }
  4061. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  4062. {
  4063. uint32 flags;
  4064. mem_offset_t offset, addr;
  4065. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4066. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4067. addr = POP_MEM_OFFSET();
  4068. CHECK_MEMORY_OVERFLOW(2);
  4069. PUSH_I64((uint64)(LOAD_U16(maddr)));
  4070. CHECK_READ_WATCHPOINT(addr, offset);
  4071. HANDLE_OP_END();
  4072. }
  4073. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  4074. {
  4075. uint32 flags;
  4076. mem_offset_t offset, addr;
  4077. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4078. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4079. addr = POP_MEM_OFFSET();
  4080. CHECK_MEMORY_OVERFLOW(4);
  4081. PUSH_I64(sign_ext_32_64(LOAD_I32(maddr)));
  4082. CHECK_READ_WATCHPOINT(addr, offset);
  4083. HANDLE_OP_END();
  4084. }
  4085. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  4086. {
  4087. uint32 flags;
  4088. mem_offset_t offset, addr;
  4089. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4090. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4091. addr = POP_MEM_OFFSET();
  4092. CHECK_MEMORY_OVERFLOW(4);
  4093. PUSH_I64((uint64)(LOAD_U32(maddr)));
  4094. CHECK_READ_WATCHPOINT(addr, offset);
  4095. HANDLE_OP_END();
  4096. }
  4097. /* memory store instructions */
  4098. HANDLE_OP(WASM_OP_I32_STORE)
  4099. HANDLE_OP(WASM_OP_F32_STORE)
  4100. {
  4101. uint32 flags;
  4102. mem_offset_t offset, addr;
  4103. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4104. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4105. frame_sp--;
  4106. addr = POP_MEM_OFFSET();
  4107. CHECK_MEMORY_OVERFLOW(4);
  4108. #if WASM_ENABLE_MEMORY64 != 0
  4109. if (is_memory64) {
  4110. STORE_U32(maddr, frame_sp[2]);
  4111. }
  4112. else
  4113. #endif
  4114. {
  4115. STORE_U32(maddr, frame_sp[1]);
  4116. }
  4117. CHECK_WRITE_WATCHPOINT(addr, offset);
  4118. HANDLE_OP_END();
  4119. }
  4120. HANDLE_OP(WASM_OP_I64_STORE)
  4121. HANDLE_OP(WASM_OP_F64_STORE)
  4122. {
  4123. uint32 flags;
  4124. mem_offset_t offset, addr;
  4125. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4126. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4127. frame_sp -= 2;
  4128. addr = POP_MEM_OFFSET();
  4129. CHECK_MEMORY_OVERFLOW(8);
  4130. #if WASM_ENABLE_MEMORY64 != 0
  4131. if (is_memory64) {
  4132. PUT_I64_TO_ADDR((mem_offset_t *)maddr,
  4133. GET_I64_FROM_ADDR(frame_sp + 2));
  4134. }
  4135. else
  4136. #endif
  4137. {
  4138. PUT_I64_TO_ADDR((uint32 *)maddr,
  4139. GET_I64_FROM_ADDR(frame_sp + 1));
  4140. }
  4141. CHECK_WRITE_WATCHPOINT(addr, offset);
  4142. HANDLE_OP_END();
  4143. }
  4144. HANDLE_OP(WASM_OP_I32_STORE8)
  4145. HANDLE_OP(WASM_OP_I32_STORE16)
  4146. {
  4147. uint32 flags;
  4148. mem_offset_t offset, addr;
  4149. uint32 sval;
  4150. opcode = *(frame_ip - 1);
  4151. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4152. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4153. sval = (uint32)POP_I32();
  4154. addr = POP_MEM_OFFSET();
  4155. if (opcode == WASM_OP_I32_STORE8) {
  4156. CHECK_MEMORY_OVERFLOW(1);
  4157. *(uint8 *)maddr = (uint8)sval;
  4158. }
  4159. else {
  4160. CHECK_MEMORY_OVERFLOW(2);
  4161. STORE_U16(maddr, (uint16)sval);
  4162. }
  4163. CHECK_WRITE_WATCHPOINT(addr, offset);
  4164. HANDLE_OP_END();
  4165. }
  4166. HANDLE_OP(WASM_OP_I64_STORE8)
  4167. HANDLE_OP(WASM_OP_I64_STORE16)
  4168. HANDLE_OP(WASM_OP_I64_STORE32)
  4169. {
  4170. uint32 flags;
  4171. mem_offset_t offset, addr;
  4172. uint64 sval;
  4173. opcode = *(frame_ip - 1);
  4174. read_leb_memarg(frame_ip, frame_ip_end, flags);
  4175. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  4176. sval = (uint64)POP_I64();
  4177. addr = POP_MEM_OFFSET();
  4178. if (opcode == WASM_OP_I64_STORE8) {
  4179. CHECK_MEMORY_OVERFLOW(1);
  4180. *(uint8 *)maddr = (uint8)sval;
  4181. }
  4182. else if (opcode == WASM_OP_I64_STORE16) {
  4183. CHECK_MEMORY_OVERFLOW(2);
  4184. STORE_U16(maddr, (uint16)sval);
  4185. }
  4186. else {
  4187. CHECK_MEMORY_OVERFLOW(4);
  4188. STORE_U32(maddr, (uint32)sval);
  4189. }
  4190. CHECK_WRITE_WATCHPOINT(addr, offset);
  4191. HANDLE_OP_END();
  4192. }
  4193. /* memory size and memory grow instructions */
  4194. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  4195. {
  4196. uint32 mem_idx;
  4197. read_leb_memidx(frame_ip, frame_ip_end, mem_idx);
  4198. PUSH_PAGE_COUNT(memory->cur_page_count);
  4199. HANDLE_OP_END();
  4200. }
  4201. HANDLE_OP(WASM_OP_MEMORY_GROW)
  4202. {
  4203. uint32 mem_idx, prev_page_count;
  4204. mem_offset_t delta;
  4205. read_leb_memidx(frame_ip, frame_ip_end, mem_idx);
  4206. prev_page_count = memory->cur_page_count;
  4207. delta = POP_PAGE_COUNT();
  4208. if (
  4209. #if WASM_ENABLE_MEMORY64 != 0
  4210. delta > UINT32_MAX ||
  4211. #endif
  4212. !wasm_enlarge_memory_with_idx(module, (uint32)delta,
  4213. mem_idx)) {
  4214. /* failed to memory.grow, return -1 */
  4215. PUSH_PAGE_COUNT(-1);
  4216. }
  4217. else {
  4218. /* success, return previous page count */
  4219. PUSH_PAGE_COUNT(prev_page_count);
  4220. /* update memory size, no need to update memory ptr as
  4221. it isn't changed in wasm_enlarge_memory */
  4222. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  4223. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  4224. || WASM_ENABLE_BULK_MEMORY != 0
  4225. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  4226. #endif
  4227. }
  4228. HANDLE_OP_END();
  4229. }
  4230. /* constant instructions */
  4231. HANDLE_OP(WASM_OP_I32_CONST)
  4232. DEF_OP_I_CONST(int32, I32);
  4233. HANDLE_OP_END();
  4234. HANDLE_OP(WASM_OP_I64_CONST)
  4235. DEF_OP_I_CONST(int64, I64);
  4236. HANDLE_OP_END();
  4237. HANDLE_OP(WASM_OP_F32_CONST)
  4238. {
  4239. uint8 *p_float = (uint8 *)frame_sp++;
  4240. for (i = 0; i < sizeof(float32); i++)
  4241. *p_float++ = *frame_ip++;
  4242. HANDLE_OP_END();
  4243. }
  4244. HANDLE_OP(WASM_OP_F64_CONST)
  4245. {
  4246. uint8 *p_float = (uint8 *)frame_sp++;
  4247. frame_sp++;
  4248. for (i = 0; i < sizeof(float64); i++)
  4249. *p_float++ = *frame_ip++;
  4250. HANDLE_OP_END();
  4251. }
  4252. /* comparison instructions of i32 */
  4253. HANDLE_OP(WASM_OP_I32_EQZ)
  4254. {
  4255. DEF_OP_EQZ(I32);
  4256. HANDLE_OP_END();
  4257. }
  4258. HANDLE_OP(WASM_OP_I32_EQ)
  4259. {
  4260. DEF_OP_CMP(uint32, I32, ==);
  4261. HANDLE_OP_END();
  4262. }
  4263. HANDLE_OP(WASM_OP_I32_NE)
  4264. {
  4265. DEF_OP_CMP(uint32, I32, !=);
  4266. HANDLE_OP_END();
  4267. }
  4268. HANDLE_OP(WASM_OP_I32_LT_S)
  4269. {
  4270. DEF_OP_CMP(int32, I32, <);
  4271. HANDLE_OP_END();
  4272. }
  4273. HANDLE_OP(WASM_OP_I32_LT_U)
  4274. {
  4275. DEF_OP_CMP(uint32, I32, <);
  4276. HANDLE_OP_END();
  4277. }
  4278. HANDLE_OP(WASM_OP_I32_GT_S)
  4279. {
  4280. DEF_OP_CMP(int32, I32, >);
  4281. HANDLE_OP_END();
  4282. }
  4283. HANDLE_OP(WASM_OP_I32_GT_U)
  4284. {
  4285. DEF_OP_CMP(uint32, I32, >);
  4286. HANDLE_OP_END();
  4287. }
  4288. HANDLE_OP(WASM_OP_I32_LE_S)
  4289. {
  4290. DEF_OP_CMP(int32, I32, <=);
  4291. HANDLE_OP_END();
  4292. }
  4293. HANDLE_OP(WASM_OP_I32_LE_U)
  4294. {
  4295. DEF_OP_CMP(uint32, I32, <=);
  4296. HANDLE_OP_END();
  4297. }
  4298. HANDLE_OP(WASM_OP_I32_GE_S)
  4299. {
  4300. DEF_OP_CMP(int32, I32, >=);
  4301. HANDLE_OP_END();
  4302. }
  4303. HANDLE_OP(WASM_OP_I32_GE_U)
  4304. {
  4305. DEF_OP_CMP(uint32, I32, >=);
  4306. HANDLE_OP_END();
  4307. }
  4308. /* comparison instructions of i64 */
  4309. HANDLE_OP(WASM_OP_I64_EQZ)
  4310. {
  4311. DEF_OP_EQZ(I64);
  4312. HANDLE_OP_END();
  4313. }
  4314. HANDLE_OP(WASM_OP_I64_EQ)
  4315. {
  4316. DEF_OP_CMP(uint64, I64, ==);
  4317. HANDLE_OP_END();
  4318. }
  4319. HANDLE_OP(WASM_OP_I64_NE)
  4320. {
  4321. DEF_OP_CMP(uint64, I64, !=);
  4322. HANDLE_OP_END();
  4323. }
  4324. HANDLE_OP(WASM_OP_I64_LT_S)
  4325. {
  4326. DEF_OP_CMP(int64, I64, <);
  4327. HANDLE_OP_END();
  4328. }
  4329. HANDLE_OP(WASM_OP_I64_LT_U)
  4330. {
  4331. DEF_OP_CMP(uint64, I64, <);
  4332. HANDLE_OP_END();
  4333. }
  4334. HANDLE_OP(WASM_OP_I64_GT_S)
  4335. {
  4336. DEF_OP_CMP(int64, I64, >);
  4337. HANDLE_OP_END();
  4338. }
  4339. HANDLE_OP(WASM_OP_I64_GT_U)
  4340. {
  4341. DEF_OP_CMP(uint64, I64, >);
  4342. HANDLE_OP_END();
  4343. }
  4344. HANDLE_OP(WASM_OP_I64_LE_S)
  4345. {
  4346. DEF_OP_CMP(int64, I64, <=);
  4347. HANDLE_OP_END();
  4348. }
  4349. HANDLE_OP(WASM_OP_I64_LE_U)
  4350. {
  4351. DEF_OP_CMP(uint64, I64, <=);
  4352. HANDLE_OP_END();
  4353. }
  4354. HANDLE_OP(WASM_OP_I64_GE_S)
  4355. {
  4356. DEF_OP_CMP(int64, I64, >=);
  4357. HANDLE_OP_END();
  4358. }
  4359. HANDLE_OP(WASM_OP_I64_GE_U)
  4360. {
  4361. DEF_OP_CMP(uint64, I64, >=);
  4362. HANDLE_OP_END();
  4363. }
  4364. /* comparison instructions of f32 */
  4365. HANDLE_OP(WASM_OP_F32_EQ)
  4366. {
  4367. DEF_OP_CMP(float32, F32, ==);
  4368. HANDLE_OP_END();
  4369. }
  4370. HANDLE_OP(WASM_OP_F32_NE)
  4371. {
  4372. DEF_OP_CMP(float32, F32, !=);
  4373. HANDLE_OP_END();
  4374. }
  4375. HANDLE_OP(WASM_OP_F32_LT)
  4376. {
  4377. DEF_OP_CMP(float32, F32, <);
  4378. HANDLE_OP_END();
  4379. }
  4380. HANDLE_OP(WASM_OP_F32_GT)
  4381. {
  4382. DEF_OP_CMP(float32, F32, >);
  4383. HANDLE_OP_END();
  4384. }
  4385. HANDLE_OP(WASM_OP_F32_LE)
  4386. {
  4387. DEF_OP_CMP(float32, F32, <=);
  4388. HANDLE_OP_END();
  4389. }
  4390. HANDLE_OP(WASM_OP_F32_GE)
  4391. {
  4392. DEF_OP_CMP(float32, F32, >=);
  4393. HANDLE_OP_END();
  4394. }
  4395. /* comparison instructions of f64 */
  4396. HANDLE_OP(WASM_OP_F64_EQ)
  4397. {
  4398. DEF_OP_CMP(float64, F64, ==);
  4399. HANDLE_OP_END();
  4400. }
  4401. HANDLE_OP(WASM_OP_F64_NE)
  4402. {
  4403. DEF_OP_CMP(float64, F64, !=);
  4404. HANDLE_OP_END();
  4405. }
  4406. HANDLE_OP(WASM_OP_F64_LT)
  4407. {
  4408. DEF_OP_CMP(float64, F64, <);
  4409. HANDLE_OP_END();
  4410. }
  4411. HANDLE_OP(WASM_OP_F64_GT)
  4412. {
  4413. DEF_OP_CMP(float64, F64, >);
  4414. HANDLE_OP_END();
  4415. }
  4416. HANDLE_OP(WASM_OP_F64_LE)
  4417. {
  4418. DEF_OP_CMP(float64, F64, <=);
  4419. HANDLE_OP_END();
  4420. }
  4421. HANDLE_OP(WASM_OP_F64_GE)
  4422. {
  4423. DEF_OP_CMP(float64, F64, >=);
  4424. HANDLE_OP_END();
  4425. }
  4426. /* numberic instructions of i32 */
  4427. HANDLE_OP(WASM_OP_I32_CLZ)
  4428. {
  4429. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  4430. HANDLE_OP_END();
  4431. }
  4432. HANDLE_OP(WASM_OP_I32_CTZ)
  4433. {
  4434. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  4435. HANDLE_OP_END();
  4436. }
  4437. HANDLE_OP(WASM_OP_I32_POPCNT)
  4438. {
  4439. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  4440. HANDLE_OP_END();
  4441. }
  4442. HANDLE_OP(WASM_OP_I32_ADD)
  4443. {
  4444. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  4445. HANDLE_OP_END();
  4446. }
  4447. HANDLE_OP(WASM_OP_I32_SUB)
  4448. {
  4449. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  4450. HANDLE_OP_END();
  4451. }
  4452. HANDLE_OP(WASM_OP_I32_MUL)
  4453. {
  4454. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  4455. HANDLE_OP_END();
  4456. }
  4457. HANDLE_OP(WASM_OP_I32_DIV_S)
  4458. {
  4459. int32 a, b;
  4460. b = POP_I32();
  4461. a = POP_I32();
  4462. if (a == (int32)0x80000000 && b == -1) {
  4463. wasm_set_exception(module, "integer overflow");
  4464. goto got_exception;
  4465. }
  4466. if (b == 0) {
  4467. wasm_set_exception(module, "integer divide by zero");
  4468. goto got_exception;
  4469. }
  4470. PUSH_I32(a / b);
  4471. HANDLE_OP_END();
  4472. }
  4473. HANDLE_OP(WASM_OP_I32_DIV_U)
  4474. {
  4475. uint32 a, b;
  4476. b = (uint32)POP_I32();
  4477. a = (uint32)POP_I32();
  4478. if (b == 0) {
  4479. wasm_set_exception(module, "integer divide by zero");
  4480. goto got_exception;
  4481. }
  4482. PUSH_I32(a / b);
  4483. HANDLE_OP_END();
  4484. }
  4485. HANDLE_OP(WASM_OP_I32_REM_S)
  4486. {
  4487. int32 a, b;
  4488. b = POP_I32();
  4489. a = POP_I32();
  4490. if (a == (int32)0x80000000 && b == -1) {
  4491. PUSH_I32(0);
  4492. HANDLE_OP_END();
  4493. }
  4494. if (b == 0) {
  4495. wasm_set_exception(module, "integer divide by zero");
  4496. goto got_exception;
  4497. }
  4498. PUSH_I32(a % b);
  4499. HANDLE_OP_END();
  4500. }
  4501. HANDLE_OP(WASM_OP_I32_REM_U)
  4502. {
  4503. uint32 a, b;
  4504. b = (uint32)POP_I32();
  4505. a = (uint32)POP_I32();
  4506. if (b == 0) {
  4507. wasm_set_exception(module, "integer divide by zero");
  4508. goto got_exception;
  4509. }
  4510. PUSH_I32(a % b);
  4511. HANDLE_OP_END();
  4512. }
  4513. HANDLE_OP(WASM_OP_I32_AND)
  4514. {
  4515. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  4516. HANDLE_OP_END();
  4517. }
  4518. HANDLE_OP(WASM_OP_I32_OR)
  4519. {
  4520. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  4521. HANDLE_OP_END();
  4522. }
  4523. HANDLE_OP(WASM_OP_I32_XOR)
  4524. {
  4525. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  4526. HANDLE_OP_END();
  4527. }
  4528. HANDLE_OP(WASM_OP_I32_SHL)
  4529. {
  4530. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  4531. HANDLE_OP_END();
  4532. }
  4533. HANDLE_OP(WASM_OP_I32_SHR_S)
  4534. {
  4535. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  4536. HANDLE_OP_END();
  4537. }
  4538. HANDLE_OP(WASM_OP_I32_SHR_U)
  4539. {
  4540. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  4541. HANDLE_OP_END();
  4542. }
  4543. HANDLE_OP(WASM_OP_I32_ROTL)
  4544. {
  4545. uint32 a, b;
  4546. b = (uint32)POP_I32();
  4547. a = (uint32)POP_I32();
  4548. PUSH_I32(rotl32(a, b));
  4549. HANDLE_OP_END();
  4550. }
  4551. HANDLE_OP(WASM_OP_I32_ROTR)
  4552. {
  4553. uint32 a, b;
  4554. b = (uint32)POP_I32();
  4555. a = (uint32)POP_I32();
  4556. PUSH_I32(rotr32(a, b));
  4557. HANDLE_OP_END();
  4558. }
  4559. /* numberic instructions of i64 */
  4560. HANDLE_OP(WASM_OP_I64_CLZ)
  4561. {
  4562. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  4563. HANDLE_OP_END();
  4564. }
  4565. HANDLE_OP(WASM_OP_I64_CTZ)
  4566. {
  4567. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  4568. HANDLE_OP_END();
  4569. }
  4570. HANDLE_OP(WASM_OP_I64_POPCNT)
  4571. {
  4572. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  4573. HANDLE_OP_END();
  4574. }
  4575. HANDLE_OP(WASM_OP_I64_ADD)
  4576. {
  4577. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  4578. HANDLE_OP_END();
  4579. }
  4580. HANDLE_OP(WASM_OP_I64_SUB)
  4581. {
  4582. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  4583. HANDLE_OP_END();
  4584. }
  4585. HANDLE_OP(WASM_OP_I64_MUL)
  4586. {
  4587. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  4588. HANDLE_OP_END();
  4589. }
  4590. HANDLE_OP(WASM_OP_I64_DIV_S)
  4591. {
  4592. int64 a, b;
  4593. b = POP_I64();
  4594. a = POP_I64();
  4595. if (a == (int64)0x8000000000000000LL && b == -1) {
  4596. wasm_set_exception(module, "integer overflow");
  4597. goto got_exception;
  4598. }
  4599. if (b == 0) {
  4600. wasm_set_exception(module, "integer divide by zero");
  4601. goto got_exception;
  4602. }
  4603. PUSH_I64(a / b);
  4604. HANDLE_OP_END();
  4605. }
  4606. HANDLE_OP(WASM_OP_I64_DIV_U)
  4607. {
  4608. uint64 a, b;
  4609. b = (uint64)POP_I64();
  4610. a = (uint64)POP_I64();
  4611. if (b == 0) {
  4612. wasm_set_exception(module, "integer divide by zero");
  4613. goto got_exception;
  4614. }
  4615. PUSH_I64(a / b);
  4616. HANDLE_OP_END();
  4617. }
  4618. HANDLE_OP(WASM_OP_I64_REM_S)
  4619. {
  4620. int64 a, b;
  4621. b = POP_I64();
  4622. a = POP_I64();
  4623. if (a == (int64)0x8000000000000000LL && b == -1) {
  4624. PUSH_I64(0);
  4625. HANDLE_OP_END();
  4626. }
  4627. if (b == 0) {
  4628. wasm_set_exception(module, "integer divide by zero");
  4629. goto got_exception;
  4630. }
  4631. PUSH_I64(a % b);
  4632. HANDLE_OP_END();
  4633. }
  4634. HANDLE_OP(WASM_OP_I64_REM_U)
  4635. {
  4636. uint64 a, b;
  4637. b = (uint64)POP_I64();
  4638. a = (uint64)POP_I64();
  4639. if (b == 0) {
  4640. wasm_set_exception(module, "integer divide by zero");
  4641. goto got_exception;
  4642. }
  4643. PUSH_I64(a % b);
  4644. HANDLE_OP_END();
  4645. }
  4646. HANDLE_OP(WASM_OP_I64_AND)
  4647. {
  4648. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  4649. HANDLE_OP_END();
  4650. }
  4651. HANDLE_OP(WASM_OP_I64_OR)
  4652. {
  4653. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  4654. HANDLE_OP_END();
  4655. }
  4656. HANDLE_OP(WASM_OP_I64_XOR)
  4657. {
  4658. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  4659. HANDLE_OP_END();
  4660. }
  4661. HANDLE_OP(WASM_OP_I64_SHL)
  4662. {
  4663. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  4664. HANDLE_OP_END();
  4665. }
  4666. HANDLE_OP(WASM_OP_I64_SHR_S)
  4667. {
  4668. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  4669. HANDLE_OP_END();
  4670. }
  4671. HANDLE_OP(WASM_OP_I64_SHR_U)
  4672. {
  4673. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  4674. HANDLE_OP_END();
  4675. }
  4676. HANDLE_OP(WASM_OP_I64_ROTL)
  4677. {
  4678. uint64 a, b;
  4679. b = (uint64)POP_I64();
  4680. a = (uint64)POP_I64();
  4681. PUSH_I64(rotl64(a, b));
  4682. HANDLE_OP_END();
  4683. }
  4684. HANDLE_OP(WASM_OP_I64_ROTR)
  4685. {
  4686. uint64 a, b;
  4687. b = (uint64)POP_I64();
  4688. a = (uint64)POP_I64();
  4689. PUSH_I64(rotr64(a, b));
  4690. HANDLE_OP_END();
  4691. }
  4692. /* numberic instructions of f32 */
  4693. HANDLE_OP(WASM_OP_F32_ABS)
  4694. {
  4695. DEF_OP_MATH(float32, F32, fabsf);
  4696. HANDLE_OP_END();
  4697. }
  4698. HANDLE_OP(WASM_OP_F32_NEG)
  4699. {
  4700. uint32 u32 = frame_sp[-1];
  4701. uint32 sign_bit = u32 & ((uint32)1 << 31);
  4702. if (sign_bit)
  4703. frame_sp[-1] = u32 & ~((uint32)1 << 31);
  4704. else
  4705. frame_sp[-1] = u32 | ((uint32)1 << 31);
  4706. HANDLE_OP_END();
  4707. }
  4708. HANDLE_OP(WASM_OP_F32_CEIL)
  4709. {
  4710. DEF_OP_MATH(float32, F32, ceilf);
  4711. HANDLE_OP_END();
  4712. }
  4713. HANDLE_OP(WASM_OP_F32_FLOOR)
  4714. {
  4715. DEF_OP_MATH(float32, F32, floorf);
  4716. HANDLE_OP_END();
  4717. }
  4718. HANDLE_OP(WASM_OP_F32_TRUNC)
  4719. {
  4720. DEF_OP_MATH(float32, F32, truncf);
  4721. HANDLE_OP_END();
  4722. }
  4723. HANDLE_OP(WASM_OP_F32_NEAREST)
  4724. {
  4725. DEF_OP_MATH(float32, F32, rintf);
  4726. HANDLE_OP_END();
  4727. }
  4728. HANDLE_OP(WASM_OP_F32_SQRT)
  4729. {
  4730. DEF_OP_MATH(float32, F32, sqrtf);
  4731. HANDLE_OP_END();
  4732. }
  4733. HANDLE_OP(WASM_OP_F32_ADD)
  4734. {
  4735. DEF_OP_NUMERIC(float32, float32, F32, +);
  4736. HANDLE_OP_END();
  4737. }
  4738. HANDLE_OP(WASM_OP_F32_SUB)
  4739. {
  4740. DEF_OP_NUMERIC(float32, float32, F32, -);
  4741. HANDLE_OP_END();
  4742. }
  4743. HANDLE_OP(WASM_OP_F32_MUL)
  4744. {
  4745. DEF_OP_NUMERIC(float32, float32, F32, *);
  4746. HANDLE_OP_END();
  4747. }
  4748. HANDLE_OP(WASM_OP_F32_DIV)
  4749. {
  4750. DEF_OP_NUMERIC(float32, float32, F32, /);
  4751. HANDLE_OP_END();
  4752. }
  4753. HANDLE_OP(WASM_OP_F32_MIN)
  4754. {
  4755. float32 a, b;
  4756. b = POP_F32();
  4757. a = POP_F32();
  4758. PUSH_F32(f32_min(a, b));
  4759. HANDLE_OP_END();
  4760. }
  4761. HANDLE_OP(WASM_OP_F32_MAX)
  4762. {
  4763. float32 a, b;
  4764. b = POP_F32();
  4765. a = POP_F32();
  4766. PUSH_F32(f32_max(a, b));
  4767. HANDLE_OP_END();
  4768. }
  4769. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  4770. {
  4771. float32 a, b;
  4772. b = POP_F32();
  4773. a = POP_F32();
  4774. PUSH_F32(local_copysignf(a, b));
  4775. HANDLE_OP_END();
  4776. }
  4777. /* numberic instructions of f64 */
  4778. HANDLE_OP(WASM_OP_F64_ABS)
  4779. {
  4780. DEF_OP_MATH(float64, F64, fabs);
  4781. HANDLE_OP_END();
  4782. }
  4783. HANDLE_OP(WASM_OP_F64_NEG)
  4784. {
  4785. uint64 u64 = GET_I64_FROM_ADDR(frame_sp - 2);
  4786. uint64 sign_bit = u64 & (((uint64)1) << 63);
  4787. if (sign_bit)
  4788. PUT_I64_TO_ADDR(frame_sp - 2, (u64 & ~(((uint64)1) << 63)));
  4789. else
  4790. PUT_I64_TO_ADDR(frame_sp - 2, (u64 | (((uint64)1) << 63)));
  4791. HANDLE_OP_END();
  4792. }
  4793. HANDLE_OP(WASM_OP_F64_CEIL)
  4794. {
  4795. DEF_OP_MATH(float64, F64, ceil);
  4796. HANDLE_OP_END();
  4797. }
  4798. HANDLE_OP(WASM_OP_F64_FLOOR)
  4799. {
  4800. DEF_OP_MATH(float64, F64, floor);
  4801. HANDLE_OP_END();
  4802. }
  4803. HANDLE_OP(WASM_OP_F64_TRUNC)
  4804. {
  4805. DEF_OP_MATH(float64, F64, trunc);
  4806. HANDLE_OP_END();
  4807. }
  4808. HANDLE_OP(WASM_OP_F64_NEAREST)
  4809. {
  4810. DEF_OP_MATH(float64, F64, rint);
  4811. HANDLE_OP_END();
  4812. }
  4813. HANDLE_OP(WASM_OP_F64_SQRT)
  4814. {
  4815. DEF_OP_MATH(float64, F64, sqrt);
  4816. HANDLE_OP_END();
  4817. }
  4818. HANDLE_OP(WASM_OP_F64_ADD)
  4819. {
  4820. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  4821. HANDLE_OP_END();
  4822. }
  4823. HANDLE_OP(WASM_OP_F64_SUB)
  4824. {
  4825. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  4826. HANDLE_OP_END();
  4827. }
  4828. HANDLE_OP(WASM_OP_F64_MUL)
  4829. {
  4830. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  4831. HANDLE_OP_END();
  4832. }
  4833. HANDLE_OP(WASM_OP_F64_DIV)
  4834. {
  4835. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  4836. HANDLE_OP_END();
  4837. }
  4838. HANDLE_OP(WASM_OP_F64_MIN)
  4839. {
  4840. float64 a, b;
  4841. b = POP_F64();
  4842. a = POP_F64();
  4843. PUSH_F64(f64_min(a, b));
  4844. HANDLE_OP_END();
  4845. }
  4846. HANDLE_OP(WASM_OP_F64_MAX)
  4847. {
  4848. float64 a, b;
  4849. b = POP_F64();
  4850. a = POP_F64();
  4851. PUSH_F64(f64_max(a, b));
  4852. HANDLE_OP_END();
  4853. }
  4854. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  4855. {
  4856. float64 a, b;
  4857. b = POP_F64();
  4858. a = POP_F64();
  4859. PUSH_F64(local_copysign(a, b));
  4860. HANDLE_OP_END();
  4861. }
  4862. /* conversions of i32 */
  4863. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  4864. {
  4865. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  4866. PUSH_I32(value);
  4867. HANDLE_OP_END();
  4868. }
  4869. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  4870. {
  4871. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  4872. since float/double values of ieee754 cannot precisely
  4873. represent all int32/uint32/int64/uint64 values, e.g.
  4874. UINT32_MAX is 4294967295, but (float32)4294967295 is
  4875. 4294967296.0f, but not 4294967295.0f. */
  4876. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  4877. HANDLE_OP_END();
  4878. }
  4879. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  4880. {
  4881. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  4882. HANDLE_OP_END();
  4883. }
  4884. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  4885. {
  4886. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  4887. /* frame_sp can't be moved in trunc function, we need to
  4888. manually adjust it if src and dst op's cell num is
  4889. different */
  4890. frame_sp--;
  4891. HANDLE_OP_END();
  4892. }
  4893. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  4894. {
  4895. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  4896. frame_sp--;
  4897. HANDLE_OP_END();
  4898. }
  4899. /* conversions of i64 */
  4900. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  4901. {
  4902. DEF_OP_CONVERT(int64, I64, int32, I32);
  4903. HANDLE_OP_END();
  4904. }
  4905. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  4906. {
  4907. DEF_OP_CONVERT(int64, I64, uint32, I32);
  4908. HANDLE_OP_END();
  4909. }
  4910. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  4911. {
  4912. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  4913. 9223372036854775808.0f, false, true);
  4914. frame_sp++;
  4915. HANDLE_OP_END();
  4916. }
  4917. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  4918. {
  4919. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  4920. frame_sp++;
  4921. HANDLE_OP_END();
  4922. }
  4923. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  4924. {
  4925. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  4926. false, true);
  4927. HANDLE_OP_END();
  4928. }
  4929. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  4930. {
  4931. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  4932. HANDLE_OP_END();
  4933. }
  4934. /* conversions of f32 */
  4935. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  4936. {
  4937. DEF_OP_CONVERT(float32, F32, int32, I32);
  4938. HANDLE_OP_END();
  4939. }
  4940. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  4941. {
  4942. DEF_OP_CONVERT(float32, F32, uint32, I32);
  4943. HANDLE_OP_END();
  4944. }
  4945. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  4946. {
  4947. DEF_OP_CONVERT(float32, F32, int64, I64);
  4948. HANDLE_OP_END();
  4949. }
  4950. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  4951. {
  4952. DEF_OP_CONVERT(float32, F32, uint64, I64);
  4953. HANDLE_OP_END();
  4954. }
  4955. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  4956. {
  4957. DEF_OP_CONVERT(float32, F32, float64, F64);
  4958. HANDLE_OP_END();
  4959. }
  4960. /* conversions of f64 */
  4961. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  4962. {
  4963. DEF_OP_CONVERT(float64, F64, int32, I32);
  4964. HANDLE_OP_END();
  4965. }
  4966. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  4967. {
  4968. DEF_OP_CONVERT(float64, F64, uint32, I32);
  4969. HANDLE_OP_END();
  4970. }
  4971. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  4972. {
  4973. DEF_OP_CONVERT(float64, F64, int64, I64);
  4974. HANDLE_OP_END();
  4975. }
  4976. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  4977. {
  4978. DEF_OP_CONVERT(float64, F64, uint64, I64);
  4979. HANDLE_OP_END();
  4980. }
  4981. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  4982. {
  4983. DEF_OP_CONVERT(float64, F64, float32, F32);
  4984. HANDLE_OP_END();
  4985. }
  4986. /* reinterpretations */
  4987. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  4988. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  4989. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  4990. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64) { HANDLE_OP_END(); }
  4991. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  4992. {
  4993. DEF_OP_CONVERT(int32, I32, int8, I32);
  4994. HANDLE_OP_END();
  4995. }
  4996. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  4997. {
  4998. DEF_OP_CONVERT(int32, I32, int16, I32);
  4999. HANDLE_OP_END();
  5000. }
  5001. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  5002. {
  5003. DEF_OP_CONVERT(int64, I64, int8, I64);
  5004. HANDLE_OP_END();
  5005. }
  5006. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  5007. {
  5008. DEF_OP_CONVERT(int64, I64, int16, I64);
  5009. HANDLE_OP_END();
  5010. }
  5011. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  5012. {
  5013. DEF_OP_CONVERT(int64, I64, int32, I64);
  5014. HANDLE_OP_END();
  5015. }
  5016. HANDLE_OP(WASM_OP_MISC_PREFIX)
  5017. {
  5018. uint32 opcode1;
  5019. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  5020. /* opcode1 was checked in loader and is no larger than
  5021. UINT8_MAX */
  5022. opcode = (uint8)opcode1;
  5023. switch (opcode) {
  5024. case WASM_OP_I32_TRUNC_SAT_S_F32:
  5025. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  5026. true, true);
  5027. break;
  5028. case WASM_OP_I32_TRUNC_SAT_U_F32:
  5029. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  5030. break;
  5031. case WASM_OP_I32_TRUNC_SAT_S_F64:
  5032. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  5033. true);
  5034. frame_sp--;
  5035. break;
  5036. case WASM_OP_I32_TRUNC_SAT_U_F64:
  5037. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  5038. frame_sp--;
  5039. break;
  5040. case WASM_OP_I64_TRUNC_SAT_S_F32:
  5041. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  5042. 9223372036854775808.0f, false,
  5043. true);
  5044. frame_sp++;
  5045. break;
  5046. case WASM_OP_I64_TRUNC_SAT_U_F32:
  5047. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  5048. false, false);
  5049. frame_sp++;
  5050. break;
  5051. case WASM_OP_I64_TRUNC_SAT_S_F64:
  5052. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  5053. 9223372036854775808.0, false,
  5054. true);
  5055. break;
  5056. case WASM_OP_I64_TRUNC_SAT_U_F64:
  5057. DEF_OP_TRUNC_SAT_F64(-1.0f, 18446744073709551616.0,
  5058. false, false);
  5059. break;
  5060. #if WASM_ENABLE_BULK_MEMORY != 0
  5061. case WASM_OP_MEMORY_INIT:
  5062. {
  5063. uint32 segment;
  5064. mem_offset_t addr;
  5065. uint64 bytes, offset, seg_len;
  5066. uint8 *data;
  5067. read_leb_uint32(frame_ip, frame_ip_end, segment);
  5068. #if WASM_ENABLE_MULTI_MEMORY != 0
  5069. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5070. #else
  5071. /* skip memory index */
  5072. frame_ip++;
  5073. #endif
  5074. bytes = (uint64)(uint32)POP_I32();
  5075. offset = (uint64)(uint32)POP_I32();
  5076. addr = (mem_offset_t)POP_MEM_OFFSET();
  5077. #if WASM_ENABLE_THREAD_MGR != 0
  5078. linear_mem_size = get_linear_mem_size();
  5079. #endif
  5080. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5081. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  5082. #else
  5083. #if WASM_ENABLE_SHARED_HEAP != 0
  5084. if (app_addr_in_shared_heap((uint64)(uint32)addr,
  5085. bytes))
  5086. shared_heap_addr_app_to_native((uint64)(uint32)addr,
  5087. maddr);
  5088. else
  5089. #endif
  5090. {
  5091. if ((uint64)(uint32)addr + bytes > linear_mem_size)
  5092. goto out_of_bounds;
  5093. maddr = memory->memory_data + (uint32)addr;
  5094. }
  5095. #endif
  5096. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  5097. segment)) {
  5098. seg_len = 0;
  5099. data = NULL;
  5100. }
  5101. else {
  5102. seg_len =
  5103. (uint64)module->module->data_segments[segment]
  5104. ->data_length;
  5105. data = module->module->data_segments[segment]->data;
  5106. }
  5107. if (offset + bytes > seg_len)
  5108. goto out_of_bounds;
  5109. bh_memcpy_s(maddr, (uint32)(linear_mem_size - addr),
  5110. data + offset, (uint32)bytes);
  5111. break;
  5112. }
  5113. case WASM_OP_DATA_DROP:
  5114. {
  5115. uint32 segment;
  5116. read_leb_uint32(frame_ip, frame_ip_end, segment);
  5117. bh_bitmap_set_bit(module->e->common.data_dropped,
  5118. segment);
  5119. break;
  5120. }
  5121. case WASM_OP_MEMORY_COPY:
  5122. {
  5123. mem_offset_t dst, src, len;
  5124. uint8 *mdst, *msrc;
  5125. uint64 dlen;
  5126. len = POP_MEM_OFFSET();
  5127. src = POP_MEM_OFFSET();
  5128. dst = POP_MEM_OFFSET();
  5129. #if WASM_ENABLE_MULTI_MEMORY != 0
  5130. /* dst memidx */
  5131. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5132. #else
  5133. /* skip dst memidx */
  5134. frame_ip += 1;
  5135. #endif
  5136. #if WASM_ENABLE_THREAD_MGR != 0
  5137. linear_mem_size = get_linear_mem_size();
  5138. #endif
  5139. dlen = linear_mem_size - dst;
  5140. /* dst boundary check */
  5141. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5142. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5143. #if WASM_ENABLE_SHARED_HEAP != 0
  5144. if (app_addr_in_shared_heap((uint64)dst, len))
  5145. dlen = shared_heap_end_off - dst + 1;
  5146. #endif
  5147. #else /* else of OS_ENABLE_HW_BOUND_CHECK */
  5148. #if WASM_ENABLE_SHARED_HEAP != 0
  5149. if (app_addr_in_shared_heap((uint64)dst, len)) {
  5150. shared_heap_addr_app_to_native((uint64)dst, mdst);
  5151. dlen = shared_heap_end_off - dst + 1;
  5152. }
  5153. else
  5154. #endif
  5155. {
  5156. if ((uint64)dst + len > linear_mem_size)
  5157. goto out_of_bounds;
  5158. mdst = memory->memory_data + dst;
  5159. }
  5160. #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
  5161. #if WASM_ENABLE_MULTI_MEMORY != 0
  5162. /* src memidx */
  5163. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5164. #else
  5165. /* skip src memidx */
  5166. frame_ip += 1;
  5167. #endif
  5168. #if WASM_ENABLE_THREAD_MGR != 0
  5169. linear_mem_size = get_linear_mem_size();
  5170. #endif
  5171. /* src boundary check */
  5172. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5173. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  5174. #else
  5175. #if WASM_ENABLE_SHARED_HEAP != 0
  5176. if (app_addr_in_shared_heap((uint64)src, len))
  5177. shared_heap_addr_app_to_native((uint64)src, msrc);
  5178. else
  5179. #endif
  5180. {
  5181. if ((uint64)src + len > linear_mem_size)
  5182. goto out_of_bounds;
  5183. msrc = memory->memory_data + src;
  5184. }
  5185. #endif
  5186. #if WASM_ENABLE_MEMORY64 == 0
  5187. /* allowing the destination and source to overlap */
  5188. bh_memmove_s(mdst, (uint32)dlen, msrc, (uint32)len);
  5189. #else
  5190. /* use memmove when memory64 is enabled since len
  5191. may be larger than UINT32_MAX */
  5192. memmove(mdst, msrc, len);
  5193. (void)dlen;
  5194. #endif
  5195. break;
  5196. }
  5197. case WASM_OP_MEMORY_FILL:
  5198. {
  5199. mem_offset_t dst, len;
  5200. uint8 fill_val, *mdst;
  5201. #if WASM_ENABLE_MULTI_MEMORY != 0
  5202. read_leb_memidx(frame_ip, frame_ip_end, memidx);
  5203. #else
  5204. /* skip memory index */
  5205. frame_ip++;
  5206. #endif
  5207. len = POP_MEM_OFFSET();
  5208. fill_val = POP_I32();
  5209. dst = POP_MEM_OFFSET();
  5210. #if WASM_ENABLE_THREAD_MGR != 0
  5211. linear_mem_size = get_linear_mem_size();
  5212. #endif
  5213. #ifndef OS_ENABLE_HW_BOUND_CHECK
  5214. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  5215. #else
  5216. #if WASM_ENABLE_SHARED_HEAP != 0
  5217. if (app_addr_in_shared_heap((uint64)(uint32)dst, len))
  5218. shared_heap_addr_app_to_native((uint64)(uint32)dst,
  5219. mdst);
  5220. else
  5221. #endif
  5222. {
  5223. if ((uint64)(uint32)dst + len > linear_mem_size)
  5224. goto out_of_bounds;
  5225. mdst = memory->memory_data + (uint32)dst;
  5226. }
  5227. #endif
  5228. memset(mdst, fill_val, len);
  5229. break;
  5230. }
  5231. #endif /* WASM_ENABLE_BULK_MEMORY */
  5232. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  5233. case WASM_OP_TABLE_INIT:
  5234. {
  5235. uint32 tbl_idx;
  5236. tbl_elem_idx_t elem_idx, d;
  5237. uint32 n, s;
  5238. WASMTableInstance *tbl_inst;
  5239. table_elem_type_t *table_elems;
  5240. InitializerExpression *tbl_seg_init_values = NULL,
  5241. *init_values;
  5242. uint32 tbl_seg_len = 0;
  5243. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5244. bh_assert(elem_idx < module->module->table_seg_count);
  5245. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5246. bh_assert(tbl_idx < module->module->table_count);
  5247. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5248. #if WASM_ENABLE_MEMORY64 != 0
  5249. is_table64 = tbl_inst->is_table64;
  5250. #endif
  5251. n = (uint32)POP_I32();
  5252. s = (uint32)POP_I32();
  5253. d = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5254. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  5255. elem_idx)) {
  5256. /* table segment isn't dropped */
  5257. tbl_seg_init_values =
  5258. module->module->table_segments[elem_idx]
  5259. .init_values;
  5260. tbl_seg_len =
  5261. module->module->table_segments[elem_idx]
  5262. .value_count;
  5263. }
  5264. /* TODO: memory64 current implementation of table64
  5265. * still assumes the max table size UINT32_MAX
  5266. */
  5267. if (
  5268. #if WASM_ENABLE_MEMORY64 != 0
  5269. d > UINT32_MAX ||
  5270. #endif
  5271. offset_len_out_of_bounds(s, n, tbl_seg_len)
  5272. || offset_len_out_of_bounds((uint32)d, n,
  5273. tbl_inst->cur_size)) {
  5274. wasm_set_exception(module,
  5275. "out of bounds table access");
  5276. goto got_exception;
  5277. }
  5278. if (!n) {
  5279. break;
  5280. }
  5281. table_elems = tbl_inst->elems + d;
  5282. init_values = tbl_seg_init_values + s;
  5283. #if WASM_ENABLE_GC != 0
  5284. SYNC_ALL_TO_FRAME();
  5285. #endif
  5286. for (i = 0; i < n; i++) {
  5287. /* UINT32_MAX indicates that it is a null ref */
  5288. bh_assert(init_values[i].init_expr_type
  5289. == INIT_EXPR_TYPE_REFNULL_CONST
  5290. || init_values[i].init_expr_type
  5291. == INIT_EXPR_TYPE_FUNCREF_CONST);
  5292. #if WASM_ENABLE_GC == 0
  5293. table_elems[i] =
  5294. (table_elem_type_t)init_values[i].u.ref_index;
  5295. #else
  5296. if (init_values[i].u.ref_index != UINT32_MAX) {
  5297. if (!(func_obj = wasm_create_func_obj(
  5298. module, init_values[i].u.ref_index,
  5299. true, NULL, 0))) {
  5300. goto got_exception;
  5301. }
  5302. table_elems[i] = func_obj;
  5303. }
  5304. else {
  5305. table_elems[i] = NULL_REF;
  5306. }
  5307. #endif
  5308. }
  5309. break;
  5310. }
  5311. case WASM_OP_ELEM_DROP:
  5312. {
  5313. uint32 elem_idx;
  5314. read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
  5315. bh_assert(elem_idx < module->module->table_seg_count);
  5316. bh_bitmap_set_bit(module->e->common.elem_dropped,
  5317. elem_idx);
  5318. break;
  5319. }
  5320. case WASM_OP_TABLE_COPY:
  5321. {
  5322. uint32 src_tbl_idx, dst_tbl_idx;
  5323. tbl_elem_idx_t n, s, d;
  5324. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  5325. read_leb_uint32(frame_ip, frame_ip_end, dst_tbl_idx);
  5326. bh_assert(dst_tbl_idx < module->table_count);
  5327. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  5328. read_leb_uint32(frame_ip, frame_ip_end, src_tbl_idx);
  5329. bh_assert(src_tbl_idx < module->table_count);
  5330. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  5331. #if WASM_ENABLE_MEMORY64 != 0
  5332. is_table64 = src_tbl_inst->is_table64
  5333. && dst_tbl_inst->is_table64;
  5334. #endif
  5335. n = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5336. #if WASM_ENABLE_MEMORY64 != 0
  5337. is_table64 = src_tbl_inst->is_table64;
  5338. #endif
  5339. s = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5340. #if WASM_ENABLE_MEMORY64 != 0
  5341. is_table64 = dst_tbl_inst->is_table64;
  5342. #endif
  5343. d = (tbl_elem_idx_t)POP_TBL_ELEM_IDX();
  5344. if (
  5345. #if WASM_ENABLE_MEMORY64 != 0
  5346. n > UINT32_MAX || s > UINT32_MAX || d > UINT32_MAX
  5347. ||
  5348. #endif
  5349. offset_len_out_of_bounds((uint32)d, (uint32)n,
  5350. dst_tbl_inst->cur_size)
  5351. || offset_len_out_of_bounds(
  5352. (uint32)s, (uint32)n, src_tbl_inst->cur_size)) {
  5353. wasm_set_exception(module,
  5354. "out of bounds table access");
  5355. goto got_exception;
  5356. }
  5357. /* if s >= d, copy from front to back */
  5358. /* if s < d, copy from back to front */
  5359. /* merge all together */
  5360. bh_memmove_s((uint8 *)dst_tbl_inst
  5361. + offsetof(WASMTableInstance, elems)
  5362. + d * sizeof(table_elem_type_t),
  5363. (uint32)((dst_tbl_inst->cur_size - d)
  5364. * sizeof(table_elem_type_t)),
  5365. (uint8 *)src_tbl_inst
  5366. + offsetof(WASMTableInstance, elems)
  5367. + s * sizeof(table_elem_type_t),
  5368. (uint32)(n * sizeof(table_elem_type_t)));
  5369. break;
  5370. }
  5371. case WASM_OP_TABLE_GROW:
  5372. {
  5373. WASMTableInstance *tbl_inst;
  5374. uint32 tbl_idx, orig_tbl_sz;
  5375. tbl_elem_idx_t n;
  5376. table_elem_type_t init_val;
  5377. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5378. bh_assert(tbl_idx < module->table_count);
  5379. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5380. #if WASM_ENABLE_MEMORY64 != 0
  5381. is_table64 = tbl_inst->is_table64;
  5382. #endif
  5383. orig_tbl_sz = tbl_inst->cur_size;
  5384. n = POP_TBL_ELEM_IDX();
  5385. #if WASM_ENABLE_GC == 0
  5386. init_val = POP_I32();
  5387. #else
  5388. init_val = POP_REF();
  5389. #endif
  5390. if (
  5391. #if WASM_ENABLE_MEMORY64 != 0
  5392. n > UINT32_MAX ||
  5393. #endif
  5394. !wasm_enlarge_table(module, tbl_idx, (uint32)n,
  5395. init_val)) {
  5396. PUSH_TBL_ELEM_IDX(-1);
  5397. }
  5398. else {
  5399. PUSH_TBL_ELEM_IDX(orig_tbl_sz);
  5400. }
  5401. break;
  5402. }
  5403. case WASM_OP_TABLE_SIZE:
  5404. {
  5405. uint32 tbl_idx;
  5406. WASMTableInstance *tbl_inst;
  5407. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5408. bh_assert(tbl_idx < module->table_count);
  5409. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5410. #if WASM_ENABLE_MEMORY64 != 0
  5411. is_table64 = tbl_inst->is_table64;
  5412. #endif
  5413. PUSH_TBL_ELEM_IDX(tbl_inst->cur_size);
  5414. break;
  5415. }
  5416. case WASM_OP_TABLE_FILL:
  5417. {
  5418. uint32 tbl_idx;
  5419. tbl_elem_idx_t n, elem_idx;
  5420. WASMTableInstance *tbl_inst;
  5421. table_elem_type_t fill_val;
  5422. read_leb_uint32(frame_ip, frame_ip_end, tbl_idx);
  5423. bh_assert(tbl_idx < module->table_count);
  5424. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  5425. #if WASM_ENABLE_MEMORY64 != 0
  5426. is_table64 = tbl_inst->is_table64;
  5427. #endif
  5428. n = POP_TBL_ELEM_IDX();
  5429. #if WASM_ENABLE_GC == 0
  5430. fill_val = POP_I32();
  5431. #else
  5432. fill_val = POP_REF();
  5433. #endif
  5434. elem_idx = POP_TBL_ELEM_IDX();
  5435. if (
  5436. #if WASM_ENABLE_MEMORY64 != 0
  5437. n > UINT32_MAX || elem_idx > UINT32_MAX ||
  5438. #endif
  5439. offset_len_out_of_bounds((uint32)elem_idx,
  5440. (uint32)n,
  5441. tbl_inst->cur_size)) {
  5442. wasm_set_exception(module,
  5443. "out of bounds table access");
  5444. goto got_exception;
  5445. }
  5446. for (; n != 0; elem_idx++, n--) {
  5447. tbl_inst->elems[elem_idx] = fill_val;
  5448. }
  5449. break;
  5450. }
  5451. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  5452. default:
  5453. wasm_set_exception(module, "unsupported opcode");
  5454. goto got_exception;
  5455. }
  5456. HANDLE_OP_END();
  5457. }
  5458. #if WASM_ENABLE_SHARED_MEMORY != 0
  5459. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5460. {
  5461. mem_offset_t offset = 0, addr;
  5462. uint32 align = 0;
  5463. uint32 opcode1;
  5464. read_leb_uint32(frame_ip, frame_ip_end, opcode1);
  5465. /* opcode1 was checked in loader and is no larger than
  5466. UINT8_MAX */
  5467. opcode = (uint8)opcode1;
  5468. if (opcode != WASM_OP_ATOMIC_FENCE) {
  5469. read_leb_uint32(frame_ip, frame_ip_end, align);
  5470. read_leb_mem_offset(frame_ip, frame_ip_end, offset);
  5471. }
  5472. switch (opcode) {
  5473. case WASM_OP_ATOMIC_NOTIFY:
  5474. {
  5475. uint32 notify_count, ret;
  5476. notify_count = POP_I32();
  5477. addr = POP_MEM_OFFSET();
  5478. CHECK_MEMORY_OVERFLOW(4);
  5479. CHECK_ATOMIC_MEMORY_ACCESS();
  5480. ret = wasm_runtime_atomic_notify(
  5481. (WASMModuleInstanceCommon *)module, maddr,
  5482. notify_count);
  5483. if (ret == (uint32)-1)
  5484. goto got_exception;
  5485. PUSH_I32(ret);
  5486. break;
  5487. }
  5488. case WASM_OP_ATOMIC_WAIT32:
  5489. {
  5490. uint64 timeout;
  5491. uint32 expect, ret;
  5492. timeout = POP_I64();
  5493. expect = POP_I32();
  5494. addr = POP_MEM_OFFSET();
  5495. CHECK_MEMORY_OVERFLOW(4);
  5496. CHECK_ATOMIC_MEMORY_ACCESS();
  5497. ret = wasm_runtime_atomic_wait(
  5498. (WASMModuleInstanceCommon *)module, maddr,
  5499. (uint64)expect, timeout, false);
  5500. if (ret == (uint32)-1)
  5501. goto got_exception;
  5502. #if WASM_ENABLE_THREAD_MGR != 0
  5503. CHECK_SUSPEND_FLAGS();
  5504. #endif
  5505. PUSH_I32(ret);
  5506. break;
  5507. }
  5508. case WASM_OP_ATOMIC_WAIT64:
  5509. {
  5510. uint64 timeout, expect;
  5511. uint32 ret;
  5512. timeout = POP_I64();
  5513. expect = POP_I64();
  5514. addr = POP_MEM_OFFSET();
  5515. CHECK_MEMORY_OVERFLOW(8);
  5516. CHECK_ATOMIC_MEMORY_ACCESS();
  5517. ret = wasm_runtime_atomic_wait(
  5518. (WASMModuleInstanceCommon *)module, maddr, expect,
  5519. timeout, true);
  5520. if (ret == (uint32)-1)
  5521. goto got_exception;
  5522. #if WASM_ENABLE_THREAD_MGR != 0
  5523. CHECK_SUSPEND_FLAGS();
  5524. #endif
  5525. PUSH_I32(ret);
  5526. break;
  5527. }
  5528. case WASM_OP_ATOMIC_FENCE:
  5529. {
  5530. /* Skip the memory index */
  5531. frame_ip++;
  5532. os_atomic_thread_fence(os_memory_order_seq_cst);
  5533. break;
  5534. }
  5535. case WASM_OP_ATOMIC_I32_LOAD:
  5536. case WASM_OP_ATOMIC_I32_LOAD8_U:
  5537. case WASM_OP_ATOMIC_I32_LOAD16_U:
  5538. {
  5539. uint32 readv;
  5540. addr = POP_MEM_OFFSET();
  5541. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  5542. CHECK_MEMORY_OVERFLOW(1);
  5543. CHECK_ATOMIC_MEMORY_ACCESS();
  5544. shared_memory_lock(memory);
  5545. readv = (uint32)(*(uint8 *)maddr);
  5546. shared_memory_unlock(memory);
  5547. }
  5548. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  5549. CHECK_MEMORY_OVERFLOW(2);
  5550. CHECK_ATOMIC_MEMORY_ACCESS();
  5551. shared_memory_lock(memory);
  5552. readv = (uint32)LOAD_U16(maddr);
  5553. shared_memory_unlock(memory);
  5554. }
  5555. else {
  5556. CHECK_MEMORY_OVERFLOW(4);
  5557. CHECK_ATOMIC_MEMORY_ACCESS();
  5558. shared_memory_lock(memory);
  5559. readv = LOAD_I32(maddr);
  5560. shared_memory_unlock(memory);
  5561. }
  5562. PUSH_I32(readv);
  5563. break;
  5564. }
  5565. case WASM_OP_ATOMIC_I64_LOAD:
  5566. case WASM_OP_ATOMIC_I64_LOAD8_U:
  5567. case WASM_OP_ATOMIC_I64_LOAD16_U:
  5568. case WASM_OP_ATOMIC_I64_LOAD32_U:
  5569. {
  5570. uint64 readv;
  5571. addr = POP_MEM_OFFSET();
  5572. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  5573. CHECK_MEMORY_OVERFLOW(1);
  5574. CHECK_ATOMIC_MEMORY_ACCESS();
  5575. shared_memory_lock(memory);
  5576. readv = (uint64)(*(uint8 *)maddr);
  5577. shared_memory_unlock(memory);
  5578. }
  5579. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  5580. CHECK_MEMORY_OVERFLOW(2);
  5581. CHECK_ATOMIC_MEMORY_ACCESS();
  5582. shared_memory_lock(memory);
  5583. readv = (uint64)LOAD_U16(maddr);
  5584. shared_memory_unlock(memory);
  5585. }
  5586. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  5587. CHECK_MEMORY_OVERFLOW(4);
  5588. CHECK_ATOMIC_MEMORY_ACCESS();
  5589. shared_memory_lock(memory);
  5590. readv = (uint64)LOAD_U32(maddr);
  5591. shared_memory_unlock(memory);
  5592. }
  5593. else {
  5594. CHECK_MEMORY_OVERFLOW(8);
  5595. CHECK_ATOMIC_MEMORY_ACCESS();
  5596. shared_memory_lock(memory);
  5597. readv = LOAD_I64(maddr);
  5598. shared_memory_unlock(memory);
  5599. }
  5600. PUSH_I64(readv);
  5601. break;
  5602. }
  5603. case WASM_OP_ATOMIC_I32_STORE:
  5604. case WASM_OP_ATOMIC_I32_STORE8:
  5605. case WASM_OP_ATOMIC_I32_STORE16:
  5606. {
  5607. uint32 sval;
  5608. sval = (uint32)POP_I32();
  5609. addr = POP_MEM_OFFSET();
  5610. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  5611. CHECK_MEMORY_OVERFLOW(1);
  5612. CHECK_ATOMIC_MEMORY_ACCESS();
  5613. shared_memory_lock(memory);
  5614. *(uint8 *)maddr = (uint8)sval;
  5615. shared_memory_unlock(memory);
  5616. }
  5617. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  5618. CHECK_MEMORY_OVERFLOW(2);
  5619. CHECK_ATOMIC_MEMORY_ACCESS();
  5620. shared_memory_lock(memory);
  5621. STORE_U16(maddr, (uint16)sval);
  5622. shared_memory_unlock(memory);
  5623. }
  5624. else {
  5625. CHECK_MEMORY_OVERFLOW(4);
  5626. CHECK_ATOMIC_MEMORY_ACCESS();
  5627. shared_memory_lock(memory);
  5628. STORE_U32(maddr, sval);
  5629. shared_memory_unlock(memory);
  5630. }
  5631. break;
  5632. }
  5633. case WASM_OP_ATOMIC_I64_STORE:
  5634. case WASM_OP_ATOMIC_I64_STORE8:
  5635. case WASM_OP_ATOMIC_I64_STORE16:
  5636. case WASM_OP_ATOMIC_I64_STORE32:
  5637. {
  5638. uint64 sval;
  5639. sval = (uint64)POP_I64();
  5640. addr = POP_MEM_OFFSET();
  5641. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  5642. CHECK_MEMORY_OVERFLOW(1);
  5643. CHECK_ATOMIC_MEMORY_ACCESS();
  5644. shared_memory_lock(memory);
  5645. *(uint8 *)maddr = (uint8)sval;
  5646. shared_memory_unlock(memory);
  5647. }
  5648. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  5649. CHECK_MEMORY_OVERFLOW(2);
  5650. CHECK_ATOMIC_MEMORY_ACCESS();
  5651. shared_memory_lock(memory);
  5652. STORE_U16(maddr, (uint16)sval);
  5653. shared_memory_unlock(memory);
  5654. }
  5655. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  5656. CHECK_MEMORY_OVERFLOW(4);
  5657. CHECK_ATOMIC_MEMORY_ACCESS();
  5658. shared_memory_lock(memory);
  5659. STORE_U32(maddr, (uint32)sval);
  5660. shared_memory_unlock(memory);
  5661. }
  5662. else {
  5663. CHECK_MEMORY_OVERFLOW(8);
  5664. CHECK_ATOMIC_MEMORY_ACCESS();
  5665. shared_memory_lock(memory);
  5666. STORE_I64(maddr, sval);
  5667. shared_memory_unlock(memory);
  5668. }
  5669. break;
  5670. }
  5671. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  5672. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  5673. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  5674. {
  5675. uint32 readv, sval, expect;
  5676. sval = POP_I32();
  5677. expect = POP_I32();
  5678. addr = POP_MEM_OFFSET();
  5679. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  5680. CHECK_MEMORY_OVERFLOW(1);
  5681. CHECK_ATOMIC_MEMORY_ACCESS();
  5682. expect = (uint8)expect;
  5683. shared_memory_lock(memory);
  5684. readv = (uint32)(*(uint8 *)maddr);
  5685. if (readv == expect)
  5686. *(uint8 *)maddr = (uint8)(sval);
  5687. shared_memory_unlock(memory);
  5688. }
  5689. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  5690. CHECK_MEMORY_OVERFLOW(2);
  5691. CHECK_ATOMIC_MEMORY_ACCESS();
  5692. expect = (uint16)expect;
  5693. shared_memory_lock(memory);
  5694. readv = (uint32)LOAD_U16(maddr);
  5695. if (readv == expect)
  5696. STORE_U16(maddr, (uint16)(sval));
  5697. shared_memory_unlock(memory);
  5698. }
  5699. else {
  5700. CHECK_MEMORY_OVERFLOW(4);
  5701. CHECK_ATOMIC_MEMORY_ACCESS();
  5702. shared_memory_lock(memory);
  5703. readv = LOAD_I32(maddr);
  5704. if (readv == expect)
  5705. STORE_U32(maddr, sval);
  5706. shared_memory_unlock(memory);
  5707. }
  5708. PUSH_I32(readv);
  5709. break;
  5710. }
  5711. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  5712. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  5713. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  5714. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  5715. {
  5716. uint64 readv, sval, expect;
  5717. sval = (uint64)POP_I64();
  5718. expect = (uint64)POP_I64();
  5719. addr = POP_MEM_OFFSET();
  5720. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  5721. CHECK_MEMORY_OVERFLOW(1);
  5722. CHECK_ATOMIC_MEMORY_ACCESS();
  5723. expect = (uint8)expect;
  5724. shared_memory_lock(memory);
  5725. readv = (uint64)(*(uint8 *)maddr);
  5726. if (readv == expect)
  5727. *(uint8 *)maddr = (uint8)(sval);
  5728. shared_memory_unlock(memory);
  5729. }
  5730. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  5731. CHECK_MEMORY_OVERFLOW(2);
  5732. CHECK_ATOMIC_MEMORY_ACCESS();
  5733. expect = (uint16)expect;
  5734. shared_memory_lock(memory);
  5735. readv = (uint64)LOAD_U16(maddr);
  5736. if (readv == expect)
  5737. STORE_U16(maddr, (uint16)(sval));
  5738. shared_memory_unlock(memory);
  5739. }
  5740. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  5741. CHECK_MEMORY_OVERFLOW(4);
  5742. CHECK_ATOMIC_MEMORY_ACCESS();
  5743. expect = (uint32)expect;
  5744. shared_memory_lock(memory);
  5745. readv = (uint64)LOAD_U32(maddr);
  5746. if (readv == expect)
  5747. STORE_U32(maddr, (uint32)(sval));
  5748. shared_memory_unlock(memory);
  5749. }
  5750. else {
  5751. CHECK_MEMORY_OVERFLOW(8);
  5752. CHECK_ATOMIC_MEMORY_ACCESS();
  5753. shared_memory_lock(memory);
  5754. readv = (uint64)LOAD_I64(maddr);
  5755. if (readv == expect)
  5756. STORE_I64(maddr, sval);
  5757. shared_memory_unlock(memory);
  5758. }
  5759. PUSH_I64(readv);
  5760. break;
  5761. }
  5762. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  5763. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  5764. DEF_ATOMIC_RMW_OPCODE(AND, &);
  5765. DEF_ATOMIC_RMW_OPCODE(OR, |);
  5766. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  5767. /* xchg, ignore the read value, and store the given
  5768. value: readv * 0 + sval */
  5769. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  5770. }
  5771. HANDLE_OP_END();
  5772. }
  5773. #endif
  5774. HANDLE_OP(WASM_OP_IMPDEP)
  5775. {
  5776. frame = prev_frame;
  5777. frame_ip = frame->ip;
  5778. frame_sp = frame->sp;
  5779. frame_csp = frame->csp;
  5780. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5781. is_return_call = false;
  5782. #endif
  5783. goto call_func_from_entry;
  5784. }
  5785. #if WASM_ENABLE_DEBUG_INTERP != 0
  5786. HANDLE_OP(DEBUG_OP_BREAK)
  5787. {
  5788. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  5789. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  5790. WASM_SUSPEND_FLAG_SUSPEND);
  5791. frame_ip--;
  5792. SYNC_ALL_TO_FRAME();
  5793. CHECK_SUSPEND_FLAGS();
  5794. HANDLE_OP_END();
  5795. }
  5796. #endif
  5797. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5798. default:
  5799. wasm_set_exception(module, "unsupported opcode");
  5800. goto got_exception;
  5801. }
  5802. #endif
  5803. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5804. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  5805. #if WASM_ENABLE_TAIL_CALL == 0
  5806. HANDLE_OP(WASM_OP_RETURN_CALL)
  5807. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  5808. #endif
  5809. #if WASM_ENABLE_SHARED_MEMORY == 0
  5810. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  5811. #endif
  5812. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  5813. HANDLE_OP(WASM_OP_SELECT_T)
  5814. HANDLE_OP(WASM_OP_TABLE_GET)
  5815. HANDLE_OP(WASM_OP_TABLE_SET)
  5816. HANDLE_OP(WASM_OP_REF_NULL)
  5817. HANDLE_OP(WASM_OP_REF_IS_NULL)
  5818. HANDLE_OP(WASM_OP_REF_FUNC)
  5819. #endif
  5820. #if WASM_ENABLE_GC == 0
  5821. HANDLE_OP(WASM_OP_CALL_REF)
  5822. HANDLE_OP(WASM_OP_RETURN_CALL_REF)
  5823. HANDLE_OP(WASM_OP_REF_EQ)
  5824. HANDLE_OP(WASM_OP_REF_AS_NON_NULL)
  5825. HANDLE_OP(WASM_OP_BR_ON_NULL)
  5826. HANDLE_OP(WASM_OP_BR_ON_NON_NULL)
  5827. HANDLE_OP(WASM_OP_GC_PREFIX)
  5828. #endif
  5829. #if WASM_ENABLE_EXCE_HANDLING == 0
  5830. HANDLE_OP(WASM_OP_TRY)
  5831. HANDLE_OP(WASM_OP_CATCH)
  5832. HANDLE_OP(WASM_OP_THROW)
  5833. HANDLE_OP(WASM_OP_RETHROW)
  5834. HANDLE_OP(WASM_OP_DELEGATE)
  5835. HANDLE_OP(WASM_OP_CATCH_ALL)
  5836. HANDLE_OP(EXT_OP_TRY)
  5837. #endif
  5838. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_SIMD != 0
  5839. /* SIMD isn't supported by interpreter, but when JIT is
  5840. enabled, `iwasm --interp <wasm_file>` may be run to
  5841. trigger the SIMD opcode in interpreter */
  5842. HANDLE_OP(WASM_OP_SIMD_PREFIX)
  5843. #endif
  5844. HANDLE_OP(WASM_OP_UNUSED_0x16)
  5845. HANDLE_OP(WASM_OP_UNUSED_0x17)
  5846. HANDLE_OP(WASM_OP_UNUSED_0x27)
  5847. /* Used by fast interpreter */
  5848. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  5849. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  5850. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  5851. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  5852. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  5853. {
  5854. wasm_set_exception(module, "unsupported opcode");
  5855. goto got_exception;
  5856. }
  5857. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES != 0 */
  5858. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  5859. continue;
  5860. #else
  5861. FETCH_OPCODE_AND_DISPATCH();
  5862. #endif
  5863. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5864. call_func_from_return_call:
  5865. {
  5866. POP(cur_func->param_cell_num);
  5867. if (cur_func->param_cell_num > 0) {
  5868. word_copy(frame->lp, frame_sp, cur_func->param_cell_num);
  5869. }
  5870. FREE_FRAME(exec_env, frame);
  5871. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  5872. is_return_call = true;
  5873. goto call_func_from_entry;
  5874. }
  5875. #endif
  5876. call_func_from_interp:
  5877. {
  5878. /* Only do the copy when it's called from interpreter. */
  5879. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  5880. if (cur_func->param_cell_num > 0) {
  5881. POP(cur_func->param_cell_num);
  5882. word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num);
  5883. }
  5884. SYNC_ALL_TO_FRAME();
  5885. prev_frame = frame;
  5886. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5887. is_return_call = false;
  5888. #endif
  5889. }
  5890. call_func_from_entry:
  5891. {
  5892. if (cur_func->is_import_func) {
  5893. #if WASM_ENABLE_MULTI_MODULE != 0
  5894. if (cur_func->import_func_inst) {
  5895. wasm_interp_call_func_import(module, exec_env, cur_func,
  5896. prev_frame);
  5897. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5898. if (is_return_call) {
  5899. /* the frame was freed before tail calling and
  5900. the prev_frame was set as exec_env's cur_frame,
  5901. so here we recover context from prev_frame */
  5902. RECOVER_CONTEXT(prev_frame);
  5903. }
  5904. else
  5905. #endif
  5906. {
  5907. prev_frame = frame->prev_frame;
  5908. cur_func = frame->function;
  5909. UPDATE_ALL_FROM_FRAME();
  5910. }
  5911. #if WASM_ENABLE_EXCE_HANDLING != 0
  5912. char uncaught_exception[128] = { 0 };
  5913. bool has_exception =
  5914. wasm_copy_exception(module, uncaught_exception);
  5915. if (has_exception
  5916. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5917. uint32 import_exception;
  5918. /* initialize imported exception index to be invalid */
  5919. SET_INVALID_TAGINDEX(import_exception);
  5920. /* pull external exception */
  5921. uint32 ext_exception = POP_I32();
  5922. /* external function came back with an exception or trap */
  5923. /* lookup exception in import tags */
  5924. WASMTagInstance *tag = module->e->tags;
  5925. for (uint32 t = 0; t < module->module->import_tag_count;
  5926. tag++, t++) {
  5927. /* compare the module and the external index with the
  5928. * imort tag data */
  5929. if ((cur_func->u.func_import->import_module
  5930. == tag->u.tag_import->import_module)
  5931. && (ext_exception
  5932. == tag->u.tag_import
  5933. ->import_tag_index_linked)) {
  5934. /* set the import_exception to the import tag */
  5935. import_exception = t;
  5936. break;
  5937. }
  5938. }
  5939. /*
  5940. * exchange the thrown exception (index valid in submodule)
  5941. * with the imported exception index (valid in this module)
  5942. * if the module did not import the exception,
  5943. * that results in a "INVALID_TAGINDEX", that triggers
  5944. * an CATCH_ALL block, if there is one.
  5945. */
  5946. PUSH_I32(import_exception);
  5947. }
  5948. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  5949. }
  5950. else
  5951. #endif /* end of WASM_ENABLE_MULTI_MODULE != 0 */
  5952. {
  5953. wasm_interp_call_func_native(module, exec_env, cur_func,
  5954. prev_frame);
  5955. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  5956. if (is_return_call) {
  5957. /* the frame was freed before tail calling and
  5958. the prev_frame was set as exec_env's cur_frame,
  5959. so here we recover context from prev_frame */
  5960. RECOVER_CONTEXT(prev_frame);
  5961. }
  5962. else
  5963. #endif
  5964. {
  5965. prev_frame = frame->prev_frame;
  5966. cur_func = frame->function;
  5967. UPDATE_ALL_FROM_FRAME();
  5968. }
  5969. }
  5970. /* update memory size, no need to update memory ptr as
  5971. it isn't changed in wasm_enlarge_memory */
  5972. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  5973. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  5974. || WASM_ENABLE_BULK_MEMORY != 0
  5975. if (memory)
  5976. linear_mem_size = get_linear_mem_size();
  5977. #endif
  5978. if (wasm_copy_exception(module, NULL)) {
  5979. #if WASM_ENABLE_EXCE_HANDLING != 0
  5980. /* the caller raised an exception */
  5981. char uncaught_exception[128] = { 0 };
  5982. bool has_exception =
  5983. wasm_copy_exception(module, uncaught_exception);
  5984. /* libc_builtin signaled a "exception thrown by stdc++" trap */
  5985. if (has_exception
  5986. && strstr(uncaught_exception,
  5987. "exception thrown by stdc++")) {
  5988. wasm_set_exception(module, NULL);
  5989. /* setup internal c++ rethrow */
  5990. exception_tag_index = 0;
  5991. goto find_a_catch_handler;
  5992. }
  5993. /* when throw hits the end of a function it signals with a
  5994. * "uncaught wasm exception" trap */
  5995. if (has_exception
  5996. && strstr(uncaught_exception, "uncaught wasm exception")) {
  5997. wasm_set_exception(module, NULL);
  5998. exception_tag_index = POP_I32();
  5999. /* rethrow the exception into that frame */
  6000. goto find_a_catch_handler;
  6001. }
  6002. #endif /* WASM_ENABLE_EXCE_HANDLING != 0 */
  6003. goto got_exception;
  6004. }
  6005. }
  6006. else {
  6007. WASMFunction *cur_wasm_func = cur_func->u.func;
  6008. WASMFuncType *func_type = cur_wasm_func->func_type;
  6009. uint32 max_stack_cell_num = cur_wasm_func->max_stack_cell_num;
  6010. uint32 cell_num_of_local_stack;
  6011. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6012. uint32 local_cell_idx;
  6013. #endif
  6014. #if WASM_ENABLE_EXCE_HANDLING != 0
  6015. /* account for exception handlers, bundle them here */
  6016. uint32 eh_size =
  6017. cur_wasm_func->exception_handler_count * sizeof(uint8 *);
  6018. max_stack_cell_num += eh_size;
  6019. #endif
  6020. cell_num_of_local_stack = cur_func->param_cell_num
  6021. + cur_func->local_cell_num
  6022. + max_stack_cell_num;
  6023. all_cell_num = cell_num_of_local_stack
  6024. + cur_wasm_func->max_block_num
  6025. * (uint32)sizeof(WASMBranchBlock) / 4;
  6026. #if WASM_ENABLE_GC != 0
  6027. /* area of frame_ref */
  6028. all_cell_num += (cell_num_of_local_stack + 3) / 4;
  6029. #endif
  6030. /* param_cell_num, local_cell_num, max_stack_cell_num and
  6031. max_block_num are all no larger than UINT16_MAX (checked
  6032. in loader), all_cell_num must be smaller than 1MB */
  6033. bh_assert(all_cell_num < 1 * BH_MB);
  6034. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6035. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  6036. frame = prev_frame;
  6037. goto got_exception;
  6038. }
  6039. /* Initialize the interpreter context. */
  6040. frame->function = cur_func;
  6041. frame_ip = wasm_get_func_code(cur_func);
  6042. frame_ip_end = wasm_get_func_code_end(cur_func);
  6043. frame_lp = frame->lp;
  6044. frame_sp = frame->sp_bottom =
  6045. frame_lp + cur_func->param_cell_num + cur_func->local_cell_num;
  6046. frame->sp_boundary = frame->sp_bottom + max_stack_cell_num;
  6047. frame_csp = frame->csp_bottom =
  6048. (WASMBranchBlock *)frame->sp_boundary;
  6049. frame->csp_boundary =
  6050. frame->csp_bottom + cur_wasm_func->max_block_num;
  6051. #if WASM_ENABLE_GC != 0
  6052. /* frame->sp and frame->ip are used during GC root set enumeration,
  6053. * so we must initialized these fields here */
  6054. frame->sp = frame_sp;
  6055. frame->ip = frame_ip;
  6056. frame_ref = (uint8 *)frame->csp_boundary;
  6057. init_frame_refs(frame_ref, (uint32)cell_num_of_local_stack,
  6058. cur_func);
  6059. #endif
  6060. /* Initialize the local variables */
  6061. memset(frame_lp + cur_func->param_cell_num, 0,
  6062. (uint32)(cur_func->local_cell_num * 4));
  6063. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  6064. /* externref/funcref should be NULL_REF rather than 0 */
  6065. local_cell_idx = cur_func->param_cell_num;
  6066. for (i = 0; i < cur_wasm_func->local_count; i++) {
  6067. if (cur_wasm_func->local_types[i] == VALUE_TYPE_EXTERNREF
  6068. || cur_wasm_func->local_types[i] == VALUE_TYPE_FUNCREF) {
  6069. *(frame_lp + local_cell_idx) = NULL_REF;
  6070. }
  6071. local_cell_idx +=
  6072. wasm_value_type_cell_num(cur_wasm_func->local_types[i]);
  6073. }
  6074. #endif
  6075. /* Push function block as first block */
  6076. cell_num = func_type->ret_cell_num;
  6077. PUSH_CSP(LABEL_TYPE_FUNCTION, 0, cell_num, frame_ip_end - 1);
  6078. wasm_exec_env_set_cur_frame(exec_env, frame);
  6079. }
  6080. #if WASM_ENABLE_THREAD_MGR != 0
  6081. CHECK_SUSPEND_FLAGS();
  6082. #endif
  6083. HANDLE_OP_END();
  6084. }
  6085. return_func:
  6086. {
  6087. FREE_FRAME(exec_env, frame);
  6088. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6089. if (!prev_frame->ip) {
  6090. /* Called from native. */
  6091. return;
  6092. }
  6093. RECOVER_CONTEXT(prev_frame);
  6094. #if WASM_ENABLE_EXCE_HANDLING != 0
  6095. if (wasm_get_exception(module)) {
  6096. wasm_set_exception(module, NULL);
  6097. exception_tag_index = POP_I32();
  6098. goto find_a_catch_handler;
  6099. }
  6100. #endif
  6101. HANDLE_OP_END();
  6102. }
  6103. #if WASM_ENABLE_SHARED_MEMORY != 0
  6104. unaligned_atomic:
  6105. wasm_set_exception(module, "unaligned atomic");
  6106. goto got_exception;
  6107. #endif
  6108. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  6109. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  6110. || WASM_ENABLE_BULK_MEMORY != 0
  6111. out_of_bounds:
  6112. wasm_set_exception(module, "out of bounds memory access");
  6113. #endif
  6114. got_exception:
  6115. #if WASM_ENABLE_DEBUG_INTERP != 0
  6116. if (wasm_exec_env_get_instance(exec_env) != NULL) {
  6117. uint8 *frame_ip_temp = frame_ip;
  6118. frame_ip = frame_ip_orig;
  6119. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TRAP);
  6120. CHECK_SUSPEND_FLAGS();
  6121. frame_ip = frame_ip_temp;
  6122. }
  6123. #endif
  6124. SYNC_ALL_TO_FRAME();
  6125. return;
  6126. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  6127. }
  6128. #else
  6129. FETCH_OPCODE_AND_DISPATCH();
  6130. #endif
  6131. }
  6132. #if WASM_ENABLE_GC != 0
  6133. bool
  6134. wasm_interp_traverse_gc_rootset(WASMExecEnv *exec_env, void *heap)
  6135. {
  6136. WASMInterpFrame *frame;
  6137. WASMObjectRef gc_obj;
  6138. int i;
  6139. frame = wasm_exec_env_get_cur_frame(exec_env);
  6140. for (; frame; frame = frame->prev_frame) {
  6141. uint8 *frame_ref = get_frame_ref(frame);
  6142. for (i = 0; i < frame->sp - frame->lp; i++) {
  6143. if (frame_ref[i]) {
  6144. gc_obj = GET_REF_FROM_ADDR(frame->lp + i);
  6145. if (wasm_obj_is_created_from_heap(gc_obj)) {
  6146. if (mem_allocator_add_root((mem_allocator_t)heap, gc_obj)) {
  6147. return false;
  6148. }
  6149. }
  6150. #if UINTPTR_MAX == UINT64_MAX
  6151. bh_assert(frame_ref[i + 1]);
  6152. i++;
  6153. #endif
  6154. }
  6155. }
  6156. }
  6157. return true;
  6158. }
  6159. #endif
  6160. #if WASM_ENABLE_FAST_JIT != 0
  6161. /*
  6162. * ASAN is not designed to work with custom stack unwind or other low-level
  6163. * things. Ignore a function that does some low-level magic. (e.g. walking
  6164. * through the thread's stack bypassing the frame boundaries)
  6165. */
  6166. #if defined(__GNUC__) || defined(__clang__)
  6167. __attribute__((no_sanitize_address))
  6168. #endif
  6169. static void
  6170. fast_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  6171. WASMExecEnv *exec_env,
  6172. WASMFunctionInstance *function,
  6173. WASMInterpFrame *frame)
  6174. {
  6175. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  6176. JitInterpSwitchInfo info;
  6177. WASMModule *module = module_inst->module;
  6178. WASMFuncType *func_type = function->u.func->func_type;
  6179. uint8 type = func_type->result_count
  6180. ? func_type->types[func_type->param_count]
  6181. : VALUE_TYPE_VOID;
  6182. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6183. uint32 func_idx_non_import = func_idx - module->import_function_count;
  6184. int32 action;
  6185. #if WASM_ENABLE_REF_TYPES != 0
  6186. if (type == VALUE_TYPE_EXTERNREF || type == VALUE_TYPE_FUNCREF)
  6187. type = VALUE_TYPE_I32;
  6188. #endif
  6189. #if WASM_ENABLE_LAZY_JIT != 0
  6190. if (!jit_compiler_compile(module, func_idx)) {
  6191. wasm_set_exception(module_inst, "failed to compile fast jit function");
  6192. return;
  6193. }
  6194. #endif
  6195. bh_assert(jit_compiler_is_compiled(module, func_idx));
  6196. /* Switch to jitted code to call the jit function */
  6197. info.out.ret.last_return_type = type;
  6198. info.frame = frame;
  6199. frame->jitted_return_addr =
  6200. (uint8 *)jit_globals->return_to_interp_from_jitted;
  6201. action = jit_interp_switch_to_jitted(
  6202. exec_env, &info, func_idx,
  6203. module_inst->fast_jit_func_ptrs[func_idx_non_import]);
  6204. bh_assert(action == JIT_INTERP_ACTION_NORMAL
  6205. || (action == JIT_INTERP_ACTION_THROWN
  6206. && wasm_copy_exception(
  6207. (WASMModuleInstance *)exec_env->module_inst, NULL)));
  6208. /* Get the return values form info.out.ret */
  6209. if (func_type->result_count) {
  6210. switch (type) {
  6211. case VALUE_TYPE_I32:
  6212. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  6213. break;
  6214. case VALUE_TYPE_I64:
  6215. *(frame->sp - function->ret_cell_num) = info.out.ret.ival[0];
  6216. *(frame->sp - function->ret_cell_num + 1) =
  6217. info.out.ret.ival[1];
  6218. break;
  6219. case VALUE_TYPE_F32:
  6220. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6221. break;
  6222. case VALUE_TYPE_F64:
  6223. *(frame->sp - function->ret_cell_num) = info.out.ret.fval[0];
  6224. *(frame->sp - function->ret_cell_num + 1) =
  6225. info.out.ret.fval[1];
  6226. break;
  6227. default:
  6228. bh_assert(0);
  6229. break;
  6230. }
  6231. }
  6232. (void)action;
  6233. (void)func_idx;
  6234. }
  6235. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  6236. #if WASM_ENABLE_JIT != 0
  6237. #if WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_PERF_PROFILING != 0 \
  6238. || WASM_ENABLE_AOT_STACK_FRAME != 0
  6239. #if WASM_ENABLE_GC == 0
  6240. bool
  6241. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6242. {
  6243. WASMModuleInstance *module_inst =
  6244. (WASMModuleInstance *)exec_env->module_inst;
  6245. WASMInterpFrame *cur_frame, *frame;
  6246. uint32 size = (uint32)offsetof(WASMInterpFrame, lp);
  6247. bh_assert(module_inst->module_type == Wasm_Module_Bytecode);
  6248. cur_frame = exec_env->cur_frame;
  6249. if (!cur_frame)
  6250. frame = (WASMInterpFrame *)exec_env->wasm_stack.bottom;
  6251. else
  6252. frame = (WASMInterpFrame *)((uint8 *)cur_frame + size);
  6253. if ((uint8 *)frame + size > exec_env->wasm_stack.top_boundary) {
  6254. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6255. return false;
  6256. }
  6257. frame->function = module_inst->e->functions + func_index;
  6258. /* No need to initialize ip, it will be committed in jitted code
  6259. when needed */
  6260. /* frame->ip = NULL; */
  6261. frame->prev_frame = cur_frame;
  6262. #if WASM_ENABLE_PERF_PROFILING != 0
  6263. frame->time_started = os_time_thread_cputime_us();
  6264. #endif
  6265. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6266. {
  6267. uint32 wasm_stack_used =
  6268. (uint8 *)frame + size - exec_env->wasm_stack.bottom;
  6269. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6270. exec_env->max_wasm_stack_used = wasm_stack_used;
  6271. }
  6272. #endif
  6273. exec_env->cur_frame = frame;
  6274. return true;
  6275. }
  6276. static inline void
  6277. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6278. {
  6279. WASMInterpFrame *frame = exec_env->cur_frame;
  6280. WASMInterpFrame *prev_frame = frame->prev_frame;
  6281. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6282. #if WASM_ENABLE_PERF_PROFILING != 0
  6283. if (frame->function) {
  6284. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6285. frame->function->total_exec_time += time_elapsed;
  6286. frame->function->total_exec_cnt++;
  6287. /* parent function */
  6288. if (prev_frame)
  6289. prev_frame->function->children_exec_time += time_elapsed;
  6290. }
  6291. #endif
  6292. exec_env->cur_frame = prev_frame;
  6293. }
  6294. void
  6295. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6296. {
  6297. llvm_jit_free_frame_internal(exec_env);
  6298. }
  6299. #else /* else of WASM_ENABLE_GC == 0 */
  6300. bool
  6301. llvm_jit_alloc_frame(WASMExecEnv *exec_env, uint32 func_index)
  6302. {
  6303. WASMModuleInstance *module_inst;
  6304. WASMModule *module;
  6305. WASMInterpFrame *frame;
  6306. uint32 size, max_local_cell_num, max_stack_cell_num;
  6307. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6308. module_inst = (WASMModuleInstance *)exec_env->module_inst;
  6309. module = module_inst->module;
  6310. if (func_index >= func_index - module->import_function_count) {
  6311. WASMFunction *func =
  6312. module->functions[func_index - module->import_function_count];
  6313. max_local_cell_num = func->param_cell_num + func->local_cell_num;
  6314. max_stack_cell_num = func->max_stack_cell_num;
  6315. }
  6316. else {
  6317. WASMFunctionImport *func =
  6318. &((module->import_functions + func_index)->u.function);
  6319. max_local_cell_num = func->func_type->param_cell_num > 2
  6320. ? func->func_type->param_cell_num
  6321. : 2;
  6322. max_stack_cell_num = 0;
  6323. }
  6324. size =
  6325. wasm_interp_interp_frame_size(max_local_cell_num + max_stack_cell_num);
  6326. frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  6327. if (!frame) {
  6328. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6329. return false;
  6330. }
  6331. frame->function = module_inst->e->functions + func_index;
  6332. #if WASM_ENABLE_PERF_PROFILING != 0
  6333. frame->time_started = os_time_thread_cputime_us();
  6334. #endif
  6335. frame->prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6336. /* No need to initialize ip, it will be committed in jitted code
  6337. when needed */
  6338. /* frame->ip = NULL; */
  6339. #if WASM_ENABLE_GC != 0
  6340. frame->sp = frame->lp + max_local_cell_num;
  6341. /* Initialize frame ref flags for import function */
  6342. if (func_index < module->import_function_count) {
  6343. WASMFunctionImport *func =
  6344. &((module->import_functions + func_index)->u.function);
  6345. WASMFuncType *func_type = func->func_type;
  6346. /* native function doesn't have operand stack and label stack */
  6347. uint8 *frame_ref = (uint8 *)frame->sp;
  6348. uint32 i, j, k, value_type_cell_num;
  6349. for (i = 0, j = 0; i < func_type->param_count; i++) {
  6350. if (wasm_is_type_reftype(func_type->types[i])
  6351. && !wasm_is_reftype_i31ref(func_type->types[i])) {
  6352. frame_ref[j++] = 1;
  6353. #if UINTPTR_MAX == UINT64_MAX
  6354. frame_ref[j++] = 1;
  6355. #endif
  6356. }
  6357. else {
  6358. value_type_cell_num =
  6359. wasm_value_type_cell_num(func_type->types[i]);
  6360. for (k = 0; k < value_type_cell_num; k++)
  6361. frame_ref[j++] = 0;
  6362. }
  6363. }
  6364. }
  6365. #endif
  6366. wasm_exec_env_set_cur_frame(exec_env, frame);
  6367. return true;
  6368. }
  6369. static inline void
  6370. llvm_jit_free_frame_internal(WASMExecEnv *exec_env)
  6371. {
  6372. WASMInterpFrame *frame;
  6373. WASMInterpFrame *prev_frame;
  6374. bh_assert(exec_env->module_inst->module_type == Wasm_Module_Bytecode);
  6375. frame = wasm_exec_env_get_cur_frame(exec_env);
  6376. prev_frame = frame->prev_frame;
  6377. #if WASM_ENABLE_PERF_PROFILING != 0
  6378. if (frame->function) {
  6379. uint64 time_elapsed = os_time_thread_cputime_us() - frame->time_started;
  6380. frame->function->total_exec_time += time_elapsed;
  6381. frame->function->total_exec_cnt++;
  6382. /* parent function */
  6383. if (prev_frame)
  6384. prev_frame->function->children_exec_time += time_elapsed;
  6385. }
  6386. #endif
  6387. wasm_exec_env_free_wasm_frame(exec_env, frame);
  6388. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6389. }
  6390. void
  6391. llvm_jit_free_frame(WASMExecEnv *exec_env)
  6392. {
  6393. llvm_jit_free_frame_internal(exec_env);
  6394. }
  6395. #endif /* end of WASM_ENABLE_GC == 0 */
  6396. void
  6397. llvm_jit_frame_update_profile_info(WASMExecEnv *exec_env, bool alloc_frame)
  6398. {
  6399. #if WASM_ENABLE_PERF_PROFILING != 0
  6400. WASMInterpFrame *cur_frame = exec_env->cur_frame;
  6401. if (alloc_frame) {
  6402. cur_frame->time_started = os_time_thread_cputime_us();
  6403. }
  6404. else {
  6405. if (cur_frame->function) {
  6406. WASMInterpFrame *prev_frame = cur_frame->prev_frame;
  6407. uint64 time_elapsed =
  6408. os_time_thread_cputime_us() - cur_frame->time_started;
  6409. cur_frame->function->total_exec_time += time_elapsed;
  6410. cur_frame->function->total_exec_cnt++;
  6411. /* parent function */
  6412. if (prev_frame)
  6413. prev_frame->function->children_exec_time += time_elapsed;
  6414. }
  6415. }
  6416. #endif
  6417. #if WASM_ENABLE_MEMORY_PROFILING != 0
  6418. if (alloc_frame) {
  6419. #if WASM_ENABLE_GC == 0
  6420. uint32 wasm_stack_used = (uint8 *)exec_env->cur_frame
  6421. + (uint32)offsetof(WASMInterpFrame, lp)
  6422. - exec_env->wasm_stack.bottom;
  6423. #else
  6424. uint32 wasm_stack_used =
  6425. exec_env->wasm_stack.top - exec_env->wasm_stack.bottom;
  6426. #endif
  6427. if (wasm_stack_used > exec_env->max_wasm_stack_used)
  6428. exec_env->max_wasm_stack_used = wasm_stack_used;
  6429. }
  6430. #endif
  6431. }
  6432. #endif /* end of WASM_ENABLE_DUMP_CALL_STACK != 0 \
  6433. || WASM_ENABLE_PERF_PROFILING != 0 \
  6434. || WASM_ENABLE_AOT_STACK_FRAME != 0 */
  6435. static bool
  6436. llvm_jit_call_func_bytecode(WASMModuleInstance *module_inst,
  6437. WASMExecEnv *exec_env,
  6438. WASMFunctionInstance *function, uint32 argc,
  6439. uint32 argv[])
  6440. {
  6441. WASMFuncType *func_type = function->u.func->func_type;
  6442. uint32 result_count = func_type->result_count;
  6443. uint32 ext_ret_count = result_count > 1 ? result_count - 1 : 0;
  6444. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6445. bool ret = false;
  6446. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6447. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6448. if (!llvm_jit_alloc_frame(exec_env, function - module_inst->e->functions)) {
  6449. /* wasm operand stack overflow has been thrown,
  6450. no need to throw again */
  6451. return false;
  6452. }
  6453. #endif
  6454. if (ext_ret_count > 0) {
  6455. uint32 cell_num = 0, i;
  6456. uint8 *ext_ret_types = func_type->types + func_type->param_count + 1;
  6457. uint32 argv1_buf[32], *argv1 = argv1_buf, *ext_rets = NULL;
  6458. uint32 *argv_ret = argv;
  6459. uint32 ext_ret_cell = wasm_get_cell_num(ext_ret_types, ext_ret_count);
  6460. uint64 size;
  6461. /* Allocate memory all arguments */
  6462. size =
  6463. sizeof(uint32) * (uint64)argc /* original arguments */
  6464. + sizeof(void *)
  6465. * (uint64)ext_ret_count /* extra result values' addr */
  6466. + sizeof(uint32) * (uint64)ext_ret_cell; /* extra result values */
  6467. if (size > sizeof(argv1_buf)) {
  6468. if (size > UINT32_MAX
  6469. || !(argv1 = wasm_runtime_malloc((uint32)size))) {
  6470. wasm_set_exception(module_inst, "allocate memory failed");
  6471. ret = false;
  6472. goto fail;
  6473. }
  6474. }
  6475. /* Copy original arguments */
  6476. bh_memcpy_s(argv1, (uint32)size, argv, sizeof(uint32) * argc);
  6477. /* Get the extra result value's address */
  6478. ext_rets =
  6479. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6480. /* Append each extra result value's address to original arguments */
  6481. for (i = 0; i < ext_ret_count; i++) {
  6482. *(uintptr_t *)(argv1 + argc + sizeof(void *) / sizeof(uint32) * i) =
  6483. (uintptr_t)(ext_rets + cell_num);
  6484. cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
  6485. }
  6486. ret = wasm_runtime_invoke_native(
  6487. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL, NULL,
  6488. argv1, argc, argv);
  6489. if (!ret) {
  6490. if (argv1 != argv1_buf)
  6491. wasm_runtime_free(argv1);
  6492. goto fail;
  6493. }
  6494. /* Get extra result values */
  6495. switch (func_type->types[func_type->param_count]) {
  6496. case VALUE_TYPE_I32:
  6497. case VALUE_TYPE_F32:
  6498. #if WASM_ENABLE_REF_TYPES != 0
  6499. case VALUE_TYPE_FUNCREF:
  6500. case VALUE_TYPE_EXTERNREF:
  6501. #endif
  6502. argv_ret++;
  6503. break;
  6504. case VALUE_TYPE_I64:
  6505. case VALUE_TYPE_F64:
  6506. argv_ret += 2;
  6507. break;
  6508. #if WASM_ENABLE_SIMD != 0
  6509. case VALUE_TYPE_V128:
  6510. argv_ret += 4;
  6511. break;
  6512. #endif
  6513. default:
  6514. bh_assert(0);
  6515. break;
  6516. }
  6517. ext_rets =
  6518. argv1 + argc + sizeof(void *) / sizeof(uint32) * ext_ret_count;
  6519. bh_memcpy_s(argv_ret, sizeof(uint32) * cell_num, ext_rets,
  6520. sizeof(uint32) * cell_num);
  6521. if (argv1 != argv1_buf)
  6522. wasm_runtime_free(argv1);
  6523. ret = true;
  6524. }
  6525. else {
  6526. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  6527. /* Quick call if the quick jit entry is registered */
  6528. if (func_type->quick_aot_entry) {
  6529. void (*invoke_native)(void *func_ptr, void *exec_env, uint32 *argv,
  6530. uint32 *argv_ret) =
  6531. func_type->quick_aot_entry;
  6532. invoke_native(module_inst->func_ptrs[func_idx], exec_env, argv,
  6533. argv);
  6534. ret = !wasm_copy_exception(module_inst, NULL);
  6535. }
  6536. else
  6537. #endif
  6538. {
  6539. ret = wasm_runtime_invoke_native(
  6540. exec_env, module_inst->func_ptrs[func_idx], func_type, NULL,
  6541. NULL, argv, argc, argv);
  6542. if (ret)
  6543. ret = !wasm_copy_exception(module_inst, NULL);
  6544. }
  6545. }
  6546. fail:
  6547. #if (WASM_ENABLE_DUMP_CALL_STACK != 0) || (WASM_ENABLE_PERF_PROFILING != 0) \
  6548. || (WASM_ENABLE_AOT_STACK_FRAME != 0)
  6549. llvm_jit_free_frame_internal(exec_env);
  6550. #endif
  6551. return ret;
  6552. }
  6553. #endif /* end of WASM_ENABLE_JIT != 0 */
  6554. void
  6555. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  6556. WASMFunctionInstance *function, uint32 argc,
  6557. uint32 argv[])
  6558. {
  6559. WASMRuntimeFrame *frame = NULL, *prev_frame, *outs_area;
  6560. RunningMode running_mode =
  6561. wasm_runtime_get_running_mode((WASMModuleInstanceCommon *)module_inst);
  6562. /* Allocate sufficient cells for all kinds of return values. */
  6563. bool alloc_frame = true;
  6564. if (argc < function->param_cell_num) {
  6565. char buf[128];
  6566. snprintf(buf, sizeof(buf),
  6567. "invalid argument count %" PRIu32
  6568. ", must be no smaller than %u",
  6569. argc, function->param_cell_num);
  6570. wasm_set_exception(module_inst, buf);
  6571. return;
  6572. }
  6573. argc = function->param_cell_num;
  6574. #if defined(OS_ENABLE_HW_BOUND_CHECK) && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
  6575. /*
  6576. * wasm_runtime_detect_native_stack_overflow is done by
  6577. * call_wasm_with_hw_bound_check.
  6578. */
  6579. #else
  6580. if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
  6581. return;
  6582. }
  6583. #endif
  6584. if (!function->is_import_func) {
  6585. /* No need to alloc frame when calling LLVM JIT function */
  6586. #if WASM_ENABLE_JIT != 0
  6587. if (running_mode == Mode_LLVM_JIT) {
  6588. alloc_frame = false;
  6589. }
  6590. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0
  6591. else if (running_mode == Mode_Multi_Tier_JIT) {
  6592. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6593. if it is compiled, else call fast jit function */
  6594. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6595. if (module_inst->module->func_ptrs_compiled
  6596. [func_idx - module_inst->module->import_function_count]) {
  6597. alloc_frame = false;
  6598. }
  6599. }
  6600. #endif
  6601. #endif
  6602. }
  6603. if (alloc_frame) {
  6604. unsigned all_cell_num =
  6605. function->ret_cell_num > 2 ? function->ret_cell_num : 2;
  6606. unsigned frame_size;
  6607. prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  6608. /* This frame won't be used by JITed code, so only allocate interp
  6609. frame here. */
  6610. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  6611. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame)))
  6612. return;
  6613. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  6614. frame->function = NULL;
  6615. frame->ip = NULL;
  6616. /* There is no local variable. */
  6617. frame->sp = frame->lp + 0;
  6618. if ((uint8 *)(outs_area->lp + function->param_cell_num)
  6619. > exec_env->wasm_stack.top_boundary) {
  6620. wasm_set_exception(module_inst, "wasm operand stack overflow");
  6621. return;
  6622. }
  6623. if (argc > 0)
  6624. word_copy(outs_area->lp, argv, argc);
  6625. wasm_exec_env_set_cur_frame(exec_env, frame);
  6626. }
  6627. #if defined(os_writegsbase)
  6628. {
  6629. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  6630. if (memory_inst)
  6631. /* write base addr of linear memory to GS segment register */
  6632. os_writegsbase(memory_inst->memory_data);
  6633. }
  6634. #endif
  6635. if (function->is_import_func) {
  6636. #if WASM_ENABLE_MULTI_MODULE != 0
  6637. if (function->import_module_inst) {
  6638. wasm_interp_call_func_import(module_inst, exec_env, function,
  6639. frame);
  6640. }
  6641. else
  6642. #endif
  6643. {
  6644. /* it is a native function */
  6645. wasm_interp_call_func_native(module_inst, exec_env, function,
  6646. frame);
  6647. }
  6648. }
  6649. else {
  6650. if (running_mode == Mode_Interp) {
  6651. wasm_interp_call_func_bytecode(module_inst, exec_env, function,
  6652. frame);
  6653. }
  6654. #if WASM_ENABLE_FAST_JIT != 0
  6655. else if (running_mode == Mode_Fast_JIT) {
  6656. fast_jit_call_func_bytecode(module_inst, exec_env, function, frame);
  6657. }
  6658. #endif
  6659. #if WASM_ENABLE_JIT != 0
  6660. else if (running_mode == Mode_LLVM_JIT) {
  6661. llvm_jit_call_func_bytecode(module_inst, exec_env, function, argc,
  6662. argv);
  6663. }
  6664. #endif
  6665. #if WASM_ENABLE_LAZY_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
  6666. && WASM_ENABLE_JIT != 0
  6667. else if (running_mode == Mode_Multi_Tier_JIT) {
  6668. /* Tier-up from Fast JIT to LLVM JIT, call llvm jit function
  6669. if it is compiled, else call fast jit function */
  6670. uint32 func_idx = (uint32)(function - module_inst->e->functions);
  6671. if (module_inst->module->func_ptrs_compiled
  6672. [func_idx - module_inst->module->import_function_count]) {
  6673. llvm_jit_call_func_bytecode(module_inst, exec_env, function,
  6674. argc, argv);
  6675. }
  6676. else {
  6677. fast_jit_call_func_bytecode(module_inst, exec_env, function,
  6678. frame);
  6679. }
  6680. }
  6681. #endif
  6682. else {
  6683. /* There should always be a supported running mode selected */
  6684. bh_assert(0);
  6685. }
  6686. (void)wasm_interp_call_func_bytecode;
  6687. #if WASM_ENABLE_FAST_JIT != 0
  6688. (void)fast_jit_call_func_bytecode;
  6689. #endif
  6690. }
  6691. /* Output the return value to the caller */
  6692. if (!wasm_copy_exception(module_inst, NULL)) {
  6693. if (alloc_frame) {
  6694. uint32 i;
  6695. for (i = 0; i < function->ret_cell_num; i++) {
  6696. argv[i] = *(frame->sp + i - function->ret_cell_num);
  6697. }
  6698. }
  6699. }
  6700. else {
  6701. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  6702. if (wasm_interp_create_call_stack(exec_env)) {
  6703. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  6704. }
  6705. #endif
  6706. }
  6707. if (alloc_frame) {
  6708. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  6709. FREE_FRAME(exec_env, frame);
  6710. }
  6711. }