wasm_mini_loader.c 260 KB

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