Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions clang/test/CodeGen/cheri/fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ extern int fail2(void);

// These should all be in AS200:
// PURECAP: @__sancov_lowest_stack = external thread_local(initialexec) addrspace(200) global i64
// PURECAP: @__sancov_gen_ = private addrspace(200) global [1 x i8] zeroinitializer, section "__sancov_cntrs", comdat($main), align 1{{$}}
// PURECAP: @__sancov_gen_.1 = private addrspace(200) constant [2 x i64] [i64 ptrtoint (ptr addrspace(200) @main to i64), i64 1], section "__sancov_pcs", comdat($main), align 8{{$}}
// PURECAP: @__sancov_gen_.2 = private addrspace(200) global [4 x i8] zeroinitializer, section "__sancov_cntrs", comdat($func2), align 1{{$}}
// PURECAP: @__sancov_gen_.3 = private addrspace(200) constant [8 x i64] [
// PURECAP: @__sancov_gen_.[[UID:.*]] = private addrspace(200) global [1 x i8] zeroinitializer, section "__sancov_cntrs", comdat($main), align 1{{$}}
// PURECAP: @__sancov_gen_.[[UID]].1 = private addrspace(200) constant [2 x i64] [i64 ptrtoint (ptr addrspace(200) @main to i64), i64 1], section "__sancov_pcs", comdat($main), align 8{{$}}
// PURECAP: @__sancov_gen_.[[UID]].2 = private addrspace(200) global [4 x i8] zeroinitializer, section "__sancov_cntrs", comdat($func2), align 1{{$}}
// PURECAP: @__sancov_gen_.[[UID]].3 = private addrspace(200) constant [8 x i64] [
// PURECAP-SAME: i64 ptrtoint (ptr addrspace(200) @func2 to i64), i64 1,
// PURECAP-SAME: i64 ptrtoint (ptr addrspace(200) blockaddress(@func2, %if.then) to i64), i64 0,
// PURECAP-SAME: i64 ptrtoint (ptr addrspace(200) blockaddress(@func2, %if.then2) to i64), i64 0,
Expand All @@ -44,7 +44,7 @@ extern int fail2(void);
// PURECAP: @llvm.global_ctors = appending addrspace(200) global [1 x { i32, ptr addrspace(200), ptr addrspace(200) }] [{ i32, ptr addrspace(200), ptr addrspace(200) } { i32 2, ptr addrspace(200) @sancov.module_ctor_8bit_counters, ptr addrspace(200) @sancov.module_ctor_8bit_counters }]
// PURECAP: @__start___sancov_pcs = extern_weak hidden addrspace(200) global i64
// PURECAP: @__stop___sancov_pcs = extern_weak hidden addrspace(200) global i64
// PURECAP: @llvm.compiler.used = appending addrspace(200) global [4 x ptr] [ptr addrspacecast (ptr addrspace(200) @__sancov_gen_ to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.1 to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.2 to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.3 to ptr)], section "llvm.metadata"
// PURECAP: @llvm.compiler.used = appending addrspace(200) global [4 x ptr] [ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]] to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]].1 to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]].2 to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]].3 to ptr)], section "llvm.metadata"

// UTC_ARGS: --enable

