mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Stewart-Gallus <sstewartgallus00@mylangara.bc.ca>
To: linux-kernel@vger.kernel.org
Subject: rt_sigreturn rejects a substitute stack frame as invalid.
Date: Mon, 18 Aug 2014 01:03:55 +0000 (GMT)	[thread overview]
Message-ID: <fadbcf1c3708.53f150fb@langara.bc.ca> (raw)

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);

}


             reply	other threads:[~2014-08-18  1:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-18  1:03 Steven Stewart-Gallus [this message]
2014-08-18 10:59 ` Mikael Pettersson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fadbcf1c3708.53f150fb@langara.bc.ca \
    --to=sstewartgallus00@mylangara.bc.ca \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome