<br><br><div class="gmail_quote">On Fri, Sep 14, 2012 at 8:35 AM, Kostya Serebryany <span dir="ltr"><<a href="mailto:kcc@google.com" target="_blank">kcc@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: kcc<br>
Date: Thu Sep 13 23:35:14 2012<br>
New Revision: 163872<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=163872&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=163872&view=rev</a><br>
Log:<br>
[asan] add asan option log_path=PATH to let users redirect asan reports to a file PATH.PID instead of stderr<br>
<br>
Added:<br>
compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc<br>
Modified:<br>
compiler-rt/trunk/include/sanitizer/common_interface_defs.h<br>
compiler-rt/trunk/lib/asan/asan_flags.h<br>
compiler-rt/trunk/lib/asan/asan_rtl.cc<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc<br>
compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc<br>
<br>
Modified: compiler-rt/trunk/include/sanitizer/common_interface_defs.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/sanitizer/common_interface_defs.h?rev=163872&r1=163871&r2=163872&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/include/sanitizer/common_interface_defs.h?rev=163872&r1=163871&r2=163872&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/include/sanitizer/common_interface_defs.h (original)<br>
+++ compiler-rt/trunk/include/sanitizer/common_interface_defs.h Thu Sep 13 23:35:14 2012<br>
@@ -53,4 +53,10 @@<br>
<br>
} // namespace __sanitizer<br>
<br>
+extern "C" {<br>
+ // Tell the tools to write their reports to "path.<pid>" instead of stderr.<br>
+ void __sanitizer_set_report_path(const char *path)<br>
+ SANITIZER_INTERFACE_ATTRIBUTE;<br>
+} // extern "C"<br>
+<br>
#endif // SANITIZER_COMMON_INTERFACE_DEFS_H<br>
<br>
Modified: compiler-rt/trunk/lib/asan/asan_flags.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_flags.h?rev=163872&r1=163871&r2=163872&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_flags.h?rev=163872&r1=163871&r2=163872&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/asan_flags.h (original)<br>
+++ compiler-rt/trunk/lib/asan/asan_flags.h Thu Sep 13 23:35:14 2012<br>
@@ -93,6 +93,8 @@<br>
// but also thread creation stacks for threads that created those threads,<br>
// etc. up to main thread.<br>
bool print_full_thread_history;<br>
+ // ASan will write logs to "log_path.pid" instead of stderr.<br>
+ const char *log_path;<br>
};<br>
<br>
Flags *flags();<br>
<br>
Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=163872&r1=163871&r2=163872&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=163872&r1=163871&r2=163872&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)<br>
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Thu Sep 13 23:35:14 2012<br>
@@ -99,6 +99,7 @@<br>
ParseFlag(str, &f->strip_path_prefix, "strip_path_prefix");<br>
ParseFlag(str, &f->allow_reexec, "allow_reexec");<br>
ParseFlag(str, &f->print_full_thread_history, "print_full_thread_history");<br>
+ ParseFlag(str, &f->log_path, "log_path");<br>
}<br>
<br>
extern "C" {<br>
@@ -137,6 +138,7 @@<br>
f->strip_path_prefix = "";<br>
f->allow_reexec = true;<br>
f->print_full_thread_history = true;<br>
+ f->log_path = 0;<br>
<br>
// Override from user-specified string.<br>
ParseFlagsFromString(f, __asan_default_options());<br>
@@ -295,6 +297,7 @@<br>
// initialization steps look at flags().<br>
const char *options = GetEnv("ASAN_OPTIONS");<br>
InitializeFlags(flags(), options);<br>
+ __sanitizer_set_report_path(flags()->log_path);<br>
<br>
if (flags()->verbosity && options) {<br>
Report("Parsed ASAN_OPTIONS: %s\n", options);<br>
<br>
Added: compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc?rev=163872&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc?rev=163872&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc (added)<br>
+++ compiler-rt/trunk/lib/asan/lit_tests/log-path_test.cc Thu Sep 13 23:35:14 2012<br>
@@ -0,0 +1,39 @@<br>
+// RUN: %clangxx_asan %s -o %t<br>
+<br>
+// Regular run.<br>
+// RUN: not %t 2> %t.out<br>
+// RUN: FileCheck %s --check-prefix=CHECK-ERROR < %t.out<br>
+<br>
+// Good log_path.<br>
+// RUN: rm -f %t.log.*<br>
+// RUN: ASAN_OPTIONS=log_path=%t.log not %t 2> %t.out<br>
+// RUN: FileCheck %s --check-prefix=CHECK-ERROR < %t.log.*<br>
+<br>
+// Invalid log_path.<br>
+// RUN: ASAN_OPTIONS=log_path=/INVALID not %t 2> %t.out<br>
+// RUN: FileCheck %s --check-prefix=CHECK-INVALID < %t.out<br>
+<br>
+// Too long log_path.<br>
+// RUN: ASAN_OPTIONS=log_path=`for((i=0;i<10000;i++)); do echo -n $i; done` \<br>
+// RUN: not %t 2> %t.out<br>
+// RUN: FileCheck %s --check-prefix=CHECK-LONG < %t.out<br>
+<br>
+// Run w/o errors should not produce any log.<br>
+// RUN: rm -f %t.log.*<br>
+// RUN: ASAN_OPTIONS=log_path=%t.log %t ARG ARG ARG<br>
+// RUN: not cat %t.log.*<br>
+<br>
+<br>
+#include <stdlib.h><br>
+#include <string.h><br>
+int main(int argc, char **argv) {<br>
+ if (argc > 2) return 0;<br>
+ char *x = (char*)malloc(10);<br>
+ memset(x, 0, 10);<br>
+ int res = x[argc * 10]; // BOOOM<br>
+ free(x);<br>
+ return res;<br>
+}<br>
+// CHECK-ERROR: ERROR: AddressSanitizer<br>
+// CHECK-INVALID: ERROR: Can't open file: /INVALID<br>
+// CHECK-LONG: ERROR: Path is too long: 01234<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=163872&r1=163871&r2=163872&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=163872&r1=163871&r2=163872&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Thu Sep 13 23:35:14 2012<br>
@@ -16,6 +16,9 @@<br>
<br>
namespace __sanitizer {<br>
<br>
+static fd_t report_fd = 2; // By default, dump to stderr.<br>
+static char report_path[4096]; // Set via __sanitizer_set_report_path.<br>
+<br>
static void (*DieCallback)(void);<br>
void SetDieCallback(void (*callback)(void)) {<br>
DieCallback = callback;<br>
@@ -46,8 +49,17 @@<br>
void RawWrite(const char *buffer) {<br>
static const char *kRawWriteError = "RawWrite can't output requested buffer!";<br>
uptr length = (uptr)internal_strlen(buffer);<br>
- if (length != internal_write(2, buffer, length)) {<br>
- internal_write(2, kRawWriteError, internal_strlen(kRawWriteError));<br>
+ if (report_fd == kInvalidFd) {<br>
+ fd_t fd = internal_open(report_path, true);<br>
+ if (fd == kInvalidFd) {<br>
+ report_fd = 2;<br>
+ Report("ERROR: Can't open file: %s\n", report_path);<br>
+ Die();<br>
+ }<br>
+ report_fd = fd;<br>
+ }<br>
+ if (length != internal_write(report_fd, buffer, length)) {<br>
+ internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));<br>
Die();<br>
}<br>
}<br>
@@ -125,3 +137,17 @@<br>
}<br>
<br>
} // namespace __sanitizer<br></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+<br>
+void __sanitizer_set_report_path(const char *path) {<br>
+ if (!path) return;<br>
+ uptr len = internal_strlen(path);<br>
+ if (len > sizeof(__sanitizer::report_path) - 100) {<br>
+ Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",<br>
+ path[0], path[1], path[2], path[3],<br>
+ path[4], path[5], path[6], path[7]);<br>
+ Die();<br>
+ }<br>
+ internal_snprintf(__sanitizer::report_path,<br>
+ sizeof(__sanitizer::report_path), "%s.%d", path, GetPid());<br>
+ __sanitizer::report_fd = kInvalidFd;<br>
+}<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc?rev=163872&r1=163871&r2=163872&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc?rev=163872&r1=163871&r2=163872&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_printf.cc Thu Sep 13 23:35:14 2012<br>
@@ -86,7 +86,7 @@<br>
int VSNPrintf(char *buff, int buff_length,<br>
const char *format, va_list args) {<br>
static const char *kPrintfFormatsHelp = "Supported Printf formats: "<br>
- "%%[z]{d,u,x}; %%p; %%s\n";<br>
+ "%%[z]{d,u,x}; %%p; %%s; %%c\n";<br>
RAW_CHECK(format);<br>
RAW_CHECK(buff_length > 0);<br>
const char *buff_end = &buff[buff_length - 1];<br>
@@ -127,6 +127,11 @@<br>
result += AppendString(&buff, buff_end, va_arg(args, char*));<br>
break;<br>
}<br>
+ case 'c': {<br>
+ RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);<br>
+ result += AppendChar(&buff, buff_end, va_arg(args, uptr));<br></blockquote><div><br></div><div>Why not va_arg(args, char)?</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ break;<br>
+ }<br>
case '%' : {<br>
RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);<br>
result += AppendChar(&buff, buff_end, '%');<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><br clear="all"><div><br></div>-- <br><div>Alexey Samsonov, MSK</div><br>