~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to o/test_memprotect.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  sample usage:
 
3
linux14% cd gcl-2.2
 
4
linux14% cd o
 
5
linux14% gcc -I../h test_memprotect.c
 
6
linux14% a.out
 
7
[val=0]
 
8
Page violation (sig=b,code=2b,scp=2b,addr=2b,fault_adr=804a005)
 
9
 
 
10
Reading pp[5] (addr=804a005) 10
 
11
linux14% 
 
12
*/
 
13
 
 
14
#define IN_GBC
 
15
#define NEED_MP_H
 
16
#include "include.h"
 
17
 
 
18
 
 
19
 
 
20
#ifdef BSD
 
21
/* ulong may have been defined in mp.h but the define is no longer needed */
 
22
#undef ulong
 
23
#include <sys/mman.h>
 
24
#define PROT_READ_WRITE (PROT_READ | PROT_WRITE |PROT_EXEC)
 
25
#endif
 
26
#ifdef AIX3
 
27
#include <sys/vmuser.h>
 
28
#define PROT_READ RDONLY
 
29
#define  PROT_READ_WRITE UDATAKEY
 
30
int mprotect();
 
31
#endif
 
32
 
 
33
#include <signal.h>
 
34
 
 
35
 
 
36
char *pp;
 
37
int psize;
 
38
 
 
39
char *malloc();
 
40
 
 
41
#include <asm/signal.h>
 
42
#include <asm/sigcontext.h>
 
43
 
 
44
void
 
45
handler(sig,code,scp,addr)
 
46
     int sig,code;
 
47
     struct sigcontext *scp;
 
48
     char *addr;
 
49
{
 
50
  struct sigcontext_struct *bil= (void *) & code;
 
51
 printf("\nPage violation (sig=%x,code=%x,scp=%x,addr=%x,fault_adr=%x)",sig,code,scp,addr,GET_FAULT_ADDR(sig,code,scp,addr));
 
52
  fflush(stdout);
 
53
  mprotect(pp, psize, PROT_READ | PROT_WRITE);
 
54
  return;
 
55
}
 
56
 
 
57
main()
 
58
{
 
59
  char *p;
 
60
  int a;
 
61
  signal(SIGSEGV, handler);
 
62
  signal(SIGBUS, handler); 
 
63
  psize = getpagesize();
 
64
  p = malloc(3 * psize);
 
65
  a = (int)p;
 
66
  pp = (char *)( ((a / psize)+ 1) * psize);
 
67
  printf("[val=%d]",mprotect(pp, psize, PROT_READ));
 
68
  pp[5] = 10;
 
69
  printf("\n\nReading pp[5] (addr=%x) %d\n",&pp[5], pp[5]);
 
70
  fflush(stdout);
 
71
}