[LLVMbugs] [Bug 23154] New: pthread with ucontext on mac

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Apr 7 15:01:50 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=23154

            Bug ID: 23154
           Summary: pthread with ucontext on mac
           Product: clang
           Version: trunk
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: albertnetymk at gmail.com
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

I am using ucontext along with pthread. The below program works OK on Linux,
but has failed assertion on Mac.

The problem seems that thread local variables are not correctly accessed after
resuming the context from another thread.

The program creates two threads, A and B. A sets the context ready before B
could resume the context, for it's synced properly.

It's very appreciated if someone could shed some light on this behavior on mac.

ENV:

```
clang version 3.7.0  (http://llvm.org/git/llvm.git
8d70064a4ac2ae09b8003173e751cfad9dc15400)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
```
Program:

```
#define _XOPEN_SOURCE 800
#include <ucontext.h>
#include <signal.h>

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>

static int flag = 0;

void swap(ucontext_t *old, ucontext_t *new)
{
    int ret = swapcontext(old, new);
    assert(ret == 0);
}

#define SSIZE MINSIGSTKSZ

static char stack[SSIZE];
static ucontext_t a_ctx[2];
static ucontext_t b_ctx[2];

volatile static __thread int bug = 0;

static void func(int b) { }

static void f1 (void)
{
    assert(bug == 0);
    func(bug);
    swap(&a_ctx[1], &a_ctx[0]);
    assert(bug == 1);
}

void *thread_a(void *arg)
{
    printf("A is %lu\n", pthread_self());

    bug = 0;

    ucontext_t ctx = a_ctx[1];

    getcontext(&ctx);
    ctx.uc_stack.ss_sp = stack;
    ctx.uc_stack.ss_size = sizeof stack;
    makecontext(&ctx, f1, 0);

    swap(&a_ctx[0], &ctx);
    __atomic_store_n(&flag, 1, __ATOMIC_RELAXED);
    sleep(1);

    return NULL;
}

void *thread_b(void *arg)
{
    printf("B is %lu\n", pthread_self());

    bug = 1;
    while(__atomic_load_n(&flag, __ATOMIC_RELAXED) == 0) ;
    swap(&b_ctx[0], &a_ctx[1]);

    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t a, b;
    pthread_create(&b, NULL, &thread_b, NULL);
    pthread_create(&a, NULL, &thread_a, NULL);
    pthread_exit(NULL);
}
```

I posted it on
[SO](http://stackoverflow.com/questions/29430932/clang-bug-on-pthread-with-ucontext-on-mac)
as well, but no real answers so far.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150407/268df143/attachment.html>


More information about the llvm-bugs mailing list