util.c revision 1.15
1/*	$OpenBSD: util.c,v 1.15 2003/06/09 16:10:03 deraadt Exp $	*/
2
3/*
4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#include <sys/types.h>
30#include <sys/param.h>
31#include <sys/mman.h>
32#include <sys/sysctl.h>
33#include <string.h>
34#include "archdep.h"
35
36/*
37 * Stack protector dummies.
38 * Ideally, a scheme to compile these stubs from libc should be used, but
39 * this would end up dragging too much code from libc here.
40 */
41long __guard[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42
43void
44__stack_smash_handler(char func[], int damaged)
45{
46	_dl_exit(127);
47}
48
49/*
50 * Static vars usable after bootstrapping.
51 */
52static void *_dl_malloc_base;
53static void *_dl_malloc_pool = 0;
54static long *_dl_malloc_free = 0;
55
56char *
57_dl_strdup(const char *orig)
58{
59	char *newstr;
60	int len;
61
62	len = _dl_strlen(orig)+1;
63	newstr = _dl_malloc(len);
64	_dl_strlcpy(newstr, orig, len);
65	return (newstr);
66}
67
68/*
69 * The following malloc/free code is a very simplified implementation
70 * of a malloc function. However, we do not need to be very complex here
71 * because we only free memory when 'dlclose()' is called and we can
72 * reuse at least the memory allocated for the object descriptor. We have
73 * one dynamic string allocated, the library name and it is likely that
74 * we can reuse that one to without a lot of complex colapsing code.
75 */
76void *
77_dl_malloc(size_t size)
78{
79	long *p, *t, *n;
80
81	size = (size + 8 + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1);
82
83	if ((t = _dl_malloc_free) != 0) {	/* Try free list first */
84		n = (long *)&_dl_malloc_free;
85		while (t && t[-1] < size) {
86			n = t;
87			t = (long *)*t;
88		}
89		if (t) {
90			*n = *t;
91			_dl_memset(t, 0, t[-1] - 4);
92			return((void *)t);
93		}
94	}
95	if (_dl_malloc_pool == 0 ||
96	    _dl_malloc_pool + size > _dl_malloc_base + 4096) {
97		_dl_malloc_pool = (void *)_dl_mmap((void *)0, 4096,
98		    PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
99		if (_dl_malloc_pool == 0 || _dl_malloc_pool == MAP_FAILED ) {
100			_dl_printf("Dynamic loader failure: malloc.\n");
101			_dl_exit(7);
102		}
103		_dl_malloc_base = _dl_malloc_pool;
104	}
105	p = _dl_malloc_pool;
106	_dl_malloc_pool += size;
107	_dl_memset(p, 0, size);
108	*p = size;
109	return((void *)(p + 1));
110}
111
112void
113_dl_free(void *p)
114{
115	long *t = (long *)p;
116
117	*t = (long)_dl_malloc_free;
118	_dl_malloc_free = p;
119}
120
121
122unsigned int
123_dl_random(void)
124{
125	int mib[2];
126	unsigned int rnd;
127	size_t len;
128
129	mib[0] = CTL_KERN;
130	mib[1] = KERN_ARND;
131	len = sizeof(rnd);
132	_dl_sysctl(mib, 2, &rnd, &len, NULL, 0);
133
134	return (rnd);
135}
136
137
138