Chinaunix首页 | 论坛 | 博客
  • 博客访问: 822321
  • 博文数量: 581
  • 博客积分: 7803
  • 博客等级: 少将
  • 技术积分: 3653
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 08:21
文章分类

全部博文(581)

文章存档

2013年(7)

2012年(414)

2011年(159)

2009年(1)

分类:

2012-03-12 12:31:59

原文地址:x86 linux内核栈溢出一则 作者:zboom

这是前一段时间遇到的一个问题,先看这段代码:

struct result{ 
    unsigned int a;
    unsigned int b;
    unsigned int c;
};

int func()
{
    struct result temp[512];
    memset(temp,0,sizeof(struct result)*512);
    /*use temp do something...*/
    return 0;
}

  这段代码在跑在mips平台的内核运行良好,但是到x86平台上,直接挂了,没有任何堆栈打印……
经高人指点,偶可能是遇到了传说中的栈溢出,做一个简单验证,将temp数组定义放到func前面,程序又恢复了正常。

于是乎采用kmalloc代替数组,问题解决。

分析一下内核栈大小,便可知道原因:
include/asm-i386/thread_info.h THREAD_SIZE
#ifdef CONFIG_4KSTACKS
#define THREAD_SIZE            (4096)
#else
#define THREAD_SIZE             (8192)
#endif

内核配置的时候,有一个4K内核栈的选项,如果选上了,内核栈为4K;如果没有选上,内核栈是8K。

    上面的代码的内核堆栈大小是4K,却申请了6K的结构,然后memset了一下,结果可想而知,
栈被写坏了。

参考:
http://blog.chinaunix.net/space.php?uid=20357359&do=blog&id=1963624
http://hi.baidu.com/whs08/blog/item/84437202d89a35074bfb5196.html


阅读(676) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~