Reimplement brk() and sbrk() to avoid the use of _end.

Previously, libc.so would initialize its notion of the break address
using _end, a special symbol emitted by the static linker following
the bss section.  Compatibility issues between lld and ld.bfd could
cause the wrong definition of _end (libc.so's definition rather than
that of the executable) to be used, breaking the brk()/sbrk()
interface.

Avoid this problem and future interoperability issues by simply not
relying on _end.  Instead, modify the break() system call to return
the kernel's view of the current break address, and have libc
initialize its state using an extra syscall upon the first use of the
interface.  As a side effect, this appears to fix brk()/sbrk() usage
in executables run with rtld direct exec, since the kernel and libc.so
no longer maintain separate views of the process' break address.

PR:		228574
Reviewed by:	kib (previous version)
MFC after:	2 months
Differential Revision:	https://reviews.freebsd.org/D15663
This commit is contained in:
Mark Johnston 2018-06-04 19:35:15 +00:00
parent ede2f7731d
commit 9f9c9b22ec
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=334626
37 changed files with 289 additions and 1117 deletions

View File

@ -63,7 +63,6 @@ FBSDprivate_1.0 {
signalcontext;
__siglongjmp;
_brk;
_end;
__sys_vfork;
_vfork;
};

View File

@ -8,7 +8,7 @@ SRCS+= \
amd64_set_fsbase.c \
amd64_set_gsbase.c
MDASM= vfork.S brk.S cerror.S getcontext.S sbrk.S
MDASM= vfork.S cerror.S getcontext.S
# Don't generate default code for these syscalls:
NOASM+= vfork.o
NOASM+= sbrk.o vfork.o

View File

@ -1,82 +0,0 @@
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)brk.s 5.2 (Berkeley) 12/17/90"
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
ENTRY(_brk)
pushq %rdi
jmp ok
END(_brk)
ENTRY(brk)
pushq %rdi
movq %rdi,%rax
#ifdef PIC
movq PIC_GOT(HIDENAME(minbrk)),%rdx
cmpq %rax,(%rdx)
#else
cmpq %rax,HIDENAME(minbrk)(%rip)
#endif
jbe ok
#ifdef PIC
movq (%rdx),%rdi
#else
movq HIDENAME(minbrk)(%rip),%rdi
#endif
ok:
movq $SYS_break,%rax
KERNCALL
jb err
movq 0(%rsp),%rax
#ifdef PIC
movq PIC_GOT(HIDENAME(curbrk)),%rdx
movq %rax,(%rdx)
#else
movq %rax,HIDENAME(curbrk)(%rip)
#endif
movq $0,%rax
popq %rdi
ret
err:
addq $8, %rsp
jmp HIDENAME(cerror)
END(brk)
.section .note.GNU-stack,"",%progbits

View File

@ -1,85 +0,0 @@
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)sbrk.s 5.1 (Berkeley) 4/23/90"
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl CNAME(_end)
.globl HIDENAME(minbrk)
.globl HIDENAME(curbrk)
.data
HIDENAME(minbrk): .quad CNAME(_end)
HIDENAME(curbrk): .quad CNAME(_end)
.text
ENTRY(sbrk)
pushq %rdi
movq %rdi,%rcx
#ifdef PIC
movq PIC_GOT(HIDENAME(curbrk)),%rdx
movq (%rdx),%rax
#else
movq HIDENAME(curbrk)(%rip),%rax
#endif
testq %rcx,%rcx
jz back
addq %rax,%rdi
mov $SYS_break,%eax
KERNCALL
jb err
#ifdef PIC
movq PIC_GOT(HIDENAME(curbrk)),%rdx
movq (%rdx),%rax
#else
movq HIDENAME(curbrk)(%rip),%rax
#endif
movq 0(%rsp), %rcx
#ifdef PIC
addq %rcx,(%rdx)
#else
addq %rcx,HIDENAME(curbrk)(%rip)
#endif
back:
addq $8, %rsp
ret
err:
addq $8, %rsp
jmp HIDENAME(cerror)
END(sbrk)
.section .note.GNU-stack,"",%progbits

