[MDEV-22671] ASAN: RockDB doesn't free ThreadLocalPtr::StaticMeta instance Created: 2020-05-22  Updated: 2023-11-28

Status: Open
Project: MariaDB Server
Component/s: Storage Engine - RocksDB
Affects Version/s: 10.2, 10.3, 10.4, 10.5, 10.6, 10.10, 10.11, 11.0, 11.1, 11.2
Fix Version/s: 10.4, 10.5, 10.6, 10.11, 11.0, 11.1

Type: Bug Priority: Minor
Reporter: Sergei Golubchik Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: upstream


 Description   

ThreadLocalPtr::StaticMeta instance is a singleton.
It is created once, immediately when ha_rocksdb.so is dlopened [1] and it doesn't seem to be freed anywhere. This triggers leak sanitizer in ASAN builds. It can perhaps exhaust memory if one installs/uninstalls (or runs show plugin soname) in a loop.

[1] static variable rocksdb_db_options initialized by calling rdb_init_rocksdb_db_options() that calls DBOptions::DBOptions(), Env::Default(), ThreadLocalPtr::InitSingletons(), ThreadLocalPtr::Instance().

Explicit delete seems to help:

diff --git a/util/thread_local.cc b/util/thread_local.cc
--- a/util/thread_local.cc
+++ b/util/thread_local.cc
@@ -335,6 +335,7 @@ ThreadLocalPtr::StaticMeta::StaticMeta()
       if (tls_) {
         OnThreadExit(tls_);
       }
+      delete Instance();
     }
   } a;
 #endif  // !defined(OS_WIN)



 Comments   
Comment by Elena Stepanova [ 2023-10-11 ]

It also makes RocksDB destructive for MSAN builds – not only tests cannot be run with RocksDB, but the presence of the library itself is dangerous as for example queries from I_S.ALL_PLUGINS fail with

11.2 872ed534

==2569534==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x7f52ed5c2c78 in rocksdb::ThreadLocalPtr::StaticMeta::StaticMeta()::A::~A() /data/src/11.2-msan/storage/rocksdb/rocksdb/util/thread_local.cc:335:11
    #1 0x562c05f7e0eb in MSanCxaAtExitWrapper(void*) (/mnt8t/src/11.2-msan/sql/mariadbd+0x7c50eb)
    #2 0x7f5311733f36 in __cxa_finalize stdlib/./stdlib/cxa_finalize.c:83:6
    #3 0x7f52ec670166 in __do_global_dtors_aux (/mnt8t/src/11.2-msan/lib/plugin/ha_rocksdb.so+0x270166)
 
  Uninitialized value was created by a heap allocation
    #0 0x562c05f7d8bd in malloc (/mnt8t/src/11.2-msan/sql/mariadbd+0x7c48bd)
    #1 0x7f5311c24ecd in malloc elf/../include/rtld-malloc.h:56:10
    #2 0x7f5311c24ecd in allocate_dtv_entry elf/../elf/dl-tls.c:684:19
    #3 0x7f5311c24ecd in allocate_and_init elf/../elf/dl-tls.c:709:31
    #4 0x7f5311c24ecd in tls_get_addr_tail elf/../elf/dl-tls.c:907:31
 
SUMMARY: MemorySanitizer: use-of-uninitialized-value /data/src/11.2-msan/storage/rocksdb/rocksdb/util/thread_local.cc:335:11 in rocksdb::ThreadLocalPtr::StaticMeta::StaticMeta()::A::~A()

Generated at Thu Feb 08 09:16:34 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.