[vm/ffi] Exercise unaligned reads/writes

This CL validates the hypothesis that the only misaligned reads/writes
which are not supported are double and float, and only on arm32.

Running these on the CI and Golem will validate this hypothesis.

Bug: https://github.com/dart-lang/sdk/issues/45009

Change-Id: I0a77fd1f47a388d1f454c1ded50cd7ecaeadb0f0
Cq-Include-Trybots: luci.dart.try:dart-sdk-linux-try,dart-sdk-mac-try,dart-sdk-win-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-mac-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-x64-try,vm-kernel-nnbd-linux-debug-ia32-try,vm-kernel-nnbd-mac-release-x64-try,vm-kernel-nnbd-win-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-linux-debug-simarm_x64-try,vm-kernel-precomp-nnbd-linux-debug-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-reload-linux-debug-x64-try,vm-kernel-reload-rollback-linux-debug-x64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-precomp-android-release-arm_x64-try,analyzer-analysis-server-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190523
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
This commit is contained in:
Daco Harkes 2021-03-16 20:17:31 +00:00 committed by commit-bot@chromium.org
parent 18bc70b898
commit f820f7ce48
4 changed files with 198 additions and 0 deletions

View file

@ -390,6 +390,30 @@ class PointerUint32 extends BenchmarkBase {
}
}
class PointerUint32Unaligned extends BenchmarkBase {
Pointer<Uint32> pointer = nullptr;
Pointer<Uint32> unalignedPointer = nullptr;
PointerUint32Unaligned() : super('FfiMemory.PointerUint32Unaligned');
@override
void setup() {
pointer = calloc(N + 1);
unalignedPointer = Pointer.fromAddress(pointer.address + 1);
}
@override
void teardown() => calloc.free(pointer);
@override
void run() {
doStoreUint32(unalignedPointer, N);
final int x = doLoadUint32(unalignedPointer, N);
if (x != N) {
throw Exception('$name: Unexpected result: $x');
}
}
}
class PointerInt64 extends BenchmarkBase {
Pointer<Int64> pointer = nullptr;
PointerInt64() : super('FfiMemory.PointerInt64');
@ -527,6 +551,7 @@ void main() {
() => PointerUint16(),
() => PointerInt32(),
() => PointerUint32(),
() => PointerUint32Unaligned(),
() => PointerInt64(),
() => PointerInt64Mint(),
() => PointerUint64(),

View file

@ -390,6 +390,30 @@ class PointerUint32 extends BenchmarkBase {
}
}
class PointerUint32Unaligned extends BenchmarkBase {
Pointer<Float> pointer;
Pointer<Uint32> unalignedPointer;
PointerUint32Unaligned() : super('FfiMemory.PointerUint32Unaligned');
@override
void setup() {
pointer = calloc(N + 1);
unalignedPointer = Pointer.fromAddress(pointer.address + 1);
}
@override
void teardown() => calloc.free(pointer);
@override
void run() {
doStoreUint32(unalignedPointer, N);
final int x = doLoadUint32(unalignedPointer, N);
if (x != N) {
throw Exception('$name: Unexpected result: $x');
}
}
}
class PointerInt64 extends BenchmarkBase {
Pointer<Int64> pointer;
PointerInt64() : super('FfiMemory.PointerInt64');
@ -527,6 +551,7 @@ void main() {
() => PointerUint16(),
() => PointerInt32(),
() => PointerUint32(),
() => PointerUint32Unaligned(),
() => PointerInt64(),
() => PointerInt64Mint(),
() => PointerUint64(),

View file

@ -0,0 +1,74 @@
// Copyright (c) 2021, 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.
// This tests exercises misaligned reads/writes on memory.
//
// The only architecture on which this is known to fail is arm32 on Android.
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';
void main() {
print("hello");
testUnalignedInt16(); //# 01: ok
testUnalignedInt32(); //# 02: ok
testUnalignedInt64(); //# 03: ok
if (!Platform.isAndroid || sizeOf<Pointer>() == 8) {
// TODO(http://dartbug.com/45009): Support unaligned reads/writes on
// Android arm32.
testUnalignedFloat(); //# 04: ok
testUnalignedDouble(); //# 05: ok
}
_freeAll();
}
void testUnalignedInt16() {
final pointer = _allocateUnaligned<Int16>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedInt32() {
final pointer = _allocateUnaligned<Int32>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedInt64() {
final pointer = _allocateUnaligned<Int64>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedFloat() {
final pointer = _allocateUnaligned<Float>();
pointer.value = 20.0;
Expect.approxEquals(20.0, pointer.value);
}
void testUnalignedDouble() {
final pointer = _allocateUnaligned<Double>();
pointer.value = 20.0;
Expect.equals(20.0, pointer.value);
}
final Set<Pointer> _pool = {};
void _freeAll() {
for (final pointer in _pool) {
calloc.free(pointer);
}
}
/// Up to `size<T>() == 8`.
Pointer<T> _allocateUnaligned<T extends NativeType>() {
final pointer = calloc<Int8>(16);
_pool.add(pointer);
final misaligned = pointer.elementAt(1).cast<T>();
Expect.equals(1, misaligned.address % 2);
return misaligned;
}

View file

@ -0,0 +1,74 @@
// Copyright (c) 2021, 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.
// This tests exercises misaligned reads/writes on memory.
//
// The only architecture on which this is known to fail is arm32 on Android.
import 'dart:ffi';
import 'dart:io';
import 'package:expect/expect.dart';
import 'package:ffi/ffi.dart';
void main() {
print("hello");
testUnalignedInt16(); //# 01: ok
testUnalignedInt32(); //# 02: ok
testUnalignedInt64(); //# 03: ok
if (!Platform.isAndroid || sizeOf<Pointer>() == 8) {
// TODO(http://dartbug.com/45009): Support unaligned reads/writes on
// Android arm32.
testUnalignedFloat(); //# 04: ok
testUnalignedDouble(); //# 05: ok
}
_freeAll();
}
void testUnalignedInt16() {
final pointer = _allocateUnaligned<Int16>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedInt32() {
final pointer = _allocateUnaligned<Int32>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedInt64() {
final pointer = _allocateUnaligned<Int64>();
pointer.value = 20;
Expect.equals(20, pointer.value);
}
void testUnalignedFloat() {
final pointer = _allocateUnaligned<Float>();
pointer.value = 20.0;
Expect.approxEquals(20.0, pointer.value);
}
void testUnalignedDouble() {
final pointer = _allocateUnaligned<Double>();
pointer.value = 20.0;
Expect.equals(20.0, pointer.value);
}
final Set<Pointer> _pool = {};
void _freeAll() {
for (final pointer in _pool) {
calloc.free(pointer);
}
}
/// Up to `size<T>() == 8`.
Pointer<T> _allocateUnaligned<T extends NativeType>() {
final pointer = calloc<Int8>(16);
_pool.add(pointer);
final misaligned = pointer.elementAt(1).cast<T>();
Expect.equals(1, misaligned.address % 2);
return misaligned;
}