rt: Make the stack canary just a word on the stk_seg struct

This commit is contained in:
Brian Anderson 2012-02-15 15:41:59 -08:00
parent 2796ab6de9
commit 853e2003b8
2 changed files with 12 additions and 13 deletions

View file

@ -3,6 +3,12 @@
#include "vg/valgrind.h"
#include "vg/memcheck.h"
#ifdef _LP64
const uintptr_t canary_value = 0xABCDABCDABCDABCD;
#else
const uintptr_t canary_value = 0xABCDABCD;
#endif
void
register_valgrind_stack(stk_seg *stk) {
stk->valgrind_id =
@ -17,8 +23,7 @@ prepare_valgrind_stack(stk_seg *stk) {
// old stack segments, since the act of popping the stack previously
// caused valgrind to consider the whole thing inaccessible.
size_t sz = stk->end - (uintptr_t)&stk->data[0];
VALGRIND_MAKE_MEM_UNDEFINED(stk->data + sizeof(stack_canary),
sz - sizeof(stack_canary));
VALGRIND_MAKE_MEM_UNDEFINED(stk->data, sz);
#endif
}
@ -29,12 +34,10 @@ deregister_valgrind_stack(stk_seg *stk) {
void
add_stack_canary(stk_seg *stk) {
memcpy(stk->data, stack_canary, sizeof(stack_canary));
assert(sizeof(stack_canary) == 16 && "Stack canary was not the expected size");
stk->canary = canary_value;
}
void
check_stack_canary(stk_seg *stk) {
assert(!memcmp(stk->data, stack_canary, sizeof(stack_canary))
&& "Somebody killed the canary");
assert(stk->canary == canary_value && "Somebody killed the canary");
}

View file

@ -10,15 +10,11 @@ struct stk_seg {
uint32_t pad;
#endif
uintptr_t canary;
uint8_t data[];
};
// 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};
// Used by create_stack
void
register_valgrind_stack(stk_seg *stk);
@ -34,7 +30,7 @@ add_stack_canary(stk_seg *stk);
template <class T>
stk_seg *
create_stack(T allocer, size_t sz) {
size_t total_sz = sizeof(stk_seg) + sz + sizeof(stack_canary);
size_t total_sz = sizeof(stk_seg) + sz;
stk_seg *stk = (stk_seg *)allocer->malloc(total_sz, "stack");
memset(stk, 0, sizeof(stk_seg));
stk->end = (uintptr_t) &stk->data[sz];