Skip to content

Commit d650ea9

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
[vm/ffi] Fix alignment of Uint64 fields in structs on some ABIs
Affected ABIs: ia32 Linux, ia32 Android, ia32 MacOS, and arm32 iOS. Fixes #43693 Change-Id: I35f719abb69bb46d99f647d66847e3d5874d9371 Cq-Include-Trybots: luci.dart.try:vm-kernel-linux-debug-ia32-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166843 Commit-Queue: Daco Harkes <[email protected]> Auto-Submit: Daco Harkes <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 03af042 commit d650ea9

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

pkg/vm/lib/transformations/ffi.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ const nonSizeAlignment = <Abi, Map<NativeType, int>>{
137137
//
138138
// iOS 32 bit alignment:
139139
// https://developer.apple.com/documentation/uikit/app_and_environment/updating_your_app_from_32-bit_to_64-bit_architecture/updating_data_structures
140-
Abi.wordSize32Align32: {NativeType.kDouble: 4, NativeType.kInt64: 4},
140+
Abi.wordSize32Align32: {
141+
NativeType.kDouble: 4,
142+
NativeType.kInt64: 4,
143+
NativeType.kUnit64: 4
144+
},
141145

142146
// The default for MSVC x86:
143147
// > The alignment-requirement for all data except structures, unions, and

runtime/bin/ffi_test/ffi_test_functions.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,4 +1042,13 @@ DART_EXPORT int32_t PassStruct(void*) {
10421042
return 42;
10431043
}
10441044

1045+
struct Struct43693 {
1046+
void* pSomePtr;
1047+
uint64_t someValue;
1048+
};
1049+
1050+
DART_EXPORT uint64_t Regress43693(Struct43693* my_struct) {
1051+
return my_struct->someValue;
1052+
}
1053+
10451054
} // namespace dart

tests/ffi/regress_43693_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2020, 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+
// SharedObjects=ffi_test_functions
6+
7+
import 'dart:ffi';
8+
9+
import 'package:ffi/ffi.dart';
10+
import 'package:expect/expect.dart';
11+
12+
import 'dylib_utils.dart';
13+
14+
class Struct43693 extends Struct {
15+
external Pointer<Void> somePtr;
16+
17+
@Uint64()
18+
external int someValue;
19+
}
20+
21+
final int Function(Pointer<Struct43693>) readMyStructSomeValue =
22+
ffiTestFunctions
23+
.lookup<NativeFunction<Uint64 Function(Pointer<Struct43693>)>>(
24+
"Regress43693")
25+
.asFunction<int Function(Pointer<Struct43693>)>();
26+
27+
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
28+
29+
void main() {
30+
final myStructs = allocate<Struct43693>();
31+
myStructs[0].somePtr = nullptr;
32+
myStructs[0].someValue = 0xAAAAAAAABBBBBBBB;
33+
final result = readMyStructSomeValue(myStructs);
34+
Expect.equals(0xAAAAAAAABBBBBBBB, result);
35+
free(myStructs);
36+
}

tests/ffi_2/regress_43693_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2020, 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+
// SharedObjects=ffi_test_functions
6+
7+
import 'dart:ffi';
8+
9+
import 'package:ffi/ffi.dart';
10+
import 'package:expect/expect.dart';
11+
12+
import 'dylib_utils.dart';
13+
14+
class Struct43693 extends Struct {
15+
Pointer<Void> somePtr;
16+
17+
@Uint64()
18+
int someValue;
19+
}
20+
21+
final int Function(Pointer<Struct43693>) readMyStructSomeValue =
22+
ffiTestFunctions
23+
.lookup<NativeFunction<Uint64 Function(Pointer<Struct43693>)>>(
24+
"Regress43693")
25+
.asFunction<int Function(Pointer<Struct43693>)>();
26+
27+
final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");
28+
29+
void main() {
30+
final myStructs = allocate<Struct43693>();
31+
myStructs[0].somePtr = nullptr;
32+
myStructs[0].someValue = 0xAAAAAAAABBBBBBBB;
33+
final result = readMyStructSomeValue(myStructs);
34+
Expect.equals(0xAAAAAAAABBBBBBBB, result);
35+
free(myStructs);
36+
}

0 commit comments

Comments
 (0)