wasm_interp_classic.c 285 KB

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