From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: [musl] mallocng switchover - opportunity to test
Date: Mon, 8 Jun 2020 23:50:10 -0400 [thread overview]
Message-ID: <20200609035010.GE1079@brightrain.aerifal.cx> (raw)
[-- Attachment #1: Type: text/plain, Size: 829 bytes --]
I just pushed a series of changes in preparation for upstreaming
mallocng. Before it's actually imported, it can be tested by
performing the following simple 4 steps:
1. mkdir src/malloc/mallocng
2. echo "MALLOC_DIR = mallocng" >> config.mak
3. Dropping the attached files into src/malloc/mallocng
4. Symlinking or copying meta.h, malloc.c, realloc.c, free.c,
malloc_usable_size.c, and aligned_alloc.c from mallocng source dir
to src/malloc/mallocng. (You can also include dump.c if desired.)
This produces a near-fully-integrated malloc, including support for
reclaim_gaps donation from ldso. The only functionality missing, which
I expect to flesh out before actual import, is handling of the case of
incomplete malloc replacement by interposition (__malloc_replaced!=0).
Please report any problems encountered.
Rich
[-- Attachment #2: glue.h --]
[-- Type: text/plain, Size: 1754 bytes --]
#ifndef MALLOC_GLUE_H
#define MALLOC_GLUE_H
#include <stdint.h>
#include <sys/mman.h>
#include <pthread.h>
#include <unistd.h>
#include <elf.h>
#include <string.h>
#include "atomic.h"
#include "syscall.h"
#include "libc.h"
#include "lock.h"
// use macros to appropriately namespace these.
#define size_classes __malloc_size_classes
#define ctx __malloc_context
#define alloc_meta __malloc_alloc_meta
#define dump_heap __dump_heap
#if USE_REAL_ASSERT
#include <assert.h>
#else
#undef assert
#define assert(x) do { if (!(x)) a_crash(); } while(0)
#endif
#define brk(p) ((uintptr_t)__syscall(SYS_brk, p))
#define mmap __mmap
#define madvise __madvise
#define mremap __mremap
#ifndef a_clz_32
#define a_clz_32 a_clz_32
static inline int a_clz_32(uint32_t x)
{
#ifdef __GNUC__
return __builtin_clz(x);
#else
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return 31-a_ctz_32(x);
#endif
}
#endif
static inline uint64_t get_random_secret()
{
uint64_t secret = (uintptr_t)&secret * 1103515245;
for (size_t i=0; libc.auxv[i]; i+=2)
if (libc.auxv[i]==AT_RANDOM)
memcpy(&secret, (char *)libc.auxv[i+1], sizeof secret);
return secret;
}
#ifndef PAGESIZE
#define PAGESIZE PAGE_SIZE
#endif
static inline size_t get_page_size()
{
return PAGESIZE;
}
// no portable "is multithreaded" predicate so assume true
#define MT (libc.need_locks)
#define RDLOCK_IS_EXCLUSIVE 1
__attribute__((__visibility__("hidden")))
extern int __malloc_lock[1];
#define LOCK_OBJ_DEF \
int __malloc_lock[1];
static inline void rdlock()
{
if (MT) LOCK(__malloc_lock);
}
static inline void wrlock()
{
if (MT) LOCK(__malloc_lock);
}
static inline void unlock()
{
UNLOCK(__malloc_lock);
}
static inline void upgradelock()
{
}
#endif
[-- Attachment #3: donate.c --]
[-- Type: text/plain, Size: 881 bytes --]
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <sys/mman.h>
#include <errno.h>
#include "meta.h"
static void donate(unsigned char *base, size_t len)
{
uintptr_t a = (uintptr_t)base;
uintptr_t b = a + len;
a += -a & (UNIT-1);
b -= b & (UNIT-1);
memset(base, 0, len);
for (int sc=47; sc>0 && b>a; sc-=4) {
if (b-a < (size_classes[sc]+1)*UNIT) continue;
struct meta *m = alloc_meta();
m->avail_mask = 0;
m->freed_mask = 1;
m->mem = (void *)a;
m->mem->meta = m;
m->last_idx = 0;
m->freeable = 0;
m->sizeclass = sc;
m->maplen = 0;
*((unsigned char *)m->mem+UNIT-4) = 0;
*((unsigned char *)m->mem+UNIT-3) = 255;
m->mem->storage[size_classes[sc]*UNIT-4] = 0;
queue(&ctx.active[sc], m);
a += (size_classes[sc]+1)*UNIT;
}
}
void __malloc_donate(char *start, char *end)
{
donate((void *)start, end-start);
}
[-- Attachment #4: replaced.c --]
[-- Type: text/plain, Size: 42 bytes --]
#include "meta.h"
int __malloc_replaced;
next reply other threads:[~2020-06-09 3:50 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-09 3:50 Rich Felker [this message]
2020-06-09 11:09 ` Szabolcs Nagy
2020-06-09 20:08 ` Rich Felker
2020-06-10 0:58 ` Rich Felker
2020-06-10 8:33 ` Szabolcs Nagy
2020-06-11 20:05 ` Rich Felker
2020-06-12 17:42 ` Szabolcs Nagy
2020-06-10 9:19 ` Szabolcs Nagy
2020-06-10 16:41 ` Rich Felker
2020-06-11 3:49 ` Rich Felker
2020-06-11 4:33 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200609035010.GE1079@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).