Skip to content

Conversation

@tlively
Copy link
Member

@tlively tlively commented Dec 13, 2025

TypeSSA already had logic to detect and resolve inadvertent conflicts
between its newly constructed types and existing types. However, this
logic did not take into account the changes that the binary writer can
make when writing types, so it was still possible to construct a
situation where TypeSSA would produce types that would start conflicting
after binary writing.

Fix the problem by adding a new UniqueRecGroups utility to
wasm-type-shape.h. This utility uses the existing RecGroupShape utility,
which is aware of how the binary writer will modify types, to detect
conflicts. It uses the BrandTypeIterator, moved to wasm-type-shape.h
from MinimizeRecGroups.cpp, to create new types to differentiate rec
groups.

Make enough constructors of Type and Field constexpr that we can make
the fieldOptions array in BrandTypeIterator constexpr as well. This lets
us remove logic for initializing this array at runtime.
TypeSSA already had logic to detect and resolve inadvertent conflicts
between its newly constructed types and existing types. However, this
logic did not take into account the changes that the binary writer can
make when writing types, so it was still possible to construct a
situation where TypeSSA would produce types that would start conflicting
after binary writing.

Fix the problem by adding a new UniqueRecGroups utility to
wasm-type-shape.h. This utility uses the existing RecGroupShape utility,
which is aware of how the binary writer will modify types, to detect
conflicts. It uses the BrandTypeIterator, moved to wasm-type-shape.h
from MinimizeRecGroups.cpp, to create new types to differentiate rec
groups.
@tlively tlively requested review from aheejin and kripken December 13, 2025 03:40
Base automatically changed from constexpr-brand-fields to main December 13, 2025 20:51
}
};

struct UniqueRecGroups {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps an overview comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

// the group will be rebuilt with a brand at the end to make it unique.
// Returns the rebuilt types (including the brand) or the original types if no
// brand was necessary.
const std::vector<HeapType>& get(std::vector<HeapType> group);
Copy link
Member

Choose a reason for hiding this comment

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

This is called "get", but it adds a rec group - perhaps "add"?

I was confused by this API in the code, too, calls to get() seemed to have an effect that I couldn't figure out without reading this header.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, you're right. I'll go with insert.

for (auto group : existing) {
std::vector<HeapType> types(group.begin(), group.end());
[[maybe_unused]] auto uniqueTypes = unique.get(std::move(types));
assert(uniqueTypes.size() == group.size() && "unexpected collision");
Copy link
Member Author

Choose a reason for hiding this comment

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

The fuzzer found a way to make this fail after about 80k iterations, so I'll investigate and fix that as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

This can still happen when custom descriptors are disabled and the input already includes rec groups that differ only in exactness. This is nonsensical, but our validator doesn't reject it, so the fuzzer can still generate it. I'll investigate rejecting these cases in the parsers.

GlobalTypeRewriter methods previously took an optional list of extra
types to consider private and therefore eligible to be modified. Remove
this parameter because it is not used.
@tlively tlively changed the base branch from main to refactor-extra-private-types December 16, 2025 02:52
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

lgtm % question and the fuzz issue you mentioned

;; CHECK-NEXT: (type $A_1 (sub $A (shared (array (mut i32)))))

;; CHECK: (type $4 (struct (field (mut i32)) (field (mut i32)) (field (mut f64)) (field (mut f64)) (field (mut i32)) (field (mut f64)) (field (mut f64)) (field (mut i32)) (field (mut i32)) (field (mut i32)) (field (mut i32))))
;; CHECK: (type $4 (struct))
Copy link
Member

Choose a reason for hiding this comment

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

Why did this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

TypeSSA no longer encodes a hash as a brand type, but rather uses the brand iterator, which produces much smaller types.

Base automatically changed from refactor-extra-private-types to main December 16, 2025 21:25
Move BrandTypeIterator from MinimizeRecGroups to wasm-type-shape.h and
use it in a new UniqueRecGroups utility that can rebuild types to be
distinct from previously seen rec groups. Use UniqueRecGroups in
GlobalTypeRewriter to ensure the newly built private types do not
conflict with public types.

Split off from #8119 because this can land sooner.
@tlively tlively changed the base branch from main to type-updater-public-conflict December 16, 2025 22:01
tlively added a commit that referenced this pull request Dec 16, 2025
Move BrandTypeIterator from MinimizeRecGroups to wasm-type-shape.h and
use it in a new UniqueRecGroups utility that can rebuild types to be
distinct from previously seen rec groups. Use UniqueRecGroups in
GlobalTypeRewriter to ensure the newly built private types do not
conflict with public types.

Split off from #8119 because this can land sooner.
Base automatically changed from type-updater-public-conflict to main December 16, 2025 22:47
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