diff --git a/clang/docs/AllocToken.rst b/clang/docs/AllocToken.rst index 1a740e5e22c29..3f319e8be6421 100644 --- a/clang/docs/AllocToken.rst +++ b/clang/docs/AllocToken.rst @@ -52,8 +52,8 @@ change or removal. These may (experimentally) be selected with ``-Xclang The following command-line options affect generated token IDs: * ``-falloc-token-max=`` - Configures the maximum number of tokens. No max by default (tokens bounded - by ``SIZE_MAX``). + Configures the maximum number of token IDs. By default the number of tokens + is bounded by ``SIZE_MAX``. Querying Token IDs with ``__builtin_infer_alloc_token`` ======================================================= @@ -129,7 +129,7 @@ Fast ABI -------- An alternative ABI can be enabled with ``-fsanitize-alloc-token-fast-abi``, -which encodes the token ID hint in the allocation function name. +which encodes the token ID in the allocation function name. .. code-block:: c diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 8aa89d8c8c807..3f042f8ddb5a1 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -566,8 +566,8 @@ class LangOptions : public LangOptionsBase { bool AtomicFineGrainedMemory = false; bool AtomicIgnoreDenormalMode = false; - /// Maximum number of allocation tokens (0 = no max), nullopt if none set (use - /// target default). + /// Maximum number of allocation tokens (0 = target SIZE_MAX), nullopt if none + /// set (use target SIZE_MAX). std::optional AllocTokenMax; /// The allocation token mode. diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td index cda11fdc94230..786acd6abbd21 100644 --- a/clang/include/clang/Options/Options.td +++ b/clang/include/clang/Options/Options.td @@ -2758,7 +2758,7 @@ defm sanitize_alloc_token_extended : BoolOption<"f", "sanitize-alloc-token-exten def falloc_token_max_EQ : Joined<["-"], "falloc-token-max=">, Group, Visibility<[ClangOption, CC1Option]>, MetaVarName<"">, - HelpText<"Limit to maximum N allocation tokens (0 = no max)">; + HelpText<"Limit to maximum N allocation tokens (0 = target SIZE_MAX)">; def falloc_token_mode_EQ : Joined<["-"], "falloc-token-mode=">, Group, Visibility<[CC1Option]>, diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp index 5a96320e12b6f..b6013834b6852 100644 --- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp +++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp @@ -1317,8 +1317,9 @@ static bool interp__builtin_infer_alloc_token(InterpState &S, CodePtr OpPC, uint64_t BitWidth = ASTCtx.getTypeSize(ASTCtx.getSizeType()); auto Mode = ASTCtx.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode); + auto MaxTokensOpt = ASTCtx.getLangOpts().AllocTokenMax; uint64_t MaxTokens = - ASTCtx.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth)); + MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth)); // We do not read any of the arguments; discard them. for (int I = Call->getNumArgs() - 1; I >= 0; --I) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 74f6e3acb6b39..120c68d27de13 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -15559,8 +15559,9 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, auto Mode = Info.getLangOpts().AllocTokenMode.value_or(llvm::DefaultAllocTokenMode); uint64_t BitWidth = Info.Ctx.getTypeSize(Info.Ctx.getSizeType()); + auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax; uint64_t MaxTokens = - Info.getLangOpts().AllocTokenMax.value_or(~0ULL >> (64 - BitWidth)); + MaxTokensOpt.value_or(0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth)); auto MaybeToken = llvm::getAllocToken(Mode, *ATMD, MaxTokens); if (!MaybeToken) return Error(E, diag::note_constexpr_infer_alloc_token_stateful_mode); diff --git a/clang/test/SemaCXX/alloc-token.cpp b/clang/test/SemaCXX/alloc-token.cpp index be7acb7d42ef2..518ad7d94eb96 100644 --- a/clang/test/SemaCXX/alloc-token.cpp +++ b/clang/test/SemaCXX/alloc-token.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s +// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=0 // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-mode=typehash -DMODE_TYPEHASH // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++23 -fsyntax-only -verify %s -falloc-token-max=2 -DTOKEN_MAX=2 diff --git a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp index 8181e4ef1d74f..cf74354cb438f 100644 --- a/llvm/lib/Transforms/Instrumentation/AllocToken.cpp +++ b/llvm/lib/Transforms/Instrumentation/AllocToken.cpp @@ -67,9 +67,10 @@ cl::opt ClFuncPrefix("alloc-token-prefix", cl::desc("The allocation function prefix"), cl::Hidden, cl::init("__alloc_token_")); -cl::opt ClMaxTokens("alloc-token-max", - cl::desc("Maximum number of tokens (0 = no max)"), - cl::Hidden, cl::init(0)); +cl::opt + ClMaxTokens("alloc-token-max", + cl::desc("Maximum number of tokens (0 = target SIZE_MAX)"), + cl::Hidden, cl::init(0)); cl::opt ClFastABI("alloc-token-fast-abi",