wasm_mini_loader.c 276 KB

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