Skip to content

Commit 0cb9eb4

Browse files
Support instrumenting the STL with ASan (#4313)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 2d18da5 commit 0cb9eb4

File tree

12 files changed

+53
-8
lines changed

12 files changed

+53
-8
lines changed

CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ option(BUILD_TESTING "Enable testing" ON)
4646
set(VCLIBS_SUFFIX "_oss" CACHE STRING "suffix for built DLL names to avoid conflicts with distributed DLLs")
4747

4848
option(STL_USE_ANALYZE "Pass the /analyze flag to MSVC" OFF)
49+
option(STL_ASAN_BUILD "Build the STL with ASan enabled" OFF)
4950

5051
set(VCLIBS_EXPLICIT_MACHINE "")
5152

@@ -117,10 +118,15 @@ set(VCLIBS_DEBUG_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:/Od>")
117118
# See GH-2108 for more info.
118119
set(VCLIBS_RELEASE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:/O2;/Os>")
119120

120-
add_subdirectory(boost-math)
121-
add_subdirectory(stl)
122-
123121
if(BUILD_TESTING)
124122
enable_testing()
125123
add_subdirectory(tests)
126124
endif()
125+
126+
if(STL_ASAN_BUILD)
127+
message(STATUS "Building with ASan enabled")
128+
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:-fsanitize=address;-fno-sanitize-address-vcasan-lib>")
129+
endif()
130+
131+
add_subdirectory(boost-math)
132+
add_subdirectory(stl)

azure-devops/asan-pipeline.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ stages:
2929
targetPlatform: x64
3030
vsDevCmdArch: amd64
3131
buildBenchmarks: false
32+
asanBuild: true
3233
testSelection: ${{ variables.testSelection }}
3334

3435
- stage: Build_And_Test_x86
@@ -43,6 +44,7 @@ stages:
4344
targetPlatform: x86
4445
vsDevCmdArch: x86
4546
buildBenchmarks: false
47+
asanBuild: true
4648
testSelection: ${{ variables.testSelection }}
4749

4850
# no coverage for ARM and ARM64

azure-devops/cmake-configure-build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ parameters:
1010
type: string
1111
- name: buildBenchmarks
1212
type: boolean
13+
- name: asanBuild
14+
type: boolean
15+
default: false
1316
- name: cmakeAdditionalFlags
1417
type: string
1518
default: ''
@@ -43,6 +46,7 @@ steps:
4346
-DCMAKE_BUILD_TYPE=Release ^
4447
-DLIT_FLAGS=${{ join(';', parameters.litFlags) }} ^
4548
-DSTL_USE_ANALYZE=ON ^
49+
-DSTL_ASAN_BUILD=${{ parameters.asanBuild }} ^
4650
-S $(Build.SourcesDirectory) -B "$(buildOutputLocation)"
4751
displayName: 'Configure the STL'
4852
timeoutInMinutes: 2

azure-devops/native-build-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ parameters:
99
- name: buildBenchmarks
1010
type: boolean
1111
default: true
12+
- name: asanBuild
13+
type: boolean
14+
default: false
1215
- name: numShards
1316
type: number
1417
default: 8
@@ -35,6 +38,7 @@ jobs:
3538
targetArch: ${{ parameters.vsDevCmdArch }}
3639
hostArch: ${{ parameters.vsDevCmdArch }}
3740
buildBenchmarks: ${{ parameters.buildBenchmarks }}
41+
asanBuild: ${{ parameters.asanBuild }}
3842
- template: run-tests.yml
3943
parameters:
4044
hostArch: ${{ parameters.vsDevCmdArch }}

stl/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,12 @@ else()
525525
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " Synchronization.lib")
526526
endif()
527527

528+
if(STL_ASAN_BUILD)
529+
set(DLL_ASAN_SOURCES ${ASAN_SOURCES})
530+
else()
531+
set(DLL_ASAN_SOURCES "")
532+
endif()
533+
528534
function(target_stl_compile_options tgt rel_or_dbg)
529535
if(rel_or_dbg STREQUAL "Release")
530536
target_compile_options(${tgt} PRIVATE ${VCLIBS_RELEASE_OPTIONS})
@@ -562,7 +568,7 @@ function(add_stl_dlls D_SUFFIX REL_OR_DBG)
562568
set(gl_flag_Debug "")
563569

564570
# msvcp140.dll
565-
add_library(msvcp${D_SUFFIX}_objects OBJECT ${DLL_SOURCES} ${SOURCES})
571+
add_library(msvcp${D_SUFFIX}_objects OBJECT ${DLL_SOURCES} ${SOURCES} ${DLL_ASAN_SOURCES})
566572
target_compile_definitions(msvcp${D_SUFFIX}_objects PRIVATE CRTDLL2 _DLL)
567573
target_compile_options(msvcp${D_SUFFIX}_objects PRIVATE ${gl_flag_${REL_OR_DBG}} /EHsc)
568574
target_stl_compile_options(msvcp${D_SUFFIX}_objects ${REL_OR_DBG})

tests/libcxx/lit.site.cfg.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ lit_config.library_dirs = getattr(lit_config, 'library_dirs', dict())
2525
lit_config.test_subdirs = getattr(lit_config, 'test_subdirs', dict())
2626

2727
lit_config.expected_results[config.name] = stl.test.file_parsing.parse_result_file('@LIBCXX_EXPECTED_RESULTS@')
28-
lit_config.include_dirs[config.name] = ['@STL_TESTED_HEADERS_DIR@', '@LIBCXX_SOURCE_DIR@/test/support']
28+
# TRANSITION, VSO-1913897: '@STL_SOURCE_DIR@/tests/std/include' is a workaround
29+
lit_config.include_dirs[config.name] = ['@STL_TESTED_HEADERS_DIR@', '@LIBCXX_SOURCE_DIR@/test/support', '@STL_SOURCE_DIR@/tests/std/include']
2930
lit_config.library_dirs[config.name] = ['@STL_LIBRARY_OUTPUT_DIRECTORY@', '@TOOLSET_LIB@']
3031
lit_config.test_subdirs[config.name] = ['@LIBCXX_SOURCE_DIR@/test/std']
3132

tests/libcxx/usual_matrix.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
RUNALL_INCLUDE ..\universal_prefix.lst
55
RUNALL_CROSSLIST
6-
* PM_CL="/EHsc /MTd /std:c++latest /permissive- /utf-8 /FImsvc_stdlib_force_include.h /wd4643 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER"
6+
* PM_CL="/EHsc /MTd /std:c++latest /permissive- /utf-8 /FImsvc_stdlib_force_include.h /FIvso1913897.hpp /wd4643 /D_STL_CALL_ABORT_INSTEAD_OF_INVALID_PARAMETER"
77
RUNALL_CROSSLIST
88
PM_CL="/analyze:autolog- /Zc:preprocessor /wd6262"
99
ASAN PM_CL="-fsanitize=address /Zi" PM_LINK="/debug"

tests/std/include/force_include.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <stdio.h>
1515
#include <stdlib.h>
1616

17+
#include "vso1913897.hpp"
18+
1719
struct TestEnvironmentPreparer {
1820
TestEnvironmentPreparer() noexcept {
1921
// avoid assertion dialog boxes; see GH-781

tests/std/include/vso1913897.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#pragma once
5+
6+
#ifndef _M_CEE
7+
8+
// TRANSITION, dynamically initialize a thread_local to workaround VSO-1913897
9+
inline int __stl_asan_init_function() {
10+
static volatile int __stl_asan_init_volatile = 42;
11+
return __stl_asan_init_volatile;
12+
}
13+
static thread_local int __stl_asan_init_variable = __stl_asan_init_function();
14+
15+
#endif // _M_CEE

tests/std/tests/GH_002030_asan_annotate_string/env.lst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# This test matrix is the usual test matrix, with all currently unsupported options removed, crossed with the ASan flags.
55

6-
# TRANSITION, google/sanitizers#328 - clang-cl does not currently support targeting /MDd or /MTd.
6+
# TRANSITION, google/sanitizers#328: clang-cl does not support /MDd or /MTd with ASan
77
RUNALL_INCLUDE ..\prefix.lst
88
RUNALL_CROSSLIST
99
PM_CL="/Zi /wd4611 /w14640 /Zc:threadSafeInit-" PM_LINK="/debug"

0 commit comments

Comments
 (0)