Skip to content

Commit 131cf7d

Browse files
authored
[AllocToken] Enable alloc token instrumentation for size-returning functions (#168840)
Consider a newly added "malloc_span" attribute in the allocation token instrumentation to ensure that allocation functions with the "malloc_span" attribute are processed similarly to other memory allocation functions. Update the tests to demonstrate applicability to __size_returning_new.
1 parent dc343d2 commit 131cf7d

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6644,6 +6644,7 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
66446644

66456645
if (auto *CalleeDecl = dyn_cast_or_null<FunctionDecl>(TargetDecl)) {
66466646
if (CalleeDecl->hasAttr<RestrictAttr>() ||
6647+
CalleeDecl->hasAttr<MallocSpanAttr>() ||
66476648
CalleeDecl->hasAttr<AllocSizeAttr>()) {
66486649
// Function has 'malloc' (aka. 'restrict') or 'alloc_size' attribute.
66496650
if (SanOpts.has(SanitizerKind::AllocToken)) {

clang/test/CodeGenCXX/alloc-token.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ struct __sized_ptr_t {
1717
size_t n;
1818
};
1919
enum class __hot_cold_t : uint8_t;
20-
__sized_ptr_t __size_returning_new(size_t size);
21-
__sized_ptr_t __size_returning_new_hot_cold(size_t, __hot_cold_t);
22-
__sized_ptr_t __size_returning_new_aligned(size_t, std::align_val_t);
23-
__sized_ptr_t __size_returning_new_aligned_hot_cold(size_t, std::align_val_t, __hot_cold_t);
20+
__sized_ptr_t __size_returning_new(size_t size) __attribute__((malloc_span));
21+
__sized_ptr_t __size_returning_new_hot_cold(size_t, __hot_cold_t) __attribute__((malloc_span));
22+
__sized_ptr_t __size_returning_new_aligned(size_t, std::align_val_t) __attribute__((malloc_span));
23+
__sized_ptr_t __size_returning_new_aligned_hot_cold(size_t, std::align_val_t, __hot_cold_t) __attribute__((malloc_span));
2424
}
2525

2626
void *sink; // prevent optimizations from removing the calls
@@ -101,12 +101,11 @@ int *test_new_array_nothrow() {
101101
}
102102

103103
// CHECK-LABEL: define dso_local void @_Z23test_size_returning_newv(
104-
// CHECK: call { ptr, i64 } @__size_returning_new(i64 noundef 8)
105-
// CHECK: call { ptr, i64 } @__size_returning_new_hot_cold(i64 noundef 8, i8 noundef zeroext 1)
106-
// CHECK: call { ptr, i64 } @__size_returning_new_aligned(i64 noundef 8, i64 noundef 32)
107-
// CHECK: call { ptr, i64 } @__size_returning_new_aligned_hot_cold(i64 noundef 8, i64 noundef 32, i8 noundef zeroext 1)
104+
// CHECK: call { ptr, i64 } @__size_returning_new(i64 noundef 8){{.*}} !alloc_token [[META_LONG]]
105+
// CHECK: call { ptr, i64 } @__size_returning_new_hot_cold(i64 noundef 8, i8 noundef zeroext 1){{.*}} !alloc_token [[META_LONG]]
106+
// CHECK: call { ptr, i64 } @__size_returning_new_aligned(i64 noundef 8, i64 noundef 32){{.*}} !alloc_token [[META_LONG]]
107+
// CHECK: call { ptr, i64 } @__size_returning_new_aligned_hot_cold(i64 noundef 8, i64 noundef 32, i8 noundef zeroext 1){{.*}}_token [[META_LONG]]
108108
void test_size_returning_new() {
109-
// FIXME: Support __size_returning_new variants.
110109
sink = __size_returning_new(sizeof(long)).p;
111110
sink = __size_returning_new_hot_cold(sizeof(long), __hot_cold_t{1}).p;
112111
sink = __size_returning_new_aligned(sizeof(long), std::align_val_t{32}).p;

0 commit comments

Comments
 (0)