Skip to content

Commit a420053

Browse files
committed
[Auditor] Don't try to dlopen libraries for incompatible ISAs
1 parent 39b2307 commit a420053

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/Auditor.jl

+7-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ function audit(prefix::Prefix, src_name::AbstractString = "";
135135
shlib_files = filter(f -> startswith(f, prefix.path) && valid_library_path(f, platform), collapse_symlinks(bin_files))
136136

137137
for f in shlib_files
138-
# Inspect all shared library files for our platform (but only if we're
139-
# running native, don't try to load library files from other platforms)
140-
if platforms_match(platform, HostPlatform())
138+
# Always include microarchitecture in the host platform: we don't want to try and
139+
# dlopen a library built for an incompatible microarchitecture.
140+
hp = augment_microarchitecture!(HostPlatform())
141+
set_compare_strategy!(hp, "march", march_comparison_strategy)
142+
# Inspect all shared library files for our platform (but only if we're running
143+
# native, don't try to load library files from other platforms or incompatible ISAs)
144+
if platforms_match(platform, hp)
141145
if verbose
142146
@info("Checking shared library $(relpath(f, prefix.path))")
143147
end

src/auditor/instruction_set.jl

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using JSON
2+
using Base.BinaryPlatforms: arch_march_isa_mapping
3+
using Base.BinaryPlatforms.CPUID
24

35
## We start with definitions of instruction mnemonics, broken down by category:
46
const instruction_categories = JSON.parsefile(joinpath(@__DIR__, "instructions.json");
@@ -168,3 +170,34 @@ function analyze_instruction_set(oh::ObjectHandle, platform::AbstractPlatform; v
168170
# Otherwise, return `min_march` and let 'em know!
169171
return min_march
170172
end
173+
174+
function augment_microarchitecture!(platform::Platform)
175+
haskey(platform, "march") && return platform
176+
177+
host_arch = arch(HostPlatform())
178+
host_isas = arch_march_isa_mapping[host_arch]
179+
idx = findlast(((name, isa),) -> isa <= CPUID.cpu_isa(), host_isas)
180+
platform["march"] = first(host_isas[idx])
181+
return platform
182+
end
183+
184+
function march_comparison_strategy(a::String, b::String, a_requested::Bool, b_requested::Bool)
185+
function get_arch_isa(isa_name::String)
186+
for (arch, isas) in arch_march_isa_mapping
187+
for (name, isa) in isas
188+
name == isa_name && return arch, isa
189+
end
190+
end
191+
return nothing, nothing
192+
end
193+
194+
a_arch, a_isa = get_arch_isa(a)
195+
b_arch, b_isa = get_arch_isa(b)
196+
if any(isnothing, (a_arch, b_arch)) || a_arch != b_arch
197+
# Architectures are definitely not compatible, exit early
198+
return false
199+
end
200+
201+
# ISA `a` is compatible with ISA `b` only if it's a subset of `b`
202+
return a_isa b_isa
203+
end

0 commit comments

Comments
 (0)