[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-09 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 421758.
clemenswasser added a comment.

Hi,
today I found some more time to play/progress with this patch. I'm generally 
optimistic that most of the basic lsan stuff should already be working on 
Windows.
However without some help of you, I'm unable to get the tests to properly work. 
The main Problem I looked into today is that `llvm-lit.py` seems to be somehow 
unable to capture the stdout/err of the tests. More precisely my Debugging 
showed that when I insert a `print(procs[-1].stdout.read())` at 
TestRunner.py:790 I get the correctly captured stdout of a test e.g.:

  =
  ==25028==ERROR: LeakSanitizer: detected memory leaks
  
  Direct leak of 1337 byte(s) in 1 object(s) allocated from:
  #0 0x7ff77fc0488e in __asan_wrap_malloc 
C:\Users\cleme\dev\cpp\llvm-project\compiler-rt\lib\lsan\lsan_interceptors.cpp:75
  #1 0x7ff77fbbaafe in main 
C:\Users\cleme\dev\cpp\llvm-project\compiler-rt\test\lsan\TestCases\leak_check_at_exit.cpp:13:40
  #2 0x7ff77fc09e2b in invoke_main 
d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  #3 0x7ff77fc09e2b in __scrt_common_main_seh 
d:\a01\_work\43\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  #4 0x7ff8cb5654df  (C:\Windows\System32\KERNEL32.DLL+0x1800154df)
  #5 0x7ff8cc9e485a  (C:\Windows\SYSTEM32\ntdll.dll+0x18000485a)
  
  SUMMARY: LeakSanitizer: 1337 byte(s) leaked in 1 allocation(s).

However further below at lines 816-829 this output is lost 
(`proc.stdout.read()` returns a empty string, which obviously causes any 
FileCheck afterwards to fail)... and I can't figure out why. The only possible 
explaination I can think of is that the captured stdout is lost during the next 
loopiteration over `cmd.commands`


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "Leak

[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-10 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 421790.
clemenswasser added a comment.

I was wrong, the Problem was, that I didn't change the exit code, when a leak 
gets detected.
I now intercept ExitProcess and change the exit code in there, with this there 
are already 12 Tests passing on Windows.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "LeakSanitizer-Standalone"
   lsan_cflags = ["-fsanitize=leak"]
 elif lsan_lit_test_mode == "AddressSanitizer":
@@ -74,12 +75,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/test/lsan/TestCases/use_unaligned.cpp
==

[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-10 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 421791.
clemenswasser added a comment.

The CI seems to fail because of the parent revision? Retry


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "LeakSanitizer-Standalone"
   lsan_cflags = ["-fsanitize=leak"]
 elif lsan_lit_test_mode == "AddressSanitizer":
@@ -74,12 +75,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/test/lsan/TestCases/use_unaligned.cpp
===
--- compiler-rt/test/lsan/TestCases/use_unaligned.cpp
+++ compiler-rt/test/lsan/TestCases/use_unaligned.cpp
@@ -1,8 +1

[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-10 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 421795.
clemenswasser added a comment.

Since I am currently unable to intercept the ExitProcess/TerminateProcess in 
the ucrt (`exit_or_terminate_process` in `Windows 
Kits\10\Source\10.0.22000.0\ucrt\startup\exit.cpp:143`)
I inserted a call to `Die` in `HandleLeaks` (just like on Linux). This brings 
the passing Test up to 22


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "LeakSanitizer-Standalone"
   lsan_cflags = ["-fsanitize=leak"]
 elif lsan_lit_test_mode == "AddressSanitizer":
@@ -74,12 +75,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compi

[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-10 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 421796.
clemenswasser added a comment.

I always forget to change the line endings, sorry :(


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "LeakSanitizer-Standalone"
   lsan_cflags = ["-fsanitize=leak"]
 elif lsan_lit_test_mode == "AddressSanitizer":
@@ -74,12 +75,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/test/lsan/TestCases/use_unaligned.cpp
===
--- compiler-rt/test/lsan/TestCases/use_unaligned.cpp
+++ compiler-rt/test/lsan/TestCases/use_unaligned.cpp
@@ -1,8 +1,7 @@

[PATCH] D115103: Leak Sanitizer port to Windows

2022-04-23 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 424702.
clemenswasser added a comment.

I have now split the lsan TestCases chages into https://reviews.llvm.org/D124322
and managed to get the lsan+asan TestCases nearly to work. The current problem 
is,
that during the start of the `RunThread` in `StopTheWorld`, we are intercepting 
a call
to `calloc` done by the ucrt in `ucrtbase!DllMainDispatch`, which somehow 
results in a deadlock.
If someone has an idea, how we could avoid/fix this, I would be very happy to 
hear it.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -74,12 +74,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -19,8 +19,13 @@
 // windows.h needs to be included before tlhelp32.h
 #  include 
 
+#  include "interception/interception.h"
 #  include "sanitizer_stoptheworld.h"
 
+DECLARE_REAL(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES lpThreadAttributes,
+ SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
+ LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
+
 namespace __sanitizer {
 
 namespace {
@@ -163,7 +168,7 @@
   DWORD trace_thread_id;
 
   auto trace_thread =
-  CreateThread(nullptr, 0, RunThread, &arg, 0, &trace_thread_id);
+  REAL(CreateThread)(nullptr, 0, RunThread, &arg, 0, &trace_thread_id);
   CHECK(trace_thread);
 
   WaitForSingleObject(trace_thread, INFINITE);
Index: compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
===
--- compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -485,13 +485,15 @@
 #define SANITIZER_INTERCEPT_MMAP SI_POSIX
 #define SANITIZER_INTERCEPT_MMAP64 SI_LINUX_NOT_ANDROID || SI_SOLARIS
 #define SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO (SI_GLIBC || SI_ANDROID)
-#define SANITIZER_INTERCEPT_MEMALIGN (!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
+#define SANITIZER_INTERCEPT_MEMALIGN \
+  (!SI_FREEBSD && !SI_MAC && !SI_NETBSD && !SI_WINDOWS)
 #define SANITIZER_INTERCEPT___LIBC_MEMALIGN SI_GLIBC
 #define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
 #define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
 #define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
 #define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
-#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
+#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE \
+  (!SI_MAC && !SI_NETBSD && !SI_WINDOWS)
 #define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
 #define SANITIZER_INTERCEPT_WCSCAT SI_POSIX
 #define 

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-04 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser created this revision.
clemenswasser added a project: Sanitizers.
Herald added subscribers: abrachet, phosek, krytarowski, mgorny.
clemenswasser requested review of this revision.
Herald added a project: clang.
Herald added subscribers: Sanitizers, cfe-commits.

This was done by me in a few hours, so don't expect too much ;)

Initial Leak Sanitizer port to Windows.
The main part of the port was to implement `StopTheWorld` for Windows.
Many tests of the lsan test suite seem to run fine (leaks are correctly being 
reported), however `__lsan_ignore_object` doesn't seem to work.
Some other tests that fail are: new_array_with_dtor_0 and large_allocation_leak.
The test suite for lsan won't run for me:

  $ "LSAN_BASE=use_stacks=0:use_registers=0"
  'LSAN_BASE=use_stacks=0:use_registers=0': command not found

Also I need to figure out, how to get clang to auto link the lsan runtime when 
specifying `-fsanitize=address` on Windows.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,137 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#define WIN32_LEAN_AND_MEAN
+#include 
+
+#include 
+
+#include "sanitizer_stoptheworld.h"
+
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *ru

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-04 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 391870.
clemenswasser added a comment.

Changed to core.autocrlf=false


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,137 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#define WIN32_LEAN_AND_MEAN
+#include 
+
+#include 
+
+#include "sanitizer_stoptheworld.h"
+
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *run_args = (RunThreadArgs *)argument;
+  const HANDLE threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+
+  CHECK(threads != INVALID_HANDLE_VALUE);
+
+  const DWORD this_thread = GetCurrentThreadId();
+  const DWORD this_process = GetCurrentProcessId();
+
+  SuspendedThreadsListWindows suspended_threads_list;
+
+  THREADENTRY32 thread_entry;
+  thread_entry.dwSize = sizeof(thread_entry);
+  if (Thread32First(threads, &thread_entry)) {
+do {
+  if (thread_entry.dwSize >=
+  FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
+  sizeof(thread_entry.th32OwnerProcessID)) {
+if (thread_entry.th32ThreadID == this_thread ||
+thread_entry.th32OwnerProcessID != this_process)
+  continue;
+
+const HANDLE thread =
+OpenThread(THR

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-04 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 391872.
clemenswasser added a comment.

Move `__lsan_init` call inside of `SANITIZER_WINDOWS` ifdef


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,137 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#define WIN32_LEAN_AND_MEAN
+#include 
+
+#include 
+
+#include "sanitizer_stoptheworld.h"
+
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *run_args = (RunThreadArgs *)argument;
+  const HANDLE threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+
+  CHECK(threads != INVALID_HANDLE_VALUE);
+
+  const DWORD this_thread = GetCurrentThreadId();
+  const DWORD this_process = GetCurrentProcessId();
+
+  SuspendedThreadsListWindows suspended_threads_list;
+
+  THREADENTRY32 thread_entry;
+  thread_entry.dwSize = sizeof(thread_entry);
+  if (Thread32First(threads, &thread_entry)) {
+do {
+  if (thread_entry.dwSize >=
+  FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
+  sizeof(thread_entry.th32OwnerProcessID)) {
+if (thread_entry.th32ThreadID == this_thread ||
+thread_entry.th32OwnerProcessID != this_process)
+  continue;
+
+const HANDLE thread 

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-04 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 391873.
clemenswasser added a comment.

Apply clang-format


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,138 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#  define WIN32_LEAN_AND_MEAN
+#  include 
+
+// windows.h needs to be included before tlhelp32.h
+#  include 
+
+#  include "sanitizer_stoptheworld.h"
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *run_args = (RunThreadArgs *)argument;
+  const HANDLE threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+
+  CHECK(threads != INVALID_HANDLE_VALUE);
+
+  const DWORD this_thread = GetCurrentThreadId();
+  const DWORD this_process = GetCurrentProcessId();
+
+  SuspendedThreadsListWindows suspended_threads_list;
+
+  THREADENTRY32 thread_entry;
+  thread_entry.dwSize = sizeof(thread_entry);
+  if (Thread32First(threads, &thread_entry)) {
+do {
+  if (thread_entry.dwSize >=
+  FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
+  sizeof(thread_entry.th32OwnerProcessID)) {
+if (thread_entry.th32ThreadID == this_thread ||
+thread_entry.th32OwnerProcessID != this_process)
+  conti

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-04 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 391875.
clemenswasser added a comment.

Use LF line endings


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,138 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#  define WIN32_LEAN_AND_MEAN
+#  include 
+
+// windows.h needs to be included before tlhelp32.h
+#  include 
+
+#  include "sanitizer_stoptheworld.h"
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *run_args = (RunThreadArgs *)argument;
+  const HANDLE threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+
+  CHECK(threads != INVALID_HANDLE_VALUE);
+
+  const DWORD this_thread = GetCurrentThreadId();
+  const DWORD this_process = GetCurrentProcessId();
+
+  SuspendedThreadsListWindows suspended_threads_list;
+
+  THREADENTRY32 thread_entry;
+  thread_entry.dwSize = sizeof(thread_entry);
+  if (Thread32First(threads, &thread_entry)) {
+do {
+  if (thread_entry.dwSize >=
+  FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
+  sizeof(thread_entry.th32OwnerProcessID)) {
+if (thread_entry.th32ThreadID == this_thread ||
+thread_entry.th32OwnerProcessID != this_process)
+  cont

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-06 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 392017.
clemenswasser added a comment.

Remove wrong interceptors


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.cpp
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/CMakeLists.txt
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -0,0 +1,138 @@
+//===-- sanitizer_stoptheworld_win.cpp ===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// See sanitizer_stoptheworld.h for details.
+//
+//===--===//
+
+#include "sanitizer_common.h"
+#include "sanitizer_internal_defs.h"
+#include "sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+
+#  define WIN32_LEAN_AND_MEAN
+#  include 
+
+// windows.h needs to be included before tlhelp32.h
+#  include 
+
+#  include "sanitizer_stoptheworld.h"
+
+namespace __sanitizer {
+
+struct SuspendedThreadsListWindows final : public SuspendedThreadsList {
+  InternalMmapVector threadHandles;
+  InternalMmapVector threadIds;
+
+  SuspendedThreadsListWindows() {
+threadIds.reserve(1024);
+threadHandles.reserve(1024);
+  }
+
+  PtraceRegistersStatus GetRegistersAndSP(uptr index,
+  InternalMmapVector *buffer,
+  uptr *sp) const override;
+
+  tid_t GetThreadID(uptr index) const override;
+  uptr ThreadCount() const override;
+};
+
+PtraceRegistersStatus SuspendedThreadsListWindows::GetRegistersAndSP(
+uptr index, InternalMmapVector *buffer, uptr *sp) const {
+  CHECK_LT(index, threadHandles.size());
+
+  CONTEXT thread_context;
+  thread_context.ContextFlags = CONTEXT_ALL;
+  CHECK(GetThreadContext(threadHandles[index], &thread_context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), &thread_context, sizeof(thread_context));
+
+  *sp = thread_context.Rsp;
+
+  return REGISTERS_AVAILABLE;
+}
+
+tid_t SuspendedThreadsListWindows::GetThreadID(uptr index) const {
+  CHECK_LT(index, threadIds.size());
+  return threadIds[index];
+}
+
+uptr SuspendedThreadsListWindows::ThreadCount() const {
+  return threadIds.size();
+}
+
+struct RunThreadArgs {
+  StopTheWorldCallback callback;
+  void *argument;
+};
+
+DWORD WINAPI RunThread(void *argument) {
+  RunThreadArgs *run_args = (RunThreadArgs *)argument;
+  const HANDLE threads = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
+
+  CHECK(threads != INVALID_HANDLE_VALUE);
+
+  const DWORD this_thread = GetCurrentThreadId();
+  const DWORD this_process = GetCurrentProcessId();
+
+  SuspendedThreadsListWindows suspended_threads_list;
+
+  THREADENTRY32 thread_entry;
+  thread_entry.dwSize = sizeof(thread_entry);
+  if (Thread32First(threads, &thread_entry)) {
+do {
+  if (thread_entry.dwSize >=
+  FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) +
+  sizeof(thread_entry.th32OwnerProcessID)) {
+if (thread_entry.th32ThreadID == this_thread ||
+thread_entry.th32OwnerProcessID != this_process)
+

[PATCH] D115103: Leak Sanitizer port to Windows

2023-02-01 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@vitalybuka There seems to be a bug in MSVCs bit-field implementation, which 
causes the `disable.c` test to fail.
This reproducer (assert) passes with gcc and fails with MSVC (`m.tag` is 
`0x`):

  cpp
  #include 
  #include 
  
  enum ChunkTag {
kDirectlyLeaked = 0,  // default
kIndirectlyLeaked = 1,
kReachable = 2,
kIgnored = 3
  };
  
  struct ChunkMetadata {
uint8_t allocated : 8;  // Must be first.
ChunkTag tag : 2;
uintptr_t requested_size : 54;
uint32_t stack_trace_id;
  };
  
  int main() {
  ChunkMetadata m;
  m.tag = kIgnored;
  assert(m.tag == kIgnored);
  }

Do you have a suggestion how I could fix this on MSVC


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2023-02-01 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@aganea Thank you very much!


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2023-02-01 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

Wow, this fixed a lot of tests, down to only 17 lsan standalone tests failing 
:) (26 already passing)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2023-02-18 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

I am currently unable to progress as all of my interceptors are not working.
The interceptors are failing, because the `GetInstructionSize` function fails.
For example, the `CreateThread` Win32 function starts on my Windows 11 
installation with `4c 8b dc mov r11,rsp` (in little-endian hex: `0xdc8b4c`).
I get this exact value when using the Visual Studio Watch Window with the 
expression: `0x00FF & *(uint32_t*)address` while debugging 
`GetInstructionSize`.
This was then verified by me again in the Visual Studio Disassembly View of 
`CreateThread` and a third time via this simple test program:

  #include 
  #include 
  #include 
  int main() {
auto* ptr = CreateThread;
printf("CreateThread: %p\n", ptr);
printf("CreateThread first bytes: %s\n", 
std::to_string(*(int*)ptr).c_str());
  }

This exact instruction is correctly handled by the following switch in 
`GetInstructionSize`:

  switch (0x00FF & *(u32*)address) {
...
case 0xdc8b4c:// 4c 8b dc : mov r11, rsp
  return 3;

But `GetInstructionSize` never returns 3 as the switch condition reads 
`0xdc8bcc` from address which is != `0xdc8b4c` and instead encodes:

  0:  cc  int3
  1:  8b dc   movebx,esp

I don't really understand why this would read this **obviously wrong** value 
when my similar test program reads it correctly.
Does anyone of you know why this might be happening and how I could fix this?
(BTW. Asan also intercepts CreateThreads and that seems to work...)


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2023-02-20 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@aganea You can find my current changes on my D115103 branch 
. I am using the 
currently failing `use_stacks.cpp` test.
Something like this should make debugging of this issue possible:

  > cd llvm-project
  > cmake -G Ninja -B build -D CMAKE_BUILD_TYPE=Debug 
-DLLVM_ENABLE_PROJECTS=clang;compiler-rt
  > cmake --build build -t check-lsan
  > cp 
build\projects\compiler-rt\test\lsan\X86_64LsanConfig\TestCases\Output\use_stacks.cpp.tmp
 
build\projects\compiler-rt\test\lsan\X86_64LsanConfig\TestCases\Output\use_stacks.cpp.exe
  > devenv /debugexe 
build\projects\compiler-rt\test\lsan\X86_64LsanConfig\TestCases\Output\use_stacks.cpp.exe

You should see that the `LSAN_MAYBE_INTERCEPT_CREATE_THREAD` in 
`__lsan::InitializeInterceptors` fails because of the wrong instruction read of 
the `CreateThread` function address in `GetInstructionSize` as described above.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2022-12-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 481042.
clemenswasser added a comment.

@vitalybuka
I've been working on this on-off for the past months and have gotten the tests 
in the following state:

  Unsupported: 41
  Passed : 19
  Failed : 48

There is still the bug with the tracer thread deadlocking during an allocation 
made in the Microsoft CRT thread setup code when using Asan+Lsan.
I hope I can get the failing tests passing.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
  compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -74,12 +74,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
===
--- compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
+++ compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp
@@ -19,8 +19,13 @@
 // windows.h needs to be included before tlhelp32.h
 #  include 
 
+#  include "interception/interception.h"
 #  include "sanitizer_stoptheworld.h"
 
+DECLARE_REAL(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES lpThreadAttributes,
+ SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress,
+ LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId);
+
 namespace __sanitizer {
 
 namespace {
@@ -162,12 +167,15 @@
   struct RunThreadArgs arg = {callback, argument};
   DWORD trace_thread_id;
 
-  auto trace_thread =
-  CreateThread(nullptr, 0, RunThread, &arg, 0, &trace_thread_id);
-  CHECK(trace_thread);
-
-  WaitForSingleObject(trace_thread, INFINITE);
-  CloseHandle(trace_thread);
+  // TODO(cwasser): Spawning a tracer thread at this point causes a deadlock in
+  // malloc during thread creation.
+  //auto trace_thread =
+  //REAL(CreateThread)(nullptr, 0, RunThread, &arg, 0, &trace_thread_id);
+  //CHECK(trace_thread);
+  //
+  //WaitForSingleObject(trace_thread, INFINITE);
+  //CloseHandle(trace_thread);
+  RunThread(&arg);
 }
 
 }  // namespace __sanitizer
Index: compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
===
--- compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -486,13 +486,15 @@
 #define SANITIZER_INTERCEPT_MMAP SI_POSIX
 #define SANITIZER_INTERCEPT_MMAP64 SI_LINUX_NOT_ANDROID || SI_SOLARIS
 #define SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO (SI_GLIBC || SI_ANDROID)
-#define SANITIZER_INTERCEPT_MEMALIGN (!SI_FREEBSD && !SI_MAC && !SI_NETBSD)
+#define SANITIZER_INTERCEPT_MEMALIGN \
+  (!SI_FREEBSD && !SI_MAC && !SI_NETBSD && !SI_WINDOWS)
 #define SANITIZER_INTERCEPT___LIBC_MEMALIGN SI_GLIBC
 #define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
 #define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
 #define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
 #define SANITIZER_

[PATCH] D115103: Leak Sanitizer port to Windows

2022-01-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@aganea Thanks for your help. I already did everything you wrote (I am however 
using the coreutils provided by Git).
`check-asan` works on my new computer. I still don't know why it didn't work on 
my older one...
I will try to get the lsan tests working (and hopefully passing 😉) on windows 
soon.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2022-01-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

All the lsan tests, when run in lsan+asan mode, Deadlock in `StopTheWord` when 
creating the tracer thread...
F21505206: image.png 
-> `check-lsan` gets stuck, due to all the deadlocking tests :(

Could it be caused by asan intercepting the Win32 calls in `StopTheWorld?
@vitalybuka Could this be a problem? If so, how can I call the "real" 
functions, without being intercepted by asan?
Is there a way (for now), to only run the lsan tests in lsan only mode (no 
asan)?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2022-01-13 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 399689.
clemenswasser edited the summary of this revision.
clemenswasser added a comment.

Updated the Tests.
The Problem with Lsan+Asan still exists. I have just disabled them for now.
I hope anyone can help me with getting the tests to run correctly?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/TestCases/Darwin/dispatch.mm
  compiler-rt/test/lsan/TestCases/Linux/cleanup_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/disabler_in_tsd_destructor.c
  compiler-rt/test/lsan/TestCases/Linux/use_tls_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_dynamic.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_pthread_specific_static.cpp
  compiler-rt/test/lsan/TestCases/Linux/use_tls_static.cpp
  compiler-rt/test/lsan/TestCases/disabler.c
  compiler-rt/test/lsan/TestCases/disabler.cpp
  compiler-rt/test/lsan/TestCases/do_leak_check_override.cpp
  compiler-rt/test/lsan/TestCases/high_allocator_contention.cpp
  compiler-rt/test/lsan/TestCases/ignore_object.c
  compiler-rt/test/lsan/TestCases/large_allocation_leak.cpp
  compiler-rt/test/lsan/TestCases/leak_check_at_exit.cpp
  compiler-rt/test/lsan/TestCases/link_turned_off.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_pthread.cpp
  compiler-rt/test/lsan/TestCases/many_tls_keys_thread.cpp
  compiler-rt/test/lsan/TestCases/pointer_to_self.cpp
  compiler-rt/test/lsan/TestCases/print_suppressions.cpp
  compiler-rt/test/lsan/TestCases/recoverable_leak_check.cpp
  compiler-rt/test/lsan/TestCases/register_root_region.cpp
  compiler-rt/test/lsan/TestCases/stale_stack_leak.cpp
  compiler-rt/test/lsan/TestCases/suppressions_default.cpp
  compiler-rt/test/lsan/TestCases/suppressions_file.cpp
  compiler-rt/test/lsan/TestCases/use_after_return.cpp
  compiler-rt/test/lsan/TestCases/use_globals_initialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_uninitialized.cpp
  compiler-rt/test/lsan/TestCases/use_globals_unused.cpp
  compiler-rt/test/lsan/TestCases/use_poisoned_asan.cpp
  compiler-rt/test/lsan/TestCases/use_registers.cpp
  compiler-rt/test/lsan/TestCases/use_registers_extra.cpp
  compiler-rt/test/lsan/TestCases/use_stacks.cpp
  compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp
  compiler-rt/test/lsan/TestCases/use_unaligned.cpp
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -22,7 +22,8 @@
 # Choose between standalone and LSan+ASan modes.
 lsan_lit_test_mode = get_required_attr(config, 'lsan_lit_test_mode')
 
-if lsan_lit_test_mode == "Standalone":
+# FIXME: All tests are Standalone on Windows for now. Since all win32 function calls in lsan get intercepted by asan :(
+if lsan_lit_test_mode == "Standalone" or config.host_os == 'Windows':
   config.name = "LeakSanitizer-Standalone"
   lsan_cflags = ["-fsanitize=leak"]
 elif lsan_lit_test_mode == "AddressSanitizer":
@@ -74,12 +75,13 @@
 config.substitutions.append( ("%clangxx_lsan ", build_invocation(clang_lsan_cxxflags)) )
 
 # LeakSanitizer tests are currently supported on
-# Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
+# Windows{x86_64, x86, aarch64, arm}, Android{aarch64, x86, x86_64}, x86-64 Linux, PowerPC64 Linux, arm Linux, mips64 Linux, s390x Linux and x86_64 Darwin.
 supported_android = config.android and config.target_arch in ['x86_64', 'i386', 'aarch64'] and 'android-thread-properties-api' in config.available_features
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows' and config.target_arch in ['x86_64', 'i386', 'aarch64', 'arm']
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/test/lsan/TestCases/use_unaligned.cpp

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added inline comments.



Comment at: compiler-rt/lib/lsan/lsan.h:20
+#elif SANITIZER_WINDOWS
+#  include "lsan_win.h"
 #endif

MyDeveloperDay wrote:
> clang-format?
Yes, this is caused by clang-format. What should I do about it?



Comment at: compiler-rt/lib/lsan/lsan.h:20
+#elif SANITIZER_WINDOWS
+#  include "lsan_win.h"
 #endif

vitalybuka wrote:
> clemenswasser wrote:
> > MyDeveloperDay wrote:
> > > clang-format?
> > Yes, this is caused by clang-format. What should I do about it?
> @clemenswasser  Can you please a separate tiny patch which clang-format 
> nearby lines
@vitalybuka Should I create a patch, where I format the whole file or just 
these `#include`s?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 392444.
clemenswasser added a comment.

Split this patch up into multiple smaller ones:

- D115186 
- D115204 
- D115262 


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/lsan/lsan_win.h
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.h
@@ -0,0 +1,41 @@
+//=-- lsan_win.h -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to Windows systems.
+//
+//===-===//
+
+#ifndef LSAN_WINDOWS_H
+#define LSAN_WINDOWS_H
+
+#include "lsan_thread.h"
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if !SANITIZER_WINDOWS
+#  error "lsan_win.h is used only on Windows systems (SANITIZER_WINDOWS)"
+#endif
+
+namespace __sanitizer {
+struct DTLS;
+}
+
+namespace __lsan {
+
+class ThreadContext final : public ThreadContextLsanBase {
+ public:
+  explicit ThreadContext(int tid);
+  void OnStarted(void *arg) override;
+};
+
+void ThreadStart(u32 tid, tid_t os_id,
+ ThreadType thread_type = ThreadType::Regular);
+
+}  // namespace __lsan
+
+#endif  // LSAN_WINDOWS_H
Index: compiler-rt/lib/lsan/lsan_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.cpp
@@ -0,0 +1,106 @@
+//=-- lsan_win.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to POSIX-like systems.
+//
+//===-===//
+
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+#  include "lsan.h"
+#  include "lsan_allocator.h"
+#  include "sanitizer_common/sanitizer_stacktrace.h"
+#  include "sanitizer_common/sanitizer_tls_get_addr.h"
+
+namespace __lsan {
+
+ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
+
+struct OnStartedArgs {
+  uptr stack_begin;
+  uptr stack_end;
+  uptr cache_begin;
+  uptr cache_end;
+  uptr tls_begin;
+  uptr tls_end;
+};
+
+void ThreadContext::OnStarted(void *arg) {
+  auto args = reinterpret_cast(arg);
+  stack_begin_ = args->stack_begin;
+  stack_end_ = args->stack_end;
+  cache_begin_ = args->cache_begin;
+  cache_end_ = args->cache_end;
+}
+
+void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
+  OnStartedArgs args;
+  uptr stack_size = 0;
+  uptr tls_size = 0;
+  GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
+   &args.tls_begin, &tls_size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
+}
+
+bool GetThreadRangesLocked(tid_t 

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-07 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 392446.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h
  compiler-rt/test/lsan/lit.common.cfg.py

Index: compiler-rt/test/lsan/lit.common.cfg.py
===
--- compiler-rt/test/lsan/lit.common.cfg.py
+++ compiler-rt/test/lsan/lit.common.cfg.py
@@ -79,7 +79,8 @@
 supported_linux = (not config.android) and config.host_os == 'Linux' and config.host_arch in ['aarch64', 'x86_64', 'ppc64', 'ppc64le', 'mips64', 'riscv64', 'arm', 'armhf', 'armv7l', 's390x']
 supported_darwin = config.host_os == 'Darwin' and config.target_arch in ['x86_64']
 supported_netbsd = config.host_os == 'NetBSD' and config.target_arch in ['x86_64', 'i386']
-if not (supported_android or supported_linux or supported_darwin or supported_netbsd):
+supported_windows = config.host_os == 'Windows'
+if not (supported_android or supported_linux or supported_darwin or supported_netbsd or supported_windows):
   config.unsupported = True
 
 # Don't support Thumb due to broken fast unwinder
Index: compiler-rt/lib/lsan/lsan_win.h
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.h
@@ -0,0 +1,41 @@
+//=-- lsan_win.h -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to Windows systems.
+//
+//===-===//
+
+#ifndef LSAN_WINDOWS_H
+#define LSAN_WINDOWS_H
+
+#include "lsan_thread.h"
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if !SANITIZER_WINDOWS
+#  error "lsan_win.h is used only on Windows systems (SANITIZER_WINDOWS)"
+#endif
+
+namespace __sanitizer {
+struct DTLS;
+}
+
+namespace __lsan {
+
+class ThreadContext final : public ThreadContextLsanBase {
+ public:
+  explicit ThreadContext(int tid);
+  void OnStarted(void *arg) override;
+};
+
+void ThreadStart(u32 tid, tid_t os_id,
+ ThreadType thread_type = ThreadType::Regular);
+
+}  // namespace __lsan
+
+#endif  // LSAN_WINDOWS_H
Index: compiler-rt/lib/lsan/lsan_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.cpp
@@ -0,0 +1,106 @@
+//=-- lsan_win.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to POSIX-like systems.
+//
+//===-===//
+
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+#  include "lsan.h"
+#  include "lsan_allocator.h"
+#  include "sanitizer_common/sanitizer_stacktrace.h"
+#  include "sanitizer_common/sanitizer_tls_get_addr.h"
+
+namespace __lsan {
+
+ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
+
+struct OnStartedArgs {
+  uptr stack_begin;
+  uptr stack_end;
+  uptr cache_begin;
+  uptr cache_end;
+  uptr tls_begin;
+  uptr tls_end;
+};
+
+void ThreadContext::OnStarted(void *arg) {
+  auto args = reinterpret_cast(arg);
+  stack_begin_ = args->stack_begin;
+  stack_end_ = args->stack_end;
+  cache_begin_ = args->cache_begin;
+  cache_end_ = args->cache_end;
+}
+
+void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
+  OnStartedArgs args;
+  uptr stack_size = 0;
+  uptr tls_size = 0;
+  GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
+   &args.tls_begin, &tls_size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
+}
+
+bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
+   uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
+   uptr *cache_end, DTLS **dtls) {
+  ThreadContext *context = static_

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-12 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 393750.
clemenswasser added a comment.
Herald added subscribers: luke957, s.egerton, simoncook.

Implement ProcessGlobalRegions
Fix Interceptors for Windows


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h

Index: compiler-rt/lib/lsan/lsan_win.h
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.h
@@ -0,0 +1,41 @@
+//=-- lsan_win.h -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to Windows systems.
+//
+//===-===//
+
+#ifndef LSAN_WINDOWS_H
+#define LSAN_WINDOWS_H
+
+#include "lsan_thread.h"
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if !SANITIZER_WINDOWS
+#  error "lsan_win.h is used only on Windows systems (SANITIZER_WINDOWS)"
+#endif
+
+namespace __sanitizer {
+struct DTLS;
+}
+
+namespace __lsan {
+
+class ThreadContext final : public ThreadContextLsanBase {
+ public:
+  explicit ThreadContext(int tid);
+  void OnStarted(void *arg) override;
+};
+
+void ThreadStart(u32 tid, tid_t os_id,
+ ThreadType thread_type = ThreadType::Regular);
+
+}  // namespace __lsan
+
+#endif  // LSAN_WINDOWS_H
Index: compiler-rt/lib/lsan/lsan_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.cpp
@@ -0,0 +1,106 @@
+//=-- lsan_win.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to POSIX-like systems.
+//
+//===-===//
+
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+#  include "lsan.h"
+#  include "lsan_allocator.h"
+#  include "sanitizer_common/sanitizer_stacktrace.h"
+#  include "sanitizer_common/sanitizer_tls_get_addr.h"
+
+namespace __lsan {
+
+ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
+
+struct OnStartedArgs {
+  uptr stack_begin;
+  uptr stack_end;
+  uptr cache_begin;
+  uptr cache_end;
+  uptr tls_begin;
+  uptr tls_end;
+};
+
+void ThreadContext::OnStarted(void *arg) {
+  auto args = reinterpret_cast(arg);
+  stack_begin_ = args->stack_begin;
+  stack_end_ = args->stack_end;
+  cache_begin_ = args->cache_begin;
+  cache_end_ = args->cache_end;
+}
+
+void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
+  OnStartedArgs args;
+  uptr stack_size = 0;
+  uptr tls_size = 0;
+  GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
+   &args.tls_begin, &tls_size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
+}
+
+bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
+   uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
+   uptr *cache_end, DTLS **dtls) {
+  ThreadContext *context = static_cast(
+  GetThreadRegistryLocked()->FindThreadContextByOsIDLocked(os_id));
+  if (!context)
+return false;
+  *stack_begin = context->stack_begin();
+  *stack_end = context->stack_end();
+  *cache_begin = context->cache_begin();
+  *cache_end = context->cache_end();
+  *dtls = 0;
+  return true;
+}
+
+void InitializeMainThread() {
+  u32 tid = ThreadCreate(kMainTid, true);
+  CHECK_EQ(tid, kMainTid);
+  ThreadStart(tid, GetTid());
+}
+
+static void OnStackUnwind(const SignalContext &sig, const void *,
+  BufferedStackTrace *stack) {
+  stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
+common_flags()->fast_unwind_on_fatal);
+}
+
+void LsanOn

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-17 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser updated this revision to Diff 395219.
clemenswasser added a comment.

Simplify Architecture checks


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

Files:
  clang/lib/Driver/ToolChains/MSVC.cpp
  compiler-rt/cmake/config-ix.cmake
  compiler-rt/lib/lsan/CMakeLists.txt
  compiler-rt/lib/lsan/lsan.h
  compiler-rt/lib/lsan/lsan_allocator.h
  compiler-rt/lib/lsan/lsan_common.cpp
  compiler-rt/lib/lsan/lsan_common.h
  compiler-rt/lib/lsan/lsan_common_win.cpp
  compiler-rt/lib/lsan/lsan_interceptors.cpp
  compiler-rt/lib/lsan/lsan_win.cpp
  compiler-rt/lib/lsan/lsan_win.h

Index: compiler-rt/lib/lsan/lsan_win.h
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.h
@@ -0,0 +1,41 @@
+//=-- lsan_win.h -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to Windows systems.
+//
+//===-===//
+
+#ifndef LSAN_WINDOWS_H
+#define LSAN_WINDOWS_H
+
+#include "lsan_thread.h"
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if !SANITIZER_WINDOWS
+#  error "lsan_win.h is used only on Windows systems (SANITIZER_WINDOWS)"
+#endif
+
+namespace __sanitizer {
+struct DTLS;
+}
+
+namespace __lsan {
+
+class ThreadContext final : public ThreadContextLsanBase {
+ public:
+  explicit ThreadContext(int tid);
+  void OnStarted(void *arg) override;
+};
+
+void ThreadStart(u32 tid, tid_t os_id,
+ ThreadType thread_type = ThreadType::Regular);
+
+}  // namespace __lsan
+
+#endif  // LSAN_WINDOWS_H
Index: compiler-rt/lib/lsan/lsan_win.cpp
===
--- /dev/null
+++ compiler-rt/lib/lsan/lsan_win.cpp
@@ -0,0 +1,106 @@
+//=-- lsan_win.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===-===//
+//
+// This file is a part of LeakSanitizer.
+// Standalone LSan RTL code common to POSIX-like systems.
+//
+//===-===//
+
+#include "sanitizer_common/sanitizer_platform.h"
+
+#if SANITIZER_WINDOWS
+#  include "lsan.h"
+#  include "lsan_allocator.h"
+#  include "sanitizer_common/sanitizer_stacktrace.h"
+#  include "sanitizer_common/sanitizer_tls_get_addr.h"
+
+namespace __lsan {
+
+ThreadContext::ThreadContext(int tid) : ThreadContextLsanBase(tid) {}
+
+struct OnStartedArgs {
+  uptr stack_begin;
+  uptr stack_end;
+  uptr cache_begin;
+  uptr cache_end;
+  uptr tls_begin;
+  uptr tls_end;
+};
+
+void ThreadContext::OnStarted(void *arg) {
+  auto args = reinterpret_cast(arg);
+  stack_begin_ = args->stack_begin;
+  stack_end_ = args->stack_end;
+  cache_begin_ = args->cache_begin;
+  cache_end_ = args->cache_end;
+}
+
+void ThreadStart(u32 tid, tid_t os_id, ThreadType thread_type) {
+  OnStartedArgs args;
+  uptr stack_size = 0;
+  uptr tls_size = 0;
+  GetThreadStackAndTls(tid == kMainTid, &args.stack_begin, &stack_size,
+   &args.tls_begin, &tls_size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(&args.cache_begin, &args.cache_end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, &args);
+}
+
+bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
+   uptr *tls_begin, uptr *tls_end, uptr *cache_begin,
+   uptr *cache_end, DTLS **dtls) {
+  ThreadContext *context = static_cast(
+  GetThreadRegistryLocked()->FindThreadContextByOsIDLocked(os_id));
+  if (!context)
+return false;
+  *stack_begin = context->stack_begin();
+  *stack_end = context->stack_end();
+  *cache_begin = context->cache_begin();
+  *cache_end = context->cache_end();
+  *dtls = 0;
+  return true;
+}
+
+void InitializeMainThread() {
+  u32 tid = ThreadCreate(kMainTid, true);
+  CHECK_EQ(tid, kMainTid);
+  ThreadStart(tid, GetTid());
+}
+
+static void OnStackUnwind(const SignalContext &sig, const void *,
+  BufferedStackTrace *stack) {
+  stack->Unwind(StackTrace::GetNextInstructionPc(sig.pc), sig.bp, sig.context,
+common_flags()->fast_unwind_on_fatal);
+}
+
+void LsanOnDeadlySignal(int signo, void *siginfo, void *context) {
+  HandleDeadlySignal(siginfo, c

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-22 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@vitalybuka could you or someone else help me get the lsan tests running on 
Windows?
For example lets look at 
`compiler-rt\test\lsan\TestCases\leak_check_at_exit.cpp` (and basically all 
other tests, this serves as an easy example)
I imagine that the following defines a variable called `LSAN_BASE` :
`// RUN: LSAN_BASE="use_stacks=0:use_registers=0"`
This doesn't seem to work on Windows.
It gives the following error message:

  [build] $ ":" "RUN: at line 2"
  [build] $ "LSAN_BASE=use_stacks=0:use_registers=0"
  [build] # command stderr:
  [build] 'LSAN_BASE=use_stacks=0:use_registers=0': command not found


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-22 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

The problem is, that `LSAN_BASE` is not always `use_stacks=0:use_registers=0`.
Also there are many test cases, where `LSAN_BASE` gets used multiple times.
Removing it would result in bad repetition.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-23 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@vitalybuka I have now removed all `LSAN_BASE` occurrences.
However the invocations of the compiled test executables don't seem to work 
either.
This doesn't work:
`// RUN: %env_lsan_opts=use_stacks=0:use_registers=0 not %run %t foo 2>&1 | 
FileCheck %s --check-prefix=CHECK-do`
It generates this error:

  [build] $ ":" "RUN: at line 3"
  [build] $ "env" "LSAN_OPTIONS=:detect_leaks=1:use_stacks=0:use_registers=0" 
"not" 
"E:\git\llvm-project\llvm\build\ninja_debug\projects\compiler-rt\test\lsan\X86_64LsanConfig\TestCases\Output\leak_check_at_exit.cpp.tmp.exe"
 "foo"
  [build] note: command had no output on stdout or stderr
  [build] error: command failed with exit status: True

One of the problems seems to be that `%t` doesn't have a `.exe` ending on 
Windows.
I also tried changing `%t` to `%t.exe`, but that doesn't fix the problem and 
produces the same error.
I think the `not` command(?) is the part that fails?

When I run it on my own, it does work:

  PS E:\git\llvm-project> & 
'E:\git\llvm-project\llvm\build\ninja_debug\projects\compiler-rt\test\lsan\X86_64LsanConfig\TestCases\Output\leak_check_at_exit.cpp.tmp.exe'
  Test alloc: 61A0.
  
  =
  ==25768==ERROR: LeakSanitizer: detected memory leaks
  
  Direct leak of 1337 byte(s) in 1 object(s) allocated from:
  #0 0x7ff6e2f745be in __asan_wrap_malloc 
E:\git\llvm-project\compiler-rt\lib\lsan\lsan_interceptors.cpp:75
  #1 0x7ff6e2f2aade in main 
E:\git\llvm-project\compiler-rt\test\lsan\TestCases\leak_check_at_exit.cpp:13:40
  #2 0x7ff6e2f79b5b in __scrt_common_main_seh 
d:\a01\_work\6\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  #3 0x7fffde187033  (C:\WINDOWS\System32\KERNEL32.DLL+0x180017033)
  #4 0x7fffde8a2650  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x180052650)
  
  SUMMARY: LeakSanitizer: 1337 byte(s) leaked in 1 allocation(s).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-24 Thread Clemens Wasser via Phabricator via cfe-commits
clemenswasser added a comment.

@vitalybuka
No `check-asan` **doesn't work** for me. It just hangs forever and does 
absolutely nothing. No output, nothing showing up in Task Manager with high CPU 
usage or anything.
However `check-clang` **does work**. Is there some documentation on running 
`check-asan` on windows.
I also saw that none of the Windows buildbots check/test anything related to 
sanitizers. It seems to me that none of the sanitizer stuff gets ever tested on 
Windows?
Do the test for sanitizers_common/asan/ubsan/etc. even work on Windows?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115103/new/

https://reviews.llvm.org/D115103

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits