Skip to content

Commit 7fbca11

Browse files
authored
0-length arrays in Buddy ranges (#672)
* backend_helpers: introduce NopRange * Fix to Buddy MIN == MAX case This fixes the 0-length arrays discussed (and made into assertion failures) in the next commit. This works because the Buddy's `MIN_SIZE_BITS` is instantiated at `MIN_CHUNK_BITS`, and so we ensure that we instantiate the LargeBuddyRange only with `max_page_chunk_size_bits` above `MIN_CHUNK_BITS`. * Buddy range: assert that MAX > MIN Now that the case leading to several 0-sized arrays in Buddy ranges, that then cause gcc's -Warray-bounds to trip, has been removed, add a static assert so that we can catch this with better error messages next time.
1 parent fcad154 commit 7fbca11

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

src/snmalloc/backend/meta_protected_range.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ namespace snmalloc
7575
CommitRange<PAL>,
7676
// In case of huge pages, we don't want to give each thread its own huge
7777
// page, so commit in the global range.
78-
LargeBuddyRange<
79-
max_page_chunk_size_bits,
80-
max_page_chunk_size_bits,
81-
Pagemap,
82-
page_size_bits>,
78+
std::conditional_t<
79+
(max_page_chunk_size_bits > MIN_CHUNK_BITS),
80+
LargeBuddyRange<
81+
max_page_chunk_size_bits,
82+
max_page_chunk_size_bits,
83+
Pagemap,
84+
page_size_bits>,
85+
NopRange>,
8386
LogRange<4>,
8487
GlobalRange,
8588
StatsRange>;

src/snmalloc/backend_helpers/backend_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "indirectrange.h"
1010
#include "largebuddyrange.h"
1111
#include "logrange.h"
12+
#include "noprange.h"
1213
#include "pagemap.h"
1314
#include "pagemapregisterrange.h"
1415
#include "palrange.h"

src/snmalloc/backend_helpers/buddy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace snmalloc
1515
template<typename Rep, size_t MIN_SIZE_BITS, size_t MAX_SIZE_BITS>
1616
class Buddy
1717
{
18+
static_assert(MAX_SIZE_BITS > MIN_SIZE_BITS);
19+
1820
struct Entry
1921
{
2022
typename Rep::Contents cache[3];
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
#include "range_helpers.h"
3+
4+
namespace snmalloc
5+
{
6+
struct NopRange
7+
{
8+
template<typename ParentRange>
9+
class Type : public ContainsParent<ParentRange>
10+
{
11+
using ContainsParent<ParentRange>::parent;
12+
13+
public:
14+
static constexpr bool Aligned = ParentRange::Aligned;
15+
16+
static constexpr bool ConcurrencySafe = ParentRange::ConcurrencySafe;
17+
18+
using ChunkBounds = typename ParentRange::ChunkBounds;
19+
static_assert(
20+
ChunkBounds::address_space_control ==
21+
capptr::dimension::AddressSpaceControl::Full);
22+
23+
constexpr Type() = default;
24+
25+
CapPtr<void, ChunkBounds> alloc_range(size_t size)
26+
{
27+
return parent.alloc_range(size);
28+
}
29+
30+
void dealloc_range(CapPtr<void, ChunkBounds> base, size_t size)
31+
{
32+
parent.dealloc_range(base, size);
33+
}
34+
};
35+
};
36+
} // namespace snmalloc

0 commit comments

Comments
 (0)