Skip to content

Commit

Permalink
Memory copies from string literals do not need to preserve tags
Browse files Browse the repository at this point in the history
  • Loading branch information
arichardson committed Oct 7, 2022
1 parent 26fe50a commit e0ebb39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CodeGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,11 @@ CodeGenTypes::copyShouldPreserveTags(const Expr *E, Optional<CharUnits> Size) {
QualType Ty = E->IgnoreParenImpCasts()->getType();
if (Ty->isAnyPointerType())
Ty = Ty->getPointeeType();
const Expr *UnderlyingExpr = E->IgnoreParenCasts();
if (const auto *SL = dyn_cast<StringLiteral>(UnderlyingExpr)) {
// String literals can never contain tag bits.
return llvm::PreserveCheriTags::Unnecessary;
}
// TODO: Find the underlying VarDecl to improve diagnostics
const VarDecl *UnderlyingVar = nullptr;
// TODO: this assertion may be overly aggressive.
Expand Down
10 changes: 7 additions & 3 deletions clang/test/CodeGen/cheri/no-tag-copy-attribute-with-caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,16 @@ void test_string_constant(struct OneCap *cap) {
// CHECK-LABEL: void @test_string_constant(
// Same for string -> char*
__builtin_memmove(cap, "abcdefghijklmnopqrstuvwxyz", sizeof(*cap));
// expected-warning@-1{{memcpy operation with capability argument <unknown type> and underaligned destination (aligned to 1 bytes) may be inefficient or result in CHERI tags bits being stripped}} expected-note@-1{{For more information}}
// CHECK: call void @llvm.memmove.p200i8.p200i8.i64(i8 addrspace(200)* align 16 {{%[a-z0-9]+}}, i8 addrspace(200)* align 1 getelementptr inbounds ([27 x i8], [27 x i8] addrspace(200)* @.str
// CHECK-SAME: , i64 0
// CHECK-SAME: , i64 0)
// CHECK-SAME: , i64 16, i1 false) [[MUST_PRESERVE_ATTR]]{{$}}
// FIXME-SAME: , i64 16, i1 false) [[NO_PRESERVE_ATTR]]{{$}}
// CHECK-SAME: , i64 16, i1 false) [[NO_PRESERVE_ATTR]]{{$}}
/// Check that the explicit cast does not affect the analysis.
__builtin_memmove(cap, (void*)"abcdefghijklmnopqrstuvwxyz", sizeof(*cap));
// CHECK: call void @llvm.memmove.p200i8.p200i8.i64(i8 addrspace(200)* align 16 {{%[a-z0-9]+}}, i8 addrspace(200)* align 1 getelementptr inbounds ([27 x i8], [27 x i8] addrspace(200)* @.str
// CHECK-SAME: , i64 0
// CHECK-SAME: , i64 0)
// CHECK-SAME: , i64 16, i1 false) [[NO_PRESERVE_ATTR]]{{$}}
}

void test_void_buffer(struct OneCap *cap, void *buf) {
Expand Down

0 comments on commit e0ebb39

Please sign in to comment.