PerlFFI / FFI-Platypus-Lang-Go

8 stars 2 forks source link

Segmentation fault when using gohttplib #14

Open Prajithp opened 3 years ago

Prajithp commented 3 years ago

Thanks for your wonderful module. I just started using it today. I'm getting this strange segmentation fault when using gohttplib sample module. Could you please help me on this?

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f1f0a3cfef2]

runtime stack:
runtime.throw(0x7f1f0973bdd5, 0x2a)
        /usr/local/go/src/runtime/panic.go:1116 +0x74
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:726 +0x4ac

goroutine 6 [syscall]:
runtime.cgocall(0x7f1f0972eee0, 0xc0000a59f0, 0xc0000901b0)
        /usr/local/go/src/runtime/cgocall.go:133 +0x5d fp=0xc0000a59c0 sp=0xc0000a5988 pc=0x7f1f094fae1d
main._Cfunc_Call_HandleFunc(0x7f1f00000000, 0xc0000901b0, 0x7f1f0bd0b010)
        _cgo_gotypes.go:80 +0x47 fp=0xc0000a59f0 sp=0xc0000a59c0 pc=0x7f1f0972d707
main.HandleFunc.func1.1(0xc000000000, 0xc0000901b0, 0xc000010028)
        /home/prajith/dev/go/src/github.com/Prajithp/FFI-Platypus-Lang-Go/examples/GoHttpLib/ffi/gohttplib.go:72 +0x7e fp=0xc0000a5a28 sp=0xc0000a59f0 pc=0x7f1f0972e23e
main.HandleFunc.func1(0x7f1f099f9240, 0xc0000b8000, 0xc0000ae000)
        /home/prajith/dev/go/src/github.com/Prajithp/FFI-Platypus-Lang-Go/examples/GoHttpLib/ffi/gohttplib.go:72 +0x3fa fp=0xc0000a5b68 sp=0xc0000a5a28 pc=0x7f1f0972e8da
net/http.HandlerFunc.ServeHTTP(0xc00005abc0, 0x7f1f099f9240, 0xc0000b8000, 0xc0000ae000)
        /usr/local/go/src/net/http/server.go:2042 +0x46 fp=0xc0000a5b90 sp=0xc0000a5b68 pc=0x7f1f09719cc6
net/http.(*ServeMux).ServeHTTP(0x7f1f09b5f640, 0x7f1f099f9240, 0xc0000b8000, 0xc0000ae000)
        /usr/local/go/src/net/http/server.go:2417 +0x1ad fp=0xc0000a5bf0 sp=0xc0000a5b90 pc=0x7f1f0971bc0d
net/http.serverHandler.ServeHTTP(0x7f1f09b5f840, 0x7f1f099f9240, 0xc0000b8000, 0xc0000ae000)
        /usr/local/go/src/net/http/server.go:2843 +0xa5 fp=0xc0000a5c20 sp=0xc0000a5bf0 pc=0x7f1f0971d245
net/http.(*conn).serve(0xc00010ca00, 0x7f1f099f9680, 0xc000092000)
        /usr/local/go/src/net/http/server.go:1925 +0x8ad fp=0xc0000a5fc8 sp=0xc0000a5c20 pc=0x7f1f09718d0d
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc0000a5fd0 sp=0xc0000a5fc8 pc=0x7f1f095618e1
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2969 +0x36c

goroutine 17 [IO wait, locked to thread]:
internal/poll.runtime_pollWait(0x7f1f080b26d8, 0x72, 0x0)
        /usr/local/go/src/runtime/netpoll.go:222 +0x65
internal/poll.(*pollDesc).wait(0xc00014a018, 0x72, 0x0, 0x0, 0x7f1f09730376)
        /usr/local/go/src/internal/poll/fd_poll_runtime.go:87 +0x47
internal/poll.(*pollDesc).waitRead(...)
        /usr/local/go/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Accept(0xc00014a000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /usr/local/go/src/internal/poll/fd_unix.go:394 +0x1fc
net.(*netFD).accept(0xc00014a000, 0x8a1a5d157fc0501, 0x0, 0x0)
        /usr/local/go/src/net/fd_unix.go:172 +0x45
net.(*TCPListener).accept(0xc00000e2c0, 0x60b46d4e, 0xc000056cb0, 0x7f1f095875a8)
        /usr/local/go/src/net/tcpsock_posix.go:139 +0x34
net.(*TCPListener).Accept(0xc00000e2c0, 0xc000056d00, 0x18, 0xc000000600, 0x7f1f0971d70c)
        /usr/local/go/src/net/tcpsock.go:261 +0x66
net/http.(*Server).Serve(0x7f1f09b5f840, 0x7f1f099f9000, 0xc00000e2c0, 0x0, 0x0)
        /usr/local/go/src/net/http/server.go:2937 +0x266
net/http.(*Server).ListenAndServe(0x7f1f09b5f840, 0xc000016404, 0xc)
        /usr/local/go/src/net/http/server.go:2866 +0xb9
main.ListenAndServe(0x16eaf10)
        /home/prajith/dev/go/src/github.com/Prajithp/FFI-Platypus-Lang-Go/examples/GoHttpLib/ffi/gohttplib.go:37 +0x5e
main._cgoexpwrap_74dd220ece92_ListenAndServe(0x16eaf10)
        _cgo_gotypes.go:126 +0x2d

goroutine 19 [IO wait]:
internal/poll.runtime_pollWait(0x7f1f080b25f0, 0x72, 0x7f1f099f5ce0)
        /usr/local/go/src/runtime/netpoll.go:222 +0x65
internal/poll.(*pollDesc).wait(0xc00014a098, 0x72, 0x7f1f099f5c00, 0x7f1f09b27c00, 0x0)
        /usr/local/go/src/internal/poll/fd_poll_runtime.go:87 +0x47
internal/poll.(*pollDesc).waitRead(...)
        /usr/local/go/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Read(0xc00014a080, 0xc000090041, 0x1, 0x1, 0x0, 0x0, 0x0)
        /usr/local/go/src/internal/poll/fd_unix.go:159 +0x1a5
net.(*netFD).Read(0xc00014a080, 0xc000090041, 0x1, 0x1, 0x0, 0x0, 0x0)
        /usr/local/go/src/net/fd_posix.go:55 +0x51
net.(*conn).Read(0xc000010048, 0xc000090041, 0x1, 0x1, 0x0, 0x0, 0x0)
        /usr/local/go/src/net/net.go:182 +0x90
net/http.(*connReader).backgroundRead(0xc000090030)
        /usr/local/go/src/net/http/server.go:690 +0x5a
created by net/http.(*connReader).startBackgroundRead
        /usr/local/go/src/net/http/server.go:686 +0xd5
Aborted

Here is the code.

package Request;

use FFI::Platypus::Record;

record_layout_1(
   'string rw' => 'Method',
   'string rw' => 'Host',
   'string rw' => 'URL',
   'string rw' => 'Body',
   'string rw' => 'Headers'
);

package main;

use FFI::Platypus;

my $ffi = FFI::Platypus->new( api => 1);
$ffi->lib('./blib/lib/auto/share/dist/GoHttpLib/lib/libgohttplib.so');

$ffi->type("record(Request)" => 'Request');
$ffi->type('unsigned int' => 'ResponseWriterPtr');

$ffi->type('(ResponseWriterPtr, Request*)->void' => 'FunPtr' );

$ffi->attach(ListenAndServe  => ['string'] => 'void' );
$ffi->attach(HandleFunc  => ['string', 'FunPtr'] => 'void' );

$ffi->attach(ResponseWriter_Write => ['ResponseWriterPtr', 'string', 'int'] => 'int');
$ffi->attach(ResponseWriter_WriteHeader => ['ResponseWriterPtr', 'int'] => 'void');

my $handler = $ffi->closure(sub {
    my ($w, $r) = @_;
    my $body = "Hello World";
    my $n = ResponseWriter_Write($w, $body, length($body));
});

HandleFunc("/", $handler);
ListenAndServe("0.0.0.0:5000");
Prajithp commented 3 years ago

Just an update, segmentation fault is happening only in threads.

Prajithp commented 3 years ago

@plicease managed to fix this by setting "PERL_SET_CONTEXT" in ffi_pl_closure_call. 910d28d

I know this is not the proper way to fix, but it just works.