[PATCH] D76073: [compiler-rt][tsan] Fix: Leak of ThreadSignalContext memory mapping when destroying fibers.

Florian via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 09:13:14 PDT 2020


Florian created this revision.
Florian added reviewers: dvyukov, vitalybuka.
Herald added subscribers: llvm-commits, Sanitizers, dberris.
Herald added projects: LLVM, Sanitizers.

When creating and destroying fibers in tsan a thread state is created and destroyed. Currently, a memory mapping is leaked with each fiber (in __tsan_destroy_fiber). This causes applications with many short running fibers to crash or hang because of linux vm.max_map_count.

The root of this is that ThreadState holds a pointer to ThreadSignalContext for handling signals. The initialization and destruction of it is tied to platform specific events in tsan_interceptors_posix and missed when destroying a fiber (specifically, SigCtx is used to lazily create the ThreadSignalContext in tsan_interceptors_posix). This patch cleans up the memory by inverting the control from the platform specific code calling the generic ThreadFinish to ThreadFinish calling a platform specific clean-up routine after finishing a thread.

The relevant code causing the leak with fibers is the fiber destruction:

  void FiberDestroy(ThreadState *thr, uptr pc, ThreadState *fiber) {
    FiberSwitchImpl(thr, fiber);
    ThreadFinish(fiber);
    FiberSwitchImpl(fiber, thr);
    internal_free(fiber);
  }

I would appreciate feedback if this way of fixing the leak is ok. 
Also, I think it would be worthwhile to more closely look at the lifecycle of ThreadState (i.e. it uses no constructor/destructor, thus requiring manual callbacks for cleanup) and how OS-Threads/user level fibers are differentiated in the codebase. I would be happy to contribute more if someone could point me at the right place to discuss this issue.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D76073

Files:
  compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
  compiler-rt/lib/tsan/rtl/tsan_platform.h
  compiler-rt/lib/tsan/rtl/tsan_platform_linux.cpp
  compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
  compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cpp
  compiler-rt/test/tsan/fiber_cleanup.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76073.249947.patch
Type: text/x-patch
Size: 5413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200312/98c2048b/attachment.bin>


More information about the llvm-commits mailing list