2012-02-08 22:47:52 -06:00
|
|
|
#ifndef RUST_STACK_H
|
|
|
|
#define RUST_STACK_H
|
|
|
|
|
2012-02-08 17:28:25 -06:00
|
|
|
struct stk_seg {
|
|
|
|
stk_seg *prev;
|
|
|
|
stk_seg *next;
|
|
|
|
uintptr_t end;
|
|
|
|
unsigned int valgrind_id;
|
|
|
|
#ifndef _LP64
|
|
|
|
uint32_t pad;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
|
2012-02-10 13:26:50 -06:00
|
|
|
// A value that goes at the end of the stack and must not be touched
|
|
|
|
const uint8_t stack_canary[] = {0xAB, 0xCD, 0xAB, 0xCD,
|
|
|
|
0xAB, 0xCD, 0xAB, 0xCD,
|
|
|
|
0xAB, 0xCD, 0xAB, 0xCD,
|
|
|
|
0xAB, 0xCD, 0xAB, 0xCD};
|
|
|
|
|
2012-02-10 13:42:33 -06:00
|
|
|
// Used by create_stack
|
2012-02-10 13:39:29 -06:00
|
|
|
void
|
|
|
|
register_valgrind_stack(stk_seg *stk);
|
|
|
|
|
2012-02-10 13:42:33 -06:00
|
|
|
// Used by destroy_stack
|
2012-02-10 13:39:29 -06:00
|
|
|
void
|
|
|
|
deregister_valgrind_stack(stk_seg *stk);
|
|
|
|
|
2012-02-10 13:42:33 -06:00
|
|
|
// Used by create_stack
|
2012-02-10 13:24:44 -06:00
|
|
|
void
|
|
|
|
add_stack_canary(stk_seg *stk);
|
|
|
|
|
2012-02-08 22:47:52 -06:00
|
|
|
template <class T>
|
|
|
|
stk_seg *
|
|
|
|
create_stack(T allocer, size_t sz) {
|
2012-02-10 13:26:50 -06:00
|
|
|
size_t total_sz = sizeof(stk_seg) + sz + sizeof(stack_canary);
|
2012-02-09 00:35:05 -06:00
|
|
|
stk_seg *stk = (stk_seg *)allocer->malloc(total_sz, "stack");
|
|
|
|
memset(stk, 0, sizeof(stk_seg));
|
|
|
|
stk->end = (uintptr_t) &stk->data[sz];
|
2012-02-10 13:39:29 -06:00
|
|
|
add_stack_canary(stk);
|
|
|
|
register_valgrind_stack(stk);
|
2012-02-09 00:35:05 -06:00
|
|
|
return stk;
|
2012-02-08 22:47:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void
|
|
|
|
destroy_stack(T allocer, stk_seg *stk) {
|
2012-02-10 13:39:29 -06:00
|
|
|
deregister_valgrind_stack(stk);
|
2012-02-08 22:47:52 -06:00
|
|
|
allocer->free(stk);
|
|
|
|
}
|
|
|
|
|
2012-02-10 13:42:33 -06:00
|
|
|
// Must be called before each time a stack is reused to tell valgrind
|
|
|
|
// that the stack is accessible.
|
2012-02-10 13:37:24 -06:00
|
|
|
void
|
|
|
|
prepare_valgrind_stack(stk_seg *stk);
|
|
|
|
|
2012-02-10 13:42:33 -06:00
|
|
|
// Run a sanity check
|
2012-02-08 17:28:25 -06:00
|
|
|
void
|
|
|
|
check_stack_canary(stk_seg *stk);
|
2012-02-08 22:47:52 -06:00
|
|
|
|
|
|
|
#endif /* RUST_STACK_H */
|