Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault in ccall #40164

Closed
jw3126 opened this issue Mar 24, 2021 · 4 comments · Fixed by #41827
Closed

Segfault in ccall #40164

jw3126 opened this issue Mar 24, 2021 · 4 comments · Fixed by #41827
Labels
bug Indicates an unexpected problem or unintended behavior compiler:codegen Generation of LLVM IR and native code

Comments

@jw3126
Copy link
Contributor

jw3126 commented Mar 24, 2021

From discourse. The following code segfaults for me:

C = """
typedef struct CParticle {
    double charge;
    double E;
    double weight;
} CParticle;


void simulate(void (*f)(CParticle x)) {
    CParticle cp;
    cp.charge = 0;
    cp.E = 1;
    cp.weight = 2;
    (*f)(cp);
    return;
}

"""

struct CParticle
    charge::Cdouble
    E::Cdouble
    weight::Cdouble
end

write("lib.c", C)
run(`gcc -c -fPIC lib.c -o lib.o`)
run(`gcc -shared lib.o -o lib.so`)
lib = "./lib.so"

function main()
    ret = CParticle[]
    f = x::CParticle -> (push!(ret, x); nothing)
    f_c = @cfunction $f Cvoid (CParticle,)
    GC.@preserve ret f f_c begin # AFAICT I don't need to GC.@preserver here
        @ccall lib.simulate(f_c::Ptr{Cvoid})::Cvoid
    end
    @show ret
end

main()

I read the manual and in particular the section about closures, but I am still not sure where the error lies.
It works, if I do one of the following:

  • make the CParticle struct smaller e.g. only charge + weight fields
  • execute the main code globally
  • don't call push! in the callback

@Gnimuc pointed out workarounds, but it is still unclear why this happens, and would be nice to fix it or document it. I can help with the latter.

@JeffBezanson
Copy link
Member

Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', and 'sret' are incompatible!
void ({} addrspace(10)**, [3 x double] addrspace(11)*)* @"jlcapi_#3_256"
julia: /workspace/srcdir/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:2369: llvm::slpvectorizer::BoUpSLP::~BoUpSLP(): Assertion `!verifyFunction(*F, &dbgs())' failed.

signal (6): Aborted
in expression starting at REPL[17]:1
gsignal at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
abort at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
unknown function (ip: 0x7f1167b3d728)
__assert_fail at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
_ZN4llvm13slpvectorizer7BoUpSLPD1Ev at /home/jeff/src/julia/usr/bin/../lib/libLLVM-11jl.so (unknown line)
_ZN4llvm17SLPVectorizerPass7runImplERNS_8FunctionEPNS_15ScalarEvolutionEPNS_19TargetTransformInfoEPNS_17TargetLibraryInfoEPNS_9AAResultsEPNS_8LoopInfoEPNS_13DominatorTreeEPNS_15AssumptionCacheEPNS_12DemandedBitsEPNS_25OptimizationRemarkEmitterE.part.1527 at /home/jeff/src/julia/usr/bin/../lib/libLLVM-11jl.so (unknown line)
_ZN12_GLOBAL__N_113SLPVectorizer13runOnFunctionERN4llvm8FunctionE at /home/jeff/src/julia/usr/bin/../lib/libLLVM-11jl.so (unknown line)
_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE at /home/jeff/src/julia/usr/bin/../lib/libLLVM-11jl.so (unknown line)

@JeffBezanson JeffBezanson added bug Indicates an unexpected problem or unintended behavior compiler:codegen Generation of LLVM IR and native code labels Mar 24, 2021
@ViralBShah
Copy link
Member

Just tried the code snippet on mac with Julia master (1.9-dev) and I don't get a segfault on mac.

@vtjnash
Copy link
Member

vtjnash commented Sep 6, 2022

Fixed by #41827, but it didn't use any of the magic words for auto-closing

@jw3126
Copy link
Contributor Author

jw3126 commented Sep 7, 2022

Works for me thanks @troels and everybody else for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:codegen Generation of LLVM IR and native code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants