Julia: codegen.cpp:2777: jl_cgval_t emit_invoke(jl_codectx_t&, jl_expr_t*, jl_value_t*): Assertion `jl_is_method_instance(mi)' failed.

Created on 24 Apr 2020  Β·  7Comments  Β·  Source: JuliaLang/julia

Perplexing crash:

julia> using StaticArrays

julia> function testcrash(sr)
           for s ∈ sr
               Amatrix = MMatrix{s,s,Float64}(undef)
               @show s, Amatrix β‰ˆ Amatrix
           end
       end
testcrash (generic function with 1 method)

julia> testcrash(2:40)
(s, Amatrix β‰ˆ Amatrix) = (2, true)
(s, Amatrix β‰ˆ Amatrix) = (3, true)
(s, Amatrix β‰ˆ Amatrix) = (4, true)
(s, Amatrix β‰ˆ Amatrix) = (5, true)
(s, Amatrix β‰ˆ Amatrix) = (6, true)
(s, Amatrix β‰ˆ Amatrix) = (7, true)
(s, Amatrix β‰ˆ Amatrix) = (8, true)
(s, Amatrix β‰ˆ Amatrix) = (9, true)
(s, Amatrix β‰ˆ Amatrix) = (10, true)
(s, Amatrix β‰ˆ Amatrix) = (11, true)
(s, Amatrix β‰ˆ Amatrix) = (12, true)
(s, Amatrix β‰ˆ Amatrix) = (13, true)
(s, Amatrix β‰ˆ Amatrix) = (14, true)
(s, Amatrix β‰ˆ Amatrix) = (15, true)
(s, Amatrix β‰ˆ Amatrix) = (16, true)
(s, Amatrix β‰ˆ Amatrix) = (17, true)
(s, Amatrix β‰ˆ Amatrix) = (18, false)
(s, Amatrix β‰ˆ Amatrix) = (19, false)
(s, Amatrix β‰ˆ Amatrix) = (20, true)
(s, Amatrix β‰ˆ Amatrix) = (21, true)
(s, Amatrix β‰ˆ Amatrix) = (22, true)
(s, Amatrix β‰ˆ Amatrix) = (23, true)
(s, Amatrix β‰ˆ Amatrix) = (24, false)
(s, Amatrix β‰ˆ Amatrix) = (25, false)
(s, Amatrix β‰ˆ Amatrix) = (26, false)
(s, Amatrix β‰ˆ Amatrix) = (27, true)
(s, Amatrix β‰ˆ Amatrix) = (28, false)
(s, Amatrix β‰ˆ Amatrix) = (29, true)
(s, Amatrix β‰ˆ Amatrix) = (30, true)
(s, Amatrix β‰ˆ Amatrix) = (31, false)
julia: /home/chriselrod/Documents/languages/julia/src/codegen.cpp:2777: jl_cgval_t emit_invoke(jl_codectx_t&, jl_expr_t*, jl_value_t*): Assertion `jl_is_method_instance(mi)' failed.

