wasm_mini_loader.c 237 KB

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