Skip to content

Instantly share code, notes, and snippets.

@disconnect3d
Last active June 14, 2020 12:30
Show Gist options
  • Save disconnect3d/4c87c15fc03009a47fa703b7379f6682 to your computer and use it in GitHub Desktop.
Save disconnect3d/4c87c15fc03009a47fa703b7379f6682 to your computer and use it in GitHub Desktop.
An attempt to make RDRAND step to fail
// g++ spam.cpp -O3 -lpthread -std=c++14 && ./a.out
#include <stdio.h>
#include <stdint.h>
#include <thread>
#include <immintrin.h>
__attribute__ ((target ("rdrnd")))
void exec(int id) {
printf("[thread %d]\n", id);
long long unsigned int i=0, num=0;
while (1) {
if (_rdrand64_step(&num) != 1)
abort();
}
}
int main() {
#define N 16
std::thread t[N];
for (int i=0; i<N; ++i)
t[i] = std::thread(exec, i);
exec(N+1);
}
@disconnect3d
Copy link
Author

Another attempt - a more tight loop (thx to a friend of mine):

// g++ -masm=intel spam.cpp -lpthread -std=c++14 && ./a.out
#include <stdio.h>
#include <thread>

void exec(int id) {
    printf("[thread %d]\n", id);
    __asm__("loop: rdrand rax\njb loop");
    abort();
}

int main() {
    #define N 8
    std::thread t[N - 1];
    for (int i = 0; i < N - 1; i++)
        t[i] = std::thread(exec, i);
    exec(N);
}

...still no failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment