Remix.run Logo
jstanley 2 hours ago

This is not the first RNG bug on Zen 2, I recall after I first got mine that some application or other would quit immediately at startup because rdrand always returned -1, i.e. all 1s. It was fixed with a microcode update.

Do we now learn that they fixed "always generate all 1s" with "never generate all 0s"??

EDIT: I've been unable to reproduce the problem on my CPU, FWIW. It's a Ryzen 5 3600.

EDIT2: OK, update, I can reproduce it with rdrand16, rdrand32 is fine but rdrand16 can never generate all 0s. So my CPU does have this problem!

0x000xca0xfe an hour ago | parent | next [-]

I can reproduce it too with rdrand16 on Zen2.

But it looks like the rdrand16 instruction can produce zeros just fine, it just sets CF=0 erroneously (indicating an error and that the user program should retry).

So keep that in mind when you try to reproduce it too and use some abstraction that could implement retries internally.

dooglius an hour ago | parent [-]

Good observation, that seems like the most likely explanation. Do you ever see "true" CF=0 (with nonzero arg) or did they just take the lazy approach?

yk 2 hours ago | parent | prev | next [-]

    return 4 # Determined by fair dice roll.
rbanffy 2 hours ago | parent | next [-]

I always think of https://www.reddit.com/r/ProgrammerHumor/comments/5yhl93/ran...

Gander5739 2 hours ago | parent | prev [-]

https://xkcd.com/221/ for those not in the know

lathiat 2 hours ago | parent | next [-]

And for the full fail story behind it, fail0verflow hacking the PS3 presentation is great and covers the bug: https://youtu.be/DUGGJpn2_zY

Most of the console hacking talks are great, both informative and entertaining.

einsteinx2 an hour ago | parent [-]

This comic predates that presentation, in fact they use it in their slide deck at 39:00 in your linked video.

That presentation is awesome though, worth a watch either way!

matja 2 hours ago | parent | prev | next [-]

CVE-2008-0166 (Debian OpenSSL Predictable PRNG Vulnerability) inspired xkcd/221 but this sort of thing happens a lot :)

Betelbuddy 2 hours ago | parent | prev [-]

https://i.imgur.com/bwFWMqQ.png

2 hours ago | parent [-]
[deleted]
peri-cl an hour ago | parent | prev | next [-]

Zen 4 reporting in. I'm unable to reproduce it (7840U).

   $ ./a.out | rg '\b\-?\d\b' | sort -n | uniq -c
   15281 -2
   15192 -1
   15273 0
   15243 1
   15269 2
I used the GCC intrinsic ( _rdrand16_step ),

    #include <immintrin.h>
    
    short rdrand16() {     // gcc -mrdrnd
        short ret;
        while (1 != _rdrand16_step(&ret)) { }    
        return ret;
    }
RandomOnyx 2 hours ago | parent | prev | next [-]

Does rdrand32 and then taking the lowest 16 bits of its result yield any zeroes?

Basically I'm wondering if it's a bug in the version of the instruction that writes to a 16-bit reg, or a bug in the underlying RNG

jstanley 2 hours ago | parent [-]

Yes it does. rdrand32()%65535 was my first attempt, and generated zeroes at about the expected rate, that's why I initially erroneously thought my CPU did not have this problem.

goalieca 2 hours ago | parent | next [-]

You should be using &0xFFFF for masking. Your mod is off by 1 too.

jstanley an hour ago | parent [-]

You're right, the code was correct but my comment above is wrong.

RandomOnyx 2 hours ago | parent | prev [-]

How about* rdrand32()%65536? Taking the remainder by 65535 doesn't take the lowest 16 bits after all

*: missed a word the first time around

JdeBP 2 hours ago | parent | prev | next [-]

You probably recall https://news.ycombinator.com/item?id=19848953 .

rbanffy 2 hours ago | parent | prev [-]

Even if you reproduce the issue, it is not a proof it can't generate a zero - just that it's very unlikely.

To prove it, we'd need to examine the chip and its microcode.