View File

@ -58,7 +58,6 @@ FBSDprivate_1.0 {
__sys_vfork;
_vfork;
_brk;
_end;
_sbrk;
_libc_arm_fpu_present;

View File

@ -2,7 +2,7 @@
SRCS+= __vdso_gettc.c
MDASM= Ovfork.S brk.S cerror.S sbrk.S syscall.S
MDASM= Ovfork.S cerror.S syscall.S
# Don't generate default code for these syscalls:
NOASM+= vfork.o
NOASM+= sbrk.o vfork.o

View File

@ -1,95 +0,0 @@
/* $NetBSD: brk.S,v 1.6 2003/08/07 16:42:04 agc Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* 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 University 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 REGENTS 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 REGENTS 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.
*
* from: @(#)brk.s 5.2 (Berkeley) 12/17/90
*/
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl _C_LABEL(_end)
.globl CURBRK
#ifdef WEAK_ALIAS
WEAK_ALIAS(brk, _brk)
#endif
.data
.align 0
.globl _C_LABEL(minbrk)
.type _C_LABEL(minbrk),#object
_C_LABEL(minbrk):
.word _C_LABEL(_end)
/*
* Change the data segment size
*/
ENTRY(_brk)
/* Setup the GOT */
GOT_INIT(r3, .Lgot, .L1)
GOT_GET(r1, r3, .Lminbrk)
/* Get the minimum allowable brk address */
ldr r1, [r1]
/*
* Valid the address specified and set to the minimum
* if the address is below minbrk.
*/
cmp r0, r1
it lt
movlt r0, r1
mov r2, r0
SYSTRAP(break)
bcs PIC_SYM(CERROR, PLT)
#ifdef PIC
ldr r1, .Lcurbrk
ldr r1, [r3, r1]
#else
ldr r1, .Lcurbrk
#endif
/* Store the new address in curbrk */
str r2, [r1]
/* Return 0 for success */
mov r0, #0x00000000
RET
.align 2
GOT_INITSYM(.Lgot, .L1)
.Lminbrk:
.word PIC_SYM(_C_LABEL(minbrk), GOT)
.Lcurbrk:
.word PIC_SYM(CURBRK, GOT)
END(_brk)
.section .note.GNU-stack,"",%progbits

View File

@ -1,82 +0,0 @@
/* $NetBSD: sbrk.S,v 1.7 2003/08/07 16:42:05 agc Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* 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 University 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 REGENTS 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 REGENTS 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.
*
* from: @(#)sbrk.s 5.1 (Berkeley) 4/23/90
*/
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl _C_LABEL(_end)
#ifdef WEAK_ALIAS
WEAK_ALIAS(sbrk, _sbrk)
#endif
.data
.align 0
.globl CURBRK
.type CURBRK,#object
CURBRK:
.word _C_LABEL(_end)
/*
* Change the data segment size
*/
ENTRY(_sbrk)
/* Setup the GOT */
GOT_INIT(r3, .Lgot, .L1)
GOT_GET(r2, r3, .Lcurbrk)
/* Get the current brk address */
ldr r1, [r2]
/* Calculate new value */
mov r3, r0
add r0, r0, r1
SYSTRAP(break)
bcs PIC_SYM(CERROR, PLT)
/* Store new curbrk value */
ldr r0, [r2]
add r1, r0, r3
str r1, [r2]
/* Return old curbrk value */
RET
.align 0
GOT_INITSYM(.Lgot, .L1)
.Lcurbrk:
.word PIC_SYM(CURBRK, GOT)
END(_sbrk)
.section .note.GNU-stack,"",%progbits

View File

@ -61,6 +61,5 @@ FBSDprivate_1.0 {
__siglongjmp;
__sys_vfork;
_vfork;
_end;
_brk;
};

View File

@ -7,9 +7,9 @@ SRCS+= i386_clr_watch.c i386_set_watch.c i386_vm86.c
SRCS+= i386_get_fsbase.c i386_get_gsbase.c i386_get_ioperm.c i386_get_ldt.c \
i386_set_fsbase.c i386_set_gsbase.c i386_set_ioperm.c i386_set_ldt.c
MDASM= Ovfork.S brk.S cerror.S getcontext.S sbrk.S syscall.S
MDASM= Ovfork.S cerror.S getcontext.S syscall.S
NOASM+= vfork.o
NOASM+= sbrk.o vfork.o
MAN+= i386_get_ioperm.2 i386_get_ldt.2 i386_vm86.2
MAN+= i386_set_watch.3

View File

@ -1,85 +0,0 @@
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)brk.s 5.2 (Berkeley) 12/17/90"
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
ENTRY(_brk)
jmp ok
END(_brk)
ENTRY(brk)
#ifdef PIC
movl 4(%esp),%eax
PIC_PROLOGUE
movl PIC_GOT(HIDENAME(curbrk)),%edx # set up GOT addressing
movl PIC_GOT(HIDENAME(minbrk)),%ecx #
PIC_EPILOGUE
cmpl %eax,(%ecx)
jbe ok
movl (%ecx),%eax
movl %eax,4(%esp)
ok:
mov $SYS_break,%eax
KERNCALL
jb HIDENAME(cerror)
movl 4(%esp),%eax
movl %eax,(%edx)
movl $0,%eax
ret
#else
movl 4(%esp),%eax
cmpl %eax,HIDENAME(minbrk)
jbe ok
movl HIDENAME(minbrk),%eax
movl %eax,4(%esp)
ok:
mov $SYS_break,%eax
KERNCALL
jb HIDENAME(cerror)
movl 4(%esp),%eax
movl %eax,HIDENAME(curbrk)
movl $0,%eax
ret
#endif
END(brk)
.section .note.GNU-stack,"",%progbits

