Skip to content

Commit

Permalink
[Auditor] Don't try to dlopen libraries for incompatible ISAs
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Mar 23, 2022
1 parent 39b2307 commit 664dcd7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Auditor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ 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())
set_compare_strategy!(hp, "march", march_comparison_strategy)
# 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
33 changes: 33 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,34 @@ function analyze_instruction_set(oh::ObjectHandle, platform::AbstractPlatform; v
# Otherwise, return `min_march` and let 'em know!
return min_march
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])
return platform
end

function march_comparison_strategy(a::String, b::String, a_requested::Bool, b_requested::Bool)
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

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

0 comments on commit 664dcd7

Please sign in to comment.