Compare commits

...

3 Commits

5 changed files with 61 additions and 22 deletions

4
.gitignore vendored
View File

@ -20,3 +20,7 @@ ncscope.*
__pycache__
.vscode
.cache/
compile_commands.json

View File

@ -535,7 +535,7 @@ CONFIG_MAGIC_SYSRQ=y
CONFIG_PSTORE=y
CONFIG_PSTORE_CONSOLE=y
CONFIG_PSTORE_RAM=y
CONFIG_PSTORE_RAM=m
CONFIG_PSTORE_PMSG=y
#CONFIG_FUNCTION_TRACER=y

View File

@ -2,7 +2,7 @@
# Makefile for the Linux testings.
#
obj-m += oops.o
obj-m += test_ps.o
all:
$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNEL_DIR) M=$(shell pwd) modules

View File

@ -1,20 +0,0 @@
#include <linux/init.h>
#include <linux/module.h>
static int __init trigger_oops_init(void) {
int *ptr = (int *)0; // 强制类型转换0地址为指针并尝试读取
printk(KERN_ALERT "Oops will trigger now!\n");
printk(KERN_ALERT "Dereferenced NULL pointer value: %d\n", *ptr);
return 0; // 加载模块不应该有返回值但这里返回0避免编译警告。
}
static void __exit trigger_oops_exit(void) {
printk(KERN_ALERT "Oops trigger module exiting\n");
}
module_init(trigger_oops_init);
module_exit(trigger_oops_exit);
MODULE_LICENSE("Dual BSD/GPL");

View File

@ -0,0 +1,55 @@
#include "asm-generic/int-ll64.h"
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
static unsigned int test_type;
module_param(test_type, uint, 0400);
MODULE_PARM_DESC(test_type, "set to 1 to try to OOM (default 0)");
static int trigger_oops(void) {
int *ptr = (int *)0; // 强制类型转换0地址为指针并尝试读取
printk(KERN_ALERT "Dereferenced NULL pointer value: %d\n", *ptr);
return 0;
}
static int trigger_oom(void) {
void *memory = NULL;
size_t memory_size = 0;
printk(KERN_INFO "oom_trigger: Initializing the oom_trigger LKM\n");
memory_size = 100*1024*1024;
while (1) { // 尝试分配大块内存
memory = vmalloc(memory_size);
if (!memory) {
printk(KERN_ALERT "oom_trigger: Memory allocation failed\n");
break;
}
// 仅为防止编译器优化,实际不访问分配的内存
memset(memory, 0, memory_size);
}
return 0;
}
static int trigger_init(void) {
if (test_type)
trigger_oom();
else
trigger_oops();
return 0; // 加载模块不应该有返回值但这里返回0避免编译警告。
}
static void __exit trigger_exit(void) {
printk(KERN_INFO "oom_trigger: Exiting the oom_trigger LKM\n");
}
module_init(trigger_init);
module_exit(trigger_exit);