View File

@ -1,88 +0,0 @@
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* William Jolitz.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)sbrk.s 5.1 (Berkeley) 4/23/90"
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl CNAME(_end)
.globl HIDENAME(minbrk)
.globl HIDENAME(curbrk)
.data
HIDENAME(minbrk): .long CNAME(_end)
HIDENAME(curbrk): .long CNAME(_end)
.text
ENTRY(sbrk)
#ifdef PIC
movl 4(%esp),%ecx
PIC_PROLOGUE
movl PIC_GOT(HIDENAME(curbrk)),%edx
movl (%edx),%eax
PIC_EPILOGUE
testl %ecx,%ecx
jz back
addl %eax,4(%esp)
mov $SYS_break,%eax
KERNCALL
jb HIDENAME(cerror)
PIC_PROLOGUE
movl PIC_GOT(HIDENAME(curbrk)),%edx
movl (%edx),%eax
addl %ecx,(%edx)
PIC_EPILOGUE
back:
ret
#else /* !PIC */
movl 4(%esp),%ecx
movl HIDENAME(curbrk),%eax
testl %ecx,%ecx
jz back
addl %eax,4(%esp)
mov $SYS_break,%eax
KERNCALL
jb HIDENAME(cerror)
movl HIDENAME(curbrk),%eax
addl %ecx,HIDENAME(curbrk)
back:
ret
#endif /* PIC */
END(sbrk)
.section .note.GNU-stack,"",%progbits

View File

@ -50,7 +50,6 @@ FBSDprivate_1.0 {
__siglongjmp;
__sys_vfork;
_vfork;
_end;
_brk;
_sbrk;
};

View File

@ -2,7 +2,7 @@
SRCS+= trivial-vdso_tc.c
MDASM= Ovfork.S brk.S cerror.S sbrk.S syscall.S
MDASM= Ovfork.S cerror.S syscall.S
# Don't generate default code for these syscalls:
NOASM+= vfork.o
NOASM+= sbrk.o vfork.o

View File

@ -1,71 +0,0 @@
/* $NetBSD: brk.S,v 1.16 2003/08/07 16:42:17 agc Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ralph Campbell.
*
* 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 University 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 REGENTS 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 REGENTS 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 <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
#if defined(LIBC_SCCS) && !defined(lint)
ASMSTR("from: @(#)brk.s 8.1 (Berkeley) 6/4/93")
ASMSTR("$NetBSD: brk.S,v 1.16 2003/08/07 16:42:17 agc Exp $")
#endif /* LIBC_SCCS and not lint */
.globl _C_LABEL(minbrk)
.globl _C_LABEL(__curbrk)
.globl _C_LABEL(_end)
.data
_C_LABEL(minbrk):
PTR_WORD _C_LABEL(_end)
.text
LEAF(__sys_brk)
WEAK_ALIAS(brk, __sys_brk)
WEAK_ALIAS(_brk, __sys_brk)
PIC_PROLOGUE(__sys_brk)
PTR_LA v0, _C_LABEL(minbrk)
PTR_L v0, 0(v0)
bgeu a0, v0, 1f
move a0, v0 # dont allow break < minbrk
1:
li v0, SYS_break
syscall
bne a3, zero, 2f
PTR_LA t0, _C_LABEL(__curbrk)
PTR_S a0, 0(t0)
move v0, zero
PIC_RETURN()
2:
PIC_TAILCALL(__cerror)
END(__sys_brk)

View File

@ -1,73 +0,0 @@
/* $NetBSD: sbrk.S,v 1.16 2005/04/22 06:58:01 simonb Exp $ */
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Ralph Campbell.
*
* 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 University 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 REGENTS 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 REGENTS 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 <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
#if defined(LIBC_SCCS) && !defined(lint)
ASMSTR("from: @(#)sbrk.s 8.1 (Berkeley) 6/4/93")
ASMSTR("$NetBSD: sbrk.S,v 1.16 2005/04/22 06:58:01 simonb Exp $")
#endif /* LIBC_SCCS and not lint */
.globl _C_LABEL(__curbrk)
.globl _C_LABEL(_end)
.data
_C_LABEL(__curbrk):
PTR_WORD _C_LABEL(_end)
.text
LEAF(__sys_sbrk)
WEAK_ALIAS(sbrk, __sys_sbrk)
WEAK_ALIAS(_sbrk, __sys_sbrk)
PIC_PROLOGUE(__sys_sbrk)
PTR_LA t0, _C_LABEL(__curbrk)
PTR_L t0, 0(t0)
PTR_ADDU a0, a0, t0
li v0, SYS_break
syscall
bne a3, zero, 1f
nop
move v0, t0 # return old val of curbrk from above
PTR_LA t0, _C_LABEL(__curbrk)
PTR_S a0, 0(t0) # save current val of curbrk from above
PIC_RETURN()
j ra
1:
PIC_TAILCALL(__cerror)
END(__sys_sbrk)

View File

@ -54,5 +54,4 @@ FBSDprivate_1.0 {
signalcontext;
__signalcontext;
__syncicache;
_end;
};

View File

@ -1,3 +1,3 @@
# $FreeBSD$
MDASM+= brk.S cerror.S sbrk.S
MDASM+= cerror.S

View File

@ -1,76 +0,0 @@
/*-
* Copyright (c) 2002 Peter Grehan.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* $NetBSD: brk.S,v 1.9 2000/06/26 06:25:43 kleink Exp $ */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
.globl CNAME(_end)
.data
HIDENAME(minbrk):
.long CNAME(_end)
.text
ENTRY(brk)
#ifdef PIC
mflr %r10
bl _GLOBAL_OFFSET_TABLE_@local-4
mflr %r9
mtlr %r10
lwz %r5,HIDENAME(minbrk)@got(%r9)
lwz %r6,0(%r5)
#else
lis %r5,HIDENAME(minbrk)@ha
lwz %r6,HIDENAME(minbrk)@l(%r5)
#endif
cmplw %r6,%r3 /* if (minbrk <= r3) */
bgt 0f
mr %r6,%r3 /* r6 = r3 */
0:
mr %r3,%r6 /* new break value */
li %r0,SYS_break
sc /* assume, that r5 is kept */
bso 1f
#ifdef PIC
lwz %r7,HIDENAME(curbrk)@got(%r9)
stw %r6,0(%r7)
#else
lis %r7,HIDENAME(curbrk)@ha /* record new break */
stw %r6,HIDENAME(curbrk)@l(%r7)
#endif
blr /* return 0 */
1:
b PIC_PLT(HIDENAME(cerror))
END(brk)
.section .note.GNU-stack,"",%progbits

