Files
sdk/tests/ffi/vmspecific_function_gc_test.dart
Ryan Macnak d36adbacaf [vm] Remove the VM isolate.
The former contents of the VM isolate are now included into each isolate group. This makes each isolate group's heap independent, and in particular allows each heap to be allocated to a separate pointer cage (not done in this CL).

The duplicated stubs that allowed PC relative calls are removed, since the originals can now be the target of PC relative calls.

The bootstrapping needing to load an AppJIT or AppAOT snapshot is reduced to allocating the oddballs. The code is entirely dropped in the AOT runtime, but the JIT runtime still has it to allow for flags to affect the compilation of the stub code. Further refactoring might be able to remove this for the JIT runtime too, with only gen_snapshot knowing how to bootstrap.

Class serialization no longer distinguishes predefined classes.

The page containing null is marked as never-evacuate. null, false and true must not move because the compiler relies on their low bits having certain patterns for some optimizations. (Previously, the entire VM isolate heap never moved.)

Compaction is disabled for IA32. Due to register pressure, some stub calls must not use a scratch register and embed the address of Code.

The page containing the call-through-safepoint stub is frozen when running with --write-protect-code and the stub is created at runtime (instead of loaded from an AppJIT or AppAOT snapshot). This stub must remain executable even during a safepoint, as a foreign call might during return during a safepoint and only block after the stub directs it to the runtime.

The snapshot symbols are renamed to kDartSnapshotData and kDartSnapshotText. There is no need to distinguish the VM isolate's snapshot, and snaphots are per isolate group not per isolate. Aliases with the old names are added to ease migration.

Some global flags that were automatically set based on the VM isolate's snapshot are now isolate group flags and automatically set by the isolate group's snapshot.

TEST=ci
Change-Id: Iee82016057d609112e9b021d178fc3d4d18b5044
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/500621
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
2026-05-18 11:35:03 -07:00

128 lines
3.7 KiB
Dart

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// VMOptions=
// VMOptions=--stacktrace-every=100
// VMOptions=--deterministic --optimization-counter-threshold=500
// VMOptions=--deterministic --optimization-counter-threshold=-1
// VMOptions=--write_protect_code=true
//
// Dart test program for stress-testing boxing and GC in return paths from FFI
// trampolines.
//
// NOTE: This test does not produce useful stderr when it fails because the
// stderr is redirected to a file for reflection.
//
// SharedObjects=ffi_test_functions
import 'dart:ffi' as ffi;
import "package:expect/expect.dart";
import 'ffi_test_helpers.dart';
main() async {
testBoxInt64();
testBoxInt32();
testBoxDouble();
testBoxPointer();
testAllocateInNative();
testRegress37069();
testWriteProtection();
}
typedef NativeNullaryOp64 = ffi.Int64 Function();
typedef NativeNullaryOp32 = ffi.Int32 Function();
typedef NativeNullaryOpDouble = ffi.Double Function();
typedef NativeNullaryOpPtr = ffi.Pointer<ffi.Void> Function();
typedef NativeUnaryOp = ffi.Void Function(ffi.Uint64);
typedef NativeUndenaryOp =
ffi.Uint64 Function(
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
ffi.Uint64,
);
typedef NullaryOp = int Function();
typedef NullaryOpDbl = double Function();
typedef NullaryOpPtr = ffi.Pointer<ffi.Void> Function();
typedef UnaryOp = void Function(int);
typedef UndenaryOp =
int Function(int, int, int, int, int, int, int, int, int, int, int);
//// These functions return values that require boxing into different types.
final minInt64 = ffiTestFunctions.lookupFunction<NativeNullaryOp64, NullaryOp>(
"MinInt64",
);
// Forces boxing into Mint on all platforms.
void testBoxInt64() {
Expect.equals(0x8000000000000000, minInt64());
}
NullaryOp minInt32 = ffiTestFunctions
.lookupFunction<NativeNullaryOp32, NullaryOp>("MinInt32");
// Forces boxing into Mint on 32-bit platforms only.
void testBoxInt32() {
Expect.equals(-0x80000000, minInt32());
}
final smallDouble = ffiTestFunctions
.lookupFunction<NativeNullaryOpDouble, NullaryOpDbl>("SmallDouble");
// Forces boxing into Double.
void testBoxDouble() {
Expect.equals(0x80000000 * -1.0, smallDouble());
}
final largePointer = ffiTestFunctions
.lookupFunction<NativeNullaryOpPtr, NullaryOpPtr>("LargePointer");
// Forces boxing into ffi.Pointer and ffi.Mint.
void testBoxPointer() {
ffi.Pointer pointer = largePointer();
if (ffi.sizeOf<ffi.Pointer>() == 4) {
Expect.equals(0x82000000, pointer.address);
} else {
Expect.equals(0x8100000082000000, pointer.address);
}
}
// Test GC in the FFI call path by calling a C function which triggers GC
// directly.
void testAllocateInNative() => triggerGc();
// This also works as a regression test for 37176.
final regress37069 = ffiTestFunctions
.lookupFunction<NativeUndenaryOp, UndenaryOp>("Regress37069");
// Test GC in the FFI call path by calling a C function which triggers GC
// directly.
void testRegress37069() {
regress37069(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
}
final unprotectCode = ffiTestFunctions
.lookupFunction<
ffi.Pointer<ffi.Void> Function(ffi.Pointer<ffi.Void>),
ffi.Pointer<ffi.Void> Function(ffi.Pointer<ffi.Void>)
>("TestUnprotectCode");
final waitForHelper = ffiTestFunctions
.lookupFunction<
ffi.Void Function(ffi.Pointer<ffi.Void>),
void Function(ffi.Pointer<ffi.Void>)
>("WaitForHelper");
void testWriteProtection() {
waitForHelper(unprotectCode(ffi.nullptr));
}