ukontainer / frankenlibc

Tools for running rump unikernels in userspace
Other
4 stars 5 forks source link

arm32 ucontext issue #10

Open thehajime opened 4 years ago

thehajime commented 4 years ago

program using threads on arm32 may sometime crash.

conditions to crash

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>

static sem_t sem;

void *thread_func(void* args)
{
    int err;

    fprintf(stderr, "thread_func\n");
    int d =0;

    while (1) {
        fprintf(stdout, "go %d\n", d++);
        sleep(1);
        fprintf(stderr, "done %d\n", d++);
    }
    return NULL;
}

void test_socket(void)
{
    struct sockaddr_in saddr, saddr1;
    int sock0, sock1;
    int on = 1, err;
    socklen_t len;

    memset(&saddr, 0x0, sizeof(saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(0);
    saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    sock0 = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    setsockopt(sock0, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    err = bind(sock0, (struct sockaddr *)&saddr, sizeof(saddr));
    if (err < 0) perror("bind");
    err = listen(sock0, 1);
    if (err < 0) perror("listen");

    len = sizeof(saddr1);
    err = getsockname(sock0, (struct sockaddr *)&saddr1, &len);
    if (err < 0) perror("getsockname");
    sock1 = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    err = connect(sock1, (struct sockaddr *)&saddr1, sizeof(saddr1));
    if (err < 0) perror("connect");

}

int main(int argc, char *argv[])
{
    pthread_t thread;

    if (pthread_create(&thread, NULL, thread_func, (void *)NULL) != 0) {
        perror("pthread_create");
        return -1;
    }

    test_socket();

    if (pthread_join(thread, NULL) != 0) {
        perror("pthread_join");
        return -1;
    }

    return 0;
}