View File

@ -1,73 +0,0 @@
/*-
* Copyright (c) 2002 Peter Grehan.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* $NetBSD: sbrk.S,v 1.8 2000/06/26 06:25:44 kleink Exp $ */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl CNAME(_end)
.data
HIDENAME(curbrk):
.long CNAME(_end)
.text
ENTRY(sbrk)
#ifdef PIC
mflr %r10
bl _GLOBAL_OFFSET_TABLE_@local-4
mflr %r5
mtlr %r10
lwz %r5,HIDENAME(curbrk)@got(%r5)
lwz %r6,0(%r5)
#else
lis %r5,HIDENAME(curbrk)@ha
lwz %r6,HIDENAME(curbrk)@l(%r5) /* r6 = old break */
#endif
cmpwi %r3,0 /* sbrk(0) - return curbrk */
beq 1f
add %r3,%r3,%r6
mr %r7,%r3 /* r7 = new break */
li %r0,SYS_break
sc /* break(new_break) */
bso 2f
#ifdef PIC
stw %r7,0(%r5)
#else
stw %r7,HIDENAME(curbrk)@l(%r5) /* record new break */
#endif
1:
mr %r3,%r6 /* set return value */
blr
2:
b PIC_PLT(HIDENAME(cerror))
END(sbrk)
.section .note.GNU-stack,"",%progbits

View File

@ -50,5 +50,4 @@ FBSDprivate_1.0 {
signalcontext;
__signalcontext;
__syncicache;
_end;
};

View File

@ -1,3 +1,3 @@
# $FreeBSD$
MDASM+= brk.S cerror.S sbrk.S
MDASM+= cerror.S

View File

@ -1,74 +0,0 @@
/*-
* Copyright (c) 2002 Peter Grehan.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* $NetBSD: brk.S,v 1.9 2000/06/26 06:25:43 kleink Exp $ */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
.globl CNAME(_end)
.data
.align 3
HIDENAME(minbrk):
.llong CNAME(_end)
.text
ENTRY(brk)
addis %r6,%r2,HIDENAME(minbrk)@toc@ha
ld %r6,HIDENAME(minbrk)@toc@l(%r6)
cmpld %r6,%r3 /* if (minbrk <= r3) */
bgt 0f
mr %r6,%r3 /* r6 = r3 */
0:
mr %r3,%r6 /* new break value */
li %r0,SYS_break
sc /* assume, that r5 is kept */
bso 1f
/* record new break */
addis %r7,%r2,HIDENAME(curbrk)@toc@ha
std %r6,HIDENAME(curbrk)@toc@l(%r7)
blr /* return 0 */
1:
mflr %r0
std %r0,16(%r1)
stdu %r1,-48(%r1)
bl HIDENAME(cerror)
nop
ld %r1,0(%r1)
ld %r0,16(%r1)
mtlr %r0
blr
END(brk)
.section .note.GNU-stack,"",%progbits

View File

@ -1,69 +0,0 @@
/*-
* Copyright (c) 2002 Peter Grehan.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* $NetBSD: sbrk.S,v 1.8 2000/06/26 06:25:44 kleink Exp $ */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl CNAME(_end)
.data
.align 3
HIDENAME(curbrk):
.llong CNAME(_end)
.text
ENTRY(sbrk)
addis %r5,%r2,HIDENAME(curbrk)@toc@ha
addi %r5,%r5,HIDENAME(curbrk)@toc@l
ld %r6,0(%r5) /* r6 = old break */
cmpdi %r3,0 /* sbrk(0) - return curbrk */
beq 1f
add %r3,%r3,%r6
mr %r7,%r3 /* r7 = new break */
li %r0,SYS_break
sc /* break(new_break) */
bso 2f
std %r7,0(%r5)
1:
mr %r3,%r6 /* set return value */
blr
2:
mflr %r0
std %r0,16(%r1)
stdu %r1,-48(%r1)
bl HIDENAME(cerror)
nop
ld %r1,0(%r1)
ld %r0,16(%r1)
mtlr %r0
blr
END(sbrk)
.section .note.GNU-stack,"",%progbits

