检查备用信号栈(不同的办法来分配的话) [英] Examining alternate signal stack (Different ways to allocate it)

查看:227
本文介绍了检查备用信号栈(不同的办法来分配的话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在备用信号栈(男人SIGALTSTACK )。

I am experimenting on alternate signal stack(man sigaltstack).

两件$ C $的Ç不同的分配堆栈:

Two pieces of code allocating stack differently:

int method1(void)
{
    struct sigaction act, oldact;

    memset(&act, 0, sizeof(act));
    act.sa_sigaction = SignalHandler;
    act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
    sigemptyset(&act.sa_mask);
    if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
        ALOGW("sigaction failed %s\n", strerror(errno));
        return -errno;
    }

    return 0;
}

我只是用SA_ONSTACK而注册的信号。而信号线被安排在pthread_create的,如果这个标志设置,堆栈的8KB分配如下( SIGSTKSZ =为0x2000(8KB)

  ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  if (ss.ss_sp != MAP_FAILED) {
    ss.ss_size = SIGSTKSZ;
    ss.ss_flags = 0;
    sigaltstack(&ss, NULL);
    thread->alternate_signal_stack = ss.ss_sp;
  }

做同样的事情,而所有注册信号处理程序的另一种方式。

Another way of doing the same thing, all while registering the signal handler.

int method2(void)
{
    struct sigaction act, oldact;
    stack_t ss;

    ss.ss_sp = malloc(SIGSTKSZ);
    if (ss.ss_sp == NULL)
        return -ENOMEM;

    ss.ss_size = SIGSTKSZ;
    ss.ss_flags = 0;
    sigaltstack(&ss, NULL);

    memset(&act, 0, sizeof(act));
    act.sa_sigaction = SignalHandler;
    act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
    sigemptyset(&act.sa_mask);
    if (sigaction(THREAD_SIGNAL, &act, &oldact) != 0) {
        ALOGW("sigaction failed %s\n", strerror(errno));
        return -errno;
    }

    return 0;
}

在这种情况下,我不靠仿生分配我默认堆栈。我分配我自己的堆栈,并使用它。

In this case, I am not relying on bionic to allocate me the default stack. I am allocating my own stack and using it.

因此​​,在这两种情况下,我分配8KB的信号栈。

So, in both these cases I am allocating signal stacks of 8kb.

我已经把而(1)信号处理器中,并检查 proc /进程/图后发出信号到处理

I have put a while(1) inside signal handler and checked proc/pid/maps after sending a signal to the process.

下面是结果:

方法1(通过堆栈中的仿生分配pthread_create的):

Method 1 (stack allocated by bionic in pthread_create):

7faa8d1000-7faa8d3000 rw-p 00000000 00:00 0        [stack:6633]

方法2(堆栈使用malloc由应用程序分配):

Method 2 (stack allocated using malloc by the application):

7fb7300000-7fb7500000 rw-p 00000000 00:00 0        [stack:6567]

但奇怪的是,当我使用方法2的malloc()只分配了8KB堆栈,堆栈似乎2MB左右已经分配(0x200000)。

The strange thing is that while I allocated only 8kb of stack using malloc() in method2, the stack seems to have allocated around 2MB(0x200000).

请指点我在哪里出了问题或者是预期的行为。

Kindly advice me on what went wrong or is it expected behaviour.

推荐答案

我见过多次在哪里就PROCFS堆栈中的信息不正确。尝试打印局部变量的地址信号处理程序。我敢打赌,你会发现,你看到堆栈主堆栈。

I've seen times where the information in procfs regarding the stack is incorrect. Try printing the address of a local variable in the signal handler. My bet is that you will find the stack you're seeing is the main stack.

我不知道为什么会这样,但它看起来就像是一个核心的东西。我已经看到了在Linux上了。

I don't know why this happens but it looks like it's a kernel thing. I've seen it on Linux too.

这篇关于检查备用信号栈(不同的办法来分配的话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