fact_manager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/fact_manager.h"
  15. #include <sstream>
  16. #include "source/fuzz/uniform_buffer_element_descriptor.h"
  17. #include "source/opt/ir_context.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. namespace {
  21. std::string ToString(const protobufs::Fact& fact) {
  22. assert(fact.fact_case() == protobufs::Fact::kConstantUniformFact &&
  23. "Right now this is the only fact.");
  24. std::stringstream stream;
  25. stream << "("
  26. << fact.constant_uniform_fact()
  27. .uniform_buffer_element_descriptor()
  28. .descriptor_set()
  29. << ", "
  30. << fact.constant_uniform_fact()
  31. .uniform_buffer_element_descriptor()
  32. .binding()
  33. << ")[";
  34. bool first = true;
  35. for (auto index : fact.constant_uniform_fact()
  36. .uniform_buffer_element_descriptor()
  37. .index()) {
  38. if (first) {
  39. first = false;
  40. } else {
  41. stream << ", ";
  42. }
  43. stream << index;
  44. }
  45. stream << "] == [";
  46. first = true;
  47. for (auto constant_word : fact.constant_uniform_fact().constant_word()) {
  48. if (first) {
  49. first = false;
  50. } else {
  51. stream << ", ";
  52. }
  53. stream << constant_word;
  54. }
  55. stream << "]";
  56. return stream.str();
  57. }
  58. } // namespace
  59. //=======================
  60. // Constant uniform facts
  61. // The purpose of this struct is to group the fields and data used to represent
  62. // facts about uniform constants.
  63. struct FactManager::ConstantUniformFacts {
  64. // See method in FactManager which delegates to this method.
  65. bool AddFact(const protobufs::FactConstantUniform& fact,
  66. opt::IRContext* context);
  67. // See method in FactManager which delegates to this method.
  68. std::vector<uint32_t> GetConstantsAvailableFromUniformsForType(
  69. opt::IRContext* ir_context, uint32_t type_id) const;
  70. // See method in FactManager which delegates to this method.
  71. const std::vector<protobufs::UniformBufferElementDescriptor>
  72. GetUniformDescriptorsForConstant(opt::IRContext* ir_context,
  73. uint32_t constant_id) const;
  74. // See method in FactManager which delegates to this method.
  75. uint32_t GetConstantFromUniformDescriptor(
  76. opt::IRContext* context,
  77. const protobufs::UniformBufferElementDescriptor& uniform_descriptor)
  78. const;
  79. // See method in FactManager which delegates to this method.
  80. std::vector<uint32_t> GetTypesForWhichUniformValuesAreKnown() const;
  81. // Returns true if and only if the words associated with
  82. // |constant_instruction| exactly match the words for the constant associated
  83. // with |constant_uniform_fact|.
  84. bool DataMatches(
  85. const opt::Instruction& constant_instruction,
  86. const protobufs::FactConstantUniform& constant_uniform_fact) const;
  87. // Yields the constant words associated with |constant_uniform_fact|.
  88. std::vector<uint32_t> GetConstantWords(
  89. const protobufs::FactConstantUniform& constant_uniform_fact) const;
  90. // Yields the id of a constant of type |type_id| whose data matches the
  91. // constant data in |constant_uniform_fact|, or 0 if no such constant is
  92. // declared.
  93. uint32_t GetConstantId(
  94. opt::IRContext* context,
  95. const protobufs::FactConstantUniform& constant_uniform_fact,
  96. uint32_t type_id) const;
  97. // Checks that the width of a floating-point constant is supported, and that
  98. // the constant is finite.
  99. bool FloatingPointValueIsSuitable(const protobufs::FactConstantUniform& fact,
  100. uint32_t width) const;
  101. std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>
  102. facts_and_type_ids;
  103. };
  104. uint32_t FactManager::ConstantUniformFacts::GetConstantId(
  105. opt::IRContext* context,
  106. const protobufs::FactConstantUniform& constant_uniform_fact,
  107. uint32_t type_id) const {
  108. auto type = context->get_type_mgr()->GetType(type_id);
  109. assert(type != nullptr && "Unknown type id.");
  110. auto constant = context->get_constant_mgr()->GetConstant(
  111. type, GetConstantWords(constant_uniform_fact));
  112. return context->get_constant_mgr()->FindDeclaredConstant(constant, type_id);
  113. }
  114. std::vector<uint32_t> FactManager::ConstantUniformFacts::GetConstantWords(
  115. const protobufs::FactConstantUniform& constant_uniform_fact) const {
  116. std::vector<uint32_t> result;
  117. for (auto constant_word : constant_uniform_fact.constant_word()) {
  118. result.push_back(constant_word);
  119. }
  120. return result;
  121. }
  122. bool FactManager::ConstantUniformFacts::DataMatches(
  123. const opt::Instruction& constant_instruction,
  124. const protobufs::FactConstantUniform& constant_uniform_fact) const {
  125. assert(constant_instruction.opcode() == SpvOpConstant);
  126. std::vector<uint32_t> data_in_constant;
  127. for (uint32_t i = 0; i < constant_instruction.NumInOperands(); i++) {
  128. data_in_constant.push_back(constant_instruction.GetSingleWordInOperand(i));
  129. }
  130. return data_in_constant == GetConstantWords(constant_uniform_fact);
  131. }
  132. std::vector<uint32_t>
  133. FactManager::ConstantUniformFacts::GetConstantsAvailableFromUniformsForType(
  134. opt::IRContext* ir_context, uint32_t type_id) const {
  135. std::vector<uint32_t> result;
  136. std::set<uint32_t> already_seen;
  137. for (auto& fact_and_type_id : facts_and_type_ids) {
  138. if (fact_and_type_id.second != type_id) {
  139. continue;
  140. }
  141. if (auto constant_id =
  142. GetConstantId(ir_context, fact_and_type_id.first, type_id)) {
  143. if (already_seen.find(constant_id) == already_seen.end()) {
  144. result.push_back(constant_id);
  145. already_seen.insert(constant_id);
  146. }
  147. }
  148. }
  149. return result;
  150. }
  151. const std::vector<protobufs::UniformBufferElementDescriptor>
  152. FactManager::ConstantUniformFacts::GetUniformDescriptorsForConstant(
  153. opt::IRContext* ir_context, uint32_t constant_id) const {
  154. std::vector<protobufs::UniformBufferElementDescriptor> result;
  155. auto constant_inst = ir_context->get_def_use_mgr()->GetDef(constant_id);
  156. assert(constant_inst->opcode() == SpvOpConstant &&
  157. "The given id must be that of a constant");
  158. auto type_id = constant_inst->type_id();
  159. for (auto& fact_and_type_id : facts_and_type_ids) {
  160. if (fact_and_type_id.second != type_id) {
  161. continue;
  162. }
  163. if (DataMatches(*constant_inst, fact_and_type_id.first)) {
  164. result.emplace_back(
  165. fact_and_type_id.first.uniform_buffer_element_descriptor());
  166. }
  167. }
  168. return result;
  169. }
  170. uint32_t FactManager::ConstantUniformFacts::GetConstantFromUniformDescriptor(
  171. opt::IRContext* context,
  172. const protobufs::UniformBufferElementDescriptor& uniform_descriptor) const {
  173. // Consider each fact.
  174. for (auto& fact_and_type : facts_and_type_ids) {
  175. // Check whether the uniform descriptor associated with the fact matches
  176. // |uniform_descriptor|.
  177. if (UniformBufferElementDescriptorEquals()(
  178. &uniform_descriptor,
  179. &fact_and_type.first.uniform_buffer_element_descriptor())) {
  180. return GetConstantId(context, fact_and_type.first, fact_and_type.second);
  181. }
  182. }
  183. // No fact associated with the given uniform descriptor was found.
  184. return 0;
  185. }
  186. std::vector<uint32_t>
  187. FactManager::ConstantUniformFacts::GetTypesForWhichUniformValuesAreKnown()
  188. const {
  189. std::vector<uint32_t> result;
  190. for (auto& fact_and_type : facts_and_type_ids) {
  191. if (std::find(result.begin(), result.end(), fact_and_type.second) ==
  192. result.end()) {
  193. result.push_back(fact_and_type.second);
  194. }
  195. }
  196. return result;
  197. }
  198. bool FactManager::ConstantUniformFacts::FloatingPointValueIsSuitable(
  199. const protobufs::FactConstantUniform& fact, uint32_t width) const {
  200. const uint32_t kFloatWidth = 32;
  201. const uint32_t kDoubleWidth = 64;
  202. if (width != kFloatWidth && width != kDoubleWidth) {
  203. // Only 32- and 64-bit floating-point types are handled.
  204. return false;
  205. }
  206. std::vector<uint32_t> words = GetConstantWords(fact);
  207. if (width == 32) {
  208. float value;
  209. memcpy(&value, words.data(), sizeof(float));
  210. if (!std::isfinite(value)) {
  211. return false;
  212. }
  213. } else {
  214. double value;
  215. memcpy(&value, words.data(), sizeof(double));
  216. if (!std::isfinite(value)) {
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. bool FactManager::ConstantUniformFacts::AddFact(
  223. const protobufs::FactConstantUniform& fact, opt::IRContext* context) {
  224. // Try to find a unique instruction that declares a variable such that the
  225. // variable is decorated with the descriptor set and binding associated with
  226. // the constant uniform fact.
  227. opt::Instruction* uniform_variable = FindUniformVariable(
  228. fact.uniform_buffer_element_descriptor(), context, true);
  229. if (!uniform_variable) {
  230. return false;
  231. }
  232. assert(SpvOpVariable == uniform_variable->opcode());
  233. assert(SpvStorageClassUniform == uniform_variable->GetSingleWordInOperand(0));
  234. auto should_be_uniform_pointer_type =
  235. context->get_type_mgr()->GetType(uniform_variable->type_id());
  236. if (!should_be_uniform_pointer_type->AsPointer()) {
  237. return false;
  238. }
  239. if (should_be_uniform_pointer_type->AsPointer()->storage_class() !=
  240. SpvStorageClassUniform) {
  241. return false;
  242. }
  243. auto should_be_uniform_pointer_instruction =
  244. context->get_def_use_mgr()->GetDef(uniform_variable->type_id());
  245. auto element_type =
  246. should_be_uniform_pointer_instruction->GetSingleWordInOperand(1);
  247. for (auto index : fact.uniform_buffer_element_descriptor().index()) {
  248. auto should_be_composite_type =
  249. context->get_def_use_mgr()->GetDef(element_type);
  250. if (SpvOpTypeStruct == should_be_composite_type->opcode()) {
  251. if (index >= should_be_composite_type->NumInOperands()) {
  252. return false;
  253. }
  254. element_type = should_be_composite_type->GetSingleWordInOperand(index);
  255. } else if (SpvOpTypeArray == should_be_composite_type->opcode()) {
  256. auto array_length_constant =
  257. context->get_constant_mgr()
  258. ->GetConstantFromInst(context->get_def_use_mgr()->GetDef(
  259. should_be_composite_type->GetSingleWordInOperand(1)))
  260. ->AsIntConstant();
  261. if (array_length_constant->words().size() != 1) {
  262. return false;
  263. }
  264. auto array_length = array_length_constant->GetU32();
  265. if (index >= array_length) {
  266. return false;
  267. }
  268. element_type = should_be_composite_type->GetSingleWordInOperand(0);
  269. } else if (SpvOpTypeVector == should_be_composite_type->opcode()) {
  270. auto vector_length = should_be_composite_type->GetSingleWordInOperand(1);
  271. if (index >= vector_length) {
  272. return false;
  273. }
  274. element_type = should_be_composite_type->GetSingleWordInOperand(0);
  275. } else {
  276. return false;
  277. }
  278. }
  279. auto final_element_type = context->get_type_mgr()->GetType(element_type);
  280. if (!(final_element_type->AsFloat() || final_element_type->AsInteger())) {
  281. return false;
  282. }
  283. auto width = final_element_type->AsFloat()
  284. ? final_element_type->AsFloat()->width()
  285. : final_element_type->AsInteger()->width();
  286. if (final_element_type->AsFloat() &&
  287. !FloatingPointValueIsSuitable(fact, width)) {
  288. return false;
  289. }
  290. auto required_words = (width + 32 - 1) / 32;
  291. if (static_cast<uint32_t>(fact.constant_word().size()) != required_words) {
  292. return false;
  293. }
  294. facts_and_type_ids.emplace_back(
  295. std::pair<protobufs::FactConstantUniform, uint32_t>(fact, element_type));
  296. return true;
  297. }
  298. // End of uniform constant facts
  299. //==============================
  300. //==============================
  301. // Id synonym facts
  302. // The purpose of this struct is to group the fields and data used to represent
  303. // facts about id synonyms.
  304. struct FactManager::IdSynonymFacts {
  305. // See method in FactManager which delegates to this method.
  306. void AddFact(const protobufs::FactIdSynonym& fact);
  307. // A record of all the synonyms that are available.
  308. std::map<uint32_t, std::vector<protobufs::DataDescriptor>> synonyms;
  309. // The set of keys to the above map; useful if you just want to know which ids
  310. // have synonyms.
  311. std::set<uint32_t> ids_with_synonyms;
  312. };
  313. void FactManager::IdSynonymFacts::AddFact(
  314. const protobufs::FactIdSynonym& fact) {
  315. if (synonyms.count(fact.id()) == 0) {
  316. assert(ids_with_synonyms.count(fact.id()) == 0);
  317. ids_with_synonyms.insert(fact.id());
  318. synonyms[fact.id()] = std::vector<protobufs::DataDescriptor>();
  319. }
  320. assert(ids_with_synonyms.count(fact.id()) == 1);
  321. synonyms[fact.id()].push_back(fact.data_descriptor());
  322. }
  323. // End of id synonym facts
  324. //==============================
  325. FactManager::FactManager()
  326. : uniform_constant_facts_(MakeUnique<ConstantUniformFacts>()),
  327. id_synonym_facts_(MakeUnique<IdSynonymFacts>()) {}
  328. FactManager::~FactManager() = default;
  329. void FactManager::AddFacts(const MessageConsumer& message_consumer,
  330. const protobufs::FactSequence& initial_facts,
  331. opt::IRContext* context) {
  332. for (auto& fact : initial_facts.fact()) {
  333. if (!AddFact(fact, context)) {
  334. message_consumer(
  335. SPV_MSG_WARNING, nullptr, {},
  336. ("Invalid fact " + ToString(fact) + " ignored.").c_str());
  337. }
  338. }
  339. }
  340. bool FactManager::AddFact(const spvtools::fuzz::protobufs::Fact& fact,
  341. spvtools::opt::IRContext* context) {
  342. switch (fact.fact_case()) {
  343. case protobufs::Fact::kConstantUniformFact:
  344. return uniform_constant_facts_->AddFact(fact.constant_uniform_fact(),
  345. context);
  346. case protobufs::Fact::kIdSynonymFact:
  347. id_synonym_facts_->AddFact(fact.id_synonym_fact());
  348. return true;
  349. default:
  350. assert(false && "Unknown fact type.");
  351. return false;
  352. }
  353. }
  354. std::vector<uint32_t> FactManager::GetConstantsAvailableFromUniformsForType(
  355. opt::IRContext* ir_context, uint32_t type_id) const {
  356. return uniform_constant_facts_->GetConstantsAvailableFromUniformsForType(
  357. ir_context, type_id);
  358. }
  359. const std::vector<protobufs::UniformBufferElementDescriptor>
  360. FactManager::GetUniformDescriptorsForConstant(opt::IRContext* ir_context,
  361. uint32_t constant_id) const {
  362. return uniform_constant_facts_->GetUniformDescriptorsForConstant(ir_context,
  363. constant_id);
  364. }
  365. uint32_t FactManager::GetConstantFromUniformDescriptor(
  366. opt::IRContext* context,
  367. const protobufs::UniformBufferElementDescriptor& uniform_descriptor) const {
  368. return uniform_constant_facts_->GetConstantFromUniformDescriptor(
  369. context, uniform_descriptor);
  370. }
  371. std::vector<uint32_t> FactManager::GetTypesForWhichUniformValuesAreKnown()
  372. const {
  373. return uniform_constant_facts_->GetTypesForWhichUniformValuesAreKnown();
  374. }
  375. const std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>&
  376. FactManager::GetConstantUniformFactsAndTypes() const {
  377. return uniform_constant_facts_->facts_and_type_ids;
  378. }
  379. const std::set<uint32_t>& FactManager::GetIdsForWhichSynonymsAreKnown() const {
  380. return id_synonym_facts_->ids_with_synonyms;
  381. }
  382. const std::vector<protobufs::DataDescriptor>& FactManager::GetSynonymsForId(
  383. uint32_t id) const {
  384. return id_synonym_facts_->synonyms.at(id);
  385. }
  386. } // namespace fuzz
  387. } // namespace spvtools