phobos icon indicating copy to clipboard operation
phobos copied to clipboard

`unpredictableSeed` doesn't work with ASAN

Open ljmf00-wekaio opened this issue 10 months ago • 4 comments
trafficstars

import std.random;

void main()
{
    auto n = unpredictableSeed!ulong;
}

Fails when compiling with ldc2 -fsanitize=address %s.

Even though -fsanitize=address is specific to LDC, this is mainly a problem with the current Phobos implementation, assuming that the compiler wont poison the assembly block. In the current form, the assembly block should be marked as naked and optimally, be on a separate function to completely avoid conflicting registers.

Also, compare and jump instructions shouldn't really be in an inline assembly block, but rather, on readable D code.

ljmf00-wekaio avatar Jan 07 '25 22:01 ljmf00-wekaio

CC @JohanEngelen

ljmf00-wekaio avatar Jan 07 '25 22:01 ljmf00-wekaio

I’d go as far as to say that this function should probably just call the system’s CSPRNG – instead of executing RDRAND by itself (assuming that’s available on the respective platform).

0xEAB avatar Jan 09 '25 00:01 0xEAB

I’d go as far as to say that this function should probably just call the system’s CSPRNG – instead of executing RDRAND by itself (assuming that’s available on the respective platform).

Arguably, but I wouldn't, unless its vDSO-based (if that is even possible to implement securely and fast enough), but mostly for performance reasons, although I can totally see a point security-wise, but if that's the argument, don't use unpredictableSeed at all. Accessing the random device with enough entropy is not strictly required here, and I think this function has a balanced trade-off of not calling a syscall, being reasonably random and being fast.

On a side note, specifically to LLVM, this should be probably an intrinsic.

ljmf00-wekaio avatar Jan 09 '25 08:01 ljmf00-wekaio

https://d.godbolt.org/z/3ej5G6Mf7

JohanEngelen avatar Jan 12 '25 15:01 JohanEngelen