[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

2023-02-20 Thread Alexandre Ganea via Phabricator via cfe-commits
aganea added a comment.

@clemenswasser We hit this once in a while, when Microsoft update their 
libraries. But I'm surprised about the CC at the beginning. How can this issue 
be reproduced? Can you show a disassembly dump from the VS debugger, around the 
address of the function that fails?


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-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-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 Alexandre Ganea via Phabricator via cfe-commits
aganea added a comment.

In D115103#4097460 , @clemenswasser 
wrote:

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

@clemenswasser The bug is there for years, I'm surprised it hasn't been fixed 
yet. MSVC doesn't support different types with bitfields. You should do:

  struct ChunkMetadata {
uint64_t allocated : 8;  // Must be first.
uint64_t tag : 2;
uint64_t requested_size : 54;
uint32_t stack_trace_id;
  };


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.

@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

2022-12-07 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

You probably can try to finish pure lsan, and then get back to lsan+asan.
So if you continue to work please send smaller and ready pieces for review?


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, , 0, _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, , 0, _thread_id);
+  //CHECK(trace_thread);
+  //
+  //WaitForSingleObject(trace_thread, INFINITE);
+  //CloseHandle(trace_thread);
+  RunThread();
 }
 
 }  // 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_INTERCEPT_ALIGNED_ALLOC 

[PATCH] D115103: Leak Sanitizer port to Windows

2022-12-07 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka requested changes to this revision.
vitalybuka added a comment.
This revision now requires changes to proceed.
Herald added a subscriber: Enna1.

