All of lore.kernel.org
 help / color / mirror / Atom feed
* rt_sigreturn rejects a substitute stack frame as invalid.
@ 2014-08-18  1:03 Steven Stewart-Gallus
  2014-08-18 10:59 ` Mikael Pettersson
  0 siblings, 1 reply; 2+ messages in thread
From: Steven Stewart-Gallus @ 2014-08-18  1:03 UTC (permalink / raw)
  To: linux-kernel

Hello,

I'm not totally sure that GLibc's setcontext is safe to use in a
signal handler. So, I decided I was going to play things safe and let
rt_sigreturn switch stacks for me instead. However, rt_sigreturn seems
to reject my substitute stack frame as invalid and I'm not sure why.

Thank you,
Steven Stewart-Gallus

The code:

#include <stdio.h>
#include <signal.h>
#include <ucontext.h>
#include <unistd.h>

static ucontext_t alternate_context;

static char alternate_context_stack[SIGSTKSZ];

static char signal_stack[SIGSTKSZ];


static void alternate_context_func(void)
{
    puts("alternate context!");
}

static void switch_stack(int signo, siginfo_t *infop, void *untyped_ucontextp)
{
    ucontext_t * ucontextp = untyped_ucontextp;

    /* I'm not sure if setcontext is async-signal-safe so set the
     * context using the return from the signal handler.
     */

    *ucontextp = alternate_context;
#ifdef __linux__
    ucontextp->uc_mcontext.fpregs = &ucontextp->__fpregs_mem;
#endif
}

int main(void)
{
    {
        stack_t stack = { 0 };

        stack.ss_sp = signal_stack;
        stack.ss_size = sizeof signal_stack;

        sigaltstack(&stack, NULL);
    }

    getcontext(&alternate_context);
    alternate_context.uc_stack.ss_sp = alternate_context_stack;
    alternate_context.uc_stack.ss_size = sizeof alternate_context_stack;
    makecontext(&alternate_context, (void (*)(void))alternate_context_func, 0U);

    {
        struct sigaction action = { 0 };

        action.sa_sigaction = switch_stack;
        action.sa_flags = SA_SIGINFO;

        sigfillset(&action.sa_mask);

        sigaction(SIGRTMIN, &action, NULL);
    }

    raise(SIGRTMIN);

}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: rt_sigreturn rejects a substitute stack frame as invalid.
  2014-08-18  1:03 rt_sigreturn rejects a substitute stack frame as invalid Steven Stewart-Gallus
@ 2014-08-18 10:59 ` Mikael Pettersson
  0 siblings, 0 replies; 2+ messages in thread
From: Mikael Pettersson @ 2014-08-18 10:59 UTC (permalink / raw)
  To: Steven Stewart-Gallus; +Cc: linux-kernel

Steven Stewart-Gallus writes:
 > Hello,
 > 
 > I'm not totally sure that GLibc's setcontext is safe to use in a
 > signal handler. So, I decided I was going to play things safe and let
 > rt_sigreturn switch stacks for me instead. However, rt_sigreturn seems
 > to reject my substitute stack frame as invalid and I'm not sure why.

I did similar things at my previous work (doing dynamic binary
instrumentation and virtualization of user-space binaries; can't
share the code alas, it's proprietary), but my code operated
directly on top of the kernel/user-space API, using the actual
kernel/user-space data structures rather than glibc's fake ones.

If you're sure that it's the kernel's rt_sigreturn and not whatever
glibc runs before it that complains, then a simple way of debugging
this is to modify your kernel to printk some diagnostics whenever
rt_sigreturn decides to error out.

You may also want to check out the 'pth' package.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-18 10:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-18  1:03 rt_sigreturn rejects a substitute stack frame as invalid Steven Stewart-Gallus
2014-08-18 10:59 ` Mikael Pettersson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.