Skip to content

Commit

Permalink
[pkg/dart2native] Add negative padding check to MachO writer.
Browse files Browse the repository at this point in the history
This catches an inadvertent partial overwrite of the first section
in the `dartaotruntime` executable that happens when there's not
enough padding after the headers. The first section is a text section,
so unless there are no calls to code in this overwritten portion, there
are no runtime failures when this happens.

However, having a MachO section which has a file offset within the
headers is checked by MachO verification code within the `codesign`
utility when run with the `-f` flag, and thus caused failures in
certain tests and builds. Note that we only use that flag when
`codesign` does not have the `linker-signed` option, which is for
MacOS versions prior to 12.0.

Example failure with this change when this case occurs (e.g., prior to
the recent clang revert):
```
$ xcodebuild/ReleaseARM64/dart-sdk/bin/dart compile exe -o test test.dart
Info: Compiling with sound null safety
Error: AOT compilation failed
FormatException: The MachO header overlaps with the first 120 bytes of the section contents
```

Change-Id: Ib27db910777f61b90f162f7a0bcfa4ba6592a5a0
Bug: #49783
Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-nnbd-mac-release-arm64-try,vm-kernel-precomp-mac-product-x64-try,dart-sdk-mac-arm64-try,dart-sdk-mac-try,pkg-mac-release-arm64-try,pkg-mac-release-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256264
Reviewed-by: Daco Harkes <[email protected]>
Reviewed-by: Martin Kustermann <[email protected]>
Commit-Queue: Tess Strickland <[email protected]>
  • Loading branch information
sstrickl authored and Commit Bot committed Aug 25, 2022
1 parent ddff459 commit 161f8fd
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/dart2native/lib/macho_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ class MachOFile {

// Pad the header according to the offset.
final int paddingAmount = headerMaxOffset - stream.positionSync();
if (paddingAmount > 0) {
if (paddingAmount < 0) {
throw FormatException(
"The MachO header overlaps with the first ${-paddingAmount} bytes of "
"the section contents");
} else if (paddingAmount > 0) {
stream.writeFromSync(List.filled(paddingAmount, 0));
}
}
Expand Down

0 comments on commit 161f8fd

Please sign in to comment.