xref: /netbsd-src/lib/libc/stdlib/alloca.3 (revision 76b8df0e8c557dc0500196e809acfe7294f78b6d)
1*76b8df0eSwiz.\"	$NetBSD: alloca.3,v 1.16 2012/10/24 22:56:27 wiz Exp $
26dda330eSthorpej.\"
35f8a6c06Sperry.\" Copyright (c) 1980, 1991, 1993
45f8a6c06Sperry.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" Redistribution and use in source and binary forms, with or without
761f28255Scgd.\" modification, are permitted provided that the following conditions
861f28255Scgd.\" are met:
961f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1061f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1161f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1261f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1361f28255Scgd.\"    documentation and/or other materials provided with the distribution.
14eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1561f28255Scgd.\"    may be used to endorse or promote products derived from this software
1661f28255Scgd.\"    without specific prior written permission.
1761f28255Scgd.\"
1861f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1961f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2061f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2161f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2261f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2361f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2461f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2561f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2661f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2761f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2861f28255Scgd.\" SUCH DAMAGE.
2961f28255Scgd.\"
305f8a6c06Sperry.\"     from: @(#)alloca.3	8.1 (Berkeley) 6/4/93
3161f28255Scgd.\"
32429bc5b7Schristos.Dd October 24, 2012
3361f28255Scgd.Dt ALLOCA 3
3438c96853Sgarbled.Os
3561f28255Scgd.Sh NAME
3661f28255Scgd.Nm alloca
3761f28255Scgd.Nd memory allocator
38312aca53Sperry.Sh LIBRARY
39312aca53Sperry.Lb libc
4061f28255Scgd.Sh SYNOPSIS
41472351e1Swiz.In stdlib.h
4261f28255Scgd.Ft void *
4361f28255Scgd.Fn alloca "size_t size"
4461f28255Scgd.Sh DESCRIPTION
4561f28255ScgdThe
4661f28255Scgd.Fn alloca
4761f28255Scgdfunction
4861f28255Scgdallocates
4961f28255Scgd.Fa size
5061f28255Scgdbytes of space in the stack frame of the caller.
5161f28255ScgdThis temporary space is automatically freed on
5261f28255Scgdreturn.
5361f28255Scgd.Sh RETURN VALUES
5461f28255ScgdThe
5561f28255Scgd.Fn alloca
5661f28255Scgdfunction returns a pointer to the beginning of the allocated space.
5761f28255ScgdIf the allocation failed, a
5861f28255Scgd.Dv NULL
5961f28255Scgdpointer is returned.
6061f28255Scgd.Sh SEE ALSO
61429bc5b7Schristos.Xr cc 1 ,
6261f28255Scgd.Xr brk 2 ,
6361f28255Scgd.Xr calloc 3 ,
6495ca3080Smikel.Xr getpagesize 3 ,
6561f28255Scgd.Xr malloc 3 ,
66429bc5b7Schristos.Xr realloc 3 ,
67429bc5b7Schristos.Xr security 7
6849bb3cb0Sjruoho.Sh CAVEATS
6949bb3cb0SjruohoFew limitations can be mentioned:
7049bb3cb0Sjruoho.Bl -bullet
7149bb3cb0Sjruoho.It
7261f28255ScgdThe
7361f28255Scgd.Fn alloca
74429bc5b7Schristosfunction is not part of any C standard and its use is not portable.
7549bb3cb0Sjruoho.It
76120c9b32SyamtThe
77120c9b32Syamt.Fn alloca
78429bc5b7Schristosfunction should be supplied by the compiler because the compiler is allowed to
79*76b8df0eSwizmake assumptions about the stack and frame pointers.
80*76b8df0eSwizThe libc
81429bc5b7Schristos.Fn alloca
82429bc5b7Schristosimplementation cannot account for those assumptions.
83429bc5b7SchristosWhile there is a
84429bc5b7Schristosmachine dependent implementation of
85429bc5b7Schristos.Fn alloca
86429bc5b7Schristosin libc, its use is discouraged and in most cases it will not work.
87429bc5b7SchristosUsing this implementation will produce linker warnings.
88429bc5b7Schristos.It
89429bc5b7SchristosThe
90429bc5b7Schristos.Fn alloca
91429bc5b7Schristosfunction is unsafe because it cannot ensure that the pointer
92120c9b32Syamtreturned points to a valid and usable block of memory.
93120c9b32SyamtThe allocation made may exceed the bounds of the stack, or even go
94120c9b32Syamtfurther into other objects in memory, and
95120c9b32Syamt.Fn alloca
96120c9b32Syamtcannot determine such an error.
97429bc5b7SchristosFor that all
98120c9b32Syamt.Fn alloca
99429bc5b7Schristosallocations should be bounded and limited to a small size.
10049bb3cb0Sjruoho.It
10149bb3cb0SjruohoSince
10249bb3cb0Sjruoho.Fn alloca
103429bc5b7Schristosmodifies the stack at runtime and the stack usage of each function frame
104429bc5b7Schristoscannot be predicted, it makes many compiler security features
105429bc5b7Schristos(such as
106429bc5b7Schristos.Xr cc 1
107429bc5b7Schristos.Fl fstack-protector )
108429bc5b7Schristosuseless for the calling function.
10949bb3cb0SjruohoSee
110e1897fd6Sjruoho.Xr security 7
11149bb3cb0Sjruohofor a discussion.
11249bb3cb0Sjruoho.El
11361f28255Scgd.\" .Sh HISTORY
11461f28255Scgd.\" The
11561f28255Scgd.\" .Fn alloca
11661f28255Scgd.\" function appeared in
11761f28255Scgd.\" .Bx ?? .
11861f28255Scgd.\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd
11961f28255Scgd.\" The first man page (or link to a man page that I can find at the
12061f28255Scgd.\" moment is 4.3...
121