Make MuJoCo work with -fsanitize=alignment.

PiperOrigin-RevId: 563702045
Change-Id: Ieca69595b7e2fc1e35ca69754d7cc7971825f48b
This commit is contained in:
Saran Tunyasuvunakool
2023-09-08 03:28:41 -07:00
committed by Copybara-Service
parent 7e5a7b76fc
commit a07e172770
3 changed files with 53 additions and 13 deletions
+11 -4
View File
@@ -1289,16 +1289,23 @@ static inline void* stackalloc(mjData* d, size_t size, size_t alignment) {
#ifdef ADDRESS_SANITIZER
// actual stack usage (without red zone bytes) is stored in the red zone
if (d->pstack) {
size_t* prev_usage_ptr = (size_t*)(end_of_arena_ptr - d->pstack);
char* prev_pstack_ptr = (char*)(end_of_arena_ptr - d->pstack);
size_t prev_misalign = (uintptr_t)prev_pstack_ptr % _Alignof(size_t);
size_t* prev_usage_ptr =
(size_t*)(prev_pstack_ptr +
(prev_misalign ? _Alignof(size_t) - prev_misalign : 0));
ASAN_UNPOISON_MEMORY_REGION(prev_usage_ptr, sizeof(size_t));
usage = current_alloc_usage + *prev_usage_ptr;
ASAN_POISON_MEMORY_REGION(prev_usage_ptr, sizeof(size_t));
}
// store new stack usage in the red zone
ASAN_UNPOISON_MEMORY_REGION((void*)new_pstack_ptr, sizeof(size_t));
*(size_t*)new_pstack_ptr = usage;
ASAN_POISON_MEMORY_REGION((void*)new_pstack_ptr, sizeof(size_t));
size_t misalign = new_pstack_ptr % _Alignof(size_t);
size_t* usage_ptr =
(size_t*)(new_pstack_ptr + (misalign ? _Alignof(size_t) - misalign : 0));
ASAN_UNPOISON_MEMORY_REGION(usage_ptr, sizeof(size_t));
*usage_ptr = usage;
ASAN_POISON_MEMORY_REGION(usage_ptr, sizeof(size_t));
// unpoison the actual usable allocation
ASAN_UNPOISON_MEMORY_REGION((void*)start_ptr, size);