fuzzer_pass_add_global_variables.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2020 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_add_global_variables.h"
  15. #include "source/fuzz/transformation_add_global_variable.h"
  16. #include "source/fuzz/transformation_add_type_pointer.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassAddGlobalVariables::FuzzerPassAddGlobalVariables(
  20. opt::IRContext* ir_context, TransformationContext* transformation_context,
  21. FuzzerContext* fuzzer_context,
  22. protobufs::TransformationSequence* transformations,
  23. bool ignore_inapplicable_transformations)
  24. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  25. transformations, ignore_inapplicable_transformations) {}
  26. void FuzzerPassAddGlobalVariables::Apply() {
  27. spv::StorageClass variable_storage_class = spv::StorageClass::Private;
  28. for (auto& entry_point : GetIRContext()->module()->entry_points()) {
  29. // If the execution model of some entry point is GLCompute,
  30. // then the variable storage class may be Workgroup.
  31. if (spv::ExecutionModel(entry_point.GetSingleWordInOperand(0)) ==
  32. spv::ExecutionModel::GLCompute) {
  33. variable_storage_class =
  34. GetFuzzerContext()->ChoosePercentage(
  35. GetFuzzerContext()->GetChanceOfChoosingWorkgroupStorageClass())
  36. ? spv::StorageClass::Workgroup
  37. : spv::StorageClass::Private;
  38. break;
  39. }
  40. }
  41. auto basic_type_ids_and_pointers =
  42. GetAvailableBasicTypesAndPointers(variable_storage_class);
  43. // These are the basic types that are available to this fuzzer pass.
  44. auto& basic_types = basic_type_ids_and_pointers.first;
  45. if (basic_types.empty()) {
  46. // There are no basic types, so there is nothing this fuzzer pass can do.
  47. return;
  48. }
  49. // These are the pointers to those basic types that are *initially* available
  50. // to the fuzzer pass. The fuzzer pass might add pointer types in cases where
  51. // none are available for a given basic type.
  52. auto& basic_type_to_pointers = basic_type_ids_and_pointers.second;
  53. // Probabilistically keep adding global variables.
  54. while (GetFuzzerContext()->ChoosePercentage(
  55. GetFuzzerContext()->GetChanceOfAddingGlobalVariable())) {
  56. // Choose a random basic type; the new variable's type will be a pointer to
  57. // this basic type.
  58. uint32_t basic_type =
  59. basic_types[GetFuzzerContext()->RandomIndex(basic_types)];
  60. uint32_t pointer_type_id;
  61. std::vector<uint32_t>& available_pointers_to_basic_type =
  62. basic_type_to_pointers.at(basic_type);
  63. // Determine whether there is at least one pointer to this basic type.
  64. if (available_pointers_to_basic_type.empty()) {
  65. // There is not. Make one, to use here, and add it to the available
  66. // pointers for the basic type so that future variables can potentially
  67. // use it.
  68. pointer_type_id = GetFuzzerContext()->GetFreshId();
  69. available_pointers_to_basic_type.push_back(pointer_type_id);
  70. ApplyTransformation(TransformationAddTypePointer(
  71. pointer_type_id, variable_storage_class, basic_type));
  72. } else {
  73. // There is - grab one.
  74. pointer_type_id =
  75. available_pointers_to_basic_type[GetFuzzerContext()->RandomIndex(
  76. available_pointers_to_basic_type)];
  77. }
  78. ApplyTransformation(TransformationAddGlobalVariable(
  79. GetFuzzerContext()->GetFreshId(), pointer_type_id,
  80. variable_storage_class,
  81. variable_storage_class == spv::StorageClass::Private
  82. ? FindOrCreateZeroConstant(basic_type, false)
  83. : 0,
  84. true));
  85. }
  86. }
  87. } // namespace fuzz
  88. } // namespace spvtools