Skip to content

Commit b749459

Browse files
melvergithub-actions[bot]
authored andcommitted
Automerge: [AllocToken] Fix and clarify -falloc-token-max=0 (#168689)
The option -falloc-token-max=0 is supposed to be usable to override previous settings back to the target default max tokens (SIZE_MAX). This did not work for the builtin: ``` | executed command: clang -cc1 [..] -nostdsysteminc -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify clang/test/SemaCXX/alloc-token.cpp -falloc-token-max=0 | clang: llvm/lib/Support/AllocToken.cpp:38: std::optional<uint64_t> llvm::getAllocToken(AllocTokenMode, const AllocTokenMetadata &, uint64_t): Assertion `MaxTokens && "Must provide non-zero max tokens"' failed. ``` Fix it by also picking the default if "0" is passed. Improve the documentation to be clearer what the value of "0" means.
2 parents 614c00a + 1500536 commit b749459

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
lines changed

clang/docs/AllocToken.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ change or removal. These may (experimentally) be selected with ``-Xclang
5252
The following command-line options affect generated token IDs:
5353

5454
* ``-falloc-token-max=<N>``
55-
Configures the maximum number of tokens. No max by default (tokens bounded
56-
by ``SIZE_MAX``).
55+
Configures the maximum number of token IDs. By default the number of tokens
56+
is bounded by ``SIZE_MAX``.
5757

5858
Querying Token IDs with ``__builtin_infer_alloc_token``
5959
=======================================================
@@ -129,7 +129,7 @@ Fast ABI
129129
--------
130130

131131
An alternative ABI can be enabled with ``-fsanitize-alloc-token-fast-abi``,
132-
which encodes the token ID hint in the allocation function name.
132+
which encodes the token ID in the allocation function name.
133133

134134
.. code-block:: c
135135

clang/include/clang/Basic/LangOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ class LangOptions : public LangOptionsBase {
566566
bool AtomicFineGrainedMemory = false;
567567
bool AtomicIgnoreDenormalMode = false;
568568

569-
/// Maximum number of allocation tokens (0 = no max), nullopt if none set (use
570-
/// target default).
569+
/// Maximum number of allocation tokens (0 = target SIZE_MAX), nullopt if none
570+
/// set (use target SIZE_MAX).
571571
std::optional<uint64_t> AllocTokenMax;
572572

573573
/// The allocation token mode.

clang/include/clang/Options/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2758,7 +2758,7 @@ defm sanitize_alloc_token_extended : BoolOption<"f", "sanitize-alloc-token-exten
27582758
def falloc_token_max_EQ : Joined<["-"], "falloc-token-max=">,
27592759
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
27602760
MetaVarName<"<N>">,
2761-
HelpText<"Limit to maximum N allocation tokens (0 = no max)">;
2761+
HelpText<"Limit to maximum N allocation tokens (0 = target SIZE_MAX)">;
27622762

27632763
def falloc_token_mode_EQ : Joined<["-"], "falloc-token-mode=">,
27642764
Group<f_Group>, Visibility<[CC1Option]>,

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1317,8 +1317,9 @@ static bool interp__builtin_infer_alloc_token(InterpState &S, CodePtr OpPC,
13171317
uint64_t BitWidth = ASTCtx.getTypeSize(ASTCtx.getSizeType());
13181318
auto Mode =
13191319
ASTCtx.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode);
1320+
auto MaxTokensOpt = ASTCtx.getLangOpts().AllocTokenMax;
13201321
uint64_t MaxTokens =
1321-
ASTCtx.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth));
1322+
MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
13221323

13231324
// We do not read any of the arguments; discard them.
13241325
for (int I = Call->getNumArgs() - 1; I >= 0; --I)

clang/lib/AST/ExprConstant.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15559,8 +15559,9 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1555915559
auto Mode =
1556015560
Info.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode);
1556115561
uint64_t BitWidth = Info.Ctx.getTypeSize(Info.Ctx.getSizeType());
15562+
auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
1556215563
uint64_t MaxTokens =
15563-
Info.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth));
15564+
MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
1556415565
auto MaybeToken = llvm::getAllocToken(Mode, *ATMD, MaxTokens);
1556515566
if (!MaybeToken)
1556615567
return Error(E, diag::note_constexpr_infer_alloc_token_stateful_mode);

clang/test/SemaCXX/alloc-token.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=0
23
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
34
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-mode=typehash -DMODE_TYPEHASH
45
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=2 -DTOKEN_MAX=2

llvm/lib/Transforms/Instrumentation/AllocToken.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ cl::opt<std::string> ClFuncPrefix("alloc-token-prefix",
6767
cl::desc("The allocation function prefix"),
6868
cl::Hidden, cl::init("__alloc_token_"));
6969

70-
cl::opt<uint64_t> ClMaxTokens("alloc-token-max",
71-
cl::desc("Maximum number of tokens (0 = no max)"),
72-
cl::Hidden, cl::init(0));
70+
cl::opt<uint64_t>
71+
ClMaxTokens("alloc-token-max",
72+
cl::desc("Maximum number of tokens (0 = target SIZE_MAX)"),
73+
cl::Hidden, cl::init(0));
7374

7475
cl::opt<bool>
7576
ClFastABI("alloc-token-fast-abi",

0 commit comments

Comments
 (0)