Skip to content

Commit 1c05238

Browse files
committed
Convert heap_allocate null pointer checks to valid tag checks.
Pointers returned by `heap_allocate` should be checked for validity through the tag bit, not by comparing them with `nullptr`. Go through all allocation sites of the repository and update where needed. This does the same as b35d1d944fd9038b0c821e9e51d2b6822dcc8cf8 in the main tree. Signed-off-by: Hugo Lefeuvre <[email protected]>
1 parent f7844cd commit 1c05238

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

lib/tcpip/BufferManagement.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
using Debug = ConditionalDebug<false, "Buffer management">;
1212

13+
using CHERI::Capability;
14+
1315
// Use a separate allocator quota for the buffer manager (false by default).
1416
// The buffer manager is responsible for allocating network buffers, which
1517
// differs from the other types of allocations the TCP/IP stack performs. It
@@ -77,14 +79,14 @@ pxGetNetworkBufferWithDescriptor(size_t xRequestedSizeBytes,
7779
static_cast<NetworkBufferDescriptor_t *>(heap_allocate(
7880
&t, BM_MALLOC_CAPABILITY, sizeof(NetworkBufferDescriptor_t))),
7981
deleter};
80-
if (descriptor == nullptr)
82+
if (!Capability{descriptor.get()}.is_valid())
8183
{
8284
Debug::log("Failed to allocate descriptor");
8385
return nullptr;
8486
}
8587
auto *buffer = static_cast<uint8_t *>(heap_allocate(
8688
&t, BM_MALLOC_CAPABILITY, xRequestedSizeBytes + ipBUFFER_PADDING));
87-
if (buffer == nullptr)
89+
if (!Capability{buffer}.is_valid())
8890
{
8991
Debug::log("Failed to allocate {} byte buffer", xRequestedSizeBytes);
9092
return nullptr;

lib/tcpip/network_wrapper.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ NetworkReceiveResult network_socket_receive(Timeout *timeout,
796796
buffer = static_cast<uint8_t *>(
797797
heap_allocate(&zeroTimeout, mallocCapability, available));
798798
timeout->elapse(zeroTimeout.elapsed);
799-
if (buffer == nullptr)
799+
if (!Capability{buffer}.is_valid())
800800
{
801801
// If there's a lot of data, just try a small
802802
// allocation and see if that works.
@@ -825,7 +825,7 @@ NetworkReceiveResult network_socket_receive(Timeout *timeout,
825825
available = -ENOMEM;
826826
return nullptr;
827827
}
828-
} while (buffer == nullptr);
828+
} while (!Capability{buffer}.is_valid());
829829
return buffer;
830830
},
831831
[&](void *buffer) -> void { heap_free(mallocCapability, buffer); });

lib/tls/tls.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ SObj tls_connection_create(Timeout *t,
414414
static_cast<br_ssl_client_context *>(
415415
heap_allocate(t, allocator, sizeof(br_ssl_client_context))),
416416
deleter};
417-
if (clientContext == nullptr)
417+
if (!Capability{clientContext.get()}.is_valid())
418418
{
419419
Debug::log("Failed to allocate client context");
420420
return nullptr;
@@ -424,7 +424,7 @@ SObj tls_connection_create(Timeout *t,
424424
heap_allocate(t, allocator, sizeof(br_x509_minimal_context))),
425425
deleter};
426426
auto *engine = &clientContext->eng;
427-
if (x509Context == nullptr)
427+
if (!Capability{x509Context.get()}.is_valid())
428428
{
429429
Debug::log("Failed to allocate X509 context");
430430
return nullptr;
@@ -442,7 +442,8 @@ SObj tls_connection_create(Timeout *t,
442442
static_cast<unsigned char *>(
443443
heap_allocate(t, allocator, MinimumBufferSize)),
444444
deleter};
445-
if (iobufIn == nullptr || iobufOut == nullptr)
445+
if (!Capability{iobufIn.get()}.is_valid() ||
446+
!Capability{iobufOut.get()}.is_valid())
446447
{
447448
Debug::log("Failed to allocate buffers");
448449
return nullptr;
@@ -638,7 +639,7 @@ NetworkReceiveResult tls_connection_receive(Timeout *t, SObj sealedConnection)
638639
heap_allocate(&zeroTimeout, mallocCapability, available));
639640
t->elapse(zeroTimeout.elapsed);
640641

641-
if (buffer == nullptr)
642+
if (!Capability{buffer}.is_valid())
642643
{
643644
// If there's a lot of data, just try a small
644645
// allocation and see if that works.
@@ -667,7 +668,7 @@ NetworkReceiveResult tls_connection_receive(Timeout *t, SObj sealedConnection)
667668
available = -ENOMEM;
668669
return nullptr;
669670
}
670-
} while (buffer == nullptr);
671+
} while (!Capability{buffer}.is_valid());
671672
return buffer;
672673
});
673674
return {result, buffer};

0 commit comments

Comments
 (0)