@@ -8350,14 +8350,16 @@ static void HandleNeonVectorTypeAttr(QualType &CurType, const ParsedAttr &Attr,
8350
8350
/// Handle the __ptrauth qualifier.
8351
8351
static void HandlePtrAuthQualifier(ASTContext &Ctx, QualType &T,
8352
8352
const ParsedAttr &Attr, Sema &S) {
8353
-
8354
- assert((Attr.getNumArgs() > 0 && Attr.getNumArgs() <= 3) &&
8355
- "__ptrauth qualifier takes between 1 and 3 arguments" );
8353
+ assert((Attr.getNumArgs() > 0 && Attr.getNumArgs() <= 4) &&
8354
+ "__ptrauth qualifier takes between 1 and 4 arguments");
8355
+ StringRef AttrName = Attr.getAttrName()->getName( );
8356
8356
Expr *KeyArg = Attr.getArgAsExpr(0);
8357
8357
Expr *IsAddressDiscriminatedArg =
8358
8358
Attr.getNumArgs() >= 2 ? Attr.getArgAsExpr(1) : nullptr;
8359
8359
Expr *ExtraDiscriminatorArg =
8360
8360
Attr.getNumArgs() >= 3 ? Attr.getArgAsExpr(2) : nullptr;
8361
+ Expr *AuthenticationOptionsArg =
8362
+ Attr.getNumArgs() >= 4 ? Attr.getArgAsExpr(3) : nullptr;
8361
8363
8362
8364
unsigned Key;
8363
8365
if (S.checkConstantPointerAuthKey(KeyArg, Key)) {
@@ -8373,10 +8375,140 @@ static void HandlePtrAuthQualifier(ASTContext &Ctx, QualType &T,
8373
8375
IsAddressDiscriminated);
8374
8376
IsInvalid |= !S.checkPointerAuthDiscriminatorArg(
8375
8377
ExtraDiscriminatorArg, PointerAuthDiscArgKind::Extra, ExtraDiscriminator);
8378
+ std::string LastAuthenticationMode;
8379
+ std::optional<PointerAuthenticationMode> AuthenticationMode = std::nullopt;
8380
+ bool IsIsaPointer = false;
8381
+ bool AuthenticatesNullValues = false;
8382
+
8383
+ if (AuthenticationOptionsArg && !AuthenticationOptionsArg->containsErrors()) {
8384
+ StringRef OptionsString;
8385
+ std::string EvaluatedString;
8386
+ bool HasEvaluatedOptionsString = false;
8387
+ const StringLiteral *OptionsStringLiteral =
8388
+ dyn_cast<StringLiteral>(AuthenticationOptionsArg);
8389
+ SourceRange AuthenticationOptionsRange =
8390
+ AuthenticationOptionsArg->getSourceRange();
8391
+ bool ReportedEvaluation = false;
8392
+ auto ReportEvaluationOfExpressionIfNeeded = [&]() {
8393
+ if (OptionsStringLiteral || !HasEvaluatedOptionsString ||
8394
+ ReportedEvaluation)
8395
+ return;
8396
+ ReportedEvaluation = true;
8397
+ S.Diag(AuthenticationOptionsRange.getBegin(),
8398
+ diag::note_ptrauth_evaluating_options)
8399
+ << OptionsString << AuthenticationOptionsRange;
8400
+ };
8401
+ auto DiagnoseInvalidOptionsParameter = [&](llvm::StringRef Reason) {
8402
+ S.Diag(AuthenticationOptionsRange.getBegin(),
8403
+ diag::err_ptrauth_invalid_option)
8404
+ << AttrName << Reason;
8405
+ Attr.setInvalid();
8406
+ IsInvalid = true;
8407
+ ReportEvaluationOfExpressionIfNeeded();
8408
+ };
8409
+ if (AuthenticationOptionsArg->isValueDependent() ||
8410
+ AuthenticationOptionsArg->isTypeDependent()) {
8411
+ DiagnoseInvalidOptionsParameter("is dependent");
8412
+ return;
8413
+ }
8414
+ if (OptionsStringLiteral) {
8415
+ OptionsString = OptionsStringLiteral->getString();
8416
+ HasEvaluatedOptionsString = true;
8417
+ } else {
8418
+ Expr::EvalResult Eval;
8419
+ bool Result = AuthenticationOptionsArg->EvaluateAsRValue(Eval, Ctx);
8420
+ if (Result && Eval.Val.isLValue()) {
8421
+ auto *BaseExpr = Eval.Val.getLValueBase().dyn_cast<const Expr *>();
8422
+ const StringLiteral *EvaluatedStringLiteral =
8423
+ dyn_cast<StringLiteral>(const_cast<Expr *>(BaseExpr));
8424
+ if (EvaluatedStringLiteral) {
8425
+ CharUnits StartOffset = Eval.Val.getLValueOffset();
8426
+ EvaluatedString = EvaluatedStringLiteral->getString().drop_front(
8427
+ StartOffset.getQuantity());
8428
+ OptionsString = EvaluatedString;
8429
+ HasEvaluatedOptionsString = true;
8430
+ }
8431
+ }
8432
+ }
8433
+ if (!HasEvaluatedOptionsString) {
8434
+ DiagnoseInvalidOptionsParameter(
8435
+ "must be a string of comma separated flags");
8436
+ return;
8437
+ }
8438
+ for (char Ch : OptionsString) {
8439
+ if (Ch != '-' && Ch != ',' && !isWhitespace(Ch) && !isalpha(Ch)) {
8440
+ DiagnoseInvalidOptionsParameter("contains invalid characters");
8441
+ return;
8442
+ }
8443
+ }
8444
+ HasEvaluatedOptionsString = true;
8445
+ OptionsString = OptionsString.trim();
8446
+ llvm::SmallVector<StringRef> Options;
8447
+ if (!OptionsString.empty())
8448
+ OptionsString.split(Options, ',');
8449
+
8450
+ auto OptionHandler = [&](auto Value, auto *Option,
8451
+ std::string *LastOption = nullptr) {
8452
+ return [&, Value, Option, LastOption](StringRef OptionString) {
8453
+ if (!*Option) {
8454
+ *Option = Value;
8455
+ if (LastOption)
8456
+ *LastOption = OptionString;
8457
+ return true;
8458
+ }
8459
+ bool IsAuthenticationMode =
8460
+ std::is_same_v<decltype(Value), PointerAuthenticationMode>;
8461
+ S.Diag(AuthenticationOptionsRange.getBegin(),
8462
+ diag::err_ptrauth_repeated_authentication_option)
8463
+ << AttrName << !IsAuthenticationMode << OptionString
8464
+ << (LastOption ? *LastOption : "");
8465
+ return false;
8466
+ };
8467
+ };
8376
8468
8377
- if (IsInvalid) {
8378
- Attr.setInvalid();
8379
- return;
8469
+ for (unsigned Idx = 0; Idx < Options.size(); ++Idx) {
8470
+ StringRef Option = Options[Idx].trim();
8471
+ if (Option.empty()) {
8472
+ bool IsLastOption = Idx == (Options.size() - 1);
8473
+ DiagnoseInvalidOptionsParameter(
8474
+ IsLastOption ? "has a trailing comma" : "contains an empty option");
8475
+ continue;
8476
+ }
8477
+ auto SelectedHandler =
8478
+ llvm::StringSwitch<std::function<bool(StringRef)>>(Option)
8479
+ .Case(PointerAuthenticationOptionStrip,
8480
+ OptionHandler(PointerAuthenticationMode::Strip,
8481
+ &AuthenticationMode, &LastAuthenticationMode))
8482
+ .Case(PointerAuthenticationOptionSignAndStrip,
8483
+ OptionHandler(PointerAuthenticationMode::SignAndStrip,
8484
+ &AuthenticationMode, &LastAuthenticationMode))
8485
+ .Case(PointerAuthenticationOptionSignAndAuth,
8486
+ OptionHandler(PointerAuthenticationMode::SignAndAuth,
8487
+ &AuthenticationMode, &LastAuthenticationMode))
8488
+ .Case(PointerAuthenticationOptionIsaPointer,
8489
+ OptionHandler(true,
8490
+ &IsIsaPointer))
8491
+ .Case(PointerAuthenticationOptionAuthenticatesNullValues,
8492
+ OptionHandler(true,
8493
+ &AuthenticatesNullValues))
8494
+ .Default([&](StringRef Option) {
8495
+ if (size_t WhitespaceIndex =
8496
+ Option.find_first_of(" \t\n\v\f\r");
8497
+ WhitespaceIndex != Option.npos) {
8498
+ StringRef LeadingOption = Option.slice(0, WhitespaceIndex);
8499
+ S.Diag(AuthenticationOptionsRange.getBegin(),
8500
+ diag::err_ptrauth_option_missing_comma)
8501
+ << AttrName << LeadingOption;
8502
+ } else {
8503
+ S.Diag(AuthenticationOptionsRange.getBegin(),
8504
+ diag::err_ptrauth_unknown_authentication_option)
8505
+ << AttrName << Option;
8506
+ }
8507
+ return false;
8508
+ });
8509
+ if (!SelectedHandler(Option))
8510
+ IsInvalid = true;
8511
+ }
8380
8512
}
8381
8513
8382
8514
if (!T->isSignableType(Ctx) && !T->isDependentType()) {
@@ -8385,6 +8517,9 @@ static void HandlePtrAuthQualifier(ASTContext &Ctx, QualType &T,
8385
8517
return;
8386
8518
}
8387
8519
8520
+ if (!AuthenticationMode)
8521
+ AuthenticationMode = PointerAuthenticationMode::SignAndAuth;
8522
+
8388
8523
if (T.getPointerAuth()) {
8389
8524
S.Diag(Attr.getLoc(), diag::err_ptrauth_qualifier_redundant) << T;
8390
8525
Attr.setInvalid();
@@ -8397,13 +8532,19 @@ static void HandlePtrAuthQualifier(ASTContext &Ctx, QualType &T,
8397
8532
return;
8398
8533
}
8399
8534
8535
+ if (IsInvalid) {
8536
+ Attr.setInvalid();
8537
+ return;
8538
+ }
8539
+
8400
8540
assert((!IsAddressDiscriminatedArg || IsAddressDiscriminated <= 1) &&
8401
8541
"address discriminator arg should be either 0 or 1");
8402
8542
PointerAuthQualifier Qual = PointerAuthQualifier::Create(
8403
- Key, IsAddressDiscriminated, ExtraDiscriminator,
8404
- PointerAuthenticationMode::SignAndAuth, /* IsIsaPointer=*/false,
8405
- /*AuthenticatesNullValues=*/false );
8543
+ Key, IsAddressDiscriminated, ExtraDiscriminator, *AuthenticationMode,
8544
+ IsIsaPointer, AuthenticatesNullValues);
8545
+ assert(Qual.getAuthenticationMode() == *AuthenticationMode );
8406
8546
T = S.Context.getPointerAuthType(T, Qual);
8547
+ assert(T.getPointerAuth().getAuthenticationMode() == *AuthenticationMode);
8407
8548
}
8408
8549
8409
8550
/// HandleArmSveVectorBitsTypeAttr - The "arm_sve_vector_bits" attribute is
0 commit comments