Skip to content

Commit 4837c82

Browse files
Fix external pagemap usage. (#221)
At some point in the refactoring, these were broken.
1 parent c89f594 commit 4837c82

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/mem/chunkmap.h

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,22 @@ namespace snmalloc
139139

140140
public:
141141
/**
142-
* Constructor. Accesses the pagemap via the C ABI accessor and casts it to
142+
* Returns the exported pagemap.
143+
* Accesses the pagemap via the C ABI accessor and casts it to
143144
* the expected type, failing in cases of ABI mismatch.
144145
*/
145-
ExternalGlobalPagemap()
146+
static ChunkmapPagemap& pagemap()
146147
{
147-
const snmalloc::PagemapConfig* c;
148-
external_pagemap =
149-
ChunkmapPagemap::cast_to_pagemap(snmalloc_pagemap_global_get(&c), c);
150-
if (!external_pagemap)
148+
if (external_pagemap == nullptr)
151149
{
152-
Pal::error("Incorrect ABI of global pagemap.");
150+
const snmalloc::PagemapConfig* c = nullptr;
151+
void* raw_pagemap = snmalloc_pagemap_global_get(&c);
152+
external_pagemap = ChunkmapPagemap::cast_to_pagemap(raw_pagemap, c);
153+
if (!external_pagemap)
154+
{
155+
Pal::error("Incorrect ABI of global pagemap.");
156+
}
153157
}
154-
}
155-
156-
/**
157-
* Returns the exported pagemap.
158-
*/
159-
static ChunkmapPagemap& pagemap()
160-
{
161158
return *external_pagemap;
162159
}
163160
};

src/mem/pagemap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ namespace snmalloc
395395
*/
396396
size_t index_for_address(uintptr_t p)
397397
{
398-
return bits::align_down(static_cast<size_t>(p) >> SHIFT, OS_PAGE_SIZE);
398+
return (static_cast<size_t>(p) >> SHIFT) % OS_PAGE_SIZE;
399399
}
400400

401401
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#if defined(SNMALLOC_PASS_THROUGH) || defined(_WIN32)
2+
// This test does not make sense with malloc pass-through, skip it.
3+
// The malloc definitions are also currently incompatible with Windows headers
4+
// so skip this test on Windows as well.
5+
int main()
6+
{
7+
return 0;
8+
}
9+
#else
10+
# define SNMALLOC_EXPOSE_PAGEMAP 1
11+
# include <override/malloc.cc>
12+
13+
int main()
14+
{
15+
auto& p = ExternalGlobalPagemap::pagemap();
16+
auto& global = GlobalPagemap::pagemap();
17+
SNMALLOC_CHECK(&p == &global);
18+
// Get a valid heap address
19+
uintptr_t addr = reinterpret_cast<uintptr_t>(malloc(42));
20+
// Make this very strongly aligned
21+
addr &= ~0xfffffULL;
22+
void* page = p.page_for_address(addr);
23+
SNMALLOC_CHECK(page == p.page_for_address(addr + 128));
24+
size_t idx = p.index_for_address(addr);
25+
size_t idx2 = p.index_for_address(addr + SUPERSLAB_SIZE);
26+
// If the pagemap ends up storing things that are not uint8_t, this test
27+
// will need modifying.
28+
SNMALLOC_CHECK(idx2 = ((idx + 1) % OS_PAGE_SIZE));
29+
}
30+
#endif

0 commit comments

Comments
 (0)