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

[Auditor] Don't try to dlopen libraries for incompatible ISAs #1194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Auditor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ function audit(prefix::Prefix, src_name::AbstractString = "";
shlib_files = filter(f -> startswith(f, prefix.path) && valid_library_path(f, platform), collapse_symlinks(bin_files))

for f in shlib_files
# Inspect all shared library files for our platform (but only if we're
# running native, don't try to load library files from other platforms)
if platforms_match(platform, HostPlatform())
# Always include microarchitecture in the host platform: we don't want to try and
# dlopen a library built for an incompatible microarchitecture.
hp = augment_microarchitecture!(HostPlatform())
# Inspect all shared library files for our platform (but only if we're running
# native, don't try to load library files from other platforms or incompatible ISAs)
if platforms_match(platform, hp)
if verbose
@info("Checking shared library $(relpath(f, prefix.path))")
end
Expand Down
45 changes: 45 additions & 0 deletions src/auditor/instruction_set.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using JSON
using Base.BinaryPlatforms: arch_march_isa_mapping, set_compare_strategy!
using Base.BinaryPlatforms.CPUID

## We start with definitions of instruction mnemonics, broken down by category:
const instruction_categories = JSON.parsefile(joinpath(@__DIR__, "instructions.json");
Expand Down Expand Up @@ -168,3 +170,46 @@ function analyze_instruction_set(oh::ObjectHandle, platform::AbstractPlatform; v
# Otherwise, return `min_march` and let 'em know!
return min_march
end

function march_comparison_strategy(a::String, b::String, a_requested::Bool, b_requested::Bool)
# If both b and a requested, then we fall back to equality:
if a_requested && b_requested
return a == b
end

function get_arch_isa(isa_name::String)
for (arch, isas) in arch_march_isa_mapping
for (name, isa) in isas
name == isa_name && return arch, isa
end
end
return nothing, nothing
end

a_arch, a_isa = get_arch_isa(a)
b_arch, b_isa = get_arch_isa(b)
if any(isnothing, (a_arch, b_arch)) || a_arch != b_arch
# Architectures are definitely not compatible, exit early
return false
end

if a_requested
# ISA `b` is compatible with ISA `a` only if it's a subset of `a`
return b_isa ≤ a_isa
else
# ISA `a` is compatible with ISA `b` only if it's a subset of `b`
return a_isa ≤ b_isa
end
return
end

function augment_microarchitecture!(platform::Platform)
haskey(platform, "march") && return platform

host_arch = arch(HostPlatform())
host_isas = arch_march_isa_mapping[host_arch]
idx = findlast(((name, isa),) -> isa <= CPUID.cpu_isa(), host_isas)
platform["march"] = first(host_isas[idx])
set_compare_strategy!(platform, "march", march_comparison_strategy)
return platform
end