mailing list of musl libc
 help / color / mirror / code / Atom feed
ace2b6ade35f3dea0a137ed17ae724996be195f1 blob 1867 bytes (raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include "libc.h"
#include "syscall.h"

/* This function returns true if the interval [old,new]
 * intersects the 'len'-sized interval below &libc.auxv
 * (interpreted as the main-thread stack) or below &b
 * (the current stack). It is used to defend against
 * buggy brk implementations that can cross the stack. */

static int traverses_stack_p(uintptr_t old, uintptr_t new)
{
#if !SAFE_STACK
	const uintptr_t len = 8<<20;
	uintptr_t a, b;

	b = (uintptr_t)libc.auxv;
	a = b > len ? b-len : 0;
	if (new>a && old<b) return 1;

	b = (uintptr_t)&b;
	a = b > len ? b-len : 0;
	if (new>a && old<b) return 1;
#endif

	return 0;
}

void *__mmap(void *, size_t, int, int, int, off_t);

/* Expand the heap in-place if brk can be used, or otherwise via mmap,
 * using an exponential lower bound on growth by mmap to make
 * fragmentation asymptotically irrelevant. The size argument is both
 * an input and an output, since the caller needs to know the size
 * allocated, which will be larger than requested due to page alignment
 * and mmap minimum size rules. The caller is responsible for locking
 * to prevent concurrent calls. */

void *__expand_heap(size_t *pn)
{
	static uintptr_t brk;
	static unsigned mmap_step;
	size_t n = *pn;

	if (n > SIZE_MAX/2 - PAGE_SIZE) {
		errno = ENOMEM;
		return 0;
	}
	n += -n & PAGE_SIZE-1;

	if (!brk) {
		brk = __syscall(SYS_brk, 0);
		brk += -brk & PAGE_SIZE-1;
	}

	if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
	    && __syscall(SYS_brk, brk+n)==brk+n) {
		*pn = n;
		brk += n;
		return (void *)(brk-n);
	}

	size_t min = (size_t)PAGE_SIZE << mmap_step/2;
	if (n < min) n = min;
	void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
		MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
	if (area == MAP_FAILED) return 0;
	*pn = n;
	mmap_step++;
	return area;
}
debug log:

solving ace2b6a ...
found ace2b6a in https://inbox.vuxu.org/musl/3f8d9836-e1ed-183c-5b51-e6407ad1fcff@intel.com/ ||
	https://inbox.vuxu.org/musl/390CE752059EB848A71F4F676EBAB76D3AC2988F@ORSMSX114.amr.corp.intel.com/
found d8c0be7 in https://git.vuxu.org/mirror/musl/
preparing index
index prepared:
100644 d8c0be742b69f1739ad54b9d3df56849a983ef2b	src/malloc/expand_heap.c

applying [1/2] https://inbox.vuxu.org/musl/3f8d9836-e1ed-183c-5b51-e6407ad1fcff@intel.com/
diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
index d8c0be7..ace2b6a 100644

Checking patch src/malloc/expand_heap.c...
error: while searching for:

 static int traverses_stack_p(uintptr_t old, uintptr_t new)
 {
     const uintptr_t len = 8<<20;
     uintptr_t a, b;


error: patch failed: src/malloc/expand_heap.c:13
error: src/malloc/expand_heap.c: patch does not apply

git apply error: exit status=1
trying https://inbox.vuxu.org/musl/390CE752059EB848A71F4F676EBAB76D3AC2988F@ORSMSX114.amr.corp.intel.com/

applying [2/2] https://inbox.vuxu.org/musl/390CE752059EB848A71F4F676EBAB76D3AC2988F@ORSMSX114.amr.corp.intel.com/
diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
index d8c0be7..ace2b6a 100644

Checking patch src/malloc/expand_heap.c...
Applied patch src/malloc/expand_heap.c cleanly.

index at:
100644 ace2b6ade35f3dea0a137ed17ae724996be195f1	src/malloc/expand_heap.c

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).