📄 svm.c
字号:
/* * Copyright (c) 1997-1999 University of Utah and the Flux Group. * All rights reserved. * * This file is part of the Flux OSKit. The OSKit is free software, also known * as "open source;" you can redistribute it and/or modify it under the terms * of the GNU General Public License (GPL), version 2, as published by the Free * Software Foundation (FSF). To explore alternate licensing terms, contact * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271. * * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have * received a copy of the GPL along with the OSKit; see the file COPYING. If * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA. *//* * This simple program tests the SVM (Simple Virtual Memory) module to * to ensure that read-only pages are indeed read-only, that stack * overflow is properly detected, and that the signal code properly * generates the proper signals and that the instruction stream can be * restarted. */#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include <string.h>#include <sys/types.h>#include <signal.h>#include <oskit/startup.h>#include <oskit/clientos.h>#include <oskit/svm.h>#include <oskit/machine/base_stack.h>voidsegvhandler(int sig, siginfo_t *info, void *scp_addr){ struct sigcontext *scp = (struct sigcontext *) scp_addr; printf("SegvHandler: sig:%d code:%d val:%d scp:%p pc:0x%x\n", info->si_signo, info->si_code, info->si_value.sival_int, scp, scp->sc_pc); /* * Munge the return address */#if defined(OSKIT_X86) scp->sc_pc += 6;#elif defined(OSKIT_ARM32) scp->sc_pc += 4;#else#error "Fix me"#endif}voidbushandler(int sig, siginfo_t *info, void *scp_addr){ struct sigcontext *scp = (struct sigcontext *) scp_addr; printf("BusHandler: sig:%d code:%d val:%d scp:%p pc:0x%x\n", info->si_signo, info->si_code, info->si_value.sival_int, scp, scp->sc_pc); /* * Stack Overflow. Just exit */ exit(1);}static intwritemem(oskit_addr_t addr){ *((volatile int *) addr) = 69; return 0;}static intreadmem(oskit_addr_t addr){ return *((volatile int *) addr);}intmain(int argc, char **argv){ oskit_addr_t addr; struct sigaction sa; void oflow(int a); /* * Init the OS. */ oskit_clientos_init(); start_clock(); svm_init((oskit_absio_t *) 0); /* * Install SIGSEGV handler. */ sa.sa_sigaction = segvhandler; sa.sa_flags = SA_SIGINFO; sa.sa_mask = 0; if (sigaction(SIGSEGV, &sa, 0) < 0) { perror("sigaction SIGSEGV1"); exit(1); } /* * Install SIGBUS handler. */ sa.sa_sigaction = bushandler; sa.sa_flags = SA_SIGINFO; sa.sa_mask = 0; if (sigaction(SIGBUS, &sa, 0) < 0) { perror("sigaction SIGSEGV1"); exit(1); } /* * Allocate some memory to play with. */ addr = 0; if (svm_alloc(&addr, 0x8000, SVM_PROT_READ, 0)) panic("svm_alloc failed"); printf("svm_alloc returned 0x%x\n", addr); /* * Try to read it. Should work. */ readmem(addr); /* * Try to write it. Should fail with a signal. */ writemem(addr); /* * Modify the protection bits and try again. */ printf("Changing to read/write and trying again ...\n"); svm_protect(addr, 0x1000, SVM_PROT_READ|SVM_PROT_WRITE); writemem(addr); /* * Once more and try again. */ printf("Changing to read only and trying again ...\n"); svm_protect(addr, 0x1000, SVM_PROT_READ); writemem(addr); /* * Lets try stack overflow detection. This should generate a SIGBUS. */ printf("Checking Stack overflow detection code ...\n"); oflow(69); exit(0);}voidoflow(int a){ void oflowaux(int); oflowaux(a);}voidoflowaux(int a){ oflow(a);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -