Skip to content

Commit eff0832

Browse files
sstricklCommit Queue
authored andcommitted
[dart2native] Remove unnecessary error in PE creation.
After writing the new PE header and original section contents of the dartaotruntime PE executable, pad to the expected file offset of the new section being added, not just to file alignment. Also fixes a case where the new section's file offset returned from appendSnapshotAndWrite could be incorrect if the header size changed, which causes the file offsets for all sections, including the new one, to be updated. TEST=pkg/dartdev/test/commands/compile_test.dart Change-Id: I060176f6771e9138f8d1a99590d455fc76bb573e Cq-Include-Trybots: luci.dart.try:pkg-win-release-try,pkg-win-release-arm64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410720 Reviewed-by: Slava Egorov <[email protected]> Commit-Queue: Tess Strickland <[email protected]>
1 parent 2c62efb commit eff0832

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

pkg/dart2native/lib/dart2native_pe.dart

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,8 @@ class CoffHeaders {
218218

219219
// Add a section header for the new "snapshot" section with the given length.
220220
//
221-
// Returns offset at which the section is expected to be located in the
222-
// file.
223-
int addSnapshotSectionHeader(int length) {
221+
// Returns the new section header.
222+
CoffSectionHeader addSnapshotSectionHeader(int length) {
224223
final oldHeadersSize = optionalHeader.headersSize;
225224
final address =
226225
align(sectionTable.addressEnd, optionalHeader.sectionAlignment);
@@ -270,7 +269,7 @@ class CoffHeaders {
270269
newHeader.virtualAddress + newHeader.virtualSize,
271270
optionalHeader.sectionAlignment);
272271

273-
return offset;
272+
return newHeader;
274273
}
275274

276275
Future<void> write(RandomAccessFile output) async {
@@ -311,36 +310,40 @@ class PortableExecutable {
311310
source, headers, fileHeaderOffset, sectionContentsOffset);
312311
}
313312

314-
Future<void> _fileAlignSectionEnd(RandomAccessFile output) async {
315-
final current = await output.position();
316-
final padding =
317-
align(current, headers.optionalHeader.fileAlignment) - current;
318-
await output.writeFrom(Uint8List(padding));
319-
}
320-
321313
Future<void> appendSnapshotAndWrite(File output, File snapshot) async {
322314
final stream = await output.open(mode: FileMode.write);
323315
// Write MS-DOS stub.
324316
await stream.writeFrom(source, 0, sourceFileHeaderOffset);
325317
// Write headers with additional snapshot section.
326318
final snapshotBytes = await snapshot.readAsBytes();
327319
final oldOffsetEnd = headers.sectionTable.offsetEnd;
328-
final expectedSnapshotOffset =
320+
final snapshotSectionHeader =
329321
headers.addSnapshotSectionHeader(snapshotBytes.length);
330322
await headers.write(stream);
331-
// Write original section contents with alignment padding.
323+
// Write original section contents.
332324
await stream.writeFrom(source, sourceSectionContentsOffset, oldOffsetEnd);
333-
await _fileAlignSectionEnd(stream);
325+
var currentOffset = await stream.position();
326+
// Pad the original contents to the file offset of the new section.
327+
final expectedSnapshotOffset = snapshotSectionHeader.fileOffset;
328+
if (currentOffset < expectedSnapshotOffset) {
329+
final padding = expectedSnapshotOffset - currentOffset;
330+
await stream.writeFrom(Uint8List(padding));
331+
currentOffset = await stream.position();
332+
}
334333
// Verify that snapshot section will start at the expected offset
335334
// and throw an error otherwise.
336-
final currentOffset = await stream.position();
337335
if (expectedSnapshotOffset != currentOffset) {
338336
throw StateError('Unexpected snapshot section offset: '
339337
'expected $expectedSnapshotOffset, got $currentOffset');
340338
}
341339
// Write snapshot with alignment padding.
342340
await stream.writeFrom(snapshotBytes);
343-
await _fileAlignSectionEnd(stream);
341+
currentOffset = await stream.position();
342+
final padding = align(currentOffset, headers.optionalHeader.fileAlignment) -
343+
currentOffset;
344+
if (padding > 0) {
345+
await stream.writeFrom(Uint8List(padding));
346+
}
344347
await stream.close();
345348
}
346349
}

0 commit comments

Comments
 (0)