Skip to content
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

Revert "Binary patching of build prefixes" #12870

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions Library/Homebrew/extend/os/mac/keg_relocate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,4 @@ def recursive_fgrep_args
# it's wrong. -O is a BSD-grep-only option.
"-lrO"
end

def egrep_args
grep_bin = "egrep"
grep_args = recursive_fgrep_args
[grep_bin, grep_args]
end

def codesign_patched_binary(binary_file)
apply_ad_hoc_signature(binary_file)
end
end
4 changes: 0 additions & 4 deletions Library/Homebrew/formula_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1230,10 +1230,6 @@ def pour
keg = Keg.new(formula.prefix)
skip_linkage = formula.bottle_specification.skip_relocation?
keg.replace_placeholders_with_locations tab.changed_files, skip_linkage: skip_linkage

return if formula.bottle_specification.skip_prefix_relocation?

keg.relocate_build_prefix(keg, Utils::Bottles.tag.default_prefix, HOMEBREW_PREFIX)
end

sig { params(output: T.nilable(String)).void }
Expand Down
84 changes: 6 additions & 78 deletions Library/Homebrew/keg_relocate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Keg
LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@"
PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@"
JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@"
BINARY_NULL_CHARACTER = "\x00"

class Relocation
extend T::Sig
Expand Down Expand Up @@ -164,49 +163,6 @@ def replace_text_in_files(relocation, files: nil)
changed_files
end

def relocate_build_prefix(keg, old_prefix, new_prefix)
# Find binaries which match prefix strings.
string_matches = Set.new
keg.each_unique_file_matching(old_prefix) do |file|
string_matches << file
end

binary_string_matches = Set.new
keg.each_unique_binary_file do |file|
binary_string_matches << file if string_matches.include?(file)
end

# Only consider string matches which are binary files with null bytes, and remove any
# matches which are sharballs found by text_files.
binary_string_matches -= text_files

# Split binary by null characters into array and substitute new cellar for old cellar.
# Null padding is added if the new string is too short.
binary_string_matches.each do |binary_file|
binary_file.ensure_writable do
binary = File.binread binary_file
puts "Replacing build prefix in: #{binary_file}"
binary_strings = binary.split(BINARY_NULL_CHARACTER)
match_indices = binary_strings.each_index.select { |i| binary_strings[i].include?(old_prefix) }

# Only perform substitution on strings which match prefix regex.
match_indices.each do |i|
s = binary_strings[i]
binary_strings[i] = s.gsub(old_prefix, new_prefix).ljust(s.size, BINARY_NULL_CHARACTER)
end

# Add back null padding at the end of the binary if needed.
patched_binary = binary_strings.join(BINARY_NULL_CHARACTER).ljust(binary.size, BINARY_NULL_CHARACTER)
if patched_binary.size != binary.size
raise "Patching failed! Original and patched binary sizes do not match."
end

binary_file.atomic_write patched_binary
end
codesign_patched_binary(binary_file)
end
end

def detect_cxx_stdlibs(_options = {})
[]
end
Expand All @@ -217,47 +173,19 @@ def recursive_fgrep_args
end
alias generic_recursive_fgrep_args recursive_fgrep_args

def egrep_args
grep_bin = "grep"
grep_args = recursive_fgrep_args
grep_args += "Pa"
[grep_bin, grep_args]
end
alias generic_egrep_args egrep_args

def each_unique_file(io)
hardlinks = Set.new

until io.eof?
file = Pathname.new(io.readline.chomp)
# Don't yield symlinks
next if file.symlink?

# Only yield a file if it has a unique inode.
# This makes sure we don't yield hardlinks.
yield file if hardlinks.add? file.stat.ino
end
end

def each_unique_file_matching(string)
Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
each_unique_file(io)
end
end
hardlinks = Set.new

def each_unique_binary_file
grep_bin, grep_args = egrep_args
until io.eof?
file = Pathname.new(io.readline.chomp)
next if file.symlink?

# An extra \ is needed for the null character when calling grep
Utils.popen_read(grep_bin, grep_args, "\#{BINARY_NULL_CHARACTER}", to_s) do |io|
each_unique_file(io)
yield file if hardlinks.add? file.stat.ino
end
end
end

def codesign_patched_binary(_binary_file)
[]
end

def lib
path/"lib"
end
Expand Down
32 changes: 1 addition & 31 deletions Library/Homebrew/software_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,6 @@ def compatible_locations?
@spec.compatible_locations?(tag: @tag)
end

# Should the build prefix be relocated?
def skip_prefix_relocation?
@spec.skip_prefix_relocation?(tag: @tag)
end

# Does the bottle need to be relocated?
def skip_relocation?
@spec.skip_relocation?(tag: @tag)
Expand Down Expand Up @@ -478,8 +473,6 @@ def root_url(val = nil, specs = {})
end

class BottleSpecification
RELOCATABLE_CELLARS = [:any, :any_skip_relocation].freeze

extend T::Sig

attr_rw :rebuild
Expand Down Expand Up @@ -520,30 +513,7 @@ def compatible_locations?(tag: Utils::Bottles.tag)
tag.default_cellar
end

return true if RELOCATABLE_CELLARS.include?(cellar)

prefix = Pathname(cellar).parent.to_s

cellar_relocatable = cellar.size >= HOMEBREW_CELLAR.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"]
prefix_relocatable = prefix.size >= HOMEBREW_PREFIX.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"]

compatible_cellar = cellar == HOMEBREW_CELLAR.to_s || cellar_relocatable
compatible_prefix = prefix == HOMEBREW_PREFIX.to_s || prefix_relocatable

compatible_cellar && compatible_prefix
end

# Should the build prefix for the {Bottle} this {BottleSpecification} belongs to be relocated?
sig { params(tag: Utils::Bottles::Tag).returns(T::Boolean) }
def skip_prefix_relocation?(tag: Utils::Bottles.tag)
spec = collector.specification_for(tag)
cellar = if spec.present?
spec.cellar
else
tag.default_cellar
end

return true if RELOCATABLE_CELLARS.include?(cellar)
return true if [:any, :any_skip_relocation].include?(cellar)

prefix = Pathname(cellar).parent.to_s

Expand Down