-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid a temporary file when processing macOS fat archives #105221
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ use tempfile::Builder as TempFileBuilder; | |
|
||
use std::error::Error; | ||
use std::fs::File; | ||
use std::io::{self, Write}; | ||
use std::io; | ||
use std::path::{Path, PathBuf}; | ||
|
||
// Re-exporting for rustc_codegen_llvm::back::archive | ||
|
@@ -116,51 +116,42 @@ impl<'a> ArArchiveBuilder<'a> { | |
} | ||
} | ||
|
||
fn try_filter_fat_archs( | ||
fn try_filter_fat_archs<'a>( | ||
archs: object::read::Result<&[impl FatArch]>, | ||
target_arch: object::Architecture, | ||
archive_path: &Path, | ||
archive_map_data: &[u8], | ||
) -> io::Result<Option<PathBuf>> { | ||
archive_map_data: &'a [u8], | ||
) -> io::Result<Option<(&'a [u8], u64)>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This commit LGTM. |
||
let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; | ||
|
||
let desired = match archs.iter().filter(|a| a.architecture() == target_arch).next() { | ||
Some(a) => a, | ||
None => return Ok(None), | ||
}; | ||
|
||
let (mut new_f, extracted_path) = tempfile::Builder::new() | ||
.suffix(archive_path.file_name().unwrap()) | ||
.tempfile()? | ||
.keep() | ||
.unwrap(); | ||
|
||
new_f.write_all( | ||
Ok(Some(( | ||
desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?, | ||
)?; | ||
|
||
Ok(Some(extracted_path)) | ||
desired.offset().into(), | ||
))) | ||
} | ||
|
||
pub fn try_extract_macho_fat_archive( | ||
pub fn try_extract_macho_fat_archive<'a>( | ||
sess: &Session, | ||
archive_path: &Path, | ||
) -> io::Result<Option<PathBuf>> { | ||
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? }; | ||
archive_bytes: &'a [u8], | ||
) -> io::Result<Option<(&'a [u8], u64)>> { | ||
let target_arch = match sess.target.arch.as_ref() { | ||
"aarch64" => object::Architecture::Aarch64, | ||
"x86_64" => object::Architecture::X86_64, | ||
_ => return Ok(None), | ||
}; | ||
|
||
match object::macho::FatHeader::parse(&*archive_map) { | ||
match object::macho::FatHeader::parse(archive_bytes) { | ||
Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC => { | ||
let archs = object::macho::FatHeader::parse_arch32(&*archive_map); | ||
try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map) | ||
let archs = object::macho::FatHeader::parse_arch32(archive_bytes); | ||
try_filter_fat_archs(archs, target_arch, archive_bytes) | ||
} | ||
Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC_64 => { | ||
let archs = object::macho::FatHeader::parse_arch64(&*archive_map); | ||
try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map) | ||
let archs = object::macho::FatHeader::parse_arch64(archive_bytes); | ||
try_filter_fat_archs(archs, target_arch, archive_bytes) | ||
} | ||
// Not a FatHeader at all, just return None. | ||
_ => Ok(None), | ||
|
@@ -173,21 +164,24 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { | |
archive_path: &Path, | ||
mut skip: Box<dyn FnMut(&str) -> bool + 'static>, | ||
) -> io::Result<()> { | ||
let mut archive_path = archive_path.to_path_buf(); | ||
if self.sess.target.llvm_target.contains("-apple-macosx") { | ||
if let Some(new_archive_path) = | ||
try_extract_macho_fat_archive(&self.sess, &archive_path)? | ||
{ | ||
archive_path = new_archive_path | ||
} | ||
} | ||
|
||
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? }; | ||
if self.src_archives.iter().any(|archive| archive.0 == archive_path) { | ||
return Ok(()); | ||
} | ||
|
||
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? }; | ||
let archive = ArchiveFile::parse(&*archive_map) | ||
let (archive_bytes, offset) = if self.sess.target.llvm_target.contains("-apple-macosx") { | ||
if let Some((sub_archive, archive_offset)) = | ||
try_extract_macho_fat_archive(&self.sess, &*archive_map)? | ||
{ | ||
(sub_archive, Some(archive_offset)) | ||
} else { | ||
(&*archive_map, None) | ||
} | ||
} else { | ||
(&*archive_map, None) | ||
}; | ||
|
||
let archive = ArchiveFile::parse(&*archive_bytes) | ||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; | ||
let archive_index = self.src_archives.len(); | ||
|
||
|
@@ -196,9 +190,13 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { | |
let file_name = String::from_utf8(entry.name().to_vec()) | ||
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; | ||
if !skip(&file_name) { | ||
let mut range = entry.file_range(); | ||
if let Some(offset) = offset { | ||
range.0 += offset; | ||
} | ||
self.entries.push(( | ||
file_name.into_bytes(), | ||
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() }, | ||
ArchiveEntry::FromArchive { archive_index, file_range: range }, | ||
)); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should keep this for now in case we need to revert back from the rust rewrite to the original version on macOS due to a bug in the rust rewrite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The LlvmArchiveWriter doesn't have a clean place to add this -- it doesn't track file_range, it just has a path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Would you mind waiting a week or so to make sure no issues pop up? r=me after that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's fine