xref: /illumos-gate/usr/src/lib/libc/port/sys/sbrk.c (revision d2a70789)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58cd45542Sraf  * Common Development and Distribution License (the "License").
68cd45542Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
218cd45542Sraf 
227c478bd9Sstevel@tonic-gate /*
238cd45542Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf #pragma weak _sbrk = sbrk
287257d1b4Sraf #pragma weak _brk = brk
297c478bd9Sstevel@tonic-gate 
307257d1b4Sraf #include "lint.h"
317c478bd9Sstevel@tonic-gate #include <synch.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <inttypes.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include "mtlib.h"
397c478bd9Sstevel@tonic-gate #include "libc.h"
407c478bd9Sstevel@tonic-gate 
41*d2a70789SRichard Lowe void *_nd = NULL;
427c478bd9Sstevel@tonic-gate mutex_t __sbrk_lock = DEFAULTMUTEX;
437c478bd9Sstevel@tonic-gate 
44*d2a70789SRichard Lowe extern intptr_t _brk_unlocked(void *);
45*d2a70789SRichard Lowe void *_sbrk_unlocked(intptr_t);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * The break must always be at least 8-byte aligned
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate #if (_MAX_ALIGNMENT < 8)
517c478bd9Sstevel@tonic-gate #define	ALIGNSZ		8
527c478bd9Sstevel@tonic-gate #else
537c478bd9Sstevel@tonic-gate #define	ALIGNSZ		_MAX_ALIGNMENT
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	BRKALIGN(x)	(caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate void *
sbrk(intptr_t addend)597c478bd9Sstevel@tonic-gate sbrk(intptr_t addend)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	void *result;
627c478bd9Sstevel@tonic-gate 
638cd45542Sraf 	if (!primary_link_map) {
648cd45542Sraf 		errno = ENOTSUP;
658cd45542Sraf 		return ((void *)-1);
668cd45542Sraf 	}
677c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
687c478bd9Sstevel@tonic-gate 	result = _sbrk_unlocked(addend);
697c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	return (result);
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * _sbrk_unlocked() aligns the old break, adds the addend, aligns
767c478bd9Sstevel@tonic-gate  * the new break, and calls _brk_unlocked() to set the new break.
777c478bd9Sstevel@tonic-gate  * We must align the old break because _nd may begin life misaligned.
787c478bd9Sstevel@tonic-gate  * The addend can be either positive or negative, so there are two
797c478bd9Sstevel@tonic-gate  * overflow/underflow edge conditions to reject:
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  *   - the addend is negative and brk + addend < 0.
827c478bd9Sstevel@tonic-gate  *   - the addend is positive and brk + addend > ULONG_MAX
837c478bd9Sstevel@tonic-gate  */
847257d1b4Sraf void *
_sbrk_unlocked(intptr_t addend)857c478bd9Sstevel@tonic-gate _sbrk_unlocked(intptr_t addend)
867c478bd9Sstevel@tonic-gate {
87*d2a70789SRichard Lowe 	char *old_brk;
88*d2a70789SRichard Lowe 	char *new_brk;
89*d2a70789SRichard Lowe 
90*d2a70789SRichard Lowe 	if (_nd == NULL) {
91*d2a70789SRichard Lowe 		_nd = (void *)_brk_unlocked(0);
92*d2a70789SRichard Lowe 	}
93*d2a70789SRichard Lowe 
94*d2a70789SRichard Lowe 	old_brk = BRKALIGN(_nd);
95*d2a70789SRichard Lowe 	new_brk = BRKALIGN(old_brk + addend);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if ((addend > 0 && new_brk < old_brk) ||
987c478bd9Sstevel@tonic-gate 	    (addend < 0 && new_brk > old_brk)) {
997c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1007c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	if (_brk_unlocked(new_brk) != 0)
1037c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1047c478bd9Sstevel@tonic-gate 	_nd = new_brk;
1057c478bd9Sstevel@tonic-gate 	return (old_brk);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
1107c478bd9Sstevel@tonic-gate  * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
1117c478bd9Sstevel@tonic-gate  * to set the new break.  The low_aligned-aligned value is returned, and
1127c478bd9Sstevel@tonic-gate  * the actual space allocated is returned through actual_size.
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
1157c478bd9Sstevel@tonic-gate  * not allow shrinking the heap.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate void *
_sbrk_grow_aligned(size_t min_size,size_t low_align,size_t high_align,size_t * actual_size)1187c478bd9Sstevel@tonic-gate _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
1197c478bd9Sstevel@tonic-gate     size_t *actual_size)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	uintptr_t old_brk;
1227c478bd9Sstevel@tonic-gate 	uintptr_t ret_brk;
1237c478bd9Sstevel@tonic-gate 	uintptr_t high_brk;
1247c478bd9Sstevel@tonic-gate 	uintptr_t new_brk;
125*d2a70789SRichard Lowe 	intptr_t brk_result;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (!primary_link_map) {
1287c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
1297c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	if ((low_align & (low_align - 1)) != 0 ||
1327c478bd9Sstevel@tonic-gate 	    (high_align & (high_align - 1)) != 0) {
1337c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1347c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	low_align = MAX(low_align, ALIGNSZ);
1377c478bd9Sstevel@tonic-gate 	high_align = MAX(high_align, ALIGNSZ);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
1407c478bd9Sstevel@tonic-gate 
141*d2a70789SRichard Lowe 	if (_nd == NULL)
142*d2a70789SRichard Lowe 		_nd = (void *)_brk_unlocked(0);
143*d2a70789SRichard Lowe 
1447c478bd9Sstevel@tonic-gate 	old_brk = (uintptr_t)BRKALIGN(_nd);
1457c478bd9Sstevel@tonic-gate 	ret_brk = P2ROUNDUP(old_brk, low_align);
1467c478bd9Sstevel@tonic-gate 	high_brk = ret_brk + min_size;
1477c478bd9Sstevel@tonic-gate 	new_brk = P2ROUNDUP(high_brk, high_align);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Check for overflow
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
1537c478bd9Sstevel@tonic-gate 		lmutex_unlock(&__sbrk_lock);
1547c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1557c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if ((brk_result = _brk_unlocked((void *)new_brk)) == 0)
1597c478bd9Sstevel@tonic-gate 		_nd = (void *)new_brk;
1607c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (brk_result != 0)
1637c478bd9Sstevel@tonic-gate 		return ((void *)-1);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (actual_size != NULL)
1667c478bd9Sstevel@tonic-gate 		*actual_size = (new_brk - ret_brk);
1677c478bd9Sstevel@tonic-gate 	return ((void *)ret_brk);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate int
brk(void * new_brk)1717c478bd9Sstevel@tonic-gate brk(void *new_brk)
1727c478bd9Sstevel@tonic-gate {
173*d2a70789SRichard Lowe 	intptr_t result;
174*d2a70789SRichard Lowe 
175*d2a70789SRichard Lowe 	/*
176*d2a70789SRichard Lowe 	 * brk(2) will return the current brk if given an argument of 0, so we
177*d2a70789SRichard Lowe 	 * need to fail it here
178*d2a70789SRichard Lowe 	 */
179*d2a70789SRichard Lowe 	if (new_brk == 0) {
180*d2a70789SRichard Lowe 		errno = ENOMEM;
181*d2a70789SRichard Lowe 		return (-1);
182*d2a70789SRichard Lowe 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (!primary_link_map) {
1857c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
1867c478bd9Sstevel@tonic-gate 		return (-1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * Need to align this here;  _brk_unlocked won't do it for us.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	new_brk = BRKALIGN(new_brk);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	lmutex_lock(&__sbrk_lock);
1947c478bd9Sstevel@tonic-gate 	if ((result = _brk_unlocked(new_brk)) == 0)
1957c478bd9Sstevel@tonic-gate 		_nd = new_brk;
1967c478bd9Sstevel@tonic-gate 	lmutex_unlock(&__sbrk_lock);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (result);
1997c478bd9Sstevel@tonic-gate }
200