Expand All @@ -65,9 +65,9 @@ extern int fail2(void);
// PURECAP-SAME: () addrspace(200) #[[ATTR0:[0-9]+]] comdat {
// PURECAP-NEXT: entry:
// PURECAP-NEXT: [[FOO:%.*]] = alloca [10 x i8], align 1, addrspace(200)
// PURECAP-NEXT: [[TMP0:%.*]] = load i8, ptr addrspace(200) @__sancov_gen_, align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP0:%.*]] = load i8, ptr addrspace(200) @__sancov_gen_.[[UID]], align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP1:%.*]] = add i8 [[TMP0]], 1
// PURECAP-NEXT: store i8 [[TMP1]], ptr addrspace(200) @__sancov_gen_, align 1, !nosanitize !5
// PURECAP-NEXT: store i8 [[TMP1]], ptr addrspace(200) @__sancov_gen_.[[UID]], align 1, !nosanitize !5
// PURECAP-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [10 x i8], ptr addrspace(200) [[FOO]], i64 0, i64 0
// PURECAP-NEXT: [[CALL:%.*]] = call ptr addrspace(200) @gets(ptr addrspace(200) noundef [[ARRAYDECAY]]) #[[ATTR3:[0-9]+]]
// PURECAP-NEXT: [[ARRAYDECAY1:%.*]] = getelementptr inbounds [10 x i8], ptr addrspace(200) [[FOO]], i64 0, i64 0
Expand Down Expand Up @@ -131,18 +131,18 @@ int main(void) {
// PURECAP-NEXT: entry:
// PURECAP-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(200)
// PURECAP-NEXT: [[I_ADDR:%.*]] = alloca i32, align 4, addrspace(200)
// PURECAP-NEXT: [[TMP0:%.*]] = load i8, ptr addrspace(200) @__sancov_gen_.2, align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP0:%.*]] = load i8, ptr addrspace(200) @__sancov_gen_.[[UID]].2, align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP1:%.*]] = add i8 [[TMP0]], 1
// PURECAP-NEXT: store i8 [[TMP1]], ptr addrspace(200) @__sancov_gen_.2, align 1, !nosanitize !5
// PURECAP-NEXT: store i8 [[TMP1]], ptr addrspace(200) @__sancov_gen_.[[UID]].2, align 1, !nosanitize !5
// PURECAP-NEXT: store i32 [[I]], ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: call void @__sanitizer_cov_trace_const_cmp4(i32 100, i32 [[TMP2]])
// PURECAP-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP2]], 100
// PURECAP-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
// PURECAP: if.then:
// PURECAP-NEXT: [[TMP3:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 1), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP3:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 1), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP4:%.*]] = add i8 [[TMP3]], 1
// PURECAP-NEXT: store i8 [[TMP4]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 1), align 1, !nosanitize !5
// PURECAP-NEXT: store i8 [[TMP4]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 1), align 1, !nosanitize !5
// PURECAP-NEXT: [[CALL:%.*]] = call signext i32 @fail1() #[[ATTR3]]
// PURECAP-NEXT: store i32 [[CALL]], ptr addrspace(200) [[RETVAL]], align 4
// PURECAP-NEXT: br label [[RETURN:%.*]]
Expand All @@ -152,18 +152,18 @@ int main(void) {
// PURECAP-NEXT: [[CMP1:%.*]] = icmp slt i32 [[TMP5]], 200
// PURECAP-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END:%.*]]
// PURECAP: if.then2:
// PURECAP-NEXT: [[TMP6:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 2), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP6:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 2), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP7:%.*]] = add i8 [[TMP6]], 1
// PURECAP-NEXT: store i8 [[TMP7]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 2), align 1, !nosanitize !5
// PURECAP-NEXT: store i8 [[TMP7]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 2), align 1, !nosanitize !5
// PURECAP-NEXT: [[CALL3:%.*]] = call signext i32 @fail2() #[[ATTR3]]
// PURECAP-NEXT: store i32 [[CALL3]], ptr addrspace(200) [[RETVAL]], align 4
// PURECAP-NEXT: br label [[RETURN]]
// PURECAP: if.end:
// PURECAP-NEXT: br label [[IF_END4:%.*]]
// PURECAP: if.end4:
// PURECAP-NEXT: [[TMP8:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 3), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP8:%.*]] = load i8, ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 3), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP9:%.*]] = add i8 [[TMP8]], 1
// PURECAP-NEXT: store i8 [[TMP9]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.2, i64 0, i64 3), align 1, !nosanitize !5
// PURECAP-NEXT: store i8 [[TMP9]], ptr addrspace(200) getelementptr inbounds ([4 x i8], ptr addrspace(200) @__sancov_gen_.[[UID]].2, i64 0, i64 3), align 1, !nosanitize !5
// PURECAP-NEXT: [[TMP10:%.*]] = load i32, ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP10]], 1
// PURECAP-NEXT: store i32 [[ADD]], ptr addrspace(200) [[RETVAL]], align 4
Expand Down
18 changes: 9 additions & 9 deletions clang/test/CodeGen/cheri/trace-pc-guard.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ extern int fail2(void);

// These should all be in AS200:
// PURECAP: @__sancov_lowest_stack = external thread_local(initialexec) addrspace(200) global i64
// PURECAP: @__sancov_gen_ = private addrspace(200) global [1 x i32] zeroinitializer, section "__sancov_guards", comdat($main), align 4{{$}}
// PURECAP: @__sancov_gen_.1 = private addrspace(200) global [4 x i32] zeroinitializer, section "__sancov_guards", comdat($func2), align 4{{$}}
// PURECAP: @__sancov_gen_.[[UID:.*]] = private addrspace(200) global [1 x i32] zeroinitializer, section "__sancov_guards", comdat($main), align 4{{$}}
// PURECAP: @__sancov_gen_.[[UID]].1 = private addrspace(200) global [4 x i32] zeroinitializer, section "__sancov_guards", comdat($func2), align 4{{$}}
// PURECAP: @__start___sancov_guards = extern_weak hidden addrspace(200) global i32
// PURECAP: @__stop___sancov_guards = extern_weak hidden addrspace(200) global i32
// PURECAP: @llvm.global_ctors = appending addrspace(200) global [1 x { i32, ptr addrspace(200), ptr addrspace(200) }] [{ i32, ptr addrspace(200), ptr addrspace(200) } { i32 2, ptr addrspace(200) @sancov.module_ctor_trace_pc_guard, ptr addrspace(200) @sancov.module_ctor_trace_pc_guard }]
// PURECAP: @llvm.compiler.used = appending addrspace(200) global [2 x ptr] [ptr addrspacecast (ptr addrspace(200) @__sancov_gen_ to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.1 to ptr)], section "llvm.metadata"
// PURECAP: @llvm.compiler.used = appending addrspace(200) global [2 x ptr] [ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]] to ptr), ptr addrspacecast (ptr addrspace(200) @__sancov_gen_.[[UID]].1 to ptr)], section "llvm.metadata"

// UTC_ARGS: --enable

Expand All @@ -50,15 +50,15 @@ extern int fail2(void);
// PURECAP-SAME: () addrspace(200) #[[ATTR0:[0-9]+]] comdat {
// PURECAP-NEXT: entry:
// PURECAP-NEXT: [[FOO:%.*]] = alloca [10 x i8], align 1, addrspace(200)
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) @__sancov_gen_) #[[ATTR3:[0-9]+]]
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) @__sancov_gen_.[[UID]]) #[[ATTR3:[0-9]+]]
// PURECAP-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [10 x i8], ptr addrspace(200) [[FOO]], i64 0, i64 0
// PURECAP-NEXT: [[CALL:%.*]] = call ptr addrspace(200) @gets(ptr addrspace(200) noundef [[ARRAYDECAY]])
// PURECAP-NEXT: [[ARRAYDECAY1:%.*]] = getelementptr inbounds [10 x i8], ptr addrspace(200) [[FOO]], i64 0, i64 0
// PURECAP-NEXT: [[CALL2:%.*]] = call signext i32 @puts(ptr addrspace(200) noundef [[ARRAYDECAY1]])
// PURECAP-NEXT: ret i32 0
//
int main(void) {
// FIXME: i32* getelementptr inbounds ([1 x i32], [1 x i32]* @__sancov_gen_, i32 0, i32 0)
// FIXME: i32* getelementptr inbounds ([1 x i32], [1 x i32]* @__sancov_gen_.[[UID]], i32 0, i32 0)
char foo[10];
gets(foo);
puts(foo);
Expand Down Expand Up @@ -105,13 +105,13 @@ int main(void) {
// PURECAP-NEXT: entry:
// PURECAP-NEXT: [[RETVAL:%.*]] = alloca i32, align 4, addrspace(200)
// PURECAP-NEXT: [[I_ADDR:%.*]] = alloca i32, align 4, addrspace(200)
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) @__sancov_gen_.1) #[[ATTR3]]
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) @__sancov_gen_.[[UID]].1) #[[ATTR3]]
// PURECAP-NEXT: store i32 [[I]], ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: [[TMP0:%.*]] = load i32, ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], 100
// PURECAP-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
// PURECAP: if.then:
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.1, i32 0, i64 1)) #[[ATTR3]]
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.[[UID]].1, i32 0, i64 1)) #[[ATTR3]]
// PURECAP-NEXT: [[CALL:%.*]] = call signext i32 @fail1()
// PURECAP-NEXT: store i32 [[CALL]], ptr addrspace(200) [[RETVAL]], align 4
// PURECAP-NEXT: br label [[RETURN:%.*]]
Expand All @@ -120,14 +120,14 @@ int main(void) {
// PURECAP-NEXT: [[CMP1:%.*]] = icmp slt i32 [[TMP1]], 200
// PURECAP-NEXT: br i1 [[CMP1]], label [[IF_THEN2:%.*]], label [[IF_END:%.*]]
// PURECAP: if.then2:
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.1, i32 0, i64 2)) #[[ATTR3]]
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.[[UID]].1, i32 0, i64 2)) #[[ATTR3]]
// PURECAP-NEXT: [[CALL3:%.*]] = call signext i32 @fail2()
// PURECAP-NEXT: store i32 [[CALL3]], ptr addrspace(200) [[RETVAL]], align 4
// PURECAP-NEXT: br label [[RETURN]]
// PURECAP: if.end:
// PURECAP-NEXT: br label [[IF_END4:%.*]]
// PURECAP: if.end4:
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.1, i32 0, i64 3)) #[[ATTR3]]
// PURECAP-NEXT: call void @__sanitizer_cov_trace_pc_guard(ptr addrspace(200) getelementptr inbounds ([4 x i32], ptr addrspace(200) @__sancov_gen_.[[UID]].1, i32 0, i64 3)) #[[ATTR3]]
// PURECAP-NEXT: [[TMP2:%.*]] = load i32, ptr addrspace(200) [[I_ADDR]], align 4
// PURECAP-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP2]], 1
// PURECAP-NEXT: store i32 [[ADD]], ptr addrspace(200) [[RETVAL]], align 4
Expand Down
17 changes: 15 additions & 2 deletions llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/PostDominators.h"
#include "llvm/IR/Constant.h"
Expand All @@ -30,6 +31,7 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/SpecialCaseList.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/TargetParser/Triple.h"
Expand Down Expand Up @@ -390,6 +392,11 @@ bool ModuleSanitizerCoverage::instrumentModule(
DL = &M.getDataLayout();
CurModule = &M;
CurModuleUniqueId = getUniqueModuleId(CurModule);
if (CurModuleUniqueId.empty()) {
CurModuleUniqueId = "clangPidTime_" +
llvm::itostr(sys::Process::getProcessId()) + "_" +
llvm::itostr(time(nullptr));
}
TargetTriple = Triple(M.getTargetTriple());
FunctionGuardArray = nullptr;
Function8bitCounterArray = nullptr;
Expand Down Expand Up @@ -724,7 +731,10 @@ GlobalVariable *ModuleSanitizerCoverage::CreateFunctionLocalArrayInSection(
ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
auto Array = new GlobalVariable(
*CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
Constant::getNullValue(ArrayTy), "__sancov_gen_");
Constant::getNullValue(ArrayTy),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this change is needed? Also please avoid hardcoded AS200 checks and use DL.isFatPointer instead. But I am not convinced this change is actually required.

DL->getGlobalsAddressSpace() == 200
? Twine("__sancov_gen_") + Twine(CurModuleUniqueId)
: "__sancov_gen_");

if (TargetTriple.supportsCOMDAT() &&
(TargetTriple.isOSBinFormatELF() || !F.isInterposable()))
Expand Down Expand Up @@ -862,7 +872,10 @@ void ModuleSanitizerCoverage::InjectTraceForSwitch(
GlobalVariable *GV = new GlobalVariable(
*CurModule, ArrayOfInt64Ty, false, GlobalVariable::InternalLinkage,
ConstantArray::get(ArrayOfInt64Ty, Initializers),
"__sancov_gen_cov_switch_values");
DL->getGlobalsAddressSpace() == 200
? Twine("__sancov_gen_cov_switch_values") +
Twine(CurModuleUniqueId)
: "__sancov_gen_cov_switch_values");
IRB.CreateCall(SanCovTraceSwitchFunction,
{Cond, IRB.CreatePointerCast(GV, GlobalsInt64PtrTy)});
}
Expand Down