Skip to content

Commit 1f56c8e

Browse files
authored
Make sure we pad variant allocations with 0-bytes (LLNL#486)
1 parent 7ffe041 commit 1f56c8e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/caliper/MemoryPool.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ struct MemoryPool::MemoryPoolImpl
4949
void expand(size_t bytes) {
5050
size_t len = max((bytes+sizeof(uint64_t)-1)/sizeof(uint64_t), chunksize);
5151

52-
m_chunks.push_back( { new uint64_t[len], 0, len } );
52+
uint64_t* ptr = new uint64_t[len];
53+
std::fill_n(ptr, len, 0);
54+
55+
m_chunks.push_back( { ptr, 0, len } );
5356

5457
m_total_reserved += len;
5558
}

src/caliper/MetadataTree.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ MetadataTree::create_path(const Attribute& attr, size_t n, const Variant* data,
161161
char* ptr = nullptr;
162162

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

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

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

183185
if (copy) {
184186
dptr = memcpy(ptr, dptr, size);
185-
ptr += size+(align-size%align);
187+
ptr += size + (align-(size+1)%align);
186188
}
187189

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

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

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

0 commit comments

Comments
 (0)