View File

@ -7,4 +7,4 @@ MDASM= cerror.S \
vfork.S
# Don't generate default code for these syscalls:
NOASM+= vfork.o
NOASM+= sbrk.o vfork.o

View File

@ -81,7 +81,6 @@ FBSDprivate_1.0 {
__siglongjmp;
__sys_brk;
_brk;
_end;
__sys_sbrk;
_sbrk;
__sys_vfork;

View File

@ -12,4 +12,4 @@ SRCS+= __sparc_sigtramp_setup.c \
CFLAGS+= -I${LIBC_SRCTOP}/sparc64/fpu
MDASM+= brk.S cerror.S sbrk.S sigaction1.S
MDASM+= cerror.S sigaction1.S

View File

@ -1,65 +0,0 @@
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*
* from: Header: brk.s,v 1.3 92/06/25 12:56:05 mccanne Exp
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)brk.s 8.1 (Berkeley) 6/4/93"
#if 0
RCSID("$NetBSD: brk.S,v 1.9 2000/07/25 20:15:40 mycroft Exp $")
#endif
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
_SYSENTRY(brk)
PIC_PROLOGUE(%o3, %o2)
SET(HIDENAME(minbrk), %o2, %o3)
ldx [%o3], %o4
cmp %o4, %o0
movg %xcc, %o4, %o0
mov %o0, %o4
mov SYS_break, %g1
ta %xcc, ST_SYSCALL
bcc,a,pt %xcc, 1f
nop
ERROR()
1: SET(HIDENAME(curbrk), %o2, %o3)
retl
stx %o4, [%o3]
_SYSEND(brk)

View File

@ -1,71 +0,0 @@
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* 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 University 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 REGENTS 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 REGENTS 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.
*
* from: Header: brk.s,v 1.3 92/06/25 12:56:05 mccanne Exp
*/
#if defined(SYSLIBC_SCCS) && !defined(lint)
.asciz "@(#)sbrk.s 8.1 (Berkeley) 6/4/93"
#if 0
RCSID("$NetBSD: sbrk.S,v 1.7 2000/07/25 15:14:46 mycroft Exp $")
#endif
#endif /* SYSLIBC_SCCS and not lint */
#include <machine/asm.h>
__FBSDID("$FreeBSD$");
#include "SYS.h"
.data
.globl HIDENAME(curbrk)
.globl HIDENAME(minbrk)
.type HIDENAME(curbrk),@object
.type HIDENAME(minbrk),@object
.align 8
HIDENAME(curbrk):
.xword CNAME(_end)
HIDENAME(minbrk):
.xword CNAME(_end)
_SYSENTRY(sbrk)
PIC_PROLOGUE(%o3, %o2)
SET(HIDENAME(curbrk), %o2, %o3)
ldx [%o3], %o4
add %o4, %o0, %o5
mov %o5, %o0
mov SYS_break, %g1
ta %xcc, ST_SYSCALL
bcc,a,pt %xcc, 1f
mov %o4, %o0
ERROR()
1: retl
stx %o5, [%o3]
_SYSEND(sbrk)

View File

@ -17,8 +17,7 @@
# While historically machine dependent, all architectures have the following
# declarations in common:
#
NOASM= break.o \
exit.o \
NOASM= exit.o \
getlogin.o \
sstk.o \
yield.o
@ -45,6 +44,7 @@ SRCS+= getdirentries.c
NOASM+= getdirentries.o
PSEUDO+= _getdirentries.o
SRCS+= brk.c
SRCS+= pipe.c
SRCS+= vadvise.c

View File

@ -28,7 +28,7 @@
.\" @(#)brk.2 8.4 (Berkeley) 5/1/95
.\" $FreeBSD$
.\"
.Dd May 24, 2018
.Dd June 2, 2018
.Dt BRK 2
.Os
.Sh NAME
@ -183,3 +183,8 @@ is sometimes used to monitor heap use by calling with an argument of 0.
The result is unlikely to reflect actual utilization in combination with an
.Xr mmap 2
based malloc.
.Pp
.Fn brk
and
.Fn sbrk
are not thread-safe.

