Disable MSAN poisoning when printing using engine_print.

When using MSAN, mjData->buffer is marked as poisoned, despite being initialized with zeroes.
This is useful for catching computations that accidentally use uninitialized values.

However, when using engine_print, it's OK to assume the buffer is initialized to zeros.

PiperOrigin-RevId: 456085776
Change-Id: I9d85e37fa82958eb54ac413140543a6581aa46ad
This commit is contained in:
Nimrod Gileadi
2022-06-20 08:57:14 -07:00
committed by Copybara-Service
parent 08b2db2f62
commit 2a26bf1ba5
3 changed files with 110 additions and 0 deletions
+20
View File
@@ -30,6 +30,10 @@
#include "engine/engine_util_misc.h"
#include "engine/engine_util_sparse.h"
#ifdef MEMORY_SANITIZER
#include <sanitizer/msan_interface.h>
#endif
#define FLOAT_FORMAT "% -9.2g"
#define FLOAT_FORMAT_MAX_LEN 20
#define INT_FORMAT " %d"
@@ -717,6 +721,16 @@ void mj_printFormattedData(const mjModel* m, mjData* d, const char* filename,
// allocate full inertia
M = mj_stackAlloc(d, m->nv*m->nv);
#ifdef MEMORY_SANITIZER
// If memory sanitizer is active, d->buffer will be marked as poisoned, even
// though it's really initialized to 0. This catches unintentionally
// using uninitialized values, but in engine_print it's OK to output zeroes.
// save current poison status of buffer before marking unpoisoned
void* shadow = mju_malloc(d->nbuffer);
__msan_copy_shadow(shadow, d->buffer, d->nbuffer);
__msan_unpoison(d->buffer, d->nbuffer);
#endif
// ---------------------------------- print mjData fields
fprintf(fp, "SIZES\n");
@@ -966,6 +980,12 @@ void mj_printFormattedData(const mjModel* m, mjData* d, const char* filename,
printArray("CFRC_INT", m->nbody, 6, d->cfrc_int, fp, float_format);
printArray("CFRC_EXT", m->nbody, 6, d->cfrc_ext, fp, float_format);
#ifdef MEMORY_SANITIZER
// restore poisoned status
__msan_copy_shadow(d->buffer, shadow, d->nbuffer);
mju_free(shadow);
#endif
if (filename) {
fclose(fp);
}