From bd81c8bc06cdf156905e35b662c642e73994ff0c Mon Sep 17 00:00:00 2001 From: Tess Strickland Date: Wed, 14 Dec 2022 20:16:57 +0000 Subject: [PATCH] [pkg/dart2native] Add checks to avoid MachO-related regressions. Now that 9df6656aa9 has appropriately propagated and the build rules in google3 have been updated, re-add checks to ensure that the empty section used to pad the original header exists and that the new header is smaller than the old header. Bug: https://github.com/dart-lang/sdk/issues/49783 Change-Id: I5b8b31bf9edf4814686eb7928457a9a7d53c0727 Cq-Include-Trybots: luci.dart.try:dart-sdk-mac-try,dart-sdk-mac-arm64-try,pkg-mac-release-arm64-try,pkg-mac-release-try,vm-kernel-mac-release-arm64-try,vm-kernel-mac-release-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275622 Commit-Queue: Tess Strickland Reviewed-by: Daco Harkes Reviewed-by: Alexander Markov --- pkg/dart2native/lib/dart2native_macho.dart | 15 +++------------ pkg/dart2native/lib/macho.dart | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/pkg/dart2native/lib/dart2native_macho.dart b/pkg/dart2native/lib/dart2native_macho.dart index fe240c352cf..6cd0cbe15ca 100644 --- a/pkg/dart2native/lib/dart2native_macho.dart +++ b/pkg/dart2native/lib/dart2native_macho.dart @@ -94,23 +94,14 @@ Future writeAppendedMachOExecutable( // First, write the new headers. outputHeaders.writeSync(output); // If the newer headers are smaller, add appropriate padding to fit. - // - // TODO(49783): Once linker flags are in place in g3, this check should always - // succeed and should be removed. - if (outputHeaders.size <= aotRuntimeHeaders.size) { - addPadding(outputHeaders.size, aotRuntimeHeaders.size); - } - // TODO(49783): Once linker flags are in place in g3, this should always be - // aotRuntimeHeaders.size, but for now allow for the possibility of - // overwriting part of the original contents with the header as before. - final originalStart = max(aotRuntimeHeaders.size, outputHeaders.size); + addPadding(outputHeaders.size, aotRuntimeHeaders.size); // Now write the original contents from the header to the __LINKEDIT segment // contents. final aotRuntimeStream = await aotRuntimeFile.open(); - await aotRuntimeStream.setPosition(originalStart); + await aotRuntimeStream.setPosition(aotRuntimeHeaders.size); await pipeStream(aotRuntimeStream, output, - numToWrite: oldLinkEdit.fileOffset - originalStart); + numToWrite: oldLinkEdit.fileOffset - aotRuntimeHeaders.size); // Now insert the snapshot contents at this position in the file. // There may be additional padding needed between the old __LINKEDIT file diff --git a/pkg/dart2native/lib/macho.dart b/pkg/dart2native/lib/macho.dart index b408b8a4fca..765f86b67fc 100644 --- a/pkg/dart2native/lib/macho.dart +++ b/pkg/dart2native/lib/macho.dart @@ -1527,8 +1527,9 @@ class MachOFile { } final reserved = reservedSegment; - // TODO(49783): Once linker flags are in place in g3, we should throw a - // FormatException if the segment used to reserve header space is not found. + if (reserved == null) { + throw FormatException("$reservedSegmentName segment not found"); + } final linkedit = linkEditSegment; if (linkedit == null) { @@ -1549,13 +1550,9 @@ class MachOFile { header.cpu, header.machine, header.type, - // If the reserved section exists, we remove it and replace it with - // the note. - // - // TODO(49783): Once linker flags are in place in g3, reserved should - // never be null. - header.loadCommandsCount + (reserved == null ? 1 : 0), - header.loadCommandsSize - (reserved?.size ?? 0) + note.size, + // We remove the reserved section and replace it with the note. + header.loadCommandsCount, + header.loadCommandsSize - reserved.size + note.size, header.flags, header.reserved); @@ -1582,8 +1579,10 @@ class MachOFile { final newFile = MachOFile._(newHeader, newCommands, hasCodeSignature); - // TODO(49783): Once linker flags are in place in g3, we should throw a - // FormatException if [newFile.size] is greater than [size]. + if (newFile.size > size) { + throw FormatException("Cannot add new note load command to header: " + "new size ${newFile.size} > the old size $size)"); + } return newFile; }