Skip to content

Instantly share code, notes, and snippets.

@nurse
Created February 5, 2013 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurse/4712877 to your computer and use it in GitHub Desktop.
Save nurse/4712877 to your computer and use it in GitHub Desktop.
A patch for darwin's Libc-825.25, which fixes a bug that backtrace(3) doesn't work if it is called from signal handler when it uses alternate signal stack. http://www.opensource.apple.com/source/Libc/Libc-825.25/gen/thread_stack_pcs.c
--- gen/thread_stack_pcs.c.orig 2013-02-04 15:51:02.000000000 +0900
+++ gen/thread_stack_pcs.c 2013-02-05 14:45:47.000000000 +0900
@@ -37,6 +37,7 @@
#endif
#define INSTACK(a) ((a) >= stackbot && (a) <= stacktop)
+#define INALTSTACK(a) ((a) >= altstackbot && (a) <= altstacktop)
#if defined(__ppc__) || defined(__ppc64__) || defined(__x86_64__)
#define ISALIGNED(a) ((((uintptr_t)(a)) & 0xf) == 0)
#elif defined(__arm__)
@@ -53,11 +54,19 @@ _thread_stack_pcs(vm_address_t *buffer,
pthread_t self = pthread_self();
void *stacktop = pthread_get_stackaddr_np(self);
void *stackbot = stacktop - pthread_get_stacksize_np(self);
+ stack_t ss;
+ int res = sigaltstack(NULL, &ss);
+ void *altstackbot, *altstacktop;
*nb = 0;
+ if (res) return;
+
+ altstackbot = ss.ss_sp;
+ altstacktop = (void *)((intptr_t)altstackbot + (intptr_t)ss.ss_size);
/* make sure return address is never out of bounds */
stacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *);
+ altstacktop -= (FP_LINK_OFFSET + 1) * sizeof(void *);
/*
* The original implementation called the first_frame_address() function,
@@ -73,18 +82,18 @@ _thread_stack_pcs(vm_address_t *buffer,
/* __builtin_frame_address IS BROKEN IN BEAKER: RADAR #2340421 */
__asm__ volatile("mr %0, r1" : "=r" (frame));
#endif
- if(!INSTACK(frame) || !ISALIGNED(frame))
+ if(!INSTACK(frame) || !INALTSTACK(frame) || !ISALIGNED(frame))
return;
#if defined(__ppc__) || defined(__ppc64__)
/* back up the stack pointer up over the current stack frame */
next = *(void **)frame;
- if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
+ if(!INSTACK(next) || !INALTSTACK(next) || !ISALIGNED(next) || next <= frame)
return;
frame = next;
#endif
while (skip--) {
next = *(void **)frame;
- if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
+ if(!INSTACK(next) || !INALTSTACK(next) || !ISALIGNED(next) || next <= frame)
return;
frame = next;
}
@@ -92,7 +101,7 @@ _thread_stack_pcs(vm_address_t *buffer,
buffer[*nb] = *(vm_address_t *)(((void **)frame) + FP_LINK_OFFSET);
(*nb)++;
next = *(void **)frame;
- if(!INSTACK(next) || !ISALIGNED(next) || next <= frame)
+ if(!INSTACK(next) || !INALTSTACK(next) || !ISALIGNED(next) || next <= frame)
return;
frame = next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment