dawn: Add shader module validation for WGSL extension

Tint has already implemented the enable directive for using WGSL
extension in the future, and using a WGSL extension that is not allowed
for the device should result in a shader creation error.
In this patch a WGSL extension allow list is added in DeviceBase, and
a validation is added in shader module base initialization to make sure
all extensions used in the WGSL program are in the allow list. This
patch also rename the `ValidateShaderModuleDescriptor` to
`ValidateAndParseShaderModule`, which is more descriptive for what it
actually does.

Bug: tint:1472
Change-Id: I4b039a3e37c25159b4fc6cfa37488aa817004ab2
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/88241
Commit-Queue: Zhaoming Jiang <zhaoming.jiang@intel.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
This commit is contained in:
Zhaoming Jiang
2022-05-06 08:51:12 +00:00
committed by Dawn LUCI CQ
parent 4cb75245e3
commit d6b2501be2
25 changed files with 189 additions and 78 deletions

View File

@@ -199,6 +199,8 @@ DeviceBase::DeviceBase(AdapterBase* adapter, const DeviceDescriptor* descriptor)
mFormatTable = BuildFormatTable(this);
SetDefaultToggles();
SetWGSLExtensionAllowList();
if (descriptor->label != nullptr && strlen(descriptor->label) != 0) {
mLabel = descriptor->label;
}
@@ -914,13 +916,13 @@ ResultOrError<Ref<ShaderModuleBase>> DeviceBase::GetOrCreateShaderModule(
if (!parseResult->HasParsedShader()) {
// We skip the parse on creation if validation isn't enabled which let's us quickly
// lookup in the cache without validating and parsing. We need the parsed module
// now, so call validate. Most of |ValidateShaderModuleDescriptor| is parsing, but
// we can consider splitting it if additional validation is added.
// now.
ASSERT(!IsValidationEnabled());
DAWN_TRY(
ValidateShaderModuleDescriptor(this, descriptor, parseResult, compilationMessages));
ValidateAndParseShaderModule(this, descriptor, parseResult, compilationMessages));
}
DAWN_TRY_ASSIGN(result, CreateShaderModuleImpl(descriptor, parseResult));
DAWN_TRY_ASSIGN(result,
CreateShaderModuleImpl(descriptor, parseResult, compilationMessages));
result->SetIsCachedReference();
result->SetContentHash(blueprintHash);
mCaches->shaderModules.insert(result.Get());
@@ -1119,7 +1121,8 @@ ShaderModuleBase* DeviceBase::APICreateShaderModule(const ShaderModuleDescriptor
result = ShaderModuleBase::MakeError(this);
}
// Move compilation messages into ShaderModuleBase and emit tint errors and warnings
// after all other operations are finished successfully.
// after all other operations are finished, even if any of them is failed and result
// is an error shader module.
result->InjectCompilationMessages(std::move(compilationMessages));
return result.Detach();
@@ -1229,6 +1232,16 @@ bool DeviceBase::IsFeatureEnabled(Feature feature) const {
return mEnabledFeatures.IsEnabled(feature);
}
void DeviceBase::SetWGSLExtensionAllowList() {
// Set the WGSL extensions allow list based on device's enabled features and other
// propority. For example:
// mWGSLExtensionAllowList.insert("InternalExtensionForTesting");
}
WGSLExtensionsSet DeviceBase::GetWGSLExtensionAllowList() const {
return mWGSLExtensionAllowList;
}
bool DeviceBase::IsValidationEnabled() const {
return !IsToggleEnabled(Toggle::SkipValidation);
}
@@ -1589,7 +1602,7 @@ ResultOrError<Ref<ShaderModuleBase>> DeviceBase::CreateShaderModule(
if (IsValidationEnabled()) {
DAWN_TRY_CONTEXT(
ValidateShaderModuleDescriptor(this, descriptor, &parseResult, compilationMessages),
ValidateAndParseShaderModule(this, descriptor, &parseResult, compilationMessages),
"validating %s", descriptor);
}