问题描述
我有一个像这样的简单模块:
I have a simple module like this:
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
int init_module(void) {
struct inode {
int i_ino;
};
struct dentry {
struct inode *d_inode;
};
struct dentry *f_dentry;
f_dentry = NULL;
struct inode * p = f_dentry->d_inode;
return 0;
}
void cleanup_module(void) {
printk("Goodbye world\n");
}
我的Makefile
是这样的:
obj-m += oops.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules$(shell uname -r)/build M=$(PWD) clean
我希望内核会崩溃,因为struct inode * p = f_dentry->d_inode;
取消引用了空指针,对吗?但事实并非如此.我的想法有什么问题吗?好吧,现在我再尝试一次.如果我的模块是这样的:
I expect that the kernel will crash because struct inode * p = f_dentry->d_inode;
has dereferenced a null pointer, right? But it does not. Anything wrong with my idea?All right, now I'll have one more try. If my module is like this:
#define MODULE
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
int init_module(void) {
*(int *)0 = 0;
return 0;
}
void cleanup_module(void) {
printk("Goodbye world\n");
}
我的计算机确实崩溃了.我以前的例子有什么问题吗?它不会取消引用空指针吗?
My computer really crashes. Or anything wrong with my former example? It doesn't dereference a null pointer?
推荐答案
如果您查看汇编代码(例如,通过objdump -D oops.ko),那么所有init_module()都会被优化,大概是因为它不会什么都不要做.
If you look at the assembly code(via e.g. objdump -D oops.ko), all of your init_module() is optimized away, presumably because it doesn't do anything.
例如做p->i_ino = 1;
,您可能会看到不同的结果(尽管这是未定义的行为,所以并不能直接推断出代码将要做什么的原因-在这种情况下也最好检查一下汇编).
If you e.g. do p->i_ino = 1;
, you'll likely see different results(Albeit this is undefined behavior, so it's not straight forward to reason about what the code is going to do - better check the assembly in this case too).
这篇关于取消引用空指针时内核崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!