107
lib/libc/sys/brk.c Normal file
View File

@ -0,0 +1,107 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2018 Mark Johnston <markj@FreeBSD.org>
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/syscall.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
void *__sys_break(char *nsize);
static uintptr_t curbrk, minbrk;
static int curbrk_initted;
static int
initbrk(void)
{
void *newbrk;
if (!curbrk_initted) {
newbrk = __sys_break(NULL);
if (newbrk == (void *)-1)
return (-1);
curbrk = minbrk = (uintptr_t)newbrk;
curbrk_initted = 1;
}
return (0);
}
static void *
mvbrk(void *addr)
{
uintptr_t oldbrk;
if ((uintptr_t)addr < minbrk) {
/* Emulate legacy error handling in the syscall. */
errno = EINVAL;
return ((void *)-1);
}
if (__sys_break(addr) == (void *)-1)
return ((void *)-1);
oldbrk = curbrk;
curbrk = (uintptr_t)addr;
return ((void *)oldbrk);
}
int
brk(const void *addr)
{
if (initbrk() == -1)
return (-1);
if ((uintptr_t)addr < minbrk)
addr = (void *)minbrk;
return (mvbrk(__DECONST(void *, addr)) == (void *)-1 ? -1 : 0);
}
int
_brk(const void *addr)
{
if (initbrk() == -1)
return (-1);
return (mvbrk(__DECONST(void *, addr)) == (void *)-1 ? -1 : 0);
}
void *
sbrk(intptr_t incr)
{
if (initbrk() == -1)
return ((void *)-1);
if ((incr > 0 && curbrk + incr < curbrk) ||
(incr < 0 && curbrk + incr > curbrk)) {
/* Emulate legacy error handling in the syscall. */
errno = EINVAL;
return ((void *)-1);
}
return (mvbrk((void *)(curbrk + incr)));
}

View File

@ -4,6 +4,7 @@ PACKAGE= tests
.include <bsd.own.mk>
ATF_TESTS_C+= brk_test
ATF_TESTS_C+= queue_test
# TODO: clone, lwp_create, lwp_ctl, posix_fadvise, recvmmsg,

View File

