Commit Graph

377 Commits

Author SHA1 Message Date
Alex Shlyapnikov d3213c792e [LSan] Make LSan allocator allocator_may_return_null compliant
Summary:
An attempt to reland D34786 (which caused bot failres on Mac), now with
properly intercepted operators new() and delete().

LSan allocator used to always return nullptr on too big allocation requests
(the definition of "too big" depends on platform and bitness), now it
follows policy configured by allocator_may_return_null flag

Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34845

llvm-svn: 306845
2017-06-30 17:21:34 +00:00
Alex Shlyapnikov 346988bf02 Merge
llvm-svn: 306748
2017-06-29 21:54:38 +00:00
Alex Shlyapnikov cb823f6a19 [Sanitizers] Rename CallocShouldReturnNullDueToOverflow to CheckForCallocOverflow
Summary:
Due to changes in semantics, CheckForCallocOverflow makes much more sense
now.

Reviewers: eugenis

Subscribers: llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D34799

llvm-svn: 306747
2017-06-29 21:54:37 +00:00
Michael Zolotukhin 1576571112 Revert "[LSan] Make LSan allocator allocator_may_return_null compliant"
This reverts commit r306624.

The committed test failed on various bots (e.g. on green dragon).

llvm-svn: 306644
2017-06-29 04:39:17 +00:00
Alex Shlyapnikov 17277f13f0 [LSan] Make LSan allocator allocator_may_return_null compliant
Summary:
LSan allocator used to always return nullptr on too big allocation requests
(the definition of "too big" depends on platform and bitness), now it
follows policy configured by allocator_may_return_null flag.

Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34786

llvm-svn: 306624
2017-06-29 01:02:40 +00:00
Alex Shlyapnikov 4b450685d3 [Sanitizers] Operator new() interceptors always die on allocation error
Summary:
Operator new interceptors behavior is now controlled by their nothrow
property as well as by allocator_may_return_null flag value:

- allocator_may_return_null=* + new()        - die on allocation error
- allocator_may_return_null=0 + new(nothrow) - die on allocation error
- allocator_may_return_null=1 + new(nothrow) - return null

Ideally new() should throw std::bad_alloc exception, but that is not
trivial to achieve, hence TODO.

Reviewers: eugenis

Subscribers: kubamracek, llvm-commits

Differential Revision: https://reviews.llvm.org/D34731

llvm-svn: 306604
2017-06-28 21:58:57 +00:00
Alex Shlyapnikov ccab11b0e8 [Sanitizers] Move cached allocator_may_return_null flag to sanitizer_allocator
Summary:
Move cached allocator_may_return_null flag to sanitizer_allocator.cc and
provide API to consolidate and unify the behavior of all specific allocators.

Make all sanitizers using CombinedAllocator to follow
AllocatorReturnNullOrDieOnOOM() rules to behave the same way when OOM
happens.

When OOM happens, turn allocator_out_of_memory flag on regardless of
allocator_may_return_null flag value (it used to not to be set when
allocator_may_return_null == true).

release_to_os_interval_ms and rss_limit_exceeded will likely be moved to
sanitizer_allocator.cc too (later).

Reviewers: eugenis

Subscribers: srhines, kubamracek, llvm-commits

Differential Revision: https://reviews.llvm.org/D34310

llvm-svn: 305858
2017-06-20 21:23:02 +00:00
Francis Ricci 42175cb0d7 Add lsan interceptors for libdispatch functions on darwin
Summary:
This is required for standalone LSan to work with libdispatch worker threads,
and is a slimmed down version of the functionality provided for ASan
in asan_mac.cc.

Re-commit of r305695 with use_stacks=0 to get around a racy lingering pointer.

Reviewers: alekseyshl, kubamracek, glider, kcc

Subscribers: mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D34247

llvm-svn: 305732
2017-06-19 19:21:31 +00:00
Francis Ricci 214b354f2e Revert "Add lsan interceptors for libdispatch functions on darwin"
This reverts r305695

llvm-svn: 305712
2017-06-19 17:08:55 +00:00
Francis Ricci 4f1e047a6d Add lsan interceptors for libdispatch functions on darwin
Summary:
This is required for standalone LSan to work with libdispatch worker threads,
and is a slimmed down version of the functionality provided for ASan
in asan_mac.cc.

Reviewers: alekseyshl, kubamracek, glider, kcc

Subscribers: mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D34247

llvm-svn: 305695
2017-06-19 14:09:10 +00:00
Alex Shlyapnikov 5a308f24c3 [Sanitizer] Remove CombinedAllocator::Allocate's 'cleared' parameter
Summary:
CombinedAllocator::Allocate cleared parameter is not used anywhere and
seem to be obsolete.

Reviewers: eugenis

Subscribers: llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D34289

llvm-svn: 305590
2017-06-16 21:00:03 +00:00
Evgeniy Stepanov 790838110f Revert r304285, r304297.
r304285 - [sanitizer] Avoid possible deadlock in child process after fork
r304297 - [sanitizer] Trying to fix MAC buildbots after r304285

These changes create deadlock when Tcl calls pthread_create from a
pthread_atfork child handler. More info in the original review at
https://reviews.llvm.org/D33325

llvm-svn: 304735
2017-06-05 21:20:55 +00:00
Renato Golin 77a12b3972 Revert "[sanitizer-coverage] test for -fsanitize-coverage=inline-8bit-counters"
Revert "Mark sancov test as unsupported on Darwin"
Revert "[LSan] Detect dynamic loader by its base address."