signal (6): Aborted
in expression starting at REPL[3]:1
gsignal at /usr/lib64/haswell/libc.so.6 (unknown line)
abort at /usr/lib64/haswell/libc.so.6 (unknown line)
unknown function (ip: 0x7fe9a31aa753)
__assert_fail at /usr/lib64/haswell/libc.so.6 (unknown line)
emit_invoke at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:2777
emit_expr at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:3625
emit_ssaval_assign at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:3336
emit_stmtpos at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:3500
emit_function at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:6158
jl_emit_code at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:6504
jl_emit_codeinst at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:6538
jl_compile_workqueue at /home/chriselrod/Documents/languages/julia/src/codegen.cpp:6630
_jl_compile_codeinst at /home/chriselrod/Documents/languages/julia/src/jitlayers.cpp:100
jl_generate_fptr at /home/chriselrod/Documents/languages/julia/src/jitlayers.cpp:277
jl_compile_method_internal at /home/chriselrod/Documents/languages/julia/src/gf.c:1968
jl_compile_method_internal at /home/chriselrod/Documents/languages/julia/src/gf.c:1935 [inlined]
_jl_invoke at /home/chriselrod/Documents/languages/julia/src/gf.c:2228 [inlined]
jl_apply_generic at /home/chriselrod/Documents/languages/julia/src/gf.c:2402
macro expansion at ./show.jl:613 [inlined]
testcrash at ./REPL[2]:4
unknown function (ip: 0x7fe97a475051)
jl_apply at /home/chriselrod/Documents/languages/julia/src/julia.h:1691 [inlined]
do_call at /home/chriselrod/Documents/languages/julia/src/interpreter.c:369
eval_value at /home/chriselrod/Documents/languages/julia/src/interpreter.c:458
eval_stmt_value at /home/chriselrod/Documents/languages/julia/src/interpreter.c:409 [inlined]
eval_body at /home/chriselrod/Documents/languages/julia/src/interpreter.c:809
jl_interpret_toplevel_thunk at /home/chriselrod/Documents/languages/julia/src/interpreter.c:921
jl_toplevel_eval_flex at /home/chriselrod/Documents/languages/julia/src/toplevel.c:823
jl_toplevel_eval_flex at /home/chriselrod/Documents/languages/julia/src/toplevel.c:773
jl_toplevel_eval_flex at /home/chriselrod/Documents/languages/julia/src/toplevel.c:773
jl_toplevel_eval_flex at /home/chriselrod/Documents/languages/julia/src/toplevel.c:773
jl_toplevel_eval at /home/chriselrod/Documents/languages/julia/src/toplevel.c:832 [inlined]
jl_toplevel_eval_in at /home/chriselrod/Documents/languages/julia/src/toplevel.c:863
eval at ./boot.jl:331
eval_user_input at /home/chriselrod/Documents/languages/julia/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:132
repl_backend_loop at /home/chriselrod/Documents/languages/julia/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:193
start_repl_backend at /home/chriselrod/Documents/languages/julia/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:178
#run_repl#35 at /home/chriselrod/Documents/languages/julia/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:290
run_repl at /home/chriselrod/Documents/languages/julia/usr/share/julia/stdlib/v1.5/REPL/src/REPL.jl:286
#803 at ./client.jl:384
jfptr_YY.803_52397 at /home/chriselrod/Documents/languages/julia/usr/lib/julia/sys.so (unknown line)
jl_apply at /home/chriselrod/Documents/languages/julia/src/julia.h:1691 [inlined]
do_apply at /home/chriselrod/Documents/languages/julia/src/builtins.c:650
jl_f__apply at /home/chriselrod/Documents/languages/julia/src/builtins.c:664 [inlined]
jl_f__apply_latest at /home/chriselrod/Documents/languages/julia/src/builtins.c:700
#invokelatest#1 at ./essentials.jl:710 [inlined]
invokelatest at ./essentials.jl:709 [inlined]
run_main_repl at ./client.jl:368
exec_options at ./client.jl:306
_start at ./client.jl:491
jfptr__start_30079 at /home/chriselrod/Documents/languages/julia/usr/lib/julia/sys.so (unknown line)
jl_apply at /home/chriselrod/Documents/languages/julia/ui/../src/julia.h:1691 [inlined]
true_main at /home/chriselrod/Documents/languages/julia/ui/repl.c:96
main at /home/chriselrod/Documents/languages/julia/ui/repl.c:217
__libc_start_main at /usr/lib64/haswell/libc.so.6 (unknown line)
_start at /home/chriselrod/Documents/languages/julia/usr/bin/julia (unknown line)
Allocations: 282341928 (Pool: 282323765; Big: 18163); GC: 190
fish: β€œ/home/chriselrod/Documents/lang…” terminated by signal SIGABRT (Abort)

Originally posted as an issue at StaticArrays: https://github.com/JuliaArrays/StaticArrays.jl/issues/771

bug codegen

Most helpful comment

Reduced to the following:

using LinearAlgebra
abstract type b{Tuple, c, d} <: AbstractArray{c,0} end
struct e{q}
    end
e(f) = e{1(f.parameters...)}()
e(::c) where c<: b{q} where q = @isdefined(q) ? e(q) : 0
g(::e{q}) where q = q
mutable struct h{q, c, d, r} <: b{q, c,0}
    function h{q,c,0,0}(k) where {q,c}
        new()
    end
end
function Base.getindex(l::h, i)
    checkbounds(l,i)
    c = eltype(0)
    if return unsafe_load(Base.unsafe_convert(Ptr{c}, pointer_from_objref(l)), i)
    end
end
m{s, n, c} = h{Tuple{s, n}, c, 0}
function (::Type{m{s,n,c}})(k) where {s,n,c}
    m{s, n, c, 0}(0)
end
Base.length(a::b) = g(e(a))
Base.:-(a::AbstractArray, ::b) = ( a, )
@inline LinearAlgebra.norm(a::b) = o(e(a), a)
@generated function o(::e{q}, a) where q
    expr = 0
    for j = 1:q
        expr = :($expr + a[$j])
    end
    quote
        $(Expr(:meta, :inline))
        0($expr)
    end
end
for f ∈ 0:32
    p = m{f,f,Float64}(0)
    p β‰ˆ p
end

Could probably reduce it further by giving the tool access to the called functions from LinearAlgebra and Base.

All 7 comments

Worth pointing out that doing things before running the loop, e.g. the first obvious test:

julia> using StaticArrays

julia> A = MMatrix{32,32,Float64}(undef);

julia> isapprox(A, A)

will not result in a crash, but then running the loop will produce a crash on a different (earlier) iteration.

I can reproduce this in the Julia 1.4.0 official 64-bit linux build, with StaticArrays v0.12.1. The assertion is slightly different though:

