[compiler-rt] Adjust 'suppress_fake_stack.cpp' and 'suppress_fake_stack_force_disabled.cpp' to work with MSVC's ASan (PR #172707)

David Justo via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 9 15:12:23 PDT 2026


https://github.com/davidmrdavid updated https://github.com/llvm/llvm-project/pull/172707

>From 5efda39115d8c9febd25e27b724f52be8e73b6e6 Mon Sep 17 00:00:00 2001
From: David Justo <david.justo.1996 at gmail.com>
Date: Wed, 17 Dec 2025 10:04:28 -0800
Subject: [PATCH] adjust 'suppress_fake_stack.cpp' and
 'suppress_fake_stack_force_disabled.cpp' to work with MSVC's ASan

---
 .../asan/TestCases/suppress_fake_stack.cpp    | 30 ++++++++++++++-----
 .../suppress_fake_stack_force_disabled.cpp    | 26 +++++++++++-----
 2 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/compiler-rt/test/asan/TestCases/suppress_fake_stack.cpp b/compiler-rt/test/asan/TestCases/suppress_fake_stack.cpp
index f072c6ad3b034..bd35df99ab705 100644
--- a/compiler-rt/test/asan/TestCases/suppress_fake_stack.cpp
+++ b/compiler-rt/test/asan/TestCases/suppress_fake_stack.cpp
@@ -1,6 +1,14 @@
-// RUN: %clangxx_asan %s -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=1 %run %t
-// RUN: %clangxx_asan %s -mllvm -asan-use-after-return=runtime -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=1 %run %t
-// RUN: %clangxx_asan %s -mllvm -asan-use-after-return=always -o %t && %run %t
+// This file tests the suppression of fake stack frames in ASan.
+
+// We need to separate the MSVC and non-MSVC tests because MSVC because the MSVC enablement of 'stack-use-after-return' detection is different.
+
+// MSVC tests
+// RUN: %if target={{.*-windows-msvc.*}} %{ %clangxx_asan -fsanitize-address-use-after-return %s -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=1 %run %t %}
+
+// Non-MSVC tests
+// RUN: %if !target={{.*-windows-msvc.*}} %{ %clangxx_asan %s -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=1 %run %t %}
+// RUN: %if !target={{.*-windows-msvc.*}} %{ %clangxx_asan %s -mllvm -asan-use-after-return=runtime -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=1 %run %t %}
+// RUN: %if !target={{.*-windows-msvc.*}} %{ %clangxx_asan %s -mllvm -asan-use-after-return=always -o %t && %run %t %}
 
 #include "defines.h"
 
@@ -9,10 +17,18 @@
 
 volatile uintptr_t saved;
 
+// Use FRAME_ADDRESS macro to get the current frame address
+#ifdef _MSC_VER
+// MSVC does not have a 'builtin_frame_address' equivalent.
+// However, for the purposes of this test, its `_AddressOfReturnAddress` built-in suffices
+#  define FRAME_ADDRESS reinterpret_cast<uintptr_t>(_AddressOfReturnAddress())
+#else
+#  define FRAME_ADDRESS reinterpret_cast<uintptr_t>(__builtin_frame_address(0))
+#endif
+
 ATTRIBUTE_NOINLINE bool IsOnRealStack(uintptr_t parent_frame,
                                       uintptr_t var_addr) {
-  uintptr_t this_frame =
-      reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
+  uintptr_t this_frame = FRAME_ADDRESS;
   return this_frame <= var_addr && var_addr <= parent_frame;
 }
 
@@ -22,9 +38,7 @@ ATTRIBUTE_NOINLINE bool IsOnRealStack(uintptr_t parent_frame) {
   return IsOnRealStack(parent_frame, saved);
 }
 
-ATTRIBUTE_NOINLINE bool IsOnRealStack() {
-  return IsOnRealStack(reinterpret_cast<uintptr_t>(__builtin_frame_address(0)));
-}
+ATTRIBUTE_NOINLINE bool IsOnRealStack() { return IsOnRealStack(FRAME_ADDRESS); }
 
 int main(int argc, char *argv[]) {
   assert(!IsOnRealStack());
diff --git a/compiler-rt/test/asan/TestCases/suppress_fake_stack_force_disabled.cpp b/compiler-rt/test/asan/TestCases/suppress_fake_stack_force_disabled.cpp
index c549f08a7f0a8..f92a42381e0a3 100644
--- a/compiler-rt/test/asan/TestCases/suppress_fake_stack_force_disabled.cpp
+++ b/compiler-rt/test/asan/TestCases/suppress_fake_stack_force_disabled.cpp
@@ -1,7 +1,11 @@
 // Check unsuppressing fake stack does not reenable it if disabled via compile or runtime options.
-//
-// RUN: %clangxx_asan %s -mllvm -asan-use-after-return=never -o %t && %run %t
-// RUN: %clangxx_asan %s -mllvm -asan-use-after-return=runtime -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=0 %run %t
+
+// MSVC tests, where disablement is only at runtime.
+// RUN: %if target={{.*-windows-msvc.*}} %{ %clangxx_asan -fsanitize-address-use-after-return %s -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=0 %run %t %}
+
+// Non-MSVC tests
+// RUN: %if !target={{.*-windows-msvc.*}} %{ %clangxx_asan %s -mllvm -asan-use-after-return=never -o %t && %run %t %}
+// RUN: %if !target={{.*-windows-msvc.*}} %{ %clangxx_asan %s -mllvm -asan-use-after-return=runtime -o %t && env ASAN_OPTIONS=detect_stack_use_after_return=0 %run %t %}
 
 #include "defines.h"
 
@@ -10,10 +14,18 @@
 
 volatile uintptr_t saved;
 
+// Use FRAME_ADDRESS macro to get the current frame address
+#ifdef _MSC_VER
+// MSVC does not have a 'builtin_frame_address' equivalent.
+// However, for the purposes of this test, its `_AddressOfReturnAddress` built-in suffices
+#  define FRAME_ADDRESS reinterpret_cast<uintptr_t>(_AddressOfReturnAddress())
+#else
+#  define FRAME_ADDRESS reinterpret_cast<uintptr_t>(__builtin_frame_address(0))
+#endif
+
 ATTRIBUTE_NOINLINE bool IsOnRealStack(uintptr_t parent_frame,
                                       uintptr_t var_addr) {
-  uintptr_t this_frame =
-      reinterpret_cast<uintptr_t>(__builtin_frame_address(0));
+  uintptr_t this_frame = FRAME_ADDRESS;
   return this_frame <= var_addr && var_addr <= parent_frame;
 }
 
@@ -23,9 +35,7 @@ ATTRIBUTE_NOINLINE bool IsOnRealStack(uintptr_t parent_frame) {
   return IsOnRealStack(parent_frame, saved);
 }
 
-ATTRIBUTE_NOINLINE bool IsOnRealStack() {
-  return IsOnRealStack(reinterpret_cast<uintptr_t>(__builtin_frame_address(0)));
-}
+ATTRIBUTE_NOINLINE bool IsOnRealStack() { return IsOnRealStack(FRAME_ADDRESS); }
 
 int main(int argc, char *argv[]) {
   assert(IsOnRealStack());



More information about the llvm-commits mailing list