dart-sdk/tests/ffi/unaligned_test.dart
Daco Harkes f820f7ce48 [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>
2021-03-16 20:17:31 +00:00

74 lines
1.9 KiB
Dart

// 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;
}