Mon Nov 24 15:33:18 2014 UTC ()
Fix ptrace %rcx corruption when pthread_errno() is used. Small example:
    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/ptrace.h>
    int main(void) { ptrace(18, getpid(), NULL, 0xabcd); }
The -lpthread cases pases 0 instead of 0xabcd


(christos)
diff -r1.5 -r1.6 src/lib/libc/arch/x86_64/sys/ptrace.S

cvs diff -r1.5 -r1.6 src/lib/libc/arch/x86_64/sys/ptrace.S (expand / switch to unified diff)

--- src/lib/libc/arch/x86_64/sys/ptrace.S 2014/05/22 15:01:57 1.5
+++ src/lib/libc/arch/x86_64/sys/ptrace.S 2014/11/24 15:33:18 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: ptrace.S,v 1.5 2014/05/22 15:01:57 uebayasi Exp $ */ 1/* $NetBSD: ptrace.S,v 1.6 2014/11/24 15:33:18 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1990 The Regents of the University of California. 4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz. 8 * William Jolitz.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -26,33 +26,42 @@ @@ -26,33 +26,42 @@
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE. 32 * SUCH DAMAGE.
33 * 33 *
34 * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90 34 * from: @(#)ptrace.s 5.1 (Berkeley) 4/23/90
35 */ 35 */
36 36
37#include <machine/asm.h> 37#include <machine/asm.h>
38#if defined(SYSLIBC_SCCS) && !defined(lint) 38#if defined(SYSLIBC_SCCS) && !defined(lint)
39 RCSID("$NetBSD: ptrace.S,v 1.5 2014/05/22 15:01:57 uebayasi Exp $") 39 RCSID("$NetBSD: ptrace.S,v 1.6 2014/11/24 15:33:18 christos Exp $")
40#endif /* SYSLIBC_SCCS and not lint */ 40#endif /* SYSLIBC_SCCS and not lint */
41 41
42#include "SYS.h" 42#include "SYS.h"
43 43
44 .globl _C_LABEL(__errno) 44 .globl _C_LABEL(__errno)
45 45
46ENTRY(ptrace) 46ENTRY(ptrace)
 47 /*
 48 * The following code calls __errno() to set it to 0 before
 49 * calling ptrace(2). The libc version of __errno() does not use
 50 * any registers, but the libpthread version clobbers %rcx
 51 * before we get a chance to store it in %r10. So we save it
 52 * in %r10 and restore it.
 53 */
 54 movq %rcx, %r10
47#ifdef __PIC__ 55#ifdef __PIC__
48 call PIC_PLT(_C_LABEL(__errno)) 56 call PIC_PLT(_C_LABEL(__errno))
49#else 57#else
50 call _C_LABEL(__errno) 58 call _C_LABEL(__errno)
51#endif /* __PIC__ */ 59#endif /* __PIC__ */
52 movl $0,(%rax) 60 movl $0,(%rax)
 61 movq %r10, %rcx
53 SYSTRAP(ptrace) 62 SYSTRAP(ptrace)
54 jc err 63 jc err
55 ret 64 ret
56err: 65err:
57 jmp CERROR 66 jmp CERROR
58END(ptrace) 67END(ptrace)