Skip to content

Commit 2557b8f

Browse files
authored
Merge branch 'llvm:main' into patch-1
2 parents c93c4c4 + 574b66f commit 2557b8f

File tree

26 files changed

+1608
-166
lines changed

26 files changed

+1608
-166
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,11 +1428,11 @@ namespace {
14281428
}
14291429
bool destroy(bool RunDestructors = true) {
14301430
bool OK = cleanup(Info, RunDestructors, OldStackSize);
1431-
OldStackSize = -1U;
1431+
OldStackSize = std::numeric_limits<unsigned>::max();
14321432
return OK;
14331433
}
14341434
~ScopeRAII() {
1435-
if (OldStackSize != -1U)
1435+
if (OldStackSize != std::numeric_limits<unsigned>::max())
14361436
destroy(false);
14371437
// Body moved to a static method to encourage the compiler to inline away
14381438
// instances of this class.

clang/lib/Format/Format.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include "UnwrappedLineFormatter.h"
2323
#include "UsingDeclarationsSorter.h"
2424
#include "clang/Tooling/Inclusions/HeaderIncludes.h"
25+
2526
#include "llvm/ADT/Sequence.h"
27+
#include <limits>
2628

2729
#define DEBUG_TYPE "format-formatter"
2830

@@ -777,7 +779,7 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> {
777779
IO.mapOptional("Maximum", signedMaximum);
778780
Space.Maximum = static_cast<unsigned>(signedMaximum);
779781

780-
if (Space.Maximum != -1u)
782+
if (Space.Maximum < std::numeric_limits<unsigned>::max())
781783
Space.Minimum = std::min(Space.Minimum, Space.Maximum);
782784
}
783785
};
@@ -1672,7 +1674,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16721674
LLVMStyle.SpacesBeforeTrailingComments = 1;
16731675
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never;
16741676
LLVMStyle.SpacesInContainerLiterals = true;
1675-
LLVMStyle.SpacesInLineCommentPrefix = {/*Minimum=*/1, /*Maximum=*/-1u};
1677+
LLVMStyle.SpacesInLineCommentPrefix = {
1678+
/*Minimum=*/1, /*Maximum=*/std::numeric_limits<unsigned>::max()};
16761679
LLVMStyle.SpacesInParens = FormatStyle::SIPO_Never;
16771680
LLVMStyle.SpacesInSquareBrackets = false;
16781681
LLVMStyle.Standard = FormatStyle::LS_Latest;
@@ -3168,11 +3171,12 @@ static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start,
31683171
// the index of the first of the duplicates as the others are going to be
31693172
// removed. OffsetToEOL describes the cursor's position relative to the end of
31703173
// its current line.
3171-
// If `Cursor` is not on any #include, `Index` will be UINT_MAX.
3174+
// If `Cursor` is not on any #include, `Index` will be
3175+
// std::numeric_limits<unsigned>::max().
31723176
static std::pair<unsigned, unsigned>
31733177
FindCursorIndex(const ArrayRef<IncludeDirective> &Includes,
31743178
const ArrayRef<unsigned> &Indices, unsigned Cursor) {
3175-
unsigned CursorIndex = UINT_MAX;
3179+
unsigned CursorIndex = std::numeric_limits<unsigned>::max();
31763180
unsigned OffsetToEOL = 0;
31773181
for (int i = 0, e = Includes.size(); i != e; ++i) {
31783182
unsigned Start = Includes[Indices[i]].Offset;
@@ -3440,11 +3444,12 @@ tooling::Replacements sortCppIncludes(const FormatStyle &Style, StringRef Code,
34403444
return Replaces;
34413445
}
34423446

3443-
// Returns group number to use as a first order sort on imports. Gives UINT_MAX
3444-
// if the import does not match any given groups.
3447+
// Returns group number to use as a first order sort on imports. Gives
3448+
// std::numeric_limits<unsigned>::max() if the import does not match any given
3449+
// groups.
34453450
static unsigned findJavaImportGroup(const FormatStyle &Style,
34463451
StringRef ImportIdentifier) {
3447-
unsigned LongestMatchIndex = UINT_MAX;
3452+
unsigned LongestMatchIndex = std::numeric_limits<unsigned>::max();
34483453
unsigned LongestMatchLength = 0;
34493454
for (unsigned I = 0; I < Style.JavaImportGroups.size(); I++) {
34503455
const std::string &GroupPrefix = Style.JavaImportGroups[I];
@@ -3673,13 +3678,15 @@ formatReplacements(StringRef Code, const tooling::Replacements &Replaces,
36733678
namespace {
36743679

36753680
inline bool isHeaderInsertion(const tooling::Replacement &Replace) {
3676-
return Replace.getOffset() == UINT_MAX && Replace.getLength() == 0 &&
3681+
return Replace.getOffset() == std::numeric_limits<unsigned>::max() &&
3682+
Replace.getLength() == 0 &&
36773683
tooling::HeaderIncludes::IncludeRegex.match(
36783684
Replace.getReplacementText());
36793685
}
36803686

36813687
inline bool isHeaderDeletion(const tooling::Replacement &Replace) {
3682-
return Replace.getOffset() == UINT_MAX && Replace.getLength() == 1;
3688+
return Replace.getOffset() == std::numeric_limits<unsigned>::max() &&
3689+
Replace.getLength() == 1;
36833690
}
36843691

36853692
// FIXME: insert empty lines between newly created blocks.
@@ -3699,7 +3706,7 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
36993706
consumeError(HeaderInsertions.add(R));
37003707
} else if (isHeaderDeletion(R)) {
37013708
HeadersToDelete.insert(R.getReplacementText());
3702-
} else if (R.getOffset() == UINT_MAX) {
3709+
} else if (R.getOffset() == std::numeric_limits<unsigned>::max()) {
37033710
llvm::errs() << "Insertions other than header #include insertion are "
37043711
"not supported! "
37053712
<< R.getReplacementText() << "\n";

clang/lib/Lex/Lexer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <cstddef>
4242
#include <cstdint>
4343
#include <cstring>
44+
#include <limits>
4445
#include <optional>
4546
#include <string>
4647
#include <tuple>
@@ -3456,7 +3457,7 @@ std::optional<uint32_t> Lexer::tryReadNumericUCN(const char *&StartPtr,
34563457
}
34573458

34583459
unsigned Value = llvm::hexDigitValue(C);
3459-
if (Value == -1U) {
3460+
if (Value == std::numeric_limits<unsigned>::max()) {
34603461
if (!Delimited)
34613462
break;
34623463
if (Diagnose)

clang/lib/Sema/SemaExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "llvm/Support/SaveAndRestore.h"
6666
#include "llvm/Support/TimeProfiler.h"
6767
#include "llvm/Support/TypeSize.h"
68+
#include <limits>
6869
#include <optional>
6970

7071
using namespace clang;
@@ -1906,7 +1907,7 @@ ExprResult Sema::CreateGenericSelectionExpr(
19061907
}
19071908

19081909
SmallVector<unsigned, 1> CompatIndices;
1909-
unsigned DefaultIndex = -1U;
1910+
unsigned DefaultIndex = std::numeric_limits<unsigned>::max();
19101911
// Look at the canonical type of the controlling expression in case it was a
19111912
// deduced type like __auto_type. However, when issuing diagnostics, use the
19121913
// type the user wrote in source rather than the canonical one.
@@ -1961,7 +1962,8 @@ ExprResult Sema::CreateGenericSelectionExpr(
19611962
// C11 6.5.1.1p2 "If a generic selection has no default generic association,
19621963
// its controlling expression shall have type compatible with exactly one of
19631964
// the types named in its generic association list."
1964-
if (DefaultIndex == -1U && CompatIndices.size() == 0) {
1965+
if (DefaultIndex == std::numeric_limits<unsigned>::max() &&
1966+
CompatIndices.size() == 0) {
19651967
auto P = GetControllingRangeAndType(ControllingExpr, ControllingType);
19661968
SourceRange SR = P.first;
19671969
Diag(SR.getBegin(), diag::err_generic_sel_no_match) << SR << P.second;

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ endmacro()
162162
# OBJECT_LIBS <object libraries to use as sources>
163163
# PARENT_TARGET <convenience parent target>
164164
# ADDITIONAL_HEADERS <header files>
165-
# EXTENSIONS <boolean>)
165+
# EXTENSIONS <boolean>
166+
# C_STANDARD <version>
167+
# CXX_STANDARD <version>)
166168
function(add_compiler_rt_runtime name type)
167169
if(NOT type MATCHES "^(OBJECT|STATIC|SHARED|MODULE)$")
168170
message(FATAL_ERROR
@@ -171,7 +173,7 @@ function(add_compiler_rt_runtime name type)
171173
endif()
172174
cmake_parse_arguments(LIB
173175
""
174-
"PARENT_TARGET"
176+
"PARENT_TARGET;C_STANDARD;CXX_STANDARD"
175177
"OS;ARCHS;SOURCES;CFLAGS;LINK_FLAGS;DEFS;DEPS;LINK_LIBS;OBJECT_LIBS;ADDITIONAL_HEADERS;EXTENSIONS"
176178
${ARGN})
177179
set(libnames)
@@ -360,6 +362,12 @@ function(add_compiler_rt_runtime name type)
360362
set_target_link_flags(${libname} ${extra_link_flags_${libname}})
361363
set_property(TARGET ${libname} APPEND PROPERTY
362364
COMPILE_DEFINITIONS ${LIB_DEFS})
365+
if(LIB_C_STANDARD)
366+
set_property(TARGET ${libname} PROPERTY C_STANDARD ${LIB_C_STANDARD})
367+
endif()
368+
if(LIB_CXX_STANDARD)
369+
set_property(TARGET ${libname} PROPERTY CXX_STANDARD ${LIB_CXX_STANDARD})
370+
endif()
363371
set_target_output_directories(${libname} ${output_dir_${libname}})
364372
install(TARGETS ${libname}
365373
ARCHIVE DESTINATION ${install_dir_${libname}}

compiler-rt/cmake/builtin-config-ix.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ builtin_check_c_compiler_flag("-Xclang -mcode-object-version=none" COMPILER_RT_H
2626
builtin_check_c_compiler_flag(-Wbuiltin-declaration-mismatch COMPILER_RT_HAS_WBUILTIN_DECLARATION_MISMATCH_FLAG)
2727
builtin_check_c_compiler_flag(/Zl COMPILER_RT_HAS_ZL_FLAG)
2828
builtin_check_c_compiler_flag(-fcf-protection=full COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
29+
builtin_check_c_compiler_flag(-nostdinc++ COMPILER_RT_HAS_NOSTDINCXX_FLAG)
2930

3031
builtin_check_c_compiler_source(COMPILER_RT_HAS_ATOMIC_KEYWORD
3132
"

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
66
cmake_minimum_required(VERSION 3.20.0)
77

88
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
9-
project(CompilerRTBuiltins C ASM)
9+
project(CompilerRTBuiltins C CXX ASM)
1010
set(COMPILER_RT_STANDALONE_BUILD TRUE)
1111
set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
1212

@@ -64,6 +64,8 @@ include(CMakePushCheckState)
6464
option(COMPILER_RT_BUILTINS_HIDE_SYMBOLS
6565
"Do not export any symbols from the static library." ON)
6666

67+
include_directories(../../../third-party/siphash/include)
68+
6769
# TODO: Need to add a mechanism for logging errors when builtin source files are
6870
# added to a sub-directory and not this CMakeLists file.
6971
set(GENERIC_SOURCES
@@ -589,6 +591,7 @@ set(aarch64_SOURCES
589591
${GENERIC_TF_SOURCES}
590592
${GENERIC_SOURCES}
591593
cpu_model/aarch64.c
594+
aarch64/emupac.cpp
592595
aarch64/fp_mode.c
593596
)
594597

@@ -836,7 +839,7 @@ else ()
836839
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
837840
endif()
838841

839-
append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)
842+
append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ BUILTIN_CFLAGS)
840843
append_list_if(COMPILER_RT_HAS_WBUILTIN_DECLARATION_MISMATCH_FLAG -Werror=builtin-declaration-mismatch BUILTIN_CFLAGS)
841844

842845
# Don't embed directives for picking any specific CRT
@@ -958,6 +961,8 @@ else ()
958961
SOURCES ${${arch}_SOURCES}
959962
DEFS ${BUILTIN_DEFS}
960963
CFLAGS ${BUILTIN_CFLAGS_${arch}}
964+
C_STANDARD 11
965+
CXX_STANDARD 17
961966
PARENT_TARGET builtins)
962967
cmake_pop_check_state()
963968
endif ()
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//===--- emupac.cpp - Emulated PAC implementation -------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements Emulated PAC using SipHash_1_3 as the IMPDEF hashing
10+
// scheme.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include <stdint.h>
15+
16+
#include "siphash/SipHash.h"
17+
18+
// EmuPAC implements runtime emulation of PAC instructions. If the current
19+
// CPU supports PAC, EmuPAC uses real PAC instructions. Otherwise, it uses the
20+
// emulation, which is effectively an implementation of PAC with an IMPDEF
21+
// hashing scheme based on SipHash_1_3.
22+
//
23+
// The purpose of the emulation is to allow programs to be built to be portable
24+
// to machines without PAC support, with some performance loss and increased
25+
// probability of false positives (due to not being able to portably determine
26+
// the VA size), while being functionally almost equivalent to running on a
27+
// machine with PAC support. One example of a use case is if PAC is used in
28+
// production as a security mitigation, but the testing environment is
29+
// heterogeneous (i.e. some machines lack PAC support). In this case we would
30+
// like the testing machines to be able to detect issues resulting
31+
// from the use of PAC instructions that would affect production by running
32+
// tests. This can be achieved by building test binaries with EmuPAC and
33+
// production binaries with real PAC.
34+
//
35+
// EmuPAC should not be used in production and is only intended for testing use
36+
// cases. This is not only because of the performance costs, which will exist
37+
// even on PAC-supporting machines because of the function call overhead for
38+
// each sign/auth operation, but because it provides weaker security compared to
39+
// real PAC: the key is constant and public, which means that we do not mix a
40+
// global secret.
41+
//
42+
// The emulation assumes that the VA size is at most 48 bits. The architecture
43+
// as of ARMv8.2, which was the last architecture version in which PAC was not
44+
// mandatory, permitted VA size up to 52 bits via ARMv8.2-LVA, but we are
45+
// unaware of an ARMv8.2 CPU that implemented ARMv8.2-LVA.
46+
47+
const uint64_t max_va_size = 48;
48+
const uint64_t pac_mask = ((1ULL << 55) - 1) & ~((1ULL << max_va_size) - 1);
49+
const uint64_t ttbr1_mask = 1ULL << 55;
50+
51+
// Determine whether PAC is supported without accessing memory. This utilizes
52+
// the XPACLRI instruction which will copy bit 55 of x30 into at least bit 54 if
53+
// PAC is supported and acts as a NOP if PAC is not supported.
54+
static bool pac_supported() {
55+
register uintptr_t x30 __asm__("x30") = 1ULL << 55;
56+
__asm__ __volatile__("xpaclri" : "+r"(x30));
57+
return x30 & (1ULL << 54);
58+
}
59+
60+
// This asm snippet is used to force the creation of a frame record when
61+
// calling the EmuPAC functions. This is important because the EmuPAC functions
62+
// may crash if an auth failure is detected and may be unwound past using a
63+
// frame pointer based unwinder.
64+
#ifdef __GCC_HAVE_DWARF2_CFI_ASM
65+
#define CFI_INST(inst) inst
66+
#else
67+
#define CFI_INST(inst)
68+
#endif
69+
70+
// clang-format off
71+
#define FRAME_POINTER_WRAP(sym) \
72+
CFI_INST(".cfi_startproc\n") \
73+
"stp x29, x30, [sp, #-16]!\n" \
74+
CFI_INST(".cfi_def_cfa_offset 16\n") \
75+
"mov x29, sp\n" \
76+
CFI_INST(".cfi_def_cfa w29, 16\n") \
77+
CFI_INST(".cfi_offset w30, -8\n") \
78+
CFI_INST(".cfi_offset w29, -16\n") \
79+
"bl " #sym "\n" \
80+
CFI_INST(".cfi_def_cfa wsp, 16\n") \
81+
"ldp x29, x30, [sp], #16\n" \
82+
CFI_INST(".cfi_def_cfa_offset 0\n") \
83+
CFI_INST(".cfi_restore w30\n") \
84+
CFI_INST(".cfi_restore w29\n") \
85+
"ret\n" \
86+
CFI_INST(".cfi_endproc\n")
87+
// clang-format on
88+
89+
// Emulated DA key value.
90+
static const uint8_t emu_da_key[16] = {0xb5, 0xd4, 0xc9, 0xeb, 0x79, 0x10,
91+
0x4a, 0x79, 0x6f, 0xec, 0x8b, 0x1b,
92+
0x42, 0x87, 0x81, 0xd4};
93+
94+
extern "C" [[gnu::flatten]] uint64_t
95+
__emupac_pacda_impl(uint64_t ptr, uint64_t disc) {
96+
if (pac_supported()) {
97+
__asm__ __volatile__(".arch_extension pauth\npacda %0, %1"
98+
: "+r"(ptr)
99+
: "r"(disc));
100+
return ptr;
101+
}
102+
if (ptr & ttbr1_mask) {
103+
if ((ptr & pac_mask) != pac_mask) {
104+
return ptr | pac_mask;
105+
}
106+
} else {
107+
if (ptr & pac_mask) {
108+
return ptr & ~pac_mask;
109+
}
110+
}
111+
uint64_t hash;
112+
siphash<1, 3>(reinterpret_cast<uint8_t *>(&ptr), 8, emu_da_key,
113+
*reinterpret_cast<uint8_t(*)[8]>(&hash));
114+
return (ptr & ~pac_mask) | (hash & pac_mask);
115+
}
116+
117+
__asm__(".globl __emupac_pacda\n"
118+
"__emupac_pacda:\n" FRAME_POINTER_WRAP(__emupac_pacda_impl));
119+
120+
extern "C" [[gnu::flatten]] uint64_t
121+
__emupac_autda_impl(uint64_t ptr, uint64_t disc) {
122+
if (pac_supported()) {
123+
__asm__ __volatile__(".arch_extension pauth\nautda %0, %1"
124+
: "+r"(ptr)
125+
: "r"(disc));
126+
return ptr;
127+
}
128+
uint64_t ptr_without_pac =
129+
(ptr & ttbr1_mask) ? (ptr | pac_mask) : (ptr & ~pac_mask);
130+
uint64_t hash;
131+
siphash<1, 3>(reinterpret_cast<uint8_t *>(&ptr_without_pac), 8, emu_da_key,
132+
*reinterpret_cast<uint8_t(*)[8]>(&hash));
133+
if (((ptr & ~pac_mask) | (hash & pac_mask)) != ptr) {
134+
__builtin_trap();
135+
}
136+
return ptr_without_pac;
137+
}
138+
139+
__asm__(".globl __emupac_autda\n"
140+
"__emupac_autda:\n" FRAME_POINTER_WRAP(__emupac_autda_impl));

0 commit comments

Comments
 (0)