wasm_mini_loader.c 270 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_FAST_JIT != 0
  14. #include "../fast-jit/jit_compiler.h"
  15. #include "../fast-jit/jit_codecache.h"
  16. #endif
  17. #if WASM_ENABLE_JIT != 0
  18. #include "../compilation/aot_llvm.h"
  19. #endif
  20. /* Read a value of given type from the address pointed to by the given
  21. pointer and increase the pointer to the position just after the
  22. value being read. */
  23. #define TEMPLATE_READ_VALUE(Type, p) \
  24. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  25. static void
  26. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  27. {
  28. if (error_buf != NULL)
  29. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  30. string);
  31. }
  32. #define CHECK_BUF(buf, buf_end, length) \
  33. do { \
  34. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  35. } while (0)
  36. #define CHECK_BUF1(buf, buf_end, length) \
  37. do { \
  38. bh_assert(buf + length >= buf && buf + length <= buf_end); \
  39. } while (0)
  40. #define skip_leb(p) while (*p++ & 0x80)
  41. #define skip_leb_int64(p, p_end) skip_leb(p)
  42. #define skip_leb_uint32(p, p_end) skip_leb(p)
  43. #define skip_leb_int32(p, p_end) skip_leb(p)
  44. static bool
  45. is_32bit_type(uint8 type)
  46. {
  47. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  48. #if WASM_ENABLE_REF_TYPES != 0
  49. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  50. #endif
  51. )
  52. return true;
  53. return false;
  54. }
  55. static bool
  56. is_64bit_type(uint8 type)
  57. {
  58. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  59. return true;
  60. return false;
  61. }
  62. static bool
  63. is_value_type(uint8 type)
  64. {
  65. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  66. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  67. #if WASM_ENABLE_REF_TYPES != 0
  68. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  69. #endif
  70. )
  71. return true;
  72. return false;
  73. }
  74. static bool
  75. is_byte_a_type(uint8 type)
  76. {
  77. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  78. }
  79. static void
  80. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  81. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  82. {
  83. const uint8 *buf = *p_buf;
  84. uint64 result = 0;
  85. uint32 shift = 0;
  86. uint32 offset = 0, bcnt = 0;
  87. uint64 byte;
  88. while (true) {
  89. bh_assert(bcnt + 1 <= (maxbits + 6) / 7);
  90. CHECK_BUF(buf, buf_end, offset + 1);
  91. byte = buf[offset];
  92. offset += 1;
  93. result |= ((byte & 0x7f) << shift);
  94. shift += 7;
  95. bcnt += 1;
  96. if ((byte & 0x80) == 0) {
  97. break;
  98. }
  99. }
  100. if (!sign && maxbits == 32 && shift >= maxbits) {
  101. /* The top bits set represent values > 32 bits */
  102. bh_assert(!(((uint8)byte) & 0xf0));
  103. }
  104. else if (sign && maxbits == 32) {
  105. if (shift < maxbits) {
  106. /* Sign extend, second highest bit is the sign bit */
  107. if ((uint8)byte & 0x40)
  108. result |= (~((uint64)0)) << shift;
  109. }
  110. else {
  111. /* The top bits should be a sign-extension of the sign bit */
  112. bool sign_bit_set = ((uint8)byte) & 0x8;
  113. int top_bits = ((uint8)byte) & 0xf0;
  114. bh_assert(!((sign_bit_set && top_bits != 0x70)
  115. || (!sign_bit_set && top_bits != 0)));
  116. (void)top_bits;
  117. (void)sign_bit_set;
  118. }
  119. }
  120. else if (sign && maxbits == 64) {
  121. if (shift < maxbits) {
  122. /* Sign extend, second highest bit is the sign bit */
  123. if ((uint8)byte & 0x40)
  124. result |= (~((uint64)0)) << shift;
  125. }
  126. else {
  127. /* The top bits should be a sign-extension of the sign bit */
  128. bool sign_bit_set = ((uint8)byte) & 0x1;
  129. int top_bits = ((uint8)byte) & 0xfe;
  130. bh_assert(!((sign_bit_set && top_bits != 0x7e)
  131. || (!sign_bit_set && top_bits != 0)));
  132. (void)top_bits;
  133. (void)sign_bit_set;
  134. }
  135. }
  136. *p_buf += offset;
  137. *p_result = result;
  138. }
  139. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  140. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  141. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  142. #define read_leb_int64(p, p_end, res) \
  143. do { \
  144. uint64 res64; \
  145. read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  146. error_buf_size); \
  147. res = (int64)res64; \
  148. } while (0)
  149. #define read_leb_uint32(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  153. error_buf_size); \
  154. res = (uint32)res64; \
  155. } while (0)
  156. #define read_leb_int32(p, p_end, res) \
  157. do { \
  158. uint64 res64; \
  159. read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  160. error_buf_size); \
  161. res = (int32)res64; \
  162. } while (0)
  163. static void *
  164. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  165. {
  166. void *mem;
  167. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  168. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  169. return NULL;
  170. }
  171. memset(mem, 0, (uint32)size);
  172. return mem;
  173. }
  174. static char *
  175. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  176. bool is_load_from_file_buf, char *error_buf,
  177. uint32 error_buf_size)
  178. {
  179. StringNode *node, *node_next;
  180. if (len == 0) {
  181. return "";
  182. }
  183. else if (is_load_from_file_buf) {
  184. /* As the file buffer can be referred to after loading, we use
  185. the previous byte of leb encoded size to adjust the string:
  186. move string 1 byte backward and then append '\0' */
  187. char *c_str = (char *)str - 1;
  188. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  189. c_str[len] = '\0';
  190. return c_str;
  191. }
  192. /* Search const str list */
  193. node = module->const_str_list;
  194. while (node) {
  195. node_next = node->next;
  196. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  197. break;
  198. node = node_next;
  199. }
  200. if (node) {
  201. LOG_DEBUG("reuse %s", node->str);
  202. return node->str;
  203. }
  204. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  205. error_buf_size))) {
  206. return NULL;
  207. }
  208. node->str = ((char *)node) + sizeof(StringNode);
  209. bh_memcpy_s(node->str, len + 1, str, len);
  210. node->str[len] = '\0';
  211. if (!module->const_str_list) {
  212. /* set as head */
  213. module->const_str_list = node;
  214. node->next = NULL;
  215. }
  216. else {
  217. /* insert it */
  218. node->next = module->const_str_list;
  219. module->const_str_list = node;
  220. }
  221. return node->str;
  222. }
  223. static void
  224. destroy_wasm_type(WASMFuncType *type)
  225. {
  226. if (type->ref_count > 1) {
  227. /* The type is referenced by other types
  228. of current wasm module */
  229. type->ref_count--;
  230. return;
  231. }
  232. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  233. && WASM_ENABLE_LAZY_JIT != 0
  234. if (type->call_to_llvm_jit_from_fast_jit)
  235. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  236. #endif
  237. wasm_runtime_free(type);
  238. }
  239. static bool
  240. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  241. InitializerExpression *init_expr, uint8 type, char *error_buf,
  242. uint32 error_buf_size)
  243. {
  244. const uint8 *p = *p_buf, *p_end = buf_end;
  245. uint8 flag, end_byte, *p_float;
  246. uint32 i;
  247. CHECK_BUF(p, p_end, 1);
  248. init_expr->init_expr_type = read_uint8(p);
  249. flag = init_expr->init_expr_type;
  250. switch (flag) {
  251. /* i32.const */
  252. case INIT_EXPR_TYPE_I32_CONST:
  253. bh_assert(type == VALUE_TYPE_I32);
  254. read_leb_int32(p, p_end, init_expr->u.i32);
  255. break;
  256. /* i64.const */
  257. case INIT_EXPR_TYPE_I64_CONST:
  258. bh_assert(type == VALUE_TYPE_I64);
  259. read_leb_int64(p, p_end, init_expr->u.i64);
  260. break;
  261. /* f32.const */
  262. case INIT_EXPR_TYPE_F32_CONST:
  263. bh_assert(type == VALUE_TYPE_F32);
  264. CHECK_BUF(p, p_end, 4);
  265. p_float = (uint8 *)&init_expr->u.f32;
  266. for (i = 0; i < sizeof(float32); i++)
  267. *p_float++ = *p++;
  268. break;
  269. /* f64.const */
  270. case INIT_EXPR_TYPE_F64_CONST:
  271. bh_assert(type == VALUE_TYPE_F64);
  272. CHECK_BUF(p, p_end, 8);
  273. p_float = (uint8 *)&init_expr->u.f64;
  274. for (i = 0; i < sizeof(float64); i++)
  275. *p_float++ = *p++;
  276. break;
  277. #if WASM_ENABLE_REF_TYPES != 0
  278. case INIT_EXPR_TYPE_FUNCREF_CONST:
  279. {
  280. bh_assert(type == VALUE_TYPE_FUNCREF);
  281. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  282. break;
  283. }
  284. case INIT_EXPR_TYPE_REFNULL_CONST:
  285. {
  286. uint8 reftype;
  287. CHECK_BUF(p, p_end, 1);
  288. reftype = read_uint8(p);
  289. bh_assert(type == reftype);
  290. init_expr->u.ref_index = NULL_REF;
  291. (void)reftype;
  292. break;
  293. }
  294. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  295. /* get_global */
  296. case INIT_EXPR_TYPE_GET_GLOBAL:
  297. read_leb_uint32(p, p_end, init_expr->u.global_index);
  298. break;
  299. default:
  300. bh_assert(0);
  301. break;
  302. }
  303. CHECK_BUF(p, p_end, 1);
  304. end_byte = read_uint8(p);
  305. bh_assert(end_byte == 0x0b);
  306. *p_buf = p;
  307. (void)end_byte;
  308. return true;
  309. }
  310. static bool
  311. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  312. char *error_buf, uint32 error_buf_size)
  313. {
  314. const uint8 *p = buf, *p_end = buf_end, *p_org;
  315. uint32 type_count, param_count, result_count, i, j;
  316. uint32 param_cell_num, ret_cell_num;
  317. uint64 total_size;
  318. uint8 flag;
  319. WASMFuncType *type;
  320. read_leb_uint32(p, p_end, type_count);
  321. if (type_count) {
  322. module->type_count = type_count;
  323. total_size = sizeof(WASMFuncType *) * (uint64)type_count;
  324. if (!(module->types =
  325. loader_malloc(total_size, error_buf, error_buf_size))) {
  326. return false;
  327. }
  328. for (i = 0; i < type_count; i++) {
  329. CHECK_BUF(p, p_end, 1);
  330. flag = read_uint8(p);
  331. bh_assert(flag == 0x60);
  332. read_leb_uint32(p, p_end, param_count);
  333. /* Resolve param count and result count firstly */
  334. p_org = p;
  335. CHECK_BUF(p, p_end, param_count);
  336. p += param_count;
  337. read_leb_uint32(p, p_end, result_count);
  338. CHECK_BUF(p, p_end, result_count);
  339. p = p_org;
  340. bh_assert(param_count <= UINT16_MAX && result_count <= UINT16_MAX);
  341. total_size = offsetof(WASMFuncType, types)
  342. + sizeof(uint8) * (uint64)(param_count + result_count);
  343. if (!(type = module->types[i] =
  344. loader_malloc(total_size, error_buf, error_buf_size))) {
  345. return false;
  346. }
  347. /* Resolve param types and result types */
  348. type->ref_count = 1;
  349. type->param_count = (uint16)param_count;
  350. type->result_count = (uint16)result_count;
  351. for (j = 0; j < param_count; j++) {
  352. CHECK_BUF(p, p_end, 1);
  353. type->types[j] = read_uint8(p);
  354. }
  355. read_leb_uint32(p, p_end, result_count);
  356. for (j = 0; j < result_count; j++) {
  357. CHECK_BUF(p, p_end, 1);
  358. type->types[param_count + j] = read_uint8(p);
  359. }
  360. for (j = 0; j < param_count + result_count; j++) {
  361. bh_assert(is_value_type(type->types[j]));
  362. }
  363. param_cell_num = wasm_get_cell_num(type->types, param_count);
  364. ret_cell_num =
  365. wasm_get_cell_num(type->types + param_count, result_count);
  366. bh_assert(param_cell_num <= UINT16_MAX
  367. && ret_cell_num <= UINT16_MAX);
  368. type->param_cell_num = (uint16)param_cell_num;
  369. type->ret_cell_num = (uint16)ret_cell_num;
  370. /* If there is already a same type created, use it instead */
  371. for (j = 0; j < i; ++j) {
  372. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  373. bh_assert(module->types[j]->ref_count != UINT16_MAX);
  374. destroy_wasm_type(type);
  375. module->types[i] = module->types[j];
  376. module->types[j]->ref_count++;
  377. break;
  378. }
  379. }
  380. }
  381. }
  382. bh_assert(p == p_end);
  383. LOG_VERBOSE("Load type section success.\n");
  384. (void)flag;
  385. return true;
  386. }
  387. static void
  388. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  389. {
  390. uint32 default_max_size =
  391. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  392. if (max_size_flag) {
  393. /* module defines the table limitation */
  394. bh_assert(init_size <= *max_size);
  395. if (init_size < *max_size) {
  396. *max_size =
  397. *max_size < default_max_size ? *max_size : default_max_size;
  398. }
  399. }
  400. else {
  401. /* partial defined table limitation, gives a default value */
  402. *max_size = default_max_size;
  403. }
  404. }
  405. static bool
  406. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  407. const WASMModule *parent_module,
  408. const char *sub_module_name, const char *function_name,
  409. WASMFunctionImport *function, char *error_buf,
  410. uint32 error_buf_size)
  411. {
  412. const uint8 *p = *p_buf, *p_end = buf_end;
  413. uint32 declare_type_index = 0;
  414. WASMFuncType *declare_func_type = NULL;
  415. WASMFunction *linked_func = NULL;
  416. const char *linked_signature = NULL;
  417. void *linked_attachment = NULL;
  418. bool linked_call_conv_raw = false;
  419. read_leb_uint32(p, p_end, declare_type_index);
  420. *p_buf = p;
  421. bh_assert(declare_type_index < parent_module->type_count);
  422. declare_func_type = parent_module->types[declare_type_index];
  423. /* check built-in modules */
  424. linked_func = wasm_native_resolve_symbol(
  425. sub_module_name, function_name, declare_func_type, &linked_signature,
  426. &linked_attachment, &linked_call_conv_raw);
  427. function->module_name = (char *)sub_module_name;
  428. function->field_name = (char *)function_name;
  429. function->func_type = declare_func_type;
  430. function->func_ptr_linked = linked_func;
  431. function->signature = linked_signature;
  432. function->attachment = linked_attachment;
  433. function->call_conv_raw = linked_call_conv_raw;
  434. return true;
  435. }
  436. static bool
  437. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  438. WASMModule *parent_module, const char *sub_module_name,
  439. const char *table_name, WASMTableImport *table,
  440. char *error_buf, uint32 error_buf_size)
  441. {
  442. const uint8 *p = *p_buf, *p_end = buf_end;
  443. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  444. declare_init_size = 0, declare_max_size = 0;
  445. CHECK_BUF(p, p_end, 1);
  446. /* 0x70 or 0x6F */
  447. declare_elem_type = read_uint8(p);
  448. bh_assert(VALUE_TYPE_FUNCREF == declare_elem_type
  449. #if WASM_ENABLE_REF_TYPES != 0
  450. || VALUE_TYPE_EXTERNREF == declare_elem_type
  451. #endif
  452. );
  453. read_leb_uint32(p, p_end, declare_max_size_flag);
  454. read_leb_uint32(p, p_end, declare_init_size);
  455. if (declare_max_size_flag & 1) {
  456. read_leb_uint32(p, p_end, declare_max_size);
  457. bh_assert(table->init_size <= table->max_size);
  458. }
  459. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  460. &declare_max_size);
  461. *p_buf = p;
  462. bh_assert(
  463. !((declare_max_size_flag & 1) && declare_init_size > declare_max_size));
  464. /* now we believe all declaration are ok */
  465. table->elem_type = declare_elem_type;
  466. table->init_size = declare_init_size;
  467. table->flags = declare_max_size_flag;
  468. table->max_size = declare_max_size;
  469. return true;
  470. }
  471. static bool
  472. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  473. WASMModule *parent_module, const char *sub_module_name,
  474. const char *memory_name, WASMMemoryImport *memory,
  475. char *error_buf, uint32 error_buf_size)
  476. {
  477. const uint8 *p = *p_buf, *p_end = buf_end;
  478. #if WASM_ENABLE_APP_FRAMEWORK != 0
  479. uint32 pool_size = wasm_runtime_memory_pool_size();
  480. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  481. / DEFAULT_NUM_BYTES_PER_PAGE;
  482. #else
  483. uint32 max_page_count = DEFAULT_MAX_PAGES;
  484. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  485. uint32 declare_max_page_count_flag = 0;
  486. uint32 declare_init_page_count = 0;
  487. uint32 declare_max_page_count = 0;
  488. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  489. read_leb_uint32(p, p_end, declare_init_page_count);
  490. bh_assert(declare_init_page_count <= 65536);
  491. if (declare_max_page_count_flag & 1) {
  492. read_leb_uint32(p, p_end, declare_max_page_count);
  493. bh_assert(declare_init_page_count <= declare_max_page_count);
  494. bh_assert(declare_max_page_count <= 65536);
  495. if (declare_max_page_count > max_page_count) {
  496. declare_max_page_count = max_page_count;
  497. }
  498. }
  499. else {
  500. /* Limit the maximum memory size to max_page_count */
  501. declare_max_page_count = max_page_count;
  502. }
  503. /* now we believe all declaration are ok */
  504. memory->flags = declare_max_page_count_flag;
  505. memory->init_page_count = declare_init_page_count;
  506. memory->max_page_count = declare_max_page_count;
  507. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  508. *p_buf = p;
  509. return true;
  510. }
  511. static bool
  512. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  513. const WASMModule *parent_module, char *sub_module_name,
  514. char *global_name, WASMGlobalImport *global, char *error_buf,
  515. uint32 error_buf_size)
  516. {
  517. const uint8 *p = *p_buf, *p_end = buf_end;
  518. uint8 declare_type = 0;
  519. uint8 declare_mutable = 0;
  520. bool is_mutable = false;
  521. bool ret = false;
  522. CHECK_BUF(p, p_end, 2);
  523. declare_type = read_uint8(p);
  524. declare_mutable = read_uint8(p);
  525. *p_buf = p;
  526. bh_assert(declare_mutable < 2);
  527. is_mutable = declare_mutable & 1 ? true : false;
  528. #if WASM_ENABLE_LIBC_BUILTIN != 0
  529. /* check built-in modules */
  530. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  531. global);
  532. if (ret) {
  533. bh_assert(global->type == declare_type
  534. && global->is_mutable != declare_mutable);
  535. }
  536. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  537. global->is_linked = ret;
  538. global->module_name = sub_module_name;
  539. global->field_name = global_name;
  540. global->type = declare_type;
  541. global->is_mutable = is_mutable;
  542. (void)p_end;
  543. return true;
  544. }
  545. static bool
  546. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  547. char *error_buf, uint32 error_buf_size)
  548. {
  549. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  550. CHECK_BUF(p, p_end, 1);
  551. /* 0x70 or 0x6F */
  552. table->elem_type = read_uint8(p);
  553. bh_assert((VALUE_TYPE_FUNCREF == table->elem_type)
  554. #if WASM_ENABLE_REF_TYPES != 0
  555. || VALUE_TYPE_EXTERNREF == table->elem_type
  556. #endif
  557. );
  558. p_org = p;
  559. read_leb_uint32(p, p_end, table->flags);
  560. bh_assert(p - p_org <= 1);
  561. bh_assert(table->flags <= 1);
  562. (void)p_org;
  563. read_leb_uint32(p, p_end, table->init_size);
  564. if (table->flags == 1) {
  565. read_leb_uint32(p, p_end, table->max_size);
  566. bh_assert(table->init_size <= table->max_size);
  567. }
  568. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  569. *p_buf = p;
  570. return true;
  571. }
  572. static bool
  573. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  574. char *error_buf, uint32 error_buf_size)
  575. {
  576. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  577. #if WASM_ENABLE_APP_FRAMEWORK != 0
  578. uint32 pool_size = wasm_runtime_memory_pool_size();
  579. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  580. / DEFAULT_NUM_BYTES_PER_PAGE;
  581. #else
  582. uint32 max_page_count = DEFAULT_MAX_PAGES;
  583. #endif
  584. p_org = p;
  585. read_leb_uint32(p, p_end, memory->flags);
  586. bh_assert(p - p_org <= 1);
  587. (void)p_org;
  588. #if WASM_ENABLE_SHARED_MEMORY == 0
  589. bh_assert(memory->flags <= 1);
  590. #else
  591. bh_assert(memory->flags <= 3 && memory->flags != 2);
  592. #endif
  593. read_leb_uint32(p, p_end, memory->init_page_count);
  594. bh_assert(memory->init_page_count <= 65536);
  595. if (memory->flags & 1) {
  596. read_leb_uint32(p, p_end, memory->max_page_count);
  597. bh_assert(memory->init_page_count <= memory->max_page_count);
  598. bh_assert(memory->max_page_count <= 65536);
  599. if (memory->max_page_count > max_page_count)
  600. memory->max_page_count = max_page_count;
  601. }
  602. else {
  603. /* Limit the maximum memory size to max_page_count */
  604. memory->max_page_count = max_page_count;
  605. }
  606. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  607. *p_buf = p;
  608. return true;
  609. }
  610. static bool
  611. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  612. bool is_load_from_file_buf, char *error_buf,
  613. uint32 error_buf_size)
  614. {
  615. const uint8 *p = buf, *p_end = buf_end, *p_old;
  616. uint32 import_count, name_len, type_index, i, u32, flags;
  617. uint64 total_size;
  618. WASMImport *import;
  619. WASMImport *import_functions = NULL, *import_tables = NULL;
  620. WASMImport *import_memories = NULL, *import_globals = NULL;
  621. char *sub_module_name, *field_name;
  622. uint8 u8, kind;
  623. read_leb_uint32(p, p_end, import_count);
  624. if (import_count) {
  625. module->import_count = import_count;
  626. total_size = sizeof(WASMImport) * (uint64)import_count;
  627. if (!(module->imports =
  628. loader_malloc(total_size, error_buf, error_buf_size))) {
  629. return false;
  630. }
  631. p_old = p;
  632. /* Scan firstly to get import count of each type */
  633. for (i = 0; i < import_count; i++) {
  634. /* module name */
  635. read_leb_uint32(p, p_end, name_len);
  636. CHECK_BUF(p, p_end, name_len);
  637. p += name_len;
  638. /* field name */
  639. read_leb_uint32(p, p_end, name_len);
  640. CHECK_BUF(p, p_end, name_len);
  641. p += name_len;
  642. CHECK_BUF(p, p_end, 1);
  643. /* 0x00/0x01/0x02/0x03 */
  644. kind = read_uint8(p);
  645. switch (kind) {
  646. case IMPORT_KIND_FUNC: /* import function */
  647. read_leb_uint32(p, p_end, type_index);
  648. module->import_function_count++;
  649. break;
  650. case IMPORT_KIND_TABLE: /* import table */
  651. CHECK_BUF(p, p_end, 1);
  652. /* 0x70 */
  653. u8 = read_uint8(p);
  654. read_leb_uint32(p, p_end, flags);
  655. read_leb_uint32(p, p_end, u32);
  656. if (flags & 1)
  657. read_leb_uint32(p, p_end, u32);
  658. module->import_table_count++;
  659. #if WASM_ENABLE_REF_TYPES == 0
  660. bh_assert(module->import_table_count <= 1);
  661. #endif
  662. break;
  663. case IMPORT_KIND_MEMORY: /* import memory */
  664. read_leb_uint32(p, p_end, flags);
  665. read_leb_uint32(p, p_end, u32);
  666. if (flags & 1)
  667. read_leb_uint32(p, p_end, u32);
  668. module->import_memory_count++;
  669. bh_assert(module->import_memory_count <= 1);
  670. break;
  671. case IMPORT_KIND_GLOBAL: /* import global */
  672. CHECK_BUF(p, p_end, 2);
  673. p += 2;
  674. module->import_global_count++;
  675. break;
  676. default:
  677. bh_assert(0);
  678. break;
  679. }
  680. }
  681. if (module->import_function_count)
  682. import_functions = module->import_functions = module->imports;
  683. if (module->import_table_count)
  684. import_tables = module->import_tables =
  685. module->imports + module->import_function_count;
  686. if (module->import_memory_count)
  687. import_memories = module->import_memories =
  688. module->imports + module->import_function_count
  689. + module->import_table_count;
  690. if (module->import_global_count)
  691. import_globals = module->import_globals =
  692. module->imports + module->import_function_count
  693. + module->import_table_count + module->import_memory_count;
  694. p = p_old;
  695. /* Scan again to resolve the data */
  696. for (i = 0; i < import_count; i++) {
  697. WASMModule *sub_module = NULL;
  698. /* load module name */
  699. read_leb_uint32(p, p_end, name_len);
  700. CHECK_BUF(p, p_end, name_len);
  701. if (!(sub_module_name = const_str_list_insert(
  702. p, name_len, module, is_load_from_file_buf, error_buf,
  703. error_buf_size))) {
  704. return false;
  705. }
  706. p += name_len;
  707. /* load field name */
  708. read_leb_uint32(p, p_end, name_len);
  709. CHECK_BUF(p, p_end, name_len);
  710. if (!(field_name = const_str_list_insert(
  711. p, name_len, module, is_load_from_file_buf, error_buf,
  712. error_buf_size))) {
  713. return false;
  714. }
  715. p += name_len;
  716. CHECK_BUF(p, p_end, 1);
  717. /* 0x00/0x01/0x02/0x03 */
  718. kind = read_uint8(p);
  719. LOG_DEBUG("import #%d: (%s, %s), kind: %d", i, sub_module_name,
  720. field_name, kind);
  721. switch (kind) {
  722. case IMPORT_KIND_FUNC: /* import function */
  723. bh_assert(import_functions);
  724. import = import_functions++;
  725. if (!load_function_import(
  726. &p, p_end, module, sub_module_name, field_name,
  727. &import->u.function, error_buf, error_buf_size)) {
  728. return false;
  729. }
  730. break;
  731. case IMPORT_KIND_TABLE: /* import table */
  732. bh_assert(import_tables);
  733. import = import_tables++;
  734. if (!load_table_import(&p, p_end, module, sub_module_name,
  735. field_name, &import->u.table,
  736. error_buf, error_buf_size)) {
  737. LOG_DEBUG("can not import such a table (%s,%s)",
  738. sub_module_name, field_name);
  739. return false;
  740. }
  741. break;
  742. case IMPORT_KIND_MEMORY: /* import memory */
  743. bh_assert(import_memories);
  744. import = import_memories++;
  745. if (!load_memory_import(&p, p_end, module, sub_module_name,
  746. field_name, &import->u.memory,
  747. error_buf, error_buf_size)) {
  748. return false;
  749. }
  750. break;
  751. case IMPORT_KIND_GLOBAL: /* import global */
  752. bh_assert(import_globals);
  753. import = import_globals++;
  754. if (!load_global_import(&p, p_end, module, sub_module_name,
  755. field_name, &import->u.global,
  756. error_buf, error_buf_size)) {
  757. return false;
  758. }
  759. break;
  760. default:
  761. bh_assert(0);
  762. import = NULL;
  763. break;
  764. }
  765. import->kind = kind;
  766. import->u.names.module_name = sub_module_name;
  767. import->u.names.field_name = field_name;
  768. (void)sub_module;
  769. }
  770. #if WASM_ENABLE_LIBC_WASI != 0
  771. import = module->import_functions;
  772. for (i = 0; i < module->import_function_count; i++, import++) {
  773. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  774. || !strcmp(import->u.names.module_name,
  775. "wasi_snapshot_preview1")) {
  776. module->import_wasi_api = true;
  777. break;
  778. }
  779. }
  780. #endif
  781. }
  782. bh_assert(p == p_end);
  783. LOG_VERBOSE("Load import section success.\n");
  784. (void)u8;
  785. (void)u32;
  786. (void)type_index;
  787. return true;
  788. }
  789. static bool
  790. init_function_local_offsets(WASMFunction *func, char *error_buf,
  791. uint32 error_buf_size)
  792. {
  793. WASMFuncType *param_type = func->func_type;
  794. uint32 param_count = param_type->param_count;
  795. uint8 *param_types = param_type->types;
  796. uint32 local_count = func->local_count;
  797. uint8 *local_types = func->local_types;
  798. uint32 i, local_offset = 0;
  799. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  800. if (total_size > 0
  801. && !(func->local_offsets =
  802. loader_malloc(total_size, error_buf, error_buf_size))) {
  803. return false;
  804. }
  805. for (i = 0; i < param_count; i++) {
  806. func->local_offsets[i] = (uint16)local_offset;
  807. local_offset += wasm_value_type_cell_num(param_types[i]);
  808. }
  809. for (i = 0; i < local_count; i++) {
  810. func->local_offsets[param_count + i] = (uint16)local_offset;
  811. local_offset += wasm_value_type_cell_num(local_types[i]);
  812. }
  813. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  814. return true;
  815. }
  816. static bool
  817. load_function_section(const uint8 *buf, const uint8 *buf_end,
  818. const uint8 *buf_code, const uint8 *buf_code_end,
  819. WASMModule *module, char *error_buf,
  820. uint32 error_buf_size)
  821. {
  822. const uint8 *p = buf, *p_end = buf_end;
  823. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  824. uint32 func_count;
  825. uint64 total_size;
  826. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  827. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  828. uint8 type;
  829. WASMFunction *func;
  830. read_leb_uint32(p, p_end, func_count);
  831. if (buf_code)
  832. read_leb_uint32(p_code, buf_code_end, code_count);
  833. bh_assert(func_count == code_count);
  834. if (func_count) {
  835. module->function_count = func_count;
  836. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  837. if (!(module->functions =
  838. loader_malloc(total_size, error_buf, error_buf_size))) {
  839. return false;
  840. }
  841. for (i = 0; i < func_count; i++) {
  842. /* Resolve function type */
  843. read_leb_uint32(p, p_end, type_index);
  844. bh_assert(type_index < module->type_count);
  845. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  846. type_index = wasm_get_smallest_type_idx(
  847. module->types, module->type_count, type_index);
  848. #endif
  849. read_leb_uint32(p_code, buf_code_end, code_size);
  850. bh_assert(code_size > 0 && p_code + code_size <= buf_code_end);
  851. /* Resolve local set count */
  852. p_code_end = p_code + code_size;
  853. local_count = 0;
  854. read_leb_uint32(p_code, buf_code_end, local_set_count);
  855. p_code_save = p_code;
  856. /* Calculate total local count */
  857. for (j = 0; j < local_set_count; j++) {
  858. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  859. bh_assert(sub_local_count <= UINT32_MAX - local_count);
  860. CHECK_BUF(p_code, buf_code_end, 1);
  861. /* 0x7F/0x7E/0x7D/0x7C */
  862. type = read_uint8(p_code);
  863. local_count += sub_local_count;
  864. }
  865. /* Alloc memory, layout: function structure + local types */
  866. code_size = (uint32)(p_code_end - p_code);
  867. total_size = sizeof(WASMFunction) + (uint64)local_count;
  868. if (!(func = module->functions[i] =
  869. loader_malloc(total_size, error_buf, error_buf_size))) {
  870. return false;
  871. }
  872. /* Set function type, local count, code size and code body */
  873. func->func_type = module->types[type_index];
  874. func->local_count = local_count;
  875. if (local_count > 0)
  876. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  877. func->code_size = code_size;
  878. /*
  879. * we shall make a copy of code body [p_code, p_code + code_size]
  880. * when we are worrying about inappropriate releasing behaviour.
  881. * all code bodies are actually in a buffer which user allocates in
  882. * his embedding environment and we don't have power on them.
  883. * it will be like:
  884. * code_body_cp = malloc(code_size);
  885. * memcpy(code_body_cp, p_code, code_size);
  886. * func->code = code_body_cp;
  887. */
  888. func->code = (uint8 *)p_code;
  889. /* Load each local type */
  890. p_code = p_code_save;
  891. local_type_index = 0;
  892. for (j = 0; j < local_set_count; j++) {
  893. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  894. /* Note: sub_local_count is allowed to be 0 */
  895. bh_assert(local_type_index <= UINT32_MAX - sub_local_count
  896. && local_type_index + sub_local_count <= local_count);
  897. CHECK_BUF(p_code, buf_code_end, 1);
  898. /* 0x7F/0x7E/0x7D/0x7C */
  899. type = read_uint8(p_code);
  900. bh_assert(is_value_type(type));
  901. for (k = 0; k < sub_local_count; k++) {
  902. func->local_types[local_type_index++] = type;
  903. }
  904. }
  905. func->param_cell_num = func->func_type->param_cell_num;
  906. func->ret_cell_num = func->func_type->ret_cell_num;
  907. local_cell_num =
  908. wasm_get_cell_num(func->local_types, func->local_count);
  909. bh_assert(local_cell_num <= UINT16_MAX);
  910. func->local_cell_num = (uint16)local_cell_num;
  911. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  912. return false;
  913. p_code = p_code_end;
  914. }
  915. }
  916. bh_assert(p == p_end);
  917. LOG_VERBOSE("Load function section success.\n");
  918. (void)code_count;
  919. return true;
  920. }
  921. static bool
  922. check_function_index(const WASMModule *module, uint32 function_index,
  923. char *error_buf, uint32 error_buf_size)
  924. {
  925. return (function_index
  926. < module->import_function_count + module->function_count);
  927. }
  928. static bool
  929. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  930. char *error_buf, uint32 error_buf_size)
  931. {
  932. const uint8 *p = buf, *p_end = buf_end;
  933. uint32 table_count, i;
  934. uint64 total_size;
  935. WASMTable *table;
  936. read_leb_uint32(p, p_end, table_count);
  937. #if WASM_ENABLE_REF_TYPES == 0
  938. bh_assert(module->import_table_count + table_count <= 1);
  939. #endif
  940. if (table_count) {
  941. module->table_count = table_count;
  942. total_size = sizeof(WASMTable) * (uint64)table_count;
  943. if (!(module->tables =
  944. loader_malloc(total_size, error_buf, error_buf_size))) {
  945. return false;
  946. }
  947. /* load each table */
  948. table = module->tables;
  949. for (i = 0; i < table_count; i++, table++)
  950. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  951. return false;
  952. }
  953. bh_assert(p == p_end);
  954. LOG_VERBOSE("Load table section success.\n");
  955. return true;
  956. }
  957. static bool
  958. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  959. char *error_buf, uint32 error_buf_size)
  960. {
  961. const uint8 *p = buf, *p_end = buf_end;
  962. uint32 memory_count, i;
  963. uint64 total_size;
  964. WASMMemory *memory;
  965. read_leb_uint32(p, p_end, memory_count);
  966. bh_assert(module->import_memory_count + memory_count <= 1);
  967. if (memory_count) {
  968. module->memory_count = memory_count;
  969. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  970. if (!(module->memories =
  971. loader_malloc(total_size, error_buf, error_buf_size))) {
  972. return false;
  973. }
  974. /* load each memory */
  975. memory = module->memories;
  976. for (i = 0; i < memory_count; i++, memory++)
  977. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  978. return false;
  979. }
  980. bh_assert(p == p_end);
  981. LOG_VERBOSE("Load memory section success.\n");
  982. return true;
  983. }
  984. static bool
  985. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  986. char *error_buf, uint32 error_buf_size)
  987. {
  988. const uint8 *p = buf, *p_end = buf_end;
  989. uint32 global_count, i;
  990. uint64 total_size;
  991. WASMGlobal *global;
  992. uint8 mutable;
  993. read_leb_uint32(p, p_end, global_count);
  994. if (global_count) {
  995. module->global_count = global_count;
  996. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  997. if (!(module->globals =
  998. loader_malloc(total_size, error_buf, error_buf_size))) {
  999. return false;
  1000. }
  1001. global = module->globals;
  1002. for (i = 0; i < global_count; i++, global++) {
  1003. CHECK_BUF(p, p_end, 2);
  1004. global->type = read_uint8(p);
  1005. mutable = read_uint8(p);
  1006. bh_assert(mutable < 2);
  1007. global->is_mutable = mutable ? true : false;
  1008. /* initialize expression */
  1009. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  1010. error_buf, error_buf_size))
  1011. return false;
  1012. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1013. /**
  1014. * Currently, constant expressions occurring as initializers
  1015. * of globals are further constrained in that contained
  1016. * global.get instructions are
  1017. * only allowed to refer to imported globals.
  1018. */
  1019. uint32 target_global_index = global->init_expr.u.global_index;
  1020. bh_assert(target_global_index < module->import_global_count);
  1021. (void)target_global_index;
  1022. }
  1023. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1024. == global->init_expr.init_expr_type) {
  1025. bh_assert(global->init_expr.u.ref_index
  1026. < module->import_function_count
  1027. + module->function_count);
  1028. }
  1029. }
  1030. }
  1031. bh_assert(p == p_end);
  1032. LOG_VERBOSE("Load global section success.\n");
  1033. return true;
  1034. }
  1035. static bool
  1036. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1037. bool is_load_from_file_buf, char *error_buf,
  1038. uint32 error_buf_size)
  1039. {
  1040. const uint8 *p = buf, *p_end = buf_end;
  1041. uint32 export_count, i, j, index;
  1042. uint64 total_size;
  1043. uint32 str_len;
  1044. WASMExport *export;
  1045. const char *name;
  1046. read_leb_uint32(p, p_end, export_count);
  1047. if (export_count) {
  1048. module->export_count = export_count;
  1049. total_size = sizeof(WASMExport) * (uint64)export_count;
  1050. if (!(module->exports =
  1051. loader_malloc(total_size, error_buf, error_buf_size))) {
  1052. return false;
  1053. }
  1054. export = module->exports;
  1055. for (i = 0; i < export_count; i++, export ++) {
  1056. read_leb_uint32(p, p_end, str_len);
  1057. CHECK_BUF(p, p_end, str_len);
  1058. for (j = 0; j < i; j++) {
  1059. name = module->exports[j].name;
  1060. bh_assert(!(strlen(name) == str_len
  1061. && memcmp(name, p, str_len) == 0));
  1062. }
  1063. if (!(export->name = const_str_list_insert(
  1064. p, str_len, module, is_load_from_file_buf, error_buf,
  1065. error_buf_size))) {
  1066. return false;
  1067. }
  1068. p += str_len;
  1069. CHECK_BUF(p, p_end, 1);
  1070. export->kind = read_uint8(p);
  1071. read_leb_uint32(p, p_end, index);
  1072. export->index = index;
  1073. switch (export->kind) {
  1074. /* function index */
  1075. case EXPORT_KIND_FUNC:
  1076. bh_assert(index < module->function_count
  1077. + module->import_function_count);
  1078. break;
  1079. /* table index */
  1080. case EXPORT_KIND_TABLE:
  1081. bh_assert(index < module->table_count
  1082. + module->import_table_count);
  1083. break;
  1084. /* memory index */
  1085. case EXPORT_KIND_MEMORY:
  1086. bh_assert(index < module->memory_count
  1087. + module->import_memory_count);
  1088. break;
  1089. /* global index */
  1090. case EXPORT_KIND_GLOBAL:
  1091. bh_assert(index < module->global_count
  1092. + module->import_global_count);
  1093. break;
  1094. default:
  1095. bh_assert(0);
  1096. break;
  1097. }
  1098. }
  1099. }
  1100. bh_assert(p == p_end);
  1101. LOG_VERBOSE("Load export section success.\n");
  1102. (void)name;
  1103. return true;
  1104. }
  1105. static bool
  1106. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  1107. uint32 error_buf_size)
  1108. {
  1109. #if WASM_ENABLE_REF_TYPES == 0
  1110. if (table_index != 0) {
  1111. return false;
  1112. }
  1113. #endif
  1114. if (table_index >= module->import_table_count + module->table_count) {
  1115. return false;
  1116. }
  1117. return true;
  1118. }
  1119. static bool
  1120. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1121. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  1122. {
  1123. const uint8 *p = *p_buf, *p_end = buf_end;
  1124. uint32 table_index;
  1125. read_leb_uint32(p, p_end, table_index);
  1126. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  1127. return false;
  1128. }
  1129. *p_table_index = table_index;
  1130. *p_buf = p;
  1131. return true;
  1132. }
  1133. #if WASM_ENABLE_REF_TYPES != 0
  1134. static bool
  1135. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  1136. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  1137. {
  1138. const uint8 *p = *p_buf, *p_end = buf_end;
  1139. uint8 elem_type;
  1140. CHECK_BUF(p, p_end, 1);
  1141. elem_type = read_uint8(p);
  1142. if ((elemkind_zero && elem_type != 0)
  1143. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  1144. && elem_type != VALUE_TYPE_EXTERNREF)) {
  1145. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  1146. return false;
  1147. }
  1148. if (elemkind_zero)
  1149. *p_elem_type = VALUE_TYPE_FUNCREF;
  1150. else
  1151. *p_elem_type = elem_type;
  1152. *p_buf = p;
  1153. (void)p_end;
  1154. return true;
  1155. }
  1156. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  1157. static bool
  1158. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  1159. WASMModule *module, WASMTableSeg *table_segment,
  1160. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  1161. {
  1162. const uint8 *p = *p_buf, *p_end = buf_end;
  1163. uint32 function_count, function_index = 0, i;
  1164. uint64 total_size;
  1165. read_leb_uint32(p, p_end, function_count);
  1166. table_segment->function_count = function_count;
  1167. total_size = sizeof(uintptr_t) * (uint64)function_count;
  1168. if (total_size > 0
  1169. && !(table_segment->func_indexes = (uintptr_t *)loader_malloc(
  1170. total_size, error_buf, error_buf_size))) {
  1171. return false;
  1172. }
  1173. for (i = 0; i < function_count; i++) {
  1174. InitializerExpression init_expr = { 0 };
  1175. #if WASM_ENABLE_REF_TYPES != 0
  1176. if (!use_init_expr) {
  1177. read_leb_uint32(p, p_end, function_index);
  1178. }
  1179. else {
  1180. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  1181. error_buf, error_buf_size))
  1182. return false;
  1183. function_index = init_expr.u.ref_index;
  1184. }
  1185. #else
  1186. read_leb_uint32(p, p_end, function_index);
  1187. #endif
  1188. /* since we are using -1 to indicate ref.null */
  1189. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  1190. && !check_function_index(module, function_index, error_buf,
  1191. error_buf_size)) {
  1192. return false;
  1193. }
  1194. table_segment->func_indexes[i] = function_index;
  1195. }
  1196. *p_buf = p;
  1197. return true;
  1198. }
  1199. static bool
  1200. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  1201. WASMModule *module, char *error_buf,
  1202. uint32 error_buf_size)
  1203. {
  1204. const uint8 *p = buf, *p_end = buf_end;
  1205. uint32 table_segment_count, i, table_index, function_count;
  1206. uint64 total_size;
  1207. WASMTableSeg *table_segment;
  1208. read_leb_uint32(p, p_end, table_segment_count);
  1209. if (table_segment_count) {
  1210. module->table_seg_count = table_segment_count;
  1211. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1212. if (!(module->table_segments =
  1213. loader_malloc(total_size, error_buf, error_buf_size))) {
  1214. return false;
  1215. }
  1216. table_segment = module->table_segments;
  1217. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1218. bh_assert(p < p_end);
  1219. #if WASM_ENABLE_REF_TYPES != 0
  1220. read_leb_uint32(p, p_end, table_segment->mode);
  1221. /* last three bits */
  1222. table_segment->mode = table_segment->mode & 0x07;
  1223. switch (table_segment->mode) {
  1224. /* elemkind/elemtype + active */
  1225. case 0:
  1226. case 4:
  1227. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  1228. table_segment->table_index = 0;
  1229. if (!check_table_index(module, table_segment->table_index,
  1230. error_buf, error_buf_size))
  1231. return false;
  1232. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1233. VALUE_TYPE_I32, error_buf,
  1234. error_buf_size))
  1235. return false;
  1236. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1237. table_segment->mode == 0 ? false
  1238. : true,
  1239. error_buf, error_buf_size))
  1240. return false;
  1241. break;
  1242. /* elemkind + passive/declarative */
  1243. case 1:
  1244. case 3:
  1245. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1246. true, error_buf, error_buf_size))
  1247. return false;
  1248. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1249. false, error_buf, error_buf_size))
  1250. return false;
  1251. break;
  1252. /* elemkind/elemtype + table_idx + active */
  1253. case 2:
  1254. case 6:
  1255. if (!load_table_index(&p, p_end, module,
  1256. &table_segment->table_index,
  1257. error_buf, error_buf_size))
  1258. return false;
  1259. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1260. VALUE_TYPE_I32, error_buf,
  1261. error_buf_size))
  1262. return false;
  1263. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1264. table_segment->mode == 2 ? true : false,
  1265. error_buf, error_buf_size))
  1266. return false;
  1267. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1268. table_segment->mode == 2 ? false
  1269. : true,
  1270. error_buf, error_buf_size))
  1271. return false;
  1272. break;
  1273. case 5:
  1274. case 7:
  1275. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  1276. false, error_buf, error_buf_size))
  1277. return false;
  1278. if (!load_func_index_vec(&p, p_end, module, table_segment,
  1279. true, error_buf, error_buf_size))
  1280. return false;
  1281. break;
  1282. default:
  1283. return false;
  1284. }
  1285. #else
  1286. /*
  1287. * like: 00 41 05 0b 04 00 01 00 01
  1288. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  1289. */
  1290. if (!load_table_index(&p, p_end, module,
  1291. &table_segment->table_index, error_buf,
  1292. error_buf_size))
  1293. return false;
  1294. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  1295. VALUE_TYPE_I32, error_buf, error_buf_size))
  1296. return false;
  1297. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  1298. error_buf, error_buf_size))
  1299. return false;
  1300. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  1301. }
  1302. }
  1303. (void)table_index;
  1304. (void)function_count;
  1305. bh_assert(p == p_end);
  1306. LOG_VERBOSE("Load table segment section success.\n");
  1307. return true;
  1308. }
  1309. static bool
  1310. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1311. WASMModule *module, char *error_buf,
  1312. uint32 error_buf_size)
  1313. {
  1314. const uint8 *p = buf, *p_end = buf_end;
  1315. uint32 data_seg_count, i, mem_index, data_seg_len;
  1316. uint64 total_size;
  1317. WASMDataSeg *dataseg;
  1318. InitializerExpression init_expr;
  1319. #if WASM_ENABLE_BULK_MEMORY != 0
  1320. bool is_passive = false;
  1321. uint32 mem_flag;
  1322. #endif
  1323. read_leb_uint32(p, p_end, data_seg_count);
  1324. #if WASM_ENABLE_BULK_MEMORY != 0
  1325. bh_assert(module->data_seg_count1 == 0
  1326. || data_seg_count == module->data_seg_count1);
  1327. #endif
  1328. if (data_seg_count) {
  1329. module->data_seg_count = data_seg_count;
  1330. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  1331. if (!(module->data_segments =
  1332. loader_malloc(total_size, error_buf, error_buf_size))) {
  1333. return false;
  1334. }
  1335. for (i = 0; i < data_seg_count; i++) {
  1336. read_leb_uint32(p, p_end, mem_index);
  1337. #if WASM_ENABLE_BULK_MEMORY != 0
  1338. is_passive = false;
  1339. mem_flag = mem_index & 0x03;
  1340. switch (mem_flag) {
  1341. case 0x01:
  1342. is_passive = true;
  1343. break;
  1344. case 0x00:
  1345. /* no memory index, treat index as 0 */
  1346. mem_index = 0;
  1347. goto check_mem_index;
  1348. case 0x02:
  1349. /* read following memory index */
  1350. read_leb_uint32(p, p_end, mem_index);
  1351. check_mem_index:
  1352. bh_assert(mem_index < module->import_memory_count
  1353. + module->memory_count);
  1354. break;
  1355. case 0x03:
  1356. default:
  1357. bh_assert(0);
  1358. break;
  1359. }
  1360. #else
  1361. bh_assert(mem_index
  1362. < module->import_memory_count + module->memory_count);
  1363. #endif /* WASM_ENABLE_BULK_MEMORY */
  1364. #if WASM_ENABLE_BULK_MEMORY != 0
  1365. if (!is_passive)
  1366. #endif
  1367. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  1368. error_buf, error_buf_size))
  1369. return false;
  1370. read_leb_uint32(p, p_end, data_seg_len);
  1371. if (!(dataseg = module->data_segments[i] = loader_malloc(
  1372. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  1373. return false;
  1374. }
  1375. #if WASM_ENABLE_BULK_MEMORY != 0
  1376. dataseg->is_passive = is_passive;
  1377. if (!is_passive)
  1378. #endif
  1379. {
  1380. bh_memcpy_s(&dataseg->base_offset,
  1381. sizeof(InitializerExpression), &init_expr,
  1382. sizeof(InitializerExpression));
  1383. dataseg->memory_index = mem_index;
  1384. }
  1385. dataseg->data_length = data_seg_len;
  1386. CHECK_BUF(p, p_end, data_seg_len);
  1387. dataseg->data = (uint8 *)p;
  1388. p += data_seg_len;
  1389. }
  1390. }
  1391. bh_assert(p == p_end);
  1392. LOG_VERBOSE("Load data segment section success.\n");
  1393. return true;
  1394. }
  1395. #if WASM_ENABLE_BULK_MEMORY != 0
  1396. static bool
  1397. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  1398. WASMModule *module, char *error_buf,
  1399. uint32 error_buf_size)
  1400. {
  1401. const uint8 *p = buf, *p_end = buf_end;
  1402. uint32 data_seg_count1 = 0;
  1403. read_leb_uint32(p, p_end, data_seg_count1);
  1404. module->data_seg_count1 = data_seg_count1;
  1405. bh_assert(p == p_end);
  1406. LOG_VERBOSE("Load datacount section success.\n");
  1407. return true;
  1408. }
  1409. #endif
  1410. static bool
  1411. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  1412. const uint8 *buf_func_end, WASMModule *module,
  1413. char *error_buf, uint32 error_buf_size)
  1414. {
  1415. const uint8 *p = buf, *p_end = buf_end;
  1416. const uint8 *p_func = buf_func;
  1417. uint32 func_count = 0, code_count;
  1418. /* code has been loaded in function section, so pass it here, just check
  1419. * whether function and code section have inconsistent lengths */
  1420. read_leb_uint32(p, p_end, code_count);
  1421. if (buf_func)
  1422. read_leb_uint32(p_func, buf_func_end, func_count);
  1423. bh_assert(func_count == code_count);
  1424. LOG_VERBOSE("Load code segment section success.\n");
  1425. (void)code_count;
  1426. (void)func_count;
  1427. return true;
  1428. }
  1429. static bool
  1430. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1431. char *error_buf, uint32 error_buf_size)
  1432. {
  1433. const uint8 *p = buf, *p_end = buf_end;
  1434. WASMFuncType *type;
  1435. uint32 start_function;
  1436. read_leb_uint32(p, p_end, start_function);
  1437. bh_assert(start_function
  1438. < module->function_count + module->import_function_count);
  1439. if (start_function < module->import_function_count)
  1440. type = module->import_functions[start_function].u.function.func_type;
  1441. else
  1442. type = module->functions[start_function - module->import_function_count]
  1443. ->func_type;
  1444. bh_assert(type->param_count == 0 && type->result_count == 0);
  1445. module->start_function = start_function;
  1446. bh_assert(p == p_end);
  1447. LOG_VERBOSE("Load start section success.\n");
  1448. (void)type;
  1449. return true;
  1450. }
  1451. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1452. static bool
  1453. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1454. bool is_load_from_file_buf, char *error_buf,
  1455. uint32 error_buf_size)
  1456. {
  1457. const uint8 *p = buf, *p_end = buf_end;
  1458. uint32 name_type, subsection_size;
  1459. uint32 previous_name_type = 0;
  1460. uint32 num_func_name;
  1461. uint32 func_index;
  1462. uint32 previous_func_index = ~0U;
  1463. uint32 func_name_len;
  1464. uint32 name_index;
  1465. int i = 0;
  1466. bh_assert(p < p_end);
  1467. while (p < p_end) {
  1468. read_leb_uint32(p, p_end, name_type);
  1469. if (i != 0) {
  1470. bh_assert(name_type > previous_name_type);
  1471. }
  1472. previous_name_type = name_type;
  1473. read_leb_uint32(p, p_end, subsection_size);
  1474. CHECK_BUF(p, p_end, subsection_size);
  1475. switch (name_type) {
  1476. case SUB_SECTION_TYPE_FUNC:
  1477. if (subsection_size) {
  1478. read_leb_uint32(p, p_end, num_func_name);
  1479. for (name_index = 0; name_index < num_func_name;
  1480. name_index++) {
  1481. read_leb_uint32(p, p_end, func_index);
  1482. bh_assert(func_index > previous_func_index);
  1483. previous_func_index = func_index;
  1484. read_leb_uint32(p, p_end, func_name_len);
  1485. CHECK_BUF(p, p_end, func_name_len);
  1486. /* Skip the import functions */
  1487. if (func_index >= module->import_function_count) {
  1488. func_index -= module->import_function_count;
  1489. bh_assert(func_index < module->function_count);
  1490. if (!(module->functions[func_index]->field_name =
  1491. const_str_list_insert(
  1492. p, func_name_len, module,
  1493. is_load_from_file_buf, error_buf,
  1494. error_buf_size))) {
  1495. return false;
  1496. }
  1497. }
  1498. p += func_name_len;
  1499. }
  1500. }
  1501. break;
  1502. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  1503. */
  1504. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  1505. default:
  1506. p = p + subsection_size;
  1507. break;
  1508. }
  1509. i++;
  1510. }
  1511. (void)previous_name_type;
  1512. (void)previous_func_index;
  1513. return true;
  1514. }
  1515. #endif
  1516. static bool
  1517. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1518. bool is_load_from_file_buf, char *error_buf,
  1519. uint32 error_buf_size)
  1520. {
  1521. const uint8 *p = buf, *p_end = buf_end;
  1522. uint32 name_len;
  1523. bh_assert(p < p_end);
  1524. read_leb_uint32(p, p_end, name_len);
  1525. bh_assert(name_len > 0 && p + name_len <= p_end);
  1526. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  1527. if (memcmp(p, "name", 4) == 0) {
  1528. p += name_len;
  1529. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  1530. error_buf_size);
  1531. }
  1532. #endif
  1533. LOG_VERBOSE("Load custom section success.\n");
  1534. (void)name_len;
  1535. return true;
  1536. }
  1537. static void
  1538. calculate_global_data_offset(WASMModule *module)
  1539. {
  1540. uint32 i, data_offset;
  1541. data_offset = 0;
  1542. for (i = 0; i < module->import_global_count; i++) {
  1543. WASMGlobalImport *import_global =
  1544. &((module->import_globals + i)->u.global);
  1545. #if WASM_ENABLE_FAST_JIT != 0
  1546. import_global->data_offset = data_offset;
  1547. #endif
  1548. data_offset += wasm_value_type_size(import_global->type);
  1549. }
  1550. for (i = 0; i < module->global_count; i++) {
  1551. WASMGlobal *global = module->globals + i;
  1552. #if WASM_ENABLE_FAST_JIT != 0
  1553. global->data_offset = data_offset;
  1554. #endif
  1555. data_offset += wasm_value_type_size(global->type);
  1556. }
  1557. module->global_data_size = data_offset;
  1558. }
  1559. #if WASM_ENABLE_FAST_JIT != 0
  1560. static bool
  1561. init_fast_jit_functions(WASMModule *module, char *error_buf,
  1562. uint32 error_buf_size)
  1563. {
  1564. #if WASM_ENABLE_LAZY_JIT != 0
  1565. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  1566. #endif
  1567. uint32 i;
  1568. if (!module->function_count)
  1569. return true;
  1570. if (!(module->fast_jit_func_ptrs =
  1571. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  1572. error_buf_size))) {
  1573. return false;
  1574. }
  1575. #if WASM_ENABLE_LAZY_JIT != 0
  1576. for (i = 0; i < module->function_count; i++) {
  1577. module->fast_jit_func_ptrs[i] =
  1578. jit_globals->compile_fast_jit_and_then_call;
  1579. }
  1580. #endif
  1581. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  1582. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  1583. set_error_buf(error_buf, error_buf_size,
  1584. "init fast jit thread lock failed");
  1585. return false;
  1586. }
  1587. module->fast_jit_thread_locks_inited[i] = true;
  1588. }
  1589. return true;
  1590. }
  1591. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  1592. #if WASM_ENABLE_JIT != 0
  1593. static bool
  1594. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  1595. uint32 error_buf_size)
  1596. {
  1597. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  1598. AOTCompOption option = { 0 };
  1599. char *aot_last_error;
  1600. uint64 size;
  1601. bool gc_enabled =
  1602. #if WASM_ENABLE_GC != 0
  1603. true
  1604. #else
  1605. false
  1606. #endif
  1607. ;
  1608. if (module->function_count == 0)
  1609. return true;
  1610. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1611. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  1612. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  1613. return false;
  1614. }
  1615. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  1616. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  1617. os_mutex_destroy(&module->tierup_wait_lock);
  1618. return false;
  1619. }
  1620. module->tierup_wait_lock_inited = true;
  1621. #endif
  1622. size = sizeof(void *) * (uint64)module->function_count
  1623. + sizeof(bool) * (uint64)module->function_count;
  1624. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  1625. return false;
  1626. }
  1627. module->func_ptrs_compiled =
  1628. (bool *)((uint8 *)module->func_ptrs
  1629. + sizeof(void *) * module->function_count);
  1630. module->comp_data = aot_create_comp_data(module, gc_enabled);
  1631. if (!module->comp_data) {
  1632. aot_last_error = aot_get_last_error();
  1633. bh_assert(aot_last_error != NULL);
  1634. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1635. return false;
  1636. }
  1637. option.is_jit_mode = true;
  1638. option.opt_level = llvm_jit_options.opt_level;
  1639. option.size_level = llvm_jit_options.size_level;
  1640. option.segue_flags = llvm_jit_options.segue_flags;
  1641. #if WASM_ENABLE_BULK_MEMORY != 0
  1642. option.enable_bulk_memory = true;
  1643. #endif
  1644. #if WASM_ENABLE_THREAD_MGR != 0
  1645. option.enable_thread_mgr = true;
  1646. #endif
  1647. #if WASM_ENABLE_TAIL_CALL != 0
  1648. option.enable_tail_call = true;
  1649. #endif
  1650. #if WASM_ENABLE_SIMD != 0
  1651. option.enable_simd = true;
  1652. #endif
  1653. #if WASM_ENABLE_REF_TYPES != 0
  1654. option.enable_ref_types = true;
  1655. #endif
  1656. option.enable_aux_stack_check = true;
  1657. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0) \
  1658. || (WASM_ENABLE_JIT_STACK_FRAME != 0)
  1659. option.enable_aux_stack_frame = true;
  1660. #endif
  1661. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1662. option.enable_stack_estimation = true;
  1663. #endif
  1664. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  1665. if (!module->comp_ctx) {
  1666. aot_last_error = aot_get_last_error();
  1667. bh_assert(aot_last_error != NULL);
  1668. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1669. return false;
  1670. }
  1671. return true;
  1672. }
  1673. static bool
  1674. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  1675. uint32 error_buf_size)
  1676. {
  1677. char *aot_last_error;
  1678. uint32 i;
  1679. if (module->function_count == 0)
  1680. return true;
  1681. if (!aot_compile_wasm(module->comp_ctx)) {
  1682. aot_last_error = aot_get_last_error();
  1683. bh_assert(aot_last_error != NULL);
  1684. set_error_buf(error_buf, error_buf_size, aot_last_error);
  1685. return false;
  1686. }
  1687. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1688. if (module->orcjit_stop_compiling)
  1689. return false;
  1690. #endif
  1691. bh_print_time("Begin to lookup llvm jit functions");
  1692. for (i = 0; i < module->function_count; i++) {
  1693. LLVMOrcJITTargetAddress func_addr = 0;
  1694. LLVMErrorRef error;
  1695. char func_name[48];
  1696. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  1697. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  1698. func_name);
  1699. if (error != LLVMErrorSuccess) {
  1700. char *err_msg = LLVMGetErrorMessage(error);
  1701. char buf[96];
  1702. snprintf(buf, sizeof(buf),
  1703. "failed to compile llvm jit function: %s", err_msg);
  1704. set_error_buf(error_buf, error_buf_size, buf);
  1705. LLVMDisposeErrorMessage(err_msg);
  1706. return false;
  1707. }
  1708. /**
  1709. * No need to lock the func_ptr[func_idx] here as it is basic
  1710. * data type, the load/store for it can be finished by one cpu
  1711. * instruction, and there can be only one cpu instruction
  1712. * loading/storing at the same time.
  1713. */
  1714. module->func_ptrs[i] = (void *)func_addr;
  1715. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1716. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  1717. if (module->orcjit_stop_compiling)
  1718. return false;
  1719. #endif
  1720. }
  1721. bh_print_time("End lookup llvm jit functions");
  1722. return true;
  1723. }
  1724. #endif /* end of WASM_ENABLE_JIT != 0 */
  1725. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1726. && WASM_ENABLE_LAZY_JIT != 0
  1727. static void *
  1728. init_llvm_jit_functions_stage2_callback(void *arg)
  1729. {
  1730. WASMModule *module = (WASMModule *)arg;
  1731. char error_buf[128];
  1732. uint32 error_buf_size = (uint32)sizeof(error_buf);
  1733. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  1734. module->orcjit_stop_compiling = true;
  1735. return NULL;
  1736. }
  1737. os_mutex_lock(&module->tierup_wait_lock);
  1738. module->llvm_jit_inited = true;
  1739. os_cond_broadcast(&module->tierup_wait_cond);
  1740. os_mutex_unlock(&module->tierup_wait_lock);
  1741. return NULL;
  1742. }
  1743. #endif
  1744. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  1745. /* The callback function to compile jit functions */
  1746. static void *
  1747. orcjit_thread_callback(void *arg)
  1748. {
  1749. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  1750. #if WASM_ENABLE_JIT != 0
  1751. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  1752. #endif
  1753. WASMModule *module = thread_arg->module;
  1754. uint32 group_idx = thread_arg->group_idx;
  1755. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  1756. uint32 func_count = module->function_count;
  1757. uint32 i;
  1758. #if WASM_ENABLE_FAST_JIT != 0
  1759. /* Compile fast jit funcitons of this group */
  1760. for (i = group_idx; i < func_count; i += group_stride) {
  1761. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  1762. os_printf("failed to compile fast jit function %u\n", i);
  1763. break;
  1764. }
  1765. if (module->orcjit_stop_compiling) {
  1766. return NULL;
  1767. }
  1768. }
  1769. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1770. os_mutex_lock(&module->tierup_wait_lock);
  1771. module->fast_jit_ready_groups++;
  1772. os_mutex_unlock(&module->tierup_wait_lock);
  1773. #endif
  1774. #endif
  1775. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1776. && WASM_ENABLE_LAZY_JIT != 0
  1777. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  1778. for (i = group_idx; i < func_count;
  1779. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  1780. uint32 j;
  1781. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  1782. if (i + j * group_stride < func_count) {
  1783. if (!jit_compiler_set_call_to_fast_jit(
  1784. module,
  1785. i + j * group_stride + module->import_function_count)) {
  1786. os_printf(
  1787. "failed to compile call_to_fast_jit for func %u\n",
  1788. i + j * group_stride + module->import_function_count);
  1789. module->orcjit_stop_compiling = true;
  1790. return NULL;
  1791. }
  1792. }
  1793. if (module->orcjit_stop_compiling) {
  1794. return NULL;
  1795. }
  1796. }
  1797. }
  1798. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  1799. fast jit functions are compiled */
  1800. os_mutex_lock(&module->tierup_wait_lock);
  1801. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  1802. && module->fast_jit_ready_groups >= group_stride)) {
  1803. os_cond_reltimedwait(&module->tierup_wait_cond,
  1804. &module->tierup_wait_lock, 10000);
  1805. if (module->orcjit_stop_compiling) {
  1806. /* init_llvm_jit_functions_stage2 failed */
  1807. os_mutex_unlock(&module->tierup_wait_lock);
  1808. return NULL;
  1809. }
  1810. }
  1811. os_mutex_unlock(&module->tierup_wait_lock);
  1812. #endif
  1813. #if WASM_ENABLE_JIT != 0
  1814. /* Compile llvm jit functions of this group */
  1815. for (i = group_idx; i < func_count;
  1816. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  1817. LLVMOrcJITTargetAddress func_addr = 0;
  1818. LLVMErrorRef error;
  1819. char func_name[48];
  1820. typedef void (*F)(void);
  1821. union {
  1822. F f;
  1823. void *v;
  1824. } u;
  1825. uint32 j;
  1826. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  1827. "_wrapper");
  1828. LOG_DEBUG("compile llvm jit func %s", func_name);
  1829. error =
  1830. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  1831. if (error != LLVMErrorSuccess) {
  1832. char *err_msg = LLVMGetErrorMessage(error);
  1833. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  1834. LLVMDisposeErrorMessage(err_msg);
  1835. break;
  1836. }
  1837. /* Call the jit wrapper function to trigger its compilation, so as
  1838. to compile the actual jit functions, since we add the latter to
  1839. function list in the PartitionFunction callback */
  1840. u.v = (void *)func_addr;
  1841. u.f();
  1842. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  1843. if (i + j * group_stride < func_count) {
  1844. module->func_ptrs_compiled[i + j * group_stride] = true;
  1845. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  1846. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  1847. i + j * group_stride);
  1848. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  1849. func_name);
  1850. if (error != LLVMErrorSuccess) {
  1851. char *err_msg = LLVMGetErrorMessage(error);
  1852. os_printf("failed to compile llvm jit function %u: %s", i,
  1853. err_msg);
  1854. LLVMDisposeErrorMessage(err_msg);
  1855. /* Ignore current llvm jit func, as its func ptr is
  1856. previous set to call_to_fast_jit, which also works */
  1857. continue;
  1858. }
  1859. jit_compiler_set_llvm_jit_func_ptr(
  1860. module,
  1861. i + j * group_stride + module->import_function_count,
  1862. (void *)func_addr);
  1863. /* Try to switch to call this llvm jit funtion instead of
  1864. fast jit function from fast jit jitted code */
  1865. jit_compiler_set_call_to_llvm_jit(
  1866. module,
  1867. i + j * group_stride + module->import_function_count);
  1868. #endif
  1869. }
  1870. }
  1871. if (module->orcjit_stop_compiling) {
  1872. break;
  1873. }
  1874. }
  1875. #endif
  1876. return NULL;
  1877. }
  1878. static void
  1879. orcjit_stop_compile_threads(WASMModule *module)
  1880. {
  1881. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  1882. / sizeof(OrcJitThreadArg));
  1883. module->orcjit_stop_compiling = true;
  1884. for (i = 0; i < thread_num; i++) {
  1885. if (module->orcjit_threads[i])
  1886. os_thread_join(module->orcjit_threads[i], NULL);
  1887. }
  1888. }
  1889. static bool
  1890. compile_jit_functions(WASMModule *module, char *error_buf,
  1891. uint32 error_buf_size)
  1892. {
  1893. uint32 thread_num =
  1894. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  1895. uint32 i, j;
  1896. bh_print_time("Begin to compile jit functions");
  1897. /* Create threads to compile the jit functions */
  1898. for (i = 0; i < thread_num && i < module->function_count; i++) {
  1899. #if WASM_ENABLE_JIT != 0
  1900. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  1901. #endif
  1902. module->orcjit_thread_args[i].module = module;
  1903. module->orcjit_thread_args[i].group_idx = i;
  1904. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  1905. (void *)&module->orcjit_thread_args[i],
  1906. APP_THREAD_STACK_SIZE_DEFAULT)
  1907. != 0) {
  1908. set_error_buf(error_buf, error_buf_size,
  1909. "create orcjit compile thread failed");
  1910. /* Terminate the threads created */
  1911. module->orcjit_stop_compiling = true;
  1912. for (j = 0; j < i; j++) {
  1913. os_thread_join(module->orcjit_threads[j], NULL);
  1914. }
  1915. return false;
  1916. }
  1917. }
  1918. #if WASM_ENABLE_LAZY_JIT == 0
  1919. /* Wait until all jit functions are compiled for eager mode */
  1920. for (i = 0; i < thread_num; i++) {
  1921. if (module->orcjit_threads[i])
  1922. os_thread_join(module->orcjit_threads[i], NULL);
  1923. }
  1924. #if WASM_ENABLE_FAST_JIT != 0
  1925. /* Ensure all the fast-jit functions are compiled */
  1926. for (i = 0; i < module->function_count; i++) {
  1927. if (!jit_compiler_is_compiled(module,
  1928. i + module->import_function_count)) {
  1929. set_error_buf(error_buf, error_buf_size,
  1930. "failed to compile fast jit function");
  1931. return false;
  1932. }
  1933. }
  1934. #endif
  1935. #if WASM_ENABLE_JIT != 0
  1936. /* Ensure all the llvm-jit functions are compiled */
  1937. for (i = 0; i < module->function_count; i++) {
  1938. if (!module->func_ptrs_compiled[i]) {
  1939. set_error_buf(error_buf, error_buf_size,
  1940. "failed to compile llvm jit function");
  1941. return false;
  1942. }
  1943. }
  1944. #endif
  1945. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  1946. bh_print_time("End compile jit functions");
  1947. return true;
  1948. }
  1949. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  1950. #if WASM_ENABLE_REF_TYPES != 0
  1951. static bool
  1952. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  1953. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  1954. {
  1955. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  1956. return false;
  1957. }
  1958. if (p_elem_type) {
  1959. if (table_idx < module->import_table_count)
  1960. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  1961. else
  1962. *p_elem_type =
  1963. module->tables[module->import_table_count + table_idx]
  1964. .elem_type;
  1965. }
  1966. return true;
  1967. }
  1968. static bool
  1969. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  1970. uint8 *p_elem_type, char *error_buf,
  1971. uint32 error_buf_size)
  1972. {
  1973. if (table_seg_idx >= module->table_seg_count) {
  1974. return false;
  1975. }
  1976. if (p_elem_type) {
  1977. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  1978. }
  1979. return true;
  1980. }
  1981. #endif
  1982. static bool
  1983. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1984. uint32 cur_func_idx, char *error_buf,
  1985. uint32 error_buf_size);
  1986. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  1987. void **
  1988. wasm_interp_get_handle_table();
  1989. static void **handle_table;
  1990. #endif
  1991. static bool
  1992. load_from_sections(WASMModule *module, WASMSection *sections,
  1993. bool is_load_from_file_buf, char *error_buf,
  1994. uint32 error_buf_size)
  1995. {
  1996. WASMExport *export;
  1997. WASMSection *section = sections;
  1998. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1999. *buf_func = NULL, *buf_func_end = NULL;
  2000. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  2001. WASMGlobal *aux_stack_top_global = NULL, *global;
  2002. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  2003. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  2004. uint32 aux_data_end_global_index = (uint32)-1;
  2005. uint32 aux_heap_base_global_index = (uint32)-1;
  2006. WASMFuncType *func_type;
  2007. /* Find code and function sections if have */
  2008. while (section) {
  2009. if (section->section_type == SECTION_TYPE_CODE) {
  2010. buf_code = section->section_body;
  2011. buf_code_end = buf_code + section->section_body_size;
  2012. }
  2013. else if (section->section_type == SECTION_TYPE_FUNC) {
  2014. buf_func = section->section_body;
  2015. buf_func_end = buf_func + section->section_body_size;
  2016. }
  2017. section = section->next;
  2018. }
  2019. section = sections;
  2020. while (section) {
  2021. buf = section->section_body;
  2022. buf_end = buf + section->section_body_size;
  2023. LOG_DEBUG("load section, type: %d", section->section_type);
  2024. switch (section->section_type) {
  2025. case SECTION_TYPE_USER:
  2026. /* unsupported user section, ignore it. */
  2027. if (!load_user_section(buf, buf_end, module,
  2028. is_load_from_file_buf, error_buf,
  2029. error_buf_size))
  2030. return false;
  2031. break;
  2032. case SECTION_TYPE_TYPE:
  2033. if (!load_type_section(buf, buf_end, module, error_buf,
  2034. error_buf_size))
  2035. return false;
  2036. break;
  2037. case SECTION_TYPE_IMPORT:
  2038. if (!load_import_section(buf, buf_end, module,
  2039. is_load_from_file_buf, error_buf,
  2040. error_buf_size))
  2041. return false;
  2042. break;
  2043. case SECTION_TYPE_FUNC:
  2044. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  2045. module, error_buf, error_buf_size))
  2046. return false;
  2047. break;
  2048. case SECTION_TYPE_TABLE:
  2049. if (!load_table_section(buf, buf_end, module, error_buf,
  2050. error_buf_size))
  2051. return false;
  2052. break;
  2053. case SECTION_TYPE_MEMORY:
  2054. if (!load_memory_section(buf, buf_end, module, error_buf,
  2055. error_buf_size))
  2056. return false;
  2057. break;
  2058. case SECTION_TYPE_GLOBAL:
  2059. if (!load_global_section(buf, buf_end, module, error_buf,
  2060. error_buf_size))
  2061. return false;
  2062. break;
  2063. case SECTION_TYPE_EXPORT:
  2064. if (!load_export_section(buf, buf_end, module,
  2065. is_load_from_file_buf, error_buf,
  2066. error_buf_size))
  2067. return false;
  2068. break;
  2069. case SECTION_TYPE_START:
  2070. if (!load_start_section(buf, buf_end, module, error_buf,
  2071. error_buf_size))
  2072. return false;
  2073. break;
  2074. case SECTION_TYPE_ELEM:
  2075. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  2076. error_buf_size))
  2077. return false;
  2078. break;
  2079. case SECTION_TYPE_CODE:
  2080. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  2081. module, error_buf, error_buf_size))
  2082. return false;
  2083. break;
  2084. case SECTION_TYPE_DATA:
  2085. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  2086. error_buf_size))
  2087. return false;
  2088. break;
  2089. #if WASM_ENABLE_BULK_MEMORY != 0
  2090. case SECTION_TYPE_DATACOUNT:
  2091. if (!load_datacount_section(buf, buf_end, module, error_buf,
  2092. error_buf_size))
  2093. return false;
  2094. break;
  2095. #endif
  2096. default:
  2097. set_error_buf(error_buf, error_buf_size, "invalid section id");
  2098. return false;
  2099. }
  2100. section = section->next;
  2101. }
  2102. module->aux_data_end_global_index = (uint32)-1;
  2103. module->aux_heap_base_global_index = (uint32)-1;
  2104. module->aux_stack_top_global_index = (uint32)-1;
  2105. /* Resolve auxiliary data/stack/heap info and reset memory info */
  2106. export = module->exports;
  2107. for (i = 0; i < module->export_count; i++, export ++) {
  2108. if (export->kind == EXPORT_KIND_GLOBAL) {
  2109. if (!strcmp(export->name, "__heap_base")) {
  2110. global_index = export->index - module->import_global_count;
  2111. global = module->globals + global_index;
  2112. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2113. && global->init_expr.init_expr_type
  2114. == INIT_EXPR_TYPE_I32_CONST) {
  2115. aux_heap_base_global = global;
  2116. aux_heap_base = global->init_expr.u.i32;
  2117. aux_heap_base_global_index = export->index;
  2118. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  2119. aux_heap_base);
  2120. }
  2121. }
  2122. else if (!strcmp(export->name, "__data_end")) {
  2123. global_index = export->index - module->import_global_count;
  2124. global = module->globals + global_index;
  2125. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  2126. && global->init_expr.init_expr_type
  2127. == INIT_EXPR_TYPE_I32_CONST) {
  2128. aux_data_end_global = global;
  2129. aux_data_end = global->init_expr.u.i32;
  2130. aux_data_end_global_index = export->index;
  2131. LOG_VERBOSE("Found aux __data_end global, value: %d",
  2132. aux_data_end);
  2133. aux_data_end = align_uint(aux_data_end, 16);
  2134. }
  2135. }
  2136. /* For module compiled with -pthread option, the global is:
  2137. [0] stack_top <-- 0
  2138. [1] tls_pointer
  2139. [2] tls_size
  2140. [3] data_end <-- 3
  2141. [4] global_base
  2142. [5] heap_base <-- 5
  2143. [6] dso_handle
  2144. For module compiled without -pthread option:
  2145. [0] stack_top <-- 0
  2146. [1] data_end <-- 1
  2147. [2] global_base
  2148. [3] heap_base <-- 3
  2149. [4] dso_handle
  2150. */
  2151. if (aux_data_end_global && aux_heap_base_global
  2152. && aux_data_end <= aux_heap_base) {
  2153. module->aux_data_end_global_index = aux_data_end_global_index;
  2154. module->aux_data_end = aux_data_end;
  2155. module->aux_heap_base_global_index = aux_heap_base_global_index;
  2156. module->aux_heap_base = aux_heap_base;
  2157. /* Resolve aux stack top global */
  2158. for (global_index = 0; global_index < module->global_count;
  2159. global_index++) {
  2160. global = module->globals + global_index;
  2161. if (global->is_mutable /* heap_base and data_end is
  2162. not mutable */
  2163. && global->type == VALUE_TYPE_I32
  2164. && global->init_expr.init_expr_type
  2165. == INIT_EXPR_TYPE_I32_CONST
  2166. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  2167. aux_stack_top_global = global;
  2168. aux_stack_top = (uint32)global->init_expr.u.i32;
  2169. module->aux_stack_top_global_index =
  2170. module->import_global_count + global_index;
  2171. module->aux_stack_bottom = aux_stack_top;
  2172. module->aux_stack_size =
  2173. aux_stack_top > aux_data_end
  2174. ? aux_stack_top - aux_data_end
  2175. : aux_stack_top;
  2176. LOG_VERBOSE("Found aux stack top global, value: %d, "
  2177. "global index: %d, stack size: %d",
  2178. aux_stack_top, global_index,
  2179. module->aux_stack_size);
  2180. break;
  2181. }
  2182. }
  2183. if (!aux_stack_top_global) {
  2184. /* Auxiliary stack global isn't found, it must be unused
  2185. in the wasm app, as if it is used, the global must be
  2186. defined. Here we set it to __heap_base global and set
  2187. its size to 0. */
  2188. aux_stack_top_global = aux_heap_base_global;
  2189. aux_stack_top = aux_heap_base;
  2190. module->aux_stack_top_global_index =
  2191. module->aux_heap_base_global_index;
  2192. module->aux_stack_bottom = aux_stack_top;
  2193. module->aux_stack_size = 0;
  2194. }
  2195. break;
  2196. }
  2197. }
  2198. }
  2199. module->malloc_function = (uint32)-1;
  2200. module->free_function = (uint32)-1;
  2201. module->retain_function = (uint32)-1;
  2202. /* Resolve malloc/free function exported by wasm module */
  2203. export = module->exports;
  2204. for (i = 0; i < module->export_count; i++, export ++) {
  2205. if (export->kind == EXPORT_KIND_FUNC) {
  2206. if (!strcmp(export->name, "malloc")
  2207. && export->index >= module->import_function_count) {
  2208. func_index = export->index - module->import_function_count;
  2209. func_type = module->functions[func_index]->func_type;
  2210. if (func_type->param_count == 1 && func_type->result_count == 1
  2211. && func_type->types[0] == VALUE_TYPE_I32
  2212. && func_type->types[1] == VALUE_TYPE_I32) {
  2213. bh_assert(module->malloc_function == (uint32)-1);
  2214. module->malloc_function = export->index;
  2215. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2216. export->name, export->index);
  2217. }
  2218. }
  2219. else if (!strcmp(export->name, "__new")
  2220. && export->index >= module->import_function_count) {
  2221. /* __new && __pin for AssemblyScript */
  2222. func_index = export->index - module->import_function_count;
  2223. func_type = module->functions[func_index]->func_type;
  2224. if (func_type->param_count == 2 && func_type->result_count == 1
  2225. && func_type->types[0] == VALUE_TYPE_I32
  2226. && func_type->types[1] == VALUE_TYPE_I32
  2227. && func_type->types[2] == VALUE_TYPE_I32) {
  2228. uint32 j;
  2229. WASMExport *export_tmp;
  2230. bh_assert(module->malloc_function == (uint32)-1);
  2231. module->malloc_function = export->index;
  2232. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  2233. export->name, export->index);
  2234. /* resolve retain function.
  2235. If not found, reset malloc function index */
  2236. export_tmp = module->exports;
  2237. for (j = 0; j < module->export_count; j++, export_tmp++) {
  2238. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  2239. && (!strcmp(export_tmp->name, "__retain")
  2240. || !strcmp(export_tmp->name, "__pin"))
  2241. && (export_tmp->index
  2242. >= module->import_function_count)) {
  2243. func_index = export_tmp->index
  2244. - module->import_function_count;
  2245. func_type =
  2246. module->functions[func_index]->func_type;
  2247. if (func_type->param_count == 1
  2248. && func_type->result_count == 1
  2249. && func_type->types[0] == VALUE_TYPE_I32
  2250. && func_type->types[1] == VALUE_TYPE_I32) {
  2251. bh_assert(module->retain_function
  2252. == (uint32)-1);
  2253. module->retain_function = export_tmp->index;
  2254. LOG_VERBOSE("Found retain function, name: %s, "
  2255. "index: %u",
  2256. export_tmp->name,
  2257. export_tmp->index);
  2258. break;
  2259. }
  2260. }
  2261. }
  2262. if (j == module->export_count) {
  2263. module->malloc_function = (uint32)-1;
  2264. LOG_VERBOSE("Can't find retain function,"
  2265. "reset malloc function index to -1");
  2266. }
  2267. }
  2268. }
  2269. else if (((!strcmp(export->name, "free"))
  2270. || (!strcmp(export->name, "__release"))
  2271. || (!strcmp(export->name, "__unpin")))
  2272. && export->index >= module->import_function_count) {
  2273. func_index = export->index - module->import_function_count;
  2274. func_type = module->functions[func_index]->func_type;
  2275. if (func_type->param_count == 1 && func_type->result_count == 0
  2276. && func_type->types[0] == VALUE_TYPE_I32) {
  2277. bh_assert(module->free_function == (uint32)-1);
  2278. module->free_function = export->index;
  2279. LOG_VERBOSE("Found free function, name: %s, index: %u",
  2280. export->name, export->index);
  2281. }
  2282. }
  2283. }
  2284. }
  2285. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  2286. handle_table = wasm_interp_get_handle_table();
  2287. #endif
  2288. for (i = 0; i < module->function_count; i++) {
  2289. WASMFunction *func = module->functions[i];
  2290. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  2291. error_buf_size)) {
  2292. return false;
  2293. }
  2294. if (i == module->function_count - 1) {
  2295. bh_assert(func->code + func->code_size == buf_code_end);
  2296. }
  2297. }
  2298. if (!module->possible_memory_grow) {
  2299. WASMMemoryImport *memory_import;
  2300. WASMMemory *memory;
  2301. if (aux_data_end_global && aux_heap_base_global
  2302. && aux_stack_top_global) {
  2303. uint64 init_memory_size;
  2304. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  2305. if (module->import_memory_count) {
  2306. memory_import = &module->import_memories[0].u.memory;
  2307. init_memory_size = (uint64)memory_import->num_bytes_per_page
  2308. * memory_import->init_page_count;
  2309. if (shrunk_memory_size <= init_memory_size) {
  2310. /* Reset memory info to decrease memory usage */
  2311. memory_import->num_bytes_per_page = shrunk_memory_size;
  2312. memory_import->init_page_count = 1;
  2313. LOG_VERBOSE("Shrink import memory size to %d",
  2314. shrunk_memory_size);
  2315. }
  2316. }
  2317. if (module->memory_count) {
  2318. memory = &module->memories[0];
  2319. init_memory_size = (uint64)memory->num_bytes_per_page
  2320. * memory->init_page_count;
  2321. if (shrunk_memory_size <= init_memory_size) {
  2322. /* Reset memory info to decrease memory usage */
  2323. memory->num_bytes_per_page = shrunk_memory_size;
  2324. memory->init_page_count = 1;
  2325. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  2326. }
  2327. }
  2328. }
  2329. if (module->import_memory_count) {
  2330. memory_import = &module->import_memories[0].u.memory;
  2331. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  2332. memory_import->num_bytes_per_page *=
  2333. memory_import->init_page_count;
  2334. else
  2335. memory_import->num_bytes_per_page = UINT32_MAX;
  2336. if (memory_import->init_page_count > 0)
  2337. memory_import->init_page_count = memory_import->max_page_count =
  2338. 1;
  2339. else
  2340. memory_import->init_page_count = memory_import->max_page_count =
  2341. 0;
  2342. }
  2343. if (module->memory_count) {
  2344. memory = &module->memories[0];
  2345. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  2346. memory->num_bytes_per_page *= memory->init_page_count;
  2347. else
  2348. memory->num_bytes_per_page = UINT32_MAX;
  2349. if (memory->init_page_count > 0)
  2350. memory->init_page_count = memory->max_page_count = 1;
  2351. else
  2352. memory->init_page_count = memory->max_page_count = 0;
  2353. }
  2354. }
  2355. calculate_global_data_offset(module);
  2356. #if WASM_ENABLE_FAST_JIT != 0
  2357. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  2358. return false;
  2359. }
  2360. #endif
  2361. #if WASM_ENABLE_JIT != 0
  2362. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  2363. return false;
  2364. }
  2365. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  2366. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2367. return false;
  2368. }
  2369. #else
  2370. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  2371. thread fast jit execution, since applying llvm optimizations in
  2372. aot_compile_wasm may cost a lot of time.
  2373. Create thread with enough native stack to apply llvm optimizations */
  2374. if (os_thread_create(&module->llvm_jit_init_thread,
  2375. init_llvm_jit_functions_stage2_callback,
  2376. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  2377. != 0) {
  2378. set_error_buf(error_buf, error_buf_size,
  2379. "create orcjit compile thread failed");
  2380. return false;
  2381. }
  2382. #endif
  2383. #endif
  2384. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2385. /* Create threads to compile the jit functions */
  2386. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  2387. return false;
  2388. }
  2389. #endif
  2390. #if WASM_ENABLE_MEMORY_TRACING != 0
  2391. wasm_runtime_dump_module_mem_consumption(module);
  2392. #endif
  2393. return true;
  2394. }
  2395. static WASMModule *
  2396. create_module(char *error_buf, uint32 error_buf_size)
  2397. {
  2398. WASMModule *module =
  2399. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  2400. bh_list_status ret;
  2401. if (!module) {
  2402. return NULL;
  2403. }
  2404. module->module_type = Wasm_Module_Bytecode;
  2405. /* Set start_function to -1, means no start function */
  2406. module->start_function = (uint32)-1;
  2407. #if WASM_ENABLE_FAST_INTERP == 0
  2408. module->br_table_cache_list = &module->br_table_cache_list_head;
  2409. ret = bh_list_init(module->br_table_cache_list);
  2410. bh_assert(ret == BH_LIST_SUCCESS);
  2411. #endif
  2412. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2413. && WASM_ENABLE_LAZY_JIT != 0
  2414. if (os_mutex_init(&module->instance_list_lock) != 0) {
  2415. set_error_buf(error_buf, error_buf_size,
  2416. "init instance list lock failed");
  2417. wasm_runtime_free(module);
  2418. return NULL;
  2419. }
  2420. #endif
  2421. (void)ret;
  2422. return module;
  2423. }
  2424. WASMModule *
  2425. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  2426. uint32 error_buf_size)
  2427. {
  2428. WASMModule *module = create_module(error_buf, error_buf_size);
  2429. if (!module)
  2430. return NULL;
  2431. if (!load_from_sections(module, section_list, false, error_buf,
  2432. error_buf_size)) {
  2433. wasm_loader_unload(module);
  2434. return NULL;
  2435. }
  2436. LOG_VERBOSE("Load module from sections success.\n");
  2437. return module;
  2438. }
  2439. static void
  2440. destroy_sections(WASMSection *section_list)
  2441. {
  2442. WASMSection *section = section_list, *next;
  2443. while (section) {
  2444. next = section->next;
  2445. wasm_runtime_free(section);
  2446. section = next;
  2447. }
  2448. }
  2449. /* clang-format off */
  2450. static uint8 section_ids[] = {
  2451. SECTION_TYPE_USER,
  2452. SECTION_TYPE_TYPE,
  2453. SECTION_TYPE_IMPORT,
  2454. SECTION_TYPE_FUNC,
  2455. SECTION_TYPE_TABLE,
  2456. SECTION_TYPE_MEMORY,
  2457. SECTION_TYPE_GLOBAL,
  2458. SECTION_TYPE_EXPORT,
  2459. SECTION_TYPE_START,
  2460. SECTION_TYPE_ELEM,
  2461. #if WASM_ENABLE_BULK_MEMORY != 0
  2462. SECTION_TYPE_DATACOUNT,
  2463. #endif
  2464. SECTION_TYPE_CODE,
  2465. SECTION_TYPE_DATA
  2466. };
  2467. /* clang-format on */
  2468. static uint8
  2469. get_section_index(uint8 section_type)
  2470. {
  2471. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  2472. for (uint8 i = 0; i < max_id; i++) {
  2473. if (section_type == section_ids[i])
  2474. return i;
  2475. }
  2476. return (uint8)-1;
  2477. }
  2478. static bool
  2479. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  2480. char *error_buf, uint32 error_buf_size)
  2481. {
  2482. WASMSection *section_list_end = NULL, *section;
  2483. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  2484. uint8 section_type, section_index, last_section_index = (uint8)-1;
  2485. uint32 section_size;
  2486. bh_assert(!*p_section_list);
  2487. p += 8;
  2488. while (p < p_end) {
  2489. CHECK_BUF(p, p_end, 1);
  2490. section_type = read_uint8(p);
  2491. section_index = get_section_index(section_type);
  2492. if (section_index != (uint8)-1) {
  2493. if (section_type != SECTION_TYPE_USER) {
  2494. /* Custom sections may be inserted at any place,
  2495. while other sections must occur at most once
  2496. and in prescribed order. */
  2497. bh_assert(last_section_index == (uint8)-1
  2498. || last_section_index < section_index);
  2499. last_section_index = section_index;
  2500. }
  2501. read_leb_uint32(p, p_end, section_size);
  2502. CHECK_BUF1(p, p_end, section_size);
  2503. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  2504. error_buf_size))) {
  2505. return false;
  2506. }
  2507. section->section_type = section_type;
  2508. section->section_body = (uint8 *)p;
  2509. section->section_body_size = section_size;
  2510. if (!*p_section_list)
  2511. *p_section_list = section_list_end = section;
  2512. else {
  2513. section_list_end->next = section;
  2514. section_list_end = section;
  2515. }
  2516. p += section_size;
  2517. }
  2518. else {
  2519. bh_assert(0);
  2520. }
  2521. }
  2522. (void)last_section_index;
  2523. return true;
  2524. }
  2525. static void
  2526. exchange32(uint8 *p_data)
  2527. {
  2528. uint8 value = *p_data;
  2529. *p_data = *(p_data + 3);
  2530. *(p_data + 3) = value;
  2531. value = *(p_data + 1);
  2532. *(p_data + 1) = *(p_data + 2);
  2533. *(p_data + 2) = value;
  2534. }
  2535. static union {
  2536. int a;
  2537. char b;
  2538. } __ue = { .a = 1 };
  2539. #define is_little_endian() (__ue.b == 1)
  2540. static bool
  2541. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  2542. uint32 error_buf_size)
  2543. {
  2544. const uint8 *buf_end = buf + size;
  2545. const uint8 *p = buf, *p_end = buf_end;
  2546. uint32 magic_number, version;
  2547. WASMSection *section_list = NULL;
  2548. CHECK_BUF1(p, p_end, sizeof(uint32));
  2549. magic_number = read_uint32(p);
  2550. if (!is_little_endian())
  2551. exchange32((uint8 *)&magic_number);
  2552. bh_assert(magic_number == WASM_MAGIC_NUMBER);
  2553. CHECK_BUF1(p, p_end, sizeof(uint32));
  2554. version = read_uint32(p);
  2555. if (!is_little_endian())
  2556. exchange32((uint8 *)&version);
  2557. if (version != WASM_CURRENT_VERSION) {
  2558. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  2559. return false;
  2560. }
  2561. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  2562. || !load_from_sections(module, section_list, true, error_buf,
  2563. error_buf_size)) {
  2564. destroy_sections(section_list);
  2565. return false;
  2566. }
  2567. destroy_sections(section_list);
  2568. (void)p_end;
  2569. return true;
  2570. }
  2571. WASMModule *
  2572. wasm_loader_load(uint8 *buf, uint32 size, char *error_buf,
  2573. uint32 error_buf_size)
  2574. {
  2575. WASMModule *module = create_module(error_buf, error_buf_size);
  2576. if (!module) {
  2577. return NULL;
  2578. }
  2579. #if WASM_ENABLE_FAST_JIT != 0
  2580. module->load_addr = (uint8 *)buf;
  2581. module->load_size = size;
  2582. #endif
  2583. if (!load(buf, size, module, error_buf, error_buf_size)) {
  2584. goto fail;
  2585. }
  2586. LOG_VERBOSE("Load module success.\n");
  2587. return module;
  2588. fail:
  2589. wasm_loader_unload(module);
  2590. return NULL;
  2591. }
  2592. void
  2593. wasm_loader_unload(WASMModule *module)
  2594. {
  2595. uint32 i;
  2596. if (!module)
  2597. return;
  2598. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2599. && WASM_ENABLE_LAZY_JIT != 0
  2600. module->orcjit_stop_compiling = true;
  2601. if (module->llvm_jit_init_thread)
  2602. os_thread_join(module->llvm_jit_init_thread, NULL);
  2603. #endif
  2604. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2605. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  2606. module internal data after they were freed */
  2607. orcjit_stop_compile_threads(module);
  2608. #endif
  2609. #if WASM_ENABLE_JIT != 0
  2610. if (module->func_ptrs)
  2611. wasm_runtime_free(module->func_ptrs);
  2612. if (module->comp_ctx)
  2613. aot_destroy_comp_context(module->comp_ctx);
  2614. if (module->comp_data)
  2615. aot_destroy_comp_data(module->comp_data);
  2616. #endif
  2617. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2618. && WASM_ENABLE_LAZY_JIT != 0
  2619. if (module->tierup_wait_lock_inited) {
  2620. os_mutex_destroy(&module->tierup_wait_lock);
  2621. os_cond_destroy(&module->tierup_wait_cond);
  2622. }
  2623. #endif
  2624. if (module->types) {
  2625. for (i = 0; i < module->type_count; i++) {
  2626. if (module->types[i])
  2627. destroy_wasm_type(module->types[i]);
  2628. }
  2629. wasm_runtime_free(module->types);
  2630. }
  2631. if (module->imports)
  2632. wasm_runtime_free(module->imports);
  2633. if (module->functions) {
  2634. for (i = 0; i < module->function_count; i++) {
  2635. if (module->functions[i]) {
  2636. if (module->functions[i]->local_offsets)
  2637. wasm_runtime_free(module->functions[i]->local_offsets);
  2638. #if WASM_ENABLE_FAST_INTERP != 0
  2639. if (module->functions[i]->code_compiled)
  2640. wasm_runtime_free(module->functions[i]->code_compiled);
  2641. if (module->functions[i]->consts)
  2642. wasm_runtime_free(module->functions[i]->consts);
  2643. #endif
  2644. #if WASM_ENABLE_FAST_JIT != 0
  2645. if (module->functions[i]->fast_jit_jitted_code) {
  2646. jit_code_cache_free(
  2647. module->functions[i]->fast_jit_jitted_code);
  2648. }
  2649. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2650. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  2651. jit_code_cache_free(
  2652. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  2653. }
  2654. #endif
  2655. #endif
  2656. wasm_runtime_free(module->functions[i]);
  2657. }
  2658. }
  2659. wasm_runtime_free(module->functions);
  2660. }
  2661. if (module->tables)
  2662. wasm_runtime_free(module->tables);
  2663. if (module->memories)
  2664. wasm_runtime_free(module->memories);
  2665. if (module->globals)
  2666. wasm_runtime_free(module->globals);
  2667. if (module->exports)
  2668. wasm_runtime_free(module->exports);
  2669. if (module->table_segments) {
  2670. for (i = 0; i < module->table_seg_count; i++) {
  2671. if (module->table_segments[i].func_indexes)
  2672. wasm_runtime_free(module->table_segments[i].func_indexes);
  2673. }
  2674. wasm_runtime_free(module->table_segments);
  2675. }
  2676. if (module->data_segments) {
  2677. for (i = 0; i < module->data_seg_count; i++) {
  2678. if (module->data_segments[i])
  2679. wasm_runtime_free(module->data_segments[i]);
  2680. }
  2681. wasm_runtime_free(module->data_segments);
  2682. }
  2683. if (module->const_str_list) {
  2684. StringNode *node = module->const_str_list, *node_next;
  2685. while (node) {
  2686. node_next = node->next;
  2687. wasm_runtime_free(node);
  2688. node = node_next;
  2689. }
  2690. }
  2691. #if WASM_ENABLE_FAST_INTERP == 0
  2692. if (module->br_table_cache_list) {
  2693. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  2694. BrTableCache *node_next;
  2695. while (node) {
  2696. node_next = bh_list_elem_next(node);
  2697. wasm_runtime_free(node);
  2698. node = node_next;
  2699. }
  2700. }
  2701. #endif
  2702. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2703. && WASM_ENABLE_LAZY_JIT != 0
  2704. os_mutex_destroy(&module->instance_list_lock);
  2705. #endif
  2706. #if WASM_ENABLE_FAST_JIT != 0
  2707. if (module->fast_jit_func_ptrs) {
  2708. wasm_runtime_free(module->fast_jit_func_ptrs);
  2709. }
  2710. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  2711. if (module->fast_jit_thread_locks_inited[i]) {
  2712. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  2713. }
  2714. }
  2715. #endif
  2716. wasm_runtime_free(module);
  2717. }
  2718. bool
  2719. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  2720. const uint8 *start_addr, const uint8 *code_end_addr,
  2721. uint8 label_type, uint8 **p_else_addr,
  2722. uint8 **p_end_addr)
  2723. {
  2724. const uint8 *p = start_addr, *p_end = code_end_addr;
  2725. uint8 *else_addr = NULL;
  2726. char error_buf[128];
  2727. uint32 block_nested_depth = 1, count, i, j, t;
  2728. uint32 error_buf_size = sizeof(error_buf);
  2729. uint8 opcode, u8;
  2730. BlockAddr block_stack[16] = { 0 }, *block;
  2731. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2732. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2733. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  2734. if (block[j].start_addr == start_addr) {
  2735. /* Cache hit */
  2736. *p_else_addr = block[j].else_addr;
  2737. *p_end_addr = block[j].end_addr;
  2738. return true;
  2739. }
  2740. }
  2741. /* Cache unhit */
  2742. block_stack[0].start_addr = start_addr;
  2743. while (p < code_end_addr) {
  2744. opcode = *p++;
  2745. switch (opcode) {
  2746. case WASM_OP_UNREACHABLE:
  2747. case WASM_OP_NOP:
  2748. break;
  2749. case WASM_OP_BLOCK:
  2750. case WASM_OP_LOOP:
  2751. case WASM_OP_IF:
  2752. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  2753. u8 = read_uint8(p);
  2754. if (block_nested_depth
  2755. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2756. block_stack[block_nested_depth].start_addr = p;
  2757. block_stack[block_nested_depth].else_addr = NULL;
  2758. }
  2759. block_nested_depth++;
  2760. break;
  2761. case EXT_OP_BLOCK:
  2762. case EXT_OP_LOOP:
  2763. case EXT_OP_IF:
  2764. /* block type */
  2765. skip_leb_uint32(p, p_end);
  2766. if (block_nested_depth
  2767. < sizeof(block_stack) / sizeof(BlockAddr)) {
  2768. block_stack[block_nested_depth].start_addr = p;
  2769. block_stack[block_nested_depth].else_addr = NULL;
  2770. }
  2771. block_nested_depth++;
  2772. break;
  2773. case WASM_OP_ELSE:
  2774. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  2775. else_addr = (uint8 *)(p - 1);
  2776. if (block_nested_depth - 1
  2777. < sizeof(block_stack) / sizeof(BlockAddr))
  2778. block_stack[block_nested_depth - 1].else_addr =
  2779. (uint8 *)(p - 1);
  2780. break;
  2781. case WASM_OP_END:
  2782. if (block_nested_depth == 1) {
  2783. if (label_type == LABEL_TYPE_IF)
  2784. *p_else_addr = else_addr;
  2785. *p_end_addr = (uint8 *)(p - 1);
  2786. block_stack[0].end_addr = (uint8 *)(p - 1);
  2787. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  2788. t++) {
  2789. start_addr = block_stack[t].start_addr;
  2790. if (start_addr) {
  2791. i = ((uintptr_t)start_addr)
  2792. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  2793. block =
  2794. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  2795. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  2796. if (!block[j].start_addr)
  2797. break;
  2798. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  2799. memmove(block + 1, block,
  2800. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  2801. * sizeof(BlockAddr));
  2802. j = 0;
  2803. }
  2804. block[j].start_addr = block_stack[t].start_addr;
  2805. block[j].else_addr = block_stack[t].else_addr;
  2806. block[j].end_addr = block_stack[t].end_addr;
  2807. }
  2808. else
  2809. break;
  2810. }
  2811. return true;
  2812. }
  2813. else {
  2814. block_nested_depth--;
  2815. if (block_nested_depth
  2816. < sizeof(block_stack) / sizeof(BlockAddr))
  2817. block_stack[block_nested_depth].end_addr =
  2818. (uint8 *)(p - 1);
  2819. }
  2820. break;
  2821. case WASM_OP_BR:
  2822. case WASM_OP_BR_IF:
  2823. skip_leb_uint32(p, p_end); /* labelidx */
  2824. break;
  2825. case WASM_OP_BR_TABLE:
  2826. read_leb_uint32(p, p_end, count); /* lable num */
  2827. #if WASM_ENABLE_FAST_INTERP != 0
  2828. for (i = 0; i <= count; i++) /* lableidxs */
  2829. skip_leb_uint32(p, p_end);
  2830. #else
  2831. p += count + 1;
  2832. while (*p == WASM_OP_NOP)
  2833. p++;
  2834. #endif
  2835. break;
  2836. #if WASM_ENABLE_FAST_INTERP == 0
  2837. case EXT_OP_BR_TABLE_CACHE:
  2838. read_leb_uint32(p, p_end, count); /* lable num */
  2839. while (*p == WASM_OP_NOP)
  2840. p++;
  2841. break;
  2842. #endif
  2843. case WASM_OP_RETURN:
  2844. break;
  2845. case WASM_OP_CALL:
  2846. #if WASM_ENABLE_TAIL_CALL != 0
  2847. case WASM_OP_RETURN_CALL:
  2848. #endif
  2849. skip_leb_uint32(p, p_end); /* funcidx */
  2850. break;
  2851. case WASM_OP_CALL_INDIRECT:
  2852. #if WASM_ENABLE_TAIL_CALL != 0
  2853. case WASM_OP_RETURN_CALL_INDIRECT:
  2854. #endif
  2855. skip_leb_uint32(p, p_end); /* typeidx */
  2856. CHECK_BUF(p, p_end, 1);
  2857. u8 = read_uint8(p); /* 0x00 */
  2858. break;
  2859. case WASM_OP_DROP:
  2860. case WASM_OP_SELECT:
  2861. case WASM_OP_DROP_64:
  2862. case WASM_OP_SELECT_64:
  2863. break;
  2864. #if WASM_ENABLE_REF_TYPES != 0
  2865. case WASM_OP_SELECT_T:
  2866. skip_leb_uint32(p, p_end); /* vec length */
  2867. CHECK_BUF(p, p_end, 1);
  2868. u8 = read_uint8(p); /* typeidx */
  2869. break;
  2870. case WASM_OP_TABLE_GET:
  2871. case WASM_OP_TABLE_SET:
  2872. skip_leb_uint32(p, p_end); /* table index */
  2873. break;
  2874. case WASM_OP_REF_NULL:
  2875. CHECK_BUF(p, p_end, 1);
  2876. u8 = read_uint8(p); /* type */
  2877. break;
  2878. case WASM_OP_REF_IS_NULL:
  2879. break;
  2880. case WASM_OP_REF_FUNC:
  2881. skip_leb_uint32(p, p_end); /* func index */
  2882. break;
  2883. #endif /* WASM_ENABLE_REF_TYPES */
  2884. case WASM_OP_GET_LOCAL:
  2885. case WASM_OP_SET_LOCAL:
  2886. case WASM_OP_TEE_LOCAL:
  2887. case WASM_OP_GET_GLOBAL:
  2888. case WASM_OP_SET_GLOBAL:
  2889. case WASM_OP_GET_GLOBAL_64:
  2890. case WASM_OP_SET_GLOBAL_64:
  2891. case WASM_OP_SET_GLOBAL_AUX_STACK:
  2892. skip_leb_uint32(p, p_end); /* localidx */
  2893. break;
  2894. case EXT_OP_GET_LOCAL_FAST:
  2895. case EXT_OP_SET_LOCAL_FAST:
  2896. case EXT_OP_TEE_LOCAL_FAST:
  2897. CHECK_BUF(p, p_end, 1);
  2898. p++;
  2899. break;
  2900. case WASM_OP_I32_LOAD:
  2901. case WASM_OP_I64_LOAD:
  2902. case WASM_OP_F32_LOAD:
  2903. case WASM_OP_F64_LOAD:
  2904. case WASM_OP_I32_LOAD8_S:
  2905. case WASM_OP_I32_LOAD8_U:
  2906. case WASM_OP_I32_LOAD16_S:
  2907. case WASM_OP_I32_LOAD16_U:
  2908. case WASM_OP_I64_LOAD8_S:
  2909. case WASM_OP_I64_LOAD8_U:
  2910. case WASM_OP_I64_LOAD16_S:
  2911. case WASM_OP_I64_LOAD16_U:
  2912. case WASM_OP_I64_LOAD32_S:
  2913. case WASM_OP_I64_LOAD32_U:
  2914. case WASM_OP_I32_STORE:
  2915. case WASM_OP_I64_STORE:
  2916. case WASM_OP_F32_STORE:
  2917. case WASM_OP_F64_STORE:
  2918. case WASM_OP_I32_STORE8:
  2919. case WASM_OP_I32_STORE16:
  2920. case WASM_OP_I64_STORE8:
  2921. case WASM_OP_I64_STORE16:
  2922. case WASM_OP_I64_STORE32:
  2923. skip_leb_uint32(p, p_end); /* align */
  2924. skip_leb_uint32(p, p_end); /* offset */
  2925. break;
  2926. case WASM_OP_MEMORY_SIZE:
  2927. case WASM_OP_MEMORY_GROW:
  2928. skip_leb_uint32(p, p_end); /* 0x00 */
  2929. break;
  2930. case WASM_OP_I32_CONST:
  2931. skip_leb_int32(p, p_end);
  2932. break;
  2933. case WASM_OP_I64_CONST:
  2934. skip_leb_int64(p, p_end);
  2935. break;
  2936. case WASM_OP_F32_CONST:
  2937. p += sizeof(float32);
  2938. break;
  2939. case WASM_OP_F64_CONST:
  2940. p += sizeof(float64);
  2941. break;
  2942. case WASM_OP_I32_EQZ:
  2943. case WASM_OP_I32_EQ:
  2944. case WASM_OP_I32_NE:
  2945. case WASM_OP_I32_LT_S:
  2946. case WASM_OP_I32_LT_U:
  2947. case WASM_OP_I32_GT_S:
  2948. case WASM_OP_I32_GT_U:
  2949. case WASM_OP_I32_LE_S:
  2950. case WASM_OP_I32_LE_U:
  2951. case WASM_OP_I32_GE_S:
  2952. case WASM_OP_I32_GE_U:
  2953. case WASM_OP_I64_EQZ:
  2954. case WASM_OP_I64_EQ:
  2955. case WASM_OP_I64_NE:
  2956. case WASM_OP_I64_LT_S:
  2957. case WASM_OP_I64_LT_U:
  2958. case WASM_OP_I64_GT_S:
  2959. case WASM_OP_I64_GT_U:
  2960. case WASM_OP_I64_LE_S:
  2961. case WASM_OP_I64_LE_U:
  2962. case WASM_OP_I64_GE_S:
  2963. case WASM_OP_I64_GE_U:
  2964. case WASM_OP_F32_EQ:
  2965. case WASM_OP_F32_NE:
  2966. case WASM_OP_F32_LT:
  2967. case WASM_OP_F32_GT:
  2968. case WASM_OP_F32_LE:
  2969. case WASM_OP_F32_GE:
  2970. case WASM_OP_F64_EQ:
  2971. case WASM_OP_F64_NE:
  2972. case WASM_OP_F64_LT:
  2973. case WASM_OP_F64_GT:
  2974. case WASM_OP_F64_LE:
  2975. case WASM_OP_F64_GE:
  2976. case WASM_OP_I32_CLZ:
  2977. case WASM_OP_I32_CTZ:
  2978. case WASM_OP_I32_POPCNT:
  2979. case WASM_OP_I32_ADD:
  2980. case WASM_OP_I32_SUB:
  2981. case WASM_OP_I32_MUL:
  2982. case WASM_OP_I32_DIV_S:
  2983. case WASM_OP_I32_DIV_U:
  2984. case WASM_OP_I32_REM_S:
  2985. case WASM_OP_I32_REM_U:
  2986. case WASM_OP_I32_AND:
  2987. case WASM_OP_I32_OR:
  2988. case WASM_OP_I32_XOR:
  2989. case WASM_OP_I32_SHL:
  2990. case WASM_OP_I32_SHR_S:
  2991. case WASM_OP_I32_SHR_U:
  2992. case WASM_OP_I32_ROTL:
  2993. case WASM_OP_I32_ROTR:
  2994. case WASM_OP_I64_CLZ:
  2995. case WASM_OP_I64_CTZ:
  2996. case WASM_OP_I64_POPCNT:
  2997. case WASM_OP_I64_ADD:
  2998. case WASM_OP_I64_SUB:
  2999. case WASM_OP_I64_MUL:
  3000. case WASM_OP_I64_DIV_S:
  3001. case WASM_OP_I64_DIV_U:
  3002. case WASM_OP_I64_REM_S:
  3003. case WASM_OP_I64_REM_U:
  3004. case WASM_OP_I64_AND:
  3005. case WASM_OP_I64_OR:
  3006. case WASM_OP_I64_XOR:
  3007. case WASM_OP_I64_SHL:
  3008. case WASM_OP_I64_SHR_S:
  3009. case WASM_OP_I64_SHR_U:
  3010. case WASM_OP_I64_ROTL:
  3011. case WASM_OP_I64_ROTR:
  3012. case WASM_OP_F32_ABS:
  3013. case WASM_OP_F32_NEG:
  3014. case WASM_OP_F32_CEIL:
  3015. case WASM_OP_F32_FLOOR:
  3016. case WASM_OP_F32_TRUNC:
  3017. case WASM_OP_F32_NEAREST:
  3018. case WASM_OP_F32_SQRT:
  3019. case WASM_OP_F32_ADD:
  3020. case WASM_OP_F32_SUB:
  3021. case WASM_OP_F32_MUL:
  3022. case WASM_OP_F32_DIV:
  3023. case WASM_OP_F32_MIN:
  3024. case WASM_OP_F32_MAX:
  3025. case WASM_OP_F32_COPYSIGN:
  3026. case WASM_OP_F64_ABS:
  3027. case WASM_OP_F64_NEG:
  3028. case WASM_OP_F64_CEIL:
  3029. case WASM_OP_F64_FLOOR:
  3030. case WASM_OP_F64_TRUNC:
  3031. case WASM_OP_F64_NEAREST:
  3032. case WASM_OP_F64_SQRT:
  3033. case WASM_OP_F64_ADD:
  3034. case WASM_OP_F64_SUB:
  3035. case WASM_OP_F64_MUL:
  3036. case WASM_OP_F64_DIV:
  3037. case WASM_OP_F64_MIN:
  3038. case WASM_OP_F64_MAX:
  3039. case WASM_OP_F64_COPYSIGN:
  3040. case WASM_OP_I32_WRAP_I64:
  3041. case WASM_OP_I32_TRUNC_S_F32:
  3042. case WASM_OP_I32_TRUNC_U_F32:
  3043. case WASM_OP_I32_TRUNC_S_F64:
  3044. case WASM_OP_I32_TRUNC_U_F64:
  3045. case WASM_OP_I64_EXTEND_S_I32:
  3046. case WASM_OP_I64_EXTEND_U_I32:
  3047. case WASM_OP_I64_TRUNC_S_F32:
  3048. case WASM_OP_I64_TRUNC_U_F32:
  3049. case WASM_OP_I64_TRUNC_S_F64:
  3050. case WASM_OP_I64_TRUNC_U_F64:
  3051. case WASM_OP_F32_CONVERT_S_I32:
  3052. case WASM_OP_F32_CONVERT_U_I32:
  3053. case WASM_OP_F32_CONVERT_S_I64:
  3054. case WASM_OP_F32_CONVERT_U_I64:
  3055. case WASM_OP_F32_DEMOTE_F64:
  3056. case WASM_OP_F64_CONVERT_S_I32:
  3057. case WASM_OP_F64_CONVERT_U_I32:
  3058. case WASM_OP_F64_CONVERT_S_I64:
  3059. case WASM_OP_F64_CONVERT_U_I64:
  3060. case WASM_OP_F64_PROMOTE_F32:
  3061. case WASM_OP_I32_REINTERPRET_F32:
  3062. case WASM_OP_I64_REINTERPRET_F64:
  3063. case WASM_OP_F32_REINTERPRET_I32:
  3064. case WASM_OP_F64_REINTERPRET_I64:
  3065. case WASM_OP_I32_EXTEND8_S:
  3066. case WASM_OP_I32_EXTEND16_S:
  3067. case WASM_OP_I64_EXTEND8_S:
  3068. case WASM_OP_I64_EXTEND16_S:
  3069. case WASM_OP_I64_EXTEND32_S:
  3070. break;
  3071. case WASM_OP_MISC_PREFIX:
  3072. {
  3073. uint32 opcode1;
  3074. read_leb_uint32(p, p_end, opcode1);
  3075. switch (opcode1) {
  3076. case WASM_OP_I32_TRUNC_SAT_S_F32:
  3077. case WASM_OP_I32_TRUNC_SAT_U_F32:
  3078. case WASM_OP_I32_TRUNC_SAT_S_F64:
  3079. case WASM_OP_I32_TRUNC_SAT_U_F64:
  3080. case WASM_OP_I64_TRUNC_SAT_S_F32:
  3081. case WASM_OP_I64_TRUNC_SAT_U_F32:
  3082. case WASM_OP_I64_TRUNC_SAT_S_F64:
  3083. case WASM_OP_I64_TRUNC_SAT_U_F64:
  3084. break;
  3085. #if WASM_ENABLE_BULK_MEMORY != 0
  3086. case WASM_OP_MEMORY_INIT:
  3087. skip_leb_uint32(p, p_end);
  3088. /* skip memory idx */
  3089. p++;
  3090. break;
  3091. case WASM_OP_DATA_DROP:
  3092. skip_leb_uint32(p, p_end);
  3093. break;
  3094. case WASM_OP_MEMORY_COPY:
  3095. /* skip two memory idx */
  3096. p += 2;
  3097. break;
  3098. case WASM_OP_MEMORY_FILL:
  3099. /* skip memory idx */
  3100. p++;
  3101. break;
  3102. #endif
  3103. #if WASM_ENABLE_REF_TYPES != 0
  3104. case WASM_OP_TABLE_INIT:
  3105. case WASM_OP_TABLE_COPY:
  3106. /* tableidx */
  3107. skip_leb_uint32(p, p_end);
  3108. /* elemidx */
  3109. skip_leb_uint32(p, p_end);
  3110. break;
  3111. case WASM_OP_ELEM_DROP:
  3112. /* elemidx */
  3113. skip_leb_uint32(p, p_end);
  3114. break;
  3115. case WASM_OP_TABLE_SIZE:
  3116. case WASM_OP_TABLE_GROW:
  3117. case WASM_OP_TABLE_FILL:
  3118. skip_leb_uint32(p, p_end); /* table idx */
  3119. break;
  3120. #endif /* WASM_ENABLE_REF_TYPES */
  3121. default:
  3122. bh_assert(0);
  3123. break;
  3124. }
  3125. break;
  3126. }
  3127. #if WASM_ENABLE_SHARED_MEMORY != 0
  3128. case WASM_OP_ATOMIC_PREFIX:
  3129. {
  3130. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  3131. opcode = read_uint8(p);
  3132. if (opcode != WASM_OP_ATOMIC_FENCE) {
  3133. skip_leb_uint32(p, p_end); /* align */
  3134. skip_leb_uint32(p, p_end); /* offset */
  3135. }
  3136. else {
  3137. /* atomic.fence doesn't have memarg */
  3138. p++;
  3139. }
  3140. break;
  3141. }
  3142. #endif
  3143. default:
  3144. bh_assert(0);
  3145. break;
  3146. }
  3147. }
  3148. (void)u8;
  3149. return false;
  3150. }
  3151. #define REF_I32 VALUE_TYPE_I32
  3152. #define REF_F32 VALUE_TYPE_F32
  3153. #define REF_I64_1 VALUE_TYPE_I64
  3154. #define REF_I64_2 VALUE_TYPE_I64
  3155. #define REF_F64_1 VALUE_TYPE_F64
  3156. #define REF_F64_2 VALUE_TYPE_F64
  3157. #define REF_ANY VALUE_TYPE_ANY
  3158. #if WASM_ENABLE_FAST_INTERP != 0
  3159. #if WASM_DEBUG_PREPROCESSOR != 0
  3160. #define LOG_OP(...) os_printf(__VA_ARGS__)
  3161. #else
  3162. #define LOG_OP(...) (void)0
  3163. #endif
  3164. #define PATCH_ELSE 0
  3165. #define PATCH_END 1
  3166. typedef struct BranchBlockPatch {
  3167. struct BranchBlockPatch *next;
  3168. uint8 patch_type;
  3169. uint8 *code_compiled;
  3170. } BranchBlockPatch;
  3171. #endif
  3172. typedef struct BranchBlock {
  3173. uint8 label_type;
  3174. BlockType block_type;
  3175. uint8 *start_addr;
  3176. uint8 *else_addr;
  3177. uint8 *end_addr;
  3178. uint32 stack_cell_num;
  3179. #if WASM_ENABLE_FAST_INTERP != 0
  3180. uint16 dynamic_offset;
  3181. uint8 *code_compiled;
  3182. BranchBlockPatch *patch_list;
  3183. /* This is used to save params frame_offset of of if block */
  3184. int16 *param_frame_offsets;
  3185. #endif
  3186. /* Indicate the operand stack is in polymorphic state.
  3187. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  3188. * to polymorphic state until the block's 'end' opcode is processed.
  3189. * If stack is in polymorphic state and stack is empty, instruction can
  3190. * pop any type of value directly without decreasing stack top pointer
  3191. * and stack cell num. */
  3192. bool is_stack_polymorphic;
  3193. } BranchBlock;
  3194. typedef struct WASMLoaderContext {
  3195. /* frame ref stack */
  3196. uint8 *frame_ref;
  3197. uint8 *frame_ref_bottom;
  3198. uint8 *frame_ref_boundary;
  3199. uint32 frame_ref_size;
  3200. uint32 stack_cell_num;
  3201. uint32 max_stack_cell_num;
  3202. /* frame csp stack */
  3203. BranchBlock *frame_csp;
  3204. BranchBlock *frame_csp_bottom;
  3205. BranchBlock *frame_csp_boundary;
  3206. uint32 frame_csp_size;
  3207. uint32 csp_num;
  3208. uint32 max_csp_num;
  3209. #if WASM_ENABLE_FAST_INTERP != 0
  3210. /* frame offset stack */
  3211. int16 *frame_offset;
  3212. int16 *frame_offset_bottom;
  3213. int16 *frame_offset_boundary;
  3214. uint32 frame_offset_size;
  3215. int16 dynamic_offset;
  3216. int16 start_dynamic_offset;
  3217. int16 max_dynamic_offset;
  3218. /* preserved local offset */
  3219. int16 preserved_local_offset;
  3220. /* const buffer */
  3221. uint8 *const_buf;
  3222. uint16 num_const;
  3223. uint16 const_cell_num;
  3224. uint32 const_buf_size;
  3225. /* processed code */
  3226. uint8 *p_code_compiled;
  3227. uint8 *p_code_compiled_end;
  3228. uint32 code_compiled_size;
  3229. /* If the last opcode will be dropped, the peak memory usage will be larger
  3230. * than the final code_compiled_size, we record the peak size to ensure
  3231. * there will not be invalid memory access during second traverse */
  3232. uint32 code_compiled_peak_size;
  3233. #endif
  3234. } WASMLoaderContext;
  3235. typedef struct Const {
  3236. WASMValue value;
  3237. uint16 slot_index;
  3238. uint8 value_type;
  3239. } Const;
  3240. static void *
  3241. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  3242. uint32 error_buf_size)
  3243. {
  3244. uint8 *mem_new;
  3245. bh_assert(size_new > size_old);
  3246. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  3247. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  3248. memset(mem_new + size_old, 0, size_new - size_old);
  3249. wasm_runtime_free(mem_old);
  3250. }
  3251. return mem_new;
  3252. }
  3253. #define MEM_REALLOC(mem, size_old, size_new) \
  3254. do { \
  3255. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  3256. error_buf_size); \
  3257. if (!mem_new) \
  3258. goto fail; \
  3259. mem = mem_new; \
  3260. } while (0)
  3261. #define CHECK_CSP_PUSH() \
  3262. do { \
  3263. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  3264. MEM_REALLOC( \
  3265. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  3266. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  3267. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  3268. ctx->frame_csp_boundary = \
  3269. ctx->frame_csp_bottom \
  3270. + ctx->frame_csp_size / sizeof(BranchBlock); \
  3271. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  3272. } \
  3273. } while (0)
  3274. #define CHECK_CSP_POP() \
  3275. do { \
  3276. bh_assert(ctx->csp_num >= 1); \
  3277. } while (0)
  3278. #if WASM_ENABLE_FAST_INTERP != 0
  3279. static bool
  3280. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  3281. uint32 error_buf_size)
  3282. {
  3283. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  3284. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  3285. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  3286. ctx->frame_offset_size + 16);
  3287. ctx->frame_offset_size += 16;
  3288. ctx->frame_offset_boundary =
  3289. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  3290. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  3291. }
  3292. return true;
  3293. fail:
  3294. return false;
  3295. }
  3296. static bool
  3297. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  3298. {
  3299. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  3300. return false;
  3301. return true;
  3302. }
  3303. static void
  3304. free_label_patch_list(BranchBlock *frame_csp)
  3305. {
  3306. BranchBlockPatch *label_patch = frame_csp->patch_list;
  3307. BranchBlockPatch *next;
  3308. while (label_patch != NULL) {
  3309. next = label_patch->next;
  3310. wasm_runtime_free(label_patch);
  3311. label_patch = next;
  3312. }
  3313. frame_csp->patch_list = NULL;
  3314. }
  3315. static void
  3316. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  3317. {
  3318. BranchBlock *tmp_csp = frame_csp;
  3319. for (uint32 i = 0; i < csp_num; i++) {
  3320. free_label_patch_list(tmp_csp);
  3321. tmp_csp++;
  3322. }
  3323. }
  3324. #endif
  3325. static bool
  3326. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  3327. {
  3328. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  3329. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  3330. ctx->frame_ref_size + 16);
  3331. ctx->frame_ref_size += 16;
  3332. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  3333. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  3334. }
  3335. return true;
  3336. fail:
  3337. return false;
  3338. }
  3339. static bool
  3340. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  3341. char *error_buf, uint32 error_buf_size)
  3342. {
  3343. bh_assert(!((is_32bit_type(type) && stack_cell_num < 1)
  3344. || (is_64bit_type(type) && stack_cell_num < 2)));
  3345. bh_assert(!(
  3346. (type == VALUE_TYPE_I32 && *(frame_ref - 1) != REF_I32)
  3347. || (type == VALUE_TYPE_F32 && *(frame_ref - 1) != REF_F32)
  3348. || (type == VALUE_TYPE_I64
  3349. && (*(frame_ref - 2) != REF_I64_1 || *(frame_ref - 1) != REF_I64_2))
  3350. || (type == VALUE_TYPE_F64
  3351. && (*(frame_ref - 2) != REF_F64_1
  3352. || *(frame_ref - 1) != REF_F64_2))));
  3353. return true;
  3354. }
  3355. static bool
  3356. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3357. uint32 error_buf_size)
  3358. {
  3359. int32 block_stack_cell_num =
  3360. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  3361. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  3362. /* the stack top is a value of any type, return success */
  3363. return true;
  3364. }
  3365. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  3366. error_buf, error_buf_size))
  3367. return false;
  3368. return true;
  3369. }
  3370. static void
  3371. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  3372. {
  3373. if (ctx) {
  3374. if (ctx->frame_ref_bottom)
  3375. wasm_runtime_free(ctx->frame_ref_bottom);
  3376. if (ctx->frame_csp_bottom) {
  3377. #if WASM_ENABLE_FAST_INTERP != 0
  3378. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  3379. #endif
  3380. wasm_runtime_free(ctx->frame_csp_bottom);
  3381. }
  3382. #if WASM_ENABLE_FAST_INTERP != 0
  3383. if (ctx->frame_offset_bottom)
  3384. wasm_runtime_free(ctx->frame_offset_bottom);
  3385. if (ctx->const_buf)
  3386. wasm_runtime_free(ctx->const_buf);
  3387. #endif
  3388. wasm_runtime_free(ctx);
  3389. }
  3390. }
  3391. static WASMLoaderContext *
  3392. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  3393. {
  3394. WASMLoaderContext *loader_ctx =
  3395. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  3396. if (!loader_ctx)
  3397. return NULL;
  3398. loader_ctx->frame_ref_size = 32;
  3399. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  3400. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  3401. goto fail;
  3402. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  3403. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  3404. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  3405. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  3406. goto fail;
  3407. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  3408. #if WASM_ENABLE_FAST_INTERP != 0
  3409. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  3410. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  3411. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  3412. error_buf_size)))
  3413. goto fail;
  3414. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  3415. loader_ctx->num_const = 0;
  3416. loader_ctx->const_buf_size = sizeof(Const) * 8;
  3417. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  3418. error_buf, error_buf_size)))
  3419. goto fail;
  3420. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  3421. set_error_buf(error_buf, error_buf_size,
  3422. "fast interpreter offset overflow");
  3423. goto fail;
  3424. }
  3425. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  3426. loader_ctx->max_dynamic_offset =
  3427. func->param_cell_num + func->local_cell_num;
  3428. #endif
  3429. return loader_ctx;
  3430. fail:
  3431. wasm_loader_ctx_destroy(loader_ctx);
  3432. return NULL;
  3433. }
  3434. static bool
  3435. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3436. uint32 error_buf_size)
  3437. {
  3438. if (type == VALUE_TYPE_VOID)
  3439. return true;
  3440. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3441. return false;
  3442. *ctx->frame_ref++ = type;
  3443. ctx->stack_cell_num++;
  3444. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  3445. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3446. if (is_32bit_type(type))
  3447. return true;
  3448. if (!check_stack_push(ctx, error_buf, error_buf_size))
  3449. return false;
  3450. *ctx->frame_ref++ = type;
  3451. ctx->stack_cell_num++;
  3452. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  3453. ctx->max_stack_cell_num = ctx->stack_cell_num;
  3454. bh_assert(ctx->max_stack_cell_num <= UINT16_MAX);
  3455. }
  3456. return true;
  3457. }
  3458. static bool
  3459. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  3460. uint32 error_buf_size)
  3461. {
  3462. BranchBlock *cur_block = ctx->frame_csp - 1;
  3463. int32 available_stack_cell =
  3464. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  3465. /* Directly return success if current block is in stack
  3466. * polymorphic state while stack is empty. */
  3467. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  3468. return true;
  3469. if (type == VALUE_TYPE_VOID)
  3470. return true;
  3471. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  3472. return false;
  3473. ctx->frame_ref--;
  3474. ctx->stack_cell_num--;
  3475. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  3476. return true;
  3477. ctx->frame_ref--;
  3478. ctx->stack_cell_num--;
  3479. return true;
  3480. }
  3481. #if WASM_ENABLE_FAST_INTERP == 0
  3482. static bool
  3483. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  3484. uint8 type_push, uint8 type_pop, char *error_buf,
  3485. uint32 error_buf_size)
  3486. {
  3487. for (int i = 0; i < pop_cnt; i++) {
  3488. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  3489. error_buf_size))
  3490. return false;
  3491. }
  3492. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  3493. return false;
  3494. return true;
  3495. }
  3496. #endif
  3497. static bool
  3498. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  3499. BlockType block_type, uint8 *start_addr,
  3500. char *error_buf, uint32 error_buf_size)
  3501. {
  3502. CHECK_CSP_PUSH();
  3503. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  3504. ctx->frame_csp->label_type = label_type;
  3505. ctx->frame_csp->block_type = block_type;
  3506. ctx->frame_csp->start_addr = start_addr;
  3507. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  3508. #if WASM_ENABLE_FAST_INTERP != 0
  3509. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  3510. ctx->frame_csp->patch_list = NULL;
  3511. #endif
  3512. ctx->frame_csp++;
  3513. ctx->csp_num++;
  3514. if (ctx->csp_num > ctx->max_csp_num) {
  3515. ctx->max_csp_num = ctx->csp_num;
  3516. bh_assert(ctx->max_csp_num <= UINT16_MAX);
  3517. }
  3518. return true;
  3519. fail:
  3520. return false;
  3521. }
  3522. static bool
  3523. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  3524. uint32 error_buf_size)
  3525. {
  3526. CHECK_CSP_POP();
  3527. #if WASM_ENABLE_FAST_INTERP != 0
  3528. if ((ctx->frame_csp - 1)->param_frame_offsets)
  3529. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  3530. #endif
  3531. ctx->frame_csp--;
  3532. ctx->csp_num--;
  3533. return true;
  3534. }
  3535. #if WASM_ENABLE_FAST_INTERP != 0
  3536. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3537. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  3538. #define emit_label(opcode) \
  3539. do { \
  3540. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  3541. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3542. } while (0)
  3543. #define skip_label() \
  3544. do { \
  3545. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  3546. LOG_OP("\ndelete last op\n"); \
  3547. } while (0)
  3548. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3549. #define emit_label(opcode) \
  3550. do { \
  3551. int32 offset = \
  3552. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  3553. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  3554. set_error_buf(error_buf, error_buf_size, \
  3555. "pre-compiled label offset out of range"); \
  3556. goto fail; \
  3557. } \
  3558. wasm_loader_emit_int16(loader_ctx, offset); \
  3559. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3560. } while (0)
  3561. #define skip_label() \
  3562. do { \
  3563. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  3564. LOG_OP("\ndelete last op\n"); \
  3565. } while (0)
  3566. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  3567. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  3568. #define emit_label(opcode) \
  3569. do { \
  3570. wasm_loader_emit_uint8(loader_ctx, opcode); \
  3571. LOG_OP("\nemit_op [%02x]\t", opcode); \
  3572. } while (0)
  3573. #define skip_label() \
  3574. do { \
  3575. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  3576. LOG_OP("\ndelete last op\n"); \
  3577. } while (0)
  3578. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  3579. #define emit_empty_label_addr_and_frame_ip(type) \
  3580. do { \
  3581. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  3582. loader_ctx->p_code_compiled, error_buf, \
  3583. error_buf_size)) \
  3584. goto fail; \
  3585. /* label address, to be patched */ \
  3586. wasm_loader_emit_ptr(loader_ctx, NULL); \
  3587. } while (0)
  3588. #define emit_br_info(frame_csp) \
  3589. do { \
  3590. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  3591. error_buf_size)) \
  3592. goto fail; \
  3593. } while (0)
  3594. #define LAST_OP_OUTPUT_I32() \
  3595. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  3596. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  3597. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  3598. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  3599. || (last_op >= WASM_OP_I32_WRAP_I64 \
  3600. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  3601. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  3602. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  3603. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  3604. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  3605. || (last_op == EXT_OP_COPY_STACK_TOP)
  3606. #define LAST_OP_OUTPUT_I64() \
  3607. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  3608. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  3609. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  3610. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  3611. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  3612. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  3613. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  3614. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  3615. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  3616. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  3617. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  3618. #define GET_CONST_OFFSET(type, val) \
  3619. do { \
  3620. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  3621. &operand_offset, error_buf, \
  3622. error_buf_size))) \
  3623. goto fail; \
  3624. } while (0)
  3625. #define GET_CONST_F32_OFFSET(type, fval) \
  3626. do { \
  3627. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3628. &operand_offset, error_buf, \
  3629. error_buf_size))) \
  3630. goto fail; \
  3631. } while (0)
  3632. #define GET_CONST_F64_OFFSET(type, fval) \
  3633. do { \
  3634. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  3635. &operand_offset, error_buf, \
  3636. error_buf_size))) \
  3637. goto fail; \
  3638. } while (0)
  3639. #define emit_operand(ctx, offset) \
  3640. do { \
  3641. wasm_loader_emit_int16(ctx, offset); \
  3642. LOG_OP("%d\t", offset); \
  3643. } while (0)
  3644. #define emit_byte(ctx, byte) \
  3645. do { \
  3646. wasm_loader_emit_uint8(ctx, byte); \
  3647. LOG_OP("%d\t", byte); \
  3648. } while (0)
  3649. #define emit_uint32(ctx, value) \
  3650. do { \
  3651. wasm_loader_emit_uint32(ctx, value); \
  3652. LOG_OP("%d\t", value); \
  3653. } while (0)
  3654. #define emit_uint64(ctx, value) \
  3655. do { \
  3656. wasm_loader_emit_const(ctx, &value, false); \
  3657. LOG_OP("%lld\t", value); \
  3658. } while (0)
  3659. #define emit_float32(ctx, value) \
  3660. do { \
  3661. wasm_loader_emit_const(ctx, &value, true); \
  3662. LOG_OP("%f\t", value); \
  3663. } while (0)
  3664. #define emit_float64(ctx, value) \
  3665. do { \
  3666. wasm_loader_emit_const(ctx, &value, false); \
  3667. LOG_OP("%f\t", value); \
  3668. } while (0)
  3669. static bool
  3670. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  3671. {
  3672. if (!(ctx->p_code_compiled =
  3673. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  3674. return false;
  3675. ctx->p_code_compiled_end =
  3676. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  3677. /* clean up frame ref */
  3678. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  3679. ctx->frame_ref = ctx->frame_ref_bottom;
  3680. ctx->stack_cell_num = 0;
  3681. /* clean up frame csp */
  3682. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  3683. ctx->frame_csp = ctx->frame_csp_bottom;
  3684. ctx->csp_num = 0;
  3685. ctx->max_csp_num = 0;
  3686. /* clean up frame offset */
  3687. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  3688. ctx->frame_offset = ctx->frame_offset_bottom;
  3689. ctx->dynamic_offset = ctx->start_dynamic_offset;
  3690. /* init preserved local offsets */
  3691. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  3692. /* const buf is reserved */
  3693. return true;
  3694. }
  3695. static void
  3696. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  3697. {
  3698. ctx->code_compiled_size += size;
  3699. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  3700. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  3701. }
  3702. }
  3703. static void
  3704. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  3705. {
  3706. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  3707. if (ctx->p_code_compiled) {
  3708. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3709. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3710. #endif
  3711. bh_memcpy_s(ctx->p_code_compiled,
  3712. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  3713. value, size);
  3714. ctx->p_code_compiled += size;
  3715. }
  3716. else {
  3717. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3718. bh_assert((ctx->code_compiled_size & 1) == 0);
  3719. #endif
  3720. increase_compiled_code_space(ctx, size);
  3721. }
  3722. }
  3723. static void
  3724. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  3725. {
  3726. if (ctx->p_code_compiled) {
  3727. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3728. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3729. #endif
  3730. STORE_U32(ctx->p_code_compiled, value);
  3731. ctx->p_code_compiled += sizeof(uint32);
  3732. }
  3733. else {
  3734. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3735. bh_assert((ctx->code_compiled_size & 1) == 0);
  3736. #endif
  3737. increase_compiled_code_space(ctx, sizeof(uint32));
  3738. }
  3739. }
  3740. static void
  3741. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  3742. {
  3743. if (ctx->p_code_compiled) {
  3744. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3745. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3746. #endif
  3747. STORE_U16(ctx->p_code_compiled, (uint16)value);
  3748. ctx->p_code_compiled += sizeof(int16);
  3749. }
  3750. else {
  3751. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3752. bh_assert((ctx->code_compiled_size & 1) == 0);
  3753. #endif
  3754. increase_compiled_code_space(ctx, sizeof(uint16));
  3755. }
  3756. }
  3757. static void
  3758. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  3759. {
  3760. if (ctx->p_code_compiled) {
  3761. *(ctx->p_code_compiled) = value;
  3762. ctx->p_code_compiled += sizeof(uint8);
  3763. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3764. ctx->p_code_compiled++;
  3765. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3766. #endif
  3767. }
  3768. else {
  3769. increase_compiled_code_space(ctx, sizeof(uint8));
  3770. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3771. increase_compiled_code_space(ctx, sizeof(uint8));
  3772. bh_assert((ctx->code_compiled_size & 1) == 0);
  3773. #endif
  3774. }
  3775. }
  3776. static void
  3777. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  3778. {
  3779. if (ctx->p_code_compiled) {
  3780. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3781. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3782. #endif
  3783. STORE_PTR(ctx->p_code_compiled, value);
  3784. ctx->p_code_compiled += sizeof(void *);
  3785. }
  3786. else {
  3787. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3788. bh_assert((ctx->code_compiled_size & 1) == 0);
  3789. #endif
  3790. increase_compiled_code_space(ctx, sizeof(void *));
  3791. }
  3792. }
  3793. static void
  3794. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  3795. {
  3796. if (ctx->p_code_compiled) {
  3797. ctx->p_code_compiled -= size;
  3798. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3799. if (size == sizeof(uint8)) {
  3800. ctx->p_code_compiled--;
  3801. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  3802. }
  3803. #endif
  3804. }
  3805. else {
  3806. ctx->code_compiled_size -= size;
  3807. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3808. if (size == sizeof(uint8)) {
  3809. ctx->code_compiled_size--;
  3810. bh_assert((ctx->code_compiled_size & 1) == 0);
  3811. }
  3812. #endif
  3813. }
  3814. }
  3815. static bool
  3816. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  3817. uint32 local_index, uint32 local_type,
  3818. bool *preserved, char *error_buf,
  3819. uint32 error_buf_size)
  3820. {
  3821. uint32 i = 0;
  3822. int16 preserved_offset = (int16)local_index;
  3823. *preserved = false;
  3824. while (i < loader_ctx->stack_cell_num) {
  3825. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3826. /* move previous local into dynamic space before a set/tee_local opcode
  3827. */
  3828. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  3829. if (!(*preserved)) {
  3830. *preserved = true;
  3831. skip_label();
  3832. preserved_offset = loader_ctx->preserved_local_offset;
  3833. if (loader_ctx->p_code_compiled) {
  3834. bh_assert(preserved_offset != (int16)local_index);
  3835. }
  3836. if (is_32bit_type(local_type)) {
  3837. /* Only increase preserve offset in the second traversal */
  3838. if (loader_ctx->p_code_compiled)
  3839. loader_ctx->preserved_local_offset++;
  3840. emit_label(EXT_OP_COPY_STACK_TOP);
  3841. }
  3842. else {
  3843. if (loader_ctx->p_code_compiled)
  3844. loader_ctx->preserved_local_offset += 2;
  3845. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3846. }
  3847. emit_operand(loader_ctx, local_index);
  3848. emit_operand(loader_ctx, preserved_offset);
  3849. emit_label(opcode);
  3850. }
  3851. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  3852. }
  3853. if (is_32bit_type(cur_type))
  3854. i++;
  3855. else
  3856. i += 2;
  3857. }
  3858. return true;
  3859. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3860. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  3861. fail:
  3862. return false;
  3863. #endif
  3864. #endif
  3865. }
  3866. static bool
  3867. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  3868. char *error_buf, uint32 error_buf_size)
  3869. {
  3870. uint32 i = 0;
  3871. bool preserve_local;
  3872. /* preserve locals before blocks to ensure that "tee/set_local" inside
  3873. blocks will not influence the value of these locals */
  3874. while (i < loader_ctx->stack_cell_num) {
  3875. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  3876. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  3877. if ((cur_offset < loader_ctx->start_dynamic_offset)
  3878. && (cur_offset >= 0)) {
  3879. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  3880. cur_type, &preserve_local,
  3881. error_buf, error_buf_size)))
  3882. return false;
  3883. }
  3884. if (is_32bit_type(cur_type == VALUE_TYPE_I32)) {
  3885. i++;
  3886. }
  3887. else {
  3888. i += 2;
  3889. }
  3890. }
  3891. return true;
  3892. }
  3893. static bool
  3894. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  3895. uint8 *p_code_compiled, char *error_buf,
  3896. uint32 error_buf_size)
  3897. {
  3898. BranchBlockPatch *patch =
  3899. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  3900. if (!patch) {
  3901. return false;
  3902. }
  3903. patch->patch_type = patch_type;
  3904. patch->code_compiled = p_code_compiled;
  3905. if (!frame_csp->patch_list) {
  3906. frame_csp->patch_list = patch;
  3907. patch->next = NULL;
  3908. }
  3909. else {
  3910. patch->next = frame_csp->patch_list;
  3911. frame_csp->patch_list = patch;
  3912. }
  3913. return true;
  3914. }
  3915. static void
  3916. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  3917. {
  3918. BranchBlock *frame_csp = ctx->frame_csp - depth;
  3919. BranchBlockPatch *node = frame_csp->patch_list;
  3920. BranchBlockPatch *node_prev = NULL, *node_next;
  3921. if (!ctx->p_code_compiled)
  3922. return;
  3923. while (node) {
  3924. node_next = node->next;
  3925. if (node->patch_type == patch_type) {
  3926. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  3927. if (node_prev == NULL) {
  3928. frame_csp->patch_list = node_next;
  3929. }
  3930. else {
  3931. node_prev->next = node_next;
  3932. }
  3933. wasm_runtime_free(node);
  3934. }
  3935. else {
  3936. node_prev = node;
  3937. }
  3938. node = node_next;
  3939. }
  3940. }
  3941. static bool
  3942. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  3943. char *error_buf, uint32 error_buf_size)
  3944. {
  3945. /* br info layout:
  3946. * a) arity of target block
  3947. * b) total cell num of arity values
  3948. * c) each arity value's cell num
  3949. * d) each arity value's src frame offset
  3950. * e) each arity values's dst dynamic offset
  3951. * f) branch target address
  3952. *
  3953. * Note: b-e are omitted when arity is 0 so that
  3954. * interpreter can recover the br info quickly.
  3955. */
  3956. BlockType *block_type = &frame_csp->block_type;
  3957. uint8 *types = NULL, cell;
  3958. uint32 arity = 0;
  3959. int32 i;
  3960. int16 *frame_offset = ctx->frame_offset;
  3961. uint16 dynamic_offset;
  3962. /* Note: loop's arity is different from if and block. loop's arity is
  3963. * its parameter count while if and block arity is result count.
  3964. */
  3965. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  3966. arity = block_type_get_param_types(block_type, &types);
  3967. else
  3968. arity = block_type_get_result_types(block_type, &types);
  3969. /* Part a */
  3970. emit_uint32(ctx, arity);
  3971. if (arity) {
  3972. /* Part b */
  3973. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  3974. /* Part c */
  3975. for (i = (int32)arity - 1; i >= 0; i--) {
  3976. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3977. emit_byte(ctx, cell);
  3978. }
  3979. /* Part d */
  3980. for (i = (int32)arity - 1; i >= 0; i--) {
  3981. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3982. frame_offset -= cell;
  3983. emit_operand(ctx, *(int16 *)(frame_offset));
  3984. }
  3985. /* Part e */
  3986. dynamic_offset =
  3987. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  3988. for (i = (int32)arity - 1; i >= 0; i--) {
  3989. cell = (uint8)wasm_value_type_cell_num(types[i]);
  3990. dynamic_offset -= cell;
  3991. emit_operand(ctx, dynamic_offset);
  3992. }
  3993. }
  3994. /* Part f */
  3995. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  3996. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  3997. }
  3998. else {
  3999. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  4000. error_buf, error_buf_size))
  4001. return false;
  4002. /* label address, to be patched */
  4003. wasm_loader_emit_ptr(ctx, NULL);
  4004. }
  4005. return true;
  4006. }
  4007. static bool
  4008. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4009. bool disable_emit, int16 operand_offset,
  4010. char *error_buf, uint32 error_buf_size)
  4011. {
  4012. if (type == VALUE_TYPE_VOID)
  4013. return true;
  4014. /* only check memory overflow in first traverse */
  4015. if (ctx->p_code_compiled == NULL) {
  4016. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4017. return false;
  4018. }
  4019. if (disable_emit)
  4020. *(ctx->frame_offset)++ = operand_offset;
  4021. else {
  4022. emit_operand(ctx, ctx->dynamic_offset);
  4023. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  4024. ctx->dynamic_offset++;
  4025. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4026. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4027. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4028. }
  4029. }
  4030. if (is_32bit_type(type))
  4031. return true;
  4032. if (ctx->p_code_compiled == NULL) {
  4033. if (!check_offset_push(ctx, error_buf, error_buf_size))
  4034. return false;
  4035. }
  4036. ctx->frame_offset++;
  4037. if (!disable_emit) {
  4038. ctx->dynamic_offset++;
  4039. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  4040. ctx->max_dynamic_offset = ctx->dynamic_offset;
  4041. bh_assert(ctx->max_dynamic_offset < INT16_MAX);
  4042. }
  4043. }
  4044. return true;
  4045. }
  4046. /* This function should be in front of wasm_loader_pop_frame_ref
  4047. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  4048. will be modified by wasm_loader_pop_frame_ref */
  4049. static bool
  4050. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  4051. char *error_buf, uint32 error_buf_size)
  4052. {
  4053. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  4054. then current block is the function block */
  4055. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  4056. BranchBlock *cur_block = ctx->frame_csp - depth;
  4057. int32 available_stack_cell =
  4058. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4059. /* Directly return success if current block is in stack
  4060. * polymorphic state while stack is empty. */
  4061. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4062. return true;
  4063. if (type == VALUE_TYPE_VOID)
  4064. return true;
  4065. if (is_32bit_type(type)) {
  4066. /* Check the offset stack bottom to ensure the frame offset
  4067. stack will not go underflow. But we don't thrown error
  4068. and return true here, because the error msg should be
  4069. given in wasm_loader_pop_frame_ref */
  4070. if (!check_offset_pop(ctx, 1))
  4071. return true;
  4072. ctx->frame_offset -= 1;
  4073. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4074. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4075. ctx->dynamic_offset -= 1;
  4076. }
  4077. else {
  4078. if (!check_offset_pop(ctx, 2))
  4079. return true;
  4080. ctx->frame_offset -= 2;
  4081. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  4082. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  4083. ctx->dynamic_offset -= 2;
  4084. }
  4085. emit_operand(ctx, *(ctx->frame_offset));
  4086. return true;
  4087. }
  4088. static bool
  4089. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4090. bool disable_emit, int16 operand_offset,
  4091. char *error_buf, uint32 error_buf_size)
  4092. {
  4093. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  4094. error_buf, error_buf_size)))
  4095. return false;
  4096. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  4097. return false;
  4098. return true;
  4099. }
  4100. static bool
  4101. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  4102. char *error_buf, uint32 error_buf_size)
  4103. {
  4104. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  4105. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  4106. return false;
  4107. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  4108. return false;
  4109. return true;
  4110. }
  4111. static bool
  4112. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  4113. uint8 type_push, uint8 type_pop,
  4114. bool disable_emit, int16 operand_offset,
  4115. char *error_buf, uint32 error_buf_size)
  4116. {
  4117. uint8 i;
  4118. for (i = 0; i < pop_cnt; i++) {
  4119. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  4120. error_buf_size))
  4121. return false;
  4122. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4123. error_buf_size))
  4124. return false;
  4125. }
  4126. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  4127. operand_offset, error_buf,
  4128. error_buf_size))
  4129. return false;
  4130. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4131. return false;
  4132. return true;
  4133. }
  4134. static bool
  4135. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  4136. int16 *offset, char *error_buf,
  4137. uint32 error_buf_size)
  4138. {
  4139. int8 bytes_to_increase;
  4140. int16 operand_offset = 0;
  4141. Const *c;
  4142. /* Search existing constant */
  4143. for (c = (Const *)ctx->const_buf;
  4144. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  4145. if ((type == c->value_type)
  4146. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  4147. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  4148. #if WASM_ENABLE_REF_TYPES != 0
  4149. || (type == VALUE_TYPE_FUNCREF
  4150. && *(int32 *)value == c->value.i32)
  4151. || (type == VALUE_TYPE_EXTERNREF
  4152. && *(int32 *)value == c->value.i32)
  4153. #endif
  4154. || (type == VALUE_TYPE_F64
  4155. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  4156. || (type == VALUE_TYPE_F32
  4157. && (0
  4158. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  4159. operand_offset = c->slot_index;
  4160. break;
  4161. }
  4162. if (c->value_type == VALUE_TYPE_I64 || c->value_type == VALUE_TYPE_F64)
  4163. operand_offset += 2;
  4164. else
  4165. operand_offset += 1;
  4166. }
  4167. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  4168. /* New constant, append to the const buffer */
  4169. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  4170. bytes_to_increase = 2;
  4171. }
  4172. else {
  4173. bytes_to_increase = 1;
  4174. }
  4175. /* The max cell num of const buffer is 32768 since the valid index range
  4176. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  4177. * full */
  4178. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  4179. *offset = 0;
  4180. return true;
  4181. }
  4182. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  4183. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  4184. ctx->const_buf_size + 4 * sizeof(Const));
  4185. ctx->const_buf_size += 4 * sizeof(Const);
  4186. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  4187. }
  4188. c->value_type = type;
  4189. switch (type) {
  4190. case VALUE_TYPE_F64:
  4191. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  4192. sizeof(float64));
  4193. ctx->const_cell_num += 2;
  4194. /* The const buf will be reversed, we use the second cell */
  4195. /* of the i64/f64 const so the finnal offset is corrent */
  4196. operand_offset++;
  4197. break;
  4198. case VALUE_TYPE_I64:
  4199. c->value.i64 = *(int64 *)value;
  4200. ctx->const_cell_num += 2;
  4201. operand_offset++;
  4202. break;
  4203. case VALUE_TYPE_F32:
  4204. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  4205. sizeof(float32));
  4206. ctx->const_cell_num++;
  4207. break;
  4208. case VALUE_TYPE_I32:
  4209. c->value.i32 = *(int32 *)value;
  4210. ctx->const_cell_num++;
  4211. break;
  4212. #if WASM_ENABLE_REF_TYPES != 0
  4213. case VALUE_TYPE_EXTERNREF:
  4214. case VALUE_TYPE_FUNCREF:
  4215. c->value.i32 = *(int32 *)value;
  4216. ctx->const_cell_num++;
  4217. break;
  4218. #endif
  4219. default:
  4220. break;
  4221. }
  4222. c->slot_index = operand_offset;
  4223. ctx->num_const++;
  4224. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  4225. (int64)c->value.i64);
  4226. }
  4227. /* use negetive index for const */
  4228. operand_offset = -(operand_offset + 1);
  4229. *offset = operand_offset;
  4230. return true;
  4231. fail:
  4232. return false;
  4233. }
  4234. /*
  4235. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  4236. -- Mostly used for the binary / compare operation
  4237. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  4238. -- Mostly used in block / control instructions
  4239. The POP will always emit the offset on the top of the frame_offset stack
  4240. PUSH can be used in two ways:
  4241. 1. directly PUSH:
  4242. PUSH_XXX();
  4243. will allocate a dynamic space and emit
  4244. 2. silent PUSH:
  4245. operand_offset = xxx; disable_emit = true;
  4246. PUSH_XXX();
  4247. only push the frame_offset stack, no emit
  4248. */
  4249. #define PUSH_I32() \
  4250. do { \
  4251. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4252. disable_emit, operand_offset, \
  4253. error_buf, error_buf_size)) \
  4254. goto fail; \
  4255. } while (0)
  4256. #define PUSH_F32() \
  4257. do { \
  4258. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4259. disable_emit, operand_offset, \
  4260. error_buf, error_buf_size)) \
  4261. goto fail; \
  4262. } while (0)
  4263. #define PUSH_I64() \
  4264. do { \
  4265. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4266. disable_emit, operand_offset, \
  4267. error_buf, error_buf_size)) \
  4268. goto fail; \
  4269. } while (0)
  4270. #define PUSH_F64() \
  4271. do { \
  4272. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4273. disable_emit, operand_offset, \
  4274. error_buf, error_buf_size)) \
  4275. goto fail; \
  4276. } while (0)
  4277. #define PUSH_FUNCREF() \
  4278. do { \
  4279. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_FUNCREF, \
  4280. disable_emit, operand_offset, \
  4281. error_buf, error_buf_size)) \
  4282. goto fail; \
  4283. } while (0)
  4284. #define POP_I32() \
  4285. do { \
  4286. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  4287. error_buf, error_buf_size)) \
  4288. goto fail; \
  4289. } while (0)
  4290. #define POP_F32() \
  4291. do { \
  4292. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  4293. error_buf, error_buf_size)) \
  4294. goto fail; \
  4295. } while (0)
  4296. #define POP_I64() \
  4297. do { \
  4298. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  4299. error_buf, error_buf_size)) \
  4300. goto fail; \
  4301. } while (0)
  4302. #define POP_F64() \
  4303. do { \
  4304. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  4305. error_buf, error_buf_size)) \
  4306. goto fail; \
  4307. } while (0)
  4308. #define PUSH_OFFSET_TYPE(type) \
  4309. do { \
  4310. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  4311. operand_offset, error_buf, \
  4312. error_buf_size))) \
  4313. goto fail; \
  4314. } while (0)
  4315. #define POP_OFFSET_TYPE(type) \
  4316. do { \
  4317. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  4318. error_buf_size))) \
  4319. goto fail; \
  4320. } while (0)
  4321. #define POP_AND_PUSH(type_pop, type_push) \
  4322. do { \
  4323. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4324. loader_ctx, 1, type_push, type_pop, disable_emit, \
  4325. operand_offset, error_buf, error_buf_size))) \
  4326. goto fail; \
  4327. } while (0)
  4328. /* type of POPs should be the same */
  4329. #define POP2_AND_PUSH(type_pop, type_push) \
  4330. do { \
  4331. if (!(wasm_loader_push_pop_frame_ref_offset( \
  4332. loader_ctx, 2, type_push, type_pop, disable_emit, \
  4333. operand_offset, error_buf, error_buf_size))) \
  4334. goto fail; \
  4335. } while (0)
  4336. #else /* WASM_ENABLE_FAST_INTERP */
  4337. #define PUSH_I32() \
  4338. do { \
  4339. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  4340. error_buf, error_buf_size))) \
  4341. goto fail; \
  4342. } while (0)
  4343. #define PUSH_F32() \
  4344. do { \
  4345. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  4346. error_buf, error_buf_size))) \
  4347. goto fail; \
  4348. } while (0)
  4349. #define PUSH_I64() \
  4350. do { \
  4351. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  4352. error_buf, error_buf_size))) \
  4353. goto fail; \
  4354. } while (0)
  4355. #define PUSH_F64() \
  4356. do { \
  4357. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  4358. error_buf, error_buf_size))) \
  4359. goto fail; \
  4360. } while (0)
  4361. #define PUSH_FUNCREF() \
  4362. do { \
  4363. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4364. error_buf, error_buf_size))) \
  4365. goto fail; \
  4366. } while (0)
  4367. #define POP_I32() \
  4368. do { \
  4369. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, error_buf, \
  4370. error_buf_size))) \
  4371. goto fail; \
  4372. } while (0)
  4373. #define POP_F32() \
  4374. do { \
  4375. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, error_buf, \
  4376. error_buf_size))) \
  4377. goto fail; \
  4378. } while (0)
  4379. #define POP_I64() \
  4380. do { \
  4381. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, error_buf, \
  4382. error_buf_size))) \
  4383. goto fail; \
  4384. } while (0)
  4385. #define POP_F64() \
  4386. do { \
  4387. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, error_buf, \
  4388. error_buf_size))) \
  4389. goto fail; \
  4390. } while (0)
  4391. #define POP_FUNCREF() \
  4392. do { \
  4393. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF, \
  4394. error_buf, error_buf_size))) \
  4395. goto fail; \
  4396. } while (0)
  4397. #define POP_AND_PUSH(type_pop, type_push) \
  4398. do { \
  4399. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  4400. type_pop, error_buf, \
  4401. error_buf_size))) \
  4402. goto fail; \
  4403. } while (0)
  4404. /* type of POPs should be the same */
  4405. #define POP2_AND_PUSH(type_pop, type_push) \
  4406. do { \
  4407. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  4408. type_pop, error_buf, \
  4409. error_buf_size))) \
  4410. goto fail; \
  4411. } while (0)
  4412. #endif /* WASM_ENABLE_FAST_INTERP */
  4413. #if WASM_ENABLE_FAST_INTERP != 0
  4414. static bool
  4415. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  4416. bool disable_emit, char *error_buf, uint32 error_buf_size)
  4417. {
  4418. int16 operand_offset = 0;
  4419. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  4420. : loader_ctx->frame_csp;
  4421. BlockType *block_type = &block->block_type;
  4422. uint8 *return_types = NULL;
  4423. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  4424. int32 i = 0;
  4425. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  4426. *frame_offset_org = NULL;
  4427. return_count = block_type_get_result_types(block_type, &return_types);
  4428. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  4429. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  4430. if (return_count == 1) {
  4431. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  4432. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  4433. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  4434. /* insert op_copy before else opcode */
  4435. if (opcode == WASM_OP_ELSE)
  4436. skip_label();
  4437. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  4438. : EXT_OP_COPY_STACK_TOP_I64);
  4439. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  4440. emit_operand(loader_ctx, block->dynamic_offset);
  4441. if (opcode == WASM_OP_ELSE) {
  4442. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  4443. }
  4444. else {
  4445. loader_ctx->frame_offset -= cell;
  4446. loader_ctx->dynamic_offset = block->dynamic_offset;
  4447. PUSH_OFFSET_TYPE(return_types[0]);
  4448. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4449. }
  4450. if (opcode == WASM_OP_ELSE)
  4451. emit_label(opcode);
  4452. }
  4453. return true;
  4454. }
  4455. /* Copy stack top values to block's results which are in dynamic space.
  4456. * The instruction format:
  4457. * Part a: values count
  4458. * Part b: all values total cell num
  4459. * Part c: each value's cell_num, src offset and dst offset
  4460. * Part d: each value's src offset and dst offset
  4461. * Part e: each value's dst offset
  4462. */
  4463. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  4464. dynamic_offset = dynamic_offset_org =
  4465. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  4466. /* First traversal to get the count of values needed to be copied. */
  4467. for (i = (int32)return_count - 1; i >= 0; i--) {
  4468. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  4469. frame_offset -= cells;
  4470. dynamic_offset -= cells;
  4471. if (dynamic_offset != *frame_offset) {
  4472. value_count++;
  4473. total_cel_num += cells;
  4474. }
  4475. }
  4476. if (value_count) {
  4477. uint32 j = 0;
  4478. uint8 *emit_data = NULL, *cells = NULL;
  4479. int16 *src_offsets = NULL;
  4480. uint16 *dst_offsets = NULL;
  4481. uint64 size =
  4482. (uint64)value_count
  4483. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  4484. /* Allocate memory for the emit data */
  4485. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4486. return false;
  4487. cells = emit_data;
  4488. src_offsets = (int16 *)(cells + value_count);
  4489. dst_offsets = (uint16 *)(src_offsets + value_count);
  4490. /* insert op_copy before else opcode */
  4491. if (opcode == WASM_OP_ELSE)
  4492. skip_label();
  4493. emit_label(EXT_OP_COPY_STACK_VALUES);
  4494. /* Part a) */
  4495. emit_uint32(loader_ctx, value_count);
  4496. /* Part b) */
  4497. emit_uint32(loader_ctx, total_cel_num);
  4498. /* Second traversal to get each value's cell num, src offset and dst
  4499. * offset. */
  4500. frame_offset = frame_offset_org;
  4501. dynamic_offset = dynamic_offset_org;
  4502. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  4503. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  4504. frame_offset -= cell;
  4505. dynamic_offset -= cell;
  4506. if (dynamic_offset != *frame_offset) {
  4507. /* cell num */
  4508. cells[j] = cell;
  4509. /* src offset */
  4510. src_offsets[j] = *frame_offset;
  4511. /* dst offset */
  4512. dst_offsets[j] = dynamic_offset;
  4513. j++;
  4514. }
  4515. if (opcode == WASM_OP_ELSE) {
  4516. *frame_offset = dynamic_offset;
  4517. }
  4518. else {
  4519. loader_ctx->frame_offset = frame_offset;
  4520. loader_ctx->dynamic_offset = dynamic_offset;
  4521. PUSH_OFFSET_TYPE(return_types[i]);
  4522. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4523. loader_ctx->frame_offset = frame_offset_org;
  4524. loader_ctx->dynamic_offset = dynamic_offset_org;
  4525. }
  4526. }
  4527. bh_assert(j == value_count);
  4528. /* Emit the cells, src_offsets and dst_offsets */
  4529. for (j = 0; j < value_count; j++)
  4530. emit_byte(loader_ctx, cells[j]);
  4531. for (j = 0; j < value_count; j++)
  4532. emit_operand(loader_ctx, src_offsets[j]);
  4533. for (j = 0; j < value_count; j++)
  4534. emit_operand(loader_ctx, dst_offsets[j]);
  4535. if (opcode == WASM_OP_ELSE)
  4536. emit_label(opcode);
  4537. wasm_runtime_free(emit_data);
  4538. }
  4539. return true;
  4540. fail:
  4541. return false;
  4542. }
  4543. #endif /* WASM_ENABLE_FAST_INTERP */
  4544. #define RESERVE_BLOCK_RET() \
  4545. do { \
  4546. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  4547. error_buf_size)) \
  4548. goto fail; \
  4549. } while (0)
  4550. #define PUSH_TYPE(type) \
  4551. do { \
  4552. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  4553. error_buf_size))) \
  4554. goto fail; \
  4555. } while (0)
  4556. #define POP_TYPE(type) \
  4557. do { \
  4558. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  4559. error_buf_size))) \
  4560. goto fail; \
  4561. } while (0)
  4562. #define PUSH_CSP(label_type, block_type, _start_addr) \
  4563. do { \
  4564. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  4565. _start_addr, error_buf, \
  4566. error_buf_size)) \
  4567. goto fail; \
  4568. } while (0)
  4569. #define POP_CSP() \
  4570. do { \
  4571. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  4572. goto fail; \
  4573. } while (0)
  4574. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  4575. do { \
  4576. read_leb_uint32(p, p_end, local_idx); \
  4577. bh_assert(local_idx < param_count + local_count); \
  4578. local_type = local_idx < param_count \
  4579. ? param_types[local_idx] \
  4580. : local_types[local_idx - param_count]; \
  4581. local_offset = local_offsets[local_idx]; \
  4582. } while (0)
  4583. #define CHECK_BR(depth) \
  4584. do { \
  4585. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  4586. error_buf_size)) \
  4587. goto fail; \
  4588. } while (0)
  4589. #define CHECK_MEMORY() \
  4590. do { \
  4591. bh_assert(module->import_memory_count + module->memory_count > 0); \
  4592. } while (0)
  4593. static bool
  4594. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  4595. char *error_buf, uint32 error_buf_size)
  4596. {
  4597. BranchBlock *target_block, *cur_block;
  4598. BlockType *target_block_type;
  4599. uint8 *types = NULL, *frame_ref;
  4600. uint32 arity = 0;
  4601. int32 i, available_stack_cell;
  4602. uint16 cell_num;
  4603. if (loader_ctx->csp_num < depth + 1) {
  4604. set_error_buf(error_buf, error_buf_size,
  4605. "unknown label, "
  4606. "unexpected end of section or function");
  4607. return false;
  4608. }
  4609. cur_block = loader_ctx->frame_csp - 1;
  4610. target_block = loader_ctx->frame_csp - (depth + 1);
  4611. target_block_type = &target_block->block_type;
  4612. frame_ref = loader_ctx->frame_ref;
  4613. /* Note: loop's arity is different from if and block. loop's arity is
  4614. * its parameter count while if and block arity is result count.
  4615. */
  4616. if (target_block->label_type == LABEL_TYPE_LOOP)
  4617. arity = block_type_get_param_types(target_block_type, &types);
  4618. else
  4619. arity = block_type_get_result_types(target_block_type, &types);
  4620. /* If the stack is in polymorphic state, just clear the stack
  4621. * and then re-push the values to make the stack top values
  4622. * match block type. */
  4623. if (cur_block->is_stack_polymorphic) {
  4624. for (i = (int32)arity - 1; i >= 0; i--) {
  4625. #if WASM_ENABLE_FAST_INTERP != 0
  4626. POP_OFFSET_TYPE(types[i]);
  4627. #endif
  4628. POP_TYPE(types[i]);
  4629. }
  4630. for (i = 0; i < (int32)arity; i++) {
  4631. #if WASM_ENABLE_FAST_INTERP != 0
  4632. bool disable_emit = true;
  4633. int16 operand_offset = 0;
  4634. PUSH_OFFSET_TYPE(types[i]);
  4635. #endif
  4636. PUSH_TYPE(types[i]);
  4637. }
  4638. return true;
  4639. }
  4640. available_stack_cell =
  4641. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  4642. /* Check stack top values match target block type */
  4643. for (i = (int32)arity - 1; i >= 0; i--) {
  4644. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  4645. error_buf, error_buf_size))
  4646. return false;
  4647. cell_num = wasm_value_type_cell_num(types[i]);
  4648. frame_ref -= cell_num;
  4649. available_stack_cell -= cell_num;
  4650. }
  4651. return true;
  4652. fail:
  4653. return false;
  4654. }
  4655. static BranchBlock *
  4656. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  4657. char *error_buf, uint32 error_buf_size)
  4658. {
  4659. uint8 *p = *p_buf, *p_end = buf_end;
  4660. BranchBlock *frame_csp_tmp;
  4661. uint32 depth;
  4662. read_leb_uint32(p, p_end, depth);
  4663. CHECK_BR(depth);
  4664. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  4665. #if WASM_ENABLE_FAST_INTERP != 0
  4666. emit_br_info(frame_csp_tmp);
  4667. #endif
  4668. *p_buf = p;
  4669. return frame_csp_tmp;
  4670. fail:
  4671. return NULL;
  4672. }
  4673. static bool
  4674. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  4675. char *error_buf, uint32 error_buf_size)
  4676. {
  4677. BlockType *block_type = &block->block_type;
  4678. uint8 *return_types = NULL;
  4679. uint32 return_count = 0;
  4680. int32 available_stack_cell, return_cell_num, i;
  4681. uint8 *frame_ref = NULL;
  4682. available_stack_cell =
  4683. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  4684. return_count = block_type_get_result_types(block_type, &return_types);
  4685. return_cell_num =
  4686. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  4687. /* If the stack is in polymorphic state, just clear the stack
  4688. * and then re-push the values to make the stack top values
  4689. * match block type. */
  4690. if (block->is_stack_polymorphic) {
  4691. for (i = (int32)return_count - 1; i >= 0; i--) {
  4692. #if WASM_ENABLE_FAST_INTERP != 0
  4693. POP_OFFSET_TYPE(return_types[i]);
  4694. #endif
  4695. POP_TYPE(return_types[i]);
  4696. }
  4697. /* Check stack is empty */
  4698. bh_assert(loader_ctx->stack_cell_num == block->stack_cell_num);
  4699. for (i = 0; i < (int32)return_count; i++) {
  4700. #if WASM_ENABLE_FAST_INTERP != 0
  4701. bool disable_emit = true;
  4702. int16 operand_offset = 0;
  4703. PUSH_OFFSET_TYPE(return_types[i]);
  4704. #endif
  4705. PUSH_TYPE(return_types[i]);
  4706. }
  4707. return true;
  4708. }
  4709. /* Check stack cell num equals return cell num */
  4710. bh_assert(available_stack_cell == return_cell_num);
  4711. /* Check stack values match return types */
  4712. frame_ref = loader_ctx->frame_ref;
  4713. for (i = (int32)return_count - 1; i >= 0; i--) {
  4714. if (!check_stack_top_values(frame_ref, available_stack_cell,
  4715. return_types[i], error_buf, error_buf_size))
  4716. return false;
  4717. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  4718. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  4719. }
  4720. (void)return_cell_num;
  4721. return true;
  4722. fail:
  4723. return false;
  4724. }
  4725. #if WASM_ENABLE_FAST_INTERP != 0
  4726. /* Copy parameters to dynamic space.
  4727. * 1) POP original parameter out;
  4728. * 2) Push and copy original values to dynamic space.
  4729. * The copy instruction format:
  4730. * Part a: param count
  4731. * Part b: all param total cell num
  4732. * Part c: each param's cell_num, src offset and dst offset
  4733. * Part d: each param's src offset
  4734. * Part e: each param's dst offset
  4735. */
  4736. static bool
  4737. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  4738. char *error_buf, uint32 error_buf_size)
  4739. {
  4740. int16 *frame_offset = NULL;
  4741. uint8 *cells = NULL, cell;
  4742. int16 *src_offsets = NULL;
  4743. uint8 *emit_data = NULL;
  4744. uint32 i;
  4745. BranchBlock *block = loader_ctx->frame_csp - 1;
  4746. BlockType *block_type = &block->block_type;
  4747. WASMFuncType *wasm_type = block_type->u.type;
  4748. uint32 param_count = block_type->u.type->param_count;
  4749. int16 condition_offset = 0;
  4750. bool disable_emit = false;
  4751. int16 operand_offset = 0;
  4752. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  4753. /* For if block, we also need copy the condition operand offset. */
  4754. if (is_if_block)
  4755. size += sizeof(*cells) + sizeof(*src_offsets);
  4756. /* Allocate memory for the emit data */
  4757. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  4758. return false;
  4759. cells = emit_data;
  4760. src_offsets = (int16 *)(cells + param_count);
  4761. if (is_if_block)
  4762. condition_offset = *loader_ctx->frame_offset;
  4763. /* POP original parameter out */
  4764. for (i = 0; i < param_count; i++) {
  4765. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  4766. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  4767. }
  4768. frame_offset = loader_ctx->frame_offset;
  4769. /* Get each param's cell num and src offset */
  4770. for (i = 0; i < param_count; i++) {
  4771. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  4772. cells[i] = cell;
  4773. src_offsets[i] = *frame_offset;
  4774. frame_offset += cell;
  4775. }
  4776. /* emit copy instruction */
  4777. emit_label(EXT_OP_COPY_STACK_VALUES);
  4778. /* Part a) */
  4779. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  4780. /* Part b) */
  4781. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  4782. : wasm_type->param_cell_num);
  4783. /* Part c) */
  4784. for (i = 0; i < param_count; i++)
  4785. emit_byte(loader_ctx, cells[i]);
  4786. if (is_if_block)
  4787. emit_byte(loader_ctx, 1);
  4788. /* Part d) */
  4789. for (i = 0; i < param_count; i++)
  4790. emit_operand(loader_ctx, src_offsets[i]);
  4791. if (is_if_block)
  4792. emit_operand(loader_ctx, condition_offset);
  4793. /* Part e) */
  4794. /* Push to dynamic space. The push will emit the dst offset. */
  4795. for (i = 0; i < param_count; i++)
  4796. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  4797. if (is_if_block)
  4798. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  4799. /* Free the emit data */
  4800. wasm_runtime_free(emit_data);
  4801. return true;
  4802. fail:
  4803. /* Free the emit data */
  4804. wasm_runtime_free(emit_data);
  4805. return false;
  4806. }
  4807. #endif
  4808. /* reset the stack to the state of before entering the last block */
  4809. #if WASM_ENABLE_FAST_INTERP != 0
  4810. #define RESET_STACK() \
  4811. do { \
  4812. loader_ctx->stack_cell_num = \
  4813. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4814. loader_ctx->frame_ref = \
  4815. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4816. loader_ctx->frame_offset = \
  4817. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  4818. } while (0)
  4819. #else
  4820. #define RESET_STACK() \
  4821. do { \
  4822. loader_ctx->stack_cell_num = \
  4823. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  4824. loader_ctx->frame_ref = \
  4825. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  4826. } while (0)
  4827. #endif
  4828. /* set current block's stack polymorphic state */
  4829. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  4830. do { \
  4831. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  4832. _cur_block->is_stack_polymorphic = flag; \
  4833. } while (0)
  4834. #define BLOCK_HAS_PARAM(block_type) \
  4835. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  4836. #define PRESERVE_LOCAL_FOR_BLOCK() \
  4837. do { \
  4838. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  4839. error_buf_size))) { \
  4840. goto fail; \
  4841. } \
  4842. } while (0)
  4843. static bool
  4844. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  4845. uint32 cur_func_idx, char *error_buf,
  4846. uint32 error_buf_size)
  4847. {
  4848. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  4849. uint32 param_count, local_count, global_count;
  4850. uint8 *param_types, *local_types, local_type, global_type;
  4851. BlockType func_block_type;
  4852. uint16 *local_offsets, local_offset;
  4853. uint32 count, local_idx, global_idx, u32, align, mem_offset, i;
  4854. int32 i32, i32_const = 0;
  4855. int64 i64_const;
  4856. uint8 opcode, u8;
  4857. bool return_value = false;
  4858. WASMLoaderContext *loader_ctx;
  4859. BranchBlock *frame_csp_tmp;
  4860. #if WASM_ENABLE_BULK_MEMORY != 0
  4861. uint32 segment_index;
  4862. #endif
  4863. #if WASM_ENABLE_FAST_INTERP != 0
  4864. uint8 *func_const_end, *func_const = NULL;
  4865. int16 operand_offset = 0;
  4866. uint8 last_op = 0;
  4867. bool disable_emit, preserve_local = false;
  4868. float32 f32_const;
  4869. float64 f64_const;
  4870. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  4871. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  4872. #endif
  4873. global_count = module->import_global_count + module->global_count;
  4874. param_count = func->func_type->param_count;
  4875. param_types = func->func_type->types;
  4876. func_block_type.is_value_type = false;
  4877. func_block_type.u.type = func->func_type;
  4878. local_count = func->local_count;
  4879. local_types = func->local_types;
  4880. local_offsets = func->local_offsets;
  4881. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  4882. goto fail;
  4883. }
  4884. #if WASM_ENABLE_FAST_INTERP != 0
  4885. /* For the first traverse, the initial value of preserved_local_offset has
  4886. * not been determined, we use the INT16_MAX to represent that a slot has
  4887. * been copied to preserve space. For second traverse, this field will be
  4888. * set to the appropriate value in wasm_loader_ctx_reinit.
  4889. * This is for Issue #1230,
  4890. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  4891. * drop opcodes need to know which slots are preserved, so those slots will
  4892. * not be treated as dynamically allocated slots */
  4893. loader_ctx->preserved_local_offset = INT16_MAX;
  4894. re_scan:
  4895. if (loader_ctx->code_compiled_size > 0) {
  4896. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  4897. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  4898. goto fail;
  4899. }
  4900. p = func->code;
  4901. func->code_compiled = loader_ctx->p_code_compiled;
  4902. func->code_compiled_size = loader_ctx->code_compiled_size;
  4903. }
  4904. #endif
  4905. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  4906. while (p < p_end) {
  4907. opcode = *p++;
  4908. #if WASM_ENABLE_FAST_INTERP != 0
  4909. p_org = p;
  4910. disable_emit = false;
  4911. emit_label(opcode);
  4912. #endif
  4913. switch (opcode) {
  4914. case WASM_OP_UNREACHABLE:
  4915. RESET_STACK();
  4916. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  4917. break;
  4918. case WASM_OP_NOP:
  4919. #if WASM_ENABLE_FAST_INTERP != 0
  4920. skip_label();
  4921. #endif
  4922. break;
  4923. case WASM_OP_IF:
  4924. #if WASM_ENABLE_FAST_INTERP != 0
  4925. PRESERVE_LOCAL_FOR_BLOCK();
  4926. #endif
  4927. POP_I32();
  4928. goto handle_op_block_and_loop;
  4929. case WASM_OP_BLOCK:
  4930. case WASM_OP_LOOP:
  4931. #if WASM_ENABLE_FAST_INTERP != 0
  4932. PRESERVE_LOCAL_FOR_BLOCK();
  4933. #endif
  4934. handle_op_block_and_loop:
  4935. {
  4936. uint8 value_type;
  4937. BlockType block_type;
  4938. p_org = p - 1;
  4939. value_type = read_uint8(p);
  4940. if (is_byte_a_type(value_type)) {
  4941. /* If the first byte is one of these special values:
  4942. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  4943. * the single return value. */
  4944. block_type.is_value_type = true;
  4945. block_type.u.value_type.type = value_type;
  4946. }
  4947. else {
  4948. uint32 type_index;
  4949. /* Resolve the leb128 encoded type index as block type */
  4950. p--;
  4951. read_leb_uint32(p, p_end, type_index);
  4952. bh_assert(type_index < module->type_count);
  4953. block_type.is_value_type = false;
  4954. block_type.u.type = module->types[type_index];
  4955. #if WASM_ENABLE_FAST_INTERP == 0
  4956. /* If block use type index as block type, change the opcode
  4957. * to new extended opcode so that interpreter can resolve
  4958. * the block quickly.
  4959. */
  4960. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  4961. #endif
  4962. }
  4963. /* Pop block parameters from stack */
  4964. if (BLOCK_HAS_PARAM(block_type)) {
  4965. WASMFuncType *wasm_type = block_type.u.type;
  4966. for (i = 0; i < block_type.u.type->param_count; i++)
  4967. POP_TYPE(
  4968. wasm_type->types[wasm_type->param_count - i - 1]);
  4969. }
  4970. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  4971. block_type, p);
  4972. /* Pass parameters to block */
  4973. if (BLOCK_HAS_PARAM(block_type)) {
  4974. for (i = 0; i < block_type.u.type->param_count; i++)
  4975. PUSH_TYPE(block_type.u.type->types[i]);
  4976. }
  4977. #if WASM_ENABLE_FAST_INTERP != 0
  4978. if (opcode == WASM_OP_BLOCK) {
  4979. skip_label();
  4980. }
  4981. else if (opcode == WASM_OP_LOOP) {
  4982. skip_label();
  4983. if (BLOCK_HAS_PARAM(block_type)) {
  4984. /* Make sure params are in dynamic space */
  4985. if (!copy_params_to_dynamic_space(
  4986. loader_ctx, false, error_buf, error_buf_size))
  4987. goto fail;
  4988. }
  4989. (loader_ctx->frame_csp - 1)->code_compiled =
  4990. loader_ctx->p_code_compiled;
  4991. }
  4992. else if (opcode == WASM_OP_IF) {
  4993. /* If block has parameters, we should make sure they are in
  4994. * dynamic space. Otherwise, when else branch is missing,
  4995. * the later opcode may consume incorrect operand offset.
  4996. * Spec case:
  4997. * (func (export "params-id") (param i32) (result i32)
  4998. * (i32.const 1)
  4999. * (i32.const 2)
  5000. * (if (param i32 i32) (result i32 i32) (local.get 0)
  5001. * (then)) (i32.add)
  5002. * )
  5003. *
  5004. * So we should emit a copy instruction before the if.
  5005. *
  5006. * And we also need to save the parameter offsets and
  5007. * recover them before entering else branch.
  5008. *
  5009. */
  5010. if (BLOCK_HAS_PARAM(block_type)) {
  5011. BranchBlock *block = loader_ctx->frame_csp - 1;
  5012. uint64 size;
  5013. /* skip the if condition operand offset */
  5014. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5015. /* skip the if label */
  5016. skip_label();
  5017. /* Emit a copy instruction */
  5018. if (!copy_params_to_dynamic_space(
  5019. loader_ctx, true, error_buf, error_buf_size))
  5020. goto fail;
  5021. /* Emit the if instruction */
  5022. emit_label(opcode);
  5023. /* Emit the new condition operand offset */
  5024. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  5025. /* Save top param_count values of frame_offset stack, so
  5026. * that we can recover it before executing else branch
  5027. */
  5028. size = sizeof(int16)
  5029. * (uint64)block_type.u.type->param_cell_num;
  5030. if (!(block->param_frame_offsets = loader_malloc(
  5031. size, error_buf, error_buf_size)))
  5032. goto fail;
  5033. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  5034. loader_ctx->frame_offset
  5035. - size / sizeof(int16),
  5036. (uint32)size);
  5037. }
  5038. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  5039. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5040. }
  5041. #endif
  5042. break;
  5043. }
  5044. case WASM_OP_ELSE:
  5045. {
  5046. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  5047. bh_assert(loader_ctx->csp_num >= 2
  5048. && (loader_ctx->frame_csp - 1)->label_type
  5049. == LABEL_TYPE_IF);
  5050. /* check whether if branch's stack matches its result type */
  5051. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  5052. error_buf, error_buf_size))
  5053. goto fail;
  5054. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  5055. #if WASM_ENABLE_FAST_INTERP != 0
  5056. /* if the result of if branch is in local or const area, add a
  5057. * copy op */
  5058. RESERVE_BLOCK_RET();
  5059. emit_empty_label_addr_and_frame_ip(PATCH_END);
  5060. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  5061. #endif
  5062. RESET_STACK();
  5063. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  5064. /* Pass parameters to if-false branch */
  5065. if (BLOCK_HAS_PARAM(block_type)) {
  5066. for (i = 0; i < block_type.u.type->param_count; i++)
  5067. PUSH_TYPE(block_type.u.type->types[i]);
  5068. }
  5069. #if WASM_ENABLE_FAST_INTERP != 0
  5070. /* Recover top param_count values of frame_offset stack */
  5071. if (BLOCK_HAS_PARAM((block_type))) {
  5072. uint32 size;
  5073. BranchBlock *block = loader_ctx->frame_csp - 1;
  5074. size = sizeof(int16) * block_type.u.type->param_cell_num;
  5075. bh_memcpy_s(loader_ctx->frame_offset, size,
  5076. block->param_frame_offsets, size);
  5077. loader_ctx->frame_offset += (size / sizeof(int16));
  5078. }
  5079. #endif
  5080. break;
  5081. }
  5082. case WASM_OP_END:
  5083. {
  5084. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5085. /* check whether block stack matches its result type */
  5086. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  5087. error_buf_size))
  5088. goto fail;
  5089. /* if no else branch, and return types do not match param types,
  5090. * fail */
  5091. if (cur_block->label_type == LABEL_TYPE_IF
  5092. && !cur_block->else_addr) {
  5093. uint32 block_param_count = 0, block_ret_count = 0;
  5094. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  5095. BlockType *cur_block_type = &cur_block->block_type;
  5096. #if WASM_ENABLE_GC != 0
  5097. uint32 block_param_reftype_map_count;
  5098. uint32 block_ret_reftype_map_count;
  5099. WASMRefTypeMap *block_param_reftype_maps;
  5100. WASMRefTypeMap *block_ret_reftype_maps;
  5101. #endif
  5102. block_param_count = block_type_get_param_types(
  5103. cur_block_type, &block_param_types
  5104. #if WASM_ENABLE_GC != 0
  5105. ,
  5106. &block_param_reftype_maps,
  5107. &block_param_reftype_map_count
  5108. #endif
  5109. );
  5110. block_ret_count = block_type_get_result_types(
  5111. cur_block_type, &block_ret_types
  5112. #if WASM_ENABLE_GC != 0
  5113. ,
  5114. &block_ret_reftype_maps, &block_ret_reftype_map_count
  5115. #endif
  5116. );
  5117. bh_assert(block_param_count == block_ret_count
  5118. && (!block_param_count
  5119. || !memcmp(block_param_types, block_ret_types,
  5120. block_param_count)));
  5121. (void)block_ret_types;
  5122. (void)block_ret_count;
  5123. (void)block_param_types;
  5124. }
  5125. POP_CSP();
  5126. #if WASM_ENABLE_FAST_INTERP != 0
  5127. skip_label();
  5128. /* copy the result to the block return address */
  5129. RESERVE_BLOCK_RET();
  5130. apply_label_patch(loader_ctx, 0, PATCH_END);
  5131. free_label_patch_list(loader_ctx->frame_csp);
  5132. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  5133. int32 idx;
  5134. uint8 ret_type;
  5135. emit_label(WASM_OP_RETURN);
  5136. for (idx = (int32)func->func_type->result_count - 1;
  5137. idx >= 0; idx--) {
  5138. ret_type = *(func->func_type->types
  5139. + func->func_type->param_count + idx);
  5140. POP_OFFSET_TYPE(ret_type);
  5141. }
  5142. }
  5143. #endif
  5144. if (loader_ctx->csp_num > 0) {
  5145. loader_ctx->frame_csp->end_addr = p - 1;
  5146. }
  5147. else {
  5148. /* end of function block, function will return */
  5149. bh_assert(p == p_end);
  5150. }
  5151. break;
  5152. }
  5153. case WASM_OP_BR:
  5154. {
  5155. if (!(frame_csp_tmp = check_branch_block(
  5156. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5157. goto fail;
  5158. RESET_STACK();
  5159. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5160. break;
  5161. }
  5162. case WASM_OP_BR_IF:
  5163. {
  5164. POP_I32();
  5165. if (!(frame_csp_tmp = check_branch_block(
  5166. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  5167. goto fail;
  5168. break;
  5169. }
  5170. case WASM_OP_BR_TABLE:
  5171. {
  5172. uint8 *ret_types = NULL;
  5173. uint32 ret_count = 0;
  5174. #if WASM_ENABLE_FAST_INTERP == 0
  5175. uint8 *p_depth_begin, *p_depth;
  5176. uint32 depth, j;
  5177. BrTableCache *br_table_cache = NULL;
  5178. p_org = p - 1;
  5179. #endif
  5180. read_leb_uint32(p, p_end, count);
  5181. #if WASM_ENABLE_FAST_INTERP != 0
  5182. emit_uint32(loader_ctx, count);
  5183. #endif
  5184. POP_I32();
  5185. #if WASM_ENABLE_FAST_INTERP == 0
  5186. p_depth_begin = p_depth = p;
  5187. #endif
  5188. for (i = 0; i <= count; i++) {
  5189. if (!(frame_csp_tmp =
  5190. check_branch_block(loader_ctx, &p, p_end,
  5191. error_buf, error_buf_size)))
  5192. goto fail;
  5193. #if WASM_ENABLE_FAST_INTERP == 0
  5194. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  5195. if (br_table_cache) {
  5196. br_table_cache->br_depths[i] = depth;
  5197. }
  5198. else {
  5199. if (depth > 255) {
  5200. /* The depth cannot be stored in one byte,
  5201. create br_table cache to store each depth */
  5202. if (!(br_table_cache = loader_malloc(
  5203. offsetof(BrTableCache, br_depths)
  5204. + sizeof(uint32)
  5205. * (uint64)(count + 1),
  5206. error_buf, error_buf_size))) {
  5207. goto fail;
  5208. }
  5209. *p_org = EXT_OP_BR_TABLE_CACHE;
  5210. br_table_cache->br_table_op_addr = p_org;
  5211. br_table_cache->br_count = count;
  5212. /* Copy previous depths which are one byte */
  5213. for (j = 0; j < i; j++) {
  5214. br_table_cache->br_depths[j] = p_depth_begin[j];
  5215. }
  5216. br_table_cache->br_depths[i] = depth;
  5217. bh_list_insert(module->br_table_cache_list,
  5218. br_table_cache);
  5219. }
  5220. else {
  5221. /* The depth can be stored in one byte, use the
  5222. byte of the leb to store it */
  5223. *p_depth++ = (uint8)depth;
  5224. }
  5225. }
  5226. #endif
  5227. }
  5228. #if WASM_ENABLE_FAST_INTERP == 0
  5229. /* Set the tailing bytes to nop */
  5230. if (br_table_cache)
  5231. p_depth = p_depth_begin;
  5232. while (p_depth < p)
  5233. *p_depth++ = WASM_OP_NOP;
  5234. #endif
  5235. RESET_STACK();
  5236. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5237. (void)ret_count;
  5238. (void)ret_types;
  5239. break;
  5240. }
  5241. case WASM_OP_RETURN:
  5242. {
  5243. int32 idx;
  5244. uint8 ret_type;
  5245. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  5246. idx--) {
  5247. ret_type = *(func->func_type->types
  5248. + func->func_type->param_count + idx);
  5249. POP_TYPE(ret_type);
  5250. #if WASM_ENABLE_FAST_INTERP != 0
  5251. /* emit the offset after return opcode */
  5252. POP_OFFSET_TYPE(ret_type);
  5253. #endif
  5254. }
  5255. RESET_STACK();
  5256. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  5257. break;
  5258. }
  5259. case WASM_OP_CALL:
  5260. #if WASM_ENABLE_TAIL_CALL != 0
  5261. case WASM_OP_RETURN_CALL:
  5262. #endif
  5263. {
  5264. WASMFuncType *func_type;
  5265. uint32 func_idx;
  5266. int32 idx;
  5267. read_leb_uint32(p, p_end, func_idx);
  5268. #if WASM_ENABLE_FAST_INTERP != 0
  5269. /* we need to emit func_idx before arguments */
  5270. emit_uint32(loader_ctx, func_idx);
  5271. #endif
  5272. bh_assert(func_idx < module->import_function_count
  5273. + module->function_count);
  5274. if (func_idx < module->import_function_count)
  5275. func_type =
  5276. module->import_functions[func_idx].u.function.func_type;
  5277. else
  5278. func_type = module
  5279. ->functions[func_idx
  5280. - module->import_function_count]
  5281. ->func_type;
  5282. if (func_type->param_count > 0) {
  5283. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5284. idx--) {
  5285. POP_TYPE(func_type->types[idx]);
  5286. #if WASM_ENABLE_FAST_INTERP != 0
  5287. POP_OFFSET_TYPE(func_type->types[idx]);
  5288. #endif
  5289. }
  5290. }
  5291. #if WASM_ENABLE_TAIL_CALL != 0
  5292. if (opcode == WASM_OP_CALL) {
  5293. #endif
  5294. for (i = 0; i < func_type->result_count; i++) {
  5295. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5296. #if WASM_ENABLE_FAST_INTERP != 0
  5297. /* Here we emit each return value's dynamic_offset. But
  5298. * in fact these offsets are continuous, so interpreter
  5299. * only need to get the first return value's offset.
  5300. */
  5301. PUSH_OFFSET_TYPE(
  5302. func_type->types[func_type->param_count + i]);
  5303. #endif
  5304. }
  5305. #if WASM_ENABLE_TAIL_CALL != 0
  5306. }
  5307. else {
  5308. bh_assert(func_type->result_count
  5309. == func->func_type->result_count);
  5310. for (i = 0; i < func_type->result_count; i++) {
  5311. bh_assert(
  5312. func_type->types[func_type->param_count + i]
  5313. == func->func_type
  5314. ->types[func->func_type->param_count + i]);
  5315. }
  5316. }
  5317. #endif
  5318. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5319. || WASM_ENABLE_WAMR_COMPILER != 0
  5320. func->has_op_func_call = true;
  5321. #endif
  5322. break;
  5323. }
  5324. case WASM_OP_CALL_INDIRECT:
  5325. #if WASM_ENABLE_TAIL_CALL != 0
  5326. case WASM_OP_RETURN_CALL_INDIRECT:
  5327. #endif
  5328. {
  5329. int32 idx;
  5330. WASMFuncType *func_type;
  5331. uint32 type_idx, table_idx;
  5332. bh_assert(module->import_table_count + module->table_count > 0);
  5333. read_leb_uint32(p, p_end, type_idx);
  5334. #if WASM_ENABLE_REF_TYPES != 0
  5335. read_leb_uint32(p, p_end, table_idx);
  5336. #else
  5337. CHECK_BUF(p, p_end, 1);
  5338. table_idx = read_uint8(p);
  5339. #endif
  5340. if (!check_table_index(module, table_idx, error_buf,
  5341. error_buf_size)) {
  5342. goto fail;
  5343. }
  5344. #if WASM_ENABLE_FAST_INTERP != 0
  5345. /* we need to emit before arguments */
  5346. emit_uint32(loader_ctx, type_idx);
  5347. emit_uint32(loader_ctx, table_idx);
  5348. #endif
  5349. /* skip elem idx */
  5350. POP_I32();
  5351. bh_assert(type_idx < module->type_count);
  5352. func_type = module->types[type_idx];
  5353. if (func_type->param_count > 0) {
  5354. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  5355. idx--) {
  5356. POP_TYPE(func_type->types[idx]);
  5357. #if WASM_ENABLE_FAST_INTERP != 0
  5358. POP_OFFSET_TYPE(func_type->types[idx]);
  5359. #endif
  5360. }
  5361. }
  5362. #if WASM_ENABLE_TAIL_CALL != 0
  5363. if (opcode == WASM_OP_CALL) {
  5364. #endif
  5365. for (i = 0; i < func_type->result_count; i++) {
  5366. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  5367. #if WASM_ENABLE_FAST_INTERP != 0
  5368. PUSH_OFFSET_TYPE(
  5369. func_type->types[func_type->param_count + i]);
  5370. #endif
  5371. }
  5372. #if WASM_ENABLE_TAIL_CALL != 0
  5373. }
  5374. else {
  5375. bh_assert(func_type->result_count
  5376. == func->func_type->result_count);
  5377. for (i = 0; i < func_type->result_count; i++) {
  5378. bh_assert(
  5379. func_type->types[func_type->param_count + i]
  5380. == func->func_type
  5381. ->types[func->func_type->param_count + i]);
  5382. }
  5383. }
  5384. #endif
  5385. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  5386. || WASM_ENABLE_WAMR_COMPILER != 0
  5387. func->has_op_func_call = true;
  5388. #endif
  5389. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5390. func->has_op_call_indirect = true;
  5391. #endif
  5392. break;
  5393. }
  5394. case WASM_OP_DROP:
  5395. {
  5396. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5397. int32 available_stack_cell =
  5398. (int32)(loader_ctx->stack_cell_num
  5399. - cur_block->stack_cell_num);
  5400. bh_assert(!(available_stack_cell <= 0
  5401. && !cur_block->is_stack_polymorphic));
  5402. if (available_stack_cell > 0) {
  5403. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  5404. loader_ctx->frame_ref--;
  5405. loader_ctx->stack_cell_num--;
  5406. #if WASM_ENABLE_FAST_INTERP != 0
  5407. skip_label();
  5408. loader_ctx->frame_offset--;
  5409. if ((*(loader_ctx->frame_offset)
  5410. > loader_ctx->start_dynamic_offset)
  5411. && (*(loader_ctx->frame_offset)
  5412. < loader_ctx->max_dynamic_offset))
  5413. loader_ctx->dynamic_offset--;
  5414. #endif
  5415. }
  5416. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  5417. loader_ctx->frame_ref -= 2;
  5418. loader_ctx->stack_cell_num -= 2;
  5419. #if WASM_ENABLE_FAST_INTERP == 0
  5420. *(p - 1) = WASM_OP_DROP_64;
  5421. #endif
  5422. #if WASM_ENABLE_FAST_INTERP != 0
  5423. skip_label();
  5424. loader_ctx->frame_offset -= 2;
  5425. if ((*(loader_ctx->frame_offset)
  5426. > loader_ctx->start_dynamic_offset)
  5427. && (*(loader_ctx->frame_offset)
  5428. < loader_ctx->max_dynamic_offset))
  5429. loader_ctx->dynamic_offset -= 2;
  5430. #endif
  5431. }
  5432. else {
  5433. bh_assert(0);
  5434. }
  5435. }
  5436. else {
  5437. #if WASM_ENABLE_FAST_INTERP != 0
  5438. skip_label();
  5439. #endif
  5440. }
  5441. break;
  5442. }
  5443. case WASM_OP_SELECT:
  5444. {
  5445. uint8 ref_type;
  5446. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5447. int32 available_stack_cell;
  5448. POP_I32();
  5449. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  5450. - cur_block->stack_cell_num);
  5451. bh_assert(!(available_stack_cell <= 0
  5452. && !cur_block->is_stack_polymorphic));
  5453. if (available_stack_cell > 0) {
  5454. switch (*(loader_ctx->frame_ref - 1)) {
  5455. case REF_I32:
  5456. case REF_F32:
  5457. break;
  5458. case REF_I64_2:
  5459. case REF_F64_2:
  5460. #if WASM_ENABLE_FAST_INTERP == 0
  5461. *(p - 1) = WASM_OP_SELECT_64;
  5462. #endif
  5463. #if WASM_ENABLE_FAST_INTERP != 0
  5464. if (loader_ctx->p_code_compiled) {
  5465. uint8 opcode_tmp = WASM_OP_SELECT_64;
  5466. uint8 *p_code_compiled_tmp =
  5467. loader_ctx->p_code_compiled - 2;
  5468. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5469. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5470. *(void **)(p_code_compiled_tmp
  5471. - sizeof(void *)) =
  5472. handle_table[opcode_tmp];
  5473. #else
  5474. int32 offset =
  5475. (int32)((uint8 *)handle_table[opcode_tmp]
  5476. - (uint8 *)handle_table[0]);
  5477. if (!(offset >= INT16_MIN
  5478. && offset < INT16_MAX)) {
  5479. set_error_buf(error_buf, error_buf_size,
  5480. "pre-compiled label offset "
  5481. "out of range");
  5482. goto fail;
  5483. }
  5484. *(int16 *)(p_code_compiled_tmp
  5485. - sizeof(int16)) = (int16)offset;
  5486. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5487. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5488. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5489. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5490. #else
  5491. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5492. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5493. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5494. }
  5495. #endif
  5496. break;
  5497. }
  5498. ref_type = *(loader_ctx->frame_ref - 1);
  5499. #if WASM_ENABLE_FAST_INTERP != 0
  5500. POP_OFFSET_TYPE(ref_type);
  5501. #endif
  5502. POP_TYPE(ref_type);
  5503. #if WASM_ENABLE_FAST_INTERP != 0
  5504. POP_OFFSET_TYPE(ref_type);
  5505. #endif
  5506. POP_TYPE(ref_type);
  5507. #if WASM_ENABLE_FAST_INTERP != 0
  5508. PUSH_OFFSET_TYPE(ref_type);
  5509. #endif
  5510. PUSH_TYPE(ref_type);
  5511. }
  5512. else {
  5513. #if WASM_ENABLE_FAST_INTERP != 0
  5514. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  5515. #endif
  5516. PUSH_TYPE(VALUE_TYPE_ANY);
  5517. }
  5518. break;
  5519. }
  5520. #if WASM_ENABLE_REF_TYPES != 0
  5521. case WASM_OP_SELECT_T:
  5522. {
  5523. uint8 vec_len, ref_type;
  5524. #if WASM_ENABLE_FAST_INTERP != 0
  5525. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  5526. #endif
  5527. read_leb_uint32(p, p_end, vec_len);
  5528. if (vec_len != 1) {
  5529. /* typed select must have exactly one result */
  5530. set_error_buf(error_buf, error_buf_size,
  5531. "invalid result arity");
  5532. goto fail;
  5533. }
  5534. CHECK_BUF(p, p_end, 1);
  5535. ref_type = read_uint8(p);
  5536. if (!is_value_type(ref_type)) {
  5537. set_error_buf(error_buf, error_buf_size,
  5538. "unknown value type");
  5539. goto fail;
  5540. }
  5541. POP_I32();
  5542. #if WASM_ENABLE_FAST_INTERP != 0
  5543. if (loader_ctx->p_code_compiled) {
  5544. uint8 opcode_tmp = WASM_OP_SELECT;
  5545. if (ref_type == VALUE_TYPE_F64
  5546. || ref_type == VALUE_TYPE_I64)
  5547. opcode_tmp = WASM_OP_SELECT_64;
  5548. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5549. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5550. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  5551. handle_table[opcode_tmp];
  5552. #else
  5553. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  5554. - (uint8 *)handle_table[0]);
  5555. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  5556. set_error_buf(error_buf, error_buf_size,
  5557. "pre-compiled label offset out of range");
  5558. goto fail;
  5559. }
  5560. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  5561. (int16)offset;
  5562. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5563. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  5564. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  5565. *(p_code_compiled_tmp - 1) = opcode_tmp;
  5566. #else
  5567. *(p_code_compiled_tmp - 2) = opcode_tmp;
  5568. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  5569. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  5570. }
  5571. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5572. #if WASM_ENABLE_FAST_INTERP != 0
  5573. POP_OFFSET_TYPE(ref_type);
  5574. POP_TYPE(ref_type);
  5575. POP_OFFSET_TYPE(ref_type);
  5576. POP_TYPE(ref_type);
  5577. PUSH_OFFSET_TYPE(ref_type);
  5578. PUSH_TYPE(ref_type);
  5579. #else
  5580. POP2_AND_PUSH(ref_type, ref_type);
  5581. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  5582. (void)vec_len;
  5583. break;
  5584. }
  5585. /* table.get x. tables[x]. [i32] -> [t] */
  5586. /* table.set x. tables[x]. [i32 t] -> [] */
  5587. case WASM_OP_TABLE_GET:
  5588. case WASM_OP_TABLE_SET:
  5589. {
  5590. uint8 decl_ref_type;
  5591. uint32 table_idx;
  5592. read_leb_uint32(p, p_end, table_idx);
  5593. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  5594. error_buf, error_buf_size))
  5595. goto fail;
  5596. #if WASM_ENABLE_FAST_INTERP != 0
  5597. emit_uint32(loader_ctx, table_idx);
  5598. #endif
  5599. if (opcode == WASM_OP_TABLE_GET) {
  5600. POP_I32();
  5601. #if WASM_ENABLE_FAST_INTERP != 0
  5602. PUSH_OFFSET_TYPE(decl_ref_type);
  5603. #endif
  5604. PUSH_TYPE(decl_ref_type);
  5605. }
  5606. else {
  5607. #if WASM_ENABLE_FAST_INTERP != 0
  5608. POP_OFFSET_TYPE(decl_ref_type);
  5609. #endif
  5610. POP_TYPE(decl_ref_type);
  5611. POP_I32();
  5612. }
  5613. break;
  5614. }
  5615. case WASM_OP_REF_NULL:
  5616. {
  5617. uint8 ref_type;
  5618. CHECK_BUF(p, p_end, 1);
  5619. ref_type = read_uint8(p);
  5620. if (ref_type != VALUE_TYPE_FUNCREF
  5621. && ref_type != VALUE_TYPE_EXTERNREF) {
  5622. set_error_buf(error_buf, error_buf_size,
  5623. "unknown value type");
  5624. goto fail;
  5625. }
  5626. #if WASM_ENABLE_FAST_INTERP != 0
  5627. PUSH_OFFSET_TYPE(ref_type);
  5628. #endif
  5629. PUSH_TYPE(ref_type);
  5630. break;
  5631. }
  5632. case WASM_OP_REF_IS_NULL:
  5633. {
  5634. #if WASM_ENABLE_FAST_INTERP != 0
  5635. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  5636. VALUE_TYPE_FUNCREF,
  5637. error_buf, error_buf_size)
  5638. && !wasm_loader_pop_frame_ref_offset(
  5639. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  5640. error_buf_size)) {
  5641. goto fail;
  5642. }
  5643. #else
  5644. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  5645. error_buf, error_buf_size)
  5646. && !wasm_loader_pop_frame_ref(loader_ctx,
  5647. VALUE_TYPE_EXTERNREF,
  5648. error_buf, error_buf_size)) {
  5649. goto fail;
  5650. }
  5651. #endif
  5652. PUSH_I32();
  5653. break;
  5654. }
  5655. case WASM_OP_REF_FUNC:
  5656. {
  5657. uint32 func_idx = 0;
  5658. read_leb_uint32(p, p_end, func_idx);
  5659. if (!check_function_index(module, func_idx, error_buf,
  5660. error_buf_size)) {
  5661. goto fail;
  5662. }
  5663. /* Refer to a forward-declared function */
  5664. if (func_idx >= cur_func_idx + module->import_function_count) {
  5665. WASMTableSeg *table_seg = module->table_segments;
  5666. bool func_declared = false;
  5667. uint32 j;
  5668. /* Check whether the function is declared in table segs */
  5669. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  5670. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  5671. && wasm_elem_is_declarative(table_seg->mode)) {
  5672. for (j = 0; j < table_seg->function_count; j++) {
  5673. if (table_seg->func_indexes[j] == func_idx) {
  5674. func_declared = true;
  5675. break;
  5676. }
  5677. }
  5678. }
  5679. }
  5680. if (!func_declared) {
  5681. /* Check whether the function is exported */
  5682. for (i = 0; i < module->export_count; i++) {
  5683. if (module->exports[i].kind == EXPORT_KIND_FUNC
  5684. && module->exports[i].index == func_idx) {
  5685. func_declared = true;
  5686. break;
  5687. }
  5688. }
  5689. }
  5690. bh_assert(func_declared);
  5691. (void)func_declared;
  5692. }
  5693. #if WASM_ENABLE_FAST_INTERP != 0
  5694. emit_uint32(loader_ctx, func_idx);
  5695. #endif
  5696. PUSH_FUNCREF();
  5697. break;
  5698. }
  5699. #endif /* WASM_ENABLE_REF_TYPES */
  5700. case WASM_OP_GET_LOCAL:
  5701. {
  5702. p_org = p - 1;
  5703. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5704. PUSH_TYPE(local_type);
  5705. #if WASM_ENABLE_FAST_INTERP != 0
  5706. /* Get Local is optimized out */
  5707. skip_label();
  5708. disable_emit = true;
  5709. operand_offset = local_offset;
  5710. PUSH_OFFSET_TYPE(local_type);
  5711. #else
  5712. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5713. && (WASM_ENABLE_FAST_JIT == 0)
  5714. if (local_offset < 0x80) {
  5715. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  5716. if (is_32bit_type(local_type))
  5717. *p_org++ = (uint8)local_offset;
  5718. else
  5719. *p_org++ = (uint8)(local_offset | 0x80);
  5720. while (p_org < p)
  5721. *p_org++ = WASM_OP_NOP;
  5722. }
  5723. #endif
  5724. #endif
  5725. break;
  5726. }
  5727. case WASM_OP_SET_LOCAL:
  5728. {
  5729. p_org = p - 1;
  5730. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5731. POP_TYPE(local_type);
  5732. #if WASM_ENABLE_FAST_INTERP != 0
  5733. if (!(preserve_referenced_local(
  5734. loader_ctx, opcode, local_offset, local_type,
  5735. &preserve_local, error_buf, error_buf_size)))
  5736. goto fail;
  5737. if (local_offset < 256) {
  5738. skip_label();
  5739. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  5740. if (loader_ctx->p_code_compiled)
  5741. STORE_U16(loader_ctx->p_code_compiled - 2,
  5742. local_offset);
  5743. loader_ctx->frame_offset--;
  5744. loader_ctx->dynamic_offset--;
  5745. }
  5746. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  5747. if (loader_ctx->p_code_compiled)
  5748. STORE_U16(loader_ctx->p_code_compiled - 2,
  5749. local_offset);
  5750. loader_ctx->frame_offset -= 2;
  5751. loader_ctx->dynamic_offset -= 2;
  5752. }
  5753. else {
  5754. if (is_32bit_type(local_type)) {
  5755. emit_label(EXT_OP_SET_LOCAL_FAST);
  5756. emit_byte(loader_ctx, (uint8)local_offset);
  5757. }
  5758. else {
  5759. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  5760. emit_byte(loader_ctx, (uint8)local_offset);
  5761. }
  5762. POP_OFFSET_TYPE(local_type);
  5763. }
  5764. }
  5765. else { /* local index larger than 255, reserve leb */
  5766. emit_uint32(loader_ctx, local_idx);
  5767. POP_OFFSET_TYPE(local_type);
  5768. }
  5769. #else
  5770. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5771. && (WASM_ENABLE_FAST_JIT == 0)
  5772. if (local_offset < 0x80) {
  5773. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  5774. if (is_32bit_type(local_type))
  5775. *p_org++ = (uint8)local_offset;
  5776. else
  5777. *p_org++ = (uint8)(local_offset | 0x80);
  5778. while (p_org < p)
  5779. *p_org++ = WASM_OP_NOP;
  5780. }
  5781. #endif
  5782. #endif
  5783. break;
  5784. }
  5785. case WASM_OP_TEE_LOCAL:
  5786. {
  5787. p_org = p - 1;
  5788. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  5789. #if WASM_ENABLE_FAST_INTERP != 0
  5790. /* If the stack is in polymorphic state, do fake pop and push on
  5791. offset stack to keep the depth of offset stack to be the
  5792. same with ref stack */
  5793. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  5794. if (cur_block->is_stack_polymorphic) {
  5795. POP_OFFSET_TYPE(local_type);
  5796. PUSH_OFFSET_TYPE(local_type);
  5797. }
  5798. #endif
  5799. POP_TYPE(local_type);
  5800. PUSH_TYPE(local_type);
  5801. #if WASM_ENABLE_FAST_INTERP != 0
  5802. if (!(preserve_referenced_local(
  5803. loader_ctx, opcode, local_offset, local_type,
  5804. &preserve_local, error_buf, error_buf_size)))
  5805. goto fail;
  5806. if (local_offset < 256) {
  5807. skip_label();
  5808. if (is_32bit_type(local_type)) {
  5809. emit_label(EXT_OP_TEE_LOCAL_FAST);
  5810. emit_byte(loader_ctx, (uint8)local_offset);
  5811. }
  5812. else {
  5813. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  5814. emit_byte(loader_ctx, (uint8)local_offset);
  5815. }
  5816. }
  5817. else { /* local index larger than 255, reserve leb */
  5818. emit_uint32(loader_ctx, local_idx);
  5819. }
  5820. emit_operand(loader_ctx,
  5821. *(loader_ctx->frame_offset
  5822. - wasm_value_type_cell_num(local_type)));
  5823. #else
  5824. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  5825. && (WASM_ENABLE_FAST_JIT == 0)
  5826. if (local_offset < 0x80) {
  5827. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  5828. if (is_32bit_type(local_type))
  5829. *p_org++ = (uint8)local_offset;
  5830. else
  5831. *p_org++ = (uint8)(local_offset | 0x80);
  5832. while (p_org < p)
  5833. *p_org++ = WASM_OP_NOP;
  5834. }
  5835. #endif
  5836. #endif
  5837. break;
  5838. }
  5839. case WASM_OP_GET_GLOBAL:
  5840. {
  5841. p_org = p - 1;
  5842. read_leb_uint32(p, p_end, global_idx);
  5843. bh_assert(global_idx < global_count);
  5844. global_type =
  5845. global_idx < module->import_global_count
  5846. ? module->import_globals[global_idx].u.global.type
  5847. : module
  5848. ->globals[global_idx
  5849. - module->import_global_count]
  5850. .type;
  5851. PUSH_TYPE(global_type);
  5852. #if WASM_ENABLE_FAST_INTERP == 0
  5853. if (global_type == VALUE_TYPE_I64
  5854. || global_type == VALUE_TYPE_F64) {
  5855. *p_org = WASM_OP_GET_GLOBAL_64;
  5856. }
  5857. #else /* else of WASM_ENABLE_FAST_INTERP */
  5858. if (is_64bit_type(global_type)) {
  5859. skip_label();
  5860. emit_label(WASM_OP_GET_GLOBAL_64);
  5861. }
  5862. emit_uint32(loader_ctx, global_idx);
  5863. PUSH_OFFSET_TYPE(global_type);
  5864. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5865. break;
  5866. }
  5867. case WASM_OP_SET_GLOBAL:
  5868. {
  5869. bool is_mutable = false;
  5870. p_org = p - 1;
  5871. read_leb_uint32(p, p_end, global_idx);
  5872. bh_assert(global_idx < global_count);
  5873. is_mutable =
  5874. global_idx < module->import_global_count
  5875. ? module->import_globals[global_idx].u.global.is_mutable
  5876. : module
  5877. ->globals[global_idx
  5878. - module->import_global_count]
  5879. .is_mutable;
  5880. bh_assert(is_mutable);
  5881. global_type =
  5882. global_idx < module->import_global_count
  5883. ? module->import_globals[global_idx].u.global.type
  5884. : module
  5885. ->globals[global_idx
  5886. - module->import_global_count]
  5887. .type;
  5888. POP_TYPE(global_type);
  5889. #if WASM_ENABLE_FAST_INTERP == 0
  5890. if (is_64bit_type(global_type)) {
  5891. *p_org = WASM_OP_SET_GLOBAL_64;
  5892. }
  5893. else if (module->aux_stack_size > 0
  5894. && global_idx == module->aux_stack_top_global_index) {
  5895. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  5896. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5897. func->has_op_set_global_aux_stack = true;
  5898. #endif
  5899. }
  5900. #else /* else of WASM_ENABLE_FAST_INTERP */
  5901. if (is_64bit_type(global_type)) {
  5902. skip_label();
  5903. emit_label(WASM_OP_SET_GLOBAL_64);
  5904. }
  5905. else if (module->aux_stack_size > 0
  5906. && global_idx == module->aux_stack_top_global_index) {
  5907. skip_label();
  5908. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  5909. }
  5910. emit_uint32(loader_ctx, global_idx);
  5911. POP_OFFSET_TYPE(global_type);
  5912. #endif /* end of WASM_ENABLE_FAST_INTERP */
  5913. (void)is_mutable;
  5914. break;
  5915. }
  5916. /* load */
  5917. case WASM_OP_I32_LOAD:
  5918. case WASM_OP_I32_LOAD8_S:
  5919. case WASM_OP_I32_LOAD8_U:
  5920. case WASM_OP_I32_LOAD16_S:
  5921. case WASM_OP_I32_LOAD16_U:
  5922. case WASM_OP_I64_LOAD:
  5923. case WASM_OP_I64_LOAD8_S:
  5924. case WASM_OP_I64_LOAD8_U:
  5925. case WASM_OP_I64_LOAD16_S:
  5926. case WASM_OP_I64_LOAD16_U:
  5927. case WASM_OP_I64_LOAD32_S:
  5928. case WASM_OP_I64_LOAD32_U:
  5929. case WASM_OP_F32_LOAD:
  5930. case WASM_OP_F64_LOAD:
  5931. /* store */
  5932. case WASM_OP_I32_STORE:
  5933. case WASM_OP_I32_STORE8:
  5934. case WASM_OP_I32_STORE16:
  5935. case WASM_OP_I64_STORE:
  5936. case WASM_OP_I64_STORE8:
  5937. case WASM_OP_I64_STORE16:
  5938. case WASM_OP_I64_STORE32:
  5939. case WASM_OP_F32_STORE:
  5940. case WASM_OP_F64_STORE:
  5941. {
  5942. #if WASM_ENABLE_FAST_INTERP != 0
  5943. /* change F32/F64 into I32/I64 */
  5944. if (opcode == WASM_OP_F32_LOAD) {
  5945. skip_label();
  5946. emit_label(WASM_OP_I32_LOAD);
  5947. }
  5948. else if (opcode == WASM_OP_F64_LOAD) {
  5949. skip_label();
  5950. emit_label(WASM_OP_I64_LOAD);
  5951. }
  5952. else if (opcode == WASM_OP_F32_STORE) {
  5953. skip_label();
  5954. emit_label(WASM_OP_I32_STORE);
  5955. }
  5956. else if (opcode == WASM_OP_F64_STORE) {
  5957. skip_label();
  5958. emit_label(WASM_OP_I64_STORE);
  5959. }
  5960. #endif
  5961. CHECK_MEMORY();
  5962. read_leb_uint32(p, p_end, align); /* align */
  5963. read_leb_uint32(p, p_end, mem_offset); /* offset */
  5964. #if WASM_ENABLE_FAST_INTERP != 0
  5965. emit_uint32(loader_ctx, mem_offset);
  5966. #endif
  5967. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  5968. func->has_memory_operations = true;
  5969. #endif
  5970. switch (opcode) {
  5971. /* load */
  5972. case WASM_OP_I32_LOAD:
  5973. case WASM_OP_I32_LOAD8_S:
  5974. case WASM_OP_I32_LOAD8_U:
  5975. case WASM_OP_I32_LOAD16_S:
  5976. case WASM_OP_I32_LOAD16_U:
  5977. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  5978. break;
  5979. case WASM_OP_I64_LOAD:
  5980. case WASM_OP_I64_LOAD8_S:
  5981. case WASM_OP_I64_LOAD8_U:
  5982. case WASM_OP_I64_LOAD16_S:
  5983. case WASM_OP_I64_LOAD16_U:
  5984. case WASM_OP_I64_LOAD32_S:
  5985. case WASM_OP_I64_LOAD32_U:
  5986. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  5987. break;
  5988. case WASM_OP_F32_LOAD:
  5989. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  5990. break;
  5991. case WASM_OP_F64_LOAD:
  5992. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  5993. break;
  5994. /* store */
  5995. case WASM_OP_I32_STORE:
  5996. case WASM_OP_I32_STORE8:
  5997. case WASM_OP_I32_STORE16:
  5998. POP_I32();
  5999. POP_I32();
  6000. break;
  6001. case WASM_OP_I64_STORE:
  6002. case WASM_OP_I64_STORE8:
  6003. case WASM_OP_I64_STORE16:
  6004. case WASM_OP_I64_STORE32:
  6005. POP_I64();
  6006. POP_I32();
  6007. break;
  6008. case WASM_OP_F32_STORE:
  6009. POP_F32();
  6010. POP_I32();
  6011. break;
  6012. case WASM_OP_F64_STORE:
  6013. POP_F64();
  6014. POP_I32();
  6015. break;
  6016. default:
  6017. break;
  6018. }
  6019. break;
  6020. }
  6021. case WASM_OP_MEMORY_SIZE:
  6022. CHECK_MEMORY();
  6023. /* reserved byte 0x00 */
  6024. bh_assert(*p == 0x00);
  6025. p++;
  6026. PUSH_I32();
  6027. module->possible_memory_grow = true;
  6028. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6029. func->has_memory_operations = true;
  6030. #endif
  6031. break;
  6032. case WASM_OP_MEMORY_GROW:
  6033. CHECK_MEMORY();
  6034. /* reserved byte 0x00 */
  6035. bh_assert(*p == 0x00);
  6036. p++;
  6037. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6038. module->possible_memory_grow = true;
  6039. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6040. || WASM_ENABLE_WAMR_COMPILER != 0
  6041. func->has_op_memory_grow = true;
  6042. #endif
  6043. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6044. func->has_memory_operations = true;
  6045. #endif
  6046. break;
  6047. case WASM_OP_I32_CONST:
  6048. read_leb_int32(p, p_end, i32_const);
  6049. #if WASM_ENABLE_FAST_INTERP != 0
  6050. skip_label();
  6051. disable_emit = true;
  6052. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  6053. if (operand_offset == 0) {
  6054. disable_emit = false;
  6055. emit_label(WASM_OP_I32_CONST);
  6056. emit_uint32(loader_ctx, i32_const);
  6057. }
  6058. #else
  6059. (void)i32_const;
  6060. #endif
  6061. PUSH_I32();
  6062. break;
  6063. case WASM_OP_I64_CONST:
  6064. read_leb_int64(p, p_end, i64_const);
  6065. #if WASM_ENABLE_FAST_INTERP != 0
  6066. skip_label();
  6067. disable_emit = true;
  6068. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  6069. if (operand_offset == 0) {
  6070. disable_emit = false;
  6071. emit_label(WASM_OP_I64_CONST);
  6072. emit_uint64(loader_ctx, i64_const);
  6073. }
  6074. #endif
  6075. PUSH_I64();
  6076. break;
  6077. case WASM_OP_F32_CONST:
  6078. p += sizeof(float32);
  6079. #if WASM_ENABLE_FAST_INTERP != 0
  6080. skip_label();
  6081. disable_emit = true;
  6082. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  6083. sizeof(float32));
  6084. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  6085. if (operand_offset == 0) {
  6086. disable_emit = false;
  6087. emit_label(WASM_OP_F32_CONST);
  6088. emit_float32(loader_ctx, f32_const);
  6089. }
  6090. #endif
  6091. PUSH_F32();
  6092. break;
  6093. case WASM_OP_F64_CONST:
  6094. p += sizeof(float64);
  6095. #if WASM_ENABLE_FAST_INTERP != 0
  6096. skip_label();
  6097. disable_emit = true;
  6098. /* Some MCU may require 8-byte align */
  6099. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  6100. sizeof(float64));
  6101. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  6102. if (operand_offset == 0) {
  6103. disable_emit = false;
  6104. emit_label(WASM_OP_F64_CONST);
  6105. emit_float64(loader_ctx, f64_const);
  6106. }
  6107. #endif
  6108. PUSH_F64();
  6109. break;
  6110. case WASM_OP_I32_EQZ:
  6111. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6112. break;
  6113. case WASM_OP_I32_EQ:
  6114. case WASM_OP_I32_NE:
  6115. case WASM_OP_I32_LT_S:
  6116. case WASM_OP_I32_LT_U:
  6117. case WASM_OP_I32_GT_S:
  6118. case WASM_OP_I32_GT_U:
  6119. case WASM_OP_I32_LE_S:
  6120. case WASM_OP_I32_LE_U:
  6121. case WASM_OP_I32_GE_S:
  6122. case WASM_OP_I32_GE_U:
  6123. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6124. break;
  6125. case WASM_OP_I64_EQZ:
  6126. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6127. break;
  6128. case WASM_OP_I64_EQ:
  6129. case WASM_OP_I64_NE:
  6130. case WASM_OP_I64_LT_S:
  6131. case WASM_OP_I64_LT_U:
  6132. case WASM_OP_I64_GT_S:
  6133. case WASM_OP_I64_GT_U:
  6134. case WASM_OP_I64_LE_S:
  6135. case WASM_OP_I64_LE_U:
  6136. case WASM_OP_I64_GE_S:
  6137. case WASM_OP_I64_GE_U:
  6138. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6139. break;
  6140. case WASM_OP_F32_EQ:
  6141. case WASM_OP_F32_NE:
  6142. case WASM_OP_F32_LT:
  6143. case WASM_OP_F32_GT:
  6144. case WASM_OP_F32_LE:
  6145. case WASM_OP_F32_GE:
  6146. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6147. break;
  6148. case WASM_OP_F64_EQ:
  6149. case WASM_OP_F64_NE:
  6150. case WASM_OP_F64_LT:
  6151. case WASM_OP_F64_GT:
  6152. case WASM_OP_F64_LE:
  6153. case WASM_OP_F64_GE:
  6154. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6155. break;
  6156. case WASM_OP_I32_CLZ:
  6157. case WASM_OP_I32_CTZ:
  6158. case WASM_OP_I32_POPCNT:
  6159. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6160. break;
  6161. case WASM_OP_I32_ADD:
  6162. case WASM_OP_I32_SUB:
  6163. case WASM_OP_I32_MUL:
  6164. case WASM_OP_I32_DIV_S:
  6165. case WASM_OP_I32_DIV_U:
  6166. case WASM_OP_I32_REM_S:
  6167. case WASM_OP_I32_REM_U:
  6168. case WASM_OP_I32_AND:
  6169. case WASM_OP_I32_OR:
  6170. case WASM_OP_I32_XOR:
  6171. case WASM_OP_I32_SHL:
  6172. case WASM_OP_I32_SHR_S:
  6173. case WASM_OP_I32_SHR_U:
  6174. case WASM_OP_I32_ROTL:
  6175. case WASM_OP_I32_ROTR:
  6176. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6177. break;
  6178. case WASM_OP_I64_CLZ:
  6179. case WASM_OP_I64_CTZ:
  6180. case WASM_OP_I64_POPCNT:
  6181. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6182. break;
  6183. case WASM_OP_I64_ADD:
  6184. case WASM_OP_I64_SUB:
  6185. case WASM_OP_I64_MUL:
  6186. case WASM_OP_I64_DIV_S:
  6187. case WASM_OP_I64_DIV_U:
  6188. case WASM_OP_I64_REM_S:
  6189. case WASM_OP_I64_REM_U:
  6190. case WASM_OP_I64_AND:
  6191. case WASM_OP_I64_OR:
  6192. case WASM_OP_I64_XOR:
  6193. case WASM_OP_I64_SHL:
  6194. case WASM_OP_I64_SHR_S:
  6195. case WASM_OP_I64_SHR_U:
  6196. case WASM_OP_I64_ROTL:
  6197. case WASM_OP_I64_ROTR:
  6198. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6199. break;
  6200. case WASM_OP_F32_ABS:
  6201. case WASM_OP_F32_NEG:
  6202. case WASM_OP_F32_CEIL:
  6203. case WASM_OP_F32_FLOOR:
  6204. case WASM_OP_F32_TRUNC:
  6205. case WASM_OP_F32_NEAREST:
  6206. case WASM_OP_F32_SQRT:
  6207. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6208. break;
  6209. case WASM_OP_F32_ADD:
  6210. case WASM_OP_F32_SUB:
  6211. case WASM_OP_F32_MUL:
  6212. case WASM_OP_F32_DIV:
  6213. case WASM_OP_F32_MIN:
  6214. case WASM_OP_F32_MAX:
  6215. case WASM_OP_F32_COPYSIGN:
  6216. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  6217. break;
  6218. case WASM_OP_F64_ABS:
  6219. case WASM_OP_F64_NEG:
  6220. case WASM_OP_F64_CEIL:
  6221. case WASM_OP_F64_FLOOR:
  6222. case WASM_OP_F64_TRUNC:
  6223. case WASM_OP_F64_NEAREST:
  6224. case WASM_OP_F64_SQRT:
  6225. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6226. break;
  6227. case WASM_OP_F64_ADD:
  6228. case WASM_OP_F64_SUB:
  6229. case WASM_OP_F64_MUL:
  6230. case WASM_OP_F64_DIV:
  6231. case WASM_OP_F64_MIN:
  6232. case WASM_OP_F64_MAX:
  6233. case WASM_OP_F64_COPYSIGN:
  6234. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  6235. break;
  6236. case WASM_OP_I32_WRAP_I64:
  6237. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  6238. break;
  6239. case WASM_OP_I32_TRUNC_S_F32:
  6240. case WASM_OP_I32_TRUNC_U_F32:
  6241. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6242. break;
  6243. case WASM_OP_I32_TRUNC_S_F64:
  6244. case WASM_OP_I32_TRUNC_U_F64:
  6245. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6246. break;
  6247. case WASM_OP_I64_EXTEND_S_I32:
  6248. case WASM_OP_I64_EXTEND_U_I32:
  6249. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6250. break;
  6251. case WASM_OP_I64_TRUNC_S_F32:
  6252. case WASM_OP_I64_TRUNC_U_F32:
  6253. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6254. break;
  6255. case WASM_OP_I64_TRUNC_S_F64:
  6256. case WASM_OP_I64_TRUNC_U_F64:
  6257. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6258. break;
  6259. case WASM_OP_F32_CONVERT_S_I32:
  6260. case WASM_OP_F32_CONVERT_U_I32:
  6261. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6262. break;
  6263. case WASM_OP_F32_CONVERT_S_I64:
  6264. case WASM_OP_F32_CONVERT_U_I64:
  6265. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  6266. break;
  6267. case WASM_OP_F32_DEMOTE_F64:
  6268. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  6269. break;
  6270. case WASM_OP_F64_CONVERT_S_I32:
  6271. case WASM_OP_F64_CONVERT_U_I32:
  6272. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  6273. break;
  6274. case WASM_OP_F64_CONVERT_S_I64:
  6275. case WASM_OP_F64_CONVERT_U_I64:
  6276. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6277. break;
  6278. case WASM_OP_F64_PROMOTE_F32:
  6279. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  6280. break;
  6281. case WASM_OP_I32_REINTERPRET_F32:
  6282. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6283. break;
  6284. case WASM_OP_I64_REINTERPRET_F64:
  6285. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6286. break;
  6287. case WASM_OP_F32_REINTERPRET_I32:
  6288. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  6289. break;
  6290. case WASM_OP_F64_REINTERPRET_I64:
  6291. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  6292. break;
  6293. case WASM_OP_I32_EXTEND8_S:
  6294. case WASM_OP_I32_EXTEND16_S:
  6295. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6296. break;
  6297. case WASM_OP_I64_EXTEND8_S:
  6298. case WASM_OP_I64_EXTEND16_S:
  6299. case WASM_OP_I64_EXTEND32_S:
  6300. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  6301. break;
  6302. case WASM_OP_MISC_PREFIX:
  6303. {
  6304. uint32 opcode1;
  6305. read_leb_uint32(p, p_end, opcode1);
  6306. #if WASM_ENABLE_FAST_INTERP != 0
  6307. emit_byte(loader_ctx, ((uint8)opcode1));
  6308. #endif
  6309. switch (opcode1) {
  6310. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6311. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6312. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  6313. break;
  6314. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6315. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6316. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  6317. break;
  6318. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6319. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6320. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  6321. break;
  6322. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6323. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6324. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  6325. break;
  6326. #if WASM_ENABLE_BULK_MEMORY != 0
  6327. case WASM_OP_MEMORY_INIT:
  6328. {
  6329. read_leb_uint32(p, p_end, segment_index);
  6330. #if WASM_ENABLE_FAST_INTERP != 0
  6331. emit_uint32(loader_ctx, segment_index);
  6332. #endif
  6333. bh_assert(module->import_memory_count
  6334. + module->memory_count
  6335. > 0);
  6336. bh_assert(*p == 0x00);
  6337. p++;
  6338. bh_assert(segment_index < module->data_seg_count);
  6339. bh_assert(module->data_seg_count1 > 0);
  6340. POP_I32();
  6341. POP_I32();
  6342. POP_I32();
  6343. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6344. func->has_memory_operations = true;
  6345. #endif
  6346. break;
  6347. }
  6348. case WASM_OP_DATA_DROP:
  6349. {
  6350. read_leb_uint32(p, p_end, segment_index);
  6351. #if WASM_ENABLE_FAST_INTERP != 0
  6352. emit_uint32(loader_ctx, segment_index);
  6353. #endif
  6354. bh_assert(segment_index < module->data_seg_count);
  6355. bh_assert(module->data_seg_count1 > 0);
  6356. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6357. func->has_memory_operations = true;
  6358. #endif
  6359. break;
  6360. }
  6361. case WASM_OP_MEMORY_COPY:
  6362. {
  6363. /* both src and dst memory index should be 0 */
  6364. bh_assert(*(int16 *)p == 0x0000);
  6365. p += 2;
  6366. bh_assert(module->import_memory_count
  6367. + module->memory_count
  6368. > 0);
  6369. POP_I32();
  6370. POP_I32();
  6371. POP_I32();
  6372. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6373. func->has_memory_operations = true;
  6374. #endif
  6375. break;
  6376. }
  6377. case WASM_OP_MEMORY_FILL:
  6378. {
  6379. bh_assert(*p == 0);
  6380. p++;
  6381. bh_assert(module->import_memory_count
  6382. + module->memory_count
  6383. > 0);
  6384. POP_I32();
  6385. POP_I32();
  6386. POP_I32();
  6387. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6388. func->has_memory_operations = true;
  6389. #endif
  6390. break;
  6391. }
  6392. #endif /* WASM_ENABLE_BULK_MEMORY */
  6393. #if WASM_ENABLE_REF_TYPES != 0
  6394. case WASM_OP_TABLE_INIT:
  6395. {
  6396. uint8 seg_ref_type, tbl_ref_type;
  6397. uint32 table_seg_idx, table_idx;
  6398. read_leb_uint32(p, p_end, table_seg_idx);
  6399. read_leb_uint32(p, p_end, table_idx);
  6400. if (!get_table_elem_type(module, table_idx,
  6401. &tbl_ref_type, error_buf,
  6402. error_buf_size))
  6403. goto fail;
  6404. if (!get_table_seg_elem_type(module, table_seg_idx,
  6405. &seg_ref_type, error_buf,
  6406. error_buf_size))
  6407. goto fail;
  6408. if (seg_ref_type != tbl_ref_type) {
  6409. set_error_buf(error_buf, error_buf_size,
  6410. "type mismatch");
  6411. goto fail;
  6412. }
  6413. #if WASM_ENABLE_FAST_INTERP != 0
  6414. emit_uint32(loader_ctx, table_seg_idx);
  6415. emit_uint32(loader_ctx, table_idx);
  6416. #endif
  6417. POP_I32();
  6418. POP_I32();
  6419. POP_I32();
  6420. break;
  6421. }
  6422. case WASM_OP_ELEM_DROP:
  6423. {
  6424. uint32 table_seg_idx;
  6425. read_leb_uint32(p, p_end, table_seg_idx);
  6426. if (!get_table_seg_elem_type(module, table_seg_idx,
  6427. NULL, error_buf,
  6428. error_buf_size))
  6429. goto fail;
  6430. #if WASM_ENABLE_FAST_INTERP != 0
  6431. emit_uint32(loader_ctx, table_seg_idx);
  6432. #endif
  6433. break;
  6434. }
  6435. case WASM_OP_TABLE_COPY:
  6436. {
  6437. uint8 src_ref_type, dst_ref_type;
  6438. uint32 src_tbl_idx, dst_tbl_idx;
  6439. read_leb_uint32(p, p_end, src_tbl_idx);
  6440. if (!get_table_elem_type(module, src_tbl_idx,
  6441. &src_ref_type, error_buf,
  6442. error_buf_size))
  6443. goto fail;
  6444. read_leb_uint32(p, p_end, dst_tbl_idx);
  6445. if (!get_table_elem_type(module, dst_tbl_idx,
  6446. &dst_ref_type, error_buf,
  6447. error_buf_size))
  6448. goto fail;
  6449. if (src_ref_type != dst_ref_type) {
  6450. set_error_buf(error_buf, error_buf_size,
  6451. "type mismatch");
  6452. goto fail;
  6453. }
  6454. #if WASM_ENABLE_FAST_INTERP != 0
  6455. emit_uint32(loader_ctx, src_tbl_idx);
  6456. emit_uint32(loader_ctx, dst_tbl_idx);
  6457. #endif
  6458. POP_I32();
  6459. POP_I32();
  6460. POP_I32();
  6461. break;
  6462. }
  6463. case WASM_OP_TABLE_SIZE:
  6464. {
  6465. uint32 table_idx;
  6466. read_leb_uint32(p, p_end, table_idx);
  6467. /* TODO: shall we create a new function to check
  6468. table idx instead of using below function? */
  6469. if (!get_table_elem_type(module, table_idx, NULL,
  6470. error_buf, error_buf_size))
  6471. goto fail;
  6472. #if WASM_ENABLE_FAST_INTERP != 0
  6473. emit_uint32(loader_ctx, table_idx);
  6474. #endif
  6475. PUSH_I32();
  6476. break;
  6477. }
  6478. case WASM_OP_TABLE_GROW:
  6479. case WASM_OP_TABLE_FILL:
  6480. {
  6481. uint8 decl_ref_type;
  6482. uint32 table_idx;
  6483. read_leb_uint32(p, p_end, table_idx);
  6484. if (!get_table_elem_type(module, table_idx,
  6485. &decl_ref_type, error_buf,
  6486. error_buf_size))
  6487. goto fail;
  6488. if (opcode1 == WASM_OP_TABLE_GROW) {
  6489. if (table_idx < module->import_table_count) {
  6490. module->import_tables[table_idx]
  6491. .u.table.possible_grow = true;
  6492. }
  6493. else {
  6494. module
  6495. ->tables[table_idx
  6496. - module->import_table_count]
  6497. .possible_grow = true;
  6498. }
  6499. }
  6500. #if WASM_ENABLE_FAST_INTERP != 0
  6501. emit_uint32(loader_ctx, table_idx);
  6502. #endif
  6503. POP_I32();
  6504. #if WASM_ENABLE_FAST_INTERP != 0
  6505. POP_OFFSET_TYPE(decl_ref_type);
  6506. #endif
  6507. POP_TYPE(decl_ref_type);
  6508. if (opcode1 == WASM_OP_TABLE_GROW)
  6509. PUSH_I32();
  6510. else
  6511. POP_I32();
  6512. break;
  6513. }
  6514. #endif /* WASM_ENABLE_REF_TYPES */
  6515. default:
  6516. bh_assert(0);
  6517. break;
  6518. }
  6519. break;
  6520. }
  6521. #if WASM_ENABLE_SHARED_MEMORY != 0
  6522. case WASM_OP_ATOMIC_PREFIX:
  6523. {
  6524. opcode = read_uint8(p);
  6525. #if WASM_ENABLE_FAST_INTERP != 0
  6526. emit_byte(loader_ctx, opcode);
  6527. #endif
  6528. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6529. CHECK_MEMORY();
  6530. read_leb_uint32(p, p_end, align); /* align */
  6531. read_leb_uint32(p, p_end, mem_offset); /* offset */
  6532. #if WASM_ENABLE_FAST_INTERP != 0
  6533. emit_uint32(loader_ctx, mem_offset);
  6534. #endif
  6535. }
  6536. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  6537. func->has_memory_operations = true;
  6538. #endif
  6539. switch (opcode) {
  6540. case WASM_OP_ATOMIC_NOTIFY:
  6541. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6542. break;
  6543. case WASM_OP_ATOMIC_WAIT32:
  6544. POP_I64();
  6545. POP_I32();
  6546. POP_I32();
  6547. PUSH_I32();
  6548. break;
  6549. case WASM_OP_ATOMIC_WAIT64:
  6550. POP_I64();
  6551. POP_I64();
  6552. POP_I32();
  6553. PUSH_I32();
  6554. break;
  6555. case WASM_OP_ATOMIC_FENCE:
  6556. /* reserved byte 0x00 */
  6557. bh_assert(*p == 0x00);
  6558. p++;
  6559. break;
  6560. case WASM_OP_ATOMIC_I32_LOAD:
  6561. case WASM_OP_ATOMIC_I32_LOAD8_U:
  6562. case WASM_OP_ATOMIC_I32_LOAD16_U:
  6563. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6564. break;
  6565. case WASM_OP_ATOMIC_I32_STORE:
  6566. case WASM_OP_ATOMIC_I32_STORE8:
  6567. case WASM_OP_ATOMIC_I32_STORE16:
  6568. POP_I32();
  6569. POP_I32();
  6570. break;
  6571. case WASM_OP_ATOMIC_I64_LOAD:
  6572. case WASM_OP_ATOMIC_I64_LOAD8_U:
  6573. case WASM_OP_ATOMIC_I64_LOAD16_U:
  6574. case WASM_OP_ATOMIC_I64_LOAD32_U:
  6575. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  6576. break;
  6577. case WASM_OP_ATOMIC_I64_STORE:
  6578. case WASM_OP_ATOMIC_I64_STORE8:
  6579. case WASM_OP_ATOMIC_I64_STORE16:
  6580. case WASM_OP_ATOMIC_I64_STORE32:
  6581. POP_I64();
  6582. POP_I32();
  6583. break;
  6584. case WASM_OP_ATOMIC_RMW_I32_ADD:
  6585. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  6586. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  6587. case WASM_OP_ATOMIC_RMW_I32_SUB:
  6588. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  6589. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  6590. case WASM_OP_ATOMIC_RMW_I32_AND:
  6591. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  6592. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  6593. case WASM_OP_ATOMIC_RMW_I32_OR:
  6594. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  6595. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  6596. case WASM_OP_ATOMIC_RMW_I32_XOR:
  6597. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  6598. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  6599. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  6600. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  6601. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  6602. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  6603. break;
  6604. case WASM_OP_ATOMIC_RMW_I64_ADD:
  6605. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  6606. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  6607. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  6608. case WASM_OP_ATOMIC_RMW_I64_SUB:
  6609. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  6610. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  6611. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  6612. case WASM_OP_ATOMIC_RMW_I64_AND:
  6613. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  6614. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  6615. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  6616. case WASM_OP_ATOMIC_RMW_I64_OR:
  6617. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  6618. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  6619. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  6620. case WASM_OP_ATOMIC_RMW_I64_XOR:
  6621. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  6622. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  6623. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  6624. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  6625. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  6626. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  6627. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  6628. POP_I64();
  6629. POP_I32();
  6630. PUSH_I64();
  6631. break;
  6632. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  6633. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  6634. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  6635. POP_I32();
  6636. POP_I32();
  6637. POP_I32();
  6638. PUSH_I32();
  6639. break;
  6640. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  6641. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  6642. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  6643. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  6644. POP_I64();
  6645. POP_I64();
  6646. POP_I32();
  6647. PUSH_I64();
  6648. break;
  6649. default:
  6650. bh_assert(0);
  6651. break;
  6652. }
  6653. break;
  6654. }
  6655. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6656. default:
  6657. bh_assert(0);
  6658. break;
  6659. }
  6660. #if WASM_ENABLE_FAST_INTERP != 0
  6661. last_op = opcode;
  6662. #endif
  6663. }
  6664. if (loader_ctx->csp_num > 0) {
  6665. set_error_buf(error_buf, error_buf_size,
  6666. "function body must end with END opcode");
  6667. goto fail;
  6668. }
  6669. #if WASM_ENABLE_FAST_INTERP != 0
  6670. if (loader_ctx->p_code_compiled == NULL)
  6671. goto re_scan;
  6672. func->const_cell_num = loader_ctx->const_cell_num;
  6673. if (func->const_cell_num > 0) {
  6674. int32 j;
  6675. if (!(func->consts = func_const = loader_malloc(
  6676. func->const_cell_num * 4, error_buf, error_buf_size)))
  6677. goto fail;
  6678. func_const_end = func->consts + func->const_cell_num * 4;
  6679. /* reverse the const buf */
  6680. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  6681. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  6682. if (c->value_type == VALUE_TYPE_F64
  6683. || c->value_type == VALUE_TYPE_I64) {
  6684. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6685. &(c->value.f64), (uint32)sizeof(int64));
  6686. func_const += sizeof(int64);
  6687. }
  6688. else {
  6689. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  6690. &(c->value.f32), (uint32)sizeof(int32));
  6691. func_const += sizeof(int32);
  6692. }
  6693. }
  6694. }
  6695. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  6696. - loader_ctx->start_dynamic_offset + 1;
  6697. #else
  6698. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  6699. #endif
  6700. func->max_block_num = loader_ctx->max_csp_num;
  6701. return_value = true;
  6702. fail:
  6703. wasm_loader_ctx_destroy(loader_ctx);
  6704. (void)u8;
  6705. (void)u32;
  6706. (void)i32;
  6707. (void)i64_const;
  6708. (void)global_count;
  6709. (void)local_count;
  6710. (void)local_offset;
  6711. (void)p_org;
  6712. (void)mem_offset;
  6713. (void)align;
  6714. #if WASM_ENABLE_BULK_MEMORY != 0
  6715. (void)segment_index;
  6716. #endif
  6717. return return_value;
  6718. }