Skip to content

Commit 850a729

Browse files
authored
Merge branch 'main' into alarm_feature
2 parents 81719c8 + de67293 commit 850a729

File tree

92 files changed

+5179
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+5179
-216
lines changed

clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/Stmt.h"
2020
#include "clang/Basic/SourceLocation.h"
2121
#include "llvm/Support/Debug.h"
22+
#include <set>
2223

2324
namespace clang {
2425

@@ -186,6 +187,8 @@ namespace internal {
186187
bool anyConflict(const llvm::SmallVectorImpl<FixItHint> &FixIts,
187188
const SourceManager &SM);
188189
} // namespace internal
190+
191+
std::set<const Expr *> findUnsafePointers(const FunctionDecl *FD);
189192
} // end namespace clang
190193

191194
#endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,8 @@ class WarningGadget : public Gadget {
11631163
virtual void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,
11641164
bool IsRelatedToDecl,
11651165
ASTContext &Ctx) const = 0;
1166+
1167+
virtual SmallVector<const Expr *, 1> getUnsafePtrs() const = 0;
11661168
};
11671169

11681170
/// Fixable gadgets correspond to code patterns that aren't always unsafe but
@@ -1245,6 +1247,10 @@ class IncrementGadget : public WarningGadget {
12451247

12461248
return std::move(Uses);
12471249
}
1250+
1251+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1252+
return {Op->getSubExpr()->IgnoreParenImpCasts()};
1253+
}
12481254
};
12491255

12501256
/// A decrement of a pointer-type value is unsafe as it may run the pointer
@@ -1288,6 +1294,10 @@ class DecrementGadget : public WarningGadget {
12881294

12891295
return {};
12901296
}
1297+
1298+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1299+
return {Op->getSubExpr()->IgnoreParenImpCasts()};
1300+
}
12911301
};
12921302

12931303
/// Array subscript expressions on raw pointers as if they're arrays. Unsafe as
@@ -1337,6 +1347,10 @@ class ArraySubscriptGadget : public WarningGadget {
13371347

13381348
return {};
13391349
}
1350+
1351+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1352+
return {ASE->getBase()->IgnoreParenImpCasts()};
1353+
}
13401354
};
13411355

13421356
/// A pointer arithmetic expression of one of the forms:
@@ -1400,6 +1414,11 @@ class PointerArithmeticGadget : public WarningGadget {
14001414

14011415
return {};
14021416
}
1417+
1418+
SmallVector<const Expr *, 1> getUnsafePtrs() const override {
1419+
return {Ptr->IgnoreParenImpCasts()};
1420+
}
1421+
14031422
// FIXME: pointer adding zero should be fine
14041423
// FIXME: this gadge will need a fix-it
14051424
};
@@ -1457,6 +1476,8 @@ class SpanTwoParamConstructorGadget : public WarningGadget {
14571476
}
14581477
return {};
14591478
}
1479+
1480+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
14601481
};
14611482

14621483
/// A pointer initialization expression of the form:
@@ -1689,6 +1710,8 @@ class UnsafeBufferUsageAttrGadget : public WarningGadget {
16891710
SourceLocation getSourceLoc() const override { return Op->getBeginLoc(); }
16901711

16911712
DeclUseList getClaimedVarUseSites() const override { return {}; }
1713+
1714+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
16921715
};
16931716

16941717
/// A call of a constructor that performs unchecked buffer operations
@@ -1727,6 +1750,8 @@ class UnsafeBufferUsageCtorAttrGadget : public WarningGadget {
17271750
SourceLocation getSourceLoc() const override { return Op->getBeginLoc(); }
17281751

17291752
DeclUseList getClaimedVarUseSites() const override { return {}; }
1753+
1754+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
17301755
};
17311756

17321757
// Warning gadget for unsafe invocation of span::data method.
@@ -1793,6 +1818,8 @@ class DataInvocationGadget : public WarningGadget {
17931818
return true;
17941819
return false;
17951820
}
1821+
1822+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
17961823
};
17971824

17981825
class UnsafeLibcFunctionCallGadget : public WarningGadget {
@@ -1896,6 +1923,8 @@ class UnsafeLibcFunctionCallGadget : public WarningGadget {
18961923
}
18971924

18981925
DeclUseList getClaimedVarUseSites() const override { return {}; }
1926+
1927+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
18991928
};
19001929

19011930
// Represents expressions of the form `DRE[*]` in the Unspecified Lvalue
@@ -2467,6 +2496,52 @@ template <typename NodeTy> struct CompareNode {
24672496
}
24682497
};
24692498

