Commit 2b0e1284 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

[mojo] Don't inline unions with string fields

Inline allocations for unions have been allowed as long as the union
doesn't contain any non-string reference-type fields (i.e. non-handle,
non-POD fields). Exceptions were made for strings since they're copyable
and don't introduce a possibility of recursive types.

InlinedStructPtr however is incompatible with a union's generated
wrapper class when the union's first field is a reference type like a
string. This results in difficult to diagnose crashes in cases that are
meant to be allowed by the bindings.

This change prohibits unions from being allocated inline unless they
contain only POD fields.

Inline allocations could be allowed in these (and other) cases, but
that would require more fundamental refactoring of the wrapper types
and/or StructPtr/InlinedStructPtr.

Fixed: 1114366
Change-Id: I623779f067e777a9240aef67a40decc884948247
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2347167Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Auto-Submit: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#796660}
parent 8e7f26bb
...@@ -1229,5 +1229,11 @@ TEST(UnionTest, UnionInInterface) { ...@@ -1229,5 +1229,11 @@ TEST(UnionTest, UnionInInterface) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
TEST(UnionTest, InlineUnionAllocationWithNonPODFirstField) {
// Regression test for https://crbug.com/1114366. Should not crash.
UnionWithStringForFirstFieldPtr u;
u = UnionWithStringForFirstField::NewS("hey");
}
} // namespace test } // namespace test
} // namespace mojo } // namespace mojo
...@@ -114,3 +114,8 @@ struct ImportedUnionStruct { ...@@ -114,3 +114,8 @@ struct ImportedUnionStruct {
union ImportedUnionUnion { union ImportedUnionUnion {
imported.PointOrShape point_or_shape; imported.PointOrShape point_or_shape;
}; };
union UnionWithStringForFirstField {
string s;
int32 i;
};
...@@ -180,9 +180,7 @@ def ShouldInlineStruct(struct): ...@@ -180,9 +180,7 @@ def ShouldInlineStruct(struct):
def ShouldInlineUnion(union): def ShouldInlineUnion(union):
return not any( return not any(mojom.IsReferenceKind(field.kind) for field in union.fields)
mojom.IsReferenceKind(field.kind) and not mojom.IsStringKind(field.kind)
for field in union.fields)
def HasPackedMethodOrdinals(interface): def HasPackedMethodOrdinals(interface):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment