Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169628
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: LINUX

2014-05-19 20:33:47

example1:

  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <malloc.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <sys/mman.h>

  8. #define handle_error(msg) \
  9.    do { perror(msg); exit(EXIT_FAILURE); } while (0)

  10. char *buffer;

  11. static void handler(int sig, siginfo_t *si, void *unused)
  12. {
  13.     printf("Got SIGSEGV at address: 0x%lx\n",
  14.      (long) si->si_addr);
  15.     exit(EXIT_FAILURE);
  16. }

  17. int main(int argc, char *argv[])
  18. {
  19.     char *p;
  20.     int pagesize;
  21.     struct sigaction sa;

  22.     sa.sa_flags = SA_SIGINFO;
  23.     sigemptyset(&sa.sa_mask);
  24.     sa.sa_sigaction = handler;
  25.     if (sigaction(SIGSEGV, &sa, NULL) == -1)
  26.         handle_error("sigaction");

  27.     pagesize = sysconf(_SC_PAGE_SIZE);
  28.     if (pagesize == -1)
  29.         handle_error("sysconf");

  30.     /* Allocate a buffer aligned on a page boundary;
  31.      initial protection is PROT_READ | PROT_WRITE */

  32.     buffer = memalign(pagesize, 4 * pagesize);
  33.     if (buffer == NULL)
  34.         handle_error("memalign");

  35.     printf("Start of region: 0x%lx\n", (long) buffer);

  36.     if (mprotect(buffer + pagesize * 2, pagesizePROT_READ) == -1)
  37.         handle_error("mprotect");

  38.     for (p = buffer ; ; )
  39.         *(p++) = 'a';

  40.     printf("Loop completed\n"); /* Should never happen */
  41.     exit(EXIT_SUCCESS);
  42. }
  
example2:


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <sys/mman.h>
  6. #include <execinfo.h>


  7. #define PAGE_SIZE    4096 //getpagesize()
  8. //#define GCC_ALIGN


  9. #ifdef GCC_ALIGN
  10. struct t {
  11.     char a;
  12. } __attribute__((aligned(PAGE_SIZE))) ta;
  13. #else
  14. struct t {
  15.     char padding[PAGE_SIZE-1];
  16.     char a;
  17. } ta;
  18. #endif

  19. char *p;


  20. void dump_stack(void)
  21. {
  22.     int j, nptrs;
  23. #define SIZE 100
  24.     void *buffer[SIZE];
  25.     char **strings;

  26.     nptrs = backtrace(buffer, SIZE);
  27.     printf("backtrace() returned %d addresses\n", nptrs);

  28.     /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
  29.      would produce similar output to the following: */

  30.     strings = backtrace_symbols(buffer, nptrs);
  31.     if (strings == NULL) {
  32.      perror("backtrace_symbols");
  33.      exit(EXIT_FAILURE);
  34.     }

  35.     for (j = 0; j < nptrs; j++)
  36.      printf("%s\n", strings[j]);

  37.     free(strings);
  38. }

  39. void segv_fault(int sig)
  40. {
  41.     printf("memory access!\n");
  42.     dump_stack();
  43.     mprotect(p, PAGE_SIZE, PROT_READ|PROT_WRITE);
  44. }

  45. main()
  46. {
  47.     signal(SIGSEGV, segv_fault);
  48.     
  49. #ifdef GCC_ALIGN
  50.     p = (char *)&(ta.a);
  51. #else
  52.     p = (char *)((unsigned long)&(ta.a) & ~(PAGE_SIZE - 1));
  53. #endif

  54.     printf("p@%p\n", p);
  55.     
  56.     if (mprotect(p, PAGE_SIZE, PROT_READ))
  57.     {
  58.         printf("mprotect error!\n");
  59.     }
  60.     
  61.     printf("write buffer\n");
  62.     ta.a = 1;
  63. }

阅读(1359) | 评论(0) | 转发(0) |
0

上一篇:sort

下一篇:ffmpeg-2.2.2 support flv hevc patch

给主人留下些什么吧!~~