[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 <sstrickl@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Tess Strickland 2022-12-14 20:16:57 +00:00 committed by Commit Queue
parent ce99386077
commit bd81c8bc06
2 changed files with 13 additions and 23 deletions

View file

@ -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

View file

@ -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;
}