transformation_add_function_test.cpp 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/fuzz/transformation_add_function.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_message.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. protobufs::AccessChainClampingInfo MakeAccessClampingInfo(
  23. uint32_t access_chain_id,
  24. const std::vector<std::pair<uint32_t, uint32_t>>& compare_and_select_ids) {
  25. protobufs::AccessChainClampingInfo result;
  26. result.set_access_chain_id(access_chain_id);
  27. for (auto& compare_and_select_id : compare_and_select_ids) {
  28. auto pair = result.add_compare_and_select_ids();
  29. pair->set_first(compare_and_select_id.first);
  30. pair->set_second(compare_and_select_id.second);
  31. }
  32. return result;
  33. }
  34. std::vector<protobufs::Instruction> GetInstructionsForFunction(
  35. spv_target_env env, const MessageConsumer& consumer,
  36. const std::string& donor, uint32_t function_id) {
  37. std::vector<protobufs::Instruction> result;
  38. const auto donor_context =
  39. BuildModule(env, consumer, donor, kFuzzAssembleOption);
  40. spvtools::ValidatorOptions validator_options;
  41. assert(fuzzerutil::IsValidAndWellFormed(
  42. donor_context.get(), validator_options, kConsoleMessageConsumer) &&
  43. "The given donor must be valid.");
  44. for (auto& function : *donor_context->module()) {
  45. if (function.result_id() == function_id) {
  46. function.ForEachInst([&result](opt::Instruction* inst) {
  47. opt::Instruction::OperandList input_operands;
  48. for (uint32_t i = 0; i < inst->NumInOperands(); i++) {
  49. input_operands.push_back(inst->GetInOperand(i));
  50. }
  51. result.push_back(MakeInstructionMessage(inst->opcode(), inst->type_id(),
  52. inst->result_id(),
  53. input_operands));
  54. });
  55. break;
  56. }
  57. }
  58. assert(!result.empty() && "The required function should have been found.");
  59. return result;
  60. }
  61. // Returns true if and only if every pointer parameter and variable associated
  62. // with |function_id| in |context| is known by |transformation_context| to be
  63. // irrelevant, with the exception of |loop_limiter_id|, which must not be
  64. // irrelevant. (It can be 0 if no loop limiter is expected, and 0 should not be
  65. // deemed irrelevant).
  66. bool AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  67. opt::IRContext* context,
  68. const TransformationContext& transformation_context, uint32_t function_id,
  69. uint32_t loop_limiter_id) {
  70. // Look at all the functions until the function of interest is found.
  71. for (auto& function : *context->module()) {
  72. if (function.result_id() != function_id) {
  73. continue;
  74. }
  75. // Check that the parameters are all irrelevant.
  76. bool found_non_irrelevant_parameter = false;
  77. function.ForEachParam([context, &transformation_context,
  78. &found_non_irrelevant_parameter](
  79. opt::Instruction* inst) {
  80. if (context->get_def_use_mgr()->GetDef(inst->type_id())->opcode() ==
  81. spv::Op::OpTypePointer &&
  82. !transformation_context.GetFactManager()->PointeeValueIsIrrelevant(
  83. inst->result_id())) {
  84. found_non_irrelevant_parameter = true;
  85. }
  86. });
  87. if (found_non_irrelevant_parameter) {
  88. // A non-irrelevant parameter was found.
  89. return false;
  90. }
  91. // Look through the instructions in the function's first block.
  92. for (auto& inst : *function.begin()) {
  93. if (inst.opcode() != spv::Op::OpVariable) {
  94. // We have found a non-variable instruction; this means we have gotten
  95. // past all variables, so we are done.
  96. return true;
  97. }
  98. // The variable should be irrelevant if and only if it is not the loop
  99. // limiter.
  100. if ((inst.result_id() == loop_limiter_id) ==
  101. transformation_context.GetFactManager()->PointeeValueIsIrrelevant(
  102. inst.result_id())) {
  103. return false;
  104. }
  105. }
  106. assert(false &&
  107. "We should have processed all variables and returned by "
  108. "this point.");
  109. }
  110. assert(false && "We should have found the function of interest.");
  111. return true;
  112. }
  113. TEST(TransformationAddFunctionTest, BasicTest) {
  114. std::string shader = R"(
  115. OpCapability Shader
  116. %1 = OpExtInstImport "GLSL.std.450"
  117. OpMemoryModel Logical GLSL450
  118. OpEntryPoint Fragment %4 "main"
  119. OpExecutionMode %4 OriginUpperLeft
  120. OpSource ESSL 310
  121. %2 = OpTypeVoid
  122. %3 = OpTypeFunction %2
  123. %6 = OpTypeInt 32 1
  124. %7 = OpTypePointer Function %6
  125. %8 = OpTypeFloat 32
  126. %9 = OpTypePointer Function %8
  127. %10 = OpTypeFunction %8 %7 %9
  128. %18 = OpConstant %8 0
  129. %20 = OpConstant %6 0
  130. %28 = OpTypeBool
  131. %37 = OpConstant %6 1
  132. %42 = OpTypePointer Private %8
  133. %43 = OpVariable %42 Private
  134. %47 = OpConstant %8 1
  135. %4 = OpFunction %2 None %3
  136. %5 = OpLabel
  137. OpReturn
  138. OpFunctionEnd
  139. )";
  140. const auto env = SPV_ENV_UNIVERSAL_1_4;
  141. const auto consumer = nullptr;
  142. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  143. spvtools::ValidatorOptions validator_options;
  144. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  145. kConsoleMessageConsumer));
  146. TransformationContext transformation_context(
  147. MakeUnique<FactManager>(context.get()), validator_options);
  148. TransformationAddFunction transformation1(std::vector<protobufs::Instruction>(
  149. {MakeInstructionMessage(spv::Op::OpFunction, 8, 13,
  150. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  151. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  152. {SPV_OPERAND_TYPE_ID, {10}}}),
  153. MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 11, {}),
  154. MakeInstructionMessage(spv::Op::OpFunctionParameter, 9, 12, {}),
  155. MakeInstructionMessage(spv::Op::OpLabel, 0, 14, {}),
  156. MakeInstructionMessage(spv::Op::OpVariable, 9, 17,
  157. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  158. {uint32_t(spv::StorageClass::Function)}}}),
  159. MakeInstructionMessage(spv::Op::OpVariable, 7, 19,
  160. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  161. {uint32_t(spv::StorageClass::Function)}}}),
  162. MakeInstructionMessage(
  163. spv::Op::OpStore, 0, 0,
  164. {{SPV_OPERAND_TYPE_ID, {17}}, {SPV_OPERAND_TYPE_ID, {18}}}),
  165. MakeInstructionMessage(
  166. spv::Op::OpStore, 0, 0,
  167. {{SPV_OPERAND_TYPE_ID, {19}}, {SPV_OPERAND_TYPE_ID, {20}}}),
  168. MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  169. {{SPV_OPERAND_TYPE_ID, {21}}}),
  170. MakeInstructionMessage(spv::Op::OpLabel, 0, 21, {}),
  171. MakeInstructionMessage(spv::Op::OpLoopMerge, 0, 0,
  172. {{SPV_OPERAND_TYPE_ID, {23}},
  173. {SPV_OPERAND_TYPE_ID, {24}},
  174. {SPV_OPERAND_TYPE_LOOP_CONTROL,
  175. {uint32_t(spv::LoopControlMask::MaskNone)}}}),
  176. MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  177. {{SPV_OPERAND_TYPE_ID, {25}}}),
  178. MakeInstructionMessage(spv::Op::OpLabel, 0, 25, {}),
  179. MakeInstructionMessage(spv::Op::OpLoad, 6, 26,
  180. {{SPV_OPERAND_TYPE_ID, {19}}}),
  181. MakeInstructionMessage(spv::Op::OpLoad, 6, 27,
  182. {{SPV_OPERAND_TYPE_ID, {11}}}),
  183. MakeInstructionMessage(
  184. spv::Op::OpSLessThan, 28, 29,
  185. {{SPV_OPERAND_TYPE_ID, {26}}, {SPV_OPERAND_TYPE_ID, {27}}}),
  186. MakeInstructionMessage(spv::Op::OpBranchConditional, 0, 0,
  187. {{SPV_OPERAND_TYPE_ID, {29}},
  188. {SPV_OPERAND_TYPE_ID, {22}},
  189. {SPV_OPERAND_TYPE_ID, {23}}}),
  190. MakeInstructionMessage(spv::Op::OpLabel, 0, 22, {}),
  191. MakeInstructionMessage(spv::Op::OpLoad, 8, 30,
  192. {{SPV_OPERAND_TYPE_ID, {12}}}),
  193. MakeInstructionMessage(spv::Op::OpLoad, 6, 31,
  194. {{SPV_OPERAND_TYPE_ID, {19}}}),
  195. MakeInstructionMessage(spv::Op::OpConvertSToF, 8, 32,
  196. {{SPV_OPERAND_TYPE_ID, {31}}}),
  197. MakeInstructionMessage(
  198. spv::Op::OpFMul, 8, 33,
  199. {{SPV_OPERAND_TYPE_ID, {30}}, {SPV_OPERAND_TYPE_ID, {32}}}),
  200. MakeInstructionMessage(spv::Op::OpLoad, 8, 34,
  201. {{SPV_OPERAND_TYPE_ID, {17}}}),
  202. MakeInstructionMessage(
  203. spv::Op::OpFAdd, 8, 35,
  204. {{SPV_OPERAND_TYPE_ID, {34}}, {SPV_OPERAND_TYPE_ID, {33}}}),
  205. MakeInstructionMessage(
  206. spv::Op::OpStore, 0, 0,
  207. {{SPV_OPERAND_TYPE_ID, {17}}, {SPV_OPERAND_TYPE_ID, {35}}}),
  208. MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  209. {{SPV_OPERAND_TYPE_ID, {24}}}),
  210. MakeInstructionMessage(spv::Op::OpLabel, 0, 24, {}),
  211. MakeInstructionMessage(spv::Op::OpLoad, 6, 36,
  212. {{SPV_OPERAND_TYPE_ID, {19}}}),
  213. MakeInstructionMessage(
  214. spv::Op::OpIAdd, 6, 38,
  215. {{SPV_OPERAND_TYPE_ID, {36}}, {SPV_OPERAND_TYPE_ID, {37}}}),
  216. MakeInstructionMessage(
  217. spv::Op::OpStore, 0, 0,
  218. {{SPV_OPERAND_TYPE_ID, {19}}, {SPV_OPERAND_TYPE_ID, {38}}}),
  219. MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  220. {{SPV_OPERAND_TYPE_ID, {21}}}),
  221. MakeInstructionMessage(spv::Op::OpLabel, 0, 23, {}),
  222. MakeInstructionMessage(spv::Op::OpLoad, 8, 39,
  223. {{SPV_OPERAND_TYPE_ID, {17}}}),
  224. MakeInstructionMessage(spv::Op::OpReturnValue, 0, 0,
  225. {{SPV_OPERAND_TYPE_ID, {39}}}),
  226. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {})}));
  227. ASSERT_TRUE(
  228. transformation1.IsApplicable(context.get(), transformation_context));
  229. ApplyAndCheckFreshIds(transformation1, context.get(),
  230. &transformation_context);
  231. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  232. kConsoleMessageConsumer));
  233. std::string after_transformation1 = R"(
  234. OpCapability Shader
  235. %1 = OpExtInstImport "GLSL.std.450"
  236. OpMemoryModel Logical GLSL450
  237. OpEntryPoint Fragment %4 "main"
  238. OpExecutionMode %4 OriginUpperLeft
  239. OpSource ESSL 310
  240. %2 = OpTypeVoid
  241. %3 = OpTypeFunction %2
  242. %6 = OpTypeInt 32 1
  243. %7 = OpTypePointer Function %6
  244. %8 = OpTypeFloat 32
  245. %9 = OpTypePointer Function %8
  246. %10 = OpTypeFunction %8 %7 %9
  247. %18 = OpConstant %8 0
  248. %20 = OpConstant %6 0
  249. %28 = OpTypeBool
  250. %37 = OpConstant %6 1
  251. %42 = OpTypePointer Private %8
  252. %43 = OpVariable %42 Private
  253. %47 = OpConstant %8 1
  254. %4 = OpFunction %2 None %3
  255. %5 = OpLabel
  256. OpReturn
  257. OpFunctionEnd
  258. %13 = OpFunction %8 None %10
  259. %11 = OpFunctionParameter %7
  260. %12 = OpFunctionParameter %9
  261. %14 = OpLabel
  262. %17 = OpVariable %9 Function
  263. %19 = OpVariable %7 Function
  264. OpStore %17 %18
  265. OpStore %19 %20
  266. OpBranch %21
  267. %21 = OpLabel
  268. OpLoopMerge %23 %24 None
  269. OpBranch %25
  270. %25 = OpLabel
  271. %26 = OpLoad %6 %19
  272. %27 = OpLoad %6 %11
  273. %29 = OpSLessThan %28 %26 %27
  274. OpBranchConditional %29 %22 %23
  275. %22 = OpLabel
  276. %30 = OpLoad %8 %12
  277. %31 = OpLoad %6 %19
  278. %32 = OpConvertSToF %8 %31
  279. %33 = OpFMul %8 %30 %32
  280. %34 = OpLoad %8 %17
  281. %35 = OpFAdd %8 %34 %33
  282. OpStore %17 %35
  283. OpBranch %24
  284. %24 = OpLabel
  285. %36 = OpLoad %6 %19
  286. %38 = OpIAdd %6 %36 %37
  287. OpStore %19 %38
  288. OpBranch %21
  289. %23 = OpLabel
  290. %39 = OpLoad %8 %17
  291. OpReturnValue %39
  292. OpFunctionEnd
  293. )";
  294. ASSERT_TRUE(IsEqual(env, after_transformation1, context.get()));
  295. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(14));
  296. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(21));
  297. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(22));
  298. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(23));
  299. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(24));
  300. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(25));
  301. TransformationAddFunction transformation2(std::vector<protobufs::Instruction>(
  302. {MakeInstructionMessage(spv::Op::OpFunction, 2, 15,
  303. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  304. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  305. {SPV_OPERAND_TYPE_ID, {3}}}),
  306. MakeInstructionMessage(spv::Op::OpLabel, 0, 16, {}),
  307. MakeInstructionMessage(spv::Op::OpVariable, 7, 44,
  308. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  309. {uint32_t(spv::StorageClass::Function)}}}),
  310. MakeInstructionMessage(spv::Op::OpVariable, 9, 45,
  311. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  312. {uint32_t(spv::StorageClass::Function)}}}),
  313. MakeInstructionMessage(spv::Op::OpVariable, 7, 48,
  314. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  315. {uint32_t(spv::StorageClass::Function)}}}),
  316. MakeInstructionMessage(spv::Op::OpVariable, 9, 49,
  317. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  318. {uint32_t(spv::StorageClass::Function)}}}),
  319. MakeInstructionMessage(
  320. spv::Op::OpStore, 0, 0,
  321. {{SPV_OPERAND_TYPE_ID, {44}}, {SPV_OPERAND_TYPE_ID, {20}}}),
  322. MakeInstructionMessage(
  323. spv::Op::OpStore, 0, 0,
  324. {{SPV_OPERAND_TYPE_ID, {45}}, {SPV_OPERAND_TYPE_ID, {18}}}),
  325. MakeInstructionMessage(spv::Op::OpFunctionCall, 8, 46,
  326. {{SPV_OPERAND_TYPE_ID, {13}},
  327. {SPV_OPERAND_TYPE_ID, {44}},
  328. {SPV_OPERAND_TYPE_ID, {45}}}),
  329. MakeInstructionMessage(
  330. spv::Op::OpStore, 0, 0,
  331. {{SPV_OPERAND_TYPE_ID, {48}}, {SPV_OPERAND_TYPE_ID, {37}}}),
  332. MakeInstructionMessage(
  333. spv::Op::OpStore, 0, 0,
  334. {{SPV_OPERAND_TYPE_ID, {49}}, {SPV_OPERAND_TYPE_ID, {47}}}),
  335. MakeInstructionMessage(spv::Op::OpFunctionCall, 8, 50,
  336. {{SPV_OPERAND_TYPE_ID, {13}},
  337. {SPV_OPERAND_TYPE_ID, {48}},
  338. {SPV_OPERAND_TYPE_ID, {49}}}),
  339. MakeInstructionMessage(
  340. spv::Op::OpFAdd, 8, 51,
  341. {{SPV_OPERAND_TYPE_ID, {46}}, {SPV_OPERAND_TYPE_ID, {50}}}),
  342. MakeInstructionMessage(
  343. spv::Op::OpStore, 0, 0,
  344. {{SPV_OPERAND_TYPE_ID, {43}}, {SPV_OPERAND_TYPE_ID, {51}}}),
  345. MakeInstructionMessage(spv::Op::OpReturn, 0, 0, {}),
  346. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {})}));
  347. ASSERT_TRUE(
  348. transformation2.IsApplicable(context.get(), transformation_context));
  349. ApplyAndCheckFreshIds(transformation2, context.get(),
  350. &transformation_context);
  351. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  352. kConsoleMessageConsumer));
  353. std::string after_transformation2 = R"(
  354. OpCapability Shader
  355. %1 = OpExtInstImport "GLSL.std.450"
  356. OpMemoryModel Logical GLSL450
  357. OpEntryPoint Fragment %4 "main"
  358. OpExecutionMode %4 OriginUpperLeft
  359. OpSource ESSL 310
  360. %2 = OpTypeVoid
  361. %3 = OpTypeFunction %2
  362. %6 = OpTypeInt 32 1
  363. %7 = OpTypePointer Function %6
  364. %8 = OpTypeFloat 32
  365. %9 = OpTypePointer Function %8
  366. %10 = OpTypeFunction %8 %7 %9
  367. %18 = OpConstant %8 0
  368. %20 = OpConstant %6 0
  369. %28 = OpTypeBool
  370. %37 = OpConstant %6 1
  371. %42 = OpTypePointer Private %8
  372. %43 = OpVariable %42 Private
  373. %47 = OpConstant %8 1
  374. %4 = OpFunction %2 None %3
  375. %5 = OpLabel
  376. OpReturn
  377. OpFunctionEnd
  378. %13 = OpFunction %8 None %10
  379. %11 = OpFunctionParameter %7
  380. %12 = OpFunctionParameter %9
  381. %14 = OpLabel
  382. %17 = OpVariable %9 Function
  383. %19 = OpVariable %7 Function
  384. OpStore %17 %18
  385. OpStore %19 %20
  386. OpBranch %21
  387. %21 = OpLabel
  388. OpLoopMerge %23 %24 None
  389. OpBranch %25
  390. %25 = OpLabel
  391. %26 = OpLoad %6 %19
  392. %27 = OpLoad %6 %11
  393. %29 = OpSLessThan %28 %26 %27
  394. OpBranchConditional %29 %22 %23
  395. %22 = OpLabel
  396. %30 = OpLoad %8 %12
  397. %31 = OpLoad %6 %19
  398. %32 = OpConvertSToF %8 %31
  399. %33 = OpFMul %8 %30 %32
  400. %34 = OpLoad %8 %17
  401. %35 = OpFAdd %8 %34 %33
  402. OpStore %17 %35
  403. OpBranch %24
  404. %24 = OpLabel
  405. %36 = OpLoad %6 %19
  406. %38 = OpIAdd %6 %36 %37
  407. OpStore %19 %38
  408. OpBranch %21
  409. %23 = OpLabel
  410. %39 = OpLoad %8 %17
  411. OpReturnValue %39
  412. OpFunctionEnd
  413. %15 = OpFunction %2 None %3
  414. %16 = OpLabel
  415. %44 = OpVariable %7 Function
  416. %45 = OpVariable %9 Function
  417. %48 = OpVariable %7 Function
  418. %49 = OpVariable %9 Function
  419. OpStore %44 %20
  420. OpStore %45 %18
  421. %46 = OpFunctionCall %8 %13 %44 %45
  422. OpStore %48 %37
  423. OpStore %49 %47
  424. %50 = OpFunctionCall %8 %13 %48 %49
  425. %51 = OpFAdd %8 %46 %50
  426. OpStore %43 %51
  427. OpReturn
  428. OpFunctionEnd
  429. )";
  430. ASSERT_TRUE(IsEqual(env, after_transformation2, context.get()));
  431. ASSERT_TRUE(transformation_context.GetFactManager()->BlockIsDead(16));
  432. }
  433. TEST(TransformationAddFunctionTest, InapplicableTransformations) {
  434. std::string shader = R"(
  435. OpCapability Shader
  436. %1 = OpExtInstImport "GLSL.std.450"
  437. OpMemoryModel Logical GLSL450
  438. OpEntryPoint Fragment %4 "main"
  439. OpExecutionMode %4 OriginUpperLeft
  440. OpSource ESSL 310
  441. %2 = OpTypeVoid
  442. %3 = OpTypeFunction %2
  443. %6 = OpTypeInt 32 1
  444. %7 = OpTypePointer Function %6
  445. %8 = OpTypeFloat 32
  446. %9 = OpTypePointer Function %8
  447. %10 = OpTypeFunction %8 %7 %9
  448. %18 = OpConstant %8 0
  449. %20 = OpConstant %6 0
  450. %28 = OpTypeBool
  451. %37 = OpConstant %6 1
  452. %42 = OpTypePointer Private %8
  453. %43 = OpVariable %42 Private
  454. %47 = OpConstant %8 1
  455. %4 = OpFunction %2 None %3
  456. %5 = OpLabel
  457. OpReturn
  458. OpFunctionEnd
  459. %13 = OpFunction %8 None %10
  460. %11 = OpFunctionParameter %7
  461. %12 = OpFunctionParameter %9
  462. %14 = OpLabel
  463. %17 = OpVariable %9 Function
  464. %19 = OpVariable %7 Function
  465. OpStore %17 %18
  466. OpStore %19 %20
  467. OpBranch %21
  468. %21 = OpLabel
  469. OpLoopMerge %23 %24 None
  470. OpBranch %25
  471. %25 = OpLabel
  472. %26 = OpLoad %6 %19
  473. %27 = OpLoad %6 %11
  474. %29 = OpSLessThan %28 %26 %27
  475. OpBranchConditional %29 %22 %23
  476. %22 = OpLabel
  477. %30 = OpLoad %8 %12
  478. %31 = OpLoad %6 %19
  479. %32 = OpConvertSToF %8 %31
  480. %33 = OpFMul %8 %30 %32
  481. %34 = OpLoad %8 %17
  482. %35 = OpFAdd %8 %34 %33
  483. OpStore %17 %35
  484. OpBranch %24
  485. %24 = OpLabel
  486. %36 = OpLoad %6 %19
  487. %38 = OpIAdd %6 %36 %37
  488. OpStore %19 %38
  489. OpBranch %21
  490. %23 = OpLabel
  491. %39 = OpLoad %8 %17
  492. OpReturnValue %39
  493. OpFunctionEnd
  494. )";
  495. const auto env = SPV_ENV_UNIVERSAL_1_4;
  496. const auto consumer = nullptr;
  497. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  498. spvtools::ValidatorOptions validator_options;
  499. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  500. kConsoleMessageConsumer));
  501. TransformationContext transformation_context(
  502. MakeUnique<FactManager>(context.get()), validator_options);
  503. // No instructions
  504. ASSERT_FALSE(
  505. TransformationAddFunction(std::vector<protobufs::Instruction>({}))
  506. .IsApplicable(context.get(), transformation_context));
  507. // No function begin
  508. ASSERT_FALSE(
  509. TransformationAddFunction(
  510. std::vector<protobufs::Instruction>(
  511. {MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 11, {}),
  512. MakeInstructionMessage(spv::Op::OpFunctionParameter, 9, 12, {}),
  513. MakeInstructionMessage(spv::Op::OpLabel, 0, 14, {})}))
  514. .IsApplicable(context.get(), transformation_context));
  515. // No OpLabel
  516. ASSERT_FALSE(
  517. TransformationAddFunction(
  518. std::vector<protobufs::Instruction>(
  519. {MakeInstructionMessage(
  520. spv::Op::OpFunction, 8, 13,
  521. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  522. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  523. {SPV_OPERAND_TYPE_ID, {10}}}),
  524. MakeInstructionMessage(spv::Op::OpReturnValue, 0, 0,
  525. {{SPV_OPERAND_TYPE_ID, {39}}}),
  526. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {})}))
  527. .IsApplicable(context.get(), transformation_context));
  528. // Abrupt end of instructions
  529. ASSERT_FALSE(TransformationAddFunction(
  530. std::vector<protobufs::Instruction>({MakeInstructionMessage(
  531. spv::Op::OpFunction, 8, 13,
  532. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  533. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  534. {SPV_OPERAND_TYPE_ID, {10}}})}))
  535. .IsApplicable(context.get(), transformation_context));
  536. // No function end
  537. ASSERT_FALSE(TransformationAddFunction(
  538. std::vector<protobufs::Instruction>(
  539. {MakeInstructionMessage(
  540. spv::Op::OpFunction, 8, 13,
  541. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  542. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  543. {SPV_OPERAND_TYPE_ID, {10}}}),
  544. MakeInstructionMessage(spv::Op::OpLabel, 0, 14, {}),
  545. MakeInstructionMessage(spv::Op::OpReturnValue, 0, 0,
  546. {{SPV_OPERAND_TYPE_ID, {39}}})}))
  547. .IsApplicable(context.get(), transformation_context));
  548. }
  549. TEST(TransformationAddFunctionTest, LoopLimiters) {
  550. std::string shader = R"(
  551. OpCapability Shader
  552. %1 = OpExtInstImport "GLSL.std.450"
  553. OpMemoryModel Logical GLSL450
  554. OpEntryPoint Fragment %4 "main"
  555. OpExecutionMode %4 OriginUpperLeft
  556. OpSource ESSL 310
  557. %2 = OpTypeVoid
  558. %3 = OpTypeFunction %2
  559. %6 = OpTypeInt 32 0
  560. %7 = OpTypePointer Function %6
  561. %8 = OpConstant %6 0
  562. %9 = OpConstant %6 1
  563. %10 = OpConstant %6 5
  564. %11 = OpTypeBool
  565. %12 = OpConstantTrue %11
  566. %4 = OpFunction %2 None %3
  567. %5 = OpLabel
  568. OpReturn
  569. OpFunctionEnd
  570. )";
  571. const auto env = SPV_ENV_UNIVERSAL_1_4;
  572. const auto consumer = nullptr;
  573. std::vector<protobufs::Instruction> instructions;
  574. instructions.push_back(
  575. MakeInstructionMessage(spv::Op::OpFunction, 2, 30,
  576. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  577. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  578. {SPV_OPERAND_TYPE_TYPE_ID, {3}}}));
  579. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 31, {}));
  580. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  581. {{SPV_OPERAND_TYPE_ID, {20}}}));
  582. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 20, {}));
  583. instructions.push_back(
  584. MakeInstructionMessage(spv::Op::OpLoopMerge, 0, 0,
  585. {{SPV_OPERAND_TYPE_ID, {21}},
  586. {SPV_OPERAND_TYPE_ID, {22}},
  587. {SPV_OPERAND_TYPE_LOOP_CONTROL,
  588. {uint32_t(spv::LoopControlMask::MaskNone)}}}));
  589. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranchConditional, 0,
  590. 0,
  591. {{SPV_OPERAND_TYPE_ID, {12}},
  592. {SPV_OPERAND_TYPE_ID, {23}},
  593. {SPV_OPERAND_TYPE_ID, {21}}}));
  594. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 23, {}));
  595. instructions.push_back(
  596. MakeInstructionMessage(spv::Op::OpLoopMerge, 0, 0,
  597. {{SPV_OPERAND_TYPE_ID, {25}},
  598. {SPV_OPERAND_TYPE_ID, {26}},
  599. {SPV_OPERAND_TYPE_LOOP_CONTROL,
  600. {uint32_t(spv::LoopControlMask::MaskNone)}}}));
  601. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  602. {{SPV_OPERAND_TYPE_ID, {28}}}));
  603. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 28, {}));
  604. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranchConditional, 0,
  605. 0,
  606. {{SPV_OPERAND_TYPE_ID, {12}},
  607. {SPV_OPERAND_TYPE_ID, {26}},
  608. {SPV_OPERAND_TYPE_ID, {25}}}));
  609. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 26, {}));
  610. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  611. {{SPV_OPERAND_TYPE_ID, {23}}}));
  612. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 25, {}));
  613. instructions.push_back(
  614. MakeInstructionMessage(spv::Op::OpLoopMerge, 0, 0,
  615. {{SPV_OPERAND_TYPE_ID, {24}},
  616. {SPV_OPERAND_TYPE_ID, {27}},
  617. {SPV_OPERAND_TYPE_LOOP_CONTROL,
  618. {uint32_t(spv::LoopControlMask::MaskNone)}}}));
  619. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranchConditional, 0,
  620. 0,
  621. {{SPV_OPERAND_TYPE_ID, {12}},
  622. {SPV_OPERAND_TYPE_ID, {24}},
  623. {SPV_OPERAND_TYPE_ID, {27}}}));
  624. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 27, {}));
  625. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  626. {{SPV_OPERAND_TYPE_ID, {25}}}));
  627. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 24, {}));
  628. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  629. {{SPV_OPERAND_TYPE_ID, {22}}}));
  630. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 22, {}));
  631. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranch, 0, 0,
  632. {{SPV_OPERAND_TYPE_ID, {20}}}));
  633. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 21, {}));
  634. instructions.push_back(MakeInstructionMessage(spv::Op::OpReturn, 0, 0, {}));
  635. instructions.push_back(
  636. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  637. spvtools::ValidatorOptions validator_options;
  638. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  639. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  640. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  641. context1.get(), validator_options, kConsoleMessageConsumer));
  642. TransformationContext transformation_context1(
  643. MakeUnique<FactManager>(context1.get()), validator_options);
  644. TransformationContext transformation_context2(
  645. MakeUnique<FactManager>(context2.get()), validator_options);
  646. TransformationAddFunction add_dead_function(instructions);
  647. ASSERT_TRUE(
  648. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  649. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  650. &transformation_context1);
  651. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  652. context1.get(), validator_options, kConsoleMessageConsumer));
  653. // The added function should not be deemed livesafe.
  654. ASSERT_FALSE(
  655. transformation_context1.GetFactManager()->FunctionIsLivesafe(30));
  656. // All variables/parameters in the function should be deemed irrelevant.
  657. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  658. context1.get(), transformation_context1, 30, 0));
  659. std::string added_as_dead_code = R"(
  660. OpCapability Shader
  661. %1 = OpExtInstImport "GLSL.std.450"
  662. OpMemoryModel Logical GLSL450
  663. OpEntryPoint Fragment %4 "main"
  664. OpExecutionMode %4 OriginUpperLeft
  665. OpSource ESSL 310
  666. %2 = OpTypeVoid
  667. %3 = OpTypeFunction %2
  668. %6 = OpTypeInt 32 0
  669. %7 = OpTypePointer Function %6
  670. %8 = OpConstant %6 0
  671. %9 = OpConstant %6 1
  672. %10 = OpConstant %6 5
  673. %11 = OpTypeBool
  674. %12 = OpConstantTrue %11
  675. %4 = OpFunction %2 None %3
  676. %5 = OpLabel
  677. OpReturn
  678. OpFunctionEnd
  679. %30 = OpFunction %2 None %3
  680. %31 = OpLabel
  681. OpBranch %20
  682. %20 = OpLabel
  683. OpLoopMerge %21 %22 None
  684. OpBranchConditional %12 %23 %21
  685. %23 = OpLabel
  686. OpLoopMerge %25 %26 None
  687. OpBranch %28
  688. %28 = OpLabel
  689. OpBranchConditional %12 %26 %25
  690. %26 = OpLabel
  691. OpBranch %23
  692. %25 = OpLabel
  693. OpLoopMerge %24 %27 None
  694. OpBranchConditional %12 %24 %27
  695. %27 = OpLabel
  696. OpBranch %25
  697. %24 = OpLabel
  698. OpBranch %22
  699. %22 = OpLabel
  700. OpBranch %20
  701. %21 = OpLabel
  702. OpReturn
  703. OpFunctionEnd
  704. )";
  705. ASSERT_TRUE(IsEqual(env, added_as_dead_code, context1.get()));
  706. protobufs::LoopLimiterInfo loop_limiter1;
  707. loop_limiter1.set_loop_header_id(20);
  708. loop_limiter1.set_load_id(101);
  709. loop_limiter1.set_increment_id(102);
  710. loop_limiter1.set_compare_id(103);
  711. loop_limiter1.set_logical_op_id(104);
  712. protobufs::LoopLimiterInfo loop_limiter2;
  713. loop_limiter2.set_loop_header_id(23);
  714. loop_limiter2.set_load_id(105);
  715. loop_limiter2.set_increment_id(106);
  716. loop_limiter2.set_compare_id(107);
  717. loop_limiter2.set_logical_op_id(108);
  718. protobufs::LoopLimiterInfo loop_limiter3;
  719. loop_limiter3.set_loop_header_id(25);
  720. loop_limiter3.set_load_id(109);
  721. loop_limiter3.set_increment_id(110);
  722. loop_limiter3.set_compare_id(111);
  723. loop_limiter3.set_logical_op_id(112);
  724. std::vector<protobufs::LoopLimiterInfo> loop_limiters = {
  725. loop_limiter1, loop_limiter2, loop_limiter3};
  726. TransformationAddFunction add_livesafe_function(instructions, 100, 10,
  727. loop_limiters, 0, {});
  728. ASSERT_TRUE(add_livesafe_function.IsApplicable(context2.get(),
  729. transformation_context2));
  730. ApplyAndCheckFreshIds(add_livesafe_function, context2.get(),
  731. &transformation_context2);
  732. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  733. context2.get(), validator_options, kConsoleMessageConsumer));
  734. // The added function should indeed be deemed livesafe.
  735. ASSERT_TRUE(transformation_context2.GetFactManager()->FunctionIsLivesafe(30));
  736. // All variables/parameters in the function should be deemed irrelevant,
  737. // except the loop limiter.
  738. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  739. context2.get(), transformation_context2, 30, 100));
  740. std::string added_as_livesafe_code = R"(
  741. OpCapability Shader
  742. %1 = OpExtInstImport "GLSL.std.450"
  743. OpMemoryModel Logical GLSL450
  744. OpEntryPoint Fragment %4 "main"
  745. OpExecutionMode %4 OriginUpperLeft
  746. OpSource ESSL 310
  747. %2 = OpTypeVoid
  748. %3 = OpTypeFunction %2
  749. %6 = OpTypeInt 32 0
  750. %7 = OpTypePointer Function %6
  751. %8 = OpConstant %6 0
  752. %9 = OpConstant %6 1
  753. %10 = OpConstant %6 5
  754. %11 = OpTypeBool
  755. %12 = OpConstantTrue %11
  756. %4 = OpFunction %2 None %3
  757. %5 = OpLabel
  758. OpReturn
  759. OpFunctionEnd
  760. %30 = OpFunction %2 None %3
  761. %31 = OpLabel
  762. %100 = OpVariable %7 Function %8
  763. OpBranch %20
  764. %20 = OpLabel
  765. OpLoopMerge %21 %22 None
  766. OpBranchConditional %12 %23 %21
  767. %23 = OpLabel
  768. OpLoopMerge %25 %26 None
  769. OpBranch %28
  770. %28 = OpLabel
  771. OpBranchConditional %12 %26 %25
  772. %26 = OpLabel
  773. %105 = OpLoad %6 %100
  774. %106 = OpIAdd %6 %105 %9
  775. OpStore %100 %106
  776. %107 = OpUGreaterThanEqual %11 %105 %10
  777. OpBranchConditional %107 %25 %23
  778. %25 = OpLabel
  779. OpLoopMerge %24 %27 None
  780. OpBranchConditional %12 %24 %27
  781. %27 = OpLabel
  782. %109 = OpLoad %6 %100
  783. %110 = OpIAdd %6 %109 %9
  784. OpStore %100 %110
  785. %111 = OpUGreaterThanEqual %11 %109 %10
  786. OpBranchConditional %111 %24 %25
  787. %24 = OpLabel
  788. OpBranch %22
  789. %22 = OpLabel
  790. %101 = OpLoad %6 %100
  791. %102 = OpIAdd %6 %101 %9
  792. OpStore %100 %102
  793. %103 = OpUGreaterThanEqual %11 %101 %10
  794. OpBranchConditional %103 %21 %20
  795. %21 = OpLabel
  796. OpReturn
  797. OpFunctionEnd
  798. )";
  799. ASSERT_TRUE(IsEqual(env, added_as_livesafe_code, context2.get()));
  800. }
  801. TEST(TransformationAddFunctionTest, KillAndUnreachableInVoidFunction) {
  802. std::string shader = R"(
  803. OpCapability Shader
  804. %1 = OpExtInstImport "GLSL.std.450"
  805. OpMemoryModel Logical GLSL450
  806. OpEntryPoint Fragment %4 "main"
  807. OpExecutionMode %4 OriginUpperLeft
  808. OpSource ESSL 310
  809. %2 = OpTypeVoid
  810. %3 = OpTypeFunction %2
  811. %6 = OpTypeInt 32 1
  812. %7 = OpTypePointer Function %6
  813. %8 = OpTypeFunction %2 %7
  814. %13 = OpConstant %6 2
  815. %14 = OpTypeBool
  816. %4 = OpFunction %2 None %3
  817. %5 = OpLabel
  818. OpReturn
  819. OpFunctionEnd
  820. )";
  821. const auto env = SPV_ENV_UNIVERSAL_1_4;
  822. const auto consumer = nullptr;
  823. std::vector<protobufs::Instruction> instructions;
  824. instructions.push_back(
  825. MakeInstructionMessage(spv::Op::OpFunction, 2, 10,
  826. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  827. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  828. {SPV_OPERAND_TYPE_TYPE_ID, {8}}}));
  829. instructions.push_back(
  830. MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 9, {}));
  831. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 11, {}));
  832. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 12,
  833. {{SPV_OPERAND_TYPE_ID, {9}}}));
  834. instructions.push_back(MakeInstructionMessage(
  835. spv::Op::OpIEqual, 14, 15,
  836. {{SPV_OPERAND_TYPE_ID, {12}}, {SPV_OPERAND_TYPE_ID, {13}}}));
  837. instructions.push_back(MakeInstructionMessage(
  838. spv::Op::OpSelectionMerge, 0, 0,
  839. {{SPV_OPERAND_TYPE_ID, {17}},
  840. {SPV_OPERAND_TYPE_SELECTION_CONTROL,
  841. {uint32_t(spv::SelectionControlMask::MaskNone)}}}));
  842. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranchConditional, 0,
  843. 0,
  844. {{SPV_OPERAND_TYPE_ID, {15}},
  845. {SPV_OPERAND_TYPE_ID, {16}},
  846. {SPV_OPERAND_TYPE_ID, {17}}}));
  847. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 16, {}));
  848. instructions.push_back(
  849. MakeInstructionMessage(spv::Op::OpUnreachable, 0, 0, {}));
  850. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 17, {}));
  851. instructions.push_back(MakeInstructionMessage(spv::Op::OpKill, 0, 0, {}));
  852. instructions.push_back(
  853. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  854. spvtools::ValidatorOptions validator_options;
  855. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  856. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  857. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  858. context1.get(), validator_options, kConsoleMessageConsumer));
  859. TransformationContext transformation_context1(
  860. MakeUnique<FactManager>(context1.get()), validator_options);
  861. TransformationContext transformation_context2(
  862. MakeUnique<FactManager>(context2.get()), validator_options);
  863. TransformationAddFunction add_dead_function(instructions);
  864. ASSERT_TRUE(
  865. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  866. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  867. &transformation_context1);
  868. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  869. context1.get(), validator_options, kConsoleMessageConsumer));
  870. // The added function should not be deemed livesafe.
  871. ASSERT_FALSE(
  872. transformation_context1.GetFactManager()->FunctionIsLivesafe(10));
  873. // All variables/parameters in the function should be deemed irrelevant.
  874. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  875. context1.get(), transformation_context1, 10, 0));
  876. std::string added_as_dead_code = R"(
  877. OpCapability Shader
  878. %1 = OpExtInstImport "GLSL.std.450"
  879. OpMemoryModel Logical GLSL450
  880. OpEntryPoint Fragment %4 "main"
  881. OpExecutionMode %4 OriginUpperLeft
  882. OpSource ESSL 310
  883. %2 = OpTypeVoid
  884. %3 = OpTypeFunction %2
  885. %6 = OpTypeInt 32 1
  886. %7 = OpTypePointer Function %6
  887. %8 = OpTypeFunction %2 %7
  888. %13 = OpConstant %6 2
  889. %14 = OpTypeBool
  890. %4 = OpFunction %2 None %3
  891. %5 = OpLabel
  892. OpReturn
  893. OpFunctionEnd
  894. %10 = OpFunction %2 None %8
  895. %9 = OpFunctionParameter %7
  896. %11 = OpLabel
  897. %12 = OpLoad %6 %9
  898. %15 = OpIEqual %14 %12 %13
  899. OpSelectionMerge %17 None
  900. OpBranchConditional %15 %16 %17
  901. %16 = OpLabel
  902. OpUnreachable
  903. %17 = OpLabel
  904. OpKill
  905. OpFunctionEnd
  906. )";
  907. ASSERT_TRUE(IsEqual(env, added_as_dead_code, context1.get()));
  908. TransformationAddFunction add_livesafe_function(instructions, 0, 0, {}, 0,
  909. {});
  910. ASSERT_TRUE(add_livesafe_function.IsApplicable(context2.get(),
  911. transformation_context2));
  912. ApplyAndCheckFreshIds(add_livesafe_function, context2.get(),
  913. &transformation_context2);
  914. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  915. context2.get(), validator_options, kConsoleMessageConsumer));
  916. // The added function should indeed be deemed livesafe.
  917. ASSERT_TRUE(transformation_context2.GetFactManager()->FunctionIsLivesafe(10));
  918. // All variables/parameters in the function should be deemed irrelevant.
  919. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  920. context2.get(), transformation_context2, 10, 0));
  921. std::string added_as_livesafe_code = R"(
  922. OpCapability Shader
  923. %1 = OpExtInstImport "GLSL.std.450"
  924. OpMemoryModel Logical GLSL450
  925. OpEntryPoint Fragment %4 "main"
  926. OpExecutionMode %4 OriginUpperLeft
  927. OpSource ESSL 310
  928. %2 = OpTypeVoid
  929. %3 = OpTypeFunction %2
  930. %6 = OpTypeInt 32 1
  931. %7 = OpTypePointer Function %6
  932. %8 = OpTypeFunction %2 %7
  933. %13 = OpConstant %6 2
  934. %14 = OpTypeBool
  935. %4 = OpFunction %2 None %3
  936. %5 = OpLabel
  937. OpReturn
  938. OpFunctionEnd
  939. %10 = OpFunction %2 None %8
  940. %9 = OpFunctionParameter %7
  941. %11 = OpLabel
  942. %12 = OpLoad %6 %9
  943. %15 = OpIEqual %14 %12 %13
  944. OpSelectionMerge %17 None
  945. OpBranchConditional %15 %16 %17
  946. %16 = OpLabel
  947. OpReturn
  948. %17 = OpLabel
  949. OpReturn
  950. OpFunctionEnd
  951. )";
  952. ASSERT_TRUE(IsEqual(env, added_as_livesafe_code, context2.get()));
  953. }
  954. TEST(TransformationAddFunctionTest, KillAndUnreachableInNonVoidFunction) {
  955. std::string shader = R"(
  956. OpCapability Shader
  957. %1 = OpExtInstImport "GLSL.std.450"
  958. OpMemoryModel Logical GLSL450
  959. OpEntryPoint Fragment %4 "main"
  960. OpExecutionMode %4 OriginUpperLeft
  961. OpSource ESSL 310
  962. %2 = OpTypeVoid
  963. %3 = OpTypeFunction %2
  964. %6 = OpTypeInt 32 1
  965. %7 = OpTypePointer Function %6
  966. %8 = OpTypeFunction %2 %7
  967. %50 = OpTypeFunction %6 %7
  968. %13 = OpConstant %6 2
  969. %14 = OpTypeBool
  970. %4 = OpFunction %2 None %3
  971. %5 = OpLabel
  972. OpReturn
  973. OpFunctionEnd
  974. )";
  975. const auto env = SPV_ENV_UNIVERSAL_1_4;
  976. const auto consumer = nullptr;
  977. std::vector<protobufs::Instruction> instructions;
  978. instructions.push_back(
  979. MakeInstructionMessage(spv::Op::OpFunction, 6, 10,
  980. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  981. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  982. {SPV_OPERAND_TYPE_TYPE_ID, {50}}}));
  983. instructions.push_back(
  984. MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 9, {}));
  985. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 11, {}));
  986. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 12,
  987. {{SPV_OPERAND_TYPE_ID, {9}}}));
  988. instructions.push_back(MakeInstructionMessage(
  989. spv::Op::OpIEqual, 14, 15,
  990. {{SPV_OPERAND_TYPE_ID, {12}}, {SPV_OPERAND_TYPE_ID, {13}}}));
  991. instructions.push_back(MakeInstructionMessage(
  992. spv::Op::OpSelectionMerge, 0, 0,
  993. {{SPV_OPERAND_TYPE_ID, {17}},
  994. {SPV_OPERAND_TYPE_SELECTION_CONTROL,
  995. {uint32_t(spv::SelectionControlMask::MaskNone)}}}));
  996. instructions.push_back(MakeInstructionMessage(spv::Op::OpBranchConditional, 0,
  997. 0,
  998. {{SPV_OPERAND_TYPE_ID, {15}},
  999. {SPV_OPERAND_TYPE_ID, {16}},
  1000. {SPV_OPERAND_TYPE_ID, {17}}}));
  1001. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 16, {}));
  1002. instructions.push_back(
  1003. MakeInstructionMessage(spv::Op::OpUnreachable, 0, 0, {}));
  1004. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 17, {}));
  1005. instructions.push_back(MakeInstructionMessage(spv::Op::OpKill, 0, 0, {}));
  1006. instructions.push_back(
  1007. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  1008. spvtools::ValidatorOptions validator_options;
  1009. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1010. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1011. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1012. context1.get(), validator_options, kConsoleMessageConsumer));
  1013. TransformationContext transformation_context1(
  1014. MakeUnique<FactManager>(context1.get()), validator_options);
  1015. TransformationContext transformation_context2(
  1016. MakeUnique<FactManager>(context2.get()), validator_options);
  1017. TransformationAddFunction add_dead_function(instructions);
  1018. ASSERT_TRUE(
  1019. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  1020. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  1021. &transformation_context1);
  1022. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1023. context1.get(), validator_options, kConsoleMessageConsumer));
  1024. // The added function should not be deemed livesafe.
  1025. ASSERT_FALSE(
  1026. transformation_context1.GetFactManager()->FunctionIsLivesafe(10));
  1027. // All variables/parameters in the function should be deemed irrelevant.
  1028. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1029. context1.get(), transformation_context1, 10, 0));
  1030. std::string added_as_dead_code = R"(
  1031. OpCapability Shader
  1032. %1 = OpExtInstImport "GLSL.std.450"
  1033. OpMemoryModel Logical GLSL450
  1034. OpEntryPoint Fragment %4 "main"
  1035. OpExecutionMode %4 OriginUpperLeft
  1036. OpSource ESSL 310
  1037. %2 = OpTypeVoid
  1038. %3 = OpTypeFunction %2
  1039. %6 = OpTypeInt 32 1
  1040. %7 = OpTypePointer Function %6
  1041. %8 = OpTypeFunction %2 %7
  1042. %50 = OpTypeFunction %6 %7
  1043. %13 = OpConstant %6 2
  1044. %14 = OpTypeBool
  1045. %4 = OpFunction %2 None %3
  1046. %5 = OpLabel
  1047. OpReturn
  1048. OpFunctionEnd
  1049. %10 = OpFunction %6 None %50
  1050. %9 = OpFunctionParameter %7
  1051. %11 = OpLabel
  1052. %12 = OpLoad %6 %9
  1053. %15 = OpIEqual %14 %12 %13
  1054. OpSelectionMerge %17 None
  1055. OpBranchConditional %15 %16 %17
  1056. %16 = OpLabel
  1057. OpUnreachable
  1058. %17 = OpLabel
  1059. OpKill
  1060. OpFunctionEnd
  1061. )";
  1062. ASSERT_TRUE(IsEqual(env, added_as_dead_code, context1.get()));
  1063. TransformationAddFunction add_livesafe_function(instructions, 0, 0, {}, 13,
  1064. {});
  1065. ASSERT_TRUE(add_livesafe_function.IsApplicable(context2.get(),
  1066. transformation_context2));
  1067. ApplyAndCheckFreshIds(add_livesafe_function, context2.get(),
  1068. &transformation_context2);
  1069. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1070. context2.get(), validator_options, kConsoleMessageConsumer));
  1071. // The added function should indeed be deemed livesafe.
  1072. ASSERT_TRUE(transformation_context2.GetFactManager()->FunctionIsLivesafe(10));
  1073. // All variables/parameters in the function should be deemed irrelevant.
  1074. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1075. context2.get(), transformation_context2, 10, 0));
  1076. std::string added_as_livesafe_code = R"(
  1077. OpCapability Shader
  1078. %1 = OpExtInstImport "GLSL.std.450"
  1079. OpMemoryModel Logical GLSL450
  1080. OpEntryPoint Fragment %4 "main"
  1081. OpExecutionMode %4 OriginUpperLeft
  1082. OpSource ESSL 310
  1083. %2 = OpTypeVoid
  1084. %3 = OpTypeFunction %2
  1085. %6 = OpTypeInt 32 1
  1086. %7 = OpTypePointer Function %6
  1087. %8 = OpTypeFunction %2 %7
  1088. %50 = OpTypeFunction %6 %7
  1089. %13 = OpConstant %6 2
  1090. %14 = OpTypeBool
  1091. %4 = OpFunction %2 None %3
  1092. %5 = OpLabel
  1093. OpReturn
  1094. OpFunctionEnd
  1095. %10 = OpFunction %6 None %50
  1096. %9 = OpFunctionParameter %7
  1097. %11 = OpLabel
  1098. %12 = OpLoad %6 %9
  1099. %15 = OpIEqual %14 %12 %13
  1100. OpSelectionMerge %17 None
  1101. OpBranchConditional %15 %16 %17
  1102. %16 = OpLabel
  1103. OpReturnValue %13
  1104. %17 = OpLabel
  1105. OpReturnValue %13
  1106. OpFunctionEnd
  1107. )";
  1108. ASSERT_TRUE(IsEqual(env, added_as_livesafe_code, context2.get()));
  1109. }
  1110. TEST(TransformationAddFunctionTest, ClampedAccessChains) {
  1111. std::string shader = R"(
  1112. OpCapability Shader
  1113. %1 = OpExtInstImport "GLSL.std.450"
  1114. OpMemoryModel Logical GLSL450
  1115. OpEntryPoint Fragment %4 "main"
  1116. OpExecutionMode %4 OriginUpperLeft
  1117. OpSource ESSL 310
  1118. %2 = OpTypeVoid
  1119. %100 = OpTypeBool
  1120. %3 = OpTypeFunction %2
  1121. %6 = OpTypeInt 32 1
  1122. %7 = OpTypePointer Function %6
  1123. %15 = OpTypeInt 32 0
  1124. %102 = OpTypePointer Function %15
  1125. %8 = OpTypeFunction %2 %7 %102 %7
  1126. %16 = OpConstant %15 5
  1127. %17 = OpTypeArray %6 %16
  1128. %18 = OpTypeArray %17 %16
  1129. %19 = OpTypePointer Private %18
  1130. %20 = OpVariable %19 Private
  1131. %21 = OpConstant %6 0
  1132. %23 = OpTypePointer Private %6
  1133. %26 = OpTypePointer Function %17
  1134. %29 = OpTypePointer Private %17
  1135. %33 = OpConstant %6 4
  1136. %200 = OpConstant %15 4
  1137. %35 = OpConstant %15 10
  1138. %36 = OpTypeArray %6 %35
  1139. %37 = OpTypePointer Private %36
  1140. %38 = OpVariable %37 Private
  1141. %54 = OpTypeFloat 32
  1142. %55 = OpTypeVector %54 4
  1143. %56 = OpTypePointer Private %55
  1144. %57 = OpVariable %56 Private
  1145. %59 = OpTypeVector %54 3
  1146. %60 = OpTypeMatrix %59 2
  1147. %61 = OpTypePointer Private %60
  1148. %62 = OpVariable %61 Private
  1149. %64 = OpTypePointer Private %54
  1150. %69 = OpConstant %54 2
  1151. %71 = OpConstant %6 1
  1152. %72 = OpConstant %6 2
  1153. %201 = OpConstant %15 2
  1154. %73 = OpConstant %6 3
  1155. %202 = OpConstant %15 3
  1156. %203 = OpConstant %6 1
  1157. %204 = OpConstant %6 9
  1158. %4 = OpFunction %2 None %3
  1159. %5 = OpLabel
  1160. OpReturn
  1161. OpFunctionEnd
  1162. )";
  1163. const auto env = SPV_ENV_UNIVERSAL_1_4;
  1164. const auto consumer = nullptr;
  1165. std::vector<protobufs::Instruction> instructions;
  1166. instructions.push_back(
  1167. MakeInstructionMessage(spv::Op::OpFunction, 2, 12,
  1168. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  1169. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  1170. {SPV_OPERAND_TYPE_TYPE_ID, {8}}}));
  1171. instructions.push_back(
  1172. MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 9, {}));
  1173. instructions.push_back(
  1174. MakeInstructionMessage(spv::Op::OpFunctionParameter, 102, 10, {}));
  1175. instructions.push_back(
  1176. MakeInstructionMessage(spv::Op::OpFunctionParameter, 7, 11, {}));
  1177. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 13, {}));
  1178. instructions.push_back(
  1179. MakeInstructionMessage(spv::Op::OpVariable, 7, 14,
  1180. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  1181. {uint32_t(spv::StorageClass::Function)}}}));
  1182. instructions.push_back(
  1183. MakeInstructionMessage(spv::Op::OpVariable, 26, 27,
  1184. {{SPV_OPERAND_TYPE_STORAGE_CLASS,
  1185. {uint32_t(spv::StorageClass::Function)}}}));
  1186. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 22,
  1187. {{SPV_OPERAND_TYPE_ID, {11}}}));
  1188. instructions.push_back(MakeInstructionMessage(spv::Op::OpAccessChain, 23, 24,
  1189. {{SPV_OPERAND_TYPE_ID, {20}},
  1190. {SPV_OPERAND_TYPE_ID, {21}},
  1191. {SPV_OPERAND_TYPE_ID, {22}}}));
  1192. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 25,
  1193. {{SPV_OPERAND_TYPE_ID, {24}}}));
  1194. instructions.push_back(MakeInstructionMessage(
  1195. spv::Op::OpStore, 0, 0,
  1196. {{SPV_OPERAND_TYPE_ID, {14}}, {SPV_OPERAND_TYPE_ID, {25}}}));
  1197. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 15, 28,
  1198. {{SPV_OPERAND_TYPE_ID, {10}}}));
  1199. instructions.push_back(MakeInstructionMessage(
  1200. spv::Op::OpAccessChain, 29, 30,
  1201. {{SPV_OPERAND_TYPE_ID, {20}}, {SPV_OPERAND_TYPE_ID, {28}}}));
  1202. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 17, 31,
  1203. {{SPV_OPERAND_TYPE_ID, {30}}}));
  1204. instructions.push_back(MakeInstructionMessage(
  1205. spv::Op::OpStore, 0, 0,
  1206. {{SPV_OPERAND_TYPE_ID, {27}}, {SPV_OPERAND_TYPE_ID, {31}}}));
  1207. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 32,
  1208. {{SPV_OPERAND_TYPE_ID, {9}}}));
  1209. instructions.push_back(MakeInstructionMessage(
  1210. spv::Op::OpInBoundsAccessChain, 7, 34,
  1211. {{SPV_OPERAND_TYPE_ID, {27}}, {SPV_OPERAND_TYPE_ID, {32}}}));
  1212. instructions.push_back(MakeInstructionMessage(
  1213. spv::Op::OpStore, 0, 0,
  1214. {{SPV_OPERAND_TYPE_ID, {34}}, {SPV_OPERAND_TYPE_ID, {33}}}));
  1215. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 39,
  1216. {{SPV_OPERAND_TYPE_ID, {9}}}));
  1217. instructions.push_back(MakeInstructionMessage(
  1218. spv::Op::OpAccessChain, 23, 40,
  1219. {{SPV_OPERAND_TYPE_ID, {38}}, {SPV_OPERAND_TYPE_ID, {33}}}));
  1220. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 41,
  1221. {{SPV_OPERAND_TYPE_ID, {40}}}));
  1222. instructions.push_back(MakeInstructionMessage(
  1223. spv::Op::OpInBoundsAccessChain, 23, 42,
  1224. {{SPV_OPERAND_TYPE_ID, {38}}, {SPV_OPERAND_TYPE_ID, {39}}}));
  1225. instructions.push_back(MakeInstructionMessage(
  1226. spv::Op::OpStore, 0, 0,
  1227. {{SPV_OPERAND_TYPE_ID, {42}}, {SPV_OPERAND_TYPE_ID, {41}}}));
  1228. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 15, 43,
  1229. {{SPV_OPERAND_TYPE_ID, {10}}}));
  1230. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 44,
  1231. {{SPV_OPERAND_TYPE_ID, {11}}}));
  1232. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 45,
  1233. {{SPV_OPERAND_TYPE_ID, {9}}}));
  1234. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 15, 46,
  1235. {{SPV_OPERAND_TYPE_ID, {10}}}));
  1236. instructions.push_back(MakeInstructionMessage(
  1237. spv::Op::OpIAdd, 6, 47,
  1238. {{SPV_OPERAND_TYPE_ID, {45}}, {SPV_OPERAND_TYPE_ID, {46}}}));
  1239. instructions.push_back(MakeInstructionMessage(
  1240. spv::Op::OpAccessChain, 23, 48,
  1241. {{SPV_OPERAND_TYPE_ID, {38}}, {SPV_OPERAND_TYPE_ID, {47}}}));
  1242. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 49,
  1243. {{SPV_OPERAND_TYPE_ID, {48}}}));
  1244. instructions.push_back(MakeInstructionMessage(spv::Op::OpInBoundsAccessChain,
  1245. 23, 50,
  1246. {{SPV_OPERAND_TYPE_ID, {20}},
  1247. {SPV_OPERAND_TYPE_ID, {43}},
  1248. {SPV_OPERAND_TYPE_ID, {44}}}));
  1249. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 51,
  1250. {{SPV_OPERAND_TYPE_ID, {50}}}));
  1251. instructions.push_back(MakeInstructionMessage(
  1252. spv::Op::OpIAdd, 6, 52,
  1253. {{SPV_OPERAND_TYPE_ID, {51}}, {SPV_OPERAND_TYPE_ID, {49}}}));
  1254. instructions.push_back(MakeInstructionMessage(spv::Op::OpAccessChain, 23, 53,
  1255. {{SPV_OPERAND_TYPE_ID, {20}},
  1256. {SPV_OPERAND_TYPE_ID, {43}},
  1257. {SPV_OPERAND_TYPE_ID, {44}}}));
  1258. instructions.push_back(MakeInstructionMessage(
  1259. spv::Op::OpStore, 0, 0,
  1260. {{SPV_OPERAND_TYPE_ID, {53}}, {SPV_OPERAND_TYPE_ID, {52}}}));
  1261. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 15, 58,
  1262. {{SPV_OPERAND_TYPE_ID, {10}}}));
  1263. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 63,
  1264. {{SPV_OPERAND_TYPE_ID, {11}}}));
  1265. instructions.push_back(MakeInstructionMessage(spv::Op::OpAccessChain, 64, 65,
  1266. {{SPV_OPERAND_TYPE_ID, {62}},
  1267. {SPV_OPERAND_TYPE_ID, {21}},
  1268. {SPV_OPERAND_TYPE_ID, {63}}}));
  1269. instructions.push_back(MakeInstructionMessage(spv::Op::OpAccessChain, 64, 101,
  1270. {{SPV_OPERAND_TYPE_ID, {62}},
  1271. {SPV_OPERAND_TYPE_ID, {45}},
  1272. {SPV_OPERAND_TYPE_ID, {46}}}));
  1273. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 54, 66,
  1274. {{SPV_OPERAND_TYPE_ID, {65}}}));
  1275. instructions.push_back(MakeInstructionMessage(
  1276. spv::Op::OpAccessChain, 64, 67,
  1277. {{SPV_OPERAND_TYPE_ID, {57}}, {SPV_OPERAND_TYPE_ID, {58}}}));
  1278. instructions.push_back(MakeInstructionMessage(
  1279. spv::Op::OpStore, 0, 0,
  1280. {{SPV_OPERAND_TYPE_ID, {67}}, {SPV_OPERAND_TYPE_ID, {66}}}));
  1281. instructions.push_back(MakeInstructionMessage(spv::Op::OpLoad, 6, 68,
  1282. {{SPV_OPERAND_TYPE_ID, {9}}}));
  1283. instructions.push_back(MakeInstructionMessage(
  1284. spv::Op::OpInBoundsAccessChain, 64, 70,
  1285. {{SPV_OPERAND_TYPE_ID, {57}}, {SPV_OPERAND_TYPE_ID, {68}}}));
  1286. instructions.push_back(MakeInstructionMessage(
  1287. spv::Op::OpStore, 0, 0,
  1288. {{SPV_OPERAND_TYPE_ID, {70}}, {SPV_OPERAND_TYPE_ID, {69}}}));
  1289. instructions.push_back(MakeInstructionMessage(spv::Op::OpReturn, 0, 0, {}));
  1290. instructions.push_back(
  1291. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  1292. spvtools::ValidatorOptions validator_options;
  1293. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1294. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1295. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1296. context1.get(), validator_options, kConsoleMessageConsumer));
  1297. TransformationContext transformation_context1(
  1298. MakeUnique<FactManager>(context1.get()), validator_options);
  1299. TransformationContext transformation_context2(
  1300. MakeUnique<FactManager>(context2.get()), validator_options);
  1301. TransformationAddFunction add_dead_function(instructions);
  1302. ASSERT_TRUE(
  1303. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  1304. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  1305. &transformation_context1);
  1306. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1307. context1.get(), validator_options, kConsoleMessageConsumer));
  1308. // The function should not be deemed livesafe
  1309. ASSERT_FALSE(
  1310. transformation_context1.GetFactManager()->FunctionIsLivesafe(12));
  1311. // All variables/parameters in the function should be deemed irrelevant.
  1312. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1313. context1.get(), transformation_context1, 12, 0));
  1314. std::string added_as_dead_code = R"(
  1315. OpCapability Shader
  1316. %1 = OpExtInstImport "GLSL.std.450"
  1317. OpMemoryModel Logical GLSL450
  1318. OpEntryPoint Fragment %4 "main"
  1319. OpExecutionMode %4 OriginUpperLeft
  1320. OpSource ESSL 310
  1321. %2 = OpTypeVoid
  1322. %100 = OpTypeBool
  1323. %3 = OpTypeFunction %2
  1324. %6 = OpTypeInt 32 1
  1325. %7 = OpTypePointer Function %6
  1326. %15 = OpTypeInt 32 0
  1327. %102 = OpTypePointer Function %15
  1328. %8 = OpTypeFunction %2 %7 %102 %7
  1329. %16 = OpConstant %15 5
  1330. %17 = OpTypeArray %6 %16
  1331. %18 = OpTypeArray %17 %16
  1332. %19 = OpTypePointer Private %18
  1333. %20 = OpVariable %19 Private
  1334. %21 = OpConstant %6 0
  1335. %23 = OpTypePointer Private %6
  1336. %26 = OpTypePointer Function %17
  1337. %29 = OpTypePointer Private %17
  1338. %33 = OpConstant %6 4
  1339. %200 = OpConstant %15 4
  1340. %35 = OpConstant %15 10
  1341. %36 = OpTypeArray %6 %35
  1342. %37 = OpTypePointer Private %36
  1343. %38 = OpVariable %37 Private
  1344. %54 = OpTypeFloat 32
  1345. %55 = OpTypeVector %54 4
  1346. %56 = OpTypePointer Private %55
  1347. %57 = OpVariable %56 Private
  1348. %59 = OpTypeVector %54 3
  1349. %60 = OpTypeMatrix %59 2
  1350. %61 = OpTypePointer Private %60
  1351. %62 = OpVariable %61 Private
  1352. %64 = OpTypePointer Private %54
  1353. %69 = OpConstant %54 2
  1354. %71 = OpConstant %6 1
  1355. %72 = OpConstant %6 2
  1356. %201 = OpConstant %15 2
  1357. %73 = OpConstant %6 3
  1358. %202 = OpConstant %15 3
  1359. %203 = OpConstant %6 1
  1360. %204 = OpConstant %6 9
  1361. %4 = OpFunction %2 None %3
  1362. %5 = OpLabel
  1363. OpReturn
  1364. OpFunctionEnd
  1365. %12 = OpFunction %2 None %8
  1366. %9 = OpFunctionParameter %7
  1367. %10 = OpFunctionParameter %102
  1368. %11 = OpFunctionParameter %7
  1369. %13 = OpLabel
  1370. %14 = OpVariable %7 Function
  1371. %27 = OpVariable %26 Function
  1372. %22 = OpLoad %6 %11
  1373. %24 = OpAccessChain %23 %20 %21 %22
  1374. %25 = OpLoad %6 %24
  1375. OpStore %14 %25
  1376. %28 = OpLoad %15 %10
  1377. %30 = OpAccessChain %29 %20 %28
  1378. %31 = OpLoad %17 %30
  1379. OpStore %27 %31
  1380. %32 = OpLoad %6 %9
  1381. %34 = OpInBoundsAccessChain %7 %27 %32
  1382. OpStore %34 %33
  1383. %39 = OpLoad %6 %9
  1384. %40 = OpAccessChain %23 %38 %33
  1385. %41 = OpLoad %6 %40
  1386. %42 = OpInBoundsAccessChain %23 %38 %39
  1387. OpStore %42 %41
  1388. %43 = OpLoad %15 %10
  1389. %44 = OpLoad %6 %11
  1390. %45 = OpLoad %6 %9
  1391. %46 = OpLoad %15 %10
  1392. %47 = OpIAdd %6 %45 %46
  1393. %48 = OpAccessChain %23 %38 %47
  1394. %49 = OpLoad %6 %48
  1395. %50 = OpInBoundsAccessChain %23 %20 %43 %44
  1396. %51 = OpLoad %6 %50
  1397. %52 = OpIAdd %6 %51 %49
  1398. %53 = OpAccessChain %23 %20 %43 %44
  1399. OpStore %53 %52
  1400. %58 = OpLoad %15 %10
  1401. %63 = OpLoad %6 %11
  1402. %65 = OpAccessChain %64 %62 %21 %63
  1403. %101 = OpAccessChain %64 %62 %45 %46
  1404. %66 = OpLoad %54 %65
  1405. %67 = OpAccessChain %64 %57 %58
  1406. OpStore %67 %66
  1407. %68 = OpLoad %6 %9
  1408. %70 = OpInBoundsAccessChain %64 %57 %68
  1409. OpStore %70 %69
  1410. OpReturn
  1411. OpFunctionEnd
  1412. )";
  1413. ASSERT_TRUE(IsEqual(env, added_as_dead_code, context1.get()));
  1414. std::vector<protobufs::AccessChainClampingInfo> access_chain_clamping_info;
  1415. access_chain_clamping_info.push_back(
  1416. MakeAccessClampingInfo(24, {{1001, 2001}, {1002, 2002}}));
  1417. access_chain_clamping_info.push_back(
  1418. MakeAccessClampingInfo(30, {{1003, 2003}}));
  1419. access_chain_clamping_info.push_back(
  1420. MakeAccessClampingInfo(34, {{1004, 2004}}));
  1421. access_chain_clamping_info.push_back(
  1422. MakeAccessClampingInfo(40, {{1005, 2005}}));
  1423. access_chain_clamping_info.push_back(
  1424. MakeAccessClampingInfo(42, {{1006, 2006}}));
  1425. access_chain_clamping_info.push_back(
  1426. MakeAccessClampingInfo(48, {{1007, 2007}}));
  1427. access_chain_clamping_info.push_back(
  1428. MakeAccessClampingInfo(50, {{1008, 2008}, {1009, 2009}}));
  1429. access_chain_clamping_info.push_back(
  1430. MakeAccessClampingInfo(53, {{1010, 2010}, {1011, 2011}}));
  1431. access_chain_clamping_info.push_back(
  1432. MakeAccessClampingInfo(65, {{1012, 2012}, {1013, 2013}}));
  1433. access_chain_clamping_info.push_back(
  1434. MakeAccessClampingInfo(101, {{1014, 2014}, {1015, 2015}}));
  1435. access_chain_clamping_info.push_back(
  1436. MakeAccessClampingInfo(67, {{1016, 2016}}));
  1437. access_chain_clamping_info.push_back(
  1438. MakeAccessClampingInfo(70, {{1017, 2017}}));
  1439. TransformationAddFunction add_livesafe_function(instructions, 0, 0, {}, 13,
  1440. access_chain_clamping_info);
  1441. ASSERT_TRUE(add_livesafe_function.IsApplicable(context2.get(),
  1442. transformation_context2));
  1443. ApplyAndCheckFreshIds(add_livesafe_function, context2.get(),
  1444. &transformation_context2);
  1445. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1446. context2.get(), validator_options, kConsoleMessageConsumer));
  1447. // The function should be deemed livesafe
  1448. ASSERT_TRUE(transformation_context2.GetFactManager()->FunctionIsLivesafe(12));
  1449. // All variables/parameters in the function should be deemed irrelevant.
  1450. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1451. context2.get(), transformation_context2, 12, 0));
  1452. std::string added_as_livesafe_code = R"(
  1453. OpCapability Shader
  1454. %1 = OpExtInstImport "GLSL.std.450"
  1455. OpMemoryModel Logical GLSL450
  1456. OpEntryPoint Fragment %4 "main"
  1457. OpExecutionMode %4 OriginUpperLeft
  1458. OpSource ESSL 310
  1459. %2 = OpTypeVoid
  1460. %100 = OpTypeBool
  1461. %3 = OpTypeFunction %2
  1462. %6 = OpTypeInt 32 1
  1463. %7 = OpTypePointer Function %6
  1464. %15 = OpTypeInt 32 0
  1465. %102 = OpTypePointer Function %15
  1466. %8 = OpTypeFunction %2 %7 %102 %7
  1467. %16 = OpConstant %15 5
  1468. %17 = OpTypeArray %6 %16
  1469. %18 = OpTypeArray %17 %16
  1470. %19 = OpTypePointer Private %18
  1471. %20 = OpVariable %19 Private
  1472. %21 = OpConstant %6 0
  1473. %23 = OpTypePointer Private %6
  1474. %26 = OpTypePointer Function %17
  1475. %29 = OpTypePointer Private %17
  1476. %33 = OpConstant %6 4
  1477. %200 = OpConstant %15 4
  1478. %35 = OpConstant %15 10
  1479. %36 = OpTypeArray %6 %35
  1480. %37 = OpTypePointer Private %36
  1481. %38 = OpVariable %37 Private
  1482. %54 = OpTypeFloat 32
  1483. %55 = OpTypeVector %54 4
  1484. %56 = OpTypePointer Private %55
  1485. %57 = OpVariable %56 Private
  1486. %59 = OpTypeVector %54 3
  1487. %60 = OpTypeMatrix %59 2
  1488. %61 = OpTypePointer Private %60
  1489. %62 = OpVariable %61 Private
  1490. %64 = OpTypePointer Private %54
  1491. %69 = OpConstant %54 2
  1492. %71 = OpConstant %6 1
  1493. %72 = OpConstant %6 2
  1494. %201 = OpConstant %15 2
  1495. %73 = OpConstant %6 3
  1496. %202 = OpConstant %15 3
  1497. %203 = OpConstant %6 1
  1498. %204 = OpConstant %6 9
  1499. %4 = OpFunction %2 None %3
  1500. %5 = OpLabel
  1501. OpReturn
  1502. OpFunctionEnd
  1503. %12 = OpFunction %2 None %8
  1504. %9 = OpFunctionParameter %7
  1505. %10 = OpFunctionParameter %102
  1506. %11 = OpFunctionParameter %7
  1507. %13 = OpLabel
  1508. %14 = OpVariable %7 Function
  1509. %27 = OpVariable %26 Function
  1510. %22 = OpLoad %6 %11
  1511. %1002 = OpULessThanEqual %100 %22 %33
  1512. %2002 = OpSelect %6 %1002 %22 %33
  1513. %24 = OpAccessChain %23 %20 %21 %2002
  1514. %25 = OpLoad %6 %24
  1515. OpStore %14 %25
  1516. %28 = OpLoad %15 %10
  1517. %1003 = OpULessThanEqual %100 %28 %200
  1518. %2003 = OpSelect %15 %1003 %28 %200
  1519. %30 = OpAccessChain %29 %20 %2003
  1520. %31 = OpLoad %17 %30
  1521. OpStore %27 %31
  1522. %32 = OpLoad %6 %9
  1523. %1004 = OpULessThanEqual %100 %32 %33
  1524. %2004 = OpSelect %6 %1004 %32 %33
  1525. %34 = OpInBoundsAccessChain %7 %27 %2004
  1526. OpStore %34 %33
  1527. %39 = OpLoad %6 %9
  1528. %40 = OpAccessChain %23 %38 %33
  1529. %41 = OpLoad %6 %40
  1530. %1006 = OpULessThanEqual %100 %39 %204
  1531. %2006 = OpSelect %6 %1006 %39 %204
  1532. %42 = OpInBoundsAccessChain %23 %38 %2006
  1533. OpStore %42 %41
  1534. %43 = OpLoad %15 %10
  1535. %44 = OpLoad %6 %11
  1536. %45 = OpLoad %6 %9
  1537. %46 = OpLoad %15 %10
  1538. %47 = OpIAdd %6 %45 %46
  1539. %1007 = OpULessThanEqual %100 %47 %204
  1540. %2007 = OpSelect %6 %1007 %47 %204
  1541. %48 = OpAccessChain %23 %38 %2007
  1542. %49 = OpLoad %6 %48
  1543. %1008 = OpULessThanEqual %100 %43 %200
  1544. %2008 = OpSelect %15 %1008 %43 %200
  1545. %1009 = OpULessThanEqual %100 %44 %33
  1546. %2009 = OpSelect %6 %1009 %44 %33
  1547. %50 = OpInBoundsAccessChain %23 %20 %2008 %2009
  1548. %51 = OpLoad %6 %50
  1549. %52 = OpIAdd %6 %51 %49
  1550. %1010 = OpULessThanEqual %100 %43 %200
  1551. %2010 = OpSelect %15 %1010 %43 %200
  1552. %1011 = OpULessThanEqual %100 %44 %33
  1553. %2011 = OpSelect %6 %1011 %44 %33
  1554. %53 = OpAccessChain %23 %20 %2010 %2011
  1555. OpStore %53 %52
  1556. %58 = OpLoad %15 %10
  1557. %63 = OpLoad %6 %11
  1558. %1013 = OpULessThanEqual %100 %63 %72
  1559. %2013 = OpSelect %6 %1013 %63 %72
  1560. %65 = OpAccessChain %64 %62 %21 %2013
  1561. %1014 = OpULessThanEqual %100 %45 %71
  1562. %2014 = OpSelect %6 %1014 %45 %71
  1563. %1015 = OpULessThanEqual %100 %46 %201
  1564. %2015 = OpSelect %15 %1015 %46 %201
  1565. %101 = OpAccessChain %64 %62 %2014 %2015
  1566. %66 = OpLoad %54 %65
  1567. %1016 = OpULessThanEqual %100 %58 %202
  1568. %2016 = OpSelect %15 %1016 %58 %202
  1569. %67 = OpAccessChain %64 %57 %2016
  1570. OpStore %67 %66
  1571. %68 = OpLoad %6 %9
  1572. %1017 = OpULessThanEqual %100 %68 %73
  1573. %2017 = OpSelect %6 %1017 %68 %73
  1574. %70 = OpInBoundsAccessChain %64 %57 %2017
  1575. OpStore %70 %69
  1576. OpReturn
  1577. OpFunctionEnd
  1578. )";
  1579. ASSERT_TRUE(IsEqual(env, added_as_livesafe_code, context2.get()));
  1580. }
  1581. TEST(TransformationAddFunctionTest, LivesafeCanCallLivesafe) {
  1582. std::string shader = R"(
  1583. OpCapability Shader
  1584. %1 = OpExtInstImport "GLSL.std.450"
  1585. OpMemoryModel Logical GLSL450
  1586. OpEntryPoint Fragment %4 "main"
  1587. OpExecutionMode %4 OriginUpperLeft
  1588. OpSource ESSL 310
  1589. %2 = OpTypeVoid
  1590. %3 = OpTypeFunction %2
  1591. %4 = OpFunction %2 None %3
  1592. %5 = OpLabel
  1593. OpReturn
  1594. OpFunctionEnd
  1595. %6 = OpFunction %2 None %3
  1596. %7 = OpLabel
  1597. OpReturn
  1598. OpFunctionEnd
  1599. )";
  1600. const auto env = SPV_ENV_UNIVERSAL_1_4;
  1601. const auto consumer = nullptr;
  1602. std::vector<protobufs::Instruction> instructions;
  1603. instructions.push_back(
  1604. MakeInstructionMessage(spv::Op::OpFunction, 2, 8,
  1605. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  1606. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  1607. {SPV_OPERAND_TYPE_TYPE_ID, {3}}}));
  1608. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 9, {}));
  1609. instructions.push_back(MakeInstructionMessage(spv::Op::OpFunctionCall, 2, 11,
  1610. {{SPV_OPERAND_TYPE_ID, {6}}}));
  1611. instructions.push_back(MakeInstructionMessage(spv::Op::OpReturn, 0, 0, {}));
  1612. instructions.push_back(
  1613. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  1614. spvtools::ValidatorOptions validator_options;
  1615. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1616. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1617. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1618. context1.get(), validator_options, kConsoleMessageConsumer));
  1619. TransformationContext transformation_context1(
  1620. MakeUnique<FactManager>(context1.get()), validator_options);
  1621. TransformationContext transformation_context2(
  1622. MakeUnique<FactManager>(context2.get()), validator_options);
  1623. // Mark function 6 as livesafe.
  1624. transformation_context2.GetFactManager()->AddFactFunctionIsLivesafe(6);
  1625. TransformationAddFunction add_dead_function(instructions);
  1626. ASSERT_TRUE(
  1627. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  1628. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  1629. &transformation_context1);
  1630. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1631. context1.get(), validator_options, kConsoleMessageConsumer));
  1632. // The function should not be deemed livesafe
  1633. ASSERT_FALSE(transformation_context1.GetFactManager()->FunctionIsLivesafe(8));
  1634. // All variables/parameters in the function should be deemed irrelevant.
  1635. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1636. context1.get(), transformation_context1, 8, 0));
  1637. std::string added_as_live_or_dead_code = R"(
  1638. OpCapability Shader
  1639. %1 = OpExtInstImport "GLSL.std.450"
  1640. OpMemoryModel Logical GLSL450
  1641. OpEntryPoint Fragment %4 "main"
  1642. OpExecutionMode %4 OriginUpperLeft
  1643. OpSource ESSL 310
  1644. %2 = OpTypeVoid
  1645. %3 = OpTypeFunction %2
  1646. %4 = OpFunction %2 None %3
  1647. %5 = OpLabel
  1648. OpReturn
  1649. OpFunctionEnd
  1650. %6 = OpFunction %2 None %3
  1651. %7 = OpLabel
  1652. OpReturn
  1653. OpFunctionEnd
  1654. %8 = OpFunction %2 None %3
  1655. %9 = OpLabel
  1656. %11 = OpFunctionCall %2 %6
  1657. OpReturn
  1658. OpFunctionEnd
  1659. )";
  1660. ASSERT_TRUE(IsEqual(env, added_as_live_or_dead_code, context1.get()));
  1661. TransformationAddFunction add_livesafe_function(instructions, 0, 0, {}, 0,
  1662. {});
  1663. ASSERT_TRUE(add_livesafe_function.IsApplicable(context2.get(),
  1664. transformation_context2));
  1665. ApplyAndCheckFreshIds(add_livesafe_function, context2.get(),
  1666. &transformation_context2);
  1667. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1668. context2.get(), validator_options, kConsoleMessageConsumer));
  1669. // The function should be deemed livesafe
  1670. ASSERT_TRUE(transformation_context2.GetFactManager()->FunctionIsLivesafe(8));
  1671. // All variables/parameters in the function should be deemed irrelevant.
  1672. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1673. context2.get(), transformation_context2, 8, 0));
  1674. ASSERT_TRUE(IsEqual(env, added_as_live_or_dead_code, context2.get()));
  1675. }
  1676. TEST(TransformationAddFunctionTest, LivesafeOnlyCallsLivesafe) {
  1677. std::string shader = R"(
  1678. OpCapability Shader
  1679. %1 = OpExtInstImport "GLSL.std.450"
  1680. OpMemoryModel Logical GLSL450
  1681. OpEntryPoint Fragment %4 "main"
  1682. OpExecutionMode %4 OriginUpperLeft
  1683. OpSource ESSL 310
  1684. %2 = OpTypeVoid
  1685. %3 = OpTypeFunction %2
  1686. %4 = OpFunction %2 None %3
  1687. %5 = OpLabel
  1688. OpReturn
  1689. OpFunctionEnd
  1690. %6 = OpFunction %2 None %3
  1691. %7 = OpLabel
  1692. OpKill
  1693. OpFunctionEnd
  1694. )";
  1695. const auto env = SPV_ENV_UNIVERSAL_1_4;
  1696. const auto consumer = nullptr;
  1697. std::vector<protobufs::Instruction> instructions;
  1698. instructions.push_back(
  1699. MakeInstructionMessage(spv::Op::OpFunction, 2, 8,
  1700. {{SPV_OPERAND_TYPE_FUNCTION_CONTROL,
  1701. {uint32_t(spv::FunctionControlMask::MaskNone)}},
  1702. {SPV_OPERAND_TYPE_TYPE_ID, {3}}}));
  1703. instructions.push_back(MakeInstructionMessage(spv::Op::OpLabel, 0, 9, {}));
  1704. instructions.push_back(MakeInstructionMessage(spv::Op::OpFunctionCall, 2, 11,
  1705. {{SPV_OPERAND_TYPE_ID, {6}}}));
  1706. instructions.push_back(MakeInstructionMessage(spv::Op::OpReturn, 0, 0, {}));
  1707. instructions.push_back(
  1708. MakeInstructionMessage(spv::Op::OpFunctionEnd, 0, 0, {}));
  1709. spvtools::ValidatorOptions validator_options;
  1710. const auto context1 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1711. const auto context2 = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1712. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1713. context1.get(), validator_options, kConsoleMessageConsumer));
  1714. TransformationContext transformation_context1(
  1715. MakeUnique<FactManager>(context1.get()), validator_options);
  1716. TransformationContext transformation_context2(
  1717. MakeUnique<FactManager>(context2.get()), validator_options);
  1718. TransformationAddFunction add_dead_function(instructions);
  1719. ASSERT_TRUE(
  1720. add_dead_function.IsApplicable(context1.get(), transformation_context1));
  1721. ApplyAndCheckFreshIds(add_dead_function, context1.get(),
  1722. &transformation_context1);
  1723. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1724. context1.get(), validator_options, kConsoleMessageConsumer));
  1725. // The function should not be deemed livesafe
  1726. ASSERT_FALSE(transformation_context1.GetFactManager()->FunctionIsLivesafe(8));
  1727. // All variables/parameters in the function should be deemed irrelevant.
  1728. ASSERT_TRUE(AllVariablesAndParametersExceptLoopLimiterAreIrrelevant(
  1729. context1.get(), transformation_context1, 8, 0));
  1730. std::string added_as_dead_code = R"(
  1731. OpCapability Shader
  1732. %1 = OpExtInstImport "GLSL.std.450"
  1733. OpMemoryModel Logical GLSL450
  1734. OpEntryPoint Fragment %4 "main"
  1735. OpExecutionMode %4 OriginUpperLeft
  1736. OpSource ESSL 310
  1737. %2 = OpTypeVoid
  1738. %3 = OpTypeFunction %2
  1739. %4 = OpFunction %2 None %3
  1740. %5 = OpLabel
  1741. OpReturn
  1742. OpFunctionEnd
  1743. %6 = OpFunction %2 None %3
  1744. %7 = OpLabel
  1745. OpKill
  1746. OpFunctionEnd
  1747. %8 = OpFunction %2 None %3
  1748. %9 = OpLabel
  1749. %11 = OpFunctionCall %2 %6
  1750. OpReturn
  1751. OpFunctionEnd
  1752. )";
  1753. ASSERT_TRUE(IsEqual(env, added_as_dead_code, context1.get()));
  1754. TransformationAddFunction add_livesafe_function(instructions, 0, 0, {}, 0,
  1755. {});
  1756. ASSERT_FALSE(add_livesafe_function.IsApplicable(context2.get(),
  1757. transformation_context2));
  1758. }
  1759. TEST(TransformationAddFunctionTest,
  1760. LoopLimitersBackEdgeBlockEndsWithConditional1) {
  1761. std::string shader = R"(
  1762. OpCapability Shader
  1763. %1 = OpExtInstImport "GLSL.std.450"
  1764. OpMemoryModel Logical GLSL450
  1765. OpEntryPoint Fragment %4 "main"
  1766. OpExecutionMode %4 OriginUpperLeft
  1767. OpSource ESSL 310
  1768. %2 = OpTypeVoid
  1769. %3 = OpTypeFunction %2
  1770. %8 = OpTypeInt 32 1
  1771. %9 = OpTypePointer Function %8
  1772. %11 = OpConstant %8 0
  1773. %18 = OpConstant %8 10
  1774. %19 = OpTypeBool
  1775. %26 = OpConstantTrue %19
  1776. %27 = OpConstantFalse %19
  1777. %28 = OpTypeInt 32 0
  1778. %29 = OpTypePointer Function %28
  1779. %30 = OpConstant %28 0
  1780. %31 = OpConstant %28 1
  1781. %32 = OpConstant %28 5
  1782. %22 = OpConstant %8 1
  1783. %4 = OpFunction %2 None %3
  1784. %5 = OpLabel
  1785. OpReturn
  1786. OpFunctionEnd
  1787. )";
  1788. std::string donor = R"(
  1789. OpCapability Shader
  1790. %1 = OpExtInstImport "GLSL.std.450"
  1791. OpMemoryModel Logical GLSL450
  1792. OpEntryPoint Fragment %4 "main"
  1793. OpExecutionMode %4 OriginUpperLeft
  1794. OpSource ESSL 310
  1795. %2 = OpTypeVoid
  1796. %3 = OpTypeFunction %2
  1797. %8 = OpTypeInt 32 1
  1798. %9 = OpTypePointer Function %8
  1799. %11 = OpConstant %8 0
  1800. %18 = OpConstant %8 10
  1801. %19 = OpTypeBool
  1802. %26 = OpConstantTrue %19
  1803. %27 = OpConstantFalse %19
  1804. %28 = OpTypeInt 32 0
  1805. %29 = OpTypePointer Function %28
  1806. %30 = OpConstant %28 0
  1807. %31 = OpConstant %28 1
  1808. %32 = OpConstant %28 5
  1809. %22 = OpConstant %8 1
  1810. %4 = OpFunction %2 None %3
  1811. %5 = OpLabel
  1812. OpReturn
  1813. OpFunctionEnd
  1814. %6 = OpFunction %2 None %3
  1815. %7 = OpLabel
  1816. %10 = OpVariable %9 Function
  1817. OpStore %10 %11
  1818. OpBranch %12
  1819. %12 = OpLabel
  1820. OpLoopMerge %14 %15 None
  1821. OpBranch %15
  1822. %15 = OpLabel
  1823. %17 = OpLoad %8 %10
  1824. %20 = OpSLessThan %19 %17 %18
  1825. %21 = OpLoad %8 %10
  1826. %23 = OpIAdd %8 %21 %22
  1827. OpStore %10 %23
  1828. OpBranchConditional %20 %12 %14
  1829. %14 = OpLabel
  1830. OpReturn
  1831. OpFunctionEnd
  1832. )";
  1833. const auto env = SPV_ENV_UNIVERSAL_1_4;
  1834. const auto consumer = nullptr;
  1835. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1836. spvtools::ValidatorOptions validator_options;
  1837. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  1838. kConsoleMessageConsumer));
  1839. TransformationContext transformation_context(
  1840. MakeUnique<FactManager>(context.get()), validator_options);
  1841. // Make a sequence of instruction messages corresponding to function %6 in
  1842. // |donor|.
  1843. std::vector<protobufs::Instruction> instructions =
  1844. GetInstructionsForFunction(env, consumer, donor, 6);
  1845. protobufs::LoopLimiterInfo loop_limiter_info;
  1846. loop_limiter_info.set_loop_header_id(12);
  1847. loop_limiter_info.set_load_id(102);
  1848. loop_limiter_info.set_increment_id(103);
  1849. loop_limiter_info.set_compare_id(104);
  1850. loop_limiter_info.set_logical_op_id(105);
  1851. TransformationAddFunction add_livesafe_function(instructions, 100, 32,
  1852. {loop_limiter_info}, 0, {});
  1853. ASSERT_TRUE(add_livesafe_function.IsApplicable(context.get(),
  1854. transformation_context));
  1855. ApplyAndCheckFreshIds(add_livesafe_function, context.get(),
  1856. &transformation_context);
  1857. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  1858. kConsoleMessageConsumer));
  1859. std::string expected = R"(
  1860. OpCapability Shader
  1861. %1 = OpExtInstImport "GLSL.std.450"
  1862. OpMemoryModel Logical GLSL450
  1863. OpEntryPoint Fragment %4 "main"
  1864. OpExecutionMode %4 OriginUpperLeft
  1865. OpSource ESSL 310
  1866. %2 = OpTypeVoid
  1867. %3 = OpTypeFunction %2
  1868. %8 = OpTypeInt 32 1
  1869. %9 = OpTypePointer Function %8
  1870. %11 = OpConstant %8 0
  1871. %18 = OpConstant %8 10
  1872. %19 = OpTypeBool
  1873. %26 = OpConstantTrue %19
  1874. %27 = OpConstantFalse %19
  1875. %28 = OpTypeInt 32 0
  1876. %29 = OpTypePointer Function %28
  1877. %30 = OpConstant %28 0
  1878. %31 = OpConstant %28 1
  1879. %32 = OpConstant %28 5
  1880. %22 = OpConstant %8 1
  1881. %4 = OpFunction %2 None %3
  1882. %5 = OpLabel
  1883. OpReturn
  1884. OpFunctionEnd
  1885. %6 = OpFunction %2 None %3
  1886. %7 = OpLabel
  1887. %100 = OpVariable %29 Function %30
  1888. %10 = OpVariable %9 Function
  1889. OpStore %10 %11
  1890. OpBranch %12
  1891. %12 = OpLabel
  1892. OpLoopMerge %14 %15 None
  1893. OpBranch %15
  1894. %15 = OpLabel
  1895. %17 = OpLoad %8 %10
  1896. %20 = OpSLessThan %19 %17 %18
  1897. %21 = OpLoad %8 %10
  1898. %23 = OpIAdd %8 %21 %22
  1899. OpStore %10 %23
  1900. %102 = OpLoad %28 %100
  1901. %103 = OpIAdd %28 %102 %31
  1902. OpStore %100 %103
  1903. %104 = OpULessThan %19 %102 %32
  1904. %105 = OpLogicalAnd %19 %20 %104
  1905. OpBranchConditional %105 %12 %14
  1906. %14 = OpLabel
  1907. OpReturn
  1908. OpFunctionEnd
  1909. )";
  1910. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  1911. }
  1912. TEST(TransformationAddFunctionTest,
  1913. LoopLimitersBackEdgeBlockEndsWithConditional2) {
  1914. std::string shader = R"(
  1915. OpCapability Shader
  1916. %1 = OpExtInstImport "GLSL.std.450"
  1917. OpMemoryModel Logical GLSL450
  1918. OpEntryPoint Fragment %4 "main"
  1919. OpExecutionMode %4 OriginUpperLeft
  1920. OpSource ESSL 310
  1921. %2 = OpTypeVoid
  1922. %3 = OpTypeFunction %2
  1923. %8 = OpTypeInt 32 1
  1924. %9 = OpTypePointer Function %8
  1925. %11 = OpConstant %8 0
  1926. %18 = OpConstant %8 10
  1927. %19 = OpTypeBool
  1928. %26 = OpConstantTrue %19
  1929. %27 = OpConstantFalse %19
  1930. %28 = OpTypeInt 32 0
  1931. %29 = OpTypePointer Function %28
  1932. %30 = OpConstant %28 0
  1933. %31 = OpConstant %28 1
  1934. %32 = OpConstant %28 5
  1935. %22 = OpConstant %8 1
  1936. %4 = OpFunction %2 None %3
  1937. %5 = OpLabel
  1938. OpReturn
  1939. OpFunctionEnd
  1940. )";
  1941. std::string donor = R"(
  1942. OpCapability Shader
  1943. %1 = OpExtInstImport "GLSL.std.450"
  1944. OpMemoryModel Logical GLSL450
  1945. OpEntryPoint Fragment %4 "main"
  1946. OpExecutionMode %4 OriginUpperLeft
  1947. OpSource ESSL 310
  1948. %2 = OpTypeVoid
  1949. %3 = OpTypeFunction %2
  1950. %8 = OpTypeInt 32 1
  1951. %9 = OpTypePointer Function %8
  1952. %11 = OpConstant %8 0
  1953. %18 = OpConstant %8 10
  1954. %19 = OpTypeBool
  1955. %26 = OpConstantTrue %19
  1956. %27 = OpConstantFalse %19
  1957. %28 = OpTypeInt 32 0
  1958. %29 = OpTypePointer Function %28
  1959. %30 = OpConstant %28 0
  1960. %31 = OpConstant %28 1
  1961. %32 = OpConstant %28 5
  1962. %22 = OpConstant %8 1
  1963. %4 = OpFunction %2 None %3
  1964. %5 = OpLabel
  1965. OpReturn
  1966. OpFunctionEnd
  1967. %6 = OpFunction %2 None %3
  1968. %7 = OpLabel
  1969. %10 = OpVariable %9 Function
  1970. OpStore %10 %11
  1971. OpBranch %12
  1972. %12 = OpLabel
  1973. OpLoopMerge %14 %15 None
  1974. OpBranch %15
  1975. %15 = OpLabel
  1976. %17 = OpLoad %8 %10
  1977. %20 = OpSLessThan %19 %17 %18
  1978. %21 = OpLoad %8 %10
  1979. %23 = OpIAdd %8 %21 %22
  1980. OpStore %10 %23
  1981. %50 = OpLogicalNot %19 %20
  1982. OpBranchConditional %50 %14 %12
  1983. %14 = OpLabel
  1984. OpReturn
  1985. OpFunctionEnd
  1986. )";
  1987. const auto env = SPV_ENV_UNIVERSAL_1_4;
  1988. const auto consumer = nullptr;
  1989. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  1990. spvtools::ValidatorOptions validator_options;
  1991. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  1992. kConsoleMessageConsumer));
  1993. TransformationContext transformation_context(
  1994. MakeUnique<FactManager>(context.get()), validator_options);
  1995. // Make a sequence of instruction messages corresponding to function %6 in
  1996. // |donor|.
  1997. std::vector<protobufs::Instruction> instructions =
  1998. GetInstructionsForFunction(env, consumer, donor, 6);
  1999. protobufs::LoopLimiterInfo loop_limiter_info;
  2000. loop_limiter_info.set_loop_header_id(12);
  2001. loop_limiter_info.set_load_id(102);
  2002. loop_limiter_info.set_increment_id(103);
  2003. loop_limiter_info.set_compare_id(104);
  2004. loop_limiter_info.set_logical_op_id(105);
  2005. TransformationAddFunction add_livesafe_function(instructions, 100, 32,
  2006. {loop_limiter_info}, 0, {});
  2007. ASSERT_TRUE(add_livesafe_function.IsApplicable(context.get(),
  2008. transformation_context));
  2009. ApplyAndCheckFreshIds(add_livesafe_function, context.get(),
  2010. &transformation_context);
  2011. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2012. kConsoleMessageConsumer));
  2013. std::string expected = R"(
  2014. OpCapability Shader
  2015. %1 = OpExtInstImport "GLSL.std.450"
  2016. OpMemoryModel Logical GLSL450
  2017. OpEntryPoint Fragment %4 "main"
  2018. OpExecutionMode %4 OriginUpperLeft
  2019. OpSource ESSL 310
  2020. %2 = OpTypeVoid
  2021. %3 = OpTypeFunction %2
  2022. %8 = OpTypeInt 32 1
  2023. %9 = OpTypePointer Function %8
  2024. %11 = OpConstant %8 0
  2025. %18 = OpConstant %8 10
  2026. %19 = OpTypeBool
  2027. %26 = OpConstantTrue %19
  2028. %27 = OpConstantFalse %19
  2029. %28 = OpTypeInt 32 0
  2030. %29 = OpTypePointer Function %28
  2031. %30 = OpConstant %28 0
  2032. %31 = OpConstant %28 1
  2033. %32 = OpConstant %28 5
  2034. %22 = OpConstant %8 1
  2035. %4 = OpFunction %2 None %3
  2036. %5 = OpLabel
  2037. OpReturn
  2038. OpFunctionEnd
  2039. %6 = OpFunction %2 None %3
  2040. %7 = OpLabel
  2041. %100 = OpVariable %29 Function %30
  2042. %10 = OpVariable %9 Function
  2043. OpStore %10 %11
  2044. OpBranch %12
  2045. %12 = OpLabel
  2046. OpLoopMerge %14 %15 None
  2047. OpBranch %15
  2048. %15 = OpLabel
  2049. %17 = OpLoad %8 %10
  2050. %20 = OpSLessThan %19 %17 %18
  2051. %21 = OpLoad %8 %10
  2052. %23 = OpIAdd %8 %21 %22
  2053. OpStore %10 %23
  2054. %50 = OpLogicalNot %19 %20
  2055. %102 = OpLoad %28 %100
  2056. %103 = OpIAdd %28 %102 %31
  2057. OpStore %100 %103
  2058. %104 = OpUGreaterThanEqual %19 %102 %32
  2059. %105 = OpLogicalOr %19 %50 %104
  2060. OpBranchConditional %105 %14 %12
  2061. %14 = OpLabel
  2062. OpReturn
  2063. OpFunctionEnd
  2064. )";
  2065. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2066. }
  2067. TEST(TransformationAddFunctionTest, LoopLimitersHeaderIsBackEdgeBlock) {
  2068. std::string shader = R"(
  2069. OpCapability Shader
  2070. %1 = OpExtInstImport "GLSL.std.450"
  2071. OpMemoryModel Logical GLSL450
  2072. OpEntryPoint Fragment %4 "main"
  2073. OpExecutionMode %4 OriginUpperLeft
  2074. OpSource ESSL 310
  2075. %2 = OpTypeVoid
  2076. %3 = OpTypeFunction %2
  2077. %8 = OpTypeInt 32 1
  2078. %9 = OpTypePointer Function %8
  2079. %11 = OpConstant %8 0
  2080. %18 = OpConstant %8 10
  2081. %19 = OpTypeBool
  2082. %26 = OpConstantTrue %19
  2083. %27 = OpConstantFalse %19
  2084. %28 = OpTypeInt 32 0
  2085. %29 = OpTypePointer Function %28
  2086. %30 = OpConstant %28 0
  2087. %31 = OpConstant %28 1
  2088. %32 = OpConstant %28 5
  2089. %22 = OpConstant %8 1
  2090. %4 = OpFunction %2 None %3
  2091. %5 = OpLabel
  2092. OpReturn
  2093. OpFunctionEnd
  2094. )";
  2095. std::string donor = R"(
  2096. OpCapability Shader
  2097. %1 = OpExtInstImport "GLSL.std.450"
  2098. OpMemoryModel Logical GLSL450
  2099. OpEntryPoint Fragment %4 "main"
  2100. OpExecutionMode %4 OriginUpperLeft
  2101. OpSource ESSL 310
  2102. %2 = OpTypeVoid
  2103. %3 = OpTypeFunction %2
  2104. %8 = OpTypeInt 32 1
  2105. %9 = OpTypePointer Function %8
  2106. %11 = OpConstant %8 0
  2107. %18 = OpConstant %8 10
  2108. %19 = OpTypeBool
  2109. %26 = OpConstantTrue %19
  2110. %27 = OpConstantFalse %19
  2111. %28 = OpTypeInt 32 0
  2112. %29 = OpTypePointer Function %28
  2113. %30 = OpConstant %28 0
  2114. %31 = OpConstant %28 1
  2115. %32 = OpConstant %28 5
  2116. %22 = OpConstant %8 1
  2117. %4 = OpFunction %2 None %3
  2118. %5 = OpLabel
  2119. OpReturn
  2120. OpFunctionEnd
  2121. %6 = OpFunction %2 None %3
  2122. %7 = OpLabel
  2123. %10 = OpVariable %9 Function
  2124. OpStore %10 %11
  2125. OpBranch %12
  2126. %12 = OpLabel
  2127. %17 = OpLoad %8 %10
  2128. %20 = OpSLessThan %19 %17 %18
  2129. %21 = OpLoad %8 %10
  2130. %23 = OpIAdd %8 %21 %22
  2131. OpStore %10 %23
  2132. %50 = OpLogicalNot %19 %20
  2133. OpLoopMerge %14 %12 None
  2134. OpBranchConditional %50 %14 %12
  2135. %14 = OpLabel
  2136. OpReturn
  2137. OpFunctionEnd
  2138. )";
  2139. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2140. const auto consumer = nullptr;
  2141. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2142. spvtools::ValidatorOptions validator_options;
  2143. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2144. kConsoleMessageConsumer));
  2145. TransformationContext transformation_context(
  2146. MakeUnique<FactManager>(context.get()), validator_options);
  2147. // Make a sequence of instruction messages corresponding to function %6 in
  2148. // |donor|.
  2149. std::vector<protobufs::Instruction> instructions =
  2150. GetInstructionsForFunction(env, consumer, donor, 6);
  2151. protobufs::LoopLimiterInfo loop_limiter_info;
  2152. loop_limiter_info.set_loop_header_id(12);
  2153. loop_limiter_info.set_load_id(102);
  2154. loop_limiter_info.set_increment_id(103);
  2155. loop_limiter_info.set_compare_id(104);
  2156. loop_limiter_info.set_logical_op_id(105);
  2157. TransformationAddFunction add_livesafe_function(instructions, 100, 32,
  2158. {loop_limiter_info}, 0, {});
  2159. ASSERT_TRUE(add_livesafe_function.IsApplicable(context.get(),
  2160. transformation_context));
  2161. ApplyAndCheckFreshIds(add_livesafe_function, context.get(),
  2162. &transformation_context);
  2163. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2164. kConsoleMessageConsumer));
  2165. std::string expected = R"(
  2166. OpCapability Shader
  2167. %1 = OpExtInstImport "GLSL.std.450"
  2168. OpMemoryModel Logical GLSL450
  2169. OpEntryPoint Fragment %4 "main"
  2170. OpExecutionMode %4 OriginUpperLeft
  2171. OpSource ESSL 310
  2172. %2 = OpTypeVoid
  2173. %3 = OpTypeFunction %2
  2174. %8 = OpTypeInt 32 1
  2175. %9 = OpTypePointer Function %8
  2176. %11 = OpConstant %8 0
  2177. %18 = OpConstant %8 10
  2178. %19 = OpTypeBool
  2179. %26 = OpConstantTrue %19
  2180. %27 = OpConstantFalse %19
  2181. %28 = OpTypeInt 32 0
  2182. %29 = OpTypePointer Function %28
  2183. %30 = OpConstant %28 0
  2184. %31 = OpConstant %28 1
  2185. %32 = OpConstant %28 5
  2186. %22 = OpConstant %8 1
  2187. %4 = OpFunction %2 None %3
  2188. %5 = OpLabel
  2189. OpReturn
  2190. OpFunctionEnd
  2191. %6 = OpFunction %2 None %3
  2192. %7 = OpLabel
  2193. %100 = OpVariable %29 Function %30
  2194. %10 = OpVariable %9 Function
  2195. OpStore %10 %11
  2196. OpBranch %12
  2197. %12 = OpLabel
  2198. %17 = OpLoad %8 %10
  2199. %20 = OpSLessThan %19 %17 %18
  2200. %21 = OpLoad %8 %10
  2201. %23 = OpIAdd %8 %21 %22
  2202. OpStore %10 %23
  2203. %50 = OpLogicalNot %19 %20
  2204. %102 = OpLoad %28 %100
  2205. %103 = OpIAdd %28 %102 %31
  2206. OpStore %100 %103
  2207. %104 = OpUGreaterThanEqual %19 %102 %32
  2208. %105 = OpLogicalOr %19 %50 %104
  2209. OpLoopMerge %14 %12 None
  2210. OpBranchConditional %105 %14 %12
  2211. %14 = OpLabel
  2212. OpReturn
  2213. OpFunctionEnd
  2214. )";
  2215. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2216. }
  2217. TEST(TransformationAddFunctionTest, InfiniteLoop) {
  2218. std::string shader = R"(
  2219. OpCapability Shader
  2220. %1 = OpExtInstImport "GLSL.std.450"
  2221. OpMemoryModel Logical GLSL450
  2222. OpEntryPoint Fragment %4 "main"
  2223. OpExecutionMode %4 OriginUpperLeft
  2224. OpSource ESSL 310
  2225. %2 = OpTypeVoid
  2226. %3 = OpTypeFunction %2
  2227. %8 = OpTypeInt 32 1
  2228. %9 = OpTypePointer Function %8
  2229. %11 = OpConstant %8 0
  2230. %18 = OpConstant %8 10
  2231. %19 = OpTypeBool
  2232. %26 = OpConstantTrue %19
  2233. %27 = OpConstantFalse %19
  2234. %28 = OpTypeInt 32 0
  2235. %29 = OpTypePointer Function %28
  2236. %30 = OpConstant %28 0
  2237. %31 = OpConstant %28 1
  2238. %32 = OpConstant %28 5
  2239. %22 = OpConstant %8 1
  2240. %4 = OpFunction %2 None %3
  2241. %5 = OpLabel
  2242. OpReturn
  2243. OpFunctionEnd
  2244. )";
  2245. std::string donor = R"(
  2246. OpCapability Shader
  2247. %1 = OpExtInstImport "GLSL.std.450"
  2248. OpMemoryModel Logical GLSL450
  2249. OpEntryPoint Fragment %4 "main"
  2250. OpExecutionMode %4 OriginUpperLeft
  2251. OpSource ESSL 310
  2252. %2 = OpTypeVoid
  2253. %3 = OpTypeFunction %2
  2254. %8 = OpTypeInt 32 1
  2255. %9 = OpTypePointer Function %8
  2256. %11 = OpConstant %8 0
  2257. %18 = OpConstant %8 10
  2258. %19 = OpTypeBool
  2259. %26 = OpConstantTrue %19
  2260. %27 = OpConstantFalse %19
  2261. %28 = OpTypeInt 32 0
  2262. %29 = OpTypePointer Function %28
  2263. %30 = OpConstant %28 0
  2264. %31 = OpConstant %28 1
  2265. %32 = OpConstant %28 5
  2266. %22 = OpConstant %8 1
  2267. %4 = OpFunction %2 None %3
  2268. %5 = OpLabel
  2269. OpReturn
  2270. OpFunctionEnd
  2271. %6 = OpFunction %2 None %3
  2272. %7 = OpLabel
  2273. %10 = OpVariable %9 Function
  2274. OpStore %10 %11
  2275. OpBranch %12
  2276. %12 = OpLabel
  2277. OpLoopMerge %14 %12 None
  2278. OpBranch %12
  2279. %14 = OpLabel
  2280. OpReturn
  2281. OpFunctionEnd
  2282. )";
  2283. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2284. const auto consumer = nullptr;
  2285. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2286. spvtools::ValidatorOptions validator_options;
  2287. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2288. kConsoleMessageConsumer));
  2289. TransformationContext transformation_context(
  2290. MakeUnique<FactManager>(context.get()), validator_options);
  2291. // Make a sequence of instruction messages corresponding to function %6 in
  2292. // |donor|.
  2293. std::vector<protobufs::Instruction> instructions =
  2294. GetInstructionsForFunction(env, consumer, donor, 6);
  2295. protobufs::LoopLimiterInfo loop_limiter_info;
  2296. loop_limiter_info.set_loop_header_id(12);
  2297. loop_limiter_info.set_load_id(102);
  2298. loop_limiter_info.set_increment_id(103);
  2299. loop_limiter_info.set_compare_id(104);
  2300. loop_limiter_info.set_logical_op_id(105);
  2301. TransformationAddFunction add_livesafe_function(instructions, 100, 32,
  2302. {loop_limiter_info}, 0, {});
  2303. // To make sure the loop's merge block is reachable, it must be dominated by
  2304. // the loop header.
  2305. ASSERT_FALSE(add_livesafe_function.IsApplicable(context.get(),
  2306. transformation_context));
  2307. }
  2308. TEST(TransformationAddFunctionTest, UnreachableContinueConstruct) {
  2309. // This captures the case where the loop's continue construct is statically
  2310. // unreachable. In this case the loop cannot iterate and so we do not add
  2311. // a loop limiter. (The reason we do not just add one anyway is that
  2312. // detecting which block would be the back-edge block is difficult in the
  2313. // absence of reliable dominance information.)
  2314. std::string shader = R"(
  2315. OpCapability Shader
  2316. %1 = OpExtInstImport "GLSL.std.450"
  2317. OpMemoryModel Logical GLSL450
  2318. OpEntryPoint Fragment %4 "main"
  2319. OpExecutionMode %4 OriginUpperLeft
  2320. OpSource ESSL 310
  2321. %2 = OpTypeVoid
  2322. %3 = OpTypeFunction %2
  2323. %8 = OpTypeInt 32 1
  2324. %9 = OpTypePointer Function %8
  2325. %11 = OpConstant %8 0
  2326. %18 = OpConstant %8 10
  2327. %19 = OpTypeBool
  2328. %23 = OpConstant %8 1
  2329. %26 = OpConstantTrue %19
  2330. %27 = OpConstantFalse %19
  2331. %28 = OpTypeInt 32 0
  2332. %29 = OpTypePointer Function %28
  2333. %30 = OpConstant %28 0
  2334. %31 = OpConstant %28 1
  2335. %32 = OpConstant %28 5
  2336. %4 = OpFunction %2 None %3
  2337. %5 = OpLabel
  2338. OpReturn
  2339. OpFunctionEnd
  2340. )";
  2341. std::string donor = R"(
  2342. OpCapability Shader
  2343. %1 = OpExtInstImport "GLSL.std.450"
  2344. OpMemoryModel Logical GLSL450
  2345. OpEntryPoint Fragment %4 "main"
  2346. OpExecutionMode %4 OriginUpperLeft
  2347. OpSource ESSL 310
  2348. %2 = OpTypeVoid
  2349. %3 = OpTypeFunction %2
  2350. %8 = OpTypeInt 32 1
  2351. %9 = OpTypePointer Function %8
  2352. %11 = OpConstant %8 0
  2353. %18 = OpConstant %8 10
  2354. %19 = OpTypeBool
  2355. %23 = OpConstant %8 1
  2356. %26 = OpConstantTrue %19
  2357. %27 = OpConstantFalse %19
  2358. %28 = OpTypeInt 32 0
  2359. %29 = OpTypePointer Function %28
  2360. %30 = OpConstant %28 0
  2361. %31 = OpConstant %28 1
  2362. %32 = OpConstant %28 5
  2363. %4 = OpFunction %2 None %3
  2364. %5 = OpLabel
  2365. OpReturn
  2366. OpFunctionEnd
  2367. %6 = OpFunction %2 None %3
  2368. %7 = OpLabel
  2369. %10 = OpVariable %9 Function
  2370. OpStore %10 %11
  2371. OpBranch %12
  2372. %12 = OpLabel
  2373. OpLoopMerge %14 %15 None
  2374. OpBranch %16
  2375. %16 = OpLabel
  2376. %17 = OpLoad %8 %10
  2377. %20 = OpSLessThan %19 %17 %18
  2378. OpBranchConditional %20 %13 %14
  2379. %13 = OpLabel
  2380. OpBranch %14
  2381. %15 = OpLabel
  2382. %22 = OpLoad %8 %10
  2383. %24 = OpIAdd %8 %22 %23
  2384. OpStore %10 %24
  2385. OpBranch %12
  2386. %14 = OpLabel
  2387. OpReturn
  2388. OpFunctionEnd
  2389. )";
  2390. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2391. const auto consumer = nullptr;
  2392. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2393. spvtools::ValidatorOptions validator_options;
  2394. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2395. kConsoleMessageConsumer));
  2396. TransformationContext transformation_context(
  2397. MakeUnique<FactManager>(context.get()), validator_options);
  2398. // Make a sequence of instruction messages corresponding to function %6 in
  2399. // |donor|.
  2400. std::vector<protobufs::Instruction> instructions =
  2401. GetInstructionsForFunction(env, consumer, donor, 6);
  2402. protobufs::LoopLimiterInfo loop_limiter_info;
  2403. loop_limiter_info.set_loop_header_id(12);
  2404. loop_limiter_info.set_load_id(102);
  2405. loop_limiter_info.set_increment_id(103);
  2406. loop_limiter_info.set_compare_id(104);
  2407. loop_limiter_info.set_logical_op_id(105);
  2408. TransformationAddFunction add_livesafe_function(instructions, 100, 32,
  2409. {loop_limiter_info}, 0, {});
  2410. ASSERT_TRUE(add_livesafe_function.IsApplicable(context.get(),
  2411. transformation_context));
  2412. ApplyAndCheckFreshIds(add_livesafe_function, context.get(),
  2413. &transformation_context);
  2414. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2415. kConsoleMessageConsumer));
  2416. std::string expected = R"(
  2417. OpCapability Shader
  2418. %1 = OpExtInstImport "GLSL.std.450"
  2419. OpMemoryModel Logical GLSL450
  2420. OpEntryPoint Fragment %4 "main"
  2421. OpExecutionMode %4 OriginUpperLeft
  2422. OpSource ESSL 310
  2423. %2 = OpTypeVoid
  2424. %3 = OpTypeFunction %2
  2425. %8 = OpTypeInt 32 1
  2426. %9 = OpTypePointer Function %8
  2427. %11 = OpConstant %8 0
  2428. %18 = OpConstant %8 10
  2429. %19 = OpTypeBool
  2430. %23 = OpConstant %8 1
  2431. %26 = OpConstantTrue %19
  2432. %27 = OpConstantFalse %19
  2433. %28 = OpTypeInt 32 0
  2434. %29 = OpTypePointer Function %28
  2435. %30 = OpConstant %28 0
  2436. %31 = OpConstant %28 1
  2437. %32 = OpConstant %28 5
  2438. %4 = OpFunction %2 None %3
  2439. %5 = OpLabel
  2440. OpReturn
  2441. OpFunctionEnd
  2442. %6 = OpFunction %2 None %3
  2443. %7 = OpLabel
  2444. %100 = OpVariable %29 Function %30
  2445. %10 = OpVariable %9 Function
  2446. OpStore %10 %11
  2447. OpBranch %12
  2448. %12 = OpLabel
  2449. OpLoopMerge %14 %15 None
  2450. OpBranch %16
  2451. %16 = OpLabel
  2452. %17 = OpLoad %8 %10
  2453. %20 = OpSLessThan %19 %17 %18
  2454. OpBranchConditional %20 %13 %14
  2455. %13 = OpLabel
  2456. OpBranch %14
  2457. %15 = OpLabel
  2458. %22 = OpLoad %8 %10
  2459. %24 = OpIAdd %8 %22 %23
  2460. OpStore %10 %24
  2461. OpBranch %12
  2462. %14 = OpLabel
  2463. OpReturn
  2464. OpFunctionEnd
  2465. )";
  2466. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2467. }
  2468. TEST(TransformationAddFunctionTest, LoopLimitersAndOpPhi1) {
  2469. // This captures the scenario where breaking a loop due to a loop limiter
  2470. // requires patching up OpPhi instructions occurring at the loop merge block.
  2471. std::string shader = R"(
  2472. OpCapability Shader
  2473. %1 = OpExtInstImport "GLSL.std.450"
  2474. OpMemoryModel Logical GLSL450
  2475. OpEntryPoint Fragment %4 "main"
  2476. OpExecutionMode %4 OriginUpperLeft
  2477. OpSource ESSL 310
  2478. %2 = OpTypeVoid
  2479. %3 = OpTypeFunction %2
  2480. %6 = OpTypeInt 32 1
  2481. %50 = OpTypeInt 32 0
  2482. %51 = OpConstant %50 0
  2483. %52 = OpConstant %50 1
  2484. %53 = OpTypePointer Function %50
  2485. %7 = OpTypeFunction %6
  2486. %10 = OpTypePointer Function %6
  2487. %12 = OpConstant %6 0
  2488. %19 = OpConstant %6 100
  2489. %20 = OpTypeBool
  2490. %23 = OpConstant %6 20
  2491. %28 = OpConstant %6 1
  2492. %4 = OpFunction %2 None %3
  2493. %5 = OpLabel
  2494. OpReturn
  2495. OpFunctionEnd
  2496. )";
  2497. std::string donor = R"(
  2498. OpCapability Shader
  2499. %1 = OpExtInstImport "GLSL.std.450"
  2500. OpMemoryModel Logical GLSL450
  2501. OpEntryPoint Fragment %4 "main"
  2502. OpExecutionMode %4 OriginUpperLeft
  2503. OpSource ESSL 310
  2504. %2 = OpTypeVoid
  2505. %3 = OpTypeFunction %2
  2506. %6 = OpTypeInt 32 1
  2507. %7 = OpTypeFunction %6
  2508. %10 = OpTypePointer Function %6
  2509. %12 = OpConstant %6 0
  2510. %19 = OpConstant %6 100
  2511. %20 = OpTypeBool
  2512. %23 = OpConstant %6 20
  2513. %28 = OpConstant %6 1
  2514. %4 = OpFunction %2 None %3
  2515. %5 = OpLabel
  2516. %36 = OpFunctionCall %6 %8
  2517. OpReturn
  2518. OpFunctionEnd
  2519. %8 = OpFunction %6 None %7
  2520. %9 = OpLabel
  2521. %11 = OpVariable %10 Function
  2522. OpStore %11 %12
  2523. OpBranch %13
  2524. %13 = OpLabel
  2525. %37 = OpPhi %6 %12 %9 %32 %16
  2526. OpLoopMerge %15 %16 None
  2527. OpBranch %17
  2528. %17 = OpLabel
  2529. %21 = OpSLessThan %20 %37 %19
  2530. OpBranchConditional %21 %14 %15
  2531. %14 = OpLabel
  2532. %24 = OpSGreaterThan %20 %37 %23
  2533. OpSelectionMerge %26 None
  2534. OpBranchConditional %24 %25 %26
  2535. %25 = OpLabel
  2536. %29 = OpIAdd %6 %37 %28
  2537. OpStore %11 %29
  2538. OpBranch %15
  2539. %26 = OpLabel
  2540. OpBranch %16
  2541. %16 = OpLabel
  2542. %32 = OpIAdd %6 %37 %28
  2543. OpStore %11 %32
  2544. OpBranch %13
  2545. %15 = OpLabel
  2546. %38 = OpPhi %6 %37 %17 %29 %25
  2547. OpReturnValue %38
  2548. OpFunctionEnd
  2549. )";
  2550. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2551. const auto consumer = nullptr;
  2552. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2553. spvtools::ValidatorOptions validator_options;
  2554. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2555. kConsoleMessageConsumer));
  2556. TransformationContext transformation_context(
  2557. MakeUnique<FactManager>(context.get()), validator_options);
  2558. // Make a sequence of instruction messages corresponding to function %8 in
  2559. // |donor|.
  2560. std::vector<protobufs::Instruction> instructions =
  2561. GetInstructionsForFunction(env, consumer, donor, 8);
  2562. protobufs::LoopLimiterInfo loop_limiter_info;
  2563. loop_limiter_info.set_loop_header_id(13);
  2564. loop_limiter_info.set_load_id(102);
  2565. loop_limiter_info.set_increment_id(103);
  2566. loop_limiter_info.set_compare_id(104);
  2567. loop_limiter_info.set_logical_op_id(105);
  2568. TransformationAddFunction no_op_phi_data(instructions, 100, 28,
  2569. {loop_limiter_info}, 0, {});
  2570. // The loop limiter info is not good enough; it does not include ids to patch
  2571. // up the OpPhi at the loop merge.
  2572. ASSERT_FALSE(
  2573. no_op_phi_data.IsApplicable(context.get(), transformation_context));
  2574. // Add a phi id for the new edge from the loop back edge block to the loop
  2575. // merge.
  2576. loop_limiter_info.add_phi_id(28);
  2577. TransformationAddFunction with_op_phi_data(instructions, 100, 28,
  2578. {loop_limiter_info}, 0, {});
  2579. ASSERT_TRUE(
  2580. with_op_phi_data.IsApplicable(context.get(), transformation_context));
  2581. ApplyAndCheckFreshIds(with_op_phi_data, context.get(),
  2582. &transformation_context);
  2583. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2584. kConsoleMessageConsumer));
  2585. std::string expected = R"(
  2586. OpCapability Shader
  2587. %1 = OpExtInstImport "GLSL.std.450"
  2588. OpMemoryModel Logical GLSL450
  2589. OpEntryPoint Fragment %4 "main"
  2590. OpExecutionMode %4 OriginUpperLeft
  2591. OpSource ESSL 310
  2592. %2 = OpTypeVoid
  2593. %3 = OpTypeFunction %2
  2594. %6 = OpTypeInt 32 1
  2595. %50 = OpTypeInt 32 0
  2596. %51 = OpConstant %50 0
  2597. %52 = OpConstant %50 1
  2598. %53 = OpTypePointer Function %50
  2599. %7 = OpTypeFunction %6
  2600. %10 = OpTypePointer Function %6
  2601. %12 = OpConstant %6 0
  2602. %19 = OpConstant %6 100
  2603. %20 = OpTypeBool
  2604. %23 = OpConstant %6 20
  2605. %28 = OpConstant %6 1
  2606. %4 = OpFunction %2 None %3
  2607. %5 = OpLabel
  2608. OpReturn
  2609. OpFunctionEnd
  2610. %8 = OpFunction %6 None %7
  2611. %9 = OpLabel
  2612. %100 = OpVariable %53 Function %51
  2613. %11 = OpVariable %10 Function
  2614. OpStore %11 %12
  2615. OpBranch %13
  2616. %13 = OpLabel
  2617. %37 = OpPhi %6 %12 %9 %32 %16
  2618. OpLoopMerge %15 %16 None
  2619. OpBranch %17
  2620. %17 = OpLabel
  2621. %21 = OpSLessThan %20 %37 %19
  2622. OpBranchConditional %21 %14 %15
  2623. %14 = OpLabel
  2624. %24 = OpSGreaterThan %20 %37 %23
  2625. OpSelectionMerge %26 None
  2626. OpBranchConditional %24 %25 %26
  2627. %25 = OpLabel
  2628. %29 = OpIAdd %6 %37 %28
  2629. OpStore %11 %29
  2630. OpBranch %15
  2631. %26 = OpLabel
  2632. OpBranch %16
  2633. %16 = OpLabel
  2634. %32 = OpIAdd %6 %37 %28
  2635. OpStore %11 %32
  2636. %102 = OpLoad %50 %100
  2637. %103 = OpIAdd %50 %102 %52
  2638. OpStore %100 %103
  2639. %104 = OpUGreaterThanEqual %20 %102 %28
  2640. OpBranchConditional %104 %15 %13
  2641. %15 = OpLabel
  2642. %38 = OpPhi %6 %37 %17 %29 %25 %28 %16
  2643. OpReturnValue %38
  2644. OpFunctionEnd
  2645. )";
  2646. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2647. }
  2648. TEST(TransformationAddFunctionTest, LoopLimitersAndOpPhi2) {
  2649. // This captures the scenario where the loop merge block already has an OpPhi
  2650. // with the loop back edge block as a predecessor.
  2651. std::string shader = R"(
  2652. OpCapability Shader
  2653. %1 = OpExtInstImport "GLSL.std.450"
  2654. OpMemoryModel Logical GLSL450
  2655. OpEntryPoint Fragment %4 "main"
  2656. OpExecutionMode %4 OriginUpperLeft
  2657. OpSource ESSL 310
  2658. %2 = OpTypeVoid
  2659. %3 = OpTypeFunction %2
  2660. %6 = OpTypeInt 32 1
  2661. %50 = OpTypeInt 32 0
  2662. %51 = OpConstant %50 0
  2663. %52 = OpConstant %50 1
  2664. %53 = OpTypePointer Function %50
  2665. %7 = OpTypeFunction %6
  2666. %10 = OpTypePointer Function %6
  2667. %12 = OpConstant %6 0
  2668. %19 = OpConstant %6 100
  2669. %20 = OpTypeBool
  2670. %60 = OpConstantTrue %20
  2671. %23 = OpConstant %6 20
  2672. %28 = OpConstant %6 1
  2673. %4 = OpFunction %2 None %3
  2674. %5 = OpLabel
  2675. OpReturn
  2676. OpFunctionEnd
  2677. )";
  2678. std::string donor = R"(
  2679. OpCapability Shader
  2680. %1 = OpExtInstImport "GLSL.std.450"
  2681. OpMemoryModel Logical GLSL450
  2682. OpEntryPoint Fragment %4 "main"
  2683. OpExecutionMode %4 OriginUpperLeft
  2684. OpSource ESSL 310
  2685. %2 = OpTypeVoid
  2686. %3 = OpTypeFunction %2
  2687. %6 = OpTypeInt 32 1
  2688. %50 = OpTypeInt 32 0
  2689. %51 = OpConstant %50 0
  2690. %52 = OpConstant %50 1
  2691. %53 = OpTypePointer Function %50
  2692. %7 = OpTypeFunction %6
  2693. %10 = OpTypePointer Function %6
  2694. %12 = OpConstant %6 0
  2695. %19 = OpConstant %6 100
  2696. %20 = OpTypeBool
  2697. %60 = OpConstantTrue %20
  2698. %23 = OpConstant %6 20
  2699. %28 = OpConstant %6 1
  2700. %4 = OpFunction %2 None %3
  2701. %5 = OpLabel
  2702. OpReturn
  2703. OpFunctionEnd
  2704. %8 = OpFunction %6 None %7
  2705. %9 = OpLabel
  2706. %11 = OpVariable %10 Function
  2707. OpStore %11 %12
  2708. OpBranch %13
  2709. %13 = OpLabel
  2710. %37 = OpPhi %6 %12 %9 %32 %16
  2711. OpLoopMerge %15 %16 None
  2712. OpBranch %17
  2713. %17 = OpLabel
  2714. %21 = OpSLessThan %20 %37 %19
  2715. OpBranchConditional %21 %14 %15
  2716. %14 = OpLabel
  2717. %24 = OpSGreaterThan %20 %37 %23
  2718. OpSelectionMerge %26 None
  2719. OpBranchConditional %24 %25 %26
  2720. %25 = OpLabel
  2721. %29 = OpIAdd %6 %37 %28
  2722. OpStore %11 %29
  2723. OpBranch %15
  2724. %26 = OpLabel
  2725. OpBranch %16
  2726. %16 = OpLabel
  2727. %32 = OpIAdd %6 %37 %28
  2728. OpStore %11 %32
  2729. OpBranchConditional %60 %15 %13
  2730. %15 = OpLabel
  2731. %38 = OpPhi %6 %37 %17 %29 %25 %23 %16
  2732. OpReturnValue %38
  2733. OpFunctionEnd
  2734. )";
  2735. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2736. const auto consumer = nullptr;
  2737. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2738. spvtools::ValidatorOptions validator_options;
  2739. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2740. kConsoleMessageConsumer));
  2741. TransformationContext transformation_context(
  2742. MakeUnique<FactManager>(context.get()), validator_options);
  2743. // Make a sequence of instruction messages corresponding to function %8 in
  2744. // |donor|.
  2745. std::vector<protobufs::Instruction> instructions =
  2746. GetInstructionsForFunction(env, consumer, donor, 8);
  2747. protobufs::LoopLimiterInfo loop_limiter_info;
  2748. loop_limiter_info.set_loop_header_id(13);
  2749. loop_limiter_info.set_load_id(102);
  2750. loop_limiter_info.set_increment_id(103);
  2751. loop_limiter_info.set_compare_id(104);
  2752. loop_limiter_info.set_logical_op_id(105);
  2753. TransformationAddFunction transformation(instructions, 100, 28,
  2754. {loop_limiter_info}, 0, {});
  2755. ASSERT_TRUE(
  2756. transformation.IsApplicable(context.get(), transformation_context));
  2757. ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
  2758. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2759. kConsoleMessageConsumer));
  2760. std::string expected = R"(
  2761. OpCapability Shader
  2762. %1 = OpExtInstImport "GLSL.std.450"
  2763. OpMemoryModel Logical GLSL450
  2764. OpEntryPoint Fragment %4 "main"
  2765. OpExecutionMode %4 OriginUpperLeft
  2766. OpSource ESSL 310
  2767. %2 = OpTypeVoid
  2768. %3 = OpTypeFunction %2
  2769. %6 = OpTypeInt 32 1
  2770. %50 = OpTypeInt 32 0
  2771. %51 = OpConstant %50 0
  2772. %52 = OpConstant %50 1
  2773. %53 = OpTypePointer Function %50
  2774. %7 = OpTypeFunction %6
  2775. %10 = OpTypePointer Function %6
  2776. %12 = OpConstant %6 0
  2777. %19 = OpConstant %6 100
  2778. %20 = OpTypeBool
  2779. %60 = OpConstantTrue %20
  2780. %23 = OpConstant %6 20
  2781. %28 = OpConstant %6 1
  2782. %4 = OpFunction %2 None %3
  2783. %5 = OpLabel
  2784. OpReturn
  2785. OpFunctionEnd
  2786. %8 = OpFunction %6 None %7
  2787. %9 = OpLabel
  2788. %100 = OpVariable %53 Function %51
  2789. %11 = OpVariable %10 Function
  2790. OpStore %11 %12
  2791. OpBranch %13
  2792. %13 = OpLabel
  2793. %37 = OpPhi %6 %12 %9 %32 %16
  2794. OpLoopMerge %15 %16 None
  2795. OpBranch %17
  2796. %17 = OpLabel
  2797. %21 = OpSLessThan %20 %37 %19
  2798. OpBranchConditional %21 %14 %15
  2799. %14 = OpLabel
  2800. %24 = OpSGreaterThan %20 %37 %23
  2801. OpSelectionMerge %26 None
  2802. OpBranchConditional %24 %25 %26
  2803. %25 = OpLabel
  2804. %29 = OpIAdd %6 %37 %28
  2805. OpStore %11 %29
  2806. OpBranch %15
  2807. %26 = OpLabel
  2808. OpBranch %16
  2809. %16 = OpLabel
  2810. %32 = OpIAdd %6 %37 %28
  2811. OpStore %11 %32
  2812. %102 = OpLoad %50 %100
  2813. %103 = OpIAdd %50 %102 %52
  2814. OpStore %100 %103
  2815. %104 = OpUGreaterThanEqual %20 %102 %28
  2816. %105 = OpLogicalOr %20 %60 %104
  2817. OpBranchConditional %105 %15 %13
  2818. %15 = OpLabel
  2819. %38 = OpPhi %6 %37 %17 %29 %25 %23 %16
  2820. OpReturnValue %38
  2821. OpFunctionEnd
  2822. )";
  2823. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2824. }
  2825. TEST(TransformationAddFunctionTest, StaticallyOutOfBoundsArrayAccess) {
  2826. std::string shader = R"(
  2827. OpCapability Shader
  2828. %1 = OpExtInstImport "GLSL.std.450"
  2829. OpMemoryModel Logical GLSL450
  2830. OpEntryPoint Fragment %4 "main"
  2831. OpExecutionMode %4 OriginUpperLeft
  2832. OpSource ESSL 310
  2833. %2 = OpTypeVoid
  2834. %3 = OpTypeFunction %2
  2835. %8 = OpTypeInt 32 1
  2836. %9 = OpTypeInt 32 0
  2837. %10 = OpConstant %9 3
  2838. %11 = OpTypeArray %8 %10
  2839. %12 = OpTypePointer Private %11
  2840. %13 = OpVariable %12 Private
  2841. %14 = OpConstant %8 3
  2842. %20 = OpConstant %8 2
  2843. %15 = OpConstant %8 1
  2844. %21 = OpTypeBool
  2845. %16 = OpTypePointer Private %8
  2846. %4 = OpFunction %2 None %3
  2847. %5 = OpLabel
  2848. OpReturn
  2849. OpFunctionEnd
  2850. )";
  2851. std::string donor = R"(
  2852. OpCapability Shader
  2853. %1 = OpExtInstImport "GLSL.std.450"
  2854. OpMemoryModel Logical GLSL450
  2855. OpEntryPoint Fragment %4 "main"
  2856. OpExecutionMode %4 OriginUpperLeft
  2857. OpSource ESSL 310
  2858. %2 = OpTypeVoid
  2859. %3 = OpTypeFunction %2
  2860. %8 = OpTypeInt 32 1
  2861. %9 = OpTypeInt 32 0
  2862. %10 = OpConstant %9 3
  2863. %11 = OpTypeArray %8 %10
  2864. %12 = OpTypePointer Private %11
  2865. %13 = OpVariable %12 Private
  2866. %14 = OpConstant %8 3
  2867. %15 = OpConstant %8 1
  2868. %16 = OpTypePointer Private %8
  2869. %4 = OpFunction %2 None %3
  2870. %5 = OpLabel
  2871. OpReturn
  2872. OpFunctionEnd
  2873. %6 = OpFunction %2 None %3
  2874. %7 = OpLabel
  2875. %17 = OpAccessChain %16 %13 %14
  2876. OpStore %17 %15
  2877. OpReturn
  2878. OpFunctionEnd
  2879. )";
  2880. const auto env = SPV_ENV_UNIVERSAL_1_4;
  2881. const auto consumer = nullptr;
  2882. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  2883. spvtools::ValidatorOptions validator_options;
  2884. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2885. kConsoleMessageConsumer));
  2886. TransformationContext transformation_context(
  2887. MakeUnique<FactManager>(context.get()), validator_options);
  2888. // Make a sequence of instruction messages corresponding to function %6 in
  2889. // |donor|.
  2890. std::vector<protobufs::Instruction> instructions =
  2891. GetInstructionsForFunction(env, consumer, donor, 6);
  2892. TransformationAddFunction add_livesafe_function(
  2893. instructions, 0, 0, {}, 0, {MakeAccessClampingInfo(17, {{100, 101}})});
  2894. ASSERT_TRUE(add_livesafe_function.IsApplicable(context.get(),
  2895. transformation_context));
  2896. ApplyAndCheckFreshIds(add_livesafe_function, context.get(),
  2897. &transformation_context);
  2898. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  2899. kConsoleMessageConsumer));
  2900. std::string expected = R"(
  2901. OpCapability Shader
  2902. %1 = OpExtInstImport "GLSL.std.450"
  2903. OpMemoryModel Logical GLSL450
  2904. OpEntryPoint Fragment %4 "main"
  2905. OpExecutionMode %4 OriginUpperLeft
  2906. OpSource ESSL 310
  2907. %2 = OpTypeVoid
  2908. %3 = OpTypeFunction %2
  2909. %8 = OpTypeInt 32 1
  2910. %9 = OpTypeInt 32 0
  2911. %10 = OpConstant %9 3
  2912. %11 = OpTypeArray %8 %10
  2913. %12 = OpTypePointer Private %11
  2914. %13 = OpVariable %12 Private
  2915. %14 = OpConstant %8 3
  2916. %20 = OpConstant %8 2
  2917. %15 = OpConstant %8 1
  2918. %21 = OpTypeBool
  2919. %16 = OpTypePointer Private %8
  2920. %4 = OpFunction %2 None %3
  2921. %5 = OpLabel
  2922. OpReturn
  2923. OpFunctionEnd
  2924. %6 = OpFunction %2 None %3
  2925. %7 = OpLabel
  2926. %100 = OpULessThanEqual %21 %14 %20
  2927. %101 = OpSelect %8 %100 %14 %20
  2928. %17 = OpAccessChain %16 %13 %101
  2929. OpStore %17 %15
  2930. OpReturn
  2931. OpFunctionEnd
  2932. )";
  2933. ASSERT_TRUE(IsEqual(env, expected, context.get()));
  2934. }
  2935. } // namespace
  2936. } // namespace fuzz
  2937. } // namespace spvtools