CHAI icon indicating copy to clipboard operation
CHAI copied to clipboard

Double free corruption when chai is used both in a shared object and an executable linking to it

Open sframba opened this issue 2 years ago • 0 comments

Hi, I don't know if this is a potential issue or simply a misuse of the library. When I have a a shared object and a main executable, both compiling against (the static library) chai, and where the executable links dynamically to my shared object, I get a double free error at the end of execution. This is most likely due to the static block that registers the chai plugin being called twice, which leads to chai being added twice to the list of the plugins. When exiting, the cleanup is done twice on the same object. Here's a minimal working example. The shared object's header:

$ cat testchai.hpp
#ifndef TESTCHAI_H
#define TESTCHAI_H
#include "chai/ArrayManager.hpp"
class TestChai
{
  public:
  void testChai();
};
#endif

and cpp file:

$ cat testchai.cpp
#include "testchai.hpp"
void TestChai::testChai()
{
  chai::ArrayManager *rm = chai::ArrayManager::getInstance();
}

and the main executable:

$ cat testchaimain.cpp
#include "chai/ArrayManager.hpp"
#include "testchai.hpp"
int main()
{
  chai::ArrayManager *rm = chai::ArrayManager::getInstance();
  TestChai t;
  t.testChai();
 return 0;
}

If now I compile the shared object:

$ g++ -o testchai.o -c testchai.cpp -I/path/to/chai/include -I/path/to/raja/include
$ g++ -shared -o testchai.so testchai.o /path/to/chai/lib/libchai.a /path/to/raja/libRAJA.a

and the executable:

$ g++ -o testchaimain.o -c testchaimain.cpp -I/path/to/chai/include -I/path/to/raja/include
$ g++ -o testchaimain testchaimain.o /path/to/chai/lib/libchai.a /path/to/chai/lib/libumpire.a /path/to/raja/libRAJA.a testchai.so

when I run I get a double free error:

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
$ ./testchaimain
free() double free detected in tchache 2
Aborted (core dumped)

Is this a proper issue or is it simply forbidden to have multiple objects compiled against the static chai library?

sframba avatar Sep 28 '22 07:09 sframba