Skip to content

[compiler-rt][windows] Test fixups for MSVC. #109887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler-rt/test/asan/TestCases/Windows/coverage-basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// RUN: %env_asan_opts=coverage=1 %run ./test.exe
//
// RUN: %sancov print *.sancov | FileCheck %s
// XFAIL: msvc

#include <stdio.h>

Expand Down
7 changes: 4 additions & 3 deletions compiler-rt/test/asan/TestCases/Windows/crash_read_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=READ
// RUN: not %run %t write 2>&1 | FileCheck %s --check-prefix=WRITE

#include <windows.h>
#include "../defines.h"
#include <stdio.h>
#include <windows.h>

static volatile int sink;
__attribute__((noinline)) void Read(int *ptr) { sink = *ptr; }
__attribute__((noinline)) void Write(int *ptr) { *ptr = 0; }
ATTRIBUTE_NOINLINE void Read(int *ptr) { sink = *ptr; }
ATTRIBUTE_NOINLINE void Write(int *ptr) { *ptr = 0; }
int main(int argc, char **argv) {
// Writes to shadow are detected as reads from shadow gap (because of how the
// shadow mapping works). This is kinda hard to fix. Test a random address in
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/test/asan/TestCases/Windows/dll_host.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// UNSUPPORTED: target={{.*-windows-gnu}}
// XFAIL: msvc

// This is a host program for DLL tests.
//
Expand Down
18 changes: 9 additions & 9 deletions compiler-rt/test/asan/TestCases/Windows/dll_intercept_memcpy.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: %clang_cl_asan %Od %p/dll_host.cpp %Fe%t
// RUN: %clang_cl_asan -Wno-fortify-source %LD %Od %s %Fe%t.dll
// RUN: %clang_cl_asan %if !MSVC %{ -Wno-fortify-source %} %LD %Od %s %Fe%t.dll
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s

// Test that it works correctly even with ICF enabled.
// RUN: %clang_cl_asan -Wno-fortify-source %LD %Od %s %Fe%t.dll \
// RUN: %clang_cl_asan %if !MSVC %{ -Wno-fortify-source %} %LD %Od %s %Fe%t.dll \
// RUN: %if target={{.*-windows-gnu}} %{ -Wl,--gc-sections,--icf=all %} \
// RUN: %else %{ -link /OPT:REF /OPT:ICF %}
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s
Expand All @@ -23,12 +23,12 @@ int test_function() {
// CHECK: Initial test OK

memcpy(buff2, buff1, 6);
// CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 6 at [[ADDR]] thread T0
// CHECK-NEXT: __asan_{{.*}}memcpy
// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cpp:[[@LINE-4]]
// CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame
// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cpp
// CHECK: 'buff2'{{.*}} <== Memory access at offset {{.*}} overflows this variable
// CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 6 at [[ADDR]] thread T0
// CHECK-NEXT: __asan_{{.*}}{{(memcpy|memmove)}}
// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cpp:[[@LINE-4]]
// CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame
// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cpp
// CHECK: 'buff2'{{.*}} <== Memory access at offset {{.*}} overflows this variable
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// RUN: %clang_cl_asan %Od %p/dll_host.cpp %Fe%t
// RUN: %clang_cl_asan -Wno-fortify-source %LD %Od %s %Fe%t.dll
// RUN: %clang_cl_asan %Od %Oy- %p/dll_host.cpp %Fe%t
// RUN: %clang_cl_asan %if !MSVC %{ -Wno-fortify-source %} %Oy- %LD %Od %s %Fe%t.dll
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s

// Test that it works correctly even with ICF enabled.
// RUN: %clang_cl_asan -Wno-fortify-source %LD %Od %s %Fe%t.dll \
// RUN: %clang_cl_asan %if !MSVC %{ -Wno-fortify-source %} %Oy- %LD %Od %s %Fe%t.dll \
// RUN: %if target={{.*-windows-gnu}} %{ -Wl,--gc-sections,--icf=all %} \
// RUN: %else %{ -link /OPT:REF /OPT:ICF %}
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/test/asan/TestCases/Windows/dll_null_deref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// RUN: %clang_cl_asan %LD %Od %s %Fe%t.dll
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s

__attribute__((noinline))
#include "../defines.h"

ATTRIBUTE_NOINLINE
static void NullDeref(int *ptr) {
// CHECK: ERROR: AddressSanitizer: access-violation on unknown address
// CHECK: {{0x0*000.. .*pc 0x.*}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
// RUN: %clang_cl_asan %LD %Od %s %Fe%t.dll
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s

#include "../defines.h"

struct C {
int x;
~C() {}
};

int __attribute__((noinline, optnone)) hide(int x) { return x; }
int ATTRIBUTE_NOINLINE
#if defined(__clang__) || !defined(_MSC_VER)
__attribute__((optnone))
#endif
hide(int x) {
return x;
}

extern "C" __declspec(dllexport)
int test_function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// RUN: %clang_cl_asan %LD %Od %s %Fe%t.dll -fsanitize-address-use-after-return=always
// RUN: not %run %t %t.dll 2>&1 | FileCheck %s

// UNSUPPORTED: msvc

#include <malloc.h>

char *x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(void) {
// CHECK: AddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: READ of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*global_const_string_oob.cpp:}}[[@LINE-5]]
// CHECK: [[ADDR]] is located 5 bytes after global variable [[STR:.*]] defined in {{'.*global_const_string_oob.cpp' .*}} of size 11
// CHECK: [[ADDR]] is located 5 bytes after global variable [[STR:.*]] defined in {{'.*global_const_string_oob.cpp.*}} of size 11
// CHECK: [[STR]] is ascii string 'foobarspam'
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %clang_cl_asan %Od %s %Fe%t
// RUN: %env_asan_opts=handle_sigill=1 not %run %t 2>&1 | FileCheck %s
// msvc doesn't have a __builtin_trap equivalent
// XFAIL: msvc

// Test the error output from an illegal instruction.

Expand Down
10 changes: 9 additions & 1 deletion compiler-rt/test/asan/TestCases/Windows/issue64990.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Repro for the issue #64990: Asan with Windows EH generates __asan_xxx runtime calls without required funclet tokens
// RUN: %clang_cl_asan %Od %s -EHsc %Fe%t
// RUN: %clang_cl_asan %Od %if MSVC %{ /Oi %} %s -EHsc %Fe%t
// RUN: not %run %t 2>&1 | FileCheck %s

// UNSUPPORTED: target={{.*-windows-gnu}}

#if defined(_MSC_VER) && !defined(__clang__)
# include <string.h>
#endif

char buff1[6] = "hello";
char buff2[6] = "hello";

Expand All @@ -12,7 +16,11 @@ int main(int argc, char **argv) {
throw 1;
} catch (...) {
// Make asan generate call to __asan_memcpy inside the EH pad.
#if defined(_MSC_VER) && !defined(__clang__)
memcpy(buff1, buff2 + 3, 6);
#else
__builtin_memcpy(buff1, buff2 + 3, 6);
#endif
}
return 0;
}
Expand Down
12 changes: 6 additions & 6 deletions compiler-rt/test/asan/TestCases/Windows/msvc/seh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// different EH personality depending on the -GS setting, so test both -GS+ and
// -GS-.
//
// RUN: cl -c %s -Fo%t.obj -DCOMPILE_SEH
// RUN: %clangxx_asan -o %t.exe %s %t.obj
// RUN: cl /EHa %MD -c %s -Fo%t.obj -DCOMPILE_SEH
// RUN: %clangxx_asan -o %t.exe %s %t.obj -DCOMPILE_MAIN
// RUN: %run %t.exe
//
// RUN: cl -GS- -c %s -Fo%t.obj -DCOMPILE_SEH
// RUN: %clangxx_asan -o %t.exe %s %t.obj
// RUN: cl /EHa %MD -GS- -c %s -Fo%t.obj -DCOMPILE_SEH
// RUN: %clangxx_asan -o %t.exe %s %t.obj -DCOMPILE_MAIN
// RUN: %run %t.exe
//
// RUN: %clang_cl_asan %s -DCOMPILE_SEH -Fe%t.exe
// RUN: %clang_cl_asan /EHa %MD %s -DCOMPILE_SEH -Fe%t.exe -DCOMPILE_MAIN
// RUN: %run %t.exe

#include <windows.h>
Expand Down Expand Up @@ -42,7 +42,7 @@ void ThrowAndCatch() {
}
#endif

#if defined(__clang__)
#if defined(COMPILE_MAIN)
int main() {
char x[32];
fprintf(stderr, "Before: %p poisoned: %d\n", &x,
Expand Down
15 changes: 0 additions & 15 deletions compiler-rt/test/asan/TestCases/Windows/null_deref.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// RUN: %clang_cl_asan %Od -DEXE %s %t1.lib %t2.lib %Fe%t
// RUN: not %run %t 2>&1 | FileCheck %s

#include "../defines.h"
#include <malloc.h>
#include <string.h>

Expand All @@ -28,7 +29,8 @@ __declspec(dllexport) void foo1() {}
}
#elif defined(DLL2)
extern "C" {
__attribute__((noinline)) static void NullDeref(int *ptr) {
ATTRIBUTE_NOINLINE
static void NullDeref(int *ptr) {
// CHECK: ERROR: AddressSanitizer: access-violation on unknown address
// CHECK: {{0x0*000.. .*pc 0x.*}}
ptr[10]++; // BOOM
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// RUN: %clang_cl_asan %Od %s %Fe%t
// RUN: not %run %t 2>&1 | FileCheck %s

#include "../defines.h"

struct C {
int x;
~C() {}
};

int __attribute__((noinline, optnone)) hide(int x) { return x; }

#if defined(_MSC_VER) && !defined(__clang__)
# pragma optimize("", off)
#else
__attribute__((optnone))
#endif
int ATTRIBUTE_NOINLINE hide(int x) { return x; }
#if defined(_MSC_VER) && !defined(__clang__)
# pragma optimize("", on)
#endif
int main() {
C *buffer = new C[42];
buffer[hide(-(1 + (int)sizeof(void*) / 4))].x = 42;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// FIXME: On MinGW frame #0 does not include the line number?
// XFAIL: target={{.*-windows-gnu}}
// XFAIL: msvc

// Test the error output from misaligned SSE2 memory access. This is a READ
// memory access. Windows appears to always provide an address of -1 for these
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// RUN: %clang_cl_asan %Od %s %Fe%t -fsanitize-address-use-after-return=always
// RUN: not %run %t 2>&1 | FileCheck %s

// XFAIL: msvc
char *x;

void foo() {
Expand Down
5 changes: 3 additions & 2 deletions compiler-rt/test/asan/TestCases/Windows/unsymbolized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
// RUN: not %run %t.exe 2>&1 | FileCheck %s
// REQUIRES: lld-available

#include <stdlib.h>
#include "../defines.h"
#include <stdio.h>
int __attribute__((noinline)) do_uaf(void);
#include <stdlib.h>
int ATTRIBUTE_NOINLINE do_uaf(void);
int main() {
int r = do_uaf();
printf("r: %d\n", r);
Expand Down
6 changes: 4 additions & 2 deletions compiler-rt/test/asan/TestCases/alloca_big_alignment.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
// RUN: not %run %t 2>&1 | FileCheck %s
//
// XFAIL: msvc

#include "defines.h"
#include <assert.h>
#include <stdint.h>

__attribute__((noinline)) void foo(int index, int len) {
volatile char str[len] __attribute__((aligned(128)));
ATTRIBUTE_NOINLINE void foo(int index, int len) {
volatile char str[len] ATTRIBUTE_ALIGNED(128);
assert(!(reinterpret_cast<uintptr_t>(str) & 127L));
str[index] = '1'; // BOOM
// CHECK: ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/test/asan/TestCases/alloca_constant_size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _MSC_VER
# include <malloc.h>
#endif

// MSVC provides _alloca instead of alloca.
#if defined(_MSC_VER) && !defined(alloca)
Expand Down
12 changes: 10 additions & 2 deletions compiler-rt/test/asan/TestCases/alloca_detect_custom_size_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
// RUN: not %run %t 2>&1 | FileCheck %s
//

#include "defines.h"
#include <assert.h>
#include <stdint.h>
#if defined(_MSC_VER) && !defined(__clang__)
# include <malloc.h>
#endif

struct A {
char a[3];
int b[3];
};

__attribute__((noinline)) void foo(int index, int len) {
volatile struct A str[len] __attribute__((aligned(32)));
ATTRIBUTE_NOINLINE void foo(int index, int len) {
#if !defined(_MSC_VER) || defined(__clang__)
volatile struct A str[len] ATTRIBUTE_ALIGNED(32);
#else
volatile struct A *str = (volatile struct A *)_alloca(len * sizeof(struct A));
#endif
assert(!(reinterpret_cast<uintptr_t>(str) & 31L));
str[index].a[0] = '1'; // BOOM
// CHECK: ERROR: AddressSanitizer: dynamic-stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// RUN: %clangxx_asan -O3 -mllvm -asan-instrument-dynamic-allocas %s -o %t
// RUN: %run %t 2>&1
//
// MSVC does not support asan-instrament-dynamic-allocas yet
// UNSUPPORTED: msvc

#include "sanitizer/asan_interface.h"
#include <assert.h>
Expand Down
8 changes: 6 additions & 2 deletions compiler-rt/test/asan/TestCases/alloca_loop_unpoisoning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

// This testcase checks that allocas and VLAs inside loop are correctly unpoisoned.

// MSVC doesn't support VLAs in the first place.
// UNSUPPORTED: msvc

#include "defines.h"
#include "sanitizer/asan_interface.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include "sanitizer/asan_interface.h"

// MSVC provides _alloca instead of alloca.
#if defined(_MSC_VER) && !defined(alloca)
Expand All @@ -21,7 +25,7 @@

void *top, *bot;

__attribute__((noinline)) void foo(int len) {
ATTRIBUTE_NOINLINE void foo(int len) {
char x;
top = &x;
volatile char array[len];
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/test/asan/TestCases/alloca_overflow_partial.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
// RUN: not %run %t 2>&1 | FileCheck %s
//
// MSVC doesn't support VLAs
// UNSUPPORTED: msvc

#include <assert.h>
#include <stdint.h>
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/test/asan/TestCases/alloca_overflow_right.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
// RUN: not %run %t 2>&1 | FileCheck %s
//
// MSVC doesn't support VLAs
// UNSUPPORTED: msvc

#include <assert.h>
#include <stdint.h>
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/test/asan/TestCases/alloca_safe_access.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %clangxx_asan -O0 -mllvm -asan-instrument-dynamic-allocas %s -o %t
// RUN: %run %t 2>&1
//
// MSVC doesn't support VLAs
// UNSUPPORTED: msvc

#include <assert.h>
#include <stdint.h>
Expand Down
Loading