This reverts commit r304633.
This reverts commit r304673.
This reverts commit r304632.

Those commit have broken LOTS of ARM/AArch64 bots for two days.

llvm-svn: 304699
2017-06-05 07:36:02 +00:00
Alex Shlyapnikov 2090504453 [LSan] Detect dynamic loader by its base address.
Summary:
Whenever possible (Linux + glibc 2.16+), detect dynamic loader module by
its base address, not by the module name matching. The current name
matching approach fails on some configurations.

Reviewers: eugenis

Subscribers: kubamracek, llvm-commits

Differential Revision: https://reviews.llvm.org/D33859

llvm-svn: 304633
2017-06-03 01:43:44 +00:00
Maxim Ostapenko b1f0a346d6 [sanitizer] Trying to fix MAC buildbots after r304285
It seems that on MAC allocator already locks on fork thus adding another ForceLock
in fork interceptor will cause a deadlock.

llvm-svn: 304297
2017-05-31 11:40:57 +00:00
Maxim Ostapenko 62a0f55930 [sanitizer] Avoid possible deadlock in child process after fork
This patch addresses https://github.com/google/sanitizers/issues/774. When we
fork a multi-threaded process it's possible to deadlock if some thread acquired
StackDepot or allocator internal lock just before fork. In this case the lock
will never be released in child process causing deadlock on following memory alloc/dealloc
routine. While calling alloc/dealloc routines after multi-threaded fork is not allowed,
most of modern allocators (Glibc, tcmalloc, jemalloc) are actually fork safe. Let's do the same
for sanitizers except TSan that has complex locking rules.

Differential Revision: https://reviews.llvm.org/D33325

llvm-svn: 304285
2017-05-31 07:28:09 +00:00
Francis Ricci 3bfbd70840 Fix typo in tls patch
llvm-svn: 303906
2017-05-25 19:55:44 +00:00
Francis Ricci 86e070f7e9 Implement tls scanning for darwin LSan
Summary:
This required for any users who call exit() after creating
thread-specific data, as tls destructors are only called when
pthread_exit() or pthread_cancel() are used. This should also
match tls behavior on linux.

Getting the base address of the tls section is straightforward,
as it's stored as a section offset in %gs. The size is a bit trickier
to work out, as there doesn't appear to be any official documentation
or source code referring to it. The size used in this patch was determined
by taking the difference between the base address and the address of the
subsequent memory region returned by vm_region_recurse_64, which was
1024 * sizeof(uptr) on all threads except the main thread, where it was
larger. Since the section must be the same size on all of the threads,
1024 * sizeof(uptr) seemed to be a reasonable size to use, barring
a more programtic way to get the size.

1024 seems like a reasonable number, given that PTHREAD_KEYS_MAX
is 512 on darwin, so pthread keys will fit inside the region while
leaving space for other tls data. A larger size would overflow the
memory region returned by vm_region_recurse_64, and a smaller size
wouldn't leave room for all the pthread keys. In addition, the
stress test added here passes, which means that we are scanning at
least the full set of possible pthread keys, and probably
the full tls section.

Reviewers: alekseyshl, kubamracek

Subscribers: krytarowski, llvm-commits

Differential Revision: https://reviews.llvm.org/D33215

llvm-svn: 303887
2017-05-25 17:41:13 +00:00
Francis Ricci dd592ff467 Use write instead of read permissions to check for global sections on mac
Summary:
The LINKEDIT section is very large and is read-only. Scanning this
section caused LSan on darwin to be very slow. When only writable sections
are scanned for global pointers, performance improved by a factor of about 25x.

Reviewers: alekseyshl, kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33322

llvm-svn: 303422
2017-05-19 13:34:02 +00:00
Francis Ricci 3b57da0c73 Revert "Implement tls scanning for darwin LSan"
This reverts r303262, due to TSan buildbot breakages.

llvm-svn: 303266
2017-05-17 15:25:41 +00:00
Francis Ricci eab89eb850 Implement tls scanning for darwin LSan
Summary:
This required for any users who call exit() after creating
thread-specific data, as tls destructors are only called when
pthread_exit() or pthread_cancel() are used. This should also
match tls behavior on linux.

Getting the base address of the tls section is straightforward,
as it's stored as a section offset in %gs. The size is a bit trickier
to work out, as there doesn't appear to be any official documentation
or source code referring to it. The size used in this patch was determined
by taking the difference between the base address and the address of the
subsequent memory region returned by vm_region_recurse_64, which was
1024 * sizeof(uptr) on all threads except the main thread, where it was
larger. Since the section must be the same size on all of the threads,
1024 * sizeof(uptr) seemed to be a reasonable size to use, barring
a more programtic way to get the size.

1024 seems like a reasonable number, given that PTHREAD_KEYS_MAX
is 512 on darwin, so pthread keys will fit inside the region while
leaving space for other tls data. A larger size would overflow the
memory region returned by vm_region_recurse_64, and a smaller size
wouldn't leave room for all the pthread keys. In addition, the
stress test added here passes, which means that we are scanning at
least the full set of possible pthread keys, and probably
the full tls section.

Reviewers: alekseyshl, kubamracek

Subscribers: krytarowski, llvm-commits

Differential Revision: https://reviews.llvm.org/D33215

