File: SoftPointerAuth.cpp

package info (click to toggle)
swiftlang 6.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,604 kB
  • sloc: cpp: 9,901,740; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (858 lines) | stat: -rw-r--r-- 31,514 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//===- SoftPointerAuth.cpp - Software lowering for ptrauth intrinsics -----===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This pass lowers the llvm.ptrauth intrinsics into something that can
// be supported (inefficiently) on an arbitrary target.
//
// The runtime functions you must define to use this pass are:
//   /// Apply a signature to the given unsigned pointer value.
//   void *__ptrauth_sign(void *pointer, int32_t key, uintptr_t discriminator);
//
//   /// Remove the signature from the given signed pointer value.
//   void *__ptrauth_strip(void *pointer, int32_t key);
//
//   /// Authenticate and remove the signature on the given signed
//   /// pointer value.  Trap on authenticate failure.
//   void *__ptrauth_auth(void *pointer, int32_t key, uintptr_t discriminator);
//
//   /// Blend a small non-zero value into a primary discriminator,
//   /// which is expected to resemble a pointer.
//   uintptr_t __ptrauth_blend(uintptr_t primaryDiscriminator,
//                             uintptr_t secondaryDiscriminator);
//
//   /// Compute a full, pointer-wide signature on a value.
//   uintptr_t __ptrauth_sign_generic(uintptr_t data, uintptr_t discriminator);
//
// The resulting code pattern does not perfectly protect against the backend
// inserting code between authentications and uses, and so the result may
// be attackable.
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Instrumentation/SoftPointerAuth.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Transforms/Instrumentation.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLExtras.h"
#include <map>
#include <optional>

#define DEBUG_TYPE "soft-ptrauth"

using namespace llvm;
using IRBuilderTy = llvm::IRBuilder<>;

namespace {

/// A structure for tracking uses of relocations within a Constant.
struct UseSite {
  /// A map from operand index to the tracking sites of children.
  /// If this is empty, the Constant is a GlobalVariable for a relocation.
  /// Otherwise, the Constant is a ConstantAggregate or ConstantExpr, and
  /// the relocation reference(s) appear further down in the tree.
  std::map<unsigned, UseSite> Children;
};

/// A linked list down to the use of a relocation.
struct UsePath {
  const UsePath *Next;
  unsigned OperandIndex;
};

enum TypeTag {
  IntPtr,                  // uintptr_t
  Discriminator = IntPtr,  // uintptr_t
  Key,                     // uint32_t
  VoidPtr,                 // i8*
};

class SoftPointerAuth {
  // The module.
  Module *M = nullptr;

  // Cached function pointers, initialized lazily.
  FunctionCallee SignPointerFn = nullptr;
  FunctionCallee AuthPointerFn = nullptr;
  FunctionCallee StripPointerFn = nullptr;
  FunctionCallee BlendDiscriminatorFn = nullptr;
  FunctionCallee SignGenericFn = nullptr;

  std::optional<IRBuilderTy> GlobalConstructorBuilder;

public:
  SoftPointerAuth() {}

  bool runOnModule(Module &M);

private:
  bool isPointerAuthRelocation(GlobalVariable *global);

  bool transformRelocations();
  void transformGlobalInitializer(GlobalVariable *global,
                                  const UseSite &usesToTransform);
  Constant *transformInitializer(GlobalVariable *global,
                                 SmallVectorImpl<Constant*> &pathToInitializer,
                                 Constant *initializer,
                                 const UseSite &usesToTransform);
  void transformInstructionOperands(Instruction *user,
                                    const UseSite &usesToTransform);
  Value *emitTransformedConstant(IRBuilderTy &builder, Constant *constant,
                                 const UseSite &usesToTransform);
  IRBuilderTy &continueGlobalConstructor();

  bool transformCalls();
  bool transformCall(CallInst *call);
  bool transformInvoke(InvokeInst *call);
  bool transformPointerAuthCall(CallBase *oldCall,
                                const OperandBundleUse &bundle);

  Value *emitSign(IRBuilderTy &builder, Value *pointer,
                  Value *key, Value *discriminator);
  Value *emitResign(IRBuilderTy &builder, Value *pointer,
                    Value *oldKey, Value *oldDiscriminator,
                    Value *newKey, Value *newDiscriminator);
  Value *emitAuth(IRBuilderTy &builder, Value *pointer,
                  Value *key, Value *discriminator);
  Value *emitStrip(IRBuilderTy &builder, Value *pointer, Value *key);
  Value *emitBlend(IRBuilderTy &builder, Value *primary, Value *secondary);
  Value *emitSignGeneric(IRBuilderTy &builder,
                         Value *value, Value *discriminator);

  /// Check whether the callee of a call has the right prototype.
  bool hasExpectedPrototype(CallBase *call, TypeTag resultTypeTag,
                            ArrayRef<TypeTag> argTypeTags) {
    if (!hasType(call, resultTypeTag))
      return false;

    if (call->arg_size() != argTypeTags.size())
      return false;
    for (unsigned i = 0, e = argTypeTags.size(); i != e; ++i) {
      if (!hasType(call->getArgOperand(i), argTypeTags[i]))
        return false;
    }
    return true;
  }

  /// Does the given value have its expected type?
  bool hasType(Value *value, TypeTag tag) {
    auto type = value->getType();
    switch (tag) {
    case VoidPtr:
      return type == PointerType::getUnqual(M->getContext());
    case Key:
      return type->isIntegerTy(32);
    case IntPtr:
      return type->isIntegerTy(M->getDataLayout().getPointerSizeInBits(0));
    }
    llvm_unreachable("unexpected type tag");
  }
  /// Fetch an expected type.
  Type *getType(TypeTag tag) {
    switch (tag) {
    case VoidPtr: return PointerType::getUnqual(M->getContext());
    case Key: return Type::getInt32Ty(M->getContext());
    case IntPtr: return Type::getIntNTy(M->getContext(),
                                  M->getDataLayout().getPointerSizeInBits(0));
    }
    llvm_unreachable("unexpected type tag");
  }

  ConstantInt *getInt32(unsigned value) {
    return ConstantInt::get(Type::getInt32Ty(M->getContext()), value);
  }

  /// Create a declaration for the given runtime function.
  FunctionCallee getOrInsertFunction(StringRef name, TypeTag resultTypeTag,
                                     ArrayRef<TypeTag> argTypeTags) {
    auto resultType = getType(resultTypeTag);
    SmallVector<Type*, 4> argTypes;
    for (auto argTypeTag : argTypeTags)
      argTypes.push_back(getType(argTypeTag));
    auto functionType = FunctionType::get(resultType, argTypes, false);
    return M->getOrInsertFunction(name, functionType);
  }

  FunctionCallee getSignPointerFn() {
    if (!SignPointerFn)
      SignPointerFn = getOrInsertFunction("__ptrauth_sign", VoidPtr,
                                          { VoidPtr, Key, Discriminator });
    return SignPointerFn;
  }

  FunctionCallee getAuthPointerFn() {
    if (!AuthPointerFn)
      AuthPointerFn = getOrInsertFunction("__ptrauth_auth", VoidPtr,
                                          { VoidPtr, Key, Discriminator });
    return AuthPointerFn;
  }

  FunctionCallee getStripPointerFn() {
    if (!StripPointerFn)
      StripPointerFn = getOrInsertFunction("__ptrauth_strip", VoidPtr,
                                           { VoidPtr, Key });
    return StripPointerFn;
  }

  FunctionCallee getBlendDiscriminatorFn() {
    if (!BlendDiscriminatorFn)
      BlendDiscriminatorFn = getOrInsertFunction("__ptrauth_blend",
                                                 Discriminator,
                                          { Discriminator, Discriminator });
    return BlendDiscriminatorFn;
  }

  FunctionCallee getSignGenericFn() {
    if (!SignGenericFn)
      SignGenericFn = getOrInsertFunction("__ptrauth_sign_generic", IntPtr,
                                          { IntPtr, Key, Discriminator });
    return SignGenericFn;
  }
};

} // end anonymous namespace

bool SoftPointerAuth::runOnModule(Module &M) {
  assert(!GlobalConstructorBuilder);

  // Reset any existing caches.
  SignPointerFn = nullptr;
  AuthPointerFn = nullptr;
  StripPointerFn = nullptr;
  BlendDiscriminatorFn = nullptr;
  SignGenericFn = nullptr;

  this->M = &M;

  bool changed = false;

  // Transform all of the intrinsic calls and operand bundles.
  // Doing this before transforming the relocations doesn't deeply matter,
  // but this pass has to walk all the functions and the relocation pass is
  // based on use lists, so this order minimizes redundant work.
  changed |= transformCalls();

  // Next, transform all the uses of relocations.
  changed |= transformRelocations();

  return changed;
}

/*****************************************************************************/
/********************************** Common ***********************************/
/*****************************************************************************/

Value *SoftPointerAuth::emitSign(IRBuilderTy &builder, Value *pointer,
                                 Value *key, Value *discriminator) {
  auto call = builder.CreateCall(getSignPointerFn(),
                                 { pointer, key, discriminator });
  call->setDoesNotThrow();
  return call;
}

Value *SoftPointerAuth::emitResign(IRBuilderTy &builder, Value *pointer,
                                   Value *oldKey, Value *oldDiscriminator,
                                   Value *newKey, Value *newDiscriminator) {
  // This is not an unattackable code pattern, but we don't emit one for
  // call operand bundles, either.
  auto rawValue = emitAuth(builder, pointer, oldKey, oldDiscriminator);
  return emitSign(builder, rawValue, newKey, newDiscriminator);
}

Value *SoftPointerAuth::emitAuth(IRBuilderTy &builder, Value *pointer,
                                 Value *key, Value *discriminator) {
  auto call = builder.CreateCall(getAuthPointerFn(),
                                 { pointer, key, discriminator });
  call->setDoesNotThrow();
  return call;
}

Value *SoftPointerAuth::emitStrip(IRBuilderTy &builder, Value *pointer,
                                  Value *key) {
  auto call = builder.CreateCall(getStripPointerFn(),
                                 { pointer, key });
  call->setDoesNotThrow();
  return call;
}

Value *SoftPointerAuth::emitBlend(IRBuilderTy &builder, Value *primary,
                                  Value *secondary) {
  auto call = builder.CreateCall(getBlendDiscriminatorFn(),
                                 { primary, secondary });
  call->setDoesNotThrow();
  return call;
}

Value *SoftPointerAuth::emitSignGeneric(IRBuilderTy &builder, Value *value,
                                        Value *discriminator) {
  auto call = builder.CreateCall(getSignGenericFn(),
                                 { value, discriminator });
  call->setDoesNotThrow();
  return call;
}

bool SoftPointerAuth::isPointerAuthRelocation(GlobalVariable *global) {
  // After checking the name, validate the type.
  if (global->getSection() == "llvm.ptrauth") {
    if (auto init = dyn_cast_or_null<ConstantStruct>(
                                                  global->getInitializer())) {
      return (init->getNumOperands() == 4 &&
              hasType(init->getOperand(0), VoidPtr) &&
              hasType(init->getOperand(1), Key) &&
              hasType(init->getOperand(2), Discriminator) &&
              hasType(init->getOperand(3), Discriminator));
    }
  }

  return false;
}

/*****************************************************************************/
/******************************** Relocations ********************************/
/*****************************************************************************/

/// Find all the top-level uses of a constant (i.e. the uses that are not
/// ConstantAggregates or ConstantExprs) and call the given callback
/// function on them.
template <class Fn>
static void findTopLevelUsesOfConstant(Constant *constant, const UsePath *path,
                                       const Fn &callback) {
  for (auto i = constant->use_begin(), e = constant->use_end(); i != e; ++i) {
    UsePath userPath = { path, i->getOperandNo() };
    auto user = i->getUser();

    // If the user is a global variable, there's only one use we care about.
    if (isa<GlobalVariable>(user)) {
      assert(userPath.OperandIndex == 0 && "non-zero use index on global var");
      callback(user, path);

    // If the user is an instruction, remember the operand index.
    } else if (isa<Instruction>(user)) {
      callback(user, &userPath);

    // If the user is some other kind of context, recurse.
    } else if (auto userConstant = dyn_cast<Constant>(user)) {
      findTopLevelUsesOfConstant(userConstant, &userPath, callback);
    }

    // TODO: metadata uses?
  }
}

bool SoftPointerAuth::transformRelocations() {
  SmallVector<GlobalVariable *, 16> relocations;
  SmallVector<User*, 16> rootUsers;
  DenseMap<User*, UseSite> useSites;

  // Walk all the globals looking for relocations.
  for (auto &global : M->globals()) {
    if (!isPointerAuthRelocation(&global))
      continue;

    // Remember this relocation.
    relocations.push_back(&global);

    // Remember all the top-level uses of the relocation, together with
    // paths down to the use.
    findTopLevelUsesOfConstant(&global, nullptr,
        [&](User *user, const UsePath *path) {
      // Look up an entry in the users map, adding one if necessary.
      // We remember the order in which we encountered things to avoid
      // non-deterministically walking over a DenseMap.  This still leaves
      // us vulnerable to use-list ordering, but that's harder to avoid.
      auto result = useSites.try_emplace(user);
      if (result.second) rootUsers.push_back(user);

      // Fill out the path down to the use.
      UseSite *site = &result.first->second;
      for (; path; path = path->Next) {
        site = &site->Children[path->OperandIndex];
      }
      (void) site;
    });
  }

  // Bail out if we didn't find any uses.
  if (relocations.empty())
    return false;

  // Rewrite all the root users.
  for (auto user : rootUsers) {
    const auto &uses = useSites.find(user)->second;
    if (auto global = dyn_cast<GlobalVariable>(user)) {
      transformGlobalInitializer(global, uses);
    } else {
      transformInstructionOperands(cast<Instruction>(user), uses);
    }
  }

  // Destroy all the relocations.
  for (auto reloc : relocations) {
    reloc->replaceAllUsesWith(ConstantPointerNull::get(reloc->getType()));
    reloc->eraseFromParent();
  }

  // Finish the global initialization function if we started one.
  if (GlobalConstructorBuilder) {
    GlobalConstructorBuilder->CreateRetVoid();
    GlobalConstructorBuilder.reset();
  }

  return true;
}

/// Transform a global initializer that contains signing relocations.
void SoftPointerAuth::transformGlobalInitializer(GlobalVariable *global,
                                           const UseSite &usesToTransform) {
  auto oldInitializer = global->getInitializer();
  assert(oldInitializer && "global has no initializer?");

  // transformInitializer wants the indices of a GEP to the initializer
  // that it's transforming.  Seed that with a '0' to enter the global.
  SmallVector<Constant*, 4> pathToInitializer;
  pathToInitializer.push_back(getInt32(0));

  auto newInitializer = transformInitializer(global, pathToInitializer,
                                             oldInitializer, usesToTransform);

  assert(newInitializer != oldInitializer && "no changes?");
  assert(pathToInitializer.size() == 1 && "didn't balance push/pop");

  global->setInitializer(newInitializer);

  // Make the global mutable; our constant initializer will change it.
  global->setConstant(false);
}

/// Transform part of a global initializer that contains signing relocations.
Constant *SoftPointerAuth::transformInitializer(GlobalVariable *global,
                                SmallVectorImpl<Constant*> &pathToInitializer,
                                Constant *initializer,
                                const UseSite &usesToTransform) {
  auto aggregate = dyn_cast<ConstantAggregate>(initializer);

  // If the initializer is a simple reference to a relocation, or an
  // expression in terms of same, compute it in the global construction.
  if (!aggregate) {
    auto &builder = continueGlobalConstructor();

    // Compute the value.
    auto transformedInitializer =
      emitTransformedConstant(builder, initializer, usesToTransform);

    // Drill down to the current position.
    Constant *addr = global;
    if (pathToInitializer.size() != 1)
      addr = ConstantExpr::getInBoundsGetElementPtr(global->getValueType(),
                                                    addr, pathToInitializer);

    // Store the transformed vlaue to this position.
    builder.CreateStore(transformedInitializer, addr);

    // Use a null value for the global position.
    return Constant::getNullValue(initializer->getType());
  }

  // Otherwise, the initializer is a constant aggregate.  Recurse into it
  // at the appropriate positions.  The goal here is to avoid emitting the
  // entire aggregate with stores.
  assert(!usesToTransform.Children.empty()
         && "walking into wrong initializer?");

  // Copy the original elements.
  SmallVector<Constant*, 16> elts;
  elts.reserve(aggregate->getNumOperands());
  for (auto &op : aggregate->operands())
    elts.push_back(cast<Constant>(&*op));

  // Modify just the elements that we decided to modify.
  for (const auto &eltIndexAndUses : usesToTransform.Children) {
    auto eltIndex = eltIndexAndUses.first;

    // Add an index to the GEP down to this position.
    pathToInitializer.push_back(getInt32(eltIndex));

    // Rewrite the element.
    elts[eltIndex] = transformInitializer(global, pathToInitializer,
                                     elts[eltIndex], eltIndexAndUses.second);

    // Pop the previously pushed path element.
    pathToInitializer.pop_back();
  }

  // Rebuild the aggregate.
  auto type = aggregate->getType();
  if (auto structType = dyn_cast<StructType>(type)) {
    return ConstantStruct::get(structType, elts);
  } else if (auto arrayType = dyn_cast<ArrayType>(type)) {
    return ConstantArray::get(arrayType, elts);
  } else {
    return ConstantVector::get(elts);
  }
}

/// Continue emitting the global constructor function.
IRBuilderTy &SoftPointerAuth::continueGlobalConstructor() {
  // Create the global initialization function if we haven't yet.
  if (!GlobalConstructorBuilder) {
    auto &context = M->getContext();

    // Create the function.
    auto fnType = FunctionType::get(Type::getVoidTy(context),
                                    {}, false);
    Function *fn = Function::Create(fnType, Function::PrivateLinkage,
                                    "ptrauth_soft_init", M);

    // Add the function to the global initializers list.
    appendToGlobalCtors(*M, fn, 0);

    auto entryBB = BasicBlock::Create(context, "", fn);

    GlobalConstructorBuilder.emplace(entryBB);
  }
  return *GlobalConstructorBuilder;
}

void SoftPointerAuth::transformInstructionOperands(Instruction *user,
                                              const UseSite &usesToTransform) {
  assert(!usesToTransform.Children.empty()
         && "no uses to transform for instruction");

  // Handle PHIs differently because we have to insert code into the
  // right predecessor(s).
  if (auto phi = dyn_cast<PHINode>(user)) {
    for (auto &useEntry : usesToTransform.Children) {
      auto operandIndex = useEntry.first;
      auto operand = cast<Constant>(phi->getOperand(operandIndex));

      // Figure out the block this edge corresponds to.
      auto incomingValueIndex =
        PHINode::getIncomingValueNumForOperand(operandIndex);
      auto incomingBlock = phi->getIncomingBlock(incomingValueIndex);

      // Split the edge if necessary & possible.
      // Note that we don't want to change anything structurally about 'phi'.
      auto newBlock = SplitCriticalEdge(incomingBlock, phi->getParent(),
                                        CriticalEdgeSplittingOptions()
                                          .setKeepOneInputPHIs());

      // Start inserting before the terminator in the new block.
      // If a critical edge was unsplittable, this will insert the code
      // unconditionally in the origin block, which is unfortunate but
      // acceptable because sign operations cannot fail.
      auto blockToInsertInto = newBlock ? newBlock : incomingBlock;
      IRBuilderTy builder(blockToInsertInto->getTerminator());

      // Transform the value.
      auto transformedOperand =
        emitTransformedConstant(builder, operand, useEntry.second);

      // Replace the incoming value.
      phi->setIncomingValue(incomingValueIndex, transformedOperand);
    }

    return;
  }

  // Otherwise, emit immediately before the user.
  IRBuilderTy builder(user);
  for (auto &useEntry : usesToTransform.Children) {
    auto operandIndex = useEntry.first;
    auto operand = cast<Constant>(user->getOperand(operandIndex));

    auto transformedOperand =
      emitTransformedConstant(builder, operand, useEntry.second);

    // Replace the incoming value.
    user->setOperand(operandIndex, transformedOperand);
  }
}


Value *SoftPointerAuth::emitTransformedConstant(IRBuilderTy &builder,
                                                Constant *constant,
                                              const UseSite &usesToTransform) {
  // If it's a direct reference to the relocation, we're done.
  if (auto global = dyn_cast<GlobalVariable>(constant)) {
    assert(isPointerAuthRelocation(global));
    assert(usesToTransform.Children.empty() &&
           "child uses of direct relocation reference?");

    // Decompose the relocation.
    ConstantStruct *init = cast<ConstantStruct>(global->getInitializer());
    auto pointer = init->getOperand(0);
    auto key = init->getOperand(1);
    auto primaryDiscriminator = init->getOperand(2);
    auto secondaryDiscriminator = init->getOperand(3);

    // Compute the discriminator.
    Value *discriminator;
    if (primaryDiscriminator->isNullValue()) {
      discriminator = secondaryDiscriminator;
    } else if (secondaryDiscriminator->isNullValue()) {
      discriminator = primaryDiscriminator;
    } else {
      discriminator = emitBlend(builder, primaryDiscriminator,
                                secondaryDiscriminator);
    }

    // Emit a sign operation.
    auto signedValue = emitSign(builder, pointer, key, discriminator);

    // Cast back to the signed pointer type.
    return builder.CreateBitCast(signedValue, global->getType());
  }

  // If it's a constant expression, make it an instruction and rebuild
  // its operands.
  if (auto expr = dyn_cast<ConstantExpr>(constant)) {
    assert(!usesToTransform.Children.empty() &&
           "direct use of constant expression?");

    auto instruction = expr->getAsInstruction();

    for (const auto &operandIndexAndUses : usesToTransform.Children) {
      auto operandIndex = operandIndexAndUses.first;

      auto newOperand =
        emitTransformedConstant(builder, expr->getOperand(operandIndex),
                                operandIndexAndUses.second);
      instruction->setOperand(operandIndex, newOperand);
    }

    builder.Insert(instruction);
    return instruction;
  }

  // Otherwise, it should be a constant aggregate.
  // Recursively emit the transformed elements.
  auto aggregate = cast<ConstantAggregate>(constant);
  assert(!usesToTransform.Children.empty() &&
         "direct use of whole constant aggregate?");

  SmallVector<Value*, 16> elts(aggregate->op_begin(), aggregate->op_end());

  // Transform all of the children we're supposed to transform.
  for (const auto &childUseEntry : usesToTransform.Children) {
    auto &elt = elts[childUseEntry.first];
    elt = emitTransformedConstant(builder, cast<Constant>(elt),
                                  childUseEntry.second);
  }

  // Build up the aggregate value using insertelement / insertvalue
  // as appropriate.
  auto type = aggregate->getType();
  bool isVector = isa<VectorType>(type);
  Value *transformedAggregate = UndefValue::get(type);
  for (unsigned i = 0, e = aggregate->getNumOperands(); i != e; ++i) {
    if (isVector)
      transformedAggregate =
        builder.CreateInsertElement(transformedAggregate, elts[i], i);
    else
      transformedAggregate =
        builder.CreateInsertValue(transformedAggregate, elts[i], i);
  }
  return transformedAggregate;
}

/*****************************************************************************/
/*********************** Intrinsics and Operand Bundles **********************/
/*****************************************************************************/

bool SoftPointerAuth::transformCalls() {
  bool changed = false;

  for (auto fi = M->begin(), fe = M->end(); fi != fe; ) {
    auto fn = &*fi;
    ++fi;

    // Soft return authentication is technically possible (even without backend
    // support) but not currently necessary.
    if (fn->hasFnAttribute("ptrauth-returns"))
      report_fatal_error("Soft. lowering of return address auth unsupported");

    for (auto bi = fn->begin(), be = fn->end(); bi != be; ) {
      auto bb = &*bi;
      ++bi;

      for (auto ii = bb->begin(), ie = bb->end(); ii != ie; ) {
        auto instruction = &*ii;
        ++ii;

        if (auto call = dyn_cast<CallInst>(instruction)) {
          changed |= transformCall(call);
        } else if (auto invoke = dyn_cast<InvokeInst>(instruction)) {
          changed |= transformInvoke(invoke);
        }
      }
    }
  }

  return changed;
}

bool SoftPointerAuth::transformCall(CallInst *call) {
  // Handle calls with the llvm.ptrauth operand bundle attached.
  if (auto bundle = call->getOperandBundle(LLVMContext::OB_ptrauth)) {
    return transformPointerAuthCall(call, *bundle);
  }

  // Otherwise, look for our intrinsics.
  auto callee = call->getCalledFunction();
  if (!callee) return false;
  auto intrinsicInst = dyn_cast<IntrinsicInst>(call);
  if (!intrinsicInst)
    return false;
  auto intrinsic = intrinsicInst->getIntrinsicID();
  auto rebuild = [&](function_ref<llvm::Value*(IRBuilderTy&)> fn) {
    IRBuilderTy builder(call);
    auto result = fn(builder);
    call->replaceAllUsesWith(result);
    call->eraseFromParent();
    return true;
  };

  switch (intrinsic) {
  case Intrinsic::ptrauth_sign:
    if (!hasExpectedPrototype(call, VoidPtr, {VoidPtr, Key, Discriminator}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitSign(builder, call->getArgOperand(0),
                      call->getArgOperand(1), call->getArgOperand(2));
    });

  case Intrinsic::ptrauth_resign:
    if (!hasExpectedPrototype(call, VoidPtr, {VoidPtr, Key, Discriminator,
                                              Key, Discriminator}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitResign(builder, call->getArgOperand(0),
                        call->getArgOperand(1), call->getArgOperand(2),
                        call->getArgOperand(3), call->getArgOperand(4));
    });

  case Intrinsic::ptrauth_auth:
    if (!hasExpectedPrototype(call, VoidPtr, {VoidPtr, Key, Discriminator}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitAuth(builder, call->getArgOperand(0),
                      call->getArgOperand(1), call->getArgOperand(2));
    });

  case Intrinsic::ptrauth_strip:
    if (!hasExpectedPrototype(call, VoidPtr, {VoidPtr, Key}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitStrip(builder, call->getArgOperand(0),
                       call->getArgOperand(1));
    });

  case Intrinsic::ptrauth_blend:
    if (!hasExpectedPrototype(call, Discriminator,
                              {Discriminator, Discriminator}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitBlend(builder, call->getArgOperand(0),
                       call->getArgOperand(1));
    });

  case Intrinsic::ptrauth_sign_generic:
    if (!hasExpectedPrototype(call, IntPtr, {IntPtr, IntPtr}))
      return false;
    return rebuild([&](IRBuilderTy &builder) {
      return emitSignGeneric(builder, call->getArgOperand(0),
                             call->getArgOperand(1));
    });

  default:
    break;
  }

  return false;
}

bool SoftPointerAuth::transformInvoke(InvokeInst *call) {
  // Handle invokes with the llvm.ptrauth operand bundle attached.
  if (auto bundle = call->getOperandBundle(LLVMContext::OB_ptrauth)) {
    return transformPointerAuthCall(call, *bundle);
  }

  return false;
}

bool SoftPointerAuth::transformPointerAuthCall(CallBase *oldCall,
                                               const OperandBundleUse &bundle) {
  if (bundle.Inputs.size() != 2 ||
      !hasType(bundle.Inputs[0], Key) ||
      !hasType(bundle.Inputs[1], Discriminator))
    return false;

  IRBuilderTy builder(oldCall);

  // Authenticate the callee.
  Value *oldCallee = oldCall->getCalledOperand();
  Value *callee = builder.CreateBitCast(oldCallee, getType(VoidPtr));
  callee = emitAuth(builder, callee, bundle.Inputs[0], bundle.Inputs[1]);
  callee = builder.CreateBitCast(callee, oldCallee->getType());

  // Get the arguments.
  SmallVector<Value*, 8> args(oldCall->arg_begin(), oldCall->arg_end());

  // Get the operand bundles besides llvm.ptrauth (probably none).
  SmallVector<OperandBundleDef, 1> opBundles;
  for (unsigned i = 0, e = oldCall->getNumOperandBundles(); i != e; ++i) {
    auto bundle = oldCall->getOperandBundleAt(i);
    if (bundle.getTagID() != LLVMContext::OB_ptrauth) {
      opBundles.emplace_back(bundle);
    }
  }

  // Build the new instruction.
  CallBase *newCall;
  if (auto *oldInvoke = dyn_cast<InvokeInst>(oldCall)) {
    newCall = builder.CreateInvoke(oldInvoke->getFunctionType(), callee,
                                   oldInvoke->getNormalDest(),
                                   oldInvoke->getUnwindDest(),
                                   args, opBundles);
  } else {
    newCall =
        builder.CreateCall(oldCall->getFunctionType(), callee, args, opBundles);
  }

  // Copy mandatory attributes.
  newCall->setCallingConv(oldCall->getCallingConv());
  newCall->setAttributes(oldCall->getAttributes());

  // TODO: copy metadata?
  newCall->takeName(oldCall);

  // Destroy the old call.
  oldCall->replaceAllUsesWith(newCall);
  oldCall->eraseFromParent();

  return true;
}

/*****************************************************************************/
/**************************** Pass Manager Support ***************************/
/*****************************************************************************/

PreservedAnalyses SoftPointerAuthPass::run(Module &M,
                                           ModuleAnalysisManager &AM) {
  SoftPointerAuth Pass;
  if (!Pass.runOnModule(M))
    return PreservedAnalyses::all();
  return PreservedAnalyses::none();
}