7 #include "trinity.h" // page_size
8 #include "arch.h" // KERNEL_ADDR etc
17 static bool within_page(void *addr, void *check)
21 if ((addr > check) && (addr < (check + page_size)))
26 bool validate_address(void *addr)
28 if (within_page(addr, shm) == TRUE)
30 if (within_page(addr, page_rand) == TRUE)
32 if (within_page(addr, page_zeros) == TRUE)
34 if (within_page(addr, page_0xff) == TRUE)
36 if (within_page(addr, page_allocs) == TRUE)
43 static void * _get_address(unsigned char null_allowed
)
47 size_t len
= page_size
;
50 /* Because we get called during startup when we create fd's, we need
51 * to special case this, as we can't use get_non_null_address at that point */
52 if (getpid() == shm
->mainpid
)
55 if (null_allowed
== TRUE
)
64 case 1: addr
= (void *) KERNEL_ADDR
;
66 case 2: addr
= page_zeros
;
68 case 3: addr
= page_0xff
;
70 case 4: addr
= page_rand
;
72 case 5: addr
= page_allocs
;
74 case 6: addr
= (void *)(unsigned long)rand64();
76 case 7: map
= get_map();
80 case 8: addr
= malloc(page_size
* 2);
81 // FIXME: We leak this. This is the address we need to store for later
82 // freeing, not the potentially munged version below.
83 // tricky. We want to hand the munged version out too, so we might end up
84 // having to split this into alloc_address / get_address.
86 case 9: addr
= page_maps
;
91 * Most of the time, we just return the address we got above unmunged.
92 * But sometimes, we return an address just before the end of the page.
93 * The idea here is that we might see some bugs that are caused by page boundary failures.
97 case 0: addr
+= (len
- sizeof(char));
99 case 1: addr
+= (len
- sizeof(int));
101 case 2: addr
+= (len
- sizeof(long));
103 case 3: addr
+= (len
/ 2);
112 void * get_address(void)
114 return _get_address(TRUE
);
117 void * get_non_null_address(void)
119 return _get_address(FALSE
);
123 unsigned long find_previous_arg_address(unsigned int argnum
, unsigned int call
, int childno
)
125 struct syscallentry
*entry
;
126 unsigned long addr
= 0;
128 entry
= syscalls
[call
].entry
;
131 if ((entry
->arg1type
== ARG_ADDRESS
) ||
132 (entry
->arg1type
== ARG_NON_NULL_ADDRESS
))
133 addr
= shm
->a1
[childno
];
136 if ((entry
->arg2type
== ARG_ADDRESS
) ||
137 (entry
->arg2type
== ARG_NON_NULL_ADDRESS
))
138 addr
= shm
->a2
[childno
];
141 if ((entry
->arg3type
== ARG_ADDRESS
) ||
142 (entry
->arg3type
== ARG_NON_NULL_ADDRESS
))
143 addr
= shm
->a3
[childno
];
146 if ((entry
->arg4type
== ARG_ADDRESS
) ||
147 (entry
->arg4type
== ARG_NON_NULL_ADDRESS
))
148 addr
= shm
->a4
[childno
];
151 if ((entry
->arg5type
== ARG_ADDRESS
) ||
152 (entry
->arg5type
== ARG_NON_NULL_ADDRESS
))
153 addr
= shm
->a5
[childno
];
159 struct iovec
* alloc_iovec(unsigned int num
)
163 iov
= malloc(num
* sizeof(struct iovec
));
167 for (i
= 0; i
< num
; i
++) {
169 iov
[i
].iov_base
= malloc(page_size
);
170 iov
[i
].iov_len
= page_size
;
175 iov
[i
].iov_base
= map
->ptr
;
176 iov
[i
].iov_len
= rand() % map
->size
;