File: safemalloc.c

package info (click to toggle)
afterstep 2.2.12-18.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,184 kB
  • sloc: ansic: 201,695; sh: 5,894; xml: 3,721; makefile: 2,094; perl: 1,558; cpp: 811
file content (361 lines) | stat: -rw-r--r-- 8,658 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (c) 2001 Sasha Vasko <sasha at aftercode.net>
 *   and many others, who has not left their copyrights here :)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define LOCAL_DEBUG
#include "config.h"

#include <stdlib.h>
#include <stdio.h>

#ifdef __CYGWIN__
#include <w32api/windows.h>
#define XMD_H
#define XPROTO_H
#endif


#include "astypes.h"
#include "output.h"
#include "selfdiag.h"
#include "safemalloc.h"

#define DETECT_BUFFER_UNDERRUN
#undef NOGUARD 
/*#define DEBUG_ALLOCS */

#ifdef DEBUG_ALLOCS
#include <string.h>
#undef malloc
#undef safemalloc
#undef safecalloc
#endif /* DEBUG_ALLOCS */

/* always undef free, as it will be both redefined with and without
   DEBUG_ALLOC */
#undef free

#if defined(__CYGWIN__)

#define AS_WIN32_PAGE_MAGIC		0xA36a5ea1
/* lets do some magic with Win32 memory allocation to test for memory corruption : */
static void *
alloc_guarded_memory( size_t length	)
{
#define WIN32_PAGE_SIZE		4096   		       /* assuming 4K pages in windows on x86 machines */
	LPVOID lpvAddr; 
    DWORD cbSize; 
    LPVOID commit, commit_guard; 
#ifdef DETECT_BUFFER_UNDERRUN		
	LPVOID commit_size;       
#endif 
	/* Reserve whole page per allocation, plus another page - for guard */
	cbSize = length+sizeof(size_t)*2; 
	if( cbSize%WIN32_PAGE_SIZE == 0 )
		cbSize = (cbSize/WIN32_PAGE_SIZE)*WIN32_PAGE_SIZE; 
	else
		cbSize = ((cbSize/WIN32_PAGE_SIZE)+1)*WIN32_PAGE_SIZE; 
 
    /* Try to allocate some memory. */
    lpvAddr = VirtualAlloc(NULL,
#ifndef DETECT_BUFFER_UNDERRUN		
							cbSize+WIN32_PAGE_SIZE,
#else
							cbSize+WIN32_PAGE_SIZE+WIN32_PAGE_SIZE,
#endif							   
							MEM_RESERVE,PAGE_NOACCESS); 
   
	if(lpvAddr == NULL) 
        fprintf(stderr,"MEMORY ERROR: VirtualAlloc failed on RESERVE with %ld\n", GetLastError()); 
	else
	{	/* Try to commit the allocated memory.  */
#ifndef DETECT_BUFFER_UNDERRUN
		commit = VirtualAlloc(lpvAddr,cbSize,MEM_COMMIT,PAGE_READWRITE); 
		commit_guard = VirtualAlloc(lpvAddr+cbSize,WIN32_PAGE_SIZE,MEM_COMMIT,PAGE_READONLY|PAGE_GUARD); 
#else
		commit_size = VirtualAlloc(lpvAddr,WIN32_PAGE_SIZE,MEM_COMMIT,PAGE_READWRITE); 
    	if(commit_size == NULL ) 
		{	
        	fprintf(stderr,"MEMORY ERROR: VirtualAlloc failed on COMMIT SIZE with %ld\n", GetLastError()); 
			return NULL ;
		}
		commit_guard = VirtualAlloc(lpvAddr+WIN32_PAGE_SIZE,WIN32_PAGE_SIZE,MEM_COMMIT,PAGE_READONLY|PAGE_GUARD); 
		commit = VirtualAlloc(lpvAddr+WIN32_PAGE_SIZE+WIN32_PAGE_SIZE,cbSize,MEM_COMMIT,PAGE_READWRITE); 
#endif
 
    	if(commit == NULL ) 
        	fprintf(stderr,"MEMORY ERROR: VirtualAlloc failed on COMMIT with %ld\n", GetLastError()); 
    	if(commit_guard == NULL ) 
        	fprintf(stderr,"MEMORY ERROR: VirtualAlloc failed on COMMIT GUARD with %ld\n", GetLastError()); 
		if( commit && commit_guard ) 
		{ 
#ifndef DETECT_BUFFER_UNDERRUN			
			size_t *size_ptr = (size_t*)commit;
			unsigned char *ptr = (unsigned char*)commit + (cbSize - length);	 
#else
			size_t *size_ptr = (size_t*)commit_size;
			unsigned char *ptr = (unsigned char*)commit;	 
#endif			   
			size_ptr[0] = length ;
			size_ptr[1] = AS_WIN32_PAGE_MAGIC ;
#ifdef LOCAL_DEBUG
			fprintf( stderr, "ALLOC: ptr %p points to a block of %d bytes. commit = %p\n", ptr, length, commit );
#endif
			return ptr ;
		}	
	}
	return NULL;
}

static size_t
get_guarded_memory_size( char *ptr )
{
	size_t *size_ptr ;
#ifndef DETECT_BUFFER_UNDERRUN			   
	unsigned long long_ptr = (unsigned long)ptr ;
	if( (long_ptr&0x00000FFF) < sizeof(size_t)*2 )
		long_ptr -= WIN32_PAGE_SIZE ;
	size_ptr = (size_t*) (long_ptr&0xFFFFF000) ;
#else
	size_ptr = (size_t*)(ptr - (WIN32_PAGE_SIZE + WIN32_PAGE_SIZE)) ;
#endif

#ifdef LOCAL_DEBUG
	fprintf( stderr, "ALLOC: ptr %p seems to point to a block of %d bytes\n", ptr, *size_ptr );
#endif
	if( size_ptr[1] != AS_WIN32_PAGE_MAGIC  )
	{
		fprintf( stderr, "ALLOC: warning - ptr %p was not allocated properly\n", ptr );		   
	}	
	return size_ptr[0] ;
}	   

