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/transformation_add_constant_boolean.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/opt/types.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
TransformationAddConstantBoolean(const protobufs::TransformationAddConstantBoolean & message)23 TransformationAddConstantBoolean::TransformationAddConstantBoolean(
24 const protobufs::TransformationAddConstantBoolean& message)
25 : message_(message) {}
26
TransformationAddConstantBoolean(uint32_t fresh_id,bool is_true,bool is_irrelevant)27 TransformationAddConstantBoolean::TransformationAddConstantBoolean(
28 uint32_t fresh_id, bool is_true, bool is_irrelevant) {
29 message_.set_fresh_id(fresh_id);
30 message_.set_is_true(is_true);
31 message_.set_is_irrelevant(is_irrelevant);
32 }
33
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const34 bool TransformationAddConstantBoolean::IsApplicable(
35 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
36 return fuzzerutil::MaybeGetBoolType(ir_context) != 0 &&
37 fuzzerutil::IsFreshId(ir_context, message_.fresh_id());
38 }
39
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const40 void TransformationAddConstantBoolean::Apply(
41 opt::IRContext* ir_context,
42 TransformationContext* transformation_context) const {
43 // Add the boolean constant to the module, ensuring the module's id bound is
44 // high enough.
45 fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
46 ir_context->module()->AddGlobalValue(
47 message_.is_true() ? SpvOpConstantTrue : SpvOpConstantFalse,
48 message_.fresh_id(), fuzzerutil::MaybeGetBoolType(ir_context));
49 // We have added an instruction to the module, so need to be careful about the
50 // validity of existing analyses.
51 ir_context->InvalidateAnalysesExceptFor(
52 opt::IRContext::Analysis::kAnalysisNone);
53
54 if (message_.is_irrelevant()) {
55 transformation_context->GetFactManager()->AddFactIdIsIrrelevant(
56 message_.fresh_id());
57 }
58 }
59
ToMessage() const60 protobufs::Transformation TransformationAddConstantBoolean::ToMessage() const {
61 protobufs::Transformation result;
62 *result.mutable_add_constant_boolean() = message_;
63 return result;
64 }
65
GetFreshIds() const66 std::unordered_set<uint32_t> TransformationAddConstantBoolean::GetFreshIds()
67 const {
68 return {message_.fresh_id()};
69 }
70
71 } // namespace fuzz
72 } // namespace spvtools
73