7 #include "trinity.h" // page_size
8 #include "arch.h" // KERNEL_ADDR etc
16 static bool within_page(void *addr, void *check)
20 if ((addr > check) && (addr < (check + page_size)))
25 bool validate_address(void *addr)
27 if (within_page(addr, shm) == TRUE)
29 if (within_page(addr, page_rand) == TRUE)
31 if (within_page(addr, page_zeros) == TRUE)
33 if (within_page(addr, page_0xff) == TRUE)
35 if (within_page(addr, page_allocs) == TRUE)
42 static void * _get_address(unsigned char null_allowed
)
47 if (null_allowed
== TRUE
)
56 case 1: addr
= (void *) KERNEL_ADDR
;
58 case 2: addr
= page_zeros
;
60 case 3: addr
= page_0xff
;
62 case 4: addr
= page_rand
;
64 case 5: addr
= page_allocs
;
66 case 6: addr
= (void *)(unsigned long)rand64();
68 case 7: addr
= get_map();
70 case 8: addr
= malloc(page_size
* 2);
71 // FIXME: We leak this. This is the address we need to store for later
72 // freeing, not the potentially munged version below.
73 // tricky. We want to hand the munged version out too, so we might end up
74 // having to split this into alloc_address / get_address.
77 BUG("unreachable!\n");
82 * Most of the time, we just return the address we got above unmunged.
83 * But sometimes, we return an address just before the end of the page.
84 * The idea here is that we might see some bugs that are caused by page boundary failures.
88 case 0: addr
+= (page_size
- sizeof(char));
90 case 1: addr
+= (page_size
- sizeof(int));
92 case 2: addr
+= (page_size
- sizeof(long));
94 case 3: addr
+= (page_size
/ 2);
103 void * get_address(void)
105 return _get_address(TRUE
);
108 void * get_non_null_address(void)
110 return _get_address(FALSE
);
114 unsigned long find_previous_arg_address(unsigned int argnum
, unsigned int call
, int childno
)
116 unsigned long addr
= 0;
119 if ((syscalls
[call
].entry
->arg1type
== ARG_ADDRESS
) ||
120 (syscalls
[call
].entry
->arg1type
== ARG_NON_NULL_ADDRESS
))
121 addr
= shm
->a1
[childno
];
124 if ((syscalls
[call
].entry
->arg2type
== ARG_ADDRESS
) ||
125 (syscalls
[call
].entry
->arg2type
== ARG_NON_NULL_ADDRESS
))
126 addr
= shm
->a2
[childno
];
129 if ((syscalls
[call
].entry
->arg3type
== ARG_ADDRESS
) ||
130 (syscalls
[call
].entry
->arg3type
== ARG_NON_NULL_ADDRESS
))
131 addr
= shm
->a3
[childno
];
134 if ((syscalls
[call
].entry
->arg4type
== ARG_ADDRESS
) ||
135 (syscalls
[call
].entry
->arg4type
== ARG_NON_NULL_ADDRESS
))
136 addr
= shm
->a4
[childno
];
139 if ((syscalls
[call
].entry
->arg5type
== ARG_ADDRESS
) ||
140 (syscalls
[call
].entry
->arg5type
== ARG_NON_NULL_ADDRESS
))
141 addr
= shm
->a5
[childno
];
148 * iovec's are just special cases of the ARG_ADDRESS's
150 struct iovec
* alloc_iovec(unsigned int num
)
155 iov
= malloc(num
* sizeof(struct iovec
));
157 for (i
= 0; i
< num
; i
++) {
158 iov
[i
].iov_base
= malloc(page_size
);
159 iov
[i
].iov_len
= page_size
;