dlang bug - accessing module variable from method segfaults only when using module reference directly

Chris Katko ckatko at gmail.com
Fri Jul 1 12:57:01 UTC 2022


dmd (but I think LDC also is affected)

this bug has bit me multiple times now, to the point I can 
recognize it. Accessing module variables, from inside a method, 
causes a segfault. Even if the variable should be available by 
then through the call order. Proving that its a bug, you can 
directly send the exact same variable through an argument, and it 
works fine.


(not sure if this code will do it, last time I tried to replicate 
it with solely this kind of code, the bug disappeared.)

````D

/// module g.d
class world
{
atlasHandler atlas;

void do()
	{
	atlas = new AtlasHanlder();
	elf e = new elf(atlas);
	}
}

/// module 'objects.d'
class atlasHandler{}

class elf
{
this(atlasHandler atlas)
	{
	assert(atlas !is null); //works fine
	assert(g.world.atlas !is null); //crashes

	writefln("atlas [%p] vs g.world.atlas [%s]", atlas, 
g.world.atlas);
	// crashes trying to read g.world.atlas
	}
}
````

gdb output (not the exact same module names but you get the 
point):
````
Thread 1 "main" received signal SIGSEGV, Segmentation fault.

(gdb) p atlas
$1 = (objects.atlasHandler *)
(gdb) p g.world
$2 = (worldmod.world_t *)
(gdb) p g.world.atlas
Cannot access memory at address 0x10
(gdb) p this
$3 = (objects.elf *)
(gdb) x atlas
0x7fffec1cf380:	0x55826580
(gdb) x g.world.atlas
Cannot access memory at address 0x10
````

It appears that whatever value its sending, is in a protected 
memory segment and automatically segfaulting even upon reading.

Worst case I can public my repo and you can see it for yourself.


More information about the Digitalmars-d-learn mailing list