Skip to content

Commit 512c4a4

Browse files
giordanoararslan
authored andcommitted
Stop compilers from using known unsafe options (#610)
For context, it was reported in JuliaCI/BaseBenchmarks.jl#253 that dlopening a library compiled with `-Ofast` caused the denormal mode to be changed. This PR disallows using compiler flags that are known to cause troubles. * Add other unsafe flags suggested by Simon Byrne Co-Authored-By: Alex Arslan <[email protected]> Co-authored-by: Alex Arslan <[email protected]>
1 parent 3aae1be commit 512c4a4

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/AutoBuild.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ function autobuild(dir::AbstractString,
642642
],
643643
compiler_wrapper_dir = joinpath(prefix, "compiler_wrappers"),
644644
src_name = src_name,
645-
extract_kwargs(kwargs, (:preferred_gcc_version,:preferred_llvm_version,:compilers))...,
645+
extract_kwargs(kwargs, (:preferred_gcc_version,:preferred_llvm_version,:compilers,:allow_unsafe_flags))...,
646646
)
647647

648648
# Set up some bash traps

src/DockerRunner.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function DockerRunner(workspace_root::String;
8383
envs = merge(platform_envs(platform, src_name; verbose=verbose), extra_env)
8484

8585
# JIT out some compiler wrappers, add it to our mounts
86-
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, extract_kwargs(kwargs, (:compilers,))...)
86+
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, extract_kwargs(kwargs, (:compilers,:allow_unsafe_flags))...)
8787
push!(workspaces, compiler_wrapper_path => "/opt/bin")
8888

8989
# the workspace_root is always a workspace, and we always mount it first

src/Runner.jl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ and even simple programs will not compile without them.
4646
function generate_compiler_wrappers!(platform::Platform; bin_path::AbstractString,
4747
host_platform::Platform = Linux(:x86_64; libc=:musl),
4848
rust_platform::Platform = Linux(:x86_64; libc=:glibc),
49-
compilers::Vector{Symbol} = [:c])
49+
compilers::Vector{Symbol} = [:c],
50+
allow_unsafe_flags::Bool = false,
51+
)
5052
global use_ccache
5153

5254
# Wipe that directory out, in case it already had compiler wrappers
@@ -66,7 +68,8 @@ function generate_compiler_wrappers!(platform::Platform; bin_path::AbstractStrin
6668
hash_args::Bool = false,
6769
extra_cmds::String = "",
6870
link_only_flags::Vector = String[],
69-
env::Dict{String,String} = Dict{String,String}())
71+
env::Dict{String,String} = Dict{String,String}(),
72+
unsafe_flags = String[])
7073
write(io, """
7174
#!/bin/bash
7275
# This compiler wrapper script brought into existence by `generate_compiler_wrappers()`
@@ -109,6 +112,16 @@ function generate_compiler_wrappers!(platform::Platform; bin_path::AbstractStrin
109112
write(io, "export $(name)=\"$(val)\"\n")
110113
end
111114

115+
if length(unsafe_flags) >= 1
116+
write(io, """
117+
if [[ \"\$@\" =~ \"$(join(unsafe_flags, "\"|\""))\" ]]; then
118+
echo -e \"You used one or more of the unsafe flags: $(join(unsafe_flags, ", "))\\nPlease repent.\" >&2
119+
exit 1
120+
fi
121+
""")
122+
println(io)
123+
end
124+
112125
if allow_ccache
113126
write(io, """
114127
if [ \${USE_CCACHE} == "true" ]; then
@@ -213,9 +226,9 @@ function generate_compiler_wrappers!(platform::Platform; bin_path::AbstractStrin
213226
end
214227

215228
# C/C++/Fortran
216-
gcc(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-gcc $(gcc_flags(p))"; hash_args=true, link_only_flags=gcc_link_flags(p))
217-
gxx(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-g++ $(gcc_flags(p))"; hash_args=true, link_only_flags=gcc_link_flags(p))
218-
gfortran(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-gfortran $(fortran_flags(p))"; allow_ccache=false)
229+
gcc(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-gcc $(gcc_flags(p))"; hash_args=true, link_only_flags=gcc_link_flags(p), unsafe_flags = allow_unsafe_flags ? String[] : ["-Ofast", "-ffast-math", "-funsafe-math-optimizations"])
230+
gxx(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-g++ $(gcc_flags(p))"; hash_args=true, link_only_flags=gcc_link_flags(p), unsafe_flags = allow_unsafe_flags ? String[] : ["-Ofast", "-ffast-math", "-funsafe-math-optimizations"])
231+
gfortran(io::IO, p::Platform) = wrapper(io, "/opt/$(triplet(p))/bin/$(triplet(p))-gfortran $(fortran_flags(p))"; allow_ccache=false, unsafe_flags = allow_unsafe_flags ? String[] : ["-Ofast", "-ffast-math", "-funsafe-math-optimizations"])
219232
clang(io::IO, p::Platform) = wrapper(io, "/opt/$(host_target)/bin/clang $(clang_flags(p))"; link_only_flags=clang_link_flags(p))
220233
clangxx(io::IO, p::Platform) = wrapper(io, "/opt/$(host_target)/bin/clang++ $(clang_flags(p))"; link_only_flags=clang_link_flags(p))
221234
objc(io::IO, p::Platform) = wrapper(io, "/opt/$(host_target)/bin/clang -x objective-c $(clang_flags(p))"; link_only_flags=clang_link_flags(p))

src/UserNSRunner.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function UserNSRunner(workspace_root::String;
4141
envs = merge(platform_envs(platform, src_name; verbose=verbose), extra_env)
4242

4343
# JIT out some compiler wrappers, add it to our mounts
44-
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, extract_kwargs(kwargs, (:compilers,))...)
44+
generate_compiler_wrappers!(platform; bin_path=compiler_wrapper_path, extract_kwargs(kwargs, (:compilers,:allow_unsafe_flags))...)
4545
push!(workspaces, compiler_wrapper_path => "/opt/bin")
4646

4747
# the workspace_root is always a workspace, and we always mount it first

0 commit comments

Comments
 (0)