Skip to content

Commit bd81c8b

Browse files
sstricklCommit Queue
authored and
Commit Queue
committed
[pkg/dart2native] Add checks to avoid MachO-related regressions.
Now that 9df6656 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: #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 <[email protected]> Reviewed-by: Daco Harkes <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent ce99386 commit bd81c8b

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

pkg/dart2native/lib/dart2native_macho.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,14 @@ Future writeAppendedMachOExecutable(
9494
// First, write the new headers.
9595
outputHeaders.writeSync(output);
9696
// If the newer headers are smaller, add appropriate padding to fit.
97-
//
98-
// TODO(49783): Once linker flags are in place in g3, this check should always
99-
// succeed and should be removed.
100-
if (outputHeaders.size <= aotRuntimeHeaders.size) {
101-
addPadding(outputHeaders.size, aotRuntimeHeaders.size);
102-
}
103-
// TODO(49783): Once linker flags are in place in g3, this should always be
104-
// aotRuntimeHeaders.size, but for now allow for the possibility of
105-
// overwriting part of the original contents with the header as before.
106-
final originalStart = max(aotRuntimeHeaders.size, outputHeaders.size);
97+
addPadding(outputHeaders.size, aotRuntimeHeaders.size);
10798

10899
// Now write the original contents from the header to the __LINKEDIT segment
109100
// contents.
110101
final aotRuntimeStream = await aotRuntimeFile.open();
111-
await aotRuntimeStream.setPosition(originalStart);
102+
await aotRuntimeStream.setPosition(aotRuntimeHeaders.size);
112103
await pipeStream(aotRuntimeStream, output,
113-
numToWrite: oldLinkEdit.fileOffset - originalStart);
104+
numToWrite: oldLinkEdit.fileOffset - aotRuntimeHeaders.size);
114105

115106
// Now insert the snapshot contents at this position in the file.
116107
// There may be additional padding needed between the old __LINKEDIT file

pkg/dart2native/lib/macho.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,9 @@ class MachOFile {
15271527
}
15281528

15291529
final reserved = reservedSegment;
1530-
// TODO(49783): Once linker flags are in place in g3, we should throw a
1531-
// FormatException if the segment used to reserve header space is not found.
1530+
if (reserved == null) {
1531+
throw FormatException("$reservedSegmentName segment not found");
1532+
}
15321533

15331534
final linkedit = linkEditSegment;
15341535
if (linkedit == null) {
@@ -1549,13 +1550,9 @@ class MachOFile {
15491550
header.cpu,
15501551
header.machine,
15511552
header.type,
1552-
// If the reserved section exists, we remove it and replace it with
1553-
// the note.
1554-
//
1555-
// TODO(49783): Once linker flags are in place in g3, reserved should
1556-
// never be null.
1557-
header.loadCommandsCount + (reserved == null ? 1 : 0),
1558-
header.loadCommandsSize - (reserved?.size ?? 0) + note.size,
1553+
// We remove the reserved section and replace it with the note.
1554+
header.loadCommandsCount,
1555+
header.loadCommandsSize - reserved.size + note.size,
15591556
header.flags,
15601557
header.reserved);
15611558

@@ -1582,8 +1579,10 @@ class MachOFile {
15821579

15831580
final newFile = MachOFile._(newHeader, newCommands, hasCodeSignature);
15841581

1585-
// TODO(49783): Once linker flags are in place in g3, we should throw a
1586-
// FormatException if [newFile.size] is greater than [size].
1582+
if (newFile.size > size) {
1583+
throw FormatException("Cannot add new note load command to header: "
1584+
"new size ${newFile.size} > the old size $size)");
1585+
}
15871586

15881587
return newFile;
15891588
}

0 commit comments

Comments
 (0)