blob: 61e79896c373e4a5ce26b0bc35bb1a09349e0062 [file] [log] [blame]
Viet-Trung Luu96b05c12016-01-11 11:26:36 -08001#include <limits.h>
2#include <stdint.h>
3#include <errno.h>
4#include <sys/mman.h>
5#include "libc.h"
6#include "syscall.h"
7
8/* This function returns true if the interval [old,new]
9 * intersects the 'len'-sized interval below &libc.auxv
10 * (interpreted as the main-thread stack) or below &b
11 * (the current stack). It is used to defend against
12 * buggy brk implementations that can cross the stack. */
13
George Kulakowski17e3b042016-02-18 15:59:50 -080014static int traverses_stack_p(uintptr_t old, uintptr_t new) {
15 const uintptr_t len = 8 << 20;
16 uintptr_t a, b;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080017
George Kulakowski17e3b042016-02-18 15:59:50 -080018 b = (uintptr_t)libc.auxv;
19 a = b > len ? b - len : 0;
20 if (new > a && old < b)
21 return 1;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080022
George Kulakowski17e3b042016-02-18 15:59:50 -080023 b = (uintptr_t)&b;
24 a = b > len ? b - len : 0;
25 if (new > a && old < b)
26 return 1;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080027
George Kulakowski17e3b042016-02-18 15:59:50 -080028 return 0;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080029}
30
George Kulakowski17e3b042016-02-18 15:59:50 -080031void* __mmap(void*, size_t, int, int, int, off_t);
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080032
33/* Expand the heap in-place if brk can be used, or otherwise via mmap,
34 * using an exponential lower bound on growth by mmap to make
35 * fragmentation asymptotically irrelevant. The size argument is both
36 * an input and an output, since the caller needs to know the size
37 * allocated, which will be larger than requested due to page alignment
38 * and mmap minimum size rules. The caller is responsible for locking
39 * to prevent concurrent calls. */
40
George Kulakowski17e3b042016-02-18 15:59:50 -080041void* __expand_heap(size_t* pn) {
42 static uintptr_t brk;
43 static unsigned mmap_step;
44 size_t n = *pn;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080045
George Kulakowski17e3b042016-02-18 15:59:50 -080046 if (n > SIZE_MAX / 2 - PAGE_SIZE) {
47 errno = ENOMEM;
48 return 0;
49 }
50 n += -n & PAGE_SIZE - 1;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080051
George Kulakowski17e3b042016-02-18 15:59:50 -080052 if (!brk) {
53 brk = __syscall(SYS_brk, 0);
54 brk += -brk & PAGE_SIZE - 1;
55 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080056
George Kulakowski17e3b042016-02-18 15:59:50 -080057 if (n < SIZE_MAX - brk && !traverses_stack_p(brk, brk + n) &&
58 __syscall(SYS_brk, brk + n) == brk + n) {
59 *pn = n;
60 brk += n;
61 return (void*)(brk - n);
62 }
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080063
George Kulakowski17e3b042016-02-18 15:59:50 -080064 size_t min = (size_t)PAGE_SIZE << mmap_step / 2;
65 if (n < min)
66 n = min;
67 void* area =
68 __mmap(0, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
69 if (area == MAP_FAILED)
70 return 0;
71 *pn = n;
72 mmap_step++;
73 return area;
Viet-Trung Luu96b05c12016-01-11 11:26:36 -080074}