Skip to content

Commit f820f7c

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[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: #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 <[email protected]> Reviewed-by: Aske Simon Christensen <[email protected]>
1 parent 18bc70b commit f820f7c

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

benchmarks/FfiMemory/dart/FfiMemory.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,30 @@ class PointerUint32 extends BenchmarkBase {
390390
}
391391
}
392392

393+
class PointerUint32Unaligned extends BenchmarkBase {
394+
Pointer<Uint32> pointer = nullptr;
395+
Pointer<Uint32> unalignedPointer = nullptr;
396+
PointerUint32Unaligned() : super('FfiMemory.PointerUint32Unaligned');
397+
398+
@override
399+
void setup() {
400+
pointer = calloc(N + 1);
401+
unalignedPointer = Pointer.fromAddress(pointer.address + 1);
402+
}
403+
404+
@override
405+
void teardown() => calloc.free(pointer);
406+
407+
@override
408+
void run() {
409+
doStoreUint32(unalignedPointer, N);
410+
final int x = doLoadUint32(unalignedPointer, N);
411+
if (x != N) {
412+
throw Exception('$name: Unexpected result: $x');
413+
}
414+
}
415+
}
416+
393417
class PointerInt64 extends BenchmarkBase {
394418
Pointer<Int64> pointer = nullptr;
395419
PointerInt64() : super('FfiMemory.PointerInt64');
@@ -527,6 +551,7 @@ void main() {
527551
() => PointerUint16(),
528552
() => PointerInt32(),
529553
() => PointerUint32(),
554+
() => PointerUint32Unaligned(),
530555
() => PointerInt64(),
531556
() => PointerInt64Mint(),
532557
() => PointerUint64(),

benchmarks/FfiMemory/dart2/FfiMemory.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,30 @@ class PointerUint32 extends BenchmarkBase {
390390
}
391391
}
392392

393+
class PointerUint32Unaligned extends BenchmarkBase {
394+
Pointer<Float> pointer;
395+
Pointer<Uint32> unalignedPointer;
396+
PointerUint32Unaligned() : super('FfiMemory.PointerUint32Unaligned');
397+
398+
@override
399+
void setup() {
400+
pointer = calloc(N + 1);
401+
unalignedPointer = Pointer.fromAddress(pointer.address + 1);
402+
}
403+
404+
@override
405+
void teardown() => calloc.free(pointer);
406+
407+
@override
408+
void run() {
409+
doStoreUint32(unalignedPointer, N);
410+
final int x = doLoadUint32(unalignedPointer, N);
411+
if (x != N) {
412+
throw Exception('$name: Unexpected result: $x');
413+
}
414+
}
415+
}
416+
393417
class PointerInt64 extends BenchmarkBase {
394418
Pointer<Int64> pointer;
395419
PointerInt64() : super('FfiMemory.PointerInt64');
@@ -527,6 +551,7 @@ void main() {
527551
() => PointerUint16(),
528552
() => PointerInt32(),
529553
() => PointerUint32(),
554+
() => PointerUint32Unaligned(),
530555
() => PointerInt64(),
531556
() => PointerInt64Mint(),
532557
() => PointerUint64(),

tests/ffi/unaligned_test.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2021, 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+
// This tests exercises misaligned reads/writes on memory.
6+
//
7+
// The only architecture on which this is known to fail is arm32 on Android.
8+
9+
import 'dart:ffi';
10+
import 'dart:io';
11+
12+
import 'package:expect/expect.dart';
13+
import 'package:ffi/ffi.dart';
14+
15+
void main() {
16+
print("hello");
17+
testUnalignedInt16(); //# 01: ok
18+
testUnalignedInt32(); //# 02: ok
19+
testUnalignedInt64(); //# 03: ok
20+
if (!Platform.isAndroid || sizeOf<Pointer>() == 8) {
21+
// TODO(http://dartbug.com/45009): Support unaligned reads/writes on
22+
// Android arm32.
23+
testUnalignedFloat(); //# 04: ok
24+
testUnalignedDouble(); //# 05: ok
25+
}
26+
_freeAll();
27+
}
28+
29+
void testUnalignedInt16() {
30+
final pointer = _allocateUnaligned<Int16>();
31+
pointer.value = 20;
32+
Expect.equals(20, pointer.value);
33+
}
34+
35+
void testUnalignedInt32() {
36+
final pointer = _allocateUnaligned<Int32>();
37+
pointer.value = 20;
38+
Expect.equals(20, pointer.value);
39+
}
40+
41+
void testUnalignedInt64() {
42+
final pointer = _allocateUnaligned<Int64>();
43+
pointer.value = 20;
44+
Expect.equals(20, pointer.value);
45+
}
46+
47+
void testUnalignedFloat() {
48+
final pointer = _allocateUnaligned<Float>();
49+
pointer.value = 20.0;
50+
Expect.approxEquals(20.0, pointer.value);
51+
}
52+
53+
void testUnalignedDouble() {
54+
final pointer = _allocateUnaligned<Double>();
55+
pointer.value = 20.0;
56+
Expect.equals(20.0, pointer.value);
57+
}
58+
59+
final Set<Pointer> _pool = {};
60+
61+
void _freeAll() {
62+
for (final pointer in _pool) {
63+
calloc.free(pointer);
64+
}
65+
}
66+
67+
/// Up to `size<T>() == 8`.
68+
Pointer<T> _allocateUnaligned<T extends NativeType>() {
69+
final pointer = calloc<Int8>(16);
70+
_pool.add(pointer);
71+
final misaligned = pointer.elementAt(1).cast<T>();
72+
Expect.equals(1, misaligned.address % 2);
73+
return misaligned;
74+
}

tests/ffi_2/unaligned_test.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2021, 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+
// This tests exercises misaligned reads/writes on memory.
6+
//
7+
// The only architecture on which this is known to fail is arm32 on Android.
8+
9+
import 'dart:ffi';
10+
import 'dart:io';
11+
12+
import 'package:expect/expect.dart';
13+
import 'package:ffi/ffi.dart';
14+
15+
void main() {
16+
print("hello");
17+
testUnalignedInt16(); //# 01: ok
18+
testUnalignedInt32(); //# 02: ok
19+
testUnalignedInt64(); //# 03: ok
20+
if (!Platform.isAndroid || sizeOf<Pointer>() == 8) {
21+
// TODO(http://dartbug.com/45009): Support unaligned reads/writes on
22+
// Android arm32.
23+
testUnalignedFloat(); //# 04: ok
24+
testUnalignedDouble(); //# 05: ok
25+
}
26+
_freeAll();
27+
}
28+
29+
void testUnalignedInt16() {
30+
final pointer = _allocateUnaligned<Int16>();
31+
pointer.value = 20;
32+
Expect.equals(20, pointer.value);
33+
}
34+
35+
void testUnalignedInt32() {
36+
final pointer = _allocateUnaligned<Int32>();
37+
pointer.value = 20;
38+
Expect.equals(20, pointer.value);
39+
}
40+
41+
void testUnalignedInt64() {
42+
final pointer = _allocateUnaligned<Int64>();
43+
pointer.value = 20;
44+
Expect.equals(20, pointer.value);
45+
}
46+
47+
void testUnalignedFloat() {
48+
final pointer = _allocateUnaligned<Float>();
49+
pointer.value = 20.0;
50+
Expect.approxEquals(20.0, pointer.value);
51+
}
52+
53+
void testUnalignedDouble() {
54+
final pointer = _allocateUnaligned<Double>();
55+
pointer.value = 20.0;
56+
Expect.equals(20.0, pointer.value);
57+
}
58+
59+
final Set<Pointer> _pool = {};
60+
61+
void _freeAll() {
62+
for (final pointer in _pool) {
63+
calloc.free(pointer);
64+
}
65+
}
66+
67+
/// Up to `size<T>() == 8`.
68+
Pointer<T> _allocateUnaligned<T extends NativeType>() {
69+
final pointer = calloc<Int8>(16);
70+
_pool.add(pointer);
71+
final misaligned = pointer.elementAt(1).cast<T>();
72+
Expect.equals(1, misaligned.address % 2);
73+
return misaligned;
74+
}

0 commit comments

Comments
 (0)