ldc-developers/ldc — Issue #56: Null pointer deref in _d_arrayappendcTp with multithreaded code
Author: dsimcha
Created: 2011-12-21 20:53:54+00:00
State: open

GDC has basically the same bug, but DMD appears not to have this issue. It could be that druntime is doing something that's thread unsafe given certain compiler optimizations, but DMD doesn't do those optimizations.

import core.thread;

void main() {
auto t = new Thread(&doIt);
t.start();
doIt();
}

void doIt() {
while(1) {
int[] arr1, arr2;
double[] arr3;

foreach(i; 0..10_000) {
arr1 ~= 1;
arr2 ~= 2;
arr3 ~= 3;
}
}
}

GDB gives the following info:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000429abd in _d_arrayappendcTX (ti=0x444940, n=1) at lifetime.d:1754
1754 if(*(cast(size_t*)info.base) == size + offset)
(gdb) bt
#0 0x0000000000429abd in _d_arrayappendcTX (ti=0x444940, n=1) at lifetime.d:1754
#1 0x00000000004020e2 in _D4test4doItFZv ()
#2 0x0000000000402043 in _Dmain ()
#3 0x000000000043542f in _D2rt6dmain24mainUiPPaZi7runMainMFZv () at dmain2.d:513
#4 0x00000000004353aa in _D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv () at dmain2.d:488
#5 0x0000000000435487 in _D2rt6dmain24mainUiPPaZi6runAllMFZv () at dmain2.d:526
#6 0x00000000004353aa in _D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv () at dmain2.d:488
#7 0x0000000000435315 in main (argc=1, argv=0x7fffffffe508) at /cis/home/dsimcha/ldcStuff/ldc/.:536
(gdb) info registers
rax 0x0 0
rbx 0x0 0
rcx 0xffffffcf 4294967247
rdx 0xe054 57428
rsi 0x7ffff6726a74 140737328081524
rdi 0x10 16
rbp 0x7fffffffe200 0x7fffffffe200
rsp 0x7fffffffe010 0x7fffffffe010
r8 0x0 0
r9 0x229c 8860
r10 0x200000002 8589934594
r11 0x200000002 8589934594
r12 0x401ef0 4202224
r13 0x7fffffffe500 140737488348416
r14 0x0 0
r15 0x0 0
rip 0x429abd 0x429abd <_d_arrayappendcTX+541>
eflags 0x10206 [ PF IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0

And the instruction that the segfault occurs on is:

=> 0x0000000000429abd <+541>: cmp %rsi,(%rax)

So, since rax is zero, it's a null pointer deref.