Skip to content

Commit 4b883f9

Browse files
committed
comments
1 parent 0dc3894 commit 4b883f9

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

include/pcc.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
//===-- pcc.h - Probabilistic Calling Context runtime ---------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// This file can be used to query the current calling context.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
115
#include <stdint.h>
216

17+
/// Query the current calling context.
318
uintptr_t __pcc_get();

pcc/PCC.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
//===-- PCC.cpp - Probabilistic Calling Context ---------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// This file contains an implementation of Michael Bond's "probabilistic
12+
/// calling context" (PCC) for maintaining runtime calling context information.
13+
///
14+
//===----------------------------------------------------------------------===//
15+
116
#include <stdint.h>
217
#include <stdlib.h>
318

@@ -15,8 +30,10 @@ using namespace llvm;
1530
namespace {
1631

1732
static const char *const PCCCalculateName = "__pcc_calculate";
18-
static const char *const PCCVName = "__pcc_V";
33+
static const char *const PCCVarName = "__pcc_V";
1934

35+
/// ProbabilisticCallingContext: instrument the code in a module to maintain a
36+
/// probabilistic unique value representing the current calling context.
2037
class ProbabilisticCallingContext : public ModulePass {
2138
public:
2239
static char ID;
@@ -27,7 +44,8 @@ class ProbabilisticCallingContext : public ModulePass {
2744

2845
} // namespace
2946

30-
static Function *getPCCFunction(Constant *FuncOrBitcast) {
47+
// Adapted from llvm::checkSanitizerInterfaceFunction
48+
static Function *checkPCCInterfaceFunction(Constant *FuncOrBitcast) {
3149
if (isa<Function>(FuncOrBitcast)) {
3250
return cast<Function>(FuncOrBitcast);
3351
}
@@ -46,29 +64,38 @@ bool ProbabilisticCallingContext::runOnModule(Module &M) {
4664
LLVMContext &C = M.getContext();
4765
const DataLayout &DL = M.getDataLayout();
4866

67+
// Either 32 or 64 bit depending on the target
4968
IntegerType *IntTy = DL.getIntPtrType(C);
5069

51-
Function *PCCFunc = getPCCFunction(
70+
// PCCCalculate is the function `__pcc_calculate` that (funny enough)
71+
// calculates the probabilistic calling context
72+
Function *PCCCalculate = checkPCCInterfaceFunction(
5273
M.getOrInsertFunction(PCCCalculateName, IntTy, IntTy, IntTy));
74+
// PCCVar is the variable `__pcc_V` that stores the probabilistic calling
75+
// context
5376
GlobalVariable *PCCVar = new GlobalVariable(
54-
M, IntTy, false, GlobalValue::ExternalLinkage, nullptr, PCCVName, nullptr,
55-
GlobalVariable::GeneralDynamicTLSModel, 0, false);
77+
M, IntTy, false, GlobalValue::ExternalLinkage, nullptr, PCCVarName,
78+
nullptr, GlobalVariable::GeneralDynamicTLSModel, 0, false);
5679

5780
for (auto &F : M.functions()) {
81+
// We can only instrument functions that we have an implementation of
5882
if (F.isDeclaration()) {
5983
continue;
6084
}
6185

86+
// Load the current PCC value into a local variable (temp)
6287
BasicBlock::iterator IP = F.getEntryBlock().getFirstInsertionPt();
6388
IRBuilder<> EntryIRB(&*IP);
6489
LoadInst *Temp = EntryIRB.CreateLoad(PCCVar);
6590

6691
for (auto I = inst_begin(F); I != inst_end(F); ++I) {
92+
// At each call site, compute the next calling context with `f` and
93+
// update the global variable `V`
6794
if (auto *Call = dyn_cast<CallInst>(&*I)) {
6895
ConstantInt *CS = ConstantInt::get(IntTy, random());
6996

7097
IRBuilder<> CallSiteIRB(Call);
71-
CallInst *CallF = CallSiteIRB.CreateCall(PCCFunc, {Temp, CS});
98+
CallInst *CallF = CallSiteIRB.CreateCall(PCCCalculate, {Temp, CS});
7299
CallSiteIRB.CreateStore(CallF, PCCVar);
73100
}
74101
}

pcc/pcc-rt.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1+
//===-- pcc-rt.cpp - Probabilistic Calling Context runtime ----------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// This file contains the run time component used to calculate and maintain
12+
/// the probabilsitic calling context (PCC).
13+
///
14+
//===----------------------------------------------------------------------===//
15+
116
#include "pcc.h"
217

18+
/// Unique value representing the current calling context.
319
__thread uintptr_t __pcc_V = 0;
420

21+
/// Non-commutative but efficiently composable function to compute PCC values.
22+
///
23+
/// This corresponds to the function \p f in Mike Bond's paper.
24+
///
25+
/// \param V current calling context value
26+
/// \param cs call site identifier
27+
///
28+
/// \returns the updated calling context value
529
uintptr_t __pcc_calculate(uintptr_t V, uintptr_t cs) { return 3 * V + cs; }
630

731
uintptr_t __pcc_get() { return __pcc_V; }

0 commit comments

Comments
 (0)