wasm_loader.c 361 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132
  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_DEBUG_INTERP != 0
  14. #include "../libraries/debug-engine/debug_engine.h"
  15. #endif
  16. #if WASM_ENABLE_FAST_JIT != 0
  17. #include "../fast-jit/jit_compiler.h"
  18. #include "../fast-jit/jit_codecache.h"
  19. #endif
  20. #if WASM_ENABLE_JIT != 0
  21. #include "../compilation/aot_llvm.h"
  22. #endif
  23. /* Read a value of given type from the address pointed to by the given
  24. pointer and increase the pointer to the position just after the
  25. value being read. */
  26. #define TEMPLATE_READ_VALUE(Type, p) \
  27. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  28. static void
  29. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  30. {
  31. if (error_buf != NULL) {
  32. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  33. string);
  34. }
  35. }
  36. static void
  37. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  38. {
  39. va_list args;
  40. char buf[128];
  41. if (error_buf != NULL) {
  42. va_start(args, format);
  43. vsnprintf(buf, sizeof(buf), format, args);
  44. va_end(args);
  45. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  46. }
  47. }
  48. static bool
  49. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  50. char *error_buf, uint32 error_buf_size)
  51. {
  52. if ((uintptr_t)buf + length < (uintptr_t)buf
  53. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  54. set_error_buf(error_buf, error_buf_size,
  55. "unexpected end of section or function");
  56. return false;
  57. }
  58. return true;
  59. }
  60. static bool
  61. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  62. char *error_buf, uint32 error_buf_size)
  63. {
  64. if ((uintptr_t)buf + length < (uintptr_t)buf
  65. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  66. set_error_buf(error_buf, error_buf_size, "unexpected end");
  67. return false;
  68. }
  69. return true;
  70. }
  71. #define CHECK_BUF(buf, buf_end, length) \
  72. do { \
  73. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  74. goto fail; \
  75. } \
  76. } while (0)
  77. #define CHECK_BUF1(buf, buf_end, length) \
  78. do { \
  79. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  80. goto fail; \
  81. } \
  82. } while (0)
  83. #define skip_leb(p) while (*p++ & 0x80)
  84. #define skip_leb_int64(p, p_end) skip_leb(p)
  85. #define skip_leb_uint32(p, p_end) skip_leb(p)
  86. #define skip_leb_int32(p, p_end) skip_leb(p)
  87. static bool
  88. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  89. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  90. {
  91. const uint8 *buf = *p_buf;
  92. uint64 result = 0;
  93. uint32 shift = 0;
  94. uint32 offset = 0, bcnt = 0;
  95. uint64 byte;
  96. while (true) {
  97. /* uN or SN must not exceed ceil(N/7) bytes */
  98. if (bcnt + 1 > (maxbits + 6) / 7) {
  99. set_error_buf(error_buf, error_buf_size,
  100. "integer representation too long");
  101. return false;
  102. }
  103. CHECK_BUF(buf, buf_end, offset + 1);
  104. byte = buf[offset];
  105. offset += 1;
  106. result |= ((byte & 0x7f) << shift);
  107. shift += 7;
  108. bcnt += 1;
  109. if ((byte & 0x80) == 0) {
  110. break;
  111. }
  112. }
  113. if (!sign && maxbits == 32 && shift >= maxbits) {
  114. /* The top bits set represent values > 32 bits */
  115. if (((uint8)byte) & 0xf0)
  116. goto fail_integer_too_large;
  117. }
  118. else if (sign && maxbits == 32) {
  119. if (shift < maxbits) {
  120. /* Sign extend, second highest bit is the sign bit */
  121. if ((uint8)byte & 0x40)
  122. result |= (~((uint64)0)) << shift;
  123. }
  124. else {
  125. /* The top bits should be a sign-extension of the sign bit */
  126. bool sign_bit_set = ((uint8)byte) & 0x8;
  127. int top_bits = ((uint8)byte) & 0xf0;
  128. if ((sign_bit_set && top_bits != 0x70)
  129. || (!sign_bit_set && top_bits != 0))
  130. goto fail_integer_too_large;
  131. }
  132. }
  133. else if (sign && maxbits == 64) {
  134. if (shift < maxbits) {
  135. /* Sign extend, second highest bit is the sign bit */
  136. if ((uint8)byte & 0x40)
  137. result |= (~((uint64)0)) << shift;
  138. }
  139. else {
  140. /* The top bits should be a sign-extension of the sign bit */
  141. bool sign_bit_set = ((uint8)byte) & 0x1;
  142. int top_bits = ((uint8)byte) & 0xfe;
  143. if ((sign_bit_set && top_bits != 0x7e)
  144. || (!sign_bit_set && top_bits != 0))
  145. goto fail_integer_too_large;
  146. }
  147. }
  148. *p_buf += offset;
  149. *p_result = result;
  150. return true;
  151. fail_integer_too_large:
  152. set_error_buf(error_buf, error_buf_size, "integer too large");
  153. fail:
  154. return false;
  155. }
  156. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  157. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  158. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  159. #define read_leb_int64(p, p_end, res) \
  160. do { \
  161. uint64 res64; \
  162. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  163. error_buf_size)) \
  164. goto fail; \
  165. res = (int64)res64; \
  166. } while (0)
  167. #define read_leb_uint32(p, p_end, res) \
  168. do { \
  169. uint64 res64; \
  170. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  171. error_buf_size)) \
  172. goto fail; \
  173. res = (uint32)res64; \
  174. } while (0)
  175. #define read_leb_int32(p, p_end, res) \
  176. do { \
  177. uint64 res64; \
  178. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  179. error_buf_size)) \
  180. goto fail; \
  181. res = (int32)res64; \
  182. } while (0)
  183. static char *
  184. type2str(uint8 type)
  185. {
  186. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  187. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  188. return type_str[type - VALUE_TYPE_V128];
  189. else if (type == VALUE_TYPE_FUNCREF)
  190. return "funcref";
  191. else if (type == VALUE_TYPE_EXTERNREF)
  192. return "externref";
  193. else
  194. return "unknown type";
  195. }
  196. static bool
  197. is_32bit_type(uint8 type)
  198. {
  199. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  200. #if WASM_ENABLE_REF_TYPES != 0
  201. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  202. #endif
  203. )
  204. return true;
  205. return false;
  206. }
  207. static bool
  208. is_64bit_type(uint8 type)
  209. {
  210. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  211. return true;
  212. return false;
  213. }
  214. static bool
  215. is_value_type(uint8 type)
  216. {
  217. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_I64
  218. || type == VALUE_TYPE_F32 || type == VALUE_TYPE_F64
  219. #if WASM_ENABLE_REF_TYPES != 0
  220. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  221. #endif
  222. #if WASM_ENABLE_SIMD != 0
  223. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  224. || type == VALUE_TYPE_V128
  225. #endif
  226. #endif
  227. )
  228. return true;
  229. return false;
  230. }
  231. static bool
  232. is_byte_a_type(uint8 type)
  233. {
  234. return is_value_type(type) || (type == VALUE_TYPE_VOID);
  235. }
  236. #if WASM_ENABLE_SIMD != 0
  237. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  238. static V128
  239. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  240. {
  241. V128 result;
  242. uint8 i;
  243. for (i = 0; i != 16; ++i) {
  244. result.i8x16[i] = read_uint8(p_buf);
  245. }
  246. return result;
  247. }
  248. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  249. #endif /* end of WASM_ENABLE_SIMD */
  250. static void *
  251. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  252. {
  253. void *mem;
  254. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  255. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  256. return NULL;
  257. }
  258. memset(mem, 0, (uint32)size);
  259. return mem;
  260. }
  261. static bool
  262. check_utf8_str(const uint8 *str, uint32 len)
  263. {
  264. /* The valid ranges are taken from page 125, below link
  265. https://www.unicode.org/versions/Unicode9.0.0/ch03.pdf */
  266. const uint8 *p = str, *p_end = str + len;
  267. uint8 chr;
  268. while (p < p_end) {
  269. chr = *p;
  270. if (chr < 0x80) {
  271. p++;
  272. }
  273. else if (chr >= 0xC2 && chr <= 0xDF && p + 1 < p_end) {
  274. if (p[1] < 0x80 || p[1] > 0xBF) {
  275. return false;
  276. }
  277. p += 2;
  278. }
  279. else if (chr >= 0xE0 && chr <= 0xEF && p + 2 < p_end) {
  280. if (chr == 0xE0) {
  281. if (p[1] < 0xA0 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  282. return false;
  283. }
  284. }
  285. else if (chr == 0xED) {
  286. if (p[1] < 0x80 || p[1] > 0x9F || p[2] < 0x80 || p[2] > 0xBF) {
  287. return false;
  288. }
  289. }
  290. else if (chr >= 0xE1 && chr <= 0xEF) {
  291. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF) {
  292. return false;
  293. }
  294. }
  295. p += 3;
  296. }
  297. else if (chr >= 0xF0 && chr <= 0xF4 && p + 3 < p_end) {
  298. if (chr == 0xF0) {
  299. if (p[1] < 0x90 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  300. || p[3] < 0x80 || p[3] > 0xBF) {
  301. return false;
  302. }
  303. }
  304. else if (chr >= 0xF1 && chr <= 0xF3) {
  305. if (p[1] < 0x80 || p[1] > 0xBF || p[2] < 0x80 || p[2] > 0xBF
  306. || p[3] < 0x80 || p[3] > 0xBF) {
  307. return false;
  308. }
  309. }
  310. else if (chr == 0xF4) {
  311. if (p[1] < 0x80 || p[1] > 0x8F || p[2] < 0x80 || p[2] > 0xBF
  312. || p[3] < 0x80 || p[3] > 0xBF) {
  313. return false;
  314. }
  315. }
  316. p += 4;
  317. }
  318. else {
  319. return false;
  320. }
  321. }
  322. return (p == p_end);
  323. }
  324. static char *
  325. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  326. bool is_load_from_file_buf, char *error_buf,
  327. uint32 error_buf_size)
  328. {
  329. StringNode *node, *node_next;
  330. if (!check_utf8_str(str, len)) {
  331. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  332. return NULL;
  333. }
  334. if (len == 0) {
  335. return "";
  336. }
  337. else if (is_load_from_file_buf) {
  338. /* As the file buffer can be referred to after loading, we use
  339. the previous byte of leb encoded size to adjust the string:
  340. move string 1 byte backward and then append '\0' */
  341. char *c_str = (char *)str - 1;
  342. bh_memmove_s(c_str, len + 1, c_str + 1, len);
  343. c_str[len] = '\0';
  344. return c_str;
  345. }
  346. /* Search const str list */
  347. node = module->const_str_list;
  348. while (node) {
  349. node_next = node->next;
  350. if (strlen(node->str) == len && !memcmp(node->str, str, len))
  351. break;
  352. node = node_next;
  353. }
  354. if (node) {
  355. return node->str;
  356. }
  357. if (!(node = loader_malloc(sizeof(StringNode) + len + 1, error_buf,
  358. error_buf_size))) {
  359. return NULL;
  360. }
  361. node->str = ((char *)node) + sizeof(StringNode);
  362. bh_memcpy_s(node->str, len + 1, str, len);
  363. node->str[len] = '\0';
  364. if (!module->const_str_list) {
  365. /* set as head */
  366. module->const_str_list = node;
  367. node->next = NULL;
  368. }
  369. else {
  370. /* insert it */
  371. node->next = module->const_str_list;
  372. module->const_str_list = node;
  373. }
  374. return node->str;
  375. }
  376. static void
  377. destroy_wasm_type(WASMType *type)
  378. {
  379. if (type->ref_count > 1) {
  380. /* The type is referenced by other types
  381. of current wasm module */
  382. type->ref_count--;
  383. return;
  384. }
  385. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  386. && WASM_ENABLE_LAZY_JIT != 0
  387. if (type->call_to_llvm_jit_from_fast_jit)
  388. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  389. #endif
  390. wasm_runtime_free(type);
  391. }
  392. static bool
  393. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  394. InitializerExpression *init_expr, uint8 type, char *error_buf,
  395. uint32 error_buf_size)
  396. {
  397. const uint8 *p = *p_buf, *p_end = buf_end;
  398. uint8 flag, end_byte, *p_float;
  399. uint32 i;
  400. CHECK_BUF(p, p_end, 1);
  401. init_expr->init_expr_type = read_uint8(p);
  402. flag = init_expr->init_expr_type;
  403. switch (flag) {
  404. /* i32.const */
  405. case INIT_EXPR_TYPE_I32_CONST:
  406. if (type != VALUE_TYPE_I32)
  407. goto fail_type_mismatch;
  408. read_leb_int32(p, p_end, init_expr->u.i32);
  409. break;
  410. /* i64.const */
  411. case INIT_EXPR_TYPE_I64_CONST:
  412. if (type != VALUE_TYPE_I64)
  413. goto fail_type_mismatch;
  414. read_leb_int64(p, p_end, init_expr->u.i64);
  415. break;
  416. /* f32.const */
  417. case INIT_EXPR_TYPE_F32_CONST:
  418. if (type != VALUE_TYPE_F32)
  419. goto fail_type_mismatch;
  420. CHECK_BUF(p, p_end, 4);
  421. p_float = (uint8 *)&init_expr->u.f32;
  422. for (i = 0; i < sizeof(float32); i++)
  423. *p_float++ = *p++;
  424. break;
  425. /* f64.const */
  426. case INIT_EXPR_TYPE_F64_CONST:
  427. if (type != VALUE_TYPE_F64)
  428. goto fail_type_mismatch;
  429. CHECK_BUF(p, p_end, 8);
  430. p_float = (uint8 *)&init_expr->u.f64;
  431. for (i = 0; i < sizeof(float64); i++)
  432. *p_float++ = *p++;
  433. break;
  434. #if WASM_ENABLE_SIMD != 0
  435. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  436. case INIT_EXPR_TYPE_V128_CONST:
  437. {
  438. uint64 high, low;
  439. if (type != VALUE_TYPE_V128)
  440. goto fail_type_mismatch;
  441. flag = read_uint8(p);
  442. (void)flag;
  443. CHECK_BUF(p, p_end, 16);
  444. wasm_runtime_read_v128(p, &high, &low);
  445. p += 16;
  446. init_expr->u.v128.i64x2[0] = high;
  447. init_expr->u.v128.i64x2[1] = low;
  448. break;
  449. }
  450. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  451. #endif /* end of WASM_ENABLE_SIMD */
  452. #if WASM_ENABLE_REF_TYPES != 0
  453. case INIT_EXPR_TYPE_FUNCREF_CONST:
  454. {
  455. if (type != VALUE_TYPE_FUNCREF)
  456. goto fail_type_mismatch;
  457. read_leb_uint32(p, p_end, init_expr->u.ref_index);
  458. break;
  459. }
  460. case INIT_EXPR_TYPE_REFNULL_CONST:
  461. {
  462. uint8 reftype;
  463. CHECK_BUF(p, p_end, 1);
  464. reftype = read_uint8(p);
  465. if (reftype != type)
  466. goto fail_type_mismatch;
  467. init_expr->u.ref_index = NULL_REF;
  468. break;
  469. }
  470. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  471. /* get_global */
  472. case INIT_EXPR_TYPE_GET_GLOBAL:
  473. read_leb_uint32(p, p_end, init_expr->u.global_index);
  474. break;
  475. default:
  476. {
  477. set_error_buf(error_buf, error_buf_size,
  478. "illegal opcode "
  479. "or constant expression required "
  480. "or type mismatch");
  481. goto fail;
  482. }
  483. }
  484. CHECK_BUF(p, p_end, 1);
  485. end_byte = read_uint8(p);
  486. if (end_byte != 0x0b)
  487. goto fail_type_mismatch;
  488. *p_buf = p;
  489. return true;
  490. fail_type_mismatch:
  491. set_error_buf(error_buf, error_buf_size,
  492. "type mismatch or constant expression required");
  493. fail:
  494. return false;
  495. }
  496. static bool
  497. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  498. char *error_buf, uint32 error_buf_size)
  499. {
  500. const uint8 *p = buf, *p_end = buf_end, *p_org;
  501. uint32 type_count, param_count, result_count, i, j;
  502. uint32 param_cell_num, ret_cell_num;
  503. uint64 total_size;
  504. uint8 flag;
  505. WASMType *type;
  506. read_leb_uint32(p, p_end, type_count);
  507. if (type_count) {
  508. module->type_count = type_count;
  509. total_size = sizeof(WASMType *) * (uint64)type_count;
  510. if (!(module->types =
  511. loader_malloc(total_size, error_buf, error_buf_size))) {
  512. return false;
  513. }
  514. for (i = 0; i < type_count; i++) {
  515. CHECK_BUF(p, p_end, 1);
  516. flag = read_uint8(p);
  517. if (flag != 0x60) {
  518. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  519. return false;
  520. }
  521. read_leb_uint32(p, p_end, param_count);
  522. /* Resolve param count and result count firstly */
  523. p_org = p;
  524. CHECK_BUF(p, p_end, param_count);
  525. p += param_count;
  526. read_leb_uint32(p, p_end, result_count);
  527. CHECK_BUF(p, p_end, result_count);
  528. p = p_org;
  529. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  530. set_error_buf(error_buf, error_buf_size,
  531. "param count or result count too large");
  532. return false;
  533. }
  534. total_size = offsetof(WASMType, types)
  535. + sizeof(uint8) * (uint64)(param_count + result_count);
  536. if (!(type = module->types[i] =
  537. loader_malloc(total_size, error_buf, error_buf_size))) {
  538. return false;
  539. }
  540. /* Resolve param types and result types */
  541. type->ref_count = 1;
  542. type->param_count = (uint16)param_count;
  543. type->result_count = (uint16)result_count;
  544. for (j = 0; j < param_count; j++) {
  545. CHECK_BUF(p, p_end, 1);
  546. type->types[j] = read_uint8(p);
  547. }
  548. read_leb_uint32(p, p_end, result_count);
  549. for (j = 0; j < result_count; j++) {
  550. CHECK_BUF(p, p_end, 1);
  551. type->types[param_count + j] = read_uint8(p);
  552. }
  553. for (j = 0; j < param_count + result_count; j++) {
  554. if (!is_value_type(type->types[j])) {
  555. set_error_buf(error_buf, error_buf_size,
  556. "unknown value type");
  557. return false;
  558. }
  559. }
  560. param_cell_num = wasm_get_cell_num(type->types, param_count);
  561. ret_cell_num =
  562. wasm_get_cell_num(type->types + param_count, result_count);
  563. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  564. set_error_buf(error_buf, error_buf_size,
  565. "param count or result count too large");
  566. return false;
  567. }
  568. type->param_cell_num = (uint16)param_cell_num;
  569. type->ret_cell_num = (uint16)ret_cell_num;
  570. /* If there is already a same type created, use it instead */
  571. for (j = 0; j < i; j++) {
  572. if (wasm_type_equal(type, module->types[j])) {
  573. if (module->types[j]->ref_count == UINT16_MAX) {
  574. set_error_buf(error_buf, error_buf_size,
  575. "wasm type's ref count too large");
  576. return false;
  577. }
  578. destroy_wasm_type(type);
  579. module->types[i] = module->types[j];
  580. module->types[j]->ref_count++;
  581. break;
  582. }
  583. }
  584. }
  585. }
  586. if (p != p_end) {
  587. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  588. return false;
  589. }
  590. LOG_VERBOSE("Load type section success.\n");
  591. return true;
  592. fail:
  593. return false;
  594. }
  595. static void
  596. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  597. {
  598. uint32 default_max_size =
  599. init_size * 2 > TABLE_MAX_SIZE ? init_size * 2 : TABLE_MAX_SIZE;
  600. if (max_size_flag) {
  601. /* module defines the table limitation */
  602. bh_assert(init_size <= *max_size);
  603. if (init_size < *max_size) {
  604. *max_size =
  605. *max_size < default_max_size ? *max_size : default_max_size;
  606. }
  607. }
  608. else {
  609. /* partial defined table limitation, gives a default value */
  610. *max_size = default_max_size;
  611. }
  612. }
  613. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  614. /**
  615. * Find export item of a module with export info:
  616. * module name, field name and export kind
  617. */
  618. static WASMExport *
  619. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  620. const char *field_name, uint8 export_kind,
  621. char *error_buf, uint32 error_buf_size)
  622. {
  623. WASMExport *export;
  624. uint32 i;
  625. for (i = 0, export = module->exports; i < module->export_count;
  626. ++i, ++export) {
  627. /**
  628. * need to consider a scenario that different kinds of exports
  629. * may have the same name, like
  630. * (table (export "m1" "exported") 10 funcref)
  631. * (memory (export "m1" "exported") 10)
  632. **/
  633. if (export->kind == export_kind && !strcmp(field_name, export->name)) {
  634. break;
  635. }
  636. }
  637. if (i == module->export_count) {
  638. LOG_DEBUG("can not find an export %d named %s in the module %s",
  639. export_kind, field_name, module_name);
  640. set_error_buf(error_buf, error_buf_size,
  641. "unknown import or incompatible import type");
  642. return NULL;
  643. }
  644. (void)module_name;
  645. /* since there is a validation in load_export_section(), it is for sure
  646. * export->index is valid*/
  647. return export;
  648. }
  649. #endif
  650. #if WASM_ENABLE_MULTI_MODULE != 0
  651. static WASMFunction *
  652. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  653. const WASMType *expected_function_type,
  654. char *error_buf, uint32 error_buf_size)
  655. {
  656. WASMModuleCommon *module_reg;
  657. WASMFunction *function = NULL;
  658. WASMExport *export = NULL;
  659. WASMModule *module = NULL;
  660. WASMType *target_function_type = NULL;
  661. module_reg = wasm_runtime_find_module_registered(module_name);
  662. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  663. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  664. function_name);
  665. set_error_buf(error_buf, error_buf_size, "unknown import");
  666. return NULL;
  667. }
  668. module = (WASMModule *)module_reg;
  669. export =
  670. wasm_loader_find_export(module, module_name, function_name,
  671. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  672. if (!export) {
  673. return NULL;
  674. }
  675. /* resolve function type and function */
  676. if (export->index < module->import_function_count) {
  677. target_function_type =
  678. module->import_functions[export->index].u.function.func_type;
  679. function = module->import_functions[export->index]
  680. .u.function.import_func_linked;
  681. }
  682. else {
  683. target_function_type =
  684. module->functions[export->index - module->import_function_count]
  685. ->func_type;
  686. function =
  687. module->functions[export->index - module->import_function_count];
  688. }
  689. /* check function type */
  690. if (!wasm_type_equal(expected_function_type, target_function_type)) {
  691. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  692. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  693. return NULL;
  694. }
  695. return function;
  696. }
  697. static WASMTable *
  698. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  699. uint32 init_size, uint32 max_size, char *error_buf,
  700. uint32 error_buf_size)
  701. {
  702. WASMModuleCommon *module_reg;
  703. WASMTable *table = NULL;
  704. WASMExport *export = NULL;
  705. WASMModule *module = NULL;
  706. module_reg = wasm_runtime_find_module_registered(module_name);
  707. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  708. LOG_DEBUG("can not find a module named %s for table", module_name);
  709. set_error_buf(error_buf, error_buf_size, "unknown import");
  710. return NULL;
  711. }
  712. module = (WASMModule *)module_reg;
  713. export =
  714. wasm_loader_find_export(module, module_name, table_name,
  715. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  716. if (!export) {
  717. return NULL;
  718. }
  719. /* resolve table and check the init/max size */
  720. if (export->index < module->import_table_count) {
  721. table =
  722. module->import_tables[export->index].u.table.import_table_linked;
  723. }
  724. else {
  725. table = &(module->tables[export->index - module->import_table_count]);
  726. }
  727. if (table->init_size < init_size || table->max_size > max_size) {
  728. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  729. module_name, table_name, table->init_size, table->max_size,
  730. init_size, max_size);
  731. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  732. return NULL;
  733. }
  734. return table;
  735. }
  736. static WASMMemory *
  737. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  738. uint32 init_page_count, uint32 max_page_count,
  739. char *error_buf, uint32 error_buf_size)
  740. {
  741. WASMModuleCommon *module_reg;
  742. WASMMemory *memory = NULL;
  743. WASMExport *export = NULL;
  744. WASMModule *module = NULL;
  745. module_reg = wasm_runtime_find_module_registered(module_name);
  746. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  747. LOG_DEBUG("can not find a module named %s for memory", module_name);
  748. set_error_buf(error_buf, error_buf_size, "unknown import");
  749. return NULL;
  750. }
  751. module = (WASMModule *)module_reg;
  752. export =
  753. wasm_loader_find_export(module, module_name, memory_name,
  754. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  755. if (!export) {
  756. return NULL;
  757. }
  758. /* resolve memory and check the init/max page count */
  759. if (export->index < module->import_memory_count) {
  760. memory = module->import_memories[export->index]
  761. .u.memory.import_memory_linked;
  762. }
  763. else {
  764. memory =
  765. &(module->memories[export->index - module->import_memory_count]);
  766. }
  767. if (memory->init_page_count < init_page_count
  768. || memory->max_page_count > max_page_count) {
  769. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  770. module_name, memory_name, memory->init_page_count,
  771. memory->max_page_count, init_page_count, max_page_count);
  772. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  773. return NULL;
  774. }
  775. return memory;
  776. }
  777. static WASMGlobal *
  778. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  779. uint8 type, bool is_mutable, char *error_buf,
  780. uint32 error_buf_size)
  781. {
  782. WASMModuleCommon *module_reg;
  783. WASMGlobal *global = NULL;
  784. WASMExport *export = NULL;
  785. WASMModule *module = NULL;
  786. module_reg = wasm_runtime_find_module_registered(module_name);
  787. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  788. LOG_DEBUG("can not find a module named %s for global", module_name);
  789. set_error_buf(error_buf, error_buf_size, "unknown import");
  790. return NULL;
  791. }
  792. module = (WASMModule *)module_reg;
  793. export =
  794. wasm_loader_find_export(module, module_name, global_name,
  795. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  796. if (!export) {
  797. return NULL;
  798. }
  799. /* resolve and check the global */
  800. if (export->index < module->import_global_count) {
  801. global =
  802. module->import_globals[export->index].u.global.import_global_linked;
  803. }
  804. else {
  805. global =
  806. &(module->globals[export->index - module->import_global_count]);
  807. }
  808. if (global->type != type || global->is_mutable != is_mutable) {
  809. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  810. module_name, global_name, global->type, global->is_mutable,
  811. type, is_mutable);
  812. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  813. return NULL;
  814. }
  815. return global;
  816. }
  817. static WASMModule *
  818. search_sub_module(const WASMModule *parent_module, const char *sub_module_name)
  819. {
  820. WASMRegisteredModule *node =
  821. bh_list_first_elem(parent_module->import_module_list);
  822. while (node && strcmp(sub_module_name, node->module_name)) {
  823. node = bh_list_elem_next(node);
  824. }
  825. return node ? (WASMModule *)node->module : NULL;
  826. }
  827. static bool
  828. register_sub_module(const WASMModule *parent_module,
  829. const char *sub_module_name, WASMModule *sub_module)
  830. {
  831. /* register sub_module into its parent sub module list */
  832. WASMRegisteredModule *node = NULL;
  833. bh_list_status ret;
  834. if (search_sub_module(parent_module, sub_module_name)) {
  835. LOG_DEBUG("%s has been registered in its parent", sub_module_name);
  836. return true;
  837. }
  838. node = loader_malloc(sizeof(WASMRegisteredModule), NULL, 0);
  839. if (!node) {
  840. return false;
  841. }
  842. node->module_name = sub_module_name;
  843. node->module = (WASMModuleCommon *)sub_module;
  844. ret = bh_list_insert(parent_module->import_module_list, node);
  845. bh_assert(BH_LIST_SUCCESS == ret);
  846. (void)ret;
  847. return true;
  848. }
  849. static WASMModule *
  850. load_depended_module(const WASMModule *parent_module,
  851. const char *sub_module_name, char *error_buf,
  852. uint32 error_buf_size)
  853. {
  854. WASMModule *sub_module = NULL;
  855. bool ret = false;
  856. uint8 *buffer = NULL;
  857. uint32 buffer_size = 0;
  858. const module_reader reader = wasm_runtime_get_module_reader();
  859. const module_destroyer destroyer = wasm_runtime_get_module_destroyer();
  860. /* check the registered module list of the parent */
  861. sub_module = search_sub_module(parent_module, sub_module_name);
  862. if (sub_module) {
  863. LOG_DEBUG("%s has been loaded before", sub_module_name);
  864. return sub_module;
  865. }
  866. /* check the global registered module list */
  867. sub_module =
  868. (WASMModule *)wasm_runtime_find_module_registered(sub_module_name);
  869. if (sub_module) {
  870. LOG_DEBUG("%s has been loaded", sub_module_name);
  871. goto register_sub_module;
  872. }
  873. LOG_VERBOSE("loading %s", sub_module_name);
  874. if (!reader) {
  875. set_error_buf_v(error_buf, error_buf_size,
  876. "no sub module reader to load %s", sub_module_name);
  877. return NULL;
  878. }
  879. /* start to maintain a loading module list */
  880. ret = wasm_runtime_is_loading_module(sub_module_name);
  881. if (ret) {
  882. set_error_buf_v(error_buf, error_buf_size,
  883. "found circular dependency on %s", sub_module_name);
  884. return NULL;
  885. }
  886. ret = wasm_runtime_add_loading_module(sub_module_name, error_buf,
  887. error_buf_size);
  888. if (!ret) {
  889. LOG_DEBUG("can not add %s into loading module list\n", sub_module_name);
  890. return NULL;
  891. }
  892. ret = reader(sub_module_name, &buffer, &buffer_size);
  893. if (!ret) {
  894. LOG_DEBUG("read the file of %s failed", sub_module_name);
  895. set_error_buf_v(error_buf, error_buf_size, "unknown import",
  896. sub_module_name);
  897. goto delete_loading_module;
  898. }
  899. sub_module =
  900. wasm_loader_load(buffer, buffer_size, false, error_buf, error_buf_size);
  901. if (!sub_module) {
  902. LOG_DEBUG("error: can not load the sub_module %s", sub_module_name);
  903. /* others will be destroyed in runtime_destroy() */
  904. goto destroy_file_buffer;
  905. }
  906. wasm_runtime_delete_loading_module(sub_module_name);
  907. /* register on a global list */
  908. ret = wasm_runtime_register_module_internal(
  909. sub_module_name, (WASMModuleCommon *)sub_module, buffer, buffer_size,
  910. error_buf, error_buf_size);
  911. if (!ret) {
  912. LOG_DEBUG("error: can not register module %s globally\n",
  913. sub_module_name);
  914. /* others will be unloaded in runtime_destroy() */
  915. goto unload_module;
  916. }
  917. /* register into its parent list */
  918. register_sub_module:
  919. ret = register_sub_module(parent_module, sub_module_name, sub_module);
  920. if (!ret) {
  921. set_error_buf_v(error_buf, error_buf_size,
  922. "failed to register sub module %s", sub_module_name);
  923. /* since it is in the global module list, no need to
  924. * unload the module. the runtime_destroy() will do it
  925. */
  926. return NULL;
  927. }
  928. return sub_module;
  929. unload_module:
  930. wasm_loader_unload(sub_module);
  931. destroy_file_buffer:
  932. if (destroyer) {
  933. destroyer(buffer, buffer_size);
  934. }
  935. else {
  936. LOG_WARNING("need to release the reading buffer of %s manually",
  937. sub_module_name);
  938. }
  939. delete_loading_module:
  940. wasm_runtime_delete_loading_module(sub_module_name);
  941. return NULL;
  942. }
  943. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  944. static bool
  945. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  946. const WASMModule *parent_module,
  947. const char *sub_module_name, const char *function_name,
  948. WASMFunctionImport *function, char *error_buf,
  949. uint32 error_buf_size)
  950. {
  951. const uint8 *p = *p_buf, *p_end = buf_end;
  952. uint32 declare_type_index = 0;
  953. WASMType *declare_func_type = NULL;
  954. WASMFunction *linked_func = NULL;
  955. #if WASM_ENABLE_MULTI_MODULE != 0
  956. WASMModule *sub_module = NULL;
  957. #endif
  958. const char *linked_signature = NULL;
  959. void *linked_attachment = NULL;
  960. bool linked_call_conv_raw = false;
  961. bool is_native_symbol = false;
  962. read_leb_uint32(p, p_end, declare_type_index);
  963. *p_buf = p;
  964. if (declare_type_index >= parent_module->type_count) {
  965. set_error_buf(error_buf, error_buf_size, "unknown type");
  966. return false;
  967. }
  968. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  969. declare_type_index = wasm_get_smallest_type_idx(
  970. parent_module->types, parent_module->type_count, declare_type_index);
  971. #endif
  972. declare_func_type = parent_module->types[declare_type_index];
  973. /* lookup registered native symbols first */
  974. linked_func = wasm_native_resolve_symbol(
  975. sub_module_name, function_name, declare_func_type, &linked_signature,
  976. &linked_attachment, &linked_call_conv_raw);
  977. if (linked_func) {
  978. is_native_symbol = true;
  979. }
  980. #if WASM_ENABLE_MULTI_MODULE != 0
  981. else {
  982. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  983. sub_module = load_depended_module(parent_module, sub_module_name,
  984. error_buf, error_buf_size);
  985. if (!sub_module) {
  986. return false;
  987. }
  988. }
  989. linked_func = wasm_loader_resolve_function(
  990. sub_module_name, function_name, declare_func_type, error_buf,
  991. error_buf_size);
  992. }
  993. #endif
  994. function->module_name = (char *)sub_module_name;
  995. function->field_name = (char *)function_name;
  996. function->func_type = declare_func_type;
  997. /* func_ptr_linked is for native registered symbol */
  998. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  999. function->signature = linked_signature;
  1000. function->attachment = linked_attachment;
  1001. function->call_conv_raw = linked_call_conv_raw;
  1002. #if WASM_ENABLE_MULTI_MODULE != 0
  1003. function->import_module = is_native_symbol ? NULL : sub_module;
  1004. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  1005. #endif
  1006. return true;
  1007. fail:
  1008. return false;
  1009. }
  1010. static bool
  1011. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1012. uint32 error_buf_size)
  1013. {
  1014. if (max_size < init_size) {
  1015. set_error_buf(error_buf, error_buf_size,
  1016. "size minimum must not be greater than maximum");
  1017. return false;
  1018. }
  1019. return true;
  1020. }
  1021. static bool
  1022. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  1023. WASMModule *parent_module, const char *sub_module_name,
  1024. const char *table_name, WASMTableImport *table,
  1025. char *error_buf, uint32 error_buf_size)
  1026. {
  1027. const uint8 *p = *p_buf, *p_end = buf_end;
  1028. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  1029. declare_init_size = 0, declare_max_size = 0;
  1030. #if WASM_ENABLE_MULTI_MODULE != 0
  1031. WASMModule *sub_module = NULL;
  1032. WASMTable *linked_table = NULL;
  1033. #endif
  1034. CHECK_BUF(p, p_end, 1);
  1035. /* 0x70 or 0x6F */
  1036. declare_elem_type = read_uint8(p);
  1037. if (VALUE_TYPE_FUNCREF != declare_elem_type
  1038. #if WASM_ENABLE_REF_TYPES != 0
  1039. && VALUE_TYPE_EXTERNREF != declare_elem_type
  1040. #endif
  1041. ) {
  1042. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1043. return false;
  1044. }
  1045. read_leb_uint32(p, p_end, declare_max_size_flag);
  1046. if (declare_max_size_flag > 1) {
  1047. set_error_buf(error_buf, error_buf_size, "integer too large");
  1048. return false;
  1049. }
  1050. read_leb_uint32(p, p_end, declare_init_size);
  1051. if (declare_max_size_flag) {
  1052. read_leb_uint32(p, p_end, declare_max_size);
  1053. if (!check_table_max_size(declare_init_size, declare_max_size,
  1054. error_buf, error_buf_size))
  1055. return false;
  1056. }
  1057. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  1058. &declare_max_size);
  1059. *p_buf = p;
  1060. #if WASM_ENABLE_MULTI_MODULE != 0
  1061. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1062. sub_module = load_depended_module(parent_module, sub_module_name,
  1063. error_buf, error_buf_size);
  1064. if (!sub_module) {
  1065. return false;
  1066. }
  1067. linked_table = wasm_loader_resolve_table(
  1068. sub_module_name, table_name, declare_init_size, declare_max_size,
  1069. error_buf, error_buf_size);
  1070. if (!linked_table) {
  1071. return false;
  1072. }
  1073. /* reset with linked table limit */
  1074. declare_elem_type = linked_table->elem_type;
  1075. declare_init_size = linked_table->init_size;
  1076. declare_max_size = linked_table->max_size;
  1077. declare_max_size_flag = linked_table->flags;
  1078. table->import_table_linked = linked_table;
  1079. table->import_module = sub_module;
  1080. }
  1081. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  1082. /* (table (export "table") 10 20 funcref) */
  1083. /* we need this section working in wamrc */
  1084. if (!strcmp("spectest", sub_module_name)) {
  1085. const uint32 spectest_table_init_size = 10;
  1086. const uint32 spectest_table_max_size = 20;
  1087. if (strcmp("table", table_name)) {
  1088. set_error_buf(error_buf, error_buf_size,
  1089. "incompatible import type or unknown import");
  1090. return false;
  1091. }
  1092. if (declare_init_size > spectest_table_init_size
  1093. || declare_max_size < spectest_table_max_size) {
  1094. set_error_buf(error_buf, error_buf_size,
  1095. "incompatible import type");
  1096. return false;
  1097. }
  1098. declare_init_size = spectest_table_init_size;
  1099. declare_max_size = spectest_table_max_size;
  1100. }
  1101. /* now we believe all declaration are ok */
  1102. table->elem_type = declare_elem_type;
  1103. table->init_size = declare_init_size;
  1104. table->flags = declare_max_size_flag;
  1105. table->max_size = declare_max_size;
  1106. (void)parent_module;
  1107. return true;
  1108. fail:
  1109. return false;
  1110. }
  1111. static bool
  1112. check_memory_init_size(uint32 init_size, char *error_buf, uint32 error_buf_size)
  1113. {
  1114. if (init_size > DEFAULT_MAX_PAGES) {
  1115. set_error_buf(error_buf, error_buf_size,
  1116. "memory size must be at most 65536 pages (4GiB)");
  1117. return false;
  1118. }
  1119. return true;
  1120. }
  1121. static bool
  1122. check_memory_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  1123. uint32 error_buf_size)
  1124. {
  1125. if (max_size < init_size) {
  1126. set_error_buf(error_buf, error_buf_size,
  1127. "size minimum must not be greater than maximum");
  1128. return false;
  1129. }
  1130. if (max_size > DEFAULT_MAX_PAGES) {
  1131. set_error_buf(error_buf, error_buf_size,
  1132. "memory size must be at most 65536 pages (4GiB)");
  1133. return false;
  1134. }
  1135. return true;
  1136. }
  1137. static bool
  1138. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  1139. WASMModule *parent_module, const char *sub_module_name,
  1140. const char *memory_name, WASMMemoryImport *memory,
  1141. char *error_buf, uint32 error_buf_size)
  1142. {
  1143. const uint8 *p = *p_buf, *p_end = buf_end;
  1144. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1145. uint32 pool_size = wasm_runtime_memory_pool_size();
  1146. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1147. / DEFAULT_NUM_BYTES_PER_PAGE;
  1148. #else
  1149. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1150. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  1151. uint32 declare_max_page_count_flag = 0;
  1152. uint32 declare_init_page_count = 0;
  1153. uint32 declare_max_page_count = 0;
  1154. #if WASM_ENABLE_MULTI_MODULE != 0
  1155. WASMModule *sub_module = NULL;
  1156. WASMMemory *linked_memory = NULL;
  1157. #endif
  1158. read_leb_uint32(p, p_end, declare_max_page_count_flag);
  1159. read_leb_uint32(p, p_end, declare_init_page_count);
  1160. if (!check_memory_init_size(declare_init_page_count, error_buf,
  1161. error_buf_size)) {
  1162. return false;
  1163. }
  1164. if (declare_max_page_count_flag & 1) {
  1165. read_leb_uint32(p, p_end, declare_max_page_count);
  1166. if (!check_memory_max_size(declare_init_page_count,
  1167. declare_max_page_count, error_buf,
  1168. error_buf_size)) {
  1169. return false;
  1170. }
  1171. if (declare_max_page_count > max_page_count) {
  1172. declare_max_page_count = max_page_count;
  1173. }
  1174. }
  1175. else {
  1176. /* Limit the maximum memory size to max_page_count */
  1177. declare_max_page_count = max_page_count;
  1178. }
  1179. #if WASM_ENABLE_MULTI_MODULE != 0
  1180. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  1181. sub_module = load_depended_module(parent_module, sub_module_name,
  1182. error_buf, error_buf_size);
  1183. if (!sub_module) {
  1184. return false;
  1185. }
  1186. linked_memory = wasm_loader_resolve_memory(
  1187. sub_module_name, memory_name, declare_init_page_count,
  1188. declare_max_page_count, error_buf, error_buf_size);
  1189. if (!linked_memory) {
  1190. return false;
  1191. }
  1192. /**
  1193. * reset with linked memory limit
  1194. */
  1195. memory->import_module = sub_module;
  1196. memory->import_memory_linked = linked_memory;
  1197. declare_init_page_count = linked_memory->init_page_count;
  1198. declare_max_page_count = linked_memory->max_page_count;
  1199. }
  1200. #endif
  1201. /* (memory (export "memory") 1 2) */
  1202. if (!strcmp("spectest", sub_module_name)) {
  1203. uint32 spectest_memory_init_page = 1;
  1204. uint32 spectest_memory_max_page = 2;
  1205. if (strcmp("memory", memory_name)) {
  1206. set_error_buf(error_buf, error_buf_size,
  1207. "incompatible import type or unknown import");
  1208. return false;
  1209. }
  1210. if (declare_init_page_count > spectest_memory_init_page
  1211. || declare_max_page_count < spectest_memory_max_page) {
  1212. set_error_buf(error_buf, error_buf_size,
  1213. "incompatible import type");
  1214. return false;
  1215. }
  1216. declare_init_page_count = spectest_memory_init_page;
  1217. declare_max_page_count = spectest_memory_max_page;
  1218. }
  1219. /* now we believe all declaration are ok */
  1220. memory->flags = declare_max_page_count_flag;
  1221. memory->init_page_count = declare_init_page_count;
  1222. memory->max_page_count = declare_max_page_count;
  1223. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1224. *p_buf = p;
  1225. (void)parent_module;
  1226. return true;
  1227. fail:
  1228. return false;
  1229. }
  1230. static bool
  1231. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  1232. const WASMModule *parent_module, char *sub_module_name,
  1233. char *global_name, WASMGlobalImport *global, char *error_buf,
  1234. uint32 error_buf_size)
  1235. {
  1236. const uint8 *p = *p_buf, *p_end = buf_end;
  1237. uint8 declare_type = 0;
  1238. uint8 declare_mutable = 0;
  1239. #if WASM_ENABLE_MULTI_MODULE != 0
  1240. WASMModule *sub_module = NULL;
  1241. WASMGlobal *linked_global = NULL;
  1242. #endif
  1243. bool ret = false;
  1244. CHECK_BUF(p, p_end, 2);
  1245. declare_type = read_uint8(p);
  1246. declare_mutable = read_uint8(p);
  1247. *p_buf = p;
  1248. if (declare_mutable >= 2) {
  1249. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1250. return false;
  1251. }
  1252. #if WASM_ENABLE_LIBC_BUILTIN != 0
  1253. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  1254. global);
  1255. if (ret) {
  1256. if (global->type != declare_type
  1257. || global->is_mutable != declare_mutable) {
  1258. set_error_buf(error_buf, error_buf_size,
  1259. "incompatible import type");
  1260. return false;
  1261. }
  1262. global->is_linked = true;
  1263. }
  1264. #endif
  1265. #if WASM_ENABLE_MULTI_MODULE != 0
  1266. if (!global->is_linked
  1267. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  1268. sub_module = load_depended_module(parent_module, sub_module_name,
  1269. error_buf, error_buf_size);
  1270. if (!sub_module) {
  1271. return false;
  1272. }
  1273. /* check sub modules */
  1274. linked_global = wasm_loader_resolve_global(
  1275. sub_module_name, global_name, declare_type, declare_mutable,
  1276. error_buf, error_buf_size);
  1277. if (linked_global) {
  1278. global->import_module = sub_module;
  1279. global->import_global_linked = linked_global;
  1280. global->is_linked = true;
  1281. }
  1282. }
  1283. #endif
  1284. global->module_name = sub_module_name;
  1285. global->field_name = global_name;
  1286. global->type = declare_type;
  1287. global->is_mutable = (declare_mutable == 1);
  1288. (void)parent_module;
  1289. (void)ret;
  1290. return true;
  1291. fail:
  1292. return false;
  1293. }
  1294. static bool
  1295. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  1296. char *error_buf, uint32 error_buf_size)
  1297. {
  1298. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1299. CHECK_BUF(p, p_end, 1);
  1300. /* 0x70 or 0x6F */
  1301. table->elem_type = read_uint8(p);
  1302. if (VALUE_TYPE_FUNCREF != table->elem_type
  1303. #if WASM_ENABLE_REF_TYPES != 0
  1304. && VALUE_TYPE_EXTERNREF != table->elem_type
  1305. #endif
  1306. ) {
  1307. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  1308. return false;
  1309. }
  1310. p_org = p;
  1311. read_leb_uint32(p, p_end, table->flags);
  1312. #if WASM_ENABLE_SHARED_MEMORY == 0
  1313. if (p - p_org > 1) {
  1314. set_error_buf(error_buf, error_buf_size,
  1315. "integer representation too long");
  1316. return false;
  1317. }
  1318. if (table->flags > 1) {
  1319. set_error_buf(error_buf, error_buf_size, "integer too large");
  1320. return false;
  1321. }
  1322. #else
  1323. if (p - p_org > 1) {
  1324. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1325. return false;
  1326. }
  1327. if (table->flags == 2) {
  1328. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  1329. return false;
  1330. }
  1331. if (table->flags > 1) {
  1332. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1333. return false;
  1334. }
  1335. #endif
  1336. read_leb_uint32(p, p_end, table->init_size);
  1337. if (table->flags) {
  1338. read_leb_uint32(p, p_end, table->max_size);
  1339. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  1340. error_buf_size))
  1341. return false;
  1342. }
  1343. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  1344. *p_buf = p;
  1345. return true;
  1346. fail:
  1347. return false;
  1348. }
  1349. static bool
  1350. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  1351. char *error_buf, uint32 error_buf_size)
  1352. {
  1353. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1354. #if WASM_ENABLE_APP_FRAMEWORK != 0
  1355. uint32 pool_size = wasm_runtime_memory_pool_size();
  1356. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  1357. / DEFAULT_NUM_BYTES_PER_PAGE;
  1358. #else
  1359. uint32 max_page_count = DEFAULT_MAX_PAGES;
  1360. #endif
  1361. p_org = p;
  1362. read_leb_uint32(p, p_end, memory->flags);
  1363. #if WASM_ENABLE_SHARED_MEMORY == 0
  1364. if (p - p_org > 1) {
  1365. set_error_buf(error_buf, error_buf_size,
  1366. "integer representation too long");
  1367. return false;
  1368. }
  1369. if (memory->flags > 1) {
  1370. set_error_buf(error_buf, error_buf_size, "integer too large");
  1371. return false;
  1372. }
  1373. #else
  1374. if (p - p_org > 1) {
  1375. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1376. return false;
  1377. }
  1378. if (memory->flags > 3) {
  1379. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  1380. return false;
  1381. }
  1382. else if (memory->flags == 2) {
  1383. set_error_buf(error_buf, error_buf_size,
  1384. "shared memory must have maximum");
  1385. return false;
  1386. }
  1387. #endif
  1388. read_leb_uint32(p, p_end, memory->init_page_count);
  1389. if (!check_memory_init_size(memory->init_page_count, error_buf,
  1390. error_buf_size))
  1391. return false;
  1392. if (memory->flags & 1) {
  1393. read_leb_uint32(p, p_end, memory->max_page_count);
  1394. if (!check_memory_max_size(memory->init_page_count,
  1395. memory->max_page_count, error_buf,
  1396. error_buf_size))
  1397. return false;
  1398. if (memory->max_page_count > max_page_count)
  1399. memory->max_page_count = max_page_count;
  1400. }
  1401. else {
  1402. /* Limit the maximum memory size to max_page_count */
  1403. memory->max_page_count = max_page_count;
  1404. }
  1405. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  1406. *p_buf = p;
  1407. return true;
  1408. fail:
  1409. return false;
  1410. }
  1411. static bool
  1412. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1413. bool is_load_from_file_buf, char *error_buf,
  1414. uint32 error_buf_size)
  1415. {
  1416. const uint8 *p = buf, *p_end = buf_end, *p_old;
  1417. uint32 import_count, name_len, type_index, i, u32, flags;
  1418. uint64 total_size;
  1419. WASMImport *import;
  1420. WASMImport *import_functions = NULL, *import_tables = NULL;
  1421. WASMImport *import_memories = NULL, *import_globals = NULL;
  1422. char *sub_module_name, *field_name;
  1423. uint8 u8, kind;
  1424. read_leb_uint32(p, p_end, import_count);
  1425. if (import_count) {
  1426. module->import_count = import_count;
  1427. total_size = sizeof(WASMImport) * (uint64)import_count;
  1428. if (!(module->imports =
  1429. loader_malloc(total_size, error_buf, error_buf_size))) {
  1430. return false;
  1431. }
  1432. p_old = p;
  1433. /* Scan firstly to get import count of each type */
  1434. for (i = 0; i < import_count; i++) {
  1435. /* module name */
  1436. read_leb_uint32(p, p_end, name_len);
  1437. CHECK_BUF(p, p_end, name_len);
  1438. p += name_len;
  1439. /* field name */
  1440. read_leb_uint32(p, p_end, name_len);
  1441. CHECK_BUF(p, p_end, name_len);
  1442. p += name_len;
  1443. CHECK_BUF(p, p_end, 1);
  1444. /* 0x00/0x01/0x02/0x03 */
  1445. kind = read_uint8(p);
  1446. switch (kind) {
  1447. case IMPORT_KIND_FUNC: /* import function */
  1448. read_leb_uint32(p, p_end, type_index);
  1449. module->import_function_count++;
  1450. break;
  1451. case IMPORT_KIND_TABLE: /* import table */
  1452. CHECK_BUF(p, p_end, 1);
  1453. /* 0x70 */
  1454. u8 = read_uint8(p);
  1455. read_leb_uint32(p, p_end, flags);
  1456. read_leb_uint32(p, p_end, u32);
  1457. if (flags & 1)
  1458. read_leb_uint32(p, p_end, u32);
  1459. module->import_table_count++;
  1460. #if WASM_ENABLE_REF_TYPES == 0
  1461. if (module->import_table_count > 1) {
  1462. set_error_buf(error_buf, error_buf_size,
  1463. "multiple tables");
  1464. return false;
  1465. }
  1466. #endif
  1467. break;
  1468. case IMPORT_KIND_MEMORY: /* import memory */
  1469. read_leb_uint32(p, p_end, flags);
  1470. read_leb_uint32(p, p_end, u32);
  1471. if (flags & 1)
  1472. read_leb_uint32(p, p_end, u32);
  1473. module->import_memory_count++;
  1474. if (module->import_memory_count > 1) {
  1475. set_error_buf(error_buf, error_buf_size,
  1476. "multiple memories");
  1477. return false;
  1478. }
  1479. break;
  1480. case IMPORT_KIND_GLOBAL: /* import global */
  1481. CHECK_BUF(p, p_end, 2);
  1482. p += 2;
  1483. module->import_global_count++;
  1484. break;
  1485. default:
  1486. set_error_buf(error_buf, error_buf_size,
  1487. "invalid import kind");
  1488. return false;
  1489. }
  1490. }
  1491. if (module->import_function_count)
  1492. import_functions = module->import_functions = module->imports;
  1493. if (module->import_table_count)
  1494. import_tables = module->import_tables =
  1495. module->imports + module->import_function_count;
  1496. if (module->import_memory_count)
  1497. import_memories = module->import_memories =
  1498. module->imports + module->import_function_count
  1499. + module->import_table_count;
  1500. if (module->import_global_count)
  1501. import_globals = module->import_globals =
  1502. module->imports + module->import_function_count
  1503. + module->import_table_count + module->import_memory_count;
  1504. p = p_old;
  1505. /* Scan again to resolve the data */
  1506. for (i = 0; i < import_count; i++) {
  1507. /* load module name */
  1508. read_leb_uint32(p, p_end, name_len);
  1509. CHECK_BUF(p, p_end, name_len);
  1510. if (!(sub_module_name = const_str_list_insert(
  1511. p, name_len, module, is_load_from_file_buf, error_buf,
  1512. error_buf_size))) {
  1513. return false;
  1514. }
  1515. p += name_len;
  1516. /* load field name */
  1517. read_leb_uint32(p, p_end, name_len);
  1518. CHECK_BUF(p, p_end, name_len);
  1519. if (!(field_name = const_str_list_insert(
  1520. p, name_len, module, is_load_from_file_buf, error_buf,
  1521. error_buf_size))) {
  1522. return false;
  1523. }
  1524. p += name_len;
  1525. CHECK_BUF(p, p_end, 1);
  1526. /* 0x00/0x01/0x02/0x03 */
  1527. kind = read_uint8(p);
  1528. switch (kind) {
  1529. case IMPORT_KIND_FUNC: /* import function */
  1530. bh_assert(import_functions);
  1531. import = import_functions++;
  1532. if (!load_function_import(
  1533. &p, p_end, module, sub_module_name, field_name,
  1534. &import->u.function, error_buf, error_buf_size)) {
  1535. return false;
  1536. }
  1537. break;
  1538. case IMPORT_KIND_TABLE: /* import table */
  1539. bh_assert(import_tables);
  1540. import = import_tables++;
  1541. if (!load_table_import(&p, p_end, module, sub_module_name,
  1542. field_name, &import->u.table,
  1543. error_buf, error_buf_size)) {
  1544. LOG_DEBUG("can not import such a table (%s,%s)",
  1545. sub_module_name, field_name);
  1546. return false;
  1547. }
  1548. break;
  1549. case IMPORT_KIND_MEMORY: /* import memory */
  1550. bh_assert(import_memories);
  1551. import = import_memories++;
  1552. if (!load_memory_import(&p, p_end, module, sub_module_name,
  1553. field_name, &import->u.memory,
  1554. error_buf, error_buf_size)) {
  1555. return false;
  1556. }
  1557. break;
  1558. case IMPORT_KIND_GLOBAL: /* import global */
  1559. bh_assert(import_globals);
  1560. import = import_globals++;
  1561. if (!load_global_import(&p, p_end, module, sub_module_name,
  1562. field_name, &import->u.global,
  1563. error_buf, error_buf_size)) {
  1564. return false;
  1565. }
  1566. break;
  1567. default:
  1568. set_error_buf(error_buf, error_buf_size,
  1569. "invalid import kind");
  1570. return false;
  1571. }
  1572. import->kind = kind;
  1573. import->u.names.module_name = sub_module_name;
  1574. import->u.names.field_name = field_name;
  1575. }
  1576. #if WASM_ENABLE_LIBC_WASI != 0
  1577. import = module->import_functions;
  1578. for (i = 0; i < module->import_function_count; i++, import++) {
  1579. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  1580. || !strcmp(import->u.names.module_name,
  1581. "wasi_snapshot_preview1")) {
  1582. module->import_wasi_api = true;
  1583. break;
  1584. }
  1585. }
  1586. #endif
  1587. }
  1588. if (p != p_end) {
  1589. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1590. return false;
  1591. }
  1592. LOG_VERBOSE("Load import section success.\n");
  1593. (void)u8;
  1594. (void)u32;
  1595. (void)type_index;
  1596. return true;
  1597. fail:
  1598. return false;
  1599. }
  1600. static bool
  1601. init_function_local_offsets(WASMFunction *func, char *error_buf,
  1602. uint32 error_buf_size)
  1603. {
  1604. WASMType *param_type = func->func_type;
  1605. uint32 param_count = param_type->param_count;
  1606. uint8 *param_types = param_type->types;
  1607. uint32 local_count = func->local_count;
  1608. uint8 *local_types = func->local_types;
  1609. uint32 i, local_offset = 0;
  1610. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  1611. /*
  1612. * Only allocate memory when total_size is not 0,
  1613. * or the return value of malloc(0) might be NULL on some platforms,
  1614. * which causes wasm loader return false.
  1615. */
  1616. if (total_size > 0
  1617. && !(func->local_offsets =
  1618. loader_malloc(total_size, error_buf, error_buf_size))) {
  1619. return false;
  1620. }
  1621. for (i = 0; i < param_count; i++) {
  1622. func->local_offsets[i] = (uint16)local_offset;
  1623. local_offset += wasm_value_type_cell_num(param_types[i]);
  1624. }
  1625. for (i = 0; i < local_count; i++) {
  1626. func->local_offsets[param_count + i] = (uint16)local_offset;
  1627. local_offset += wasm_value_type_cell_num(local_types[i]);
  1628. }
  1629. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  1630. return true;
  1631. }
  1632. static bool
  1633. load_function_section(const uint8 *buf, const uint8 *buf_end,
  1634. const uint8 *buf_code, const uint8 *buf_code_end,
  1635. WASMModule *module, char *error_buf,
  1636. uint32 error_buf_size)
  1637. {
  1638. const uint8 *p = buf, *p_end = buf_end;
  1639. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  1640. uint32 func_count;
  1641. uint64 total_size;
  1642. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  1643. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  1644. uint8 type;
  1645. WASMFunction *func;
  1646. read_leb_uint32(p, p_end, func_count);
  1647. if (buf_code)
  1648. read_leb_uint32(p_code, buf_code_end, code_count);
  1649. if (func_count != code_count) {
  1650. set_error_buf(error_buf, error_buf_size,
  1651. "function and code section have inconsistent lengths or "
  1652. "unexpected end");
  1653. return false;
  1654. }
  1655. if (func_count) {
  1656. module->function_count = func_count;
  1657. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  1658. if (!(module->functions =
  1659. loader_malloc(total_size, error_buf, error_buf_size))) {
  1660. return false;
  1661. }
  1662. for (i = 0; i < func_count; i++) {
  1663. /* Resolve function type */
  1664. read_leb_uint32(p, p_end, type_index);
  1665. if (type_index >= module->type_count) {
  1666. set_error_buf(error_buf, error_buf_size, "unknown type");
  1667. return false;
  1668. }
  1669. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1670. type_index = wasm_get_smallest_type_idx(
  1671. module->types, module->type_count, type_index);
  1672. #endif
  1673. read_leb_uint32(p_code, buf_code_end, code_size);
  1674. if (code_size == 0 || p_code + code_size > buf_code_end) {
  1675. set_error_buf(error_buf, error_buf_size,
  1676. "invalid function code size");
  1677. return false;
  1678. }
  1679. /* Resolve local set count */
  1680. p_code_end = p_code + code_size;
  1681. local_count = 0;
  1682. read_leb_uint32(p_code, buf_code_end, local_set_count);
  1683. p_code_save = p_code;
  1684. /* Calculate total local count */
  1685. for (j = 0; j < local_set_count; j++) {
  1686. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1687. if (sub_local_count > UINT32_MAX - local_count) {
  1688. set_error_buf(error_buf, error_buf_size, "too many locals");
  1689. return false;
  1690. }
  1691. CHECK_BUF(p_code, buf_code_end, 1);
  1692. /* 0x7F/0x7E/0x7D/0x7C */
  1693. type = read_uint8(p_code);
  1694. local_count += sub_local_count;
  1695. }
  1696. /* Alloc memory, layout: function structure + local types */
  1697. code_size = (uint32)(p_code_end - p_code);
  1698. total_size = sizeof(WASMFunction) + (uint64)local_count;
  1699. if (!(func = module->functions[i] =
  1700. loader_malloc(total_size, error_buf, error_buf_size))) {
  1701. return false;
  1702. }
  1703. /* Set function type, local count, code size and code body */
  1704. func->func_type = module->types[type_index];
  1705. func->local_count = local_count;
  1706. if (local_count > 0)
  1707. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  1708. func->code_size = code_size;
  1709. /*
  1710. * we shall make a copy of code body [p_code, p_code + code_size]
  1711. * when we are worrying about inappropriate releasing behaviour.
  1712. * all code bodies are actually in a buffer which user allocates in
  1713. * his embedding environment and we don't have power on them.
  1714. * it will be like:
  1715. * code_body_cp = malloc(code_size);
  1716. * memcpy(code_body_cp, p_code, code_size);
  1717. * func->code = code_body_cp;
  1718. */
  1719. func->code = (uint8 *)p_code;
  1720. /* Load each local type */
  1721. p_code = p_code_save;
  1722. local_type_index = 0;
  1723. for (j = 0; j < local_set_count; j++) {
  1724. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  1725. /* Note: sub_local_count is allowed to be 0 */
  1726. if (local_type_index > UINT32_MAX - sub_local_count
  1727. || local_type_index + sub_local_count > local_count) {
  1728. set_error_buf(error_buf, error_buf_size,
  1729. "invalid local count");
  1730. return false;
  1731. }
  1732. CHECK_BUF(p_code, buf_code_end, 1);
  1733. /* 0x7F/0x7E/0x7D/0x7C */
  1734. type = read_uint8(p_code);
  1735. if (!is_value_type(type)) {
  1736. if (type == VALUE_TYPE_V128)
  1737. set_error_buf(error_buf, error_buf_size,
  1738. "v128 value type requires simd feature");
  1739. else if (type == VALUE_TYPE_FUNCREF
  1740. || type == VALUE_TYPE_EXTERNREF)
  1741. set_error_buf(error_buf, error_buf_size,
  1742. "ref value type requires "
  1743. "reference types feature");
  1744. else
  1745. set_error_buf_v(error_buf, error_buf_size,
  1746. "invalid local type 0x%02X", type);
  1747. return false;
  1748. }
  1749. for (k = 0; k < sub_local_count; k++) {
  1750. func->local_types[local_type_index++] = type;
  1751. }
  1752. }
  1753. func->param_cell_num = func->func_type->param_cell_num;
  1754. func->ret_cell_num = func->func_type->ret_cell_num;
  1755. local_cell_num =
  1756. wasm_get_cell_num(func->local_types, func->local_count);
  1757. if (local_cell_num > UINT16_MAX) {
  1758. set_error_buf(error_buf, error_buf_size,
  1759. "local count too large");
  1760. return false;
  1761. }
  1762. func->local_cell_num = (uint16)local_cell_num;
  1763. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  1764. return false;
  1765. p_code = p_code_end;
  1766. }
  1767. }
  1768. if (p != p_end) {
  1769. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1770. return false;
  1771. }
  1772. LOG_VERBOSE("Load function section success.\n");
  1773. return true;
  1774. fail:
  1775. return false;
  1776. }
  1777. static bool
  1778. check_function_index(const WASMModule *module, uint32 function_index,
  1779. char *error_buf, uint32 error_buf_size)
  1780. {
  1781. if (function_index
  1782. >= module->import_function_count + module->function_count) {
  1783. set_error_buf_v(error_buf, error_buf_size, "unknown function %d",
  1784. function_index);
  1785. return false;
  1786. }
  1787. return true;
  1788. }
  1789. static bool
  1790. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1791. char *error_buf, uint32 error_buf_size)
  1792. {
  1793. const uint8 *p = buf, *p_end = buf_end;
  1794. uint32 table_count, i;
  1795. uint64 total_size;
  1796. WASMTable *table;
  1797. read_leb_uint32(p, p_end, table_count);
  1798. #if WASM_ENABLE_REF_TYPES == 0
  1799. if (module->import_table_count + table_count > 1) {
  1800. /* a total of one table is allowed */
  1801. set_error_buf(error_buf, error_buf_size, "multiple tables");
  1802. return false;
  1803. }
  1804. #endif
  1805. if (table_count) {
  1806. module->table_count = table_count;
  1807. total_size = sizeof(WASMTable) * (uint64)table_count;
  1808. if (!(module->tables =
  1809. loader_malloc(total_size, error_buf, error_buf_size))) {
  1810. return false;
  1811. }
  1812. /* load each table */
  1813. table = module->tables;
  1814. for (i = 0; i < table_count; i++, table++)
  1815. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  1816. return false;
  1817. }
  1818. if (p != p_end) {
  1819. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1820. return false;
  1821. }
  1822. LOG_VERBOSE("Load table section success.\n");
  1823. return true;
  1824. fail:
  1825. return false;
  1826. }
  1827. static bool
  1828. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1829. char *error_buf, uint32 error_buf_size)
  1830. {
  1831. const uint8 *p = buf, *p_end = buf_end;
  1832. uint32 memory_count, i;
  1833. uint64 total_size;
  1834. WASMMemory *memory;
  1835. read_leb_uint32(p, p_end, memory_count);
  1836. /* a total of one memory is allowed */
  1837. if (module->import_memory_count + memory_count > 1) {
  1838. set_error_buf(error_buf, error_buf_size, "multiple memories");
  1839. return false;
  1840. }
  1841. if (memory_count) {
  1842. module->memory_count = memory_count;
  1843. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  1844. if (!(module->memories =
  1845. loader_malloc(total_size, error_buf, error_buf_size))) {
  1846. return false;
  1847. }
  1848. /* load each memory */
  1849. memory = module->memories;
  1850. for (i = 0; i < memory_count; i++, memory++)
  1851. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  1852. return false;
  1853. }
  1854. if (p != p_end) {
  1855. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1856. return false;
  1857. }
  1858. LOG_VERBOSE("Load memory section success.\n");
  1859. return true;
  1860. fail:
  1861. return false;
  1862. }
  1863. static bool
  1864. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1865. char *error_buf, uint32 error_buf_size)
  1866. {
  1867. const uint8 *p = buf, *p_end = buf_end;
  1868. uint32 global_count, i;
  1869. uint64 total_size;
  1870. WASMGlobal *global;
  1871. uint8 mutable;
  1872. read_leb_uint32(p, p_end, global_count);
  1873. if (global_count) {
  1874. module->global_count = global_count;
  1875. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  1876. if (!(module->globals =
  1877. loader_malloc(total_size, error_buf, error_buf_size))) {
  1878. return false;
  1879. }
  1880. global = module->globals;
  1881. for (i = 0; i < global_count; i++, global++) {
  1882. CHECK_BUF(p, p_end, 2);
  1883. global->type = read_uint8(p);
  1884. mutable = read_uint8(p);
  1885. if (mutable >= 2) {
  1886. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1887. return false;
  1888. }
  1889. global->is_mutable = mutable ? true : false;
  1890. /* initialize expression */
  1891. if (!load_init_expr(&p, p_end, &(global->init_expr), global->type,
  1892. error_buf, error_buf_size))
  1893. return false;
  1894. if (INIT_EXPR_TYPE_GET_GLOBAL == global->init_expr.init_expr_type) {
  1895. /**
  1896. * Currently, constant expressions occurring as initializers
  1897. * of globals are further constrained in that contained
  1898. * global.get instructions are
  1899. * only allowed to refer to imported globals.
  1900. */
  1901. uint32 target_global_index = global->init_expr.u.global_index;
  1902. if (target_global_index >= module->import_global_count) {
  1903. set_error_buf(error_buf, error_buf_size, "unknown global");
  1904. return false;
  1905. }
  1906. }
  1907. else if (INIT_EXPR_TYPE_FUNCREF_CONST
  1908. == global->init_expr.init_expr_type) {
  1909. if (!check_function_index(module, global->init_expr.u.ref_index,
  1910. error_buf, error_buf_size)) {
  1911. return false;
  1912. }
  1913. }
  1914. }
  1915. }
  1916. if (p != p_end) {
  1917. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1918. return false;
  1919. }
  1920. LOG_VERBOSE("Load global section success.\n");
  1921. return true;
  1922. fail:
  1923. return false;
  1924. }
  1925. static bool
  1926. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1927. bool is_load_from_file_buf, char *error_buf,
  1928. uint32 error_buf_size)
  1929. {
  1930. const uint8 *p = buf, *p_end = buf_end;
  1931. uint32 export_count, i, j, index;
  1932. uint64 total_size;
  1933. uint32 str_len;
  1934. WASMExport *export;
  1935. const char *name;
  1936. read_leb_uint32(p, p_end, export_count);
  1937. if (export_count) {
  1938. module->export_count = export_count;
  1939. total_size = sizeof(WASMExport) * (uint64)export_count;
  1940. if (!(module->exports =
  1941. loader_malloc(total_size, error_buf, error_buf_size))) {
  1942. return false;
  1943. }
  1944. export = module->exports;
  1945. for (i = 0; i < export_count; i++, export ++) {
  1946. #if WASM_ENABLE_THREAD_MGR == 0
  1947. if (p == p_end) {
  1948. /* export section with inconsistent count:
  1949. n export declared, but less than n given */
  1950. set_error_buf(error_buf, error_buf_size,
  1951. "length out of bounds");
  1952. return false;
  1953. }
  1954. #endif
  1955. read_leb_uint32(p, p_end, str_len);
  1956. CHECK_BUF(p, p_end, str_len);
  1957. for (j = 0; j < i; j++) {
  1958. name = module->exports[j].name;
  1959. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  1960. set_error_buf(error_buf, error_buf_size,
  1961. "duplicate export name");
  1962. return false;
  1963. }
  1964. }
  1965. if (!(export->name = const_str_list_insert(
  1966. p, str_len, module, is_load_from_file_buf, error_buf,
  1967. error_buf_size))) {
  1968. return false;
  1969. }
  1970. p += str_len;
  1971. CHECK_BUF(p, p_end, 1);
  1972. export->kind = read_uint8(p);
  1973. read_leb_uint32(p, p_end, index);
  1974. export->index = index;
  1975. switch (export->kind) {
  1976. /* function index */
  1977. case EXPORT_KIND_FUNC:
  1978. if (index >= module->function_count
  1979. + module->import_function_count) {
  1980. set_error_buf(error_buf, error_buf_size,
  1981. "unknown function");
  1982. return false;
  1983. }
  1984. #if WASM_ENABLE_SIMD != 0
  1985. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  1986. /* TODO: check func type, if it has v128 param or result,
  1987. report error */
  1988. #endif
  1989. #endif
  1990. break;
  1991. /* table index */
  1992. case EXPORT_KIND_TABLE:
  1993. if (index
  1994. >= module->table_count + module->import_table_count) {
  1995. set_error_buf(error_buf, error_buf_size,
  1996. "unknown table");
  1997. return false;
  1998. }
  1999. break;
  2000. /* memory index */
  2001. case EXPORT_KIND_MEMORY:
  2002. if (index
  2003. >= module->memory_count + module->import_memory_count) {
  2004. set_error_buf(error_buf, error_buf_size,
  2005. "unknown memory");
  2006. return false;
  2007. }
  2008. break;
  2009. /* global index */
  2010. case EXPORT_KIND_GLOBAL:
  2011. if (index
  2012. >= module->global_count + module->import_global_count) {
  2013. set_error_buf(error_buf, error_buf_size,
  2014. "unknown global");
  2015. return false;
  2016. }
  2017. break;
  2018. default:
  2019. set_error_buf(error_buf, error_buf_size,
  2020. "invalid export kind");
  2021. return false;
  2022. }
  2023. }
  2024. }
  2025. if (p != p_end) {
  2026. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2027. return false;
  2028. }
  2029. LOG_VERBOSE("Load export section success.\n");
  2030. return true;
  2031. fail:
  2032. return false;
  2033. }
  2034. static bool
  2035. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  2036. uint32 error_buf_size)
  2037. {
  2038. #if WASM_ENABLE_REF_TYPES == 0
  2039. if (table_index != 0) {
  2040. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  2041. return false;
  2042. }
  2043. #endif
  2044. if (table_index >= module->import_table_count + module->table_count) {
  2045. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  2046. table_index);
  2047. return false;
  2048. }
  2049. return true;
  2050. }
  2051. static bool
  2052. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2053. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  2054. {
  2055. const uint8 *p = *p_buf, *p_end = buf_end;
  2056. uint32 table_index;
  2057. read_leb_uint32(p, p_end, table_index);
  2058. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  2059. return false;
  2060. }
  2061. *p_table_index = table_index;
  2062. *p_buf = p;
  2063. return true;
  2064. fail:
  2065. return false;
  2066. }
  2067. #if WASM_ENABLE_REF_TYPES != 0
  2068. static bool
  2069. load_elem_type(const uint8 **p_buf, const uint8 *buf_end, uint32 *p_elem_type,
  2070. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  2071. {
  2072. const uint8 *p = *p_buf, *p_end = buf_end;
  2073. uint8 elem_type;
  2074. CHECK_BUF(p, p_end, 1);
  2075. elem_type = read_uint8(p);
  2076. if ((elemkind_zero && elem_type != 0)
  2077. || (!elemkind_zero && elem_type != VALUE_TYPE_FUNCREF
  2078. && elem_type != VALUE_TYPE_EXTERNREF)) {
  2079. set_error_buf(error_buf, error_buf_size, "invalid reference type");
  2080. return false;
  2081. }
  2082. if (elemkind_zero)
  2083. *p_elem_type = VALUE_TYPE_FUNCREF;
  2084. else
  2085. *p_elem_type = elem_type;
  2086. *p_buf = p;
  2087. return true;
  2088. fail:
  2089. return false;
  2090. }
  2091. #endif /* WASM_ENABLE_REF_TYPES != 0*/
  2092. static bool
  2093. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  2094. WASMModule *module, WASMTableSeg *table_segment,
  2095. bool use_init_expr, char *error_buf, uint32 error_buf_size)
  2096. {
  2097. const uint8 *p = *p_buf, *p_end = buf_end;
  2098. uint32 function_count, function_index = 0, i;
  2099. uint64 total_size;
  2100. read_leb_uint32(p, p_end, function_count);
  2101. table_segment->function_count = function_count;
  2102. total_size = sizeof(uint32) * (uint64)function_count;
  2103. if (total_size > 0
  2104. && !(table_segment->func_indexes = (uint32 *)loader_malloc(
  2105. total_size, error_buf, error_buf_size))) {
  2106. return false;
  2107. }
  2108. for (i = 0; i < function_count; i++) {
  2109. InitializerExpression init_expr = { 0 };
  2110. #if WASM_ENABLE_REF_TYPES != 0
  2111. if (!use_init_expr) {
  2112. read_leb_uint32(p, p_end, function_index);
  2113. }
  2114. else {
  2115. if (!load_init_expr(&p, p_end, &init_expr, table_segment->elem_type,
  2116. error_buf, error_buf_size))
  2117. return false;
  2118. function_index = init_expr.u.ref_index;
  2119. }
  2120. #else
  2121. read_leb_uint32(p, p_end, function_index);
  2122. (void)use_init_expr;
  2123. #endif
  2124. /* since we are using -1 to indicate ref.null */
  2125. if (init_expr.init_expr_type != INIT_EXPR_TYPE_REFNULL_CONST
  2126. && !check_function_index(module, function_index, error_buf,
  2127. error_buf_size)) {
  2128. return false;
  2129. }
  2130. table_segment->func_indexes[i] = function_index;
  2131. }
  2132. *p_buf = p;
  2133. return true;
  2134. fail:
  2135. return false;
  2136. }
  2137. static bool
  2138. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  2139. WASMModule *module, char *error_buf,
  2140. uint32 error_buf_size)
  2141. {
  2142. const uint8 *p = buf, *p_end = buf_end;
  2143. uint32 table_segment_count, i;
  2144. uint64 total_size;
  2145. WASMTableSeg *table_segment;
  2146. read_leb_uint32(p, p_end, table_segment_count);
  2147. if (table_segment_count) {
  2148. module->table_seg_count = table_segment_count;
  2149. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  2150. if (!(module->table_segments =
  2151. loader_malloc(total_size, error_buf, error_buf_size))) {
  2152. return false;
  2153. }
  2154. table_segment = module->table_segments;
  2155. for (i = 0; i < table_segment_count; i++, table_segment++) {
  2156. if (p >= p_end) {
  2157. set_error_buf(error_buf, error_buf_size,
  2158. "invalid value type or "
  2159. "invalid elements segment kind");
  2160. return false;
  2161. }
  2162. #if WASM_ENABLE_REF_TYPES != 0
  2163. read_leb_uint32(p, p_end, table_segment->mode);
  2164. /* last three bits */
  2165. table_segment->mode = table_segment->mode & 0x07;
  2166. switch (table_segment->mode) {
  2167. /* elemkind/elemtype + active */
  2168. case 0:
  2169. case 4:
  2170. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  2171. table_segment->table_index = 0;
  2172. if (!check_table_index(module, table_segment->table_index,
  2173. error_buf, error_buf_size))
  2174. return false;
  2175. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2176. VALUE_TYPE_I32, error_buf,
  2177. error_buf_size))
  2178. return false;
  2179. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2180. table_segment->mode == 0 ? false
  2181. : true,
  2182. error_buf, error_buf_size))
  2183. return false;
  2184. break;
  2185. /* elemkind + passive/declarative */
  2186. case 1:
  2187. case 3:
  2188. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2189. true, error_buf, error_buf_size))
  2190. return false;
  2191. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2192. false, error_buf, error_buf_size))
  2193. return false;
  2194. break;
  2195. /* elemkind/elemtype + table_idx + active */
  2196. case 2:
  2197. case 6:
  2198. if (!load_table_index(&p, p_end, module,
  2199. &table_segment->table_index,
  2200. error_buf, error_buf_size))
  2201. return false;
  2202. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2203. VALUE_TYPE_I32, error_buf,
  2204. error_buf_size))
  2205. return false;
  2206. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2207. table_segment->mode == 2 ? true : false,
  2208. error_buf, error_buf_size))
  2209. return false;
  2210. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2211. table_segment->mode == 2 ? false
  2212. : true,
  2213. error_buf, error_buf_size))
  2214. return false;
  2215. break;
  2216. case 5:
  2217. case 7:
  2218. if (!load_elem_type(&p, p_end, &table_segment->elem_type,
  2219. false, error_buf, error_buf_size))
  2220. return false;
  2221. if (!load_func_index_vec(&p, p_end, module, table_segment,
  2222. true, error_buf, error_buf_size))
  2223. return false;
  2224. break;
  2225. default:
  2226. set_error_buf(error_buf, error_buf_size,
  2227. "unknown element segment kind");
  2228. return false;
  2229. }
  2230. #else
  2231. /*
  2232. * like: 00 41 05 0b 04 00 01 00 01
  2233. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  2234. */
  2235. if (!load_table_index(&p, p_end, module,
  2236. &table_segment->table_index, error_buf,
  2237. error_buf_size))
  2238. return false;
  2239. if (!load_init_expr(&p, p_end, &table_segment->base_offset,
  2240. VALUE_TYPE_I32, error_buf, error_buf_size))
  2241. return false;
  2242. if (!load_func_index_vec(&p, p_end, module, table_segment, false,
  2243. error_buf, error_buf_size))
  2244. return false;
  2245. #endif /* WASM_ENABLE_REF_TYPES != 0 */
  2246. }
  2247. }
  2248. if (p != p_end) {
  2249. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2250. return false;
  2251. }
  2252. LOG_VERBOSE("Load table segment section success.\n");
  2253. return true;
  2254. fail:
  2255. return false;
  2256. }
  2257. static bool
  2258. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  2259. WASMModule *module, char *error_buf,
  2260. uint32 error_buf_size)
  2261. {
  2262. const uint8 *p = buf, *p_end = buf_end;
  2263. uint32 data_seg_count, i, mem_index, data_seg_len;
  2264. uint64 total_size;
  2265. WASMDataSeg *dataseg;
  2266. InitializerExpression init_expr;
  2267. #if WASM_ENABLE_BULK_MEMORY != 0
  2268. bool is_passive = false;
  2269. uint32 mem_flag;
  2270. #endif
  2271. read_leb_uint32(p, p_end, data_seg_count);
  2272. #if WASM_ENABLE_BULK_MEMORY != 0
  2273. if ((module->data_seg_count1 != 0)
  2274. && (data_seg_count != module->data_seg_count1)) {
  2275. set_error_buf(error_buf, error_buf_size,
  2276. "data count and data section have inconsistent lengths");
  2277. return false;
  2278. }
  2279. #endif
  2280. if (data_seg_count) {
  2281. module->data_seg_count = data_seg_count;
  2282. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  2283. if (!(module->data_segments =
  2284. loader_malloc(total_size, error_buf, error_buf_size))) {
  2285. return false;
  2286. }
  2287. for (i = 0; i < data_seg_count; i++) {
  2288. read_leb_uint32(p, p_end, mem_index);
  2289. #if WASM_ENABLE_BULK_MEMORY != 0
  2290. is_passive = false;
  2291. mem_flag = mem_index & 0x03;
  2292. switch (mem_flag) {
  2293. case 0x01:
  2294. is_passive = true;
  2295. break;
  2296. case 0x00:
  2297. /* no memory index, treat index as 0 */
  2298. mem_index = 0;
  2299. goto check_mem_index;
  2300. case 0x02:
  2301. /* read following memory index */
  2302. read_leb_uint32(p, p_end, mem_index);
  2303. check_mem_index:
  2304. if (mem_index
  2305. >= module->import_memory_count + module->memory_count) {
  2306. set_error_buf_v(error_buf, error_buf_size,
  2307. "unknown memory %d", mem_index);
  2308. return false;
  2309. }
  2310. break;
  2311. case 0x03:
  2312. default:
  2313. set_error_buf(error_buf, error_buf_size, "unknown memory");
  2314. return false;
  2315. break;
  2316. }
  2317. #else
  2318. if (mem_index
  2319. >= module->import_memory_count + module->memory_count) {
  2320. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  2321. mem_index);
  2322. return false;
  2323. }
  2324. #endif /* WASM_ENABLE_BULK_MEMORY */
  2325. #if WASM_ENABLE_BULK_MEMORY != 0
  2326. if (!is_passive)
  2327. #endif
  2328. if (!load_init_expr(&p, p_end, &init_expr, VALUE_TYPE_I32,
  2329. error_buf, error_buf_size))
  2330. return false;
  2331. read_leb_uint32(p, p_end, data_seg_len);
  2332. if (!(dataseg = module->data_segments[i] = loader_malloc(
  2333. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  2334. return false;
  2335. }
  2336. #if WASM_ENABLE_BULK_MEMORY != 0
  2337. dataseg->is_passive = is_passive;
  2338. if (!is_passive)
  2339. #endif
  2340. {
  2341. bh_memcpy_s(&dataseg->base_offset,
  2342. sizeof(InitializerExpression), &init_expr,
  2343. sizeof(InitializerExpression));
  2344. dataseg->memory_index = mem_index;
  2345. }
  2346. dataseg->data_length = data_seg_len;
  2347. CHECK_BUF(p, p_end, data_seg_len);
  2348. dataseg->data = (uint8 *)p;
  2349. p += data_seg_len;
  2350. }
  2351. }
  2352. if (p != p_end) {
  2353. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2354. return false;
  2355. }
  2356. LOG_VERBOSE("Load data segment section success.\n");
  2357. return true;
  2358. fail:
  2359. return false;
  2360. }
  2361. #if WASM_ENABLE_BULK_MEMORY != 0
  2362. static bool
  2363. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  2364. WASMModule *module, char *error_buf,
  2365. uint32 error_buf_size)
  2366. {
  2367. const uint8 *p = buf, *p_end = buf_end;
  2368. uint32 data_seg_count1 = 0;
  2369. read_leb_uint32(p, p_end, data_seg_count1);
  2370. module->data_seg_count1 = data_seg_count1;
  2371. if (p != p_end) {
  2372. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2373. return false;
  2374. }
  2375. LOG_VERBOSE("Load datacount section success.\n");
  2376. return true;
  2377. fail:
  2378. return false;
  2379. }
  2380. #endif
  2381. static bool
  2382. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  2383. const uint8 *buf_func_end, WASMModule *module,
  2384. char *error_buf, uint32 error_buf_size)
  2385. {
  2386. const uint8 *p = buf, *p_end = buf_end;
  2387. const uint8 *p_func = buf_func;
  2388. uint32 func_count = 0, code_count;
  2389. /* code has been loaded in function section, so pass it here, just check
  2390. * whether function and code section have inconsistent lengths */
  2391. read_leb_uint32(p, p_end, code_count);
  2392. if (buf_func)
  2393. read_leb_uint32(p_func, buf_func_end, func_count);
  2394. if (func_count != code_count) {
  2395. set_error_buf(error_buf, error_buf_size,
  2396. "function and code section have inconsistent lengths");
  2397. return false;
  2398. }
  2399. LOG_VERBOSE("Load code segment section success.\n");
  2400. (void)module;
  2401. return true;
  2402. fail:
  2403. return false;
  2404. }
  2405. static bool
  2406. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2407. char *error_buf, uint32 error_buf_size)
  2408. {
  2409. const uint8 *p = buf, *p_end = buf_end;
  2410. WASMType *type;
  2411. uint32 start_function;
  2412. read_leb_uint32(p, p_end, start_function);
  2413. if (start_function
  2414. >= module->function_count + module->import_function_count) {
  2415. set_error_buf(error_buf, error_buf_size, "unknown function");
  2416. return false;
  2417. }
  2418. if (start_function < module->import_function_count)
  2419. type = module->import_functions[start_function].u.function.func_type;
  2420. else
  2421. type = module->functions[start_function - module->import_function_count]
  2422. ->func_type;
  2423. if (type->param_count != 0 || type->result_count != 0) {
  2424. set_error_buf(error_buf, error_buf_size, "invalid start function");
  2425. return false;
  2426. }
  2427. module->start_function = start_function;
  2428. if (p != p_end) {
  2429. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2430. return false;
  2431. }
  2432. LOG_VERBOSE("Load start section success.\n");
  2433. return true;
  2434. fail:
  2435. return false;
  2436. }
  2437. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2438. static bool
  2439. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2440. bool is_load_from_file_buf, char *error_buf,
  2441. uint32 error_buf_size)
  2442. {
  2443. const uint8 *p = buf, *p_end = buf_end;
  2444. uint32 name_type, subsection_size;
  2445. uint32 previous_name_type = 0;
  2446. uint32 num_func_name;
  2447. uint32 func_index;
  2448. uint32 previous_func_index = ~0U;
  2449. uint32 func_name_len;
  2450. uint32 name_index;
  2451. int i = 0;
  2452. if (p >= p_end) {
  2453. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2454. return false;
  2455. }
  2456. while (p < p_end) {
  2457. read_leb_uint32(p, p_end, name_type);
  2458. if (i != 0) {
  2459. if (name_type == previous_name_type) {
  2460. set_error_buf(error_buf, error_buf_size,
  2461. "duplicate sub-section");
  2462. return false;
  2463. }
  2464. if (name_type < previous_name_type) {
  2465. set_error_buf(error_buf, error_buf_size,
  2466. "out-of-order sub-section");
  2467. return false;
  2468. }
  2469. }
  2470. previous_name_type = name_type;
  2471. read_leb_uint32(p, p_end, subsection_size);
  2472. CHECK_BUF(p, p_end, subsection_size);
  2473. switch (name_type) {
  2474. case SUB_SECTION_TYPE_FUNC:
  2475. if (subsection_size) {
  2476. read_leb_uint32(p, p_end, num_func_name);
  2477. for (name_index = 0; name_index < num_func_name;
  2478. name_index++) {
  2479. read_leb_uint32(p, p_end, func_index);
  2480. if (func_index == previous_func_index) {
  2481. set_error_buf(error_buf, error_buf_size,
  2482. "duplicate function name");
  2483. return false;
  2484. }
  2485. if (func_index < previous_func_index
  2486. && previous_func_index != ~0U) {
  2487. set_error_buf(error_buf, error_buf_size,
  2488. "out-of-order function index ");
  2489. return false;
  2490. }
  2491. previous_func_index = func_index;
  2492. read_leb_uint32(p, p_end, func_name_len);
  2493. CHECK_BUF(p, p_end, func_name_len);
  2494. /* Skip the import functions */
  2495. if (func_index >= module->import_function_count) {
  2496. func_index -= module->import_function_count;
  2497. if (func_index >= module->function_count) {
  2498. set_error_buf(error_buf, error_buf_size,
  2499. "out-of-range function index");
  2500. return false;
  2501. }
  2502. if (!(module->functions[func_index]->field_name =
  2503. const_str_list_insert(
  2504. p, func_name_len, module,
  2505. is_load_from_file_buf, error_buf,
  2506. error_buf_size))) {
  2507. return false;
  2508. }
  2509. }
  2510. p += func_name_len;
  2511. }
  2512. }
  2513. break;
  2514. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  2515. */
  2516. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  2517. default:
  2518. p = p + subsection_size;
  2519. break;
  2520. }
  2521. i++;
  2522. }
  2523. return true;
  2524. fail:
  2525. return false;
  2526. }
  2527. #endif
  2528. static bool
  2529. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2530. bool is_load_from_file_buf, char *error_buf,
  2531. uint32 error_buf_size)
  2532. {
  2533. const uint8 *p = buf, *p_end = buf_end;
  2534. char section_name[32];
  2535. uint32 name_len, buffer_len;
  2536. if (p >= p_end) {
  2537. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2538. return false;
  2539. }
  2540. read_leb_uint32(p, p_end, name_len);
  2541. if (name_len == 0 || p + name_len > p_end) {
  2542. set_error_buf(error_buf, error_buf_size, "unexpected end");
  2543. return false;
  2544. }
  2545. if (!check_utf8_str(p, name_len)) {
  2546. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  2547. return false;
  2548. }
  2549. buffer_len = sizeof(section_name);
  2550. memset(section_name, 0, buffer_len);
  2551. if (name_len < buffer_len) {
  2552. bh_memcpy_s(section_name, buffer_len, p, name_len);
  2553. }
  2554. else {
  2555. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  2556. memset(section_name + buffer_len - 4, '.', 3);
  2557. }
  2558. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  2559. if (memcmp(p, "name", 4) == 0) {
  2560. module->name_section_buf = buf;
  2561. module->name_section_buf_end = buf_end;
  2562. p += name_len;
  2563. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  2564. error_buf_size);
  2565. LOG_VERBOSE("Load custom name section success.");
  2566. return true;
  2567. }
  2568. #endif
  2569. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  2570. {
  2571. WASMCustomSection *section =
  2572. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  2573. if (!section) {
  2574. return false;
  2575. }
  2576. section->name_addr = (char *)p;
  2577. section->name_len = name_len;
  2578. section->content_addr = (uint8 *)(p + name_len);
  2579. section->content_len = (uint32)(p_end - p - name_len);
  2580. section->next = module->custom_section_list;
  2581. module->custom_section_list = section;
  2582. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  2583. return true;
  2584. }
  2585. #endif
  2586. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  2587. (void)is_load_from_file_buf;
  2588. (void)module;
  2589. return true;
  2590. fail:
  2591. return false;
  2592. }
  2593. static void
  2594. calculate_global_data_offset(WASMModule *module)
  2595. {
  2596. uint32 i, data_offset;
  2597. data_offset = 0;
  2598. for (i = 0; i < module->import_global_count; i++) {
  2599. WASMGlobalImport *import_global =
  2600. &((module->import_globals + i)->u.global);
  2601. #if WASM_ENABLE_FAST_JIT != 0
  2602. import_global->data_offset = data_offset;
  2603. #endif
  2604. data_offset += wasm_value_type_size(import_global->type);
  2605. }
  2606. for (i = 0; i < module->global_count; i++) {
  2607. WASMGlobal *global = module->globals + i;
  2608. #if WASM_ENABLE_FAST_JIT != 0
  2609. global->data_offset = data_offset;
  2610. #endif
  2611. data_offset += wasm_value_type_size(global->type);
  2612. }
  2613. module->global_data_size = data_offset;
  2614. }
  2615. #if WASM_ENABLE_FAST_JIT != 0
  2616. static bool
  2617. init_fast_jit_functions(WASMModule *module, char *error_buf,
  2618. uint32 error_buf_size)
  2619. {
  2620. #if WASM_ENABLE_LAZY_JIT != 0
  2621. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  2622. #endif
  2623. uint32 i;
  2624. if (!module->function_count)
  2625. return true;
  2626. if (!(module->fast_jit_func_ptrs =
  2627. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  2628. error_buf_size))) {
  2629. return false;
  2630. }
  2631. #if WASM_ENABLE_LAZY_JIT != 0
  2632. for (i = 0; i < module->function_count; i++) {
  2633. module->fast_jit_func_ptrs[i] =
  2634. jit_globals->compile_fast_jit_and_then_call;
  2635. }
  2636. #endif
  2637. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  2638. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  2639. set_error_buf(error_buf, error_buf_size,
  2640. "init fast jit thread lock failed");
  2641. return false;
  2642. }
  2643. module->fast_jit_thread_locks_inited[i] = true;
  2644. }
  2645. return true;
  2646. }
  2647. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  2648. #if WASM_ENABLE_JIT != 0
  2649. static bool
  2650. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  2651. uint32 error_buf_size)
  2652. {
  2653. LLVMJITOptions llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  2654. AOTCompOption option = { 0 };
  2655. char *aot_last_error;
  2656. uint64 size;
  2657. if (module->function_count == 0)
  2658. return true;
  2659. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2660. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  2661. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  2662. return false;
  2663. }
  2664. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  2665. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  2666. os_mutex_destroy(&module->tierup_wait_lock);
  2667. return false;
  2668. }
  2669. module->tierup_wait_lock_inited = true;
  2670. #endif
  2671. size = sizeof(void *) * (uint64)module->function_count
  2672. + sizeof(bool) * (uint64)module->function_count;
  2673. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  2674. return false;
  2675. }
  2676. module->func_ptrs_compiled =
  2677. (bool *)((uint8 *)module->func_ptrs
  2678. + sizeof(void *) * module->function_count);
  2679. module->comp_data = aot_create_comp_data(module);
  2680. if (!module->comp_data) {
  2681. aot_last_error = aot_get_last_error();
  2682. bh_assert(aot_last_error != NULL);
  2683. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2684. return false;
  2685. }
  2686. option.is_jit_mode = true;
  2687. llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  2688. option.opt_level = llvm_jit_options.opt_level;
  2689. option.size_level = llvm_jit_options.size_level;
  2690. option.segue_flags = llvm_jit_options.segue_flags;
  2691. #if WASM_ENABLE_BULK_MEMORY != 0
  2692. option.enable_bulk_memory = true;
  2693. #endif
  2694. #if WASM_ENABLE_THREAD_MGR != 0
  2695. option.enable_thread_mgr = true;
  2696. #endif
  2697. #if WASM_ENABLE_TAIL_CALL != 0
  2698. option.enable_tail_call = true;
  2699. #endif
  2700. #if WASM_ENABLE_SIMD != 0
  2701. option.enable_simd = true;
  2702. #endif
  2703. #if WASM_ENABLE_REF_TYPES != 0
  2704. option.enable_ref_types = true;
  2705. #endif
  2706. option.enable_aux_stack_check = true;
  2707. #if (WASM_ENABLE_PERF_PROFILING != 0) || (WASM_ENABLE_DUMP_CALL_STACK != 0)
  2708. option.enable_aux_stack_frame = true;
  2709. #endif
  2710. #if WASM_ENABLE_MEMORY_PROFILING != 0
  2711. option.enable_stack_estimation = true;
  2712. #endif
  2713. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  2714. if (!module->comp_ctx) {
  2715. aot_last_error = aot_get_last_error();
  2716. bh_assert(aot_last_error != NULL);
  2717. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2718. return false;
  2719. }
  2720. return true;
  2721. }
  2722. static bool
  2723. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  2724. uint32 error_buf_size)
  2725. {
  2726. char *aot_last_error;
  2727. uint32 i;
  2728. if (module->function_count == 0)
  2729. return true;
  2730. if (!aot_compile_wasm(module->comp_ctx)) {
  2731. aot_last_error = aot_get_last_error();
  2732. bh_assert(aot_last_error != NULL);
  2733. set_error_buf(error_buf, error_buf_size, aot_last_error);
  2734. return false;
  2735. }
  2736. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2737. if (module->orcjit_stop_compiling)
  2738. return false;
  2739. #endif
  2740. bh_print_time("Begin to lookup llvm jit functions");
  2741. for (i = 0; i < module->function_count; i++) {
  2742. LLVMOrcJITTargetAddress func_addr = 0;
  2743. LLVMErrorRef error;
  2744. char func_name[48];
  2745. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  2746. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  2747. func_name);
  2748. if (error != LLVMErrorSuccess) {
  2749. char *err_msg = LLVMGetErrorMessage(error);
  2750. set_error_buf_v(error_buf, error_buf_size,
  2751. "failed to compile llvm jit function: %s", err_msg);
  2752. LLVMDisposeErrorMessage(err_msg);
  2753. return false;
  2754. }
  2755. /**
  2756. * No need to lock the func_ptr[func_idx] here as it is basic
  2757. * data type, the load/store for it can be finished by one cpu
  2758. * instruction, and there can be only one cpu instruction
  2759. * loading/storing at the same time.
  2760. */
  2761. module->func_ptrs[i] = (void *)func_addr;
  2762. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2763. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  2764. if (module->orcjit_stop_compiling)
  2765. return false;
  2766. #endif
  2767. }
  2768. bh_print_time("End lookup llvm jit functions");
  2769. return true;
  2770. }
  2771. #endif /* end of WASM_ENABLE_JIT != 0 */
  2772. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2773. && WASM_ENABLE_LAZY_JIT != 0
  2774. static void *
  2775. init_llvm_jit_functions_stage2_callback(void *arg)
  2776. {
  2777. WASMModule *module = (WASMModule *)arg;
  2778. char error_buf[128];
  2779. uint32 error_buf_size = (uint32)sizeof(error_buf);
  2780. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  2781. module->orcjit_stop_compiling = true;
  2782. return NULL;
  2783. }
  2784. os_mutex_lock(&module->tierup_wait_lock);
  2785. module->llvm_jit_inited = true;
  2786. os_cond_broadcast(&module->tierup_wait_cond);
  2787. os_mutex_unlock(&module->tierup_wait_lock);
  2788. return NULL;
  2789. }
  2790. #endif
  2791. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  2792. /* The callback function to compile jit functions */
  2793. static void *
  2794. orcjit_thread_callback(void *arg)
  2795. {
  2796. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  2797. #if WASM_ENABLE_JIT != 0
  2798. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  2799. #endif
  2800. WASMModule *module = thread_arg->module;
  2801. uint32 group_idx = thread_arg->group_idx;
  2802. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  2803. uint32 func_count = module->function_count;
  2804. uint32 i;
  2805. #if WASM_ENABLE_FAST_JIT != 0
  2806. /* Compile fast jit funcitons of this group */
  2807. for (i = group_idx; i < func_count; i += group_stride) {
  2808. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  2809. os_printf("failed to compile fast jit function %u\n", i);
  2810. break;
  2811. }
  2812. if (module->orcjit_stop_compiling) {
  2813. return NULL;
  2814. }
  2815. }
  2816. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2817. os_mutex_lock(&module->tierup_wait_lock);
  2818. module->fast_jit_ready_groups++;
  2819. os_mutex_unlock(&module->tierup_wait_lock);
  2820. #endif
  2821. #endif
  2822. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  2823. && WASM_ENABLE_LAZY_JIT != 0
  2824. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  2825. for (i = group_idx; i < func_count;
  2826. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2827. uint32 j;
  2828. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2829. if (i + j * group_stride < func_count) {
  2830. if (!jit_compiler_set_call_to_fast_jit(
  2831. module,
  2832. i + j * group_stride + module->import_function_count)) {
  2833. os_printf(
  2834. "failed to compile call_to_fast_jit for func %u\n",
  2835. i + j * group_stride + module->import_function_count);
  2836. module->orcjit_stop_compiling = true;
  2837. return NULL;
  2838. }
  2839. }
  2840. if (module->orcjit_stop_compiling) {
  2841. return NULL;
  2842. }
  2843. }
  2844. }
  2845. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  2846. fast jit functions are compiled */
  2847. os_mutex_lock(&module->tierup_wait_lock);
  2848. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  2849. && module->fast_jit_ready_groups >= group_stride)) {
  2850. os_cond_reltimedwait(&module->tierup_wait_cond,
  2851. &module->tierup_wait_lock, 10000);
  2852. if (module->orcjit_stop_compiling) {
  2853. /* init_llvm_jit_functions_stage2 failed */
  2854. os_mutex_unlock(&module->tierup_wait_lock);
  2855. return NULL;
  2856. }
  2857. }
  2858. os_mutex_unlock(&module->tierup_wait_lock);
  2859. #endif
  2860. #if WASM_ENABLE_JIT != 0
  2861. /* Compile llvm jit functions of this group */
  2862. for (i = group_idx; i < func_count;
  2863. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  2864. LLVMOrcJITTargetAddress func_addr = 0;
  2865. LLVMErrorRef error;
  2866. char func_name[48];
  2867. typedef void (*F)(void);
  2868. union {
  2869. F f;
  2870. void *v;
  2871. } u;
  2872. uint32 j;
  2873. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  2874. "_wrapper");
  2875. LOG_DEBUG("compile llvm jit func %s", func_name);
  2876. error =
  2877. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  2878. if (error != LLVMErrorSuccess) {
  2879. char *err_msg = LLVMGetErrorMessage(error);
  2880. os_printf("failed to compile llvm jit function %u: %s", i, err_msg);
  2881. LLVMDisposeErrorMessage(err_msg);
  2882. break;
  2883. }
  2884. /* Call the jit wrapper function to trigger its compilation, so as
  2885. to compile the actual jit functions, since we add the latter to
  2886. function list in the PartitionFunction callback */
  2887. u.v = (void *)func_addr;
  2888. u.f();
  2889. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  2890. if (i + j * group_stride < func_count) {
  2891. module->func_ptrs_compiled[i + j * group_stride] = true;
  2892. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  2893. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  2894. i + j * group_stride);
  2895. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  2896. func_name);
  2897. if (error != LLVMErrorSuccess) {
  2898. char *err_msg = LLVMGetErrorMessage(error);
  2899. os_printf("failed to compile llvm jit function %u: %s", i,
  2900. err_msg);
  2901. LLVMDisposeErrorMessage(err_msg);
  2902. /* Ignore current llvm jit func, as its func ptr is
  2903. previous set to call_to_fast_jit, which also works */
  2904. continue;
  2905. }
  2906. jit_compiler_set_llvm_jit_func_ptr(
  2907. module,
  2908. i + j * group_stride + module->import_function_count,
  2909. (void *)func_addr);
  2910. /* Try to switch to call this llvm jit funtion instead of
  2911. fast jit function from fast jit jitted code */
  2912. jit_compiler_set_call_to_llvm_jit(
  2913. module,
  2914. i + j * group_stride + module->import_function_count);
  2915. #endif
  2916. }
  2917. }
  2918. if (module->orcjit_stop_compiling) {
  2919. break;
  2920. }
  2921. }
  2922. #endif
  2923. return NULL;
  2924. }
  2925. static void
  2926. orcjit_stop_compile_threads(WASMModule *module)
  2927. {
  2928. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  2929. / sizeof(OrcJitThreadArg));
  2930. module->orcjit_stop_compiling = true;
  2931. for (i = 0; i < thread_num; i++) {
  2932. if (module->orcjit_threads[i])
  2933. os_thread_join(module->orcjit_threads[i], NULL);
  2934. }
  2935. }
  2936. static bool
  2937. compile_jit_functions(WASMModule *module, char *error_buf,
  2938. uint32 error_buf_size)
  2939. {
  2940. uint32 thread_num =
  2941. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  2942. uint32 i, j;
  2943. bh_print_time("Begin to compile jit functions");
  2944. /* Create threads to compile the jit functions */
  2945. for (i = 0; i < thread_num && i < module->function_count; i++) {
  2946. #if WASM_ENABLE_JIT != 0
  2947. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  2948. #endif
  2949. module->orcjit_thread_args[i].module = module;
  2950. module->orcjit_thread_args[i].group_idx = i;
  2951. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  2952. (void *)&module->orcjit_thread_args[i],
  2953. APP_THREAD_STACK_SIZE_DEFAULT)
  2954. != 0) {
  2955. set_error_buf(error_buf, error_buf_size,
  2956. "create orcjit compile thread failed");
  2957. /* Terminate the threads created */
  2958. module->orcjit_stop_compiling = true;
  2959. for (j = 0; j < i; j++) {
  2960. os_thread_join(module->orcjit_threads[j], NULL);
  2961. }
  2962. return false;
  2963. }
  2964. }
  2965. #if WASM_ENABLE_LAZY_JIT == 0
  2966. /* Wait until all jit functions are compiled for eager mode */
  2967. for (i = 0; i < thread_num; i++) {
  2968. if (module->orcjit_threads[i])
  2969. os_thread_join(module->orcjit_threads[i], NULL);
  2970. }
  2971. #if WASM_ENABLE_FAST_JIT != 0
  2972. /* Ensure all the fast-jit functions are compiled */
  2973. for (i = 0; i < module->function_count; i++) {
  2974. if (!jit_compiler_is_compiled(module,
  2975. i + module->import_function_count)) {
  2976. set_error_buf(error_buf, error_buf_size,
  2977. "failed to compile fast jit function");
  2978. return false;
  2979. }
  2980. }
  2981. #endif
  2982. #if WASM_ENABLE_JIT != 0
  2983. /* Ensure all the llvm-jit functions are compiled */
  2984. for (i = 0; i < module->function_count; i++) {
  2985. if (!module->func_ptrs_compiled[i]) {
  2986. set_error_buf(error_buf, error_buf_size,
  2987. "failed to compile llvm jit function");
  2988. return false;
  2989. }
  2990. }
  2991. #endif
  2992. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  2993. bh_print_time("End compile jit functions");
  2994. return true;
  2995. }
  2996. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  2997. static bool
  2998. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  2999. uint32 cur_func_idx, char *error_buf,
  3000. uint32 error_buf_size);
  3001. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  3002. void **
  3003. wasm_interp_get_handle_table();
  3004. static void **handle_table;
  3005. #endif
  3006. static bool
  3007. load_from_sections(WASMModule *module, WASMSection *sections,
  3008. bool is_load_from_file_buf, char *error_buf,
  3009. uint32 error_buf_size)
  3010. {
  3011. WASMExport *export;
  3012. WASMSection *section = sections;
  3013. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  3014. *buf_func = NULL, *buf_func_end = NULL;
  3015. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  3016. WASMGlobal *aux_stack_top_global = NULL, *global;
  3017. uint32 aux_data_end = (uint32)-1, aux_heap_base = (uint32)-1;
  3018. uint32 aux_stack_top = (uint32)-1, global_index, func_index, i;
  3019. uint32 aux_data_end_global_index = (uint32)-1;
  3020. uint32 aux_heap_base_global_index = (uint32)-1;
  3021. WASMType *func_type;
  3022. /* Find code and function sections if have */
  3023. while (section) {
  3024. if (section->section_type == SECTION_TYPE_CODE) {
  3025. buf_code = section->section_body;
  3026. buf_code_end = buf_code + section->section_body_size;
  3027. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  3028. module->buf_code = (uint8 *)buf_code;
  3029. module->buf_code_size = section->section_body_size;
  3030. #endif
  3031. }
  3032. else if (section->section_type == SECTION_TYPE_FUNC) {
  3033. buf_func = section->section_body;
  3034. buf_func_end = buf_func + section->section_body_size;
  3035. }
  3036. section = section->next;
  3037. }
  3038. section = sections;
  3039. while (section) {
  3040. buf = section->section_body;
  3041. buf_end = buf + section->section_body_size;
  3042. switch (section->section_type) {
  3043. case SECTION_TYPE_USER:
  3044. /* unsupported user section, ignore it. */
  3045. if (!load_user_section(buf, buf_end, module,
  3046. is_load_from_file_buf, error_buf,
  3047. error_buf_size))
  3048. return false;
  3049. break;
  3050. case SECTION_TYPE_TYPE:
  3051. if (!load_type_section(buf, buf_end, module, error_buf,
  3052. error_buf_size))
  3053. return false;
  3054. break;
  3055. case SECTION_TYPE_IMPORT:
  3056. if (!load_import_section(buf, buf_end, module,
  3057. is_load_from_file_buf, error_buf,
  3058. error_buf_size))
  3059. return false;
  3060. break;
  3061. case SECTION_TYPE_FUNC:
  3062. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  3063. module, error_buf, error_buf_size))
  3064. return false;
  3065. break;
  3066. case SECTION_TYPE_TABLE:
  3067. if (!load_table_section(buf, buf_end, module, error_buf,
  3068. error_buf_size))
  3069. return false;
  3070. break;
  3071. case SECTION_TYPE_MEMORY:
  3072. if (!load_memory_section(buf, buf_end, module, error_buf,
  3073. error_buf_size))
  3074. return false;
  3075. break;
  3076. case SECTION_TYPE_GLOBAL:
  3077. if (!load_global_section(buf, buf_end, module, error_buf,
  3078. error_buf_size))
  3079. return false;
  3080. break;
  3081. case SECTION_TYPE_EXPORT:
  3082. if (!load_export_section(buf, buf_end, module,
  3083. is_load_from_file_buf, error_buf,
  3084. error_buf_size))
  3085. return false;
  3086. break;
  3087. case SECTION_TYPE_START:
  3088. if (!load_start_section(buf, buf_end, module, error_buf,
  3089. error_buf_size))
  3090. return false;
  3091. break;
  3092. case SECTION_TYPE_ELEM:
  3093. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  3094. error_buf_size))
  3095. return false;
  3096. break;
  3097. case SECTION_TYPE_CODE:
  3098. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  3099. module, error_buf, error_buf_size))
  3100. return false;
  3101. break;
  3102. case SECTION_TYPE_DATA:
  3103. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  3104. error_buf_size))
  3105. return false;
  3106. break;
  3107. #if WASM_ENABLE_BULK_MEMORY != 0
  3108. case SECTION_TYPE_DATACOUNT:
  3109. if (!load_datacount_section(buf, buf_end, module, error_buf,
  3110. error_buf_size))
  3111. return false;
  3112. break;
  3113. #endif
  3114. default:
  3115. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3116. return false;
  3117. }
  3118. section = section->next;
  3119. }
  3120. module->aux_data_end_global_index = (uint32)-1;
  3121. module->aux_heap_base_global_index = (uint32)-1;
  3122. module->aux_stack_top_global_index = (uint32)-1;
  3123. /* Resolve auxiliary data/stack/heap info and reset memory info */
  3124. export = module->exports;
  3125. for (i = 0; i < module->export_count; i++, export ++) {
  3126. if (export->kind == EXPORT_KIND_GLOBAL) {
  3127. if (!strcmp(export->name, "__heap_base")) {
  3128. global_index = export->index - module->import_global_count;
  3129. global = module->globals + global_index;
  3130. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  3131. && global->init_expr.init_expr_type
  3132. == INIT_EXPR_TYPE_I32_CONST) {
  3133. aux_heap_base_global = global;
  3134. aux_heap_base = global->init_expr.u.i32;
  3135. aux_heap_base_global_index = export->index;
  3136. LOG_VERBOSE("Found aux __heap_base global, value: %d",
  3137. aux_heap_base);
  3138. }
  3139. }
  3140. else if (!strcmp(export->name, "__data_end")) {
  3141. global_index = export->index - module->import_global_count;
  3142. global = module->globals + global_index;
  3143. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  3144. && global->init_expr.init_expr_type
  3145. == INIT_EXPR_TYPE_I32_CONST) {
  3146. aux_data_end_global = global;
  3147. aux_data_end = global->init_expr.u.i32;
  3148. aux_data_end_global_index = export->index;
  3149. LOG_VERBOSE("Found aux __data_end global, value: %d",
  3150. aux_data_end);
  3151. aux_data_end = align_uint(aux_data_end, 16);
  3152. }
  3153. }
  3154. /* For module compiled with -pthread option, the global is:
  3155. [0] stack_top <-- 0
  3156. [1] tls_pointer
  3157. [2] tls_size
  3158. [3] data_end <-- 3
  3159. [4] global_base
  3160. [5] heap_base <-- 5
  3161. [6] dso_handle
  3162. For module compiled without -pthread option:
  3163. [0] stack_top <-- 0
  3164. [1] data_end <-- 1
  3165. [2] global_base
  3166. [3] heap_base <-- 3
  3167. [4] dso_handle
  3168. */
  3169. if (aux_data_end_global && aux_heap_base_global
  3170. && aux_data_end <= aux_heap_base) {
  3171. module->aux_data_end_global_index = aux_data_end_global_index;
  3172. module->aux_data_end = aux_data_end;
  3173. module->aux_heap_base_global_index = aux_heap_base_global_index;
  3174. module->aux_heap_base = aux_heap_base;
  3175. /* Resolve aux stack top global */
  3176. for (global_index = 0; global_index < module->global_count;
  3177. global_index++) {
  3178. global = module->globals + global_index;
  3179. if (global->is_mutable /* heap_base and data_end is
  3180. not mutable */
  3181. && global->type == VALUE_TYPE_I32
  3182. && global->init_expr.init_expr_type
  3183. == INIT_EXPR_TYPE_I32_CONST
  3184. && (uint32)global->init_expr.u.i32 <= aux_heap_base) {
  3185. aux_stack_top_global = global;
  3186. aux_stack_top = (uint32)global->init_expr.u.i32;
  3187. module->aux_stack_top_global_index =
  3188. module->import_global_count + global_index;
  3189. module->aux_stack_bottom = aux_stack_top;
  3190. module->aux_stack_size =
  3191. aux_stack_top > aux_data_end
  3192. ? aux_stack_top - aux_data_end
  3193. : aux_stack_top;
  3194. LOG_VERBOSE("Found aux stack top global, value: %d, "
  3195. "global index: %d, stack size: %d",
  3196. aux_stack_top, global_index,
  3197. module->aux_stack_size);
  3198. break;
  3199. }
  3200. }
  3201. if (!aux_stack_top_global) {
  3202. /* Auxiliary stack global isn't found, it must be unused
  3203. in the wasm app, as if it is used, the global must be
  3204. defined. Here we set it to __heap_base global and set
  3205. its size to 0. */
  3206. aux_stack_top_global = aux_heap_base_global;
  3207. aux_stack_top = aux_heap_base;
  3208. module->aux_stack_top_global_index =
  3209. module->aux_heap_base_global_index;
  3210. module->aux_stack_bottom = aux_stack_top;
  3211. module->aux_stack_size = 0;
  3212. }
  3213. break;
  3214. }
  3215. }
  3216. }
  3217. module->malloc_function = (uint32)-1;
  3218. module->free_function = (uint32)-1;
  3219. module->retain_function = (uint32)-1;
  3220. /* Resolve malloc/free function exported by wasm module */
  3221. export = module->exports;
  3222. for (i = 0; i < module->export_count; i++, export ++) {
  3223. if (export->kind == EXPORT_KIND_FUNC) {
  3224. if (!strcmp(export->name, "malloc")
  3225. && export->index >= module->import_function_count) {
  3226. func_index = export->index - module->import_function_count;
  3227. func_type = module->functions[func_index]->func_type;
  3228. if (func_type->param_count == 1 && func_type->result_count == 1
  3229. && func_type->types[0] == VALUE_TYPE_I32
  3230. && func_type->types[1] == VALUE_TYPE_I32) {
  3231. bh_assert(module->malloc_function == (uint32)-1);
  3232. module->malloc_function = export->index;
  3233. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3234. export->name, export->index);
  3235. }
  3236. }
  3237. else if (!strcmp(export->name, "__new")
  3238. && export->index >= module->import_function_count) {
  3239. /* __new && __pin for AssemblyScript */
  3240. func_index = export->index - module->import_function_count;
  3241. func_type = module->functions[func_index]->func_type;
  3242. if (func_type->param_count == 2 && func_type->result_count == 1
  3243. && func_type->types[0] == VALUE_TYPE_I32
  3244. && func_type->types[1] == VALUE_TYPE_I32
  3245. && func_type->types[2] == VALUE_TYPE_I32) {
  3246. uint32 j;
  3247. WASMExport *export_tmp;
  3248. bh_assert(module->malloc_function == (uint32)-1);
  3249. module->malloc_function = export->index;
  3250. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  3251. export->name, export->index);
  3252. /* resolve retain function.
  3253. If not found, reset malloc function index */
  3254. export_tmp = module->exports;
  3255. for (j = 0; j < module->export_count; j++, export_tmp++) {
  3256. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  3257. && (!strcmp(export_tmp->name, "__retain")
  3258. || (!strcmp(export_tmp->name, "__pin")))
  3259. && (export_tmp->index
  3260. >= module->import_function_count)) {
  3261. func_index = export_tmp->index
  3262. - module->import_function_count;
  3263. func_type =
  3264. module->functions[func_index]->func_type;
  3265. if (func_type->param_count == 1
  3266. && func_type->result_count == 1
  3267. && func_type->types[0] == VALUE_TYPE_I32
  3268. && func_type->types[1] == VALUE_TYPE_I32) {
  3269. bh_assert(module->retain_function
  3270. == (uint32)-1);
  3271. module->retain_function = export_tmp->index;
  3272. LOG_VERBOSE("Found retain function, name: %s, "
  3273. "index: %u",
  3274. export_tmp->name,
  3275. export_tmp->index);
  3276. break;
  3277. }
  3278. }
  3279. }
  3280. if (j == module->export_count) {
  3281. module->malloc_function = (uint32)-1;
  3282. LOG_VERBOSE("Can't find retain function,"
  3283. "reset malloc function index to -1");
  3284. }
  3285. }
  3286. }
  3287. else if (((!strcmp(export->name, "free"))
  3288. || (!strcmp(export->name, "__release"))
  3289. || (!strcmp(export->name, "__unpin")))
  3290. && export->index >= module->import_function_count) {
  3291. func_index = export->index - module->import_function_count;
  3292. func_type = module->functions[func_index]->func_type;
  3293. if (func_type->param_count == 1 && func_type->result_count == 0
  3294. && func_type->types[0] == VALUE_TYPE_I32) {
  3295. bh_assert(module->free_function == (uint32)-1);
  3296. module->free_function = export->index;
  3297. LOG_VERBOSE("Found free function, name: %s, index: %u",
  3298. export->name, export->index);
  3299. }
  3300. }
  3301. }
  3302. }
  3303. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  3304. handle_table = wasm_interp_get_handle_table();
  3305. #endif
  3306. for (i = 0; i < module->function_count; i++) {
  3307. WASMFunction *func = module->functions[i];
  3308. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  3309. error_buf_size)) {
  3310. return false;
  3311. }
  3312. if (i == module->function_count - 1
  3313. && func->code + func->code_size != buf_code_end) {
  3314. set_error_buf(error_buf, error_buf_size,
  3315. "code section size mismatch");
  3316. return false;
  3317. }
  3318. }
  3319. if (!module->possible_memory_grow) {
  3320. WASMMemoryImport *memory_import;
  3321. WASMMemory *memory;
  3322. if (aux_data_end_global && aux_heap_base_global
  3323. && aux_stack_top_global) {
  3324. uint64 init_memory_size;
  3325. uint32 shrunk_memory_size = align_uint(aux_heap_base, 8);
  3326. if (module->import_memory_count) {
  3327. memory_import = &module->import_memories[0].u.memory;
  3328. init_memory_size = (uint64)memory_import->num_bytes_per_page
  3329. * memory_import->init_page_count;
  3330. if (shrunk_memory_size <= init_memory_size) {
  3331. /* Reset memory info to decrease memory usage */
  3332. memory_import->num_bytes_per_page = shrunk_memory_size;
  3333. memory_import->init_page_count = 1;
  3334. LOG_VERBOSE("Shrink import memory size to %d",
  3335. shrunk_memory_size);
  3336. }
  3337. }
  3338. if (module->memory_count) {
  3339. memory = &module->memories[0];
  3340. init_memory_size = (uint64)memory->num_bytes_per_page
  3341. * memory->init_page_count;
  3342. if (shrunk_memory_size <= init_memory_size) {
  3343. /* Reset memory info to decrease memory usage */
  3344. memory->num_bytes_per_page = shrunk_memory_size;
  3345. memory->init_page_count = 1;
  3346. LOG_VERBOSE("Shrink memory size to %d", shrunk_memory_size);
  3347. }
  3348. }
  3349. }
  3350. #if WASM_ENABLE_MULTI_MODULE == 0
  3351. if (module->import_memory_count) {
  3352. memory_import = &module->import_memories[0].u.memory;
  3353. if (memory_import->init_page_count < DEFAULT_MAX_PAGES)
  3354. memory_import->num_bytes_per_page *=
  3355. memory_import->init_page_count;
  3356. else
  3357. memory_import->num_bytes_per_page = UINT32_MAX;
  3358. if (memory_import->init_page_count > 0)
  3359. memory_import->init_page_count = memory_import->max_page_count =
  3360. 1;
  3361. else
  3362. memory_import->init_page_count = memory_import->max_page_count =
  3363. 0;
  3364. }
  3365. if (module->memory_count) {
  3366. memory = &module->memories[0];
  3367. if (memory->init_page_count < DEFAULT_MAX_PAGES)
  3368. memory->num_bytes_per_page *= memory->init_page_count;
  3369. else
  3370. memory->num_bytes_per_page = UINT32_MAX;
  3371. if (memory->init_page_count > 0)
  3372. memory->init_page_count = memory->max_page_count = 1;
  3373. else
  3374. memory->init_page_count = memory->max_page_count = 0;
  3375. }
  3376. #endif
  3377. }
  3378. calculate_global_data_offset(module);
  3379. #if WASM_ENABLE_FAST_JIT != 0
  3380. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  3381. return false;
  3382. }
  3383. #endif
  3384. #if WASM_ENABLE_JIT != 0
  3385. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  3386. return false;
  3387. }
  3388. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  3389. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  3390. return false;
  3391. }
  3392. #else
  3393. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  3394. thread fast jit execution, since applying llvm optimizations in
  3395. aot_compile_wasm may cost a lot of time.
  3396. Create thread with enough native stack to apply llvm optimizations */
  3397. if (os_thread_create(&module->llvm_jit_init_thread,
  3398. init_llvm_jit_functions_stage2_callback,
  3399. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  3400. != 0) {
  3401. set_error_buf(error_buf, error_buf_size,
  3402. "create orcjit compile thread failed");
  3403. return false;
  3404. }
  3405. #endif
  3406. #endif
  3407. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3408. /* Create threads to compile the jit functions */
  3409. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  3410. return false;
  3411. }
  3412. #endif
  3413. #if WASM_ENABLE_MEMORY_TRACING != 0
  3414. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  3415. #endif
  3416. return true;
  3417. }
  3418. static WASMModule *
  3419. create_module(char *error_buf, uint32 error_buf_size)
  3420. {
  3421. WASMModule *module =
  3422. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  3423. bh_list_status ret;
  3424. if (!module) {
  3425. return NULL;
  3426. }
  3427. module->module_type = Wasm_Module_Bytecode;
  3428. /* Set start_function to -1, means no start function */
  3429. module->start_function = (uint32)-1;
  3430. #if WASM_ENABLE_FAST_INTERP == 0
  3431. module->br_table_cache_list = &module->br_table_cache_list_head;
  3432. ret = bh_list_init(module->br_table_cache_list);
  3433. bh_assert(ret == BH_LIST_SUCCESS);
  3434. #endif
  3435. #if WASM_ENABLE_MULTI_MODULE != 0
  3436. module->import_module_list = &module->import_module_list_head;
  3437. ret = bh_list_init(module->import_module_list);
  3438. bh_assert(ret == BH_LIST_SUCCESS);
  3439. #endif
  3440. #if WASM_ENABLE_DEBUG_INTERP != 0
  3441. ret = bh_list_init(&module->fast_opcode_list);
  3442. bh_assert(ret == BH_LIST_SUCCESS);
  3443. #endif
  3444. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  3445. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3446. && WASM_ENABLE_LAZY_JIT != 0)
  3447. if (os_mutex_init(&module->instance_list_lock) != 0) {
  3448. set_error_buf(error_buf, error_buf_size,
  3449. "init instance list lock failed");
  3450. wasm_runtime_free(module);
  3451. return NULL;
  3452. }
  3453. #endif
  3454. (void)ret;
  3455. return module;
  3456. }
  3457. #if WASM_ENABLE_DEBUG_INTERP != 0
  3458. static bool
  3459. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  3460. uint32 error_buf_size)
  3461. {
  3462. WASMFastOPCodeNode *fast_op =
  3463. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  3464. if (fast_op) {
  3465. fast_op->offset = pos - module->load_addr;
  3466. fast_op->orig_op = orig_op;
  3467. bh_list_insert(&module->fast_opcode_list, fast_op);
  3468. }
  3469. return fast_op ? true : false;
  3470. }
  3471. #endif
  3472. WASMModule *
  3473. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  3474. uint32 error_buf_size)
  3475. {
  3476. WASMModule *module = create_module(error_buf, error_buf_size);
  3477. if (!module)
  3478. return NULL;
  3479. if (!load_from_sections(module, section_list, false, error_buf,
  3480. error_buf_size)) {
  3481. wasm_loader_unload(module);
  3482. return NULL;
  3483. }
  3484. LOG_VERBOSE("Load module from sections success.\n");
  3485. return module;
  3486. }
  3487. static void
  3488. destroy_sections(WASMSection *section_list)
  3489. {
  3490. WASMSection *section = section_list, *next;
  3491. while (section) {
  3492. next = section->next;
  3493. wasm_runtime_free(section);
  3494. section = next;
  3495. }
  3496. }
  3497. /* clang-format off */
  3498. static uint8 section_ids[] = {
  3499. SECTION_TYPE_USER,
  3500. SECTION_TYPE_TYPE,
  3501. SECTION_TYPE_IMPORT,
  3502. SECTION_TYPE_FUNC,
  3503. SECTION_TYPE_TABLE,
  3504. SECTION_TYPE_MEMORY,
  3505. SECTION_TYPE_GLOBAL,
  3506. SECTION_TYPE_EXPORT,
  3507. SECTION_TYPE_START,
  3508. SECTION_TYPE_ELEM,
  3509. #if WASM_ENABLE_BULK_MEMORY != 0
  3510. SECTION_TYPE_DATACOUNT,
  3511. #endif
  3512. SECTION_TYPE_CODE,
  3513. SECTION_TYPE_DATA
  3514. };
  3515. /* clang-format on */
  3516. static uint8
  3517. get_section_index(uint8 section_type)
  3518. {
  3519. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  3520. for (uint8 i = 0; i < max_id; i++) {
  3521. if (section_type == section_ids[i])
  3522. return i;
  3523. }
  3524. return (uint8)-1;
  3525. }
  3526. static bool
  3527. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  3528. char *error_buf, uint32 error_buf_size)
  3529. {
  3530. WASMSection *section_list_end = NULL, *section;
  3531. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  3532. uint8 section_type, section_index, last_section_index = (uint8)-1;
  3533. uint32 section_size;
  3534. bh_assert(!*p_section_list);
  3535. p += 8;
  3536. while (p < p_end) {
  3537. CHECK_BUF(p, p_end, 1);
  3538. section_type = read_uint8(p);
  3539. section_index = get_section_index(section_type);
  3540. if (section_index != (uint8)-1) {
  3541. if (section_type != SECTION_TYPE_USER) {
  3542. /* Custom sections may be inserted at any place,
  3543. while other sections must occur at most once
  3544. and in prescribed order. */
  3545. if (last_section_index != (uint8)-1
  3546. && (section_index <= last_section_index)) {
  3547. set_error_buf(error_buf, error_buf_size,
  3548. "unexpected content after last section or "
  3549. "junk after last section");
  3550. return false;
  3551. }
  3552. last_section_index = section_index;
  3553. }
  3554. read_leb_uint32(p, p_end, section_size);
  3555. CHECK_BUF1(p, p_end, section_size);
  3556. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  3557. error_buf_size))) {
  3558. return false;
  3559. }
  3560. section->section_type = section_type;
  3561. section->section_body = (uint8 *)p;
  3562. section->section_body_size = section_size;
  3563. if (!section_list_end)
  3564. *p_section_list = section_list_end = section;
  3565. else {
  3566. section_list_end->next = section;
  3567. section_list_end = section;
  3568. }
  3569. p += section_size;
  3570. }
  3571. else {
  3572. set_error_buf(error_buf, error_buf_size, "invalid section id");
  3573. return false;
  3574. }
  3575. }
  3576. return true;
  3577. fail:
  3578. return false;
  3579. }
  3580. static void
  3581. exchange32(uint8 *p_data)
  3582. {
  3583. uint8 value = *p_data;
  3584. *p_data = *(p_data + 3);
  3585. *(p_data + 3) = value;
  3586. value = *(p_data + 1);
  3587. *(p_data + 1) = *(p_data + 2);
  3588. *(p_data + 2) = value;
  3589. }
  3590. static union {
  3591. int a;
  3592. char b;
  3593. } __ue = { .a = 1 };
  3594. #define is_little_endian() (__ue.b == 1)
  3595. static bool
  3596. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  3597. uint32 error_buf_size)
  3598. {
  3599. const uint8 *buf_end = buf + size;
  3600. const uint8 *p = buf, *p_end = buf_end;
  3601. uint32 magic_number, version;
  3602. WASMSection *section_list = NULL;
  3603. CHECK_BUF1(p, p_end, sizeof(uint32));
  3604. magic_number = read_uint32(p);
  3605. if (!is_little_endian())
  3606. exchange32((uint8 *)&magic_number);
  3607. if (magic_number != WASM_MAGIC_NUMBER) {
  3608. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  3609. return false;
  3610. }
  3611. CHECK_BUF1(p, p_end, sizeof(uint32));
  3612. version = read_uint32(p);
  3613. if (!is_little_endian())
  3614. exchange32((uint8 *)&version);
  3615. if (version != WASM_CURRENT_VERSION) {
  3616. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  3617. return false;
  3618. }
  3619. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  3620. || !load_from_sections(module, section_list, true, error_buf,
  3621. error_buf_size)) {
  3622. destroy_sections(section_list);
  3623. return false;
  3624. }
  3625. destroy_sections(section_list);
  3626. return true;
  3627. fail:
  3628. return false;
  3629. }
  3630. #if WASM_ENABLE_LIBC_WASI != 0
  3631. /**
  3632. * refer to
  3633. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  3634. */
  3635. static bool
  3636. check_wasi_abi_compatibility(const WASMModule *module,
  3637. #if WASM_ENABLE_MULTI_MODULE != 0
  3638. bool main_module,
  3639. #endif
  3640. char *error_buf, uint32 error_buf_size)
  3641. {
  3642. /**
  3643. * be careful with:
  3644. * wasi compatiable modules(command/reactor) which don't import any wasi
  3645. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  3646. * reactor can depend on nothing. At the same time, each has its own entry
  3647. * point.
  3648. *
  3649. * observations:
  3650. * - clang always injects `_start` into a command
  3651. * - clang always injects `_initialize` into a reactor
  3652. * - `iwasm -f` allows to run a function in the reactor
  3653. *
  3654. * strong assumptions:
  3655. * - no one will define either `_start` or `_initialize` on purpose
  3656. * - `_start` should always be `void _start(void)`
  3657. * - `_initialize` should always be `void _initialize(void)`
  3658. *
  3659. */
  3660. /* clang-format off */
  3661. /**
  3662. *
  3663. * | | import_wasi_api True | | import_wasi_api False | |
  3664. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  3665. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  3666. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  3667. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  3668. */
  3669. /* clang-format on */
  3670. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  3671. /* (func (export "_start") (...) */
  3672. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  3673. error_buf, error_buf_size);
  3674. if (start) {
  3675. WASMType *func_type =
  3676. module->functions[start->index - module->import_function_count]
  3677. ->func_type;
  3678. if (func_type->param_count || func_type->result_count) {
  3679. set_error_buf(error_buf, error_buf_size,
  3680. "the signature of builtin _start function is wrong");
  3681. return false;
  3682. }
  3683. }
  3684. /* (func (export "_initialize") (...) */
  3685. initialize = wasm_loader_find_export(
  3686. module, "", "_initialize", EXPORT_KIND_FUNC, error_buf, error_buf_size);
  3687. if (initialize) {
  3688. WASMType *func_type =
  3689. module->functions[initialize->index - module->import_function_count]
  3690. ->func_type;
  3691. if (func_type->param_count || func_type->result_count) {
  3692. set_error_buf(
  3693. error_buf, error_buf_size,
  3694. "the signature of builtin _initialize function is wrong");
  3695. return false;
  3696. }
  3697. }
  3698. /* filter out non-wasi compatiable modules */
  3699. if (!module->import_wasi_api && !start && !initialize) {
  3700. return true;
  3701. }
  3702. /* should have one at least */
  3703. if (module->import_wasi_api && !start && !initialize) {
  3704. LOG_WARNING("warning: a module with WASI apis should be either "
  3705. "a command or a reactor");
  3706. }
  3707. /*
  3708. * there is at least one of `_start` and `_initialize` in below cases.
  3709. * according to the assumption, they should be all wasi compatiable
  3710. */
  3711. /* always can not have both at the same time */
  3712. if (start && initialize) {
  3713. set_error_buf(
  3714. error_buf, error_buf_size,
  3715. "neither a command nor a reactor can both have _start function "
  3716. "and _initialize function at the same time");
  3717. return false;
  3718. }
  3719. #if WASM_ENABLE_MULTI_MODULE != 0
  3720. /* filter out commands (with `_start`) cases */
  3721. if (start && !main_module) {
  3722. set_error_buf(
  3723. error_buf, error_buf_size,
  3724. "a command (with _start function) can not be a sub-module");
  3725. return false;
  3726. }
  3727. #endif
  3728. /*
  3729. * it is ok a reactor acts as a main module,
  3730. * so skip the check about (with `_initialize`)
  3731. */
  3732. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  3733. error_buf, error_buf_size);
  3734. if (!memory
  3735. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  3736. /*
  3737. * with wasi-threads, it's still an open question if a memory
  3738. * should be exported.
  3739. *
  3740. * https://github.com/WebAssembly/wasi-threads/issues/22
  3741. * https://github.com/WebAssembly/WASI/issues/502
  3742. *
  3743. * Note: this code assumes the number of memories is at most 1.
  3744. */
  3745. && module->import_memory_count == 0
  3746. #endif
  3747. ) {
  3748. set_error_buf(error_buf, error_buf_size,
  3749. "a module with WASI apis must export memory by default");
  3750. return false;
  3751. }
  3752. return true;
  3753. }
  3754. #endif
  3755. WASMModule *
  3756. wasm_loader_load(uint8 *buf, uint32 size,
  3757. #if WASM_ENABLE_MULTI_MODULE != 0
  3758. bool main_module,
  3759. #endif
  3760. char *error_buf, uint32 error_buf_size)
  3761. {
  3762. WASMModule *module = create_module(error_buf, error_buf_size);
  3763. if (!module) {
  3764. return NULL;
  3765. }
  3766. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0
  3767. module->load_addr = (uint8 *)buf;
  3768. module->load_size = size;
  3769. #endif
  3770. if (!load(buf, size, module, error_buf, error_buf_size)) {
  3771. goto fail;
  3772. }
  3773. #if WASM_ENABLE_LIBC_WASI != 0
  3774. /* Check the WASI application ABI */
  3775. if (!check_wasi_abi_compatibility(module,
  3776. #if WASM_ENABLE_MULTI_MODULE != 0
  3777. main_module,
  3778. #endif
  3779. error_buf, error_buf_size)) {
  3780. goto fail;
  3781. }
  3782. #endif
  3783. LOG_VERBOSE("Load module success.\n");
  3784. return module;
  3785. fail:
  3786. wasm_loader_unload(module);
  3787. return NULL;
  3788. }
  3789. void
  3790. wasm_loader_unload(WASMModule *module)
  3791. {
  3792. uint32 i;
  3793. if (!module)
  3794. return;
  3795. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3796. && WASM_ENABLE_LAZY_JIT != 0
  3797. module->orcjit_stop_compiling = true;
  3798. if (module->llvm_jit_init_thread)
  3799. os_thread_join(module->llvm_jit_init_thread, NULL);
  3800. #endif
  3801. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  3802. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  3803. module internal data after they were freed */
  3804. orcjit_stop_compile_threads(module);
  3805. #endif
  3806. #if WASM_ENABLE_JIT != 0
  3807. if (module->func_ptrs)
  3808. wasm_runtime_free(module->func_ptrs);
  3809. if (module->comp_ctx)
  3810. aot_destroy_comp_context(module->comp_ctx);
  3811. if (module->comp_data)
  3812. aot_destroy_comp_data(module->comp_data);
  3813. #endif
  3814. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3815. && WASM_ENABLE_LAZY_JIT != 0
  3816. if (module->tierup_wait_lock_inited) {
  3817. os_mutex_destroy(&module->tierup_wait_lock);
  3818. os_cond_destroy(&module->tierup_wait_cond);
  3819. }
  3820. #endif
  3821. if (module->types) {
  3822. for (i = 0; i < module->type_count; i++) {
  3823. if (module->types[i])
  3824. destroy_wasm_type(module->types[i]);
  3825. }
  3826. wasm_runtime_free(module->types);
  3827. }
  3828. if (module->imports)
  3829. wasm_runtime_free(module->imports);
  3830. if (module->functions) {
  3831. for (i = 0; i < module->function_count; i++) {
  3832. if (module->functions[i]) {
  3833. if (module->functions[i]->local_offsets)
  3834. wasm_runtime_free(module->functions[i]->local_offsets);
  3835. #if WASM_ENABLE_FAST_INTERP != 0
  3836. if (module->functions[i]->code_compiled)
  3837. wasm_runtime_free(module->functions[i]->code_compiled);
  3838. if (module->functions[i]->consts)
  3839. wasm_runtime_free(module->functions[i]->consts);
  3840. #endif
  3841. #if WASM_ENABLE_FAST_JIT != 0
  3842. if (module->functions[i]->fast_jit_jitted_code) {
  3843. jit_code_cache_free(
  3844. module->functions[i]->fast_jit_jitted_code);
  3845. }
  3846. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  3847. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  3848. jit_code_cache_free(
  3849. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  3850. }
  3851. #endif
  3852. #endif
  3853. wasm_runtime_free(module->functions[i]);
  3854. }
  3855. }
  3856. wasm_runtime_free(module->functions);
  3857. }
  3858. if (module->tables)
  3859. wasm_runtime_free(module->tables);
  3860. if (module->memories)
  3861. wasm_runtime_free(module->memories);
  3862. if (module->globals)
  3863. wasm_runtime_free(module->globals);
  3864. if (module->exports)
  3865. wasm_runtime_free(module->exports);
  3866. if (module->table_segments) {
  3867. for (i = 0; i < module->table_seg_count; i++) {
  3868. if (module->table_segments[i].func_indexes)
  3869. wasm_runtime_free(module->table_segments[i].func_indexes);
  3870. }
  3871. wasm_runtime_free(module->table_segments);
  3872. }
  3873. if (module->data_segments) {
  3874. for (i = 0; i < module->data_seg_count; i++) {
  3875. if (module->data_segments[i])
  3876. wasm_runtime_free(module->data_segments[i]);
  3877. }
  3878. wasm_runtime_free(module->data_segments);
  3879. }
  3880. if (module->const_str_list) {
  3881. StringNode *node = module->const_str_list, *node_next;
  3882. while (node) {
  3883. node_next = node->next;
  3884. wasm_runtime_free(node);
  3885. node = node_next;
  3886. }
  3887. }
  3888. #if WASM_ENABLE_FAST_INTERP == 0
  3889. if (module->br_table_cache_list) {
  3890. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  3891. BrTableCache *node_next;
  3892. while (node) {
  3893. node_next = bh_list_elem_next(node);
  3894. wasm_runtime_free(node);
  3895. node = node_next;
  3896. }
  3897. }
  3898. #endif
  3899. #if WASM_ENABLE_MULTI_MODULE != 0
  3900. /* just release the sub module list */
  3901. if (module->import_module_list) {
  3902. WASMRegisteredModule *node =
  3903. bh_list_first_elem(module->import_module_list);
  3904. while (node) {
  3905. WASMRegisteredModule *next = bh_list_elem_next(node);
  3906. bh_list_remove(module->import_module_list, node);
  3907. /*
  3908. * unload(sub_module) will be trigged during runtime_destroy().
  3909. * every module in the global module list will be unloaded one by
  3910. * one. so don't worry.
  3911. */
  3912. wasm_runtime_free(node);
  3913. /*
  3914. * the module file reading buffer will be released
  3915. * in runtime_destroy()
  3916. */
  3917. node = next;
  3918. }
  3919. }
  3920. #endif
  3921. #if WASM_ENABLE_DEBUG_INTERP != 0
  3922. WASMFastOPCodeNode *fast_opcode =
  3923. bh_list_first_elem(&module->fast_opcode_list);
  3924. while (fast_opcode) {
  3925. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  3926. wasm_runtime_free(fast_opcode);
  3927. fast_opcode = next;
  3928. }
  3929. #endif
  3930. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  3931. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  3932. && WASM_ENABLE_LAZY_JIT != 0)
  3933. os_mutex_destroy(&module->instance_list_lock);
  3934. #endif
  3935. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  3936. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  3937. #endif
  3938. #if WASM_ENABLE_FAST_JIT != 0
  3939. if (module->fast_jit_func_ptrs) {
  3940. wasm_runtime_free(module->fast_jit_func_ptrs);
  3941. }
  3942. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  3943. if (module->fast_jit_thread_locks_inited[i]) {
  3944. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  3945. }
  3946. }
  3947. #endif
  3948. wasm_runtime_free(module);
  3949. }
  3950. bool
  3951. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  3952. const uint8 *start_addr, const uint8 *code_end_addr,
  3953. uint8 label_type, uint8 **p_else_addr,
  3954. uint8 **p_end_addr)
  3955. {
  3956. const uint8 *p = start_addr, *p_end = code_end_addr;
  3957. uint8 *else_addr = NULL;
  3958. char error_buf[128];
  3959. uint32 block_nested_depth = 1, count, i, j, t;
  3960. uint32 error_buf_size = sizeof(error_buf);
  3961. uint8 opcode, u8;
  3962. BlockAddr block_stack[16] = { { 0 } }, *block;
  3963. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3964. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  3965. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  3966. if (block[j].start_addr == start_addr) {
  3967. /* Cache hit */
  3968. *p_else_addr = block[j].else_addr;
  3969. *p_end_addr = block[j].end_addr;
  3970. return true;
  3971. }
  3972. }
  3973. /* Cache unhit */
  3974. block_stack[0].start_addr = start_addr;
  3975. while (p < code_end_addr) {
  3976. opcode = *p++;
  3977. #if WASM_ENABLE_DEBUG_INTERP != 0
  3978. op_break_retry:
  3979. #endif
  3980. switch (opcode) {
  3981. case WASM_OP_UNREACHABLE:
  3982. case WASM_OP_NOP:
  3983. break;
  3984. case WASM_OP_BLOCK:
  3985. case WASM_OP_LOOP:
  3986. case WASM_OP_IF:
  3987. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  3988. u8 = read_uint8(p);
  3989. if (block_nested_depth
  3990. < sizeof(block_stack) / sizeof(BlockAddr)) {
  3991. block_stack[block_nested_depth].start_addr = p;
  3992. block_stack[block_nested_depth].else_addr = NULL;
  3993. }
  3994. block_nested_depth++;
  3995. break;
  3996. case EXT_OP_BLOCK:
  3997. case EXT_OP_LOOP:
  3998. case EXT_OP_IF:
  3999. /* block type */
  4000. skip_leb_uint32(p, p_end);
  4001. if (block_nested_depth
  4002. < sizeof(block_stack) / sizeof(BlockAddr)) {
  4003. block_stack[block_nested_depth].start_addr = p;
  4004. block_stack[block_nested_depth].else_addr = NULL;
  4005. }
  4006. block_nested_depth++;
  4007. break;
  4008. case WASM_OP_ELSE:
  4009. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  4010. else_addr = (uint8 *)(p - 1);
  4011. if (block_nested_depth - 1
  4012. < sizeof(block_stack) / sizeof(BlockAddr))
  4013. block_stack[block_nested_depth - 1].else_addr =
  4014. (uint8 *)(p - 1);
  4015. break;
  4016. case WASM_OP_END:
  4017. if (block_nested_depth == 1) {
  4018. if (label_type == LABEL_TYPE_IF)
  4019. *p_else_addr = else_addr;
  4020. *p_end_addr = (uint8 *)(p - 1);
  4021. block_stack[0].end_addr = (uint8 *)(p - 1);
  4022. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  4023. t++) {
  4024. start_addr = block_stack[t].start_addr;
  4025. if (start_addr) {
  4026. i = ((uintptr_t)start_addr)
  4027. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  4028. block =
  4029. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  4030. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  4031. if (!block[j].start_addr)
  4032. break;
  4033. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  4034. memmove(block + 1, block,
  4035. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  4036. * sizeof(BlockAddr));
  4037. j = 0;
  4038. }
  4039. block[j].start_addr = block_stack[t].start_addr;
  4040. block[j].else_addr = block_stack[t].else_addr;
  4041. block[j].end_addr = block_stack[t].end_addr;
  4042. }
  4043. else
  4044. break;
  4045. }
  4046. return true;
  4047. }
  4048. else {
  4049. block_nested_depth--;
  4050. if (block_nested_depth
  4051. < sizeof(block_stack) / sizeof(BlockAddr))
  4052. block_stack[block_nested_depth].end_addr =
  4053. (uint8 *)(p - 1);
  4054. }
  4055. break;
  4056. case WASM_OP_BR:
  4057. case WASM_OP_BR_IF:
  4058. skip_leb_uint32(p, p_end); /* labelidx */
  4059. break;
  4060. case WASM_OP_BR_TABLE:
  4061. read_leb_uint32(p, p_end, count); /* lable num */
  4062. #if WASM_ENABLE_FAST_INTERP != 0
  4063. for (i = 0; i <= count; i++) /* lableidxs */
  4064. skip_leb_uint32(p, p_end);
  4065. #else
  4066. p += count + 1;
  4067. while (*p == WASM_OP_NOP)
  4068. p++;
  4069. #endif
  4070. break;
  4071. #if WASM_ENABLE_FAST_INTERP == 0
  4072. case EXT_OP_BR_TABLE_CACHE:
  4073. read_leb_uint32(p, p_end, count); /* lable num */
  4074. while (*p == WASM_OP_NOP)
  4075. p++;
  4076. break;
  4077. #endif
  4078. case WASM_OP_RETURN:
  4079. break;
  4080. case WASM_OP_CALL:
  4081. #if WASM_ENABLE_TAIL_CALL != 0
  4082. case WASM_OP_RETURN_CALL:
  4083. #endif
  4084. skip_leb_uint32(p, p_end); /* funcidx */
  4085. break;
  4086. case WASM_OP_CALL_INDIRECT:
  4087. #if WASM_ENABLE_TAIL_CALL != 0
  4088. case WASM_OP_RETURN_CALL_INDIRECT:
  4089. #endif
  4090. skip_leb_uint32(p, p_end); /* typeidx */
  4091. CHECK_BUF(p, p_end, 1);
  4092. u8 = read_uint8(p); /* 0x00 */
  4093. break;
  4094. case WASM_OP_DROP:
  4095. case WASM_OP_SELECT:
  4096. case WASM_OP_DROP_64:
  4097. case WASM_OP_SELECT_64:
  4098. break;
  4099. #if WASM_ENABLE_REF_TYPES != 0
  4100. case WASM_OP_SELECT_T:
  4101. skip_leb_uint32(p, p_end); /* vec length */
  4102. CHECK_BUF(p, p_end, 1);
  4103. u8 = read_uint8(p); /* typeidx */
  4104. break;
  4105. case WASM_OP_TABLE_GET:
  4106. case WASM_OP_TABLE_SET:
  4107. skip_leb_uint32(p, p_end); /* table index */
  4108. break;
  4109. case WASM_OP_REF_NULL:
  4110. CHECK_BUF(p, p_end, 1);
  4111. u8 = read_uint8(p); /* type */
  4112. break;
  4113. case WASM_OP_REF_IS_NULL:
  4114. break;
  4115. case WASM_OP_REF_FUNC:
  4116. skip_leb_uint32(p, p_end); /* func index */
  4117. break;
  4118. #endif /* WASM_ENABLE_REF_TYPES */
  4119. case WASM_OP_GET_LOCAL:
  4120. case WASM_OP_SET_LOCAL:
  4121. case WASM_OP_TEE_LOCAL:
  4122. case WASM_OP_GET_GLOBAL:
  4123. case WASM_OP_SET_GLOBAL:
  4124. case WASM_OP_GET_GLOBAL_64:
  4125. case WASM_OP_SET_GLOBAL_64:
  4126. case WASM_OP_SET_GLOBAL_AUX_STACK:
  4127. skip_leb_uint32(p, p_end); /* local index */
  4128. break;
  4129. case EXT_OP_GET_LOCAL_FAST:
  4130. case EXT_OP_SET_LOCAL_FAST:
  4131. case EXT_OP_TEE_LOCAL_FAST:
  4132. CHECK_BUF(p, p_end, 1);
  4133. p++;
  4134. break;
  4135. case WASM_OP_I32_LOAD:
  4136. case WASM_OP_I64_LOAD:
  4137. case WASM_OP_F32_LOAD:
  4138. case WASM_OP_F64_LOAD:
  4139. case WASM_OP_I32_LOAD8_S:
  4140. case WASM_OP_I32_LOAD8_U:
  4141. case WASM_OP_I32_LOAD16_S:
  4142. case WASM_OP_I32_LOAD16_U:
  4143. case WASM_OP_I64_LOAD8_S:
  4144. case WASM_OP_I64_LOAD8_U:
  4145. case WASM_OP_I64_LOAD16_S:
  4146. case WASM_OP_I64_LOAD16_U:
  4147. case WASM_OP_I64_LOAD32_S:
  4148. case WASM_OP_I64_LOAD32_U:
  4149. case WASM_OP_I32_STORE:
  4150. case WASM_OP_I64_STORE:
  4151. case WASM_OP_F32_STORE:
  4152. case WASM_OP_F64_STORE:
  4153. case WASM_OP_I32_STORE8:
  4154. case WASM_OP_I32_STORE16:
  4155. case WASM_OP_I64_STORE8:
  4156. case WASM_OP_I64_STORE16:
  4157. case WASM_OP_I64_STORE32:
  4158. skip_leb_uint32(p, p_end); /* align */
  4159. skip_leb_uint32(p, p_end); /* offset */
  4160. break;
  4161. case WASM_OP_MEMORY_SIZE:
  4162. case WASM_OP_MEMORY_GROW:
  4163. skip_leb_uint32(p, p_end); /* 0x00 */
  4164. break;
  4165. case WASM_OP_I32_CONST:
  4166. skip_leb_int32(p, p_end);
  4167. break;
  4168. case WASM_OP_I64_CONST:
  4169. skip_leb_int64(p, p_end);
  4170. break;
  4171. case WASM_OP_F32_CONST:
  4172. p += sizeof(float32);
  4173. break;
  4174. case WASM_OP_F64_CONST:
  4175. p += sizeof(float64);
  4176. break;
  4177. case WASM_OP_I32_EQZ:
  4178. case WASM_OP_I32_EQ:
  4179. case WASM_OP_I32_NE:
  4180. case WASM_OP_I32_LT_S:
  4181. case WASM_OP_I32_LT_U:
  4182. case WASM_OP_I32_GT_S:
  4183. case WASM_OP_I32_GT_U:
  4184. case WASM_OP_I32_LE_S:
  4185. case WASM_OP_I32_LE_U:
  4186. case WASM_OP_I32_GE_S:
  4187. case WASM_OP_I32_GE_U:
  4188. case WASM_OP_I64_EQZ:
  4189. case WASM_OP_I64_EQ:
  4190. case WASM_OP_I64_NE:
  4191. case WASM_OP_I64_LT_S:
  4192. case WASM_OP_I64_LT_U:
  4193. case WASM_OP_I64_GT_S:
  4194. case WASM_OP_I64_GT_U:
  4195. case WASM_OP_I64_LE_S:
  4196. case WASM_OP_I64_LE_U:
  4197. case WASM_OP_I64_GE_S:
  4198. case WASM_OP_I64_GE_U:
  4199. case WASM_OP_F32_EQ:
  4200. case WASM_OP_F32_NE:
  4201. case WASM_OP_F32_LT:
  4202. case WASM_OP_F32_GT:
  4203. case WASM_OP_F32_LE:
  4204. case WASM_OP_F32_GE:
  4205. case WASM_OP_F64_EQ:
  4206. case WASM_OP_F64_NE:
  4207. case WASM_OP_F64_LT:
  4208. case WASM_OP_F64_GT:
  4209. case WASM_OP_F64_LE:
  4210. case WASM_OP_F64_GE:
  4211. case WASM_OP_I32_CLZ:
  4212. case WASM_OP_I32_CTZ:
  4213. case WASM_OP_I32_POPCNT:
  4214. case WASM_OP_I32_ADD:
  4215. case WASM_OP_I32_SUB:
  4216. case WASM_OP_I32_MUL:
  4217. case WASM_OP_I32_DIV_S:
  4218. case WASM_OP_I32_DIV_U:
  4219. case WASM_OP_I32_REM_S:
  4220. case WASM_OP_I32_REM_U:
  4221. case WASM_OP_I32_AND:
  4222. case WASM_OP_I32_OR:
  4223. case WASM_OP_I32_XOR:
  4224. case WASM_OP_I32_SHL:
  4225. case WASM_OP_I32_SHR_S:
  4226. case WASM_OP_I32_SHR_U:
  4227. case WASM_OP_I32_ROTL:
  4228. case WASM_OP_I32_ROTR:
  4229. case WASM_OP_I64_CLZ:
  4230. case WASM_OP_I64_CTZ:
  4231. case WASM_OP_I64_POPCNT:
  4232. case WASM_OP_I64_ADD:
  4233. case WASM_OP_I64_SUB:
  4234. case WASM_OP_I64_MUL:
  4235. case WASM_OP_I64_DIV_S:
  4236. case WASM_OP_I64_DIV_U:
  4237. case WASM_OP_I64_REM_S:
  4238. case WASM_OP_I64_REM_U:
  4239. case WASM_OP_I64_AND:
  4240. case WASM_OP_I64_OR:
  4241. case WASM_OP_I64_XOR:
  4242. case WASM_OP_I64_SHL:
  4243. case WASM_OP_I64_SHR_S:
  4244. case WASM_OP_I64_SHR_U:
  4245. case WASM_OP_I64_ROTL:
  4246. case WASM_OP_I64_ROTR:
  4247. case WASM_OP_F32_ABS:
  4248. case WASM_OP_F32_NEG:
  4249. case WASM_OP_F32_CEIL:
  4250. case WASM_OP_F32_FLOOR:
  4251. case WASM_OP_F32_TRUNC:
  4252. case WASM_OP_F32_NEAREST:
  4253. case WASM_OP_F32_SQRT:
  4254. case WASM_OP_F32_ADD:
  4255. case WASM_OP_F32_SUB:
  4256. case WASM_OP_F32_MUL:
  4257. case WASM_OP_F32_DIV:
  4258. case WASM_OP_F32_MIN:
  4259. case WASM_OP_F32_MAX:
  4260. case WASM_OP_F32_COPYSIGN:
  4261. case WASM_OP_F64_ABS:
  4262. case WASM_OP_F64_NEG:
  4263. case WASM_OP_F64_CEIL:
  4264. case WASM_OP_F64_FLOOR:
  4265. case WASM_OP_F64_TRUNC:
  4266. case WASM_OP_F64_NEAREST:
  4267. case WASM_OP_F64_SQRT:
  4268. case WASM_OP_F64_ADD:
  4269. case WASM_OP_F64_SUB:
  4270. case WASM_OP_F64_MUL:
  4271. case WASM_OP_F64_DIV:
  4272. case WASM_OP_F64_MIN:
  4273. case WASM_OP_F64_MAX:
  4274. case WASM_OP_F64_COPYSIGN:
  4275. case WASM_OP_I32_WRAP_I64:
  4276. case WASM_OP_I32_TRUNC_S_F32:
  4277. case WASM_OP_I32_TRUNC_U_F32:
  4278. case WASM_OP_I32_TRUNC_S_F64:
  4279. case WASM_OP_I32_TRUNC_U_F64:
  4280. case WASM_OP_I64_EXTEND_S_I32:
  4281. case WASM_OP_I64_EXTEND_U_I32:
  4282. case WASM_OP_I64_TRUNC_S_F32:
  4283. case WASM_OP_I64_TRUNC_U_F32:
  4284. case WASM_OP_I64_TRUNC_S_F64:
  4285. case WASM_OP_I64_TRUNC_U_F64:
  4286. case WASM_OP_F32_CONVERT_S_I32:
  4287. case WASM_OP_F32_CONVERT_U_I32:
  4288. case WASM_OP_F32_CONVERT_S_I64:
  4289. case WASM_OP_F32_CONVERT_U_I64:
  4290. case WASM_OP_F32_DEMOTE_F64:
  4291. case WASM_OP_F64_CONVERT_S_I32:
  4292. case WASM_OP_F64_CONVERT_U_I32:
  4293. case WASM_OP_F64_CONVERT_S_I64:
  4294. case WASM_OP_F64_CONVERT_U_I64:
  4295. case WASM_OP_F64_PROMOTE_F32:
  4296. case WASM_OP_I32_REINTERPRET_F32:
  4297. case WASM_OP_I64_REINTERPRET_F64:
  4298. case WASM_OP_F32_REINTERPRET_I32:
  4299. case WASM_OP_F64_REINTERPRET_I64:
  4300. case WASM_OP_I32_EXTEND8_S:
  4301. case WASM_OP_I32_EXTEND16_S:
  4302. case WASM_OP_I64_EXTEND8_S:
  4303. case WASM_OP_I64_EXTEND16_S:
  4304. case WASM_OP_I64_EXTEND32_S:
  4305. break;
  4306. case WASM_OP_MISC_PREFIX:
  4307. {
  4308. uint32 opcode1;
  4309. read_leb_uint32(p, p_end, opcode1);
  4310. switch (opcode1) {
  4311. case WASM_OP_I32_TRUNC_SAT_S_F32:
  4312. case WASM_OP_I32_TRUNC_SAT_U_F32:
  4313. case WASM_OP_I32_TRUNC_SAT_S_F64:
  4314. case WASM_OP_I32_TRUNC_SAT_U_F64:
  4315. case WASM_OP_I64_TRUNC_SAT_S_F32:
  4316. case WASM_OP_I64_TRUNC_SAT_U_F32:
  4317. case WASM_OP_I64_TRUNC_SAT_S_F64:
  4318. case WASM_OP_I64_TRUNC_SAT_U_F64:
  4319. break;
  4320. #if WASM_ENABLE_BULK_MEMORY != 0
  4321. case WASM_OP_MEMORY_INIT:
  4322. skip_leb_uint32(p, p_end);
  4323. /* skip memory idx */
  4324. p++;
  4325. break;
  4326. case WASM_OP_DATA_DROP:
  4327. skip_leb_uint32(p, p_end);
  4328. break;
  4329. case WASM_OP_MEMORY_COPY:
  4330. /* skip two memory idx */
  4331. p += 2;
  4332. break;
  4333. case WASM_OP_MEMORY_FILL:
  4334. /* skip memory idx */
  4335. p++;
  4336. break;
  4337. #endif /* WASM_ENABLE_BULK_MEMORY */
  4338. #if WASM_ENABLE_REF_TYPES != 0
  4339. case WASM_OP_TABLE_INIT:
  4340. case WASM_OP_TABLE_COPY:
  4341. /* tableidx */
  4342. skip_leb_uint32(p, p_end);
  4343. /* elemidx */
  4344. skip_leb_uint32(p, p_end);
  4345. break;
  4346. case WASM_OP_ELEM_DROP:
  4347. /* elemidx */
  4348. skip_leb_uint32(p, p_end);
  4349. break;
  4350. case WASM_OP_TABLE_SIZE:
  4351. case WASM_OP_TABLE_GROW:
  4352. case WASM_OP_TABLE_FILL:
  4353. skip_leb_uint32(p, p_end); /* table idx */
  4354. break;
  4355. #endif /* WASM_ENABLE_REF_TYPES */
  4356. default:
  4357. return false;
  4358. }
  4359. break;
  4360. }
  4361. #if WASM_ENABLE_SIMD != 0
  4362. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4363. case WASM_OP_SIMD_PREFIX:
  4364. {
  4365. /* TODO: shall we ceate a table to be friendly to branch
  4366. * prediction */
  4367. opcode = read_uint8(p);
  4368. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  4369. */
  4370. switch (opcode) {
  4371. case SIMD_v128_load:
  4372. case SIMD_v128_load8x8_s:
  4373. case SIMD_v128_load8x8_u:
  4374. case SIMD_v128_load16x4_s:
  4375. case SIMD_v128_load16x4_u:
  4376. case SIMD_v128_load32x2_s:
  4377. case SIMD_v128_load32x2_u:
  4378. case SIMD_v128_load8_splat:
  4379. case SIMD_v128_load16_splat:
  4380. case SIMD_v128_load32_splat:
  4381. case SIMD_v128_load64_splat:
  4382. case SIMD_v128_store:
  4383. /* memarg align */
  4384. skip_leb_uint32(p, p_end);
  4385. /* memarg offset*/
  4386. skip_leb_uint32(p, p_end);
  4387. break;
  4388. case SIMD_v128_const:
  4389. case SIMD_v8x16_shuffle:
  4390. /* immByte[16] immLaneId[16] */
  4391. CHECK_BUF1(p, p_end, 16);
  4392. p += 16;
  4393. break;
  4394. case SIMD_i8x16_extract_lane_s:
  4395. case SIMD_i8x16_extract_lane_u:
  4396. case SIMD_i8x16_replace_lane:
  4397. case SIMD_i16x8_extract_lane_s:
  4398. case SIMD_i16x8_extract_lane_u:
  4399. case SIMD_i16x8_replace_lane:
  4400. case SIMD_i32x4_extract_lane:
  4401. case SIMD_i32x4_replace_lane:
  4402. case SIMD_i64x2_extract_lane:
  4403. case SIMD_i64x2_replace_lane:
  4404. case SIMD_f32x4_extract_lane:
  4405. case SIMD_f32x4_replace_lane:
  4406. case SIMD_f64x2_extract_lane:
  4407. case SIMD_f64x2_replace_lane:
  4408. /* ImmLaneId */
  4409. CHECK_BUF(p, p_end, 1);
  4410. p++;
  4411. break;
  4412. case SIMD_v128_load8_lane:
  4413. case SIMD_v128_load16_lane:
  4414. case SIMD_v128_load32_lane:
  4415. case SIMD_v128_load64_lane:
  4416. case SIMD_v128_store8_lane:
  4417. case SIMD_v128_store16_lane:
  4418. case SIMD_v128_store32_lane:
  4419. case SIMD_v128_store64_lane:
  4420. /* memarg align */
  4421. skip_leb_uint32(p, p_end);
  4422. /* memarg offset*/
  4423. skip_leb_uint32(p, p_end);
  4424. /* ImmLaneId */
  4425. CHECK_BUF(p, p_end, 1);
  4426. p++;
  4427. break;
  4428. case SIMD_v128_load32_zero:
  4429. case SIMD_v128_load64_zero:
  4430. /* memarg align */
  4431. skip_leb_uint32(p, p_end);
  4432. /* memarg offset*/
  4433. skip_leb_uint32(p, p_end);
  4434. break;
  4435. default:
  4436. /*
  4437. * since latest SIMD specific used almost every value
  4438. * from 0x00 to 0xff, the default branch will present
  4439. * all opcodes without imm
  4440. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  4441. */
  4442. break;
  4443. }
  4444. break;
  4445. }
  4446. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  4447. #endif /* end of WASM_ENABLE_SIMD */
  4448. #if WASM_ENABLE_SHARED_MEMORY != 0
  4449. case WASM_OP_ATOMIC_PREFIX:
  4450. {
  4451. /* atomic_op (1 u8) + memarg (2 u32_leb) */
  4452. opcode = read_uint8(p);
  4453. if (opcode != WASM_OP_ATOMIC_FENCE) {
  4454. skip_leb_uint32(p, p_end); /* align */
  4455. skip_leb_uint32(p, p_end); /* offset */
  4456. }
  4457. else {
  4458. /* atomic.fence doesn't have memarg */
  4459. p++;
  4460. }
  4461. break;
  4462. }
  4463. #endif
  4464. #if WASM_ENABLE_DEBUG_INTERP != 0
  4465. case DEBUG_OP_BREAK:
  4466. {
  4467. WASMDebugInstance *debug_instance =
  4468. wasm_exec_env_get_instance(exec_env);
  4469. char orignal_opcode[1];
  4470. uint64 size = 1;
  4471. WASMModuleInstance *module_inst =
  4472. (WASMModuleInstance *)exec_env->module_inst;
  4473. uint64 offset = (p - 1) >= module_inst->module->load_addr
  4474. ? (p - 1) - module_inst->module->load_addr
  4475. : ~0;
  4476. if (debug_instance) {
  4477. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  4478. orignal_opcode, &size)
  4479. && size == 1) {
  4480. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  4481. "with %02x: ",
  4482. orignal_opcode[0]);
  4483. opcode = orignal_opcode[0];
  4484. goto op_break_retry;
  4485. }
  4486. }
  4487. break;
  4488. }
  4489. #endif
  4490. default:
  4491. return false;
  4492. }
  4493. }
  4494. (void)u8;
  4495. (void)exec_env;
  4496. return false;
  4497. fail:
  4498. return false;
  4499. }
  4500. #define REF_ANY VALUE_TYPE_ANY
  4501. #define REF_I32 VALUE_TYPE_I32
  4502. #define REF_F32 VALUE_TYPE_F32
  4503. #define REF_I64_1 VALUE_TYPE_I64
  4504. #define REF_I64_2 VALUE_TYPE_I64
  4505. #define REF_F64_1 VALUE_TYPE_F64
  4506. #define REF_F64_2 VALUE_TYPE_F64
  4507. #define REF_V128_1 VALUE_TYPE_V128
  4508. #define REF_V128_2 VALUE_TYPE_V128
  4509. #define REF_V128_3 VALUE_TYPE_V128
  4510. #define REF_V128_4 VALUE_TYPE_V128
  4511. #define REF_FUNCREF VALUE_TYPE_FUNCREF
  4512. #define REF_EXTERNREF VALUE_TYPE_EXTERNREF
  4513. #if WASM_ENABLE_FAST_INTERP != 0
  4514. #if WASM_DEBUG_PREPROCESSOR != 0
  4515. #define LOG_OP(...) os_printf(__VA_ARGS__)
  4516. #else
  4517. #define LOG_OP(...) (void)0
  4518. #endif
  4519. #define PATCH_ELSE 0
  4520. #define PATCH_END 1
  4521. typedef struct BranchBlockPatch {
  4522. struct BranchBlockPatch *next;
  4523. uint8 patch_type;
  4524. uint8 *code_compiled;
  4525. } BranchBlockPatch;
  4526. #endif
  4527. typedef struct BranchBlock {
  4528. uint8 label_type;
  4529. BlockType block_type;
  4530. uint8 *start_addr;
  4531. uint8 *else_addr;
  4532. uint8 *end_addr;
  4533. uint32 stack_cell_num;
  4534. #if WASM_ENABLE_FAST_INTERP != 0
  4535. uint16 dynamic_offset;
  4536. uint8 *code_compiled;
  4537. BranchBlockPatch *patch_list;
  4538. /* This is used to save params frame_offset of of if block */
  4539. int16 *param_frame_offsets;
  4540. #endif
  4541. /* Indicate the operand stack is in polymorphic state.
  4542. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  4543. * to polymorphic state until the block's 'end' opcode is processed.
  4544. * If stack is in polymorphic state and stack is empty, instruction can
  4545. * pop any type of value directly without decreasing stack top pointer
  4546. * and stack cell num. */
  4547. bool is_stack_polymorphic;
  4548. } BranchBlock;
  4549. typedef struct WASMLoaderContext {
  4550. /* frame ref stack */
  4551. uint8 *frame_ref;
  4552. uint8 *frame_ref_bottom;
  4553. uint8 *frame_ref_boundary;
  4554. uint32 frame_ref_size;
  4555. uint32 stack_cell_num;
  4556. uint32 max_stack_cell_num;
  4557. /* frame csp stack */
  4558. BranchBlock *frame_csp;
  4559. BranchBlock *frame_csp_bottom;
  4560. BranchBlock *frame_csp_boundary;
  4561. uint32 frame_csp_size;
  4562. uint32 csp_num;
  4563. uint32 max_csp_num;
  4564. #if WASM_ENABLE_FAST_INTERP != 0
  4565. /* frame offset stack */
  4566. int16 *frame_offset;
  4567. int16 *frame_offset_bottom;
  4568. int16 *frame_offset_boundary;
  4569. uint32 frame_offset_size;
  4570. int16 dynamic_offset;
  4571. int16 start_dynamic_offset;
  4572. int16 max_dynamic_offset;
  4573. /* preserved local offset */
  4574. int16 preserved_local_offset;
  4575. /* const buffer */
  4576. uint8 *const_buf;
  4577. uint16 num_const;
  4578. uint16 const_cell_num;
  4579. uint32 const_buf_size;
  4580. /* processed code */
  4581. uint8 *p_code_compiled;
  4582. uint8 *p_code_compiled_end;
  4583. uint32 code_compiled_size;
  4584. /* If the last opcode will be dropped, the peak memory usage will be larger
  4585. * than the final code_compiled_size, we record the peak size to ensure
  4586. * there will not be invalid memory access during second traverse */
  4587. uint32 code_compiled_peak_size;
  4588. #endif
  4589. } WASMLoaderContext;
  4590. typedef struct Const {
  4591. WASMValue value;
  4592. uint16 slot_index;
  4593. uint8 value_type;
  4594. } Const;
  4595. static void *
  4596. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  4597. uint32 error_buf_size)
  4598. {
  4599. uint8 *mem_new;
  4600. bh_assert(size_new > size_old);
  4601. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  4602. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  4603. memset(mem_new + size_old, 0, size_new - size_old);
  4604. wasm_runtime_free(mem_old);
  4605. }
  4606. return mem_new;
  4607. }
  4608. #define MEM_REALLOC(mem, size_old, size_new) \
  4609. do { \
  4610. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  4611. error_buf_size); \
  4612. if (!mem_new) \
  4613. goto fail; \
  4614. mem = mem_new; \
  4615. } while (0)
  4616. #define CHECK_CSP_PUSH() \
  4617. do { \
  4618. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  4619. MEM_REALLOC( \
  4620. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  4621. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  4622. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  4623. ctx->frame_csp_boundary = \
  4624. ctx->frame_csp_bottom \
  4625. + ctx->frame_csp_size / sizeof(BranchBlock); \
  4626. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  4627. } \
  4628. } while (0)
  4629. #define CHECK_CSP_POP() \
  4630. do { \
  4631. if (ctx->csp_num < 1) { \
  4632. set_error_buf(error_buf, error_buf_size, \
  4633. "type mismatch: " \
  4634. "expect data but block stack was empty"); \
  4635. goto fail; \
  4636. } \
  4637. } while (0)
  4638. #if WASM_ENABLE_FAST_INTERP != 0
  4639. static bool
  4640. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  4641. uint32 error_buf_size)
  4642. {
  4643. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  4644. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  4645. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  4646. ctx->frame_offset_size + 16);
  4647. ctx->frame_offset_size += 16;
  4648. ctx->frame_offset_boundary =
  4649. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  4650. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  4651. }
  4652. return true;
  4653. fail:
  4654. return false;
  4655. }
  4656. static bool
  4657. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  4658. {
  4659. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  4660. return false;
  4661. return true;
  4662. }
  4663. static void
  4664. free_label_patch_list(BranchBlock *frame_csp)
  4665. {
  4666. BranchBlockPatch *label_patch = frame_csp->patch_list;
  4667. BranchBlockPatch *next;
  4668. while (label_patch != NULL) {
  4669. next = label_patch->next;
  4670. wasm_runtime_free(label_patch);
  4671. label_patch = next;
  4672. }
  4673. frame_csp->patch_list = NULL;
  4674. }
  4675. static void
  4676. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  4677. {
  4678. BranchBlock *tmp_csp = frame_csp;
  4679. for (uint32 i = 0; i < csp_num; i++) {
  4680. free_label_patch_list(tmp_csp);
  4681. tmp_csp++;
  4682. }
  4683. }
  4684. #endif /* end of WASM_ENABLE_FAST_INTERP */
  4685. static bool
  4686. check_stack_push(WASMLoaderContext *ctx, char *error_buf, uint32 error_buf_size)
  4687. {
  4688. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  4689. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  4690. ctx->frame_ref_size + 16);
  4691. ctx->frame_ref_size += 16;
  4692. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  4693. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  4694. }
  4695. return true;
  4696. fail:
  4697. return false;
  4698. }
  4699. static bool
  4700. check_stack_top_values(uint8 *frame_ref, int32 stack_cell_num, uint8 type,
  4701. char *error_buf, uint32 error_buf_size)
  4702. {
  4703. if ((is_32bit_type(type) && stack_cell_num < 1)
  4704. || (is_64bit_type(type) && stack_cell_num < 2)
  4705. #if WASM_ENABLE_SIMD != 0
  4706. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4707. || (type == VALUE_TYPE_V128 && stack_cell_num < 4)
  4708. #endif
  4709. #endif
  4710. ) {
  4711. set_error_buf(error_buf, error_buf_size,
  4712. "type mismatch: expect data but stack was empty");
  4713. return false;
  4714. }
  4715. if ((is_32bit_type(type) && *(frame_ref - 1) != type)
  4716. || (is_64bit_type(type)
  4717. && (*(frame_ref - 2) != type || *(frame_ref - 1) != type))
  4718. #if WASM_ENABLE_SIMD != 0
  4719. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4720. || (type == VALUE_TYPE_V128
  4721. && (*(frame_ref - 4) != REF_V128_1 || *(frame_ref - 3) != REF_V128_2
  4722. || *(frame_ref - 2) != REF_V128_3
  4723. || *(frame_ref - 1) != REF_V128_4))
  4724. #endif
  4725. #endif
  4726. ) {
  4727. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  4728. "type mismatch: expect ", type2str(type),
  4729. " but got other");
  4730. return false;
  4731. }
  4732. return true;
  4733. }
  4734. static bool
  4735. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4736. uint32 error_buf_size)
  4737. {
  4738. int32 block_stack_cell_num =
  4739. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  4740. if (block_stack_cell_num > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  4741. /* the stack top is a value of any type, return success */
  4742. return true;
  4743. }
  4744. if (!check_stack_top_values(ctx->frame_ref, block_stack_cell_num, type,
  4745. error_buf, error_buf_size))
  4746. return false;
  4747. return true;
  4748. }
  4749. static void
  4750. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  4751. {
  4752. if (ctx) {
  4753. if (ctx->frame_ref_bottom)
  4754. wasm_runtime_free(ctx->frame_ref_bottom);
  4755. if (ctx->frame_csp_bottom) {
  4756. #if WASM_ENABLE_FAST_INTERP != 0
  4757. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  4758. #endif
  4759. wasm_runtime_free(ctx->frame_csp_bottom);
  4760. }
  4761. #if WASM_ENABLE_FAST_INTERP != 0
  4762. if (ctx->frame_offset_bottom)
  4763. wasm_runtime_free(ctx->frame_offset_bottom);
  4764. if (ctx->const_buf)
  4765. wasm_runtime_free(ctx->const_buf);
  4766. #endif
  4767. wasm_runtime_free(ctx);
  4768. }
  4769. }
  4770. static WASMLoaderContext *
  4771. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  4772. {
  4773. WASMLoaderContext *loader_ctx =
  4774. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  4775. if (!loader_ctx)
  4776. return NULL;
  4777. loader_ctx->frame_ref_size = 32;
  4778. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  4779. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  4780. goto fail;
  4781. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  4782. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  4783. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  4784. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  4785. goto fail;
  4786. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  4787. #if WASM_ENABLE_FAST_INTERP != 0
  4788. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  4789. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  4790. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  4791. error_buf_size)))
  4792. goto fail;
  4793. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  4794. loader_ctx->num_const = 0;
  4795. loader_ctx->const_buf_size = sizeof(Const) * 8;
  4796. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  4797. error_buf, error_buf_size)))
  4798. goto fail;
  4799. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  4800. set_error_buf(error_buf, error_buf_size,
  4801. "fast interpreter offset overflow");
  4802. goto fail;
  4803. }
  4804. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  4805. loader_ctx->max_dynamic_offset =
  4806. func->param_cell_num + func->local_cell_num;
  4807. #endif
  4808. return loader_ctx;
  4809. fail:
  4810. wasm_loader_ctx_destroy(loader_ctx);
  4811. return NULL;
  4812. }
  4813. static bool
  4814. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4815. uint32 error_buf_size)
  4816. {
  4817. if (type == VALUE_TYPE_VOID)
  4818. return true;
  4819. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4820. return false;
  4821. *ctx->frame_ref++ = type;
  4822. ctx->stack_cell_num++;
  4823. if (is_32bit_type(type) || type == VALUE_TYPE_ANY)
  4824. goto check_stack_and_return;
  4825. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4826. return false;
  4827. *ctx->frame_ref++ = type;
  4828. ctx->stack_cell_num++;
  4829. #if WASM_ENABLE_SIMD != 0
  4830. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4831. if (type == VALUE_TYPE_V128) {
  4832. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4833. return false;
  4834. *ctx->frame_ref++ = type;
  4835. ctx->stack_cell_num++;
  4836. if (!check_stack_push(ctx, error_buf, error_buf_size))
  4837. return false;
  4838. *ctx->frame_ref++ = type;
  4839. ctx->stack_cell_num++;
  4840. }
  4841. #endif
  4842. #endif
  4843. check_stack_and_return:
  4844. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  4845. ctx->max_stack_cell_num = ctx->stack_cell_num;
  4846. if (ctx->max_stack_cell_num > UINT16_MAX) {
  4847. set_error_buf(error_buf, error_buf_size,
  4848. "operand stack depth limit exceeded");
  4849. return false;
  4850. }
  4851. }
  4852. return true;
  4853. }
  4854. static bool
  4855. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  4856. uint32 error_buf_size)
  4857. {
  4858. BranchBlock *cur_block = ctx->frame_csp - 1;
  4859. int32 available_stack_cell =
  4860. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  4861. /* Directly return success if current block is in stack
  4862. * polymorphic state while stack is empty. */
  4863. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  4864. return true;
  4865. if (type == VALUE_TYPE_VOID)
  4866. return true;
  4867. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  4868. return false;
  4869. ctx->frame_ref--;
  4870. ctx->stack_cell_num--;
  4871. if (is_32bit_type(type) || *ctx->frame_ref == VALUE_TYPE_ANY)
  4872. return true;
  4873. ctx->frame_ref--;
  4874. ctx->stack_cell_num--;
  4875. #if WASM_ENABLE_SIMD != 0
  4876. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  4877. if (type == VALUE_TYPE_V128) {
  4878. ctx->frame_ref -= 2;
  4879. ctx->stack_cell_num -= 2;
  4880. }
  4881. #endif
  4882. #endif
  4883. return true;
  4884. }
  4885. static bool
  4886. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  4887. uint8 type_push, uint8 type_pop, char *error_buf,
  4888. uint32 error_buf_size)
  4889. {
  4890. for (int i = 0; i < pop_cnt; i++) {
  4891. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  4892. error_buf_size))
  4893. return false;
  4894. }
  4895. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  4896. return false;
  4897. return true;
  4898. }
  4899. static bool
  4900. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  4901. BlockType block_type, uint8 *start_addr,
  4902. char *error_buf, uint32 error_buf_size)
  4903. {
  4904. CHECK_CSP_PUSH();
  4905. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  4906. ctx->frame_csp->label_type = label_type;
  4907. ctx->frame_csp->block_type = block_type;
  4908. ctx->frame_csp->start_addr = start_addr;
  4909. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  4910. #if WASM_ENABLE_FAST_INTERP != 0
  4911. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  4912. ctx->frame_csp->patch_list = NULL;
  4913. #endif
  4914. ctx->frame_csp++;
  4915. ctx->csp_num++;
  4916. if (ctx->csp_num > ctx->max_csp_num) {
  4917. ctx->max_csp_num = ctx->csp_num;
  4918. if (ctx->max_csp_num > UINT16_MAX) {
  4919. set_error_buf(error_buf, error_buf_size,
  4920. "label stack depth limit exceeded");
  4921. return false;
  4922. }
  4923. }
  4924. return true;
  4925. fail:
  4926. return false;
  4927. }
  4928. static bool
  4929. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  4930. uint32 error_buf_size)
  4931. {
  4932. CHECK_CSP_POP();
  4933. #if WASM_ENABLE_FAST_INTERP != 0
  4934. if ((ctx->frame_csp - 1)->param_frame_offsets)
  4935. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  4936. #endif
  4937. ctx->frame_csp--;
  4938. ctx->csp_num--;
  4939. return true;
  4940. fail:
  4941. return false;
  4942. }
  4943. #if WASM_ENABLE_FAST_INTERP != 0
  4944. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  4945. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  4946. #define emit_label(opcode) \
  4947. do { \
  4948. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  4949. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4950. } while (0)
  4951. #define skip_label() \
  4952. do { \
  4953. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  4954. LOG_OP("\ndelete last op\n"); \
  4955. } while (0)
  4956. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4957. #define emit_label(opcode) \
  4958. do { \
  4959. int32 offset = \
  4960. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  4961. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  4962. set_error_buf(error_buf, error_buf_size, \
  4963. "pre-compiled label offset out of range"); \
  4964. goto fail; \
  4965. } \
  4966. wasm_loader_emit_int16(loader_ctx, offset); \
  4967. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4968. } while (0)
  4969. #define skip_label() \
  4970. do { \
  4971. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  4972. LOG_OP("\ndelete last op\n"); \
  4973. } while (0)
  4974. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  4975. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  4976. #define emit_label(opcode) \
  4977. do { \
  4978. wasm_loader_emit_uint8(loader_ctx, opcode); \
  4979. LOG_OP("\nemit_op [%02x]\t", opcode); \
  4980. } while (0)
  4981. #define skip_label() \
  4982. do { \
  4983. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  4984. LOG_OP("\ndelete last op\n"); \
  4985. } while (0)
  4986. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  4987. #define emit_empty_label_addr_and_frame_ip(type) \
  4988. do { \
  4989. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  4990. loader_ctx->p_code_compiled, error_buf, \
  4991. error_buf_size)) \
  4992. goto fail; \
  4993. /* label address, to be patched */ \
  4994. wasm_loader_emit_ptr(loader_ctx, NULL); \
  4995. } while (0)
  4996. #define emit_br_info(frame_csp) \
  4997. do { \
  4998. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, error_buf, \
  4999. error_buf_size)) \
  5000. goto fail; \
  5001. } while (0)
  5002. #define LAST_OP_OUTPUT_I32() \
  5003. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  5004. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  5005. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  5006. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  5007. || (last_op >= WASM_OP_I32_WRAP_I64 \
  5008. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  5009. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  5010. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  5011. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  5012. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  5013. || (last_op == EXT_OP_COPY_STACK_TOP)
  5014. #define LAST_OP_OUTPUT_I64() \
  5015. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  5016. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  5017. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  5018. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  5019. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  5020. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  5021. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  5022. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  5023. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  5024. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  5025. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  5026. #define GET_CONST_OFFSET(type, val) \
  5027. do { \
  5028. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  5029. &operand_offset, error_buf, \
  5030. error_buf_size))) \
  5031. goto fail; \
  5032. } while (0)
  5033. #define GET_CONST_F32_OFFSET(type, fval) \
  5034. do { \
  5035. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  5036. &operand_offset, error_buf, \
  5037. error_buf_size))) \
  5038. goto fail; \
  5039. } while (0)
  5040. #define GET_CONST_F64_OFFSET(type, fval) \
  5041. do { \
  5042. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  5043. &operand_offset, error_buf, \
  5044. error_buf_size))) \
  5045. goto fail; \
  5046. } while (0)
  5047. #define emit_operand(ctx, offset) \
  5048. do { \
  5049. wasm_loader_emit_int16(ctx, offset); \
  5050. LOG_OP("%d\t", offset); \
  5051. } while (0)
  5052. #define emit_byte(ctx, byte) \
  5053. do { \
  5054. wasm_loader_emit_uint8(ctx, byte); \
  5055. LOG_OP("%d\t", byte); \
  5056. } while (0)
  5057. #define emit_uint32(ctx, value) \
  5058. do { \
  5059. wasm_loader_emit_uint32(ctx, value); \
  5060. LOG_OP("%d\t", value); \
  5061. } while (0)
  5062. #define emit_uint64(ctx, value) \
  5063. do { \
  5064. wasm_loader_emit_const(ctx, &value, false); \
  5065. LOG_OP("%lld\t", value); \
  5066. } while (0)
  5067. #define emit_float32(ctx, value) \
  5068. do { \
  5069. wasm_loader_emit_const(ctx, &value, true); \
  5070. LOG_OP("%f\t", value); \
  5071. } while (0)
  5072. #define emit_float64(ctx, value) \
  5073. do { \
  5074. wasm_loader_emit_const(ctx, &value, false); \
  5075. LOG_OP("%f\t", value); \
  5076. } while (0)
  5077. static bool
  5078. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  5079. {
  5080. if (!(ctx->p_code_compiled =
  5081. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  5082. return false;
  5083. ctx->p_code_compiled_end =
  5084. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  5085. /* clean up frame ref */
  5086. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  5087. ctx->frame_ref = ctx->frame_ref_bottom;
  5088. ctx->stack_cell_num = 0;
  5089. /* clean up frame csp */
  5090. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  5091. ctx->frame_csp = ctx->frame_csp_bottom;
  5092. ctx->csp_num = 0;
  5093. ctx->max_csp_num = 0;
  5094. /* clean up frame offset */
  5095. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  5096. ctx->frame_offset = ctx->frame_offset_bottom;
  5097. ctx->dynamic_offset = ctx->start_dynamic_offset;
  5098. /* init preserved local offsets */
  5099. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  5100. /* const buf is reserved */
  5101. return true;
  5102. }
  5103. static void
  5104. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  5105. {
  5106. ctx->code_compiled_size += size;
  5107. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  5108. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  5109. }
  5110. }
  5111. static void
  5112. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  5113. {
  5114. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  5115. if (ctx->p_code_compiled) {
  5116. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5117. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5118. #endif
  5119. bh_memcpy_s(ctx->p_code_compiled,
  5120. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  5121. value, size);
  5122. ctx->p_code_compiled += size;
  5123. }
  5124. else {
  5125. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5126. bh_assert((ctx->code_compiled_size & 1) == 0);
  5127. #endif
  5128. increase_compiled_code_space(ctx, size);
  5129. }
  5130. }
  5131. static void
  5132. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  5133. {
  5134. if (ctx->p_code_compiled) {
  5135. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5136. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5137. #endif
  5138. STORE_U32(ctx->p_code_compiled, value);
  5139. ctx->p_code_compiled += sizeof(uint32);
  5140. }
  5141. else {
  5142. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5143. bh_assert((ctx->code_compiled_size & 1) == 0);
  5144. #endif
  5145. increase_compiled_code_space(ctx, sizeof(uint32));
  5146. }
  5147. }
  5148. static void
  5149. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  5150. {
  5151. if (ctx->p_code_compiled) {
  5152. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5153. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5154. #endif
  5155. STORE_U16(ctx->p_code_compiled, (uint16)value);
  5156. ctx->p_code_compiled += sizeof(int16);
  5157. }
  5158. else {
  5159. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5160. bh_assert((ctx->code_compiled_size & 1) == 0);
  5161. #endif
  5162. increase_compiled_code_space(ctx, sizeof(uint16));
  5163. }
  5164. }
  5165. static void
  5166. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  5167. {
  5168. if (ctx->p_code_compiled) {
  5169. *(ctx->p_code_compiled) = value;
  5170. ctx->p_code_compiled += sizeof(uint8);
  5171. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5172. ctx->p_code_compiled++;
  5173. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5174. #endif
  5175. }
  5176. else {
  5177. increase_compiled_code_space(ctx, sizeof(uint8));
  5178. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5179. increase_compiled_code_space(ctx, sizeof(uint8));
  5180. bh_assert((ctx->code_compiled_size & 1) == 0);
  5181. #endif
  5182. }
  5183. }
  5184. static void
  5185. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  5186. {
  5187. if (ctx->p_code_compiled) {
  5188. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5189. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5190. #endif
  5191. STORE_PTR(ctx->p_code_compiled, value);
  5192. ctx->p_code_compiled += sizeof(void *);
  5193. }
  5194. else {
  5195. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5196. bh_assert((ctx->code_compiled_size & 1) == 0);
  5197. #endif
  5198. increase_compiled_code_space(ctx, sizeof(void *));
  5199. }
  5200. }
  5201. static void
  5202. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  5203. {
  5204. if (ctx->p_code_compiled) {
  5205. ctx->p_code_compiled -= size;
  5206. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5207. if (size == sizeof(uint8)) {
  5208. ctx->p_code_compiled--;
  5209. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  5210. }
  5211. #endif
  5212. }
  5213. else {
  5214. ctx->code_compiled_size -= size;
  5215. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5216. if (size == sizeof(uint8)) {
  5217. ctx->code_compiled_size--;
  5218. bh_assert((ctx->code_compiled_size & 1) == 0);
  5219. }
  5220. #endif
  5221. }
  5222. }
  5223. static bool
  5224. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  5225. uint32 local_index, uint32 local_type,
  5226. bool *preserved, char *error_buf,
  5227. uint32 error_buf_size)
  5228. {
  5229. uint32 i = 0;
  5230. int16 preserved_offset = (int16)local_index;
  5231. *preserved = false;
  5232. while (i < loader_ctx->stack_cell_num) {
  5233. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  5234. /* move previous local into dynamic space before a set/tee_local opcode
  5235. */
  5236. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  5237. if (!(*preserved)) {
  5238. *preserved = true;
  5239. skip_label();
  5240. preserved_offset = loader_ctx->preserved_local_offset;
  5241. if (loader_ctx->p_code_compiled) {
  5242. bh_assert(preserved_offset != (int16)local_index);
  5243. }
  5244. if (is_32bit_type(local_type)) {
  5245. /* Only increase preserve offset in the second traversal */
  5246. if (loader_ctx->p_code_compiled)
  5247. loader_ctx->preserved_local_offset++;
  5248. emit_label(EXT_OP_COPY_STACK_TOP);
  5249. }
  5250. else {
  5251. if (loader_ctx->p_code_compiled)
  5252. loader_ctx->preserved_local_offset += 2;
  5253. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  5254. }
  5255. emit_operand(loader_ctx, local_index);
  5256. emit_operand(loader_ctx, preserved_offset);
  5257. emit_label(opcode);
  5258. }
  5259. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  5260. }
  5261. if (is_32bit_type(cur_type))
  5262. i++;
  5263. else
  5264. i += 2;
  5265. }
  5266. (void)error_buf;
  5267. (void)error_buf_size;
  5268. return true;
  5269. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  5270. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  5271. fail:
  5272. return false;
  5273. #endif
  5274. #endif
  5275. }
  5276. static bool
  5277. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  5278. char *error_buf, uint32 error_buf_size)
  5279. {
  5280. uint32 i = 0;
  5281. bool preserve_local;
  5282. /* preserve locals before blocks to ensure that "tee/set_local" inside
  5283. blocks will not influence the value of these locals */
  5284. while (i < loader_ctx->stack_cell_num) {
  5285. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  5286. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  5287. if ((cur_offset < loader_ctx->start_dynamic_offset)
  5288. && (cur_offset >= 0)) {
  5289. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  5290. cur_type, &preserve_local,
  5291. error_buf, error_buf_size)))
  5292. return false;
  5293. }
  5294. if (is_32bit_type(cur_type)) {
  5295. i++;
  5296. }
  5297. else {
  5298. i += 2;
  5299. }
  5300. }
  5301. return true;
  5302. }
  5303. static bool
  5304. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  5305. uint8 *p_code_compiled, char *error_buf,
  5306. uint32 error_buf_size)
  5307. {
  5308. BranchBlockPatch *patch =
  5309. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  5310. if (!patch) {
  5311. return false;
  5312. }
  5313. patch->patch_type = patch_type;
  5314. patch->code_compiled = p_code_compiled;
  5315. if (!frame_csp->patch_list) {
  5316. frame_csp->patch_list = patch;
  5317. patch->next = NULL;
  5318. }
  5319. else {
  5320. patch->next = frame_csp->patch_list;
  5321. frame_csp->patch_list = patch;
  5322. }
  5323. return true;
  5324. }
  5325. static void
  5326. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  5327. {
  5328. BranchBlock *frame_csp = ctx->frame_csp - depth;
  5329. BranchBlockPatch *node = frame_csp->patch_list;
  5330. BranchBlockPatch *node_prev = NULL, *node_next;
  5331. if (!ctx->p_code_compiled)
  5332. return;
  5333. while (node) {
  5334. node_next = node->next;
  5335. if (node->patch_type == patch_type) {
  5336. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  5337. if (node_prev == NULL) {
  5338. frame_csp->patch_list = node_next;
  5339. }
  5340. else {
  5341. node_prev->next = node_next;
  5342. }
  5343. wasm_runtime_free(node);
  5344. }
  5345. else {
  5346. node_prev = node;
  5347. }
  5348. node = node_next;
  5349. }
  5350. }
  5351. static bool
  5352. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  5353. char *error_buf, uint32 error_buf_size)
  5354. {
  5355. /* br info layout:
  5356. * a) arity of target block
  5357. * b) total cell num of arity values
  5358. * c) each arity value's cell num
  5359. * d) each arity value's src frame offset
  5360. * e) each arity values's dst dynamic offset
  5361. * f) branch target address
  5362. *
  5363. * Note: b-e are omitted when arity is 0 so that
  5364. * interpreter can recover the br info quickly.
  5365. */
  5366. BlockType *block_type = &frame_csp->block_type;
  5367. uint8 *types = NULL, cell;
  5368. uint32 arity = 0;
  5369. int32 i;
  5370. int16 *frame_offset = ctx->frame_offset;
  5371. uint16 dynamic_offset;
  5372. /* Note: loop's arity is different from if and block. loop's arity is
  5373. * its parameter count while if and block arity is result count.
  5374. */
  5375. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  5376. arity = block_type_get_param_types(block_type, &types);
  5377. else
  5378. arity = block_type_get_result_types(block_type, &types);
  5379. /* Part a */
  5380. emit_uint32(ctx, arity);
  5381. if (arity) {
  5382. /* Part b */
  5383. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  5384. /* Part c */
  5385. for (i = (int32)arity - 1; i >= 0; i--) {
  5386. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5387. emit_byte(ctx, cell);
  5388. }
  5389. /* Part d */
  5390. for (i = (int32)arity - 1; i >= 0; i--) {
  5391. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5392. frame_offset -= cell;
  5393. emit_operand(ctx, *(int16 *)(frame_offset));
  5394. }
  5395. /* Part e */
  5396. dynamic_offset =
  5397. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  5398. for (i = (int32)arity - 1; i >= 0; i--) {
  5399. cell = (uint8)wasm_value_type_cell_num(types[i]);
  5400. dynamic_offset -= cell;
  5401. emit_operand(ctx, dynamic_offset);
  5402. }
  5403. }
  5404. /* Part f */
  5405. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  5406. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  5407. }
  5408. else {
  5409. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  5410. error_buf, error_buf_size))
  5411. return false;
  5412. /* label address, to be patched */
  5413. wasm_loader_emit_ptr(ctx, NULL);
  5414. }
  5415. return true;
  5416. }
  5417. static bool
  5418. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  5419. bool disable_emit, int16 operand_offset,
  5420. char *error_buf, uint32 error_buf_size)
  5421. {
  5422. if (type == VALUE_TYPE_VOID)
  5423. return true;
  5424. /* only check memory overflow in first traverse */
  5425. if (ctx->p_code_compiled == NULL) {
  5426. if (!check_offset_push(ctx, error_buf, error_buf_size))
  5427. return false;
  5428. }
  5429. if (disable_emit)
  5430. *(ctx->frame_offset)++ = operand_offset;
  5431. else {
  5432. emit_operand(ctx, ctx->dynamic_offset);
  5433. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  5434. ctx->dynamic_offset++;
  5435. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  5436. ctx->max_dynamic_offset = ctx->dynamic_offset;
  5437. if (ctx->max_dynamic_offset >= INT16_MAX) {
  5438. goto fail;
  5439. }
  5440. }
  5441. }
  5442. if (is_32bit_type(type))
  5443. return true;
  5444. if (ctx->p_code_compiled == NULL) {
  5445. if (!check_offset_push(ctx, error_buf, error_buf_size))
  5446. return false;
  5447. }
  5448. ctx->frame_offset++;
  5449. if (!disable_emit) {
  5450. ctx->dynamic_offset++;
  5451. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  5452. ctx->max_dynamic_offset = ctx->dynamic_offset;
  5453. if (ctx->max_dynamic_offset >= INT16_MAX) {
  5454. goto fail;
  5455. }
  5456. }
  5457. }
  5458. return true;
  5459. fail:
  5460. set_error_buf(error_buf, error_buf_size,
  5461. "fast interpreter offset overflow");
  5462. return false;
  5463. }
  5464. /* This function should be in front of wasm_loader_pop_frame_ref
  5465. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  5466. will be modified by wasm_loader_pop_frame_ref */
  5467. static bool
  5468. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  5469. char *error_buf, uint32 error_buf_size)
  5470. {
  5471. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  5472. then current block is the function block */
  5473. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  5474. BranchBlock *cur_block = ctx->frame_csp - depth;
  5475. int32 available_stack_cell =
  5476. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  5477. /* Directly return success if current block is in stack
  5478. * polymorphic state while stack is empty. */
  5479. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  5480. return true;
  5481. if (type == VALUE_TYPE_VOID)
  5482. return true;
  5483. if (is_32bit_type(type)) {
  5484. /* Check the offset stack bottom to ensure the frame offset
  5485. stack will not go underflow. But we don't thrown error
  5486. and return true here, because the error msg should be
  5487. given in wasm_loader_pop_frame_ref */
  5488. if (!check_offset_pop(ctx, 1))
  5489. return true;
  5490. ctx->frame_offset -= 1;
  5491. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  5492. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  5493. ctx->dynamic_offset -= 1;
  5494. }
  5495. else {
  5496. if (!check_offset_pop(ctx, 2))
  5497. return true;
  5498. ctx->frame_offset -= 2;
  5499. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  5500. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  5501. ctx->dynamic_offset -= 2;
  5502. }
  5503. emit_operand(ctx, *(ctx->frame_offset));
  5504. (void)error_buf;
  5505. (void)error_buf_size;
  5506. return true;
  5507. }
  5508. static bool
  5509. wasm_loader_push_pop_frame_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5510. uint8 type_push, uint8 type_pop,
  5511. bool disable_emit, int16 operand_offset,
  5512. char *error_buf, uint32 error_buf_size)
  5513. {
  5514. uint8 i;
  5515. for (i = 0; i < pop_cnt; i++) {
  5516. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  5517. error_buf_size))
  5518. return false;
  5519. }
  5520. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  5521. operand_offset, error_buf,
  5522. error_buf_size))
  5523. return false;
  5524. return true;
  5525. }
  5526. static bool
  5527. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5528. bool disable_emit, int16 operand_offset,
  5529. char *error_buf, uint32 error_buf_size)
  5530. {
  5531. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  5532. error_buf, error_buf_size)))
  5533. return false;
  5534. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  5535. return false;
  5536. return true;
  5537. }
  5538. static bool
  5539. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  5540. char *error_buf, uint32 error_buf_size)
  5541. {
  5542. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  5543. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  5544. return false;
  5545. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  5546. return false;
  5547. return true;
  5548. }
  5549. static bool
  5550. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  5551. uint8 type_push, uint8 type_pop,
  5552. bool disable_emit, int16 operand_offset,
  5553. char *error_buf, uint32 error_buf_size)
  5554. {
  5555. if (!wasm_loader_push_pop_frame_offset(ctx, pop_cnt, type_push, type_pop,
  5556. disable_emit, operand_offset,
  5557. error_buf, error_buf_size))
  5558. return false;
  5559. if (!wasm_loader_push_pop_frame_ref(ctx, pop_cnt, type_push, type_pop,
  5560. error_buf, error_buf_size))
  5561. return false;
  5562. return true;
  5563. }
  5564. static bool
  5565. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  5566. int16 *offset, char *error_buf,
  5567. uint32 error_buf_size)
  5568. {
  5569. int8 bytes_to_increase;
  5570. int16 operand_offset = 0;
  5571. Const *c;
  5572. /* Search existing constant */
  5573. for (c = (Const *)ctx->const_buf;
  5574. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  5575. /* TODO: handle v128 type? */
  5576. if ((type == c->value_type)
  5577. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  5578. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  5579. #if WASM_ENABLE_REF_TYPES != 0
  5580. || (type == VALUE_TYPE_FUNCREF
  5581. && *(int32 *)value == c->value.i32)
  5582. || (type == VALUE_TYPE_EXTERNREF
  5583. && *(int32 *)value == c->value.i32)
  5584. #endif
  5585. || (type == VALUE_TYPE_F64
  5586. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  5587. || (type == VALUE_TYPE_F32
  5588. && (0
  5589. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  5590. operand_offset = c->slot_index;
  5591. break;
  5592. }
  5593. if (is_32bit_type(c->value_type))
  5594. operand_offset += 1;
  5595. else
  5596. operand_offset += 2;
  5597. }
  5598. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  5599. /* New constant, append to the const buffer */
  5600. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  5601. bytes_to_increase = 2;
  5602. }
  5603. else {
  5604. bytes_to_increase = 1;
  5605. }
  5606. /* The max cell num of const buffer is 32768 since the valid index range
  5607. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  5608. * full */
  5609. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  5610. *offset = 0;
  5611. return true;
  5612. }
  5613. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  5614. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  5615. ctx->const_buf_size + 4 * sizeof(Const));
  5616. ctx->const_buf_size += 4 * sizeof(Const);
  5617. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  5618. }
  5619. c->value_type = type;
  5620. switch (type) {
  5621. case VALUE_TYPE_F64:
  5622. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  5623. sizeof(float64));
  5624. ctx->const_cell_num += 2;
  5625. /* The const buf will be reversed, we use the second cell */
  5626. /* of the i64/f64 const so the finnal offset is corrent */
  5627. operand_offset++;
  5628. break;
  5629. case VALUE_TYPE_I64:
  5630. c->value.i64 = *(int64 *)value;
  5631. ctx->const_cell_num += 2;
  5632. operand_offset++;
  5633. break;
  5634. case VALUE_TYPE_F32:
  5635. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  5636. sizeof(float32));
  5637. ctx->const_cell_num++;
  5638. break;
  5639. case VALUE_TYPE_I32:
  5640. c->value.i32 = *(int32 *)value;
  5641. ctx->const_cell_num++;
  5642. break;
  5643. #if WASM_ENABLE_REF_TYPES != 0
  5644. case VALUE_TYPE_EXTERNREF:
  5645. case VALUE_TYPE_FUNCREF:
  5646. c->value.i32 = *(int32 *)value;
  5647. ctx->const_cell_num++;
  5648. break;
  5649. #endif
  5650. default:
  5651. break;
  5652. }
  5653. c->slot_index = operand_offset;
  5654. ctx->num_const++;
  5655. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  5656. (int64)c->value.i64);
  5657. }
  5658. /* use negetive index for const */
  5659. operand_offset = -(operand_offset + 1);
  5660. *offset = operand_offset;
  5661. return true;
  5662. fail:
  5663. return false;
  5664. }
  5665. /*
  5666. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  5667. -- Mostly used for the binary / compare operation
  5668. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  5669. -- Mostly used in block / control instructions
  5670. The POP will always emit the offset on the top of the frame_offset stack
  5671. PUSH can be used in two ways:
  5672. 1. directly PUSH:
  5673. PUSH_XXX();
  5674. will allocate a dynamic space and emit
  5675. 2. silent PUSH:
  5676. operand_offset = xxx; disable_emit = true;
  5677. PUSH_XXX();
  5678. only push the frame_offset stack, no emit
  5679. */
  5680. #define TEMPLATE_PUSH(Type) \
  5681. do { \
  5682. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5683. disable_emit, operand_offset, \
  5684. error_buf, error_buf_size)) \
  5685. goto fail; \
  5686. } while (0)
  5687. #define TEMPLATE_POP(Type) \
  5688. do { \
  5689. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  5690. error_buf, error_buf_size)) \
  5691. goto fail; \
  5692. } while (0)
  5693. #define PUSH_OFFSET_TYPE(type) \
  5694. do { \
  5695. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  5696. operand_offset, error_buf, \
  5697. error_buf_size))) \
  5698. goto fail; \
  5699. } while (0)
  5700. #define POP_OFFSET_TYPE(type) \
  5701. do { \
  5702. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  5703. error_buf_size))) \
  5704. goto fail; \
  5705. } while (0)
  5706. #define POP_AND_PUSH(type_pop, type_push) \
  5707. do { \
  5708. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5709. loader_ctx, 1, type_push, type_pop, disable_emit, \
  5710. operand_offset, error_buf, error_buf_size))) \
  5711. goto fail; \
  5712. } while (0)
  5713. /* type of POPs should be the same */
  5714. #define POP2_AND_PUSH(type_pop, type_push) \
  5715. do { \
  5716. if (!(wasm_loader_push_pop_frame_ref_offset( \
  5717. loader_ctx, 2, type_push, type_pop, disable_emit, \
  5718. operand_offset, error_buf, error_buf_size))) \
  5719. goto fail; \
  5720. } while (0)
  5721. #else /* WASM_ENABLE_FAST_INTERP */
  5722. #define TEMPLATE_PUSH(Type) \
  5723. do { \
  5724. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5725. error_buf, error_buf_size))) \
  5726. goto fail; \
  5727. } while (0)
  5728. #define TEMPLATE_POP(Type) \
  5729. do { \
  5730. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  5731. error_buf, error_buf_size))) \
  5732. goto fail; \
  5733. } while (0)
  5734. #define POP_AND_PUSH(type_pop, type_push) \
  5735. do { \
  5736. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  5737. type_pop, error_buf, \
  5738. error_buf_size))) \
  5739. goto fail; \
  5740. } while (0)
  5741. /* type of POPs should be the same */
  5742. #define POP2_AND_PUSH(type_pop, type_push) \
  5743. do { \
  5744. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  5745. type_pop, error_buf, \
  5746. error_buf_size))) \
  5747. goto fail; \
  5748. } while (0)
  5749. #endif /* WASM_ENABLE_FAST_INTERP */
  5750. #define PUSH_I32() TEMPLATE_PUSH(I32)
  5751. #define PUSH_F32() TEMPLATE_PUSH(F32)
  5752. #define PUSH_I64() TEMPLATE_PUSH(I64)
  5753. #define PUSH_F64() TEMPLATE_PUSH(F64)
  5754. #define PUSH_V128() TEMPLATE_PUSH(V128)
  5755. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  5756. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  5757. #define POP_I32() TEMPLATE_POP(I32)
  5758. #define POP_F32() TEMPLATE_POP(F32)
  5759. #define POP_I64() TEMPLATE_POP(I64)
  5760. #define POP_F64() TEMPLATE_POP(F64)
  5761. #define POP_V128() TEMPLATE_POP(V128)
  5762. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  5763. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  5764. #if WASM_ENABLE_FAST_INTERP != 0
  5765. static bool
  5766. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  5767. bool disable_emit, char *error_buf, uint32 error_buf_size)
  5768. {
  5769. int16 operand_offset = 0;
  5770. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  5771. : loader_ctx->frame_csp;
  5772. BlockType *block_type = &block->block_type;
  5773. uint8 *return_types = NULL;
  5774. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  5775. int32 i = 0;
  5776. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  5777. *frame_offset_org = NULL;
  5778. return_count = block_type_get_result_types(block_type, &return_types);
  5779. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  5780. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  5781. if (return_count == 1) {
  5782. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  5783. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  5784. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  5785. /* insert op_copy before else opcode */
  5786. if (opcode == WASM_OP_ELSE)
  5787. skip_label();
  5788. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  5789. : EXT_OP_COPY_STACK_TOP_I64);
  5790. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  5791. emit_operand(loader_ctx, block->dynamic_offset);
  5792. if (opcode == WASM_OP_ELSE) {
  5793. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  5794. }
  5795. else {
  5796. loader_ctx->frame_offset -= cell;
  5797. loader_ctx->dynamic_offset = block->dynamic_offset;
  5798. PUSH_OFFSET_TYPE(return_types[0]);
  5799. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5800. }
  5801. if (opcode == WASM_OP_ELSE)
  5802. emit_label(opcode);
  5803. }
  5804. return true;
  5805. }
  5806. /* Copy stack top values to block's results which are in dynamic space.
  5807. * The instruction format:
  5808. * Part a: values count
  5809. * Part b: all values total cell num
  5810. * Part c: each value's cell_num, src offset and dst offset
  5811. * Part d: each value's src offset and dst offset
  5812. * Part e: each value's dst offset
  5813. */
  5814. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  5815. dynamic_offset = dynamic_offset_org =
  5816. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  5817. /* First traversal to get the count of values needed to be copied. */
  5818. for (i = (int32)return_count - 1; i >= 0; i--) {
  5819. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  5820. frame_offset -= cells;
  5821. dynamic_offset -= cells;
  5822. if (dynamic_offset != *frame_offset) {
  5823. value_count++;
  5824. total_cel_num += cells;
  5825. }
  5826. }
  5827. if (value_count) {
  5828. uint32 j = 0;
  5829. uint8 *emit_data = NULL, *cells = NULL;
  5830. int16 *src_offsets = NULL;
  5831. uint16 *dst_offsets = NULL;
  5832. uint64 size =
  5833. (uint64)value_count
  5834. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  5835. /* Allocate memory for the emit data */
  5836. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  5837. return false;
  5838. cells = emit_data;
  5839. src_offsets = (int16 *)(cells + value_count);
  5840. dst_offsets = (uint16 *)(src_offsets + value_count);
  5841. /* insert op_copy before else opcode */
  5842. if (opcode == WASM_OP_ELSE)
  5843. skip_label();
  5844. emit_label(EXT_OP_COPY_STACK_VALUES);
  5845. /* Part a) */
  5846. emit_uint32(loader_ctx, value_count);
  5847. /* Part b) */
  5848. emit_uint32(loader_ctx, total_cel_num);
  5849. /* Second traversal to get each value's cell num, src offset and dst
  5850. * offset. */
  5851. frame_offset = frame_offset_org;
  5852. dynamic_offset = dynamic_offset_org;
  5853. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  5854. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  5855. frame_offset -= cell;
  5856. dynamic_offset -= cell;
  5857. if (dynamic_offset != *frame_offset) {
  5858. /* cell num */
  5859. cells[j] = cell;
  5860. /* src offset */
  5861. src_offsets[j] = *frame_offset;
  5862. /* dst offset */
  5863. dst_offsets[j] = dynamic_offset;
  5864. j++;
  5865. }
  5866. if (opcode == WASM_OP_ELSE) {
  5867. *frame_offset = dynamic_offset;
  5868. }
  5869. else {
  5870. loader_ctx->frame_offset = frame_offset;
  5871. loader_ctx->dynamic_offset = dynamic_offset;
  5872. PUSH_OFFSET_TYPE(return_types[i]);
  5873. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  5874. loader_ctx->frame_offset = frame_offset_org;
  5875. loader_ctx->dynamic_offset = dynamic_offset_org;
  5876. }
  5877. }
  5878. bh_assert(j == value_count);
  5879. /* Emit the cells, src_offsets and dst_offsets */
  5880. for (j = 0; j < value_count; j++)
  5881. emit_byte(loader_ctx, cells[j]);
  5882. for (j = 0; j < value_count; j++)
  5883. emit_operand(loader_ctx, src_offsets[j]);
  5884. for (j = 0; j < value_count; j++)
  5885. emit_operand(loader_ctx, dst_offsets[j]);
  5886. if (opcode == WASM_OP_ELSE)
  5887. emit_label(opcode);
  5888. wasm_runtime_free(emit_data);
  5889. }
  5890. return true;
  5891. fail:
  5892. return false;
  5893. }
  5894. #endif /* WASM_ENABLE_FAST_INTERP */
  5895. #define RESERVE_BLOCK_RET() \
  5896. do { \
  5897. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  5898. error_buf_size)) \
  5899. goto fail; \
  5900. } while (0)
  5901. #define PUSH_TYPE(type) \
  5902. do { \
  5903. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  5904. error_buf_size))) \
  5905. goto fail; \
  5906. } while (0)
  5907. #define POP_TYPE(type) \
  5908. do { \
  5909. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  5910. error_buf_size))) \
  5911. goto fail; \
  5912. } while (0)
  5913. #define PUSH_CSP(label_type, block_type, _start_addr) \
  5914. do { \
  5915. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  5916. _start_addr, error_buf, \
  5917. error_buf_size)) \
  5918. goto fail; \
  5919. } while (0)
  5920. #define POP_CSP() \
  5921. do { \
  5922. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  5923. goto fail; \
  5924. } while (0)
  5925. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  5926. do { \
  5927. read_leb_uint32(p, p_end, local_idx); \
  5928. if (local_idx >= param_count + local_count) { \
  5929. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  5930. goto fail; \
  5931. } \
  5932. local_type = local_idx < param_count \
  5933. ? param_types[local_idx] \
  5934. : local_types[local_idx - param_count]; \
  5935. local_offset = local_offsets[local_idx]; \
  5936. } while (0)
  5937. #define CHECK_BR(depth) \
  5938. do { \
  5939. if (!wasm_loader_check_br(loader_ctx, depth, error_buf, \
  5940. error_buf_size)) \
  5941. goto fail; \
  5942. } while (0)
  5943. static bool
  5944. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  5945. {
  5946. if (module->memory_count == 0 && module->import_memory_count == 0) {
  5947. set_error_buf(error_buf, error_buf_size, "unknown memory");
  5948. return false;
  5949. }
  5950. return true;
  5951. }
  5952. #define CHECK_MEMORY() \
  5953. do { \
  5954. if (!check_memory(module, error_buf, error_buf_size)) \
  5955. goto fail; \
  5956. } while (0)
  5957. static bool
  5958. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5959. uint32 error_buf_size)
  5960. {
  5961. uint8 mem_access_aligns[] = {
  5962. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  5963. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  5964. };
  5965. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  5966. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  5967. set_error_buf(error_buf, error_buf_size,
  5968. "alignment must not be larger than natural");
  5969. return false;
  5970. }
  5971. return true;
  5972. }
  5973. #if WASM_ENABLE_SIMD != 0
  5974. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  5975. static bool
  5976. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  5977. uint32 error_buf_size)
  5978. {
  5979. uint8 mem_access_aligns[] = {
  5980. 4, /* load */
  5981. 3, 3, 3, 3, 3, 3, /* load and extend */
  5982. 0, 1, 2, 3, /* load and splat */
  5983. 4, /* store */
  5984. };
  5985. uint8 mem_access_aligns_load_lane[] = {
  5986. 0, 1, 2, 3, /* load lane */
  5987. 0, 1, 2, 3, /* store lane */
  5988. 2, 3 /* store zero */
  5989. };
  5990. if (!((opcode <= SIMD_v128_store)
  5991. || (SIMD_v128_load8_lane <= opcode
  5992. && opcode <= SIMD_v128_load64_zero))) {
  5993. set_error_buf(error_buf, error_buf_size,
  5994. "the opcode doesn't include memarg");
  5995. return false;
  5996. }
  5997. if ((opcode <= SIMD_v128_store
  5998. && align > mem_access_aligns[opcode - SIMD_v128_load])
  5999. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  6000. && align > mem_access_aligns_load_lane[opcode
  6001. - SIMD_v128_load8_lane])) {
  6002. set_error_buf(error_buf, error_buf_size,
  6003. "alignment must not be larger than natural");
  6004. return false;
  6005. }
  6006. return true;
  6007. }
  6008. static bool
  6009. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  6010. uint32 error_buf_size)
  6011. {
  6012. switch (opcode) {
  6013. case SIMD_i8x16_extract_lane_s:
  6014. case SIMD_i8x16_extract_lane_u:
  6015. case SIMD_i8x16_replace_lane:
  6016. if (lane >= 16) {
  6017. goto fail;
  6018. }
  6019. break;
  6020. case SIMD_i16x8_extract_lane_s:
  6021. case SIMD_i16x8_extract_lane_u:
  6022. case SIMD_i16x8_replace_lane:
  6023. if (lane >= 8) {
  6024. goto fail;
  6025. }
  6026. break;
  6027. case SIMD_i32x4_extract_lane:
  6028. case SIMD_i32x4_replace_lane:
  6029. case SIMD_f32x4_extract_lane:
  6030. case SIMD_f32x4_replace_lane:
  6031. if (lane >= 4) {
  6032. goto fail;
  6033. }
  6034. break;
  6035. case SIMD_i64x2_extract_lane:
  6036. case SIMD_i64x2_replace_lane:
  6037. case SIMD_f64x2_extract_lane:
  6038. case SIMD_f64x2_replace_lane:
  6039. if (lane >= 2) {
  6040. goto fail;
  6041. }
  6042. break;
  6043. case SIMD_v128_load8_lane:
  6044. case SIMD_v128_load16_lane:
  6045. case SIMD_v128_load32_lane:
  6046. case SIMD_v128_load64_lane:
  6047. case SIMD_v128_store8_lane:
  6048. case SIMD_v128_store16_lane:
  6049. case SIMD_v128_store32_lane:
  6050. case SIMD_v128_store64_lane:
  6051. case SIMD_v128_load32_zero:
  6052. case SIMD_v128_load64_zero:
  6053. {
  6054. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  6055. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  6056. goto fail;
  6057. }
  6058. break;
  6059. }
  6060. default:
  6061. goto fail;
  6062. }
  6063. return true;
  6064. fail:
  6065. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  6066. return false;
  6067. }
  6068. static bool
  6069. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  6070. {
  6071. uint8 i;
  6072. for (i = 0; i != 16; ++i) {
  6073. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  6074. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  6075. return false;
  6076. }
  6077. }
  6078. return true;
  6079. }
  6080. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6081. #endif /* end of WASM_ENABLE_SIMD */
  6082. #if WASM_ENABLE_SHARED_MEMORY != 0
  6083. static bool
  6084. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  6085. uint32 error_buf_size)
  6086. {
  6087. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  6088. uint8 mem_access_aligns[] = {
  6089. 2, 3, 0, 1, 0, 1, 2,
  6090. };
  6091. uint8 expect;
  6092. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  6093. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  6094. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  6095. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  6096. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  6097. }
  6098. else {
  6099. /* 7 opcodes in every group */
  6100. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  6101. }
  6102. if (align != expect) {
  6103. set_error_buf(error_buf, error_buf_size,
  6104. "alignment isn't equal to natural");
  6105. return false;
  6106. }
  6107. return true;
  6108. }
  6109. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  6110. static bool
  6111. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth,
  6112. char *error_buf, uint32 error_buf_size)
  6113. {
  6114. BranchBlock *target_block, *cur_block;
  6115. BlockType *target_block_type;
  6116. uint8 *types = NULL, *frame_ref;
  6117. uint32 arity = 0;
  6118. int32 i, available_stack_cell;
  6119. uint16 cell_num;
  6120. if (loader_ctx->csp_num < depth + 1) {
  6121. set_error_buf(error_buf, error_buf_size,
  6122. "unknown label, "
  6123. "unexpected end of section or function");
  6124. return false;
  6125. }
  6126. cur_block = loader_ctx->frame_csp - 1;
  6127. target_block = loader_ctx->frame_csp - (depth + 1);
  6128. target_block_type = &target_block->block_type;
  6129. frame_ref = loader_ctx->frame_ref;
  6130. /* Note: loop's arity is different from if and block. loop's arity is
  6131. * its parameter count while if and block arity is result count.
  6132. */
  6133. if (target_block->label_type == LABEL_TYPE_LOOP)
  6134. arity = block_type_get_param_types(target_block_type, &types);
  6135. else
  6136. arity = block_type_get_result_types(target_block_type, &types);
  6137. /* If the stack is in polymorphic state, just clear the stack
  6138. * and then re-push the values to make the stack top values
  6139. * match block type. */
  6140. if (cur_block->is_stack_polymorphic) {
  6141. for (i = (int32)arity - 1; i >= 0; i--) {
  6142. #if WASM_ENABLE_FAST_INTERP != 0
  6143. POP_OFFSET_TYPE(types[i]);
  6144. #endif
  6145. POP_TYPE(types[i]);
  6146. }
  6147. for (i = 0; i < (int32)arity; i++) {
  6148. #if WASM_ENABLE_FAST_INTERP != 0
  6149. bool disable_emit = true;
  6150. int16 operand_offset = 0;
  6151. PUSH_OFFSET_TYPE(types[i]);
  6152. #endif
  6153. PUSH_TYPE(types[i]);
  6154. }
  6155. return true;
  6156. }
  6157. available_stack_cell =
  6158. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  6159. /* Check stack top values match target block type */
  6160. for (i = (int32)arity - 1; i >= 0; i--) {
  6161. if (!check_stack_top_values(frame_ref, available_stack_cell, types[i],
  6162. error_buf, error_buf_size))
  6163. return false;
  6164. cell_num = wasm_value_type_cell_num(types[i]);
  6165. frame_ref -= cell_num;
  6166. available_stack_cell -= cell_num;
  6167. }
  6168. return true;
  6169. fail:
  6170. return false;
  6171. }
  6172. static BranchBlock *
  6173. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  6174. char *error_buf, uint32 error_buf_size)
  6175. {
  6176. uint8 *p = *p_buf, *p_end = buf_end;
  6177. BranchBlock *frame_csp_tmp;
  6178. uint32 depth;
  6179. read_leb_uint32(p, p_end, depth);
  6180. CHECK_BR(depth);
  6181. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  6182. #if WASM_ENABLE_FAST_INTERP != 0
  6183. emit_br_info(frame_csp_tmp);
  6184. #endif
  6185. *p_buf = p;
  6186. return frame_csp_tmp;
  6187. fail:
  6188. return NULL;
  6189. }
  6190. static bool
  6191. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  6192. char *error_buf, uint32 error_buf_size)
  6193. {
  6194. BlockType *block_type = &block->block_type;
  6195. uint8 *return_types = NULL;
  6196. uint32 return_count = 0;
  6197. int32 available_stack_cell, return_cell_num, i;
  6198. uint8 *frame_ref = NULL;
  6199. available_stack_cell =
  6200. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  6201. return_count = block_type_get_result_types(block_type, &return_types);
  6202. return_cell_num =
  6203. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  6204. /* If the stack is in polymorphic state, just clear the stack
  6205. * and then re-push the values to make the stack top values
  6206. * match block type. */
  6207. if (block->is_stack_polymorphic) {
  6208. for (i = (int32)return_count - 1; i >= 0; i--) {
  6209. #if WASM_ENABLE_FAST_INTERP != 0
  6210. POP_OFFSET_TYPE(return_types[i]);
  6211. #endif
  6212. POP_TYPE(return_types[i]);
  6213. }
  6214. /* Check stack is empty */
  6215. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  6216. set_error_buf(
  6217. error_buf, error_buf_size,
  6218. "type mismatch: stack size does not match block type");
  6219. goto fail;
  6220. }
  6221. for (i = 0; i < (int32)return_count; i++) {
  6222. #if WASM_ENABLE_FAST_INTERP != 0
  6223. bool disable_emit = true;
  6224. int16 operand_offset = 0;
  6225. PUSH_OFFSET_TYPE(return_types[i]);
  6226. #endif
  6227. PUSH_TYPE(return_types[i]);
  6228. }
  6229. return true;
  6230. }
  6231. /* Check stack cell num equals return cell num */
  6232. if (available_stack_cell != return_cell_num) {
  6233. set_error_buf(error_buf, error_buf_size,
  6234. "type mismatch: stack size does not match block type");
  6235. goto fail;
  6236. }
  6237. /* Check stack values match return types */
  6238. frame_ref = loader_ctx->frame_ref;
  6239. for (i = (int32)return_count - 1; i >= 0; i--) {
  6240. if (!check_stack_top_values(frame_ref, available_stack_cell,
  6241. return_types[i], error_buf, error_buf_size))
  6242. return false;
  6243. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  6244. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  6245. }
  6246. return true;
  6247. fail:
  6248. return false;
  6249. }
  6250. #if WASM_ENABLE_FAST_INTERP != 0
  6251. /* Copy parameters to dynamic space.
  6252. * 1) POP original parameter out;
  6253. * 2) Push and copy original values to dynamic space.
  6254. * The copy instruction format:
  6255. * Part a: param count
  6256. * Part b: all param total cell num
  6257. * Part c: each param's cell_num, src offset and dst offset
  6258. * Part d: each param's src offset
  6259. * Part e: each param's dst offset
  6260. */
  6261. static bool
  6262. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  6263. char *error_buf, uint32 error_buf_size)
  6264. {
  6265. int16 *frame_offset = NULL;
  6266. uint8 *cells = NULL, cell;
  6267. int16 *src_offsets = NULL;
  6268. uint8 *emit_data = NULL;
  6269. uint32 i;
  6270. BranchBlock *block = loader_ctx->frame_csp - 1;
  6271. BlockType *block_type = &block->block_type;
  6272. WASMType *wasm_type = block_type->u.type;
  6273. uint32 param_count = block_type->u.type->param_count;
  6274. int16 condition_offset = 0;
  6275. bool disable_emit = false;
  6276. int16 operand_offset = 0;
  6277. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  6278. /* For if block, we also need copy the condition operand offset. */
  6279. if (is_if_block)
  6280. size += sizeof(*cells) + sizeof(*src_offsets);
  6281. /* Allocate memory for the emit data */
  6282. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  6283. return false;
  6284. cells = emit_data;
  6285. src_offsets = (int16 *)(cells + param_count);
  6286. if (is_if_block)
  6287. condition_offset = *loader_ctx->frame_offset;
  6288. /* POP original parameter out */
  6289. for (i = 0; i < param_count; i++) {
  6290. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  6291. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  6292. }
  6293. frame_offset = loader_ctx->frame_offset;
  6294. /* Get each param's cell num and src offset */
  6295. for (i = 0; i < param_count; i++) {
  6296. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  6297. cells[i] = cell;
  6298. src_offsets[i] = *frame_offset;
  6299. frame_offset += cell;
  6300. }
  6301. /* emit copy instruction */
  6302. emit_label(EXT_OP_COPY_STACK_VALUES);
  6303. /* Part a) */
  6304. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  6305. /* Part b) */
  6306. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  6307. : wasm_type->param_cell_num);
  6308. /* Part c) */
  6309. for (i = 0; i < param_count; i++)
  6310. emit_byte(loader_ctx, cells[i]);
  6311. if (is_if_block)
  6312. emit_byte(loader_ctx, 1);
  6313. /* Part d) */
  6314. for (i = 0; i < param_count; i++)
  6315. emit_operand(loader_ctx, src_offsets[i]);
  6316. if (is_if_block)
  6317. emit_operand(loader_ctx, condition_offset);
  6318. /* Part e) */
  6319. /* Push to dynamic space. The push will emit the dst offset. */
  6320. for (i = 0; i < param_count; i++)
  6321. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  6322. if (is_if_block)
  6323. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  6324. /* Free the emit data */
  6325. wasm_runtime_free(emit_data);
  6326. return true;
  6327. fail:
  6328. return false;
  6329. }
  6330. #endif
  6331. /* reset the stack to the state of before entering the last block */
  6332. #if WASM_ENABLE_FAST_INTERP != 0
  6333. #define RESET_STACK() \
  6334. do { \
  6335. loader_ctx->stack_cell_num = \
  6336. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  6337. loader_ctx->frame_ref = \
  6338. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  6339. loader_ctx->frame_offset = \
  6340. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  6341. } while (0)
  6342. #else
  6343. #define RESET_STACK() \
  6344. do { \
  6345. loader_ctx->stack_cell_num = \
  6346. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  6347. loader_ctx->frame_ref = \
  6348. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  6349. } while (0)
  6350. #endif
  6351. /* set current block's stack polymorphic state */
  6352. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  6353. do { \
  6354. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  6355. _cur_block->is_stack_polymorphic = flag; \
  6356. } while (0)
  6357. #define BLOCK_HAS_PARAM(block_type) \
  6358. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  6359. #define PRESERVE_LOCAL_FOR_BLOCK() \
  6360. do { \
  6361. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  6362. error_buf_size))) { \
  6363. goto fail; \
  6364. } \
  6365. } while (0)
  6366. #if WASM_ENABLE_REF_TYPES != 0
  6367. static bool
  6368. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  6369. uint8 *p_elem_type, char *error_buf, uint32 error_buf_size)
  6370. {
  6371. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  6372. return false;
  6373. }
  6374. if (p_elem_type) {
  6375. if (table_idx < module->import_table_count)
  6376. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  6377. else
  6378. *p_elem_type =
  6379. module->tables[module->import_table_count + table_idx]
  6380. .elem_type;
  6381. }
  6382. return true;
  6383. }
  6384. static bool
  6385. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  6386. uint8 *p_elem_type, char *error_buf,
  6387. uint32 error_buf_size)
  6388. {
  6389. if (table_seg_idx >= module->table_seg_count) {
  6390. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  6391. table_seg_idx);
  6392. return false;
  6393. }
  6394. if (p_elem_type) {
  6395. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  6396. }
  6397. return true;
  6398. }
  6399. #endif
  6400. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6401. const uint8 *
  6402. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  6403. uint32 *len)
  6404. {
  6405. WASMCustomSection *section = module->custom_section_list;
  6406. while (section) {
  6407. if ((section->name_len == strlen(name))
  6408. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  6409. if (len) {
  6410. *len = section->content_len;
  6411. }
  6412. return section->content_addr;
  6413. }
  6414. section = section->next;
  6415. }
  6416. return false;
  6417. }
  6418. #endif
  6419. static bool
  6420. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  6421. uint32 cur_func_idx, char *error_buf,
  6422. uint32 error_buf_size)
  6423. {
  6424. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  6425. uint32 param_count, local_count, global_count;
  6426. uint8 *param_types, *local_types, local_type, global_type;
  6427. BlockType func_block_type;
  6428. uint16 *local_offsets, local_offset;
  6429. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  6430. uint32 table_seg_idx, data_seg_idx, count, align, mem_offset, i;
  6431. int32 i32_const = 0;
  6432. int64 i64_const;
  6433. uint8 opcode;
  6434. bool return_value = false;
  6435. WASMLoaderContext *loader_ctx;
  6436. BranchBlock *frame_csp_tmp;
  6437. #if WASM_ENABLE_FAST_INTERP != 0
  6438. uint8 *func_const_end, *func_const = NULL;
  6439. int16 operand_offset = 0;
  6440. uint8 last_op = 0;
  6441. bool disable_emit, preserve_local = false;
  6442. float32 f32_const;
  6443. float64 f64_const;
  6444. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  6445. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  6446. #endif
  6447. global_count = module->import_global_count + module->global_count;
  6448. param_count = func->func_type->param_count;
  6449. param_types = func->func_type->types;
  6450. func_block_type.is_value_type = false;
  6451. func_block_type.u.type = func->func_type;
  6452. local_count = func->local_count;
  6453. local_types = func->local_types;
  6454. local_offsets = func->local_offsets;
  6455. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  6456. goto fail;
  6457. }
  6458. #if WASM_ENABLE_FAST_INTERP != 0
  6459. /* For the first traverse, the initial value of preserved_local_offset has
  6460. * not been determined, we use the INT16_MAX to represent that a slot has
  6461. * been copied to preserve space. For second traverse, this field will be
  6462. * set to the appropriate value in wasm_loader_ctx_reinit.
  6463. * This is for Issue #1230,
  6464. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  6465. * drop opcodes need to know which slots are preserved, so those slots will
  6466. * not be treated as dynamically allocated slots */
  6467. loader_ctx->preserved_local_offset = INT16_MAX;
  6468. re_scan:
  6469. if (loader_ctx->code_compiled_size > 0) {
  6470. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  6471. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  6472. goto fail;
  6473. }
  6474. p = func->code;
  6475. func->code_compiled = loader_ctx->p_code_compiled;
  6476. func->code_compiled_size = loader_ctx->code_compiled_size;
  6477. }
  6478. #endif
  6479. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  6480. while (p < p_end) {
  6481. opcode = *p++;
  6482. #if WASM_ENABLE_FAST_INTERP != 0
  6483. p_org = p;
  6484. disable_emit = false;
  6485. emit_label(opcode);
  6486. #endif
  6487. switch (opcode) {
  6488. case WASM_OP_UNREACHABLE:
  6489. RESET_STACK();
  6490. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6491. break;
  6492. case WASM_OP_NOP:
  6493. #if WASM_ENABLE_FAST_INTERP != 0
  6494. skip_label();
  6495. #endif
  6496. break;
  6497. case WASM_OP_IF:
  6498. #if WASM_ENABLE_FAST_INTERP != 0
  6499. PRESERVE_LOCAL_FOR_BLOCK();
  6500. #endif
  6501. POP_I32();
  6502. goto handle_op_block_and_loop;
  6503. case WASM_OP_BLOCK:
  6504. case WASM_OP_LOOP:
  6505. #if WASM_ENABLE_FAST_INTERP != 0
  6506. PRESERVE_LOCAL_FOR_BLOCK();
  6507. #endif
  6508. handle_op_block_and_loop:
  6509. {
  6510. uint8 value_type;
  6511. BlockType block_type;
  6512. p_org = p - 1;
  6513. value_type = read_uint8(p);
  6514. if (is_byte_a_type(value_type)) {
  6515. /* If the first byte is one of these special values:
  6516. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  6517. * the single return value. */
  6518. block_type.is_value_type = true;
  6519. block_type.u.value_type = value_type;
  6520. }
  6521. else {
  6522. uint32 type_index;
  6523. /* Resolve the leb128 encoded type index as block type */
  6524. p--;
  6525. read_leb_uint32(p, p_end, type_index);
  6526. if (type_index >= module->type_count) {
  6527. set_error_buf(error_buf, error_buf_size,
  6528. "unknown type");
  6529. goto fail;
  6530. }
  6531. block_type.is_value_type = false;
  6532. block_type.u.type = module->types[type_index];
  6533. #if WASM_ENABLE_FAST_INTERP == 0
  6534. /* If block use type index as block type, change the opcode
  6535. * to new extended opcode so that interpreter can resolve
  6536. * the block quickly.
  6537. */
  6538. #if WASM_ENABLE_DEBUG_INTERP != 0
  6539. if (!record_fast_op(module, p_org, *p_org, error_buf,
  6540. error_buf_size)) {
  6541. goto fail;
  6542. }
  6543. #endif
  6544. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  6545. #endif
  6546. }
  6547. /* Pop block parameters from stack */
  6548. if (BLOCK_HAS_PARAM(block_type)) {
  6549. WASMType *wasm_type = block_type.u.type;
  6550. for (i = 0; i < block_type.u.type->param_count; i++)
  6551. POP_TYPE(
  6552. wasm_type->types[wasm_type->param_count - i - 1]);
  6553. }
  6554. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  6555. block_type, p);
  6556. /* Pass parameters to block */
  6557. if (BLOCK_HAS_PARAM(block_type)) {
  6558. for (i = 0; i < block_type.u.type->param_count; i++)
  6559. PUSH_TYPE(block_type.u.type->types[i]);
  6560. }
  6561. #if WASM_ENABLE_FAST_INTERP != 0
  6562. if (opcode == WASM_OP_BLOCK) {
  6563. skip_label();
  6564. }
  6565. else if (opcode == WASM_OP_LOOP) {
  6566. skip_label();
  6567. if (BLOCK_HAS_PARAM(block_type)) {
  6568. /* Make sure params are in dynamic space */
  6569. if (!copy_params_to_dynamic_space(
  6570. loader_ctx, false, error_buf, error_buf_size))
  6571. goto fail;
  6572. }
  6573. (loader_ctx->frame_csp - 1)->code_compiled =
  6574. loader_ctx->p_code_compiled;
  6575. }
  6576. else if (opcode == WASM_OP_IF) {
  6577. /* If block has parameters, we should make sure they are in
  6578. * dynamic space. Otherwise, when else branch is missing,
  6579. * the later opcode may consume incorrect operand offset.
  6580. * Spec case:
  6581. * (func (export "params-id") (param i32) (result i32)
  6582. * (i32.const 1)
  6583. * (i32.const 2)
  6584. * (if (param i32 i32) (result i32 i32) (local.get 0)
  6585. * (then)) (i32.add)
  6586. * )
  6587. *
  6588. * So we should emit a copy instruction before the if.
  6589. *
  6590. * And we also need to save the parameter offsets and
  6591. * recover them before entering else branch.
  6592. *
  6593. */
  6594. if (BLOCK_HAS_PARAM(block_type)) {
  6595. BranchBlock *block = loader_ctx->frame_csp - 1;
  6596. uint64 size;
  6597. /* skip the if condition operand offset */
  6598. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  6599. /* skip the if label */
  6600. skip_label();
  6601. /* Emit a copy instruction */
  6602. if (!copy_params_to_dynamic_space(
  6603. loader_ctx, true, error_buf, error_buf_size))
  6604. goto fail;
  6605. /* Emit the if instruction */
  6606. emit_label(opcode);
  6607. /* Emit the new condition operand offset */
  6608. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  6609. /* Save top param_count values of frame_offset stack, so
  6610. * that we can recover it before executing else branch
  6611. */
  6612. size = sizeof(int16)
  6613. * (uint64)block_type.u.type->param_cell_num;
  6614. if (!(block->param_frame_offsets = loader_malloc(
  6615. size, error_buf, error_buf_size)))
  6616. goto fail;
  6617. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  6618. loader_ctx->frame_offset
  6619. - size / sizeof(int16),
  6620. (uint32)size);
  6621. }
  6622. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  6623. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6624. }
  6625. #endif
  6626. break;
  6627. }
  6628. case WASM_OP_ELSE:
  6629. {
  6630. BlockType block_type = (loader_ctx->frame_csp - 1)->block_type;
  6631. if (loader_ctx->csp_num < 2
  6632. || (loader_ctx->frame_csp - 1)->label_type
  6633. != LABEL_TYPE_IF) {
  6634. set_error_buf(
  6635. error_buf, error_buf_size,
  6636. "opcode else found without matched opcode if");
  6637. goto fail;
  6638. }
  6639. /* check whether if branch's stack matches its result type */
  6640. if (!check_block_stack(loader_ctx, loader_ctx->frame_csp - 1,
  6641. error_buf, error_buf_size))
  6642. goto fail;
  6643. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  6644. #if WASM_ENABLE_FAST_INTERP != 0
  6645. /* if the result of if branch is in local or const area, add a
  6646. * copy op */
  6647. RESERVE_BLOCK_RET();
  6648. emit_empty_label_addr_and_frame_ip(PATCH_END);
  6649. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  6650. #endif
  6651. RESET_STACK();
  6652. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  6653. /* Pass parameters to if-false branch */
  6654. if (BLOCK_HAS_PARAM(block_type)) {
  6655. for (i = 0; i < block_type.u.type->param_count; i++)
  6656. PUSH_TYPE(block_type.u.type->types[i]);
  6657. }
  6658. #if WASM_ENABLE_FAST_INTERP != 0
  6659. /* Recover top param_count values of frame_offset stack */
  6660. if (BLOCK_HAS_PARAM((block_type))) {
  6661. uint32 size;
  6662. BranchBlock *block = loader_ctx->frame_csp - 1;
  6663. size = sizeof(int16) * block_type.u.type->param_cell_num;
  6664. bh_memcpy_s(loader_ctx->frame_offset, size,
  6665. block->param_frame_offsets, size);
  6666. loader_ctx->frame_offset += (size / sizeof(int16));
  6667. }
  6668. #endif
  6669. break;
  6670. }
  6671. case WASM_OP_END:
  6672. {
  6673. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  6674. /* check whether block stack matches its result type */
  6675. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  6676. error_buf_size))
  6677. goto fail;
  6678. /* if no else branch, and return types do not match param types,
  6679. * fail */
  6680. if (cur_block->label_type == LABEL_TYPE_IF
  6681. && !cur_block->else_addr) {
  6682. uint32 block_param_count = 0, block_ret_count = 0;
  6683. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  6684. BlockType *cur_block_type = &cur_block->block_type;
  6685. if (cur_block_type->is_value_type) {
  6686. if (cur_block_type->u.value_type != VALUE_TYPE_VOID) {
  6687. block_ret_count = 1;
  6688. block_ret_types = &cur_block_type->u.value_type;
  6689. }
  6690. }
  6691. else {
  6692. block_param_count = cur_block_type->u.type->param_count;
  6693. block_ret_count = cur_block_type->u.type->result_count;
  6694. block_param_types = cur_block_type->u.type->types;
  6695. block_ret_types =
  6696. cur_block_type->u.type->types + block_param_count;
  6697. }
  6698. if (block_param_count != block_ret_count
  6699. || (block_param_count
  6700. && memcmp(block_param_types, block_ret_types,
  6701. block_param_count))) {
  6702. set_error_buf(error_buf, error_buf_size,
  6703. "type mismatch: else branch missing");
  6704. goto fail;
  6705. }
  6706. }
  6707. POP_CSP();
  6708. #if WASM_ENABLE_FAST_INTERP != 0
  6709. skip_label();
  6710. /* copy the result to the block return address */
  6711. RESERVE_BLOCK_RET();
  6712. apply_label_patch(loader_ctx, 0, PATCH_END);
  6713. free_label_patch_list(loader_ctx->frame_csp);
  6714. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  6715. int32 idx;
  6716. uint8 ret_type;
  6717. emit_label(WASM_OP_RETURN);
  6718. for (idx = (int32)func->func_type->result_count - 1;
  6719. idx >= 0; idx--) {
  6720. ret_type = *(func->func_type->types
  6721. + func->func_type->param_count + idx);
  6722. POP_OFFSET_TYPE(ret_type);
  6723. }
  6724. }
  6725. #endif
  6726. if (loader_ctx->csp_num > 0) {
  6727. loader_ctx->frame_csp->end_addr = p - 1;
  6728. }
  6729. else {
  6730. /* end of function block, function will return */
  6731. if (p < p_end) {
  6732. set_error_buf(error_buf, error_buf_size,
  6733. "section size mismatch");
  6734. goto fail;
  6735. }
  6736. }
  6737. break;
  6738. }
  6739. case WASM_OP_BR:
  6740. {
  6741. if (!(frame_csp_tmp = check_branch_block(
  6742. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6743. goto fail;
  6744. RESET_STACK();
  6745. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6746. break;
  6747. }
  6748. case WASM_OP_BR_IF:
  6749. {
  6750. POP_I32();
  6751. if (!(frame_csp_tmp = check_branch_block(
  6752. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  6753. goto fail;
  6754. break;
  6755. }
  6756. case WASM_OP_BR_TABLE:
  6757. {
  6758. uint8 *ret_types = NULL;
  6759. uint32 ret_count = 0;
  6760. #if WASM_ENABLE_FAST_INTERP == 0
  6761. uint8 *p_depth_begin, *p_depth;
  6762. uint32 depth, j;
  6763. BrTableCache *br_table_cache = NULL;
  6764. p_org = p - 1;
  6765. #endif
  6766. read_leb_uint32(p, p_end, count);
  6767. #if WASM_ENABLE_FAST_INTERP != 0
  6768. emit_uint32(loader_ctx, count);
  6769. #endif
  6770. POP_I32();
  6771. #if WASM_ENABLE_FAST_INTERP == 0
  6772. p_depth_begin = p_depth = p;
  6773. #endif
  6774. for (i = 0; i <= count; i++) {
  6775. if (!(frame_csp_tmp =
  6776. check_branch_block(loader_ctx, &p, p_end,
  6777. error_buf, error_buf_size)))
  6778. goto fail;
  6779. if (i == 0) {
  6780. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6781. ret_count = block_type_get_result_types(
  6782. &frame_csp_tmp->block_type, &ret_types);
  6783. }
  6784. else {
  6785. uint8 *tmp_ret_types = NULL;
  6786. uint32 tmp_ret_count = 0;
  6787. /* Check whether all table items have the same return
  6788. * type */
  6789. if (frame_csp_tmp->label_type != LABEL_TYPE_LOOP)
  6790. tmp_ret_count = block_type_get_result_types(
  6791. &frame_csp_tmp->block_type, &tmp_ret_types);
  6792. if (ret_count != tmp_ret_count
  6793. || (ret_count
  6794. && 0
  6795. != memcmp(ret_types, tmp_ret_types,
  6796. ret_count))) {
  6797. set_error_buf(
  6798. error_buf, error_buf_size,
  6799. "type mismatch: br_table targets must "
  6800. "all use same result type");
  6801. goto fail;
  6802. }
  6803. }
  6804. #if WASM_ENABLE_FAST_INTERP == 0
  6805. depth = (uint32)(loader_ctx->frame_csp - 1 - frame_csp_tmp);
  6806. if (br_table_cache) {
  6807. br_table_cache->br_depths[i] = depth;
  6808. }
  6809. else {
  6810. if (depth > 255) {
  6811. /* The depth cannot be stored in one byte,
  6812. create br_table cache to store each depth */
  6813. #if WASM_ENABLE_DEBUG_INTERP != 0
  6814. if (!record_fast_op(module, p_org, *p_org,
  6815. error_buf, error_buf_size)) {
  6816. goto fail;
  6817. }
  6818. #endif
  6819. if (!(br_table_cache = loader_malloc(
  6820. offsetof(BrTableCache, br_depths)
  6821. + sizeof(uint32)
  6822. * (uint64)(count + 1),
  6823. error_buf, error_buf_size))) {
  6824. goto fail;
  6825. }
  6826. *p_org = EXT_OP_BR_TABLE_CACHE;
  6827. br_table_cache->br_table_op_addr = p_org;
  6828. br_table_cache->br_count = count;
  6829. /* Copy previous depths which are one byte */
  6830. for (j = 0; j < i; j++) {
  6831. br_table_cache->br_depths[j] = p_depth_begin[j];
  6832. }
  6833. br_table_cache->br_depths[i] = depth;
  6834. bh_list_insert(module->br_table_cache_list,
  6835. br_table_cache);
  6836. }
  6837. else {
  6838. /* The depth can be stored in one byte, use the
  6839. byte of the leb to store it */
  6840. *p_depth++ = (uint8)depth;
  6841. }
  6842. }
  6843. #endif
  6844. }
  6845. #if WASM_ENABLE_FAST_INTERP == 0
  6846. /* Set the tailing bytes to nop */
  6847. if (br_table_cache)
  6848. p_depth = p_depth_begin;
  6849. while (p_depth < p)
  6850. *p_depth++ = WASM_OP_NOP;
  6851. #endif
  6852. RESET_STACK();
  6853. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6854. break;
  6855. }
  6856. case WASM_OP_RETURN:
  6857. {
  6858. int32 idx;
  6859. uint8 ret_type;
  6860. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  6861. idx--) {
  6862. ret_type = *(func->func_type->types
  6863. + func->func_type->param_count + idx);
  6864. POP_TYPE(ret_type);
  6865. #if WASM_ENABLE_FAST_INTERP != 0
  6866. /* emit the offset after return opcode */
  6867. POP_OFFSET_TYPE(ret_type);
  6868. #endif
  6869. }
  6870. RESET_STACK();
  6871. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6872. break;
  6873. }
  6874. case WASM_OP_CALL:
  6875. #if WASM_ENABLE_TAIL_CALL != 0
  6876. case WASM_OP_RETURN_CALL:
  6877. #endif
  6878. {
  6879. WASMType *func_type;
  6880. int32 idx;
  6881. read_leb_uint32(p, p_end, func_idx);
  6882. #if WASM_ENABLE_FAST_INTERP != 0
  6883. /* we need to emit func_idx before arguments */
  6884. emit_uint32(loader_ctx, func_idx);
  6885. #endif
  6886. if (!check_function_index(module, func_idx, error_buf,
  6887. error_buf_size)) {
  6888. goto fail;
  6889. }
  6890. if (func_idx < module->import_function_count)
  6891. func_type =
  6892. module->import_functions[func_idx].u.function.func_type;
  6893. else
  6894. func_type = module
  6895. ->functions[func_idx
  6896. - module->import_function_count]
  6897. ->func_type;
  6898. if (func_type->param_count > 0) {
  6899. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6900. idx--) {
  6901. POP_TYPE(func_type->types[idx]);
  6902. #if WASM_ENABLE_FAST_INTERP != 0
  6903. POP_OFFSET_TYPE(func_type->types[idx]);
  6904. #endif
  6905. }
  6906. }
  6907. #if WASM_ENABLE_TAIL_CALL != 0
  6908. if (opcode == WASM_OP_CALL) {
  6909. #endif
  6910. for (i = 0; i < func_type->result_count; i++) {
  6911. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  6912. #if WASM_ENABLE_FAST_INTERP != 0
  6913. /* Here we emit each return value's dynamic_offset. But
  6914. * in fact these offsets are continuous, so interpreter
  6915. * only need to get the first return value's offset.
  6916. */
  6917. PUSH_OFFSET_TYPE(
  6918. func_type->types[func_type->param_count + i]);
  6919. #endif
  6920. }
  6921. #if WASM_ENABLE_TAIL_CALL != 0
  6922. }
  6923. else {
  6924. uint8 type;
  6925. if (func_type->result_count
  6926. != func->func_type->result_count) {
  6927. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  6928. "type mismatch: expect ",
  6929. func->func_type->result_count,
  6930. " return values but got other");
  6931. goto fail;
  6932. }
  6933. for (i = 0; i < func_type->result_count; i++) {
  6934. type = func->func_type
  6935. ->types[func->func_type->param_count + i];
  6936. if (func_type->types[func_type->param_count + i]
  6937. != type) {
  6938. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  6939. "type mismatch: expect ",
  6940. type2str(type), " but got other");
  6941. goto fail;
  6942. }
  6943. }
  6944. RESET_STACK();
  6945. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  6946. }
  6947. #endif
  6948. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  6949. || WASM_ENABLE_WAMR_COMPILER != 0
  6950. func->has_op_func_call = true;
  6951. #endif
  6952. break;
  6953. }
  6954. /*
  6955. * if disable reference type: call_indirect typeidx, 0x00
  6956. * if enable reference type: call_indirect typeidx, tableidx
  6957. */
  6958. case WASM_OP_CALL_INDIRECT:
  6959. #if WASM_ENABLE_TAIL_CALL != 0
  6960. case WASM_OP_RETURN_CALL_INDIRECT:
  6961. #endif
  6962. {
  6963. int32 idx;
  6964. WASMType *func_type;
  6965. read_leb_uint32(p, p_end, type_idx);
  6966. #if WASM_ENABLE_REF_TYPES != 0
  6967. read_leb_uint32(p, p_end, table_idx);
  6968. #else
  6969. CHECK_BUF(p, p_end, 1);
  6970. table_idx = read_uint8(p);
  6971. #endif
  6972. if (!check_table_index(module, table_idx, error_buf,
  6973. error_buf_size)) {
  6974. goto fail;
  6975. }
  6976. #if WASM_ENABLE_FAST_INTERP != 0
  6977. /* we need to emit before arguments */
  6978. #if WASM_ENABLE_TAIL_CALL != 0
  6979. emit_byte(loader_ctx, opcode);
  6980. #endif
  6981. emit_uint32(loader_ctx, type_idx);
  6982. emit_uint32(loader_ctx, table_idx);
  6983. #endif
  6984. /* skip elem idx */
  6985. POP_I32();
  6986. if (type_idx >= module->type_count) {
  6987. set_error_buf(error_buf, error_buf_size, "unknown type");
  6988. goto fail;
  6989. }
  6990. func_type = module->types[type_idx];
  6991. if (func_type->param_count > 0) {
  6992. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  6993. idx--) {
  6994. POP_TYPE(func_type->types[idx]);
  6995. #if WASM_ENABLE_FAST_INTERP != 0
  6996. POP_OFFSET_TYPE(func_type->types[idx]);
  6997. #endif
  6998. }
  6999. }
  7000. #if WASM_ENABLE_TAIL_CALL != 0
  7001. if (opcode == WASM_OP_CALL_INDIRECT) {
  7002. #endif
  7003. for (i = 0; i < func_type->result_count; i++) {
  7004. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  7005. #if WASM_ENABLE_FAST_INTERP != 0
  7006. PUSH_OFFSET_TYPE(
  7007. func_type->types[func_type->param_count + i]);
  7008. #endif
  7009. }
  7010. #if WASM_ENABLE_TAIL_CALL != 0
  7011. }
  7012. else {
  7013. uint8 type;
  7014. if (func_type->result_count
  7015. != func->func_type->result_count) {
  7016. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  7017. "type mismatch: expect ",
  7018. func->func_type->result_count,
  7019. " return values but got other");
  7020. goto fail;
  7021. }
  7022. for (i = 0; i < func_type->result_count; i++) {
  7023. type = func->func_type
  7024. ->types[func->func_type->param_count + i];
  7025. if (func_type->types[func_type->param_count + i]
  7026. != type) {
  7027. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7028. "type mismatch: expect ",
  7029. type2str(type), " but got other");
  7030. goto fail;
  7031. }
  7032. }
  7033. RESET_STACK();
  7034. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  7035. }
  7036. #endif
  7037. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  7038. || WASM_ENABLE_WAMR_COMPILER != 0
  7039. func->has_op_func_call = true;
  7040. #endif
  7041. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7042. func->has_op_call_indirect = true;
  7043. #endif
  7044. break;
  7045. }
  7046. case WASM_OP_DROP:
  7047. {
  7048. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7049. int32 available_stack_cell =
  7050. (int32)(loader_ctx->stack_cell_num
  7051. - cur_block->stack_cell_num);
  7052. if (available_stack_cell <= 0
  7053. && !cur_block->is_stack_polymorphic) {
  7054. set_error_buf(error_buf, error_buf_size,
  7055. "type mismatch, opcode drop was found "
  7056. "but stack was empty");
  7057. goto fail;
  7058. }
  7059. if (available_stack_cell > 0) {
  7060. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  7061. loader_ctx->frame_ref--;
  7062. loader_ctx->stack_cell_num--;
  7063. #if WASM_ENABLE_FAST_INTERP != 0
  7064. skip_label();
  7065. loader_ctx->frame_offset--;
  7066. if ((*(loader_ctx->frame_offset)
  7067. > loader_ctx->start_dynamic_offset)
  7068. && (*(loader_ctx->frame_offset)
  7069. < loader_ctx->max_dynamic_offset))
  7070. loader_ctx->dynamic_offset--;
  7071. #endif
  7072. }
  7073. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  7074. loader_ctx->frame_ref -= 2;
  7075. loader_ctx->stack_cell_num -= 2;
  7076. #if WASM_ENABLE_FAST_INTERP == 0
  7077. *(p - 1) = WASM_OP_DROP_64;
  7078. #endif
  7079. #if WASM_ENABLE_FAST_INTERP != 0
  7080. skip_label();
  7081. loader_ctx->frame_offset -= 2;
  7082. if ((*(loader_ctx->frame_offset)
  7083. > loader_ctx->start_dynamic_offset)
  7084. && (*(loader_ctx->frame_offset)
  7085. < loader_ctx->max_dynamic_offset))
  7086. loader_ctx->dynamic_offset -= 2;
  7087. #endif
  7088. }
  7089. #if WASM_ENABLE_SIMD != 0
  7090. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7091. else if (*(loader_ctx->frame_ref - 1) == REF_V128_1) {
  7092. loader_ctx->frame_ref -= 4;
  7093. loader_ctx->stack_cell_num -= 4;
  7094. }
  7095. #endif
  7096. #endif
  7097. else {
  7098. set_error_buf(error_buf, error_buf_size,
  7099. "type mismatch");
  7100. goto fail;
  7101. }
  7102. }
  7103. else {
  7104. #if WASM_ENABLE_FAST_INTERP != 0
  7105. skip_label();
  7106. #endif
  7107. }
  7108. break;
  7109. }
  7110. case WASM_OP_SELECT:
  7111. {
  7112. uint8 ref_type;
  7113. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7114. int32 available_stack_cell;
  7115. POP_I32();
  7116. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  7117. - cur_block->stack_cell_num);
  7118. if (available_stack_cell <= 0
  7119. && !cur_block->is_stack_polymorphic) {
  7120. set_error_buf(error_buf, error_buf_size,
  7121. "type mismatch or invalid result arity, "
  7122. "opcode select was found "
  7123. "but stack was empty");
  7124. goto fail;
  7125. }
  7126. if (available_stack_cell > 0) {
  7127. switch (*(loader_ctx->frame_ref - 1)) {
  7128. case REF_I32:
  7129. case REF_F32:
  7130. break;
  7131. case REF_I64_2:
  7132. case REF_F64_2:
  7133. #if WASM_ENABLE_FAST_INTERP == 0
  7134. *(p - 1) = WASM_OP_SELECT_64;
  7135. #endif
  7136. #if WASM_ENABLE_FAST_INTERP != 0
  7137. if (loader_ctx->p_code_compiled) {
  7138. uint8 opcode_tmp = WASM_OP_SELECT_64;
  7139. uint8 *p_code_compiled_tmp =
  7140. loader_ctx->p_code_compiled - 2;
  7141. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7142. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7143. *(void **)(p_code_compiled_tmp
  7144. - sizeof(void *)) =
  7145. handle_table[opcode_tmp];
  7146. #else
  7147. int32 offset =
  7148. (int32)((uint8 *)handle_table[opcode_tmp]
  7149. - (uint8 *)handle_table[0]);
  7150. if (!(offset >= INT16_MIN
  7151. && offset < INT16_MAX)) {
  7152. set_error_buf(error_buf, error_buf_size,
  7153. "pre-compiled label offset "
  7154. "out of range");
  7155. goto fail;
  7156. }
  7157. *(int16 *)(p_code_compiled_tmp
  7158. - sizeof(int16)) = (int16)offset;
  7159. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7160. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7161. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7162. *(p_code_compiled_tmp - 1) = opcode_tmp;
  7163. #else
  7164. *(p_code_compiled_tmp - 2) = opcode_tmp;
  7165. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7166. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7167. }
  7168. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7169. break;
  7170. #if WASM_ENABLE_SIMD != 0
  7171. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  7172. case REF_V128_4:
  7173. break;
  7174. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  7175. #endif /* WASM_ENABLE_SIMD != 0 */
  7176. default:
  7177. {
  7178. set_error_buf(error_buf, error_buf_size,
  7179. "type mismatch");
  7180. goto fail;
  7181. }
  7182. }
  7183. ref_type = *(loader_ctx->frame_ref - 1);
  7184. #if WASM_ENABLE_FAST_INTERP != 0
  7185. POP_OFFSET_TYPE(ref_type);
  7186. POP_TYPE(ref_type);
  7187. POP_OFFSET_TYPE(ref_type);
  7188. POP_TYPE(ref_type);
  7189. PUSH_OFFSET_TYPE(ref_type);
  7190. PUSH_TYPE(ref_type);
  7191. #else
  7192. POP2_AND_PUSH(ref_type, ref_type);
  7193. #endif
  7194. }
  7195. else {
  7196. #if WASM_ENABLE_FAST_INTERP != 0
  7197. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  7198. #endif
  7199. PUSH_TYPE(VALUE_TYPE_ANY);
  7200. }
  7201. break;
  7202. }
  7203. #if WASM_ENABLE_REF_TYPES != 0
  7204. case WASM_OP_SELECT_T:
  7205. {
  7206. uint8 vec_len, ref_type;
  7207. read_leb_uint32(p, p_end, vec_len);
  7208. if (!vec_len) {
  7209. set_error_buf(error_buf, error_buf_size,
  7210. "invalid result arity");
  7211. goto fail;
  7212. }
  7213. CHECK_BUF(p, p_end, 1);
  7214. ref_type = read_uint8(p);
  7215. if (!is_value_type(ref_type)) {
  7216. set_error_buf(error_buf, error_buf_size,
  7217. "unknown value type");
  7218. goto fail;
  7219. }
  7220. POP_I32();
  7221. #if WASM_ENABLE_FAST_INTERP != 0
  7222. if (loader_ctx->p_code_compiled) {
  7223. uint8 opcode_tmp = WASM_OP_SELECT;
  7224. uint8 *p_code_compiled_tmp =
  7225. loader_ctx->p_code_compiled - 2;
  7226. if (ref_type == VALUE_TYPE_V128) {
  7227. #if (WASM_ENABLE_SIMD == 0) \
  7228. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  7229. set_error_buf(error_buf, error_buf_size,
  7230. "SIMD v128 type isn't supported");
  7231. goto fail;
  7232. #endif
  7233. }
  7234. else {
  7235. if (ref_type == VALUE_TYPE_F64
  7236. || ref_type == VALUE_TYPE_I64)
  7237. opcode_tmp = WASM_OP_SELECT_64;
  7238. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7239. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7240. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  7241. handle_table[opcode_tmp];
  7242. #else
  7243. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  7244. - (uint8 *)handle_table[0]);
  7245. if (!(offset >= INT16_MIN && offset < INT16_MAX)) {
  7246. set_error_buf(
  7247. error_buf, error_buf_size,
  7248. "pre-compiled label offset out of range");
  7249. goto fail;
  7250. }
  7251. *(int16 *)(p_code_compiled_tmp - sizeof(int16)) =
  7252. (int16)offset;
  7253. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7254. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7255. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7256. *(p_code_compiled_tmp - 1) = opcode_tmp;
  7257. #else
  7258. *(p_code_compiled_tmp - 2) = opcode_tmp;
  7259. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7260. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7261. }
  7262. }
  7263. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  7264. #if WASM_ENABLE_FAST_INTERP != 0
  7265. POP_OFFSET_TYPE(ref_type);
  7266. POP_TYPE(ref_type);
  7267. POP_OFFSET_TYPE(ref_type);
  7268. POP_TYPE(ref_type);
  7269. PUSH_OFFSET_TYPE(ref_type);
  7270. PUSH_TYPE(ref_type);
  7271. #else
  7272. POP2_AND_PUSH(ref_type, ref_type);
  7273. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  7274. (void)vec_len;
  7275. break;
  7276. }
  7277. /* table.get x. tables[x]. [i32] -> [t] */
  7278. /* table.set x. tables[x]. [i32 t] -> [] */
  7279. case WASM_OP_TABLE_GET:
  7280. case WASM_OP_TABLE_SET:
  7281. {
  7282. uint8 decl_ref_type;
  7283. read_leb_uint32(p, p_end, table_idx);
  7284. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  7285. error_buf, error_buf_size))
  7286. goto fail;
  7287. #if WASM_ENABLE_FAST_INTERP != 0
  7288. emit_uint32(loader_ctx, table_idx);
  7289. #endif
  7290. if (opcode == WASM_OP_TABLE_GET) {
  7291. POP_I32();
  7292. #if WASM_ENABLE_FAST_INTERP != 0
  7293. PUSH_OFFSET_TYPE(decl_ref_type);
  7294. #endif
  7295. PUSH_TYPE(decl_ref_type);
  7296. }
  7297. else {
  7298. #if WASM_ENABLE_FAST_INTERP != 0
  7299. POP_OFFSET_TYPE(decl_ref_type);
  7300. #endif
  7301. POP_TYPE(decl_ref_type);
  7302. POP_I32();
  7303. }
  7304. break;
  7305. }
  7306. case WASM_OP_REF_NULL:
  7307. {
  7308. uint8 ref_type;
  7309. CHECK_BUF(p, p_end, 1);
  7310. ref_type = read_uint8(p);
  7311. if (ref_type != VALUE_TYPE_FUNCREF
  7312. && ref_type != VALUE_TYPE_EXTERNREF) {
  7313. set_error_buf(error_buf, error_buf_size,
  7314. "unknown value type");
  7315. goto fail;
  7316. }
  7317. #if WASM_ENABLE_FAST_INTERP != 0
  7318. PUSH_OFFSET_TYPE(ref_type);
  7319. #endif
  7320. PUSH_TYPE(ref_type);
  7321. break;
  7322. }
  7323. case WASM_OP_REF_IS_NULL:
  7324. {
  7325. #if WASM_ENABLE_FAST_INTERP != 0
  7326. if (!wasm_loader_pop_frame_ref_offset(loader_ctx,
  7327. VALUE_TYPE_FUNCREF,
  7328. error_buf, error_buf_size)
  7329. && !wasm_loader_pop_frame_ref_offset(
  7330. loader_ctx, VALUE_TYPE_EXTERNREF, error_buf,
  7331. error_buf_size)) {
  7332. goto fail;
  7333. }
  7334. #else
  7335. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  7336. error_buf, error_buf_size)
  7337. && !wasm_loader_pop_frame_ref(loader_ctx,
  7338. VALUE_TYPE_EXTERNREF,
  7339. error_buf, error_buf_size)) {
  7340. goto fail;
  7341. }
  7342. #endif
  7343. PUSH_I32();
  7344. break;
  7345. }
  7346. case WASM_OP_REF_FUNC:
  7347. {
  7348. read_leb_uint32(p, p_end, func_idx);
  7349. if (!check_function_index(module, func_idx, error_buf,
  7350. error_buf_size)) {
  7351. goto fail;
  7352. }
  7353. /* Refer to a forward-declared function */
  7354. if (func_idx >= cur_func_idx + module->import_function_count) {
  7355. WASMTableSeg *table_seg = module->table_segments;
  7356. bool func_declared = false;
  7357. uint32 j;
  7358. /* Check whether the function is declared in table segs */
  7359. for (i = 0; i < module->table_seg_count; i++, table_seg++) {
  7360. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  7361. && wasm_elem_is_declarative(table_seg->mode)) {
  7362. for (j = 0; j < table_seg->function_count; j++) {
  7363. if (table_seg->func_indexes[j] == func_idx) {
  7364. func_declared = true;
  7365. break;
  7366. }
  7367. }
  7368. }
  7369. }
  7370. if (!func_declared) {
  7371. /* Check whether the function is exported */
  7372. for (i = 0; i < module->export_count; i++) {
  7373. if (module->exports[i].kind == EXPORT_KIND_FUNC
  7374. && module->exports[i].index == func_idx) {
  7375. func_declared = true;
  7376. break;
  7377. }
  7378. }
  7379. }
  7380. if (!func_declared) {
  7381. set_error_buf(error_buf, error_buf_size,
  7382. "undeclared function reference");
  7383. goto fail;
  7384. }
  7385. }
  7386. #if WASM_ENABLE_FAST_INTERP != 0
  7387. emit_uint32(loader_ctx, func_idx);
  7388. #endif
  7389. PUSH_FUNCREF();
  7390. break;
  7391. }
  7392. #endif /* WASM_ENABLE_REF_TYPES */
  7393. case WASM_OP_GET_LOCAL:
  7394. {
  7395. p_org = p - 1;
  7396. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7397. PUSH_TYPE(local_type);
  7398. #if WASM_ENABLE_FAST_INTERP != 0
  7399. /* Get Local is optimized out */
  7400. skip_label();
  7401. disable_emit = true;
  7402. operand_offset = local_offset;
  7403. PUSH_OFFSET_TYPE(local_type);
  7404. #else
  7405. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7406. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7407. if (local_offset < 0x80) {
  7408. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  7409. if (is_32bit_type(local_type)) {
  7410. *p_org++ = (uint8)local_offset;
  7411. }
  7412. else {
  7413. *p_org++ = (uint8)(local_offset | 0x80);
  7414. }
  7415. while (p_org < p) {
  7416. *p_org++ = WASM_OP_NOP;
  7417. }
  7418. }
  7419. #endif
  7420. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7421. break;
  7422. }
  7423. case WASM_OP_SET_LOCAL:
  7424. {
  7425. p_org = p - 1;
  7426. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7427. POP_TYPE(local_type);
  7428. #if WASM_ENABLE_FAST_INTERP != 0
  7429. if (!(preserve_referenced_local(
  7430. loader_ctx, opcode, local_offset, local_type,
  7431. &preserve_local, error_buf, error_buf_size)))
  7432. goto fail;
  7433. if (local_offset < 256) {
  7434. skip_label();
  7435. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  7436. if (loader_ctx->p_code_compiled)
  7437. STORE_U16(loader_ctx->p_code_compiled - 2,
  7438. local_offset);
  7439. loader_ctx->frame_offset--;
  7440. loader_ctx->dynamic_offset--;
  7441. }
  7442. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  7443. if (loader_ctx->p_code_compiled)
  7444. STORE_U16(loader_ctx->p_code_compiled - 2,
  7445. local_offset);
  7446. loader_ctx->frame_offset -= 2;
  7447. loader_ctx->dynamic_offset -= 2;
  7448. }
  7449. else {
  7450. if (is_32bit_type(local_type)) {
  7451. emit_label(EXT_OP_SET_LOCAL_FAST);
  7452. emit_byte(loader_ctx, (uint8)local_offset);
  7453. }
  7454. else {
  7455. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  7456. emit_byte(loader_ctx, (uint8)local_offset);
  7457. }
  7458. POP_OFFSET_TYPE(local_type);
  7459. }
  7460. }
  7461. else { /* local index larger than 255, reserve leb */
  7462. emit_uint32(loader_ctx, local_idx);
  7463. POP_OFFSET_TYPE(local_type);
  7464. }
  7465. #else
  7466. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7467. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7468. if (local_offset < 0x80) {
  7469. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  7470. if (is_32bit_type(local_type)) {
  7471. *p_org++ = (uint8)local_offset;
  7472. }
  7473. else {
  7474. *p_org++ = (uint8)(local_offset | 0x80);
  7475. }
  7476. while (p_org < p) {
  7477. *p_org++ = WASM_OP_NOP;
  7478. }
  7479. }
  7480. #endif
  7481. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7482. break;
  7483. }
  7484. case WASM_OP_TEE_LOCAL:
  7485. {
  7486. p_org = p - 1;
  7487. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  7488. #if WASM_ENABLE_FAST_INTERP != 0
  7489. /* If the stack is in polymorphic state, do fake pop and push on
  7490. offset stack to keep the depth of offset stack to be the
  7491. same with ref stack */
  7492. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  7493. if (cur_block->is_stack_polymorphic) {
  7494. POP_OFFSET_TYPE(local_type);
  7495. PUSH_OFFSET_TYPE(local_type);
  7496. }
  7497. #endif
  7498. POP_TYPE(local_type);
  7499. PUSH_TYPE(local_type);
  7500. #if WASM_ENABLE_FAST_INTERP != 0
  7501. if (!(preserve_referenced_local(
  7502. loader_ctx, opcode, local_offset, local_type,
  7503. &preserve_local, error_buf, error_buf_size)))
  7504. goto fail;
  7505. if (local_offset < 256) {
  7506. skip_label();
  7507. if (is_32bit_type(local_type)) {
  7508. emit_label(EXT_OP_TEE_LOCAL_FAST);
  7509. emit_byte(loader_ctx, (uint8)local_offset);
  7510. }
  7511. else {
  7512. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  7513. emit_byte(loader_ctx, (uint8)local_offset);
  7514. }
  7515. }
  7516. else { /* local index larger than 255, reserve leb */
  7517. emit_uint32(loader_ctx, local_idx);
  7518. }
  7519. emit_operand(loader_ctx,
  7520. *(loader_ctx->frame_offset
  7521. - wasm_value_type_cell_num(local_type)));
  7522. #else
  7523. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  7524. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  7525. if (local_offset < 0x80) {
  7526. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  7527. if (is_32bit_type(local_type)) {
  7528. *p_org++ = (uint8)local_offset;
  7529. }
  7530. else {
  7531. *p_org++ = (uint8)(local_offset | 0x80);
  7532. }
  7533. while (p_org < p) {
  7534. *p_org++ = WASM_OP_NOP;
  7535. }
  7536. }
  7537. #endif
  7538. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  7539. break;
  7540. }
  7541. case WASM_OP_GET_GLOBAL:
  7542. {
  7543. p_org = p - 1;
  7544. read_leb_uint32(p, p_end, global_idx);
  7545. if (global_idx >= global_count) {
  7546. set_error_buf(error_buf, error_buf_size, "unknown global");
  7547. goto fail;
  7548. }
  7549. global_type =
  7550. global_idx < module->import_global_count
  7551. ? module->import_globals[global_idx].u.global.type
  7552. : module
  7553. ->globals[global_idx
  7554. - module->import_global_count]
  7555. .type;
  7556. PUSH_TYPE(global_type);
  7557. #if WASM_ENABLE_FAST_INTERP == 0
  7558. if (global_type == VALUE_TYPE_I64
  7559. || global_type == VALUE_TYPE_F64) {
  7560. #if WASM_ENABLE_DEBUG_INTERP != 0
  7561. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7562. error_buf_size)) {
  7563. goto fail;
  7564. }
  7565. #endif
  7566. *p_org = WASM_OP_GET_GLOBAL_64;
  7567. }
  7568. #else /* else of WASM_ENABLE_FAST_INTERP */
  7569. if (global_type == VALUE_TYPE_I64
  7570. || global_type == VALUE_TYPE_F64) {
  7571. skip_label();
  7572. emit_label(WASM_OP_GET_GLOBAL_64);
  7573. }
  7574. emit_uint32(loader_ctx, global_idx);
  7575. PUSH_OFFSET_TYPE(global_type);
  7576. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7577. break;
  7578. }
  7579. case WASM_OP_SET_GLOBAL:
  7580. {
  7581. bool is_mutable = false;
  7582. p_org = p - 1;
  7583. read_leb_uint32(p, p_end, global_idx);
  7584. if (global_idx >= global_count) {
  7585. set_error_buf(error_buf, error_buf_size, "unknown global");
  7586. goto fail;
  7587. }
  7588. is_mutable =
  7589. global_idx < module->import_global_count
  7590. ? module->import_globals[global_idx].u.global.is_mutable
  7591. : module
  7592. ->globals[global_idx
  7593. - module->import_global_count]
  7594. .is_mutable;
  7595. if (!is_mutable) {
  7596. set_error_buf(error_buf, error_buf_size,
  7597. "global is immutable");
  7598. goto fail;
  7599. }
  7600. global_type =
  7601. global_idx < module->import_global_count
  7602. ? module->import_globals[global_idx].u.global.type
  7603. : module
  7604. ->globals[global_idx
  7605. - module->import_global_count]
  7606. .type;
  7607. POP_TYPE(global_type);
  7608. #if WASM_ENABLE_FAST_INTERP == 0
  7609. if (global_type == VALUE_TYPE_I64
  7610. || global_type == VALUE_TYPE_F64) {
  7611. #if WASM_ENABLE_DEBUG_INTERP != 0
  7612. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7613. error_buf_size)) {
  7614. goto fail;
  7615. }
  7616. #endif
  7617. *p_org = WASM_OP_SET_GLOBAL_64;
  7618. }
  7619. else if (module->aux_stack_size > 0
  7620. && global_idx == module->aux_stack_top_global_index) {
  7621. #if WASM_ENABLE_DEBUG_INTERP != 0
  7622. if (!record_fast_op(module, p_org, *p_org, error_buf,
  7623. error_buf_size)) {
  7624. goto fail;
  7625. }
  7626. #endif
  7627. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  7628. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7629. func->has_op_set_global_aux_stack = true;
  7630. #endif
  7631. }
  7632. #else /* else of WASM_ENABLE_FAST_INTERP */
  7633. if (global_type == VALUE_TYPE_I64
  7634. || global_type == VALUE_TYPE_F64) {
  7635. skip_label();
  7636. emit_label(WASM_OP_SET_GLOBAL_64);
  7637. }
  7638. else if (module->aux_stack_size > 0
  7639. && global_idx == module->aux_stack_top_global_index) {
  7640. skip_label();
  7641. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  7642. }
  7643. emit_uint32(loader_ctx, global_idx);
  7644. POP_OFFSET_TYPE(global_type);
  7645. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7646. break;
  7647. }
  7648. /* load */
  7649. case WASM_OP_I32_LOAD:
  7650. case WASM_OP_I32_LOAD8_S:
  7651. case WASM_OP_I32_LOAD8_U:
  7652. case WASM_OP_I32_LOAD16_S:
  7653. case WASM_OP_I32_LOAD16_U:
  7654. case WASM_OP_I64_LOAD:
  7655. case WASM_OP_I64_LOAD8_S:
  7656. case WASM_OP_I64_LOAD8_U:
  7657. case WASM_OP_I64_LOAD16_S:
  7658. case WASM_OP_I64_LOAD16_U:
  7659. case WASM_OP_I64_LOAD32_S:
  7660. case WASM_OP_I64_LOAD32_U:
  7661. case WASM_OP_F32_LOAD:
  7662. case WASM_OP_F64_LOAD:
  7663. /* store */
  7664. case WASM_OP_I32_STORE:
  7665. case WASM_OP_I32_STORE8:
  7666. case WASM_OP_I32_STORE16:
  7667. case WASM_OP_I64_STORE:
  7668. case WASM_OP_I64_STORE8:
  7669. case WASM_OP_I64_STORE16:
  7670. case WASM_OP_I64_STORE32:
  7671. case WASM_OP_F32_STORE:
  7672. case WASM_OP_F64_STORE:
  7673. {
  7674. #if WASM_ENABLE_FAST_INTERP != 0
  7675. /* change F32/F64 into I32/I64 */
  7676. if (opcode == WASM_OP_F32_LOAD) {
  7677. skip_label();
  7678. emit_label(WASM_OP_I32_LOAD);
  7679. }
  7680. else if (opcode == WASM_OP_F64_LOAD) {
  7681. skip_label();
  7682. emit_label(WASM_OP_I64_LOAD);
  7683. }
  7684. else if (opcode == WASM_OP_F32_STORE) {
  7685. skip_label();
  7686. emit_label(WASM_OP_I32_STORE);
  7687. }
  7688. else if (opcode == WASM_OP_F64_STORE) {
  7689. skip_label();
  7690. emit_label(WASM_OP_I64_STORE);
  7691. }
  7692. #endif
  7693. CHECK_MEMORY();
  7694. read_leb_uint32(p, p_end, align); /* align */
  7695. read_leb_uint32(p, p_end, mem_offset); /* offset */
  7696. if (!check_memory_access_align(opcode, align, error_buf,
  7697. error_buf_size)) {
  7698. goto fail;
  7699. }
  7700. #if WASM_ENABLE_FAST_INTERP != 0
  7701. emit_uint32(loader_ctx, mem_offset);
  7702. #endif
  7703. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7704. func->has_memory_operations = true;
  7705. #endif
  7706. switch (opcode) {
  7707. /* load */
  7708. case WASM_OP_I32_LOAD:
  7709. case WASM_OP_I32_LOAD8_S:
  7710. case WASM_OP_I32_LOAD8_U:
  7711. case WASM_OP_I32_LOAD16_S:
  7712. case WASM_OP_I32_LOAD16_U:
  7713. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7714. break;
  7715. case WASM_OP_I64_LOAD:
  7716. case WASM_OP_I64_LOAD8_S:
  7717. case WASM_OP_I64_LOAD8_U:
  7718. case WASM_OP_I64_LOAD16_S:
  7719. case WASM_OP_I64_LOAD16_U:
  7720. case WASM_OP_I64_LOAD32_S:
  7721. case WASM_OP_I64_LOAD32_U:
  7722. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7723. break;
  7724. case WASM_OP_F32_LOAD:
  7725. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  7726. break;
  7727. case WASM_OP_F64_LOAD:
  7728. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  7729. break;
  7730. /* store */
  7731. case WASM_OP_I32_STORE:
  7732. case WASM_OP_I32_STORE8:
  7733. case WASM_OP_I32_STORE16:
  7734. POP_I32();
  7735. POP_I32();
  7736. break;
  7737. case WASM_OP_I64_STORE:
  7738. case WASM_OP_I64_STORE8:
  7739. case WASM_OP_I64_STORE16:
  7740. case WASM_OP_I64_STORE32:
  7741. POP_I64();
  7742. POP_I32();
  7743. break;
  7744. case WASM_OP_F32_STORE:
  7745. POP_F32();
  7746. POP_I32();
  7747. break;
  7748. case WASM_OP_F64_STORE:
  7749. POP_F64();
  7750. POP_I32();
  7751. break;
  7752. default:
  7753. break;
  7754. }
  7755. break;
  7756. }
  7757. case WASM_OP_MEMORY_SIZE:
  7758. CHECK_MEMORY();
  7759. /* reserved byte 0x00 */
  7760. if (*p++ != 0x00) {
  7761. set_error_buf(error_buf, error_buf_size,
  7762. "zero byte expected");
  7763. goto fail;
  7764. }
  7765. PUSH_I32();
  7766. module->possible_memory_grow = true;
  7767. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7768. func->has_memory_operations = true;
  7769. #endif
  7770. break;
  7771. case WASM_OP_MEMORY_GROW:
  7772. CHECK_MEMORY();
  7773. /* reserved byte 0x00 */
  7774. if (*p++ != 0x00) {
  7775. set_error_buf(error_buf, error_buf_size,
  7776. "zero byte expected");
  7777. goto fail;
  7778. }
  7779. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7780. module->possible_memory_grow = true;
  7781. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  7782. || WASM_ENABLE_WAMR_COMPILER != 0
  7783. func->has_op_memory_grow = true;
  7784. #endif
  7785. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  7786. func->has_memory_operations = true;
  7787. #endif
  7788. break;
  7789. case WASM_OP_I32_CONST:
  7790. read_leb_int32(p, p_end, i32_const);
  7791. #if WASM_ENABLE_FAST_INTERP != 0
  7792. skip_label();
  7793. disable_emit = true;
  7794. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  7795. if (operand_offset == 0) {
  7796. disable_emit = false;
  7797. emit_label(WASM_OP_I32_CONST);
  7798. emit_uint32(loader_ctx, i32_const);
  7799. }
  7800. #else
  7801. (void)i32_const;
  7802. #endif
  7803. PUSH_I32();
  7804. break;
  7805. case WASM_OP_I64_CONST:
  7806. read_leb_int64(p, p_end, i64_const);
  7807. #if WASM_ENABLE_FAST_INTERP != 0
  7808. skip_label();
  7809. disable_emit = true;
  7810. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  7811. if (operand_offset == 0) {
  7812. disable_emit = false;
  7813. emit_label(WASM_OP_I64_CONST);
  7814. emit_uint64(loader_ctx, i64_const);
  7815. }
  7816. #endif
  7817. PUSH_I64();
  7818. break;
  7819. case WASM_OP_F32_CONST:
  7820. p += sizeof(float32);
  7821. #if WASM_ENABLE_FAST_INTERP != 0
  7822. skip_label();
  7823. disable_emit = true;
  7824. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  7825. sizeof(float32));
  7826. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  7827. if (operand_offset == 0) {
  7828. disable_emit = false;
  7829. emit_label(WASM_OP_F32_CONST);
  7830. emit_float32(loader_ctx, f32_const);
  7831. }
  7832. #endif
  7833. PUSH_F32();
  7834. break;
  7835. case WASM_OP_F64_CONST:
  7836. p += sizeof(float64);
  7837. #if WASM_ENABLE_FAST_INTERP != 0
  7838. skip_label();
  7839. disable_emit = true;
  7840. /* Some MCU may require 8-byte align */
  7841. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  7842. sizeof(float64));
  7843. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  7844. if (operand_offset == 0) {
  7845. disable_emit = false;
  7846. emit_label(WASM_OP_F64_CONST);
  7847. emit_float64(loader_ctx, f64_const);
  7848. }
  7849. #endif
  7850. PUSH_F64();
  7851. break;
  7852. case WASM_OP_I32_EQZ:
  7853. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7854. break;
  7855. case WASM_OP_I32_EQ:
  7856. case WASM_OP_I32_NE:
  7857. case WASM_OP_I32_LT_S:
  7858. case WASM_OP_I32_LT_U:
  7859. case WASM_OP_I32_GT_S:
  7860. case WASM_OP_I32_GT_U:
  7861. case WASM_OP_I32_LE_S:
  7862. case WASM_OP_I32_LE_U:
  7863. case WASM_OP_I32_GE_S:
  7864. case WASM_OP_I32_GE_U:
  7865. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7866. break;
  7867. case WASM_OP_I64_EQZ:
  7868. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7869. break;
  7870. case WASM_OP_I64_EQ:
  7871. case WASM_OP_I64_NE:
  7872. case WASM_OP_I64_LT_S:
  7873. case WASM_OP_I64_LT_U:
  7874. case WASM_OP_I64_GT_S:
  7875. case WASM_OP_I64_GT_U:
  7876. case WASM_OP_I64_LE_S:
  7877. case WASM_OP_I64_LE_U:
  7878. case WASM_OP_I64_GE_S:
  7879. case WASM_OP_I64_GE_U:
  7880. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7881. break;
  7882. case WASM_OP_F32_EQ:
  7883. case WASM_OP_F32_NE:
  7884. case WASM_OP_F32_LT:
  7885. case WASM_OP_F32_GT:
  7886. case WASM_OP_F32_LE:
  7887. case WASM_OP_F32_GE:
  7888. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7889. break;
  7890. case WASM_OP_F64_EQ:
  7891. case WASM_OP_F64_NE:
  7892. case WASM_OP_F64_LT:
  7893. case WASM_OP_F64_GT:
  7894. case WASM_OP_F64_LE:
  7895. case WASM_OP_F64_GE:
  7896. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7897. break;
  7898. case WASM_OP_I32_CLZ:
  7899. case WASM_OP_I32_CTZ:
  7900. case WASM_OP_I32_POPCNT:
  7901. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7902. break;
  7903. case WASM_OP_I32_ADD:
  7904. case WASM_OP_I32_SUB:
  7905. case WASM_OP_I32_MUL:
  7906. case WASM_OP_I32_DIV_S:
  7907. case WASM_OP_I32_DIV_U:
  7908. case WASM_OP_I32_REM_S:
  7909. case WASM_OP_I32_REM_U:
  7910. case WASM_OP_I32_AND:
  7911. case WASM_OP_I32_OR:
  7912. case WASM_OP_I32_XOR:
  7913. case WASM_OP_I32_SHL:
  7914. case WASM_OP_I32_SHR_S:
  7915. case WASM_OP_I32_SHR_U:
  7916. case WASM_OP_I32_ROTL:
  7917. case WASM_OP_I32_ROTR:
  7918. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  7919. break;
  7920. case WASM_OP_I64_CLZ:
  7921. case WASM_OP_I64_CTZ:
  7922. case WASM_OP_I64_POPCNT:
  7923. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7924. break;
  7925. case WASM_OP_I64_ADD:
  7926. case WASM_OP_I64_SUB:
  7927. case WASM_OP_I64_MUL:
  7928. case WASM_OP_I64_DIV_S:
  7929. case WASM_OP_I64_DIV_U:
  7930. case WASM_OP_I64_REM_S:
  7931. case WASM_OP_I64_REM_U:
  7932. case WASM_OP_I64_AND:
  7933. case WASM_OP_I64_OR:
  7934. case WASM_OP_I64_XOR:
  7935. case WASM_OP_I64_SHL:
  7936. case WASM_OP_I64_SHR_S:
  7937. case WASM_OP_I64_SHR_U:
  7938. case WASM_OP_I64_ROTL:
  7939. case WASM_OP_I64_ROTR:
  7940. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  7941. break;
  7942. case WASM_OP_F32_ABS:
  7943. case WASM_OP_F32_NEG:
  7944. case WASM_OP_F32_CEIL:
  7945. case WASM_OP_F32_FLOOR:
  7946. case WASM_OP_F32_TRUNC:
  7947. case WASM_OP_F32_NEAREST:
  7948. case WASM_OP_F32_SQRT:
  7949. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7950. break;
  7951. case WASM_OP_F32_ADD:
  7952. case WASM_OP_F32_SUB:
  7953. case WASM_OP_F32_MUL:
  7954. case WASM_OP_F32_DIV:
  7955. case WASM_OP_F32_MIN:
  7956. case WASM_OP_F32_MAX:
  7957. case WASM_OP_F32_COPYSIGN:
  7958. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  7959. break;
  7960. case WASM_OP_F64_ABS:
  7961. case WASM_OP_F64_NEG:
  7962. case WASM_OP_F64_CEIL:
  7963. case WASM_OP_F64_FLOOR:
  7964. case WASM_OP_F64_TRUNC:
  7965. case WASM_OP_F64_NEAREST:
  7966. case WASM_OP_F64_SQRT:
  7967. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7968. break;
  7969. case WASM_OP_F64_ADD:
  7970. case WASM_OP_F64_SUB:
  7971. case WASM_OP_F64_MUL:
  7972. case WASM_OP_F64_DIV:
  7973. case WASM_OP_F64_MIN:
  7974. case WASM_OP_F64_MAX:
  7975. case WASM_OP_F64_COPYSIGN:
  7976. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  7977. break;
  7978. case WASM_OP_I32_WRAP_I64:
  7979. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  7980. break;
  7981. case WASM_OP_I32_TRUNC_S_F32:
  7982. case WASM_OP_I32_TRUNC_U_F32:
  7983. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  7984. break;
  7985. case WASM_OP_I32_TRUNC_S_F64:
  7986. case WASM_OP_I32_TRUNC_U_F64:
  7987. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  7988. break;
  7989. case WASM_OP_I64_EXTEND_S_I32:
  7990. case WASM_OP_I64_EXTEND_U_I32:
  7991. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  7992. break;
  7993. case WASM_OP_I64_TRUNC_S_F32:
  7994. case WASM_OP_I64_TRUNC_U_F32:
  7995. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  7996. break;
  7997. case WASM_OP_I64_TRUNC_S_F64:
  7998. case WASM_OP_I64_TRUNC_U_F64:
  7999. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  8000. break;
  8001. case WASM_OP_F32_CONVERT_S_I32:
  8002. case WASM_OP_F32_CONVERT_U_I32:
  8003. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  8004. break;
  8005. case WASM_OP_F32_CONVERT_S_I64:
  8006. case WASM_OP_F32_CONVERT_U_I64:
  8007. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  8008. break;
  8009. case WASM_OP_F32_DEMOTE_F64:
  8010. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  8011. break;
  8012. case WASM_OP_F64_CONVERT_S_I32:
  8013. case WASM_OP_F64_CONVERT_U_I32:
  8014. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  8015. break;
  8016. case WASM_OP_F64_CONVERT_S_I64:
  8017. case WASM_OP_F64_CONVERT_U_I64:
  8018. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  8019. break;
  8020. case WASM_OP_F64_PROMOTE_F32:
  8021. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  8022. break;
  8023. case WASM_OP_I32_REINTERPRET_F32:
  8024. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  8025. break;
  8026. case WASM_OP_I64_REINTERPRET_F64:
  8027. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  8028. break;
  8029. case WASM_OP_F32_REINTERPRET_I32:
  8030. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  8031. break;
  8032. case WASM_OP_F64_REINTERPRET_I64:
  8033. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  8034. break;
  8035. case WASM_OP_I32_EXTEND8_S:
  8036. case WASM_OP_I32_EXTEND16_S:
  8037. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8038. break;
  8039. case WASM_OP_I64_EXTEND8_S:
  8040. case WASM_OP_I64_EXTEND16_S:
  8041. case WASM_OP_I64_EXTEND32_S:
  8042. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  8043. break;
  8044. case WASM_OP_MISC_PREFIX:
  8045. {
  8046. uint32 opcode1;
  8047. read_leb_uint32(p, p_end, opcode1);
  8048. #if WASM_ENABLE_FAST_INTERP != 0
  8049. emit_byte(loader_ctx, ((uint8)opcode1));
  8050. #endif
  8051. switch (opcode1) {
  8052. case WASM_OP_I32_TRUNC_SAT_S_F32:
  8053. case WASM_OP_I32_TRUNC_SAT_U_F32:
  8054. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  8055. break;
  8056. case WASM_OP_I32_TRUNC_SAT_S_F64:
  8057. case WASM_OP_I32_TRUNC_SAT_U_F64:
  8058. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  8059. break;
  8060. case WASM_OP_I64_TRUNC_SAT_S_F32:
  8061. case WASM_OP_I64_TRUNC_SAT_U_F32:
  8062. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  8063. break;
  8064. case WASM_OP_I64_TRUNC_SAT_S_F64:
  8065. case WASM_OP_I64_TRUNC_SAT_U_F64:
  8066. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  8067. break;
  8068. #if WASM_ENABLE_BULK_MEMORY != 0
  8069. case WASM_OP_MEMORY_INIT:
  8070. {
  8071. read_leb_uint32(p, p_end, data_seg_idx);
  8072. #if WASM_ENABLE_FAST_INTERP != 0
  8073. emit_uint32(loader_ctx, data_seg_idx);
  8074. #endif
  8075. if (module->import_memory_count == 0
  8076. && module->memory_count == 0)
  8077. goto fail_unknown_memory;
  8078. if (*p++ != 0x00)
  8079. goto fail_zero_byte_expected;
  8080. if (data_seg_idx >= module->data_seg_count) {
  8081. set_error_buf_v(error_buf, error_buf_size,
  8082. "unknown data segment %d",
  8083. data_seg_idx);
  8084. goto fail;
  8085. }
  8086. if (module->data_seg_count1 == 0)
  8087. goto fail_data_cnt_sec_require;
  8088. POP_I32();
  8089. POP_I32();
  8090. POP_I32();
  8091. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8092. func->has_memory_operations = true;
  8093. #endif
  8094. break;
  8095. }
  8096. case WASM_OP_DATA_DROP:
  8097. {
  8098. read_leb_uint32(p, p_end, data_seg_idx);
  8099. #if WASM_ENABLE_FAST_INTERP != 0
  8100. emit_uint32(loader_ctx, data_seg_idx);
  8101. #endif
  8102. if (data_seg_idx >= module->data_seg_count) {
  8103. set_error_buf(error_buf, error_buf_size,
  8104. "unknown data segment");
  8105. goto fail;
  8106. }
  8107. if (module->data_seg_count1 == 0)
  8108. goto fail_data_cnt_sec_require;
  8109. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8110. func->has_memory_operations = true;
  8111. #endif
  8112. break;
  8113. }
  8114. case WASM_OP_MEMORY_COPY:
  8115. {
  8116. /* both src and dst memory index should be 0 */
  8117. if (*(int16 *)p != 0x0000)
  8118. goto fail_zero_byte_expected;
  8119. p += 2;
  8120. if (module->import_memory_count == 0
  8121. && module->memory_count == 0)
  8122. goto fail_unknown_memory;
  8123. POP_I32();
  8124. POP_I32();
  8125. POP_I32();
  8126. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8127. func->has_memory_operations = true;
  8128. #endif
  8129. break;
  8130. }
  8131. case WASM_OP_MEMORY_FILL:
  8132. {
  8133. if (*p++ != 0x00) {
  8134. goto fail_zero_byte_expected;
  8135. }
  8136. if (module->import_memory_count == 0
  8137. && module->memory_count == 0) {
  8138. goto fail_unknown_memory;
  8139. }
  8140. POP_I32();
  8141. POP_I32();
  8142. POP_I32();
  8143. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8144. func->has_memory_operations = true;
  8145. #endif
  8146. break;
  8147. }
  8148. fail_zero_byte_expected:
  8149. set_error_buf(error_buf, error_buf_size,
  8150. "zero byte expected");
  8151. goto fail;
  8152. fail_unknown_memory:
  8153. set_error_buf(error_buf, error_buf_size,
  8154. "unknown memory 0");
  8155. goto fail;
  8156. fail_data_cnt_sec_require:
  8157. set_error_buf(error_buf, error_buf_size,
  8158. "data count section required");
  8159. goto fail;
  8160. #endif /* WASM_ENABLE_BULK_MEMORY */
  8161. #if WASM_ENABLE_REF_TYPES != 0
  8162. case WASM_OP_TABLE_INIT:
  8163. {
  8164. uint8 seg_ref_type = 0, tbl_ref_type = 0;
  8165. read_leb_uint32(p, p_end, table_seg_idx);
  8166. read_leb_uint32(p, p_end, table_idx);
  8167. if (!get_table_elem_type(module, table_idx,
  8168. &tbl_ref_type, error_buf,
  8169. error_buf_size))
  8170. goto fail;
  8171. if (!get_table_seg_elem_type(module, table_seg_idx,
  8172. &seg_ref_type, error_buf,
  8173. error_buf_size))
  8174. goto fail;
  8175. if (seg_ref_type != tbl_ref_type) {
  8176. set_error_buf(error_buf, error_buf_size,
  8177. "type mismatch");
  8178. goto fail;
  8179. }
  8180. #if WASM_ENABLE_FAST_INTERP != 0
  8181. emit_uint32(loader_ctx, table_seg_idx);
  8182. emit_uint32(loader_ctx, table_idx);
  8183. #endif
  8184. POP_I32();
  8185. POP_I32();
  8186. POP_I32();
  8187. break;
  8188. }
  8189. case WASM_OP_ELEM_DROP:
  8190. {
  8191. read_leb_uint32(p, p_end, table_seg_idx);
  8192. if (!get_table_seg_elem_type(module, table_seg_idx,
  8193. NULL, error_buf,
  8194. error_buf_size))
  8195. goto fail;
  8196. #if WASM_ENABLE_FAST_INTERP != 0
  8197. emit_uint32(loader_ctx, table_seg_idx);
  8198. #endif
  8199. break;
  8200. }
  8201. case WASM_OP_TABLE_COPY:
  8202. {
  8203. uint8 src_ref_type, dst_ref_type;
  8204. uint32 src_tbl_idx, dst_tbl_idx;
  8205. read_leb_uint32(p, p_end, src_tbl_idx);
  8206. if (!get_table_elem_type(module, src_tbl_idx,
  8207. &src_ref_type, error_buf,
  8208. error_buf_size))
  8209. goto fail;
  8210. read_leb_uint32(p, p_end, dst_tbl_idx);
  8211. if (!get_table_elem_type(module, dst_tbl_idx,
  8212. &dst_ref_type, error_buf,
  8213. error_buf_size))
  8214. goto fail;
  8215. if (src_ref_type != dst_ref_type) {
  8216. set_error_buf(error_buf, error_buf_size,
  8217. "type mismatch");
  8218. goto fail;
  8219. }
  8220. #if WASM_ENABLE_FAST_INTERP != 0
  8221. emit_uint32(loader_ctx, src_tbl_idx);
  8222. emit_uint32(loader_ctx, dst_tbl_idx);
  8223. #endif
  8224. POP_I32();
  8225. POP_I32();
  8226. POP_I32();
  8227. break;
  8228. }
  8229. case WASM_OP_TABLE_SIZE:
  8230. {
  8231. read_leb_uint32(p, p_end, table_idx);
  8232. /* TODO: shall we create a new function to check
  8233. table idx instead of using below function? */
  8234. if (!get_table_elem_type(module, table_idx, NULL,
  8235. error_buf, error_buf_size))
  8236. goto fail;
  8237. #if WASM_ENABLE_FAST_INTERP != 0
  8238. emit_uint32(loader_ctx, table_idx);
  8239. #endif
  8240. PUSH_I32();
  8241. break;
  8242. }
  8243. case WASM_OP_TABLE_GROW:
  8244. case WASM_OP_TABLE_FILL:
  8245. {
  8246. uint8 decl_ref_type;
  8247. read_leb_uint32(p, p_end, table_idx);
  8248. if (!get_table_elem_type(module, table_idx,
  8249. &decl_ref_type, error_buf,
  8250. error_buf_size))
  8251. goto fail;
  8252. if (opcode1 == WASM_OP_TABLE_GROW) {
  8253. if (table_idx < module->import_table_count) {
  8254. module->import_tables[table_idx]
  8255. .u.table.possible_grow = true;
  8256. }
  8257. else {
  8258. module
  8259. ->tables[table_idx
  8260. - module->import_table_count]
  8261. .possible_grow = true;
  8262. }
  8263. }
  8264. #if WASM_ENABLE_FAST_INTERP != 0
  8265. emit_uint32(loader_ctx, table_idx);
  8266. #endif
  8267. POP_I32();
  8268. #if WASM_ENABLE_FAST_INTERP != 0
  8269. POP_OFFSET_TYPE(decl_ref_type);
  8270. #endif
  8271. POP_TYPE(decl_ref_type);
  8272. if (opcode1 == WASM_OP_TABLE_GROW)
  8273. PUSH_I32();
  8274. else
  8275. POP_I32();
  8276. break;
  8277. }
  8278. #endif /* WASM_ENABLE_REF_TYPES */
  8279. default:
  8280. set_error_buf_v(error_buf, error_buf_size,
  8281. "%s %02x %02x", "unsupported opcode",
  8282. 0xfc, opcode1);
  8283. goto fail;
  8284. }
  8285. break;
  8286. }
  8287. #if WASM_ENABLE_SIMD != 0
  8288. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8289. case WASM_OP_SIMD_PREFIX:
  8290. {
  8291. opcode = read_uint8(p);
  8292. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  8293. */
  8294. switch (opcode) {
  8295. /* memory instruction */
  8296. case SIMD_v128_load:
  8297. case SIMD_v128_load8x8_s:
  8298. case SIMD_v128_load8x8_u:
  8299. case SIMD_v128_load16x4_s:
  8300. case SIMD_v128_load16x4_u:
  8301. case SIMD_v128_load32x2_s:
  8302. case SIMD_v128_load32x2_u:
  8303. case SIMD_v128_load8_splat:
  8304. case SIMD_v128_load16_splat:
  8305. case SIMD_v128_load32_splat:
  8306. case SIMD_v128_load64_splat:
  8307. {
  8308. CHECK_MEMORY();
  8309. read_leb_uint32(p, p_end, align); /* align */
  8310. if (!check_simd_memory_access_align(
  8311. opcode, align, error_buf, error_buf_size)) {
  8312. goto fail;
  8313. }
  8314. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8315. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  8316. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8317. func->has_memory_operations = true;
  8318. #endif
  8319. break;
  8320. }
  8321. case SIMD_v128_store:
  8322. {
  8323. CHECK_MEMORY();
  8324. read_leb_uint32(p, p_end, align); /* align */
  8325. if (!check_simd_memory_access_align(
  8326. opcode, align, error_buf, error_buf_size)) {
  8327. goto fail;
  8328. }
  8329. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8330. POP_V128();
  8331. POP_I32();
  8332. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8333. func->has_memory_operations = true;
  8334. #endif
  8335. break;
  8336. }
  8337. /* basic operation */
  8338. case SIMD_v128_const:
  8339. {
  8340. CHECK_BUF1(p, p_end, 16);
  8341. p += 16;
  8342. PUSH_V128();
  8343. break;
  8344. }
  8345. case SIMD_v8x16_shuffle:
  8346. {
  8347. V128 mask;
  8348. CHECK_BUF1(p, p_end, 16);
  8349. mask = read_i8x16(p, error_buf, error_buf_size);
  8350. p += 16;
  8351. if (!check_simd_shuffle_mask(mask, error_buf,
  8352. error_buf_size)) {
  8353. goto fail;
  8354. }
  8355. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8356. break;
  8357. }
  8358. case SIMD_v8x16_swizzle:
  8359. {
  8360. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8361. break;
  8362. }
  8363. /* splat operation */
  8364. case SIMD_i8x16_splat:
  8365. case SIMD_i16x8_splat:
  8366. case SIMD_i32x4_splat:
  8367. case SIMD_i64x2_splat:
  8368. case SIMD_f32x4_splat:
  8369. case SIMD_f64x2_splat:
  8370. {
  8371. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  8372. VALUE_TYPE_I32, VALUE_TYPE_I64,
  8373. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  8374. POP_AND_PUSH(pop_type[opcode - SIMD_i8x16_splat],
  8375. VALUE_TYPE_V128);
  8376. break;
  8377. }
  8378. /* lane operation */
  8379. case SIMD_i8x16_extract_lane_s:
  8380. case SIMD_i8x16_extract_lane_u:
  8381. case SIMD_i8x16_replace_lane:
  8382. case SIMD_i16x8_extract_lane_s:
  8383. case SIMD_i16x8_extract_lane_u:
  8384. case SIMD_i16x8_replace_lane:
  8385. case SIMD_i32x4_extract_lane:
  8386. case SIMD_i32x4_replace_lane:
  8387. case SIMD_i64x2_extract_lane:
  8388. case SIMD_i64x2_replace_lane:
  8389. case SIMD_f32x4_extract_lane:
  8390. case SIMD_f32x4_replace_lane:
  8391. case SIMD_f64x2_extract_lane:
  8392. case SIMD_f64x2_replace_lane:
  8393. {
  8394. uint8 lane;
  8395. /* clang-format off */
  8396. uint8 replace[] = {
  8397. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  8398. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  8399. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  8400. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  8401. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  8402. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  8403. };
  8404. uint8 push_type[] = {
  8405. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  8406. VALUE_TYPE_V128,
  8407. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  8408. VALUE_TYPE_V128,
  8409. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  8410. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  8411. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  8412. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  8413. };
  8414. /* clang-format on */
  8415. CHECK_BUF(p, p_end, 1);
  8416. lane = read_uint8(p);
  8417. if (!check_simd_access_lane(opcode, lane, error_buf,
  8418. error_buf_size)) {
  8419. goto fail;
  8420. }
  8421. if (replace[opcode - SIMD_i8x16_extract_lane_s]) {
  8422. if (!(wasm_loader_pop_frame_ref(
  8423. loader_ctx,
  8424. replace[opcode - SIMD_i8x16_extract_lane_s],
  8425. error_buf, error_buf_size)))
  8426. goto fail;
  8427. }
  8428. POP_AND_PUSH(
  8429. VALUE_TYPE_V128,
  8430. push_type[opcode - SIMD_i8x16_extract_lane_s]);
  8431. break;
  8432. }
  8433. /* i8x16 compare operation */
  8434. case SIMD_i8x16_eq:
  8435. case SIMD_i8x16_ne:
  8436. case SIMD_i8x16_lt_s:
  8437. case SIMD_i8x16_lt_u:
  8438. case SIMD_i8x16_gt_s:
  8439. case SIMD_i8x16_gt_u:
  8440. case SIMD_i8x16_le_s:
  8441. case SIMD_i8x16_le_u:
  8442. case SIMD_i8x16_ge_s:
  8443. case SIMD_i8x16_ge_u:
  8444. /* i16x8 compare operation */
  8445. case SIMD_i16x8_eq:
  8446. case SIMD_i16x8_ne:
  8447. case SIMD_i16x8_lt_s:
  8448. case SIMD_i16x8_lt_u:
  8449. case SIMD_i16x8_gt_s:
  8450. case SIMD_i16x8_gt_u:
  8451. case SIMD_i16x8_le_s:
  8452. case SIMD_i16x8_le_u:
  8453. case SIMD_i16x8_ge_s:
  8454. case SIMD_i16x8_ge_u:
  8455. /* i32x4 compare operation */
  8456. case SIMD_i32x4_eq:
  8457. case SIMD_i32x4_ne:
  8458. case SIMD_i32x4_lt_s:
  8459. case SIMD_i32x4_lt_u:
  8460. case SIMD_i32x4_gt_s:
  8461. case SIMD_i32x4_gt_u:
  8462. case SIMD_i32x4_le_s:
  8463. case SIMD_i32x4_le_u:
  8464. case SIMD_i32x4_ge_s:
  8465. case SIMD_i32x4_ge_u:
  8466. /* f32x4 compare operation */
  8467. case SIMD_f32x4_eq:
  8468. case SIMD_f32x4_ne:
  8469. case SIMD_f32x4_lt:
  8470. case SIMD_f32x4_gt:
  8471. case SIMD_f32x4_le:
  8472. case SIMD_f32x4_ge:
  8473. /* f64x2 compare operation */
  8474. case SIMD_f64x2_eq:
  8475. case SIMD_f64x2_ne:
  8476. case SIMD_f64x2_lt:
  8477. case SIMD_f64x2_gt:
  8478. case SIMD_f64x2_le:
  8479. case SIMD_f64x2_ge:
  8480. {
  8481. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8482. break;
  8483. }
  8484. /* v128 operation */
  8485. case SIMD_v128_not:
  8486. {
  8487. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8488. break;
  8489. }
  8490. case SIMD_v128_and:
  8491. case SIMD_v128_andnot:
  8492. case SIMD_v128_or:
  8493. case SIMD_v128_xor:
  8494. {
  8495. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8496. break;
  8497. }
  8498. case SIMD_v128_bitselect:
  8499. {
  8500. POP_V128();
  8501. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8502. break;
  8503. }
  8504. case SIMD_v128_any_true:
  8505. {
  8506. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8507. break;
  8508. }
  8509. /* Load Lane Operation */
  8510. case SIMD_v128_load8_lane:
  8511. case SIMD_v128_load16_lane:
  8512. case SIMD_v128_load32_lane:
  8513. case SIMD_v128_load64_lane:
  8514. case SIMD_v128_store8_lane:
  8515. case SIMD_v128_store16_lane:
  8516. case SIMD_v128_store32_lane:
  8517. case SIMD_v128_store64_lane:
  8518. {
  8519. uint8 lane;
  8520. CHECK_MEMORY();
  8521. read_leb_uint32(p, p_end, align); /* align */
  8522. if (!check_simd_memory_access_align(
  8523. opcode, align, error_buf, error_buf_size)) {
  8524. goto fail;
  8525. }
  8526. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8527. CHECK_BUF(p, p_end, 1);
  8528. lane = read_uint8(p);
  8529. if (!check_simd_access_lane(opcode, lane, error_buf,
  8530. error_buf_size)) {
  8531. goto fail;
  8532. }
  8533. POP_V128();
  8534. POP_I32();
  8535. if (opcode < SIMD_v128_store8_lane) {
  8536. PUSH_V128();
  8537. }
  8538. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8539. func->has_memory_operations = true;
  8540. #endif
  8541. break;
  8542. }
  8543. case SIMD_v128_load32_zero:
  8544. case SIMD_v128_load64_zero:
  8545. {
  8546. CHECK_MEMORY();
  8547. read_leb_uint32(p, p_end, align); /* align */
  8548. if (!check_simd_memory_access_align(
  8549. opcode, align, error_buf, error_buf_size)) {
  8550. goto fail;
  8551. }
  8552. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8553. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  8554. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8555. func->has_memory_operations = true;
  8556. #endif
  8557. break;
  8558. }
  8559. /* Float conversion */
  8560. case SIMD_f32x4_demote_f64x2_zero:
  8561. case SIMD_f64x2_promote_low_f32x4_zero:
  8562. {
  8563. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8564. break;
  8565. }
  8566. /* i8x16 Operation */
  8567. case SIMD_i8x16_abs:
  8568. case SIMD_i8x16_neg:
  8569. case SIMD_i8x16_popcnt:
  8570. {
  8571. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8572. break;
  8573. }
  8574. case SIMD_i8x16_all_true:
  8575. case SIMD_i8x16_bitmask:
  8576. {
  8577. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8578. break;
  8579. }
  8580. case SIMD_i8x16_narrow_i16x8_s:
  8581. case SIMD_i8x16_narrow_i16x8_u:
  8582. {
  8583. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8584. break;
  8585. }
  8586. case SIMD_f32x4_ceil:
  8587. case SIMD_f32x4_floor:
  8588. case SIMD_f32x4_trunc:
  8589. case SIMD_f32x4_nearest:
  8590. {
  8591. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8592. break;
  8593. }
  8594. case SIMD_i8x16_shl:
  8595. case SIMD_i8x16_shr_s:
  8596. case SIMD_i8x16_shr_u:
  8597. {
  8598. POP_I32();
  8599. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8600. break;
  8601. }
  8602. case SIMD_i8x16_add:
  8603. case SIMD_i8x16_add_sat_s:
  8604. case SIMD_i8x16_add_sat_u:
  8605. case SIMD_i8x16_sub:
  8606. case SIMD_i8x16_sub_sat_s:
  8607. case SIMD_i8x16_sub_sat_u:
  8608. {
  8609. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8610. break;
  8611. }
  8612. case SIMD_f64x2_ceil:
  8613. case SIMD_f64x2_floor:
  8614. {
  8615. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8616. break;
  8617. }
  8618. case SIMD_i8x16_min_s:
  8619. case SIMD_i8x16_min_u:
  8620. case SIMD_i8x16_max_s:
  8621. case SIMD_i8x16_max_u:
  8622. {
  8623. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8624. break;
  8625. }
  8626. case SIMD_f64x2_trunc:
  8627. {
  8628. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8629. break;
  8630. }
  8631. case SIMD_i8x16_avgr_u:
  8632. {
  8633. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8634. break;
  8635. }
  8636. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  8637. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  8638. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  8639. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  8640. /* i16x8 operation */
  8641. case SIMD_i16x8_abs:
  8642. case SIMD_i16x8_neg:
  8643. {
  8644. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8645. break;
  8646. }
  8647. case SIMD_i16x8_q15mulr_sat_s:
  8648. {
  8649. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8650. break;
  8651. }
  8652. case SIMD_i16x8_all_true:
  8653. case SIMD_i16x8_bitmask:
  8654. {
  8655. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8656. break;
  8657. }
  8658. case SIMD_i16x8_narrow_i32x4_s:
  8659. case SIMD_i16x8_narrow_i32x4_u:
  8660. {
  8661. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8662. break;
  8663. }
  8664. case SIMD_i16x8_extend_low_i8x16_s:
  8665. case SIMD_i16x8_extend_high_i8x16_s:
  8666. case SIMD_i16x8_extend_low_i8x16_u:
  8667. case SIMD_i16x8_extend_high_i8x16_u:
  8668. {
  8669. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8670. break;
  8671. }
  8672. case SIMD_i16x8_shl:
  8673. case SIMD_i16x8_shr_s:
  8674. case SIMD_i16x8_shr_u:
  8675. {
  8676. POP_I32();
  8677. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8678. break;
  8679. }
  8680. case SIMD_i16x8_add:
  8681. case SIMD_i16x8_add_sat_s:
  8682. case SIMD_i16x8_add_sat_u:
  8683. case SIMD_i16x8_sub:
  8684. case SIMD_i16x8_sub_sat_s:
  8685. case SIMD_i16x8_sub_sat_u:
  8686. {
  8687. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8688. break;
  8689. }
  8690. case SIMD_f64x2_nearest:
  8691. {
  8692. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8693. break;
  8694. }
  8695. case SIMD_i16x8_mul:
  8696. case SIMD_i16x8_min_s:
  8697. case SIMD_i16x8_min_u:
  8698. case SIMD_i16x8_max_s:
  8699. case SIMD_i16x8_max_u:
  8700. case SIMD_i16x8_avgr_u:
  8701. case SIMD_i16x8_extmul_low_i8x16_s:
  8702. case SIMD_i16x8_extmul_high_i8x16_s:
  8703. case SIMD_i16x8_extmul_low_i8x16_u:
  8704. case SIMD_i16x8_extmul_high_i8x16_u:
  8705. {
  8706. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8707. break;
  8708. }
  8709. /* i32x4 operation */
  8710. case SIMD_i32x4_abs:
  8711. case SIMD_i32x4_neg:
  8712. {
  8713. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8714. break;
  8715. }
  8716. case SIMD_i32x4_all_true:
  8717. case SIMD_i32x4_bitmask:
  8718. {
  8719. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8720. break;
  8721. }
  8722. case SIMD_i32x4_narrow_i64x2_s:
  8723. case SIMD_i32x4_narrow_i64x2_u:
  8724. {
  8725. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8726. break;
  8727. }
  8728. case SIMD_i32x4_extend_low_i16x8_s:
  8729. case SIMD_i32x4_extend_high_i16x8_s:
  8730. case SIMD_i32x4_extend_low_i16x8_u:
  8731. case SIMD_i32x4_extend_high_i16x8_u:
  8732. {
  8733. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8734. break;
  8735. }
  8736. case SIMD_i32x4_shl:
  8737. case SIMD_i32x4_shr_s:
  8738. case SIMD_i32x4_shr_u:
  8739. {
  8740. POP_I32();
  8741. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8742. break;
  8743. }
  8744. case SIMD_i32x4_add:
  8745. case SIMD_i32x4_sub:
  8746. case SIMD_i32x4_mul:
  8747. case SIMD_i32x4_min_s:
  8748. case SIMD_i32x4_min_u:
  8749. case SIMD_i32x4_max_s:
  8750. case SIMD_i32x4_max_u:
  8751. case SIMD_i32x4_dot_i16x8_s:
  8752. case SIMD_i32x4_avgr_u:
  8753. case SIMD_i32x4_extmul_low_i16x8_s:
  8754. case SIMD_i32x4_extmul_high_i16x8_s:
  8755. case SIMD_i32x4_extmul_low_i16x8_u:
  8756. case SIMD_i32x4_extmul_high_i16x8_u:
  8757. {
  8758. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8759. break;
  8760. }
  8761. /* i64x2 operation */
  8762. case SIMD_i64x2_abs:
  8763. case SIMD_i64x2_neg:
  8764. {
  8765. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8766. break;
  8767. }
  8768. case SIMD_i64x2_all_true:
  8769. case SIMD_i64x2_bitmask:
  8770. {
  8771. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  8772. break;
  8773. }
  8774. case SIMD_i64x2_extend_low_i32x4_s:
  8775. case SIMD_i64x2_extend_high_i32x4_s:
  8776. case SIMD_i64x2_extend_low_i32x4_u:
  8777. case SIMD_i64x2_extend_high_i32x4_u:
  8778. {
  8779. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8780. break;
  8781. }
  8782. case SIMD_i64x2_shl:
  8783. case SIMD_i64x2_shr_s:
  8784. case SIMD_i64x2_shr_u:
  8785. {
  8786. POP_I32();
  8787. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8788. break;
  8789. }
  8790. case SIMD_i64x2_add:
  8791. case SIMD_i64x2_sub:
  8792. case SIMD_i64x2_mul:
  8793. case SIMD_i64x2_eq:
  8794. case SIMD_i64x2_ne:
  8795. case SIMD_i64x2_lt_s:
  8796. case SIMD_i64x2_gt_s:
  8797. case SIMD_i64x2_le_s:
  8798. case SIMD_i64x2_ge_s:
  8799. case SIMD_i64x2_extmul_low_i32x4_s:
  8800. case SIMD_i64x2_extmul_high_i32x4_s:
  8801. case SIMD_i64x2_extmul_low_i32x4_u:
  8802. case SIMD_i64x2_extmul_high_i32x4_u:
  8803. {
  8804. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8805. break;
  8806. }
  8807. /* f32x4 operation */
  8808. case SIMD_f32x4_abs:
  8809. case SIMD_f32x4_neg:
  8810. case SIMD_f32x4_round:
  8811. case SIMD_f32x4_sqrt:
  8812. {
  8813. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8814. break;
  8815. }
  8816. case SIMD_f32x4_add:
  8817. case SIMD_f32x4_sub:
  8818. case SIMD_f32x4_mul:
  8819. case SIMD_f32x4_div:
  8820. case SIMD_f32x4_min:
  8821. case SIMD_f32x4_max:
  8822. case SIMD_f32x4_pmin:
  8823. case SIMD_f32x4_pmax:
  8824. {
  8825. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8826. break;
  8827. }
  8828. /* f64x2 operation */
  8829. case SIMD_f64x2_abs:
  8830. case SIMD_f64x2_neg:
  8831. case SIMD_f64x2_round:
  8832. case SIMD_f64x2_sqrt:
  8833. {
  8834. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8835. break;
  8836. }
  8837. case SIMD_f64x2_add:
  8838. case SIMD_f64x2_sub:
  8839. case SIMD_f64x2_mul:
  8840. case SIMD_f64x2_div:
  8841. case SIMD_f64x2_min:
  8842. case SIMD_f64x2_max:
  8843. case SIMD_f64x2_pmin:
  8844. case SIMD_f64x2_pmax:
  8845. {
  8846. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8847. break;
  8848. }
  8849. case SIMD_i32x4_trunc_sat_f32x4_s:
  8850. case SIMD_i32x4_trunc_sat_f32x4_u:
  8851. case SIMD_f32x4_convert_i32x4_s:
  8852. case SIMD_f32x4_convert_i32x4_u:
  8853. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  8854. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  8855. case SIMD_f64x2_convert_low_i32x4_s:
  8856. case SIMD_f64x2_convert_low_i32x4_u:
  8857. {
  8858. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  8859. break;
  8860. }
  8861. default:
  8862. {
  8863. if (error_buf != NULL) {
  8864. snprintf(error_buf, error_buf_size,
  8865. "WASM module load failed: "
  8866. "invalid opcode 0xfd %02x.",
  8867. opcode);
  8868. }
  8869. goto fail;
  8870. }
  8871. }
  8872. break;
  8873. }
  8874. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8875. #endif /* end of WASM_ENABLE_SIMD */
  8876. #if WASM_ENABLE_SHARED_MEMORY != 0
  8877. case WASM_OP_ATOMIC_PREFIX:
  8878. {
  8879. opcode = read_uint8(p);
  8880. #if WASM_ENABLE_FAST_INTERP != 0
  8881. emit_byte(loader_ctx, opcode);
  8882. #endif
  8883. if (opcode != WASM_OP_ATOMIC_FENCE) {
  8884. CHECK_MEMORY();
  8885. read_leb_uint32(p, p_end, align); /* align */
  8886. read_leb_uint32(p, p_end, mem_offset); /* offset */
  8887. if (!check_memory_align_equal(opcode, align, error_buf,
  8888. error_buf_size)) {
  8889. goto fail;
  8890. }
  8891. #if WASM_ENABLE_FAST_INTERP != 0
  8892. emit_uint32(loader_ctx, mem_offset);
  8893. #endif
  8894. }
  8895. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  8896. func->has_memory_operations = true;
  8897. #endif
  8898. switch (opcode) {
  8899. case WASM_OP_ATOMIC_NOTIFY:
  8900. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8901. break;
  8902. case WASM_OP_ATOMIC_WAIT32:
  8903. POP_I64();
  8904. POP_I32();
  8905. POP_I32();
  8906. PUSH_I32();
  8907. break;
  8908. case WASM_OP_ATOMIC_WAIT64:
  8909. POP_I64();
  8910. POP_I64();
  8911. POP_I32();
  8912. PUSH_I32();
  8913. break;
  8914. case WASM_OP_ATOMIC_FENCE:
  8915. /* reserved byte 0x00 */
  8916. if (*p++ != 0x00) {
  8917. set_error_buf(error_buf, error_buf_size,
  8918. "zero byte expected");
  8919. goto fail;
  8920. }
  8921. break;
  8922. case WASM_OP_ATOMIC_I32_LOAD:
  8923. case WASM_OP_ATOMIC_I32_LOAD8_U:
  8924. case WASM_OP_ATOMIC_I32_LOAD16_U:
  8925. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8926. break;
  8927. case WASM_OP_ATOMIC_I32_STORE:
  8928. case WASM_OP_ATOMIC_I32_STORE8:
  8929. case WASM_OP_ATOMIC_I32_STORE16:
  8930. POP_I32();
  8931. POP_I32();
  8932. break;
  8933. case WASM_OP_ATOMIC_I64_LOAD:
  8934. case WASM_OP_ATOMIC_I64_LOAD8_U:
  8935. case WASM_OP_ATOMIC_I64_LOAD16_U:
  8936. case WASM_OP_ATOMIC_I64_LOAD32_U:
  8937. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  8938. break;
  8939. case WASM_OP_ATOMIC_I64_STORE:
  8940. case WASM_OP_ATOMIC_I64_STORE8:
  8941. case WASM_OP_ATOMIC_I64_STORE16:
  8942. case WASM_OP_ATOMIC_I64_STORE32:
  8943. POP_I64();
  8944. POP_I32();
  8945. break;
  8946. case WASM_OP_ATOMIC_RMW_I32_ADD:
  8947. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  8948. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  8949. case WASM_OP_ATOMIC_RMW_I32_SUB:
  8950. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  8951. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  8952. case WASM_OP_ATOMIC_RMW_I32_AND:
  8953. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  8954. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  8955. case WASM_OP_ATOMIC_RMW_I32_OR:
  8956. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  8957. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  8958. case WASM_OP_ATOMIC_RMW_I32_XOR:
  8959. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  8960. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  8961. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  8962. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  8963. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  8964. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  8965. break;
  8966. case WASM_OP_ATOMIC_RMW_I64_ADD:
  8967. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  8968. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  8969. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  8970. case WASM_OP_ATOMIC_RMW_I64_SUB:
  8971. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  8972. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  8973. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  8974. case WASM_OP_ATOMIC_RMW_I64_AND:
  8975. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  8976. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  8977. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  8978. case WASM_OP_ATOMIC_RMW_I64_OR:
  8979. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  8980. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  8981. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  8982. case WASM_OP_ATOMIC_RMW_I64_XOR:
  8983. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  8984. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  8985. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  8986. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  8987. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  8988. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  8989. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  8990. POP_I64();
  8991. POP_I32();
  8992. PUSH_I64();
  8993. break;
  8994. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  8995. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  8996. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  8997. POP_I32();
  8998. POP_I32();
  8999. POP_I32();
  9000. PUSH_I32();
  9001. break;
  9002. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  9003. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  9004. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  9005. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  9006. POP_I64();
  9007. POP_I64();
  9008. POP_I32();
  9009. PUSH_I64();
  9010. break;
  9011. default:
  9012. set_error_buf_v(error_buf, error_buf_size,
  9013. "%s %02x %02x", "unsupported opcode",
  9014. 0xfe, opcode);
  9015. goto fail;
  9016. }
  9017. break;
  9018. }
  9019. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9020. default:
  9021. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  9022. "unsupported opcode", opcode);
  9023. goto fail;
  9024. }
  9025. #if WASM_ENABLE_FAST_INTERP != 0
  9026. last_op = opcode;
  9027. #endif
  9028. }
  9029. if (loader_ctx->csp_num > 0) {
  9030. if (cur_func_idx < module->function_count - 1)
  9031. /* Function with missing end marker (between two functions) */
  9032. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  9033. else
  9034. /* Function with missing end marker
  9035. (at EOF or end of code sections) */
  9036. set_error_buf(error_buf, error_buf_size,
  9037. "unexpected end of section or function, "
  9038. "or section size mismatch");
  9039. goto fail;
  9040. }
  9041. #if WASM_ENABLE_FAST_INTERP != 0
  9042. if (loader_ctx->p_code_compiled == NULL)
  9043. goto re_scan;
  9044. func->const_cell_num = loader_ctx->const_cell_num;
  9045. if (func->const_cell_num > 0) {
  9046. int32 j;
  9047. if (!(func->consts = func_const = loader_malloc(
  9048. func->const_cell_num * 4, error_buf, error_buf_size)))
  9049. goto fail;
  9050. func_const_end = func->consts + func->const_cell_num * 4;
  9051. /* reverse the const buf */
  9052. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  9053. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  9054. if (c->value_type == VALUE_TYPE_F64
  9055. || c->value_type == VALUE_TYPE_I64) {
  9056. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  9057. &(c->value.f64), (uint32)sizeof(int64));
  9058. func_const += sizeof(int64);
  9059. }
  9060. else {
  9061. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  9062. &(c->value.f32), (uint32)sizeof(int32));
  9063. func_const += sizeof(int32);
  9064. }
  9065. }
  9066. }
  9067. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  9068. - loader_ctx->start_dynamic_offset + 1;
  9069. #else
  9070. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  9071. #endif
  9072. func->max_block_num = loader_ctx->max_csp_num;
  9073. return_value = true;
  9074. fail:
  9075. wasm_loader_ctx_destroy(loader_ctx);
  9076. (void)table_idx;
  9077. (void)table_seg_idx;
  9078. (void)data_seg_idx;
  9079. (void)i64_const;
  9080. (void)local_offset;
  9081. (void)p_org;
  9082. (void)mem_offset;
  9083. (void)align;
  9084. return return_value;
  9085. }