Define ADDRESS_SANITIZER portably using standard compiler macros

Use `__SANITIZE_ADDRESS__` (GCC) and `__has_feature(address_sanitizer)` (Clang)
to define ADDRESS_SANITIZER when the toolchain doesn't provide it natively.

Fixes #3160

PiperOrigin-RevId: 884447485
Change-Id: I48ef51bca8f5c5f15f62089e73cd8fa649723636
This commit is contained in:
Yuval Tassa
2026-03-16 08:17:27 -07:00
committed by Copybara-Service
parent 63bab26536
commit eca5758bf9
4 changed files with 17 additions and 0 deletions
+7
View File
@@ -15,6 +15,13 @@
#ifndef MUJOCO_INCLUDE_MJSAN_H_
#define MUJOCO_INCLUDE_MJSAN_H_
// Define ADDRESS_SANITIZER if implied by other macros.
#if !defined(ADDRESS_SANITIZER)
#if defined(__SANITIZE_ADDRESS__) || (defined(__has_feature) && __has_feature(address_sanitizer))
#define ADDRESS_SANITIZER
#endif
#endif
// Include asan interface header, or provide stubs for poison/unpoison macros when not using asan.
#ifdef ADDRESS_SANITIZER
#include <sanitizer/asan_interface.h>
+7
View File
@@ -58,6 +58,13 @@
#define mjUNLIKELY(x) (x)
#endif
// Define ADDRESS_SANITIZER if implied by other macros.
#if !defined(ADDRESS_SANITIZER)
#if defined(__SANITIZE_ADDRESS__) || (defined(__has_feature) && __has_feature(address_sanitizer))
#define ADDRESS_SANITIZER
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
+2
View File
@@ -17,6 +17,8 @@
#include <mujoco/mjdata.h>
#include <mujoco/mjexport.h>
#include <mujoco/mjtnum.h>
#include <mujoco/mjsan.h> // IWYU pragma: keep
#include <mujoco/mjxmacro.h>
#ifdef __cplusplus
+1
View File
@@ -60,6 +60,7 @@ public const int mjNAUX = 10;
public const int mjMAXTEXTURE = 1000;
public const int mjMAXMATERIAL = 1000;
public const bool THIRD_PARTY_MUJOCO_INCLUDE_MJSAN_H_ = true;
public const bool ADDRESS_SANITIZER = true;
public const bool THIRD_PARTY_MUJOCO_INCLUDE_MJSPEC_H_ = true;
public const bool THIRD_PARTY_MUJOCO_INCLUDE_MJTHREAD_H_ = true;
public const int mjMAXTHREAD = 128;