aboutsummaryrefslogtreecommitdiff
path: root/source/fuzz/transformation_add_local_variable.cpp
blob: 9ee210cbecf294f02b5eefd65ccf231d509568b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "source/fuzz/transformation_add_local_variable.h"

#include "source/fuzz/fuzzer_util.h"

namespace spvtools {
namespace fuzz {

TransformationAddLocalVariable::TransformationAddLocalVariable(
    spvtools::fuzz::protobufs::TransformationAddLocalVariable message)
    : message_(std::move(message)) {}

TransformationAddLocalVariable::TransformationAddLocalVariable(
    uint32_t fresh_id, uint32_t type_id, uint32_t function_id,
    uint32_t initializer_id, bool value_is_irrelevant) {
  message_.set_fresh_id(fresh_id);
  message_.set_type_id(type_id);
  message_.set_function_id(function_id);
  message_.set_initializer_id(initializer_id);
  message_.set_value_is_irrelevant(value_is_irrelevant);
}

bool TransformationAddLocalVariable::IsApplicable(
    opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  // The provided id must be fresh.
  if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
    return false;
  }
  // The pointer type id must indeed correspond to a pointer, and it must have
  // function storage class.
  auto type_instruction =
      ir_context->get_def_use_mgr()->GetDef(message_.type_id());
  if (!type_instruction ||
      type_instruction->opcode() != spv::Op::OpTypePointer ||
      spv::StorageClass(type_instruction->GetSingleWordInOperand(0)) !=
          spv::StorageClass::Function) {
    return false;
  }
  // The initializer must...
  auto initializer_instruction =
      ir_context->get_def_use_mgr()->GetDef(message_.initializer_id());
  // ... exist, ...
  if (!initializer_instruction) {
    return false;
  }
  // ... be a constant, ...
  if (!spvOpcodeIsConstant(initializer_instruction->opcode())) {
    return false;
  }
  // ... and have the same type as the pointee type.
  if (initializer_instruction->type_id() !=
      type_instruction->GetSingleWordInOperand(1)) {
    return false;
  }
  // The function to which the local variable is to be added must exist.
  return fuzzerutil::FindFunction(ir_context, message_.function_id());
}

void TransformationAddLocalVariable::Apply(
    opt::IRContext* ir_context,
    TransformationContext* transformation_context) const {
  opt::Instruction* new_instruction = fuzzerutil::AddLocalVariable(
      ir_context, message_.fresh_id(), message_.type_id(),
      message_.function_id(), message_.initializer_id());

  // Inform the def-use manager about the new instruction.
  ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
  ir_context->set_instr_block(
      new_instruction,
      fuzzerutil::FindFunction(ir_context, message_.function_id())
          ->entry()
          .get());

  if (message_.value_is_irrelevant()) {
    transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
        message_.fresh_id());
  }
}

protobufs::Transformation TransformationAddLocalVariable::ToMessage() const {
  protobufs::Transformation result;
  *result.mutable_add_local_variable() = message_;
  return result;
}

std::unordered_set<uint32_t> TransformationAddLocalVariable::GetFreshIds()
    const {
  return {message_.fresh_id()};
}

}  // namespace fuzz
}  // namespace spvtools