wasm_loader.c 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "../common/wasm_native.h"
  12. /* Read a value of given type from the address pointed to by the given
  13. pointer and increase the pointer to the position just after the
  14. value being read. */
  15. #define TEMPLATE_READ_VALUE(Type, p) \
  16. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  17. static void
  18. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  19. {
  20. if (error_buf != NULL)
  21. snprintf(error_buf, error_buf_size, "%s", string);
  22. }
  23. #define CHECK_BUF(buf, buf_end, length) do { \
  24. if (buf + length > buf_end) { \
  25. set_error_buf(error_buf, error_buf_size, \
  26. "WASM module load failed: " \
  27. "unexpected end of section or function"); \
  28. return false; \
  29. } \
  30. } while (0)
  31. #define CHECK_BUF1(buf, buf_end, length) do { \
  32. if (buf + length > buf_end) { \
  33. set_error_buf(error_buf, error_buf_size, \
  34. "WASM module load failed: unexpected end");\
  35. return false; \
  36. } \
  37. } while (0)
  38. static bool
  39. skip_leb(const uint8 *buf, const uint8 *buf_end,
  40. uint32 *p_offset, uint32 maxbits,
  41. char* error_buf, uint32 error_buf_size)
  42. {
  43. uint32 bcnt = 0;
  44. uint64 byte;
  45. while (true) {
  46. if (bcnt + 1 > (maxbits + 6) / 7) {
  47. set_error_buf(error_buf, error_buf_size,
  48. "WASM module load failed: "
  49. "integer representation too long");
  50. return false;
  51. }
  52. CHECK_BUF(buf, buf_end, *p_offset + 1);
  53. byte = buf[*p_offset];
  54. *p_offset += 1;
  55. bcnt += 1;
  56. if ((byte & 0x80) == 0) {
  57. break;
  58. }
  59. }
  60. return true;
  61. }
  62. #define skip_leb_int64(p, p_end) do { \
  63. uint32 off = 0; \
  64. if (!skip_leb(p, p_end, &off, 64, \
  65. error_buf, error_buf_size)) \
  66. return false; \
  67. p += off; \
  68. } while (0)
  69. #define skip_leb_uint32(p, p_end) do { \
  70. uint32 off = 0; \
  71. if (!skip_leb(p, p_end, &off, 32, \
  72. error_buf, error_buf_size)) \
  73. return false; \
  74. p += off; \
  75. } while (0)
  76. #define skip_leb_int32(p, p_end) do { \
  77. uint32 off = 0; \
  78. if (!skip_leb(p, p_end, &off, 32, \
  79. error_buf, error_buf_size)) \
  80. return false; \
  81. p += off; \
  82. } while (0)
  83. static bool
  84. read_leb(const uint8 *buf, const uint8 *buf_end,
  85. uint32 *p_offset, uint32 maxbits,
  86. bool sign, uint64 *p_result,
  87. char* error_buf, uint32 error_buf_size)
  88. {
  89. uint64 result = 0;
  90. uint32 shift = 0;
  91. uint32 bcnt = 0;
  92. uint64 byte;
  93. while (true) {
  94. if (bcnt + 1 > (maxbits + 6) / 7) {
  95. set_error_buf(error_buf, error_buf_size,
  96. "WASM module load failed: "
  97. "integer representation too long");
  98. return false;
  99. }
  100. CHECK_BUF(buf, buf_end, *p_offset + 1);
  101. byte = buf[*p_offset];
  102. *p_offset += 1;
  103. result |= ((byte & 0x7f) << shift);
  104. shift += 7;
  105. bcnt += 1;
  106. if ((byte & 0x80) == 0) {
  107. break;
  108. }
  109. }
  110. if (!sign && maxbits == 32 && shift >= maxbits) {
  111. /* The top bits set represent values > 32 bits */
  112. if (((uint8)byte) & 0xf0)
  113. goto fail_integer_too_large;
  114. }
  115. else if (sign && maxbits == 32) {
  116. if (shift < maxbits) {
  117. /* Sign extend */
  118. result = (((int32)result) << (maxbits - shift))
  119. >> (maxbits - shift);
  120. }
  121. else {
  122. /* The top bits should be a sign-extension of the sign bit */
  123. bool sign_bit_set = ((uint8)byte) & 0x8;
  124. int top_bits = ((uint8)byte) & 0xf0;
  125. if ((sign_bit_set && top_bits != 0x70)
  126. || (!sign_bit_set && top_bits != 0))
  127. goto fail_integer_too_large;
  128. }
  129. }
  130. else if (sign && maxbits == 64) {
  131. if (shift < maxbits) {
  132. /* Sign extend */
  133. result = (((int64)result) << (maxbits - shift))
  134. >> (maxbits - shift);
  135. }
  136. else {
  137. /* The top bits should be a sign-extension of the sign bit */
  138. bool sign_bit_set = ((uint8)byte) & 0x1;
  139. int top_bits = ((uint8)byte) & 0xfe;
  140. if ((sign_bit_set && top_bits != 0x7e)
  141. || (!sign_bit_set && top_bits != 0))
  142. goto fail_integer_too_large;
  143. }
  144. }
  145. *p_result = result;
  146. return true;
  147. fail_integer_too_large:
  148. set_error_buf(error_buf, error_buf_size,
  149. "WASM module load failed: integer too large");
  150. return false;
  151. }
  152. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  153. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  154. #define read_bool(p) TEMPLATE_READ_VALUE(bool, p)
  155. #define read_leb_int64(p, p_end, res) do { \
  156. if (p < p_end) { \
  157. uint8 _val = *p; \
  158. if (!(_val & 0x80)) { \
  159. res = (int64)_val; \
  160. if (_val & 0x40) \
  161. /* sign extend */ \
  162. res |= 0xFFFFFFFFFFFFFF80LL; \
  163. p++; \
  164. break; \
  165. } \
  166. } \
  167. uint32 off = 0; \
  168. uint64 res64; \
  169. if (!read_leb(p, p_end, &off, 64, true, &res64, \
  170. error_buf, error_buf_size)) \
  171. return false; \
  172. p += off; \
  173. res = (int64)res64; \
  174. } while (0)
  175. #define read_leb_uint32(p, p_end, res) do { \
  176. if (p < p_end) { \
  177. uint8 _val = *p; \
  178. if (!(_val & 0x80)) { \
  179. res = _val; \
  180. p++; \
  181. break; \
  182. } \
  183. } \
  184. uint32 off = 0; \
  185. uint64 res64; \
  186. if (!read_leb(p, p_end, &off, 32, false, &res64, \
  187. error_buf, error_buf_size)) \
  188. return false; \
  189. p += off; \
  190. res = (uint32)res64; \
  191. } while (0)
  192. #define read_leb_int32(p, p_end, res) do { \
  193. if (p < p_end) { \
  194. uint8 _val = *p; \
  195. if (!(_val & 0x80)) { \
  196. res = (int32)_val; \
  197. if (_val & 0x40) \
  198. /* sign extend */ \
  199. res |= 0xFFFFFF80; \
  200. p++; \
  201. break; \
  202. } \
  203. } \
  204. uint32 off = 0; \
  205. uint64 res64; \
  206. if (!read_leb(p, p_end, &off, 32, true, &res64, \
  207. error_buf, error_buf_size)) \
  208. return false; \
  209. p += off; \
  210. res = (int32)res64; \
  211. } while (0)
  212. static bool
  213. check_utf8_str(const uint8* str, uint32 len)
  214. {
  215. const uint8 *p = str, *p_end = str + len, *p_end1;
  216. uint8 chr, n_bytes;
  217. while (p < p_end) {
  218. chr = *p++;
  219. if (chr >= 0x80) {
  220. /* Calculate the byte count: the first byte must be
  221. 110XXXXX, 1110XXXX, 11110XXX, 111110XX, or 1111110X,
  222. the count of leading '1' denotes the total byte count */
  223. n_bytes = 0;
  224. while ((chr & 0x80) != 0) {
  225. chr = (uint8)(chr << 1);
  226. n_bytes++;
  227. }
  228. /* Check byte count */
  229. if (n_bytes < 2 || n_bytes > 6
  230. || p + n_bytes - 1 > p_end)
  231. return false;
  232. /* Check the following bytes, which must be 10XXXXXX */
  233. p_end1 = p + n_bytes - 1;
  234. while (p < p_end1) {
  235. if (!(*p & 0x80) || (*p | 0x40))
  236. return false;
  237. p++;
  238. }
  239. }
  240. }
  241. return true;
  242. }
  243. static char*
  244. const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
  245. char* error_buf, uint32 error_buf_size)
  246. {
  247. StringNode *node, *node_next;
  248. if (!check_utf8_str(str, len)) {
  249. set_error_buf(error_buf, error_buf_size,
  250. "WASM module load failed: "
  251. "invalid UTF-8 encoding");
  252. return NULL;
  253. }
  254. /* Search const str list */
  255. node = module->const_str_list;
  256. while (node) {
  257. node_next = node->next;
  258. if (strlen(node->str) == len
  259. && !memcmp(node->str, str, len))
  260. break;
  261. node = node_next;
  262. }
  263. if (node)
  264. return node->str;
  265. if (!(node = wasm_runtime_malloc(sizeof(StringNode) + len + 1))) {
  266. set_error_buf(error_buf, error_buf_size,
  267. "WASM module load failed: "
  268. "allocate memory failed.");
  269. return NULL;
  270. }
  271. node->str = ((char*)node) + sizeof(StringNode);
  272. bh_memcpy_s(node->str, len + 1, str, len);
  273. node->str[len] = '\0';
  274. if (!module->const_str_list) {
  275. /* set as head */
  276. module->const_str_list = node;
  277. node->next = NULL;
  278. }
  279. else {
  280. /* insert it */
  281. node->next = module->const_str_list;
  282. module->const_str_list = node;
  283. }
  284. return node->str;
  285. }
  286. static bool
  287. load_init_expr(const uint8 **p_buf, const uint8 *buf_end,
  288. InitializerExpression *init_expr,
  289. char *error_buf, uint32 error_buf_size)
  290. {
  291. const uint8 *p = *p_buf, *p_end = buf_end;
  292. uint8 flag, end_byte, *p_float;
  293. uint32 i;
  294. CHECK_BUF(p, p_end, 1);
  295. init_expr->init_expr_type = read_uint8(p);
  296. flag = init_expr->init_expr_type;
  297. switch (flag) {
  298. /* i32.const */
  299. case INIT_EXPR_TYPE_I32_CONST:
  300. read_leb_int32(p, p_end, init_expr->u.i32);
  301. break;
  302. /* i64.const */
  303. case INIT_EXPR_TYPE_I64_CONST:
  304. read_leb_int64(p, p_end, init_expr->u.i64);
  305. break;
  306. /* f32.const */
  307. case INIT_EXPR_TYPE_F32_CONST:
  308. CHECK_BUF(p, p_end, 4);
  309. p_float = (uint8*)&init_expr->u.f32;
  310. for (i = 0; i < sizeof(float32); i++)
  311. *p_float++ = *p++;
  312. break;
  313. /* f64.const */
  314. case INIT_EXPR_TYPE_F64_CONST:
  315. CHECK_BUF(p, p_end, 8);
  316. p_float = (uint8*)&init_expr->u.f64;
  317. for (i = 0; i < sizeof(float64); i++)
  318. *p_float++ = *p++;
  319. break;
  320. /* get_global */
  321. case INIT_EXPR_TYPE_GET_GLOBAL:
  322. read_leb_uint32(p, p_end, init_expr->u.global_index);
  323. break;
  324. default:
  325. set_error_buf(error_buf, error_buf_size,
  326. "WASM module load failed: type mismatch");
  327. return false;
  328. }
  329. CHECK_BUF(p, p_end, 1);
  330. end_byte = read_uint8(p);
  331. if (end_byte != 0x0b) {
  332. set_error_buf(error_buf, error_buf_size,
  333. "WASM module load failed: "
  334. "unexpected end of section or function");
  335. return false;
  336. }
  337. *p_buf = p;
  338. return true;
  339. }
  340. static bool
  341. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  342. char *error_buf, uint32 error_buf_size)
  343. {
  344. const uint8 *p = buf, *p_end = buf_end, *p_org;
  345. uint32 type_count, param_count, result_count, i, j;
  346. uint64 total_size;
  347. uint8 flag;
  348. WASMType *type;
  349. read_leb_uint32(p, p_end, type_count);
  350. if (type_count) {
  351. module->type_count = type_count;
  352. total_size = sizeof(WASMType*) * (uint64)type_count;
  353. if (total_size >= UINT32_MAX
  354. || !(module->types = wasm_runtime_malloc((uint32)total_size))) {
  355. set_error_buf(error_buf, error_buf_size,
  356. "Load type section failed: allocate memory failed.");
  357. return false;
  358. }
  359. memset(module->types, 0, (uint32)total_size);
  360. for (i = 0; i < type_count; i++) {
  361. CHECK_BUF(p, p_end, 1);
  362. flag = read_uint8(p);
  363. if (flag != 0x60) {
  364. set_error_buf(error_buf, error_buf_size,
  365. "Load type section failed: invalid type flag.");
  366. return false;
  367. }
  368. read_leb_uint32(p, p_end, param_count);
  369. /* Resolve param count and result count firstly */
  370. p_org = p;
  371. CHECK_BUF(p, p_end, param_count);
  372. p += param_count;
  373. read_leb_uint32(p, p_end, result_count);
  374. if (result_count > 1) {
  375. set_error_buf(error_buf, error_buf_size,
  376. "Load type section failed: invalid result count.");
  377. return false;
  378. }
  379. CHECK_BUF(p, p_end, result_count);
  380. p = p_org;
  381. total_size = offsetof(WASMType, types) +
  382. sizeof(uint8) * (uint64)(param_count + result_count);
  383. if (total_size >= UINT32_MAX
  384. || !(type = module->types[i] =
  385. wasm_runtime_malloc((uint32)total_size))) {
  386. set_error_buf(error_buf, error_buf_size,
  387. "Load type section failed: allocate memory failed.");
  388. return false;
  389. }
  390. /* Resolve param types and result types */
  391. type->param_count = param_count;
  392. type->result_count = result_count;
  393. for (j = 0; j < param_count; j++) {
  394. CHECK_BUF(p, p_end, 1);
  395. type->types[j] = read_uint8(p);
  396. }
  397. read_leb_uint32(p, p_end, result_count);
  398. for (j = 0; j < result_count; j++) {
  399. CHECK_BUF(p, p_end, 1);
  400. type->types[param_count + j] = read_uint8(p);
  401. }
  402. }
  403. }
  404. if (p != p_end) {
  405. set_error_buf(error_buf, error_buf_size,
  406. "Load type section failed: section size mismatch");
  407. return false;
  408. }
  409. LOG_VERBOSE("Load type section success.\n");
  410. return true;
  411. }
  412. static bool
  413. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  414. WASMTableImport *table,
  415. char *error_buf, uint32 error_buf_size)
  416. {
  417. const uint8 *p = *p_buf, *p_end = buf_end;
  418. CHECK_BUF(p, p_end, 1);
  419. /* 0x70 */
  420. table->elem_type = read_uint8(p);
  421. bh_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
  422. read_leb_uint32(p, p_end, table->flags);
  423. read_leb_uint32(p, p_end, table->init_size);
  424. if (table->flags & 1)
  425. read_leb_uint32(p, p_end, table->max_size);
  426. else
  427. table->max_size = 0x10000;
  428. *p_buf = p;
  429. return true;
  430. }
  431. unsigned
  432. wasm_runtime_memory_pool_size();
  433. static bool
  434. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  435. WASMMemoryImport *memory,
  436. char *error_buf, uint32 error_buf_size)
  437. {
  438. const uint8 *p = *p_buf, *p_end = buf_end;
  439. uint32 pool_size = wasm_runtime_memory_pool_size();
  440. #if WASM_ENABLE_APP_FRAMEWORK != 0
  441. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  442. / DEFAULT_NUM_BYTES_PER_PAGE;
  443. #else
  444. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  445. #endif
  446. read_leb_uint32(p, p_end, memory->flags);
  447. read_leb_uint32(p, p_end, memory->init_page_count);
  448. if (memory->flags & 1) {
  449. read_leb_uint32(p, p_end, memory->max_page_count);
  450. if (memory->max_page_count > max_page_count)
  451. memory->max_page_count = max_page_count;
  452. }
  453. else
  454. /* Limit the maximum memory size to max_page_count */
  455. memory->max_page_count = max_page_count;
  456. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  457. *p_buf = p;
  458. return true;
  459. }
  460. static bool
  461. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMTable *table,
  462. char *error_buf, uint32 error_buf_size)
  463. {
  464. const uint8 *p = *p_buf, *p_end = buf_end;
  465. CHECK_BUF(p, p_end, 1);
  466. /* 0x70 */
  467. table->elem_type = read_uint8(p);
  468. bh_assert(table->elem_type == TABLE_ELEM_TYPE_ANY_FUNC);
  469. read_leb_uint32(p, p_end, table->flags);
  470. read_leb_uint32(p, p_end, table->init_size);
  471. if (table->flags & 1)
  472. read_leb_uint32(p, p_end, table->max_size);
  473. else
  474. table->max_size = 0x10000;
  475. *p_buf = p;
  476. return true;
  477. }
  478. static bool
  479. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  480. char *error_buf, uint32 error_buf_size)
  481. {
  482. const uint8 *p = *p_buf, *p_end = buf_end;
  483. uint32 pool_size = wasm_runtime_memory_pool_size();
  484. #if WASM_ENABLE_APP_FRAMEWORK != 0
  485. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  486. / DEFAULT_NUM_BYTES_PER_PAGE;
  487. #else
  488. uint32 max_page_count = pool_size / DEFAULT_NUM_BYTES_PER_PAGE;
  489. #endif
  490. read_leb_uint32(p, p_end, memory->flags);
  491. read_leb_uint32(p, p_end, memory->init_page_count);
  492. if (memory->flags & 1) {
  493. read_leb_uint32(p, p_end, memory->max_page_count);
  494. if (memory->max_page_count > max_page_count)
  495. memory->max_page_count = max_page_count;
  496. }
  497. else
  498. /* Limit the maximum memory size to max_page_count */
  499. memory->max_page_count = max_page_count;
  500. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  501. *p_buf = p;
  502. return true;
  503. }
  504. static bool
  505. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  506. char *error_buf, uint32 error_buf_size)
  507. {
  508. const uint8 *p = buf, *p_end = buf_end, *p_old;
  509. uint32 import_count, name_len, type_index, i, u32, flags;
  510. uint64 total_size;
  511. WASMImport *import;
  512. WASMImport *import_functions = NULL, *import_tables = NULL;
  513. WASMImport *import_memories = NULL, *import_globals = NULL;
  514. char *module_name, *field_name;
  515. uint8 mutable, u8, kind;
  516. read_leb_uint32(p, p_end, import_count);
  517. if (import_count) {
  518. module->import_count = import_count;
  519. total_size = sizeof(WASMImport) * (uint64)import_count;
  520. if (total_size >= UINT32_MAX
  521. || !(module->imports = wasm_runtime_malloc((uint32)total_size))) {
  522. set_error_buf(error_buf, error_buf_size,
  523. "Load import section failed: allocate memory failed.");
  524. return false;
  525. }
  526. memset(module->imports, 0, (uint32)total_size);
  527. p_old = p;
  528. /* Scan firstly to get import count of each type */
  529. for (i = 0; i < import_count; i++) {
  530. /* module name */
  531. read_leb_uint32(p, p_end, name_len);
  532. CHECK_BUF(p, p_end, name_len);
  533. p += name_len;
  534. /* field name */
  535. read_leb_uint32(p, p_end, name_len);
  536. CHECK_BUF(p, p_end, name_len);
  537. p += name_len;
  538. CHECK_BUF(p, p_end, 1);
  539. /* 0x00/0x01/0x02/0x03 */
  540. kind = read_uint8(p);
  541. switch (kind) {
  542. case IMPORT_KIND_FUNC: /* import function */
  543. read_leb_uint32(p, p_end, type_index);
  544. module->import_function_count++;
  545. break;
  546. case IMPORT_KIND_TABLE: /* import table */
  547. CHECK_BUF(p, p_end, 1);
  548. /* 0x70 */
  549. u8 = read_uint8(p);
  550. read_leb_uint32(p, p_end, flags);
  551. read_leb_uint32(p, p_end, u32);
  552. if (flags & 1)
  553. read_leb_uint32(p, p_end, u32);
  554. module->import_table_count++;
  555. if (module->import_table_count > 1) {
  556. set_error_buf(error_buf, error_buf_size,
  557. "Load import section failed: multiple tables");
  558. return false;
  559. }
  560. break;
  561. case IMPORT_KIND_MEMORY: /* import memory */
  562. read_leb_uint32(p, p_end, flags);
  563. read_leb_uint32(p, p_end, u32);
  564. if (flags & 1)
  565. read_leb_uint32(p, p_end, u32);
  566. module->import_memory_count++;
  567. if (module->import_memory_count > 1) {
  568. set_error_buf(error_buf, error_buf_size,
  569. "Load import section failed: multiple memories");
  570. return false;
  571. }
  572. break;
  573. case IMPORT_KIND_GLOBAL: /* import global */
  574. CHECK_BUF(p, p_end, 2);
  575. p += 2;
  576. module->import_global_count++;
  577. break;
  578. default:
  579. set_error_buf(error_buf, error_buf_size,
  580. "Load import section failed: invalid import type.");
  581. return false;
  582. }
  583. }
  584. if (module->import_function_count)
  585. import_functions = module->import_functions = module->imports;
  586. if (module->import_table_count)
  587. import_tables = module->import_tables =
  588. module->imports + module->import_function_count;
  589. if (module->import_memory_count)
  590. import_memories = module->import_memories =
  591. module->imports + module->import_function_count + module->import_table_count;
  592. if (module->import_global_count)
  593. import_globals = module->import_globals =
  594. module->imports + module->import_function_count + module->import_table_count
  595. + module->import_memory_count;
  596. p = p_old;
  597. /* insert "env" and "wasi_unstable" to const str list */
  598. if (!const_str_list_insert((uint8*)"env", 3, module, error_buf, error_buf_size)
  599. || !const_str_list_insert((uint8*)"wasi_unstable", 13, module,
  600. error_buf, error_buf_size)) {
  601. return false;
  602. }
  603. /* Scan again to read the data */
  604. for (i = 0; i < import_count; i++) {
  605. /* load module name */
  606. read_leb_uint32(p, p_end, name_len);
  607. CHECK_BUF(p, p_end, name_len);
  608. if (!(module_name = const_str_list_insert
  609. (p, name_len, module, error_buf, error_buf_size))) {
  610. return false;
  611. }
  612. p += name_len;
  613. /* load field name */
  614. read_leb_uint32(p, p_end, name_len);
  615. CHECK_BUF(p, p_end, name_len);
  616. if (!(field_name = const_str_list_insert
  617. (p, name_len, module, error_buf, error_buf_size))) {
  618. return false;
  619. }
  620. p += name_len;
  621. CHECK_BUF(p, p_end, 1);
  622. /* 0x00/0x01/0x02/0x03 */
  623. kind = read_uint8(p);
  624. switch (kind) {
  625. case IMPORT_KIND_FUNC: /* import function */
  626. bh_assert(import_functions);
  627. import = import_functions++;
  628. read_leb_uint32(p, p_end, type_index);
  629. if (type_index >= module->type_count) {
  630. set_error_buf(error_buf, error_buf_size,
  631. "Load import section failed: "
  632. "function type index out of range.");
  633. return false;
  634. }
  635. import->u.function.func_type = module->types[type_index];
  636. if (!(import->u.function.func_ptr_linked =
  637. wasm_native_resolve_symbol(module_name, field_name,
  638. import->u.function.func_type,
  639. &import->u.function.signature))) {
  640. #if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */
  641. LOG_WARNING("warning: fail to link import function (%s, %s)\n",
  642. module_name, field_name);
  643. #endif
  644. }
  645. break;
  646. case IMPORT_KIND_TABLE: /* import table */
  647. bh_assert(import_tables);
  648. import = import_tables++;
  649. if (!load_table_import(&p, p_end, &import->u.table,
  650. error_buf, error_buf_size))
  651. return false;
  652. if (module->import_table_count > 1) {
  653. set_error_buf(error_buf, error_buf_size, "multiple tables");
  654. return false;
  655. }
  656. break;
  657. case IMPORT_KIND_MEMORY: /* import memory */
  658. bh_assert(import_memories);
  659. import = import_memories++;
  660. if (!load_memory_import(&p, p_end, &import->u.memory,
  661. error_buf, error_buf_size))
  662. return false;
  663. if (module->import_memory_count > 1) {
  664. set_error_buf(error_buf, error_buf_size,
  665. "Load import section failed: multiple memories");
  666. return false;
  667. }
  668. break;
  669. case IMPORT_KIND_GLOBAL: /* import global */
  670. bh_assert(import_globals);
  671. import = import_globals++;
  672. CHECK_BUF(p, p_end, 2);
  673. import->u.global.type = read_uint8(p);
  674. mutable = read_uint8(p);
  675. if (mutable >= 2) {
  676. set_error_buf(error_buf, error_buf_size,
  677. "Load import section failed: "
  678. "invalid mutability");
  679. return false;
  680. }
  681. import->u.global.is_mutable = mutable & 1 ? true : false;
  682. #if WASM_ENABLE_LIBC_BUILTIN != 0
  683. if (!(wasm_native_lookup_libc_builtin_global(
  684. module_name, field_name,
  685. &import->u.global))) {
  686. if (error_buf != NULL)
  687. snprintf(error_buf, error_buf_size,
  688. "Load import section failed: "
  689. "resolve import global (%s, %s) failed.",
  690. module_name, field_name);
  691. return false;
  692. }
  693. #endif
  694. break;
  695. default:
  696. set_error_buf(error_buf, error_buf_size,
  697. "Load import section failed: "
  698. "invalid import type.");
  699. return false;
  700. }
  701. import->kind = kind;
  702. import->u.names.module_name = module_name;
  703. import->u.names.field_name = field_name;
  704. }
  705. #if WASM_ENABLE_LIBC_WASI != 0
  706. import = module->import_functions;
  707. for (i = 0; i < module->import_function_count; i++, import++) {
  708. if (!strcmp(import->u.names.module_name, "wasi_unstable")) {
  709. module->is_wasi_module = true;
  710. break;
  711. }
  712. }
  713. #endif
  714. }
  715. if (p != p_end) {
  716. set_error_buf(error_buf, error_buf_size,
  717. "Load import section failed: section size mismatch");
  718. return false;
  719. }
  720. LOG_VERBOSE("Load import section success.\n");
  721. (void)u8;
  722. (void)u32;
  723. return true;
  724. }
  725. static bool
  726. init_function_local_offsets(WASMFunction *func,
  727. char *error_buf, uint32 error_buf_size)
  728. {
  729. WASMType *param_type = func->func_type;
  730. uint32 param_count = param_type->param_count;
  731. uint8 *param_types = param_type->types;
  732. uint32 local_count = func->local_count;
  733. uint8 *local_types = func->local_types;
  734. uint32 i, local_offset = 0;
  735. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  736. if (total_size >= UINT32_MAX
  737. || !(func->local_offsets = wasm_runtime_malloc((uint32)total_size))) {
  738. set_error_buf(error_buf, error_buf_size,
  739. "Load function section failed: allocate memory failed.");
  740. return false;
  741. }
  742. for (i = 0; i < param_count; i++) {
  743. func->local_offsets[i] = (uint16)local_offset;
  744. local_offset += wasm_value_type_cell_num(param_types[i]);
  745. }
  746. for (i = 0; i < local_count; i++) {
  747. func->local_offsets[param_count + i] = (uint16)local_offset;
  748. local_offset += wasm_value_type_cell_num(local_types[i]);
  749. }
  750. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  751. return true;
  752. }
  753. static bool
  754. load_function_section(const uint8 *buf, const uint8 *buf_end,
  755. const uint8 *buf_code, const uint8 *buf_code_end,
  756. WASMModule *module,
  757. char *error_buf, uint32 error_buf_size)
  758. {
  759. const uint8 *p = buf, *p_end = buf_end;
  760. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  761. uint32 func_count;
  762. uint64 total_size;
  763. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  764. uint32 local_count, local_set_count, sub_local_count;
  765. uint8 type;
  766. WASMFunction *func;
  767. read_leb_uint32(p, p_end, func_count);
  768. if (buf_code)
  769. read_leb_uint32(p_code, buf_code_end, code_count);
  770. if (func_count != code_count) {
  771. set_error_buf(error_buf, error_buf_size,
  772. "Load function section failed: "
  773. "function and code section have inconsistent lengths");
  774. return false;
  775. }
  776. if (func_count) {
  777. module->function_count = func_count;
  778. total_size = sizeof(WASMFunction*) * (uint64)func_count;
  779. if (total_size >= UINT32_MAX
  780. || !(module->functions = wasm_runtime_malloc((uint32)total_size))) {
  781. set_error_buf(error_buf, error_buf_size,
  782. "Load function section failed: allocate memory failed.");
  783. return false;
  784. }
  785. memset(module->functions, 0, (uint32)total_size);
  786. for (i = 0; i < func_count; i++) {
  787. /* Resolve function type */
  788. read_leb_uint32(p, p_end, type_index);
  789. if (type_index >= module->type_count) {
  790. set_error_buf(error_buf, error_buf_size,
  791. "Load function section failed: "
  792. "function type index out of range.");
  793. return false;
  794. }
  795. read_leb_uint32(p_code, buf_code_end, code_size);
  796. if (code_size == 0
  797. || p_code + code_size > buf_code_end) {
  798. set_error_buf(error_buf, error_buf_size,
  799. "Load function section failed: "
  800. "invalid function code size.");
  801. return false;
  802. }
  803. /* Resolve local set count */
  804. p_code_end = p_code + code_size;
  805. local_count = 0;
  806. read_leb_uint32(p_code, buf_code_end, local_set_count);
  807. p_code_save = p_code;
  808. /* Calculate total local count */
  809. for (j = 0; j < local_set_count; j++) {
  810. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  811. if (sub_local_count > UINT32_MAX - local_count) {
  812. set_error_buf(error_buf, error_buf_size,
  813. "Load function section failed: "
  814. "too many locals");
  815. return false;
  816. }
  817. CHECK_BUF(p_code, buf_code_end, 1);
  818. /* 0x7F/0x7E/0x7D/0x7C */
  819. type = read_uint8(p_code);
  820. local_count += sub_local_count;
  821. }
  822. /* Alloc memory, layout: function structure + local types */
  823. code_size = (uint32)(p_code_end - p_code);
  824. total_size = sizeof(WASMFunction) + (uint64)local_count;
  825. if (total_size >= UINT32_MAX
  826. || !(func = module->functions[i] =
  827. wasm_runtime_malloc((uint32)total_size))) {
  828. set_error_buf(error_buf, error_buf_size,
  829. "Load function section failed: "
  830. "allocate memory failed.");
  831. return false;
  832. }
  833. /* Set function type, local count, code size and code body */
  834. memset(func, 0, (uint32)total_size);
  835. func->func_type = module->types[type_index];
  836. func->local_count = local_count;
  837. if (local_count > 0)
  838. func->local_types = (uint8*)func + sizeof(WASMFunction);
  839. func->code_size = code_size;
  840. func->code = (uint8*)p_code;
  841. /* Load each local type */
  842. p_code = p_code_save;
  843. local_type_index = 0;
  844. for (j = 0; j < local_set_count; j++) {
  845. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  846. if (local_type_index + sub_local_count <= local_type_index
  847. || local_type_index + sub_local_count > local_count) {
  848. set_error_buf(error_buf, error_buf_size,
  849. "Load function section failed: "
  850. "invalid local count.");
  851. return false;
  852. }
  853. CHECK_BUF(p_code, buf_code_end, 1);
  854. /* 0x7F/0x7E/0x7D/0x7C */
  855. type = read_uint8(p_code);
  856. if (type < VALUE_TYPE_F64 || type > VALUE_TYPE_I32) {
  857. set_error_buf(error_buf, error_buf_size,
  858. "Load function section failed: "
  859. "invalid local type.");
  860. return false;
  861. }
  862. for (k = 0; k < sub_local_count; k++) {
  863. func->local_types[local_type_index++] = type;
  864. }
  865. }
  866. func->param_cell_num = wasm_type_param_cell_num(func->func_type);
  867. func->ret_cell_num = wasm_type_return_cell_num(func->func_type);
  868. func->local_cell_num =
  869. wasm_get_cell_num(func->local_types, func->local_count);
  870. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  871. return false;
  872. p_code = p_code_end;
  873. }
  874. }
  875. if (p != p_end) {
  876. set_error_buf(error_buf, error_buf_size,
  877. "Load function section failed: section size mismatch");
  878. return false;
  879. }
  880. LOG_VERBOSE("Load function section success.\n");
  881. return true;
  882. }
  883. static bool
  884. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  885. char *error_buf, uint32 error_buf_size)
  886. {
  887. const uint8 *p = buf, *p_end = buf_end;
  888. uint32 table_count, i;
  889. uint64 total_size;
  890. WASMTable *table;
  891. read_leb_uint32(p, p_end, table_count);
  892. bh_assert(table_count == 1);
  893. if (table_count) {
  894. if (table_count > 1) {
  895. set_error_buf(error_buf, error_buf_size,
  896. "Load table section failed: multiple memories");
  897. return false;
  898. }
  899. module->table_count = table_count;
  900. total_size = sizeof(WASMTable) * (uint64)table_count;
  901. if (total_size >= UINT32_MAX
  902. || !(module->tables = wasm_runtime_malloc((uint32)total_size))) {
  903. set_error_buf(error_buf, error_buf_size,
  904. "Load table section failed: allocate memory failed.");
  905. return false;
  906. }
  907. memset(module->tables, 0, (uint32)total_size);
  908. /* load each table */
  909. table = module->tables;
  910. for (i = 0; i < table_count; i++, table++)
  911. if (!load_table(&p, p_end, table, error_buf, error_buf_size))
  912. return false;
  913. }
  914. if (p != p_end) {
  915. set_error_buf(error_buf, error_buf_size,
  916. "Load table section failed: section size mismatch");
  917. return false;
  918. }
  919. LOG_VERBOSE("Load table section success.\n");
  920. return true;
  921. }
  922. static bool
  923. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  924. char *error_buf, uint32 error_buf_size)
  925. {
  926. const uint8 *p = buf, *p_end = buf_end;
  927. uint32 memory_count, i;
  928. uint64 total_size;
  929. WASMMemory *memory;
  930. read_leb_uint32(p, p_end, memory_count);
  931. bh_assert(memory_count == 1);
  932. if (memory_count) {
  933. if (memory_count > 1) {
  934. set_error_buf(error_buf, error_buf_size,
  935. "Load memory section failed: multiple memories");
  936. return false;
  937. }
  938. module->memory_count = memory_count;
  939. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  940. if (total_size >= UINT32_MAX
  941. || !(module->memories = wasm_runtime_malloc((uint32)total_size))) {
  942. set_error_buf(error_buf, error_buf_size,
  943. "Load memory section failed: allocate memory failed.");
  944. return false;
  945. }
  946. memset(module->memories, 0, (uint32)total_size);
  947. /* load each memory */
  948. memory = module->memories;
  949. for (i = 0; i < memory_count; i++, memory++)
  950. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  951. return false;
  952. }
  953. if (p != p_end) {
  954. set_error_buf(error_buf, error_buf_size,
  955. "Load memory section failed: section size mismatch");
  956. return false;
  957. }
  958. LOG_VERBOSE("Load memory section success.\n");
  959. return true;
  960. }
  961. static bool
  962. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  963. char *error_buf, uint32 error_buf_size)
  964. {
  965. const uint8 *p = buf, *p_end = buf_end;
  966. uint32 global_count, i;
  967. uint64 total_size;
  968. WASMGlobal *global;
  969. uint8 mutable;
  970. read_leb_uint32(p, p_end, global_count);
  971. if (global_count) {
  972. module->global_count = global_count;
  973. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  974. if (total_size >= UINT32_MAX
  975. || !(module->globals = wasm_runtime_malloc((uint32)total_size))) {
  976. set_error_buf(error_buf, error_buf_size,
  977. "Load global section failed: "
  978. "allocate memory failed.");
  979. return false;
  980. }
  981. memset(module->globals, 0, (uint32)total_size);
  982. global = module->globals;
  983. for(i = 0; i < global_count; i++, global++) {
  984. CHECK_BUF(p, p_end, 2);
  985. global->type = read_uint8(p);
  986. mutable = read_uint8(p);
  987. if (mutable >= 2) {
  988. set_error_buf(error_buf, error_buf_size,
  989. "Load import section failed: "
  990. "invalid mutability");
  991. return false;
  992. }
  993. global->is_mutable = mutable ? true : false;
  994. /* initialize expression */
  995. if (!load_init_expr(&p, p_end, &(global->init_expr), error_buf, error_buf_size))
  996. return false;
  997. }
  998. }
  999. if (p != p_end) {
  1000. set_error_buf(error_buf, error_buf_size,
  1001. "Load global section failed: section size mismatch");
  1002. return false;
  1003. }
  1004. LOG_VERBOSE("Load global section success.\n");
  1005. return true;
  1006. }
  1007. static bool
  1008. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1009. char *error_buf, uint32 error_buf_size)
  1010. {
  1011. const uint8 *p = buf, *p_end = buf_end;
  1012. uint32 export_count, i, index;
  1013. uint64 total_size;
  1014. uint32 str_len;
  1015. WASMExport *export;
  1016. read_leb_uint32(p, p_end, export_count);
  1017. if (export_count) {
  1018. module->export_count = export_count;
  1019. total_size = sizeof(WASMExport) * (uint64)export_count;
  1020. if (total_size >= UINT32_MAX
  1021. || !(module->exports = wasm_runtime_malloc((uint32)total_size))) {
  1022. set_error_buf(error_buf, error_buf_size,
  1023. "Load export section failed: "
  1024. "allocate memory failed.");
  1025. return false;
  1026. }
  1027. memset(module->exports, 0, (uint32)total_size);
  1028. export = module->exports;
  1029. for (i = 0; i < export_count; i++, export++) {
  1030. read_leb_uint32(p, p_end, str_len);
  1031. CHECK_BUF(p, p_end, str_len);
  1032. if (!(export->name = const_str_list_insert(p, str_len, module,
  1033. error_buf, error_buf_size))) {
  1034. return false;
  1035. }
  1036. p += str_len;
  1037. CHECK_BUF(p, p_end, 1);
  1038. export->kind = read_uint8(p);
  1039. read_leb_uint32(p, p_end, index);
  1040. export->index = index;
  1041. switch(export->kind) {
  1042. /*function index*/
  1043. case EXPORT_KIND_FUNC:
  1044. if (index >= module->function_count + module->import_function_count) {
  1045. set_error_buf(error_buf, error_buf_size,
  1046. "Load export section failed: "
  1047. "function index out of range.");
  1048. return false;
  1049. }
  1050. break;
  1051. /*table index*/
  1052. case EXPORT_KIND_TABLE:
  1053. if (index >= module->table_count + module->import_table_count) {
  1054. set_error_buf(error_buf, error_buf_size,
  1055. "Load export section failed: "
  1056. "table index out of range.");
  1057. return false;
  1058. }
  1059. break;
  1060. /*memory index*/
  1061. case EXPORT_KIND_MEMORY:
  1062. if (index >= module->memory_count + module->import_memory_count) {
  1063. set_error_buf(error_buf, error_buf_size,
  1064. "Load export section failed: "
  1065. "memory index out of range.");
  1066. return false;
  1067. }
  1068. break;
  1069. /*global index*/
  1070. case EXPORT_KIND_GLOBAL:
  1071. if (index >= module->global_count + module->import_global_count) {
  1072. set_error_buf(error_buf, error_buf_size,
  1073. "Load export section failed: "
  1074. "global index out of range.");
  1075. return false;
  1076. }
  1077. break;
  1078. default:
  1079. set_error_buf(error_buf, error_buf_size,
  1080. "Load export section failed: "
  1081. "invalid export kind.");
  1082. return false;
  1083. }
  1084. }
  1085. }
  1086. if (p != p_end) {
  1087. set_error_buf(error_buf, error_buf_size,
  1088. "Load export section failed: section size mismatch");
  1089. return false;
  1090. }
  1091. LOG_VERBOSE("Load export section success.\n");
  1092. return true;
  1093. }
  1094. static bool
  1095. load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1096. char *error_buf, uint32 error_buf_size)
  1097. {
  1098. const uint8 *p = buf, *p_end = buf_end;
  1099. uint32 table_segment_count, i, j, table_index, function_count, function_index;
  1100. uint64 total_size;
  1101. WASMTableSeg *table_segment;
  1102. read_leb_uint32(p, p_end, table_segment_count);
  1103. if (table_segment_count) {
  1104. module->table_seg_count = table_segment_count;
  1105. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  1106. if (total_size >= UINT32_MAX
  1107. || !(module->table_segments = wasm_runtime_malloc((uint32)total_size))) {
  1108. set_error_buf(error_buf, error_buf_size,
  1109. "Load table segment section failed: "
  1110. "allocate memory failed.");
  1111. return false;
  1112. }
  1113. memset(module->table_segments, 0, (uint32)total_size);
  1114. table_segment = module->table_segments;
  1115. for (i = 0; i < table_segment_count; i++, table_segment++) {
  1116. if (p >= p_end) {
  1117. set_error_buf(error_buf, error_buf_size,
  1118. "Load table segment section failed: "
  1119. "invalid value type");
  1120. return false;
  1121. }
  1122. read_leb_uint32(p, p_end, table_index);
  1123. table_segment->table_index = table_index;
  1124. /* initialize expression */
  1125. if (!load_init_expr(&p, p_end, &(table_segment->base_offset),
  1126. error_buf, error_buf_size))
  1127. return false;
  1128. read_leb_uint32(p, p_end, function_count);
  1129. table_segment->function_count = function_count;
  1130. total_size = sizeof(uint32) * (uint64)function_count;
  1131. if (total_size >= UINT32_MAX
  1132. || !(table_segment->func_indexes = (uint32 *)
  1133. wasm_runtime_malloc((uint32)total_size))) {
  1134. set_error_buf(error_buf, error_buf_size,
  1135. "Load table segment section failed: "
  1136. "allocate memory failed.");
  1137. return false;
  1138. }
  1139. for (j = 0; j < function_count; j++) {
  1140. read_leb_uint32(p, p_end, function_index);
  1141. table_segment->func_indexes[j] = function_index;
  1142. }
  1143. }
  1144. }
  1145. if (p != p_end) {
  1146. set_error_buf(error_buf, error_buf_size,
  1147. "Load table segment section failed: section size mismatch");
  1148. return false;
  1149. }
  1150. LOG_VERBOSE("Load table segment section success.\n");
  1151. return true;
  1152. }
  1153. static bool
  1154. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  1155. WASMModule *module,
  1156. char *error_buf, uint32 error_buf_size)
  1157. {
  1158. const uint8 *p = buf, *p_end = buf_end;
  1159. uint32 data_seg_count, i, mem_index, data_seg_len;
  1160. uint64 total_size;
  1161. WASMDataSeg *dataseg;
  1162. InitializerExpression init_expr;
  1163. read_leb_uint32(p, p_end, data_seg_count);
  1164. if (data_seg_count) {
  1165. module->data_seg_count = data_seg_count;
  1166. total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
  1167. if (total_size >= UINT32_MAX
  1168. || !(module->data_segments = wasm_runtime_malloc((uint32)total_size))) {
  1169. set_error_buf(error_buf, error_buf_size,
  1170. "Load data segment section failed: "
  1171. "allocate memory failed.");
  1172. return false;
  1173. }
  1174. memset(module->data_segments, 0, (uint32)total_size);
  1175. for (i = 0; i < data_seg_count; i++) {
  1176. read_leb_uint32(p, p_end, mem_index);
  1177. if (!load_init_expr(&p, p_end, &init_expr, error_buf, error_buf_size))
  1178. return false;
  1179. read_leb_uint32(p, p_end, data_seg_len);
  1180. if (!(dataseg = module->data_segments[i] =
  1181. wasm_runtime_malloc((uint32)sizeof(WASMDataSeg)))) {
  1182. set_error_buf(error_buf, error_buf_size,
  1183. "Load data segment section failed: "
  1184. "allocate memory failed.");
  1185. return false;
  1186. }
  1187. bh_memcpy_s(&dataseg->base_offset, sizeof(InitializerExpression),
  1188. &init_expr, sizeof(InitializerExpression));
  1189. dataseg->memory_index = mem_index;
  1190. dataseg->data_length = data_seg_len;
  1191. CHECK_BUF(p, p_end, data_seg_len);
  1192. dataseg->data = (uint8*)p;
  1193. p += data_seg_len;
  1194. }
  1195. }
  1196. if (p != p_end) {
  1197. set_error_buf(error_buf, error_buf_size,
  1198. "Load data segment section failed: section size mismatch");
  1199. return false;
  1200. }
  1201. LOG_VERBOSE("Load data segment section success.\n");
  1202. return true;
  1203. }
  1204. static bool
  1205. load_code_section(const uint8 *buf, const uint8 *buf_end,
  1206. const uint8 *buf_func,
  1207. const uint8 *buf_func_end,
  1208. WASMModule *module,
  1209. char *error_buf, uint32 error_buf_size)
  1210. {
  1211. const uint8 *p = buf, *p_end = buf_end;
  1212. const uint8 *p_func = buf_func;
  1213. uint32 func_count = 0, code_count;
  1214. /* code has been loaded in function section, so pass it here, just check
  1215. * whether function and code section have inconsistent lengths */
  1216. read_leb_uint32(p, p_end, code_count);
  1217. if (buf_func)
  1218. read_leb_uint32(p_func, buf_func_end, func_count);
  1219. if (func_count != code_count) {
  1220. set_error_buf(error_buf, error_buf_size,
  1221. "Load code section failed: "
  1222. "function and code section have inconsistent lengths");
  1223. return false;
  1224. }
  1225. LOG_VERBOSE("Load code segment section success.\n");
  1226. return true;
  1227. }
  1228. static bool
  1229. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1230. char *error_buf, uint32 error_buf_size)
  1231. {
  1232. const uint8 *p = buf, *p_end = buf_end;
  1233. uint32 start_function;
  1234. read_leb_uint32(p, p_end, start_function);
  1235. if (start_function) {
  1236. if (start_function >= module->function_count + module->import_function_count) {
  1237. set_error_buf(error_buf, error_buf_size,
  1238. "Load start section failed: "
  1239. "function index out of range.");
  1240. return false;
  1241. }
  1242. module->start_function = start_function;
  1243. }
  1244. if (p != p_end) {
  1245. set_error_buf(error_buf, error_buf_size,
  1246. "Load start section failed: section size mismatch");
  1247. return false;
  1248. }
  1249. LOG_VERBOSE("Load start section success.\n");
  1250. return true;
  1251. }
  1252. static bool
  1253. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1254. char *error_buf, uint32 error_buf_size)
  1255. {
  1256. const uint8 *p = buf, *p_end = buf_end;
  1257. uint32 name_len;
  1258. if (p >= p_end) {
  1259. set_error_buf(error_buf, error_buf_size,
  1260. "Load custom section failed: unexpected end");
  1261. return false;
  1262. }
  1263. read_leb_uint32(p, p_end, name_len);
  1264. if (name_len == 0
  1265. || p + name_len > p_end) {
  1266. set_error_buf(error_buf, error_buf_size,
  1267. "Load custom section failed: unexpected end");
  1268. return false;
  1269. }
  1270. if (!check_utf8_str(p, name_len)) {
  1271. set_error_buf(error_buf, error_buf_size,
  1272. "WASM module load failed: "
  1273. "invalid UTF-8 encoding");
  1274. return false;
  1275. }
  1276. LOG_VERBOSE("Load custom section success.\n");
  1277. return true;
  1278. }
  1279. static bool
  1280. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  1281. BlockAddr *block_addr_cache,
  1282. char *error_buf, uint32 error_buf_size);
  1283. #if WASM_ENABLE_FAST_INTERP != 0
  1284. void **
  1285. wasm_interp_get_handle_table();
  1286. static void **handle_table;
  1287. #endif
  1288. static bool
  1289. load_from_sections(WASMModule *module, WASMSection *sections,
  1290. char *error_buf, uint32 error_buf_size)
  1291. {
  1292. WASMExport *export;
  1293. WASMSection *section = sections;
  1294. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  1295. *buf_func = NULL, *buf_func_end = NULL;
  1296. WASMGlobal *llvm_data_end_global = NULL, *llvm_heap_base_global = NULL;
  1297. WASMGlobal *llvm_stack_top_global = NULL, *global;
  1298. uint32 llvm_data_end = UINT32_MAX, llvm_heap_base = UINT32_MAX;
  1299. uint32 llvm_stack_top = UINT32_MAX, global_index, i;
  1300. uint32 data_end_global_index = UINT32_MAX;
  1301. uint32 heap_base_global_index = UINT32_MAX;
  1302. uint32 stack_top_global_index = UINT32_MAX;
  1303. BlockAddr *block_addr_cache;
  1304. uint64 total_size;
  1305. /* Find code and function sections if have */
  1306. while (section) {
  1307. if (section->section_type == SECTION_TYPE_CODE) {
  1308. buf_code = section->section_body;
  1309. buf_code_end = buf_code + section->section_body_size;
  1310. }
  1311. else if (section->section_type == SECTION_TYPE_FUNC) {
  1312. buf_func = section->section_body;
  1313. buf_func_end = buf_func + section->section_body_size;
  1314. }
  1315. section = section->next;
  1316. }
  1317. section = sections;
  1318. while (section) {
  1319. buf = section->section_body;
  1320. buf_end = buf + section->section_body_size;
  1321. switch (section->section_type) {
  1322. case SECTION_TYPE_USER:
  1323. /* unsupported user section, ignore it. */
  1324. if (!load_user_section(buf, buf_end, module, error_buf, error_buf_size))
  1325. return false;
  1326. break;
  1327. case SECTION_TYPE_TYPE:
  1328. if (!load_type_section(buf, buf_end, module, error_buf, error_buf_size))
  1329. return false;
  1330. break;
  1331. case SECTION_TYPE_IMPORT:
  1332. if (!load_import_section(buf, buf_end, module, error_buf, error_buf_size))
  1333. return false;
  1334. break;
  1335. case SECTION_TYPE_FUNC:
  1336. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  1337. module, error_buf, error_buf_size))
  1338. return false;
  1339. break;
  1340. case SECTION_TYPE_TABLE:
  1341. if (!load_table_section(buf, buf_end, module, error_buf, error_buf_size))
  1342. return false;
  1343. break;
  1344. case SECTION_TYPE_MEMORY:
  1345. if (!load_memory_section(buf, buf_end, module, error_buf, error_buf_size))
  1346. return false;
  1347. break;
  1348. case SECTION_TYPE_GLOBAL:
  1349. if (!load_global_section(buf, buf_end, module, error_buf, error_buf_size))
  1350. return false;
  1351. break;
  1352. case SECTION_TYPE_EXPORT:
  1353. if (!load_export_section(buf, buf_end, module, error_buf, error_buf_size))
  1354. return false;
  1355. break;
  1356. case SECTION_TYPE_START:
  1357. if (!load_start_section(buf, buf_end, module, error_buf, error_buf_size))
  1358. return false;
  1359. break;
  1360. case SECTION_TYPE_ELEM:
  1361. if (!load_table_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1362. return false;
  1363. break;
  1364. case SECTION_TYPE_CODE:
  1365. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  1366. module, error_buf, error_buf_size))
  1367. return false;
  1368. break;
  1369. case SECTION_TYPE_DATA:
  1370. if (!load_data_segment_section(buf, buf_end, module, error_buf, error_buf_size))
  1371. return false;
  1372. break;
  1373. default:
  1374. set_error_buf(error_buf, error_buf_size,
  1375. "WASM module load failed: invalid section id");
  1376. return false;
  1377. }
  1378. section = section->next;
  1379. }
  1380. #if WASM_ENABLE_FAST_INTERP != 0
  1381. handle_table = wasm_interp_get_handle_table();
  1382. #endif
  1383. total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE;
  1384. if (total_size >= UINT32_MAX
  1385. || !(block_addr_cache = wasm_runtime_malloc((uint32)total_size))) {
  1386. set_error_buf(error_buf, error_buf_size,
  1387. "WASM module load failed: allocate memory failed");
  1388. return false;
  1389. }
  1390. for (i = 0; i < module->function_count; i++) {
  1391. WASMFunction *func = module->functions[i];
  1392. memset(block_addr_cache, 0, (uint32)total_size);
  1393. if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache, error_buf, error_buf_size))
  1394. return false;
  1395. }
  1396. wasm_runtime_free(block_addr_cache);
  1397. /* Resolve llvm auxiliary data/stack/heap info and reset memory info */
  1398. if (!module->possible_memory_grow) {
  1399. export = module->exports;
  1400. for (i = 0; i < module->export_count; i++, export++) {
  1401. if (export->kind == EXPORT_KIND_GLOBAL) {
  1402. if (!strcmp(export->name, "__heap_base")) {
  1403. global_index = export->index - module->import_global_count;
  1404. global = module->globals + global_index;
  1405. if (global->type == VALUE_TYPE_I32
  1406. && !global->is_mutable
  1407. && global->init_expr.init_expr_type ==
  1408. INIT_EXPR_TYPE_I32_CONST) {
  1409. heap_base_global_index = global_index;
  1410. llvm_heap_base_global = global;
  1411. llvm_heap_base = global->init_expr.u.i32;
  1412. LOG_VERBOSE("found llvm __heap_base global, value: %d\n",
  1413. llvm_heap_base);
  1414. }
  1415. }
  1416. else if (!strcmp(export->name, "__data_end")) {
  1417. global_index = export->index - module->import_global_count;
  1418. global = module->globals + global_index;
  1419. if (global->type == VALUE_TYPE_I32
  1420. && !global->is_mutable
  1421. && global->init_expr.init_expr_type ==
  1422. INIT_EXPR_TYPE_I32_CONST) {
  1423. data_end_global_index = global_index;
  1424. llvm_data_end_global = global;
  1425. llvm_data_end = global->init_expr.u.i32;
  1426. LOG_VERBOSE("found llvm __data_end global, value: %d\n",
  1427. llvm_data_end);
  1428. llvm_data_end = align_uint(llvm_data_end, 16);
  1429. }
  1430. }
  1431. if (llvm_data_end_global && llvm_heap_base_global) {
  1432. if ((data_end_global_index == heap_base_global_index + 1
  1433. && (int32)data_end_global_index > 1)
  1434. || (heap_base_global_index == data_end_global_index + 1
  1435. && (int32)heap_base_global_index > 1)) {
  1436. global_index =
  1437. data_end_global_index < heap_base_global_index
  1438. ? data_end_global_index - 1 : heap_base_global_index - 1;
  1439. global = module->globals + global_index;
  1440. if (global->type == VALUE_TYPE_I32
  1441. && global->is_mutable
  1442. && global->init_expr.init_expr_type ==
  1443. INIT_EXPR_TYPE_I32_CONST) {
  1444. llvm_stack_top_global = global;
  1445. llvm_stack_top = global->init_expr.u.i32;
  1446. stack_top_global_index = global_index;
  1447. LOG_VERBOSE("found llvm stack top global, "
  1448. "value: %d, global index: %d\n",
  1449. llvm_stack_top, global_index);
  1450. }
  1451. }
  1452. break;
  1453. }
  1454. }
  1455. }
  1456. if (llvm_data_end_global
  1457. && llvm_heap_base_global
  1458. && llvm_stack_top_global
  1459. && llvm_stack_top <= llvm_heap_base) {
  1460. WASMMemoryImport *memory_import;
  1461. WASMMemory *memory;
  1462. uint64 init_memory_size;
  1463. uint32 shrunk_memory_size = llvm_heap_base > llvm_data_end
  1464. ? llvm_heap_base : llvm_data_end;
  1465. if (module->import_memory_count) {
  1466. memory_import = &module->import_memories[0].u.memory;
  1467. init_memory_size = (uint64)memory_import->num_bytes_per_page *
  1468. memory_import->init_page_count;
  1469. if (llvm_heap_base <= init_memory_size
  1470. && llvm_data_end <= init_memory_size) {
  1471. /* Reset memory info to decrease memory usage */
  1472. memory_import->num_bytes_per_page = shrunk_memory_size;
  1473. memory_import->init_page_count = 1;
  1474. LOG_VERBOSE("reset import memory size to %d\n",
  1475. shrunk_memory_size);
  1476. }
  1477. }
  1478. if (module->memory_count) {
  1479. memory = &module->memories[0];
  1480. init_memory_size = (uint64)memory->num_bytes_per_page *
  1481. memory->init_page_count;
  1482. if (llvm_heap_base <= init_memory_size
  1483. && llvm_data_end <= init_memory_size) {
  1484. /* Reset memory info to decrease memory usage */
  1485. memory->num_bytes_per_page = shrunk_memory_size;
  1486. memory->init_page_count = 1;
  1487. LOG_VERBOSE("reset memory size to %d\n", shrunk_memory_size);
  1488. }
  1489. }
  1490. module->llvm_aux_data_end = llvm_data_end;
  1491. module->llvm_aux_stack_bottom = llvm_stack_top;
  1492. module->llvm_aux_stack_size = llvm_stack_top > llvm_data_end
  1493. ? llvm_stack_top - llvm_data_end
  1494. : llvm_stack_top;
  1495. module->llvm_aux_stack_global_index = stack_top_global_index;
  1496. LOG_VERBOSE("aux stack bottom: %d, size: %d\n",
  1497. module->llvm_aux_stack_bottom,
  1498. module->llvm_aux_stack_size);
  1499. }
  1500. }
  1501. return true;
  1502. }
  1503. #if BH_ENABLE_MEMORY_PROFILING != 0
  1504. static void wasm_loader_free(void *ptr)
  1505. {
  1506. wasm_runtime_free(ptr);
  1507. }
  1508. #else
  1509. #define wasm_loader_free wasm_free
  1510. #endif
  1511. static WASMModule*
  1512. create_module(char *error_buf, uint32 error_buf_size)
  1513. {
  1514. WASMModule *module = wasm_runtime_malloc(sizeof(WASMModule));
  1515. if (!module) {
  1516. set_error_buf(error_buf, error_buf_size,
  1517. "WASM module load failed: "
  1518. "allocate memory failed.");
  1519. return NULL;
  1520. }
  1521. memset(module, 0, sizeof(WASMModule));
  1522. module->module_type = Wasm_Module_Bytecode;
  1523. /* Set start_function to -1, means no start function */
  1524. module->start_function = (uint32)-1;
  1525. return module;
  1526. }
  1527. WASMModule *
  1528. wasm_loader_load_from_sections(WASMSection *section_list,
  1529. char *error_buf, uint32 error_buf_size)
  1530. {
  1531. WASMModule *module = create_module(error_buf, error_buf_size);
  1532. if (!module)
  1533. return NULL;
  1534. if (!load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1535. wasm_loader_unload(module);
  1536. return NULL;
  1537. }
  1538. LOG_VERBOSE("Load module from sections success.\n");
  1539. return module;
  1540. }
  1541. static void
  1542. destroy_sections(WASMSection *section_list)
  1543. {
  1544. WASMSection *section = section_list, *next;
  1545. while (section) {
  1546. next = section->next;
  1547. wasm_runtime_free(section);
  1548. section = next;
  1549. }
  1550. }
  1551. static bool
  1552. create_sections(const uint8 *buf, uint32 size,
  1553. WASMSection **p_section_list,
  1554. char *error_buf, uint32 error_buf_size)
  1555. {
  1556. WASMSection *section_list_end = NULL, *section;
  1557. const uint8 *p = buf, *p_end = buf + size/*, *section_body*/;
  1558. uint8 section_type, last_section_type = (uint8)-1;
  1559. uint32 section_size;
  1560. bh_assert(!*p_section_list);
  1561. p += 8;
  1562. while (p < p_end) {
  1563. CHECK_BUF(p, p_end, 1);
  1564. section_type = read_uint8(p);
  1565. if (section_type <= SECTION_TYPE_DATA) {
  1566. if (section_type != SECTION_TYPE_USER) {
  1567. /* Custom sections may be inserted at any place,
  1568. while other sections must occur at most once
  1569. and in prescribed order. */
  1570. if (last_section_type != (uint8)-1
  1571. && section_type <= last_section_type) {
  1572. set_error_buf(error_buf, error_buf_size,
  1573. "WASM module load failed: "
  1574. "junk after last section");
  1575. return false;
  1576. }
  1577. last_section_type = section_type;
  1578. }
  1579. CHECK_BUF1(p, p_end, 1);
  1580. read_leb_uint32(p, p_end, section_size);
  1581. CHECK_BUF1(p, p_end, section_size);
  1582. if (!(section = wasm_runtime_malloc(sizeof(WASMSection)))) {
  1583. set_error_buf(error_buf, error_buf_size,
  1584. "WASM module load failed: "
  1585. "allocate memory failed.");
  1586. return false;
  1587. }
  1588. memset(section, 0, sizeof(WASMSection));
  1589. section->section_type = section_type;
  1590. section->section_body = (uint8*)p;
  1591. section->section_body_size = section_size;
  1592. if (!*p_section_list)
  1593. *p_section_list = section_list_end = section;
  1594. else {
  1595. section_list_end->next = section;
  1596. section_list_end = section;
  1597. }
  1598. p += section_size;
  1599. }
  1600. else {
  1601. set_error_buf(error_buf, error_buf_size,
  1602. "WASM module load failed: invalid section id");
  1603. return false;
  1604. }
  1605. }
  1606. return true;
  1607. }
  1608. static void
  1609. exchange32(uint8* p_data)
  1610. {
  1611. uint8 value = *p_data;
  1612. *p_data = *(p_data + 3);
  1613. *(p_data + 3) = value;
  1614. value = *(p_data + 1);
  1615. *(p_data + 1) = *(p_data + 2);
  1616. *(p_data + 2) = value;
  1617. }
  1618. static union {
  1619. int a;
  1620. char b;
  1621. } __ue = { .a = 1 };
  1622. #define is_little_endian() (__ue.b == 1)
  1623. static bool
  1624. load(const uint8 *buf, uint32 size, WASMModule *module,
  1625. char *error_buf, uint32 error_buf_size)
  1626. {
  1627. const uint8 *buf_end = buf + size;
  1628. const uint8 *p = buf, *p_end = buf_end;
  1629. uint32 magic_number, version;
  1630. WASMSection *section_list = NULL;
  1631. CHECK_BUF1(p, p_end, sizeof(uint32));
  1632. magic_number = read_uint32(p);
  1633. if (!is_little_endian())
  1634. exchange32((uint8*)&magic_number);
  1635. if (magic_number != WASM_MAGIC_NUMBER) {
  1636. set_error_buf(error_buf, error_buf_size,
  1637. "WASM module load failed: magic header not detected");
  1638. return false;
  1639. }
  1640. CHECK_BUF1(p, p_end, sizeof(uint32));
  1641. version = read_uint32(p);
  1642. if (!is_little_endian())
  1643. exchange32((uint8*)&version);
  1644. if (version != WASM_CURRENT_VERSION) {
  1645. set_error_buf(error_buf, error_buf_size,
  1646. "WASM module load failed: unknown binary version");
  1647. return false;
  1648. }
  1649. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  1650. || !load_from_sections(module, section_list, error_buf, error_buf_size)) {
  1651. destroy_sections(section_list);
  1652. return false;
  1653. }
  1654. destroy_sections(section_list);
  1655. return true;
  1656. }
  1657. WASMModule*
  1658. wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
  1659. {
  1660. WASMModule *module = wasm_runtime_malloc(sizeof(WASMModule));
  1661. if (!module) {
  1662. set_error_buf(error_buf, error_buf_size,
  1663. "WASM module load failed: allocate memory failed.");
  1664. return NULL;
  1665. }
  1666. memset(module, 0, sizeof(WASMModule));
  1667. module->module_type = Wasm_Module_Bytecode;
  1668. /* Set start_function to -1, means no start function */
  1669. module->start_function = (uint32)-1;
  1670. if (!load(buf, size, module, error_buf, error_buf_size))
  1671. goto fail;
  1672. LOG_VERBOSE("Load module success.\n");
  1673. return module;
  1674. fail:
  1675. wasm_loader_unload(module);
  1676. return NULL;
  1677. }
  1678. void
  1679. wasm_loader_unload(WASMModule *module)
  1680. {
  1681. uint32 i;
  1682. if (!module)
  1683. return;
  1684. if (module->types) {
  1685. for (i = 0; i < module->type_count; i++) {
  1686. if (module->types[i])
  1687. wasm_runtime_free(module->types[i]);
  1688. }
  1689. wasm_runtime_free(module->types);
  1690. }
  1691. if (module->imports)
  1692. wasm_runtime_free(module->imports);
  1693. if (module->functions) {
  1694. for (i = 0; i < module->function_count; i++) {
  1695. if (module->functions[i]) {
  1696. if (module->functions[i]->local_offsets)
  1697. wasm_runtime_free(module->functions[i]->local_offsets);
  1698. #if WASM_ENABLE_FAST_INTERP != 0
  1699. if (module->functions[i]->code_compiled)
  1700. wasm_runtime_free(module->functions[i]->code_compiled);
  1701. if (module->functions[i]->consts)
  1702. wasm_runtime_free(module->functions[i]->consts);
  1703. #endif
  1704. wasm_runtime_free(module->functions[i]);
  1705. }
  1706. }
  1707. wasm_runtime_free(module->functions);
  1708. }
  1709. if (module->tables)
  1710. wasm_runtime_free(module->tables);
  1711. if (module->memories)
  1712. wasm_runtime_free(module->memories);
  1713. if (module->globals)
  1714. wasm_runtime_free(module->globals);
  1715. if (module->exports)
  1716. wasm_runtime_free(module->exports);
  1717. if (module->table_segments) {
  1718. for (i = 0; i < module->table_seg_count; i++) {
  1719. if (module->table_segments[i].func_indexes)
  1720. wasm_runtime_free(module->table_segments[i].func_indexes);
  1721. }
  1722. wasm_runtime_free(module->table_segments);
  1723. }
  1724. if (module->data_segments) {
  1725. for (i = 0; i < module->data_seg_count; i++) {
  1726. if (module->data_segments[i])
  1727. wasm_runtime_free(module->data_segments[i]);
  1728. }
  1729. wasm_runtime_free(module->data_segments);
  1730. }
  1731. if (module->const_str_list) {
  1732. StringNode *node = module->const_str_list, *node_next;
  1733. while (node) {
  1734. node_next = node->next;
  1735. wasm_runtime_free(node);
  1736. node = node_next;
  1737. }
  1738. }
  1739. wasm_runtime_free(module);
  1740. }
  1741. bool
  1742. wasm_loader_find_block_addr(BlockAddr *block_addr_cache,
  1743. const uint8 *start_addr,
  1744. const uint8 *code_end_addr,
  1745. uint8 block_type,
  1746. uint8 **p_else_addr,
  1747. uint8 **p_end_addr,
  1748. char *error_buf,
  1749. uint32 error_buf_size)
  1750. {
  1751. const uint8 *p = start_addr, *p_end = code_end_addr;
  1752. uint8 *else_addr = NULL;
  1753. uint32 block_nested_depth = 1, count, i;
  1754. uint8 opcode, u8;
  1755. BlockAddr block_stack[16] = { 0 }, *block;
  1756. uint32 j, t;
  1757. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  1758. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  1759. /* Cache unhit */
  1760. block_stack[0].start_addr = start_addr;
  1761. while (p < code_end_addr) {
  1762. opcode = *p++;
  1763. switch (opcode) {
  1764. case WASM_OP_UNREACHABLE:
  1765. case WASM_OP_NOP:
  1766. break;
  1767. case WASM_OP_BLOCK:
  1768. case WASM_OP_LOOP:
  1769. case WASM_OP_IF:
  1770. CHECK_BUF(p, p_end, 1);
  1771. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  1772. u8 = read_uint8(p);
  1773. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr)) {
  1774. block_stack[block_nested_depth].start_addr = p;
  1775. block_stack[block_nested_depth].else_addr = NULL;
  1776. }
  1777. block_nested_depth++;
  1778. break;
  1779. case WASM_OP_ELSE:
  1780. if (block_type == BLOCK_TYPE_IF && block_nested_depth == 1)
  1781. else_addr = (uint8*)(p - 1);
  1782. if (block_nested_depth - 1 < sizeof(block_stack)/sizeof(BlockAddr))
  1783. block_stack[block_nested_depth - 1].else_addr = (uint8*)(p - 1);
  1784. break;
  1785. case WASM_OP_END:
  1786. if (block_nested_depth == 1) {
  1787. if (block_type == BLOCK_TYPE_IF)
  1788. *p_else_addr = else_addr;
  1789. *p_end_addr = (uint8*)(p - 1);
  1790. block_stack[0].end_addr = (uint8*)(p - 1);
  1791. for (t = 0; t < sizeof(block_stack)/sizeof(BlockAddr); t++) {
  1792. start_addr = block_stack[t].start_addr;
  1793. if (start_addr) {
  1794. i = ((uintptr_t)start_addr) % BLOCK_ADDR_CACHE_SIZE;
  1795. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  1796. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  1797. if (!block[j].start_addr)
  1798. break;
  1799. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  1800. memmove(block + 1, block, (BLOCK_ADDR_CONFLICT_SIZE - 1) *
  1801. sizeof(BlockAddr));
  1802. j = 0;
  1803. }
  1804. block[j].start_addr = block_stack[t].start_addr;
  1805. block[j].else_addr = block_stack[t].else_addr;
  1806. block[j].end_addr = block_stack[t].end_addr;
  1807. }
  1808. else
  1809. break;
  1810. }
  1811. return true;
  1812. }
  1813. else {
  1814. block_nested_depth--;
  1815. if (block_nested_depth < sizeof(block_stack)/sizeof(BlockAddr))
  1816. block_stack[block_nested_depth].end_addr = (uint8*)(p - 1);
  1817. }
  1818. break;
  1819. case WASM_OP_BR:
  1820. case WASM_OP_BR_IF:
  1821. skip_leb_uint32(p, p_end); /* labelidx */
  1822. break;
  1823. case WASM_OP_BR_TABLE:
  1824. read_leb_uint32(p, p_end, count); /* lable num */
  1825. for (i = 0; i <= count; i++) /* lableidxs */
  1826. skip_leb_uint32(p, p_end);
  1827. break;
  1828. case WASM_OP_RETURN:
  1829. break;
  1830. case WASM_OP_CALL:
  1831. skip_leb_uint32(p, p_end); /* funcidx */
  1832. break;
  1833. case WASM_OP_CALL_INDIRECT:
  1834. skip_leb_uint32(p, p_end); /* typeidx */
  1835. CHECK_BUF(p, p_end, 1);
  1836. u8 = read_uint8(p); /* 0x00 */
  1837. break;
  1838. case WASM_OP_DROP:
  1839. case WASM_OP_SELECT:
  1840. case WASM_OP_DROP_64:
  1841. case WASM_OP_SELECT_64:
  1842. break;
  1843. case WASM_OP_GET_LOCAL:
  1844. case WASM_OP_SET_LOCAL:
  1845. case WASM_OP_TEE_LOCAL:
  1846. case WASM_OP_GET_GLOBAL:
  1847. case WASM_OP_SET_GLOBAL:
  1848. skip_leb_uint32(p, p_end); /* localidx */
  1849. break;
  1850. case EXT_OP_GET_LOCAL_FAST:
  1851. case EXT_OP_SET_LOCAL_FAST:
  1852. case EXT_OP_TEE_LOCAL_FAST:
  1853. CHECK_BUF(p, p_end, 1);
  1854. p++;
  1855. break;
  1856. case WASM_OP_I32_LOAD:
  1857. case WASM_OP_I64_LOAD:
  1858. case WASM_OP_F32_LOAD:
  1859. case WASM_OP_F64_LOAD:
  1860. case WASM_OP_I32_LOAD8_S:
  1861. case WASM_OP_I32_LOAD8_U:
  1862. case WASM_OP_I32_LOAD16_S:
  1863. case WASM_OP_I32_LOAD16_U:
  1864. case WASM_OP_I64_LOAD8_S:
  1865. case WASM_OP_I64_LOAD8_U:
  1866. case WASM_OP_I64_LOAD16_S:
  1867. case WASM_OP_I64_LOAD16_U:
  1868. case WASM_OP_I64_LOAD32_S:
  1869. case WASM_OP_I64_LOAD32_U:
  1870. case WASM_OP_I32_STORE:
  1871. case WASM_OP_I64_STORE:
  1872. case WASM_OP_F32_STORE:
  1873. case WASM_OP_F64_STORE:
  1874. case WASM_OP_I32_STORE8:
  1875. case WASM_OP_I32_STORE16:
  1876. case WASM_OP_I64_STORE8:
  1877. case WASM_OP_I64_STORE16:
  1878. case WASM_OP_I64_STORE32:
  1879. skip_leb_uint32(p, p_end); /* align */
  1880. skip_leb_uint32(p, p_end); /* offset */
  1881. break;
  1882. case WASM_OP_MEMORY_SIZE:
  1883. case WASM_OP_MEMORY_GROW:
  1884. skip_leb_uint32(p, p_end); /* 0x00 */
  1885. break;
  1886. case WASM_OP_I32_CONST:
  1887. skip_leb_int32(p, p_end);
  1888. break;
  1889. case WASM_OP_I64_CONST:
  1890. skip_leb_int64(p, p_end);
  1891. break;
  1892. case WASM_OP_F32_CONST:
  1893. p += sizeof(float32);
  1894. break;
  1895. case WASM_OP_F64_CONST:
  1896. p += sizeof(float64);
  1897. break;
  1898. case WASM_OP_I32_EQZ:
  1899. case WASM_OP_I32_EQ:
  1900. case WASM_OP_I32_NE:
  1901. case WASM_OP_I32_LT_S:
  1902. case WASM_OP_I32_LT_U:
  1903. case WASM_OP_I32_GT_S:
  1904. case WASM_OP_I32_GT_U:
  1905. case WASM_OP_I32_LE_S:
  1906. case WASM_OP_I32_LE_U:
  1907. case WASM_OP_I32_GE_S:
  1908. case WASM_OP_I32_GE_U:
  1909. case WASM_OP_I64_EQZ:
  1910. case WASM_OP_I64_EQ:
  1911. case WASM_OP_I64_NE:
  1912. case WASM_OP_I64_LT_S:
  1913. case WASM_OP_I64_LT_U:
  1914. case WASM_OP_I64_GT_S:
  1915. case WASM_OP_I64_GT_U:
  1916. case WASM_OP_I64_LE_S:
  1917. case WASM_OP_I64_LE_U:
  1918. case WASM_OP_I64_GE_S:
  1919. case WASM_OP_I64_GE_U:
  1920. case WASM_OP_F32_EQ:
  1921. case WASM_OP_F32_NE:
  1922. case WASM_OP_F32_LT:
  1923. case WASM_OP_F32_GT:
  1924. case WASM_OP_F32_LE:
  1925. case WASM_OP_F32_GE:
  1926. case WASM_OP_F64_EQ:
  1927. case WASM_OP_F64_NE:
  1928. case WASM_OP_F64_LT:
  1929. case WASM_OP_F64_GT:
  1930. case WASM_OP_F64_LE:
  1931. case WASM_OP_F64_GE:
  1932. case WASM_OP_I32_CLZ:
  1933. case WASM_OP_I32_CTZ:
  1934. case WASM_OP_I32_POPCNT:
  1935. case WASM_OP_I32_ADD:
  1936. case WASM_OP_I32_SUB:
  1937. case WASM_OP_I32_MUL:
  1938. case WASM_OP_I32_DIV_S:
  1939. case WASM_OP_I32_DIV_U:
  1940. case WASM_OP_I32_REM_S:
  1941. case WASM_OP_I32_REM_U:
  1942. case WASM_OP_I32_AND:
  1943. case WASM_OP_I32_OR:
  1944. case WASM_OP_I32_XOR:
  1945. case WASM_OP_I32_SHL:
  1946. case WASM_OP_I32_SHR_S:
  1947. case WASM_OP_I32_SHR_U:
  1948. case WASM_OP_I32_ROTL:
  1949. case WASM_OP_I32_ROTR:
  1950. case WASM_OP_I64_CLZ:
  1951. case WASM_OP_I64_CTZ:
  1952. case WASM_OP_I64_POPCNT:
  1953. case WASM_OP_I64_ADD:
  1954. case WASM_OP_I64_SUB:
  1955. case WASM_OP_I64_MUL:
  1956. case WASM_OP_I64_DIV_S:
  1957. case WASM_OP_I64_DIV_U:
  1958. case WASM_OP_I64_REM_S:
  1959. case WASM_OP_I64_REM_U:
  1960. case WASM_OP_I64_AND:
  1961. case WASM_OP_I64_OR:
  1962. case WASM_OP_I64_XOR:
  1963. case WASM_OP_I64_SHL:
  1964. case WASM_OP_I64_SHR_S:
  1965. case WASM_OP_I64_SHR_U:
  1966. case WASM_OP_I64_ROTL:
  1967. case WASM_OP_I64_ROTR:
  1968. case WASM_OP_F32_ABS:
  1969. case WASM_OP_F32_NEG:
  1970. case WASM_OP_F32_CEIL:
  1971. case WASM_OP_F32_FLOOR:
  1972. case WASM_OP_F32_TRUNC:
  1973. case WASM_OP_F32_NEAREST:
  1974. case WASM_OP_F32_SQRT:
  1975. case WASM_OP_F32_ADD:
  1976. case WASM_OP_F32_SUB:
  1977. case WASM_OP_F32_MUL:
  1978. case WASM_OP_F32_DIV:
  1979. case WASM_OP_F32_MIN:
  1980. case WASM_OP_F32_MAX:
  1981. case WASM_OP_F32_COPYSIGN:
  1982. case WASM_OP_F64_ABS:
  1983. case WASM_OP_F64_NEG:
  1984. case WASM_OP_F64_CEIL:
  1985. case WASM_OP_F64_FLOOR:
  1986. case WASM_OP_F64_TRUNC:
  1987. case WASM_OP_F64_NEAREST:
  1988. case WASM_OP_F64_SQRT:
  1989. case WASM_OP_F64_ADD:
  1990. case WASM_OP_F64_SUB:
  1991. case WASM_OP_F64_MUL:
  1992. case WASM_OP_F64_DIV:
  1993. case WASM_OP_F64_MIN:
  1994. case WASM_OP_F64_MAX:
  1995. case WASM_OP_F64_COPYSIGN:
  1996. case WASM_OP_I32_WRAP_I64:
  1997. case WASM_OP_I32_TRUNC_S_F32:
  1998. case WASM_OP_I32_TRUNC_U_F32:
  1999. case WASM_OP_I32_TRUNC_S_F64:
  2000. case WASM_OP_I32_TRUNC_U_F64:
  2001. case WASM_OP_I64_EXTEND_S_I32:
  2002. case WASM_OP_I64_EXTEND_U_I32:
  2003. case WASM_OP_I64_TRUNC_S_F32:
  2004. case WASM_OP_I64_TRUNC_U_F32:
  2005. case WASM_OP_I64_TRUNC_S_F64:
  2006. case WASM_OP_I64_TRUNC_U_F64:
  2007. case WASM_OP_F32_CONVERT_S_I32:
  2008. case WASM_OP_F32_CONVERT_U_I32:
  2009. case WASM_OP_F32_CONVERT_S_I64:
  2010. case WASM_OP_F32_CONVERT_U_I64:
  2011. case WASM_OP_F32_DEMOTE_F64:
  2012. case WASM_OP_F64_CONVERT_S_I32:
  2013. case WASM_OP_F64_CONVERT_U_I32:
  2014. case WASM_OP_F64_CONVERT_S_I64:
  2015. case WASM_OP_F64_CONVERT_U_I64:
  2016. case WASM_OP_F64_PROMOTE_F32:
  2017. case WASM_OP_I32_REINTERPRET_F32:
  2018. case WASM_OP_I64_REINTERPRET_F64:
  2019. case WASM_OP_F32_REINTERPRET_I32:
  2020. case WASM_OP_F64_REINTERPRET_I64:
  2021. break;
  2022. default:
  2023. if (error_buf)
  2024. snprintf(error_buf, error_buf_size,
  2025. "WASM loader find block addr failed: "
  2026. "invalid opcode %02x.", opcode);
  2027. return false;
  2028. }
  2029. }
  2030. (void)u8;
  2031. return false;
  2032. }
  2033. #define REF_I32 VALUE_TYPE_I32
  2034. #define REF_F32 VALUE_TYPE_F32
  2035. #define REF_I64_1 VALUE_TYPE_I64
  2036. #define REF_I64_2 VALUE_TYPE_I64
  2037. #define REF_F64_1 VALUE_TYPE_F64
  2038. #define REF_F64_2 VALUE_TYPE_F64
  2039. #if WASM_ENABLE_FAST_INTERP != 0
  2040. #if WASM_DEBUG_PREPROCESSOR != 0
  2041. #define LOG_OP(...) os_printf(__VA_ARGS__)
  2042. #else
  2043. #define LOG_OP(...)
  2044. #endif
  2045. #define PATCH_ELSE 0
  2046. #define PATCH_END 1
  2047. typedef struct BranchBlockPatch {
  2048. struct BranchBlockPatch *next;
  2049. uint8 patch_type;
  2050. uint8 *code_compiled;
  2051. } BranchBlockPatch;
  2052. #endif
  2053. typedef struct BranchBlock {
  2054. uint8 block_type;
  2055. uint8 return_type;
  2056. bool is_block_reachable;
  2057. uint8 *start_addr;
  2058. uint8 *else_addr;
  2059. uint8 *end_addr;
  2060. uint32 stack_cell_num;
  2061. #if WASM_ENABLE_FAST_INTERP != 0
  2062. uint16 dynamic_offset;
  2063. uint8 *code_compiled;
  2064. BranchBlockPatch *patch_list;
  2065. #endif
  2066. } BranchBlock;
  2067. typedef struct WASMLoaderContext {
  2068. /* frame ref stack */
  2069. uint8 *frame_ref;
  2070. uint8 *frame_ref_bottom;
  2071. uint8 *frame_ref_boundary;
  2072. uint32 frame_ref_size;
  2073. uint32 stack_cell_num;
  2074. uint32 max_stack_cell_num;
  2075. /* frame csp stack */
  2076. BranchBlock *frame_csp;
  2077. BranchBlock *frame_csp_bottom;
  2078. BranchBlock *frame_csp_boundary;
  2079. uint32 frame_csp_size;
  2080. uint32 csp_num;
  2081. uint32 max_csp_num;
  2082. #if WASM_ENABLE_FAST_INTERP != 0
  2083. /* frame offset stack */
  2084. int16 *frame_offset;
  2085. int16 *frame_offset_bottom;
  2086. int16 *frame_offset_boundary;
  2087. uint32 frame_offset_size;
  2088. int16 dynamic_offset;
  2089. int16 start_dynamic_offset;
  2090. int16 max_dynamic_offset;
  2091. /* const buffer */
  2092. uint8 *const_buf;
  2093. uint16 num_const;
  2094. uint16 const_buf_size;
  2095. uint16 const_cell_num;
  2096. /* processed code */
  2097. uint8 *p_code_compiled;
  2098. uint8 *p_code_compiled_end;
  2099. uint32 code_compiled_size;
  2100. #endif
  2101. } WASMLoaderContext;
  2102. typedef struct Const {
  2103. WASMValue value;
  2104. uint16 slot_index;
  2105. uint8 value_type;
  2106. } Const;
  2107. static void*
  2108. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new,
  2109. char *error_buf, uint32 error_buf_size)
  2110. {
  2111. uint8 *mem_new;
  2112. bh_assert(size_new > size_old);
  2113. if ((mem_new = wasm_runtime_malloc(size_new))) {
  2114. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  2115. memset(mem_new + size_old, 0, size_new - size_old);
  2116. wasm_runtime_free(mem_old);
  2117. }
  2118. else {
  2119. set_error_buf(error_buf, error_buf_size,
  2120. "WASM loader prepare bytecode failed: "
  2121. "allocate memory failed.");
  2122. }
  2123. return mem_new;
  2124. }
  2125. #define MEM_REALLOC(mem, size_old, size_new) do { \
  2126. void *mem_new = memory_realloc(mem, size_old, size_new, \
  2127. error_buf, error_buf_size); \
  2128. if (!mem_new) \
  2129. goto fail; \
  2130. mem = mem_new; \
  2131. } while (0)
  2132. #define CHECK_CSP_PUSH() do { \
  2133. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  2134. MEM_REALLOC(ctx->frame_csp_bottom, ctx->frame_csp_size, \
  2135. (uint32)(ctx->frame_csp_size \
  2136. + 8 * sizeof(BranchBlock))); \
  2137. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  2138. ctx->frame_csp_boundary = ctx->frame_csp_bottom + \
  2139. ctx->frame_csp_size / sizeof(BranchBlock); \
  2140. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  2141. } \
  2142. } while (0)
  2143. #define CHECK_CSP_POP() do { \
  2144. if (ctx->csp_num < 1) { \
  2145. set_error_buf(error_buf, error_buf_size, \
  2146. "WASM module load failed: type mismatch: " \
  2147. "expect data but block stack was empty"); \
  2148. goto fail; \
  2149. } \
  2150. } while (0)
  2151. #if WASM_ENABLE_FAST_INTERP != 0
  2152. static bool
  2153. check_offset_push(WASMLoaderContext *ctx,
  2154. char *error_buf, uint32 error_buf_size)
  2155. {
  2156. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  2157. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  2158. ctx->frame_offset_size + 16);
  2159. ctx->frame_offset_size += 16;
  2160. ctx->frame_offset_boundary = ctx->frame_offset_bottom +
  2161. ctx->frame_offset_size / sizeof(int16);
  2162. ctx->frame_offset = ctx->frame_offset_bottom + ctx->stack_cell_num;
  2163. }
  2164. return true;
  2165. fail:
  2166. return false;
  2167. }
  2168. static void free_label_patch_list(BranchBlock *frame_csp)
  2169. {
  2170. BranchBlockPatch *label_patch = frame_csp->patch_list;
  2171. BranchBlockPatch *next;
  2172. while (label_patch != NULL) {
  2173. next = label_patch->next;
  2174. wasm_runtime_free(label_patch);
  2175. label_patch = next;
  2176. }
  2177. frame_csp->patch_list = NULL;
  2178. }
  2179. static void free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  2180. {
  2181. BranchBlock *tmp_csp = frame_csp;
  2182. for (uint32 i = 0; i < csp_num; i++) {
  2183. free_label_patch_list(tmp_csp);
  2184. tmp_csp ++;
  2185. }
  2186. }
  2187. #endif
  2188. static bool
  2189. check_stack_push(WASMLoaderContext *ctx,
  2190. char *error_buf, uint32 error_buf_size)
  2191. {
  2192. if (ctx->frame_ref >= ctx->frame_ref_boundary) {
  2193. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  2194. ctx->frame_ref_size + 16);
  2195. ctx->frame_ref_size += 16;
  2196. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  2197. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  2198. }
  2199. return true;
  2200. fail:
  2201. return false;
  2202. }
  2203. static bool
  2204. check_stack_pop(WASMLoaderContext *ctx, uint8 type,
  2205. char *error_buf, uint32 error_buf_size,
  2206. const char *type_str)
  2207. {
  2208. if (((type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2209. && ctx->stack_cell_num < 1)
  2210. || ((type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64)
  2211. && ctx->stack_cell_num < 2)) {
  2212. set_error_buf(error_buf, error_buf_size,
  2213. "WASM module load failed: "
  2214. "type mismatch: expect data but stack was empty");
  2215. return false;
  2216. }
  2217. if ((type == VALUE_TYPE_I32 && *(ctx->frame_ref - 1) != REF_I32)
  2218. || (type == VALUE_TYPE_F32 && *(ctx->frame_ref - 1) != REF_F32)
  2219. || (type == VALUE_TYPE_I64
  2220. && (*(ctx->frame_ref - 2) != REF_I64_1
  2221. || *(ctx->frame_ref - 1) != REF_I64_2))
  2222. || (type == VALUE_TYPE_F64
  2223. && (*(ctx->frame_ref - 2) != REF_F64_1
  2224. || *(ctx->frame_ref - 1) != REF_F64_2))) {
  2225. if (error_buf != NULL)
  2226. snprintf(error_buf, error_buf_size, "%s%s%s",
  2227. "WASM module load failed: type mismatch: expect ",
  2228. type_str, " but got other");
  2229. return false;
  2230. }
  2231. return true;
  2232. }
  2233. static void wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  2234. {
  2235. if (ctx) {
  2236. if (ctx->frame_ref_bottom)
  2237. wasm_runtime_free(ctx->frame_ref_bottom);
  2238. if (ctx->frame_csp_bottom) {
  2239. #if WASM_ENABLE_FAST_INTERP != 0
  2240. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  2241. #endif
  2242. wasm_runtime_free(ctx->frame_csp_bottom);
  2243. }
  2244. #if WASM_ENABLE_FAST_INTERP != 0
  2245. if (ctx->frame_offset_bottom)
  2246. wasm_runtime_free(ctx->frame_offset_bottom);
  2247. if (ctx->const_buf)
  2248. wasm_runtime_free(ctx->const_buf);
  2249. #endif
  2250. wasm_runtime_free(ctx);
  2251. }
  2252. }
  2253. static WASMLoaderContext*
  2254. wasm_loader_ctx_init(WASMFunction *func)
  2255. {
  2256. WASMLoaderContext *loader_ctx =
  2257. wasm_runtime_malloc(sizeof(WASMLoaderContext));
  2258. if (!loader_ctx)
  2259. return false;
  2260. memset(loader_ctx, 0, sizeof(WASMLoaderContext));
  2261. loader_ctx->frame_ref_size = 32;
  2262. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
  2263. wasm_runtime_malloc(loader_ctx->frame_ref_size)))
  2264. goto fail;
  2265. memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
  2266. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom +
  2267. loader_ctx->frame_ref_size;
  2268. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  2269. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
  2270. wasm_runtime_malloc(loader_ctx->frame_csp_size)))
  2271. goto fail;
  2272. memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
  2273. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  2274. #if WASM_ENABLE_FAST_INTERP != 0
  2275. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  2276. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  2277. wasm_runtime_malloc(loader_ctx->frame_offset_size)))
  2278. goto fail;
  2279. memset(loader_ctx->frame_offset_bottom, 0,
  2280. loader_ctx->frame_offset_size);
  2281. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  2282. loader_ctx->num_const = 0;
  2283. loader_ctx->const_buf_size = sizeof(Const) * 8;
  2284. if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size)))
  2285. goto fail;
  2286. memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
  2287. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  2288. loader_ctx->max_dynamic_offset = func->param_cell_num +
  2289. func->local_cell_num;
  2290. #endif
  2291. return loader_ctx;
  2292. fail:
  2293. wasm_loader_ctx_destroy(loader_ctx);
  2294. return NULL;
  2295. }
  2296. static bool
  2297. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type,
  2298. char *error_buf, uint32 error_buf_size)
  2299. {
  2300. if (type == VALUE_TYPE_VOID)
  2301. return true;
  2302. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2303. return false;
  2304. *ctx->frame_ref++ = type;
  2305. ctx->stack_cell_num++;
  2306. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2307. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2308. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2309. return true;
  2310. if (!check_stack_push(ctx, error_buf, error_buf_size))
  2311. return false;
  2312. *ctx->frame_ref++ = type;
  2313. ctx->stack_cell_num++;
  2314. if (ctx->stack_cell_num > ctx->max_stack_cell_num)
  2315. ctx->max_stack_cell_num = ctx->stack_cell_num;
  2316. return true;
  2317. }
  2318. static bool
  2319. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type,
  2320. char *error_buf, uint32 error_buf_size)
  2321. {
  2322. char *type_str[] = { "f64", "f32", "i64", "i32" };
  2323. if (type == VALUE_TYPE_VOID)
  2324. return true;
  2325. if (!check_stack_pop(ctx, type, error_buf, error_buf_size,
  2326. type_str[type - VALUE_TYPE_F64]))
  2327. return false;
  2328. ctx->frame_ref--;
  2329. ctx->stack_cell_num--;
  2330. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2331. return true;
  2332. ctx->frame_ref--;
  2333. ctx->stack_cell_num--;
  2334. return true;
  2335. }
  2336. static bool
  2337. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 type,
  2338. uint8 ret_type, uint8* start_addr,
  2339. char *error_buf, uint32 error_buf_size)
  2340. {
  2341. CHECK_CSP_PUSH();
  2342. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  2343. ctx->frame_csp->block_type = type;
  2344. ctx->frame_csp->return_type = ret_type;
  2345. ctx->frame_csp->start_addr = start_addr;
  2346. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  2347. #if WASM_ENABLE_FAST_INTERP != 0
  2348. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  2349. ctx->frame_csp->patch_list = NULL;
  2350. #endif
  2351. ctx->frame_csp++;
  2352. ctx->csp_num++;
  2353. if (ctx->csp_num > ctx->max_csp_num)
  2354. ctx->max_csp_num = ctx->csp_num;
  2355. return true;
  2356. fail:
  2357. return false;
  2358. }
  2359. static bool
  2360. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx,
  2361. char *error_buf, uint32 error_buf_size)
  2362. {
  2363. CHECK_CSP_POP();
  2364. ctx->frame_csp--;
  2365. ctx->csp_num--;
  2366. return true;
  2367. fail:
  2368. return false;
  2369. }
  2370. static bool
  2371. wasm_loader_check_br(WASMLoaderContext *ctx, uint32 depth,
  2372. char *error_buf, uint32 error_buf_size)
  2373. {
  2374. if (ctx->csp_num < depth + 1) {
  2375. set_error_buf(error_buf, error_buf_size,
  2376. "WASM module load failed: type mismatch: "
  2377. "unexpected end of section or function");
  2378. return false;
  2379. }
  2380. if ((ctx->frame_csp - (depth + 1))->block_type != BLOCK_TYPE_LOOP) {
  2381. uint8 tmp_ret_type = (ctx->frame_csp - (depth + 1))->return_type;
  2382. if ((tmp_ret_type == VALUE_TYPE_I32
  2383. && (ctx->stack_cell_num < 1 || *(ctx->frame_ref - 1) != REF_I32))
  2384. || (tmp_ret_type == VALUE_TYPE_F32
  2385. && (ctx->stack_cell_num < 1 || *(ctx->frame_ref - 1) != REF_F32))
  2386. || (tmp_ret_type == VALUE_TYPE_I64
  2387. && (ctx->stack_cell_num < 2
  2388. || *(ctx->frame_ref - 2) != REF_I64_1
  2389. || *(ctx->frame_ref - 1) != REF_I64_2))
  2390. || (tmp_ret_type == VALUE_TYPE_F64
  2391. && (ctx->stack_cell_num < 2
  2392. || *(ctx->frame_ref - 2) != REF_F64_1
  2393. || *(ctx->frame_ref - 1) != REF_F64_2))) {
  2394. set_error_buf(error_buf, error_buf_size,
  2395. "WASM module load failed: type mismatch: "
  2396. "expect data but stack was empty or other type");
  2397. return false;
  2398. }
  2399. (ctx->frame_csp - (depth + 1))->is_block_reachable = true;
  2400. }
  2401. return true;
  2402. }
  2403. #if WASM_ENABLE_FAST_INTERP != 0
  2404. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  2405. #define emit_label(opcode) do { \
  2406. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  2407. LOG_OP("\nemit_op [%02x]\t", opcode); \
  2408. } while (0)
  2409. #define skip_label() do { \
  2410. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  2411. LOG_OP("\ndelete last op\n"); \
  2412. } while (0)
  2413. #else
  2414. #define emit_label(opcode) do { \
  2415. int32 offset = (int32)(handle_table[opcode] - handle_table[0]); \
  2416. if (!(offset >= INT16_MIN && offset < INT16_MAX)) { \
  2417. set_error_buf(error_buf, error_buf_size, \
  2418. "WASM module load failed: " \
  2419. "pre-compiled label offset out of range"); \
  2420. goto fail; \
  2421. } \
  2422. wasm_loader_emit_int16(loader_ctx, offset); \
  2423. LOG_OP("\nemit_op [%02x]\t", opcode); \
  2424. } while (0)
  2425. // drop local.get / const / block / loop / end
  2426. #define skip_label() do { \
  2427. wasm_loader_emit_backspace(loader_ctx, sizeof(int16)); \
  2428. LOG_OP("\ndelete last op\n"); \
  2429. } while (0)
  2430. #endif /* WASM_ENABLE_ABS_LABEL_ADDR */
  2431. #define emit_empty_label_addr_and_frame_ip(type) do { \
  2432. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  2433. loader_ctx->p_code_compiled, \
  2434. error_buf, error_buf_size)) \
  2435. goto fail; \
  2436. /* label address, to be patched */ \
  2437. wasm_loader_emit_ptr(loader_ctx, NULL); \
  2438. } while (0)
  2439. #define emit_br_info(frame_csp) do { \
  2440. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, \
  2441. error_buf, error_buf_size)) \
  2442. goto fail; \
  2443. } while (0)
  2444. #define LAST_OP_OUTPUT_I32() (last_op >= WASM_OP_I32_EQZ \
  2445. && last_op <= WASM_OP_I32_ROTR) \
  2446. || (last_op == WASM_OP_I32_LOAD \
  2447. || last_op == WASM_OP_F32_LOAD) \
  2448. || (last_op >= WASM_OP_I32_LOAD8_S \
  2449. && last_op <= WASM_OP_I32_LOAD16_U) \
  2450. || (last_op >= WASM_OP_F32_ABS \
  2451. && last_op <= WASM_OP_F32_COPYSIGN) \
  2452. || (last_op >= WASM_OP_I32_WRAP_I64 \
  2453. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  2454. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  2455. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  2456. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  2457. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  2458. || (last_op == EXT_OP_COPY_STACK_TOP)
  2459. #define LAST_OP_OUTPUT_I64() (last_op >= WASM_OP_I64_CLZ \
  2460. && last_op <= WASM_OP_I64_ROTR) \
  2461. || (last_op >= WASM_OP_F64_ABS \
  2462. && last_op <= WASM_OP_F64_COPYSIGN) \
  2463. || (last_op == WASM_OP_I64_LOAD \
  2464. || last_op == WASM_OP_F64_LOAD) \
  2465. || (last_op >= WASM_OP_I64_LOAD8_S \
  2466. && last_op <= WASM_OP_I64_LOAD32_U) \
  2467. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  2468. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  2469. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  2470. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  2471. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  2472. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  2473. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  2474. #define GET_CONST_OFFSET(type, val) do { \
  2475. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2476. val, 0, 0, &operand_offset, \
  2477. error_buf, error_buf_size))) \
  2478. goto fail; \
  2479. } while (0)
  2480. #define GET_CONST_F32_OFFSET(type, fval) do { \
  2481. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2482. 0, fval, 0, &operand_offset, \
  2483. error_buf, error_buf_size))) \
  2484. goto fail; \
  2485. } while (0)
  2486. #define GET_CONST_F64_OFFSET(type, fval) do { \
  2487. if (!(wasm_loader_get_const_offset(loader_ctx, type, \
  2488. 0, 0, fval, &operand_offset, \
  2489. error_buf, error_buf_size))) \
  2490. goto fail; \
  2491. } while (0)
  2492. #define emit_operand(ctx, offset) do { \
  2493. wasm_loader_emit_int16(ctx, offset); \
  2494. LOG_OP("%d\t", offset); \
  2495. } while (0)
  2496. #define emit_byte(ctx, byte) do { \
  2497. wasm_loader_emit_uint8(ctx, byte); \
  2498. LOG_OP("%d\t", byte); \
  2499. } while (0)
  2500. #define emit_leb() do { \
  2501. wasm_loader_emit_leb(loader_ctx, p_org, p); \
  2502. } while (0)
  2503. #define emit_const(value) do { \
  2504. GET_CONST_OFFSET(VALUE_TYPE_I32, value); \
  2505. emit_operand(loader_ctx, operand_offset); \
  2506. } while (0)
  2507. static bool
  2508. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  2509. {
  2510. if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
  2511. return false;
  2512. memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
  2513. ctx->p_code_compiled_end = ctx->p_code_compiled +
  2514. ctx->code_compiled_size;
  2515. /* clean up frame ref */
  2516. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  2517. ctx->frame_ref = ctx->frame_ref_bottom;
  2518. ctx->stack_cell_num = 0;
  2519. /* clean up frame csp */
  2520. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  2521. ctx->frame_csp = ctx->frame_csp_bottom;
  2522. ctx->csp_num = 0;
  2523. ctx->max_csp_num = 0;
  2524. /* clean up frame offset */
  2525. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  2526. ctx->frame_offset = ctx->frame_offset_bottom;
  2527. ctx->dynamic_offset = ctx->start_dynamic_offset;
  2528. /* const buf is reserved */
  2529. return true;
  2530. }
  2531. static void
  2532. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  2533. {
  2534. if (ctx->p_code_compiled) {
  2535. *(int16*)(ctx->p_code_compiled) = value;
  2536. ctx->p_code_compiled += sizeof(int16);
  2537. }
  2538. else
  2539. ctx->code_compiled_size += sizeof(int16);
  2540. }
  2541. static void
  2542. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  2543. {
  2544. if (ctx->p_code_compiled) {
  2545. *(ctx->p_code_compiled) = value;
  2546. ctx->p_code_compiled += sizeof(uint8);
  2547. }
  2548. else
  2549. ctx->code_compiled_size += sizeof(uint8);
  2550. }
  2551. static void
  2552. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  2553. {
  2554. if (ctx->p_code_compiled) {
  2555. *(uint8**)(ctx->p_code_compiled) = value;
  2556. ctx->p_code_compiled += sizeof(void *);
  2557. }
  2558. else
  2559. ctx->code_compiled_size += sizeof(void *);
  2560. }
  2561. static void
  2562. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  2563. {
  2564. if (ctx->p_code_compiled) {
  2565. ctx->p_code_compiled -= size;
  2566. }
  2567. else
  2568. ctx->code_compiled_size -= size;
  2569. }
  2570. static void
  2571. wasm_loader_emit_leb(WASMLoaderContext *ctx, uint8* start, uint8* end)
  2572. {
  2573. if (ctx->p_code_compiled) {
  2574. bh_memcpy_s(ctx->p_code_compiled,
  2575. ctx->p_code_compiled_end - ctx->p_code_compiled,
  2576. start, end - start);
  2577. ctx->p_code_compiled += (end - start);
  2578. }
  2579. else {
  2580. ctx->code_compiled_size += (end - start);
  2581. }
  2582. }
  2583. static bool
  2584. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  2585. uint32 local_index, uint32 local_type, bool *preserved,
  2586. char *error_buf, uint32 error_buf_size)
  2587. {
  2588. int16 preserved_offset = (int16)local_index;
  2589. *preserved = false;
  2590. for (uint32 i = 0; i < loader_ctx->stack_cell_num; i++) {
  2591. /* move previous local into dynamic space before a set/tee_local opcode */
  2592. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  2593. if (preserved_offset == (int16)local_index) {
  2594. *preserved = true;
  2595. skip_label();
  2596. if (local_type == VALUE_TYPE_I32
  2597. || local_type == VALUE_TYPE_F32) {
  2598. preserved_offset = loader_ctx->dynamic_offset++;
  2599. emit_label(EXT_OP_COPY_STACK_TOP);
  2600. }
  2601. else {
  2602. preserved_offset = loader_ctx->dynamic_offset;
  2603. loader_ctx->dynamic_offset += 2;
  2604. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  2605. }
  2606. emit_operand(loader_ctx, local_index);
  2607. emit_operand(loader_ctx, preserved_offset);
  2608. emit_label(opcode);
  2609. if (loader_ctx->dynamic_offset > loader_ctx->max_dynamic_offset)
  2610. loader_ctx->max_dynamic_offset = loader_ctx->dynamic_offset;
  2611. }
  2612. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  2613. }
  2614. }
  2615. return true;
  2616. #if WASM_ENABLE_ABS_LABEL_ADDR == 0
  2617. fail:
  2618. return false;
  2619. #endif
  2620. }
  2621. static bool
  2622. add_label_patch_to_list(BranchBlock *frame_csp,
  2623. uint8 patch_type, uint8 *p_code_compiled,
  2624. char *error_buf, uint32 error_buf_size)
  2625. {
  2626. BranchBlockPatch *patch = wasm_runtime_malloc(sizeof(BranchBlockPatch));
  2627. if (!patch) {
  2628. set_error_buf(error_buf, error_buf_size,
  2629. "WASM loader prepare bytecode failed: "
  2630. "allocate memory failed");
  2631. return false;
  2632. }
  2633. patch->patch_type = patch_type;
  2634. patch->code_compiled = p_code_compiled;
  2635. if (!frame_csp->patch_list) {
  2636. frame_csp->patch_list = patch;
  2637. patch->next = NULL;
  2638. }
  2639. else {
  2640. patch->next = frame_csp->patch_list;
  2641. frame_csp->patch_list = patch;
  2642. }
  2643. return true;
  2644. }
  2645. static void
  2646. apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
  2647. uint8 patch_type)
  2648. {
  2649. BranchBlock *frame_csp = ctx->frame_csp - depth;
  2650. BranchBlockPatch *node = frame_csp->patch_list;
  2651. BranchBlockPatch *node_prev = NULL, *node_next;
  2652. if (!ctx->p_code_compiled)
  2653. return;
  2654. while (node) {
  2655. node_next = node->next;
  2656. if (node->patch_type == patch_type) {
  2657. *((uint8**)node->code_compiled) = ctx->p_code_compiled;
  2658. if (node_prev == NULL) {
  2659. frame_csp->patch_list = node_next;
  2660. }
  2661. else {
  2662. node_prev->next = node_next;
  2663. }
  2664. wasm_runtime_free(node);
  2665. }
  2666. else {
  2667. node_prev = node;
  2668. }
  2669. node = node_next;
  2670. }
  2671. }
  2672. static bool
  2673. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  2674. char *error_buf, uint32 error_buf_size)
  2675. {
  2676. emit_operand(ctx, frame_csp->dynamic_offset);
  2677. if (frame_csp->block_type == BLOCK_TYPE_LOOP ||
  2678. frame_csp->return_type == VALUE_TYPE_VOID) {
  2679. emit_byte(ctx, 0);
  2680. emit_operand(ctx, 0);
  2681. }
  2682. else if (frame_csp->return_type == VALUE_TYPE_I32
  2683. || frame_csp->return_type == VALUE_TYPE_F32) {
  2684. emit_byte(ctx, 1);
  2685. emit_operand(ctx, *(int16*)(ctx->frame_offset - 1));
  2686. }
  2687. else if (frame_csp->return_type == VALUE_TYPE_I64
  2688. || frame_csp->return_type == VALUE_TYPE_F64) {
  2689. emit_byte(ctx, 2);
  2690. emit_operand(ctx, *(int16*)(ctx->frame_offset - 2));
  2691. }
  2692. if (frame_csp->block_type == BLOCK_TYPE_LOOP) {
  2693. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  2694. }
  2695. else {
  2696. if (!add_label_patch_to_list(frame_csp, PATCH_END,
  2697. ctx->p_code_compiled,
  2698. error_buf, error_buf_size))
  2699. return false;
  2700. /* label address, to be patched */
  2701. wasm_loader_emit_ptr(ctx, NULL);
  2702. }
  2703. return true;
  2704. }
  2705. static bool
  2706. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  2707. bool disable_emit, int16 operand_offset,
  2708. char *error_buf, uint32 error_buf_size)
  2709. {
  2710. if (type == VALUE_TYPE_VOID)
  2711. return true;
  2712. // only check memory overflow in first traverse
  2713. if (ctx->p_code_compiled == NULL) {
  2714. if (!check_offset_push(ctx, error_buf, error_buf_size))
  2715. return false;
  2716. }
  2717. if (disable_emit)
  2718. *(ctx->frame_offset)++ = operand_offset;
  2719. else {
  2720. emit_operand(ctx, ctx->dynamic_offset);
  2721. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  2722. ctx->dynamic_offset++;
  2723. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  2724. ctx->max_dynamic_offset = ctx->dynamic_offset;
  2725. }
  2726. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32)
  2727. return true;
  2728. if (ctx->p_code_compiled == NULL) {
  2729. if (!check_offset_push(ctx, error_buf, error_buf_size))
  2730. return false;
  2731. }
  2732. ctx->frame_offset++;
  2733. ctx->dynamic_offset++;
  2734. if (ctx->dynamic_offset > ctx->max_dynamic_offset)
  2735. ctx->max_dynamic_offset = ctx->dynamic_offset;
  2736. return true;
  2737. }
  2738. /* The frame_offset stack should always keep the same depth with
  2739. frame_ref, so we don't check pop of frame_offset */
  2740. static bool
  2741. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  2742. char *error_buf, uint32 error_buf_size)
  2743. {
  2744. if (type == VALUE_TYPE_VOID)
  2745. return true;
  2746. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32) {
  2747. ctx->frame_offset -= 1;
  2748. if (*(ctx->frame_offset) > ctx->start_dynamic_offset)
  2749. ctx->dynamic_offset -= 1;
  2750. }
  2751. else {
  2752. ctx->frame_offset -= 2;
  2753. if (*(ctx->frame_offset) > ctx->start_dynamic_offset)
  2754. ctx->dynamic_offset -= 2;
  2755. }
  2756. emit_operand(ctx, *(ctx->frame_offset));
  2757. return true;
  2758. }
  2759. static bool
  2760. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  2761. bool disable_emit, int16 operand_offset,
  2762. char *error_buf, uint32 error_buf_size)
  2763. {
  2764. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  2765. return false;
  2766. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  2767. error_buf, error_buf_size)))
  2768. return false;
  2769. return true;
  2770. }
  2771. static bool
  2772. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  2773. char *error_buf, uint32 error_buf_size)
  2774. {
  2775. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  2776. return false;
  2777. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  2778. return false;
  2779. return true;
  2780. }
  2781. static bool
  2782. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type,
  2783. int64 val_int, float32 val_f32,
  2784. float64 val_f64, int16 *offset,
  2785. char *error_buf, uint32 error_buf_size)
  2786. {
  2787. int16 operand_offset = 0;
  2788. Const *c;
  2789. for (c = (Const *)ctx->const_buf;
  2790. (uint8*)c < ctx->const_buf + ctx->num_const * sizeof(Const); c ++) {
  2791. if ((type == c->value_type)
  2792. && ((type == VALUE_TYPE_I64 && (int64)val_int == c->value.i64)
  2793. || (type == VALUE_TYPE_I32 && (int32)val_int == c->value.i32)
  2794. || (type == VALUE_TYPE_F64 && (float64)val_f64 == c->value.f64)
  2795. || (type == VALUE_TYPE_F32 && (float32)val_f32 == c->value.f32))) {
  2796. operand_offset = c->slot_index;
  2797. break;
  2798. }
  2799. if (c->value_type == VALUE_TYPE_I64
  2800. || c->value_type == VALUE_TYPE_F64)
  2801. operand_offset += 2;
  2802. else
  2803. operand_offset += 1;
  2804. }
  2805. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  2806. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  2807. MEM_REALLOC(ctx->const_buf,
  2808. ctx->const_buf_size,
  2809. ctx->const_buf_size + 4 * sizeof(Const));
  2810. ctx->const_buf_size += 4 * sizeof(Const);
  2811. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  2812. }
  2813. c->value_type = type;
  2814. switch (type) {
  2815. case VALUE_TYPE_F64:
  2816. c->value.f64 = (float64)val_f64;
  2817. ctx->const_cell_num += 2;
  2818. /* The const buf will be reversed, we use the second cell */
  2819. /* of the i64/f64 const so the finnal offset is corrent */
  2820. operand_offset ++;
  2821. break;
  2822. case VALUE_TYPE_I64:
  2823. c->value.i64 = (int64)val_int;
  2824. ctx->const_cell_num += 2;
  2825. operand_offset ++;
  2826. break;
  2827. case VALUE_TYPE_F32:
  2828. c->value.f32 = (float32)val_f32;
  2829. ctx->const_cell_num ++;
  2830. break;
  2831. case VALUE_TYPE_I32:
  2832. c->value.i32 = (int32)val_int;
  2833. ctx->const_cell_num ++;
  2834. break;
  2835. default:
  2836. break;
  2837. }
  2838. c->slot_index = operand_offset;
  2839. ctx->num_const ++;
  2840. LOG_OP("#### new const [%d]: %ld\n",
  2841. ctx->num_const, (int64)c->value.i64);
  2842. }
  2843. /* use negetive index for const */
  2844. operand_offset = -(operand_offset + 1);
  2845. *offset = operand_offset;
  2846. return true;
  2847. fail:
  2848. return false;
  2849. }
  2850. /*
  2851. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  2852. -- Mostly used for the binary / compare operation
  2853. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  2854. -- Mostly used in block / control instructions
  2855. The POP will always emit the offset on the top of the frame_offset stack
  2856. PUSH can be used in two ways:
  2857. 1. directly PUSH:
  2858. PUSH_XXX();
  2859. will allocate a dynamic space and emit
  2860. 2. silent PUSH:
  2861. operand_offset = xxx; disable_emit = true;
  2862. PUSH_XXX();
  2863. only push the frame_offset stack, no emit
  2864. */
  2865. #define PUSH_I32() do { \
  2866. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  2867. disable_emit, operand_offset,\
  2868. error_buf, error_buf_size)) \
  2869. goto fail; \
  2870. } while (0)
  2871. #define PUSH_F32() do { \
  2872. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  2873. disable_emit, operand_offset,\
  2874. error_buf, error_buf_size)) \
  2875. goto fail; \
  2876. } while (0)
  2877. #define PUSH_I64() do { \
  2878. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  2879. disable_emit, operand_offset,\
  2880. error_buf, error_buf_size)) \
  2881. goto fail; \
  2882. } while (0)
  2883. #define PUSH_F64() do { \
  2884. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  2885. disable_emit, operand_offset,\
  2886. error_buf, error_buf_size)) \
  2887. goto fail; \
  2888. } while (0)
  2889. #define POP_I32() do { \
  2890. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I32, \
  2891. error_buf, error_buf_size)) \
  2892. goto fail; \
  2893. } while (0)
  2894. #define POP_F32() do { \
  2895. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F32, \
  2896. error_buf, error_buf_size)) \
  2897. goto fail; \
  2898. } while (0)
  2899. #define POP_I64() do { \
  2900. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_I64, \
  2901. error_buf, error_buf_size)) \
  2902. goto fail; \
  2903. } while (0)
  2904. #define POP_F64() do { \
  2905. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_F64, \
  2906. error_buf, error_buf_size)) \
  2907. goto fail; \
  2908. } while (0)
  2909. #define PUSH_OFFSET_TYPE(type) do { \
  2910. if (!(wasm_loader_push_frame_offset(loader_ctx, type, \
  2911. disable_emit, operand_offset, \
  2912. error_buf, error_buf_size))) \
  2913. goto fail; \
  2914. } while (0)
  2915. #define POP_OFFSET_TYPE(type) do { \
  2916. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, \
  2917. error_buf, error_buf_size))) \
  2918. goto fail; \
  2919. } while (0)
  2920. #else /* WASM_ENABLE_FAST_INTERP */
  2921. #define PUSH_I32() do { \
  2922. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  2923. error_buf, error_buf_size))) \
  2924. goto fail; \
  2925. } while (0)
  2926. #define PUSH_F32() do { \
  2927. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  2928. error_buf, error_buf_size))) \
  2929. goto fail; \
  2930. } while (0)
  2931. #define PUSH_I64() do { \
  2932. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  2933. error_buf, error_buf_size))) \
  2934. goto fail; \
  2935. } while (0)
  2936. #define PUSH_F64() do { \
  2937. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  2938. error_buf, error_buf_size))) \
  2939. goto fail; \
  2940. } while (0)
  2941. #define POP_I32() do { \
  2942. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I32, \
  2943. error_buf, error_buf_size))) \
  2944. goto fail; \
  2945. } while (0)
  2946. #define POP_F32() do { \
  2947. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F32, \
  2948. error_buf, error_buf_size))) \
  2949. goto fail; \
  2950. } while (0)
  2951. #define POP_I64() do { \
  2952. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_I64, \
  2953. error_buf, error_buf_size))) \
  2954. goto fail; \
  2955. } while (0)
  2956. #define POP_F64() do { \
  2957. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_F64, \
  2958. error_buf, error_buf_size))) \
  2959. goto fail; \
  2960. } while (0)
  2961. #endif /* WASM_ENABLE_FAST_INTERP */
  2962. #define PUSH_TYPE(type) do { \
  2963. if (!(wasm_loader_push_frame_ref(loader_ctx, type, \
  2964. error_buf, error_buf_size))) \
  2965. goto fail; \
  2966. } while (0)
  2967. #define POP_TYPE(type) do { \
  2968. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, \
  2969. error_buf, error_buf_size))) \
  2970. goto fail; \
  2971. } while (0)
  2972. #define PUSH_CSP(type, ret_type, _start_addr) do { \
  2973. if (!wasm_loader_push_frame_csp(loader_ctx, type, ret_type, \
  2974. _start_addr, error_buf, \
  2975. error_buf_size)) \
  2976. goto fail; \
  2977. } while (0)
  2978. #define POP_CSP() do { \
  2979. if (!wasm_loader_pop_frame_csp(loader_ctx, \
  2980. error_buf, error_buf_size)) \
  2981. goto fail; \
  2982. } while (0)
  2983. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
  2984. read_leb_uint32(p, p_end, local_idx); \
  2985. if (local_idx >= param_count + local_count) { \
  2986. set_error_buf(error_buf, error_buf_size, \
  2987. "WASM module load failed: " \
  2988. "local index out of range"); \
  2989. goto fail; \
  2990. } \
  2991. local_type = local_idx < param_count \
  2992. ? param_types[local_idx] \
  2993. : local_types[local_idx - param_count]; \
  2994. local_offset = local_offsets[local_idx]; \
  2995. } while (0)
  2996. #define CHECK_BR(depth) do { \
  2997. if (!wasm_loader_check_br(loader_ctx, depth, \
  2998. error_buf, error_buf_size)) \
  2999. goto fail; \
  3000. } while (0)
  3001. static bool
  3002. check_memory(WASMModule *module,
  3003. char *error_buf, uint32 error_buf_size)
  3004. {
  3005. if (module->memory_count == 0
  3006. && module->import_memory_count == 0) {
  3007. set_error_buf(error_buf, error_buf_size,
  3008. "WASM module load failed: "
  3009. "load or store in module without default memory");
  3010. return false;
  3011. }
  3012. return true;
  3013. }
  3014. #define CHECK_MEMORY() do { \
  3015. if (!check_memory(module, error_buf, error_buf_size)) \
  3016. goto fail; \
  3017. } while (0)
  3018. #if WASM_ENABLE_FAST_INTERP != 0
  3019. #endif /* WASM_ENABLE_FAST_INTERP */
  3020. static bool
  3021. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  3022. BlockAddr *block_addr_cache,
  3023. char *error_buf, uint32 error_buf_size)
  3024. {
  3025. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  3026. uint32 param_count, local_count, global_count;
  3027. uint8 *param_types, ret_type, *local_types, local_type, global_type;
  3028. uint16 *local_offsets, local_offset;
  3029. uint32 count, i, local_idx, global_idx, depth, u32, align, mem_offset;
  3030. uint32 cache_index, item_index;
  3031. int32 i32, i32_const = 0;
  3032. int64 i64;
  3033. uint8 opcode, u8, block_return_type;
  3034. bool return_value = false, is_i32_const = false;
  3035. BlockAddr *cache_items;
  3036. WASMLoaderContext *loader_ctx;
  3037. #if WASM_ENABLE_FAST_INTERP != 0
  3038. uint8 *func_const_end, *func_const;
  3039. int16 operand_offset;
  3040. uint8 last_op = 0;
  3041. bool disable_emit, preserve_local = false;
  3042. float32 f32;
  3043. float64 f64;
  3044. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  3045. func->param_cell_num,
  3046. func->local_cell_num,
  3047. func->ret_cell_num);
  3048. #endif
  3049. global_count = module->import_global_count + module->global_count;
  3050. param_count = func->func_type->param_count;
  3051. param_types = func->func_type->types;
  3052. ret_type = func->func_type->result_count
  3053. ? param_types[param_count] : VALUE_TYPE_VOID;
  3054. local_count = func->local_count;
  3055. local_types = func->local_types;
  3056. local_offsets = func->local_offsets;
  3057. if (!(loader_ctx = wasm_loader_ctx_init(func))) {
  3058. set_error_buf(error_buf, error_buf_size,
  3059. "WASM loader prepare bytecode failed: "
  3060. "allocate memory failed");
  3061. goto fail;
  3062. }
  3063. #if WASM_ENABLE_FAST_INTERP != 0
  3064. re_scan:
  3065. if (loader_ctx->code_compiled_size > 0) {
  3066. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  3067. set_error_buf(error_buf, error_buf_size,
  3068. "WASM loader prepare bytecode failed: "
  3069. "allocate memory failed");
  3070. goto fail;
  3071. }
  3072. p = func->code;
  3073. func->code_compiled = loader_ctx->p_code_compiled;
  3074. }
  3075. #endif
  3076. PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type, p);
  3077. (loader_ctx->frame_csp - 1)->is_block_reachable = true;
  3078. while (p < p_end) {
  3079. opcode = *p++;
  3080. #if WASM_ENABLE_FAST_INTERP != 0
  3081. p_org = p;
  3082. disable_emit = false;
  3083. emit_label(opcode);
  3084. #endif
  3085. switch (opcode) {
  3086. case WASM_OP_UNREACHABLE:
  3087. goto handle_next_reachable_block;
  3088. case WASM_OP_NOP:
  3089. #if WASM_ENABLE_FAST_INTERP != 0
  3090. skip_label();
  3091. #endif
  3092. break;
  3093. case WASM_OP_BLOCK:
  3094. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  3095. block_return_type = read_uint8(p);
  3096. PUSH_CSP(BLOCK_TYPE_BLOCK, block_return_type, p);
  3097. #if WASM_ENABLE_FAST_INTERP != 0
  3098. skip_label();
  3099. #endif
  3100. break;
  3101. case WASM_OP_LOOP:
  3102. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  3103. block_return_type = read_uint8(p);
  3104. PUSH_CSP(BLOCK_TYPE_LOOP, block_return_type, p);
  3105. #if WASM_ENABLE_FAST_INTERP != 0
  3106. skip_label();
  3107. (loader_ctx->frame_csp - 1)->code_compiled =
  3108. loader_ctx->p_code_compiled;
  3109. #endif
  3110. break;
  3111. case WASM_OP_IF:
  3112. POP_I32();
  3113. /* 0x40/0x7F/0x7E/0x7D/0x7C */
  3114. block_return_type = read_uint8(p);
  3115. PUSH_CSP(BLOCK_TYPE_IF, block_return_type, p);
  3116. #if WASM_ENABLE_FAST_INTERP != 0
  3117. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  3118. emit_empty_label_addr_and_frame_ip(PATCH_END);
  3119. #endif
  3120. if (!is_i32_const)
  3121. (loader_ctx->frame_csp - 1)->is_block_reachable = true;
  3122. else {
  3123. if (!i32_const) {
  3124. cache_index = ((uintptr_t)(loader_ctx->frame_csp - 1)->start_addr)
  3125. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3126. cache_items = block_addr_cache +
  3127. BLOCK_ADDR_CONFLICT_SIZE * cache_index;
  3128. for (item_index = 0; item_index < BLOCK_ADDR_CONFLICT_SIZE;
  3129. item_index++) {
  3130. if (cache_items[item_index].start_addr ==
  3131. (loader_ctx->frame_csp - 1)->start_addr) {
  3132. (loader_ctx->frame_csp - 1)->else_addr =
  3133. cache_items[item_index].else_addr;
  3134. (loader_ctx->frame_csp - 1)->end_addr =
  3135. cache_items[item_index].end_addr;
  3136. break;
  3137. }
  3138. }
  3139. if (item_index == BLOCK_ADDR_CONFLICT_SIZE
  3140. && !wasm_loader_find_block_addr(block_addr_cache,
  3141. (loader_ctx->frame_csp - 1)->start_addr,
  3142. p_end,
  3143. (loader_ctx->frame_csp - 1)->block_type,
  3144. &(loader_ctx->frame_csp - 1)->else_addr,
  3145. &(loader_ctx->frame_csp - 1)->end_addr,
  3146. error_buf, error_buf_size))
  3147. goto fail;
  3148. if ((loader_ctx->frame_csp - 1)->else_addr) {
  3149. #if WASM_ENABLE_FAST_INTERP != 0
  3150. loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
  3151. (loader_ctx->frame_csp - 1)->stack_cell_num;
  3152. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  3153. #endif
  3154. p = (loader_ctx->frame_csp - 1)->else_addr + 1;
  3155. }
  3156. else {
  3157. p = (loader_ctx->frame_csp - 1)->end_addr;
  3158. }
  3159. is_i32_const = false;
  3160. continue;
  3161. }
  3162. }
  3163. break;
  3164. case WASM_OP_ELSE:
  3165. if (loader_ctx->csp_num < 2
  3166. || (loader_ctx->frame_csp - 1)->block_type != BLOCK_TYPE_IF) {
  3167. set_error_buf(error_buf, error_buf_size,
  3168. "WASM loader prepare bytecode failed: "
  3169. "opcode else found without matched opcode if");
  3170. goto fail;
  3171. }
  3172. (loader_ctx->frame_csp - 1)->else_addr = p - 1;
  3173. loader_ctx->stack_cell_num = (loader_ctx->frame_csp - 1)->stack_cell_num;
  3174. loader_ctx->frame_ref = loader_ctx->frame_ref_bottom +
  3175. loader_ctx->stack_cell_num;
  3176. #if WASM_ENABLE_FAST_INTERP != 0
  3177. /* if the result of if branch is in local or const area, add a copy op */
  3178. if ((loader_ctx->frame_csp - 1)->return_type != VALUE_TYPE_VOID) {
  3179. uint8 return_cells;
  3180. if ((loader_ctx->frame_csp - 1)->return_type == VALUE_TYPE_I32
  3181. || (loader_ctx->frame_csp - 1)->return_type == VALUE_TYPE_F32)
  3182. return_cells = 1;
  3183. else
  3184. return_cells = 2;
  3185. if ((loader_ctx->frame_csp - 1)->dynamic_offset !=
  3186. *(loader_ctx->frame_offset - return_cells)) {
  3187. skip_label();
  3188. if (return_cells == 1)
  3189. emit_label(EXT_OP_COPY_STACK_TOP);
  3190. else
  3191. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3192. emit_operand(loader_ctx, *(loader_ctx->frame_offset - return_cells));
  3193. emit_operand(loader_ctx, (loader_ctx->frame_csp - 1)->dynamic_offset);
  3194. *(loader_ctx->frame_offset - return_cells) =
  3195. (loader_ctx->frame_csp - 1)->dynamic_offset;
  3196. emit_label(opcode);
  3197. }
  3198. }
  3199. loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
  3200. loader_ctx->stack_cell_num;
  3201. emit_empty_label_addr_and_frame_ip(PATCH_END);
  3202. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  3203. #endif
  3204. break;
  3205. case WASM_OP_END:
  3206. {
  3207. POP_CSP();
  3208. POP_TYPE(loader_ctx->frame_csp->return_type);
  3209. PUSH_TYPE(loader_ctx->frame_csp->return_type);
  3210. #if WASM_ENABLE_FAST_INTERP != 0
  3211. skip_label();
  3212. // copy the result to the block return address
  3213. if (loader_ctx->frame_csp->return_type != VALUE_TYPE_VOID) {
  3214. uint8 return_cells;
  3215. if (loader_ctx->frame_csp->return_type == VALUE_TYPE_I32
  3216. || loader_ctx->frame_csp->return_type == VALUE_TYPE_F32)
  3217. return_cells = 1;
  3218. else
  3219. return_cells = 2;
  3220. if (loader_ctx->frame_csp->dynamic_offset !=
  3221. *(loader_ctx->frame_offset - return_cells)) {
  3222. if (return_cells == 1)
  3223. emit_label(EXT_OP_COPY_STACK_TOP);
  3224. else
  3225. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  3226. emit_operand(loader_ctx, *(loader_ctx->frame_offset - return_cells));
  3227. emit_operand(loader_ctx, loader_ctx->frame_csp->dynamic_offset);
  3228. }
  3229. // the frame_offset stack top should be the return address of the block
  3230. loader_ctx->frame_offset -= return_cells;
  3231. loader_ctx->dynamic_offset = loader_ctx->frame_csp->dynamic_offset;
  3232. PUSH_OFFSET_TYPE(loader_ctx->frame_csp->return_type);
  3233. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  3234. }
  3235. apply_label_patch(loader_ctx, 0, PATCH_END);
  3236. free_label_patch_list(loader_ctx->frame_csp);
  3237. if (loader_ctx->frame_csp->block_type == BLOCK_TYPE_FUNCTION) {
  3238. emit_label(WASM_OP_RETURN);
  3239. POP_OFFSET_TYPE(loader_ctx->frame_csp->return_type);
  3240. }
  3241. #endif
  3242. if (loader_ctx->csp_num > 0) {
  3243. loader_ctx->frame_csp->end_addr = p - 1;
  3244. }
  3245. else {
  3246. /* end of function block, function will return,
  3247. ignore the following bytecodes */
  3248. p = p_end;
  3249. is_i32_const = false;
  3250. continue;
  3251. }
  3252. break;
  3253. }
  3254. case WASM_OP_BR:
  3255. {
  3256. #if WASM_ENABLE_FAST_INTERP != 0
  3257. BranchBlock *frame_csp_tmp;
  3258. #endif
  3259. read_leb_uint32(p, p_end, depth);
  3260. CHECK_BR(depth);
  3261. #if WASM_ENABLE_FAST_INTERP != 0
  3262. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  3263. emit_br_info(frame_csp_tmp);
  3264. #endif
  3265. handle_next_reachable_block:
  3266. for (i = 1; i <= loader_ctx->csp_num; i++)
  3267. if ((loader_ctx->frame_csp - i)->is_block_reachable)
  3268. break;
  3269. block_return_type = (loader_ctx->frame_csp - i)->return_type;
  3270. cache_index = ((uintptr_t)(loader_ctx->frame_csp - i)->start_addr)
  3271. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  3272. cache_items = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * cache_index;
  3273. for (item_index = 0; item_index < BLOCK_ADDR_CONFLICT_SIZE; item_index++) {
  3274. if (cache_items[item_index].start_addr == (loader_ctx->frame_csp - i)->start_addr) {
  3275. (loader_ctx->frame_csp - i)->else_addr = cache_items[item_index].else_addr;
  3276. (loader_ctx->frame_csp - i)->end_addr = cache_items[item_index].end_addr;
  3277. break;
  3278. }
  3279. }
  3280. if(item_index == BLOCK_ADDR_CONFLICT_SIZE
  3281. && !wasm_loader_find_block_addr(block_addr_cache,
  3282. (loader_ctx->frame_csp - i)->start_addr,
  3283. p_end,
  3284. (loader_ctx->frame_csp - i)->block_type,
  3285. &(loader_ctx->frame_csp - i)->else_addr,
  3286. &(loader_ctx->frame_csp - i)->end_addr,
  3287. error_buf, error_buf_size))
  3288. goto fail;
  3289. loader_ctx->stack_cell_num = (loader_ctx->frame_csp - i)->stack_cell_num;
  3290. loader_ctx->frame_ref = loader_ctx->frame_ref_bottom +
  3291. loader_ctx->stack_cell_num;
  3292. loader_ctx->csp_num -= i - 1;
  3293. loader_ctx->frame_csp -= i - 1;
  3294. if ((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF
  3295. && (loader_ctx->frame_csp - 1)->else_addr != NULL
  3296. && p <= (loader_ctx->frame_csp - 1)->else_addr) {
  3297. #if WASM_ENABLE_FAST_INTERP != 0
  3298. loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
  3299. (loader_ctx->frame_csp - 1)->stack_cell_num;
  3300. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  3301. #endif
  3302. p = (loader_ctx->frame_csp - 1)->else_addr + 1;
  3303. }
  3304. else {
  3305. p = (loader_ctx->frame_csp - 1)->end_addr;
  3306. PUSH_TYPE(block_return_type);
  3307. #if WASM_ENABLE_FAST_INTERP != 0
  3308. loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
  3309. loader_ctx->stack_cell_num;
  3310. #endif
  3311. }
  3312. is_i32_const = false;
  3313. continue;
  3314. }
  3315. case WASM_OP_BR_IF:
  3316. {
  3317. #if WASM_ENABLE_FAST_INTERP != 0
  3318. BranchBlock *frame_csp_tmp;
  3319. #endif
  3320. read_leb_uint32(p, p_end, depth);
  3321. POP_I32();
  3322. CHECK_BR(depth);
  3323. #if WASM_ENABLE_FAST_INTERP != 0
  3324. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  3325. emit_br_info(frame_csp_tmp);
  3326. #endif
  3327. if (!is_i32_const)
  3328. (loader_ctx->frame_csp - (depth + 1))->is_block_reachable = true;
  3329. else {
  3330. if (i32_const)
  3331. goto handle_next_reachable_block;
  3332. }
  3333. break;
  3334. }
  3335. case WASM_OP_BR_TABLE:
  3336. {
  3337. #if WASM_ENABLE_FAST_INTERP != 0
  3338. BranchBlock *frame_csp_tmp;
  3339. #endif
  3340. read_leb_uint32(p, p_end, count);
  3341. #if WASM_ENABLE_FAST_INTERP != 0
  3342. emit_const(count);
  3343. #endif
  3344. POP_I32();
  3345. /* TODO: check the const */
  3346. for (i = 0; i <= count; i++) {
  3347. read_leb_uint32(p, p_end, depth);
  3348. CHECK_BR(depth);
  3349. #if WASM_ENABLE_FAST_INTERP != 0
  3350. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  3351. emit_br_info(frame_csp_tmp);
  3352. #endif
  3353. }
  3354. goto handle_next_reachable_block;
  3355. }
  3356. case WASM_OP_RETURN:
  3357. {
  3358. POP_TYPE(ret_type);
  3359. PUSH_TYPE(ret_type);
  3360. #if WASM_ENABLE_FAST_INTERP != 0
  3361. // emit the offset after return opcode
  3362. POP_OFFSET_TYPE(ret_type);
  3363. loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
  3364. loader_ctx->stack_cell_num;
  3365. #endif
  3366. is_i32_const = false;
  3367. goto handle_next_reachable_block;
  3368. }
  3369. case WASM_OP_CALL:
  3370. {
  3371. WASMType *func_type;
  3372. uint32 func_idx;
  3373. int32 idx;
  3374. read_leb_uint32(p, p_end, func_idx);
  3375. #if WASM_ENABLE_FAST_INTERP != 0
  3376. // we need to emit func_idx before arguments
  3377. emit_const(func_idx);
  3378. #endif
  3379. if (func_idx >= module->import_function_count + module->function_count) {
  3380. set_error_buf(error_buf, error_buf_size,
  3381. "WASM loader prepare bytecode failed: "
  3382. "function index out of range");
  3383. goto fail;
  3384. }
  3385. if (func_idx < module->import_function_count)
  3386. func_type = module->import_functions[func_idx].u.function.func_type;
  3387. else
  3388. func_type =
  3389. module->functions[func_idx - module->import_function_count]->func_type;
  3390. if (func_type->param_count > 0) {
  3391. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  3392. POP_TYPE(func_type->types[idx]);
  3393. #if WASM_ENABLE_FAST_INTERP != 0
  3394. POP_OFFSET_TYPE(func_type->types[idx]);
  3395. #endif
  3396. }
  3397. }
  3398. if (func_type->result_count) {
  3399. PUSH_TYPE(func_type->types[func_type->param_count]);
  3400. #if WASM_ENABLE_FAST_INTERP != 0
  3401. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count]);
  3402. #endif
  3403. }
  3404. func->has_op_func_call = true;
  3405. break;
  3406. }
  3407. case WASM_OP_CALL_INDIRECT:
  3408. {
  3409. int32 idx;
  3410. WASMType *func_type;
  3411. uint32 type_idx;
  3412. if (module->table_count == 0
  3413. && module->import_table_count == 0) {
  3414. set_error_buf(error_buf, error_buf_size,
  3415. "WASM loader prepare bytecode failed: "
  3416. "call indirect without default table");
  3417. goto fail;
  3418. }
  3419. read_leb_uint32(p, p_end, type_idx);
  3420. #if WASM_ENABLE_FAST_INTERP != 0
  3421. // we need to emit func_idx before arguments
  3422. emit_const(type_idx);
  3423. #endif
  3424. /* reserved byte 0x00 */
  3425. if (*p++ != 0x00) {
  3426. set_error_buf(error_buf, error_buf_size,
  3427. "WASM loader prepare bytecode failed: "
  3428. "zero flag expected");
  3429. goto fail;
  3430. }
  3431. POP_I32();
  3432. if (type_idx >= module->type_count) {
  3433. set_error_buf(error_buf, error_buf_size,
  3434. "WASM loader prepare bytecode failed: "
  3435. "function index out of range");
  3436. goto fail;
  3437. }
  3438. func_type = module->types[type_idx];
  3439. if (func_type->param_count > 0) {
  3440. for (idx = (int32)(func_type->param_count - 1); idx >= 0; idx--) {
  3441. POP_TYPE(func_type->types[idx]);
  3442. #if WASM_ENABLE_FAST_INTERP != 0
  3443. POP_OFFSET_TYPE(func_type->types[idx]);
  3444. #endif
  3445. }
  3446. }
  3447. if (func_type->result_count > 0) {
  3448. PUSH_TYPE(func_type->types[func_type->param_count]);
  3449. #if WASM_ENABLE_FAST_INTERP != 0
  3450. PUSH_OFFSET_TYPE(func_type->types[func_type->param_count]);
  3451. #endif
  3452. }
  3453. func->has_op_func_call = true;
  3454. break;
  3455. }
  3456. case WASM_OP_DROP:
  3457. case WASM_OP_DROP_64:
  3458. {
  3459. if (loader_ctx->stack_cell_num <= 0) {
  3460. set_error_buf(error_buf, error_buf_size,
  3461. "WASM loader prepare bytecode failed: "
  3462. "opcode drop was found but stack was empty");
  3463. goto fail;
  3464. }
  3465. if (*(loader_ctx->frame_ref - 1) == REF_I32
  3466. || *(loader_ctx->frame_ref - 1) == REF_F32) {
  3467. loader_ctx->frame_ref--;
  3468. loader_ctx->stack_cell_num--;
  3469. #if WASM_ENABLE_FAST_INTERP != 0
  3470. skip_label();
  3471. loader_ctx->frame_offset--;
  3472. if (*(loader_ctx->frame_offset) >
  3473. loader_ctx->start_dynamic_offset)
  3474. loader_ctx->dynamic_offset --;
  3475. #endif
  3476. }
  3477. else {
  3478. if (loader_ctx->stack_cell_num <= 1) {
  3479. set_error_buf(error_buf, error_buf_size,
  3480. "WASM loader prepare bytecode failed: "
  3481. "opcode drop was found but stack was empty");
  3482. goto fail;
  3483. }
  3484. loader_ctx->frame_ref -= 2;
  3485. loader_ctx->stack_cell_num -= 2;
  3486. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  3487. *(p - 1) = WASM_OP_DROP_64;
  3488. #endif
  3489. #if WASM_ENABLE_FAST_INTERP != 0
  3490. skip_label();
  3491. loader_ctx->frame_offset -= 2;
  3492. if (*(loader_ctx->frame_offset) >
  3493. loader_ctx->start_dynamic_offset)
  3494. loader_ctx->dynamic_offset -= 2;
  3495. #endif
  3496. }
  3497. break;
  3498. }
  3499. case WASM_OP_SELECT:
  3500. case WASM_OP_SELECT_64:
  3501. {
  3502. uint8 ref_type;
  3503. POP_I32();
  3504. if (loader_ctx->stack_cell_num <= 0) {
  3505. set_error_buf(error_buf, error_buf_size,
  3506. "WASM loader prepare bytecode failed: "
  3507. "opcode select was found but stack was empty");
  3508. goto fail;
  3509. }
  3510. switch (*(loader_ctx->frame_ref - 1)) {
  3511. case REF_I32:
  3512. case REF_F32:
  3513. break;
  3514. case REF_I64_2:
  3515. case REF_F64_2:
  3516. #if (WASM_ENABLE_FAST_INTERP == 0) || (WASM_ENABLE_JIT != 0)
  3517. *(p - 1) = WASM_OP_SELECT_64;
  3518. #endif
  3519. #if WASM_ENABLE_FAST_INTERP != 0
  3520. if (loader_ctx->p_code_compiled) {
  3521. #if WASM_ENABLE_ABS_LABEL_ADDR != 0
  3522. *(void**)(loader_ctx->p_code_compiled - 10) =
  3523. handle_table[WASM_OP_SELECT_64];
  3524. #else
  3525. *((int16*)loader_ctx->p_code_compiled - 2) = (int16)
  3526. (handle_table[WASM_OP_SELECT_64] - handle_table[0]);
  3527. #endif
  3528. }
  3529. #endif
  3530. break;
  3531. }
  3532. ref_type = *(loader_ctx->frame_ref - 1);
  3533. POP_TYPE(ref_type);
  3534. POP_TYPE(ref_type);
  3535. PUSH_TYPE(ref_type);
  3536. #if WASM_ENABLE_FAST_INTERP != 0
  3537. POP_OFFSET_TYPE(ref_type);
  3538. POP_OFFSET_TYPE(ref_type);
  3539. PUSH_OFFSET_TYPE(ref_type);
  3540. #endif
  3541. break;
  3542. }
  3543. case WASM_OP_GET_LOCAL:
  3544. {
  3545. p_org = p - 1;
  3546. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3547. PUSH_TYPE(local_type);
  3548. #if WASM_ENABLE_FAST_INTERP != 0
  3549. /* Get Local is optimized out */
  3550. skip_label();
  3551. disable_emit = true;
  3552. operand_offset = local_offset;
  3553. PUSH_OFFSET_TYPE(local_type);
  3554. #else
  3555. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  3556. if (local_offset < 0x80) {
  3557. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  3558. if (local_type == VALUE_TYPE_I32
  3559. || local_type == VALUE_TYPE_F32)
  3560. *p_org++ = (uint8)local_offset;
  3561. else
  3562. *p_org++ = (uint8)(local_offset | 0x80);
  3563. while (p_org < p)
  3564. *p_org++ = WASM_OP_NOP;
  3565. }
  3566. #endif
  3567. #endif
  3568. break;
  3569. }
  3570. case WASM_OP_SET_LOCAL:
  3571. {
  3572. p_org = p - 1;
  3573. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3574. POP_TYPE(local_type);
  3575. #if WASM_ENABLE_FAST_INTERP != 0
  3576. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  3577. local_type, &preserve_local,
  3578. error_buf, error_buf_size)))
  3579. goto fail;
  3580. if (local_offset < 256) {
  3581. skip_label();
  3582. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  3583. if (loader_ctx->p_code_compiled)
  3584. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  3585. loader_ctx->frame_offset --;
  3586. loader_ctx->dynamic_offset --;
  3587. }
  3588. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  3589. if (loader_ctx->p_code_compiled)
  3590. *(int16*)(loader_ctx->p_code_compiled - 2) = local_offset;
  3591. loader_ctx->frame_offset -= 2;
  3592. loader_ctx->dynamic_offset -= 2;
  3593. }
  3594. else {
  3595. if (local_type == VALUE_TYPE_I32
  3596. || local_type == VALUE_TYPE_F32) {
  3597. emit_label(EXT_OP_SET_LOCAL_FAST);
  3598. emit_byte(loader_ctx, local_offset);
  3599. }
  3600. else {
  3601. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  3602. emit_byte(loader_ctx, local_offset);
  3603. }
  3604. POP_OFFSET_TYPE(local_type);
  3605. }
  3606. }
  3607. else { /* local index larger than 255, reserve leb */
  3608. p_org ++;
  3609. emit_leb();
  3610. POP_OFFSET_TYPE(local_type);
  3611. }
  3612. #else
  3613. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  3614. if (local_offset < 0x80) {
  3615. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  3616. if (local_type == VALUE_TYPE_I32
  3617. || local_type == VALUE_TYPE_F32)
  3618. *p_org++ = (uint8)local_offset;
  3619. else
  3620. *p_org++ = (uint8)(local_offset | 0x80);
  3621. while (p_org < p)
  3622. *p_org++ = WASM_OP_NOP;
  3623. }
  3624. #endif
  3625. #endif
  3626. break;
  3627. }
  3628. case WASM_OP_TEE_LOCAL:
  3629. {
  3630. p_org = p - 1;
  3631. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  3632. POP_TYPE(local_type);
  3633. PUSH_TYPE(local_type);
  3634. #if WASM_ENABLE_FAST_INTERP != 0
  3635. if (!(preserve_referenced_local(loader_ctx, opcode, local_offset,
  3636. local_type, &preserve_local,
  3637. error_buf, error_buf_size)))
  3638. goto fail;
  3639. if (local_offset < 256) {
  3640. skip_label();
  3641. if (local_type == VALUE_TYPE_I32
  3642. || local_type == VALUE_TYPE_F32) {
  3643. emit_label(EXT_OP_TEE_LOCAL_FAST);
  3644. emit_byte(loader_ctx, local_offset);
  3645. }
  3646. else {
  3647. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  3648. emit_byte(loader_ctx, local_offset);
  3649. }
  3650. }
  3651. else { /* local index larger than 255, reserve leb */
  3652. p_org ++;
  3653. emit_leb();
  3654. }
  3655. emit_operand(loader_ctx, *(loader_ctx->frame_offset -
  3656. wasm_value_type_cell_num(local_type)));
  3657. #else
  3658. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  3659. if (local_offset < 0x80) {
  3660. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  3661. if (local_type == VALUE_TYPE_I32
  3662. || local_type == VALUE_TYPE_F32)
  3663. *p_org++ = (uint8)local_offset;
  3664. else
  3665. *p_org++ = (uint8)(local_offset | 0x80);
  3666. while (p_org < p)
  3667. *p_org++ = WASM_OP_NOP;
  3668. }
  3669. #endif
  3670. #endif
  3671. break;
  3672. }
  3673. case WASM_OP_GET_GLOBAL:
  3674. {
  3675. read_leb_uint32(p, p_end, global_idx);
  3676. if (global_idx >= global_count) {
  3677. set_error_buf(error_buf, error_buf_size,
  3678. "WASM loader prepare bytecode failed: "
  3679. "global index out of range");
  3680. goto fail;
  3681. }
  3682. global_type = global_idx < module->import_global_count
  3683. ? module->import_globals[global_idx].u.global.type
  3684. :module->globals[global_idx - module->import_global_count].type;
  3685. PUSH_TYPE(global_type);
  3686. #if WASM_ENABLE_FAST_INTERP != 0
  3687. emit_const(global_idx);
  3688. PUSH_OFFSET_TYPE(global_type);
  3689. #endif
  3690. break;
  3691. }
  3692. case WASM_OP_SET_GLOBAL:
  3693. {
  3694. read_leb_uint32(p, p_end, global_idx);
  3695. if (global_idx >= global_count) {
  3696. set_error_buf(error_buf, error_buf_size,
  3697. "WASM loader prepare bytecode failed: "
  3698. "global index out of range");
  3699. goto fail;
  3700. }
  3701. global_type = global_idx < module->import_global_count
  3702. ? module->import_globals[global_idx].u.global.type
  3703. : module->globals[global_idx - module->import_global_count].type;
  3704. POP_TYPE(global_type);
  3705. #if WASM_ENABLE_FAST_INTERP != 0
  3706. emit_const(global_idx);
  3707. POP_OFFSET_TYPE(global_type);
  3708. #endif
  3709. break;
  3710. }
  3711. /* load */
  3712. case WASM_OP_I32_LOAD:
  3713. case WASM_OP_I32_LOAD8_S:
  3714. case WASM_OP_I32_LOAD8_U:
  3715. case WASM_OP_I32_LOAD16_S:
  3716. case WASM_OP_I32_LOAD16_U:
  3717. case WASM_OP_I64_LOAD:
  3718. case WASM_OP_I64_LOAD8_S:
  3719. case WASM_OP_I64_LOAD8_U:
  3720. case WASM_OP_I64_LOAD16_S:
  3721. case WASM_OP_I64_LOAD16_U:
  3722. case WASM_OP_I64_LOAD32_S:
  3723. case WASM_OP_I64_LOAD32_U:
  3724. case WASM_OP_F32_LOAD:
  3725. case WASM_OP_F64_LOAD:
  3726. /* store */
  3727. case WASM_OP_I32_STORE:
  3728. case WASM_OP_I32_STORE8:
  3729. case WASM_OP_I32_STORE16:
  3730. case WASM_OP_I64_STORE:
  3731. case WASM_OP_I64_STORE8:
  3732. case WASM_OP_I64_STORE16:
  3733. case WASM_OP_I64_STORE32:
  3734. case WASM_OP_F32_STORE:
  3735. case WASM_OP_F64_STORE:
  3736. {
  3737. #if WASM_ENABLE_FAST_INTERP != 0
  3738. /* change F32/F64 into I32/I64 */
  3739. if (opcode == WASM_OP_F32_LOAD) {
  3740. skip_label();
  3741. emit_label(WASM_OP_I32_LOAD);
  3742. }
  3743. else if (opcode == WASM_OP_F64_LOAD) {
  3744. skip_label();
  3745. emit_label(WASM_OP_I64_LOAD);
  3746. }
  3747. else if (opcode == WASM_OP_F32_STORE) {
  3748. skip_label();
  3749. emit_label(WASM_OP_I32_STORE);
  3750. }
  3751. else if (opcode == WASM_OP_F64_STORE) {
  3752. skip_label();
  3753. emit_label(WASM_OP_I64_STORE);
  3754. }
  3755. #endif
  3756. CHECK_MEMORY();
  3757. read_leb_uint32(p, p_end, align); /* align */
  3758. read_leb_uint32(p, p_end, mem_offset); /* offset */
  3759. #if WASM_ENABLE_FAST_INTERP != 0
  3760. emit_const(mem_offset);
  3761. #endif
  3762. switch (opcode)
  3763. {
  3764. /* load */
  3765. case WASM_OP_I32_LOAD:
  3766. case WASM_OP_I32_LOAD8_S:
  3767. case WASM_OP_I32_LOAD8_U:
  3768. case WASM_OP_I32_LOAD16_S:
  3769. case WASM_OP_I32_LOAD16_U:
  3770. POP_I32();
  3771. PUSH_I32();
  3772. break;
  3773. case WASM_OP_I64_LOAD:
  3774. case WASM_OP_I64_LOAD8_S:
  3775. case WASM_OP_I64_LOAD8_U:
  3776. case WASM_OP_I64_LOAD16_S:
  3777. case WASM_OP_I64_LOAD16_U:
  3778. case WASM_OP_I64_LOAD32_S:
  3779. case WASM_OP_I64_LOAD32_U:
  3780. POP_I32();
  3781. PUSH_I64();
  3782. break;
  3783. case WASM_OP_F32_LOAD:
  3784. POP_I32();
  3785. PUSH_F32();
  3786. break;
  3787. case WASM_OP_F64_LOAD:
  3788. POP_I32();
  3789. PUSH_F64();
  3790. break;
  3791. /* store */
  3792. case WASM_OP_I32_STORE:
  3793. case WASM_OP_I32_STORE8:
  3794. case WASM_OP_I32_STORE16:
  3795. POP_I32();
  3796. POP_I32();
  3797. break;
  3798. case WASM_OP_I64_STORE:
  3799. case WASM_OP_I64_STORE8:
  3800. case WASM_OP_I64_STORE16:
  3801. case WASM_OP_I64_STORE32:
  3802. POP_I64();
  3803. POP_I32();
  3804. break;
  3805. case WASM_OP_F32_STORE:
  3806. POP_F32();
  3807. POP_I32();
  3808. break;
  3809. case WASM_OP_F64_STORE:
  3810. POP_F64();
  3811. POP_I32();
  3812. break;
  3813. default:
  3814. break;
  3815. }
  3816. break;
  3817. }
  3818. case WASM_OP_MEMORY_SIZE:
  3819. CHECK_MEMORY();
  3820. /* reserved byte 0x00 */
  3821. if (*p++ != 0x00) {
  3822. set_error_buf(error_buf, error_buf_size,
  3823. "WASM loader prepare bytecode failed: "
  3824. "zero flag expected");
  3825. goto fail;
  3826. }
  3827. PUSH_I32();
  3828. break;
  3829. case WASM_OP_MEMORY_GROW:
  3830. CHECK_MEMORY();
  3831. /* reserved byte 0x00 */
  3832. if (*p++ != 0x00) {
  3833. set_error_buf(error_buf, error_buf_size,
  3834. "WASM loader prepare bytecode failed: "
  3835. "zero flag expected");
  3836. goto fail;
  3837. }
  3838. POP_I32();
  3839. PUSH_I32();
  3840. func->has_op_memory_grow = true;
  3841. module->possible_memory_grow = true;
  3842. break;
  3843. case WASM_OP_I32_CONST:
  3844. read_leb_int32(p, p_end, i32_const);
  3845. /* Currently we only track simple I32_CONST opcode. */
  3846. is_i32_const = true;
  3847. #if WASM_ENABLE_FAST_INTERP != 0
  3848. skip_label();
  3849. disable_emit = true;
  3850. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  3851. #endif
  3852. PUSH_I32();
  3853. break;
  3854. case WASM_OP_I64_CONST:
  3855. read_leb_int64(p, p_end, i64);
  3856. #if WASM_ENABLE_FAST_INTERP != 0
  3857. skip_label();
  3858. disable_emit = true;
  3859. GET_CONST_OFFSET(VALUE_TYPE_I64, i64);
  3860. #endif
  3861. PUSH_I64();
  3862. break;
  3863. case WASM_OP_F32_CONST:
  3864. p += sizeof(float32);
  3865. #if WASM_ENABLE_FAST_INTERP != 0
  3866. skip_label();
  3867. disable_emit = true;
  3868. f32 = *(float32 *)p_org;
  3869. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32);
  3870. #endif
  3871. PUSH_F32();
  3872. break;
  3873. case WASM_OP_F64_CONST:
  3874. p += sizeof(float64);
  3875. #if WASM_ENABLE_FAST_INTERP != 0
  3876. skip_label();
  3877. disable_emit = true;
  3878. /* Some MCU may require 8-byte align */
  3879. memcpy((uint8*)&f64, p_org, sizeof(float64));
  3880. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64);
  3881. #endif
  3882. PUSH_F64();
  3883. break;
  3884. case WASM_OP_I32_EQZ:
  3885. POP_I32();
  3886. PUSH_I32();
  3887. break;
  3888. case WASM_OP_I32_EQ:
  3889. case WASM_OP_I32_NE:
  3890. case WASM_OP_I32_LT_S:
  3891. case WASM_OP_I32_LT_U:
  3892. case WASM_OP_I32_GT_S:
  3893. case WASM_OP_I32_GT_U:
  3894. case WASM_OP_I32_LE_S:
  3895. case WASM_OP_I32_LE_U:
  3896. case WASM_OP_I32_GE_S:
  3897. case WASM_OP_I32_GE_U:
  3898. POP_I32();
  3899. POP_I32();
  3900. PUSH_I32();
  3901. break;
  3902. case WASM_OP_I64_EQZ:
  3903. POP_I64();
  3904. PUSH_I32();
  3905. break;
  3906. case WASM_OP_I64_EQ:
  3907. case WASM_OP_I64_NE:
  3908. case WASM_OP_I64_LT_S:
  3909. case WASM_OP_I64_LT_U:
  3910. case WASM_OP_I64_GT_S:
  3911. case WASM_OP_I64_GT_U:
  3912. case WASM_OP_I64_LE_S:
  3913. case WASM_OP_I64_LE_U:
  3914. case WASM_OP_I64_GE_S:
  3915. case WASM_OP_I64_GE_U:
  3916. POP_I64();
  3917. POP_I64();
  3918. PUSH_I32();
  3919. break;
  3920. case WASM_OP_F32_EQ:
  3921. case WASM_OP_F32_NE:
  3922. case WASM_OP_F32_LT:
  3923. case WASM_OP_F32_GT:
  3924. case WASM_OP_F32_LE:
  3925. case WASM_OP_F32_GE:
  3926. POP_F32();
  3927. POP_F32();
  3928. PUSH_I32();
  3929. break;
  3930. case WASM_OP_F64_EQ:
  3931. case WASM_OP_F64_NE:
  3932. case WASM_OP_F64_LT:
  3933. case WASM_OP_F64_GT:
  3934. case WASM_OP_F64_LE:
  3935. case WASM_OP_F64_GE:
  3936. POP_F64();
  3937. POP_F64();
  3938. PUSH_I32();
  3939. break;
  3940. break;
  3941. case WASM_OP_I32_CLZ:
  3942. case WASM_OP_I32_CTZ:
  3943. case WASM_OP_I32_POPCNT:
  3944. POP_I32();
  3945. PUSH_I32();
  3946. break;
  3947. case WASM_OP_I32_ADD:
  3948. case WASM_OP_I32_SUB:
  3949. case WASM_OP_I32_MUL:
  3950. case WASM_OP_I32_DIV_S:
  3951. case WASM_OP_I32_DIV_U:
  3952. case WASM_OP_I32_REM_S:
  3953. case WASM_OP_I32_REM_U:
  3954. case WASM_OP_I32_AND:
  3955. case WASM_OP_I32_OR:
  3956. case WASM_OP_I32_XOR:
  3957. case WASM_OP_I32_SHL:
  3958. case WASM_OP_I32_SHR_S:
  3959. case WASM_OP_I32_SHR_U:
  3960. case WASM_OP_I32_ROTL:
  3961. case WASM_OP_I32_ROTR:
  3962. POP_I32();
  3963. POP_I32();
  3964. PUSH_I32();
  3965. break;
  3966. case WASM_OP_I64_CLZ:
  3967. case WASM_OP_I64_CTZ:
  3968. case WASM_OP_I64_POPCNT:
  3969. POP_I64();
  3970. PUSH_I64();
  3971. break;
  3972. case WASM_OP_I64_ADD:
  3973. case WASM_OP_I64_SUB:
  3974. case WASM_OP_I64_MUL:
  3975. case WASM_OP_I64_DIV_S:
  3976. case WASM_OP_I64_DIV_U:
  3977. case WASM_OP_I64_REM_S:
  3978. case WASM_OP_I64_REM_U:
  3979. case WASM_OP_I64_AND:
  3980. case WASM_OP_I64_OR:
  3981. case WASM_OP_I64_XOR:
  3982. case WASM_OP_I64_SHL:
  3983. case WASM_OP_I64_SHR_S:
  3984. case WASM_OP_I64_SHR_U:
  3985. case WASM_OP_I64_ROTL:
  3986. case WASM_OP_I64_ROTR:
  3987. POP_I64();
  3988. POP_I64();
  3989. PUSH_I64();
  3990. break;
  3991. case WASM_OP_F32_ABS:
  3992. case WASM_OP_F32_NEG:
  3993. case WASM_OP_F32_CEIL:
  3994. case WASM_OP_F32_FLOOR:
  3995. case WASM_OP_F32_TRUNC:
  3996. case WASM_OP_F32_NEAREST:
  3997. case WASM_OP_F32_SQRT:
  3998. POP_F32();
  3999. PUSH_F32();
  4000. break;
  4001. case WASM_OP_F32_ADD:
  4002. case WASM_OP_F32_SUB:
  4003. case WASM_OP_F32_MUL:
  4004. case WASM_OP_F32_DIV:
  4005. case WASM_OP_F32_MIN:
  4006. case WASM_OP_F32_MAX:
  4007. case WASM_OP_F32_COPYSIGN:
  4008. POP_F32();
  4009. POP_F32();
  4010. PUSH_F32();
  4011. break;
  4012. case WASM_OP_F64_ABS:
  4013. case WASM_OP_F64_NEG:
  4014. case WASM_OP_F64_CEIL:
  4015. case WASM_OP_F64_FLOOR:
  4016. case WASM_OP_F64_TRUNC:
  4017. case WASM_OP_F64_NEAREST:
  4018. case WASM_OP_F64_SQRT:
  4019. POP_F64();
  4020. PUSH_F64();
  4021. break;
  4022. case WASM_OP_F64_ADD:
  4023. case WASM_OP_F64_SUB:
  4024. case WASM_OP_F64_MUL:
  4025. case WASM_OP_F64_DIV:
  4026. case WASM_OP_F64_MIN:
  4027. case WASM_OP_F64_MAX:
  4028. case WASM_OP_F64_COPYSIGN:
  4029. POP_F64();
  4030. POP_F64();
  4031. PUSH_F64();
  4032. break;
  4033. case WASM_OP_I32_WRAP_I64:
  4034. POP_I64();
  4035. PUSH_I32();
  4036. break;
  4037. case WASM_OP_I32_TRUNC_S_F32:
  4038. case WASM_OP_I32_TRUNC_U_F32:
  4039. POP_F32();
  4040. PUSH_I32();
  4041. break;
  4042. case WASM_OP_I32_TRUNC_S_F64:
  4043. case WASM_OP_I32_TRUNC_U_F64:
  4044. POP_F64();
  4045. PUSH_I32();
  4046. break;
  4047. case WASM_OP_I64_EXTEND_S_I32:
  4048. case WASM_OP_I64_EXTEND_U_I32:
  4049. POP_I32();
  4050. PUSH_I64();
  4051. break;
  4052. case WASM_OP_I64_TRUNC_S_F32:
  4053. case WASM_OP_I64_TRUNC_U_F32:
  4054. POP_F32();
  4055. PUSH_I64();
  4056. break;
  4057. case WASM_OP_I64_TRUNC_S_F64:
  4058. case WASM_OP_I64_TRUNC_U_F64:
  4059. POP_F64();
  4060. PUSH_I64();
  4061. break;
  4062. case WASM_OP_F32_CONVERT_S_I32:
  4063. case WASM_OP_F32_CONVERT_U_I32:
  4064. POP_I32();
  4065. PUSH_F32();
  4066. break;
  4067. case WASM_OP_F32_CONVERT_S_I64:
  4068. case WASM_OP_F32_CONVERT_U_I64:
  4069. POP_I64();
  4070. PUSH_F32();
  4071. break;
  4072. case WASM_OP_F32_DEMOTE_F64:
  4073. POP_F64();
  4074. PUSH_F32();
  4075. break;
  4076. case WASM_OP_F64_CONVERT_S_I32:
  4077. case WASM_OP_F64_CONVERT_U_I32:
  4078. POP_I32();
  4079. PUSH_F64();
  4080. break;
  4081. case WASM_OP_F64_CONVERT_S_I64:
  4082. case WASM_OP_F64_CONVERT_U_I64:
  4083. POP_I64();
  4084. PUSH_F64();
  4085. break;
  4086. case WASM_OP_F64_PROMOTE_F32:
  4087. POP_F32();
  4088. PUSH_F64();
  4089. break;
  4090. case WASM_OP_I32_REINTERPRET_F32:
  4091. POP_F32();
  4092. PUSH_I32();
  4093. break;
  4094. case WASM_OP_I64_REINTERPRET_F64:
  4095. POP_F64();
  4096. PUSH_I64();
  4097. break;
  4098. case WASM_OP_F32_REINTERPRET_I32:
  4099. POP_I32();
  4100. PUSH_F32();
  4101. break;
  4102. case WASM_OP_F64_REINTERPRET_I64:
  4103. POP_I64();
  4104. PUSH_F64();
  4105. break;
  4106. default:
  4107. if (error_buf != NULL)
  4108. snprintf(error_buf, error_buf_size,
  4109. "WASM module load failed: "
  4110. "invalid opcode %02x.", opcode);
  4111. goto fail;
  4112. }
  4113. if (opcode != WASM_OP_I32_CONST)
  4114. is_i32_const = false;
  4115. #if WASM_ENABLE_FAST_INTERP != 0
  4116. last_op = opcode;
  4117. #endif
  4118. }
  4119. if (loader_ctx->csp_num > 0) {
  4120. set_error_buf(error_buf, error_buf_size,
  4121. "WASM module load failed: "
  4122. "function body must end with END opcode.");
  4123. goto fail;
  4124. }
  4125. #if WASM_ENABLE_FAST_INTERP != 0
  4126. if (loader_ctx->p_code_compiled == NULL)
  4127. goto re_scan;
  4128. func->const_cell_num = loader_ctx->const_cell_num;
  4129. if (!(func->consts = func_const =
  4130. wasm_runtime_malloc(func->const_cell_num * 4))) {
  4131. set_error_buf(error_buf, error_buf_size,
  4132. "WASM loader prepare bytecode failed: "
  4133. "allocate memory failed");
  4134. goto fail;
  4135. }
  4136. memset(func->consts, 0, func->const_cell_num * 4);
  4137. func_const_end = func->consts + func->const_cell_num * 4;
  4138. // reverse the const buf
  4139. for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
  4140. Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
  4141. if (c->value_type == VALUE_TYPE_F64
  4142. || c->value_type == VALUE_TYPE_I64) {
  4143. bh_memcpy_s(func_const, func_const_end - func_const,
  4144. &c->value.f64, sizeof(int64));
  4145. func_const += sizeof(int64);
  4146. } else {
  4147. *(uint32*)func_const = c->value.i32;
  4148. func_const += sizeof(int32);
  4149. }
  4150. }
  4151. func->max_stack_cell_num = loader_ctx->max_dynamic_offset -
  4152. loader_ctx->start_dynamic_offset + 1;
  4153. #else
  4154. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  4155. #endif
  4156. func->max_block_num = loader_ctx->max_csp_num;
  4157. return_value = true;
  4158. fail:
  4159. wasm_loader_ctx_destroy(loader_ctx);
  4160. (void)u8;
  4161. (void)u32;
  4162. (void)i32;
  4163. (void)i64;
  4164. (void)local_offset;
  4165. (void)p_org;
  4166. (void)mem_offset;
  4167. (void)align;
  4168. return return_value;
  4169. }