public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory
@ 2014-10-16 17:03 bernd.edlinger at hotmail dot de
  2014-10-16 17:10 ` [Bug sanitizer/63564] " jakub at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: bernd.edlinger at hotmail dot de @ 2014-10-16 17:03 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63564

            Bug ID: 63564
           Summary: -fsanitize=address obscures access to free memory
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bernd.edlinger at hotmail dot de
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org

Created attachment 33737
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=33737&action=edit
test example with sem_post to free memory

Hi,

apparently the address sanitizer does not check the sem_post, sem_wait
and similar if the memory is free or the semaphore already deleted.

what makes this worse, is that the attached example crashes
in the following malloc but "works" with -fsanitize=address.

gcc -pthread test.c
./a.out
Segmentation fault (core dumped)

=> app crashes in malloc, not in sem_post!

gcc -pthread -fsanitize=address test.c
./a.out

=> works, and sanitizer "fixes" the malloc!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug sanitizer/63564] -fsanitize=address obscures access to free memory
  2014-10-16 17:03 [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory bernd.edlinger at hotmail dot de
@ 2014-10-16 17:10 ` jakub at gcc dot gnu.org
  2014-10-16 17:11 ` kcc at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-10-16 17:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63564

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
You would need glibc compiled with -fsanitize=address (unless the write is done
in assembly) to detect that.
And, the reason that glibc malloc reports the problem is that it performs some
cheap checks, in particular if you happen to overwrite glibc malloc's internal
data structures, it will sometimes be able to cheaply detect that and report.
asan malloc doesn't have anything like that.
Expecting that -fsanitize=address will reveal all issues in your code is
unrealistic.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug sanitizer/63564] -fsanitize=address obscures access to free memory
  2014-10-16 17:03 [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory bernd.edlinger at hotmail dot de
  2014-10-16 17:10 ` [Bug sanitizer/63564] " jakub at gcc dot gnu.org
@ 2014-10-16 17:11 ` kcc at gcc dot gnu.org
  2014-10-16 17:58 ` bernd.edlinger at hotmail dot de
  2014-10-16 18:14 ` kcc at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: kcc at gcc dot gnu.org @ 2014-10-16 17:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63564

--- Comment #2 from Kostya Serebryany <kcc at gcc dot gnu.org> ---
We don't intercept sem_post/sem_wait and don't instrument glibc where they are
implemented. Sadly, the same applies to quite a few other glibc functions. 
In future we may have fully instrumented glibc,
but in the meantime we need to add more interceptors. 
Patches are welcome, 
see https://code.google.com/p/address-sanitizer/wiki/HowToContribute

Most likely sanitizer_common/sanitizer_common_interceptors.inc  will need to be
modified.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug sanitizer/63564] -fsanitize=address obscures access to free memory
  2014-10-16 17:03 [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory bernd.edlinger at hotmail dot de
  2014-10-16 17:10 ` [Bug sanitizer/63564] " jakub at gcc dot gnu.org
  2014-10-16 17:11 ` kcc at gcc dot gnu.org
@ 2014-10-16 17:58 ` bernd.edlinger at hotmail dot de
  2014-10-16 18:14 ` kcc at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: bernd.edlinger at hotmail dot de @ 2014-10-16 17:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63564

--- Comment #3 from Bernd Edlinger <bernd.edlinger at hotmail dot de> ---
In the original example (it was ported from windows,
and the windows semaphores are completely immune
against this kind of error) the sem_post were in
*another* thread and there were several milliseconds
between the free the next malloc. So this is
already a really, really hard to find bug.
But what I don't understand, why the malloc does
*not* crash when address sanitizer is used. 
The same for thread sanitizer, it does not spot
the error, and the error does not happen in debug
builds only in release builds.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug sanitizer/63564] -fsanitize=address obscures access to free memory
  2014-10-16 17:03 [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory bernd.edlinger at hotmail dot de
                   ` (2 preceding siblings ...)
  2014-10-16 17:58 ` bernd.edlinger at hotmail dot de
@ 2014-10-16 18:14 ` kcc at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: kcc at gcc dot gnu.org @ 2014-10-16 18:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63564

--- Comment #4 from Kostya Serebryany <kcc at gcc dot gnu.org> ---
(In reply to Bernd Edlinger from comment #3)
> In the original example (it was ported from windows,
> and the windows semaphores are completely immune
> against this kind of error) the sem_post were in
> *another* thread and there were several milliseconds
> between the free the next malloc. So this is
> already a really, really hard to find bug.
> But what I don't understand, why the malloc does
> *not* crash when address sanitizer is used. 

If the program has undefined behavior then the program's behavior is, well, 
undefined. You can not expect malloc to crash if there is a bug in the code.
asan's malloc does not rely on malloc headers for bookeeping so it did not
crash.

> The same for thread sanitizer, it does not spot
> the error, and the error does not happen in debug
> builds only in release builds.

Good point. 
tsan does intercept sem_post/sem_wait but does not detect when there is a
use-after-free on it (we do it for pthread_mutex_lock though). 

Dmitry, You may want to add that to tsan.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-10-16 18:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-16 17:03 [Bug sanitizer/63564] New: -fsanitize=address obscures access to free memory bernd.edlinger at hotmail dot de
2014-10-16 17:10 ` [Bug sanitizer/63564] " jakub at gcc dot gnu.org
2014-10-16 17:11 ` kcc at gcc dot gnu.org
2014-10-16 17:58 ` bernd.edlinger at hotmail dot de
2014-10-16 18:14 ` kcc at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).