revert to oldcode
[snitchaser:mainline.git] / checkpoint / mmap.c
1
2 #include "syscalls.h"
3 #include <sys/mman.h>
4 #ifdef IN_INJECTOR
5 # include "injector.h"
6 #endif
7
8 #define PAGE_MASK       (~(PAGE_SIZE-1))
9 #define TASK_SIZE       (0xC0000000UL)
10
11 #ifndef SYSCALL_PRINTER
12 int SCOPE
13 post_mmap(const struct syscall_regs * regs)
14 {
15         write_eax(regs);
16 #if 0
17         /* we have to check mmap, if it is a file map and successed,
18          * we must save its content, for replay use. */
19
20         /* eax is result */
21         uint32_t addr = regs->eax;
22         uint32_t len = regs->ecx;
23         uint32_t flags = regs->esi;
24
25         if (!((addr & ~PAGE_MASK) || (addr > TASK_SIZE))) {
26                 /* check whether it is a file map */
27                 write_obj(flags);
28                 if (!(flags & MAP_ANONYMOUS)) {
29                         /* its a filemap, write memory */
30                         write_obj(len);
31                         /* no, don't write memory. */
32                         /* most of the time, file mapping is readonly */
33                         write_mem(addr, len);
34                 }
35         }
36 #endif
37         return 0;
38 }
39
40 int SCOPE
41 replay_mmap(const struct syscall_regs * regs)
42 {
43         uint32_t eax;
44         eax = read_uint32();
45         /* eax is result */
46
47         if (!((eax & ~PAGE_MASK) || (eax > TASK_SIZE))) {
48                 /* we must do the mmap for target */
49 #ifdef IN_INJECTOR
50                 uint32_t addr = eax;
51                 uint32_t len = regs->ecx;
52                 uint32_t prot = regs->edx;
53                 uint32_t flags = regs->esi;
54                 uint32_t fd = regs->edi;
55                 uint32_t pgoff = regs->ebp;
56
57
58                 uint32_t ret;
59                 /* it is a anon map */
60                 ret = INTERNAL_SYSCALL(mmap, 6,
61                                 addr, len, prot,
62                                 flags | MAP_FIXED, fd, pgoff);
63                 ASSERT(ret == eax, regs, "mmap inconsistent\n");
64
65 #if 0
66
67                 /* DON'T INJECT MEMORY NOW */
68
69                 uint32_t lflags = read_uint32();
70                 ASSERT(lflags == flags, regs, "lflags=0x%x, flags=0x%x\n",
71                                 lflags, flags);
72
73                 /* check if it is a file map */
74                 if ((flags & MAP_ANONYMOUS)) {
75                         uint32_t ret;
76                         /* it is a anon map */
77                         ret = INTERNAL_SYSCALL(mmap, 6,
78                                         addr, len, prot,
79                                         flags | MAP_FIXED, fd, pgoff);
80                         ASSERT(ret == eax, regs, "");
81                 } else {
82                         /* it is a file map. the file shoule have
83                          * been opened... */
84
85                         /* read len */
86                         int32_t llen;
87                         llen = read_uint32();
88                         ASSERT(len == llen, regs, "len=%d, llen=%d\n", len, llen);
89
90                         /* we must unset 'shared' flag */
91                         flags &= (~(MAP_SHARED));
92                         flags |= MAP_PRIVATE;
93                         flags |= MAP_FIXED;
94                         uint32_t ret;
95                         /* enable write */
96                         ret = INTERNAL_SYSCALL(mmap, 6,
97                                         addr, len, prot | PROT_WRITE,
98                                         flags, fd, pgoff);
99                         ASSERT(ret == eax, regs, "\n");
100
101                         /* then inject memory */
102                         INJ_WARNING("len=0x%x, ret=0x%x, eax=0x%x\n", len, ret, eax);
103                         INJ_WARNING("0x%x\n", *(uint32_t*)(ret));
104
105                         read_mem(ret, len);
106                 }
107 #endif
108 #endif
109         }
110         return eax;
111 }
112
113
114 #else
115
116 void
117 output_mmap(int nr)
118 {
119         int32_t addr = read_eax();
120         printf("mmap:\t0x%x\n", addr);
121 #if 0
122         if (!((addr & ~PAGE_MASK) || (addr > TASK_SIZE))) {
123                 uint32_t flags;
124                 read_obj(flags);
125                 if (!(flags & MAP_ANONYMOUS)) {
126                         uint32_t len;
127                         read_obj(len);
128                         skip(len);
129                 }
130         }
131 #endif  
132 }
133 #endif
134