Skip to content

Commit e4a923a

Browse files
dcharkesHassnaa9
authored andcommitted
[code_assets] [native_toolchain_c] Add arm64e (dart-lang#3589)
Bug: * dart-lang#3379 This PR. * Adds the architecture to the code assets package for the protocol. * Consume it in native_toolchain_c`. * Validates the mach-o headers in the protocol. * And changes the macho validator test to test against arm64 arm64e mismatch.
1 parent 7eac7df commit e4a923a

17 files changed

Lines changed: 159 additions & 37 deletions

File tree

‎pkgs/code_assets/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.0-wip
2+
3+
- Added `Architecture.arm64e`.
4+
15
## 2.0.0
26

37
- Validate bundled dynamic libraries by reading their header: when the file is

‎pkgs/code_assets/doc/schema/shared/shared_definitions.schema.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"enum": [
2121
"arm",
2222
"arm64",
23+
"arm64e",
2324
"ia32",
2425
"riscv32",
2526
"riscv64",

‎pkgs/code_assets/lib/src/code_assets/architecture.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ final class Architecture {
2323
/// The [AArch64](https://en.wikipedia.org/wiki/AArch64) architecture.
2424
static const Architecture arm64 = Architecture._('arm64');
2525

26+
/// The [arm64e](https://developer.apple.com/documentation/security/preparing-your-app-to-work-with-pointer-authentication)
27+
/// architecture (ARM64 with pointer authentication).
28+
static const Architecture arm64e = Architecture._('arm64e');
29+
2630
/// The [IA-32](https://en.wikipedia.org/wiki/IA-32) architecture.
2731
static const Architecture ia32 = Architecture._('ia32');
2832

@@ -39,6 +43,7 @@ final class Architecture {
3943
static const List<Architecture> values = [
4044
arm,
4145
arm64,
46+
arm64e,
4247
ia32,
4348
riscv32,
4449
riscv64,

‎pkgs/code_assets/lib/src/code_assets/mach_o_validator.dart‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ final class MachOValidator implements NativeLibraryValidator {
4141
static const _cpuTypeX64 = 0x01000007;
4242
static const _cpuTypeArm = 0x0000000c;
4343
static const _cpuTypeArm64 = 0x0100000c;
44+
static const _cpuSubtypeMask = 0xff000000;
45+
static const _cpuSubtypeArm64e = 2;
4446

45-
// The magic number and the 32-bit field after it (the CPU type for a thin
46-
// binary, or the slice count for a fat binary) occupy the first 8 bytes. The
47-
// fat path re-reads the architecture entries from the file directly, so
48-
// nothing past this is read from the header here.
49-
static const _minLength = 8;
47+
// The magic number, CPU type, and CPU subtype for a thin binary occupy the
48+
// first 12 bytes. The fat path re-reads the architecture entries from the
49+
// file directly, so nothing past this is read from the header here.
50+
static const _minLength = 12;
5051

5152
@override
5253
Future<NativeLibraryValidation> validate(
@@ -91,8 +92,9 @@ final class MachOValidator implements NativeLibraryValidator {
9192
return const NativeLibraryValidation.notRecognized();
9293
}
9394
final cpuType = data.getUint32(4, endian);
95+
final cpuSubtype = data.getUint32(8, endian);
9496
return _checkArchitecture(
95-
_architectureForCpuType(cpuType),
97+
_architectureForCpu(cpuType: cpuType, cpuSubtype: cpuSubtype),
9698
cpuType,
9799
context.config.targetArchitecture,
98100
);
@@ -123,24 +125,27 @@ final class MachOValidator implements NativeLibraryValidator {
123125
}
124126
if (sliceCount > 1) return _multiArchitecture();
125127

126-
final cpuType = ByteData.sublistView(entries).getUint32(0, Endian.big);
128+
final sliceData = ByteData.sublistView(entries);
129+
final cpuType = sliceData.getUint32(0, Endian.big);
130+
final cpuSubtype = sliceData.getUint32(4, Endian.big);
127131
return _checkArchitecture(
128-
_architectureForCpuType(cpuType),
132+
_architectureForCpu(cpuType: cpuType, cpuSubtype: cpuSubtype),
129133
cpuType,
130134
target,
131135
);
132136
}
133137

134-
static Architecture? _architectureForCpuType(
135-
int cpuType,
136-
) => switch (cpuType) {
138+
static Architecture? _architectureForCpu({
139+
required int cpuType,
140+
required int cpuSubtype,
141+
}) => switch (cpuType) {
137142
_cpuTypeX86 => Architecture.ia32,
138143
_cpuTypeX64 => Architecture.x64,
139144
_cpuTypeArm => Architecture.arm,
140-
// arm64e uses CPU_TYPE_ARM64 and identifies the ABI in cpusubtype. Until
141-
// TODO(https://github.com/dart-lang/native/issues/3379) reads that subtype,
142-
// treat arm64e as arm64 rather than claiming to validate its ABI.
143-
_cpuTypeArm64 => Architecture.arm64,
145+
_cpuTypeArm64 =>
146+
(cpuSubtype & ~_cpuSubtypeMask) == _cpuSubtypeArm64e
147+
? Architecture.arm64e
148+
: Architecture.arm64,
144149
_ => null,
145150
};
146151

‎pkgs/code_assets/lib/src/code_assets/syntax.g.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ArchitectureSyntax {
4545

4646
static const arm64 = ArchitectureSyntax._('arm64');
4747

48+
static const arm64e = ArchitectureSyntax._('arm64e');
49+
4850
static const ia32 = ArchitectureSyntax._('ia32');
4951

5052
static const riscv32 = ArchitectureSyntax._('riscv32');
@@ -56,6 +58,7 @@ class ArchitectureSyntax {
5658
static const List<ArchitectureSyntax> values = [
5759
arm,
5860
arm64,
61+
arm64e,
5962
ia32,
6063
riscv32,
6164
riscv64,

‎pkgs/code_assets/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
This library contains the hook protocol specification for bundling native code
44
with Dart packages.
55
6-
version: 2.0.0
6+
version: 2.1.0-wip
77

88
repository: https://github.com/dart-lang/native/tree/main/pkgs/code_assets
99

‎pkgs/code_assets/test/code_assets/asset_test.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,11 @@ void main() {
159159
final current = Architecture.current;
160160
expect(current.toString(), Abi.current().toString().split('_')[1]);
161161
});
162+
163+
test('Architecture arm64e', () async {
164+
expect(Architecture.arm64e.name, 'arm64e');
165+
expect(Architecture.arm64e.toString(), 'arm64e');
166+
expect(Architecture.fromString('arm64e'), Architecture.arm64e);
167+
expect(Architecture.values, contains(Architecture.arm64e));
168+
});
162169
}

‎pkgs/code_assets/test/code_assets/validation_architecture_test.dart‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,54 @@ void main() {
159159
expect(records, isEmpty);
160160
});
161161

162+
test('thin arm64e matches macOS arm64e', () async {
163+
final (errors, records) = await validate(
164+
machOHeader(cpuType: 0x0100000c, cpuSubtype: 2),
165+
targetOS: OS.macOS,
166+
targetArchitecture: Architecture.arm64e,
167+
);
168+
expect(errors, isEmpty);
169+
expect(records, isEmpty);
170+
});
171+
172+
test('thin arm64e with PAC flags matches macOS arm64e', () async {
173+
final (errors, records) = await validate(
174+
machOHeader(cpuType: 0x0100000c, cpuSubtype: 0x80000002),
175+
targetOS: OS.macOS,
176+
targetArchitecture: Architecture.arm64e,
177+
);
178+
expect(errors, isEmpty);
179+
expect(records, isEmpty);
180+
});
181+
182+
test('thin arm64 mismatches macOS arm64e', () async {
183+
final (errors, _) = await validate(
184+
machOHeader(cpuType: 0x0100000c, cpuSubtype: 0),
185+
targetOS: OS.macOS,
186+
targetArchitecture: Architecture.arm64e,
187+
);
188+
expect(
189+
errors,
190+
contains(
191+
contains('is built for arm64, but the target architecture is arm64e'),
192+
),
193+
);
194+
});
195+
196+
test('thin arm64e mismatches macOS arm64', () async {
197+
final (errors, _) = await validate(
198+
machOHeader(cpuType: 0x0100000c, cpuSubtype: 2),
199+
targetOS: OS.macOS,
200+
targetArchitecture: Architecture.arm64,
201+
);
202+
expect(
203+
errors,
204+
contains(
205+
contains('is built for arm64e, but the target architecture is arm64'),
206+
),
207+
);
208+
});
209+
162210
test('thin x64 mismatches macOS arm64', () async {
163211
final (errors, _) = await validate(
164212
machOHeader(cpuType: 0x01000007),
@@ -194,6 +242,16 @@ void main() {
194242
);
195243
expect(errors, contains(contains('is built for x64')));
196244
});
245+
246+
test('fat arm64e matches iOS arm64e', () async {
247+
final (errors, records) = await validate(
248+
machOFatHeader(cpuTypes: [0x0100000c], cpuSubtypes: [2]),
249+
targetOS: OS.iOS,
250+
targetArchitecture: Architecture.arm64e,
251+
);
252+
expect(errors, isEmpty);
253+
expect(records, isEmpty);
254+
});
197255
});
198256

199257
group('PE', () {
@@ -340,24 +398,33 @@ Uint8List elfHeader({
340398
return bytes;
341399
}
342400

343-
/// A minimal 32-byte thin Mach-O header with [cpuType] after the magic.
344-
Uint8List machOHeader({required int cpuType, bool littleEndian = true}) {
401+
/// A minimal 32-byte thin Mach-O header with [cpuType] and [cpuSubtype].
402+
Uint8List machOHeader({
403+
required int cpuType,
404+
int cpuSubtype = 0,
405+
bool littleEndian = true,
406+
}) {
345407
final bytes = Uint8List(32);
346408
final data = bytes.buffer.asByteData();
347409
final endian = littleEndian ? Endian.little : Endian.big;
348410
data.setUint32(0, 0xfeedfacf, endian);
349411
data.setUint32(4, cpuType, endian);
412+
data.setUint32(8, cpuSubtype, endian);
350413
return bytes;
351414
}
352415

353416
/// A minimal big-endian fat Mach-O header with one slice per cpu type.
354-
Uint8List machOFatHeader({required List<int> cpuTypes}) {
417+
Uint8List machOFatHeader({
418+
required List<int> cpuTypes,
419+
List<int>? cpuSubtypes,
420+
}) {
355421
final bytes = Uint8List(8 + cpuTypes.length * 20);
356422
final data = bytes.buffer.asByteData();
357423
data.setUint32(0, 0xcafebabe);
358424
data.setUint32(4, cpuTypes.length);
359425
for (var slice = 0; slice < cpuTypes.length; slice++) {
360426
data.setUint32(8 + slice * 20, cpuTypes[slice]);
427+
data.setUint32(8 + slice * 20 + 4, cpuSubtypes?[slice] ?? 0);
361428
}
362429
return bytes;
363430
}

‎pkgs/hooks_runner/test_data/wrong_architecture_asset/hook/build.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void main(List<String> arguments) async {
1111
final codeConfig = input.config.code;
1212
final wrongArchitecture =
1313
codeConfig.targetArchitecture == Architecture.arm64
14-
? Architecture.x64
14+
? Architecture.arm64e
1515
: Architecture.arm64;
1616
final wrongInputBuilder = BuildInputBuilder()
1717
..setupShared(

‎pkgs/native_toolchain_c/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.19.5-wip
22

3+
- Support `Architecture.arm64e` for macOS and iOS.
34
- Access all file system state through a `package:file` `FileSystem`, exposed as
45
an optional parameter on `CBuilder.run`, `CLinker.run`, `CLibrary.build`, and
56
`CLibrary.link` (defaulting to `LocalFileSystem`), so file system access can be

0 commit comments

Comments
 (0)