Thread overview
Did you know that inline assembly is safe?
May 06, 2023
Dany12L
May 06, 2023
Quirin Schroll
May 06, 2023
Dany12L
May 06, 2023
Nick Treleaven
May 06, 2023

Hi,

I found that in D for some reason it is possible to declare the assembly inline as @safe, this honestly seems crazy to me... The compiler can't do any checks on the assembly code to ensure it's @safe and it's certainly up to the developer to check it but D is @trusted to do this.

void main() @safe {
    asm @safe {
        mov [RIP], 0;
    }
}

This code simply compiles and of course then you get SIGSEGV when you run it..

I believe inline assembly should only be allowed @system and @trusted, and under no circumstances should it allowed be @safe

May 06, 2023

On Saturday, 6 May 2023 at 14:55:08 UTC, Dany12L wrote:

>

Hi,

I found that in D for some reason it is possible to declare the assembly inline as @safe, this honestly seems crazy to me... The compiler can't do any checks on the assembly code to ensure it's @safe and it's certainly up to the developer to check it but D is @trusted to do this.

void main() @safe {
    asm @safe {
        mov [RIP], 0;
    }
}

This code simply compiles and of course then you get SIGSEGV when you run it..

I believe inline assembly should only be allowed @system and @trusted, and under no circumstances should it allowed be @safe

@safe should be immediately disallowed for asm. I guess, theoretically, assembly code can be @safe under very specific circumstances, e.g. code that writes no memory. Useful? I don’t think so.

May 06, 2023

On Saturday, 6 May 2023 at 16:23:22 UTC, Quirin Schroll wrote:

>

I guess,
theoretically, assembly code can be @safe under very specific circumstances, e.g. code that writes no memory. Useful? I don’t think so.

Of course, for example this:

void main() @safe {
    asm @safe {
        nop;
    }
}

Technically it would be right to mark it as @safe, but it must be the compiler to evaluate whether it is safe or not.

Until we have a technology to do this (and honestly it would be useless in my opinion for the use of inline asm) we should not allow @safe in inline assembly

May 06, 2023

On Saturday, 6 May 2023 at 14:55:08 UTC, Dany12L wrote:

>

I believe inline assembly should only be allowed @system and @trusted, and under no circumstances should it allowed be @safe

https://github.com/dlang/dmd/pull/15192