fuzzer_pass_donate_modules_test.cpp 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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/fuzzer_pass_donate_modules.h"
  15. #include <algorithm>
  16. #include "gtest/gtest.h"
  17. #include "source/fuzz/pseudo_random_generator.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. TEST(FuzzerPassDonateModulesTest, BasicDonation) {
  23. std::string recipient_shader = R"(
  24. OpCapability Shader
  25. %1 = OpExtInstImport "GLSL.std.450"
  26. OpMemoryModel Logical GLSL450
  27. OpEntryPoint Fragment %4 "main"
  28. OpExecutionMode %4 OriginUpperLeft
  29. OpSource ESSL 310
  30. OpName %4 "main"
  31. OpName %10 "m"
  32. OpName %16 "v"
  33. OpDecorate %16 RelaxedPrecision
  34. OpDecorate %20 RelaxedPrecision
  35. %2 = OpTypeVoid
  36. %3 = OpTypeFunction %2
  37. %6 = OpTypeFloat 32
  38. %7 = OpTypeVector %6 3
  39. %8 = OpTypeMatrix %7 2
  40. %9 = OpTypePointer Private %8
  41. %10 = OpVariable %9 Private
  42. %11 = OpTypeInt 32 1
  43. %12 = OpConstant %11 0
  44. %13 = OpTypeInt 32 0
  45. %14 = OpTypeVector %13 4
  46. %15 = OpTypePointer Private %14
  47. %16 = OpVariable %15 Private
  48. %17 = OpConstant %13 2
  49. %18 = OpTypePointer Private %13
  50. %22 = OpConstant %13 0
  51. %23 = OpTypePointer Private %6
  52. %4 = OpFunction %2 None %3
  53. %5 = OpLabel
  54. %19 = OpAccessChain %18 %16 %17
  55. %20 = OpLoad %13 %19
  56. %21 = OpConvertUToF %6 %20
  57. %24 = OpAccessChain %23 %10 %12 %22
  58. OpStore %24 %21
  59. OpReturn
  60. OpFunctionEnd
  61. )";
  62. std::string donor_shader = R"(
  63. OpCapability Shader
  64. %1 = OpExtInstImport "GLSL.std.450"
  65. OpMemoryModel Logical GLSL450
  66. OpEntryPoint Fragment %4 "main"
  67. OpExecutionMode %4 OriginUpperLeft
  68. OpSource ESSL 310
  69. OpName %4 "main"
  70. OpName %12 "bar(mf24;"
  71. OpName %11 "m"
  72. OpName %20 "foo(vu4;"
  73. OpName %19 "v"
  74. OpName %23 "x"
  75. OpName %26 "param"
  76. OpName %29 "result"
  77. OpName %31 "i"
  78. OpName %81 "param"
  79. %2 = OpTypeVoid
  80. %3 = OpTypeFunction %2
  81. %6 = OpTypeFloat 32
  82. %7 = OpTypeVector %6 4
  83. %8 = OpTypeMatrix %7 2
  84. %9 = OpTypePointer Function %8
  85. %10 = OpTypeFunction %6 %9
  86. %14 = OpTypeInt 32 0
  87. %15 = OpTypeVector %14 4
  88. %16 = OpTypePointer Function %15
  89. %17 = OpTypeInt 32 1
  90. %18 = OpTypeFunction %17 %16
  91. %22 = OpTypePointer Function %17
  92. %24 = OpConstant %14 2
  93. %25 = OpConstantComposite %15 %24 %24 %24 %24
  94. %28 = OpTypePointer Function %6
  95. %30 = OpConstant %6 0
  96. %32 = OpConstant %17 0
  97. %39 = OpConstant %17 10
  98. %40 = OpTypeBool
  99. %43 = OpConstant %17 3
  100. %50 = OpConstant %17 1
  101. %55 = OpConstant %14 0
  102. %56 = OpTypePointer Function %14
  103. %59 = OpConstant %14 1
  104. %65 = OpConstant %17 2
  105. %68 = OpConstant %6 1
  106. %69 = OpConstant %6 2
  107. %70 = OpConstant %6 3
  108. %71 = OpConstant %6 4
  109. %72 = OpConstant %14 3
  110. %76 = OpConstant %6 6
  111. %77 = OpConstant %6 7
  112. %4 = OpFunction %2 None %3
  113. %5 = OpLabel
  114. %23 = OpVariable %22 Function
  115. %26 = OpVariable %16 Function
  116. OpStore %26 %25
  117. %27 = OpFunctionCall %17 %20 %26
  118. OpStore %23 %27
  119. OpReturn
  120. OpFunctionEnd
  121. %12 = OpFunction %6 None %10
  122. %11 = OpFunctionParameter %9
  123. %13 = OpLabel
  124. %29 = OpVariable %28 Function
  125. %31 = OpVariable %22 Function
  126. OpStore %29 %30
  127. OpStore %31 %32
  128. OpBranch %33
  129. %33 = OpLabel
  130. OpLoopMerge %35 %36 None
  131. OpBranch %37
  132. %37 = OpLabel
  133. %38 = OpLoad %17 %31
  134. %41 = OpSLessThan %40 %38 %39
  135. OpBranchConditional %41 %34 %35
  136. %34 = OpLabel
  137. %42 = OpLoad %17 %31
  138. %44 = OpExtInst %17 %1 SClamp %42 %32 %43
  139. %45 = OpAccessChain %28 %11 %32 %44
  140. %46 = OpLoad %6 %45
  141. %47 = OpLoad %6 %29
  142. %48 = OpFAdd %6 %47 %46
  143. OpStore %29 %48
  144. OpBranch %36
  145. %36 = OpLabel
  146. %49 = OpLoad %17 %31
  147. %51 = OpIAdd %17 %49 %50
  148. OpStore %31 %51
  149. OpBranch %33
  150. %35 = OpLabel
  151. %52 = OpLoad %6 %29
  152. OpReturnValue %52
  153. OpFunctionEnd
  154. %20 = OpFunction %17 None %18
  155. %19 = OpFunctionParameter %16
  156. %21 = OpLabel
  157. %81 = OpVariable %9 Function
  158. %57 = OpAccessChain %56 %19 %55
  159. %58 = OpLoad %14 %57
  160. %60 = OpAccessChain %56 %19 %59
  161. %61 = OpLoad %14 %60
  162. %62 = OpUGreaterThan %40 %58 %61
  163. OpSelectionMerge %64 None
  164. OpBranchConditional %62 %63 %67
  165. %63 = OpLabel
  166. OpReturnValue %65
  167. %67 = OpLabel
  168. %73 = OpAccessChain %56 %19 %72
  169. %74 = OpLoad %14 %73
  170. %75 = OpConvertUToF %6 %74
  171. %78 = OpCompositeConstruct %7 %30 %68 %69 %70
  172. %79 = OpCompositeConstruct %7 %71 %75 %76 %77
  173. %80 = OpCompositeConstruct %8 %78 %79
  174. OpStore %81 %80
  175. %82 = OpFunctionCall %6 %12 %81
  176. %83 = OpConvertFToS %17 %82
  177. OpReturnValue %83
  178. %64 = OpLabel
  179. %85 = OpUndef %17
  180. OpReturnValue %85
  181. OpFunctionEnd
  182. )";
  183. const auto env = SPV_ENV_UNIVERSAL_1_3;
  184. const auto consumer = nullptr;
  185. spvtools::ValidatorOptions validator_options;
  186. const auto recipient_context =
  187. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  188. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  189. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  190. const auto donor_context =
  191. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  192. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  193. donor_context.get(), validator_options, kConsoleMessageConsumer));
  194. TransformationContext transformation_context(
  195. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  196. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  197. false);
  198. protobufs::TransformationSequence transformation_sequence;
  199. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  200. &transformation_context, &fuzzer_context,
  201. &transformation_sequence, false, {});
  202. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  203. // We just check that the result is valid. Checking to what it should be
  204. // exactly equal to would be very fragile.
  205. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  206. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  207. }
  208. TEST(FuzzerPassDonateModulesTest, DonationWithUniforms) {
  209. // This test checks that when donating a shader that contains uniforms,
  210. // uniform variables and associated pointer types are demoted from having
  211. // Uniform storage class to Private storage class.
  212. std::string recipient_and_donor_shader = R"(
  213. OpCapability Shader
  214. %1 = OpExtInstImport "GLSL.std.450"
  215. OpMemoryModel Logical GLSL450
  216. OpEntryPoint Fragment %4 "main"
  217. OpExecutionMode %4 OriginUpperLeft
  218. OpSource ESSL 310
  219. OpMemberDecorate %9 0 Offset 0
  220. OpDecorate %9 Block
  221. OpDecorate %11 DescriptorSet 0
  222. OpDecorate %11 Binding 0
  223. OpMemberDecorate %19 0 Offset 0
  224. OpDecorate %19 Block
  225. OpDecorate %21 DescriptorSet 0
  226. OpDecorate %21 Binding 1
  227. %2 = OpTypeVoid
  228. %3 = OpTypeFunction %2
  229. %6 = OpTypeFloat 32
  230. %7 = OpTypePointer Function %6
  231. %9 = OpTypeStruct %6
  232. %10 = OpTypePointer Uniform %9
  233. %11 = OpVariable %10 Uniform
  234. %12 = OpTypeInt 32 1
  235. %13 = OpConstant %12 0
  236. %14 = OpTypePointer Uniform %6
  237. %17 = OpTypePointer Function %12
  238. %19 = OpTypeStruct %12
  239. %20 = OpTypePointer Uniform %19
  240. %21 = OpVariable %20 Uniform
  241. %22 = OpTypePointer Uniform %12
  242. %4 = OpFunction %2 None %3
  243. %5 = OpLabel
  244. %8 = OpVariable %7 Function
  245. %18 = OpVariable %17 Function
  246. %15 = OpAccessChain %14 %11 %13
  247. %16 = OpLoad %6 %15
  248. OpStore %8 %16
  249. %23 = OpAccessChain %22 %21 %13
  250. %24 = OpLoad %12 %23
  251. OpStore %18 %24
  252. OpReturn
  253. OpFunctionEnd
  254. )";
  255. const auto env = SPV_ENV_UNIVERSAL_1_3;
  256. const auto consumer = nullptr;
  257. spvtools::ValidatorOptions validator_options;
  258. const auto recipient_context = BuildModule(
  259. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  260. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  261. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  262. const auto donor_context = BuildModule(
  263. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  264. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  265. donor_context.get(), validator_options, kConsoleMessageConsumer));
  266. TransformationContext transformation_context(
  267. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  268. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  269. false);
  270. protobufs::TransformationSequence transformation_sequence;
  271. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  272. &transformation_context, &fuzzer_context,
  273. &transformation_sequence, false, {});
  274. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  275. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  276. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  277. std::string after_transformation = R"(
  278. OpCapability Shader
  279. %1 = OpExtInstImport "GLSL.std.450"
  280. OpMemoryModel Logical GLSL450
  281. OpEntryPoint Fragment %4 "main"
  282. OpExecutionMode %4 OriginUpperLeft
  283. OpSource ESSL 310
  284. OpMemberDecorate %9 0 Offset 0
  285. OpDecorate %9 Block
  286. OpDecorate %11 DescriptorSet 0
  287. OpDecorate %11 Binding 0
  288. OpMemberDecorate %19 0 Offset 0
  289. OpDecorate %19 Block
  290. OpDecorate %21 DescriptorSet 0
  291. OpDecorate %21 Binding 1
  292. %2 = OpTypeVoid
  293. %3 = OpTypeFunction %2
  294. %6 = OpTypeFloat 32
  295. %7 = OpTypePointer Function %6
  296. %9 = OpTypeStruct %6
  297. %10 = OpTypePointer Uniform %9
  298. %11 = OpVariable %10 Uniform
  299. %12 = OpTypeInt 32 1
  300. %13 = OpConstant %12 0
  301. %14 = OpTypePointer Uniform %6
  302. %17 = OpTypePointer Function %12
  303. %19 = OpTypeStruct %12
  304. %20 = OpTypePointer Uniform %19
  305. %21 = OpVariable %20 Uniform
  306. %22 = OpTypePointer Uniform %12
  307. %100 = OpTypePointer Function %6
  308. %101 = OpTypeStruct %6
  309. %102 = OpTypePointer Private %101
  310. %104 = OpConstant %6 0
  311. %105 = OpConstantComposite %101 %104
  312. %103 = OpVariable %102 Private %105
  313. %106 = OpConstant %12 0
  314. %107 = OpTypePointer Private %6
  315. %108 = OpTypePointer Function %12
  316. %109 = OpTypeStruct %12
  317. %110 = OpTypePointer Private %109
  318. %112 = OpConstantComposite %109 %13
  319. %111 = OpVariable %110 Private %112
  320. %113 = OpTypePointer Private %12
  321. %4 = OpFunction %2 None %3
  322. %5 = OpLabel
  323. %8 = OpVariable %7 Function
  324. %18 = OpVariable %17 Function
  325. %15 = OpAccessChain %14 %11 %13
  326. %16 = OpLoad %6 %15
  327. OpStore %8 %16
  328. %23 = OpAccessChain %22 %21 %13
  329. %24 = OpLoad %12 %23
  330. OpStore %18 %24
  331. OpReturn
  332. OpFunctionEnd
  333. %114 = OpFunction %2 None %3
  334. %115 = OpLabel
  335. %116 = OpVariable %100 Function %104
  336. %117 = OpVariable %108 Function %13
  337. %118 = OpAccessChain %107 %103 %106
  338. %119 = OpLoad %6 %118
  339. OpStore %116 %119
  340. %120 = OpAccessChain %113 %111 %106
  341. %121 = OpLoad %12 %120
  342. OpStore %117 %121
  343. OpReturn
  344. OpFunctionEnd
  345. )";
  346. ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
  347. }
  348. TEST(FuzzerPassDonateModulesTest, DonationWithInputAndOutputVariables) {
  349. // This test checks that when donating a shader that contains input and output
  350. // variables, such variables and associated pointer types are demoted to have
  351. // the Private storage class.
  352. std::string recipient_and_donor_shader = R"(
  353. OpCapability Shader
  354. %1 = OpExtInstImport "GLSL.std.450"
  355. OpMemoryModel Logical GLSL450
  356. OpEntryPoint Fragment %4 "main" %9 %11
  357. OpExecutionMode %4 OriginUpperLeft
  358. OpSource ESSL 310
  359. OpDecorate %9 Location 0
  360. OpDecorate %11 Location 1
  361. %2 = OpTypeVoid
  362. %3 = OpTypeFunction %2
  363. %6 = OpTypeFloat 32
  364. %7 = OpTypeVector %6 4
  365. %8 = OpTypePointer Output %7
  366. %9 = OpVariable %8 Output
  367. %10 = OpTypePointer Input %7
  368. %11 = OpVariable %10 Input
  369. %4 = OpFunction %2 None %3
  370. %5 = OpLabel
  371. %12 = OpLoad %7 %11
  372. OpStore %9 %12
  373. OpReturn
  374. OpFunctionEnd
  375. )";
  376. const auto env = SPV_ENV_UNIVERSAL_1_3;
  377. const auto consumer = nullptr;
  378. spvtools::ValidatorOptions validator_options;
  379. const auto recipient_context = BuildModule(
  380. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  381. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  382. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  383. const auto donor_context = BuildModule(
  384. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  385. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  386. donor_context.get(), validator_options, kConsoleMessageConsumer));
  387. TransformationContext transformation_context(
  388. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  389. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  390. false);
  391. protobufs::TransformationSequence transformation_sequence;
  392. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  393. &transformation_context, &fuzzer_context,
  394. &transformation_sequence, false, {});
  395. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  396. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  397. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  398. std::string after_transformation = R"(
  399. OpCapability Shader
  400. %1 = OpExtInstImport "GLSL.std.450"
  401. OpMemoryModel Logical GLSL450
  402. OpEntryPoint Fragment %4 "main" %9 %11
  403. OpExecutionMode %4 OriginUpperLeft
  404. OpSource ESSL 310
  405. OpDecorate %9 Location 0
  406. OpDecorate %11 Location 1
  407. %2 = OpTypeVoid
  408. %3 = OpTypeFunction %2
  409. %6 = OpTypeFloat 32
  410. %7 = OpTypeVector %6 4
  411. %8 = OpTypePointer Output %7
  412. %9 = OpVariable %8 Output
  413. %10 = OpTypePointer Input %7
  414. %11 = OpVariable %10 Input
  415. %100 = OpTypePointer Private %7
  416. %102 = OpConstant %6 0
  417. %103 = OpConstantComposite %7 %102 %102 %102 %102
  418. %101 = OpVariable %100 Private %103
  419. %104 = OpTypePointer Private %7
  420. %105 = OpVariable %104 Private %103
  421. %4 = OpFunction %2 None %3
  422. %5 = OpLabel
  423. %12 = OpLoad %7 %11
  424. OpStore %9 %12
  425. OpReturn
  426. OpFunctionEnd
  427. %106 = OpFunction %2 None %3
  428. %107 = OpLabel
  429. %108 = OpLoad %7 %105
  430. OpStore %101 %108
  431. OpReturn
  432. OpFunctionEnd
  433. )";
  434. ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
  435. }
  436. TEST(FuzzerPassDonateModulesTest, DonateFunctionTypeWithDifferentPointers) {
  437. std::string recipient_and_donor_shader = R"(
  438. OpCapability Shader
  439. %1 = OpExtInstImport "GLSL.std.450"
  440. OpMemoryModel Logical GLSL450
  441. OpEntryPoint Fragment %4 "main"
  442. OpExecutionMode %4 OriginUpperLeft
  443. OpSource ESSL 310
  444. %2 = OpTypeVoid
  445. %3 = OpTypeFunction %2
  446. %6 = OpTypeInt 32 0
  447. %7 = OpTypePointer Function %6
  448. %8 = OpTypeFunction %2 %7
  449. %4 = OpFunction %2 None %3
  450. %5 = OpLabel
  451. %9 = OpVariable %7 Function
  452. %10 = OpFunctionCall %2 %11 %9
  453. OpReturn
  454. OpFunctionEnd
  455. %11 = OpFunction %2 None %8
  456. %12 = OpFunctionParameter %7
  457. %13 = OpLabel
  458. OpReturn
  459. OpFunctionEnd
  460. )";
  461. const auto env = SPV_ENV_UNIVERSAL_1_5;
  462. const auto consumer = nullptr;
  463. spvtools::ValidatorOptions validator_options;
  464. const auto recipient_context = BuildModule(
  465. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  466. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  467. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  468. const auto donor_context = BuildModule(
  469. env, consumer, recipient_and_donor_shader, kFuzzAssembleOption);
  470. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  471. donor_context.get(), validator_options, kConsoleMessageConsumer));
  472. TransformationContext transformation_context(
  473. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  474. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  475. false);
  476. protobufs::TransformationSequence transformation_sequence;
  477. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  478. &transformation_context, &fuzzer_context,
  479. &transformation_sequence, false, {});
  480. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  481. // We just check that the result is valid. Checking to what it should be
  482. // exactly equal to would be very fragile.
  483. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  484. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  485. }
  486. TEST(FuzzerPassDonateModulesTest, DonateOpConstantNull) {
  487. std::string recipient_shader = R"(
  488. OpCapability Shader
  489. OpCapability ImageQuery
  490. OpCapability VariablePointers
  491. %1 = OpExtInstImport "GLSL.std.450"
  492. OpMemoryModel Logical GLSL450
  493. OpEntryPoint Fragment %4 "main"
  494. OpExecutionMode %4 OriginUpperLeft
  495. OpSource ESSL 320
  496. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  497. %2 = OpTypeVoid
  498. %3 = OpTypeFunction %2
  499. %4 = OpFunction %2 None %3
  500. %5 = OpLabel
  501. OpReturn
  502. OpFunctionEnd
  503. )";
  504. std::string donor_shader = R"(
  505. OpCapability Shader
  506. OpCapability ImageQuery
  507. OpCapability VariablePointers
  508. %1 = OpExtInstImport "GLSL.std.450"
  509. OpMemoryModel Logical GLSL450
  510. OpEntryPoint Fragment %4 "main"
  511. OpExecutionMode %4 OriginUpperLeft
  512. OpSource ESSL 320
  513. %2 = OpTypeVoid
  514. %3 = OpTypeFunction %2
  515. %6 = OpTypeFloat 32
  516. %7 = OpTypePointer Private %6
  517. %8 = OpConstantNull %7
  518. %4 = OpFunction %2 None %3
  519. %5 = OpLabel
  520. OpReturn
  521. OpFunctionEnd
  522. )";
  523. const auto env = SPV_ENV_UNIVERSAL_1_3;
  524. const auto consumer = nullptr;
  525. spvtools::ValidatorOptions validator_options;
  526. const auto recipient_context =
  527. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  528. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  529. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  530. const auto donor_context =
  531. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  532. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  533. donor_context.get(), validator_options, kConsoleMessageConsumer));
  534. TransformationContext transformation_context(
  535. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  536. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  537. false);
  538. protobufs::TransformationSequence transformation_sequence;
  539. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  540. &transformation_context, &fuzzer_context,
  541. &transformation_sequence, false, {});
  542. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  543. // We just check that the result is valid. Checking to what it should be
  544. // exactly equal to would be very fragile.
  545. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  546. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  547. }
  548. TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImages) {
  549. std::string recipient_shader = R"(
  550. OpCapability Shader
  551. OpCapability ImageQuery
  552. %1 = OpExtInstImport "GLSL.std.450"
  553. OpMemoryModel Logical GLSL450
  554. OpEntryPoint Fragment %4 "main"
  555. OpExecutionMode %4 OriginUpperLeft
  556. OpSource ESSL 320
  557. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  558. %2 = OpTypeVoid
  559. %3 = OpTypeFunction %2
  560. %4 = OpFunction %2 None %3
  561. %5 = OpLabel
  562. OpReturn
  563. OpFunctionEnd
  564. )";
  565. std::string donor_shader = R"(
  566. OpCapability Shader
  567. OpCapability ImageQuery
  568. %1 = OpExtInstImport "GLSL.std.450"
  569. OpMemoryModel Logical GLSL450
  570. OpEntryPoint Fragment %4 "main"
  571. OpExecutionMode %4 OriginUpperLeft
  572. OpSource ESSL 320
  573. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  574. OpName %4 "main"
  575. OpName %10 "mySampler"
  576. OpName %21 "myTexture"
  577. OpName %33 "v"
  578. OpDecorate %10 RelaxedPrecision
  579. OpDecorate %10 DescriptorSet 0
  580. OpDecorate %10 Binding 0
  581. OpDecorate %11 RelaxedPrecision
  582. OpDecorate %21 RelaxedPrecision
  583. OpDecorate %21 DescriptorSet 0
  584. OpDecorate %21 Binding 1
  585. OpDecorate %22 RelaxedPrecision
  586. OpDecorate %34 RelaxedPrecision
  587. OpDecorate %40 RelaxedPrecision
  588. OpDecorate %42 RelaxedPrecision
  589. OpDecorate %43 RelaxedPrecision
  590. %2 = OpTypeVoid
  591. %3 = OpTypeFunction %2
  592. %6 = OpTypeFloat 32
  593. %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
  594. %8 = OpTypeSampledImage %7
  595. %9 = OpTypePointer UniformConstant %8
  596. %10 = OpVariable %9 UniformConstant
  597. %12 = OpTypeInt 32 1
  598. %13 = OpConstant %12 2
  599. %15 = OpTypeVector %12 2
  600. %17 = OpTypeInt 32 0
  601. %18 = OpConstant %17 0
  602. %20 = OpTypePointer UniformConstant %7
  603. %21 = OpVariable %20 UniformConstant
  604. %23 = OpConstant %12 1
  605. %25 = OpConstant %17 1
  606. %27 = OpTypeBool
  607. %31 = OpTypeVector %6 4
  608. %32 = OpTypePointer Function %31
  609. %35 = OpConstantComposite %15 %23 %23
  610. %36 = OpConstant %12 3
  611. %37 = OpConstant %12 4
  612. %38 = OpConstantComposite %15 %36 %37
  613. %4 = OpFunction %2 None %3
  614. %5 = OpLabel
  615. %33 = OpVariable %32 Function
  616. %11 = OpLoad %8 %10
  617. %14 = OpImage %7 %11
  618. %16 = OpImageQuerySizeLod %15 %14 %13
  619. %19 = OpCompositeExtract %12 %16 0
  620. %22 = OpLoad %7 %21
  621. %24 = OpImageQuerySizeLod %15 %22 %23
  622. %26 = OpCompositeExtract %12 %24 1
  623. %28 = OpSGreaterThan %27 %19 %26
  624. OpSelectionMerge %30 None
  625. OpBranchConditional %28 %29 %41
  626. %29 = OpLabel
  627. %34 = OpLoad %8 %10
  628. %39 = OpImage %7 %34
  629. %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
  630. OpStore %33 %40
  631. OpBranch %30
  632. %41 = OpLabel
  633. %42 = OpLoad %7 %21
  634. %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
  635. OpStore %33 %43
  636. OpBranch %30
  637. %30 = OpLabel
  638. OpReturn
  639. OpFunctionEnd
  640. )";
  641. const auto env = SPV_ENV_UNIVERSAL_1_3;
  642. const auto consumer = nullptr;
  643. spvtools::ValidatorOptions validator_options;
  644. const auto recipient_context =
  645. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  646. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  647. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  648. const auto donor_context =
  649. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  650. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  651. donor_context.get(), validator_options, kConsoleMessageConsumer));
  652. TransformationContext transformation_context(
  653. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  654. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  655. false);
  656. protobufs::TransformationSequence transformation_sequence;
  657. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  658. &transformation_context, &fuzzer_context,
  659. &transformation_sequence, false, {});
  660. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  661. // We just check that the result is valid. Checking to what it should be
  662. // exactly equal to would be very fragile.
  663. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  664. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  665. }
  666. TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesSampler) {
  667. std::string recipient_shader = R"(
  668. OpCapability Shader
  669. OpCapability ImageQuery
  670. %1 = OpExtInstImport "GLSL.std.450"
  671. OpMemoryModel Logical GLSL450
  672. OpEntryPoint Fragment %4 "main"
  673. OpExecutionMode %4 OriginUpperLeft
  674. OpSource ESSL 320
  675. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  676. %2 = OpTypeVoid
  677. %3 = OpTypeFunction %2
  678. %4 = OpFunction %2 None %3
  679. %5 = OpLabel
  680. OpReturn
  681. OpFunctionEnd
  682. )";
  683. std::string donor_shader = R"(
  684. OpCapability Shader
  685. OpCapability ImageQuery
  686. %1 = OpExtInstImport "GLSL.std.450"
  687. OpMemoryModel Logical GLSL450
  688. OpEntryPoint Fragment %4 "main"
  689. OpExecutionMode %4 OriginUpperLeft
  690. OpSource ESSL 320
  691. OpDecorate %16 DescriptorSet 0
  692. OpDecorate %16 Binding 0
  693. OpDecorate %12 DescriptorSet 0
  694. OpDecorate %12 Binding 64
  695. %2 = OpTypeVoid
  696. %3 = OpTypeFunction %2
  697. %23 = OpTypeFloat 32
  698. %6 = OpTypeImage %23 2D 2 0 0 1 Unknown
  699. %47 = OpTypePointer UniformConstant %6
  700. %12 = OpVariable %47 UniformConstant
  701. %15 = OpTypeSampler
  702. %55 = OpTypePointer UniformConstant %15
  703. %17 = OpTypeSampledImage %6
  704. %16 = OpVariable %55 UniformConstant
  705. %37 = OpTypeVector %23 4
  706. %109 = OpConstant %23 0
  707. %66 = OpConstantComposite %37 %109 %109 %109 %109
  708. %56 = OpTypeBool
  709. %54 = OpConstantTrue %56
  710. %4 = OpFunction %2 None %3
  711. %5 = OpLabel
  712. OpBranch %50
  713. %50 = OpLabel
  714. %51 = OpPhi %37 %66 %5 %111 %53
  715. OpLoopMerge %52 %53 None
  716. OpBranchConditional %54 %53 %52
  717. %53 = OpLabel
  718. %106 = OpLoad %6 %12
  719. %107 = OpLoad %15 %16
  720. %110 = OpSampledImage %17 %106 %107
  721. %111 = OpImageSampleImplicitLod %37 %110 %66 Bias %109
  722. OpBranch %50
  723. %52 = OpLabel
  724. OpReturn
  725. OpFunctionEnd
  726. )";
  727. const auto env = SPV_ENV_UNIVERSAL_1_3;
  728. const auto consumer = nullptr;
  729. spvtools::ValidatorOptions validator_options;
  730. const auto recipient_context =
  731. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  732. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  733. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  734. const auto donor_context =
  735. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  736. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  737. donor_context.get(), validator_options, kConsoleMessageConsumer));
  738. TransformationContext transformation_context(
  739. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  740. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  741. false);
  742. protobufs::TransformationSequence transformation_sequence;
  743. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  744. &transformation_context, &fuzzer_context,
  745. &transformation_sequence, false, {});
  746. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  747. // We just check that the result is valid. Checking to what it should be
  748. // exactly equal to would be very fragile.
  749. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  750. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  751. }
  752. TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImageStructField) {
  753. std::string recipient_shader = R"(
  754. OpCapability Shader
  755. OpCapability ImageQuery
  756. %1 = OpExtInstImport "GLSL.std.450"
  757. OpMemoryModel Logical GLSL450
  758. OpEntryPoint Fragment %4 "main"
  759. OpExecutionMode %4 OriginUpperLeft
  760. OpSource ESSL 320
  761. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  762. %2 = OpTypeVoid
  763. %3 = OpTypeFunction %2
  764. %4 = OpFunction %2 None %3
  765. %5 = OpLabel
  766. OpReturn
  767. OpFunctionEnd
  768. )";
  769. std::string donor_shader = R"(
  770. OpCapability Shader
  771. OpCapability ImageQuery
  772. %1 = OpExtInstImport "GLSL.std.450"
  773. OpMemoryModel Logical GLSL450
  774. OpEntryPoint Fragment %4 "main"
  775. OpExecutionMode %4 OriginUpperLeft
  776. OpSource ESSL 320
  777. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  778. OpName %4 "main"
  779. OpName %10 "mySampler"
  780. OpName %21 "myTexture"
  781. OpName %33 "v"
  782. OpDecorate %10 RelaxedPrecision
  783. OpDecorate %10 DescriptorSet 0
  784. OpDecorate %10 Binding 0
  785. OpDecorate %11 RelaxedPrecision
  786. OpDecorate %21 RelaxedPrecision
  787. OpDecorate %21 DescriptorSet 0
  788. OpDecorate %21 Binding 1
  789. OpDecorate %22 RelaxedPrecision
  790. OpDecorate %34 RelaxedPrecision
  791. OpDecorate %40 RelaxedPrecision
  792. OpDecorate %42 RelaxedPrecision
  793. OpDecorate %43 RelaxedPrecision
  794. %2 = OpTypeVoid
  795. %3 = OpTypeFunction %2
  796. %6 = OpTypeFloat 32
  797. %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
  798. %8 = OpTypeSampledImage %7
  799. %9 = OpTypePointer UniformConstant %8
  800. %10 = OpVariable %9 UniformConstant
  801. %12 = OpTypeInt 32 1
  802. %13 = OpConstant %12 2
  803. %15 = OpTypeVector %12 2
  804. %17 = OpTypeInt 32 0
  805. %18 = OpConstant %17 0
  806. %20 = OpTypePointer UniformConstant %7
  807. %21 = OpVariable %20 UniformConstant
  808. %23 = OpConstant %12 1
  809. %25 = OpConstant %17 1
  810. %27 = OpTypeBool
  811. %31 = OpTypeVector %6 4
  812. %32 = OpTypePointer Function %31
  813. %35 = OpConstantComposite %15 %23 %23
  814. %36 = OpConstant %12 3
  815. %37 = OpConstant %12 4
  816. %38 = OpConstantComposite %15 %36 %37
  817. %201 = OpTypeStruct %7 %7
  818. %4 = OpFunction %2 None %3
  819. %5 = OpLabel
  820. %33 = OpVariable %32 Function
  821. %11 = OpLoad %8 %10
  822. %14 = OpImage %7 %11
  823. %22 = OpLoad %7 %21
  824. %200 = OpCompositeConstruct %201 %14 %22
  825. %202 = OpCompositeExtract %7 %200 0
  826. %203 = OpCompositeExtract %7 %200 1
  827. %24 = OpImageQuerySizeLod %15 %203 %23
  828. %16 = OpImageQuerySizeLod %15 %202 %13
  829. %26 = OpCompositeExtract %12 %24 1
  830. %19 = OpCompositeExtract %12 %16 0
  831. %28 = OpSGreaterThan %27 %19 %26
  832. OpSelectionMerge %30 None
  833. OpBranchConditional %28 %29 %41
  834. %29 = OpLabel
  835. %34 = OpLoad %8 %10
  836. %39 = OpImage %7 %34
  837. %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
  838. OpStore %33 %40
  839. OpBranch %30
  840. %41 = OpLabel
  841. %42 = OpLoad %7 %21
  842. %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
  843. OpStore %33 %43
  844. OpBranch %30
  845. %30 = OpLabel
  846. OpReturn
  847. OpFunctionEnd
  848. )";
  849. const auto env = SPV_ENV_UNIVERSAL_1_3;
  850. const auto consumer = nullptr;
  851. spvtools::ValidatorOptions validator_options;
  852. const auto recipient_context =
  853. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  854. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  855. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  856. const auto donor_context =
  857. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  858. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  859. donor_context.get(), validator_options, kConsoleMessageConsumer));
  860. TransformationContext transformation_context(
  861. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  862. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  863. false);
  864. protobufs::TransformationSequence transformation_sequence;
  865. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  866. &transformation_context, &fuzzer_context,
  867. &transformation_sequence, false, {});
  868. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  869. // We just check that the result is valid. Checking to what it should be
  870. // exactly equal to would be very fragile.
  871. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  872. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  873. }
  874. TEST(FuzzerPassDonateModulesTest, DonateCodeThatUsesImageFunctionParameter) {
  875. std::string recipient_shader = R"(
  876. OpCapability Shader
  877. OpCapability ImageQuery
  878. %1 = OpExtInstImport "GLSL.std.450"
  879. OpMemoryModel Logical GLSL450
  880. OpEntryPoint Fragment %4 "main"
  881. OpExecutionMode %4 OriginUpperLeft
  882. OpSource ESSL 320
  883. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  884. %2 = OpTypeVoid
  885. %3 = OpTypeFunction %2
  886. %4 = OpFunction %2 None %3
  887. %5 = OpLabel
  888. OpReturn
  889. OpFunctionEnd
  890. )";
  891. std::string donor_shader = R"(
  892. OpCapability Shader
  893. OpCapability ImageQuery
  894. %1 = OpExtInstImport "GLSL.std.450"
  895. OpMemoryModel Logical GLSL450
  896. OpEntryPoint Fragment %4 "main"
  897. OpExecutionMode %4 OriginUpperLeft
  898. OpSource ESSL 320
  899. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  900. OpName %4 "main"
  901. OpName %10 "mySampler"
  902. OpName %21 "myTexture"
  903. OpName %33 "v"
  904. OpDecorate %10 RelaxedPrecision
  905. OpDecorate %10 DescriptorSet 0
  906. OpDecorate %10 Binding 0
  907. OpDecorate %11 RelaxedPrecision
  908. OpDecorate %21 RelaxedPrecision
  909. OpDecorate %21 DescriptorSet 0
  910. OpDecorate %21 Binding 1
  911. OpDecorate %22 RelaxedPrecision
  912. OpDecorate %34 RelaxedPrecision
  913. OpDecorate %40 RelaxedPrecision
  914. OpDecorate %42 RelaxedPrecision
  915. OpDecorate %43 RelaxedPrecision
  916. %2 = OpTypeVoid
  917. %3 = OpTypeFunction %2
  918. %6 = OpTypeFloat 32
  919. %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
  920. %8 = OpTypeSampledImage %7
  921. %9 = OpTypePointer UniformConstant %8
  922. %10 = OpVariable %9 UniformConstant
  923. %12 = OpTypeInt 32 1
  924. %13 = OpConstant %12 2
  925. %15 = OpTypeVector %12 2
  926. %17 = OpTypeInt 32 0
  927. %18 = OpConstant %17 0
  928. %20 = OpTypePointer UniformConstant %7
  929. %21 = OpVariable %20 UniformConstant
  930. %23 = OpConstant %12 1
  931. %25 = OpConstant %17 1
  932. %27 = OpTypeBool
  933. %31 = OpTypeVector %6 4
  934. %32 = OpTypePointer Function %31
  935. %35 = OpConstantComposite %15 %23 %23
  936. %36 = OpConstant %12 3
  937. %37 = OpConstant %12 4
  938. %38 = OpConstantComposite %15 %36 %37
  939. %201 = OpTypeFunction %15 %7 %12
  940. %4 = OpFunction %2 None %3
  941. %5 = OpLabel
  942. %33 = OpVariable %32 Function
  943. %11 = OpLoad %8 %10
  944. %14 = OpImage %7 %11
  945. %16 = OpFunctionCall %15 %200 %14 %13
  946. %19 = OpCompositeExtract %12 %16 0
  947. %22 = OpLoad %7 %21
  948. %24 = OpImageQuerySizeLod %15 %22 %23
  949. %26 = OpCompositeExtract %12 %24 1
  950. %28 = OpSGreaterThan %27 %19 %26
  951. OpSelectionMerge %30 None
  952. OpBranchConditional %28 %29 %41
  953. %29 = OpLabel
  954. %34 = OpLoad %8 %10
  955. %39 = OpImage %7 %34
  956. %40 = OpImageFetch %31 %39 %35 Lod|ConstOffset %13 %38
  957. OpStore %33 %40
  958. OpBranch %30
  959. %41 = OpLabel
  960. %42 = OpLoad %7 %21
  961. %43 = OpImageFetch %31 %42 %35 Lod|ConstOffset %13 %38
  962. OpStore %33 %43
  963. OpBranch %30
  964. %30 = OpLabel
  965. OpReturn
  966. OpFunctionEnd
  967. %200 = OpFunction %15 None %201
  968. %202 = OpFunctionParameter %7
  969. %203 = OpFunctionParameter %12
  970. %204 = OpLabel
  971. %205 = OpImageQuerySizeLod %15 %202 %203
  972. OpReturnValue %205
  973. OpFunctionEnd
  974. )";
  975. const auto env = SPV_ENV_UNIVERSAL_1_3;
  976. const auto consumer = nullptr;
  977. spvtools::ValidatorOptions validator_options;
  978. const auto recipient_context =
  979. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  980. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  981. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  982. const auto donor_context =
  983. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  984. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  985. donor_context.get(), validator_options, kConsoleMessageConsumer));
  986. TransformationContext transformation_context(
  987. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  988. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  989. false);
  990. protobufs::TransformationSequence transformation_sequence;
  991. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  992. &transformation_context, &fuzzer_context,
  993. &transformation_sequence, false, {});
  994. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  995. // We just check that the result is valid. Checking to what it should be
  996. // exactly equal to would be very fragile.
  997. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  998. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  999. }
  1000. TEST(FuzzerPassDonateModulesTest, DonateShaderWithImageStorageClass) {
  1001. std::string recipient_shader = R"(
  1002. OpCapability Shader
  1003. OpCapability ImageQuery
  1004. %1 = OpExtInstImport "GLSL.std.450"
  1005. OpMemoryModel Logical GLSL450
  1006. OpEntryPoint Fragment %4 "main"
  1007. OpExecutionMode %4 OriginUpperLeft
  1008. OpSource ESSL 320
  1009. OpSourceExtension "GL_EXT_samplerless_texture_functions"
  1010. %2 = OpTypeVoid
  1011. %3 = OpTypeFunction %2
  1012. %4 = OpFunction %2 None %3
  1013. %5 = OpLabel
  1014. OpReturn
  1015. OpFunctionEnd
  1016. )";
  1017. std::string donor_shader = R"(
  1018. OpCapability Shader
  1019. OpCapability SampledBuffer
  1020. OpCapability ImageBuffer
  1021. %1 = OpExtInstImport "GLSL.std.450"
  1022. OpMemoryModel Logical GLSL450
  1023. OpEntryPoint Fragment %2 "MainPSPacked"
  1024. OpExecutionMode %2 OriginUpperLeft
  1025. OpDecorate %18 DescriptorSet 0
  1026. OpDecorate %18 Binding 128
  1027. %49 = OpTypeInt 32 0
  1028. %50 = OpTypeFloat 32
  1029. %58 = OpConstant %50 1
  1030. %66 = OpConstant %49 0
  1031. %87 = OpTypeVector %50 2
  1032. %88 = OpConstantComposite %87 %58 %58
  1033. %17 = OpTypeImage %49 2D 2 0 0 2 R32ui
  1034. %118 = OpTypePointer UniformConstant %17
  1035. %123 = OpTypeVector %49 2
  1036. %132 = OpTypeVoid
  1037. %133 = OpTypeFunction %132
  1038. %142 = OpTypePointer Image %49
  1039. %18 = OpVariable %118 UniformConstant
  1040. %2 = OpFunction %132 None %133
  1041. %153 = OpLabel
  1042. %495 = OpConvertFToU %123 %88
  1043. %501 = OpImageTexelPointer %142 %18 %495 %66
  1044. OpReturn
  1045. OpFunctionEnd
  1046. )";
  1047. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1048. const auto consumer = nullptr;
  1049. spvtools::ValidatorOptions validator_options;
  1050. const auto recipient_context =
  1051. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1052. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1053. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1054. const auto donor_context =
  1055. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1056. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1057. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1058. TransformationContext transformation_context(
  1059. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1060. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1061. false);
  1062. protobufs::TransformationSequence transformation_sequence;
  1063. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1064. &transformation_context, &fuzzer_context,
  1065. &transformation_sequence, false, {});
  1066. fuzzer_pass.DonateSingleModule(donor_context.get(), true);
  1067. // We just check that the result is valid. Checking to what it should be
  1068. // exactly equal to would be very fragile.
  1069. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1070. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1071. }
  1072. TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithRuntimeArray) {
  1073. std::string recipient_shader = R"(
  1074. OpCapability Shader
  1075. %1 = OpExtInstImport "GLSL.std.450"
  1076. OpMemoryModel Logical GLSL450
  1077. OpEntryPoint GLCompute %4 "main"
  1078. OpExecutionMode %4 LocalSize 1 1 1
  1079. OpSource ESSL 310
  1080. %2 = OpTypeVoid
  1081. %3 = OpTypeFunction %2
  1082. %4 = OpFunction %2 None %3
  1083. %5 = OpLabel
  1084. OpReturn
  1085. OpFunctionEnd
  1086. )";
  1087. std::string donor_shader = R"(
  1088. OpCapability Shader
  1089. %1 = OpExtInstImport "GLSL.std.450"
  1090. OpMemoryModel Logical GLSL450
  1091. OpEntryPoint GLCompute %4 "main"
  1092. OpExecutionMode %4 LocalSize 1 1 1
  1093. OpSource ESSL 310
  1094. OpDecorate %9 ArrayStride 4
  1095. OpMemberDecorate %10 0 Offset 0
  1096. OpDecorate %10 BufferBlock
  1097. OpDecorate %12 DescriptorSet 0
  1098. OpDecorate %12 Binding 0
  1099. %2 = OpTypeVoid
  1100. %3 = OpTypeFunction %2
  1101. %6 = OpTypeInt 32 1
  1102. %7 = OpTypePointer Function %6
  1103. %9 = OpTypeRuntimeArray %6
  1104. %10 = OpTypeStruct %9
  1105. %11 = OpTypePointer Uniform %10
  1106. %12 = OpVariable %11 Uniform
  1107. %13 = OpTypeInt 32 0
  1108. %16 = OpConstant %6 0
  1109. %18 = OpConstant %6 1
  1110. %20 = OpTypePointer Uniform %6
  1111. %4 = OpFunction %2 None %3
  1112. %5 = OpLabel
  1113. %8 = OpVariable %7 Function
  1114. %14 = OpArrayLength %13 %12 0
  1115. %15 = OpBitcast %6 %14
  1116. OpStore %8 %15
  1117. %17 = OpLoad %6 %8
  1118. %19 = OpISub %6 %17 %18
  1119. %21 = OpAccessChain %20 %12 %16 %19
  1120. OpStore %21 %16
  1121. OpReturn
  1122. OpFunctionEnd
  1123. )";
  1124. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1125. const auto consumer = nullptr;
  1126. spvtools::ValidatorOptions validator_options;
  1127. const auto recipient_context =
  1128. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1129. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1130. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1131. const auto donor_context =
  1132. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1133. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1134. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1135. TransformationContext transformation_context(
  1136. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1137. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1138. false);
  1139. protobufs::TransformationSequence transformation_sequence;
  1140. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1141. &transformation_context, &fuzzer_context,
  1142. &transformation_sequence, false, {});
  1143. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1144. // We just check that the result is valid. Checking to what it should be
  1145. // exactly equal to would be very fragile.
  1146. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1147. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1148. }
  1149. TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithRuntimeArrayLivesafe) {
  1150. std::string recipient_shader = R"(
  1151. OpCapability Shader
  1152. %1 = OpExtInstImport "GLSL.std.450"
  1153. OpMemoryModel Logical GLSL450
  1154. OpEntryPoint GLCompute %4 "main"
  1155. OpExecutionMode %4 LocalSize 1 1 1
  1156. OpSource ESSL 310
  1157. %2 = OpTypeVoid
  1158. %3 = OpTypeFunction %2
  1159. %4 = OpFunction %2 None %3
  1160. %5 = OpLabel
  1161. OpReturn
  1162. OpFunctionEnd
  1163. )";
  1164. std::string donor_shader = R"(
  1165. OpCapability Shader
  1166. %1 = OpExtInstImport "GLSL.std.450"
  1167. OpMemoryModel Logical GLSL450
  1168. OpEntryPoint GLCompute %4 "main"
  1169. OpExecutionMode %4 LocalSize 1 1 1
  1170. OpSource ESSL 310
  1171. OpDecorate %16 ArrayStride 4
  1172. OpMemberDecorate %17 0 Offset 0
  1173. OpDecorate %17 BufferBlock
  1174. OpDecorate %19 DescriptorSet 0
  1175. OpDecorate %19 Binding 0
  1176. %2 = OpTypeVoid
  1177. %3 = OpTypeFunction %2
  1178. %6 = OpTypeInt 32 1
  1179. %7 = OpTypePointer Function %6
  1180. %9 = OpConstant %6 0
  1181. %16 = OpTypeRuntimeArray %6
  1182. %17 = OpTypeStruct %16
  1183. %18 = OpTypePointer Uniform %17
  1184. %19 = OpVariable %18 Uniform
  1185. %20 = OpTypeInt 32 0
  1186. %23 = OpTypeBool
  1187. %26 = OpConstant %6 32
  1188. %27 = OpTypePointer Uniform %6
  1189. %30 = OpConstant %6 1
  1190. %4 = OpFunction %2 None %3
  1191. %5 = OpLabel
  1192. %8 = OpVariable %7 Function
  1193. OpStore %8 %9
  1194. OpBranch %10
  1195. %10 = OpLabel
  1196. OpLoopMerge %12 %13 None
  1197. OpBranch %14
  1198. %14 = OpLabel
  1199. %15 = OpLoad %6 %8
  1200. %21 = OpArrayLength %20 %19 0
  1201. %22 = OpBitcast %6 %21
  1202. %24 = OpSLessThan %23 %15 %22
  1203. OpBranchConditional %24 %11 %12
  1204. %11 = OpLabel
  1205. %25 = OpLoad %6 %8
  1206. %28 = OpAccessChain %27 %19 %9 %25
  1207. OpStore %28 %26
  1208. OpBranch %13
  1209. %13 = OpLabel
  1210. %29 = OpLoad %6 %8
  1211. %31 = OpIAdd %6 %29 %30
  1212. OpStore %8 %31
  1213. OpBranch %10
  1214. %12 = OpLabel
  1215. OpReturn
  1216. OpFunctionEnd
  1217. )";
  1218. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1219. const auto consumer = nullptr;
  1220. spvtools::ValidatorOptions validator_options;
  1221. const auto recipient_context =
  1222. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1223. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1224. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1225. const auto donor_context =
  1226. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1227. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1228. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1229. TransformationContext transformation_context(
  1230. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1231. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1232. false);
  1233. protobufs::TransformationSequence transformation_sequence;
  1234. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1235. &transformation_context, &fuzzer_context,
  1236. &transformation_sequence, false, {});
  1237. fuzzer_pass.DonateSingleModule(donor_context.get(), true);
  1238. // We just check that the result is valid. Checking to what it should be
  1239. // exactly equal to would be very fragile.
  1240. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1241. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1242. }
  1243. TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithWorkgroupVariables) {
  1244. std::string recipient_shader = R"(
  1245. OpCapability Shader
  1246. %1 = OpExtInstImport "GLSL.std.450"
  1247. OpMemoryModel Logical GLSL450
  1248. OpEntryPoint GLCompute %4 "main"
  1249. OpExecutionMode %4 LocalSize 1 1 1
  1250. OpSource ESSL 310
  1251. %2 = OpTypeVoid
  1252. %3 = OpTypeFunction %2
  1253. %4 = OpFunction %2 None %3
  1254. %5 = OpLabel
  1255. OpReturn
  1256. OpFunctionEnd
  1257. )";
  1258. std::string donor_shader = R"(
  1259. OpCapability Shader
  1260. %1 = OpExtInstImport "GLSL.std.450"
  1261. OpMemoryModel Logical GLSL450
  1262. OpEntryPoint GLCompute %4 "main"
  1263. OpExecutionMode %4 LocalSize 1 1 1
  1264. OpSource ESSL 310
  1265. %2 = OpTypeVoid
  1266. %3 = OpTypeFunction %2
  1267. %6 = OpTypeInt 32 1
  1268. %7 = OpTypePointer Workgroup %6
  1269. %8 = OpVariable %7 Workgroup
  1270. %9 = OpConstant %6 2
  1271. %10 = OpVariable %7 Workgroup
  1272. %4 = OpFunction %2 None %3
  1273. %5 = OpLabel
  1274. OpStore %8 %9
  1275. %11 = OpLoad %6 %8
  1276. OpStore %10 %11
  1277. OpReturn
  1278. OpFunctionEnd
  1279. )";
  1280. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1281. const auto consumer = nullptr;
  1282. spvtools::ValidatorOptions validator_options;
  1283. const auto recipient_context =
  1284. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1285. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1286. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1287. const auto donor_context =
  1288. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1289. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1290. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1291. TransformationContext transformation_context(
  1292. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1293. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1294. false);
  1295. protobufs::TransformationSequence transformation_sequence;
  1296. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1297. &transformation_context, &fuzzer_context,
  1298. &transformation_sequence, false, {});
  1299. fuzzer_pass.DonateSingleModule(donor_context.get(), true);
  1300. // We just check that the result is valid. Checking to what it should be
  1301. // exactly equal to would be very fragile.
  1302. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1303. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1304. }
  1305. TEST(FuzzerPassDonateModulesTest, DonateComputeShaderWithAtomics) {
  1306. std::string recipient_shader = R"(
  1307. OpCapability Shader
  1308. %1 = OpExtInstImport "GLSL.std.450"
  1309. OpMemoryModel Logical GLSL450
  1310. OpEntryPoint GLCompute %4 "main"
  1311. OpExecutionMode %4 LocalSize 1 1 1
  1312. OpSource ESSL 310
  1313. %2 = OpTypeVoid
  1314. %3 = OpTypeFunction %2
  1315. %4 = OpFunction %2 None %3
  1316. %5 = OpLabel
  1317. OpReturn
  1318. OpFunctionEnd
  1319. )";
  1320. std::string donor_shader = R"(
  1321. OpCapability Shader
  1322. %1 = OpExtInstImport "GLSL.std.450"
  1323. OpMemoryModel Logical GLSL450
  1324. OpEntryPoint GLCompute %4 "main"
  1325. OpExecutionMode %4 LocalSize 1 1 1
  1326. OpSource ESSL 310
  1327. OpMemberDecorate %9 0 Offset 0
  1328. OpDecorate %9 BufferBlock
  1329. OpDecorate %11 DescriptorSet 0
  1330. OpDecorate %11 Binding 0
  1331. %2 = OpTypeVoid
  1332. %3 = OpTypeFunction %2
  1333. %6 = OpTypeInt 32 0
  1334. %7 = OpTypePointer Function %6
  1335. %9 = OpTypeStruct %6
  1336. %10 = OpTypePointer Uniform %9
  1337. %11 = OpVariable %10 Uniform
  1338. %12 = OpTypeInt 32 1
  1339. %13 = OpConstant %12 0
  1340. %14 = OpTypePointer Uniform %6
  1341. %16 = OpConstant %6 1
  1342. %17 = OpConstant %6 0
  1343. %4 = OpFunction %2 None %3
  1344. %5 = OpLabel
  1345. %8 = OpVariable %7 Function
  1346. %15 = OpAccessChain %14 %11 %13
  1347. %18 = OpAtomicIAdd %6 %15 %16 %17 %16
  1348. OpStore %8 %18
  1349. %19 = OpAccessChain %14 %11 %13
  1350. %20 = OpLoad %6 %8
  1351. %21 = OpAtomicUMin %6 %19 %16 %17 %20
  1352. OpStore %8 %21
  1353. %22 = OpAccessChain %14 %11 %13
  1354. %23 = OpLoad %6 %8
  1355. %24 = OpAtomicUMax %6 %22 %16 %17 %23
  1356. OpStore %8 %24
  1357. %25 = OpAccessChain %14 %11 %13
  1358. %26 = OpLoad %6 %8
  1359. %27 = OpAtomicAnd %6 %25 %16 %17 %26
  1360. OpStore %8 %27
  1361. %28 = OpAccessChain %14 %11 %13
  1362. %29 = OpLoad %6 %8
  1363. %30 = OpAtomicOr %6 %28 %16 %17 %29
  1364. OpStore %8 %30
  1365. %31 = OpAccessChain %14 %11 %13
  1366. %32 = OpLoad %6 %8
  1367. %33 = OpAtomicXor %6 %31 %16 %17 %32
  1368. OpStore %8 %33
  1369. %34 = OpAccessChain %14 %11 %13
  1370. %35 = OpLoad %6 %8
  1371. %36 = OpAtomicExchange %6 %34 %16 %17 %35
  1372. OpStore %8 %36
  1373. %37 = OpAccessChain %14 %11 %13
  1374. %38 = OpLoad %6 %8
  1375. %39 = OpAtomicCompareExchange %6 %37 %16 %17 %17 %16 %38
  1376. OpStore %8 %39
  1377. OpReturn
  1378. OpFunctionEnd
  1379. )";
  1380. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1381. const auto consumer = nullptr;
  1382. spvtools::ValidatorOptions validator_options;
  1383. const auto recipient_context =
  1384. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1385. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1386. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1387. const auto donor_context =
  1388. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1389. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1390. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1391. TransformationContext transformation_context(
  1392. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1393. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1394. false);
  1395. protobufs::TransformationSequence transformation_sequence;
  1396. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1397. &transformation_context, &fuzzer_context,
  1398. &transformation_sequence, false, {});
  1399. fuzzer_pass.DonateSingleModule(donor_context.get(), true);
  1400. // We just check that the result is valid. Checking to what it should be
  1401. // exactly equal to would be very fragile.
  1402. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1403. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1404. }
  1405. TEST(FuzzerPassDonateModulesTest, Miscellaneous1) {
  1406. std::string recipient_shader = R"(
  1407. OpCapability Shader
  1408. %1 = OpExtInstImport "GLSL.std.450"
  1409. OpMemoryModel Logical GLSL450
  1410. OpEntryPoint Fragment %4 "main"
  1411. OpExecutionMode %4 OriginUpperLeft
  1412. OpSource ESSL 310
  1413. %2 = OpTypeVoid
  1414. %3 = OpTypeFunction %2
  1415. %4 = OpFunction %2 None %3
  1416. %5 = OpLabel
  1417. OpReturn
  1418. OpFunctionEnd
  1419. )";
  1420. std::string donor_shader = R"(
  1421. OpCapability Shader
  1422. %1 = OpExtInstImport "GLSL.std.450"
  1423. OpMemoryModel Logical GLSL450
  1424. OpEntryPoint Fragment %4 "main"
  1425. OpExecutionMode %4 OriginUpperLeft
  1426. OpSource ESSL 310
  1427. OpName %4 "main"
  1428. OpName %6 "foo("
  1429. OpName %10 "x"
  1430. OpName %12 "i"
  1431. OpName %33 "i"
  1432. OpName %42 "j"
  1433. OpDecorate %10 RelaxedPrecision
  1434. OpDecorate %12 RelaxedPrecision
  1435. OpDecorate %19 RelaxedPrecision
  1436. OpDecorate %23 RelaxedPrecision
  1437. OpDecorate %24 RelaxedPrecision
  1438. OpDecorate %25 RelaxedPrecision
  1439. OpDecorate %26 RelaxedPrecision
  1440. OpDecorate %27 RelaxedPrecision
  1441. OpDecorate %28 RelaxedPrecision
  1442. OpDecorate %30 RelaxedPrecision
  1443. OpDecorate %33 RelaxedPrecision
  1444. OpDecorate %39 RelaxedPrecision
  1445. OpDecorate %42 RelaxedPrecision
  1446. OpDecorate %49 RelaxedPrecision
  1447. OpDecorate %52 RelaxedPrecision
  1448. OpDecorate %53 RelaxedPrecision
  1449. OpDecorate %58 RelaxedPrecision
  1450. OpDecorate %59 RelaxedPrecision
  1451. OpDecorate %60 RelaxedPrecision
  1452. OpDecorate %63 RelaxedPrecision
  1453. OpDecorate %64 RelaxedPrecision
  1454. %2 = OpTypeVoid
  1455. %3 = OpTypeFunction %2
  1456. %8 = OpTypeInt 32 1
  1457. %9 = OpTypePointer Function %8
  1458. %11 = OpConstant %8 2
  1459. %13 = OpConstant %8 0
  1460. %20 = OpConstant %8 100
  1461. %21 = OpTypeBool
  1462. %29 = OpConstant %8 1
  1463. %40 = OpConstant %8 10
  1464. %43 = OpConstant %8 20
  1465. %61 = OpConstant %8 4
  1466. %4 = OpFunction %2 None %3
  1467. %5 = OpLabel
  1468. %33 = OpVariable %9 Function
  1469. %42 = OpVariable %9 Function
  1470. %32 = OpFunctionCall %2 %6
  1471. OpStore %33 %13
  1472. OpBranch %34
  1473. %34 = OpLabel
  1474. OpLoopMerge %36 %37 None
  1475. OpBranch %38
  1476. %38 = OpLabel
  1477. %39 = OpLoad %8 %33
  1478. %41 = OpSLessThan %21 %39 %40
  1479. OpBranchConditional %41 %35 %36
  1480. %35 = OpLabel
  1481. OpStore %42 %43
  1482. OpBranch %44
  1483. %44 = OpLabel
  1484. OpLoopMerge %46 %47 None
  1485. OpBranch %48
  1486. %48 = OpLabel
  1487. %49 = OpLoad %8 %42
  1488. %50 = OpSGreaterThan %21 %49 %13
  1489. OpBranchConditional %50 %45 %46
  1490. %45 = OpLabel
  1491. %51 = OpFunctionCall %2 %6
  1492. %52 = OpLoad %8 %42
  1493. %53 = OpISub %8 %52 %29
  1494. OpStore %42 %53
  1495. OpBranch %47
  1496. %47 = OpLabel
  1497. OpBranch %44
  1498. %46 = OpLabel
  1499. OpBranch %54
  1500. %54 = OpLabel
  1501. OpLoopMerge %56 %57 None
  1502. OpBranch %55
  1503. %55 = OpLabel
  1504. %58 = OpLoad %8 %33
  1505. %59 = OpIAdd %8 %58 %29
  1506. OpStore %33 %59
  1507. OpBranch %57
  1508. %57 = OpLabel
  1509. %60 = OpLoad %8 %33
  1510. %62 = OpSLessThan %21 %60 %61
  1511. OpBranchConditional %62 %54 %56
  1512. %56 = OpLabel
  1513. OpBranch %37
  1514. %37 = OpLabel
  1515. %63 = OpLoad %8 %33
  1516. %64 = OpIAdd %8 %63 %29
  1517. OpStore %33 %64
  1518. OpBranch %34
  1519. %36 = OpLabel
  1520. OpReturn
  1521. OpFunctionEnd
  1522. %6 = OpFunction %2 None %3
  1523. %7 = OpLabel
  1524. %10 = OpVariable %9 Function
  1525. %12 = OpVariable %9 Function
  1526. OpStore %10 %11
  1527. OpStore %12 %13
  1528. OpBranch %14
  1529. %14 = OpLabel
  1530. OpLoopMerge %16 %17 None
  1531. OpBranch %18
  1532. %18 = OpLabel
  1533. %19 = OpLoad %8 %12
  1534. %22 = OpSLessThan %21 %19 %20
  1535. OpBranchConditional %22 %15 %16
  1536. %15 = OpLabel
  1537. %23 = OpLoad %8 %12
  1538. %24 = OpLoad %8 %10
  1539. %25 = OpIAdd %8 %24 %23
  1540. OpStore %10 %25
  1541. %26 = OpLoad %8 %10
  1542. %27 = OpIMul %8 %26 %11
  1543. OpStore %10 %27
  1544. OpBranch %17
  1545. %17 = OpLabel
  1546. %28 = OpLoad %8 %12
  1547. %30 = OpIAdd %8 %28 %29
  1548. OpStore %12 %30
  1549. OpBranch %14
  1550. %16 = OpLabel
  1551. OpReturn
  1552. OpFunctionEnd
  1553. )";
  1554. const auto env = SPV_ENV_UNIVERSAL_1_5;
  1555. const auto consumer = nullptr;
  1556. spvtools::ValidatorOptions validator_options;
  1557. const auto recipient_context =
  1558. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1559. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1560. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1561. const auto donor_context =
  1562. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1563. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1564. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1565. TransformationContext transformation_context(
  1566. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1567. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1568. false);
  1569. protobufs::TransformationSequence transformation_sequence;
  1570. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1571. &transformation_context, &fuzzer_context,
  1572. &transformation_sequence, false, {});
  1573. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1574. // We just check that the result is valid. Checking to what it should be
  1575. // exactly equal to would be very fragile.
  1576. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1577. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1578. }
  1579. TEST(FuzzerPassDonateModulesTest, OpSpecConstantInstructions) {
  1580. std::string donor_shader = R"(
  1581. OpCapability Shader
  1582. %1 = OpExtInstImport "GLSL.std.450"
  1583. OpMemoryModel Logical GLSL450
  1584. OpEntryPoint Fragment %4 "main"
  1585. OpExecutionMode %4 OriginUpperLeft
  1586. OpSource ESSL 310
  1587. %2 = OpTypeVoid
  1588. %3 = OpTypeFunction %2
  1589. %6 = OpTypeBool
  1590. %7 = OpTypeInt 32 1
  1591. %8 = OpTypeStruct %6 %6 %7
  1592. %9 = OpSpecConstantTrue %6
  1593. %10 = OpSpecConstantFalse %6
  1594. %11 = OpSpecConstant %7 2
  1595. %12 = OpSpecConstantComposite %8 %9 %10 %11
  1596. %13 = OpSpecConstantOp %6 LogicalEqual %9 %10
  1597. %4 = OpFunction %2 None %3
  1598. %5 = OpLabel
  1599. OpReturn
  1600. OpFunctionEnd
  1601. )";
  1602. std::string recipient_shader = R"(
  1603. OpCapability Shader
  1604. %1 = OpExtInstImport "GLSL.std.450"
  1605. OpMemoryModel Logical GLSL450
  1606. OpEntryPoint Fragment %4 "main"
  1607. OpExecutionMode %4 OriginUpperLeft
  1608. OpSource ESSL 310
  1609. %2 = OpTypeVoid
  1610. %3 = OpTypeFunction %2
  1611. %4 = OpFunction %2 None %3
  1612. %5 = OpLabel
  1613. OpReturn
  1614. OpFunctionEnd
  1615. )";
  1616. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1617. const auto consumer = nullptr;
  1618. spvtools::ValidatorOptions validator_options;
  1619. const auto recipient_context =
  1620. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1621. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1622. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1623. const auto donor_context =
  1624. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1625. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1626. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1627. TransformationContext transformation_context(
  1628. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1629. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1630. false);
  1631. protobufs::TransformationSequence transformation_sequence;
  1632. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1633. &transformation_context, &fuzzer_context,
  1634. &transformation_sequence, false, {});
  1635. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1636. // Check that the module is valid first.
  1637. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1638. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1639. std::string expected_shader = R"(
  1640. OpCapability Shader
  1641. %1 = OpExtInstImport "GLSL.std.450"
  1642. OpMemoryModel Logical GLSL450
  1643. OpEntryPoint Fragment %4 "main"
  1644. OpExecutionMode %4 OriginUpperLeft
  1645. OpSource ESSL 310
  1646. %2 = OpTypeVoid
  1647. %3 = OpTypeFunction %2
  1648. %100 = OpTypeBool
  1649. %101 = OpTypeInt 32 1
  1650. %102 = OpTypeStruct %100 %100 %101
  1651. %103 = OpConstantTrue %100
  1652. %104 = OpConstantFalse %100
  1653. %105 = OpConstant %101 2
  1654. %106 = OpConstantComposite %102 %103 %104 %105
  1655. %107 = OpSpecConstantOp %100 LogicalEqual %103 %104
  1656. %4 = OpFunction %2 None %3
  1657. %5 = OpLabel
  1658. OpReturn
  1659. OpFunctionEnd
  1660. %108 = OpFunction %2 None %3
  1661. %109 = OpLabel
  1662. OpReturn
  1663. OpFunctionEnd
  1664. )";
  1665. // Now check that the transformation has produced the expected result.
  1666. ASSERT_TRUE(IsEqual(env, expected_shader, recipient_context.get()));
  1667. }
  1668. TEST(FuzzerPassDonateModulesTest, DonationSupportsOpTypeRuntimeArray) {
  1669. std::string donor_shader = R"(
  1670. OpCapability Shader
  1671. OpExtension "SPV_KHR_storage_buffer_storage_class"
  1672. OpMemoryModel Logical GLSL450
  1673. OpEntryPoint GLCompute %29 "kernel_1"
  1674. OpEntryPoint GLCompute %37 "kernel_2"
  1675. OpSource OpenCL_C 120
  1676. OpDecorate %2 ArrayStride 4
  1677. OpMemberDecorate %3 0 Offset 0
  1678. OpDecorate %3 Block
  1679. OpMemberDecorate %5 0 Offset 0
  1680. OpMemberDecorate %6 0 Offset 0
  1681. OpDecorate %6 Block
  1682. OpDecorate %21 BuiltIn WorkgroupSize
  1683. OpDecorate %23 DescriptorSet 0
  1684. OpDecorate %23 Binding 0
  1685. OpDecorate %25 SpecId 3
  1686. OpDecorate %18 SpecId 0
  1687. OpDecorate %19 SpecId 1
  1688. OpDecorate %20 SpecId 2
  1689. %1 = OpTypeInt 32 0
  1690. %2 = OpTypeRuntimeArray %1
  1691. %3 = OpTypeStruct %2
  1692. %4 = OpTypePointer StorageBuffer %3
  1693. %5 = OpTypeStruct %1
  1694. %6 = OpTypeStruct %5
  1695. %7 = OpTypePointer PushConstant %6
  1696. %8 = OpTypeFloat 32
  1697. %9 = OpTypeVoid
  1698. %10 = OpTypeFunction %9
  1699. %11 = OpTypePointer Workgroup %1
  1700. %12 = OpTypePointer PushConstant %5
  1701. %13 = OpTypePointer StorageBuffer %1
  1702. %14 = OpTypeFunction %1 %1
  1703. %15 = OpTypeVector %1 3
  1704. %16 = OpTypePointer Private %15
  1705. %17 = OpConstant %1 0
  1706. %18 = OpSpecConstant %1 1
  1707. %19 = OpSpecConstant %1 1
  1708. %20 = OpSpecConstant %1 1
  1709. %21 = OpSpecConstantComposite %15 %18 %19 %20
  1710. %25 = OpSpecConstant %1 1
  1711. %26 = OpTypeArray %1 %25
  1712. %27 = OpTypePointer Workgroup %26
  1713. %22 = OpVariable %16 Private %21
  1714. %23 = OpVariable %4 StorageBuffer
  1715. %24 = OpVariable %7 PushConstant
  1716. %28 = OpVariable %27 Workgroup
  1717. %29 = OpFunction %9 None %10
  1718. %30 = OpLabel
  1719. %31 = OpAccessChain %11 %28 %17
  1720. %32 = OpAccessChain %12 %24 %17
  1721. %33 = OpLoad %5 %32
  1722. %34 = OpCompositeExtract %1 %33 0
  1723. %35 = OpFunctionCall %1 %45 %34
  1724. %36 = OpAccessChain %13 %23 %17 %34
  1725. OpStore %36 %35
  1726. OpReturn
  1727. OpFunctionEnd
  1728. %37 = OpFunction %9 None %10
  1729. %38 = OpLabel
  1730. %39 = OpAccessChain %11 %28 %17
  1731. %40 = OpAccessChain %12 %24 %17
  1732. %41 = OpLoad %5 %40
  1733. %42 = OpCompositeExtract %1 %41 0
  1734. %43 = OpFunctionCall %1 %45 %42
  1735. %44 = OpAccessChain %13 %23 %17 %42
  1736. OpStore %44 %43
  1737. OpReturn
  1738. OpFunctionEnd
  1739. %45 = OpFunction %1 Pure %14
  1740. %46 = OpFunctionParameter %1
  1741. %47 = OpLabel
  1742. %48 = OpAccessChain %11 %28 %46
  1743. %49 = OpLoad %1 %48
  1744. OpReturnValue %49
  1745. OpFunctionEnd
  1746. )";
  1747. std::string recipient_shader = R"(
  1748. OpCapability Shader
  1749. %1 = OpExtInstImport "GLSL.std.450"
  1750. OpMemoryModel Logical GLSL450
  1751. OpEntryPoint Fragment %4 "main"
  1752. OpExecutionMode %4 OriginUpperLeft
  1753. OpSource ESSL 310
  1754. %2 = OpTypeVoid
  1755. %3 = OpTypeFunction %2
  1756. %4 = OpFunction %2 None %3
  1757. %5 = OpLabel
  1758. OpReturn
  1759. OpFunctionEnd
  1760. )";
  1761. const auto env = SPV_ENV_UNIVERSAL_1_0;
  1762. const auto consumer = nullptr;
  1763. spvtools::ValidatorOptions validator_options;
  1764. const auto recipient_context =
  1765. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1766. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1767. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1768. const auto donor_context =
  1769. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1770. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1771. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1772. TransformationContext transformation_context(
  1773. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1774. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1775. false);
  1776. protobufs::TransformationSequence transformation_sequence;
  1777. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1778. &transformation_context, &fuzzer_context,
  1779. &transformation_sequence, false, {});
  1780. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1781. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1782. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1783. }
  1784. TEST(FuzzerPassDonateModulesTest, HandlesCapabilities) {
  1785. std::string donor_shader = R"(
  1786. OpCapability VariablePointersStorageBuffer
  1787. %1 = OpExtInstImport "GLSL.std.450"
  1788. OpMemoryModel Logical GLSL450
  1789. OpEntryPoint Fragment %4 "main"
  1790. OpExecutionMode %4 OriginUpperLeft
  1791. OpSource ESSL 310
  1792. %2 = OpTypeVoid
  1793. %3 = OpTypeFunction %2
  1794. %6 = OpTypeFloat 32
  1795. %11 = OpConstant %6 23
  1796. %7 = OpTypePointer Function %6
  1797. %4 = OpFunction %2 None %3
  1798. %5 = OpLabel
  1799. %8 = OpVariable %7 Function
  1800. OpBranch %9
  1801. %9 = OpLabel
  1802. %10 = OpPhi %7 %8 %5
  1803. OpStore %10 %11
  1804. OpReturn
  1805. OpFunctionEnd
  1806. )";
  1807. std::string recipient_shader = R"(
  1808. OpCapability Shader
  1809. %1 = OpExtInstImport "GLSL.std.450"
  1810. OpMemoryModel Logical GLSL450
  1811. OpEntryPoint Fragment %4 "main"
  1812. OpExecutionMode %4 OriginUpperLeft
  1813. OpSource ESSL 310
  1814. %2 = OpTypeVoid
  1815. %3 = OpTypeFunction %2
  1816. %4 = OpFunction %2 None %3
  1817. %5 = OpLabel
  1818. OpReturn
  1819. OpFunctionEnd
  1820. )";
  1821. const auto env = SPV_ENV_UNIVERSAL_1_3;
  1822. const auto consumer = nullptr;
  1823. spvtools::ValidatorOptions validator_options;
  1824. const auto recipient_context =
  1825. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  1826. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1827. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1828. const auto donor_context =
  1829. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  1830. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1831. donor_context.get(), validator_options, kConsoleMessageConsumer));
  1832. TransformationContext transformation_context(
  1833. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  1834. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  1835. false);
  1836. protobufs::TransformationSequence transformation_sequence;
  1837. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  1838. &transformation_context, &fuzzer_context,
  1839. &transformation_sequence, false, {});
  1840. ASSERT_TRUE(donor_context->get_feature_mgr()->HasCapability(
  1841. spv::Capability::VariablePointersStorageBuffer));
  1842. ASSERT_FALSE(recipient_context->get_feature_mgr()->HasCapability(
  1843. spv::Capability::VariablePointersStorageBuffer));
  1844. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1845. // Check that recipient module hasn't changed.
  1846. ASSERT_TRUE(IsEqual(env, recipient_shader, recipient_context.get()));
  1847. // Add the missing capability.
  1848. //
  1849. // We are adding VariablePointers to test the case when donor and recipient
  1850. // have different OpCapability instructions but the same capabilities. In our
  1851. // example, VariablePointers implicitly declares
  1852. // VariablePointersStorageBuffer. Thus, two modules must be compatible.
  1853. recipient_context->AddCapability(spv::Capability::VariablePointers);
  1854. ASSERT_TRUE(donor_context->get_feature_mgr()->HasCapability(
  1855. spv::Capability::VariablePointersStorageBuffer));
  1856. ASSERT_TRUE(recipient_context->get_feature_mgr()->HasCapability(
  1857. spv::Capability::VariablePointersStorageBuffer));
  1858. fuzzer_pass.DonateSingleModule(donor_context.get(), false);
  1859. // Check that donation was successful.
  1860. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  1861. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  1862. std::string after_transformation = R"(
  1863. OpCapability Shader
  1864. OpCapability VariablePointers
  1865. %1 = OpExtInstImport "GLSL.std.450"
  1866. OpMemoryModel Logical GLSL450
  1867. OpEntryPoint Fragment %4 "main"
  1868. OpExecutionMode %4 OriginUpperLeft
  1869. OpSource ESSL 310
  1870. %2 = OpTypeVoid
  1871. %3 = OpTypeFunction %2
  1872. %100 = OpTypeFloat 32
  1873. %101 = OpConstant %100 23
  1874. %102 = OpTypePointer Function %100
  1875. %105 = OpConstant %100 0
  1876. %4 = OpFunction %2 None %3
  1877. %5 = OpLabel
  1878. OpReturn
  1879. OpFunctionEnd
  1880. %103 = OpFunction %2 None %3
  1881. %104 = OpLabel
  1882. %106 = OpVariable %102 Function %105
  1883. OpBranch %107
  1884. %107 = OpLabel
  1885. %108 = OpPhi %102 %106 %104
  1886. OpStore %108 %101
  1887. OpReturn
  1888. OpFunctionEnd
  1889. )";
  1890. ASSERT_TRUE(IsEqual(env, after_transformation, recipient_context.get()));
  1891. }
  1892. TEST(FuzzerPassDonateModulesTest, HandlesOpPhisInMergeBlock) {
  1893. std::string donor_shader = R"(
  1894. ; OpPhis don't support pointers without this capability
  1895. ; and we need pointers to test some of the functionality
  1896. OpCapability VariablePointers
  1897. OpCapability Shader
  1898. %1 = OpExtInstImport "GLSL.std.450"
  1899. OpMemoryModel Logical GLSL450
  1900. OpEntryPoint Fragment %4 "main"
  1901. OpExecutionMode %4 OriginUpperLeft
  1902. OpSource ESSL 310
  1903. %2 = OpTypeVoid
  1904. %3 = OpTypeFunction %2
  1905. %14 = OpTypeBool
  1906. %15 = OpConstantTrue %14
  1907. %42 = OpTypePointer Function %14
  1908. ; back-edge block is unreachable in the CFG
  1909. %4 = OpFunction %2 None %3
  1910. %5 = OpLabel
  1911. OpBranch %6
  1912. %6 = OpLabel
  1913. OpLoopMerge %8 %7 None
  1914. OpBranch %8
  1915. %7 = OpLabel
  1916. OpBranch %6
  1917. %8 = OpLabel
  1918. OpReturn
  1919. OpFunctionEnd
  1920. ; back-edge block already has an edge to the merge block
  1921. %9 = OpFunction %2 None %3
  1922. %10 = OpLabel
  1923. OpBranch %11
  1924. %11 = OpLabel
  1925. OpLoopMerge %13 %12 None
  1926. OpBranch %12
  1927. %12 = OpLabel
  1928. OpBranchConditional %15 %11 %13
  1929. %13 = OpLabel
  1930. OpReturn
  1931. OpFunctionEnd
  1932. ; merge block has no OpPhis
  1933. %16 = OpFunction %2 None %3
  1934. %17 = OpLabel
  1935. OpBranch %18
  1936. %18 = OpLabel
  1937. OpLoopMerge %20 %19 None
  1938. OpBranchConditional %15 %19 %20
  1939. %19 = OpLabel
  1940. OpBranch %18
  1941. %20 = OpLabel
  1942. OpReturn
  1943. OpFunctionEnd
  1944. ; merge block has OpPhis and some of their operands are available at
  1945. ; the back-edge block
  1946. %21 = OpFunction %2 None %3
  1947. %22 = OpLabel
  1948. OpBranch %23
  1949. %23 = OpLabel
  1950. %24 = OpCopyObject %14 %15
  1951. OpLoopMerge %28 %27 None
  1952. OpBranchConditional %15 %25 %28
  1953. %25 = OpLabel
  1954. %26 = OpCopyObject %14 %15
  1955. OpBranchConditional %15 %28 %27
  1956. %27 = OpLabel
  1957. OpBranch %23
  1958. %28 = OpLabel
  1959. %29 = OpPhi %14 %24 %23 %26 %25
  1960. OpReturn
  1961. OpFunctionEnd
  1962. ; none of the OpPhis' operands dominate the back-edge block but some of
  1963. ; them have basic type
  1964. %30 = OpFunction %2 None %3
  1965. %31 = OpLabel
  1966. OpBranch %32
  1967. %32 = OpLabel
  1968. OpLoopMerge %40 %39 None
  1969. OpBranch %33
  1970. %33 = OpLabel
  1971. OpSelectionMerge %38 None
  1972. OpBranchConditional %15 %34 %36
  1973. %34 = OpLabel
  1974. %35 = OpCopyObject %14 %15
  1975. OpBranchConditional %35 %38 %40
  1976. %36 = OpLabel
  1977. %37 = OpCopyObject %14 %15
  1978. OpBranchConditional %37 %38 %40
  1979. %38 = OpLabel
  1980. OpBranch %39
  1981. %39 = OpLabel
  1982. OpBranch %32
  1983. %40 = OpLabel
  1984. %41 = OpPhi %14 %35 %34 %37 %36
  1985. OpReturn
  1986. OpFunctionEnd
  1987. ; none of the OpPhis' operands dominate the back-edge block and none of
  1988. ; them have basic type
  1989. %43 = OpFunction %2 None %3
  1990. %44 = OpLabel
  1991. %45 = OpVariable %42 Function
  1992. OpBranch %46
  1993. %46 = OpLabel
  1994. OpLoopMerge %54 %53 None
  1995. OpBranch %47
  1996. %47 = OpLabel
  1997. OpSelectionMerge %52 None
  1998. OpBranchConditional %15 %48 %50
  1999. %48 = OpLabel
  2000. %49 = OpCopyObject %42 %45
  2001. OpBranchConditional %15 %52 %54
  2002. %50 = OpLabel
  2003. %51 = OpCopyObject %42 %45
  2004. OpBranchConditional %15 %52 %54
  2005. %52 = OpLabel
  2006. OpBranch %53
  2007. %53 = OpLabel
  2008. OpBranch %46
  2009. %54 = OpLabel
  2010. %55 = OpPhi %42 %49 %48 %51 %50
  2011. OpReturn
  2012. OpFunctionEnd
  2013. )";
  2014. std::string recipient_shader = R"(
  2015. ; OpPhis don't support pointers without this capability
  2016. ; and we need pointers to test some of the functionality
  2017. OpCapability VariablePointers
  2018. OpCapability Shader
  2019. %1 = OpExtInstImport "GLSL.std.450"
  2020. OpMemoryModel Logical GLSL450
  2021. OpEntryPoint Fragment %4 "main"
  2022. OpExecutionMode %4 OriginUpperLeft
  2023. OpSource ESSL 310
  2024. %2 = OpTypeVoid
  2025. %3 = OpTypeFunction %2
  2026. %4 = OpFunction %2 None %3
  2027. %5 = OpLabel
  2028. OpReturn
  2029. OpFunctionEnd
  2030. )";
  2031. const auto env = SPV_ENV_UNIVERSAL_1_3;
  2032. const auto consumer = nullptr;
  2033. spvtools::ValidatorOptions validator_options;
  2034. const auto recipient_context =
  2035. BuildModule(env, consumer, recipient_shader, kFuzzAssembleOption);
  2036. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  2037. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  2038. const auto donor_context =
  2039. BuildModule(env, consumer, donor_shader, kFuzzAssembleOption);
  2040. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  2041. donor_context.get(), validator_options, kConsoleMessageConsumer));
  2042. TransformationContext transformation_context(
  2043. MakeUnique<FactManager>(recipient_context.get()), validator_options);
  2044. FuzzerContext fuzzer_context(MakeUnique<PseudoRandomGenerator>(0), 100,
  2045. false);
  2046. protobufs::TransformationSequence transformation_sequence;
  2047. FuzzerPassDonateModules fuzzer_pass(recipient_context.get(),
  2048. &transformation_context, &fuzzer_context,
  2049. &transformation_sequence, false, {});
  2050. fuzzer_pass.DonateSingleModule(donor_context.get(), true);
  2051. // We just check that the result is valid. Checking to what it should be
  2052. // exactly equal to would be very fragile.
  2053. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  2054. recipient_context.get(), validator_options, kConsoleMessageConsumer));
  2055. }
  2056. } // namespace
  2057. } // namespace fuzz
  2058. } // namespace spvtools