spirv-fuzz: Fix problem with instruction context (#4394)

Adds an additional validity check to ensure that every instruction's
context pointer matches the enclosing IR context. Avoids a redundant
copy constructor call in TransformationDuplicateRegionWithSelection
that was leading to a bad IR context for some instructions.

Related: #4387, #4388.
Fixes #4393.
This commit is contained in:
Alastair Donaldson 2021-07-22 23:44:29 +01:00 committed by GitHub
parent 94bcae1344
commit 183fb9fe4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -502,8 +502,12 @@ bool IsValidAndWellFormed(const opt::IRContext* ir_context,
// this is a useful aid to debugging.
std::unordered_map<uint32_t, opt::Instruction*> unique_ids;
bool found_duplicate = false;
ir_context->module()->ForEachInst([&consumer, &found_duplicate,
ir_context->module()->ForEachInst([&consumer, &found_duplicate, ir_context,
&unique_ids](opt::Instruction* inst) {
(void)ir_context; // Only used in an assertion; keep release-mode compilers
// happy.
assert(inst->context() == ir_context &&
"Instruction has wrong IR context.");
if (unique_ids.count(inst->unique_id()) != 0) {
consumer(SPV_MSG_INFO, nullptr, {},
"Two instructions have the same unique id (set a breakpoint to "

View File

@ -309,9 +309,9 @@ void TransformationDuplicateRegionWithSelection::Apply(
// Construct the merge block.
std::unique_ptr<opt::BasicBlock> merge_block =
MakeUnique<opt::BasicBlock>(MakeUnique<opt::Instruction>(opt::Instruction(
MakeUnique<opt::BasicBlock>(MakeUnique<opt::Instruction>(
ir_context, SpvOpLabel, 0, message_.merge_label_fresh_id(),
opt::Instruction::OperandList())));
opt::Instruction::OperandList()));
// Get the maps from the protobuf.
std::map<uint32_t, uint32_t> original_label_to_duplicate_label =