Move sanitizer instrumentation to a separate header file. Fixes #2049.
PiperOrigin-RevId: 672986547 Change-Id: I42522f4925237a73168e965364a2dd65c0f067cb
This commit is contained in:
committed by
Copybara-Service
parent
8fa7fe7f5b
commit
e4d4153352
@@ -15,17 +15,6 @@
|
||||
#ifndef MUJOCO_MJMACRO_H_
|
||||
#define MUJOCO_MJMACRO_H_
|
||||
|
||||
// include asan interface header, or provide stubs for poison/unpoison macros when not using asan
|
||||
#ifdef ADDRESS_SANITIZER
|
||||
#include <sanitizer/asan_interface.h>
|
||||
#elif defined(_MSC_VER)
|
||||
#define ASAN_POISON_MEMORY_REGION(addr, size)
|
||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
|
||||
#else
|
||||
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||
#endif
|
||||
|
||||
// max and min (use only for primitive types)
|
||||
#define mjMAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||
#define mjMIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// Copyright 2024 DeepMind Technologies Limited
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef MUJOCO_INCLUDE_MJSAN_H_
|
||||
#define MUJOCO_INCLUDE_MJSAN_H_
|
||||
|
||||
// Include asan interface header, or provide stubs for poison/unpoison macros when not using asan.
|
||||
#ifdef ADDRESS_SANITIZER
|
||||
#include <sanitizer/asan_interface.h>
|
||||
#elif defined(_MSC_VER)
|
||||
#define ASAN_POISON_MEMORY_REGION(addr, size)
|
||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
|
||||
#else
|
||||
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||
#endif
|
||||
|
||||
// When built and run under address sanitizer (asan), mj_markStack and mj_freeStack are instrumented
|
||||
// to detect leakage of mjData stack frames. When the compiler inlines several callees that call
|
||||
// into mark/free into the same function, this instrumentation requires that the compiler retains
|
||||
// separate mark/free calls for each original callee. The memory-clobbered asm blocks act as a
|
||||
// barrier to prevent mark/free calls from being combined under optimization.
|
||||
#ifdef ADDRESS_SANITIZER
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void mj__markStack(mjData*) __attribute__((noinline));
|
||||
static inline void mj_markStack(mjData* d) __attribute__((always_inline)) {
|
||||
asm volatile("" ::: "memory");
|
||||
mj__markStack(d);
|
||||
asm volatile("" ::: "memory");
|
||||
}
|
||||
|
||||
void mj__freeStack(mjData*) __attribute__((noinline));
|
||||
static inline void mj_freeStack(mjData* d) __attribute__((always_inline)) {
|
||||
asm volatile("" ::: "memory");
|
||||
mj__freeStack(d);
|
||||
asm volatile("" ::: "memory");
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif // __cplusplus
|
||||
#endif // ADDRESS_SANITIZER
|
||||
|
||||
#endif // MUJOCO_INCLUDE_MJSAN_H_
|
||||
+2
-30
@@ -29,6 +29,7 @@
|
||||
#include <mujoco/mjmacro.h>
|
||||
#include <mujoco/mjplugin.h>
|
||||
#include <mujoco/mjrender.h>
|
||||
#include <mujoco/mjsan.h>
|
||||
#include <mujoco/mjspec.h>
|
||||
#include <mujoco/mjthread.h>
|
||||
#include <mujoco/mjtnum.h>
|
||||
@@ -197,7 +198,7 @@ MJAPI void mj_resetDataDebug(const mjModel* m, mjData* d, unsigned char debug_va
|
||||
// Reset data. If 0 <= key < nkey, set fields from specified keyframe.
|
||||
MJAPI void mj_resetDataKeyframe(const mjModel* m, mjData* d, int key);
|
||||
|
||||
#ifndef ADDRESS_SANITIZER
|
||||
#ifndef ADDRESS_SANITIZER // Stack management functions declared in mjsan.h if ASAN is active.
|
||||
|
||||
// Mark a new frame on the mjData stack.
|
||||
MJAPI void mj_markStack(mjData* d);
|
||||
@@ -1766,35 +1767,6 @@ MJAPI void mjs_defaultKey(mjsKey* key);
|
||||
// Default plugin attributes.
|
||||
MJAPI void mjs_defaultPlugin(mjsPlugin* plugin);
|
||||
|
||||
|
||||
//---------------------------------- Sanitizer instrumentation -------------------------------------
|
||||
|
||||
// Most users can ignore these functions, the following comments are primarily for developers.
|
||||
//
|
||||
// When built and run under address sanitizer (asan), mj_markStack and mj_freeStack are instrumented
|
||||
// to detect leakage of mjData stack frames. When the compiler inlines several callees that call
|
||||
// into mark/free into the same function, this instrumentation requires that the compiler retains
|
||||
// separate mark/free calls for each original callee. The memory-clobbered asm blocks act as a
|
||||
// barrier to prevent mark/free calls from being combined under optimization.
|
||||
|
||||
#ifdef ADDRESS_SANITIZER
|
||||
|
||||
void mj__markStack(mjData*) __attribute__((noinline));
|
||||
static inline void mj_markStack(mjData* d) __attribute__((always_inline)) {
|
||||
asm volatile("" ::: "memory");
|
||||
mj__markStack(d);
|
||||
asm volatile("" ::: "memory");
|
||||
}
|
||||
|
||||
void mj__freeStack(mjData*) __attribute__((noinline));
|
||||
static inline void mj_freeStack(mjData* d) __attribute__((always_inline)) {
|
||||
asm volatile("" ::: "memory");
|
||||
mj__freeStack(d);
|
||||
asm volatile("" ::: "memory");
|
||||
}
|
||||
|
||||
#endif // ADDRESS_SANITIZER
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user