From 488ccfdf70762127bd21f36492748d407b3e9fc6 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 18 Jul 2021 10:48:03 +0800 Subject: [PATCH 1/2] formula_cellar_checks: check keg for mismatched arches There have been a few instances I've noticed that we've been silently installing binaries built for x86_64 on ARM. There's probably more that I haven't found yet, so it seems useful to check this with an audit. --- Library/Homebrew/formula_cellar_checks.rb | 17 +++++++++++++++++ Library/Homebrew/os/mac/mach.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 77e6050314..187c055ff4 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -314,6 +314,22 @@ module FormulaCellarChecks "No `cpuid` instruction detected. #{formula} should not use `ENV.runtime_cpu_detection`." end + def check_binary_arches(formula) + return unless formula.prefix.directory? + + keg = Keg.new(formula.prefix) + mismatches = keg.binary_executable_or_library_files.reject do |file| + file.arch == Hardware::CPU.arch + end + return if mismatches.empty? + + <<~EOS + Binaries built for a non-native architecture were installed into #{formula}'s prefix. + The offending files are: + #{mismatches * "\n "} + EOS + end + def audit_installed @new_formula ||= false @@ -334,6 +350,7 @@ module FormulaCellarChecks problem_if_output(check_plist(formula.prefix, formula.plist)) problem_if_output(check_python_symlinks(formula.name, formula.keg_only?)) problem_if_output(check_cpuid_instruction(formula)) + problem_if_output(check_binary_arches(formula)) end alias generic_audit_installed audit_installed diff --git a/Library/Homebrew/os/mac/mach.rb b/Library/Homebrew/os/mac/mach.rb index adae8e32f6..0b4d8a0c63 100644 --- a/Library/Homebrew/os/mac/mach.rb +++ b/Library/Homebrew/os/mac/mach.rb @@ -29,7 +29,7 @@ module MachOShim machos.each do |m| arch = case m.cputype - when :x86_64, :i386, :ppc64 then m.cputype + when :x86_64, :i386, :ppc64, :arm64, :arm then m.cputype when :ppc then :ppc7400 else :dunno end From d696250ddbeb347866e5404f74c8abf2002183de Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 18 Jul 2021 16:55:57 +0800 Subject: [PATCH 2/2] Fix ELF architecture offset for x86_64 The current offset results in ELF binaries returning an `#arch` of `:dunno`. Also, skip the `check_binary_arches` audit on the generic OS. --- Library/Homebrew/formula_cellar_checks.rb | 2 ++ Library/Homebrew/os/linux/elf.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 187c055ff4..cff0baa647 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -316,6 +316,8 @@ module FormulaCellarChecks def check_binary_arches(formula) return unless formula.prefix.directory? + # There is no `binary_executable_or_library_files` method for the generic OS + return if !OS.mac? && !OS.linux? keg = Keg.new(formula.prefix) mismatches = keg.binary_executable_or_library_files.reject do |file| diff --git a/Library/Homebrew/os/linux/elf.rb b/Library/Homebrew/os/linux/elf.rb index f31a40f806..77bf6f4139 100644 --- a/Library/Homebrew/os/linux/elf.rb +++ b/Library/Homebrew/os/linux/elf.rb @@ -33,7 +33,7 @@ module ELFShim private_constant :ARCHITECTURE_POWERPC ARCHITECTURE_ARM = 0x28 private_constant :ARCHITECTURE_ARM - ARCHITECTURE_X86_64 = 0x62 + ARCHITECTURE_X86_64 = 0x3E private_constant :ARCHITECTURE_X86_64 ARCHITECTURE_AARCH64 = 0xB7 private_constant :ARCHITECTURE_AARCH64