File: GlobalPtrAuthInfo.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (152 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===- GlobalPtrAuthInfo.cpp - Analysis tools for ptrauth globals ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/GlobalPtrAuthInfo.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"

using namespace llvm;

Expected<GlobalPtrAuthInfo> GlobalPtrAuthInfo::tryAnalyze(const Value *V) {
  auto Invalid = [](const Twine &Reason) {
    return make_error<StringError>(Reason, inconvertibleErrorCode());
  };

  auto &Ctx = V->getContext();

  V = V->stripPointerCasts();

  auto *GV = dyn_cast<GlobalVariable>(V);
  if (!GV)
    return Invalid("value isn't a global");

  if (GV->getSection() != "llvm.ptrauth")
    return Invalid("global isn't in section \"llvm.ptrauth\"");

  if (!GV->hasInitializer())
    return Invalid("global doesn't have an initializer");

  auto *Init = GV->getInitializer();

  auto *Ty = dyn_cast<StructType>(GV->getInitializer()->getType());
  if (!Ty)
    return Invalid("global isn't a struct");

  auto *I64Ty = Type::getInt64Ty(Ctx);
  auto *I32Ty = Type::getInt32Ty(Ctx);
  auto *P0I8Ty = Type::getInt8PtrTy(Ctx);
  // Check that the struct matches its expected shape:
  //   { i8*, i32, i64, i64 }
  if (!Ty->isLayoutIdentical(
        StructType::get(Ctx, {P0I8Ty, I32Ty, I64Ty, I64Ty})))
    return Invalid("global doesn't have type '{ i8*, i32, i64, i64 }'");

  auto *Key = dyn_cast<ConstantInt>(Init->getOperand(1));
  if (!Key)
    return Invalid("key isn't a constant integer");

  auto *AddrDiscriminator = Init->getOperand(2);
  if (!isa<ConstantInt>(AddrDiscriminator) &&
      !isa<ConstantExpr>(AddrDiscriminator))
    return Invalid("address discriminator isn't a constant integer or expr");

  auto *Discriminator = dyn_cast<ConstantInt>(Init->getOperand(3));
  if (!Discriminator)
    return Invalid("discriminator isn't a constant integer");

  return GlobalPtrAuthInfo(GV);
}

std::optional<GlobalPtrAuthInfo> GlobalPtrAuthInfo::analyze(const Value *V) {
  if (auto PAIOrErr = tryAnalyze(V)) {
    return *PAIOrErr;
  } else {
    consumeError(PAIOrErr.takeError());
    return std::nullopt;
  }
}

static bool areEquivalentAddrDiscriminators(const Value *V1, const Value *V2,
                                            const DataLayout &DL) {
  APInt V1Off(DL.getPointerSizeInBits(), 0);
  APInt V2Off(DL.getPointerSizeInBits(), 0);

  if (auto *V1Cast = dyn_cast<PtrToIntOperator>(V1))
    V1 = V1Cast->getPointerOperand();
  if (auto *V2Cast = dyn_cast<PtrToIntOperator>(V2))
    V2 = V2Cast->getPointerOperand();
  auto *V1Base = V1->stripAndAccumulateInBoundsConstantOffsets(DL, V1Off);
  auto *V2Base = V2->stripAndAccumulateInBoundsConstantOffsets(DL, V2Off);
  return V1Base == V2Base && V1Off == V2Off;
}

bool GlobalPtrAuthInfo::isCompatibleWith(const Value *Key,
                                         const Value *Discriminator,
                                         const DataLayout &DL) const {
  // If the keys are different, there's no chance for this to be compatible.
  if (Key != getKey())
    return false;

  // If the discriminators are the same, this is compatible iff there is no
  // address discriminator.
  if (Discriminator == getDiscriminator())
    return getAddrDiscriminator()->isNullValue();

  // If we dynamically blend the discriminator with the address discriminator,
  // this is compatible.
  if (auto *DiscBlend = dyn_cast<IntrinsicInst>(Discriminator)) {
    if (DiscBlend->getIntrinsicID() == Intrinsic::ptrauth_blend &&
        DiscBlend->getOperand(1) == getDiscriminator() &&
        areEquivalentAddrDiscriminators(DiscBlend->getOperand(0),
                                        getAddrDiscriminator(), DL))
      return true;
  }

  // If we don't have a non-address discriminator, we don't need a blend in
  // the first place:  accept the address discriminator as the discriminator.
  if (getDiscriminator()->isNullValue() &&
      areEquivalentAddrDiscriminators(getAddrDiscriminator(), Discriminator,
                                      DL))
    return true;

  // Otherwise, we don't know.
  return false;
}

Constant *GlobalPtrAuthInfo::createWithSameSchema(Module &M,
                                                  Constant *Pointer) const {
  return create(M, Pointer, const_cast<ConstantInt*>(getKey()),
                const_cast<Constant*>(getAddrDiscriminator()),
                const_cast<ConstantInt*>(getDiscriminator()));
}

Constant *GlobalPtrAuthInfo::create(Module &M, Constant *Pointer,
                                    ConstantInt *Key,
                                    Constant *AddrDiscriminator,
                                    ConstantInt *Discriminator) {
  auto CastPointer =
    ConstantExpr::getBitCast(Pointer, Type::getInt8PtrTy(M.getContext()));

  auto Init = ConstantStruct::getAnon({CastPointer, Key, AddrDiscriminator,
                                       Discriminator}, /*packed*/ false);

  // TODO: look for an existing global with the right setup?
  auto GV = new GlobalVariable(M, Init->getType(), /*constant*/ true,
                               GlobalVariable::PrivateLinkage, Init);
  GV->setSection("llvm.ptrauth");

  auto Result = ConstantExpr::getBitCast(GV, Pointer->getType());

  assert(analyze(Result).has_value() && "invalid ptrauth constant");

  return Result;
}