memtag_test: fix sporadic failures of overflow/underflow tests

This commit is contained in:
Dmitry Muhomor 2023-11-01 21:33:54 +02:00 committed by Daniel Micay
parent f793a3edf6
commit 88b3c1acf9

View File

@ -10,6 +10,7 @@
#include <unistd.h>
#include <map>
#include <set>
#include <string>
#include <unordered_map>
@ -181,9 +182,26 @@ void tag_distinctness() {
}
u8* alloc_default() {
u8 *p = (u8 *) malloc(DEFAULT_ALLOC_SIZE);
assert(p);
return p;
const size_t full_alloc_size = DEFAULT_ALLOC_SIZE + CANARY_SIZE;
set<uptr> addrs;
// make sure allocation has both left and right neighbors, otherwise overflow/underflow tests
// will fail when allocation is at the end/beginning of slab
for (;;) {
u8 *p = (u8 *) malloc(DEFAULT_ALLOC_SIZE);
assert(p);
uptr addr = (uptr) untag_pointer(p);
uptr left = addr - full_alloc_size;
if (addrs.find(left) != addrs.end()) {
uptr right = addr + full_alloc_size;
if (addrs.find(right) != addrs.end()) {
return p;
}
}
addrs.emplace(addr);
}
}
volatile u8 u8_var;