Skip to content

Commit db76210

Browse files
dcharkesCommit Queue
authored and
Commit Queue
committed
[vm/ffi] Fix Pointer deferred materialization on deopt
`Pointer`s were not handled in deferred materialization of objects on deoptimizations. This lead to the address field being written as a tagged value, causing the actual address to be smitagged. Subsequent uses of those addresses would lead to segfaults. This CL handles the `Pointer`s manually to deal with the untagged address. `--trace-deoptimization-verbose` before this CL: ``` Deoptimizing [...]     _typedDataBase@8050071 <- Pointer: address=0x7ffff7488081     null Field @ offset(8) <- 140736616678128     null Field @ offset(16) <- TypeArguments: (H39d2e3b4) [Type: Never] ``` after this CL: ``` Deoptimizing [...] _typedDataBase@8050071 <- Pointer: address=0x7fa2c0a88081 pointer@data <- 0x7fa29c16c0e0 pointer@type_args <- TypeArguments: (H39d2e3b4) [Type: Never] ``` TEST=runtime/tests/vm/dart/regress_54871_test.dart TEST=tests/ffi/structs_test.dart with --hot-reload-rollback-test-mode and --optimization-counter-threshold=50 Closes: #54871 Change-Id: I13b6404c8b098643b8ac0f59ee8e9bc635f33b8d Cq-Include-Trybots: luci.dart.try:vm-reload-rollback-linux-debug-x64-try,vm-reload-rollback-linux-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352300 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent 29782f8 commit db76210

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Regression test for https://dartbug.com/54871.
6+
7+
import 'dart:ffi';
8+
import 'dart:_internal';
9+
10+
const address = 0xaabbccdd;
11+
bool deoptimize = false;
12+
13+
main() {
14+
for (int i = 0; i < 100000; ++i) {
15+
foo();
16+
}
17+
deoptimize = true;
18+
foo();
19+
}
20+
21+
@pragma('vm:never-inline')
22+
void foo() {
23+
final pointer = Pointer<Void>.fromAddress(address);
24+
useInteger(pointer.address);
25+
final pointerAddress = pointer.address;
26+
if (address != pointerAddress) {
27+
throw '$address vs $pointerAddress';
28+
}
29+
}
30+
31+
@pragma('vm:never-inline')
32+
void useInteger(int address) {
33+
if (deoptimize) {
34+
VMInternalsForTesting.deoptimizeFunctionsOnStack();
35+
}
36+
}

runtime/vm/deferred_objects.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "vm/deopt_instructions.h"
1212
#include "vm/flags.h"
1313
#include "vm/object.h"
14+
#include "vm/object_store.h"
1415

1516
namespace dart {
1617

@@ -350,6 +351,29 @@ void DeferredObject::Fill() {
350351
}
351352
}
352353
} break;
354+
case kPointerCid: {
355+
auto* const zone = Thread::Current()->zone();
356+
const int kDataIndex = 0;
357+
const int kTypeArgIndex = 1;
358+
ASSERT(field_count_ == 2);
359+
ASSERT(Smi::Cast(Object::Handle(zone, GetFieldOffset(kDataIndex)))
360+
.AsInt64Value() == PointerBase::data_offset());
361+
ASSERT(Smi::Cast(Object::Handle(zone, GetFieldOffset(kTypeArgIndex)))
362+
.AsInt64Value() == Pointer::type_arguments_offset());
363+
364+
const auto& pointer = Pointer::Cast(*object_);
365+
const size_t address =
366+
Integer::Cast(Object::Handle(zone, GetValue(kDataIndex)))
367+
.AsInt64Value();
368+
pointer.SetNativeAddress(address);
369+
const auto& type_args = TypeArguments::Handle(
370+
zone, IsolateGroup::Current()->object_store()->type_argument_never());
371+
pointer.SetTypeArguments(type_args);
372+
if (FLAG_trace_deoptimization_verbose) {
373+
OS::PrintErr(" pointer@data <- 0x%" Px "\n", address);
374+
OS::PrintErr(" pointer@type_args <- %s\n", type_args.ToCString());
375+
}
376+
} break;
353377
case kRecordCid: {
354378
const Record& record = Record::Cast(*object_);
355379

runtime/vm/object.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26099,7 +26099,7 @@ PointerPtr Pointer::New(uword native_address, Heap::Space space) {
2609926099
Thread* thread = Thread::Current();
2610026100
Zone* zone = thread->zone();
2610126101

26102-
TypeArguments& type_args = TypeArguments::Handle(
26102+
const auto& type_args = TypeArguments::Handle(
2610326103
zone, IsolateGroup::Current()->object_store()->type_argument_never());
2610426104

2610526105
const Class& cls =

0 commit comments

Comments
 (0)