julia> testcrash(2:40)
(s, Amatrix β‰ˆ Amatrix) = (2, true)
[...]
(s, Amatrix β‰ˆ Amatrix) = (31, true)
jl: /buildworker/worker/package_linux64/build/src/codegen.cpp:3322: jl_cgval_t emit_invoke(jl_codectx_t&, jl_expr_t*, jl_value_t*): Assertion `(((jl_value_t*)(((jl_taggedvalue_t*)((char*)(mi) - sizeof(jl_taggedvalue_t)))->header & ~(uintptr_t)15))==(jl_value_t*)(jl_method_instance_type))' failed.

The assertion is exactly the same.

Fair enough, a different way to write the same thing.

Reduced to the following:

using LinearAlgebra
abstract type b{Tuple, c, d} <: AbstractArray{c,0} end
struct e{q}
    end
e(f) = e{1(f.parameters...)}()
e(::c) where c<: b{q} where q = @isdefined(q) ? e(q) : 0
g(::e{q}) where q = q
mutable struct h{q, c, d, r} <: b{q, c,0}
    function h{q,c,0,0}(k) where {q,c}
        new()
    end
end
function Base.getindex(l::h, i)
    checkbounds(l,i)
    c = eltype(0)
    if return unsafe_load(Base.unsafe_convert(Ptr{c}, pointer_from_objref(l)), i)
    end
end
m{s, n, c} = h{Tuple{s, n}, c, 0}
function (::Type{m{s,n,c}})(k) where {s,n,c}
    m{s, n, c, 0}(0)
end
Base.length(a::b) = g(e(a))
Base.:-(a::AbstractArray, ::b) = ( a, )
@inline LinearAlgebra.norm(a::b) = o(e(a), a)
@generated function o(::e{q}, a) where q
    expr = 0
    for j = 1:q
        expr = :($expr + a[$j])
    end
    quote
        $(Expr(:meta, :inline))
        0($expr)
    end
end
for f ∈ 0:32
    p = m{f,f,Float64}(0)
    p β‰ˆ p
end

Could probably reduce it further by giving the tool access to the called functions from LinearAlgebra and Base.

Reducing a bit further: most of Base is gone, all unsafe operations too:

t(u, p) = w(u...) && w(u...)
function w(aa, p=2)
    if p == 2
    elseif t(aa, p)
    end
end
abstract type b{Tuple, c, d} <: AbstractArray{c,0} end
struct e{q} end
e(f) = e{1(f.parameters...)}()
e(::c) where c <: b{q} where q = 0 ? e(q) : 0
g(::e{q}) where q = q
mutable struct h{q, c, d, r} <: b{q,c,0} end
h{q,c,0,0}(k) where {q,c} = h{q,c,0,0}()
ac(x, ad) = Base.OneTo(ae(ad))
Base.getindex(l::h, i) = checkindex(Bool, ac(x, l), i) && ab
m{s, n} = h{Tuple{s, n}, 0}
(::Type{h{Tuple{s, n}, c, 0}})(k) where {s,n,c} = m{s, n, c, 0}(0)
ae(a) = g(e(a))
@inline w(a::b) = o(e(a), a)
ai(x) = aj(x...)
aj(x, ak) = ak
function af(ad, ag=(ad,))
    y = iterate(ag...)
    y == nothing && return
    ad, (ag[1], ai(y))
end
@generated function o(::e{q}, a) where q
    ah = z = 1:q
    j = af(z)
    while j != nothing
        ah = :($ah + getindex(a, $j[1]))
        j = af(z, j[2])
    end
    quote
        $(Expr(:meta, :inline))
        $ah
    end
end
for f ∈ 0:32
    p = m{f,f,0}(0)
    w((p,))
end

The final reduced example still crashes for me in Julia 1.5.2, but in Julia 1.6.0-DEV.1399 it seems to work (well, at least it does not crash).

But the original example still produces a crash, albeit a different one:

...
(s, Amatrix β‰ˆ Amatrix) = (27, false)
(s, Amatrix β‰ˆ Amatrix) = (28, false)

signal (11): Segmentation fault: 11
in expression starting at REPL[21]:1
jl_rettype_inferred at /Users/mhorn/Projekte/Julia/julia.master/src/gf.c:339
emit_invoke at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:3424
emit_expr at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:4341
emit_ssaval_assign at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:4001
emit_stmtpos at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:0 [inlined]
emit_function at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:6798
jl_emit_code at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:7144
jl_emit_codeinst at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:7189
jl_compile_workqueue at /Users/mhorn/Projekte/Julia/julia.master/src/codegen.cpp:7291
_jl_compile_codeinst at /Users/mhorn/Projekte/Julia/julia.master/src/jitlayers.cpp:116
jl_generate_fptr at /Users/mhorn/Projekte/Julia/julia.master/src/jitlayers.cpp:328
jl_compile_method_internal at /Users/mhorn/Projekte/Julia/julia.master/src/gf.c:1924
_jl_invoke at /Users/mhorn/Projekte/Julia/julia.master/src/gf.c:2183 [inlined]
jl_apply_generic at /Users/mhorn/Projekte/Julia/julia.master/src/gf.c:2373
macro expansion at ./show.jl:904 [inlined]
testcrash at ./REPL[18]:4
...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

StefanKarpinski picture StefanKarpinski  Β·  113Comments

mbauman picture mbauman  Β·  276Comments

StefanKarpinski picture StefanKarpinski  Β·  141Comments

jebej picture jebej  Β·  208Comments

JeffBezanson picture JeffBezanson  Β·  167Comments