| 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 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_adjust_memory_operands_masks.h" |
| 16 |
|
| 17 |
#include "source/fuzz/instruction_descriptor.h" |
| 18 |
#include "source/fuzz/transformation_set_memory_operands_mask.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace fuzz { |
| 22 |
|
| 23 |
FuzzerPassAdjustMemoryOperandsMasks::FuzzerPassAdjustMemoryOperandsMasks( |
| 24 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 25 |
FuzzerContext* fuzzer_context, |
| 26 |
protobufs::TransformationSequence* transformations) |
| 27 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 28 |
transformations) {} |
| 29 |
|
| 30 |
FuzzerPassAdjustMemoryOperandsMasks::~FuzzerPassAdjustMemoryOperandsMasks() = |
| 31 |
default; |
| 32 |
|
| 33 |
void FuzzerPassAdjustMemoryOperandsMasks::Apply() { |
| 34 |
// Consider every block in every function. |
| 35 |
for (auto& function : *GetIRContext()->module()) { |
| 36 |
for (auto& block : function) { |
| 37 |
// Consider every instruction in this block, using an explicit iterator so |
| 38 |
// that when we find an instruction of interest we can search backwards to |
| 39 |
// create an id descriptor for it. |
| 40 |
for (auto inst_it = block.cbegin(); inst_it != block.cend(); ++inst_it) { |
| 41 |
if (!TransformationSetMemoryOperandsMask::IsMemoryAccess(*inst_it)) { |
| 42 |
// We are only interested in memory access instructions. |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
std::vector<uint32_t> indices_of_available_masks_to_adjust; |
| 47 |
// All memory instructions have at least one memory operands mask. |
| 48 |
indices_of_available_masks_to_adjust.push_back(0); |
| 49 |
// From SPIR-V 1.4 onwards, OpCopyMemory and OpCopyMemorySized have a |
| 50 |
// second mask. |
| 51 |
switch (inst_it->opcode()) { |
| 52 |
case SpvOpCopyMemory: |
| 53 |
case SpvOpCopyMemorySized: |
| 54 |
if (TransformationSetMemoryOperandsMask:: |
| 55 |
MultipleMemoryOperandMasksAreSupported(GetIRContext())) { |
| 56 |
indices_of_available_masks_to_adjust.push_back(1); |
| 57 |
} |
| 58 |
break; |
| 59 |
default: |
| 60 |
break; |
| 61 |
} |
| 62 |
|
| 63 |
// Consider the available masks |
| 64 |
for (auto mask_index : indices_of_available_masks_to_adjust) { |
| 65 |
// Randomly decide whether to adjust this mask. |
| 66 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 67 |
GetFuzzerContext() |
| 68 |
->GetChanceOfAdjustingMemoryOperandsMask())) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
// Get the existing mask, using None if there was no mask present at |
| 72 |
// all. |
| 73 |
auto existing_mask_in_operand_index = |
| 74 |
TransformationSetMemoryOperandsMask::GetInOperandIndexForMask( |
| 75 |
*inst_it, mask_index); |
| 76 |
auto existing_mask = |
| 77 |
existing_mask_in_operand_index < inst_it->NumInOperands() |
| 78 |
? inst_it->GetSingleWordInOperand( |
| 79 |
existing_mask_in_operand_index) |
| 80 |
: static_cast<uint32_t>(SpvMemoryAccessMaskNone); |
| 81 |
|
| 82 |
// There are two things we can do to a mask: |
| 83 |
// - add Volatile if not already present |
| 84 |
// - toggle Nontemporal |
| 85 |
// The following ensures that we do at least one of these |
| 86 |
bool add_volatile = !(existing_mask & SpvMemoryAccessVolatileMask) && |
| 87 |
GetFuzzerContext()->ChooseEven(); |
| 88 |
bool toggle_nontemporal = |
| 89 |
!add_volatile || GetFuzzerContext()->ChooseEven(); |
| 90 |
|
| 91 |
// These bitwise operations use '|' to add Volatile if desired, and |
| 92 |
// '^' to toggle Nontemporal if desired. |
| 93 |
uint32_t new_mask = |
| 94 |
(existing_mask | (add_volatile ? SpvMemoryAccessVolatileMask |
| 95 |
: SpvMemoryAccessMaskNone)) ^ |
| 96 |
(toggle_nontemporal ? SpvMemoryAccessNontemporalMask |
| 97 |
: SpvMemoryAccessMaskNone); |
| 98 |
|
| 99 |
TransformationSetMemoryOperandsMask transformation( |
| 100 |
MakeInstructionDescriptor(block, inst_it), new_mask, mask_index); |
| 101 |
ApplyTransformation(transformation); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} // namespace fuzz |
| 109 |
} // namespace spvtools |