From 31a9f130c34ff0b1ebc418d4bc845856faaba754 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Fri, 5 Jan 2024 02:59:49 +0100 Subject: [PATCH] Properly rename EnterNode scope after code coverage insertion (#52720) Fixes #52672 and changes the emission path to move the error to the point of corruption instead of the point of first use. --- base/compiler/optimize.jl | 6 +++++- src/codegen.cpp | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/base/compiler/optimize.jl b/base/compiler/optimize.jl index 64c25584f0449..c7d2983bced82 100644 --- a/base/compiler/optimize.jl +++ b/base/compiler/optimize.jl @@ -1379,7 +1379,11 @@ function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, lab @assert !isdefined(el, :scope) body[i] = nothing else - body[i] = EnterNode(el, tgt + labelchangemap[tgt]) + if isdefined(el, :scope) && isa(el.scope, SSAValue) + body[i] = EnterNode(tgt + labelchangemap[tgt], SSAValue(el.scope.id + ssachangemap[el.scope.id])) + else + body[i] = EnterNode(el, tgt + labelchangemap[tgt]) + end end end elseif isa(el, Expr) diff --git a/src/codegen.cpp b/src/codegen.cpp index ceef6486f8c31..ca96e1bcc5545 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8765,6 +8765,14 @@ static jl_llvm_functions_t jl_aliasinfo_t scope_ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_gcframe); if (jl_enternode_scope(stmt)) { jl_cgval_t new_scope = emit_expr(ctx, jl_enternode_scope(stmt)); + if (new_scope.typ == jl_bottom_type) { + // Probably dead code, but let's be loud about it in case it isn't, so we fail + // at the point of the miscompile, rather than later when something attempts to + // read the scope. + emit_error(ctx, "(INTERNAL ERROR): Attempted to execute EnterNode with bad scope"); + find_next_stmt(-1); + continue; + } Value *new_scope_boxed = boxed(ctx, new_scope); scope_ptr = get_scope_field(ctx); old_scope = scope_ai.decorateInst(