llvm-svn: 303262
2017-05-17 14:35:17 +00:00
Alex Shlyapnikov cbb1fdf350 [lsan] Report the missing linker only when the linker is actually missing.
Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33218

llvm-svn: 303129
2017-05-15 23:11:01 +00:00
Kostya Kortchinsky dc646a0889 [sanitizer] Change SizeClassAllocator32 to accept just one template
Summary:
With rL279771, SizeClassAllocator64 was changed to accept only one template
instead of 5, for the following reasons: "First, this will make the mangled
names shorter. Second, this will make adding more parameters simpler". This
patch mirrors that work for SizeClassAllocator32.

This is in preparation for introducing the randomization of chunks in the
32-bit SizeClassAllocator in a later patch.

Reviewers: kcc, alekseyshl, dvyukov

Reviewed By: alekseyshl

Subscribers: llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D33141

llvm-svn: 303071
2017-05-15 14:47:19 +00:00
Francis Ricci 0f3d30960e Avoid unnecessary calls to vm_region_recurse
Summary: This should significantly improve darwin lsan performance in cases where root regions are not used.

Reviewers: alekseyshl, kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32966

llvm-svn: 302530
2017-05-09 14:10:30 +00:00
Maxim Ostapenko 726701b0ed [sanitizer] Intercept mcheck and mprobe on Linux
This patch addresses https://github.com/google/sanitizers/issues/804.
Users can use mcheck and mprobe functions to verify heap state so we should intercept them to avoid breakage of valid code.

Differential Revision: https://reviews.llvm.org/D32589

llvm-svn: 302001
2017-05-03 07:09:10 +00:00
Alex Shlyapnikov 67f83373e9 [lsan] When necessary, define LSan suppression for tls_get_addr.
Summary:
Generalize already defined LSan suppression for the leak on
tls_get_addr, some envs do not have the entire call stack symbolized,
so we have to be less specific.

Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32545

llvm-svn: 301434
2017-04-26 17:13:31 +00:00
Alex Shlyapnikov 7ca80051b9 [lsan] When necessary, define LSan suppression for pthread_exit.
Summary:
Generalize already defined LSan suppression for the leak on
pthread_exit, some envs do not have the entire call stack symbolized,
so we have to be less specific.

Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32497

llvm-svn: 301335
2017-04-25 17:24:27 +00:00
Alex Shlyapnikov 342586d728 [lsan] Enable LSan on PowerPC64.
Summary: Re-landing reverted D31995 with suppressions defined in D32303 and D32377.

Reviewers: eugenis

Subscribers: nemanjai, llvm-commits

Differential Revision: https://reviews.llvm.org/D32379

llvm-svn: 301048
2017-04-21 21:59:53 +00:00
Alex Shlyapnikov a3417bc4dd Suppress DTLS leak happening in some glibc versions.
Summary: Refer to https://sourceware.org/bugzilla/show_bug.cgi?id=12650 for the context.

Reviewers: eugenis

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32377

llvm-svn: 301043
2017-04-21 21:34:37 +00:00
Alex Shlyapnikov c426666b4d Disable LSan on ppc64, some tests are failing.
llvm-svn: 300933
2017-04-21 00:36:29 +00:00
Alex Shlyapnikov 906ffb7b8f Enable LSan on PowerPC64.
Summary: Re-landing reverted D31995 with suppressions defined in D32303.

Reviewers: eugenis

Subscribers: nemanjai, llvm-commits

Differential Revision: https://reviews.llvm.org/D32314

llvm-svn: 300903
2017-04-20 21:56:36 +00:00
Alex Shlyapnikov 3106fc476c Define standard suppressions for LSan, start with this one.
llvm-svn: 300887
2017-04-20 20:54:22 +00:00
Alex Shlyapnikov bdbb894a52 Define a suppression for known leaks on pthread_exit call.
Summary: Refer to D32194 for the context.

Reviewers: eugenis

Subscribers: kubamracek, llvm-commits

Differential Revision: https://reviews.llvm.org/D32303

llvm-svn: 300886
2017-04-20 20:54:19 +00:00
Francis Ricci 6e2b22f929 Fixup style from r300760
llvm-svn: 300765
2017-04-19 21:25:06 +00:00
Francis Ricci 9be010f0d0 Make sure to scan mmap'd memory regions for root pointers on OS X
Summary:
In the general case, we only need to check for root regions inside
the memory map returned by procmaps. However, on Darwin,
we also need to check inside mmap'd regions, which aren't returned
in the list of modules we get from procmaps.

This patch refactors memory region scanning on darwin to reduce
code duplication with the kernel alloc once page scan.

Reviewers: kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32190

llvm-svn: 300760
2017-04-19 21:11:08 +00:00
Francis Ricci eb930609e8 Implement StopTheWorld for Darwin
Reviewers: kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32189

llvm-svn: 300759
2017-04-19 21:11:07 +00:00
Francis Ricci 2096fa4bf9 Move valid caller-pc checks out of platform-specific checks
Summary:
ProcessPlatformSpecificAllocations for linux leak sanitizer iterated over
memory chunks and ran two checks concurrently:
1) Ensured the pc was valid
2) Checked whether it was a linker allocation

All platforms will need the valid pc check, so it is moved out of the platform-
specific file. To prevent code and logic duplication, the linker allocation
check is moved as well, with the name of the linker supplied by the platform-specific
module. In cases where we don't need to check for linker allocations (ie Darwin),
this name will be a nullptr, and we'll only run the caller pc checks.

Reviewers: kubamracek, alekseyshl, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32130

llvm-svn: 300690
2017-04-19 14:00:35 +00:00
Francis Ricci 5989dd241e Update suspended threads info to be compatible with darwin
Summary:
On Darwin, we need to track thread and tid as separate values.
This patch splits out the implementation of the suspended threads list
to be OS-specific.

Reviewers: glider, kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31474

llvm-svn: 300491
2017-04-17 20:29:38 +00:00
Kuba Mracek ceb30b0717 [sanitizer] Introduce tid_t as a typedef for OS-provided thread IDs
We seem to assume that OS-provided thread IDs are either uptr or int, neither of which is true on Darwin. This introduces a tid_t type, which holds a OS-provided thread ID (gettid on Linux, pthread_threadid_np on Darwin, pthread_self on FreeBSD).

Differential Revision: https://reviews.llvm.org/D31774

llvm-svn: 300473
2017-04-17 18:17:38 +00:00
Francis Ricci 7c6bf1cc9f Don't read non-readable address ranges during lsan pointer scanning
Summary: This specifically addresses the Mach-O zero page, which we cannot read from.

Reviewers: kubamracek, samsonov, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32044

llvm-svn: 300456
2017-04-17 16:34:38 +00:00
Francis Ricci 7de60c501c Scan Kernel Alloc Once page for global pointers
Summary: libxpc stashes some pointers here.

Reviewers: kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32045

llvm-svn: 300450
2017-04-17 14:07:06 +00:00
Francis Ricci 54ce07d093 Disable use of tls scanning on darwin leak sanitizer
Summary:
These checks appear linux-specific, disable them on darwin, at
least for now.

Reviewers: kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32013

llvm-svn: 300248
2017-04-13 20:14:15 +00:00
Alex Shlyapnikov 4765f17738 Revert "Enable LSan on PowerPC64."
This reverts commit r300204. Breaks ASAN tests on PPC.

llvm-svn: 300237
2017-04-13 18:49:29 +00:00
Francis Ricci 9e2152a4b8 Implement global pointer scanning for darwin leak sanitizer
Reviewers: kubamracek, kcc, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32012

llvm-svn: 300234
2017-04-13 18:40:19 +00:00
Francis Ricci bdb8b58d16 Don't assume PTHREAD_CREATE_JOINABLE is 0 on all systems
Summary:
Lsan was using PTHREAD_CREATE_JOINABLE/PTHREAD_CREATE_DETACHED
as truthy values, which works on Linux, where the values are 0 and 1,
but this fails on OS X, where the values are 1 and 2.

Set PTHREAD_CREATE_DETACHED to the correct value for a given system.

Reviewers: kcc, glider, kubamracek, alekseyshl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31883

llvm-svn: 300221
2017-04-13 17:28:52 +00:00
Alex Shlyapnikov d77394c5f2 Enable LSan on PowerPC64.
Summary:
With D31555 commited, looks like basic LSan functionality
works on PPC64. Time to enable LSan there.

Reviewers: eugenis

Subscribers: nemanjai, llvm-commits

Differential Revision: https://reviews.llvm.org/D31995

llvm-svn: 300204
2017-04-13 16:17:32 +00:00
Francis Ricci 03b2a8e47e Implement standalone lsan interceptors for OS X
Summary:
Mimicks the existing tsan and asan implementations of
Darwin interception.

Reviewers: kubamracek, kcc, glider

Subscribers: llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D31889

llvm-svn: 299979
2017-04-11 20:05:02 +00:00
Francis Ricci 84f17f32ad Don't delete lsan thread-local data until it's no longer required
Summary:
The routines for thread destruction in the thread registry require
the lsan thread index, which is stored in pthread tls on OS X.
This means that we need to make sure that the lsan tls isn't destroyed
until after the thread registry tls. This change ensures that we
don't delete the lsan tls until we've finished destroying the thread
in the registry, ensuring that the destructor for the lsan tls runs
after the destructor for the thread registry tls.

This patch also adds a check to ensure that the thread ID is valid before
returning it in GetThreadID(), to ensure that the above behavior
is working correctly.

Reviewers: dvyukov, kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31884

llvm-svn: 299978
2017-04-11 19:57:12 +00:00
Maxim Ostapenko de3b9a2ecc Reapply "Enable LSan for arm Linux"
This patch reapplies r299923 with typo fixed in BLX macros.

llvm-svn: 299948
2017-04-11 14:58:26 +00:00
Nico Weber 7124b5f6f9 Revert r299923, it doesn't build in bootstrap builds.
FAILED: lib/sanitizer_common/CMakeFiles/RTSanitizerCommon.arm.dir/sanitizer_linux.cc.o 
lib/sanitizer_common/sanitizer_linux.cc:1340:24: error: invalid instruction
                       BLX(ip)
                       ^
lib/sanitizer_common/sanitizer_linux.cc:1313:19: note: expanded from macro 'BLX'
#  define BLX(R) "mov lr, pc; bx" #R "\n"
                  ^
<inline asm>:6:13: note: instantiated into assembly here
mov lr, pc; bxip
            ^~~~

llvm-svn: 299943
2017-04-11 14:28:49 +00:00
Maxim Ostapenko 950d2809d5 [lsan] Enable LSan for arm Linux
This patch enables LSan for arm Linux.

Differential Revision: https://reviews.llvm.org/D29586

llvm-svn: 299923
2017-04-11 08:13:38 +00:00
Francis Ricci 4cce35f0ce Enable builds of darwin lsan by default
Summary: Testing and asan leak detection are disabled by default.

Reviewers: kubamracek, kcc

Subscribers: srhines, llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D31307

llvm-svn: 299669
2017-04-06 17:41:26 +00:00
Maxim Ostapenko fe863a6510 [lsan] Avoid segfaults during threads destruction under high load
This patch addresses two issues:

	* It turned out that suspended thread may have dtls->dtv_size == kDestroyedThread (-1)
	and LSan wrongly assumes that DTV is available. This leads to SEGV when LSan tries to
	iterate through DTV that is invalid.
	* In some rare cases GetRegistersAndSP can fail with errno 3 (ESRCH). In this case LSan
	assumes that the whole stack of a given thread is available. This is wrong because ESRCH
	can indicate that suspended thread was destroyed and its stack was unmapped. This patch
	properly handles ESRCH from GetRegistersAndSP in order to avoid invalid accesses to already
	unpapped threads stack.

Differential Revision: https://reviews.llvm.org/D30818

llvm-svn: 299630
2017-04-06 07:42:27 +00:00
Francis Ricci a79b8a22b4 Move current thread data out of lsan_common on linux
Summary:
Now that we have a platform-specific non-common lsan file, use
it to store non-common lsan data.

Reviewers: kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31472

llvm-svn: 299032
2017-03-29 21:49:47 +00:00
Francis Ricci 1aa20001ed Remove failing check from platform specific darwin lsan initializer
Summary:
We currently don't have any platform specific darwin
lsan modules, don't force failure if they don't exist.

Reviewers: kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31473

llvm-svn: 299031
2017-03-29 21:49:13 +00:00
Francis Ricci 99a84b163f Postpone lsan tls allocation until required
Summary:
This prevents InternalAlloc from being called before the sanitizers
are fully initialized.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31306

llvm-svn: 298947
2017-03-28 21:56:45 +00:00
Francis Ricci ab85292261 Enable i386 builds for darwin lsan
Summary:
Now that __thread is no longer used for lsan on darwin, i386 builds
can be enabled.

Reviewers: kcc, kubamracek

Subscribers: danalbert, srhines, mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D29995

llvm-svn: 298946
2017-03-28 21:56:44 +00:00
Francis Ricci f1c2f2a668 Fix missing sanitizer platform include
llvm-svn: 298884
2017-03-27 20:22:02 +00:00
Francis Ricci 2609fd319f Fix comment in lsan_linux.cc
llvm-svn: 298879
2017-03-27 19:44:11 +00:00
Francis Ricci 3e564e985f Move lsan allocator cache from lsan_common_linux to lsan_linux
Having this function in common seems to trigger a lot of unrelated
test failures. Given that this isn't really common code anyway,
move this to a new linux-specific lsan file.

llvm-svn: 298878
2017-03-27 19:42:37 +00:00
Francis Ricci d668a018f7 Use pthreads for thread-local lsan allocator cache on darwin
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D31291

llvm-svn: 298848
2017-03-27 14:07:50 +00:00
Francis Ricci dc13921fbc Factor lsan allocator cache accesses into a function
Summary:
This patch is the first step towards allows us to move away from using
__thread for the allocator cache on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

This will be followed by patches to move the function into OS-specific files.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29994

llvm-svn: 298537
2017-03-22 18:42:43 +00:00
Chandler Carruth 985f1a9417 Revert r298274: "Use pthreads for thread-local lsan allocator cache on darwin"
This fixes a failure currently present on the upstream linux boxes (and
reproduces for me as well):
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux/builds/1130/steps/64-bit%20check-asan-dynamic/logs/stdio

llvm-svn: 298382
2017-03-21 15:31:15 +00:00
Francis Ricci b91a5eabb3 Use pthreads for thread-local lsan allocator cache on darwin
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29994

llvm-svn: 298274
2017-03-20 17:06:42 +00:00
Francis Ricci d7b08a6c73 Revert "Use pthreads for thread-local lsan allocator cache on darwin"
This is still failing stack-use-after-return on linux-aarch64.

This reverts commit 5b350130fc4bf6f70c078a5d97096df98a17a057.

llvm-svn: 298246
2017-03-20 13:45:29 +00:00
Francis Ricci 9971b76d20 Use pthreads for thread-local lsan allocator cache on darwin
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29994

llvm-svn: 298214
2017-03-19 15:02:43 +00:00
Francis Ricci 7cd7d9bc8e Revert "Use pthreads for thread-local lsan allocator cache on darwin"
Reverting due to revert of prerequisite patch r296706

This reverts commit 6e1f23078c1acc44295065d28167043c4d31ddd1.

llvm-svn: 296720
2017-03-02 00:12:58 +00:00
Francis Ricci 923a320441 Use pthreads for thread-local lsan allocator cache on darwin
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29994

llvm-svn: 296707
2017-03-01 23:18:15 +00:00
Francis Ricci f891e90b35 Revert "Use pthreads for thread-local lsan allocator cache on darwin"
This caused a failure in Linux-x86_64 stack-use-after-return

This reverts commit 1f9563141e999016d13ac3fc6a50fde690381e82.

llvm-svn: 295449
2017-02-17 14:42:30 +00:00
Francis Ricci 01159f5c58 Use pthreads for thread-local lsan allocator cache on darwin
Summary:
This patch allows us to move away from using __thread on darwin,
which is requiring for building lsan for darwin on ios version 7
and on iossim i386.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29994