@ -0,0 +1,149 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2018 Mark Johnston <markj@FreeBSD.org>
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <atf-c.h>
ATF_TC(brk_basic);
ATF_TC_HEAD(brk_basic, tc)
{
atf_tc_set_md_var(tc, "descr", "Verify basic brk() functionality");
}
ATF_TC_BODY(brk_basic, tc)
{
void *oldbrk, *newbrk;
int error;
/* Reset the break. */
error = brk(0);
ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
oldbrk = sbrk(0);
ATF_REQUIRE(oldbrk != (void *)-1);
/* Try to allocate a page. */
error = brk((void *)((intptr_t)oldbrk + PAGE_SIZE * 2));
ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
/*
* Attempt to set the break below minbrk. This should have no effect.
*/
error = brk((void *)((intptr_t)oldbrk - 1));
ATF_REQUIRE_MSG(error == 0, "brk: %s", strerror(errno));
newbrk = sbrk(0);
ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
ATF_REQUIRE(newbrk == oldbrk);
}
ATF_TC(sbrk_basic);
ATF_TC_HEAD(sbrk_basic, tc)
{
atf_tc_set_md_var(tc, "descr", "Verify basic sbrk() functionality");
}
ATF_TC_BODY(sbrk_basic, tc)
{
void *newbrk, *oldbrk;
int *p;
oldbrk = sbrk(0);
ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
p = sbrk(sizeof(*p));
*p = 0;
ATF_REQUIRE(oldbrk == p);
newbrk = sbrk(-sizeof(*p));
ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
ATF_REQUIRE(oldbrk == sbrk(0));
oldbrk = sbrk(PAGE_SIZE * 2 + 1);
ATF_REQUIRE_MSG(oldbrk != (void *)-1, "sbrk: %s", strerror(errno));
memset(oldbrk, 0, PAGE_SIZE * 2 + 1);
newbrk = sbrk(-(PAGE_SIZE * 2 + 1));
ATF_REQUIRE_MSG(newbrk != (void *)-1, "sbrk: %s", strerror(errno));
ATF_REQUIRE(sbrk(0) == oldbrk);
}
ATF_TC(mlockfuture);
ATF_TC_HEAD(mlockfuture, tc)
{
atf_tc_set_md_var(tc, "descr",
"Verify that mlockall(MCL_FUTURE) applies to the data segment");
}
ATF_TC_BODY(mlockfuture, tc)
{
void *oldbrk, *n, *newbrk;
int error;
char v;
error = mlockall(MCL_FUTURE);
ATF_REQUIRE_MSG(error == 0,
"mlockall: %s", strerror(errno));
/*
* Advance the break so that at least one page is added to the data
* segment. This page should be automatically faulted in to the address
* space.
*/
oldbrk = sbrk(0);
ATF_REQUIRE(oldbrk != (void *)-1);
newbrk = sbrk(PAGE_SIZE * 2);
ATF_REQUIRE(newbrk != (void *)-1);
n = (void *)(((uintptr_t)oldbrk + PAGE_SIZE) & ~PAGE_SIZE);
v = 0;
error = mincore(n, PAGE_SIZE, &v);
ATF_REQUIRE_MSG(error == 0,
"mincore: %s", strerror(errno));
ATF_REQUIRE_MSG((v & MINCORE_INCORE) != 0,
"unexpected page flags %#x", v);
error = brk(oldbrk);
ATF_REQUIRE(error == 0);
error = munlockall();
ATF_REQUIRE_MSG(error == 0,
"munlockall: %s", strerror(errno));
}
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, brk_basic);
ATF_TP_ADD_TC(tp, sbrk_basic);
ATF_TP_ADD_TC(tp, mlockfuture);
return (atf_no_error());
}

View File

@ -87,7 +87,7 @@
int mode, int dev); }
15 AUE_CHMOD NOPROTO { int chmod(char *path, int mode); }
16 AUE_CHOWN NOPROTO { int chown(char *path, int uid, int gid); }
17 AUE_NULL NOPROTO { int obreak(char *nsize); } break \
17 AUE_NULL NOPROTO { caddr_t obreak(char *nsize); } break \
obreak_args int
18 AUE_GETFSSTAT COMPAT4 { int freebsd32_getfsstat( \
struct statfs32 *buf, long bufsize, \

View File

@ -118,7 +118,7 @@
15 AUE_CHMOD STD { int chmod(_In_z_ char *path, int mode); }
16 AUE_CHOWN STD { int chown(_In_z_ char *path, \
int uid, int gid); }
17 AUE_NULL STD { int obreak(_In_ char *nsize); } break \
17 AUE_NULL STD { caddr_t obreak(_In_ char *nsize); } break \
obreak_args int
18 AUE_GETFSSTAT COMPAT4 { int getfsstat( \
_Out_writes_bytes_opt_(bufsize) \

View File

@ -102,13 +102,16 @@ sys_obreak(struct thread *td, struct obreak_args *uap)
}
} else if (new < base) {
/*
* This is simply an invalid value. If someone wants to
* do fancy address space manipulations, mmap and munmap
* can do most of what the user would want.
* Simply return the current break address without
* modifying any state. This is an ad-hoc interface
* used by libc to determine the initial break address,
* avoiding a dependency on magic features in the system
* linker.
*/
error = EINVAL;
new = old;
goto done;
}
if (new > old) {
if (!old_mlock && map->flags & MAP_WIREFUTURE) {
if (ptoa(pmap_wired_count(map->pmap)) +
@ -225,6 +228,9 @@ sys_obreak(struct thread *td, struct obreak_args *uap)
(void) vm_map_wire(map, old, new,
VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
if (error == 0)
td->td_retval[0] = new;
return (error);
#else /* defined(__aarch64__) || defined(__riscv__) */
return (ENOSYS);