• 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-2013 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_libcsetjmp.h"    // to keep _threadstate.h happy
38 #include "pub_core_threadstate.h"
39 #include "pub_core_aspacemgr.h"
40 #include "pub_core_libcbase.h"
41 #include "pub_core_libcassert.h"
42 #include "pub_core_libcprint.h"
43 #include "pub_core_machine.h"
44 #include "pub_core_options.h"
45 #include "pub_core_signals.h"
46 #include "pub_core_tooliface.h"
47 #include "pub_core_trampoline.h"
48 #include "pub_core_sigframe.h"      /* self */
49 
50 
51 /* Cheap-ass hack copied from ppc32-aix5 code, just to get started.
52    Produce a frame with layout entirely of our own choosing. */
53 
54 /* This module creates and removes signal frames for signal deliveries
55    on x86-darwin.  Kludgey; the machine state ought to be saved in a
56    ucontext and retrieved from it later, so the handler can modify it
57    and return.  However .. for now .. just stick the vex guest state
58    in the frame and snarf it again later.
59 
60    Also, don't bother with creating siginfo and ucontext in the
61    handler, although do point them somewhere non-faulting.
62 
63    Frame should have a 16-aligned size, just in case that turns out to
64    be important for Darwin.  (be conservative)
65 */
66 struct hacky_sigframe {
67    /* first four words look like a call to a 3-arg x86 function */
68    UInt             returnAddr;
69    UInt             a1_signo;
70    UInt             a2_siginfo;
71    UInt             a3_ucontext;
72    UChar            lower_guardzone[512];  // put nothing here
73    VexGuestX86State gst;
74    VexGuestX86State gshadow1;
75    VexGuestX86State gshadow2;
76    vki_siginfo_t    fake_siginfo;
77    struct vki_ucontext fake_ucontext;
78    UInt             magicPI;
79    UInt             sigNo_private;
80    vki_sigset_t     mask; // saved sigmask; restore when hdlr returns
81    UInt             __pad[1];
82    UChar            upper_guardzone[512]; // put nothing here
83    // and don't zero it, since that might overwrite the client's
84    // stack redzone, at least on archs which have one
85 };
86 
87 
88 /* Extend the stack segment downwards if needed so as to ensure the
89    new signal frames are mapped to something.  Return a Bool
90    indicating whether or not the operation was successful.
91 */
extend(ThreadState * tst,Addr addr,SizeT size)92 static Bool extend ( ThreadState *tst, Addr addr, SizeT size )
93 {
94    ThreadId tid = tst->tid;
95    /* For tracking memory events, indicate the entire frame has been
96       allocated.  Except, don't mess with the area which
97       overlaps the previous frame's redzone. */
98    /* XXX is the following call really right?  compared with the
99       amd64-linux version, this doesn't appear to handle the redzone
100       in the same way. */
101    VG_TRACK( new_mem_stack_signal,
102              addr - VG_STACK_REDZONE_SZB, size, tid );
103    return True;
104 }
105 
106 
107 /* Create a signal frame for thread 'tid'.  Make a 3-arg frame
108    regardless of whether the client originally requested a 1-arg
109    version (no SA_SIGINFO) or a 3-arg one (SA_SIGINFO) since in the
110    former case, the x86 calling conventions will simply cause the
111    extra 2 args to be ignored (inside the handler). */
VG_(sigframe_create)112 void VG_(sigframe_create) ( ThreadId tid,
113                             Addr sp_top_of_frame,
114                             const vki_siginfo_t *siginfo,
115                             const struct vki_ucontext *siguc,
116                             void *handler,
117                             UInt flags,
118                             const vki_sigset_t *mask,
119                             void *restorer )
120 {
121    ThreadState* tst;
122    Addr esp;
123    struct hacky_sigframe* frame;
124    Int sigNo = siginfo->si_signo;
125 
126    vg_assert(VG_IS_16_ALIGNED(sizeof(struct hacky_sigframe)));
127 
128    sp_top_of_frame &= ~0xf;
129    esp = sp_top_of_frame - sizeof(struct hacky_sigframe);
130    esp -= 4; /* ELF ABI says that esp+4 must be 16 aligned on
131                 entry to a function. */
132 
133    tst = VG_(get_ThreadState)(tid);
134    if (!extend(tst, esp, sp_top_of_frame - esp))
135       return;
136 
137    vg_assert(VG_IS_16_ALIGNED(esp+4));
138 
139    frame = (struct hacky_sigframe *) esp;
140 
141    /* clear it (very conservatively) (why so conservatively??) */
142    VG_(memset)(&frame->lower_guardzone, 0, 512);
143    VG_(memset)(&frame->gst,      0, sizeof(VexGuestX86State));
144    VG_(memset)(&frame->gshadow1, 0, sizeof(VexGuestX86State));
145    VG_(memset)(&frame->gshadow2, 0, sizeof(VexGuestX86State));
146    VG_(memset)(&frame->fake_siginfo,  0, sizeof(frame->fake_siginfo));
147    VG_(memset)(&frame->fake_ucontext, 0, sizeof(frame->fake_ucontext));
148 
149    /* save stuff in frame */
150    frame->gst           = tst->arch.vex;
151    frame->gshadow1      = tst->arch.vex_shadow1;
152    frame->gshadow2      = tst->arch.vex_shadow2;
153    frame->sigNo_private = sigNo;
154    frame->mask          = tst->sig_mask;
155    frame->magicPI       = 0x31415927;
156 
157    /* Minimally fill in the siginfo and ucontext.  Note, utter
158       lameness prevails.  Be underwhelmed, be very underwhelmed. */
159    frame->fake_siginfo.si_signo = sigNo;
160    frame->fake_siginfo.si_code  = siginfo->si_code;
161 
162    /* Set up stack pointer */
163    vg_assert(esp == (Addr)&frame->returnAddr);
164    VG_(set_SP)(tid, esp);
165    VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_STACK_PTR, sizeof(UInt));
166 
167    /* Set up program counter */
168    VG_(set_IP)(tid, (UInt)handler);
169    VG_TRACK( post_reg_write, Vg_CoreSignal, tid, VG_O_INSTR_PTR, sizeof(UInt));
170 
171    /* Set up RA and args for the frame */
172    VG_TRACK( pre_mem_write, Vg_CoreSignal, tid, "signal handler frame",
173              (Addr)frame, 4*sizeof(UInt) );
174    frame->returnAddr  = (UInt)&VG_(x86_darwin_SUBST_FOR_sigreturn);
175    frame->a1_signo    = sigNo;
176    frame->a2_siginfo  = (UInt)&frame->fake_siginfo;  /* oh well */
177    frame->a3_ucontext = (UInt)&frame->fake_ucontext; /* oh well */
178    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
179              (Addr)frame, 4*sizeof(UInt) );
180    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
181              (Addr)&frame->fake_siginfo, sizeof(frame->fake_siginfo));
182    VG_TRACK( post_mem_write, Vg_CoreSignal, tid,
183              (Addr)&frame->fake_ucontext, sizeof(frame->fake_ucontext));
184 
185    if (VG_(clo_trace_signals))
186       VG_(message)(Vg_DebugMsg,
187                    "sigframe_create (thread %d): "
188                    "next EIP=%#lx, next ESP=%#lx\n",
189                    tid, (Addr)handler, (Addr)frame );
190 }
191 
192 
193 /* Remove a signal frame from thread 'tid's stack, and restore the CPU
194    state from it.  Note, isRT is irrelevant here. */
VG_(sigframe_destroy)195 void VG_(sigframe_destroy)( ThreadId tid, Bool isRT )
196 {
197    ThreadState *tst;
198    Addr esp;
199    Int sigNo;
200    struct hacky_sigframe* frame;
201 
202    vg_assert(VG_(is_valid_tid)(tid));
203    tst = VG_(get_ThreadState)(tid);
204 
205    /* Check that the stack frame looks valid */
206    esp = VG_(get_SP)(tid);
207 
208    /* why -4 ? because the signal handler's return will have popped
209       the return address off the stack; and the return address is the
210       lowest-addressed element of hacky_sigframe. */
211    frame = (struct hacky_sigframe*)(esp - 4);
212    vg_assert(frame->magicPI == 0x31415927);
213 
214    /* This +8 is because of the -4 referred to in the ELF ABI comment
215       in VG_(sigframe_create) just above. */
216    vg_assert(VG_IS_16_ALIGNED((Addr)frame + 4));
217 
218    /* restore the entire guest state, and shadows, from the
219       frame.  Note, as per comments above, this is a kludge - should
220       restore it from saved ucontext.  Oh well. */
221    tst->arch.vex = frame->gst;
222    tst->arch.vex_shadow1 = frame->gshadow1;
223    tst->arch.vex_shadow2 = frame->gshadow2;
224    tst->sig_mask = frame->mask;
225    tst->tmp_sig_mask = frame->mask;
226    sigNo = frame->sigNo_private;
227 
228    if (VG_(clo_trace_signals))
229       VG_(message)(Vg_DebugMsg,
230                    "sigframe_destroy (thread %d): "
231                    "valid magic; next EIP=%#x\n",
232                    tid, tst->arch.vex.guest_EIP);
233 
234    VG_TRACK( die_mem_stack_signal,
235              (Addr)frame - VG_STACK_REDZONE_SZB,
236              sizeof(struct hacky_sigframe) );
237 
238    /* tell the tools */
239    VG_TRACK( post_deliver_signal, tid, sigNo );
240 }
241 
242 #endif // defined(VGP_x86_darwin)
243 
244 /*--------------------------------------------------------------------*/
245 /*--- end                                                          ---*/
246 /*--------------------------------------------------------------------*/
247