llvm-svn: 295413
2017-02-17 04:32:19 +00:00
Francis Ricci a0921f68ab Fix build breakage from moving kInvalidTid to lsan_common
llvm-svn: 295409
2017-02-17 03:34:56 +00:00
Francis Ricci 571c05a550 Use pthreads to store current thread id on darwin
Summary:
__thread is not supported by all darwin versions and architectures,
use pthreads instead to allow for building darwin lsan on iossim.

Reviewers: kubamracek, kcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29993

llvm-svn: 295405
2017-02-17 03:23:07 +00:00
Francis Ricci 8187c7b06b Add cmake build support for lsan on OS X
Summary:
Adds a new cmake flag 'COMPILER_RT_ENABLE_LSAN_OSX', which enables lsan
compilation and is turned off by default. Patches to fix build errors
when this flag is enabled will be uploaded soon.

This is part of an effort to port LSan to OS X, but LSan on OS X does not
currently work or pass tests currently.

Reviewers: kubamracek, kcc, glider, alekseyshl

Reviewed By: kubamracek

Subscribers: danalbert, srhines, mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D29783

llvm-svn: 295012
2017-02-14 00:56:53 +00:00
Francis Ricci 7cda03e6d1 Use pthreads to manage thread-local storage on darwin for leak sanitizer
Summary:
__thread is supported on Darwin, but is implemented dynamically via
function calls to __tls_get_addr. This causes two issues when combined
with leak sanitizer, due to malloc() interception.

- The dynamic loader calls malloc during the process of loading
  the sanitizer dylib, while swapping a placeholder tlv_boostrap
  function for __tls_get_addr. This will cause tlv_bootstrap to
  be called in DisabledInThisThread() via the asan allocator.

- The first time __tls_get_addr is called, it allocates memory
  for the thread-local object, during which it calls malloc(). This
  call will be intercepted, leading to an infinite loop in the asan
  allocator, in which the allocator calls DisabledInThisThread,
  which calls tls_get_addr, which calls into the allocator again.

Reviewers: kcc, glider, kubamracek

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29786

llvm-svn: 294994
2017-02-13 22:20:07 +00:00
Francis Ricci 259ad9cf08 Disable darwin interception of non-darwin functions
Summary: Fixes build issues when compiling lsan for darwin.

Reviewers: kubamracek, kcc, rnk

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D29785

llvm-svn: 294984
2017-02-13 21:06:13 +00:00
Francis Ricci 7406f0f54f Add lsan function stubs for darwin
Summary:
This patch provides stubs for all of the lsan platform-specific
functions which need to be implemented for darwin. Currently
all of these functions are stubs, for the purpose of fixing
compilation.

Reviewers: kcc, glider, kubamracek

Subscribers: mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D29784

llvm-svn: 294983
2017-02-13 21:03:15 +00:00
Kuba Mracek bba1d40589 [tsan] Properly describe GCD worker threads in reports
When dealing with GCD worker threads, TSan currently prints weird things like "created by thread T-1" and "[failed to restore the stack]" in reports. This patch avoids that and instead prints "Thread T3 (...) is a GCD worker thread".

Differential Revision: https://reviews.llvm.org/D29103

llvm-svn: 293882
2017-02-02 12:54:21 +00:00
Maxim Ostapenko 651cfe3cfa [lsan] Renable LSan for x86 Linux
The missed clang part was committed at https://reviews.llvm.org/rL293609 thus
we can reenable LSan for x86 Linux.

Differential Revision: https://reviews.llvm.org/D28609

llvm-svn: 293610
2017-01-31 07:15:37 +00:00
Dimitry Andric f2ac1d1df2 Recommit: Stop intercepting some malloc-related functions on FreeBSD and
macOS

Summary:
In https://bugs.freebsd.org/215125 I was notified that some configure
scripts attempt to test for the Linux-specific `mallinfo` and `mallopt`
functions by compiling and linking small programs which references the
functions, and observing whether that results in errors.

FreeBSD and macOS do not have the `mallinfo` and `mallopt` functions, so
normally these tests would fail, but when sanitizers are enabled, they
incorrectly succeed, because the sanitizers define interceptors for
these functions.  This also applies to some other malloc-related
functions, such as `memalign`, `pvalloc` and `cfree`.

Fix this by not intercepting `mallinfo`, `mallopt`, `memalign`,
`pvalloc` and `cfree` for FreeBSD and macOS, in all sanitizers.

Also delete the non-functional `cfree` wrapper for Windows, to fix the
test cases on that platform.

Reviewers: emaste, kcc, rnk

Subscribers: timurrrr, eugenis, hans, joerg, llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D27654

llvm-svn: 293536
2017-01-30 19:06:13 +00:00
Evgeniy Stepanov 8bf0633288 Revert "Stop intercepting some malloc-related functions on FreeBSD and macOS"
This reverts r293337, which breaks tests on Windows:

malloc-no-intercept-499eb7.o : error LNK2019: unresolved external symbol _mallinfo referenced in function _main

llvm-svn: 293346
2017-01-27 23:53:20 +00:00
Dimitry Andric 47e0ef3148 Stop intercepting some malloc-related functions on FreeBSD and macOS
Summary:
In https://bugs.freebsd.org/215125 I was notified that some configure
scripts attempt to test for the Linux-specific `mallinfo` and `mallopt`
functions by compiling and linking small programs which references the
functions, and observing whether that results in errors.

FreeBSD and macOS do not have the `mallinfo` and `mallopt` functions, so
normally these tests would fail, but when sanitizers are enabled, they
incorrectly succeed, because the sanitizers define interceptors for
these functions.  This also applies to some other malloc-related
functions, such as `memalign`, `pvalloc` and `cfree`.

Fix this by not intercepting `mallinfo`, `mallopt`, `memalign`,
`pvalloc` and `cfree` for FreeBSD and macOS, in all sanitizers.

Reviewers: emaste, kcc

Subscribers: hans, joerg, llvm-commits, kubamracek

Differential Revision: https://reviews.llvm.org/D27654

llvm-svn: 293337
2017-01-27 22:19:11 +00:00
Evgeniy Stepanov 0f550c8176 Revert "[lsan] Enable LSan for x86 Linux."
Breaks tests on i686/Linux due to missing clang driver support:
  error: unsupported option '-fsanitize=leak' for target 'i386-unknown-linux-gnu'

llvm-svn: 292844
2017-01-23 22:52:31 +00:00
Maxim Ostapenko 2523faf677 [lsan] Enable LSan for x86 Linux.
People keep asking LSan to be available on 32 bit targets (e.g. https://github.com/google/sanitizers/issues/403)
despite the fact that false negative ratio might be huge (up to 85%). This happens for big real world applications
that may contain random binary data (e.g. browser), but for smaller apps situation is not so terrible and LSan still might be useful.
This patch adds initial support for x86 Linux (disabled by default), ARM32 is in TODO list.
We used this patch (well, ported to GCC) on our 32 bit mobile emulators and it worked pretty fine
thus I'm posting it here to initiate further discussion.

Differential Revision: https://reviews.llvm.org/D28609

llvm-svn: 292775
2017-01-23 08:45:17 +00:00
Mike Aizatsky 9700acba11 Revert "[sancov] moving sancov rt to sancov/ directory"
This reverts commit https://reviews.llvm.org/rL291734
Reason: mac breakage
http://lab.llvm.org:8080/green//job/clang-stage1-configure-RA_build/28798/consoleFull#1657087648e9a0fee5-ebcc-4238-a641-c5aa112c323e

llvm-svn: 291736
2017-01-12 01:37:35 +00:00
Mike Aizatsky 875572f358 [sancov] moving sancov rt to sancov/ directory
Subscribers: kubabrecka, mgorny

Differential Revision: https://reviews.llvm.org/D28541

llvm-svn: 291734
2017-01-12 01:19:34 +00:00
Francis Ricci 9a2a9922ca Provide default implementations for sanitizer interface functions
Summary:
Adds a few default implementations for weak
interface functions on platforms where weak hooks are not supported.

Reviewers: eugenis, samsonov, timurrrr

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D28201

llvm-svn: 291313
2017-01-07 00:31:20 +00:00
Francis Ricci 1fb895e890 Ensure that only one compiler-rt component is created for lsan
Summary:
The lsan cmake configuration failed when targeting more
than one architecture, because it would attempt to create multiple
components with the same name. Ensure that only one lsan component
is ever created.

Reviewers: beanz, bogner

Subscribers: dberris, llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D28151

llvm-svn: 291294
2017-01-06 22:45:49 +00:00
Evgeniy Stepanov d3305afc75 Return memory to OS right after free (not in the async thread).
Summary:
In order to avoid starting a separate thread to return unused memory to
the system (the thread interferes with process startup on Android,
Zygota waits for all threads to exit before fork, but this thread never
exits), try to return it right after free.

Reviewers: eugenis

Subscribers: cryptoad, filcab, danalbert, kubabrecka, llvm-commits

Patch by Aleksey Shlyapnikov.

Differential Revision: https://reviews.llvm.org/D27003

llvm-svn: 288091
2016-11-29 00:22:50 +00:00
Maxim Ostapenko 984f42eed5 [asan/lsan] Avoid possible deadlock in dynamic ASan runtime thread initialization.
There is possible deadlock in dynamic ASan runtime when we dlopen() shared lib
which creates a thread at the global initialization stage. The scenario:

1) dlopen grabs a GI_pthread_mutex_lock in main thread.
2) main thread calls pthread_create, ASan intercepts it, calls real pthread_create
   and waits for the second thread to be "fully initialized".
3) Newly created thread tries to access a thread local disable_counter in LSan
   (to complete its "full initialization") and hangs in tls_get_addr_tail, because
   it also tries to acquire GI_pthread_mutex_lock.

The issue is reproducible on relative recent Glibc versions e.g. 2.23.

Differential Revision: https://reviews.llvm.org/D26028

llvm-svn: 285385
2016-10-28 06:49:53 +00:00
Maxim Ostapenko 6aafa89c34 [lsan] Relax check for allocator_end in ProcessGlobalRegionsCallback.
Differential Revision: https://reviews.llvm.org/D25945

llvm-svn: 285177
2016-10-26 06:56:51 +00:00
Kostya Serebryany 8633900e03 Make lsan complain loudly when running under ptrace
Summary:
LeakSanitizer does not work with ptrace but currently it
will print warnings (only under verbosity=1) and then proceed
to print tons of false reports.
This patch makes lsan fail hard under ptrace with a verbose message.

https://github.com/google/sanitizers/issues/728

Reviewers: eugenis, vitalybuka, aizatsky

