• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "source/fuzz/transformation_add_constant_null.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 
19 namespace spvtools {
20 namespace fuzz {
21 
TransformationAddConstantNull(spvtools::fuzz::protobufs::TransformationAddConstantNull message)22 TransformationAddConstantNull::TransformationAddConstantNull(
23     spvtools::fuzz::protobufs::TransformationAddConstantNull message)
24     : message_(std::move(message)) {}
25 
TransformationAddConstantNull(uint32_t fresh_id,uint32_t type_id)26 TransformationAddConstantNull::TransformationAddConstantNull(uint32_t fresh_id,
27                                                              uint32_t type_id) {
28   message_.set_fresh_id(fresh_id);
29   message_.set_type_id(type_id);
30 }
31 
IsApplicable(opt::IRContext * context,const TransformationContext &) const32 bool TransformationAddConstantNull::IsApplicable(
33     opt::IRContext* context, const TransformationContext& /*unused*/) const {
34   // A fresh id is required.
35   if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
36     return false;
37   }
38   auto type = context->get_def_use_mgr()->GetDef(message_.type_id());
39   // The type must exist.
40   if (!type) {
41     return false;
42   }
43   // The type must be one of the types for which null constants are allowed,
44   // according to the SPIR-V spec.
45   return fuzzerutil::IsNullConstantSupported(context, *type);
46 }
47 
Apply(opt::IRContext * ir_context,TransformationContext *) const48 void TransformationAddConstantNull::Apply(
49     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
50   auto new_instruction = MakeUnique<opt::Instruction>(
51       ir_context, SpvOpConstantNull, message_.type_id(), message_.fresh_id(),
52       opt::Instruction::OperandList());
53   auto new_instruction_ptr = new_instruction.get();
54   ir_context->module()->AddGlobalValue(std::move(new_instruction));
55   fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
56 
57   // Inform the def-use manager about the new instruction. Invalidate the
58   // constant manager as we have added a new constant.
59   ir_context->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr);
60   ir_context->InvalidateAnalyses(opt::IRContext::kAnalysisConstants);
61 }
62 
ToMessage() const63 protobufs::Transformation TransformationAddConstantNull::ToMessage() const {
64   protobufs::Transformation result;
65   *result.mutable_add_constant_null() = message_;
66   return result;
67 }
68 
GetFreshIds() const69 std::unordered_set<uint32_t> TransformationAddConstantNull::GetFreshIds()
70     const {
71   return {message_.fresh_id()};
72 }
73 
74 }  // namespace fuzz
75 }  // namespace spvtools
76