Skip to content

Commit 4effda0

Browse files
author
Chen Zheng
authored
[ASan] return 0 for current allocated bytes if malloc/free are never happend (llvm#67394)
This is found during address sanitizer enablement on AIX. On platforms that has no malloc/free calls before user's malloc/free calls, `__sanitizer_get_current_allocated_bytes()` should return 0. Otherwise the case like `compiler-rt/test/sanitizer_common/TestCases/allocator_interface.cpp` will fail at below scenario: ``` void Test(int size) { auto allocated_bytes_before = __sanitizer_get_current_allocated_bytes(); int *p = (int *)malloc(size); assert(__sanitizer_get_current_allocated_bytes() >= size + allocated_bytes_before); // if allocated_bytes_before is 1, this assert will fail. allocated_bytes_before should be 0 } ```
1 parent bcf07ce commit 4effda0

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

compiler-rt/lib/asan/asan_stats.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ uptr __sanitizer_get_current_allocated_bytes() {
142142
uptr freed = stats.freed;
143143
// Return sane value if malloced < freed due to racy
144144
// way we update accumulated stats.
145-
return (malloced > freed) ? malloced - freed : 1;
145+
return (malloced > freed) ? malloced - freed : 0;
146146
}
147147

148148
uptr __sanitizer_get_heap_size() {
@@ -161,7 +161,7 @@ uptr __sanitizer_get_free_bytes() {
161161
+ stats.malloced_redzones;
162162
// Return sane value if total_free < total_used due to racy
163163
// way we update accumulated stats.
164-
return (total_free > total_used) ? total_free - total_used : 1;
164+
return (total_free > total_used) ? total_free - total_used : 0;
165165
}
166166

167167
uptr __sanitizer_get_unmapped_bytes() {

compiler-rt/test/asan/TestCases/Posix/current_allocated_bytes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void* allocate(void *arg) {
1717
}
1818

1919
void* check_stats(void *arg) {
20-
assert(__sanitizer_get_current_allocated_bytes() > 0);
20+
assert(__sanitizer_get_current_allocated_bytes() >= 0);
2121
return 0;
2222
}
2323

0 commit comments

Comments
 (0)