Patchwork Copy libsanitizer from llvm at revision 167890

login
register
mail settings
Submitter H.J. Lu
Date Nov. 14, 2012, 12:26 p.m.
Message ID <20121114122609.GA9465@gmail.com>
Download mbox | patch
Permalink /patch/198885/
State New
Headers show

Comments

H.J. Lu - Nov. 14, 2012, 12:26 p.m.
Hi,

This patch copies libsanitizer from llvm at revision 167890.
Tested on Linux/x32.  OK to install?

Thanks.


H.J.
---
 libsanitizer/asan/asan_allocator.cc                |  2 +
 libsanitizer/asan/asan_allocator.h                 |  2 +
 libsanitizer/asan/asan_flags.h                     |  2 +
 libsanitizer/asan/asan_globals.cc                  |  2 +
 libsanitizer/asan/asan_intercepted_functions.h     |  2 +
 libsanitizer/asan/asan_interceptors.cc             | 26 +++++++++--
 libsanitizer/asan/asan_interceptors.h              |  2 +
 libsanitizer/asan/asan_internal.h                  |  2 +
 libsanitizer/asan/asan_linux.cc                    | 16 +------
 libsanitizer/asan/asan_lock.h                      |  2 +
 libsanitizer/asan/asan_mac.cc                      | 12 ++++-
 libsanitizer/asan/asan_mac.h                       |  7 ++-
 libsanitizer/asan/asan_malloc_linux.cc             |  2 +
 libsanitizer/asan/asan_malloc_mac.cc               | 32 ++++++-------
 libsanitizer/asan/asan_malloc_win.cc               |  2 +
 libsanitizer/asan/asan_mapping.h                   |  2 +
 libsanitizer/asan/asan_new_delete.cc               | 40 +++++++++++------
 libsanitizer/asan/asan_poisoning.cc                |  2 +
 libsanitizer/asan/asan_posix.cc                    |  2 +
 libsanitizer/asan/asan_report.cc                   | 16 ++++---
 libsanitizer/asan/asan_report.h                    |  2 +
 libsanitizer/asan/asan_rtl.cc                      |  2 +
 libsanitizer/asan/asan_stack.cc                    |  2 +
 libsanitizer/asan/asan_stack.h                     |  2 +
 libsanitizer/asan/asan_stats.cc                    |  2 +
 libsanitizer/asan/asan_stats.h                     |  2 +
 libsanitizer/asan/asan_thread.cc                   |  2 +
 libsanitizer/asan/asan_thread.h                    |  2 +
 libsanitizer/asan/asan_thread_registry.cc          | 21 ++++++---
 libsanitizer/asan/asan_thread_registry.h           |  2 +
 libsanitizer/asan/asan_win.cc                      |  2 +
 libsanitizer/include/sanitizer/asan_interface.h    |  2 +
 .../include/sanitizer/common_interface_defs.h      |  7 +++
 libsanitizer/interception/interception.h           |  2 +
 libsanitizer/interception/interception_linux.cc    |  2 +
 libsanitizer/interception/interception_linux.h     |  2 +
 libsanitizer/interception/interception_mac.cc      |  2 +
 libsanitizer/interception/interception_mac.h       |  2 +
 libsanitizer/interception/interception_win.cc      |  2 +
 libsanitizer/interception/interception_win.h       |  2 +
 .../sanitizer_common/sanitizer_allocator.cc        |  2 +
 .../sanitizer_common/sanitizer_allocator64.h       |  2 +
 libsanitizer/sanitizer_common/sanitizer_atomic.h   |  2 +
 .../sanitizer_common/sanitizer_atomic_clang.h      |  2 +
 .../sanitizer_common/sanitizer_atomic_msvc.h       |  2 +
 libsanitizer/sanitizer_common/sanitizer_common.cc  | 52 ++++++++++++++++------
 libsanitizer/sanitizer_common/sanitizer_common.h   |  4 ++
 libsanitizer/sanitizer_common/sanitizer_flags.cc   |  2 +
 libsanitizer/sanitizer_common/sanitizer_flags.h    |  2 +
 .../sanitizer_common/sanitizer_internal_defs.h     | 31 +++++++------
 libsanitizer/sanitizer_common/sanitizer_libc.cc    | 19 ++++++++
 libsanitizer/sanitizer_common/sanitizer_libc.h     |  7 +++
 libsanitizer/sanitizer_common/sanitizer_linux.cc   | 20 ++++++++-
 libsanitizer/sanitizer_common/sanitizer_list.h     |  2 +
 libsanitizer/sanitizer_common/sanitizer_mac.cc     | 10 +++++
 libsanitizer/sanitizer_common/sanitizer_mutex.h    |  2 +
 .../sanitizer_common/sanitizer_placement_new.h     |  2 +
 libsanitizer/sanitizer_common/sanitizer_posix.cc   | 19 ++++++--
 libsanitizer/sanitizer_common/sanitizer_printf.cc  | 50 ++++++++++++++-------
 libsanitizer/sanitizer_common/sanitizer_procmaps.h |  3 +-
 .../sanitizer_common/sanitizer_stackdepot.cc       |  2 +
 .../sanitizer_common/sanitizer_stackdepot.h        |  2 +
 .../sanitizer_common/sanitizer_stacktrace.cc       |  5 ++-
 .../sanitizer_common/sanitizer_stacktrace.h        |  2 +
 .../sanitizer_common/sanitizer_symbolizer.cc       |  5 ++-
 .../sanitizer_common/sanitizer_symbolizer.h        |  4 +-
 .../sanitizer_common/sanitizer_symbolizer_linux.cc | 22 ++++++++-
 .../sanitizer_common/sanitizer_symbolizer_mac.cc   |  4 +-
 .../sanitizer_common/sanitizer_symbolizer_win.cc   |  4 +-
 libsanitizer/sanitizer_common/sanitizer_win.cc     | 39 +++++++++-------
 71 files changed, 426 insertions(+), 141 deletions(-)

2012-11-14  H.J. Lu  <hongjiu.lu@intel.com>

	* Copied from llvm at revision 167890.
Rainer Orth - Nov. 14, 2012, 12:36 p.m.
"H.J. Lu" <hjl.tools@gmail.com> writes:

> diff --git a/libsanitizer/asan/asan_linux.cc b/libsanitizer/asan/asan_linux.cc
> index ea7ee9e..5c52ddc 100644
> --- a/libsanitizer/asan/asan_linux.cc
> +++ b/libsanitizer/asan/asan_linux.cc
> @@ -1,5 +1,7 @@
>  //===-- asan_linux.cc -----------------------------------------------------===//
>  //
> +//                     The LLVM Compiler Infrastructure
> +//
>  // This file is distributed under the University of Illinois Open Source
>  // License. See LICENSE.TXT for details.
>  //
> @@ -66,20 +68,6 @@ void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
>    *pc = ucontext->uc_mcontext.gregs[REG_EIP];
>    *bp = ucontext->uc_mcontext.gregs[REG_EBP];
>    *sp = ucontext->uc_mcontext.gregs[REG_ESP];
> -# elif defined(__sparc__)
> -  ucontext_t *ucontext = (ucontext_t*)context;
> -  uptr *stk_ptr;
> -# if defined (__arch64__)
> -  *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
> -  *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
> -  stk_ptr = (uptr *) (*sp + 2047);
> -  *bp = stk_ptr[15];
> -# else
> -  *pc = ucontext->uc_mcontext.gregs[REG_PC];
> -  *sp = ucontext->uc_mcontext.gregs[REG_O6];
> -  stk_ptr = (uptr *) *sp;
> -  *bp = stk_ptr[15];
> -# endif
>  #else
>  # error "Unsupported arch"
>  #endif

I don't think removing this code is desirable.  As discussed, there
needs to be one of the libsanitizer maintainers who takes care of
porting changes from the GCC side to upstream and importing upstream, as
Ian does for libgo.

Btw., is there some kind of libsanitizer porting guide, or is one
supposed to figure this out on one's own?

	Rainer
H.J. Lu - Nov. 14, 2012, 12:41 p.m.
On Wed, Nov 14, 2012 at 4:36 AM, Rainer Orth
<ro@cebitec.uni-bielefeld.de> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>> diff --git a/libsanitizer/asan/asan_linux.cc b/libsanitizer/asan/asan_linux.cc
>> index ea7ee9e..5c52ddc 100644
>> --- a/libsanitizer/asan/asan_linux.cc
>> +++ b/libsanitizer/asan/asan_linux.cc
>> @@ -1,5 +1,7 @@
>>  //===-- asan_linux.cc -----------------------------------------------------===//
>>  //
>> +//                     The LLVM Compiler Infrastructure
>> +//
>>  // This file is distributed under the University of Illinois Open Source
>>  // License. See LICENSE.TXT for details.
>>  //
>> @@ -66,20 +68,6 @@ void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
>>    *pc = ucontext->uc_mcontext.gregs[REG_EIP];
>>    *bp = ucontext->uc_mcontext.gregs[REG_EBP];
>>    *sp = ucontext->uc_mcontext.gregs[REG_ESP];
>> -# elif defined(__sparc__)
>> -  ucontext_t *ucontext = (ucontext_t*)context;
>> -  uptr *stk_ptr;
>> -# if defined (__arch64__)
>> -  *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
>> -  *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
>> -  stk_ptr = (uptr *) (*sp + 2047);
>> -  *bp = stk_ptr[15];
>> -# else
>> -  *pc = ucontext->uc_mcontext.gregs[REG_PC];
>> -  *sp = ucontext->uc_mcontext.gregs[REG_O6];
>> -  stk_ptr = (uptr *) *sp;
>> -  *bp = stk_ptr[15];
>> -# endif
>>  #else
>>  # error "Unsupported arch"
>>  #endif
>
> I don't think removing this code is desirable.  As discussed, there
> needs to be one of the libsanitizer maintainers who takes care of
> porting changes from the GCC side to upstream and importing upstream, as
> Ian does for libgo.
>

I think all changes should go upstream first.  It was a mistake
to check sparc changes into GCC tree.

Konstantin, can you take a look at

http://gcc.gnu.org/ml/gcc-patches/2012-11/msg00967.html

and get it into upstream?

Thanks.
Rainer Orth - Nov. 14, 2012, 12:44 p.m.
"H.J. Lu" <hjl.tools@gmail.com> writes:

>> I don't think removing this code is desirable.  As discussed, there
>> needs to be one of the libsanitizer maintainers who takes care of
>> porting changes from the GCC side to upstream and importing upstream, as
>> Ian does for libgo.
>
> I think all changes should go upstream first.  It was a mistake
> to check sparc changes into GCC tree.

I disagree, as do others: it is undesirable for gcc maintainers to have
to interact with many different upstream communities to get their
changes in.  This is far better dealt with by the respective
subsystem/library maintainers who have links to both communities.

Btw., currently there's no libsanitizer maintainer listed in
MAINTAINERS.  This needs to change.

	Rainer
H.J. Lu - Nov. 14, 2012, 12:55 p.m.
On Wed, Nov 14, 2012 at 4:44 AM, Rainer Orth
<ro@cebitec.uni-bielefeld.de> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>>> I don't think removing this code is desirable.  As discussed, there
>>> needs to be one of the libsanitizer maintainers who takes care of
>>> porting changes from the GCC side to upstream and importing upstream, as
>>> Ian does for libgo.
>>
>> I think all changes should go upstream first.  It was a mistake
>> to check sparc changes into GCC tree.
>
> I disagree, as do others: it is undesirable for gcc maintainers to have
> to interact with many different upstream communities to get their
> changes in.  This is far better dealt with by the respective
> subsystem/library maintainers who have links to both communities.

This may work for a mature library.  libsanitizer keeps changes.
Local changes make it hard to sync with upstream.

> Btw., currently there's no libsanitizer maintainer listed in
> MAINTAINERS.  This needs to change.
>

That is the real problem.   We need a GCC libsanitizer maintainer:

1. He/she must have checkin privilege for upstream.
2. He/she should keep libsanitizer up to date.
3. All GCC libsanitizer changes shared with upstream must be
approved by him/her.
Rainer Orth - Nov. 14, 2012, 1:01 p.m.
"H.J. Lu" <hjl.tools@gmail.com> writes:

>>> I think all changes should go upstream first.  It was a mistake
>>> to check sparc changes into GCC tree.
>>
>> I disagree, as do others: it is undesirable for gcc maintainers to have
>> to interact with many different upstream communities to get their
>> changes in.  This is far better dealt with by the respective
>> subsystem/library maintainers who have links to both communities.
>
> This may work for a mature library.  libsanitizer keeps changes.
> Local changes make it hard to sync with upstream.

I'm not talking about local changes, just about a liaison to take care
of moving changes started on the gcc side upstream.  Say you're working
on a platform not supported by LLVM (e.g. Solaris, I haven't checked):
why should you be forced to deal with them and their infrastructure
(mailinglists etc.) to get asan in gcc working on your platform?

This is exactly how Ian moves my libgo changes upstream and imports
upstream libgo once in a while.

	Rainer
H.J. Lu - Nov. 14, 2012, 1:10 p.m.
On Wed, Nov 14, 2012 at 5:01 AM, Rainer Orth
<ro@cebitec.uni-bielefeld.de> wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
>>>> I think all changes should go upstream first.  It was a mistake
>>>> to check sparc changes into GCC tree.
>>>
>>> I disagree, as do others: it is undesirable for gcc maintainers to have
>>> to interact with many different upstream communities to get their
>>> changes in.  This is far better dealt with by the respective
>>> subsystem/library maintainers who have links to both communities.
>>
>> This may work for a mature library.  libsanitizer keeps changes.
>> Local changes make it hard to sync with upstream.
>
> I'm not talking about local changes, just about a liaison to take care
> of moving changes started on the gcc side upstream.  Say you're working
> on a platform not supported by LLVM (e.g. Solaris, I haven't checked):
> why should you be forced to deal with them and their infrastructure
> (mailinglists etc.) to get asan in gcc working on your platform?
>
> This is exactly how Ian moves my libgo changes upstream and imports
> upstream libgo once in a while.
>

We don't have a maintainer is a problem, not go upstream first.

I have a patch pending to enable mulltib.  But libsanitizer doesn't
work on x32 and it doesn't cause bootstrap problem with x32
enabled on Linux/x86-64.  As soon as multlib is enabled, my bootstrap
will fail since libsanitizer doesn't compile for x32.   It has been fixed
upstream.    Should we apply a local fix or copy from upstream?
Rainer Orth - Nov. 14, 2012, 1:14 p.m.
"H.J. Lu" <hjl.tools@gmail.com> writes:

> We don't have a maintainer is a problem, not go upstream first.

Agreed.  I've no idea if the SC appointed one when it accepted
libsanitizer etc. into GCC.

> I have a patch pending to enable mulltib.  But libsanitizer doesn't
> work on x32 and it doesn't cause bootstrap problem with x32
> enabled on Linux/x86-64.  As soon as multlib is enabled, my bootstrap
> will fail since libsanitizer doesn't compile for x32.   It has been fixed
> upstream.    Should we apply a local fix or copy from upstream?

I don't care: in similar cases for libgo, Ian applies the patch to the
gcc tree at once and at about the same time installs it upstream so it
isn't lost with the next import from upstream.

	Rainer
Jakub Jelinek - Nov. 14, 2012, 1:25 p.m.
On Wed, Nov 14, 2012 at 04:26:09AM -0800, H.J. Lu wrote:
> 2012-11-14  H.J. Lu  <hongjiu.lu@intel.com>
> 
> 	* Copied from llvm at revision 167890.
> 
> diff --git a/libsanitizer/asan/asan_allocator.cc b/libsanitizer/asan/asan_allocator.cc
> index 3a92802..de37137 100644
> --- a/libsanitizer/asan/asan_allocator.cc
> +++ b/libsanitizer/asan/asan_allocator.cc
> @@ -1,5 +1,7 @@
>  //===-- asan_allocator.cc -------------------------------------------------===//
>  //
> +//                     The LLVM Compiler Infrastructure
> +//

I believe all these lines were striped off intentationally, Wei should know
better.  So we shouldn't reintroduce them.

	Jakub
Diego Novillo - Nov. 14, 2012, 8:57 p.m.
(Adding ASAN maintainers to the CC)

On Wed, Nov 14, 2012 at 7:55 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Wed, Nov 14, 2012 at 4:44 AM, Rainer Orth
>>
>> Btw., currently there's no libsanitizer maintainer listed in
>> MAINTAINERS.  This needs to change.
>>
>
> That is the real problem.   We need a GCC libsanitizer maintainer

We already have FOUR.  Kostya, Jakub, Dmitry and Dodji.

Folks, please update MAINTAINERS to avoid confusion.


Thanks.  Diego.

Patch

diff --git a/libsanitizer/asan/asan_allocator.cc b/libsanitizer/asan/asan_allocator.cc
index 3a92802..de37137 100644
--- a/libsanitizer/asan/asan_allocator.cc
+++ b/libsanitizer/asan/asan_allocator.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_allocator.cc -------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_allocator.h b/libsanitizer/asan/asan_allocator.h
index 372ca06..1c6c30b 100644
--- a/libsanitizer/asan/asan_allocator.h
+++ b/libsanitizer/asan/asan_allocator.h
@@ -1,5 +1,7 @@ 
 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_flags.h b/libsanitizer/asan/asan_flags.h
index a0dcf3e..989784d 100644
--- a/libsanitizer/asan/asan_flags.h
+++ b/libsanitizer/asan/asan_flags.h
@@ -1,5 +1,7 @@ 
 //===-- asan_flags.h -------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_globals.cc b/libsanitizer/asan/asan_globals.cc
index b195a90..6e90cb5 100644
--- a/libsanitizer/asan/asan_globals.cc
+++ b/libsanitizer/asan/asan_globals.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_globals.cc ---------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_intercepted_functions.h b/libsanitizer/asan/asan_intercepted_functions.h
index ceb596c..03f126b 100644
--- a/libsanitizer/asan/asan_intercepted_functions.h
+++ b/libsanitizer/asan/asan_intercepted_functions.h
@@ -1,5 +1,7 @@ 
 //===-- asan_intercepted_functions.h ----------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_interceptors.cc b/libsanitizer/asan/asan_interceptors.cc
index 8e15d34..48dfeb0 100644
--- a/libsanitizer/asan/asan_interceptors.cc
+++ b/libsanitizer/asan/asan_interceptors.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_interceptors.cc ----------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -237,13 +239,17 @@  INTERCEPTOR(void*, memcpy, void *to, const void *from, uptr size) {
     ASAN_WRITE_RANGE(from, size);
     ASAN_READ_RANGE(to, size);
   }
+#if MAC_INTERPOSE_FUNCTIONS
+  // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
+  // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
+  return internal_memcpy(to, from, size);
+#else
   return REAL(memcpy)(to, from, size);
+#endif
 }
 
 INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
-#if MAC_INTERPOSE_FUNCTIONS
-  if (!asan_inited) return REAL(memmove)(to, from, size);
-#endif
+  if (!asan_inited) return internal_memmove(to, from, size);
   if (asan_init_is_running) {
     return REAL(memmove)(to, from, size);
   }
@@ -252,7 +258,13 @@  INTERCEPTOR(void*, memmove, void *to, const void *from, uptr size) {
     ASAN_WRITE_RANGE(from, size);
     ASAN_READ_RANGE(to, size);
   }
+#if MAC_INTERPOSE_FUNCTIONS
+  // Interposing of resolver functions is broken on Mac OS 10.7 and 10.8.
+  // See also http://code.google.com/p/address-sanitizer/issues/detail?id=116.
+  return internal_memmove(to, from, size);
+#else
   return REAL(memmove)(to, from, size);
+#endif
 }
 
 INTERCEPTOR(void*, memset, void *block, int c, uptr size) {
@@ -370,6 +382,14 @@  INTERCEPTOR(char*, strcpy, char *to, const char *from) {  // NOLINT
 
 #if ASAN_INTERCEPT_STRDUP
 INTERCEPTOR(char*, strdup, const char *s) {
+#if MAC_INTERPOSE_FUNCTIONS
+  // FIXME: because internal_strdup() uses InternalAlloc(), which currently
+  // just calls malloc() on Mac, we can't use internal_strdup() with the
+  // dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc
+  // starts using mmap() instead.
+  // See also http://code.google.com/p/address-sanitizer/issues/detail?id=123.
+  if (!asan_inited) return REAL(strdup)(s);
+#endif
   if (!asan_inited) return internal_strdup(s);
   ENSURE_ASAN_INITED();
   if (flags()->replace_str) {
diff --git a/libsanitizer/asan/asan_interceptors.h b/libsanitizer/asan/asan_interceptors.h
index a6ab030..3b3e90e 100644
--- a/libsanitizer/asan/asan_interceptors.h
+++ b/libsanitizer/asan/asan_interceptors.h
@@ -1,5 +1,7 @@ 
 //===-- asan_interceptors.h -------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_internal.h b/libsanitizer/asan/asan_internal.h
index 21368ee..f9a6149 100644
--- a/libsanitizer/asan/asan_internal.h
+++ b/libsanitizer/asan/asan_internal.h
@@ -1,5 +1,7 @@ 
 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_linux.cc b/libsanitizer/asan/asan_linux.cc
index ea7ee9e..5c52ddc 100644
--- a/libsanitizer/asan/asan_linux.cc
+++ b/libsanitizer/asan/asan_linux.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_linux.cc -----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -66,20 +68,6 @@  void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
   *pc = ucontext->uc_mcontext.gregs[REG_EIP];
   *bp = ucontext->uc_mcontext.gregs[REG_EBP];
   *sp = ucontext->uc_mcontext.gregs[REG_ESP];
-# elif defined(__sparc__)
-  ucontext_t *ucontext = (ucontext_t*)context;
-  uptr *stk_ptr;
-# if defined (__arch64__)
-  *pc = ucontext->uc_mcontext.mc_gregs[MC_PC];
-  *sp = ucontext->uc_mcontext.mc_gregs[MC_O6];
-  stk_ptr = (uptr *) (*sp + 2047);
-  *bp = stk_ptr[15];
-# else
-  *pc = ucontext->uc_mcontext.gregs[REG_PC];
-  *sp = ucontext->uc_mcontext.gregs[REG_O6];
-  stk_ptr = (uptr *) *sp;
-  *bp = stk_ptr[15];
-# endif
 #else
 # error "Unsupported arch"
 #endif
diff --git a/libsanitizer/asan/asan_lock.h b/libsanitizer/asan/asan_lock.h
index 2392e3c..edee49a 100644
--- a/libsanitizer/asan/asan_lock.h
+++ b/libsanitizer/asan/asan_lock.h
@@ -1,5 +1,7 @@ 
 //===-- asan_lock.h ---------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_mac.cc b/libsanitizer/asan/asan_mac.cc
index 81e25e8..3ff8470 100644
--- a/libsanitizer/asan/asan_mac.cc
+++ b/libsanitizer/asan/asan_mac.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_mac.cc -------------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -66,6 +68,7 @@  int GetMacosVersion() {
       switch (version[1]) {
         case '0': return MACOS_VERSION_SNOW_LEOPARD;
         case '1': return MACOS_VERSION_LION;
+        case '2': return MACOS_VERSION_MOUNTAIN_LION;
         default: return MACOS_VERSION_UNKNOWN;
       }
     }
@@ -128,7 +131,14 @@  bool AsanInterceptsSignal(int signum) {
 }
 
 void AsanPlatformThreadInit() {
-  ReplaceCFAllocator();
+  // For the first program thread, we can't replace the allocator before
+  // __CFInitialize() has been called. If it hasn't, we'll call
+  // MaybeReplaceCFAllocator() later on this thread.
+  // For other threads __CFInitialize() has been called before their creation.
+  // See also asan_malloc_mac.cc.
+  if (((CFRuntimeBase*)kCFAllocatorSystemDefault)->_cfisa) {
+    MaybeReplaceCFAllocator();
+  }
 }
 
 AsanLock::AsanLock(LinkerInitialized) {
diff --git a/libsanitizer/asan/asan_mac.h b/libsanitizer/asan/asan_mac.h
index 18aca0d..be91386 100644
--- a/libsanitizer/asan/asan_mac.h
+++ b/libsanitizer/asan/asan_mac.h
@@ -1,5 +1,7 @@ 
 //===-- asan_mac.h ----------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -38,7 +40,8 @@  enum {
   MACOS_VERSION_UNKNOWN = 0,
   MACOS_VERSION_LEOPARD,
   MACOS_VERSION_SNOW_LEOPARD,
-  MACOS_VERSION_LION
+  MACOS_VERSION_LION,
+  MACOS_VERSION_MOUNTAIN_LION
 };
 
 // Used by asan_malloc_mac.cc and asan_mac.cc
@@ -47,7 +50,7 @@  extern "C" void __CFInitialize();
 namespace __asan {
 
 int GetMacosVersion();
-void ReplaceCFAllocator();
+void MaybeReplaceCFAllocator();
 
 }  // namespace __asan
 
diff --git a/libsanitizer/asan/asan_malloc_linux.cc b/libsanitizer/asan/asan_malloc_linux.cc
index b00bbe5..5eb23dd 100644
--- a/libsanitizer/asan/asan_malloc_linux.cc
+++ b/libsanitizer/asan/asan_malloc_linux.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_malloc_linux.cc ----------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_malloc_mac.cc b/libsanitizer/asan/asan_malloc_mac.cc
index 2df3484..5b47e12 100644
--- a/libsanitizer/asan/asan_malloc_mac.cc
+++ b/libsanitizer/asan/asan_malloc_mac.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_malloc_mac.cc ------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -95,10 +97,6 @@  INTERCEPTOR(void, free, void *ptr) {
   }
 }
 
-namespace __asan {
-  void ReplaceCFAllocator();
-}
-
 // We can't always replace the default CFAllocator with cf_asan right in
 // ReplaceSystemMalloc(), because it is sometimes called before
 // __CFInitialize(), when the default allocator is invalid and replacing it may
@@ -116,7 +114,7 @@  INTERCEPTOR(void, __CFInitialize, void) {
   CHECK(asan_inited);
 #endif
   REAL(__CFInitialize)();
-  if (!cf_asan && asan_inited) ReplaceCFAllocator();
+  if (!cf_asan && asan_inited) MaybeReplaceCFAllocator();
 }
 
 namespace {
@@ -291,12 +289,10 @@  size_t mi_good_size(malloc_zone_t *zone, size_t size) {
 
 boolean_t mi_check(malloc_zone_t *zone) {
   UNIMPLEMENTED();
-  return true;
 }
 
 void mi_print(malloc_zone_t *zone, boolean_t verbose) {
   UNIMPLEMENTED();
-  return;
 }
 
 void mi_log(malloc_zone_t *zone, void *address) {
@@ -331,7 +327,7 @@  boolean_t mi_zone_locked(malloc_zone_t *zone) {
 extern int __CFRuntimeClassTableSize;
 
 namespace __asan {
-void ReplaceCFAllocator() {
+void MaybeReplaceCFAllocator() {
   static CFAllocatorContext asan_context = {
         /*version*/ 0, /*info*/ &asan_zone,
         /*retain*/ 0, /*release*/ 0,
@@ -342,7 +338,7 @@  void ReplaceCFAllocator() {
         /*preferredSize*/ 0 };
   if (!cf_asan)
     cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
-  if (CFAllocatorGetDefault() != cf_asan)
+  if (flags()->replace_cfallocator && CFAllocatorGetDefault() != cf_asan)
     CFAllocatorSetDefault(cf_asan);
 }
 
@@ -410,16 +406,14 @@  void ReplaceSystemMalloc() {
   // Make sure the default allocator was replaced.
   CHECK(malloc_default_zone() == &asan_zone);
 
-  if (flags()->replace_cfallocator) {
-    // If __CFInitialize() hasn't been called yet, cf_asan will be created and
-    // installed as the default allocator after __CFInitialize() finishes (see
-    // the interceptor for __CFInitialize() above). Otherwise install cf_asan
-    // right now. On both Snow Leopard and Lion __CFInitialize() calls
-    // __CFAllocatorInitialize(), which initializes the _base._cfisa field of
-    // the default allocators we check here.
-    if (((CFRuntimeBase*)kCFAllocatorSystemDefault)->_cfisa) {
-      ReplaceCFAllocator();
-    }
+  // If __CFInitialize() hasn't been called yet, cf_asan will be created and
+  // installed as the default allocator after __CFInitialize() finishes (see
+  // the interceptor for __CFInitialize() above). Otherwise install cf_asan
+  // right now. On both Snow Leopard and Lion __CFInitialize() calls
+  // __CFAllocatorInitialize(), which initializes the _base._cfisa field of
+  // the default allocators we check here.
+  if (((CFRuntimeBase*)kCFAllocatorSystemDefault)->_cfisa) {
+    MaybeReplaceCFAllocator();
   }
 }
 }  // namespace __asan
diff --git a/libsanitizer/asan/asan_malloc_win.cc b/libsanitizer/asan/asan_malloc_win.cc
index 7389a25..3ec76d8 100644
--- a/libsanitizer/asan/asan_malloc_win.cc
+++ b/libsanitizer/asan/asan_malloc_win.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_malloc_win.cc ------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_mapping.h b/libsanitizer/asan/asan_mapping.h
index 40a38db..40e3220 100644
--- a/libsanitizer/asan/asan_mapping.h
+++ b/libsanitizer/asan/asan_mapping.h
@@ -1,5 +1,7 @@ 
 //===-- asan_mapping.h ------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_new_delete.cc b/libsanitizer/asan/asan_new_delete.cc
index a2180ae..756810d 100644
--- a/libsanitizer/asan/asan_new_delete.cc
+++ b/libsanitizer/asan/asan_new_delete.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_interceptors.cc ----------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -15,7 +17,6 @@ 
 #include "asan_stack.h"
 
 #include <stddef.h>
-#include <new>
 
 namespace __asan {
 // This function is a no-op. We need it to make sure that object file
@@ -26,29 +27,40 @@  void ReplaceOperatorsNewAndDelete() { }
 
 using namespace __asan;  // NOLINT
 
+// On Android new() goes through malloc interceptors.
+#if !ASAN_ANDROID
+
+// Fake std::nothrow_t to avoid including <new>.
+namespace std {
+struct nothrow_t {};
+}  // namespace std
+
 #define OPERATOR_NEW_BODY \
   GET_STACK_TRACE_HERE_FOR_MALLOC;\
   return asan_memalign(0, size, &stack);
 
-#if ASAN_ANDROID
+INTERCEPTOR_ATTRIBUTE
 void *operator new(size_t size) { OPERATOR_NEW_BODY; }
+INTERCEPTOR_ATTRIBUTE
 void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
-#else
-void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
-void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
-void *operator new(size_t size, std::nothrow_t const&) throw()
-{ OPERATOR_NEW_BODY; }
-void *operator new[](size_t size, std::nothrow_t const&) throw()
-{ OPERATOR_NEW_BODY; }
-#endif
+INTERCEPTOR_ATTRIBUTE
+void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
+INTERCEPTOR_ATTRIBUTE
+void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
 
 #define OPERATOR_DELETE_BODY \
   GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
   asan_free(ptr, &stack);
 
-void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
-void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
-void operator delete(void *ptr, std::nothrow_t const&) throw()
+INTERCEPTOR_ATTRIBUTE
+void operator delete(void *ptr) { OPERATOR_DELETE_BODY; }
+INTERCEPTOR_ATTRIBUTE
+void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; }
+INTERCEPTOR_ATTRIBUTE
+void operator delete(void *ptr, std::nothrow_t const&)
 { OPERATOR_DELETE_BODY; }
-void operator delete[](void *ptr, std::nothrow_t const&) throw()
+INTERCEPTOR_ATTRIBUTE
+void operator delete[](void *ptr, std::nothrow_t const&)
 { OPERATOR_DELETE_BODY; }
+
+#endif
diff --git a/libsanitizer/asan/asan_poisoning.cc b/libsanitizer/asan/asan_poisoning.cc
index 716be7e..ce9de92 100644
--- a/libsanitizer/asan/asan_poisoning.cc
+++ b/libsanitizer/asan/asan_poisoning.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_poisoning.cc -------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_posix.cc b/libsanitizer/asan/asan_posix.cc
index 177b84a..ceaf120 100644
--- a/libsanitizer/asan/asan_posix.cc
+++ b/libsanitizer/asan/asan_posix.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_linux.cc -----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_report.cc b/libsanitizer/asan/asan_report.cc
index 7e6381c..2fbf8fd 100644
--- a/libsanitizer/asan/asan_report.cc
+++ b/libsanitizer/asan/asan_report.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_report.cc ----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -320,7 +322,7 @@  class ScopedInErrorReport {
 
 void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer crashed on unknown address %p"
+  Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
              " (pc %p sp %p bp %p T%d)\n",
              (void*)addr, (void*)pc, (void*)sp, (void*)bp,
              asanThreadRegistry().GetCurrentTidOrInvalid());
@@ -331,14 +333,14 @@  void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
 
 void ReportDoubleFree(uptr addr, StackTrace *stack) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer attempting double-free on %p:\n", addr);
+  Report("ERROR: AddressSanitizer: attempting double-free on %p:\n", addr);
   PrintStack(stack);
   DescribeHeapAddress(addr, 1);
 }
 
 void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer attempting free on address "
+  Report("ERROR: AddressSanitizer: attempting free on address "
              "which was not malloc()-ed: %p\n", addr);
   PrintStack(stack);
   DescribeHeapAddress(addr, 1);
@@ -346,7 +348,7 @@  void ReportFreeNotMalloced(uptr addr, StackTrace *stack) {
 
 void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer attempting to call "
+  Report("ERROR: AddressSanitizer: attempting to call "
              "malloc_usable_size() for pointer which is "
              "not owned: %p\n", addr);
   PrintStack(stack);
@@ -355,7 +357,7 @@  void ReportMallocUsableSizeNotOwned(uptr addr, StackTrace *stack) {
 
 void ReportAsanGetAllocatedSizeNotOwned(uptr addr, StackTrace *stack) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer attempting to call "
+  Report("ERROR: AddressSanitizer: attempting to call "
              "__asan_get_allocated_size() for pointer which is "
              "not owned: %p\n", addr);
   PrintStack(stack);
@@ -366,7 +368,7 @@  void ReportStringFunctionMemoryRangesOverlap(
     const char *function, const char *offset1, uptr length1,
     const char *offset2, uptr length2, StackTrace *stack) {
   ScopedInErrorReport in_report;
-  Report("ERROR: AddressSanitizer %s-param-overlap: "
+  Report("ERROR: AddressSanitizer: %s-param-overlap: "
              "memory ranges [%p,%p) and [%p, %p) overlap\n", \
              function, offset1, offset1 + length1, offset2, offset2 + length2);
   PrintStack(stack);
@@ -459,7 +461,7 @@  void __asan_report_error(uptr pc, uptr bp, uptr sp,
     }
   }
 
-  Report("ERROR: AddressSanitizer %s on address "
+  Report("ERROR: AddressSanitizer: %s on address "
              "%p at pc 0x%zx bp 0x%zx sp 0x%zx\n",
              bug_descr, (void*)addr, pc, bp, sp);
 
diff --git a/libsanitizer/asan/asan_report.h b/libsanitizer/asan/asan_report.h
index 9710bd7..dc3bf9b 100644
--- a/libsanitizer/asan/asan_report.h
+++ b/libsanitizer/asan/asan_report.h
@@ -1,5 +1,7 @@ 
 //===-- asan_report.h -------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_rtl.cc b/libsanitizer/asan/asan_rtl.cc
index 442d41c..14667e6 100644
--- a/libsanitizer/asan/asan_rtl.cc
+++ b/libsanitizer/asan/asan_rtl.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_rtl.cc -------------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_stack.cc b/libsanitizer/asan/asan_stack.cc
index 2531a7f..53a4647 100644
--- a/libsanitizer/asan/asan_stack.cc
+++ b/libsanitizer/asan/asan_stack.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_stack.cc -----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_stack.h b/libsanitizer/asan/asan_stack.h
index da622ed..99f2cac 100644
--- a/libsanitizer/asan/asan_stack.h
+++ b/libsanitizer/asan/asan_stack.h
@@ -1,5 +1,7 @@ 
 //===-- asan_stack.h --------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_stats.cc b/libsanitizer/asan/asan_stats.cc
index fa4adcb..189315b 100644
--- a/libsanitizer/asan/asan_stats.cc
+++ b/libsanitizer/asan/asan_stats.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_stats.cc -----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_stats.h b/libsanitizer/asan/asan_stats.h
index c2b3298..0c02b3a 100644
--- a/libsanitizer/asan/asan_stats.h
+++ b/libsanitizer/asan/asan_stats.h
@@ -1,5 +1,7 @@ 
 //===-- asan_stats.h --------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_thread.cc b/libsanitizer/asan/asan_thread.cc
index 9295c15..bdb5022 100644
--- a/libsanitizer/asan/asan_thread.cc
+++ b/libsanitizer/asan/asan_thread.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_thread.cc ----------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_thread.h b/libsanitizer/asan/asan_thread.h
index dff8c88..4d4c439 100644
--- a/libsanitizer/asan/asan_thread.h
+++ b/libsanitizer/asan/asan_thread.h
@@ -1,5 +1,7 @@ 
 //===-- asan_thread.h -------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_thread_registry.cc b/libsanitizer/asan/asan_thread_registry.cc
index 840837e..4bf63cd 100644
--- a/libsanitizer/asan/asan_thread_registry.cc
+++ b/libsanitizer/asan/asan_thread_registry.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_thread_registry.cc -------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -111,7 +113,11 @@  AsanStats AsanThreadRegistry::GetAccumulatedStats() {
 uptr AsanThreadRegistry::GetCurrentAllocatedBytes() {
   ScopedLock lock(&mu_);
   UpdateAccumulatedStatsUnlocked();
-  return accumulated_stats_.malloced - accumulated_stats_.freed;
+  uptr malloced = accumulated_stats_.malloced;
+  uptr freed = accumulated_stats_.freed;
+  // Return sane value if malloced < freed due to racy
+  // way we update accumulated stats.
+  return (malloced > freed) ? malloced - freed : 1;
 }
 
 uptr AsanThreadRegistry::GetHeapSize() {
@@ -123,11 +129,14 @@  uptr AsanThreadRegistry::GetHeapSize() {
 uptr AsanThreadRegistry::GetFreeBytes() {
   ScopedLock lock(&mu_);
   UpdateAccumulatedStatsUnlocked();
-  return accumulated_stats_.mmaped
-         - accumulated_stats_.malloced
-         - accumulated_stats_.malloced_redzones
-         + accumulated_stats_.really_freed
-         + accumulated_stats_.really_freed_redzones;
+  uptr total_free = accumulated_stats_.mmaped
+                  + accumulated_stats_.really_freed
+                  + accumulated_stats_.really_freed_redzones;
+  uptr total_used = accumulated_stats_.malloced
+                  + accumulated_stats_.malloced_redzones;
+  // Return sane value if total_free < total_used due to racy
+  // way we update accumulated stats.
+  return (total_free > total_used) ? total_free - total_used : 1;
 }
 
 // Return several stats counters with a single call to
diff --git a/libsanitizer/asan/asan_thread_registry.h b/libsanitizer/asan/asan_thread_registry.h
index 99d5cb5..3ad55f5 100644
--- a/libsanitizer/asan/asan_thread_registry.h
+++ b/libsanitizer/asan/asan_thread_registry.h
@@ -1,5 +1,7 @@ 
 //===-- asan_thread_registry.h ----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/asan/asan_win.cc b/libsanitizer/asan/asan_win.cc
index a5c0441..f04c73c 100644
--- a/libsanitizer/asan/asan_win.cc
+++ b/libsanitizer/asan/asan_win.cc
@@ -1,5 +1,7 @@ 
 //===-- asan_win.cc -------------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/include/sanitizer/asan_interface.h b/libsanitizer/include/sanitizer/asan_interface.h
index c7d57d6..788e90f 100644
--- a/libsanitizer/include/sanitizer/asan_interface.h
+++ b/libsanitizer/include/sanitizer/asan_interface.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/include/sanitizer/common_interface_defs.h b/libsanitizer/include/sanitizer/common_interface_defs.h
index 4ac7609..4666318 100644
--- a/libsanitizer/include/sanitizer/common_interface_defs.h
+++ b/libsanitizer/include/sanitizer/common_interface_defs.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer/common_interface_defs.h -----------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -61,6 +63,11 @@  extern "C" {
   // Tell the tools to write their reports to "path.<pid>" instead of stderr.
   void __sanitizer_set_report_path(const char *path)
       SANITIZER_INTERFACE_ATTRIBUTE;
+
+  // Tell the tools to write their reports to given file descriptor instead of
+  // stderr.
+  void __sanitizer_set_report_fd(int fd)
+      SANITIZER_INTERFACE_ATTRIBUTE;
 }  // extern "C"
 
 #endif  // SANITIZER_COMMON_INTERFACE_DEFS_H
diff --git a/libsanitizer/interception/interception.h b/libsanitizer/interception/interception.h
index 8094fe5..7dad07f 100644
--- a/libsanitizer/interception/interception.h
+++ b/libsanitizer/interception/interception.h
@@ -1,5 +1,7 @@ 
 //===-- interception.h ------------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_linux.cc b/libsanitizer/interception/interception_linux.cc
index 4929a7f..009098f 100644
--- a/libsanitizer/interception/interception_linux.cc
+++ b/libsanitizer/interception/interception_linux.cc
@@ -1,5 +1,7 @@ 
 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_linux.h b/libsanitizer/interception/interception_linux.h
index 7940ef2..dba60bf 100644
--- a/libsanitizer/interception/interception_linux.h
+++ b/libsanitizer/interception/interception_linux.h
@@ -1,5 +1,7 @@ 
 //===-- interception_linux.h ------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_mac.cc b/libsanitizer/interception/interception_mac.cc
index c9b7dd3..2c10a71 100644
--- a/libsanitizer/interception/interception_mac.cc
+++ b/libsanitizer/interception/interception_mac.cc
@@ -1,5 +1,7 @@ 
 //===-- interception_mac.cc -------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_mac.h b/libsanitizer/interception/interception_mac.h
index 244b457..6e9e808 100644
--- a/libsanitizer/interception/interception_mac.h
+++ b/libsanitizer/interception/interception_mac.h
@@ -1,5 +1,7 @@ 
 //===-- interception_mac.h --------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_win.cc b/libsanitizer/interception/interception_win.cc
index 443bdce..abbab24 100644
--- a/libsanitizer/interception/interception_win.cc
+++ b/libsanitizer/interception/interception_win.cc
@@ -1,5 +1,7 @@ 
 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/interception/interception_win.h b/libsanitizer/interception/interception_win.h
index ebac168..c64af1b 100644
--- a/libsanitizer/interception/interception_win.h
+++ b/libsanitizer/interception/interception_win.h
@@ -1,5 +1,7 @@ 
 //===-- interception_linux.h ------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_allocator.cc b/libsanitizer/sanitizer_common/sanitizer_allocator.cc
index ff176a8..985f91a 100644
--- a/libsanitizer/sanitizer_common/sanitizer_allocator.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_allocator.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_allocator.cc --------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_allocator64.h b/libsanitizer/sanitizer_common/sanitizer_allocator64.h
index 2477198..71196b1 100644
--- a/libsanitizer/sanitizer_common/sanitizer_allocator64.h
+++ b/libsanitizer/sanitizer_common/sanitizer_allocator64.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_allocator64.h ---------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_atomic.h b/libsanitizer/sanitizer_common/sanitizer_atomic.h
index f2bf235..61e6dfd 100644
--- a/libsanitizer/sanitizer_common/sanitizer_atomic.h
+++ b/libsanitizer/sanitizer_common/sanitizer_atomic.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_atomic.h --------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_atomic_clang.h b/libsanitizer/sanitizer_common/sanitizer_atomic_clang.h
index 68e79f6..af70441 100644
--- a/libsanitizer/sanitizer_common/sanitizer_atomic_clang.h
+++ b/libsanitizer/sanitizer_common/sanitizer_atomic_clang.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_atomic_msvc.h b/libsanitizer/sanitizer_common/sanitizer_atomic_msvc.h
index 2c02baa..d7d5929 100644
--- a/libsanitizer/sanitizer_common/sanitizer_atomic_msvc.h
+++ b/libsanitizer/sanitizer_common/sanitizer_atomic_msvc.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_atomic_msvc.h ---------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_common.cc b/libsanitizer/sanitizer_common/sanitizer_common.cc
index 43ef980..e0f9997 100644
--- a/libsanitizer/sanitizer_common/sanitizer_common.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_common.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_common.cc -----------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -14,7 +16,9 @@ 
 
 namespace __sanitizer {
 
-static fd_t report_fd = 2;  // By default, dump to stderr.
+// By default, dump to stderr. If report_fd is kInvalidFd, try to obtain file
+// descriptor by opening file in report_path.
+static fd_t report_fd = kStderrFd;
 static char report_path[4096];  // Set via __sanitizer_set_report_path.
 
 static void (*DieCallback)(void);
@@ -44,18 +48,27 @@  void NORETURN CheckFailed(const char *file, int line, const char *cond,
   Die();
 }
 
+static void MaybeOpenReportFile() {
+  if (report_fd != kInvalidFd)
+    return;
+  fd_t fd = internal_open(report_path, true);
+  if (fd == kInvalidFd) {
+    report_fd = kStderrFd;
+    Report("ERROR: Can't open file: %s\n", report_path);
+    Die();
+  }
+  report_fd = fd;
+}
+
+bool PrintsToTty() {
+  MaybeOpenReportFile();
+  return internal_isatty(report_fd);
+}
+
 void RawWrite(const char *buffer) {
   static const char *kRawWriteError = "RawWrite can't output requested buffer!";
   uptr length = (uptr)internal_strlen(buffer);
-  if (report_fd == kInvalidFd) {
-    fd_t fd = internal_open(report_path, true);
-    if (fd == kInvalidFd) {
-      report_fd = 2;
-      Report("ERROR: Can't open file: %s\n", report_path);
-      Die();
-    }
-    report_fd = fd;
-  }
+  MaybeOpenReportFile();
   if (length != internal_write(report_fd, buffer, length)) {
     internal_write(report_fd, kRawWriteError, internal_strlen(kRawWriteError));
     Die();
@@ -136,16 +149,27 @@  void SortArray(uptr *array, uptr size) {
 
 }  // namespace __sanitizer
 
+using namespace __sanitizer;  // NOLINT
+
+extern "C" {
 void __sanitizer_set_report_path(const char *path) {
   if (!path) return;
   uptr len = internal_strlen(path);
-  if (len > sizeof(__sanitizer::report_path) - 100) {
+  if (len > sizeof(report_path) - 100) {
     Report("ERROR: Path is too long: %c%c%c%c%c%c%c%c...\n",
            path[0], path[1], path[2], path[3],
            path[4], path[5], path[6], path[7]);
     Die();
   }
-  internal_snprintf(__sanitizer::report_path,
-                    sizeof(__sanitizer::report_path), "%s.%d", path, GetPid());
-  __sanitizer::report_fd = kInvalidFd;
+  internal_snprintf(report_path, sizeof(report_path), "%s.%d", path, GetPid());
+  report_fd = kInvalidFd;
+}
+
+void __sanitizer_set_report_fd(int fd) {
+  if (report_fd != kStdoutFd &&
+      report_fd != kStderrFd &&
+      report_fd != kInvalidFd)
+    internal_close(report_fd);
+  report_fd = fd;
 }
+}  // extern "C"
diff --git a/libsanitizer/sanitizer_common/sanitizer_common.h b/libsanitizer/sanitizer_common/sanitizer_common.h
index cddefd7..27cc308 100644
--- a/libsanitizer/sanitizer_common/sanitizer_common.h
+++ b/libsanitizer/sanitizer_common/sanitizer_common.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -96,6 +98,7 @@  void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
 
 // IO
 void RawWrite(const char *buffer);
+bool PrintsToTty();
 void Printf(const char *format, ...);
 void Report(const char *format, ...);
 void SetPrintfAndReportCallback(void (*callback)(const char *));
@@ -114,6 +117,7 @@  void *MapFileToMemory(const char *file_name, uptr *buff_size);
 // OS
 void DisableCoreDumper();
 void DumpProcessMap();
+bool FileExists(const char *filename);
 const char *GetEnv(const char *name);
 const char *GetPwd();
 void ReExec();
diff --git a/libsanitizer/sanitizer_common/sanitizer_flags.cc b/libsanitizer/sanitizer_common/sanitizer_flags.cc
index 837738c..eca910c 100644
--- a/libsanitizer/sanitizer_common/sanitizer_flags.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_flags.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_flags.cc ------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_flags.h b/libsanitizer/sanitizer_common/sanitizer_flags.h
index 1b9bf5b..b7ce452 100644
--- a/libsanitizer/sanitizer_common/sanitizer_flags.h
+++ b/libsanitizer/sanitizer_common/sanitizer_flags.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_flags.h ---------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_internal_defs.h b/libsanitizer/sanitizer_common/sanitizer_internal_defs.h
index da4d049..09c6f97 100644
--- a/libsanitizer/sanitizer_common/sanitizer_internal_defs.h
+++ b/libsanitizer/sanitizer_common/sanitizer_internal_defs.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -22,8 +24,7 @@  using namespace __sanitizer;  // NOLINT
 #define WEAK SANITIZER_WEAK_ATTRIBUTE
 
 // Platform-specific defs.
-#if defined(_WIN32)
-typedef unsigned long    DWORD;  // NOLINT
+#if defined(_MSC_VER)
 # define ALWAYS_INLINE __declspec(forceinline)
 // FIXME(timurrrr): do we need this on Windows?
 # define ALIAS(x)
@@ -33,7 +34,11 @@  typedef unsigned long    DWORD;  // NOLINT
 # define NORETURN __declspec(noreturn)
 # define THREADLOCAL   __declspec(thread)
 # define NOTHROW
-#else  // _WIN32
+# define LIKELY(x) (x)
+# define UNLIKELY(x) (x)
+# define UNUSED
+# define USED
+#else  // _MSC_VER
 # define ALWAYS_INLINE __attribute__((always_inline))
 # define ALIAS(x) __attribute__((alias(x)))
 # define ALIGNED(x) __attribute__((aligned(x)))
@@ -41,22 +46,15 @@  typedef unsigned long    DWORD;  // NOLINT
 # define NOINLINE __attribute__((noinline))
 # define NORETURN  __attribute__((noreturn))
 # define THREADLOCAL   __thread
-# ifdef __cplusplus
-#   define NOTHROW throw()
-# else
-#   define NOTHROW __attribute__((__nothrow__))
-#endif
-#endif  // _WIN32
-
-// We have no equivalent of these on Windows.
-#ifndef _WIN32
+# define NOTHROW throw()
 # define LIKELY(x)     __builtin_expect(!!(x), 1)
 # define UNLIKELY(x)   __builtin_expect(!!(x), 0)
 # define UNUSED __attribute__((unused))
 # define USED __attribute__((used))
-#endif
+#endif  // _MSC_VER
 
 #if defined(_WIN32)
+typedef unsigned long DWORD;  // NOLINT
 typedef DWORD thread_return_t;
 # define THREAD_CALLING_CONV __stdcall
 #else  // _WIN32
@@ -128,7 +126,12 @@  void NORETURN CheckFailed(const char *file, int line, const char *cond,
 #define DCHECK_GE(a, b)
 #endif
 
-#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
+#define UNREACHABLE(msg) do { \
+  CHECK(0 && msg); \
+  Die(); \
+} while (0)
+
+#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
 
 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
 
diff --git a/libsanitizer/sanitizer_common/sanitizer_libc.cc b/libsanitizer/sanitizer_common/sanitizer_libc.cc
index 21869bc..01eaef3 100644
--- a/libsanitizer/sanitizer_common/sanitizer_libc.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_libc.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_libc.cc -------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -42,6 +44,23 @@  void *internal_memcpy(void *dest, const void *src, uptr n) {
   return dest;
 }
 
+void *internal_memmove(void *dest, const void *src, uptr n) {
+  char *d = (char*)dest;
+  char *s = (char*)src;
+  sptr i, signed_n = (sptr)n;
+  CHECK_GE(signed_n, 0);
+  if (d < s) {
+    for (i = 0; i < signed_n; ++i)
+      d[i] = s[i];
+  } else {
+    if (d > s && signed_n > 0)
+      for (i = signed_n - 1; i >= 0 ; --i) {
+        d[i] = s[i];
+      }
+  }
+  return dest;
+}
+
 void *internal_memset(void* s, int c, uptr n) {
   // The next line prevents Clang from making a call to memset() instead of the
   // loop below.
diff --git a/libsanitizer/sanitizer_common/sanitizer_libc.h b/libsanitizer/sanitizer_common/sanitizer_libc.h
index 1352851..7979483 100644
--- a/libsanitizer/sanitizer_common/sanitizer_libc.h
+++ b/libsanitizer/sanitizer_common/sanitizer_libc.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_libc.h ----------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -27,6 +29,7 @@  s64 internal_atoll(const char *nptr);
 void *internal_memchr(const void *s, int c, uptr n);
 int internal_memcmp(const void* s1, const void* s2, uptr n);
 void *internal_memcpy(void *dest, const void *src, uptr n);
+void *internal_memmove(void *dest, const void *src, uptr n);
 // Should not be used in performance-critical places.
 void *internal_memset(void *s, int c, uptr n);
 char* internal_strchr(const char *s, int c);
@@ -52,7 +55,11 @@  int internal_munmap(void *addr, uptr length);
 // I/O
 typedef int fd_t;
 const fd_t kInvalidFd = -1;
+const fd_t kStdinFd = 0;
+const fd_t kStdoutFd = 1;
+const fd_t kStderrFd = 2;
 int internal_close(fd_t fd);
+int internal_isatty(fd_t fd);
 fd_t internal_open(const char *filename, bool write);
 uptr internal_read(fd_t fd, void *buf, uptr count);
 uptr internal_write(fd_t fd, const void *buf, uptr count);
diff --git a/libsanitizer/sanitizer_common/sanitizer_linux.cc b/libsanitizer/sanitizer_common/sanitizer_linux.cc
index ab6c5a4..51ad4ce 100644
--- a/libsanitizer/sanitizer_common/sanitizer_linux.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_linux.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_linux.cc ------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -34,7 +36,7 @@  namespace __sanitizer {
 // --------------- sanitizer_libc.h
 void *internal_mmap(void *addr, uptr length, int prot, int flags,
                     int fd, u64 offset) {
-#if __WORDSIZE == 64
+#if defined __x86_64__
   return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
 #else
   return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
@@ -67,7 +69,7 @@  uptr internal_write(fd_t fd, const void *buf, uptr count) {
 }
 
 uptr internal_filesize(fd_t fd) {
-#if __WORDSIZE == 64
+#if defined __x86_64__
   struct stat st;
   if (syscall(__NR_fstat, fd, &st))
     return -1;
@@ -92,6 +94,20 @@  int internal_sched_yield() {
 }
 
 // ----------------- sanitizer_common.h
+bool FileExists(const char *filename) {
+#if defined __x86_64__
+  struct stat st;
+  if (syscall(__NR_stat, filename, &st))
+    return false;
+#else
+  struct stat64 st;
+  if (syscall(__NR_stat64, filename, &st))
+    return false;
+#endif
+  // Sanity check: filename is a regular file.
+  return S_ISREG(st.st_mode);
+}
+
 uptr GetTid() {
   return syscall(__NR_gettid);
 }
diff --git a/libsanitizer/sanitizer_common/sanitizer_list.h b/libsanitizer/sanitizer_common/sanitizer_list.h
index 3df12f5..ef98eee 100644
--- a/libsanitizer/sanitizer_common/sanitizer_list.h
+++ b/libsanitizer/sanitizer_common/sanitizer_list.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_list.h ----------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_mac.cc b/libsanitizer/sanitizer_common/sanitizer_mac.cc
index 400cd21..3c30da3 100644
--- a/libsanitizer/sanitizer_common/sanitizer_mac.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_mac.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_mac.cc --------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -78,6 +80,14 @@  int internal_sched_yield() {
 }
 
 // ----------------- sanitizer_common.h
+bool FileExists(const char *filename) {
+  struct stat st;
+  if (stat(filename, &st))
+    return false;
+  // Sanity check: filename is a regular file.
+  return S_ISREG(st.st_mode);
+}
+
 uptr GetTid() {
   return reinterpret_cast<uptr>(pthread_self());
 }
diff --git a/libsanitizer/sanitizer_common/sanitizer_mutex.h b/libsanitizer/sanitizer_common/sanitizer_mutex.h
index a38a49a..6b6c25d 100644
--- a/libsanitizer/sanitizer_common/sanitizer_mutex.h
+++ b/libsanitizer/sanitizer_common/sanitizer_mutex.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_mutex.h ---------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_placement_new.h b/libsanitizer/sanitizer_common/sanitizer_placement_new.h
index d149683..f133a6f 100644
--- a/libsanitizer/sanitizer_common/sanitizer_placement_new.h
+++ b/libsanitizer/sanitizer_common/sanitizer_placement_new.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_placement_new.h -------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_posix.cc b/libsanitizer/sanitizer_common/sanitizer_posix.cc
index 8f71cfc..8117fd4 100644
--- a/libsanitizer/sanitizer_common/sanitizer_posix.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_posix.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_posix.cc ------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -72,10 +74,15 @@  void UnmapOrDie(void *addr, uptr size) {
 }
 
 void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
-  return internal_mmap((void*)fixed_addr, size,
-                      PROT_READ | PROT_WRITE,
-                      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
-                      -1, 0);
+  void *p = internal_mmap((void*)(fixed_addr & ~(kPageSize - 1)),
+      RoundUpTo(size, kPageSize),
+      PROT_READ | PROT_WRITE,
+      MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
+      -1, 0);
+  if (p == (void*)-1)
+    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at address %p (%d)\n",
+           size, size, fixed_addr, errno);
+  return p;
 }
 
 void *Mprotect(uptr fixed_addr, uptr size) {
@@ -182,6 +189,10 @@  int Atexit(void (*function)(void)) {
 #endif
 }
 
+int internal_isatty(fd_t fd) {
+  return isatty(fd);
+}
+
 }  // namespace __sanitizer
 
 #endif  // __linux__ || __APPLE_
diff --git a/libsanitizer/sanitizer_common/sanitizer_printf.cc b/libsanitizer/sanitizer_common/sanitizer_printf.cc
index da4dc7f..9383848 100644
--- a/libsanitizer/sanitizer_common/sanitizer_printf.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_printf.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_printf.cc -----------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -53,13 +55,16 @@  static int AppendUnsigned(char **buff, const char *buff_end, u64 num,
   return result;
 }
 
-static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num) {
+static int AppendSignedDecimal(char **buff, const char *buff_end, s64 num,
+                               u8 minimal_num_length) {
   int result = 0;
   if (num < 0) {
     result += AppendChar(buff, buff_end, '-');
     num = -num;
+    if (minimal_num_length)
+      --minimal_num_length;
   }
-  result += AppendUnsigned(buff, buff_end, (u64)num, 10, 0);
+  result += AppendUnsigned(buff, buff_end, (u64)num, 10, minimal_num_length);
   return result;
 }
 
@@ -83,8 +88,8 @@  static int AppendPointer(char **buff, const char *buff_end, u64 ptr_value) {
 
 int VSNPrintf(char *buff, int buff_length,
               const char *format, va_list args) {
-  static const char *kPrintfFormatsHelp = "Supported Printf formats: "
-                                          "%%[z]{d,u,x}; %%p; %%s; %%c\n";
+  static const char *kPrintfFormatsHelp =
+    "Supported Printf formats: %%(0[0-9]*)?(z|ll)?{d,u,x}; %%p; %%s; %%c\n";
   RAW_CHECK(format);
   RAW_CHECK(buff_length > 0);
   const char *buff_end = &buff[buff_length - 1];
@@ -96,42 +101,55 @@  int VSNPrintf(char *buff, int buff_length,
       continue;
     }
     cur++;
+    bool have_width = (*cur == '0');
+    int width = 0;
+    if (have_width) {
+      while (*cur >= '0' && *cur <= '9') {
+        have_width = true;
+        width = width * 10 + *cur++ - '0';
+      }
+    }
     bool have_z = (*cur == 'z');
     cur += have_z;
+    bool have_ll = !have_z && (cur[0] == 'l' && cur[1] == 'l');
+    cur += have_ll * 2;
     s64 dval;
     u64 uval;
+    bool have_flags = have_width | have_z | have_ll;
     switch (*cur) {
       case 'd': {
-        dval = have_z ? va_arg(args, sptr)
-                      : va_arg(args, int);
-        result += AppendSignedDecimal(&buff, buff_end, dval);
+        dval = have_ll ? va_arg(args, s64)
+             : have_z ? va_arg(args, sptr)
+             : va_arg(args, int);
+        result += AppendSignedDecimal(&buff, buff_end, dval, width);
         break;
       }
       case 'u':
       case 'x': {
-        uval = have_z ? va_arg(args, uptr)
-                      : va_arg(args, unsigned);
+        uval = have_ll ? va_arg(args, u64)
+             : have_z ? va_arg(args, uptr)
+             : va_arg(args, unsigned);
         result += AppendUnsigned(&buff, buff_end, uval,
-                                 (*cur == 'u') ? 10 : 16, 0);
+                                 (*cur == 'u') ? 10 : 16, width);
         break;
       }
       case 'p': {
-        RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
+        RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
         result += AppendPointer(&buff, buff_end, va_arg(args, uptr));
         break;
       }
       case 's': {
-        RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
+        RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
         result += AppendString(&buff, buff_end, va_arg(args, char*));
         break;
       }
       case 'c': {
-        RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
+        RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
         result += AppendChar(&buff, buff_end, va_arg(args, int));
         break;
       }
       case '%' : {
-        RAW_CHECK_MSG(!have_z, kPrintfFormatsHelp);
+        RAW_CHECK_MSG(!have_flags, kPrintfFormatsHelp);
         result += AppendChar(&buff, buff_end, '%');
         break;
       }
@@ -151,7 +169,7 @@  void SetPrintfAndReportCallback(void (*callback)(const char *)) {
 }
 
 void Printf(const char *format, ...) {
-  const int kLen = 1024 * 4;
+  const int kLen = 16 * 1024;
   InternalScopedBuffer<char> buffer(kLen);
   va_list args;
   va_start(args, format);
@@ -177,7 +195,7 @@  int internal_snprintf(char *buffer, uptr length, const char *format, ...) {
 
 // Like Printf, but prints the current PID before the output string.
 void Report(const char *format, ...) {
-  const int kLen = 1024 * 4;
+  const int kLen = 16 * 1024;
   InternalScopedBuffer<char> buffer(kLen);
   int needed_length = internal_snprintf(buffer.data(),
                                         kLen, "==%d== ", GetPid());
diff --git a/libsanitizer/sanitizer_common/sanitizer_procmaps.h b/libsanitizer/sanitizer_common/sanitizer_procmaps.h
index 5e5e5ce..f4f6241 100644
--- a/libsanitizer/sanitizer_common/sanitizer_procmaps.h
+++ b/libsanitizer/sanitizer_common/sanitizer_procmaps.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -23,7 +25,6 @@  class MemoryMappingLayout {
   bool GetObjectNameAndOffset(uptr addr, uptr *offset,
                               char filename[], uptr filename_size) {
     UNIMPLEMENTED();
-    return false;
   }
 };
 
diff --git a/libsanitizer/sanitizer_common/sanitizer_stackdepot.cc b/libsanitizer/sanitizer_common/sanitizer_stackdepot.cc
index d9c5b69..6fb3d2d 100644
--- a/libsanitizer/sanitizer_common/sanitizer_stackdepot.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_stackdepot.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_stackdepot.cc -------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_stackdepot.h b/libsanitizer/sanitizer_common/sanitizer_stackdepot.h
index c4c388a..98db08a 100644
--- a/libsanitizer/sanitizer_common/sanitizer_stackdepot.h
+++ b/libsanitizer/sanitizer_common/sanitizer_stackdepot.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_stackdepot.h ----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_stacktrace.cc b/libsanitizer/sanitizer_common/sanitizer_stacktrace.cc
index f6d7a09..0a54e7c 100644
--- a/libsanitizer/sanitizer_common/sanitizer_stacktrace.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_stacktrace.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_stacktrace.cc -------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -75,7 +77,8 @@  void StackTrace::PrintStack(const uptr *addr, uptr size,
         Printf(" %s\n", StripPathPrefix(buff.data(), strip_file_prefix));
         frame_num++;
       }
-    } else if (symbolize) {
+    }
+    if (symbolize && addr_frames_num == 0) {
       // Use our own (online) symbolizer, if necessary.
       addr_frames_num = SymbolizeCode(pc, addr_frames.data(),
                                       addr_frames.size());
diff --git a/libsanitizer/sanitizer_common/sanitizer_stacktrace.h b/libsanitizer/sanitizer_common/sanitizer_stacktrace.h
index a7934c6..b823a7e 100644
--- a/libsanitizer/sanitizer_common/sanitizer_stacktrace.h
+++ b/libsanitizer/sanitizer_common/sanitizer_stacktrace.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_stacktrace.h ----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer.cc b/libsanitizer/sanitizer_common/sanitizer_symbolizer.cc
index 66ac3c8..30bcd96 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_symbolizer.cc -------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -291,7 +293,8 @@  class Symbolizer {
     }
   }
 
-  static const uptr kMaxNumberOfModuleContexts = 4096;
+  // 16K loaded modules should be enough for everyone.
+  static const uptr kMaxNumberOfModuleContexts = 1 << 14;
   LoadedModule *modules_;  // Array of module descriptions is leaked.
   uptr n_modules_;
 
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer.h b/libsanitizer/sanitizer_common/sanitizer_symbolizer.h
index 83adf02..1042c8e 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer.h
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer.h
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_symbolizer.h ----------------------------------*- C++ -*-===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -77,7 +79,7 @@  class LoadedModule {
   };
   char *full_name_;
   uptr base_address_;
-  static const uptr kMaxNumberOfAddressRanges = 8;
+  static const uptr kMaxNumberOfAddressRanges = 6;
   AddressRange ranges_[kMaxNumberOfAddressRanges];
   uptr n_ranges_;
 };
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer_linux.cc b/libsanitizer/sanitizer_common/sanitizer_symbolizer_linux.cc
index 50e39a7..4bd3dc8 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer_linux.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer_linux.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_symbolizer_linux.cc -------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -21,6 +23,7 @@ 
 #include <poll.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
 #if !defined(__ANDROID__) && !defined(ANDROID)
@@ -29,8 +32,15 @@ 
 
 namespace __sanitizer {
 
+static const int kSymbolizerStartupTimeMillis = 10;
+
 bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
                                int *input_fd, int *output_fd) {
+  if (!FileExists(path_to_symbolizer)) {
+    Report("WARNING: invalid path to external symbolizer!\n");
+    return false;
+  }
+
   int *infd = NULL;
   int *outfd = NULL;
   // The client program may close its stdin and/or stdout and/or stderr
@@ -97,13 +107,23 @@  bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
   internal_close(infd[1]);
   *input_fd = infd[0];
   *output_fd = outfd[1];
+
+  // Check that symbolizer subprocess started successfully.
+  int pid_status;
+  SleepForMillis(kSymbolizerStartupTimeMillis);
+  int exited_pid = waitpid(pid, &pid_status, WNOHANG);
+  if (exited_pid != 0) {
+    // Either waitpid failed, or child has already exited.
+    Report("WARNING: external symbolizer didn't start up correctly!\n");
+    return false;
+  }
+
   return true;
 }
 
 #if defined(__ANDROID__) || defined(ANDROID)
 uptr GetListOfModules(LoadedModule *modules, uptr max_modules) {
   UNIMPLEMENTED();
-  return 0;
 }
 #else  // ANDROID
 typedef ElfW(Phdr) Elf_Phdr;
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer_mac.cc b/libsanitizer/sanitizer_common/sanitizer_symbolizer_mac.cc
index c5ca616..2399360 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer_mac.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer_mac.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_symbolizer_mac.cc ---------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -18,12 +20,10 @@  namespace __sanitizer {
 bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
                                int *input_fd, int *output_fd) {
   UNIMPLEMENTED();
-  return false;
 }
 
 uptr GetListOfModules(LoadedModule *modules, uptr max_modules) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 }  // namespace __sanitizer
diff --git a/libsanitizer/sanitizer_common/sanitizer_symbolizer_win.cc b/libsanitizer/sanitizer_common/sanitizer_symbolizer_win.cc
index 7e6ba53..ad6c303 100644
--- a/libsanitizer/sanitizer_common/sanitizer_symbolizer_win.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_symbolizer_win.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_symbolizer_win.cc ---------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -20,12 +22,10 @@  namespace __sanitizer {
 bool StartSymbolizerSubprocess(const char *path_to_symbolizer,
                                int *input_fd, int *output_fd) {
   UNIMPLEMENTED();
-  return false;
 }
 
 uptr GetListOfModules(LoadedModule *modules, uptr max_modules) {
   UNIMPLEMENTED();
-  return 0;
 };
 
 }  // namespace __sanitizer
diff --git a/libsanitizer/sanitizer_common/sanitizer_win.cc b/libsanitizer/sanitizer_common/sanitizer_win.cc
index 3148523..90bf35a 100644
--- a/libsanitizer/sanitizer_common/sanitizer_win.cc
+++ b/libsanitizer/sanitizer_common/sanitizer_win.cc
@@ -1,5 +1,7 @@ 
 //===-- sanitizer_win.cc --------------------------------------------------===//
 //
+//                     The LLVM Compiler Infrastructure
+//
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
@@ -10,6 +12,9 @@ 
 // sanitizer_libc.h.
 //===----------------------------------------------------------------------===//
 #ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#include <stdlib.h>
 #include <windows.h>
 
 #include "sanitizer_common.h"
@@ -18,6 +23,10 @@ 
 namespace __sanitizer {
 
 // --------------------- sanitizer_common.h
+bool FileExists(const char *filename) {
+  UNIMPLEMENTED();
+}
+
 int GetPid() {
   return GetProcessId(GetCurrentProcess());
 }
@@ -39,7 +48,6 @@  void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
   *stack_bottom = (uptr)mbi.AllocationBase;
 }
 
-
 void *MmapOrDie(uptr size, const char *mem_type) {
   void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
   if (rv == 0) {
@@ -59,8 +67,12 @@  void UnmapOrDie(void *addr, uptr size) {
 }
 
 void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
-  return VirtualAlloc((LPVOID)fixed_addr, size,
-                      MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
+  void *p = VirtualAlloc((LPVOID)fixed_addr, size,
+      MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
+  if (p == 0)
+    Report("ERROR: Failed to allocate 0x%zx (%zd) bytes at %p (%d)\n",
+           size, size, fixed_addr, GetLastError());
+  return p;
 }
 
 void *Mprotect(uptr fixed_addr, uptr size) {
@@ -75,7 +87,6 @@  bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
 
 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 const char *GetEnv(const char *name) {
@@ -96,7 +107,6 @@  const char *GetEnv(const char *name) {
 
 const char *GetPwd() {
   UNIMPLEMENTED();
-  return 0;
 }
 
 void DumpProcessMap() {
@@ -113,7 +123,6 @@  void ReExec() {
 
 bool StackSizeIsUnlimited() {
   UNIMPLEMENTED();
-  return false;
 }
 
 void SetStackSizeLimitInBytes(uptr limit) {
@@ -137,39 +146,40 @@  void Abort() {
   _exit(-1);  // abort is not NORETURN on Windows.
 }
 
+#ifndef SANITIZER_GO
 int Atexit(void (*function)(void)) {
   return atexit(function);
 }
+#endif
 
 // ------------------ sanitizer_libc.h
 void *internal_mmap(void *addr, uptr length, int prot, int flags,
                     int fd, u64 offset) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 int internal_munmap(void *addr, uptr length) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 int internal_close(fd_t fd) {
   UNIMPLEMENTED();
-  return 0;
+}
+
+int internal_isatty(fd_t fd) {
+  UNIMPLEMENTED();
 }
 
 fd_t internal_open(const char *filename, bool write) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 uptr internal_read(fd_t fd, void *buf, uptr count) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 uptr internal_write(fd_t fd, const void *buf, uptr count) {
-  if (fd != 2)
+  if (fd != kStderrFd)
     UNIMPLEMENTED();
   HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
   if (err == 0)
@@ -182,21 +192,18 @@  uptr internal_write(fd_t fd, const void *buf, uptr count) {
 
 uptr internal_filesize(fd_t fd) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 int internal_dup2(int oldfd, int newfd) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
   UNIMPLEMENTED();
-  return 0;
 }
 
 int internal_sched_yield() {
-  UNIMPLEMENTED();
+  Sleep(0);
   return 0;
 }