Skip to content

Commit bcaf426

Browse files
alexmarkovCommit Queue
authored andcommitted
[vm,dyn_modules] Fix canonicalization of types when serializing bytecode
Avoid caching generic types C<T1,...,Tn> in the object table if any of type arguments T1,...,Tn are not cacheable. Otherwise, bytecode reader might create function type 'C<Y0> Function<Y0>()' where cached type 'C<Y0>' references parameter type Y0 which belongs to a wrong owner, such as another function type 'C<Y0> Function<Y0>(int)'. Also, fix equivalence of default values of function type parameters in case matching 'null' (meaning all dynamic) and '[dynamic, ..., dynamic]'. This may happen during canonicalization before '[dynamic, ..., dynamic]' type arguments are replaced with 'null'. TEST=ci Change-Id: If81736fb18a62fd16fd93e59436c8cd7ccce94ff Cq-Include-Trybots: luci.dart.try:vm-aot-dyn-linux-debug-x64-try,vm-aot-dyn-linux-product-x64-try,vm-dyn-linux-debug-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437522 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
1 parent 4eeb8cf commit bcaf426

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

pkg/dart2bytecode/lib/object_table.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ class _GenericTypeHandle extends _TypeHandle {
650650
_GenericTypeHandle(this.class_, this.typeArgs, Nullability nullability)
651651
: super(TypeTag.kGenericType, nullability);
652652

653+
@override
654+
bool get isCacheable => typeArgs?.isCacheable ?? true;
655+
653656
@override
654657
void writeContents(BufferedWriter writer) {
655658
writer.writePackedObject(class_);

runtime/vm/object.cc

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7449,8 +7449,10 @@ bool TypeArguments::IsSubvectorEquivalent(
74497449
return true;
74507450
}
74517451
if (kind == TypeEquality::kCanonical) {
7452-
if (IsNull() || other.IsNull()) {
7453-
return false;
7452+
if (IsNull()) {
7453+
return other.IsRaw(from_index, len);
7454+
} else if (other.IsNull()) {
7455+
return IsRaw(from_index, len);
74547456
}
74557457
if (Length() != other.Length()) {
74567458
return false;
@@ -10597,16 +10599,12 @@ bool FunctionType::HasSameTypeParametersAndBounds(
1059710599
TypeArguments::Handle(zone, type_params.defaults());
1059810600
const TypeArguments& other_defaults =
1059910601
TypeArguments::Handle(zone, other_type_params.defaults());
10600-
if (defaults.IsNull()) {
10601-
if (!other_defaults.IsNull()) {
10602-
TRACE_TYPE_CHECKS_VERBOSE(
10603-
" - result: false (mismatch in defaults)\n");
10604-
return false;
10605-
}
10606-
} else if (!defaults.IsEquivalent(other_defaults, kind,
10607-
function_type_equivalence)) {
10602+
if (!defaults.IsEquivalent(other_defaults, kind,
10603+
function_type_equivalence)) {
1060810604
TRACE_TYPE_CHECKS_VERBOSE(
10609-
" - result: false (default types are not equivalent)\n");
10605+
" - result: false (default types are not equivalent,"
10606+
" %s - %s)\n",
10607+
defaults.ToCString(), other_defaults.ToCString());
1061010608
return false;
1061110609
}
1061210610
}

runtime/vm/object.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8839,14 +8839,20 @@ class TypeArguments : public Instance {
88398839

88408840
// Check if the vectors are equal (they may be null).
88418841
bool Equals(const TypeArguments& other) const {
8842-
return IsSubvectorEquivalent(other, 0, IsNull() ? 0 : Length(),
8842+
if (ptr() == other.ptr()) {
8843+
return true;
8844+
}
8845+
return IsSubvectorEquivalent(other, 0, IsNull() ? other.Length() : Length(),
88438846
TypeEquality::kCanonical);
88448847
}
88458848

88468849
bool IsEquivalent(
88478850
const TypeArguments& other,
88488851
TypeEquality kind,
88498852
FunctionTypeMapping* function_type_equivalence = nullptr) const {
8853+
if (ptr() == other.ptr()) {
8854+
return true;
8855+
}
88508856
// Make a null vector a vector of dynamic as long as the other vector.
88518857
return IsSubvectorEquivalent(other, 0, IsNull() ? other.Length() : Length(),
88528858
kind, function_type_equivalence);

0 commit comments

Comments
 (0)