内核模块中的 Stackoverflow

Stackoverflow in kernel module

我在读罗伯特·洛夫的书 "Linux Kernel. Development. Third Edition."。

我在这本书中读到的关于堆栈大小的内容:

On x86, the stack size is configurable at compile-time and can be either 4KB or 8KB. Historically, the kernel stack is two pages, which generally implies that it is 8KB on 32-bit architectures and 16KB on 64-bit architectures—this size is fixed and absolute

我有 ubuntu 16.06 64 位 4.15 内核的虚拟机。所以我的堆栈大小应该是 16000 字节 (16KB)

我尝试检查 Whosebug 行为。我在使用超过 16000 个字节的堆栈上创建数组。

#include <linux/module.h>
#include <linux/init.h>

int __init overflow_start(void)
{
        printk(KERN_INFO "Overflow Test\n");
        char array[170000] = {[0 ... 16999] =  'A'};

        printk(KERN_ERR "%c\n", array[16999]);

        return 0;
}

void __exit overflow_end(void)
{
        printk(KERN_ERR "Test success\n");
}

module_init(overflow_start);
module_exit(overflow_end);

MODULE_LICENSE("GPL");

我认为我应该看到内核恐慌与堆栈粉碎或类似的东西,但我只看到正确的输出。为什么它不打破堆栈?

您也可以将数组设置为可变的,GCC 将不会对其进行优化。

volatile char array[170000] = {[0 ... 16999] =  'A'};