标签:
试图使用mprotect保护一个静态变量数组,当对该静态变量受保护的部分写时,程序core dump
#include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <unistd.h> int protect_bss(void * p, size_t len) { size_t page_size = sysconf(_SC_PAGE_SIZE); if(page_size == -1) { return -1; } size_t start = (size_t)p; size_t new_start = (start + (page_size - 1)) / page_size * page_size; size_t new_len = (start + len - new_start) / page_size * page_size; if(mprotect((void *)new_start, new_len, PROT_READ) == 0) { printf( "bss [%p, %p), protected [%p, %p)\n", p, p+len, (void*)new_start, (void*)new_start+new_len); return 0; } return -1; } char bss[20000]; int main(void) { printf("%d\n", protect_bss(bss, sizeof(bss))); bss[10000] = 1; return 0; }
标签:
原文地址:http://my.oschina.net/2bit/blog/395784