Skip to content

Commit ba4509f

Browse files
committed
[libc][stdlib] Fix UB in freelist
Some of the freelist code uses type punning which is UB in C++, namely because we read from a union member that is not the active union member. For cases we used this, we should either use memcpy for storing or reinterpret_cast<cpp::byte*> for comparing.
1 parent f074500 commit ba4509f

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

libc/src/stdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ else()
401401
libc.src.__support.CPP.cstddef
402402
libc.src.__support.CPP.array
403403
libc.src.__support.CPP.span
404+
libc.src.string.memcpy
404405
)
405406
add_entrypoint_external(
406407
malloc

libc/src/stdlib/freelist.h

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "src/__support/CPP/cstddef.h"
1414
#include "src/__support/CPP/span.h"
1515
#include "src/__support/fixedvector.h"
16+
#include "src/string/memcpy.h"
1617

1718
namespace LIBC_NAMESPACE {
1819

@@ -92,19 +93,14 @@ bool FreeList<NUM_BUCKETS>::add_chunk(span<cpp::byte> chunk) {
9293
if (chunk.size() < sizeof(FreeListNode))
9394
return false;
9495

95-
union {
96-
FreeListNode *node;
97-
cpp::byte *bytes;
98-
} aliased;
99-
100-
aliased.bytes = chunk.data();
101-
96+
// Add it to the correct list.
10297
size_t chunk_ptr = find_chunk_ptr_for_size(chunk.size(), false);
10398

104-
// Add it to the correct list.
105-
aliased.node->size = chunk.size();
106-
aliased.node->next = chunks_[chunk_ptr];
107-
chunks_[chunk_ptr] = aliased.node;
99+
FreeListNode node;
100+
node.next = chunks_[chunk_ptr];
101+
node.size = chunk.size();
102+
LIBC_NAMESPACE::memcpy(chunk.data(), &node, sizeof(node));
103+
chunks_[chunk_ptr] = reinterpret_cast<FreeListNode *>(chunk.data());
108104

109105
return true;
110106
}
@@ -123,17 +119,13 @@ span<cpp::byte> FreeList<NUM_BUCKETS>::find_chunk(size_t size) const {
123119

124120
// Now iterate up the buckets, walking each list to find a good candidate
125121
for (size_t i = chunk_ptr; i < chunks_.size(); i++) {
126-
union {
127-
FreeListNode *node;
128-
cpp::byte *data;
129-
} aliased;
130-
aliased.node = chunks_[static_cast<unsigned short>(i)];
122+
FreeListNode *node = chunks_[static_cast<unsigned short>(i)];
131123

132-
while (aliased.node != nullptr) {
133-
if (aliased.node->size >= size)
134-
return span<cpp::byte>(aliased.data, aliased.node->size);
124+
while (node != nullptr) {
125+
if (node->size >= size)
126+
return span<cpp::byte>(reinterpret_cast<cpp::byte *>(node), node->size);
135127

136-
aliased.node = aliased.node->next;
128+
node = node->next;
137129
}
138130
}
139131

@@ -150,30 +142,29 @@ bool FreeList<NUM_BUCKETS>::remove_chunk(span<cpp::byte> chunk) {
150142
union {
151143
FreeListNode *node;
152144
cpp::byte *data;
153-
} aliased, aliased_next;
145+
} aliased_next;
154146

155147
// Check head first.
156148
if (chunks_[chunk_ptr] == nullptr)
157149
return false;
158150

159-
aliased.node = chunks_[chunk_ptr];
160-
if (aliased.data == chunk.data()) {
161-
chunks_[chunk_ptr] = aliased.node->next;
151+
FreeListNode *node = chunks_[chunk_ptr];
152+
if (reinterpret_cast<cpp::byte *>(node) == chunk.data()) {
153+
chunks_[chunk_ptr] = node->next;
162154
return true;
163155
}
164156

165157
// No? Walk the nodes.
166-
aliased.node = chunks_[chunk_ptr];
158+
node = chunks_[chunk_ptr];
167159

168-
while (aliased.node->next != nullptr) {
169-
aliased_next.node = aliased.node->next;
170-
if (aliased_next.data == chunk.data()) {
160+
while (node->next != nullptr) {
161+
if (reinterpret_cast<cpp::byte *>(node->next) == chunk.data()) {
171162
// Found it, remove this node out of the chain
172-
aliased.node->next = aliased_next.node->next;
163+
node->next = node->next->next;
173164
return true;
174165
}
175166

176-
aliased.node = aliased.node->next;
167+
node = node->next;
177168
}
178169

179170
return false;

0 commit comments

Comments
 (0)