<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Very exciting to see this! <div class=""><br class=""></div><div class=""><div class="">The commit has caused one test failure on darwin:</div><div class=""><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lab.llvm.org-3A8080_green_view_All_job_clang-2Dstage1-2Dcmake-2DRA-5Fcheck_4796_console&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=c0nwteAJsiykGnlab9C3pr-xsnl_XIrYSVZ8MByvnSQ&e=" class="">http://lab.llvm.org:8080/green/view/All/job/clang-stage1-cmake-RA_check/4796/console</a></div><div class=""><br class=""></div><div class=""><pre class="console-output" style="box-sizing: border-box; white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 0px; color: rgb(51, 51, 51); font-size: 13px;">Failing Tests (1):
SafeStack :: pthread.c</pre><div class=""><br class=""></div></div><div class="">Will you be able to investigate?</div><div class=""><br class=""></div><div class="">Thanks!</div><div class="">Anna.</div></div><div><blockquote type="cite" class=""><div class="">On Jun 15, 2015, at 2:08 PM, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" class="">peter@pcc.me.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">Author: pcc<br class="">Date: Mon Jun 15 16:08:47 2015<br class="">New Revision: 239763<br class=""><br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D239763-26view-3Drev&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=ykh8ZAM43TRYYYjR8tczLslBhv_9Z7llRl8YE8tbWqA&e=" class="">http://llvm.org/viewvc/llvm-project?rev=239763&view=rev</a><br class="">Log:<br class="">Protection against stack-based memory corruption errors using SafeStack: compiler-rt runtime support library<br class=""><br class="">This patch adds runtime support for the Safe Stack protection to compiler-rt<br class="">(see <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__reviews.llvm.org_D6094&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=qSpkmQBjWDVSNdV_0cA6pIKxKSV1wXSmhSli5lpFhSY&e=" class="">http://reviews.llvm.org/D6094</a> for the detailed description of the<br class="">Safe Stack).<br class=""><br class="">This patch is our implementation of the safe stack on top of compiler-rt. The<br class="">patch adds basic runtime support for the safe stack to compiler-rt that<br class="">manages unsafe stack allocation/deallocation for each thread.<br class=""><br class="">Original patch by Volodymyr Kuznetsov and others at the Dependable Systems<br class="">Lab at EPFL; updates and upstreaming by myself.<br class=""><br class="">Differential Revision: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__reviews.llvm.org_D6096&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=OqiqqtNSuh3lUxhQkxM9mLzmZlg7HlMM9uYXSDCuALQ&e=" class="">http://reviews.llvm.org/D6096</a><br class=""><br class="">Added:<br class=""> compiler-rt/trunk/lib/safestack/<br class=""> compiler-rt/trunk/lib/safestack/CMakeLists.txt<br class=""> compiler-rt/trunk/lib/safestack/<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a><br class=""> compiler-rt/trunk/test/safestack/<br class=""> compiler-rt/trunk/test/safestack/CMakeLists.txt<br class=""> compiler-rt/trunk/test/safestack/buffer-copy-vla.c<br class=""> compiler-rt/trunk/test/safestack/buffer-copy.c<br class=""> compiler-rt/trunk/test/safestack/init.c<br class=""> compiler-rt/trunk/test/safestack/lit.cfg<br class=""> compiler-rt/trunk/test/safestack/lit.site.cfg.in<br class=""> compiler-rt/trunk/test/safestack/lto.c<br class=""> compiler-rt/trunk/test/safestack/overflow.c<br class=""> compiler-rt/trunk/test/safestack/pthread-cleanup.c<br class=""> compiler-rt/trunk/test/safestack/pthread.c<br class=""> compiler-rt/trunk/test/safestack/utils.h<br class="">Modified:<br class=""> compiler-rt/trunk/CMakeLists.txt<br class=""> compiler-rt/trunk/cmake/config-ix.cmake<br class=""> compiler-rt/trunk/lib/CMakeLists.txt<br class=""> compiler-rt/trunk/test/CMakeLists.txt<br class=""><br class="">Modified: compiler-rt/trunk/CMakeLists.txt<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_CMakeLists.txt-3Frev-3D239763-26r1-3D239762-26r2-3D239763-26view-3Ddiff&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=jxN52eC5uTcV3H23K1M0esimiFufNyfuMa--lNMOLlo&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/CMakeLists.txt?rev=239763&r1=239762&r2=239763&view=diff</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/CMakeLists.txt (original)<br class="">+++ compiler-rt/trunk/CMakeLists.txt Mon Jun 15 16:08:47 2015<br class="">@@ -207,6 +207,7 @@ append_list_if(COMPILER_RT_HAS_FNO_EXCEP<br class=""> append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS)<br class=""> append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS)<br class=""> append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS)<br class="">+append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS)<br class=""> append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS)<br class=""> append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections SANITIZER_COMMON_CFLAGS)<br class=""> append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS)<br class=""><br class="">Modified: compiler-rt/trunk/cmake/config-ix.cmake<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_cmake_config-2Dix.cmake-3Frev-3D239763-26r1-3D239762-26r2-3D239763-26view-3Ddiff&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=8uPaN234pT7PLrj49VgeIhdSfOur9y66wxATBKvW7Xk&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/cmake/config-ix.cmake?rev=239763&r1=239762&r2=239763&view=diff</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/cmake/config-ix.cmake (original)<br class="">+++ compiler-rt/trunk/cmake/config-ix.cmake Mon Jun 15 16:08:47 2015<br class="">@@ -19,6 +19,7 @@ check_cxx_compiler_flag(-fno-exceptions<br class=""> check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)<br class=""> check_cxx_compiler_flag(-funwind-tables COMPILER_RT_HAS_FUNWIND_TABLES_FLAG)<br class=""> check_cxx_compiler_flag(-fno-stack-protector COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG)<br class="">+check_cxx_compiler_flag(-fno-sanitize=safe-stack COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG)<br class=""> check_cxx_compiler_flag(-fvisibility=hidden COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG)<br class=""> check_cxx_compiler_flag(-fno-rtti COMPILER_RT_HAS_FNO_RTTI_FLAG)<br class=""> check_cxx_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG)<br class="">@@ -256,6 +257,7 @@ filter_available_targets(PROFILE_SUPPORT<br class=""> filter_available_targets(TSAN_SUPPORTED_ARCH x86_64 mips64 mips64el)<br class=""> filter_available_targets(UBSAN_SUPPORTED_ARCH x86_64 i386 i686 arm aarch64 mips<br class=""> mipsel mips64 mips64el powerpc64 powerpc64le)<br class="">+filter_available_targets(SAFESTACK_SUPPORTED_ARCH x86_64 i386 i686)<br class=""><br class=""> if(ANDROID)<br class=""> set(OS_NAME "Android")<br class="">@@ -334,3 +336,10 @@ endif()<br class=""> if("${LLVM_NATIVE_ARCH}" STREQUAL "Mips")<br class=""> set(COMPILER_RT_HAS_MSSE3_FLAG FALSE)<br class=""> endif()<br class="">+<br class="">+if (SAFESTACK_SUPPORTED_ARCH AND<br class="">+ OS_NAME MATCHES "Darwin|Linux|FreeBSD")<br class="">+ set(COMPILER_RT_HAS_SAFESTACK TRUE)<br class="">+else()<br class="">+ set(COMPILER_RT_HAS_SAFESTACK FALSE)<br class="">+endif()<br class=""><br class="">Modified: compiler-rt/trunk/lib/CMakeLists.txt<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_lib_CMakeLists.txt-3Frev-3D239763-26r1-3D239762-26r2-3D239763-26view-3Ddiff&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=HWLUGX14Q-vEGLMs2Cn8YLr2fI6-FHoxdLy02ZG16Mg&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/CMakeLists.txt?rev=239763&r1=239762&r2=239763&view=diff</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/lib/CMakeLists.txt (original)<br class="">+++ compiler-rt/trunk/lib/CMakeLists.txt Mon Jun 15 16:08:47 2015<br class="">@@ -34,3 +34,6 @@ if(COMPILER_RT_HAS_TSAN)<br class=""> add_subdirectory(tsan/dd)<br class=""> endif()<br class=""><br class="">+if(COMPILER_RT_HAS_SAFESTACK)<br class="">+ add_subdirectory(safestack)<br class="">+endif()<br class=""><br class="">Added: compiler-rt/trunk/lib/safestack/CMakeLists.txt<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_lib_safestack_CMakeLists.txt-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=FPQU-IZINulRS8-FnwMkOf-RMo5KozjK1XppNoCafT8&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/CMakeLists.txt?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/lib/safestack/CMakeLists.txt (added)<br class="">+++ compiler-rt/trunk/lib/safestack/CMakeLists.txt Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,28 @@<br class="">+add_custom_target(safestack)<br class="">+<br class="">+set(SAFESTACK_SOURCES <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a>)<br class="">+<br class="">+include_directories(..)<br class="">+<br class="">+set(SAFESTACK_CFLAGS ${SANITIZER_COMMON_CFLAGS})<br class="">+<br class="">+if(APPLE)<br class="">+ # Build universal binary on APPLE.<br class="">+ add_compiler_rt_osx_static_runtime(clang_rt.safestack_osx<br class="">+ ARCH ${SAFESTACK_SUPPORTED_ARCH}<br class="">+ SOURCES ${SAFESTACK_SOURCES}<br class="">+ $<TARGET_OBJECTS:RTInterception.osx><br class="">+ $<TARGET_OBJECTS:RTSanitizerCommon.osx><br class="">+ CFLAGS ${SAFESTACK_CFLAGS})<br class="">+ add_dependencies(safestack clang_rt.safestack_osx)<br class="">+else()<br class="">+ # Otherwise, build separate libraries for each target.<br class="">+ foreach(arch ${SAFESTACK_SUPPORTED_ARCH})<br class="">+ add_compiler_rt_runtime(clang_rt.safestack-${arch} ${arch} STATIC<br class="">+ SOURCES ${SAFESTACK_SOURCES}<br class="">+ $<TARGET_OBJECTS:RTInterception.${arch}><br class="">+ $<TARGET_OBJECTS:RTSanitizerCommon.${arch}><br class="">+ CFLAGS ${SAFESTACK_CFLAGS})<br class="">+ add_dependencies(safestack clang_rt.safestack-${arch})<br class="">+ endforeach()<br class="">+endif()<br class=""><br class="">Added: compiler-rt/trunk/lib/safestack/<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a><br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_lib_safestack_safestack.cc-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=Gebt4ly6ktaK7Gwlaw2lFez5Uv-0IcjQmvK1neegD34&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/safestack/safestack.cc?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/lib/safestack/<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a> (added)<br class="">+++ compiler-rt/trunk/lib/safestack/<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a> Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,236 @@<br class="">+//===-- <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__safestack.cc&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=kpDVBtDe97vRb3RgdUgxVmsohxBBMpUQ_ho8xNktS3E&e=" class="">safestack.cc</a> ------------------------------------------------------===//<br class="">+//<br class="">+// The LLVM Compiler Infrastructure<br class="">+//<br class="">+// This file is distributed under the University of Illinois Open Source<br class="">+// License. See LICENSE.TXT for details.<br class="">+//<br class="">+//===----------------------------------------------------------------------===//<br class="">+//<br class="">+// This file implements the runtime support for the safe stack protection<br class="">+// mechanism. The runtime manages allocation/deallocation of the unsafe stack<br class="">+// for the main thread, as well as all pthreads that are created/destroyed<br class="">+// during program execution.<br class="">+//<br class="">+//===----------------------------------------------------------------------===//<br class="">+<br class="">+#include <limits.h><br class="">+#include <pthread.h><br class="">+#include <stddef.h><br class="">+#include <sys/resource.h><br class="">+#include <sys/user.h><br class="">+<br class="">+#include "interception/interception.h"<br class="">+#include "sanitizer_common/sanitizer_common.h"<br class="">+<br class="">+// TODO: The runtime library does not currently protect the safe stack. The<br class="">+// protection of the (safe) stack can be provided by two alternative features<br class="">+// that requires C library support:<br class="">+//<br class="">+// 1) Protection via hardware segmentation on x32 architectures: the (safe)<br class="">+// stack segment (implicitly accessed via the %ss segment register) can be<br class="">+// separated from the data segment (implicitly accessed via the %ds segment<br class="">+// register). Dereferencing a pointer to the safe segment would result in a<br class="">+// segmentation fault.<br class="">+//<br class="">+// 2) Protection via information hiding on 64 bit architectures: the location of<br class="">+// the safe stack can be randomized through secure mechanisms, and the leakage<br class="">+// of the stack pointer can be prevented. Currently, libc can leak the stack<br class="">+// pointer in several ways (e.g. in longjmp, signal handling, user-level context<br class="">+// switching related functions, etc.). These can be fixed in libc and in other<br class="">+// low-level libraries, by either eliminating the escaping/dumping of the stack<br class="">+// pointer (i.e., %rsp) when that's possible, or by using encryption/PTR_MANGLE<br class="">+// (XOR-ing the dumped stack pointer with another secret we control and protect<br class="">+// better). (This is already done for setjmp in glibc.) Furthermore, a static<br class="">+// machine code level verifier can be ran after code generation to make sure<br class="">+// that the stack pointer is never written to memory, or if it is, its written<br class="">+// on the safe stack.<br class="">+//<br class="">+// Finally, while the Unsafe Stack pointer is currently stored in a thread local<br class="">+// variable, with libc support it could be stored in the TCB (thread control<br class="">+// block) as well, eliminating another level of indirection. Alternatively,<br class="">+// dedicating a separate register for storing it would also be possible.<br class="">+<br class="">+/// Minimum stack alignment for the unsafe stack.<br class="">+const unsigned kStackAlign = 16;<br class="">+<br class="">+/// Default size of the unsafe stack. This value is only used if the stack<br class="">+/// size rlimit is set to infinity.<br class="">+const unsigned kDefaultUnsafeStackSize = 0x2800000;<br class="">+<br class="">+// TODO: To make accessing the unsafe stack pointer faster, we plan to<br class="">+// eventually store it directly in the thread control block data structure on<br class="">+// platforms where this structure is pointed to by %fs or %gs. This is exactly<br class="">+// the same mechanism as currently being used by the traditional stack<br class="">+// protector pass to store the stack guard (see getStackCookieLocation()<br class="">+// function above). Doing so requires changing the tcbhead_t struct in glibc<br class="">+// on Linux and tcb struct in libc on FreeBSD.<br class="">+//<br class="">+// For now, store it in a thread-local variable.<br class="">+extern "C" {<br class="">+__attribute__((visibility(<br class="">+ "default"))) __thread void *__safestack_unsafe_stack_ptr = nullptr;<br class="">+}<br class="">+<br class="">+// Per-thread unsafe stack information. It's not frequently accessed, so there<br class="">+// it can be kept out of the tcb in normal thread-local variables.<br class="">+static __thread void *unsafe_stack_start = nullptr;<br class="">+static __thread size_t unsafe_stack_size = 0;<br class="">+static __thread size_t unsafe_stack_guard = 0;<br class="">+<br class="">+static inline void *unsafe_stack_alloc(size_t size, size_t guard) {<br class="">+ CHECK_GE(size + guard, size);<br class="">+ void *addr = MmapOrDie(size + guard, "unsafe_stack_alloc");<br class="">+ MprotectNoAccess((uptr)addr, (uptr)guard);<br class="">+ return (char *)addr + guard;<br class="">+}<br class="">+<br class="">+static inline void unsafe_stack_setup(void *start, size_t size, size_t guard) {<br class="">+ CHECK_GE((char *)start + size, (char *)start);<br class="">+ CHECK_GE((char *)start + guard, (char *)start);<br class="">+ void *stack_ptr = (char *)start + size;<br class="">+ CHECK_EQ((((size_t)stack_ptr) & (kStackAlign - 1)), 0);<br class="">+<br class="">+ __safestack_unsafe_stack_ptr = stack_ptr;<br class="">+ unsafe_stack_start = start;<br class="">+ unsafe_stack_size = size;<br class="">+ unsafe_stack_guard = guard;<br class="">+}<br class="">+<br class="">+static void unsafe_stack_free() {<br class="">+ if (unsafe_stack_start) {<br class="">+ UnmapOrDie((char *)unsafe_stack_start - unsafe_stack_guard,<br class="">+ unsafe_stack_size + unsafe_stack_guard);<br class="">+ }<br class="">+ unsafe_stack_start = nullptr;<br class="">+}<br class="">+<br class="">+/// Thread data for the cleanup handler<br class="">+static pthread_key_t thread_cleanup_key;<br class="">+<br class="">+/// Safe stack per-thread information passed to the thread_start function<br class="">+struct tinfo {<br class="">+ void *(*start_routine)(void *);<br class="">+ void *start_routine_arg;<br class="">+<br class="">+ void *unsafe_stack_start;<br class="">+ size_t unsafe_stack_size;<br class="">+ size_t unsafe_stack_guard;<br class="">+};<br class="">+<br class="">+/// Wrap the thread function in order to deallocate the unsafe stack when the<br class="">+/// thread terminates by returning from its main function.<br class="">+static void *thread_start(void *arg) {<br class="">+ struct tinfo *tinfo = (struct tinfo *)arg;<br class="">+<br class="">+ void *(*start_routine)(void *) = tinfo->start_routine;<br class="">+ void *start_routine_arg = tinfo->start_routine_arg;<br class="">+<br class="">+ // Setup the unsafe stack; this will destroy tinfo content<br class="">+ unsafe_stack_setup(tinfo->unsafe_stack_start, tinfo->unsafe_stack_size,<br class="">+ tinfo->unsafe_stack_guard);<br class="">+<br class="">+ // Make sure out thread-specific destructor will be called<br class="">+ // FIXME: we can do this only any other specific key is set by<br class="">+ // intercepting the pthread_setspecific function itself<br class="">+ pthread_setspecific(thread_cleanup_key, (void *)1);<br class="">+<br class="">+ return start_routine(start_routine_arg);<br class="">+}<br class="">+<br class="">+/// Thread-specific data destructor<br class="">+static void thread_cleanup_handler(void *_iter) {<br class="">+ // We want to free the unsafe stack only after all other destructors<br class="">+ // have already run. We force this function to be called multiple times.<br class="">+ // User destructors that might run more then PTHREAD_DESTRUCTOR_ITERATIONS-1<br class="">+ // times might still end up executing after the unsafe stack is deallocated.<br class="">+ size_t iter = (size_t)_iter;<br class="">+ if (iter < PTHREAD_DESTRUCTOR_ITERATIONS) {<br class="">+ pthread_setspecific(thread_cleanup_key, (void *)(iter + 1));<br class="">+ } else {<br class="">+ // This is the last iteration<br class="">+ unsafe_stack_free();<br class="">+ }<br class="">+}<br class="">+<br class="">+/// Intercept thread creation operation to allocate and setup the unsafe stack<br class="">+INTERCEPTOR(int, pthread_create, pthread_t *thread,<br class="">+ const pthread_attr_t *attr,<br class="">+ void *(*start_routine)(void*), void *arg) {<br class="">+<br class="">+ size_t size = 0;<br class="">+ size_t guard = 0;<br class="">+<br class="">+ if (attr != NULL) {<br class="">+ pthread_attr_getstacksize(attr, &size);<br class="">+ pthread_attr_getguardsize(attr, &guard);<br class="">+ } else {<br class="">+ // get pthread default stack size<br class="">+ pthread_attr_t tmpattr;<br class="">+ pthread_attr_init(&tmpattr);<br class="">+ pthread_attr_getstacksize(&tmpattr, &size);<br class="">+ pthread_attr_getguardsize(&tmpattr, &guard);<br class="">+ pthread_attr_destroy(&tmpattr);<br class="">+ }<br class="">+<br class="">+ CHECK_NE(size, 0);<br class="">+ CHECK_EQ((size & (kStackAlign - 1)), 0);<br class="">+ CHECK_EQ((guard & (PAGE_SIZE - 1)), 0);<br class="">+<br class="">+ void *addr = unsafe_stack_alloc(size, guard);<br class="">+ struct tinfo *tinfo =<br class="">+ (struct tinfo *)(((char *)addr) + size - sizeof(struct tinfo));<br class="">+ tinfo->start_routine = start_routine;<br class="">+ tinfo->start_routine_arg = arg;<br class="">+ tinfo->unsafe_stack_start = addr;<br class="">+ tinfo->unsafe_stack_size = size;<br class="">+ tinfo->unsafe_stack_guard = guard;<br class="">+<br class="">+ return REAL(pthread_create)(thread, attr, thread_start, tinfo);<br class="">+}<br class="">+<br class="">+extern "C" __attribute__((visibility("default")))<br class="">+#if !SANITIZER_CAN_USE_PREINIT_ARRAY<br class="">+// On ELF platforms, the constructor is invoked using .preinit_array (see below)<br class="">+__attribute__((constructor(0)))<br class="">+#endif<br class="">+void __safestack_init() {<br class="">+ // Determine the stack size for the main thread.<br class="">+ size_t size = kDefaultUnsafeStackSize;<br class="">+ size_t guard = 4096;<br class="">+<br class="">+ struct rlimit limit;<br class="">+ if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur != RLIM_INFINITY)<br class="">+ size = limit.rlim_cur;<br class="">+<br class="">+ // Allocate unsafe stack for main thread<br class="">+ void *addr = unsafe_stack_alloc(size, guard);<br class="">+<br class="">+ unsafe_stack_setup(addr, size, guard);<br class="">+<br class="">+ // Initialize pthread interceptors for thread allocation<br class="">+ INTERCEPT_FUNCTION(pthread_create);<br class="">+<br class="">+ // Setup the cleanup handler<br class="">+ pthread_key_create(&thread_cleanup_key, thread_cleanup_handler);<br class="">+}<br class="">+<br class="">+#if SANITIZER_CAN_USE_PREINIT_ARRAY<br class="">+// On ELF platforms, run safestack initialization before any other constructors.<br class="">+// On other platforms we use the constructor attribute to arrange to run our<br class="">+// initialization early.<br class="">+extern "C" {<br class="">+__attribute__((section(".preinit_array"),<br class="">+ used)) void (*__safestack_preinit)(void) = __safestack_init;<br class="">+}<br class="">+#endif<br class="">+<br class="">+extern "C"<br class="">+ __attribute__((visibility("default"))) void *__get_unsafe_stack_start() {<br class="">+ return unsafe_stack_start;<br class="">+}<br class="">+<br class="">+extern "C"<br class="">+ __attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {<br class="">+ return __safestack_unsafe_stack_ptr;<br class="">+}<br class=""><br class="">Modified: compiler-rt/trunk/test/CMakeLists.txt<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_CMakeLists.txt-3Frev-3D239763-26r1-3D239762-26r2-3D239763-26view-3Ddiff&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=s3FvOQ0gr_RH-MENKTYlaoxSjVyabnH8R6qC5X3KyEM&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/CMakeLists.txt?rev=239763&r1=239762&r2=239763&view=diff</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/CMakeLists.txt (original)<br class="">+++ compiler-rt/trunk/test/CMakeLists.txt Mon Jun 15 16:08:47 2015<br class="">@@ -58,6 +58,9 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS)<br class=""> add_subdirectory(ubsan)<br class=""> endif()<br class=""> add_subdirectory(cfi)<br class="">+ if(COMPILER_RT_HAS_SAFESTACK)<br class="">+ add_subdirectory(safestack)<br class="">+ endif()<br class=""> endif()<br class=""><br class=""> if(COMPILER_RT_STANDALONE_BUILD)<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/CMakeLists.txt<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_CMakeLists.txt-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=NJS3dg00lnQI8iCtWExqVeMaG5uDKlPJQqTXFM-PBMo&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/CMakeLists.txt?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/CMakeLists.txt (added)<br class="">+++ compiler-rt/trunk/test/safestack/CMakeLists.txt Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,29 @@<br class="">+set(SAFESTACK_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})<br class="">+set(SAFESTACK_LIT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})<br class="">+<br class="">+set(SAFESTACK_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})<br class="">+if(NOT COMPILER_RT_STANDALONE_BUILD)<br class="">+ list(APPEND SAFESTACK_TEST_DEPS safestack)<br class="">+<br class="">+ # Some tests require LTO, so add a dependency on the relevant LTO plugin.<br class="">+ if(LLVM_ENABLE_PIC AND LLVM_BINUTILS_INCDIR)<br class="">+ list(APPEND SAFESTACK_TEST_DEPS<br class="">+ LLVMgold<br class="">+ )<br class="">+ endif()<br class="">+ if(APPLE)<br class="">+ list(APPEND SAFESTACK_TEST_DEPS<br class="">+ LTO<br class="">+ )<br class="">+ endif()<br class="">+endif()<br class="">+<br class="">+configure_lit_site_cfg(<br class="">+ ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in<br class="">+ ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg<br class="">+ )<br class="">+<br class="">+add_lit_testsuite(check-safestack "Running the SafeStack tests"<br class="">+ ${CMAKE_CURRENT_BINARY_DIR}<br class="">+ DEPENDS ${SAFESTACK_TEST_DEPS})<br class="">+set_target_properties(check-safestack PROPERTIES FOLDER "SafeStack tests")<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/buffer-copy-vla.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_buffer-2Dcopy-2Dvla.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=Q6J5zLCXmgYH_UMW3QhAUno1uzRFr0HPVXTDHTEx2Mc&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/buffer-copy-vla.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/buffer-copy-vla.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/buffer-copy-vla.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,26 @@<br class="">+// RUN: %clang_safestack %s -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+#include "utils.h"<br class="">+<br class="">+// Test that loads/stores work correctly for VLAs on the unsafe stack.<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ int i = 128;<br class="">+ break_optimization(&i);<br class="">+ char buffer[i];<br class="">+<br class="">+ // check that we can write to a buffer<br class="">+ for (i = 0; argv[0][i] && i < sizeof (buffer) - 1; ++i)<br class="">+ buffer[i] = argv[0][i];<br class="">+ buffer[i] = '\0';<br class="">+<br class="">+ break_optimization(buffer);<br class="">+<br class="">+ // check that we can read from a buffer<br class="">+ for (i = 0; argv[0][i] && i < sizeof (buffer) - 1; ++i)<br class="">+ if (buffer[i] != argv[0][i])<br class="">+ return 1;<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/buffer-copy.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_buffer-2Dcopy.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=BHtaHzzSk_tATjZQRiD4VO5XGOhUOqgKoLB9T0yn_fc&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/buffer-copy.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/buffer-copy.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/buffer-copy.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,25 @@<br class="">+// RUN: %clang_safestack %s -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+#include "utils.h"<br class="">+<br class="">+// Test that loads/stores work correctly for variables on the unsafe stack.<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ int i;<br class="">+ char buffer[128];<br class="">+<br class="">+ // check that we can write to a buffer<br class="">+ for (i = 0; argv[0][i] && i < sizeof (buffer) - 1; ++i)<br class="">+ buffer[i] = argv[0][i];<br class="">+ buffer[i] = '\0';<br class="">+<br class="">+ break_optimization(buffer);<br class="">+<br class="">+ // check that we can read from a buffer<br class="">+ for (i = 0; argv[0][i] && i < sizeof (buffer) - 1; ++i)<br class="">+ if (buffer[i] != argv[0][i])<br class="">+ return 1;<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/init.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_init.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=FUn26damoiCafiWylFoWH4J9p__mHA3YwzfEegQ3MoY&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/init.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/init.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/init.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,9 @@<br class="">+// RUN: %clang_safestack %s -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+// Basic smoke test for the runtime library.<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/lit.cfg<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_lit.cfg-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=RxkQqcVIh04L1WV-M3f49L-78y2-QH4ZsyynWWO9NjM&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/lit.cfg?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/lit.cfg (added)<br class="">+++ compiler-rt/trunk/test/safestack/lit.cfg Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,24 @@<br class="">+# -*- Python -*-<br class="">+<br class="">+import os<br class="">+<br class="">+# Setup config name.<br class="">+config.name = 'SafeStack'<br class="">+<br class="">+# Setup source root.<br class="">+config.test_source_root = os.path.dirname(__file__)<br class="">+<br class="">+# Test suffixes.<br class="">+config.suffixes = ['.c', '.cc', '.cpp', '.m', '.mm', '.ll', '.test']<br class="">+<br class="">+# Add clang substitutions.<br class="">+config.substitutions.append( ("%clang_nosafestack ", config.clang + " -O0 -fno-sanitize=safe-stack ") )<br class="">+config.substitutions.append( ("%clang_safestack ", config.clang + " -O0 -fsanitize=safe-stack ") )<br class="">+<br class="">+if config.lto_supported:<br class="">+ config.available_features.add('lto')<br class="">+ config.substitutions.append((r"%clang_lto_safestack ", ' '.join(config.lto_launch + [config.clang] + config.lto_flags + ['-flto -fsanitize=safe-stack '])))<br class="">+<br class="">+# SafeStack tests are currently supported on Linux, FreeBSD and Darwin only.<br class="">+if config.host_os not in ['Linux', 'FreeBSD', 'Darwin']:<br class="">+ config.unsupported = True<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/lit.site.cfg.in<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_lit.site.cfg.in-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=Ic-uyY16QTO424jgVVz-dYLvy8gRVjBWucjJ0qy_W7g&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/lit.site.cfg.in?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/lit.site.cfg.in (added)<br class="">+++ compiler-rt/trunk/test/safestack/lit.site.cfg.in Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,8 @@<br class="">+## Autogenerated by LLVM/Clang configuration.<br class="">+# Do not edit!<br class="">+<br class="">+# Load common config for all compiler-rt lit tests.<br class="">+lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")<br class="">+<br class="">+# Load tool-specific config that would do the real work.<br class="">+lit_config.load_config(config, "@SAFESTACK_LIT_SOURCE_DIR@/lit.cfg")<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/lto.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_lto.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=1BDw54-xQTfCXN0tNOBZYS_7MUWBYECrIwZ3bwzVILI&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/lto.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/lto.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/lto.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,12 @@<br class="">+// REQUIRES: lto<br class="">+<br class="">+// RUN: %clang_lto_safestack %s -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+// Test that safe stack works with LTO.<br class="">+<br class="">+int main() {<br class="">+ char c[] = "hello world";<br class="">+ puts(c);<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/overflow.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_overflow.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=b-uHXYqRhZZv8FKaLdaX_-Y64rTsVGlci-KB6kd4Xnc&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/overflow.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/overflow.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/overflow.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,23 @@<br class="">+// RUN: %clang_safestack %s -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+// RUN: %clang_nosafestack -fno-stack-protector %s -o %t<br class="">+// RUN: not %run %t<br class="">+<br class="">+// Test that buffer overflows on the unsafe stack do not affect variables on the<br class="">+// safe stack.<br class="">+<br class="">+__attribute__((noinline))<br class="">+void fct(volatile int *buffer)<br class="">+{<br class="">+ memset(buffer - 1, 0, 7 * sizeof(int));<br class="">+}<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ int value1 = 42;<br class="">+ int buffer[5];<br class="">+ int value2 = 42;<br class="">+ fct(buffer);<br class="">+ return value1 != 42 || value2 != 42;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/pthread-cleanup.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_pthread-2Dcleanup.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=fWuDHRmRK9uf-bPes23MFCi9QmAER_nf0DPjHCHA_bs&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/pthread-cleanup.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/pthread-cleanup.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/pthread-cleanup.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,31 @@<br class="">+// RUN: %clang_safestack %s -pthread -o %t<br class="">+// RUN: not --crash %run %t<br class="">+<br class="">+// Test that unsafe stacks are deallocated correctly on thread exit.<br class="">+<br class="">+#include <stdlib.h><br class="">+#include <string.h><br class="">+#include <pthread.h><br class="">+<br class="">+enum { kBufferSize = (1 << 15) };<br class="">+<br class="">+void *t1_start(void *ptr)<br class="">+{<br class="">+ char buffer[kBufferSize];<br class="">+ return buffer;<br class="">+}<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ pthread_t t1;<br class="">+ char *buffer = NULL;<br class="">+<br class="">+ if (pthread_create(&t1, NULL, t1_start, NULL))<br class="">+ abort();<br class="">+ if (pthread_join(t1, &buffer))<br class="">+ abort();<br class="">+<br class="">+ // should segfault here<br class="">+ memset(buffer, 0, kBufferSize);<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/pthread.c<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_pthread.c-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=WKv1H0zLHIL-w-8Sjv-elWVikLexeUi2z5L50H73Lm0&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/pthread.c?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/pthread.c (added)<br class="">+++ compiler-rt/trunk/test/safestack/pthread.c Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,40 @@<br class="">+// RUN: %clang_safestack %s -pthread -o %t<br class="">+// RUN: %run %t<br class="">+<br class="">+// Test that pthreads receive their own unsafe stack.<br class="">+<br class="">+#include <stdlib.h><br class="">+#include <string.h><br class="">+#include <pthread.h><br class="">+#include "utils.h"<br class="">+<br class="">+static int ptr_test = 42;<br class="">+<br class="">+void *t1_start(void *ptr)<br class="">+{<br class="">+ if (ptr != &ptr_test)<br class="">+ abort();<br class="">+<br class="">+ // safe stack<br class="">+ int val = ptr_test * 5;<br class="">+<br class="">+ // unsafe stack<br class="">+ char buffer[8096]; // two pages<br class="">+ memset(buffer, val, sizeof (buffer));<br class="">+ break_optimization(buffer);<br class="">+<br class="">+ return ptr;<br class="">+}<br class="">+<br class="">+int main(int argc, char **argv)<br class="">+{<br class="">+ pthread_t t1;<br class="">+ void *ptr = NULL;<br class="">+ if (pthread_create(&t1, NULL, t1_start, &ptr_test))<br class="">+ abort();<br class="">+ if (pthread_join(t1, &ptr))<br class="">+ abort();<br class="">+ if (ptr != &ptr_test)<br class="">+ abort();<br class="">+ return 0;<br class="">+}<br class=""><br class="">Added: compiler-rt/trunk/test/safestack/utils.h<br class="">URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_compiler-2Drt_trunk_test_safestack_utils.h-3Frev-3D239763-26view-3Dauto&d=AwMFAg&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=M7jwU5b0yPNtA41RHLRwF7crGQpVFZJhzGj3DOrGiwM&s=6jTEPzkuKWyKyrGD7tq-UwsOg_9TVnHqLCC1hkCXybo&e=" class="">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/safestack/utils.h?rev=239763&view=auto</a><br class="">==============================================================================<br class="">--- compiler-rt/trunk/test/safestack/utils.h (added)<br class="">+++ compiler-rt/trunk/test/safestack/utils.h Mon Jun 15 16:08:47 2015<br class="">@@ -0,0 +1,8 @@<br class="">+#ifndef UTILS_H<br class="">+#define UTILS_H<br class="">+<br class="">+static inline void break_optimization(void *arg) {<br class="">+ __asm__ __volatile__("" : : "r" (arg) : "memory");<br class="">+}<br class="">+<br class="">+#endif<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@cs.uiuc.edu" class="">llvm-commits@cs.uiuc.edu</a><br class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits<br class=""></div></blockquote></div><br class=""></body></html>