Skip to content

[asan][win][msvc] override new and delete and seperate TUs #68754

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f7576fc
win_new_delete_override1
Jul 12, 2023
58ebaf4
add GET_STACK_TRACE_EXPLICIT for windows
Jul 12, 2023
84c8d79
add static implib to asan static runtime
Jul 18, 2023
16efb89
char* -> char**
Jul 19, 2023
6b8daf3
Summary: [asan][win][msvc] override new and delete and seperate TUs
Jul 20, 2023
e051421
only do the implib injections on windows, fixes build on linux
Jul 24, 2023
cf0eb93
Use conditionals at configure time to add implib object library
Jul 25, 2023
4e8b2ce
add new and delete asan implementations to interface.inc
Aug 2, 2023
dc990f4
Add dependency between overall asan target and the modified implib
Aug 3, 2023
e438a19
use the same __asan_win_stack_data in every TU
Oct 9, 2023
d5668b2
eliminate duplicate definition of GET_CALLER_PC and GET_CURRENT_FRAME
Oct 10, 2023
4cd5d2c
clang-format
Oct 31, 2023
dfdd9a2
remove usage of special _WIN stack trace functions
Oct 31, 2023
541f6f4
enable exception handling in asan under MSVC
Nov 3, 2023
3a0dbea
clang format
Nov 7, 2023
9fd02b3
remove special stack trace functions from new/delete overrides for wi…
Nov 7, 2023
71a6c3e
make tests deal with new/delete not being on top of the reported stac…
Nov 7, 2023
70459a1
clang-format
barcharcraz Nov 9, 2023
b725812
remove GET_STACK_TRACE_*_WIN and GET_STACK_TRACE_EXPLICIT
barcharcraz Nov 16, 2023
e0dcc8e
Remove the big asci-art graphs from each new/delete TU and instead re…
barcharcraz Nov 21, 2023
08cfda4
Add include guards to asan_win_new_delete_thunk_common
barcharcraz Nov 21, 2023
4e482e0
use "" includes instead of <> in asan_win_new_delete_thunk_common.h
barcharcraz Nov 21, 2023
873e287
Add trailing newlines to mfc new/delete tests
barcharcraz Nov 21, 2023
daae45a
move __asan memory allocation interface functions to asan_win_new_del…
barcharcraz Nov 22, 2023
33d2b2f
clang-format
barcharcraz Nov 22, 2023
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
93 changes: 90 additions & 3 deletions compiler-rt/lib/asan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,40 @@ if (NOT WIN32 AND NOT APPLE)
)
endif()

set(ASAN_CXX_SOURCES
asan_new_delete.cpp
)
if (WIN32)
set(ASAN_CXX_SOURCES asan_win_new_delete.cpp)
else()
set(ASAN_CXX_SOURCES asan_new_delete.cpp)
endif()

if (APPLE)
set(ASAN_SOURCES ASAN_CXX_SOURCES)
endif()

if (WIN32)
set(ASAN_STATIC_IMPLIB_SOURCES
asan_win_delete_array_thunk.cpp
asan_win_delete_array_align_thunk.cpp
asan_win_delete_array_align_nothrow_thunk.cpp
asan_win_delete_array_nothrow_thunk.cpp
asan_win_delete_array_size_thunk.cpp
asan_win_delete_array_size_align_thunk.cpp
asan_win_delete_scalar_thunk.cpp
asan_win_delete_scalar_align_thunk.cpp
asan_win_delete_scalar_align_nothrow_thunk.cpp
asan_win_delete_scalar_nothrow_thunk.cpp
asan_win_delete_scalar_size_thunk.cpp
asan_win_delete_scalar_size_align_thunk.cpp
asan_win_new_array_thunk.cpp
asan_win_new_array_align_thunk.cpp
asan_win_new_array_align_nothrow_thunk.cpp
asan_win_new_array_nothrow_thunk.cpp
asan_win_new_scalar_thunk.cpp
asan_win_new_scalar_align_thunk.cpp
asan_win_new_scalar_align_nothrow_thunk.cpp
asan_win_new_scalar_nothrow_thunk.cpp
)
endif()

