Skip to content

Commit

Permalink
Fix binding model tests that use external funcs
Browse files Browse the repository at this point in the history
In binding model tests, the external function array was not created
properly. This lead to SIGILL (invalid instructions) on delete[]-ing
the array. The offending construct has been replaced by a std::vector.
  • Loading branch information
sleweke committed May 25, 2022
1 parent 8070044 commit 761d9cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
22 changes: 15 additions & 7 deletions test/BindingModelTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ namespace binding
ConfiguredBindingModel::~ConfiguredBindingModel()
{
delete _binding;
delete[] _extFuns;
delete[] _boundOffset;

for (cadet::IExternalFunction*& ef : _extFuns)
delete ef;

::operator delete(_bufferMemory);
}

Expand Down Expand Up @@ -86,8 +88,11 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned
}

// Assign external functions
cadet::IExternalFunction* extFuns = new LinearExternalFunction[50];
bm->setExternalFunctions(&extFuns, 50);
std::vector<cadet::IExternalFunction*> extFuns(50, nullptr);
for (int i = 0; i < 50; ++i)
extFuns[i] = new LinearExternalFunction();

bm->setExternalFunctions(extFuns.data(), 50);

// Allocate memory buffer
unsigned int requiredMem = 0;
Expand All @@ -103,7 +108,7 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned
std::memset(buffer, 0, requiredMem);
}

return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, extFuns);
return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, std::move(extFuns));
}

ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned int nComp, unsigned int const* nBound, int const* isKinetic, const char* config)
Expand All @@ -130,8 +135,11 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned
}

// Assign external functions
cadet::IExternalFunction* extFuns = new LinearExternalFunction[50];
bm->setExternalFunctions(&extFuns, 50);
std::vector<cadet::IExternalFunction*> extFuns(50, nullptr);
for (int i = 0; i < 50; ++i)
extFuns[i] = new LinearExternalFunction();

bm->setExternalFunctions(extFuns.data(), 50);

// Allocate memory buffer
unsigned int requiredMem = 0;
Expand All @@ -147,7 +155,7 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned
std::memset(buffer, 0, requiredMem);
}

return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, extFuns);
return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, std::move(extFuns));
}

void ConfiguredBindingModel::increaseBufferSize(int inc)
Expand Down
12 changes: 5 additions & 7 deletions test/BindingModelTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ namespace binding
public:

ConfiguredBindingModel(ConfiguredBindingModel&& cpy) CADET_NOEXCEPT
: _binding(cpy._binding), _nComp(cpy._nComp), _nBound(cpy._nBound), _boundOffset(cpy._boundOffset), _buffer(std::move(cpy._buffer)), _bufferMemory(cpy._bufferMemory), _extFuns(cpy._extFuns)
: _binding(cpy._binding), _nComp(cpy._nComp), _nBound(cpy._nBound), _boundOffset(cpy._boundOffset), _buffer(std::move(cpy._buffer)), _bufferMemory(cpy._bufferMemory), _extFuns(std::move(cpy._extFuns))
{
cpy._binding = nullptr;
cpy._nBound = nullptr;
cpy._boundOffset = nullptr;
cpy._bufferMemory = nullptr;
cpy._extFuns = nullptr;
}

~ConfiguredBindingModel();
Expand All @@ -80,13 +79,12 @@ namespace binding
_boundOffset = cpy._boundOffset;
_buffer = std::move(cpy._buffer);
_bufferMemory = cpy._bufferMemory;
_extFuns = cpy._extFuns;
_extFuns = std::move(cpy._extFuns);

cpy._binding = nullptr;
cpy._nBound = nullptr;
cpy._boundOffset = nullptr;
cpy._bufferMemory = nullptr;
cpy._extFuns = nullptr;

return *this;
}
Expand All @@ -109,8 +107,8 @@ namespace binding

private:

ConfiguredBindingModel(cadet::model::IBindingModel* binding, unsigned int nComp, unsigned int const* nBound, unsigned int const* boundOffset, void* bufferStart, void* bufferEnd, cadet::IExternalFunction* extFuns)
: _binding(binding), _nComp(nComp), _nBound(nBound), _boundOffset(boundOffset), _buffer(bufferStart, bufferEnd), _bufferMemory(bufferStart), _extFuns(extFuns)
ConfiguredBindingModel(cadet::model::IBindingModel* binding, unsigned int nComp, unsigned int const* nBound, unsigned int const* boundOffset, void* bufferStart, void* bufferEnd, std::vector<cadet::IExternalFunction*>&& extFuns)
: _binding(binding), _nComp(nComp), _nBound(nBound), _boundOffset(boundOffset), _buffer(bufferStart, bufferEnd), _bufferMemory(bufferStart), _extFuns(std::move(extFuns))
{
}

Expand All @@ -120,7 +118,7 @@ namespace binding
unsigned int const* _boundOffset;
cadet::LinearBufferAllocator _buffer;
void* _bufferMemory;
cadet::IExternalFunction* _extFuns;
std::vector<cadet::IExternalFunction*> _extFuns;
};

/**
Expand Down

0 comments on commit 761d9cd

Please sign in to comment.