2499+
std::set<const Expr *> clang::findUnsafePointers(const FunctionDecl *FD) {
2500+
class MockReporter : public UnsafeBufferUsageHandler {
2501+
public:
2502+
MockReporter() {}
2503+
void handleUnsafeOperation(const Stmt *, bool, ASTContext &) override {}
2504+
void handleUnsafeLibcCall(const CallExpr *, unsigned, ASTContext &,
2505+
const Expr *UnsafeArg = nullptr) override {}
2506+
void handleUnsafeOperationInContainer(const Stmt *, bool,
2507+
ASTContext &) override {}
2508+
void handleUnsafeVariableGroup(const VarDecl *,
2509+
const VariableGroupsManager &, FixItList &&,
2510+
const Decl *,
2511+
const FixitStrategy &) override {}
2512+
bool isSafeBufferOptOut(const SourceLocation &) const override {
2513+
return false;
2514+
}
2515+
bool ignoreUnsafeBufferInContainer(const SourceLocation &) const override {
2516+
return false;
2517+
}
2518+
bool ignoreUnsafeBufferInLibcCall(const SourceLocation &) const override {
2519+
return false;
2520+
}
2521+
std::string getUnsafeBufferUsageAttributeTextAt(
2522+
SourceLocation, StringRef WSSuffix = "") const override {
2523+
return "";
2524+
}
2525+
};
2526+
2527+
FixableGadgetList FixableGadgets;
2528+
WarningGadgetList WarningGadgets;
2529+
DeclUseTracker Tracker;
2530+
MockReporter IgnoreHandler;
2531+
2532+
findGadgets(FD->getBody(), FD->getASTContext(), IgnoreHandler, false,
2533+
FixableGadgets, WarningGadgets, Tracker);
2534+
2535+
std::set<const Expr *> Result;
2536+
for (auto &G : WarningGadgets) {
2537+
for (const Expr *E : G->getUnsafePtrs()) {
2538+
Result.insert(E);
2539+
}
2540+
}
2541+
2542+
return Result;
2543+
}
2544+
24702545
struct WarningGadgetSets {
24712546
std::map<const VarDecl *, std::set<const WarningGadget *>,
24722547
// To keep keys sorted by their locations in the map so that the

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,10 +2226,7 @@ void ASTStmtReader::VisitSubstNonTypeTemplateParmExpr(
22262226
E->AssociatedDeclAndRef.setPointer(readDeclAs<Decl>());
22272227
E->AssociatedDeclAndRef.setInt(CurrentUnpackingBits->getNextBit());
22282228
E->Index = CurrentUnpackingBits->getNextBits(/*Width=*/12);
2229-
if (CurrentUnpackingBits->getNextBit())
2230-
E->PackIndex = Record.readInt();
2231-
else
2232-
E->PackIndex = 0;
2229+
E->PackIndex = Record.readUnsignedOrNone().toInternalRepresentation();
22332230
E->Final = CurrentUnpackingBits->getNextBit();
22342231
E->SubstNonTypeTemplateParmExprBits.NameLoc = readSourceLocation();
22352232
E->Replacement = Record.readSubExpr();
@@ -2239,6 +2236,7 @@ void ASTStmtReader::VisitSubstNonTypeTemplateParmPackExpr(
22392236
SubstNonTypeTemplateParmPackExpr *E) {
22402237
VisitExpr(E);
22412238
E->AssociatedDecl = readDeclAs<Decl>();
2239+
E->Final = CurrentUnpackingBits->getNextBit();
22422240
E->Index = Record.readInt();
22432241
TemplateArgument ArgPack = Record.readTemplateArgument();
22442242
if (ArgPack.getKind() != TemplateArgument::Pack)

clang/lib/Serialization/ASTWriterStmt.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,9 +2228,7 @@ void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
22282228
Record.AddDeclRef(E->getAssociatedDecl());
22292229
CurrentPackingBits.addBit(E->isReferenceParameter());
22302230
CurrentPackingBits.addBits(E->getIndex(), /*Width=*/12);
2231-
CurrentPackingBits.addBit((bool)E->getPackIndex());
2232-
if (auto PackIndex = E->getPackIndex())
2233-
Record.push_back(*PackIndex + 1);
2231+
Record.writeUnsignedOrNone(E->getPackIndex());
22342232
CurrentPackingBits.addBit(E->getFinal());
22352233

22362234
Record.AddSourceLocation(E->getNameLoc());
@@ -2242,6 +2240,7 @@ void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
22422240
SubstNonTypeTemplateParmPackExpr *E) {
22432241
VisitExpr(E);
22442242
Record.AddDeclRef(E->getAssociatedDecl());
2243+
CurrentPackingBits.addBit(E->getFinal());
22452244
Record.push_back(E->getIndex());
22462245
Record.AddTemplateArgument(E->getArgumentPack());
22472246
Record.AddSourceLocation(E->getParameterPackLocation());

compiler-rt/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
569569
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
570570
list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
571571
endif()
572+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Haiku")
573+
list(APPEND SANITIZER_COMMON_LINK_LIBS root)
574+
list(APPEND SANITIZER_COMMON_LINK_LIBS bsd)
575+
endif()
572576

573577
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
574578
set(SANITIZER_NO_UNDEFINED_SYMBOLS_DEFAULT ON)

compiler-rt/cmake/config-ix.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ set(COMPILER_RT_SANITIZERS_TO_BUILD all CACHE STRING
760760
list_replace(COMPILER_RT_SANITIZERS_TO_BUILD all "${ALL_SANITIZERS}")
761761

762762
if (SANITIZER_COMMON_SUPPORTED_ARCH AND NOT LLVM_USE_SANITIZER AND
763-
(OS_NAME MATCHES "Android|Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS" OR
763+
(OS_NAME MATCHES "Android|Darwin|Linux|FreeBSD|NetBSD|Fuchsia|SunOS|Haiku" OR
764764
(OS_NAME MATCHES "Windows" AND NOT CYGWIN AND
765765
(NOT MINGW OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))))
766766
set(COMPILER_RT_HAS_SANITIZER_COMMON TRUE)
@@ -875,7 +875,7 @@ else()
875875
endif()
876876

877877
if (COMPILER_RT_HAS_SANITIZER_COMMON AND UBSAN_SUPPORTED_ARCH AND
878-
OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS")
878+
OS_NAME MATCHES "Darwin|Linux|FreeBSD|NetBSD|Windows|Android|Fuchsia|SunOS|Haiku")
879879
set(COMPILER_RT_HAS_UBSAN TRUE)
880880
else()
881881
set(COMPILER_RT_HAS_UBSAN FALSE)

compiler-rt/lib/asan/asan_linux.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
#include "sanitizer_common/sanitizer_platform.h"
1515
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
16-
SANITIZER_SOLARIS
16+
SANITIZER_SOLARIS || SANITIZER_HAIKU
17+
18+
# if SANITIZER_HAIKU
19+
# define _DEFAULT_SOURCE
20+
# endif
1721

1822
# include <dlfcn.h>
1923
# include <fcntl.h>
@@ -22,7 +26,9 @@
2226
# include <stdio.h>
2327
# include <sys/mman.h>
2428
# include <sys/resource.h>
25-
# include <sys/syscall.h>
29+
# if !SANITIZER_HAIKU
30+
# include <sys/syscall.h>
31+
# endif
2632
# include <sys/time.h>
2733
# include <sys/types.h>
2834
# include <unistd.h>
@@ -37,7 +43,7 @@
3743
# include "sanitizer_common/sanitizer_libc.h"
3844
# include "sanitizer_common/sanitizer_procmaps.h"
3945

40-
# if SANITIZER_FREEBSD
46+
# if SANITIZER_FREEBSD || SANITIZER_HAIKU
4147
# include <sys/link_elf.h>
4248
# endif
4349

@@ -54,6 +60,8 @@
5460
# elif SANITIZER_NETBSD
5561
# include <link_elf.h>
5662
# include <ucontext.h>
63+
# elif SANITIZER_HAIKU
64+
extern "C" void *_DYNAMIC;
5765
# else
5866
# include <link.h>
5967
# include <sys/ucontext.h>
@@ -162,6 +170,12 @@ static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size,
162170
return 0;
163171
}
164172

173+
# if SANITIZER_HAIKU
174+
if (!info->dlpi_name[0] ||
175+
internal_strncmp(info->dlpi_name, "/boot/system/runtime_loader",
176+
sizeof("/boot/system/runtime_loader") - 1) == 0)
177+
return 0;
178+
# endif
165179
# if SANITIZER_LINUX
166180
// Ignore vDSO. glibc versions earlier than 2.15 (and some patched
167181
// by distributors) return an empty name for the vDSO entry, so

compiler-rt/lib/asan/asan_malloc_linux.cpp

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

1616
#include "sanitizer_common/sanitizer_platform.h"
1717
#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
18-
SANITIZER_NETBSD || SANITIZER_SOLARIS
18+
SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
1919

2020
# include "asan_allocator.h"
2121
# include "asan_interceptors.h"

compiler-rt/lib/asan/asan_posix.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static void AfterFork(bool fork_child) {
180180

181181
void InstallAtForkHandler() {
182182
# if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE || \
183-
(SANITIZER_LINUX && SANITIZER_SPARC)
183+
(SANITIZER_LINUX && SANITIZER_SPARC) || SANITIZER_HAIKU
184184
// While other Linux targets use clone in internal_fork which doesn't
185185
// trigger pthread_atfork handlers, Linux/sparc64 uses __fork, causing a
186186
// hang.

compiler-rt/lib/asan/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ if(NOT APPLE)
109109
append_list_if(COMPILER_RT_HAS_LIBM -lm ASAN_UNITTEST_NOINST_LINK_FLAGS)
110110
append_list_if(COMPILER_RT_HAS_LIBDL -ldl ASAN_UNITTEST_NOINST_LINK_FLAGS)
111111
append_list_if(COMPILER_RT_HAS_LIBRT -lrt ASAN_UNITTEST_NOINST_LINK_FLAGS)
112+
append_list_if(HAIKU -lbsd ASAN_UNITTEST_NOINST_LINK_FLAGS)
112113
append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_UNITTEST_NOINST_LINK_FLAGS)
113114
append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread ASAN_DYNAMIC_UNITTEST_INSTRUMENTED_LINK_FLAGS)
114115
endif()

0 commit comments

Comments
 (0)