Skip to content

Commit 8a09d7a

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/bytecode] Remove alignment of sections in bytecode
Previously, before switching to compact encoding of bytecode instructions, we had to align bytecode instructions by 4. Since that time sections of bytecode binary were aligned. However, this alignment doesn't work well in AOT mode if dill file contains both bytecode and AST, as other metadata (such as obfuscation prohibitions) may precede bytecode metadata and may make the whole bytecode binary misaligned. This results in incorrect calculation of section offsets, failed assertion assert(writer.offset - start == section.offset); and VM may fail to load bytecode binary with error bytecode_reader.cc: 1368: error: unreachable code In order to fix these problems, extra alignment of bytecode sections is removed. Change-Id: Iaa3b7c46999615aaa446781a7018db409261ead8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/123686 Reviewed-by: Régis Crelier <regis@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
1 parent d9b069b commit 8a09d7a

File tree

7 files changed

+99
-20
lines changed

7 files changed

+99
-20
lines changed

pkg/vm/lib/bytecode/bytecode_serialization.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class BufferedWriter {
4141
final StringWriter stringWriter;
4242
final ObjectWriter objectWriter;
4343
final LinkWriter linkWriter;
44-
final int baseOffset;
4544

4645
// Prefix of the data stored in this writer.
4746
// List of pairs Uint8List buffer, int length.
@@ -53,9 +52,8 @@ class BufferedWriter {
5352
int _currentLength = 0;
5453
int _nextBufferSize = initialSize << 1;
5554

56-
BufferedWriter(
57-
this.formatVersion, this.stringWriter, this.objectWriter, this.linkWriter,
58-
{this.baseOffset: 0});
55+
BufferedWriter(this.formatVersion, this.stringWriter, this.objectWriter,
56+
this.linkWriter);
5957

6058
factory BufferedWriter.fromWriter(BufferedWriter writer) =>
6159
new BufferedWriter(writer.formatVersion, writer.stringWriter,
@@ -266,18 +264,6 @@ class BufferedWriter {
266264
final offset = linkWriter.getOffset(target);
267265
writePackedUInt30(offset);
268266
}
269-
270-
void align(int alignment) {
271-
assert(alignment & (alignment - 1) == 0);
272-
int offs = baseOffset + offset;
273-
int padding = ((offs + alignment - 1) & -alignment) - offs;
274-
if (_currentLength > _currentBuffer.length - padding) {
275-
_grow();
276-
}
277-
for (int i = 0; i < padding; ++i) {
278-
_addByte(0);
279-
}
280-
}
281267
}
282268

283269
class BufferedReader {

pkg/vm/lib/bytecode/declarations.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,6 @@ class Component {
13651365
int offset = headerSize;
13661366
for (var section in sections) {
13671367
if (section.writer != null) {
1368-
offset = (offset + sectionAlignment - 1) & ~(sectionAlignment - 1);
13691368
section.offset = offset;
13701369
offset += section.size;
13711370
} else {
@@ -1384,7 +1383,6 @@ class Component {
13841383
assert(writer.offset - start == headerSize);
13851384
for (var section in sections) {
13861385
if (section.writer != null) {
1387-
writer.align(sectionAlignment);
13881386
assert(writer.offset - start == section.offset);
13891387
writer.appendWriter(section.writer);
13901388
}

pkg/vm/lib/metadata/bytecode.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class BytecodeMetadataRepository extends MetadataRepository<BytecodeMetadata> {
5353
bytecodeComponent.version,
5454
bytecodeComponent.stringTable,
5555
bytecodeComponent.objectTable,
56-
linkWriter,
57-
baseOffset: sink.getBufferOffset());
56+
linkWriter);
5857
metadata.write(writer);
5958
writer.writeContentsToBinarySink(sink);
6059
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2019, 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+
// OtherResources=bytecode_with_ast_in_aot_test_body1.dart
6+
// OtherResources=bytecode_with_ast_in_aot_test_body2.dart
7+
8+
// Tests that gen_kernel is able to produce dill file with both bytecode
9+
// and AST in AOT mode, and gen_snapshot is able to consume them.
10+
// Two test cases are only different in number of entry points, so
11+
// obfuscation prohibitions metadata has different size, causing
12+
// different alignment of bytecode metadata.
13+
14+
import 'dart:io' show Platform;
15+
16+
import 'package:path/path.dart' as path;
17+
import 'snapshot_test_helper.dart';
18+
19+
testAOTOnBytecodeWithAST(String temp, String source) async {
20+
final sourcePath = Platform.script.resolve(source).toFilePath();
21+
final dillPath = path.join(temp, '${source}.dill');
22+
final snapshotPath = path.join(temp, '${source}.so');
23+
24+
final genKernelResult = await runGenKernel('BUILD DILL FILE', [
25+
'--aot',
26+
'--gen-bytecode',
27+
'--no-drop-ast',
28+
'--output=$dillPath',
29+
sourcePath,
30+
]);
31+
print(genKernelResult);
32+
final genSnapshotResult = await runGenSnapshot('GENERATE SNAPSHOT', [
33+
'--use-bytecode-compiler',
34+
'--snapshot-kind=app-aot-elf',
35+
'--elf=$snapshotPath',
36+
dillPath,
37+
]);
38+
print(genSnapshotResult);
39+
final runResult =
40+
await runBinary('RUN SNAPSHOT', dartPrecompiledRuntime, [snapshotPath]);
41+
expectOutput("OK", runResult);
42+
}
43+
44+
main() async {
45+
await withTempDir((String temp) async {
46+
await testAOTOnBytecodeWithAST(
47+
temp, 'bytecode_with_ast_in_aot_test_body1.dart');
48+
await testAOTOnBytecodeWithAST(
49+
temp, 'bytecode_with_ast_in_aot_test_body2.dart');
50+
});
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2019, 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+
// Test case for bytecode_with_ast_in_aot_test.dart.
6+
// Contains 1 entry point.
7+
8+
import 'package:expect/expect.dart';
9+
10+
class A {
11+
// Make sure obfuscation prohibitions metadata is generated.
12+
@pragma('vm:entry-point')
13+
void foofoo1() {}
14+
}
15+
16+
main() {
17+
new A();
18+
print('OK');
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2019, 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+
// Test case for bytecode_with_ast_in_aot_test.dart.
6+
// Contains 2 entry points.
7+
8+
import 'package:expect/expect.dart';
9+
10+
class A {
11+
// Make sure obfuscation prohibitions metadata is generated.
12+
@pragma('vm:entry-point')
13+
void foofoo1() {}
14+
15+
// One more entry for obfuscation prohibitions metadata,
16+
// raises the chances that subsequent bytecode metadata is misaligned.
17+
@pragma('vm:entry-point')
18+
void foofoo2() {}
19+
}
20+
21+
main() {
22+
new A();
23+
print('OK');
24+
}

runtime/tests/vm/vm.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dart/transferable_throws_oom_test: SkipByDesign # This test tries to allocate to
2424

2525
[ $builder_tag == crossword ]
2626
dart/emit_aot_size_info_flag_test: SkipByDesign # The test itself cannot determine the location of gen_snapshot (only tools/test.py knows where it is).
27+
dart/bytecode_with_ast_in_aot_test: SkipByDesign # The test doesn't know location of cross-platform gen_snapshot
2728

2829
[ $builder_tag == obfuscated ]
2930
dart/causal_stacks/*: SkipByDesign # Asserts exact stacktrace output.
@@ -244,6 +245,7 @@ dart/redirection_type_shuffling_test/none: RuntimeError
244245
[ $mode == debug || $runtime != dart_precompiled || $system == android ]
245246
dart/emit_aot_size_info_flag_test: SkipByDesign # This test is for VM AOT only and is quite slow (so we don't run it in debug mode).
246247
dart/use_bare_instructions_flag_test: SkipByDesign # This test is for VM AOT only and is quite slow (so we don't run it in debug mode).
248+
dart/bytecode_with_ast_in_aot_test: SkipByDesign # This test is for VM AOT only and is quite slow (so we don't run it in debug mode).
247249

248250
# It makes no sense to run any test that uses spawnURI under the simulator
249251
# as that would involve running CFE (the front end) in simulator mode

0 commit comments

Comments
 (0)