[Mod] 完善测试用例test_ps,支持test_type:0,Oops测试;1 Oom测试;
This commit is contained in:
@ -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
|
||||
|
||||
@ -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");
|
||||
55
osdrv/extdrv/testing/test_ps.c
Normal file
55
osdrv/extdrv/testing/test_ps.c
Normal 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);
|
||||
Reference in New Issue
Block a user