From be3673ca9bc16dacc721e76589f951e5df5ad996 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 22 Oct 2026 18:19:09 +0200 Subject: [PATCH 1/2] Fix use-after-free in Builder::visit_case for unresolvable payload types When the payload type of an enum case could not be resolved, visit_case popped the EnumItem from the items vector, destroying the case that _current_enum_case pointed into, and then fell through to code that read the case's children and popped it from the destroyed EnumItem. Return immediately after popping the EnumItem instead, restoring the enum case depth and current enum case, as the original implementation does. Callers already revert their items when visit_case returns false. Co-Authored-By: Claude Opus 5.5 --- .../ComputeCxx/Comparison/LayoutDescriptor.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp b/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp index 088cef34..21ae5b34 100644 --- a/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp +++ b/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp @@ -1145,23 +1145,25 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record }; enum_item.cases.push_back(std::move(enum_case)); - bool result; - _enum_case_depth += 1; EnumItem::Case *prev_enum_case = _current_enum_case; _current_enum_case = &enum_item.cases.back(); if (field.isIndirectCase() && _current_comparison_mode == 0) { add_field(sizeof(void *)); - result = true; } else { const auto *mangled_type_name = field.MangledTypeName.get(); const auto *field_type = mangled_type_name != nullptr ? type.mangled_type_name_ref(mangled_type_name, false, nullptr) : nullptr; if (field_type == nullptr) { - // bail out if we can't get a type for the enum case payload + // bail out if we can't get a type for the enum case payload. The enum item and its cases are destroyed, so + // return without touching them again. items.pop_back(); - result = false; + + _enum_case_depth -= 1; + _current_enum_case = prev_enum_case; + + return false; } else if (field.isIndirectCase()) { if (auto field_size = field_type->vw_size()) { IndirectItem item = { @@ -1171,7 +1173,6 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record }; _current_enum_case->children.push_back(item); } - result = true; } else { IAGComparisonMode prev_comparison_mode = _current_comparison_mode; _current_comparison_mode = mode_for_type(&type, _current_comparison_mode); @@ -1209,8 +1210,6 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record } _current_comparison_mode = prev_comparison_mode; - - result = true; } } @@ -1221,7 +1220,7 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record _enum_case_depth -= 1; _current_enum_case = prev_enum_case; - return result; + return true; } bool Builder::visit_existential(const swift::existential_type_metadata &type) { From 5ba9cad10cebb4d12afb892734e5b8ce80c1475a Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 22 Oct 2026 18:19:10 +0200 Subject: [PATCH 2/2] Capture item count before visiting enum case payload in Builder::visit_case The item count passed to revert() was read after visiting the payload type, so the revert never removed anything. Read it before the visit, as the original implementation and visit_element do. This does not change the resulting layout, since the case's children are cleared immediately after reverting. Co-Authored-By: Claude Opus 5.5 --- Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp b/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp index 21ae5b34..ecfe2a2d 100644 --- a/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp +++ b/Sources/ComputeCxx/Comparison/LayoutDescriptor.cpp @@ -1181,6 +1181,7 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record // same as visit_element auto &items = get_items(); + auto num_items = items.size(); size_t prev_offset = -1; size_t prev_size = 0; if (auto *data_item = items.size() > 0 ? std::get_if(&items.back()) : nullptr) { @@ -1190,7 +1191,7 @@ bool Builder::visit_case(const swift::metadata &type, const swift::field_record if (!field_type->visit(*this)) { RevertItemsInfo info = { - items.size(), + num_items, { prev_offset, prev_size,