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_function_controls.h"
16
17 #include "source/fuzz/transformation_set_function_control.h"
18
19 namespace spvtools {
20 namespace fuzz {
21
FuzzerPassAdjustFunctionControls(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)22 FuzzerPassAdjustFunctionControls::FuzzerPassAdjustFunctionControls(
23 opt::IRContext* ir_context, TransformationContext* transformation_context,
24 FuzzerContext* fuzzer_context,
25 protobufs::TransformationSequence* transformations)
26 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
27 transformations) {}
28
29 FuzzerPassAdjustFunctionControls::~FuzzerPassAdjustFunctionControls() = default;
30
Apply()31 void FuzzerPassAdjustFunctionControls::Apply() {
32 // Consider every function in the module.
33 for (auto& function : *GetIRContext()->module()) {
34 // Randomly decide whether to adjust this function's controls.
35 if (GetFuzzerContext()->ChoosePercentage(
36 GetFuzzerContext()->GetChanceOfAdjustingFunctionControl())) {
37 // Grab the function control mask for the function in its present form.
38 uint32_t existing_function_control_mask =
39 function.DefInst().GetSingleWordInOperand(0);
40
41 // For the new mask, we first randomly select one of three basic masks:
42 // None, Inline or DontInline. These are always valid (and are mutually
43 // exclusive).
44 std::vector<uint32_t> basic_function_control_masks = {
45 SpvFunctionControlMaskNone, SpvFunctionControlInlineMask,
46 SpvFunctionControlDontInlineMask};
47 uint32_t new_function_control_mask =
48 basic_function_control_masks[GetFuzzerContext()->RandomIndex(
49 basic_function_control_masks)];
50
51 // We now consider the Pure and Const mask bits. If these are already
52 // set on the function then it's OK to keep them, but also interesting
53 // to consider dropping them, so we decide randomly in each case.
54 for (auto mask_bit :
55 {SpvFunctionControlPureMask, SpvFunctionControlConstMask}) {
56 if ((existing_function_control_mask & mask_bit) &&
57 GetFuzzerContext()->ChooseEven()) {
58 new_function_control_mask |= mask_bit;
59 }
60 }
61
62 // Create and add a transformation.
63 TransformationSetFunctionControl transformation(
64 function.DefInst().result_id(), new_function_control_mask);
65 ApplyTransformation(transformation);
66 }
67 }
68 }
69
70 } // namespace fuzz
71 } // namespace spvtools
72