• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*--------------------------------------------------------------------*/
3 /*--- Create/destroy signal delivery frames.                       ---*/
4 /*---                                        sigframe-x86-darwin.c ---*/
5 /*--------------------------------------------------------------------*/
6 
7 /*
8    This file is part of Valgrind, a dynamic binary instrumentation
9    framework.
10 
11    Copyright (C) 2006-2010 OpenWorks Ltd
12       info@open-works.co.uk
13 
14    This program is free software; you can redistribute it and/or
15    modify it under the terms of the GNU General Public License as
16    published by the Free Software Foundation; either version 2 of the
17    License, or (at your option) any later version.
18 
19    This program is distributed in the hope that it will be useful, but
20    WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22    General Public License for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27    02111-1307, USA.
28 
29    The GNU General Public License is contained in the file COPYING.
30 */
31 
32 #if defined(VGP_x86_darwin)
33 
34 #include "pub_core_basics.h"
35 #include "pub_core_vki.h"
36 #include "pub_core_vkiscnums.h"
37 #include "pub_core_threadstate.h"
38 #include "pub_core_aspacemgr.h"
39 #include "pub_core_libcbase.h"
40 #include "pub_core_libcassert.h"
41 #include "pub_core_libcprint.h"
42 #include "pub_core_machine.h"
43 #include "pub_core_options.h"
44 #include "pub_core_signals.h"
45 #include "pub_core_tooliface.h"
46 #include "pub_core_trampoline.h"
47 #include "pub_core_sigframe.h"      /* self */
48 
49 
50 /* Cheap-ass hack copied from ppc32-aix5 code, just to get started.
51    Produce a frame with layout entirely of our own choosing. */
52 
53 /* This module creates and removes signal frames for signal deliveries
54    on x86-darwin.  Kludgey; the machine state ought to be saved in a
55    ucontext and retrieved from it later, so the handler can modify it
56    and return.  However .. for now .. just stick the vex guest state
57    in the frame and snarf it again later.
58 
59    Also, don't bother with creating siginfo and ucontext in the
60    handler, although do point them somewhere non-faulting.
61 
62    Frame should have a 16-aligned size, just in case that turns out to
63    be important for Darwin.  (be conservative)
64 */
65 struct hacky_sigframe {
66    /* first four words look like a call to a 3-arg x86 function */
67    UInt             returnAddr;
68    UInt             a1_signo;
69    UInt             a2_siginfo;
70    UInt             a3_ucontext;
71    UChar            lower_guardzone[512];  // put nothing here
72    VexGuestX86State gst;
73    VexGuestX86State gshadow1;
74    VexGuestX86State gshadow2;
75    vki_siginfo_t    fake_siginfo;
76    struct vki_ucontext fake_ucontext;
77    UInt             magicPI;
78    UInt             sigNo_private;
79    vki_sigset_t     mask; // saved sigmask; restore when hdlr returns
80    UInt             __pad[1];
81    UChar            upper_guardzone[512]; // put nothing here
82    // and don't zero it, since that might overwrite the client's
83    // stack redzone, at least on archs which have one
84 };
85 
86 
87 /* Extend the stack segment downwards if needed so as to ensure the
88    new signal frames are mapped to something.  Return a Bool
89    indicating whether or not the operation was successful.
90 */
extend(ThreadState * tst,Addr addr,SizeT size)91 static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
92 {
93    ThreadId tid = tst->tid;
94    /* For tracking memory events, indicate the entire frame has been
95       allocated.  Except, don't mess with the area which
96       overlaps the previous frame's redzone. */
97    /* XXX is the following call really right?  compared with the
98       amd64-linux version, this doesn't appear to handle the redzone
99       in the same way. */
100    VG_TRACK( new_mem_stack_signal,
101              addr - VG_STACK_REDZONE_SZB, size, tid );
102    return True;
103 }
104 
105 
106 /* Create a signal frame for thread 'tid'.  Make a 3-arg frame
107    regardless of whether the client originally requested a 1-arg
108    version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
109    former case, the x86 calling conventions will simply cause the
110    extra 2 args to be ignored (inside the handler). */
VG_(sigframe_create)111 void VG_(sigframe_create) ( ThreadId tid,
112                             Addr sp_top_of_frame,
113                             const vki_siginfo_t *siginfo,
114                             const struct vki_ucontext *siguc,
115                             void *handler,
116                             UInt flags,
117                             const vki_sigset_t *mask,
118                             void *restorer )
119 {
120    ThreadState* tst;
121    Addr esp;
122    struct hacky_sigframe* frame;
123    Int sigNo = siginfo->si_signo;
124 
125    vg_assert(VG_IS_16_ALIGNED(sizeof(struct hacky_sigframe)));
126 
127    sp_top_of_frame &= ~0xf;
128    esp = sp_top_of_frame - sizeof(struct hacky_sigframe);
129 
130    tst = VG_(get_ThreadState)(tid);
131    if (!extend(tst, esp, sp_top_of_frame - esp))
132       return;
133 
134    vg_assert(VG_IS_16_ALIGNED(esp));
135 
136    frame = (struct hacky_sigframe *) esp;
137 
138    /* clear it (very conservatively) (why so conservatively??) */
139    VG_(memset)(&frame->lower_guardzone, 0, 512);
140    VG_(memset)(&frame->gst,      0, sizeof(VexGuestX86State));
141    VG_(memset)(&frame->gshadow1, 0, sizeof(VexGuestX86State));
142    VG_(memset)(&frame->gshadow2, 0, sizeof(VexGuestX86State));
143    VG_(memset)(&frame->fake_siginfo,  0, sizeof(frame->fake_siginfo));
144    VG_(memset)(&frame->fake_ucontext, 0, sizeof(frame->fake_ucontext));
145 
146    /* save stuff in frame */
147    frame->gst           = tst->arch.vex;
148    frame->gshadow1      = tst->arch.vex_shadow1;
149    frame->gshadow2      = tst->arch.vex_shadow2;
150    frame->sigNo_private = sigNo;
151    frame->mask          = tst->sig_mask;
152    frame->magicPI       = 0x31415927;
153 
154    /* Minimally fill in the siginfo and ucontext.  Note, utter
155       lameness prevails.  Be underwhelmed, be very underwhelmed. */
156    frame->fake_siginfo.si_signo = sigNo;
157    frame->fake_siginfo.si_code  = siginfo->si_code;
158 
159    /* Set up stack pointer */
160    vg_assert(esp == (Addr)&frame->returnAddr);
161    VG_(set_SP)(tid, esp);
162    VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR, sizeof(UInt));
163 
164    /* Set up program counter */
165    VG_(set_IP)(tid, (UInt)handler);
166    VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_INSTR_PTR, sizeof(UInt));
167 
168    /* Set up RA and args for the frame */
169    VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame",
170              (Addr)frame, 4*sizeof(UInt) );
171    frame->returnAddr  = (UInt)&VG_(x86_darwin_SUBST_FOR_sigreturn);
172    frame->a1_signo    = sigNo;
173    frame->a2_siginfo  = (UInt)&frame->fake_siginfo;  /* oh well */
174    frame->a3_ucontext = (UInt)&frame->fake_ucontext; /* oh well */
175    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
176              (Addr)frame, 4*sizeof(UInt) );
177    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
178              (Addr)&frame->fake_siginfo, sizeof(frame->fake_siginfo));
179    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
180              (Addr)&frame->fake_ucontext, sizeof(frame->fake_ucontext));
181 
182    if (VG_(clo_trace_signals))
183       VG_(message)(Vg_DebugMsg,
184                    "sigframe_create (thread %d): next EIP=%#lx, next ESP=%#lx",
185                    tid, (Addr)handler, (Addr)frame );
186 }
187 
188 
189 /* Remove a signal frame from thread 'tid's stack, and restore the CPU
190    state from it.  Note, isRT is irrelevant here. */
VG_(sigframe_destroy)191 void VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
192 {
193    ThreadState *tst;
194    Addr esp;
195    Int sigNo;
196    struct hacky_sigframe* frame;
197 
198    vg_assert(VG_(is_valid_tid)(tid));
199    tst = VG_(get_ThreadState)(tid);
200 
201    /* Check that the stack frame looks valid */
202    esp = VG_(get_SP)(tid);
203 
204    /* why -4 ? because the signal handler's return will have popped
205       the return address of the stack; and the return address is the
206       lowest-addressed element of hacky_sigframe. */
207    frame = (struct hacky_sigframe*)(esp - 4);
208    vg_assert(frame->magicPI == 0x31415927);
209    vg_assert(VG_IS_16_ALIGNED(frame));
210 
211    /* restore the entire guest state, and shadows, from the
212       frame.  Note, as per comments above, this is a kludge - should
213       restore it from saved ucontext.  Oh well. */
214    tst->arch.vex = frame->gst;
215    tst->arch.vex_shadow1 = frame->gshadow1;
216    tst->arch.vex_shadow2 = frame->gshadow2;
217    tst->sig_mask = frame->mask;
218    tst->tmp_sig_mask = frame->mask;
219    sigNo = frame->sigNo_private;
220 
221    if (VG_(clo_trace_signals))
222       VG_(message)(Vg_DebugMsg,
223                    "sigframe_destroy (thread %d): valid magic; next EIP=%#x",
224                    tid, tst->arch.vex.guest_EIP);
225 
226    VG_TRACK( die_mem_stack_signal,
227              (Addr)frame - VG_STACK_REDZONE_SZB,
228              sizeof(struct hacky_sigframe) );
229 
230    /* tell the tools */
231    VG_TRACK( post_deliver_signal, tid, sigNo );
232 }
233 
234 #endif // defined(VGP_x86_darwin)
235 
236 /*--------------------------------------------------------------------*/
237 /*--- end                                                          ---*/
238 /*--------------------------------------------------------------------*/
239