Validation within function body when doing a FunctionCall. (#1790)

When validating a FunctionCall we can trigger an assert if we are not
currently within a function body. This CL adds verification that we are
within a function before attempting to add a function call.

Issue 1789.
This commit is contained in:
dan sinclair 2018-08-02 16:58:45 -04:00 committed by GitHub
parent 6aa8a59415
commit d38a0a3b44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -266,8 +266,14 @@ spv_result_t ValidateBinaryUsingContextAndValidationState(
vstate->RegisterEntryPoint(entry_point, execution_model,
std::move(desc));
}
if (inst->opcode() == SpvOpFunctionCall)
if (inst->opcode() == SpvOpFunctionCall) {
if (!vstate->in_function_body()) {
return vstate->diag(SPV_ERROR_INVALID_LAYOUT, &instruction)
<< "A FunctionCall must happen within a function body.";
}
vstate->AddFunctionCallTarget(inst->GetOperandAs<uint32_t>(2));
}
if (vstate->in_function_body()) {
inst->set_function(&(vstate->current_function()));

View File

@ -494,6 +494,21 @@ TEST_F(ValidateEntryPoint, FunctionIsTargetOfEntryPointAndFunctionCallBad) {
"instruction and an OpFunctionCall instruction."));
}
// Invalid. Must be within a function to make a function call.
TEST_F(ValidateEntryPoint, FunctionCallOutsideFunctionBody) {
std::string spirv = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpName %variableName "variableName"
%34 = OpFunctionCall %variableName %1
)";
CompileSuccessfully(spirv);
EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("FunctionCall must happen within a function body."));
}
// Valid. Module with a function but no entry point is valid when Linkage
// Capability is used.
TEST_F(ValidateEntryPoint, NoEntryPointWithLinkageCapGood) {