summaryrefslogtreecommitdiffstats
path: root/private/crt32/heap/heapchk.c
blob: 872b124d5110a05fb0665600e97b0f618d6a776d (plain) (blame)
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
/***
*heapchk.c - perform a consistency check on the heap
*
*	Copyright (c) 1989-1991, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	Defines the _heapchk() and _heapset() functions
*
*Revision History:
*	06-30-89   JCR	Module created.
*	07-28-89   GJF	Added check for free block preceding the rover
*	11-13-89   GJF	Added MTHREAD support, also fixed copyright
*	12-13-89   GJF	Added check for descriptor order, did some tuning,
*			changed header file name to heap.h
*	12-15-89   GJF	Purged DEBUG286 stuff. Also added explicit _cdecl to
*			function definitions.
*	12-19-89   GJF	Got rid of checks involving plastdesc (revised check
*			of proverdesc and DEBUG errors accordingly)
*	03-09-90   GJF	Replaced _cdecl with _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	03-29-90   GJF	Made _heap_checkset() _CALLTYPE4.
*	09-27-90   GJF	New-style function declarators.
*	03-05-91   GJF	Changed strategy for rover - old version available
*			by #define-ing _OLDROVER_.
*
*******************************************************************************/

#include <cruntime.h>
#include <heap.h>
#include <malloc.h>
#include <os2dll.h>
#include <stddef.h>
#include <string.h>
#ifdef DEBUG
#include <stdio.h>
#endif

static int _CALLTYPE4 _heap_checkset(unsigned int _fill);

/* Debug error values */
#define _EMPTYHEAP   0
#define _BADROVER    1
#define _BADRANGE    2
#define _BADSENTINEL 3
#define _BADEMPTY    4
#define _EMPTYLOOP   5
#define _BADFREE     6
#define _BADORDER    7

#ifdef DEBUG

static char *errmsgs[] = {
	"_heap_desc.pfirstdesc == NULL",
	"_heap_desc.proverdesc not found in desc list",
	"address is outside the heap",
	"sentinel descriptor corrupted",
	"empty desc pblock != NULL (debug version)",
	"header ptr found twice in emptylist",
	"free block precedes rover",
	"adjacent descriptors in reverse order from heap blocks"
	};

#define _PRINTERR(msg) \
	printf("\n*** HEAP ERROR: %s ***\n", errmsgs[(msg)]);  \
	fflush(stdout);

#else	/* !DEBUG */

#define _PRINTERR(msg)

#endif	/* DEBUG */


/***
*int _heapchk()      - Validate the heap
*int _heapset(_fill) - Validate the heap and fill in free entries
*
*Purpose:
*	Performs a consistency check on the heap.
*
*Entry:
*	For heapchk()
*		No arguments
*	For heapset()
*		int _fill - value to be used as filler in free entries
*
*Exit:
*	Returns one of the following values:
*
*		_HEAPOK 	 - completed okay
*		_HEAPEMPTY	 - heap not initialized
*		_HEAPBADBEGIN	 - can't find initial header info
*		_HEAPBADNODE	 - malformed node somewhere
*
*	Debug version prints out a diagnostic message if an error is found
*	(see errmsg[] above).
*
*	NOTE:  Add code to support memory regions.
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/

int _CALLTYPE1 _heapchk(void)
{
	return(_heap_checkset((unsigned int)_HEAP_NOFILL));
}

/******************************************************************************/

int _CALLTYPE1 _heapset (
	unsigned int _fill
	)
{
	return(_heap_checkset(_fill));
}


/***
*static int _heap_checkset(_fill) - check the heap and fill in the
*	free entries
*
*Purpose:
*	Workhorse routine for both _heapchk() and _heapset().
*
*Entry:
*	int _fill - either _HEAP_NOFILL or a value to be used as filler in
*		    free entries
*
*Exit:
*	See description of _heapchk()/_heapset()
*
*******************************************************************************/