set(ASAN_STATIC_SOURCES
asan_rtl_static.cpp
Expand Down Expand Up @@ -83,12 +114,20 @@ SET(ASAN_HEADERS
asan_thread.h
)

if (WIN32)
list(APPEND ASAN_HEADERS
asan_win_new_delete_thunk_common.h
asan_win_thunk_common.h
)
endif()

include_directories(..)

set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
set(ASAN_COMMON_DEFINITIONS ${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION})

append_rtti_flag(OFF ASAN_CFLAGS)
append_list_if(MSVC /EHsc ASAN_CFLAGS)

# Silence warnings in system headers with MSVC.
if(NOT CLANG_CL)
Expand Down Expand Up @@ -139,6 +178,15 @@ add_compiler_rt_object_libraries(RTAsan_dynamic
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS})

if (WIN32)
add_compiler_rt_object_libraries(RTAsan_static_implib
ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES ${ASAN_STATIC_IMPLIB_SOURCES}
ADDITIONAL_HEADERS ${ASAN_HEADERS}
CFLAGS ${ASAN_CFLAGS} ${NO_DEFAULT_LIBS_OPTION}
DEFS ${ASAN_COMMON_DEFINITIONS})
endif()

if(NOT APPLE)
add_compiler_rt_object_libraries(RTAsan
ARCHS ${ASAN_SUPPORTED_ARCH}
Expand Down Expand Up @@ -239,13 +287,24 @@ else()
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)

if(WIN32)
add_compiler_rt_runtime(clang_rt.asan_static
STATIC
ARCHS ${ASAN_SUPPORTED_ARCH}
OBJECT_LIBS RTAsan_static
RTAsan_static_implib
CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)
else()
add_compiler_rt_runtime(clang_rt.asan_static
STATIC
ARCHS ${ASAN_SUPPORTED_ARCH}
OBJECT_LIBS RTAsan_static
CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)
endif()

add_compiler_rt_runtime(clang_rt.asan-preinit
STATIC
Expand All @@ -255,6 +314,13 @@ else()
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)


if (MSVC AND COMPILER_RT_DEBUG)
set(MSVC_DBG_SUFFIX _dbg)
else()
set(MSVC_DBG_SUFFIX )
endif()

foreach(arch ${ASAN_SUPPORTED_ARCH})
if (COMPILER_RT_HAS_VERSION_SCRIPT)
add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
Expand Down Expand Up @@ -311,6 +377,27 @@ else()
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
PARENT_TARGET asan)

if(WIN32)
set_target_properties(clang_rt.asan${MSVC_DBG_SUFFIX}-dynamic-${arch}
PROPERTIES ARCHIVE_OUTPUT_NAME clang_rt.asan_dynamic-${arch}_implib
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")

add_library(clang_rt.asan-${arch}_implib STATIC)
target_link_libraries(clang_rt.asan-${arch}_implib RTAsan_static_implib.${arch})
add_dependencies(asan clang_rt.asan-${arch}_implib)
add_dependencies(clang_rt.asan-${arch}_implib clang_rt.asan${MSVC_DBG_SUFFIX}-dynamic-${arch})
get_compiler_rt_output_dir(${arch} IMPLIB_OUTPUT_DIR)
set_target_properties(clang_rt.asan-${arch}_implib
PROPERTIES ARCHIVE_OUTPUT_NAME clang_rt.asan${MSVC_DBG_SUFFIX}_dynamic-${arch}
ARCHIVE_OUTPUT_DIRECTORY ${IMPLIB_OUTPUT_DIR}
STATIC_LIBRARY_OPTIONS "$<TARGET_LINKER_FILE:clang_rt.asan${MSVC_DBG_SUFFIX}-dynamic-${arch}>")
get_compiler_rt_install_dir(${arch} IMPLIB_INSTALL_DIR)
install(TARGETS clang_rt.asan-${arch}_implib
ARCHIVE DESTINATION ${IMPLIB_INSTALL_DIR}
LIBRARY DESTINATION ${IMPLIB_INSTALL_DIR}
RUNTIME DESTINATION ${IMPLIB_INSTALL_DIR})
endif()

if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
add_sanitizer_rt_symbols(clang_rt.asan_cxx
ARCHS ${arch})
Expand Down
19 changes: 19 additions & 0 deletions compiler-rt/lib/asan/asan_interface.inc
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,22 @@ INTERFACE_FUNCTION(__asan_update_allocation_context)
INTERFACE_WEAK_FUNCTION(__asan_default_options)
INTERFACE_WEAK_FUNCTION(__asan_default_suppressions)
INTERFACE_WEAK_FUNCTION(__asan_on_error)

#if SANITIZER_WINDOWS
INTERFACE_FUNCTION(__asan_delete)
INTERFACE_FUNCTION(__asan_delete_align)
INTERFACE_FUNCTION(__asan_delete_array)
INTERFACE_FUNCTION(__asan_delete_array_align)
INTERFACE_FUNCTION(__asan_delete_array_size)
INTERFACE_FUNCTION(__asan_delete_array_size_align)
INTERFACE_FUNCTION(__asan_delete_size)
INTERFACE_FUNCTION(__asan_delete_size_align)
INTERFACE_FUNCTION(__asan_new)
INTERFACE_FUNCTION(__asan_new_align)
INTERFACE_FUNCTION(__asan_new_align_nothrow)
INTERFACE_FUNCTION(__asan_new_array)
INTERFACE_FUNCTION(__asan_new_array_align)
INTERFACE_FUNCTION(__asan_new_array_align_nothrow)
INTERFACE_FUNCTION(__asan_new_array_nothrow)
INTERFACE_FUNCTION(__asan_new_nothrow)
#endif // SANITIZER_WINDOWS
24 changes: 24 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_align_nothrow_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- asan_win_delete_array_align_nothrow_thunk.cc ----------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr, std::align_val_t align,
std::nothrow_t const&) noexcept {
// nothrow version is identical to throwing version
operator delete[](ptr, align);
}
28 changes: 28 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_align_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- asan_win_delete_array_align_thunk.cc ------------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

