vibe-d / eventcore

High performance proactor event loop abstraction library
MIT License
60 stars 42 forks source link

posix/dns: Fix SEGV on Musl when an error happens #150

Closed Geod24 closed 4 years ago

Geod24 commented 4 years ago
When an error happens, the 'struct addrinfo' (ai)
passed to 'passToDNSCallback' can be 'null'.
It end up being passed to 'freeaddrinfo'.
With glibc, or on OSX, it is okay to pass a 'null'
pointer to 'freeaddrinfo', however this will cause
a SIGSEGV on Musl.
The standard defines that 'freeaddrinfo' must accept
what was given to 'getaddrinfo', and 'getaddrinfo'
does not accept null pointer, so the musl behavior
is not wrong per se.
Geod24 commented 4 years ago

The stack trace I got was:

Thread 1 "agora" received signal SIGSEGV, Segmentation fault.
0x00007ffff7f9f5ed in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
10  src/network/freeaddrinfo.c: No such file or directory.
(gdb) bt
#0  0x00007ffff7f9f5ed in freeaddrinfo (p=0x0) at src/network/freeaddrinfo.c:10
#1  0x00005555559acd9a in _D9eventcore7drivers5posix3dns__T17passToDNSCallbackZQuFNbNeSQCh6driver11DNSLookupIDMDFNbNfQBfEQDpQBi9DNSStatusMACQEiQCb10RefAddressZvQBoPS4core3sysQEy5netdb8addrinfoZv (id=..., cb=...,
    status=eventcore.driver.error, ai_orig=0x0) at dns.d:443
#2  0x00005555559aca49 in _D9eventcore7drivers5posix3dns__T18EventDriverDNS_GAIHTCQCcQBvQBq6events__T22PosixEventDriverEventsHTCQDwQDpQDk5epoll14EpollEventLoopHTCQFeQExQEs7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEmHTCQHnQHgQHb7signals__T26SignalFDEventDriverSignalsHTQFqZQBiZQIo11onDNSSignalMFNbNeSQKq6driver7EventIDZ14__foreachbody2MFNbNfmKSQMiQMbQLwQLt__TQLsHTQLbHTQFvZQMg6LookupZi (i=0, l=...) at dns.d:183
#3  0x00005555559c815a in _D9eventcore8internal5utils__T13ChoppedVectorTSQBt7drivers5posix3dns__T18EventDriverDNS_GAIHTCQDoQBvQBq6events__T22PosixEventDriverEventsHTCQFiQDpQDk5epoll14EpollEventLoopHTCQGqQExQEs7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEmHTCQIzQHgQHb7signals__T26SignalFDEventDriverSignalsHTQFqZQBiZQIo6LookupVmi8192ZQKv7opApplyMFNbNfMDFNbNfmKQLfZiZi (this=<optimized out>, del=...) at utils.d:193
#4  0x00005555559ac85e in _D9eventcore7drivers5posix3dns__T18EventDriverDNS_GAIHTCQCcQBvQBq6events__T22PosixEventDriverEventsHTCQDwQDpQDk5epoll14EpollEventLoopHTCQFeQExQEs7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEmHTCQHnQHgQHb7signals__T26SignalFDEventDriverSignalsHTQFqZQBiZQIo11onDNSSignalMFNbNeSQKq6driver7EventIDZv (this=0x555555ca6460,
    event=...) at dns.d:151
#5  0x00005555559b1c87 in _D9eventcore7drivers5posix6events__T22PosixEventDriverEventsHTCQCjQCcQBx5epoll14EpollEventLoopHTCQDrQDkQDf7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEm7triggerMFNbNfSQGm6driver7EventIDbZ14__foreachbody3MFNbNfKDFNbNfQByZvZi (__applyArg0=...) at events.d:127
#6  0x00005555559c4158 in _D9eventcore8internal15consumablequeue__T15ConsumableQueueTDFNbNfSQCm6driver7EventIDZvZQBu13ConsumedRange7opApplyMFNbNfMDFNbNfKQCqZiZi (this=0x555555ca6520, del=...) at consumablequeue.d:155
#7  0x00005555559b1810 in _D9eventcore7drivers5posix6events__T22PosixEventDriverEventsHTCQCjQCcQBx5epoll14EpollEventLoopHTCQDrQDkQDf7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEm7triggerMFNbNfSQGm6driver7EventIDbZv (
    this=0x555555c6d360, event=..., notify_all=true) at events.d:124
#8  0x00005555559b1717 in _D9eventcore7drivers5posix6events__T22PosixEventDriverEventsHTCQCjQCcQBx5epoll14EpollEventLoopHTCQDrQDkQDf7sockets__T23PosixEventDriverSocketsHTQDeZQBfZQEm7onEventMFNbNeSQGm6driver2FDZv (this=0x555555c6d360,
    fd=...) at events.d:171
#9  0x00005555559aef21 in _D9eventcore7drivers5posix6driver14PosixEventLoop__T6notifyVEQChQCaQBvQBs9EventTypei0ZQBiMFNbNfmZv (this=0x555555c6fbc0, fd=9) at driver.d:349
#10 0x00005555559b1164 in eventcore.drivers.posix.epoll.EpollEventLoop.doProcessEvents(core.time.Duration) (
    this=0x555555c6fbc0, timeout=...) at epoll.d:56
#11 0x00005555559ae58b in _D9eventcore7drivers5posix6driver__T20PosixEventDriverCoreHTCQChQCaQBv5epoll14EpollEventLoopHTCQDpQDi5timer22LoopTimeoutTimerDriverHTCQFcQEvQEq6events__T22PosixEventDriverEventsHTQEpHTCQHbQGuQGp7sockets__T23PosixEventDriverSocketsHTQGqZQBfZQDjHTCQJkQJdQIy9processes__T25PosixEventDriverProcessesHTQJdZQBhZQKj13processEventsMFNbNfS4core4time8DurationZEQNlQMq10ExitReason (this=0x555555c72140, timeout=...) at driver.d:232
#12 0x000055555599a878 in vibe.core.task.TaskScheduler.waitAndProcess() (this=0x7ffff7304000) at task.d:869
#13 0x000055555597d684 in _D4vibe4coreQf12runEventLoopFNbNfZi () at core.d:220
#14 0x000055555564bbee in D main (args=...) at /root/agora/source/agora/node/main.d:85

The musl code can be seen here: https://github.com/ifduyue/musl/blob/79f653c6bc2881dd6855299c908a442f56cb7c2b/src/network/freeaddrinfo.c#L10

Geod24 commented 4 years ago

FYI @Cogitri

Geod24 commented 4 years ago

Tested, works.