Skip to content

Commit

Permalink
Validate more on assertion builds, and abort on errors (JuliaLang#52830)
Browse files Browse the repository at this point in the history
This PR add a `jl_is_assertsbuild` function (cfr. `jl_is_debugbuild`)
for checking if this is an assertions build.
It's used in two places:

1. to enable additional validation, which currently often requires`-g2`
2. to make internal compiler errors fatal

Both these changes are intended to help PkgEval in detecting more issues
(invalid Julia IR) and make for more robust detection (a fatal signal
instead of having to grep logs for `Internal error`, which leads to
false positives).
  • Loading branch information
maleadt authored Jan 14, 2024
1 parent a034aa1 commit 77652fd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function InferenceState(result::InferenceResult, cache_mode::UInt8, interp::Abst
world = get_world_counter(interp)
src = retrieve_code_info(result.linfo, world)
src === nothing && return nothing
validate_code_in_debug_mode(result.linfo, src, "lowered")
maybe_validate_code(result.linfo, src, "lowered")
return InferenceState(result, src, cache_mode, interp)
end
InferenceState(result::InferenceResult, cache_mode::Symbol, interp::AbstractInterpreter) =
Expand Down
11 changes: 7 additions & 4 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function ir_to_codeinf!(opt::OptimizationState)
(; linfo, src) = opt
src = ir_to_codeinf!(src, opt.ir::IRCode)
opt.ir = nothing
validate_code_in_debug_mode(linfo, src, "optimized")
maybe_validate_code(linfo, src, "optimized")
return src
end

Expand Down Expand Up @@ -934,8 +934,11 @@ function run_passes_ipo_safe(
if made_changes
@pass "compact 3" ir = compact!(ir, true)
end
if JLOptions().debug_level == 2
@timeit "verify 3" (verify_ir(ir, true, false, optimizer_lattice(sv.inlining.interp)); verify_linetable(ir.linetable))
if is_asserts()
@timeit "verify 3" begin
verify_ir(ir, true, false, optimizer_lattice(sv.inlining.interp))
verify_linetable(ir.linetable)
end
end
@label __done__ # used by @pass
return ir
Expand Down Expand Up @@ -1056,7 +1059,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
ssaflags[block_end] = IR_FLAG_NOTHROW

# Verify that type-inference did its job
if JLOptions().debug_level == 2
if is_asserts()
for i = (oldidx + 1):last(sv.cfg.blocks[block].stmts)
@assert i in sv.unreachable
end
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ function finish(me::InferenceState, interp::AbstractInterpreter)
end
end

validate_code_in_debug_mode(me.linfo, me.src, "inferred")
maybe_validate_code(me.linfo, me.src, "inferred")
nothing
end

Expand Down
4 changes: 4 additions & 0 deletions base/compiler/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ end
is_root_module(m::Module) = false

inlining_enabled() = (JLOptions().can_inline == 1)

function coverage_enabled(m::Module)
generating_output() && return false # don't alter caches
cov = JLOptions().code_coverage
Expand All @@ -533,9 +534,12 @@ function coverage_enabled(m::Module)
end
return false
end

function inbounds_option()
opt_check_bounds = JLOptions().check_bounds
opt_check_bounds == 0 && return :default
opt_check_bounds == 1 && return :on
return :off
end

is_asserts() = ccall(:jl_is_assertsbuild, Cint, ()) == 1
6 changes: 3 additions & 3 deletions base/compiler/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ struct InvalidCodeError <: Exception
end
InvalidCodeError(kind::AbstractString) = InvalidCodeError(kind, nothing)

function validate_code_in_debug_mode(linfo::MethodInstance, src::CodeInfo, kind::String)
if JLOptions().debug_level == 2
# this is a debug build of julia, so let's validate linfo
function maybe_validate_code(linfo::MethodInstance, src::CodeInfo, kind::String)
if is_asserts()
errors = validate_code(linfo, src)
if !isempty(errors)
for e in errors
Expand All @@ -75,6 +74,7 @@ function validate_code_in_debug_mode(linfo::MethodInstance, src::CodeInfo, kind:
linfo.def, ": ", e)
end
end
error("")
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9187,6 +9187,9 @@ jl_llvm_functions_t jl_emit_code(
jl_static_show((JL_STREAM*)STDERR_FILENO, jl_current_exception());
jl_printf((JL_STREAM*)STDERR_FILENO, "\n");
jlbacktrace(); // written to STDERR_FILENO
#ifndef JL_NDEBUG
abort();
#endif
}

return decls;
Expand Down
3 changes: 3 additions & 0 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ jl_code_info_t *jl_type_infer(jl_method_instance_t *mi, size_t world, int force)
jlbacktrace(); // written to STDERR_FILENO
}
src = NULL;
#ifndef JL_NDEBUG
abort();
#endif
}
ct->world_age = last_age;
ct->reentrant_timing -= 0b10;
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
XX(jl_is_binding_deprecated) \
XX(jl_is_char_signed) \
XX(jl_is_const) \
XX(jl_is_assertsbuild) \
XX(jl_is_debugbuild) \
XX(jl_is_foreign_type) \
XX(jl_is_identifier) \
Expand Down
13 changes: 13 additions & 0 deletions src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,19 @@ JL_DLLEXPORT int jl_is_debugbuild(void) JL_NOTSAFEPOINT
#endif
}

/**
* @brief Check if Julia has been build with assertions enabled.
*
* @return Returns 1 if assertions are enabled, 0 otherwise.
*/
JL_DLLEXPORT int8_t jl_is_assertsbuild(void) JL_NOTSAFEPOINT {
#ifndef JL_NDEBUG
return 1;
#else
return 0;
#endif
}

/**
* @brief Check if Julia's memory debugging is enabled.
*
Expand Down
5 changes: 4 additions & 1 deletion stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ let _true = Ref(true), f, g, h
end

# manually generate a broken function, which will break codegen
# and make sure Julia doesn't crash
# and make sure Julia doesn't crash (when using a non-asserts build)
is_asserts() = ccall(:jl_is_assertsbuild, Cint, ()) == 1
if !is_asserts()
@eval @noinline Base.@constprop :none f_broken_code() = 0
let m = which(f_broken_code, ())
let src = Base.uncompressed_ast(m)
Expand Down Expand Up @@ -371,6 +373,7 @@ let err = tempname(),
rm(err)
end
end
end

# Issue #33163
A33163(x; y) = x + y
Expand Down

0 comments on commit 77652fd

Please sign in to comment.