Skip to content

Commit 01628d6

Browse files
authored
spirv-opt: Fix id overflow in ConvertToSampledImage (#6339)
In ConvertImageVariableToSampledImage, the result of FindPointerToType is not checked before being passed to MoveInstructionNextToType. This can cause a crash if FindPointerToType returns 0. This CL adds a check to ensure type_id is not 0 before calling MoveInstructionNextToType. If type_id is 0, the function will return false.
1 parent 19138be commit 01628d6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

source/opt/convert_to_sampled_image_pass.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ Instruction* ConvertToSampledImagePass::CreateImageExtraction(
246246
InstructionBuilder builder(
247247
context(), sampled_image->NextNode(),
248248
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
249-
return builder.AddUnaryOp(
249+
Instruction* result = builder.AddUnaryOp(
250250
GetImageTypeOfSampledImage(context()->get_type_mgr(), sampled_image),
251251
spv::Op::OpImage, sampled_image->result_id());
252+
return result;
252253
}
253254

254255
uint32_t ConvertToSampledImagePass::GetSampledImageTypeForImage(
@@ -270,6 +271,9 @@ Instruction* ConvertToSampledImagePass::UpdateImageUses(
270271
if (uses_of_load.empty()) return nullptr;
271272

272273
auto* extracted_image = CreateImageExtraction(sampled_image_load);
274+
if (extracted_image == nullptr) {
275+
return nullptr;
276+
}
273277
for (auto* user : uses_of_load) {
274278
user->SetInOperand(0, {extracted_image->result_id()});
275279
context()->get_def_use_mgr()->AnalyzeInstUse(user);
@@ -306,8 +310,12 @@ void ConvertToSampledImagePass::UpdateSampledImageUses(
306310
def_use_mgr->AnalyzeInstUse(image_load);
307311
context()->KillInst(sampled_image_inst);
308312
} else {
309-
if (!image_extraction)
313+
if (!image_extraction) {
310314
image_extraction = CreateImageExtraction(image_load);
315+
if (image_extraction == nullptr) {
316+
return;
317+
}
318+
}
311319
sampled_image_inst->SetInOperand(0, {image_extraction->result_id()});
312320
def_use_mgr->AnalyzeInstUse(sampled_image_inst);
313321
}
@@ -333,6 +341,9 @@ bool ConvertToSampledImagePass::ConvertImageVariableToSampledImage(
333341
// reference.
334342
uint32_t type_id = context()->get_type_mgr()->FindPointerToType(
335343
sampled_image_type_id, storage_class);
344+
if (type_id == 0) {
345+
return false;
346+
}
336347
MoveInstructionNextToType(image_variable, type_id);
337348
return true;
338349
}

0 commit comments

Comments
 (0)