Skip to content

Commit

Permalink
mem leak reported with TVM runtime
Browse files Browse the repository at this point in the history
[Problem]
In TVM Runtime code, we have a comment that indicates the
manager instance and registry were deliberately leaked to
enable interoperability with Python clients. This shows
up as a leaked resource in C++ API clients.

[Solution]
Add a static guard to differentiate between C++ only clients
Allocate via make_shared()

Leak Report Before
```
2020-09-13T23:39:23.200-07:00    ==3194==ERROR: LeakSanitizer: detected
memory leaks
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 2560 byte(s) in 40
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12a187a57 in
tvm::runtime::Registry::Register(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&, bool)
(MYAPP/resnet18_v1/libdlr.so+0x72a57)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 2240 byte(s) in 40
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12a188c99 in
std::__detail::_Hash_node<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>, true>*
std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>, true> >
>::_M_allocate_node<std::piecewise_construct_t const&,
std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&>, std::tuple<>
>(std::piecewise_construct_t const&,
std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&>&&, std::tuple<>&&)
(MYAPP/resnet18_v1/libdlr.so+0x73c99)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 1116 byte(s) in 36
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12d83cafe in
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)
(/lib/x86_64-linux-gnu/libstdc++.so.6+0x142afe)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 866 byte(s) in 36
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12a188ce0 in
std::__detail::_Hash_node<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>, true>*
std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>, true> >
>::_M_allocate_node<std::piecewise_construct_t const&,
std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&>, std::tuple<>
>(std::piecewise_construct_t const&,
std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&>&&, std::tuple<>&&)
(MYAPP//resnet18_v1/libdlr.so+0x73ce0)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 464 byte(s) in 2
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12a188e0a in
std::_Hashtable<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>,
std::allocator<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*> >, std::__detail::_Select1st,
std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > >, std::hash<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash,
std::__detail::_Prime_rehash_policy,
std::__detail::_Hashtable_traits<true, false, true>
>::_M_insert_unique_node(unsigned long, unsigned long,
std::__detail::_Hash_node<std::pair<std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const,
tvm::runtime::Registry*>, true>*, unsigned long)
(MYAPP//resnet18_v1/libdlr.so+0x73e0a)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    Indirect leak of 192 byte(s) in 2
object(s) allocated from:
    2020-09-13T23:39:23.200-07:00    #0 0x5979ed in operator
new(unsigned long)
(MYAPP/core_unit_tests+0x5979ed)
    2020-09-13T23:39:23.200-07:00    neo-ai#1 0x7ff12a187aca in
tvm::runtime::Registry::Register(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&, bool)
(MYAPP//resnet18_v1/libdlr.so+0x72aca)
    2020-09-13T23:39:23.200-07:00
    2020-09-13T23:39:23.200-07:00    SUMMARY: AddressSanitizer: 7438
byte(s) leaked in 156 allocation(s).
```
  • Loading branch information
mycpuorg committed Oct 21, 2020
1 parent e41640d commit e80f22f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/runtime/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ struct Registry::Manager {
// We deliberately leak the Manager instance, to avoid leak sanitizers
// complaining about the entries in Manager::fmap being leaked at program
// exit.
#ifdef PYTHON_INTEROP_REGISTRY
static Manager* inst = new Manager();
return inst;
#else
std::shared_ptr<Manager> m_inst = std::make_shared<Manager>();
return m_inst.get();
#endif
}
};

Expand All @@ -68,9 +73,15 @@ Registry& Registry::Register(const std::string& name, bool can_override) { // N
CHECK(can_override) << "Global PackedFunc " << name << " is already registered";
}

Registry* r = new Registry();
Registry* r = nullptr;
#ifdef PYTHON_INTEROP_REGISTRY
r = new Registry();
r->name_ = name;
m->fmap[name] = r;
#else
std::shared_ptr<Registry> r_shared = std::make_shared<Registry>();
r = r_shared.get();
#endif
return *r;
}

Expand Down

0 comments on commit e80f22f

Please sign in to comment.