Skip to content

Commit a6c1fda

Browse files
committed
[CodeGen][KCFI] Introduce llvm::getKCFITypeID to wrap hash implementation
KCFI generates hashes in two places. Instead of exposing the hash implementation in both places, introduce a helper that wraps the specific hash implementation in a single place. Signed-off-by: Kees Cook <[email protected]>
1 parent c4bbb04 commit a6c1fda

File tree

6 files changed

+56
-9
lines changed

6 files changed

+56
-9
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
#include "llvm/Support/ConvertUTF.h"
6868
#include "llvm/Support/ErrorHandling.h"
6969
#include "llvm/Support/TimeProfiler.h"
70-
#include "llvm/Support/xxhash.h"
7170
#include "llvm/TargetParser/RISCVISAInfo.h"
7271
#include "llvm/TargetParser/Triple.h"
7372
#include "llvm/TargetParser/X86TargetParser.h"
73+
#include "llvm/Transforms/Instrumentation/KCFI.h"
7474
#include "llvm/Transforms/Utils/BuildLibCalls.h"
7575
#include <optional>
7676
#include <set>
@@ -2434,8 +2434,7 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T, StringRef Salt) {
24342434
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
24352435
Out << ".generalized";
24362436

2437-
return llvm::ConstantInt::get(Int32Ty,
2438-
static_cast<uint32_t>(llvm::xxHash64(OutName)));
2437+
return llvm::ConstantInt::get(Int32Ty, llvm::getKCFITypeID(OutName));
24392438
}
24402439

24412440
void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD,

llvm/include/llvm/Support/Hash.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- llvm/Support/Hash.h - Hash functions --------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file provides hash functions.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_SUPPORT_HASH_H
14+
#define LLVM_SUPPORT_HASH_H
15+
16+
#include "llvm/ADT/StringRef.h"
17+
#include <cstdint>
18+
19+
namespace llvm {
20+
21+
/// Compute KCFI type ID from mangled type name using FNV-1a hash.
22+
uint32_t getKCFITypeID(StringRef MangledTypeName);
23+
24+
} // end namespace llvm
25+
26+
#endif // LLVM_SUPPORT_HASH_H

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ add_llvm_component_library(LLVMSupport
202202
FormatVariadic.cpp
203203
GlobPattern.cpp
204204
GraphWriter.cpp
205+
Hash.cpp
205206
HexagonAttributeParser.cpp
206207
HexagonAttributes.cpp
207208
InitLLVM.cpp

llvm/lib/Support/Hash.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- Hash.cpp - Hash functions ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements hash functions.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/Support/Hash.h"
14+
#include "llvm/Support/xxhash.h"
15+
16+
using namespace llvm;
17+
18+
uint32_t llvm::getKCFITypeID(StringRef MangledTypeName) {
19+
return static_cast<uint32_t>(xxHash64(MangledTypeName));
20+
}

llvm/lib/Transforms/Instrumentation/KCFI.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/IR/Intrinsics.h"
2424
#include "llvm/IR/MDBuilder.h"
2525
#include "llvm/IR/Module.h"
26+
#include "llvm/Support/xxhash.h"
2627
#include "llvm/Target/TargetMachine.h"
2728
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
2829

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Transforms/Utils/ModuleUtils.h"
14-
#include "llvm/Analysis/VectorUtils.h"
1514
#include "llvm/ADT/SmallString.h"
15+
#include "llvm/Analysis/VectorUtils.h"
1616
#include "llvm/IR/DerivedTypes.h"
1717
#include "llvm/IR/Function.h"
1818
#include "llvm/IR/IRBuilder.h"
@@ -21,7 +21,7 @@
2121
#include "llvm/Support/Casting.h"
2222
#include "llvm/Support/MD5.h"
2323
#include "llvm/Support/raw_ostream.h"
24-
#include "llvm/Support/xxhash.h"
24+
#include "llvm/Transforms/Instrumentation/KCFI.h"
2525

2626
using namespace llvm;
2727

@@ -208,10 +208,10 @@ void llvm::setKCFIType(Module &M, Function &F, StringRef MangledType) {
208208
std::string Type = MangledType.str();
209209
if (M.getModuleFlag("cfi-normalize-integers"))
210210
Type += ".normalized";
211-
F.setMetadata(LLVMContext::MD_kcfi_type,
212-
MDNode::get(Ctx, MDB.createConstant(ConstantInt::get(
213-
Type::getInt32Ty(Ctx),
214-
static_cast<uint32_t>(xxHash64(Type))))));
211+
F.setMetadata(
212+
LLVMContext::MD_kcfi_type,
213+
MDNode::get(Ctx, MDB.createConstant(ConstantInt::get(
214+
Type::getInt32Ty(Ctx), getKCFITypeID(Type)))));
215215
// If the module was compiled with -fpatchable-function-entry, ensure
216216
// we use the same patchable-function-prefix.
217217
if (auto *MD = mdconst::extract_or_null<ConstantInt>(

0 commit comments

Comments
 (0)