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

[Impeller] Fix a buffer overrun in ImpellerC reflector resource offsets #51108

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions impeller/compiler/reflector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ std::shared_ptr<RuntimeStageData::Shader> Reflector::GenerateRuntimeStageData()
const auto inputs = compiler_->get_shader_resources().stage_inputs;
auto input_offsets = ComputeOffsets(inputs);
for (const auto& input : inputs) {
auto location = compiler_->get_decoration(
input.id, spv::Decoration::DecorationLocation);
std::optional<size_t> offset = input_offsets[location];
std::optional<size_t> offset = GetOffset(input.id, input_offsets);

const auto type = compiler_->get_type(input.type_id);

Expand Down Expand Up @@ -518,9 +516,7 @@ std::shared_ptr<ShaderBundleData> Reflector::GenerateShaderBundleData() const {
const auto inputs = compiler_->get_shader_resources().stage_inputs;
auto input_offsets = ComputeOffsets(inputs);
for (const auto& input : inputs) {
auto location = compiler_->get_decoration(
input.id, spv::Decoration::DecorationLocation);
std::optional<size_t> offset = input_offsets[location];
std::optional<size_t> offset = GetOffset(input.id, input_offsets);

const auto type = compiler_->get_type(input.type_id);

Expand Down Expand Up @@ -624,6 +620,17 @@ std::vector<size_t> Reflector::ComputeOffsets(
return offsets;
}

std::optional<size_t> Reflector::GetOffset(
spirv_cross::ID id,
const std::vector<size_t>& offsets) const {
uint32_t location =
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know there was just a tool at the time impellerc was written, but there is spriv-val that we can run over the intermediate Stage 1 SPIRV to avoid getting into this situation. But being defensive here make sense to me.

compiler_->get_decoration(id, spv::Decoration::DecorationLocation);
if (location >= offsets.size()) {
return std::nullopt;
}
return offsets[location];
}

std::optional<nlohmann::json::object_t> Reflector::ReflectResource(
const spirv_cross::Resource& resource,
std::optional<size_t> offset) const {
Expand Down Expand Up @@ -698,9 +705,7 @@ std::optional<nlohmann::json::array_t> Reflector::ReflectResources(
for (const auto& resource : resources) {
std::optional<size_t> maybe_offset = std::nullopt;
if (compute_offsets) {
auto location = compiler_->get_decoration(
resource.id, spv::Decoration::DecorationLocation);
maybe_offset = offsets[location];
maybe_offset = GetOffset(resource.id, offsets);
}
if (auto reflected = ReflectResource(resource, maybe_offset);
reflected.has_value()) {
Expand Down
3 changes: 3 additions & 0 deletions impeller/compiler/reflector.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class Reflector {
std::vector<size_t> ComputeOffsets(
const spirv_cross::SmallVector<spirv_cross::Resource>& resources) const;

std::optional<size_t> GetOffset(spirv_cross::ID id,
const std::vector<size_t>& offsets) const;

std::optional<nlohmann::json::object_t> ReflectType(
const spirv_cross::TypeID& type_id) const;

Expand Down