valgrind and pthreads (on os x El Capitan): can

2019-07-08 05:05发布

问题:

A pthreads program seems to work fine for me on El Capitan, but when I run with valgrind I get a SIGSEGV crash 'Access not within mapped region at address 0x700002F32C05' from within pthread_join.

The stacktrace differs slightly, based on whether I'm trying to access a return-value from the thread or not (ends in _pthread_find_thread vs _OSSpinLockLockSlow).

My question: Am I mis-understanding how to use pthreads, or is this a valgrind-on-Yosemite issue? (Or, something else?)

I'm running valgrind 3.11, gcc clang-700.1.76 ('thread model: posix'), OS X 10.11.3. (I see just now that valgrind says v3.11.0 is "preliminary support for El Capitan", so maybe that's the issue?)

==== version A ====
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>

void *myThread() {
  int* retPtr = malloc(sizeof(int));
  *retPtr = 42;
  return (void*) retPtr;
  }

int main() {
   pthread_t tid;
   int *answer;

   pthread_create(&tid, NULL, myThread, NULL);
   pthread_join(tid, (void**)&answer);

   printf("%d\n", *answer);
   free((void*)answer);
   return 0;
}

==== Version B (EDIT: now pasted correctly) ====

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

void *myThread() { 
    return NULL;
    }

int main() {
    pthread_t tid;

    pthread_create(&tid, NULL, myThread, NULL);
    pthread_join(tid, NULL);

    return 0;
    }

==== stack trace for version A: ====

==39520== Process terminating with default action of signal 11 (SIGSEGV)
==39520==  Access not within mapped region at address 0x700002F32C3E
==39520==    at 0x10044437F: _pthread_find_thread (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100446CE2: _pthread_lookup_thread (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100446ABA: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==39520==    by 0x100000F27: main (hoo.c:15)

==== Stack trace for Version B: ====

==39528== Process terminating with default action of signal 11 (SIGSEGV)
==39528==  Access not within mapped region at address 0x700002F32C05
==39528==    at 0x100432E2B: _OSSpinLockLockSlow (in /usr/lib/system/libsystem_platform.dylib)
==39528==    by 0x100446B10: pthread_join (in /usr/lib/system/libsystem_pthread.dylib)
==39528==    by 0x100000F57: main (hoo.c:14)