properly access mcontext_t program counter in cancellation handler

using the actual mcontext_t definition rather than an overlaid pointer
array both improves correctness/readability and eliminates some ugly
hacks for archs with 64-bit registers bit 32-bit program counter.

also fix UB due to comparison of pointers not in a common array
object.
This commit is contained in:
Rich Felker 2015-11-02 12:39:28 -05:00
parent fead7e3fc0
commit cb1bf2f321
11 changed files with 14 additions and 15 deletions

View File

@ -8,4 +8,4 @@ static inline struct pthread *__pthread_self()
#define TLS_ABOVE_TP #define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 16) #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 16)
#define CANCEL_REG_IP 33 #define MC_PC pc

View File

@ -27,4 +27,4 @@ static inline pthread_t __pthread_self()
#define TLS_ABOVE_TP #define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 8) #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 8)
#define CANCEL_REG_IP 18 #define MC_PC arm_pc

View File

@ -7,4 +7,4 @@ static inline struct pthread *__pthread_self()
#define TP_ADJ(p) (p) #define TP_ADJ(p) (p)
#define CANCEL_REG_IP 14 #define MC_PC gregs[REG_EIP]

View File

@ -7,4 +7,4 @@ static inline struct pthread *__pthread_self()
#define TP_ADJ(p) (p) #define TP_ADJ(p) (p)
#define CANCEL_REG_IP 32 #define MC_PC regs.pc

View File

@ -16,4 +16,4 @@ static inline struct pthread *__pthread_self()
#define DTP_OFFSET 0x8000 #define DTP_OFFSET 0x8000
#define CANCEL_REG_IP (3-(union {int __i; char __b;}){1}.__b) #define MC_PC pc

View File

@ -14,5 +14,4 @@ static inline struct pthread *__pthread_self()
#define TLS_ABOVE_TP #define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread)) #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread))
/* word-offset to 'pc' in mcontext_t */ #define MC_PC regs.pc
#define CANCEL_REG_IP 32

View File

@ -15,9 +15,8 @@ static inline struct pthread *__pthread_self()
#define DTP_OFFSET 0x8000 #define DTP_OFFSET 0x8000
// offset of the PC register in mcontext_t, divided by the system wordsize
// the kernel calls the ip "nip", it's the first saved value after the 32 // the kernel calls the ip "nip", it's the first saved value after the 32
// GPRs. // GPRs.
#define CANCEL_REG_IP 32 #define MC_PC gregs[32]
#define CANARY canary_at_end #define CANARY canary_at_end

View File

@ -8,4 +8,4 @@ static inline struct pthread *__pthread_self()
#define TLS_ABOVE_TP #define TLS_ABOVE_TP
#define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 8) #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) - 8)
#define CANCEL_REG_IP 17 #define MC_PC sc_pc

View File

@ -7,6 +7,6 @@ static inline struct pthread *__pthread_self()
#define TP_ADJ(p) (p) #define TP_ADJ(p) (p)
#define CANCEL_REG_IP 32 #define MC_PC gregs[REG_RIP]
#define CANARY canary2 #define CANARY canary2

View File

@ -7,4 +7,4 @@ static inline struct pthread *__pthread_self()
#define TP_ADJ(p) (p) #define TP_ADJ(p) (p)
#define CANCEL_REG_IP 16 #define MC_PC gregs[REG_RIP]

View File

@ -1,3 +1,4 @@
#define _GNU_SOURCE
#include <string.h> #include <string.h>
#include "pthread_impl.h" #include "pthread_impl.h"
#include "syscall.h" #include "syscall.h"
@ -61,15 +62,15 @@ static void cancel_handler(int sig, siginfo_t *si, void *ctx)
{ {
pthread_t self = __pthread_self(); pthread_t self = __pthread_self();
ucontext_t *uc = ctx; ucontext_t *uc = ctx;
const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP]; uintptr_t pc = uc->uc_mcontext.MC_PC;
a_barrier(); a_barrier();
if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return; if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
_sigaddset(&uc->uc_sigmask, SIGCANCEL); _sigaddset(&uc->uc_sigmask, SIGCANCEL);
if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) { if (self->cancelasync || pc >= (uintptr_t)__cp_begin && pc < (uintptr_t)__cp_end) {
((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel; uc->uc_mcontext.MC_PC = (uintptr_t)__cp_cancel;
return; return;
} }