Skip to content

Commit

Permalink
Add proper error for attempted use of undef slot
Browse files Browse the repository at this point in the history
Fixes the segfault in #50518 and turns it into a proper error at
both the syntax level (to catch lowering generating bad slot references)
as well as at the codegen level (to catch e.g. bad generated functions
and opaque closures). However, note that the latter case is technically
undefined behavior, because we do not model the possibility that an
otherwise-defined argument could throw at access time. Of course,
throwing an error is allowable as undefined behavior and preferable
to a segfault.
  • Loading branch information
Keno committed Jul 14, 2023
1 parent ae08077 commit 94219a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4921,6 +4921,12 @@ static jl_cgval_t emit_local(jl_codectx_t &ctx, jl_value_t *slotload)
size_t sl = jl_slot_number(slotload) - 1;
jl_varinfo_t &vi = ctx.slots[sl];
jl_sym_t *sym = slot_symbol(ctx, sl);
if (sym == jl_unused_sym) {
// This shouldn't happen in well-formed input, but let's be robust,
// since we otherwise cause undefined behavior here.
emit_error(ctx, "(INTERNAL ERROR): Tried to use `#undef#` argument.");
return jl_cgval_t();
}
return emit_varinfo(ctx, vi, sym);
}

Expand Down
3 changes: 2 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5051,7 +5051,8 @@ f(x) = yt(x)
(define slot-table (symbol-to-idx-map (map car (car (lam:vinfo lam)))))
(define sp-table (symbol-to-idx-map (lam:sp lam)))
(define (renumber-stuff e)
(cond ((symbol? e)
(cond ((eq? e UNUSED) (error "Attempted to use slot marked unused"))
((symbol? e)
(let ((idx (get slot-table e #f)))
(if idx
`(slot ,idx)
Expand Down
5 changes: 5 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3507,3 +3507,8 @@ let x = 1 => 2
@test_throws ErrorException @eval a => b = 2
@test_throws "function Base.=> must be explicitly imported to be extended" @eval a => b = 2
end

# Test that bad lowering does not segfault (ref #50518)
@test_throws ErrorException("syntax: Attempted to use slot marked unused") @eval function funused50518(::Float64)
$(Symbol("#unused#"))
end

0 comments on commit 94219a4

Please sign in to comment.