forked from unikraft/app-elfloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brk.c
149 lines (130 loc) · 3.99 KB
/
brk.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Authors: Simon Kuenzer <[email protected]>
*
* Copyright (c) 2019, NEC Laboratories Europe GmbH,
* NEC Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <uk/alloc.h>
#include <uk/print.h>
#include <uk/syscall.h>
#include <uk/arch/limits.h>
#ifndef PAGES2BYTES
#define PAGES2BYTES(x) ((x) << __PAGE_SHIFT)
#endif
#define HEAP_PAGES CONFIG_APPELFLOADER_BRK_NBPAGES
#define HEAP_LEN PAGES2BYTES(CONFIG_APPELFLOADER_BRK_NBPAGES)
/*
* For now we only support one custom heap
*/
static void *base = NULL;
static void *brk_cur = NULL;
static void *zeroed = NULL;
static intptr_t len = 0;
UK_LLSYSCALL_R_DEFINE(void *, brk, void *, addr)
{
/* allocate brk context */
if (!base) {
base = uk_palloc(uk_alloc_get_default(), HEAP_PAGES);
if (!base) {
uk_pr_crit("Could not allocate memory for heap (%"PRIu64" KiB): Out of memory\n",
(uint64_t) HEAP_LEN / 1024);
return ERR2PTR(-ENOMEM);
}
/* initialize brk_cur with start of allocated heap region */
brk_cur = base;
zeroed = base;
len = 0;
uk_pr_debug("New brk heap region: %p-%p\n",
base, base + HEAP_LEN);
}
UK_ASSERT(brk_cur != NULL);
if (addr < base || addr >= (base + HEAP_LEN)) {
uk_pr_debug("Outside of brk range, return current brk %p\n",
brk_cur);
return brk_cur;
}
/* Zero out requested memory (e.g., glibc requires) */
if (addr > zeroed) {
uk_pr_debug("zeroing %p-%p...\n", zeroed, addr);
memset(zeroed, 0x0, (size_t) (addr - zeroed));
}
brk_cur = addr;
zeroed = addr;
len = addr - base;
uk_pr_debug("brk @ %p (brk heap region: %p-%p)\n", addr, base, base + HEAP_LEN);
return addr;
}
#if LIBC_SYSCALLS
#include <unistd.h>
#include <uk/errptr.h>
int brk(void *addr)
{
long ret;
ret = uk_syscall_r_brk(addr);
if (ret == 0) {
errno = EFAULT;
return -1;
}
if (PTRISERR(ret)) {
errno = PTR2ERR(ret);
return -1;
}
return 0;
}
void *sbrk(intptr_t inc)
{
long ret;
void *prev_base = base;
if (!base) {
/* Case when we do not have any memory allocated yet */
if (inc > HEAP_LEN) {
errno = ENOMEM;
return (void *) -1;
}
ret = uk_syscall_r_brk(NULL);
} else {
/* We are increasing or reducing our range */
ret = uk_syscall_r_brk((long)base + len + inc);
}
if (ret == 0) {
errno = EFAULT;
return (void *) -1;
}
if (PTRISERR(ret)) {
errno = PTR2ERR(ret);
return (void *) -1;
}
if (!prev_base)
return base;
return prev_base;
}
#endif /* LIBC_SYSCALLS */