March 03, 2023

I couldn't figure out dustmite, so i started from 0 and managed to hit something:

https://github.com/ryuukk/dmd_bug

Assertion failed: array index out of bounds, file game\app.d, line 5

Wich indicates probably TLS problem?

This now reminds me of: https://issues.dlang.org/show_bug.cgi?id=23310

I am clueless what to do next, but at least now there is a small repro available for knowledgeable people

March 03, 2023

On Friday, 3 March 2023 at 01:07:07 UTC, ryuukk_ wrote:

>

I couldn't figure out dustmite, so i started from 0 and managed to hit something:

https://github.com/ryuukk/dmd_bug

Assertion failed: array index out of bounds, file game\app.d, line 5

Wich indicates probably TLS problem?

Yeah... rt.a.stuffs is a TLS variable. The D runtime manages TLS. extern(C) main bypasses D runtime initialization.

March 03, 2023

On Friday, 3 March 2023 at 01:11:06 UTC, Vladimir Panteleev wrote:

>

On Friday, 3 March 2023 at 01:07:07 UTC, ryuukk_ wrote:

>

I couldn't figure out dustmite, so i started from 0 and managed to hit something:

https://github.com/ryuukk/dmd_bug

Assertion failed: array index out of bounds, file game\app.d, line 5

Wich indicates probably TLS problem?

Yeah... rt.a.stuffs is a TLS variable. The D runtime manages TLS. extern(C) main bypasses D runtime initialization.

I have some questions:

  1. why does it work with LDC?
  2. why does it work with DMD when build/link in 2 step?
  3. why it doesn't work when DMD is invoked once for build/link
  4. is this a bug in DMD or my code is wrong?
  5. if it's wrong, then why does it compile/no error?
#!/usr/bin/env bash

set -e

build_dmd() {
	echo "build dmd"
	dmd \
	-debug -g \
	-m64 -vcolumns -betterC -w -i -i=-std -i=-core \
	-Igame \
	-Istuff/ \
	stuff/rt/object.d \
	game/app.d \
	-of=game.exe
}

build_ldc() {
	echo "build ldc"
	ldc2 \
	-d-debug -g \
	-m64 -vcolumns -betterC -w -i -i=-std -i=-core \
	-Igame \
	-Istuff/ \
	stuff/rt/object.d \
	game/app.d \
	-of=game.exe
}

build_dmd_and_link() {
	echo "build dmd and link (2step)"
	dmd \
	-debug -g -c \
	-m64 -vcolumns -betterC -w -i -i=-std -i=-core \
	-Igame \
	-Istuff/ \
	stuff/rt/object.d \
	game/app.d \
	-of=game.obj

	dmd \
	-debug -g  \
	-m64 -vcolumns -betterC -w -i -i=-std -i=-core \
	-Igame \
	-Istuff/ \
	game.obj \
	-of=game.exe
}


build_ldc
./game.exe
rm game.obj game.exe

build_dmd_and_link
./game.exe
rm game.obj game.exe

build_dmd
./game.exe
rm game.obj game.exe
March 03, 2023
This works:

```d
extern(C) void main()
{
    Stuff[5] temp = [
	Stuff(),
	Stuff(),
	Stuff(),
	Stuff(),
	Stuff(),
];
    stuffs = temp[];
	stuffs[0].do_something();
}
```

```d
Stuff[] stuffs;
```

The problem here is dmd isn't initializing TLS with a value, but TLS itself is working.
March 03, 2023
On Friday, 3 March 2023 at 01:24:42 UTC, Richard (Rikki) Andrew Cattermole wrote:
> This works:
>
> ```d
> extern(C) void main()
> {
>     Stuff[5] temp = [
> 	Stuff(),
> 	Stuff(),
> 	Stuff(),
> 	Stuff(),
> 	Stuff(),
> ];
>     stuffs = temp[];
> 	stuffs[0].do_something();
> }
> ```
>
> ```d
> Stuff[] stuffs;
> ```
>
> The problem here is dmd isn't initializing TLS with a value, but TLS itself is working.

So it is a DMD bug?
March 03, 2023
On 03/03/2023 2:33 PM, ryuukk_ wrote:
> So it is a DMD bug?

Yes and thanks to you I can now say that we can absolutely get rid of DllMain requirement for DLLs!
March 03, 2023
I added a note here: https://issues.dlang.org/show_bug.cgi?id=20737
March 03, 2023
On Friday, 3 March 2023 at 01:37:42 UTC, Richard (Rikki) Andrew Cattermole wrote:
> On 03/03/2023 2:33 PM, ryuukk_ wrote:
>> So it is a DMD bug?
>
> Yes and thanks to you I can now say that we can absolutely get rid of DllMain requirement for DLLs!

glad the outcome is positive, and i apologies about earlier comments, i may have sounded a little bit rude


March 03, 2023

On Friday, 3 March 2023 at 01:21:52 UTC, ryuukk_ wrote:

>

I have some questions:

  1. why does it work with LDC?
  2. why does it work with DMD when build/link in 2 step?
  3. why it doesn't work when DMD is invoked once for build/link

I think these are probably coincidences and the answer can be summarized as "that's what ends up happening due to the details of the implementation".

>
  1. is this a bug in DMD or my code is wrong?

I would say the code is wrong in principle, though as you've noted it can still work in some specific circumstances.

>
  1. if it's wrong, then why does it compile/no error?

extern(C) main is a low-level feature, because it effectively turns off parts of the language. However, the rest of the program doesn't know that this is the case - currently the compiler simply assumes you know what you're doing. Maybe it's not OK that it's easy to use it by accident.

1 2
Next ›   Last »