From 8c7cdfdc79e1ad0a7786d8039a52bffde673f7a5 Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 29 Dec 2019 01:19:31 +0300 Subject: [PATCH] mdbx: fix ASAN issues (minor). This resolves https://github.com/leo-yuriev/libmdbx/issues/73 Change-Id: Ifb59f5ba66321bb362b81085e0f25a82a2d76d5f --- src/elements/core.c | 9 ++++++--- src/elements/internals.h | 2 +- src/elements/osal.c | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/elements/core.c b/src/elements/core.c index e62fd79b..335095b3 100644 --- a/src/elements/core.c +++ b/src/elements/core.c @@ -4883,7 +4883,7 @@ static void mdbx_txn_valgrind(MDBX_env *env, MDBX_txn *txn) { mdbx_txn_unlock(env); } } -#endif /* MDBX_USE_VALGRIND */ +#endif /* MDBX_USE_VALGRIND || __SANITIZE_ADDRESS__ */ /* Common code for mdbx_txn_begin() and mdbx_txn_renew(). */ static int mdbx_txn_renew0(MDBX_txn *txn, unsigned flags) { @@ -7464,7 +7464,7 @@ static int mdbx_sync_locked(MDBX_env *env, unsigned flags, ASAN_POISON_MEMORY_REGION(env->me_map + pgno2bytes(env, largest_pgno), pgno2bytes(env, edge - largest_pgno)); } -#endif /* MDBX_USE_VALGRIND */ +#endif /* MDBX_USE_VALGRIND || __SANITIZE_ADDRESS__ */ #if defined(MADV_DONTNEED) const size_t largest_bytes = pgno2bytes(env, largest_pgno); const size_t madvise_gap = (largest_bytes < 65536 * 256) @@ -8420,7 +8420,7 @@ static int __cold mdbx_setup_dxb(MDBX_env *env, const int lck_rc) { ASAN_POISON_MEMORY_REGION(env->me_map + used_bytes, env->me_dxb_mmap.limit - used_bytes); env->me_poison_edge = bytes2pgno(env, env->me_dxb_mmap.limit); -#endif /* MDBX_USE_VALGRIND */ +#endif /* MDBX_USE_VALGRIND || __SANITIZE_ADDRESS__ */ /* NOTE: AddressSanitizer (at least GCC 7.x, 8.x) could generate * false-positive alarm here. I have no other explanation for this @@ -9213,6 +9213,9 @@ int __cold mdbx_env_open(MDBX_env *env, const char *pathname, unsigned flags, #endif bailout: +#if defined(MDBX_USE_VALGRIND) || defined(__SANITIZE_ADDRESS__) + mdbx_txn_valgrind(env, nullptr); +#endif if (rc) { rc = mdbx_env_close0(env) ? MDBX_PANIC : rc; env->me_flags = saved_me_flags | MDBX_FATAL_ERROR; diff --git a/src/elements/internals.h b/src/elements/internals.h index daeab529..7ae594ab 100644 --- a/src/elements/internals.h +++ b/src/elements/internals.h @@ -949,7 +949,7 @@ struct MDBX_env { #endif #if defined(MDBX_USE_VALGRIND) || defined(__SANITIZE_ADDRESS__) pgno_t me_poison_edge; -#endif +#endif /* MDBX_USE_VALGRIND || __SANITIZE_ADDRESS__ */ MDBX_env *me_lcklist_next; /* struct me_dbgeo used for accepting db-geo params from user for the new diff --git a/src/elements/osal.c b/src/elements/osal.c index d2c950b3..708cdfd1 100644 --- a/src/elements/osal.c +++ b/src/elements/osal.c @@ -1378,10 +1378,14 @@ MDBX_INTERNAL_FUNC int mdbx_mmap(const int flags, mdbx_mmap_t *map, #endif + VALGRIND_MAKE_MEM_DEFINED(map->address, map->current); + ASAN_UNPOISON_MEMORY_REGION(map->address, map->current); return MDBX_SUCCESS; } MDBX_INTERNAL_FUNC int mdbx_munmap(mdbx_mmap_t *map) { + VALGRIND_MAKE_MEM_NOACCESS(map->address, map->current); + ASAN_POISON_MEMORY_REGION(map->address, map->current); #if defined(_WIN32) || defined(_WIN64) if (map->section) NtClose(map->section);