__asan_InitDefine<op_delete_array_align> init_delete_array_align;

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr, std::align_val_t align) noexcept {
if (__asan_InitDefine<op_delete_scalar_align>::defined) {
__asan_delete_array_align(ptr, align);
} else {
operator delete(ptr, align);
}
}
23 changes: 23 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_nothrow_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===-- asan_win_delete_array_nothrow_thunk.cc ----------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr, std::nothrow_t const&) noexcept {
// nothrow version is identical to throwing version
operator delete[](ptr);
}
28 changes: 28 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_size_align_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- asan_win_delete_array_size_align_thunk.cc -------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr, size_t size,
std::align_val_t align) noexcept {
if (__asan_InitDefine<op_delete_scalar_align>::defined &&
__asan_InitDefine<op_delete_array_align>::defined) {
__asan_delete_array_size_align(ptr, size, align);
} else {
operator delete[](ptr, align);
}
}
27 changes: 27 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_size_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- asan_win_delete_array_size_thunk.cc -------------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr, size_t size) noexcept {
if (__asan_InitDefine<op_delete_scalar>::defined &&
__asan_InitDefine<op_delete_array>::defined) {
__asan_delete_array_size(ptr, size);
} else {
operator delete[](ptr);
}
}
28 changes: 28 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_array_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- asan_win_delete_array_thunk.cc ------------------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

__asan_InitDefine<op_delete_array> init_delete_array;

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete[](void* ptr) noexcept {
if (__asan_InitDefine<op_delete_scalar>::defined) {
__asan_delete_array(ptr);
} else {
operator delete(ptr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- asan_win_delete_scalar_align_nothrow_thunk.cc ---------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete(void* ptr, std::align_val_t align,
std::nothrow_t const&) noexcept {
// nothrow version is identical to throwing version
operator delete(ptr, align);
}
24 changes: 24 additions & 0 deletions compiler-rt/lib/asan/asan_win_delete_scalar_align_thunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- asan_win_delete_scalar_align_thunk.cc -----------------------------===//
//
// 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 AddressSanitizer, an address sanity checker.
//
// Windows-specific user-provided new/delete operator detection and fallback.
//===----------------------------------------------------------------------===//
#include "asan_win_new_delete_thunk_common.h"

// see diagram in asan_win_new_delete_thunk_common.h for the ordering of the
// new/delete fallbacks.

__asan_InitDefine<op_delete_scalar_align> init_delete_scalar_align;

// Avoid tailcall optimization to preserve stack frame.
#pragma optimize("", off)
void operator delete(void* ptr, std::align_val_t align) noexcept {
__asan_delete_align(ptr, align);
}
Loading