static int _CALLTYPE4 _heap_checkset (
	unsigned int _fill
	)
{
	REG1 _PBLKDESC pdesc;
	REG2 _PBLKDESC pnext;
	int roverfound=0;
	int retval = _HEAPOK;

	/*
	 * lock the heap
	 */

	_mlock(_HEAP_LOCK);

	/*
	 * Validate the sentinel
	 */

	if (_heap_desc.sentinel.pnextdesc != NULL) {
		_PRINTERR(_BADSENTINEL);
		retval = _HEAPBADNODE;
		goto done;
	}

	/*
	 * Test for an empty heap
	 */

	if ( (_heap_desc.pfirstdesc == &_heap_desc.sentinel) &&
	     (_heap_desc.proverdesc == &_heap_desc.sentinel) ) {
		retval = _HEAPEMPTY;
		goto done;
	}

	/*
	 * Get and validate the first descriptor
	 */

	if ((pdesc = _heap_desc.pfirstdesc) == NULL) {
		_PRINTERR(_EMPTYHEAP);
		retval = _HEAPBADBEGIN;
		goto done;
	}

	/*
	 * Walk the heap descriptor list
	 */

	while (pdesc != &_heap_desc.sentinel) {

		/*
		 * Make sure address for this entry is in range.
		 */

		if ( (_ADDRESS(pdesc) < _ADDRESS(_heap_desc.pfirstdesc)) ||
		     (_ADDRESS(pdesc) > _heap_desc.sentinel.pblock) ) {
			_PRINTERR(_BADRANGE);
			retval = _HEAPBADNODE;
			goto done;
		}

		pnext = pdesc->pnextdesc;

		/*
		 * Make sure the blocks corresponding to pdesc and pnext are
		 * in proper order.
		 */

		if ( _ADDRESS(pdesc) >= _ADDRESS(pnext) ) {
			_PRINTERR(_BADORDER);
			retval = _HEAPBADNODE;
			goto done;
		}

		/*
		 * Check the backpointer.
		 */

		if (_IS_INUSE(pdesc) || _IS_FREE(pdesc)) {

			if (!_CHECK_PDESC(pdesc)) {
				retval = _HEAPBADPTR;
				goto done;
			}
		}

		/*
		 * Check for proverdesc
		 */

		if (pdesc == _heap_desc.proverdesc)
			roverfound++;

#ifdef	_OLDROVER_

		/*
		 * If it is free, make sure it doesn't precede the rover
		 * block and fill it in if appropriate
		 */

		if ( _IS_FREE(pdesc) ) {

			if ( roverfound == 0 ) {
				_PRINTERR(_BADFREE);
				retval = _HEAPBADNODE;
				goto done;
			}

			if ( _fill != _HEAP_NOFILL )
				memset( (void *)((unsigned)_ADDRESS(pdesc)+_HDRSIZE),
				_fill, _BLKSIZE(pdesc) );
		}

#else	/* ndef _OLDROVER_ */

		/*
		 * If it is free, fill it in if appropriate
		 */

		if ( _IS_FREE(pdesc) && (_fill != _HEAP_NOFILL) )
			memset( (void *)((unsigned)_ADDRESS(pdesc)+_HDRSIZE),
			_fill, _BLKSIZE(pdesc) );

#endif	/* _OLDROVER_ */

		/*
		 * Onto the next block
		 */

		pdesc = pnext;
	}

	/*
	 * Make sure we found 1 and only 1 rover
	 */

	if (_heap_desc.proverdesc == &_heap_desc.sentinel)
		roverfound++;

	if (roverfound != 1) {
		_PRINTERR(_BADROVER);
		retval = _HEAPBADBEGIN;
		goto done;
	}

	/*
	 * Walk the empty list.  We can't really compare values against
	 * anything but we may loop forever or may cause a fault.
	 */

	pdesc = _heap_desc.emptylist;

	while (pdesc != NULL) {

#ifdef DEBUG
		if (pdesc->pblock != NULL) {
			_PRINTERR(_BADEMPTY)
			retval = _HEAPBADPTR;
			goto done;
		}
#endif

		pnext = pdesc->pnextdesc;

		/*
		 * Header should only appear once
		 */

		if (pnext == _heap_desc.emptylist) {
			_PRINTERR(_EMPTYLOOP)
			retval = _HEAPBADPTR;
			goto done;
		}

		pdesc = pnext;

	}


	/*
	 * Common return
	 */

done:
	/*
	 * release the heap lock
	 */

	_munlock(_HEAP_LOCK);

	return(retval);

}