From 8d8b48a90196fdb52e4832b38a90340c4ea27048 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 5 Oct 2011 15:44:52 -0700 Subject: [PATCH] rt: Make C stack switching Valgrind-clean by warning Valgrind when we're about to write to the C stack from the Rust stack --- mk/platform.mk | 4 ++++ src/rt/arch/i386/context.h | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mk/platform.mk b/mk/platform.mk index 75201fabf57..d1ac90835c5 100644 --- a/mk/platform.mk +++ b/mk/platform.mk @@ -7,6 +7,10 @@ CFG_GCCISH_LINK_FLAGS := # embedded into the executable, so use a no-op command. CFG_DSYMUTIL := true +ifneq ($(CFG_VALGRIND),) + CFG_GCCISH_CFLAGS += -DHAVE_VALGRIND +endif + ifneq ($(findstring freebsd,$(CFG_OSTYPE)),) CFG_LIB_NAME=lib$(1).so CFG_GCCISH_CFLAGS += -fPIC -march=i686 -I/usr/local/include diff --git a/src/rt/arch/i386/context.h b/src/rt/arch/i386/context.h index 9a225b79606..131a994ba45 100644 --- a/src/rt/arch/i386/context.h +++ b/src/rt/arch/i386/context.h @@ -7,6 +7,10 @@ #include #include +#ifdef HAVE_VALGRIND +#include +#endif + template T align_down(T sp) { @@ -44,7 +48,14 @@ public: // function being called causes the task to fail, then we have to avoid // leaking space on the C stack. inline void *alloc_stack(size_t nbytes) { - return (void *)(align_down(regs.esp - nbytes)); + uint32_t bot = regs.esp; + uint32_t top = align_down(bot - nbytes); + +#ifdef HAVE_VALGRIND + (void)VALGRIND_MAKE_MEM_UNDEFINED(top - 4, bot - top + 4); +#endif + + return reinterpret_cast(top); } };