1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <sys/ioctl.h>
8
9
10 enum {
11 PTRACEXX_REMAP = 1,
12 };
13
14 typedef struct {
15 int pid;
16 unsigned long old_start, old_end;
17 unsigned long new_start, new_end;
18 } RemapDataT;
19
20 typedef union {
21 RemapDataT remap;
22 } IoctlDataT;
23
main()24 int main() {
25 char fb[64];
26 IoctlDataT d;
27 int fd, w=0;
28 long rc;
29 int pid=2024;
30 // 7fffbebcf000-7fffbebd1000
31 unsigned long start=0x7fffbebcf000L;
32 unsigned long end=0x7fffbebd1000L;
33 unsigned long nstart=0x7ffff0000000L;
34 unsigned int magic = 0x12345678;
35 unsigned int v, vv;
36 size_t n;
37 fd = open("/dev/ptracexx", O_NONBLOCK);
38 if (fd<0) { perror("fail to open ptracexx\n"); return -1; }
39 sprintf(fb, "/proc/%d/mem", pid);
40 FILE *mem = fopen(fb, "rb");
41 if (mem==NULL) {
42 printf("fail to open virtual mem space for pid %d\n", pid);
43 close(fd);
44 return -1;
45 }
46 d.remap.pid = pid;;
47 d.remap.old_start = start;
48 d.remap.old_end = end;
49 d.remap.new_start = nstart;
50 d.remap.new_end = end-start+nstart;
51 fseek(mem, start, SEEK_SET);
52 if (w) {
53 n = fwrite(&magic, 1, sizeof(magic), mem);
54 printf("write magic ==> %ld\n", n);
55 }
56 fread(&v, 1, sizeof(v), mem);
57 fclose(mem); mem=NULL;
58 // remap
59 rc = ioctl(fd, PTRACEXX_REMAP, &d);
60 if (rc<0) {
61 printf("fail to remap %ld\n", rc);
62 } else {
63 mem = fopen(fb, "rb");
64 fseek(mem, nstart, SEEK_SET);
65 if (w) {
66 n = fread(&vv, 1, sizeof(vv), mem);
67 printf("read back(%ld) %d, expect %d\n", n, vv, magic);
68 }
69 n = fread(&vv, 1, sizeof(vv), mem);
70 printf("read after remap(%ld) %d, old value %d\n",n, vv, v);
71 }
72 if (mem) fclose(mem);
73 close(fd);
74 return 0;
75 }
76