Files
parallel-ps2/common/AlignedMalloc.cpp
T
LibretroAdmin b74dc66370 AlignedMalloc: honor the ISO C11 aligned_alloc size contract
aligned_alloc requires size to be a multiple of alignment.  glibc
happens to accept violations but ASan enforces the contract and other
libcs (musl historically, macOS) reject it outright; the very first
allocation through this path in a libretro run is aligned_alloc(64,
0x46) out of VifUnpackSSE_Init, which is a hard error under ASan.
Round the size up, matching what parallel-gs Granite's memalign_alloc
already does.

Found by ASan (invalid-alignment hard error) during a full
ASan+UBSan+LSan sweep; after the fix the sweep runs both harness
lifecycles with zero ASan errors.
2026-08-07 20:09:32 +02:00

51 lines
1.7 KiB
C++

/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2010 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
// This module contains implementations of _aligned_malloc for platforms that don't have
// it built into their CRT/libc.
#if !defined(_WIN32)
#include "AlignedMalloc.h"
#include <algorithm>
#include <cstdlib>
void* _aligned_malloc(size_t size, size_t align)
{
#if defined(__USE_ISOC11) && !defined(ASAN_WORKAROUND) // not supported yet on gcc 4.9
/* ISO C11/C17 requires size to be a multiple of align; glibc happens
* to accept anything but ASan enforces the contract and other libcs
* (musl historically, macOS) reject it. Round up, same as
* parallel-gs Granite's memalign_alloc does. */
return aligned_alloc(align, (size + align - 1) & ~(align - 1));
#else
#ifdef __APPLE__
// MacOS has a bug where posix_memalign is ridiculously slow on unaligned sizes
// This especially bad on M1s for some reason
size = (size + align - 1) & ~(align - 1);
#endif
void* result = 0;
posix_memalign(&result, align, size);
return result;
#endif
}
__fi void _aligned_free(void* pmem)
{
free(pmem);
}
#endif