tsan and memory management

56 views
Skip to first unread message

Darin Ohashi

unread,
Aug 17, 2023, 8:32:00 AM8/17/23
to thread-sanitizer
Hello,

If I am using a custom memory manager that can recycle memory between threads, would tsan flag that as a data race?   So thread A allocates memory, uses it, then frees it to the memory manager.  Then thread B allocates that same block of memory and uses it.  Could tsan see that as a data race?

I am getting data race messages for cases that don't look like they should be a data race, but the thread is writing into a block of memory that is "newly allocated" from my custom memory manager.  It is possible that the block of memory was previously used on another thread.

If so, is there a way to tell tsan about this behaviour so these cases aren't reported?

Thanks

Darin

Dmitry Vyukov

unread,
Aug 17, 2023, 8:49:25 AM8/17/23
to Darin Ohashi, thread-sanitizer
On Thu, 17 Aug 2023 at 14:32, Darin Ohashi <doh...@gmail.com> wrote:
>
> Hello,
>
> If I am using a custom memory manager that can recycle memory between threads, would tsan flag that as a data race? So thread A allocates memory, uses it, then frees it to the memory manager. Then thread B allocates that same block of memory and uses it. Could tsan see that as a data race?
>
> I am getting data race messages for cases that don't look like they should be a data race, but the thread is writing into a block of memory that is "newly allocated" from my custom memory manager. It is possible that the block of memory was previously used on another thread.

Hi Darin,

From ThreadSantizer perspective a custom memory allocator is not
different from any other container. If it's implemented correctly,
then ThreadSantizer is not supposed to produce race reports.

> If so, is there a way to tell tsan about this behaviour so these cases aren't reported?

Fix bugs in the allocator ;)

Darin Ohashi

unread,
Aug 17, 2023, 9:46:06 AM8/17/23
to Dmitry Vyukov, thread-sanitizer
I guess I was thinking that since tsan doesn't know about the custom allocator, it doesn't know that when Thread A frees a piece of memory, it will not longer access it.  So when that memory is given to Thread B to use, tsan might not recognize the synchronization created but the free/allocate cycle and flag that as a data race.  If it is smart enough to work that out, that's great.

Thanks

Darin

Dmitry Vyukov

unread,
Aug 17, 2023, 9:54:36 AM8/17/23
to Darin Ohashi, thread-sanitizer
On Thu, 17 Aug 2023 at 15:46, Darin Ohashi <doh...@gmail.com> wrote:
>
> I guess I was thinking that since tsan doesn't know about the custom allocator, it doesn't know that when Thread A frees a piece of memory, it will not longer access it.

Does it need to know it?

Freeing and allocating a block is no different than pushing and
popping a pointer to/from some thread-safe container.
Reply all
Reply to author
Forward
0 new messages