| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* brk -- Adjust the "break" at the end of initial data.  NaCl version. | 
|  | 2 | Copyright (C) 2015-2016 Free Software Foundation, Inc. | 
|  | 3 | This file is part of the GNU C Library. | 
|  | 4 |  | 
|  | 5 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 6 | modify it under the terms of the GNU Lesser General Public | 
|  | 7 | License as published by the Free Software Foundation; either | 
|  | 8 | version 2.1 of the License, or (at your option) any later version. | 
|  | 9 |  | 
|  | 10 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 13 | Lesser General Public License for more details. | 
|  | 14 |  | 
|  | 15 | You should have received a copy of the GNU Lesser General Public | 
|  | 16 | License along with the GNU C Library; if not, see | 
|  | 17 | <http://www.gnu.org/licenses/>.  */ | 
|  | 18 |  | 
|  | 19 | #include <errno.h> | 
|  | 20 | #include <libc-internal.h> | 
|  | 21 | #include <stdint.h> | 
|  | 22 | #include <sys/mman.h> | 
|  | 23 | #include <sys/param.h> | 
|  | 24 | #include <unistd.h> | 
|  | 25 |  | 
|  | 26 | /* sbrk.c expects this.  */ | 
|  | 27 | void *__curbrk; | 
|  | 28 |  | 
|  | 29 | static uintptr_t | 
|  | 30 | page_above (void *addr) | 
|  | 31 | { | 
|  | 32 | return ALIGN_UP ((uintptr_t) addr, EXEC_PAGESIZE); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | /* Set the end of the process's data space to ADDR. | 
|  | 36 | Return 0 if successful, -1 if not.  */ | 
|  | 37 | int | 
|  | 38 | __brk (void *addr) | 
|  | 39 | { | 
|  | 40 | /* The NaCl sysbrk call is deprecated, so we do not use it here.  Other | 
|  | 41 | libc code expects that __sbrk can be used at least a little bit, so | 
|  | 42 | rather than a plain stub we have a minimal __brk implementation here. | 
|  | 43 | It just uses mmap/munmap to grow or shrink the break area, punting as | 
|  | 44 | soon as mmap fails to use the same contiguous area.  */ | 
|  | 45 |  | 
|  | 46 | if (__glibc_unlikely (__curbrk == NULL)) | 
|  | 47 | { | 
|  | 48 | /* This is the first call.  We must initialize the record | 
|  | 49 | of the current position.  It starts out at the end of the | 
|  | 50 | main program's data segment.  */ | 
|  | 51 |  | 
|  | 52 | /* XXX dynamic case??? */ | 
|  | 53 | extern char _end[]; | 
|  | 54 | __curbrk = _end; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | if (__glibc_unlikely (addr == NULL)) | 
|  | 58 | /* This is a call just to ensure that __curbrk is set up.  */ | 
|  | 59 | return 0; | 
|  | 60 |  | 
|  | 61 | uintptr_t old_limit = page_above (__curbrk); | 
|  | 62 | uintptr_t new_limit = page_above (addr); | 
|  | 63 |  | 
|  | 64 | if (old_limit > new_limit) | 
|  | 65 | { | 
|  | 66 | /* We're shrinking the old heap enough to release some pages.  */ | 
|  | 67 | if (__munmap ((void *) new_limit, old_limit - new_limit) != 0) | 
|  | 68 | return -1; | 
|  | 69 | } | 
|  | 70 | else if (old_limit < new_limit) | 
|  | 71 | { | 
|  | 72 | /* We're growing the old heap enough to need some more pages. | 
|  | 73 | See if they are available.  */ | 
|  | 74 | void *new_space = __mmap ((void *) old_limit, new_limit - old_limit, | 
|  | 75 | PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); | 
|  | 76 | if (new_space != (void *) old_limit) | 
|  | 77 | { | 
|  | 78 | if (new_space != MAP_FAILED) | 
|  | 79 | { | 
|  | 80 | /* mmap chose some different place for the pages | 
|  | 81 | because the contiguous area was not available. | 
|  | 82 | Oh well.  We can't use that.  */ | 
|  | 83 | __munmap (new_space, new_limit - old_limit); | 
|  | 84 | __set_errno (ENOMEM); | 
|  | 85 | } | 
|  | 86 | return -1; | 
|  | 87 | } | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | __curbrk = addr; | 
|  | 91 | return 0; | 
|  | 92 | } | 
|  | 93 | weak_alias (__brk, brk) |