openvino icon indicating copy to clipboard operation
openvino copied to clipboard

[Bug]: LSAN reports leaks in ONNX frontend

Open vient opened this issue 1 year ago • 1 comments

OpenVINO Version

2024.0.0

Operating System

Ubuntu 20.04 (LTS)

Device used for inference

CPU

Framework

None

Model used

No response

Issue description

ONNX frontend uses protobuf library which allocates some memory on startup and expects user to call google::protobuf::ShutdownProtobufLibrary() on exit. OpenVINO does it here https://github.com/openvinotoolkit/openvino/blob/releases/2024/0/src/frontends/common/shutdown_protobuf/shutdown_protobuf.cpp#L27-L30 but, at least in my builds, this function does not get called, it is not even present in final libopenvino_onnx_frontend.so.2024.0.0.

I've checked that it is there in libopenvino_protobuf_shutdown.a, and the issue seems to be that __attribute__((destructor)) does nothing, library_unload is not actually registered as destructor. Adding -Wl,--whole-archive to libopenvino_protobuf_shutdown.a linkage does not help - library_unload is present in final .so after this but is not called anywhere.

I've fixed this for myself by replacing this function with

struct ProtobufsPleaseShutdown {
    ~ProtobufsPleaseShutdown() {
        google::protobuf::ShutdownProtobufLibrary();
    }
} protobuf_killer;

Step-by-step reproduction

No response

Relevant log output

No response

Issue submission checklist

  • [X] I'm reporting an issue. It's not a question.
  • [X] I checked the problem with the documentation, FAQ, open issues, Stack Overflow, etc., and have not found a solution.
  • [X] There is reproducer code and related data files such as images, videos, models, etc.

vient avatar May 22 '24 13:05 vient

Correction: I also changed openvino::protobuf_shutdown to "$<LINK_LIBRARY:WHOLE_ARCHIVE,openvino::protobuf_shutdown>" here https://github.com/openvinotoolkit/openvino/blob/releases/2024/0/cmake/developer_package/frontends/frontends.cmake#L204. Without it linker throws away protobuf_killer object. Maybe it is enough to declare it as extern "C" or explicitly set visibility instead, did not try it.

LINK_LIBRARY:WHOLE_ARCHIVE is pretty fresh, don't know what cmake version you target. It it is too new, it is possible to create a proxy library instead which has explicit -Wl,--whole-archive in its link options.

Note that whole-archive does not help by itself, I still needed to replace destructor function with global object.

My toolchain is LLVM 18, GCC 14 (stdlib provider), cmake 3.29, ninja 1.11 on Ubuntu 20.04

vient avatar May 22 '24 16:05 vient

@vient thank you for information, I'll check it!

gkrivor avatar Jul 26 '24 08:07 gkrivor