Skip to content

Commit 767d9ca

Browse files
committed
Merge pull request #1434 from apple/asan
[asan] Add basic support for Address Sanitizer function instrumentation
2 parents cc224e1 + 2110235 commit 767d9ca

File tree

18 files changed

+359
-30
lines changed

18 files changed

+359
-30
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ ERROR(error_unsupported_target_os, none,
5252
ERROR(error_unsupported_target_arch, none,
5353
"unsupported target architecture: '%0'", (StringRef))
5454

55+
ERROR(error_unsupported_opt_for_target, none,
56+
"unsupported option '%0' for target '%1'", (StringRef, StringRef))
57+
5558
ERROR(cannot_open_file,none,
5659
"cannot open file '%0' (%1)", (StringRef, StringRef))
5760
ERROR(cannot_open_serialized_file,none,
@@ -67,6 +70,8 @@ ERROR(error_unknown_arg,none,
6770
"unknown argument: '%0'", (StringRef))
6871
ERROR(error_invalid_arg_value,none,
6972
"invalid value '%1' in '%0'", (StringRef, StringRef))
73+
ERROR(error_unsupported_option_argument,none,
74+
"unsupported argument '%1' to option '%0'", (StringRef, StringRef))
7075
ERROR(error_immediate_mode_missing_stdlib,none,
7176
"could not load the swift standard library", ())
7277
ERROR(error_immediate_mode_missing_library,none,

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_AST_IRGENOPTIONS_H
2020

2121
#include "swift/AST/LinkLibrary.h"
22+
#include "swift/Basic/Sanitizers.h"
2223
#include <string>
2324
#include <vector>
2425

@@ -87,6 +88,9 @@ class IRGenOptions {
8788
/// Whether or not to run optimization passes.
8889
unsigned Optimize : 1;
8990

91+
/// Which sanitizer is turned on.
92+
SanitizerKind Sanitize : 2;
93+
9094
/// Whether we should emit debug info.
9195
IRGenDebugInfoKind DebugInfoKind : 2;
9296

@@ -153,7 +157,8 @@ class IRGenOptions {
153157
unsigned UseIncrementalLLVMCodeGen : 1;
154158

155159
IRGenOptions() : OutputKind(IRGenOutputKind::LLVMAssembly), Verify(true),
156-
Optimize(false), DebugInfoKind(IRGenDebugInfoKind::None),
160+
Optimize(false), Sanitize(SanitizerKind::None),
161+
DebugInfoKind(IRGenDebugInfoKind::None),
157162
UseJIT(false), DisableLLVMOptzns(false),
158163
DisableLLVMARCOpts(false), DisableLLVMSLPVectorizer(false),
159164
DisableFPElim(true), Playground(false),

include/swift/Basic/Platform.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ namespace llvm {
2222
}
2323

2424
namespace swift {
25+
26+
enum class DarwinPlatformKind : unsigned {
27+
MacOS,
28+
IPhoneOS,
29+
IPhoneOSSimulator,
30+
TvOS,
31+
TvOSSimulator,
32+
WatchOS,
33+
WatchOSSimulator
34+
};
35+
2536
/// Returns true if the given triple represents iOS running in a simulator.
2637
bool tripleIsiOSSimulator(const llvm::Triple &triple);
2738

@@ -43,6 +54,9 @@ namespace swift {
4354
/// If the triple does not correspond to a known platform, the empty string is
4455
/// returned.
4556
StringRef getPlatformNameForTriple(const llvm::Triple &triple);
57+
58+
/// Returns the platform Kind for Darwin triples.
59+
DarwinPlatformKind getDarwinPlatformKind(const llvm::Triple &triple);
4660
}
4761

4862
#endif // SWIFT_BASIC_PLATFORM_H

include/swift/Basic/Sanitizers.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----- Sanitizers.h - Helpers related to sanitizers --------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===---------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_SANITIZERS_H
14+
#define SWIFT_BASIC_SANITIZERS_H
15+
16+
namespace swift {
17+
18+
enum class SanitizerKind : unsigned {
19+
None = 0,
20+
Address,
21+
};
22+
23+
}
24+
#endif // SWIFT_BASIC_SANITIZERS_H

include/swift/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/AST/IRGenOptions.h"
2121
#include "swift/Basic/LLVM.h"
22+
#include "swift/Basic/Sanitizers.h"
2223
#include "swift/Driver/Types.h"
2324
#include "swift/Driver/Util.h"
2425
#include "llvm/ADT/DenseMap.h"
@@ -112,6 +113,8 @@ class OutputInfo {
112113
/// The path to the SDK against which to build.
113114
/// (If empty, this implies no SDK.)
114115
std::string SDKPath;
116+
117+
enum SanitizerKind SelectedSanitizer;
115118
};
116119

117120
class Driver {

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,8 @@ def enable_testing : Flag<["-"], "enable-testing">,
452452
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
453453
HelpText<"Allows this module's internal API to be accessed for testing">;
454454

455+
def sanitize_EQ : CommaJoined<["-"], "sanitize=">,
456+
Flags<[FrontendOption, NoInteractiveOption]>, MetaVarName<"<check>">,
457+
HelpText<"Turn on runtime checks for erroneous behavior.">;
458+
455459
include "FrontendOptions.td"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----- SanitizerOptions.h - Helpers related to sanitizers --*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===---------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_OPTIONS_SANITIZER_OPTIONS_H
14+
#define SWIFT_OPTIONS_SANITIZER_OPTIONS_H
15+
16+
#include "swift/Basic/Sanitizers.h"
17+
#include "llvm/ADT/Triple.h"
18+
#include "llvm/Option/Arg.h"
19+
20+
namespace swift {
21+
class DiagnosticEngine;
22+
23+
/// \brief Parses a -sanitize= argument's values.
24+
///
25+
/// \param Diag If non null, the argument is used to diagnose invalid values.
26+
/// \return Returns a SanitizerKind.
27+
SanitizerKind parseSanitizerArgValues(const llvm::opt::Arg *A,
28+
const llvm::Triple &Triple,
29+
DiagnosticEngine &Diag);
30+
}
31+
#endif // SWIFT_OPTIONS_SANITIZER_OPTIONS_H

lib/Basic/Platform.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,54 @@ bool swift::tripleIsAnySimulator(const llvm::Triple &triple) {
3939
tripleIsAppleTVSimulator(triple);
4040
}
4141

42-
StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
42+
DarwinPlatformKind swift::getDarwinPlatformKind(const llvm::Triple &triple) {
4343
if (triple.isiOS()) {
4444
if (triple.isTvOS()) {
4545
if (tripleIsAppleTVSimulator(triple))
46-
return "appletvsimulator";
47-
return "appletvos";
46+
return DarwinPlatformKind::TvOSSimulator;
47+
return DarwinPlatformKind::TvOS;
4848
}
4949

5050
if (tripleIsiOSSimulator(triple))
51-
return "iphonesimulator";
52-
return "iphoneos";
51+
return DarwinPlatformKind::IPhoneOSSimulator;
52+
return DarwinPlatformKind::IPhoneOS;
5353
}
5454

5555
if (triple.isWatchOS()) {
5656
if (tripleIsWatchSimulator(triple))
57-
return "watchsimulator";
58-
return "watchos";
57+
return DarwinPlatformKind::WatchOSSimulator;
58+
return DarwinPlatformKind::WatchOS;
5959
}
6060

6161
if (triple.isMacOSX())
62+
return DarwinPlatformKind::MacOS;
63+
64+
llvm_unreachable("Unsupported Darwin platform");
65+
}
66+
67+
static StringRef getPlatformNameForDarwin(const DarwinPlatformKind platform) {
68+
switch (platform) {
69+
case DarwinPlatformKind::MacOS:
6270
return "macosx";
71+
case DarwinPlatformKind::IPhoneOS:
72+
return "iphoneos";
73+
case DarwinPlatformKind::IPhoneOSSimulator:
74+
return "iphonesimulator";
75+
case DarwinPlatformKind::TvOS:
76+
return "appletvos";
77+
case DarwinPlatformKind::TvOSSimulator:
78+
return "appletvsimulator";
79+
case DarwinPlatformKind::WatchOS:
80+
return "watchos";
81+
case DarwinPlatformKind::WatchOSSimulator:
82+
return "watchsimulator";
83+
}
84+
llvm_unreachable("Unsupported Darwin platform");
85+
}
86+
87+
StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
88+
if (triple.isOSDarwin())
89+
return getPlatformNameForDarwin(getDarwinPlatformKind(triple));
6390

6491
if (triple.isOSLinux())
6592
return "linux";

lib/ClangImporter/ClangImporter.cpp

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ void ClangImporter::clearTypeResolver() {
261261
#define SHIMS_INCLUDE_FLAG "-I"
262262
#endif
263263

264+
static StringRef
265+
getMinVersionOptNameForDarwinTriple(const llvm::Triple &triple) {
266+
switch(getDarwinPlatformKind(triple)) {
267+
case DarwinPlatformKind::MacOS:
268+
return "-mmacosx-version-min=";
269+
case DarwinPlatformKind::IPhoneOS:
270+
return "-mios-version-min=";
271+
case DarwinPlatformKind::IPhoneOSSimulator:
272+
return "-mios-simulator-version-min=";
273+
case DarwinPlatformKind::TvOS:
274+
return "-mtvos-version-min=";
275+
case DarwinPlatformKind::TvOSSimulator:
276+
return "-mtvos-simulator-version-min=";
277+
case DarwinPlatformKind::WatchOS:
278+
return "-mwatchos-version-min=";
279+
case DarwinPlatformKind::WatchOSSimulator:
280+
return "-mwatchos-simulator-version-min=";
281+
}
282+
llvm_unreachable("Unsupported Darwin platform");
283+
}
284+
264285
static void
265286
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
266287
ASTContext &ctx,
@@ -342,31 +363,15 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
342363
if (triple.isOSDarwin()) {
343364
std::string minVersionBuf;
344365
llvm::raw_string_ostream minVersionOpt{minVersionBuf};
366+
minVersionOpt << getMinVersionOptNameForDarwinTriple(triple);
367+
345368
unsigned major, minor, micro;
346369
if (triple.isiOS()) {
347-
bool isiOSSimulator = swift::tripleIsiOSSimulator(triple);
348-
if (triple.isTvOS()) {
349-
if (isiOSSimulator)
350-
minVersionOpt << "-mtvos-simulator-version-min=";
351-
else
352-
minVersionOpt << "-mtvos-version-min=";
353-
} else {
354-
if (isiOSSimulator)
355-
minVersionOpt << "-mios-simulator-version-min=";
356-
else
357-
minVersionOpt << "-mios-version-min=";
358-
}
359-
360370
triple.getiOSVersion(major, minor, micro);
361371
} else if(triple.isWatchOS()) {
362-
if (tripleIsWatchSimulator(triple))
363-
minVersionOpt << "-mwatchos-simulator-version-min=";
364-
else
365-
minVersionOpt << "-mwatchos-version-min=";
366-
triple.getOSVersion(major, minor, micro);
372+
triple.getWatchOSVersion(major, minor, micro);
367373
} else {
368374
assert(triple.isMacOSX());
369-
minVersionOpt << "-mmacosx-version-min=";
370375
triple.getMacOSXVersion(major, minor, micro);
371376
}
372377
minVersionOpt << clang::VersionTuple(major, minor, micro);

lib/Driver/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "swift/Driver/OutputFileMap.h"
3333
#include "swift/Driver/ToolChain.h"
3434
#include "swift/Option/Options.h"
35+
#include "swift/Option/SanitizerOptions.h"
3536
#include "swift/Parse/Lexer.h"
3637
#include "swift/Config.h"
3738
#include "llvm/ADT/DenseSet.h"
@@ -1099,6 +1100,10 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
10991100
}
11001101
}
11011102
}
1103+
1104+
OI.SelectedSanitizer = SanitizerKind::None;
1105+
if (const Arg *A = Args.getLastArg(options::OPT_sanitize_EQ))
1106+
OI.SelectedSanitizer = parseSanitizerArgValues(A, TC.getTriple(), Diags);
11021107
}
11031108

11041109
void Driver::buildActions(const ToolChain &TC,

0 commit comments

Comments
 (0)