|
| 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 | +import 'dart:async'; |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | +import 'dart:typed_data'; |
| 9 | + |
| 10 | +import 'package:path/path.dart' as path; |
| 11 | +import 'package:expect/expect.dart'; |
| 12 | + |
| 13 | +import 'snapshot_test_helper.dart'; |
| 14 | + |
| 15 | +// Keep in sync with pkg/kernel/lib/binary/tag.dart: |
| 16 | +const tagComponentFile = [0x90, 0xAB, 0xCD, 0xEF]; |
| 17 | +const tagBinaryFormatVersion = [0x00, 0x00, 0x00, 43]; |
| 18 | + |
| 19 | +Future<void> main(List<String> args) async { |
| 20 | + if (args.length == 1 && args[0] == '--child') { |
| 21 | + print('Hello, SDK Hash!'); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + final String sourcePath = |
| 26 | + path.join('runtime', 'tests', 'vm', 'dart_2', 'sdk_hash_test.dart'); |
| 27 | + |
| 28 | + await withTempDir((String tmp) async { |
| 29 | + final String dillPath = path.join(tmp, 'test.dill'); |
| 30 | + |
| 31 | + { |
| 32 | + final result = await Process.run(dart, [ |
| 33 | + genKernel, |
| 34 | + '--platform', |
| 35 | + platformDill, |
| 36 | + '-o', |
| 37 | + dillPath, |
| 38 | + sourcePath, |
| 39 | + ]); |
| 40 | + Expect.equals('', result.stderr); |
| 41 | + Expect.equals(0, result.exitCode); |
| 42 | + Expect.equals('', result.stdout); |
| 43 | + } |
| 44 | + |
| 45 | + { |
| 46 | + final result = await Process.run(dart, [dillPath, '--child']); |
| 47 | + Expect.equals('', result.stderr); |
| 48 | + Expect.equals(0, result.exitCode); |
| 49 | + Expect.equals('Hello, SDK Hash!', result.stdout.trim()); |
| 50 | + } |
| 51 | + |
| 52 | + // Invalidate the SDK hash in the kernel dill: |
| 53 | + { |
| 54 | + final myFile = File(dillPath); |
| 55 | + Uint8List bytes = myFile.readAsBytesSync(); |
| 56 | + // The SDK Hash is located after the ComponentFile and BinaryFormatVersion |
| 57 | + // tags (both UInt32). |
| 58 | + Expect.listEquals(tagComponentFile, bytes.sublist(0, 4)); |
| 59 | + Expect.listEquals(tagBinaryFormatVersion, bytes.sublist(4, 8)); |
| 60 | + // Flip the first byte in the hash: |
| 61 | + bytes[8] ^= bytes[8]; |
| 62 | + myFile.writeAsBytesSync(bytes); |
| 63 | + } |
| 64 | + |
| 65 | + { |
| 66 | + final result = await Process.run(dart, [dillPath, '--child']); |
| 67 | + Expect.equals( |
| 68 | + 'Can\'t load Kernel binary: Invalid SDK hash.', result.stderr.trim()); |
| 69 | + Expect.equals(253, result.exitCode); |
| 70 | + Expect.equals('', result.stdout); |
| 71 | + } |
| 72 | + |
| 73 | + // Zero out the SDK hash in the kernel dill to disable the check: |
| 74 | + { |
| 75 | + final myFile = File(dillPath); |
| 76 | + Uint8List bytes = myFile.readAsBytesSync(); |
| 77 | + bytes.setRange(8, 18, ascii.encode('0000000000')); |
| 78 | + myFile.writeAsBytesSync(bytes); |
| 79 | + } |
| 80 | + |
| 81 | + { |
| 82 | + final result = await Process.run(dart, [dillPath, '--child']); |
| 83 | + Expect.equals('', result.stderr); |
| 84 | + Expect.equals(0, result.exitCode); |
| 85 | + Expect.equals('Hello, SDK Hash!', result.stdout.trim()); |
| 86 | + } |
| 87 | + }); |
| 88 | +} |
0 commit comments