<div dir="ltr"><span style="font-family:arial,sans-serif;font-size:16px">> Also, move some </span><span style="font-family:arial,sans-serif;font-size:16px">globals into local scope (they had no business being global anyway).</span><br>
<div><span style="font-family:arial,sans-serif;font-size:16px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:16px">Documenting that this refers to a draft version of the patch and is no longer applicable.</span></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 1, 2013 at 6:38 PM, Alexander Potapenko <span dir="ltr"><<a href="mailto:glider@google.com" target="_blank">glider@google.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: glider<br>
Date: Mon Apr  1 09:38:56 2013<br>
New Revision: 178464<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=178464&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=178464&view=rev</a><br>
Log:<br>
[libsanitizer] Run the callback on a separate stack in StopTheWorld.<br>
<br>
Currently the callback runs on the caller's stack. If this stack<br>
contains values that have gone out of scope, and we are not super careful, those<br>
values can propagate into global variables (the libc sigaction() in particular<br>
has a side effect that can lead to this). This has caused false negatives in<br>
leak checking code.<br>
<br>
Changes: map a separate stack space for the tracer thread. Also, move some<br>
globals into local scope (they had no business being global anyway).<br>
<br>
Patch by Sergey Matveev (<a href="mailto:earthdok@google.com">earthdok@google.com</a>)<br>
<br>
Modified:<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc?rev=178464&r1=178463&r2=178464&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc?rev=178464&r1=178463&r2=178464&view=diff</a><br>

==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_stoptheworld_linux.cc Mon Apr  1 09:38:56 2013<br>
@@ -248,6 +248,30 @@ static int TracerThread(void* argument)<br>
   return exit_code;<br>
 }<br>
<br>
+class ScopedStackSpaceWithGuard {<br>
+ public:<br>
+  explicit ScopedStackSpaceWithGuard(uptr stack_size) {<br>
+    stack_size_ = stack_size;<br>
+    guard_size_ = GetPageSizeCached();<br>
+    // FIXME: Omitting MAP_STACK here works in current kernels but might break<br>
+    // in the future.<br>
+    guard_start_ = (uptr)MmapOrDie(stack_size_ + guard_size_,<br>
+                                   "ScopedStackWithGuard");<br>
+    CHECK_EQ(guard_start_, (uptr)Mprotect((uptr)guard_start_, guard_size_));<br>
+  }<br>
+  ~ScopedStackSpaceWithGuard() {<br>
+    UnmapOrDie((void *)guard_start_, stack_size_ + guard_size_);<br>
+  }<br>
+  void *Bottom() const {<br>
+    return (void *)(guard_start_ + stack_size_ + guard_size_);<br>
+  }<br>
+<br>
+ private:<br>
+  uptr stack_size_;<br>
+  uptr guard_size_;<br>
+  uptr guard_start_;<br>
+};<br>
+<br>
 static sigset_t blocked_sigset;<br>
 static sigset_t old_sigset;<br>
 static struct sigaction old_sigactions[ARRAY_SIZE(kUnblockedSignals)];<br>
@@ -282,16 +306,12 @@ void StopTheWorld(StopTheWorldCallback c<br>
   struct TracerThreadArgument tracer_thread_argument;<br>
   tracer_thread_argument.callback = callback;<br>
   tracer_thread_argument.callback_argument = argument;<br>
+  const uptr kTracerStackSize = 2 * 1024 * 1024;<br>
+  ScopedStackSpaceWithGuard tracer_stack(kTracerStackSize);<br>
   // Block the execution of TracerThread until after we have set ptrace<br>
   // permissions.<br>
   tracer_thread_argument.mutex.Lock();<br>
-  // The tracer thread will run on the same stack, so we must reserve some<br>
-  // stack space for the caller thread to run in as it waits on the tracer.<br>
-  const uptr kReservedStackSize = 4096;<br>
-  // Get a 16-byte aligned pointer for stack.<br>
-  int a_local_variable __attribute__((__aligned__(16)));<br>
-  pid_t tracer_pid = clone(TracerThread,<br>
-                          (char *)&a_local_variable - kReservedStackSize,<br>
+  pid_t tracer_pid = clone(TracerThread, tracer_stack.Bottom(),<br>
                           CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_UNTRACED,<br>
                           &tracer_thread_argument, 0, 0, 0);<br>
   if (tracer_pid < 0) {<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>