Skip to content

Commit

Permalink
Make sure we pad variant allocations with 0-bytes (LLNL#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
daboehme authored May 23, 2023
1 parent 7ffe041 commit 1f56c8e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/caliper/MemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ struct MemoryPool::MemoryPoolImpl
void expand(size_t bytes) {
size_t len = max((bytes+sizeof(uint64_t)-1)/sizeof(uint64_t), chunksize);

m_chunks.push_back( { new uint64_t[len], 0, len } );
uint64_t* ptr = new uint64_t[len];
std::fill_n(ptr, len, 0);

m_chunks.push_back( { ptr, 0, len } );

m_total_reserved += len;
}
Expand Down
10 changes: 6 additions & 4 deletions src/caliper/MetadataTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data,
char* ptr = nullptr;

if (copy) {
for (size_t i = 0; i < n; ++i)
data_size += data[i].size() + (align - data[i].size()%align);
for (size_t i = 0; i < n; ++i) {
// ensure all allocations are aligned and have 0-padding so we can safely hand out string ptrs
data_size += data[i].size() + (align - (data[i].size()+1)%align);
}

ptr = static_cast<char*>(m_mempool.allocate(data_size));

Expand All @@ -182,7 +184,7 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data,

if (copy) {
dptr = memcpy(ptr, dptr, size);
ptr += size+(align-size%align);
ptr += size + (align-(size+1)%align);
}

size_t index = m_nodeblock->index++;
Expand Down Expand Up @@ -212,7 +214,7 @@ MetadataTree::create_child(const Attribute& attr, const Variant& value, Node* pa
void* ptr = nullptr;

if (value.has_unmanaged_data())
ptr = m_mempool.allocate(value.size());
ptr = m_mempool.allocate(value.size() + 1 /* ensure 0-padding so we can safely hand out string ptrs */);

size_t index = m_nodeblock->index++;
GlobalData* g = mG.load();
Expand Down

0 comments on commit 1f56c8e

Please sign in to comment.