Skip to content

Commit

Permalink
merge main into amd-staging
Browse files Browse the repository at this point in the history
Change-Id: Ie599504ba4c5ea709b625b7ec025dbec244ff258
  • Loading branch information
Jenkins committed Oct 6, 2024
2 parents 24cddcc + 46944b0 commit 2f2ce7e
Show file tree
Hide file tree
Showing 261 changed files with 455 additions and 534 deletions.
80 changes: 60 additions & 20 deletions clang/docs/RealtimeSanitizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Introduction
============
RealtimeSanitizer (a.k.a. RTSan) is a real-time safety testing tool for C and C++
projects. RTSan can be used to detect real-time violations, i.e. calls to methods
that are not safe for use in functions with deterministic runtime requirements.
that are not safe for use in functions with deterministic run time requirements.
RTSan considers any function marked with the ``[[clang::nonblocking]]`` attribute
to be a real-time function. If RTSan detects a call to ``malloc``, ``free``,
``pthread_mutex_lock``, or anything else that could have a non-deterministic
Expand Down Expand Up @@ -58,31 +58,71 @@ code.
return 0;
}
# Compile and link
% clang++ -fsanitize=realtime -g example_realtime_violation.cpp
% clang++ -fsanitize=realtime example_realtime_violation.cpp
If a real-time safety violation is detected in a ``[[clang::nonblocking]]``
context, or any function invoked by that function, the program will exit with a
non-zero exit code.

.. code-block:: console
% clang++ -fsanitize=realtime -g example_realtime_violation.cpp
% clang++ -fsanitize=realtime example_realtime_violation.cpp
% ./a.out
Real-time violation: intercepted call to real-time unsafe function `malloc` in real-time context! Stack trace:
#0 0x000102893034 in __rtsan::PrintStackTrace() rtsan_stack.cpp:45
#1 0x000102892e64 in __rtsan::Context::ExpectNotRealtime(char const*) rtsan_context.cpp:78
#2 0x00010289397c in malloc rtsan_interceptors.cpp:286
#3 0x000195bd7bd0 in operator new(unsigned long)+0x1c (libc++abi.dylib:arm64+0x16bd0)
#4 0x5c7f00010230f07c (<unknown module>)
#5 0x00010230f058 in std::__1::__libcpp_allocate[abi:ue170006](unsigned long, unsigned long) new:324
#6 0x00010230effc in std::__1::allocator<float>::allocate[abi:ue170006](unsigned long) allocator.h:114
... snip ...
#10 0x00010230e4bc in std::__1::vector<float, std::__1::allocator<float>>::__append(unsigned long) vector:1162
#11 0x00010230dcdc in std::__1::vector<float, std::__1::allocator<float>>::resize(unsigned long) vector:1981
#12 0x00010230dc28 in violation() main.cpp:5
#13 0x00010230dd64 in main main.cpp:9
#14 0x0001958960dc (<unknown module>)
#15 0x2f557ffffffffffc (<unknown module>)
==76290==ERROR: RealtimeSanitizer: unsafe-library-call
Intercepted call to real-time unsafe function `malloc` in real-time context!
#0 0x000102a7b884 in malloc rtsan_interceptors.cpp:426
#1 0x00019c326bd0 in operator new(unsigned long)+0x1c (libc++abi.dylib:arm64+0x16bd0)
#2 0xa30d0001024f79a8 (<unknown module>)
#3 0x0001024f794c in std::__1::__libcpp_allocate[abi:ne200000](unsigned long, unsigned long)+0x44
#4 0x0001024f78c4 in std::__1::allocator<float>::allocate[abi:ne200000](unsigned long)+0x44
... snip ...
#9 0x0001024f6868 in std::__1::vector<float, std::__1::allocator<float>>::resize(unsigned long)+0x48
#10 0x0001024f67b4 in violation()+0x24
#11 0x0001024f68f0 in main+0x18 (a.out:arm64+0x1000028f0)
#12 0x00019bfe3150 (<unknown module>)
#13 0xed5efffffffffffc (<unknown module>)
Blocking functions
------------------