Subscribers: kubabrecka, llvm-commits

Differential Revision: https://reviews.llvm.org/D25538

llvm-svn: 284171
2016-10-13 22:34:13 +00:00
Etienne Bergeron e7863a86ad [compiler-rt][lsan] Fix compiler error due to attribute (windows)
Summary:
Window compiler is stricter for attributes location. This patch fixes a compilation error.
```
D:\src\llvm\llvm\projects\compiler-rt\lib\lsan\lsan_thread.cc(39): error C2144: syntax error: 'int' should be preceded by ';'
```

Reviewers: rnk, majnemer

Subscribers: majnemer, llvm-commits, chrisha, dberris

Differential Revision: https://reviews.llvm.org/D24810

llvm-svn: 282254
2016-09-23 15:11:41 +00:00
Chris Bieneman 21395f9839 [CMake] Connect Compiler-RT targets to LLVM Runtimes directory
This patch builds on LLVM r279776.

In this patch I've done some cleanup and abstracted three common steps runtime components have in their CMakeLists files, and added a fourth.

The three steps I abstract are:

(1) Add a top-level target (i.e asan, msan, ...)
(2) Set the target properties for sorting files in IDE generators
(3) Make the compiler-rt target depend on the top-level target

The new step is to check if a command named "runtime_register_component" is defined, and to call it with the component name.

The runtime_register_component command is defined in llvm/runtimes/CMakeLists.txt, and presently just adds the component to a list of sub-components, which later gets used to generate target mappings.

With this patch a new workflow for runtimes builds is supported. The new workflow when building runtimes from the LLVM runtimes directory is:

> cmake [...]
> ninja runtimes-configure
> ninja asan

The "runtimes-configure" target builds all the dependencies for configuring the runtimes projects, and runs CMake on the runtimes projects. Running the runtimes CMake generates a list of targets to bind into the top-level CMake so subsequent build invocations will have access to some of Compiler-RT's targets through the top-level build.

Note: This patch does exclude some top-level targets from compiler-rt libraries because they either don't install files (sanitizer_common), or don't have a cooresponding `check` target (stats).
llvm-svn: 279863
2016-08-26 20:52:22 +00:00
Kostya Serebryany 7c5ae7cbc6 [sanitizer] enable random shuffling the memory chunks inside the allocator, under a flag. Set this flag for the scudo allocator, add a test.
llvm-svn: 279793
2016-08-26 00:06:03 +00:00
Kostya Serebryany 15647b17f3 [sanitizer] change SizeClassAllocator64 to accept just one template parameter instead of 5. First, this will make the mangled names shorter. Second, this will make adding more parameters simpler.
llvm-svn: 279771
2016-08-25 20:23:08 +00:00
Etienne Bergeron ab42f4ddba [compiler-rt] Fix VisualStudio virtual folders layout
Summary:
This patch is a refactoring of the way cmake 'targets' are grouped.
It won't affect non-UI cmake-generators.

Clang/LLVM are using a structured way to group targets which ease
navigation through Visual Studio UI. The Compiler-RT projects
differ from the way Clang/LLVM are grouping targets.

This patch doesn't contain behavior changes.

Reviewers: kubabrecka, rnk

Subscribers: wang0109, llvm-commits, kubabrecka, chrisha

Differential Revision: http://reviews.llvm.org/D21952

llvm-svn: 275111
2016-07-11 21:51:56 +00:00
Kostya Serebryany bf6a04fde8 [sanitizers] introduce yet another API function: __sanitizer_install_malloc_and_free_hooks
llvm-svn: 272943
2016-06-16 20:06:06 +00:00
Kuba Brecka 688c3d3bf1 [asan] Turn LSan-related #if’s into regular if’s in ASan initializer
Removing some preprocessor #if’s in favor of regular if’s. However, we need to declare empty stub functions to avoid linker errors.

Differential Revision: http://reviews.llvm.org/D20911

llvm-svn: 272047
2016-06-07 19:13:38 +00:00
Sagar Thakur 7bec3a94c6 [LSAN] Fix test swapcontext.cc on MIPS
There is no frame validity check in the slow unwinder like there is in the fast unwinder due to which lsan reports a leak even for heap allocated coroutine in the test swapcontext.cc. Since mips/linux uses slow unwindwer instead of fast unwinder, the test fails for mips/linux. Therefore adding the checks before unwinding fixes the test for mips/linux.

Reviewed by aizatsky.
Differential: http://reviews.llvm.org/D19961

llvm-svn: 269882
2016-05-18 06:09:26 +00:00
Marcin Koscielnicki b7b5ac60c4 [sanitizer] [SystemZ] Abort if the kernel might be vulnerable to CVE-2016-2143.
In short, CVE-2016-2143 will crash the machine if a process uses both >4TB
virtual addresses and fork().  ASan, TSan, and MSan will, by necessity, map
a sizable chunk of virtual address space, which is much larger than 4TB.
Even worse, sanitizers will always use fork() for llvm-symbolizer when a bug
is detected.  Disable all three by aborting on process initialization if
the running kernel version is not known to contain a fix.

Unfortunately, there's no reliable way to detect the fix without crashing
the kernel.  So, we rely on whitelisting - I've included a list of upstream
kernel versions that will work.  In case someone uses a distribution kernel
or applied the fix themselves, an override switch is also included.

Differential Revision: http://reviews.llvm.org/D19576

llvm-svn: 267747
2016-04-27 17:42:00 +00:00