| // Copyright (c) 2026, 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. | |
| import 'dart:convert'; | |
| import 'dart:io'; | |
| import 'dart:typed_data'; | |
| import 'package:dart2native/dart2native_elf.dart'; | |
| import 'package:path/path.dart' as path; | |
| import 'package:test/test.dart'; | |
| import 'utils.dart'; | |
| Uint8List _buildSyntheticElf({ | |
| required bool is64Bit, | |
| int segmentAlign = 4, | |
| String ownerName = 'Dart.Cli.v1', | |
| bool includePrecedingNote = false, | |
| bool includeCliBundleNote = true, | |
| int? overrideProgramHeaderCount, | |
| }) { | |
| final headerSize = is64Bit ? 64 : 52; | |
| final phdrSize = is64Bit ? 56 : 32; | |
| final notesBuilder = BytesBuilder(); | |
| int alignUp(int value, int align) => (value + align - 1) & ~(align - 1); | |
| void appendNote({ | |
| required String owner, | |
| required int type, | |
| required List<int> descriptor, | |
| }) { | |
| final ownerBytes = [...utf8.encode(owner), 0]; | |
| final noteHeader = ByteData(12) | |
| ..setUint32(0, ownerBytes.length, Endian.little) | |
| ..setUint32(4, descriptor.length, Endian.little) | |
| ..setUint32(8, type, Endian.little); | |
| notesBuilder.add(noteHeader.buffer.asUint8List()); | |
| notesBuilder.add(ownerBytes); | |
| final afterName = notesBuilder.length; | |
| final alignedAfterName = alignUp(afterName, segmentAlign); | |
| notesBuilder.add(Uint8List(alignedAfterName - afterName)); | |
| notesBuilder.add(descriptor); | |
| final afterDesc = notesBuilder.length; | |
| final alignedAfterDesc = alignUp(afterDesc, segmentAlign); | |
| notesBuilder.add(Uint8List(alignedAfterDesc - afterDesc)); | |
| } | |
| if (includePrecedingNote) { | |
| appendNote( | |
| owner: 'GNU', | |
| type: 3, | |
| descriptor: List<int>.filled(20, 0xab), | |
| ); | |
| } | |
| if (includeCliBundleNote) { | |
| final descriptor = Uint8List(528); | |
| descriptor.setAll(0, const [ | |
| 0xdc, | |
| 0xdc, | |
| 0x44, | |
| 0x43, | |
| 0x4c, | |
| 0x49, | |
| 0x00, | |
| 0x01, | |
| ]); | |
| appendNote( | |
| owner: ownerName, | |
| type: 0x44434c49, | |
| descriptor: descriptor, | |
| ); | |
| } | |
| final noteBytes = notesBuilder.takeBytes(); | |
| final segmentOffset = alignUp(headerSize + phdrSize, segmentAlign); | |
| final totalBytes = Uint8List(segmentOffset + noteBytes.length); | |
| final data = ByteData.sublistView(totalBytes); | |
| // ELF identification. | |
| totalBytes.setAll(0, const [0x7f, 0x45, 0x4c, 0x46]); | |
| totalBytes[4] = is64Bit ? 2 : 1; | |
| totalBytes[5] = 1; // Little-endian. | |
| final phdrCount = overrideProgramHeaderCount ?? 1; | |
| if (is64Bit) { | |
| data.setUint64(32, headerSize, Endian.little); | |
| data.setUint16(54, phdrSize, Endian.little); | |
| data.setUint16(56, phdrCount, Endian.little); | |
| data.setUint32(headerSize + 0, 4, Endian.little); // PT_NOTE | |
| data.setUint64(headerSize + 8, segmentOffset, Endian.little); | |
| data.setUint64(headerSize + 32, noteBytes.length, Endian.little); | |
| data.setUint64(headerSize + 48, segmentAlign, Endian.little); | |
| } else { | |
| data.setUint32(28, headerSize, Endian.little); | |
| data.setUint16(42, phdrSize, Endian.little); | |
| data.setUint16(44, phdrCount, Endian.little); | |
| data.setUint32(headerSize + 0, 4, Endian.little); // PT_NOTE | |
| data.setUint32(headerSize + 4, segmentOffset, Endian.little); | |
| data.setUint32(headerSize + 16, noteBytes.length, Endian.little); | |
| data.setUint32(headerSize + 28, segmentAlign, Endian.little); | |
| } | |
| totalBytes.setAll(segmentOffset, noteBytes); | |
| return totalBytes; | |
| } | |
| void main() { | |
| late Directory tempDir; | |
| setUp(() async { | |
| tempDir = await Directory.systemTemp.createTemp('dart2native_elf_test_'); | |
| }); | |
| tearDown(() async { | |
| await deleteDirectory(tempDir); | |
| }); | |
| for (final is64Bit in [false, true]) { | |
| final label = is64Bit ? 'ELF64' : 'ELF32'; | |
| test('$label patches .note.dart.cli after preceding note', () async { | |
| final inputPath = path.join(tempDir.path, 'runtime'); | |
| final outputPath = path.join(tempDir.path, 'launcher'); | |
| await File(inputPath).writeAsBytes( | |
| _buildSyntheticElf( | |
| is64Bit: is64Bit, | |
| segmentAlign: is64Bit ? 8 : 4, | |
| includePrecedingNote: true, | |
| ), | |
| ); | |
| expect(hasCliBundleNote(inputPath), isTrue); | |
| const fallback = '../lib/libdartaotmy_tool.so'; | |
| await writeCliBundleExecutable( | |
| inputPath, | |
| outputPath, | |
| fallbackSnapshotRelativePath: fallback, | |
| ); | |
| expect(hasCliBundleNote(outputPath), isTrue); | |
| final patched = await File(outputPath).readAsBytes(); | |
| final fallbackBytes = utf8.encode(fallback); | |
| // Locate the patched descriptor via its 8-byte magic header. | |
| var magicIndex = -1; | |
| for (var i = 0; i <= patched.length - 8; i++) { | |
| if (patched[i] == 0xdc && | |
| patched[i + 1] == 0xdc && | |
| patched[i + 2] == 0x44 && | |
| patched[i + 3] == 0x43) { | |
| magicIndex = i; | |
| break; | |
| } | |
| } | |
| expect(magicIndex, greaterThan(0)); | |
| expect(patched[magicIndex + 8], 1); // enabled | |
| final storedLen = ByteData.sublistView( | |
| patched, | |
| magicIndex + 14, | |
| magicIndex + 16, | |
| ).getUint16(0, Endian.little); | |
| expect(storedLen, fallbackBytes.length); | |
| expect( | |
| patched.sublist(magicIndex + 16, magicIndex + 16 + storedLen), | |
| fallbackBytes, | |
| ); | |
| expect(patched[magicIndex + 16 + storedLen], 0); | |
| }); | |
| } | |
| test('rejects missing, mismatched, or truncated ELF inputs', () async { | |
| expect(hasCliBundleNote(path.join(tempDir.path, 'missing')), isFalse); | |
| final wrongOwnerPath = path.join(tempDir.path, 'wrong_owner'); | |
| await File(wrongOwnerPath).writeAsBytes( | |
| _buildSyntheticElf(is64Bit: true, ownerName: 'Other.Note1'), | |
| ); | |
| expect(hasCliBundleNote(wrongOwnerPath), isFalse); | |
| final truncatedPhdrPath = path.join(tempDir.path, 'truncated_phdr'); | |
| await File(truncatedPhdrPath).writeAsBytes( | |
| _buildSyntheticElf(is64Bit: true, overrideProgramHeaderCount: 99), | |
| ); | |
| expect(hasCliBundleNote(truncatedPhdrPath), isFalse); | |
| await expectLater( | |
| writeCliBundleExecutable( | |
| wrongOwnerPath, | |
| path.join(tempDir.path, 'out'), | |
| ), | |
| throwsA(isA<StateError>()), | |
| ); | |
| }); | |
| test('validates fallbackSnapshotRelativePath', () async { | |
| final runtimePath = path.join(tempDir.path, 'runtime'); | |
| final outPath = path.join(tempDir.path, 'out'); | |
| await File(runtimePath).writeAsBytes(_buildSyntheticElf(is64Bit: true)); | |
| await expectLater( | |
| writeCliBundleExecutable( | |
| runtimePath, | |
| outPath, | |
| fallbackSnapshotRelativePath: 'a' * 512, | |
| ), | |
| throwsA(isA<ArgumentError>()), | |
| ); | |
| await expectLater( | |
| writeCliBundleExecutable( | |
| runtimePath, | |
| outPath, | |
| fallbackSnapshotRelativePath: 'bad\x00path', | |
| ), | |
| throwsA(isA<ArgumentError>()), | |
| ); | |
| }); | |
| } |