From e0d5b92b84424b5b1dbd2dec7d32e92e1550832b Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 8 Feb 2012 15:28:25 -0800 Subject: rt: Begin moving stack-building functions to rust_stack.cpp --- src/rt/rust_stack.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/rt/rust_stack.cpp (limited to 'src/rt/rust_stack.cpp') diff --git a/src/rt/rust_stack.cpp b/src/rt/rust_stack.cpp new file mode 100644 index 00000000000..c3cde81925f --- /dev/null +++ b/src/rt/rust_stack.cpp @@ -0,0 +1,42 @@ +#include "rust_internal.h" + +#include "vg/valgrind.h" +#include "vg/memcheck.h" + +// 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}; + +void +config_valgrind_stack(stk_seg *stk) { + stk->valgrind_id = + VALGRIND_STACK_REGISTER(&stk->data[0], + stk->end); +#ifndef NVALGRIND + // Establish that the stack is accessible. This must be done when reusing + // 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)); +#endif +} + +void +unconfig_valgrind_stack(stk_seg *stk) { + VALGRIND_STACK_DEREGISTER(stk->valgrind_id); +} + +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"); +} + +void +check_stack_canary(stk_seg *stk) { + assert(!memcmp(stk->data, stack_canary, sizeof(stack_canary)) + && "Somebody killed the canary"); +} -- cgit 1.4.1-3-g733a5