#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" }; 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(" tasks (struct list_head) : %2llu -> %2llu\n", 24, offsetof(struct task_struct, tasks)); printf(" stuck1 (int) : %2llu -> %2llu\n", 40, offsetof(struct task_struct, stuck1)); printf(" stuck2 (int) : %2llu -> %2llu\n", 44, offsetof(struct task_struct, stuck2)); } 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)->tasks); printf("DIY offsetof(task_struct, tasks) 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()); return 0; }