assorted/kernel-thread-repro/repro.cc

35 lines
1,004 B
C++

#include <sched.h>
#include <sys/wait.h>
#include <unistd.h>
void *threadSafeAlloc(size_t n) {
static size_t end_index = 0;
static char buffer[1 << 25];
size_t start_index = __atomic_load_n(&end_index, __ATOMIC_SEQ_CST);
while (1) {
if (start_index + n > sizeof(buffer)) _exit(1);
if (__atomic_compare_exchange_n(&end_index, &start_index, start_index + n, 1, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) return buffer + start_index;
}
}
int thread(void *arg) {
size_t i;
size_t n = 1 << 7;
char *items;
(void)arg;
while (1) {
items = threadSafeAlloc(n);
for (i = 0; i != n; i += 1) items[i] = '@';
for (i = 0; i != n; i += 1) if (items[i] != '@') _exit(2);
}
}
int main(void) {
static size_t stacks[2][1 << 9];
size_t i;
for (i = 0; i != 2; i += 1) clone(&thread, &stacks[i] + 1, CLONE_THREAD | CLONE_VM | CLONE_SIGHAND, NULL);
while (1) {
if (fork() == 0) _exit(0);
(void)wait(NULL);
}
}