10b57cec5SDimitry Andric //===- Sanitizers.cpp - C Language Family Language Options ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file defines the classes from Sanitizers.h
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "clang/Basic/Sanitizers.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Hashing.h"
15fe6060f1SDimitry Andric #include "llvm/ADT/SmallVector.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
17fe6060f1SDimitry Andric #include "llvm/Support/MathExtras.h"
180b57cec5SDimitry Andric
190b57cec5SDimitry Andric using namespace clang;
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric // Once LLVM switches to C++17, the constexpr variables can be inline and we
220b57cec5SDimitry Andric // won't need this.
230b57cec5SDimitry Andric #define SANITIZER(NAME, ID) constexpr SanitizerMask SanitizerKind::ID;
240b57cec5SDimitry Andric #define SANITIZER_GROUP(NAME, ID, ALIAS) \
250b57cec5SDimitry Andric constexpr SanitizerMask SanitizerKind::ID; \
260b57cec5SDimitry Andric constexpr SanitizerMask SanitizerKind::ID##Group;
270b57cec5SDimitry Andric #include "clang/Basic/Sanitizers.def"
280b57cec5SDimitry Andric
parseSanitizerValue(StringRef Value,bool AllowGroups)290b57cec5SDimitry Andric SanitizerMask clang::parseSanitizerValue(StringRef Value, bool AllowGroups) {
300b57cec5SDimitry Andric SanitizerMask ParsedKind = llvm::StringSwitch<SanitizerMask>(Value)
310b57cec5SDimitry Andric #define SANITIZER(NAME, ID) .Case(NAME, SanitizerKind::ID)
320b57cec5SDimitry Andric #define SANITIZER_GROUP(NAME, ID, ALIAS) \
330b57cec5SDimitry Andric .Case(NAME, AllowGroups ? SanitizerKind::ID##Group : SanitizerMask())
340b57cec5SDimitry Andric #include "clang/Basic/Sanitizers.def"
350b57cec5SDimitry Andric .Default(SanitizerMask());
360b57cec5SDimitry Andric return ParsedKind;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
serializeSanitizerSet(SanitizerSet Set,SmallVectorImpl<StringRef> & Values)39fe6060f1SDimitry Andric void clang::serializeSanitizerSet(SanitizerSet Set,
40fe6060f1SDimitry Andric SmallVectorImpl<StringRef> &Values) {
41fe6060f1SDimitry Andric #define SANITIZER(NAME, ID) \
42fe6060f1SDimitry Andric if (Set.has(SanitizerKind::ID)) \
43fe6060f1SDimitry Andric Values.push_back(NAME);
44fe6060f1SDimitry Andric #include "clang/Basic/Sanitizers.def"
45fe6060f1SDimitry Andric }
46fe6060f1SDimitry Andric
expandSanitizerGroups(SanitizerMask Kinds)470b57cec5SDimitry Andric SanitizerMask clang::expandSanitizerGroups(SanitizerMask Kinds) {
480b57cec5SDimitry Andric #define SANITIZER(NAME, ID)
490b57cec5SDimitry Andric #define SANITIZER_GROUP(NAME, ID, ALIAS) \
500b57cec5SDimitry Andric if (Kinds & SanitizerKind::ID##Group) \
510b57cec5SDimitry Andric Kinds |= SanitizerKind::ID;
520b57cec5SDimitry Andric #include "clang/Basic/Sanitizers.def"
530b57cec5SDimitry Andric return Kinds;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
hash_value() const560b57cec5SDimitry Andric llvm::hash_code SanitizerMask::hash_value() const {
570b57cec5SDimitry Andric return llvm::hash_combine_range(&maskLoToHigh[0], &maskLoToHigh[kNumElem]);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric namespace clang {
countPopulation() const61fe6060f1SDimitry Andric unsigned SanitizerMask::countPopulation() const {
62fe6060f1SDimitry Andric unsigned total = 0;
63fe6060f1SDimitry Andric for (const auto &Val : maskLoToHigh)
64*bdd1243dSDimitry Andric total += llvm::popcount(Val);
65fe6060f1SDimitry Andric return total;
66fe6060f1SDimitry Andric }
67fe6060f1SDimitry Andric
hash_value(const clang::SanitizerMask & Arg)680b57cec5SDimitry Andric llvm::hash_code hash_value(const clang::SanitizerMask &Arg) {
690b57cec5SDimitry Andric return Arg.hash_value();
700b57cec5SDimitry Andric }
71fe6060f1SDimitry Andric
AsanDtorKindToString(llvm::AsanDtorKind kind)72fe6060f1SDimitry Andric StringRef AsanDtorKindToString(llvm::AsanDtorKind kind) {
73fe6060f1SDimitry Andric switch (kind) {
74fe6060f1SDimitry Andric case llvm::AsanDtorKind::None:
75fe6060f1SDimitry Andric return "none";
76fe6060f1SDimitry Andric case llvm::AsanDtorKind::Global:
77fe6060f1SDimitry Andric return "global";
78fe6060f1SDimitry Andric case llvm::AsanDtorKind::Invalid:
79fe6060f1SDimitry Andric return "invalid";
80fe6060f1SDimitry Andric }
81fe6060f1SDimitry Andric return "invalid";
82fe6060f1SDimitry Andric }
83fe6060f1SDimitry Andric
AsanDtorKindFromString(StringRef kindStr)84fe6060f1SDimitry Andric llvm::AsanDtorKind AsanDtorKindFromString(StringRef kindStr) {
85fe6060f1SDimitry Andric return llvm::StringSwitch<llvm::AsanDtorKind>(kindStr)
86fe6060f1SDimitry Andric .Case("none", llvm::AsanDtorKind::None)
87fe6060f1SDimitry Andric .Case("global", llvm::AsanDtorKind::Global)
88fe6060f1SDimitry Andric .Default(llvm::AsanDtorKind::Invalid);
89fe6060f1SDimitry Andric }
90fe6060f1SDimitry Andric
AsanDetectStackUseAfterReturnModeToString(llvm::AsanDetectStackUseAfterReturnMode mode)91fe6060f1SDimitry Andric StringRef AsanDetectStackUseAfterReturnModeToString(
92fe6060f1SDimitry Andric llvm::AsanDetectStackUseAfterReturnMode mode) {
93fe6060f1SDimitry Andric switch (mode) {
94fe6060f1SDimitry Andric case llvm::AsanDetectStackUseAfterReturnMode::Always:
95fe6060f1SDimitry Andric return "always";
96fe6060f1SDimitry Andric case llvm::AsanDetectStackUseAfterReturnMode::Runtime:
97fe6060f1SDimitry Andric return "runtime";
98fe6060f1SDimitry Andric case llvm::AsanDetectStackUseAfterReturnMode::Never:
99fe6060f1SDimitry Andric return "never";
100fe6060f1SDimitry Andric case llvm::AsanDetectStackUseAfterReturnMode::Invalid:
101fe6060f1SDimitry Andric return "invalid";
102fe6060f1SDimitry Andric }
103fe6060f1SDimitry Andric return "invalid";
104fe6060f1SDimitry Andric }
105fe6060f1SDimitry Andric
106fe6060f1SDimitry Andric llvm::AsanDetectStackUseAfterReturnMode
AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr)107fe6060f1SDimitry Andric AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr) {
108fe6060f1SDimitry Andric return llvm::StringSwitch<llvm::AsanDetectStackUseAfterReturnMode>(modeStr)
109fe6060f1SDimitry Andric .Case("always", llvm::AsanDetectStackUseAfterReturnMode::Always)
110fe6060f1SDimitry Andric .Case("runtime", llvm::AsanDetectStackUseAfterReturnMode::Runtime)
111fe6060f1SDimitry Andric .Case("never", llvm::AsanDetectStackUseAfterReturnMode::Never)
112fe6060f1SDimitry Andric .Default(llvm::AsanDetectStackUseAfterReturnMode::Invalid);
113fe6060f1SDimitry Andric }
114fe6060f1SDimitry Andric
1150b57cec5SDimitry Andric } // namespace clang
116