Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve invalid shader handling #2880

Merged
merged 2 commits into from
Dec 14, 2023

Conversation

5p4k
Copy link
Contributor

@5p4k 5p4k commented Dec 12, 2023

  • Prevent trimSpirvDebugInfo from looping indefinitely on invalid input
    An invalid SPIR-V binary might contain data that does not match any opCode, and yields a wordCount of zero, triggering an infinite loop. trimSpirvDebugInfo now returns Expected<unsigned> and will give an error in such a situation. Caller functions getShaderCode and getCodeSize (as well as their callers BuildShaderModule and getModuleData) have been updated to handle the error.

  • Make BuildComputePipeline fail if the shader does not pass validation
    Validation might fail e.g. because the shader entry point is missing. This change allows the error to bubble up through the call stack until vkCreateComputePipelines, instead of killing the app later on with exit code 1. BuildComputePipeline is converted to early-return style.

I did not add any test for these conditions, but I could do so if it's considered valuable.

@5p4k 5p4k requested a review from a team as a code owner December 12, 2023 18:11
@5p4k 5p4k force-pushed the improve_invalid_shader_handling branch from 8ae925f to c1021ac Compare December 12, 2023 18:27
Copy link
Member

@nhaehnle nhaehnle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. We're generally quite liberal with crashing on invalid SPIR-V input, and many cases just use an assert. But for catching some early issues like that it may be useful.

auto codeOrErr = getShaderCode(shaderInfo, codeBuffer);
if (Error err = codeOrErr.takeError()) {
return errorToResult(std::move(err));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No braces necessary

}
} else {
// Allocator is not specified
return Result::ErrorInvalidPointer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be turned into an early-out.

@amdvlk-admin
Copy link

Test summary for commit c1021ac

CTS tests (Failed: 0/138443)
  • Built with version 1.3.5.2
  • Ubuntu navi3x, Srdcvk
    • Passed: 35211/69228 (50.9%)
    • Failed: 0/69228 (0.0%)
    • Not Supported: 34017/69228 (49.1%)
    • Warnings: 0/69228 (0.0%)
    Ubuntu navi2x, Srdcvk
    • Passed: 35242/69215 (50.9%)
    • Failed: 0/69215 (0.0%)
    • Not Supported: 33973/69215 (49.1%)
    • Warnings: 0/69215 (0.0%)

5p4k added 2 commits December 13, 2023 11:36
Validation might fail e.g. because the shader entry point is missing.
This change allows the error to bubble up through the call stack until vkCreateComputePipelines, instead of killing the app later on with exit code 1.

BuildComputePipeline is converted to early-return style.
An invalid SPIR-V binary might contain data that does not match any opCode, and yields a wordCount of zero, triggering an infinite loop.

trimSpirvDebugInfo now returns Expected<unsigned> and will give an error in such a situation.
Caller functions getShaderCode and getCodeSize (as well as their callers BuildShaderModule and getModuleData) have been updated to handle the error.
@5p4k 5p4k force-pushed the improve_invalid_shader_handling branch from c1021ac to c7656ef Compare December 13, 2023 10:37
@amdvlk-admin
Copy link

Test summary for commit c7656ef

CTS tests (Failed: 0/138378)
  • Built with version 1.3.5.2
  • Ubuntu navi3x, Srdcvk
    • Passed: 35162/69163 (50.8%)
    • Failed: 0/69163 (0.0%)
    • Not Supported: 34001/69163 (49.2%)
    • Warnings: 0/69163 (0.0%)
    Ubuntu navi2x, Srdcvk
    • Passed: 35241/69215 (50.9%)
    • Failed: 0/69215 (0.0%)
    • Not Supported: 33974/69215 (49.1%)
    • Warnings: 0/69215 (0.0%)

Copy link
Member

@nhaehnle nhaehnle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM

@5p4k 5p4k merged commit 2fcce3b into GPUOpen-Drivers:dev Dec 14, 2023
10 checks passed
@5p4k 5p4k deleted the improve_invalid_shader_handling branch December 19, 2023 09:26
qiaojbao pushed a commit that referenced this pull request Jan 2, 2024
* Make BuildComputePipeline fail if the shader does not pass validation

Validation might fail e.g. because the shader entry point is missing.
This change allows the error to bubble up through the call stack until vkCreateComputePipelines, instead of killing the app later on with exit code 1.
BuildComputePipeline is converted to early-return style.

* Prevent trimSpirvDebugInfo from looping indefinitely on invalid input

An invalid SPIR-V binary might contain data that does not match any opCode, and yields a wordCount of zero, triggering an infinite loop.
trimSpirvDebugInfo now returns Expected<unsigned> and will give an error in such a situation.
Caller functions getShaderCode and getCodeSize (as well as their callers BuildShaderModule and getModuleData) have been updated to handle the error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants