<commit_before>//===--- GenPointerAuth.cpp - IRGen for pointer authentication ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements general support for pointer authentication in Swift.
//
//===----------------------------------------------------------------------===//

#include "GenPointerAuth.h"
#include "Callee.h"
#include "ConstantBuilder.h"
#include "GenType.h"
#include "IRGenFunction.h"
#include "IRGenMangler.h"
#include "IRGenModule.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/SIL/TypeLowering.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "llvm/ADT/APInt.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
using namespace irgen;

/**************************** INTRINSIC OPERATIONS ****************************/

/// Return the key and discriminator value for the given auth info,
/// as demanded by the ptrauth intrinsics.
static std::pair<llvm::Constant*, llvm::Value*>
getPointerAuthPair(IRGenFunction &IGF, const PointerAuthInfo &authInfo) {
  auto key = llvm::ConstantInt::get(IGF.IGM.Int32Ty, authInfo.getKey());
  llvm::Value *discriminator = authInfo.getDiscriminator();
  if (discriminator->getType()->isPointerTy()) {
    discriminator = IGF.Builder.CreatePtrToInt(discriminator, IGF.IGM.IntPtrTy);
  }
  return { key, discriminator };
}

llvm::Value *irgen::emitPointerAuthBlend(IRGenFunction &IGF,
                                         llvm::Value *address,
                                         llvm::Value *other) {
  address = IGF.Builder.CreatePtrToInt(address, IGF.IGM.IntPtrTy);
  auto intrinsic =
    llvm::Intrinsic::getDeclaration(&IGF.IGM.Module,
                                    llvm::Intrinsic::ptrauth_blend,
                                    { IGF.IGM.IntPtrTy });
  return IGF.Builder.CreateCall(intrinsic, {address, other});
}

llvm::Value *irgen::emitPointerAuthStrip(IRGenFunction &IGF,
                                          llvm::Value *fnPtr,
                                          unsigned Key) {
  auto fnVal = IGF.Builder.CreatePtrToInt(fnPtr, IGF.IGM.IntPtrTy);
  auto keyArg = llvm::ConstantInt::get(IGF.IGM.Int32Ty, Key);
  auto intrinsic =
    llvm::Intrinsic::getDeclaration(&IGF.IGM.Module,
                                    llvm::Intrinsic::ptrauth_strip,
                                    { IGF.IGM.IntPtrTy });
  auto strippedPtr = IGF.Builder.CreateCall(intrinsic, {fnVal, keyArg});
  return IGF.Builder.CreateIntToPtr(strippedPtr, fnPtr->getType());
}

FunctionPointer irgen::emitPointerAuthResign(IRGenFunction &IGF,
                                             const FunctionPointer &fn,
                                          const PointerAuthInfo &newAuthInfo) {
  // TODO: Handle resigning AsyncFunctionPointers.
  assert(fn.getKind().value == FunctionPointer::KindTy::Value::Function);
  llvm::Value *fnPtr = emitPointerAuthResign(IGF, fn.getPointer(IGF),
                                             fn.getAuthInfo(), newAuthInfo);
  return FunctionPointer(fn.getKind(), fnPtr, newAuthInfo, fn.getSignature());
}

llvm::Value *irgen::emitPointerAuthResign(IRGenFunction &IGF,<commit_msg>[Async CC] Resigning AFP results in AFP.<commit_after>//===--- GenPointerAuth.cpp - IRGen for pointer authentication ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
//  This file implements general support for pointer authentication in Swift.
//
//===----------------------------------------------------------------------===//

#include "GenPointerAuth.h"
#include "Callee.h"
#include "ConstantBuilder.h"
#include "GenType.h"
#include "IRGenFunction.h"
#include "IRGenMangler.h"
#include "IRGenModule.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/SIL/TypeLowering.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "llvm/ADT/APInt.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
using namespace irgen;

/**************************** INTRINSIC OPERATIONS ****************************/

/// Return the key and discriminator value for the given auth info,
/// as demanded by the ptrauth intrinsics.
static std::pair<llvm::Constant*, llvm::Value*>
getPointerAuthPair(IRGenFunction &IGF, const PointerAuthInfo &authInfo) {
  auto key = llvm::ConstantInt::get(IGF.IGM.Int32Ty, authInfo.getKey());
  llvm::Value *discriminator = authInfo.getDiscriminator();
  if (discriminator->getType()->isPointerTy()) {
    discriminator = IGF.Builder.CreatePtrToInt(discriminator, IGF.IGM.IntPtrTy);
  }
  return { key, discriminator };
}

llvm::Value *irgen::emitPointerAuthBlend(IRGenFunction &IGF,
                                         llvm::Value *address,
                                         llvm::Value *other) {
  address = IGF.Builder.CreatePtrToInt(address, IGF.IGM.IntPtrTy);
  auto intrinsic =
    llvm::Intrinsic::getDeclaration(&IGF.IGM.Module,
                                    llvm::Intrinsic::ptrauth_blend,
                                    { IGF.IGM.IntPtrTy });
  return IGF.Builder.CreateCall(intrinsic, {address, other});
}

llvm::Value *irgen::emitPointerAuthStrip(IRGenFunction &IGF,
                                          llvm::Value *fnPtr,
                                          unsigned Key) {
  auto fnVal = IGF.Builder.CreatePtrToInt(fnPtr, IGF.IGM.IntPtrTy);
  auto keyArg = llvm::ConstantInt::get(IGF.IGM.Int32Ty, Key);
  auto intrinsic =
    llvm::Intrinsic::getDeclaration(&IGF.IGM.Module,
                                    llvm::Intrinsic::ptrauth_strip,
                                    { IGF.IGM.IntPtrTy });
  auto strippedPtr = IGF.Builder.CreateCall(intrinsic, {fnVal, keyArg});
  return IGF.Builder.CreateIntToPtr(strippedPtr, fnPtr->getType());
}

FunctionPointer irgen::emitPointerAuthResign(IRGenFunction &IGF,
                                             const FunctionPointer &fn,
                                          const PointerAuthInfo &newAuthInfo) {
  llvm::Value *fnPtr = emitPointerAuthResign(IGF, fn.getRawPointer(),
                                             fn.getAuthInfo(), newAuthInfo);
  return FunctionPointer(fn.getKind(), fnPtr, newAuthInfo, fn.getSignature());
}

llvm::Value *irgen::emitPointerAuthResign(IRGenFunction &IGF,
{'fraction_non_alphanumeric': 0.17591533180778032, 'fraction_numerical': 0.004004576659038902, 'mean_word_length': 2.386440677966102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3488969', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1880, 'n_words': 417}