一、 测试用例
写了一个简单的测试程序进行测试:
代码如下:
#include <linux/compat.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/phantom.h>
static int __init test_init(void)
{
printk("drivr test \n");
return 0;
}
static void test_crash(void)
{
char *pstr = NULL;
printk("drivr exit \n");
printk("drivr exit 111 \n");
*pstr = 12;
printk("%s \n",pstr);
return;
}
static void __exit test_exit(void)
{
test_crash();
return ;
}
module_init(test_init);
module_exit(test_exit);
MODULE_AUTHOR("Alex<free5home5@163.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("test driver");
MODULE_VERSION("v0.0.1");
我把它编译成了一个叫 driver_test.ko的驱动文件
执行:insmod driver_test.ko
然后执行rmmod driver_test.ko
内核直接崩溃了。
二、错误分析
代码一看就明白了,对空指针进行操作导致的崩溃。
错误打印如下:
Unable to handle kernel NULL pointer dereference at virtual address 00000000
Mem abort info:
ESR = 0x96000046
Exception class = DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
Data abort info:
ISV = 0, ISS = 0x00000070