Is this still relevant?


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-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, , 0, _thread_id);
+  REAL(CreateThread)(nullptr, 0, RunThread, , 0, _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

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-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: 

[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 

[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-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 = 

[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

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-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-06 Thread Alexandre Ganea via Phabricator via cfe-commits
aganea added a comment.

It does work indeed. I am running under a "x64 Native Tools Command Prompt for 
VS 2019" prompt with MSVC 16.11.8.

  D:\git\llvm-project>mkdir release && cd release
  D:\git\llvm-project\release>cmake ../llvm -G Ninja 
-DLLVM_ENABLE_PROJECTS="clang;lld;llvm;compiler-rt" -DCMAKE_BUILD_TYPE=Release 
-DLLVM_ENABLE_PDB=ON -DLLVM_ENABLE_ASSERTIONS=ON
  ...
  D:\git\llvm-project\release>ninja check-asan
  ninja: Entering directory `release'
  [0/1] Running the AddressSanitizer tests
  -- Testing: 669 tests, 16 workers --
  Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
  
  Testing Time: 86.33s
Unsupported  : 229
Passed   : 426
Expectedly Failed:  14

However you might need some Unix tools that don't come out-of-the-box on 
Windows.
You could first install the scoop  package manager and then 
install any tool that you need:

  D:\git\llvm-project>powershell
  ...
  D:\git\llvm-project [main ↓9 +10 ~0 -0 !]> iwr -useb get.scoop.sh | iex
  ...
  D:\git\llvm-project [main ↓9 +10 ~0 -0 !]> scoop install coreutils grep make 
sed unxutils sysinternals

And then run `ninja check-asan` again.

If you install `sysinternals` with scoop above, you can run `procexp64` prior 
to `ninja check-asan`. That will give a much finer view on the running 
processes, as compared to the Task Manager. Let me know if I can help further.


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-06 Thread Reid Kleckner via Phabricator via cfe-commits
rnk added a subscriber: aganea.
rnk added a comment.

See a recent change rG7cd109b92c72855937273a6c8ab19016fbe27d33 
 by 
@aganea, who presumably was able to run the ASan tests for it.


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-06 Thread Reid Kleckner via Phabricator via cfe-commits
rnk added a comment.

In D115103#3209468 , @vitalybuka 
wrote:

> In D115103#3209326 , @clemenswasser 
> wrote:
>
>> @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?
>
> This one runs asan on Windows, you can try to replicated using logs. 
> https://lab.llvm.org/buildbot/#/builders/127
> I rarely work on Windows, but i did similar setup some time ago.

I will just confirm that `check-asan` is supposed to work, it worked for me a 
few months ago, and the linked buildbot tests it continuously.

However, WinASan is very sensitive to the OS and SDK version. The failure mode 
you described sounds plausible, something like a crash on startup that results 
in a message box that you can't see or something like that.


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 Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

In D115103#3209326 , @clemenswasser 
wrote:

> @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?

This one runs asan on Windows, you can try to replicated using logs. 
https://lab.llvm.org/buildbot/#/builders/127
I rarely work on Windows, but i did similar setup some time ago.


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


[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-23 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

In D115103#3207867 , @clemenswasser 
wrote:

> @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

does check-asan work for your setup?


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-22 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

In D115103#3207210 , @clemenswasser 
wrote:

> The problem is, that `LSAN_BASE` is not always `use_stacks=0:use_registers=0`.

Off-cause , I am asking to replace with correct value each test.

> Also there are many test cases, where `LSAN_BASE` gets used multiple times.
> Removing it would result in bad repetition.

I don't like LSAN_BASE even without Windows.
Usually when test fail, I have a single command line to reproduce or put into 
debugger for just the step. Here I need to cherry-pick two line: LSAN_BASE=  
and actual step
also in most case it used just twice, so it's not a big loss in 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-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-22 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

In D115103#3207150 , @clemenswasser 
wrote:

> @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

Can you try to remove LSAN_BASE and just hardcode use_stacks=0:use_registers=0 ?
If it works  don't mind if we do so for the rest (in a separate patch)


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.

@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-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, _begin, _size,
+   _begin, _size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(_begin, _end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, );
+}
+
+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 , 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, context, GetCurrentThread(), ,
+ 

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-16 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added inline comments.



Comment at: compiler-rt/lib/lsan/lsan_allocator.h:52-53
 
-#if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
-defined(__arm__) || SANITIZER_RISCV64 || defined(__hexagon__)
+#if defined(__mips64) || defined(__aarch64__) || defined(_M_ARM64) || \
+defined(__i386__) || defined(_M_IX86) || defined(__arm__) ||  \
+defined(_M_ARM) || SANITIZER_RISCV64 || defined(__hexagon__)





Comment at: compiler-rt/lib/lsan/lsan_allocator.h:69
 using PrimaryAllocator = PrimaryAllocatorASVT;
-#elif defined(__x86_64__) || defined(__powerpc64__) || defined(__s390x__)
-# if SANITIZER_FUCHSIA
+#elif defined(__x86_64__) || defined(_M_X64) || defined(__powerpc64__) || \
+defined(__s390x__)





Comment at: compiler-rt/lib/lsan/lsan_common.cpp:248-253
+#  if defined(__x86_64__) || defined(_M_X64)
   // Accept only canonical form user-space addresses.
   return ((p >> 47) == 0);
 #  elif defined(__mips64)
   return ((p >> 40) == 0);
+#  elif defined(__aarch64__) || defined(_M_ARM64)

same


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-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, _begin, _size,
+   _begin, _size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(_begin, _end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, );
+}
+
+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 , 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) {
+  

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-07 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a comment.

In D115103#3176852 , @clemenswasser 
wrote:

> Split this patch up into multiple smaller ones:
>
> - D115186 
> - D115204 
> - D115262 

Please check "Edit Related Revisions" on the right of Phabricator. You can 
nicely chain this patches into "Stack"

In D115103#3176852 , @clemenswasser 
wrote:

> 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

___
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 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, _begin, _size,
+   _begin, _size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(_begin, _end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, );
+}
+
+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(
+  

[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, _begin, _size,
+   _begin, _size);
+  args.stack_end = args.stack_begin + stack_size;
+  args.tls_end = args.tls_begin + tls_size;
+  GetAllocatorCacheRange(_begin, _end);
+  ThreadContextLsanBase::ThreadStart(tid, os_id, thread_type, );
+}
+
+bool GetThreadRangesLocked(tid_t os_id, uptr *stack_begin, uptr *stack_end,
+ 

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-07 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added inline comments.



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

MyDeveloperDay wrote:
> clemenswasser wrote:
> > 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?
> Sorry, is there a reason we can't follow the current style in this patch? 
> sorry did I miss something?  Its not about fighting clang-format its about 
> ensuring we follow the style from the lines around it which are following the 
> clang-format style. I don't need you to clang-format the whole file (although 
> that would be great because this directory has very low clang-format status)
> 
> {F20932130, size=full}
> https://clang.llvm.org/docs/ClangFormattedStatus.html
> 
> All I'm asking is that we don't indent the preprocessor directives to a non 
> LLVM style.
> 
> i.e.
> 
> ```
> #include "lsan_win.h"
> #if !SANITIZER_WINDOWS
> #endif
> ```
> 
> vs
> 
> ```
> #  include "lsan_win.h"
> #  if !SANITIZER_WINDOWS
> #  endif
> ```
You can ignore my comment, whilst I think the lsan .clang-format file is 
incorrect in terms of matching the LLVM style it is correct from the lsan 
subprojects point of view. I see now that you are doing what is in the local 
.clang-format file and its the rest of the file is incorrect. (you would handle 
that separately or not at all)

The fact that the .clang-format file doesn't follow the LLVM style is 
unfortunate, but as I expressed thats upto you all. 


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 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 MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

oh! gosh I'm sorry I didn't realize that `lsan` has this non standard LLVM 
style .clang-format, my apologies!  D100238: [sanitizer] Set 
IndentPPDirectives: AfterHash in .clang-format 


kind of surprised TBH, doesn't feel the indentation happens enough to warrant 
it, but that's up to you its not my backyard.

  $ grep "#if" *.cpp | wc -l
  461
  llvm-project/compiler-rt/lib/sanitizer_common
  $ grep "# if" *.cpp | wc -l
  27


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 MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a subscriber: kuhnel.
MyDeveloperDay added inline comments.



Comment at: compiler-rt/lib/lsan/lsan_common.h:48
+#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA || SANITIZER_WINDOWS
+#  define CAN_SANITIZE_LEAKS 1
 #else

vitalybuka wrote:
> MyDeveloperDay wrote:
> > you probably don't want to change this do you?
> I am fine like this, fighting clang-format is not useful
> however it would be nice if you clang-format relevant block in a separate 
> patch then then rebase this patch on top of it
it might not seem useful to individuals,  but I think its the guidance, and a 
consistent style really help new people to understand what's going on.

https://llvm.org/docs/Contributing.html#how-to-submit-a-patch

I'm a little surprise the "llvm-premerge-checks" didn't highlight this @kuhnel 




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 MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added inline comments.



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

vitalybuka wrote:
> MyDeveloperDay wrote:
> > clang-format?
> @clemenswasser  Can you please a separate tiny patch which clang-format 
> nearby lines
Sorry, is there a reason we can't follow the current style in this patch? sorry 
did I miss something?  Its not about fighting clang-format its about ensuring 
we follow the style from the lines around it which are following the 
clang-format style. I don't need you to clang-format the whole file (although 
that would be great because this directory has very low clang-format status)

{F20932130, size=full}
https://clang.llvm.org/docs/ClangFormattedStatus.html

All I'm asking is that we don't indent the preprocessor directives to a non 
LLVM style.

i.e.

```
#include "lsan_win.h"
#if !SANITIZER_WINDOWS
#endif
```

vs

```
#  include "lsan_win.h"
#  if !SANITIZER_WINDOWS
#  endif
```


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-06 Thread Vitaly Buka via Phabricator via cfe-commits
vitalybuka added a reviewer: rnk.
vitalybuka added a subscriber: rnk.
vitalybuka added a comment.

LGTM in general, but it can be cut into smaller patches.

+ @rnk for Windows suff




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

MyDeveloperDay wrote:
> clang-format?
@clemenswasser  Can you please a separate tiny patch which clang-format nearby 
lines



Comment at: compiler-rt/lib/lsan/lsan_allocator.cpp:30
 #if defined(__i386__) || defined(__arm__)
-static const uptr kMaxAllowedMallocSize = 1UL << 30;
+static const uptr kMaxAllowedMallocSize = static_cast(1) << 30;
 #elif defined(__mips64) || defined(__aarch64__)

This also can go into a separate patch
BTW I would slightly prefer just UL -> ULL 



Comment at: compiler-rt/lib/lsan/lsan_common.cpp:411
 
+#  if !SANITIZER_WINDOWS
 static void ProcessRootRegion(Frontier *frontier,

I guess do needs this as well on windows
The problem is that we have no MemoryMappingLayout implementation for Windows.
I guess it can be done with VirtualQueryEx
for now can you just ifdef only body of the ProcessRootRegion with TODO?



Comment at: compiler-rt/lib/lsan/lsan_common.h:48
+#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA || SANITIZER_WINDOWS
+#  define CAN_SANITIZE_LEAKS 1
 #else

MyDeveloperDay wrote:
> you probably don't want to change this do you?
I am fine like this, fighting clang-format is not useful
however it would be nice if you clang-format relevant block in a separate patch 
then then rebase this patch on top of it



Comment at: compiler-rt/lib/lsan/lsan_common_win.cpp:48
+
+void ProcessGlobalRegions(Frontier *frontier) {}
+void ProcessPlatformSpecificAllocations(Frontier *frontier) {}

how this can work without ProcessGlobalRegions?



Comment at: compiler-rt/lib/lsan/lsan_interceptors.cpp:506
+  INTERCEPT_FUNCTION(malloc);
+  INTERCEPT_FUNCTION(free);
+  INTERCEPT_FUNCTION(calloc);

Can you please just convert unneeded stuff from INTERCEPT_FUNCTION into 
LSAN_MAYBE_INTERCEPT
In a separate patch



Comment at: compiler-rt/lib/sanitizer_common/sanitizer_stoptheworld_win.cpp:4
+// 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

sanitizer_stoptheworld_test.cpp needs to be ported to windows, probably with 
std::
can you please move this work and sanitizer_stoptheworld_win.cpp into separate 
patch/patches


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-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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), _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, _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;
+
+  

[PATCH] D115103: Leak Sanitizer port to Windows

2021-12-06 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

You need to add some reviewers to this revision. check out the CODE_OWNERS.txt 
or what I sometimes do is to `git log` one of the files you are changing and 
see who is a major contributor.


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-06 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added inline comments.



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

clang-format?



Comment at: compiler-rt/lib/lsan/lsan_common.h:48
+#elif SANITIZER_NETBSD || SANITIZER_FUCHSIA || SANITIZER_WINDOWS
+#  define CAN_SANITIZE_LEAKS 1
 #else

you probably don't want to change this do you?


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-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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), _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, _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;
+
+

[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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) /
+ sizeof(uptr));
+  internal_memcpy(buffer->data(), _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, _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;
+
+

[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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), _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, _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 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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), _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, _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(THREAD_ALL_ACCESS, 

[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], _context));
+
+  buffer->resize(RoundUpTo(sizeof(thread_context), sizeof(uptr)) / sizeof(uptr));
+  internal_memcpy(buffer->data(), _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 =