transformation_load_test.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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_load.h"
  15. #include "gtest/gtest.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "test/fuzz/fuzz_test_util.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. namespace {
  22. TEST(TransformationLoadTest, BasicTest) {
  23. std::string shader = R"(
  24. OpCapability Shader
  25. OpCapability VariablePointers
  26. %1 = OpExtInstImport "GLSL.std.450"
  27. OpMemoryModel Logical GLSL450
  28. OpEntryPoint Fragment %4 "main"
  29. OpExecutionMode %4 OriginUpperLeft
  30. OpSource ESSL 310
  31. %2 = OpTypeVoid
  32. %3 = OpTypeFunction %2
  33. %6 = OpTypeInt 32 1
  34. %7 = OpTypeFloat 32
  35. %8 = OpTypeStruct %6 %7
  36. %9 = OpTypePointer Function %8
  37. %10 = OpTypeFunction %6 %9
  38. %14 = OpConstant %6 0
  39. %15 = OpTypePointer Function %6
  40. %51 = OpTypePointer Private %6
  41. %21 = OpConstant %6 2
  42. %23 = OpConstant %6 1
  43. %24 = OpConstant %7 1
  44. %25 = OpTypePointer Function %7
  45. %50 = OpTypePointer Private %7
  46. %34 = OpTypeBool
  47. %35 = OpConstantFalse %34
  48. %60 = OpConstantNull %50
  49. %52 = OpVariable %50 Private
  50. %53 = OpVariable %51 Private
  51. %4 = OpFunction %2 None %3
  52. %5 = OpLabel
  53. %20 = OpVariable %9 Function
  54. %27 = OpVariable %9 Function ; irrelevant
  55. %22 = OpAccessChain %15 %20 %14
  56. %44 = OpCopyObject %9 %20
  57. %26 = OpAccessChain %25 %20 %23
  58. %29 = OpFunctionCall %6 %12 %27
  59. %30 = OpAccessChain %15 %20 %14
  60. %45 = OpCopyObject %15 %30
  61. %33 = OpAccessChain %15 %20 %14
  62. OpSelectionMerge %37 None
  63. OpBranchConditional %35 %36 %37
  64. %36 = OpLabel
  65. %38 = OpAccessChain %15 %20 %14
  66. %40 = OpAccessChain %15 %20 %14
  67. %43 = OpAccessChain %15 %20 %14
  68. OpBranch %37
  69. %37 = OpLabel
  70. OpReturn
  71. OpFunctionEnd
  72. %12 = OpFunction %6 None %10
  73. %11 = OpFunctionParameter %9 ; irrelevant
  74. %13 = OpLabel
  75. %46 = OpCopyObject %9 %11 ; irrelevant
  76. %16 = OpAccessChain %15 %11 %14 ; irrelevant
  77. OpReturnValue %21
  78. OpFunctionEnd
  79. )";
  80. const auto env = SPV_ENV_UNIVERSAL_1_4;
  81. const auto consumer = nullptr;
  82. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  83. spvtools::ValidatorOptions validator_options;
  84. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  85. kConsoleMessageConsumer));
  86. TransformationContext transformation_context(
  87. MakeUnique<FactManager>(context.get()), validator_options);
  88. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  89. 27);
  90. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  91. 11);
  92. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  93. 46);
  94. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  95. 16);
  96. transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
  97. 52);
  98. transformation_context.GetFactManager()->AddFactBlockIsDead(36);
  99. // Variables with pointee types:
  100. // 52 - ptr_to(7)
  101. // 53 - ptr_to(6)
  102. // 20 - ptr_to(8)
  103. // 27 - ptr_to(8) - irrelevant
  104. // Access chains with pointee type:
  105. // 22 - ptr_to(6)
  106. // 26 - ptr_to(6)
  107. // 30 - ptr_to(6)
  108. // 33 - ptr_to(6)
  109. // 38 - ptr_to(6)
  110. // 40 - ptr_to(6)
  111. // 43 - ptr_to(6)
  112. // 16 - ptr_to(6) - irrelevant
  113. // Copied object with pointee type:
  114. // 44 - ptr_to(8)
  115. // 45 - ptr_to(6)
  116. // 46 - ptr_to(8) - irrelevant
  117. // Function parameters with pointee type:
  118. // 11 - ptr_to(8) - irrelevant
  119. // Pointers that cannot be used:
  120. // 60 - null
  121. // Bad: id is not fresh
  122. ASSERT_FALSE(TransformationLoad(
  123. 33, 33, false, 0, 0,
  124. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  125. .IsApplicable(context.get(), transformation_context));
  126. // Bad: attempt to load from 11 from outside its function
  127. ASSERT_FALSE(TransformationLoad(
  128. 100, 11, false, 0, 0,
  129. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  130. .IsApplicable(context.get(), transformation_context));
  131. // Bad: pointer is not available
  132. ASSERT_FALSE(TransformationLoad(
  133. 100, 33, false, 0, 0,
  134. MakeInstructionDescriptor(45, spv::Op::OpCopyObject, 0))
  135. .IsApplicable(context.get(), transformation_context));
  136. // Bad: attempt to insert before OpVariable
  137. ASSERT_FALSE(
  138. TransformationLoad(100, 27, false, 0, 0,
  139. MakeInstructionDescriptor(27, spv::Op::OpVariable, 0))
  140. .IsApplicable(context.get(), transformation_context));
  141. // Bad: pointer id does not exist
  142. ASSERT_FALSE(TransformationLoad(
  143. 100, 1000, false, 0, 0,
  144. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  145. .IsApplicable(context.get(), transformation_context));
  146. // Bad: pointer id exists but does not have a type
  147. ASSERT_FALSE(TransformationLoad(
  148. 100, 5, false, 0, 0,
  149. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  150. .IsApplicable(context.get(), transformation_context));
  151. // Bad: pointer id exists and has a type, but is not a pointer
  152. ASSERT_FALSE(TransformationLoad(
  153. 100, 24, false, 0, 0,
  154. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  155. .IsApplicable(context.get(), transformation_context));
  156. // Bad: attempt to load from null pointer
  157. ASSERT_FALSE(TransformationLoad(
  158. 100, 60, false, 0, 0,
  159. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0))
  160. .IsApplicable(context.get(), transformation_context));
  161. // Bad: %40 is not available at the program point
  162. ASSERT_FALSE(
  163. TransformationLoad(100, 40, false, 0, 0,
  164. MakeInstructionDescriptor(37, spv::Op::OpReturn, 0))
  165. .IsApplicable(context.get(), transformation_context));
  166. // Bad: The described instruction does not exist
  167. ASSERT_FALSE(
  168. TransformationLoad(100, 33, false, 0, 0,
  169. MakeInstructionDescriptor(1000, spv::Op::OpReturn, 0))
  170. .IsApplicable(context.get(), transformation_context));
  171. {
  172. TransformationLoad transformation(
  173. 100, 33, false, 0, 0,
  174. MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0));
  175. ASSERT_TRUE(
  176. transformation.IsApplicable(context.get(), transformation_context));
  177. ApplyAndCheckFreshIds(transformation, context.get(),
  178. &transformation_context);
  179. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  180. context.get(), validator_options, kConsoleMessageConsumer));
  181. }
  182. {
  183. TransformationLoad transformation(
  184. 101, 46, false, 0, 0,
  185. MakeInstructionDescriptor(16, spv::Op::OpReturnValue, 0));
  186. ASSERT_TRUE(
  187. transformation.IsApplicable(context.get(), transformation_context));
  188. ApplyAndCheckFreshIds(transformation, context.get(),
  189. &transformation_context);
  190. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  191. context.get(), validator_options, kConsoleMessageConsumer));
  192. }
  193. {
  194. TransformationLoad transformation(
  195. 102, 16, false, 0, 0,
  196. MakeInstructionDescriptor(16, spv::Op::OpReturnValue, 0));
  197. ASSERT_TRUE(
  198. transformation.IsApplicable(context.get(), transformation_context));
  199. ApplyAndCheckFreshIds(transformation, context.get(),
  200. &transformation_context);
  201. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  202. context.get(), validator_options, kConsoleMessageConsumer));
  203. }
  204. {
  205. TransformationLoad transformation(
  206. 103, 40, false, 0, 0,
  207. MakeInstructionDescriptor(43, spv::Op::OpAccessChain, 0));
  208. ASSERT_TRUE(
  209. transformation.IsApplicable(context.get(), transformation_context));
  210. ApplyAndCheckFreshIds(transformation, context.get(),
  211. &transformation_context);
  212. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  213. context.get(), validator_options, kConsoleMessageConsumer));
  214. }
  215. std::string after_transformation = R"(
  216. OpCapability Shader
  217. OpCapability VariablePointers
  218. %1 = OpExtInstImport "GLSL.std.450"
  219. OpMemoryModel Logical GLSL450
  220. OpEntryPoint Fragment %4 "main"
  221. OpExecutionMode %4 OriginUpperLeft
  222. OpSource ESSL 310
  223. %2 = OpTypeVoid
  224. %3 = OpTypeFunction %2
  225. %6 = OpTypeInt 32 1
  226. %7 = OpTypeFloat 32
  227. %8 = OpTypeStruct %6 %7
  228. %9 = OpTypePointer Function %8
  229. %10 = OpTypeFunction %6 %9
  230. %14 = OpConstant %6 0
  231. %15 = OpTypePointer Function %6
  232. %51 = OpTypePointer Private %6
  233. %21 = OpConstant %6 2
  234. %23 = OpConstant %6 1
  235. %24 = OpConstant %7 1
  236. %25 = OpTypePointer Function %7
  237. %50 = OpTypePointer Private %7
  238. %34 = OpTypeBool
  239. %35 = OpConstantFalse %34
  240. %60 = OpConstantNull %50
  241. %52 = OpVariable %50 Private
  242. %53 = OpVariable %51 Private
  243. %4 = OpFunction %2 None %3
  244. %5 = OpLabel
  245. %20 = OpVariable %9 Function
  246. %27 = OpVariable %9 Function ; irrelevant
  247. %22 = OpAccessChain %15 %20 %14
  248. %44 = OpCopyObject %9 %20
  249. %26 = OpAccessChain %25 %20 %23
  250. %29 = OpFunctionCall %6 %12 %27
  251. %30 = OpAccessChain %15 %20 %14
  252. %45 = OpCopyObject %15 %30
  253. %33 = OpAccessChain %15 %20 %14
  254. OpSelectionMerge %37 None
  255. OpBranchConditional %35 %36 %37
  256. %36 = OpLabel
  257. %100 = OpLoad %6 %33
  258. %38 = OpAccessChain %15 %20 %14
  259. %40 = OpAccessChain %15 %20 %14
  260. %103 = OpLoad %6 %40
  261. %43 = OpAccessChain %15 %20 %14
  262. OpBranch %37
  263. %37 = OpLabel
  264. OpReturn
  265. OpFunctionEnd
  266. %12 = OpFunction %6 None %10
  267. %11 = OpFunctionParameter %9 ; irrelevant
  268. %13 = OpLabel
  269. %46 = OpCopyObject %9 %11 ; irrelevant
  270. %16 = OpAccessChain %15 %11 %14 ; irrelevant
  271. %101 = OpLoad %8 %46
  272. %102 = OpLoad %6 %16
  273. OpReturnValue %21
  274. OpFunctionEnd
  275. )";
  276. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  277. }
  278. TEST(TransformationLoadTest, AtomicLoadTestCase) {
  279. const std::string shader = R"(
  280. OpCapability Shader
  281. OpCapability Int8
  282. %1 = OpExtInstImport "GLSL.std.450"
  283. OpMemoryModel Logical GLSL450
  284. OpEntryPoint Fragment %4 "main"
  285. OpExecutionMode %4 OriginUpperLeft
  286. OpSource ESSL 320
  287. %2 = OpTypeVoid
  288. %3 = OpTypeFunction %2
  289. %6 = OpTypeInt 32 1
  290. %7 = OpTypeInt 8 1
  291. %9 = OpTypeInt 32 0
  292. %26 = OpTypeFloat 32
  293. %8 = OpTypeStruct %6
  294. %10 = OpTypePointer StorageBuffer %8
  295. %11 = OpVariable %10 StorageBuffer
  296. %19 = OpConstant %26 0
  297. %18 = OpConstant %9 1
  298. %12 = OpConstant %6 0
  299. %13 = OpTypePointer StorageBuffer %6
  300. %15 = OpConstant %6 4
  301. %16 = OpConstant %6 7
  302. %17 = OpConstant %7 4
  303. %20 = OpConstant %9 64
  304. %4 = OpFunction %2 None %3
  305. %5 = OpLabel
  306. %14 = OpAccessChain %13 %11 %12
  307. %24 = OpAccessChain %13 %11 %12
  308. OpReturn
  309. OpFunctionEnd
  310. )";
  311. const auto env = SPV_ENV_UNIVERSAL_1_3;
  312. const auto consumer = nullptr;
  313. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  314. spvtools::ValidatorOptions validator_options;
  315. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  316. kConsoleMessageConsumer));
  317. TransformationContext transformation_context(
  318. MakeUnique<FactManager>(context.get()), validator_options);
  319. // Bad: id is not fresh.
  320. ASSERT_FALSE(TransformationLoad(
  321. 14, 14, true, 15, 20,
  322. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  323. .IsApplicable(context.get(), transformation_context));
  324. // Bad: id 100 of memory scope instruction does not exist.
  325. ASSERT_FALSE(TransformationLoad(
  326. 21, 14, true, 100, 20,
  327. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  328. .IsApplicable(context.get(), transformation_context));
  329. // Bad: id 100 of memory semantics instruction does not exist.
  330. ASSERT_FALSE(TransformationLoad(
  331. 21, 14, true, 15, 100,
  332. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  333. .IsApplicable(context.get(), transformation_context));
  334. // Bad: memory scope should be |OpConstant| opcode.
  335. ASSERT_FALSE(TransformationLoad(
  336. 21, 14, true, 5, 20,
  337. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  338. .IsApplicable(context.get(), transformation_context));
  339. // Bad: memory semantics should be |OpConstant| opcode.
  340. ASSERT_FALSE(TransformationLoad(
  341. 21, 14, true, 15, 5,
  342. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  343. .IsApplicable(context.get(), transformation_context));
  344. // Bad: The memory scope instruction must have an Integer operand.
  345. ASSERT_FALSE(TransformationLoad(
  346. 21, 14, true, 15, 19,
  347. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  348. .IsApplicable(context.get(), transformation_context));
  349. // Bad: The memory memory semantics instruction must have an Integer operand.
  350. ASSERT_FALSE(TransformationLoad(
  351. 21, 14, true, 19, 20,
  352. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  353. .IsApplicable(context.get(), transformation_context));
  354. // Bad: Integer size of the memory scope must be equal to 32 bits.
  355. ASSERT_FALSE(TransformationLoad(
  356. 21, 14, true, 17, 20,
  357. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  358. .IsApplicable(context.get(), transformation_context));
  359. // Bad: Integer size of memory semantics must be equal to 32 bits.
  360. ASSERT_FALSE(TransformationLoad(
  361. 21, 14, true, 15, 17,
  362. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  363. .IsApplicable(context.get(), transformation_context));
  364. // Bad: memory scope value must be 4 (spv::Scope::Invocation).
  365. ASSERT_FALSE(TransformationLoad(
  366. 21, 14, true, 16, 20,
  367. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  368. .IsApplicable(context.get(), transformation_context));
  369. // Bad: memory semantics value must be either:
  370. // 64 (SpvMemorySemanticsUniformMemoryMask)
  371. // 256 (SpvMemorySemanticsWorkgroupMemoryMask)
  372. ASSERT_FALSE(TransformationLoad(
  373. 21, 14, true, 15, 16,
  374. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0))
  375. .IsApplicable(context.get(), transformation_context));
  376. // Bad: The described instruction does not exist
  377. ASSERT_FALSE(TransformationLoad(
  378. 21, 14, false, 15, 20,
  379. MakeInstructionDescriptor(150, spv::Op::OpAccessChain, 0))
  380. .IsApplicable(context.get(), transformation_context));
  381. // Successful transformations.
  382. {
  383. TransformationLoad transformation(
  384. 21, 14, true, 15, 20,
  385. MakeInstructionDescriptor(24, spv::Op::OpAccessChain, 0));
  386. ASSERT_TRUE(
  387. transformation.IsApplicable(context.get(), transformation_context));
  388. ApplyAndCheckFreshIds(transformation, context.get(),
  389. &transformation_context);
  390. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  391. context.get(), validator_options, kConsoleMessageConsumer));
  392. }
  393. const std::string after_transformation = R"(
  394. OpCapability Shader
  395. OpCapability Int8
  396. %1 = OpExtInstImport "GLSL.std.450"
  397. OpMemoryModel Logical GLSL450
  398. OpEntryPoint Fragment %4 "main"
  399. OpExecutionMode %4 OriginUpperLeft
  400. OpSource ESSL 320
  401. %2 = OpTypeVoid
  402. %3 = OpTypeFunction %2
  403. %6 = OpTypeInt 32 1
  404. %7 = OpTypeInt 8 1
  405. %9 = OpTypeInt 32 0
  406. %26 = OpTypeFloat 32
  407. %8 = OpTypeStruct %6
  408. %10 = OpTypePointer StorageBuffer %8
  409. %11 = OpVariable %10 StorageBuffer
  410. %19 = OpConstant %26 0
  411. %18 = OpConstant %9 1
  412. %12 = OpConstant %6 0
  413. %13 = OpTypePointer StorageBuffer %6
  414. %15 = OpConstant %6 4
  415. %16 = OpConstant %6 7
  416. %17 = OpConstant %7 4
  417. %20 = OpConstant %9 64
  418. %4 = OpFunction %2 None %3
  419. %5 = OpLabel
  420. %14 = OpAccessChain %13 %11 %12
  421. %21 = OpAtomicLoad %6 %14 %15 %20
  422. %24 = OpAccessChain %13 %11 %12
  423. OpReturn
  424. OpFunctionEnd
  425. )";
  426. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  427. }
  428. TEST(TransformationLoadTest, AtomicLoadTestCaseForWorkgroupMemory) {
  429. std::string shader = R"(
  430. OpCapability Shader
  431. OpCapability Int8
  432. %1 = OpExtInstImport "GLSL.std.450"
  433. OpMemoryModel Logical GLSL450
  434. OpEntryPoint Fragment %4 "main"
  435. OpExecutionMode %4 OriginUpperLeft
  436. OpSource ESSL 310
  437. %2 = OpTypeVoid
  438. %3 = OpTypeFunction %2
  439. %6 = OpTypeInt 32 1
  440. %26 = OpTypeFloat 32
  441. %27 = OpTypeInt 8 1
  442. %7 = OpTypeInt 32 0 ; 0 means unsigned
  443. %8 = OpConstant %7 0
  444. %17 = OpConstant %27 4
  445. %19 = OpConstant %26 0
  446. %9 = OpTypePointer Function %6
  447. %13 = OpTypeStruct %6
  448. %12 = OpTypePointer Workgroup %13
  449. %11 = OpVariable %12 Workgroup
  450. %14 = OpConstant %6 0
  451. %15 = OpTypePointer Function %6
  452. %51 = OpTypePointer Private %6
  453. %21 = OpConstant %6 4
  454. %23 = OpConstant %6 256
  455. %25 = OpTypePointer Function %7
  456. %50 = OpTypePointer Workgroup %6
  457. %34 = OpTypeBool
  458. %35 = OpConstantFalse %34
  459. %53 = OpVariable %51 Private
  460. %4 = OpFunction %2 None %3
  461. %5 = OpLabel
  462. OpSelectionMerge %37 None
  463. OpBranchConditional %35 %36 %37
  464. %36 = OpLabel
  465. %38 = OpAccessChain %50 %11 %14
  466. %40 = OpAccessChain %50 %11 %14
  467. OpBranch %37
  468. %37 = OpLabel
  469. OpReturn
  470. OpFunctionEnd
  471. )";
  472. const auto env = SPV_ENV_UNIVERSAL_1_3;
  473. const auto consumer = nullptr;
  474. const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
  475. spvtools::ValidatorOptions validator_options;
  476. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
  477. kConsoleMessageConsumer));
  478. TransformationContext transformation_context(
  479. MakeUnique<FactManager>(context.get()), validator_options);
  480. // Bad: Can't insert OpAccessChain before the id 23 of memory scope.
  481. ASSERT_FALSE(TransformationLoad(
  482. 60, 38, true, 21, 23,
  483. MakeInstructionDescriptor(23, spv::Op::OpAccessChain, 0))
  484. .IsApplicable(context.get(), transformation_context));
  485. // Bad: Can't insert OpAccessChain before the id 23 of memory semantics.
  486. ASSERT_FALSE(TransformationLoad(
  487. 60, 38, true, 21, 23,
  488. MakeInstructionDescriptor(21, spv::Op::OpAccessChain, 0))
  489. .IsApplicable(context.get(), transformation_context));
  490. // Successful transformations.
  491. {
  492. TransformationLoad transformation(
  493. 60, 38, true, 21, 23,
  494. MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0));
  495. ASSERT_TRUE(
  496. transformation.IsApplicable(context.get(), transformation_context));
  497. ApplyAndCheckFreshIds(transformation, context.get(),
  498. &transformation_context);
  499. ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
  500. context.get(), validator_options, kConsoleMessageConsumer));
  501. }
  502. std::string after_transformation = R"(
  503. OpCapability Shader
  504. OpCapability Int8
  505. %1 = OpExtInstImport "GLSL.std.450"
  506. OpMemoryModel Logical GLSL450
  507. OpEntryPoint Fragment %4 "main"
  508. OpExecutionMode %4 OriginUpperLeft
  509. OpSource ESSL 310
  510. %2 = OpTypeVoid
  511. %3 = OpTypeFunction %2
  512. %6 = OpTypeInt 32 1
  513. %26 = OpTypeFloat 32
  514. %27 = OpTypeInt 8 1
  515. %7 = OpTypeInt 32 0 ; 0 means unsigned
  516. %8 = OpConstant %7 0
  517. %17 = OpConstant %27 4
  518. %19 = OpConstant %26 0
  519. %9 = OpTypePointer Function %6
  520. %13 = OpTypeStruct %6
  521. %12 = OpTypePointer Workgroup %13
  522. %11 = OpVariable %12 Workgroup
  523. %14 = OpConstant %6 0
  524. %15 = OpTypePointer Function %6
  525. %51 = OpTypePointer Private %6
  526. %21 = OpConstant %6 4
  527. %23 = OpConstant %6 256
  528. %25 = OpTypePointer Function %7
  529. %50 = OpTypePointer Workgroup %6
  530. %34 = OpTypeBool
  531. %35 = OpConstantFalse %34
  532. %53 = OpVariable %51 Private
  533. %4 = OpFunction %2 None %3
  534. %5 = OpLabel
  535. OpSelectionMerge %37 None
  536. OpBranchConditional %35 %36 %37
  537. %36 = OpLabel
  538. %38 = OpAccessChain %50 %11 %14
  539. %60 = OpAtomicLoad %6 %38 %21 %23
  540. %40 = OpAccessChain %50 %11 %14
  541. OpBranch %37
  542. %37 = OpLabel
  543. OpReturn
  544. OpFunctionEnd
  545. )";
  546. ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
  547. }
  548. } // namespace
  549. } // namespace fuzz
  550. } // namespace spvtools