#include #include "task_struct.h" int second_pid(); const char* second_comm(); int subsecond_pid(); const char* subsecond_comm(); struct task_struct global = { .pid = 42, .comm = "main_global", .arrfun = { { .a = 1, .b = 2, .c = 3, .d = 4.0 }, { .a = 5, .b = 6, .c = 7, .d = 8.0 }, { .a = 9, .b = 10, .c = 11, .d = 12.0 }, { .a = 13, .b = 14, .c = 15, .d = 16.0 }, { .a = 17, .b = 18, .c = 19, .d = 20.0 } }, .tasks = LIST_HEAD_SELF(global.tasks) }; static void print_layout() { // TODO -> Make builtin __spslr_initial_offsetof(type, field) that is not patched printf("Current task_struct layout:\n"); printf(" stuck0 (int) : %2llu -> %2llu\n", 0, offsetof(struct task_struct, stuck0)); printf(" pid (int) : %2llu -> %2llu\n", 8, offsetof(struct task_struct, pid)); printf(" comm (const char*) : %2llu -> %2llu\n", 16, offsetof(struct task_struct, comm)); printf(" arrfun (struct ArrayFun [5]) : %2llu -> %2llu\n", 24, offsetof(struct task_struct, arrfun)); printf(" tasks (struct list_head) : %2llu -> %2llu\n", 144, offsetof(struct task_struct, tasks)); printf(" stuck1 (int) : %2llu -> %2llu\n", 160, offsetof(struct task_struct, stuck1)); printf(" stuck2 (int) : %2llu -> %2llu\n", 166, offsetof(struct task_struct, stuck2)); printf("Current ArrayFun layout:\n"); printf(" a (int) : %2llu -> %2llu\n", 0, offsetof(struct ArrayFun, a)); printf(" b (int) : %2llu -> %2llu\n", 4, offsetof(struct ArrayFun, b)); printf(" c (int) : %2llu -> %2llu\n", 8, offsetof(struct ArrayFun, c)); printf(" d (double) : %2llu -> %2llu\n", 16, offsetof(struct ArrayFun, d)); } void arr_test() { printf("Global arrfun[3].b: %d (should be 14)\n", global.arrfun[3].b); } int main(void) { spslr_selfpatch(); print_layout(); struct list_head task_list; INIT_LIST_HEAD(&task_list); struct task_struct t1 = { .pid = 1, .comm = "init" }; struct task_struct t2 = { .pid = 2, .comm = "kthreadd" }; struct task_struct t3 = { .pid = 3, .comm = "worker" }; INIT_LIST_HEAD(&t1.tasks); INIT_LIST_HEAD(&t2.tasks); INIT_LIST_HEAD(&t3.tasks); list_add_tail(&t1.tasks, &task_list); list_add_tail(&t2.tasks, &task_list); list_add_tail(&t3.tasks, &task_list); printf("Task list:\n"); struct list_head *pos; list_for_each(pos, &task_list) { struct task_struct *task = list_entry(pos, struct task_struct, tasks); printf(" pid=%d, comm=%s\n", task->pid, task->comm); } size_t myOffset = ((size_t)&((struct task_struct*)0)->pid); printf("DIY offsetof(task_struct, pid) yiels %2llu\n", myOffset); printf("Global: pid=%d comm=\"%s\"\n", global.pid, global.comm); printf("Second global: pid=%d comm=\"%s\"\n", second_pid(), second_comm()); printf("Subsecond global: pid=%d comm=\"%s\"\n", subsecond_pid(), subsecond_comm()); arr_test(); return 0; }