Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch invalid uses of {must,no}_preserve_cheri_tag #648

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 7 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,13 @@ bool TargetLowering::findOptimalMemOpLowering(
}

// If we are preserving capabilities, the first VT must be a capability
if (Op.PreserveTags == PreserveCheriTags::Required && MemOps.empty() &&
!VT.isFatPointer()) {
return false;
if (Op.PreserveTags == PreserveCheriTags::Required && MemOps.empty()) {
assert(cheriCapabilityType().isValid());
assert(Size >= cheriCapabilityType().getStoreSize().getFixedSize() &&
"PreserveCheriTags::Required should not be possible for copies of "
"less than capability size");
if (!VT.isFatPointer())
return false;
}

MemOps.push_back(VT);
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/IR/IntrinsicInst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,13 @@ void llvm::setPreserveCheriTags(IntrinsicInst *I, PreserveCheriTags NewValue,
assert(DL.hasCheriCapabilities());
assert(!I->hasFnAttr(Attribute::NoPreserveCheriTags) &&
"attempting to set conflicting attributes");
#ifndef NDEBUG
if (auto MTI = dyn_cast<AnyMemTransferInst>(I))
if (auto *Length = dyn_cast<ConstantInt>(MTI->getLength()))
assert(Length->getZExtValue() >= DL.getMaxPointerSize() &&
"must_preserve_cheri_tags cannot be set on copies of less than "
"capability size");
#endif
I->addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::MustPreserveCheriTags);
NumMustPreserveTagAttrs++;
Expand Down
21 changes: 21 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,13 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
"Attribute 'jumptable' requires 'unnamed_addr'", V);
}

if (Attrs.hasFnAttribute(Attribute::NoPreserveCheriTags))
Assert(DL.hasCheriCapabilities(),
"Attribute 'no_preserve_cheri_tags' requires a CHERI target", V);
if (Attrs.hasFnAttribute(Attribute::MustPreserveCheriTags))
Assert(DL.hasCheriCapabilities(),
"Attribute 'must_preserve_cheri_tags' requires a CHERI target", V);

if (Attrs.hasFnAttribute(Attribute::AllocSize)) {
std::pair<unsigned, Optional<unsigned>> Args =
Attrs.getAllocSizeArgs(AttributeList::FunctionIndex);
Expand Down Expand Up @@ -4706,6 +4713,20 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
Assert(IsValidAlignment(MTI->getSourceAlignment()),
"alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
Call);
if (DL.hasCheriCapabilities() &&
MTI->shouldPreserveCheriTags() == PreserveCheriTags::Required)
if (auto *Length = dyn_cast<ConstantInt>(MTI->getLength()))
Assert(Length->getZExtValue() >= DL.getMaxPointerSize(),
"Attribute 'must_preserve_cheri_tags' cannot be set on copies "
"of less than capability size",
Call);
} else {
Assert(shouldPreserveTags(MI) != PreserveCheriTags::Required,
"Attribute 'must_preserve_cheri_tags' cannot be used with memset",
Call);
Assert(shouldPreserveTags(MI) != PreserveCheriTags::Unnecessary,
"Attribute 'no_preserve_cheri_tags' cannot be used with memset",
Call);
}

break;
Expand Down
75 changes: 0 additions & 75 deletions llvm/test/CodeGen/Mips/memcpy-nobuiltin.ll

This file was deleted.

39 changes: 39 additions & 0 deletions llvm/test/Verifier/cheri-preserve-tags.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
; RUN: not opt -S %s -verify 2>&1 | FileCheck %s --check-prefixes=CHECK,PURECAP
; RUN: not opt -data-layout="p0:64:64:64" -S %s -verify 2>&1 | FileCheck %s --check-prefixes=CHECK,NON-CHERI

target datalayout = "pf200:128:128:128:64"

declare void @llvm.memcpy.p200i8.p200i8.i64(i8 addrspace(200)* nocapture writeonly, i8 addrspace(200)* nocapture readonly, i64, i1)
declare void @llvm.memmove.p200i8.p200i8.i64(i8 addrspace(200)* nocapture writeonly, i8 addrspace(200)* nocapture readonly, i64, i1)
declare void @llvm.memset.p200i8.i64(i8 addrspace(200)* nocapture, i8, i64, i1) nounwind

define void @too_small(i8 addrspace(200)* %dst, i8 addrspace(200)* %src) #0 {
entry:

call void @llvm.memcpy.p200i8.p200i8.i64(i8 addrspace(200)* align 1 %dst, i8 addrspace(200)* align 16 %src, i64 15, i1 false) must_preserve_cheri_tags
; PURECAP: Attribute 'must_preserve_cheri_tags' cannot be set on copies of less than capability size
; NON-CHERI: Attribute 'must_preserve_cheri_tags' requires a CHERI target
; CHECK-NEXT: call void @llvm.memcpy.p200i8.p200i8.i64(i8 addrspace(200)* align 1 %dst, i8 addrspace(200)* align 16 %src, i64 15, i1 false)
call void @llvm.memmove.p200i8.p200i8.i64(i8 addrspace(200)* align 1 %dst, i8 addrspace(200)* align 16 %src, i64 15, i1 false) must_preserve_cheri_tags
; PURECAP-NEXT: Attribute 'must_preserve_cheri_tags' cannot be set on copies of less than capability size
; NON-CHERI-NEXT: Attribute 'must_preserve_cheri_tags' requires a CHERI target
; CHECK-NEXT: call void @llvm.memmove.p200i8.p200i8.i64(i8 addrspace(200)* align 1 %dst, i8 addrspace(200)* align 16 %src, i64 15, i1 false)
ret void
}

define void @memset_invalid(i8 addrspace(200)* %dst, i8 addrspace(200)* %src) #0 {
entry:
call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false) must_preserve_cheri_tags
; NON-CHERI-NEXT: Attribute 'must_preserve_cheri_tags' requires a CHERI target
; NON-CHERI-NEXT: call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false)
; CHECK-NEXT: Attribute 'must_preserve_cheri_tags' cannot be used with memset
; CHECK-NEXT: call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false)
call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false) no_preserve_cheri_tags
; NON-CHERI-NEXT: Attribute 'no_preserve_cheri_tags' requires a CHERI target
; NON-CHERI-NEXT: call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false)
; CHECK-NEXT: Attribute 'no_preserve_cheri_tags' cannot be used with memset
; CHECK-NEXT: call void @llvm.memset.p200i8.i64(i8 addrspace(200)* align 16 %dst, i8 0, i64 16, i1 false)
ret void
}

; CHECK-NEXT: error: input module is broken!