Skip to content

Commit 024edd6

Browse files
authored
Add proper error for attempted use of undef slot (#50556)
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.
2 parents c22b1c1 + 38ae975 commit 024edd6

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/codegen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,6 +4921,12 @@ static jl_cgval_t emit_local(jl_codectx_t &ctx, jl_value_t *slotload)
49214921
size_t sl = jl_slot_number(slotload) - 1;
49224922
jl_varinfo_t &vi = ctx.slots[sl];
49234923
jl_sym_t *sym = slot_symbol(ctx, sl);
4924+
if (sym == jl_unused_sym) {
4925+
// This shouldn't happen in well-formed input, but let's be robust,
4926+
// since we otherwise cause undefined behavior here.
4927+
emit_error(ctx, "(INTERNAL ERROR): Tried to use `#undef#` argument.");
4928+
return jl_cgval_t();
4929+
}
49244930
return emit_varinfo(ctx, vi, sym);
49254931
}
49264932

src/julia-syntax.scm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
(if (eq? n '|#self#|) (gensy) n))
297297
arg-names))))
298298
(let ((body (insert-after-meta body ;; don't specialize on generator arguments
299-
`((meta nospecialize ,@arg-names)))))
299+
`((meta nospecialize ,@(map (lambda (idx) `(slot ,(+ idx 1))) (iota (length arg-names))))))))
300300
`(block
301301
(global ,name)
302302
(function (call ,name ,@arg-names) ,body)))))
@@ -5051,7 +5051,8 @@ f(x) = yt(x)
50515051
(define slot-table (symbol-to-idx-map (map car (car (lam:vinfo lam)))))
50525052
(define sp-table (symbol-to-idx-map (lam:sp lam)))
50535053
(define (renumber-stuff e)
5054-
(cond ((symbol? e)
5054+
(cond ((eq? e UNUSED) (error "Attempted to use slot marked unused"))
5055+
((symbol? e)
50555056
(let ((idx (get slot-table e #f)))
50565057
(if idx
50575058
`(slot ,idx)

test/syntax.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,3 +3507,8 @@ let x = 1 => 2
35073507
@test_throws ErrorException @eval a => b = 2
35083508
@test_throws "function Base.=> must be explicitly imported to be extended" @eval a => b = 2
35093509
end
3510+
3511+
# Test that bad lowering does not segfault (ref #50518)
3512+
@test_throws ErrorException("syntax: Attempted to use slot marked unused") @eval function funused50518(::Float64)
3513+
$(Symbol("#unused#"))
3514+
end

0 commit comments

Comments
 (0)