static void
free_guarded_memory( void *ptr )
{
	size_t *size_ptr ;
#ifndef DETECT_BUFFER_UNDERRUN			   
	unsigned long long_ptr = (unsigned long)ptr ;
	if( (long_ptr&0x00000FFF) < sizeof(size_t)*2 )
		long_ptr -= WIN32_PAGE_SIZE  ;
	size_ptr = (size_t*) (long_ptr&0xFFFFF000) ;
#else
	size_ptr = (size_t*)(ptr - (WIN32_PAGE_SIZE + WIN32_PAGE_SIZE)) ;
#endif
	
	if( size_ptr[1] != AS_WIN32_PAGE_MAGIC  )
	{	
		char *suicide = NULL;
		fprintf( stderr, "FREE: warning - freeing ptr %p that was not allocated properly (size_ptr = %p)\n", ptr, size_ptr );		   
		fflush( stderr );
		*suicide = 1 ;		
	}else
	{	
#ifdef LOCAL_DEBUG
		fprintf( stderr, "FREE: ptr %p points to a block of %d bytes. commit = %p\n", ptr, size_ptr[0], size_ptr );
#endif
		VirtualFree( size_ptr, 0, MEM_RELEASE );
	}
}	   

#endif

void
failed_alloc( const char * function, size_t size )
{
	char *suicide = NULL;
	fprintf (stderr, "%s of %lu bytes failed. Exiting\n", function, (unsigned long)size);
	*suicide = 1 ;
	exit (1);
}
	  


void         *
safemalloc (size_t length)
{
#if defined(__CYGWIN__) && defined(DEBUG_ALLOCS) && !defined(NOGUARD)
	return guarded_malloc (length);
#else
	char         *ptr = NULL ;

	if (length <= 0)
		length = 1;

	ptr = malloc (length);

	if (ptr == (char *)0)
		failed_alloc( "malloc", length );
	
	return ptr;
#endif
}

void         *
guarded_malloc (size_t length)
{
	char         *ptr = NULL ;

	if (length <= 0)
		length = 1;

#if defined(__CYGWIN__)
	/* lets do some magic with Win32 memory allocation to test for memory corruption : */
	ptr = alloc_guarded_memory( length );	   
#else
	ptr = malloc (length);
#endif

	if (ptr == (char *)0)
		failed_alloc( "malloc", length );
	
	return ptr;
}


void         *
saferealloc (void *orig_ptr, size_t length)
{
#if defined(__CYGWIN__) && defined(DEBUG_ALLOCS) && !defined(NOGUARD)
	return guarded_realloc( orig_ptr, length );
#else
	char         *ptr = NULL ;

	if (length <= 0)
		length = 1;

	ptr = realloc (orig_ptr, length);

	if (ptr == (char *)0)
		failed_alloc( "realloc", length );
	
	return ptr;
#endif
}

void         *
guarded_realloc (void *orig_ptr, size_t length)
{
	char         *ptr = NULL ;

	if (length <= 0)
		length = 1;

#if defined(__CYGWIN__)
	/* lets do some magic with Win32 memory allocation to test for memory corruption : */
	ptr = alloc_guarded_memory( length );	   
	if( orig_ptr && ptr ) 
	{
		size_t old_size = get_guarded_memory_size( orig_ptr );	
		if( length < old_size ) 
		{	
#ifdef LOCAL_DEBUG			
			fprintf( stderr, "ALLOC: trying to reallocate memory from %d bytes to %d bytes - truncating.\n", old_size, length );
#endif			
			old_size = length;
		}
		memcpy( ptr, orig_ptr, old_size );
		free_guarded_memory( orig_ptr );
	}
#else
	ptr = realloc (orig_ptr, length);
#endif

	if (ptr == (char *)0)
		failed_alloc( "guarded_realloc", length );
	
	return ptr;
}


void         *
safecalloc (size_t num, size_t blength)
{
#if defined(__CYGWIN__) && defined(DEBUG_ALLOCS) && !defined(NOGUARD)
	return guarded_calloc( num, blength );
#else
	char         *ptr;

    if (blength <= 0)
        blength = 1;
    
	if (num <= 0)
        num = 1;

	ptr = calloc (num, blength);
	if (ptr == (char *)0)
		failed_alloc( "calloc", num*blength );
	return ptr;
#endif
}

void         *
guarded_calloc (size_t num, size_t blength)
{
	char         *ptr;

    if (blength <= 0)
        blength = 1;
    
	if (num <= 0)
        num = 1;

#if defined(__CYGWIN__)
	ptr = alloc_guarded_memory( num * blength );	   
	if(ptr)
		memset( ptr, 0x00, num*blength );
#else    
	ptr = calloc (num, blength);
#endif

	if (ptr == (char *)0)
		failed_alloc( "guarded_calloc", num*blength );
	return ptr;
}


void
safefree (void *ptr)
{
    if (ptr)
	{
#if defined(__CYGWIN__) && defined(DEBUG_ALLOCS) && !defined(NOGUARD)
		free_guarded_memory( ptr );		   
#else
		free (ptr);
#endif
	}
}

void
guarded_free (void *ptr)
{
    if (ptr)
#if defined(__CYGWIN__)
		free_guarded_memory( ptr );		   
#else
		free (ptr);
#endif
}


void
dump_memory()
{
}