Calls to system library functions such as ``malloc`` are automatically caught by
RealtimeSanitizer. Real-time programmers may also write their own blocking
(real-time unsafe) functions that they wish RealtimeSanitizer to be aware of.
RealtimeSanitizer will raise an error at run time if any function attributed
with ``[[clang::blocking]]`` is called in a ``[[clang::nonblocking]]`` context.

.. code-block:: console
$ cat example_blocking_violation.cpp
#include <atomic>
#include <thread>
std::atomic<bool> has_permission{false};
int wait_for_permission() [[clang::blocking]] {
while (has_permission.load() == false)
std::this_thread::yield();
return 0;
}
int real_time_function() [[clang::nonblocking]] {
return wait_for_permission();
}
int main() {
return real_time_function();
}
$ clang++ -fsanitize=realtime example_blocking_violation.cpp && ./a.out
==76131==ERROR: RealtimeSanitizer: blocking-call
Call to blocking function `wait_for_permission()` in real-time context!
#0 0x0001000c3db0 in wait_for_permission()+0x10 (a.out:arm64+0x100003db0)
#1 0x0001000c3e3c in real_time_function()+0x10 (a.out:arm64+0x100003e3c)
#2 0x0001000c3e68 in main+0x10 (a.out:arm64+0x100003e68)
#3 0x00019bfe3150 (<unknown module>)
#4 0x5a27fffffffffffc (<unknown module>)
Run-time flags
--------------
Expand Down Expand Up @@ -159,7 +199,7 @@ Disabling

In some circumstances, you may want to suppress error reporting in a specific scope.

In C++, this is achieved via ``__rtsan::ScopedDisabler``. Within the scope where the ``ScopedDisabler`` object is instantiated, all sanitizer error reports are suppressed. This suppression applies to the current scope as well as all invoked functions, including any functions called transitively.
In C++, this is achieved via ``__rtsan::ScopedDisabler``. Within the scope where the ``ScopedDisabler`` object is instantiated, all sanitizer error reports are suppressed. This suppression applies to the current scope as well as all invoked functions, including any functions called transitively.

.. code-block:: c++

Expand All @@ -174,7 +214,7 @@ In C++, this is achieved via ``__rtsan::ScopedDisabler``. Within the scope wher

If RealtimeSanitizer is not enabled at compile time (i.e., the code is not compiled with the ``-fsanitize=realtime`` flag), the ``ScopedDisabler`` is compiled as a no-op.

In C, you can use the ``__rtsan_disable()`` and ``rtsan_enable()`` functions to manually disable and re-enable RealtimeSanitizer checks.
In C, you can use the ``__rtsan_disable()`` and ``rtsan_enable()`` functions to manually disable and re-enable RealtimeSanitizer checks.

.. code-block:: c++

Expand Down
12 changes: 5 additions & 7 deletions clang/lib/Basic/TargetID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ parseTargetIDWithFormatCheckingOnly(llvm::StringRef TargetID,
if (Sign != '+' && Sign != '-')
return std::nullopt;
bool IsOn = Sign == '+';
auto Loc = FeatureMap->find(Feature);
// Each feature can only show up at most once in target ID.
if (Loc != FeatureMap->end())
if (!FeatureMap->try_emplace(Feature, IsOn).second)
return std::nullopt;
(*FeatureMap)[Feature] = IsOn;
Features = Splits.second;
}
return Processor;
Expand Down Expand Up @@ -147,15 +145,15 @@ getConflictTargetIDCombination(const std::set<llvm::StringRef> &TargetIDs) {
struct Info {
llvm::StringRef TargetID;
llvm::StringMap<bool> Features;
Info(llvm::StringRef TargetID, const llvm::StringMap<bool> &Features)
: TargetID(TargetID), Features(Features) {}
};
llvm::StringMap<Info> FeatureMap;
for (auto &&ID : TargetIDs) {
llvm::StringMap<bool> Features;
llvm::StringRef Proc = *parseTargetIDWithFormatCheckingOnly(ID, &Features);
auto Loc = FeatureMap.find(Proc);
if (Loc == FeatureMap.end())
FeatureMap[Proc] = Info{ID, Features};
else {
auto [Loc, Inserted] = FeatureMap.try_emplace(Proc, ID, Features);
if (!Inserted) {
auto &ExistingFeatures = Loc->second.Features;
if (llvm::any_of(Features, [&](auto &F) {
return ExistingFeatures.count(F.first()) == 0;
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Optimizer/CodeGen/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ std::unique_ptr<mlir::Pass> createLLVMDialectToLLVMPass(
[](llvm::Module &m, llvm::raw_ostream &out) { m.print(out, nullptr); });

/// Populate the given list with patterns that convert from FIR to LLVM.
void populateFIRToLLVMConversionPatterns(fir::LLVMTypeConverter &converter,
mlir::RewritePatternSet &patterns,
fir::FIRToLLVMPassOptions &options);
void populateFIRToLLVMConversionPatterns(
const fir::LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns,
fir::FIRToLLVMPassOptions &options);

/// Populate the pattern set with the PreCGRewrite patterns.
void populatePreCGRewritePatterns(mlir::RewritePatternSet &patterns,
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/CodeGen/CodeGenOpenMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LLVMTypeConverter;
/// dialect, utilised in cases where the default OpenMP dialect handling cannot
/// handle all cases for intermingled fir types and operations.
void populateOpenMPFIRToLLVMConversionPatterns(
LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns);
const LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns);

} // namespace fir

Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Optimizer/Transforms/CufOpConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DataLayout;

namespace cuf {

void populateCUFToFIRConversionPatterns(fir::LLVMTypeConverter &converter,
void populateCUFToFIRConversionPatterns(const fir::LLVMTypeConverter &converter,
mlir::DataLayout &dl,
mlir::RewritePatternSet &patterns);

Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/CodeGen/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3823,7 +3823,7 @@ fir::createLLVMDialectToLLVMPass(llvm::raw_ostream &output,
}

void fir::populateFIRToLLVMConversionPatterns(
fir::LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns,
const fir::LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns,
fir::FIRToLLVMPassOptions &options) {
patterns.insert<
AbsentOpConversion, AddcOpConversion, AddrOfOpConversion,
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ struct MapInfoOpConversion
} // namespace

void fir::populateOpenMPFIRToLLVMConversionPatterns(
LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns) {
const LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns) {
patterns.add<MapInfoOpConversion>(converter);
}
6 changes: 3 additions & 3 deletions flang/lib/Optimizer/Transforms/CufOpConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ struct CufAllocOpConversion : public mlir::OpRewritePattern<cuf::AllocOp> {
using OpRewritePattern::OpRewritePattern;

CufAllocOpConversion(mlir::MLIRContext *context, mlir::DataLayout *dl,
fir::LLVMTypeConverter *typeConverter)
const fir::LLVMTypeConverter *typeConverter)
: OpRewritePattern(context), dl{dl}, typeConverter{typeConverter} {}

mlir::LogicalResult
Expand Down Expand Up @@ -311,7 +311,7 @@ struct CufAllocOpConversion : public mlir::OpRewritePattern<cuf::AllocOp> {

private:
mlir::DataLayout *dl;
fir::LLVMTypeConverter *typeConverter;
const fir::LLVMTypeConverter *typeConverter;
};

struct CufFreeOpConversion : public mlir::OpRewritePattern<cuf::FreeOp> {
Expand Down Expand Up @@ -583,7 +583,7 @@ class CufOpConversion : public fir::impl::CufOpConversionBase<CufOpConversion> {
} // namespace

void cuf::populateCUFToFIRConversionPatterns(
fir::LLVMTypeConverter &converter, mlir::DataLayout &dl,
const fir::LLVMTypeConverter &converter, mlir::DataLayout &dl,
mlir::RewritePatternSet &patterns) {
patterns.insert<CufAllocOpConversion>(patterns.getContext(), &dl, &converter);
patterns.insert<CufAllocateOpConversion, CufDeallocateOpConversion,
Expand Down
8 changes: 8 additions & 0 deletions libc/include/llvm-libc-macros/linux/error-number-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@
#define EHWPOISON 133
#endif // EHWPOISON

#ifndef EOPNOTSUPP
#define EOPNOTSUPP 95
#endif

#ifndef ENOTSUP
#define ENOTSUP EOPNOTSUPP
#endif

#endif // LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
6 changes: 3 additions & 3 deletions libc/src/__support/StringUtil/tables/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_header_library(
HDRS
stdc_errors.h
DEPENDS
libc.include.errno
libc.src.errno.errno
libc.src.__support.StringUtil.message_mapper
)

Expand All @@ -12,7 +12,7 @@ add_header_library(
HDRS
posix_errors.h
DEPENDS
libc.include.errno
libc.src.errno.errno
libc.src.__support.StringUtil.message_mapper
)

Expand All @@ -21,8 +21,8 @@ add_header_library(
HDRS
linux_extension_errors.h
DEPENDS
libc.include.errno
libc.src.__support.StringUtil.message_mapper
libc.src.errno.errno
)

add_header_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/macros/config.h"

#include <errno.h> // For error macros
#include "src/errno/libc_errno.h"

namespace LIBC_NAMESPACE_DECL {

Expand Down
3 changes: 1 addition & 2 deletions libc/src/__support/StringUtil/tables/posix_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_POSIX_ERRORS_H
#define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_POSIX_ERRORS_H

#include "hdr/errno_macros.h"
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/macros/config.h"

#include <errno.h> // For error macros

namespace LIBC_NAMESPACE_DECL {

LIBC_INLINE_VAR constexpr MsgTable<76> POSIX_ERRORS = {
Expand Down
2 changes: 0 additions & 2 deletions libc/src/__support/StringUtil/tables/stdc_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/macros/config.h"

#include <errno.h> // For error macros

namespace LIBC_NAMESPACE_DECL {

LIBC_INLINE_VAR constexpr const MsgTable<4> STDC_ERRORS = {
Expand Down
10 changes: 0 additions & 10 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,6 @@ add_entrypoint_object(
DEPENDS
.common_constants
.explogxf
libc.include.errno
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
Expand Down Expand Up @@ -1406,7 +1405,6 @@ add_entrypoint_object(
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.include.errno
libc.src.errno.errno
COMPILE_OPTIONS
-O3
Expand Down Expand Up @@ -1445,7 +1443,6 @@ add_entrypoint_object(
DEPENDS
.common_constants
.explogxf
libc.include.errno
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
Expand Down Expand Up @@ -1478,7 +1475,6 @@ add_header_library(
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.common
libc.include.errno
libc.src.errno.errno
)

Expand Down Expand Up @@ -1548,7 +1544,6 @@ add_entrypoint_object(
DEPENDS
.common_constants
.explogxf
libc.include.errno
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
Expand Down Expand Up @@ -1580,7 +1575,6 @@ add_header_library(
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.src.__support.common
libc.include.errno
libc.src.errno.errno
COMPILE_OPTIONS
-O3
Expand Down Expand Up @@ -1632,7 +1626,6 @@ add_entrypoint_object(
DEPENDS
.common_constants
.explogxf
libc.include.errno
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.dyadic_float
Expand Down Expand Up @@ -1666,7 +1659,6 @@ add_entrypoint_object(
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.rounding_mode
libc.src.__support.macros.optimization
libc.include.errno
libc.src.errno.errno
COMPILE_OPTIONS
-O3
Expand Down Expand Up @@ -1705,7 +1697,6 @@ add_entrypoint_object(
.exp10f_impl
.exp2f_impl
.explogxf
libc.include.errno
libc.src.__support.CPP.bit
libc.src.__support.CPP.optional
libc.src.__support.FPUtil.fenv_impl
Expand Down Expand Up @@ -4144,7 +4135,6 @@ add_object_library(
libc.src.__support.FPUtil.nearest_integer
libc.src.__support.FPUtil.polyeval
libc.src.__support.common
libc.include.errno
libc.src.errno.errno
COMPILE_OPTIONS
-O3
Expand Down
2 changes: 0 additions & 2 deletions libc/src/math/generic/acosf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY

#include <errno.h>

#include "inv_trigf_utils.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
2 changes: 0 additions & 2 deletions libc/src/math/generic/asinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA

#include <errno.h>

#include "inv_trigf_utils.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
Loading

0 comments on commit 2f2ce7e

Please sign in to comment.