forked from CTSRD-CHERI/llvm-project
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't perform type-base tag-preservation analysis with -fno-strict-al…
…iasing Marking a memcpy() to/from a long* as not tag-preserving could result in tag stripping for code that assumes relaxed aliasing. Example from CTSRD-CHERI#506: ``` void *malloc(__SIZE_TYPE__); void *memcpy(void *, const void *, __SIZE_TYPE__); void foo(long **p, long **q) { *p = malloc(32); *q = malloc(32); (*p)[0] = 1; (*p)[1] = 2; *(void (**)(long **, long **))(*p + 2) = &foo; memcpy(*q, *p, 32); } ``` Adding the no_preserve_tags attribute to memcpy() is safe under -fstrict-aliasing (since the store above is invalid), but with -fno-strict-aliasing this aligned memcpy must preserve tags.
- Loading branch information
1 parent
567a13a
commit 0fb29f1
Showing
10 changed files
with
106 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// RUN: %riscv64_cheri_purecap_cc1 %s -emit-llvm -o - -O0 | FileCheck %s --check-prefixes=CHECK,CHECK-STRICT-ALIASING | ||
// RUN: %riscv64_cheri_purecap_cc1 %s -relaxed-aliasing -emit-llvm -o - -O0 | FileCheck %s --check-prefixes=CHECK,CHECK-RELAXED-ALIASING | ||
|
||
void *malloc(__SIZE_TYPE__); | ||
void *memcpy(void *, const void *, __SIZE_TYPE__); | ||
|
||
void foo(long **p, long **q) { | ||
*p = malloc(32); | ||
*q = malloc(32); | ||
(*p)[0] = 1; | ||
(*p)[1] = 2; | ||
// Note: Storing a function pointer to a long* breaks the strict-aliasing rules | ||
*(void (**)(long **, long **))(*p + 2) = &foo; | ||
// As this is invalid under -fstrict-aliasing, we can add the no_preserve_tags metadata here despite knowing that | ||
// it conains tags. However, with -fno-strict-aliasing, we must ensure that the memcpy() retains tags if possible: | ||
// XXX: Maybe we should add a -fsanitize=cheri flag to diagnose this problem? | ||
memcpy(*q, *p, 32); | ||
// CHECK: @foo(i64 addrspace(200)* addrspace(200)* [[P:%.*]], i64 addrspace(200)* addrspace(200)* [[Q:%.*]]) addrspace(200) | ||
// CHECK: call void @llvm.memcpy.p200i8.p200i8.i64(i8 addrspace(200)* align 8 {{%[0-9]+}}, i8 addrspace(200)* align 8 {{%[0-9]+}} | ||
// CHECK-STRICT-ALIASING-SAME: , i64 32, i1 false) [[NO_TAGS_ATTR:#.*]]{{$}} | ||
// CHECK-RELAXED-ALIASING-SAME: , i64 32, i1 false){{$}} | ||
// CHECK-NEXT: ret void | ||
} | ||
|
||
// CHECK-STRICT-ALIASING: [[NO_TAGS_ATTR]] = { no_preserve_cheri_tags } |