kerneltest/e32test/heap/t_heapcheck.cpp
author hgs
Wed, 20 Oct 2010 13:58:28 +0100
changeset 293 0659d0e1a03c
parent 189 a5496987b1da
permissions -rw-r--r--
201041_07
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
189
a5496987b1da 201025_04
hgs
parents:
diff changeset
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
a5496987b1da 201025_04
hgs
parents:
diff changeset
     2
// All rights reserved.
a5496987b1da 201025_04
hgs
parents:
diff changeset
     3
// This component and the accompanying materials are made available
a5496987b1da 201025_04
hgs
parents:
diff changeset
     4
// under the terms of the License "Eclipse Public License v1.0"
a5496987b1da 201025_04
hgs
parents:
diff changeset
     5
// which accompanies this distribution, and is available
a5496987b1da 201025_04
hgs
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
a5496987b1da 201025_04
hgs
parents:
diff changeset
     7
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
     8
// Initial Contributors:
a5496987b1da 201025_04
hgs
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
a5496987b1da 201025_04
hgs
parents:
diff changeset
    10
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    11
// Contributors:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    12
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    13
// Description:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    14
// e32test\debug\t_heapcorruption.cpp
a5496987b1da 201025_04
hgs
parents:
diff changeset
    15
// This is a test application that will cause heap corruption 
a5496987b1da 201025_04
hgs
parents:
diff changeset
    16
// to generate BTrace events (EHeapCorruption).
a5496987b1da 201025_04
hgs
parents:
diff changeset
    17
// 
a5496987b1da 201025_04
hgs
parents:
diff changeset
    18
//
a5496987b1da 201025_04
hgs
parents:
diff changeset
    19
a5496987b1da 201025_04
hgs
parents:
diff changeset
    20
//  Include Files
a5496987b1da 201025_04
hgs
parents:
diff changeset
    21
#include <e32test.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
    22
#include <e32base.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
    23
#include <e32panic.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
    24
#include <e32cmn.h>
a5496987b1da 201025_04
hgs
parents:
diff changeset
    25
#include "dla.h"
a5496987b1da 201025_04
hgs
parents:
diff changeset
    26
#include "slab.h"
a5496987b1da 201025_04
hgs
parents:
diff changeset
    27
#include "page_alloc.h"
a5496987b1da 201025_04
hgs
parents:
diff changeset
    28
#include "heap_hybrid.h"
a5496987b1da 201025_04
hgs
parents:
diff changeset
    29
a5496987b1da 201025_04
hgs
parents:
diff changeset
    30
LOCAL_D RTest test(_L("T_HEAPCHECK"));
a5496987b1da 201025_04
hgs
parents:
diff changeset
    31
a5496987b1da 201025_04
hgs
parents:
diff changeset
    32
TUint32 gSeed = 0xb504f334;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    33
a5496987b1da 201025_04
hgs
parents:
diff changeset
    34
_LIT(KLitHeapCheck,"Heap Check");
a5496987b1da 201025_04
hgs
parents:
diff changeset
    35
a5496987b1da 201025_04
hgs
parents:
diff changeset
    36
a5496987b1da 201025_04
hgs
parents:
diff changeset
    37
TUint32 Random()
a5496987b1da 201025_04
hgs
parents:
diff changeset
    38
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
    39
	gSeed *= 69069;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    40
	gSeed += 41;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    41
	return gSeed;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    42
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
    43
a5496987b1da 201025_04
hgs
parents:
diff changeset
    44
TInt RandomNumber(TInt aMin, TInt aMax)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    45
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
    46
	TInt y = aMax - aMin;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    47
	if ( y <= 0 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
    48
		return aMax;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    49
	TUint32 x = Random() & 0xff;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    50
	TInt s = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    51
	while ( y > (0x100 << s) )
a5496987b1da 201025_04
hgs
parents:
diff changeset
    52
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
    53
		s++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    54
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
    55
	return (aMin + (x << s) % y);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    56
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
    57
a5496987b1da 201025_04
hgs
parents:
diff changeset
    58
a5496987b1da 201025_04
hgs
parents:
diff changeset
    59
/**
a5496987b1da 201025_04
hgs
parents:
diff changeset
    60
Friend class of RHeapHybrid to access to hybrid heap metadata
a5496987b1da 201025_04
hgs
parents:
diff changeset
    61
*/
a5496987b1da 201025_04
hgs
parents:
diff changeset
    62
class TestHybridHeap
a5496987b1da 201025_04
hgs
parents:
diff changeset
    63
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
    64
	public:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    65
		TBool Init();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    66
		TBool Check();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    67
		TUint8* Alloc(TInt aLth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    68
		TUint8* ReAlloc(TAny* aBfr, TInt aLth, TInt aMode);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    69
		void   Free(TAny* aBfr);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    70
		TInt  AllocLen(TAny* aBfr);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    71
		TInt  AllocSize(TInt& aTotalAllocSize);						
a5496987b1da 201025_04
hgs
parents:
diff changeset
    72
		TBool SlabAllocatorExists();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    73
		TBool PageAllocatorExists();			   
a5496987b1da 201025_04
hgs
parents:
diff changeset
    74
		TBool SlabsCreated();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    75
		TBool CorruptSmallBin();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    76
		TBool CorruptTreeBin();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    77
		TBool ConfigurePageAllocator();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    78
		TInt  CopyPageBitmap(TUint8* aBitmap, TInt aLth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    79
		TBool RestorePageBitmap(TUint8* aBitmap, TInt aLth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    80
		void AllocateSomeBuffers(TUint8** aBfrs, TInt aMinLth, TInt MaxLth, TInt aCount);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    81
		TBool PrintHeapInitData();		
a5496987b1da 201025_04
hgs
parents:
diff changeset
    82
a5496987b1da 201025_04
hgs
parents:
diff changeset
    83
	private:
a5496987b1da 201025_04
hgs
parents:
diff changeset
    84
		RHybridHeap* iHybridHeap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    85
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
    86
a5496987b1da 201025_04
hgs
parents:
diff changeset
    87
a5496987b1da 201025_04
hgs
parents:
diff changeset
    88
a5496987b1da 201025_04
hgs
parents:
diff changeset
    89
TBool TestHybridHeap::Init()
a5496987b1da 201025_04
hgs
parents:
diff changeset
    90
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
    91
	RHybridHeap::STestCommand cmd;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    92
	cmd.iCommand = RHybridHeap::EHeapMetaData;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    93
	RAllocator& heap = User::Allocator();
a5496987b1da 201025_04
hgs
parents:
diff changeset
    94
	TInt ret = heap.DebugFunction(RHeap::EHybridHeap, &cmd, 0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
    95
	if (ret != KErrNone)
a5496987b1da 201025_04
hgs
parents:
diff changeset
    96
		return EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    97
	iHybridHeap = (RHybridHeap*) cmd.iData;
a5496987b1da 201025_04
hgs
parents:
diff changeset
    98
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
    99
	return ETrue;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   100
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   101
a5496987b1da 201025_04
hgs
parents:
diff changeset
   102
TBool TestHybridHeap::Check()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   103
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   104
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   105
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   106
		iHybridHeap->Check();  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   107
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   108
a5496987b1da 201025_04
hgs
parents:
diff changeset
   109
	return EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   110
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   111
a5496987b1da 201025_04
hgs
parents:
diff changeset
   112
TUint8* TestHybridHeap::Alloc(TInt aLth)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   113
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   114
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   115
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   116
		return (TUint8*)iHybridHeap->Alloc(aLth);  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   117
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   118
a5496987b1da 201025_04
hgs
parents:
diff changeset
   119
	return NULL;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   120
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   121
a5496987b1da 201025_04
hgs
parents:
diff changeset
   122
TUint8* TestHybridHeap::ReAlloc(TAny* aBfr, TInt aLth, TInt aMode)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   123
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   124
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   125
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   126
		return (TUint8*)iHybridHeap->ReAlloc(aBfr, aLth, aMode);  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   127
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   128
a5496987b1da 201025_04
hgs
parents:
diff changeset
   129
	return NULL;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   130
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   131
a5496987b1da 201025_04
hgs
parents:
diff changeset
   132
void TestHybridHeap::Free(TAny* aBfr)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   133
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   134
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   135
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   136
		iHybridHeap->Free(aBfr);  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   137
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   138
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   139
a5496987b1da 201025_04
hgs
parents:
diff changeset
   140
TInt TestHybridHeap::AllocLen(TAny* aBfr)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   141
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   142
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   143
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   144
		return iHybridHeap->AllocLen(aBfr);  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   145
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   146
	return 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   147
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   148
a5496987b1da 201025_04
hgs
parents:
diff changeset
   149
TInt TestHybridHeap::AllocSize(TInt& aTotalAllocSize)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   150
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   151
	aTotalAllocSize = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   152
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   153
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   154
		return iHybridHeap->AllocSize(aTotalAllocSize);  
a5496987b1da 201025_04
hgs
parents:
diff changeset
   155
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   156
	return 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   157
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   158
a5496987b1da 201025_04
hgs
parents:
diff changeset
   159
TBool TestHybridHeap::SlabAllocatorExists()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   160
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   161
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   162
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   163
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   164
		status = !iHybridHeap->iDLOnly;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   165
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   166
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   167
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   168
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   169
a5496987b1da 201025_04
hgs
parents:
diff changeset
   170
TBool TestHybridHeap::PageAllocatorExists()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   171
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   172
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   173
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   174
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   175
		status = (!iHybridHeap->iDLOnly && (iHybridHeap->iPageThreshold < 31));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   176
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   177
a5496987b1da 201025_04
hgs
parents:
diff changeset
   178
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   179
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   180
a5496987b1da 201025_04
hgs
parents:
diff changeset
   181
TBool TestHybridHeap::SlabsCreated()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   182
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   183
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   184
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   185
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   186
		status = (iHybridHeap->iSlabThreshold != 0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   187
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   188
a5496987b1da 201025_04
hgs
parents:
diff changeset
   189
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   190
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   191
a5496987b1da 201025_04
hgs
parents:
diff changeset
   192
TBool TestHybridHeap::ConfigurePageAllocator()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   193
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   194
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   195
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   196
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   197
		RHybridHeap::STestCommand conf;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   198
		conf.iCommand = RHybridHeap::ESetConfig;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   199
		conf.iConfig.iPagePower = 14;  // 16 Kb		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   200
		if ( iHybridHeap->DebugFunction(RHeap::EHybridHeap, (TAny*)&conf ) == KErrNone )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   201
			status = ETrue;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   202
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   203
a5496987b1da 201025_04
hgs
parents:
diff changeset
   204
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   205
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   206
a5496987b1da 201025_04
hgs
parents:
diff changeset
   207
a5496987b1da 201025_04
hgs
parents:
diff changeset
   208
TBool TestHybridHeap::CorruptTreeBin()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   209
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   210
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   211
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   212
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   213
		TUint i;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   214
		for (i = 0; i < NTREEBINS; ++i)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   215
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   216
			tbinptr* tb = TREEBIN_AT(&iHybridHeap->iGlobalMallocState, i);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   217
			tchunkptr t = *tb;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   218
			if ( t )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   219
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   220
				// Corrupt tree bin by writing erroneous index value
a5496987b1da 201025_04
hgs
parents:
diff changeset
   221
				t->iIndex ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   222
				return ETrue;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   223
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   224
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   225
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   226
a5496987b1da 201025_04
hgs
parents:
diff changeset
   227
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   228
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   229
a5496987b1da 201025_04
hgs
parents:
diff changeset
   230
TBool TestHybridHeap::CorruptSmallBin()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   231
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   232
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   233
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   234
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   235
		TUint i;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   236
		for (i = 0; i < NSMALLBINS; ++i)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   237
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   238
			sbinptr b = SMALLBIN_AT(&iHybridHeap->iGlobalMallocState, i);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   239
			mchunkptr p = b->iBk;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   240
			if ( p != b )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   241
				{ 
a5496987b1da 201025_04
hgs
parents:
diff changeset
   242
				b->iBk = b;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   243
				status = ETrue;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   244
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   245
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   246
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   247
a5496987b1da 201025_04
hgs
parents:
diff changeset
   248
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   249
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   250
a5496987b1da 201025_04
hgs
parents:
diff changeset
   251
TInt TestHybridHeap::CopyPageBitmap(TUint8* aBitmap, TInt aLth)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   252
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   253
	TInt lth = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   254
	if ( iHybridHeap && (aLth > (TInt) sizeof(iHybridHeap->iBitMapBuffer)) )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   255
		{// Dirty version
a5496987b1da 201025_04
hgs
parents:
diff changeset
   256
		memcpy(aBitmap, &iHybridHeap->iBitMapBuffer[0], sizeof(iHybridHeap->iBitMapBuffer));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   257
        lth = sizeof(iHybridHeap->iBitMapBuffer) << 3;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   258
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   259
a5496987b1da 201025_04
hgs
parents:
diff changeset
   260
	return lth;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   261
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   262
a5496987b1da 201025_04
hgs
parents:
diff changeset
   263
TBool TestHybridHeap::RestorePageBitmap(TUint8* aBitmap, TInt aLth)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   264
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   265
	TBool status = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   266
	if ( iHybridHeap && ((aLth >> 3) <= (TInt) sizeof(iHybridHeap->iBitMapBuffer)) )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   267
		{// Dirty version
a5496987b1da 201025_04
hgs
parents:
diff changeset
   268
		memcpy(&iHybridHeap->iBitMapBuffer[0], aBitmap, (aLth >> 3));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   269
		status = ETrue;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   270
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   271
a5496987b1da 201025_04
hgs
parents:
diff changeset
   272
	return status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   273
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   274
a5496987b1da 201025_04
hgs
parents:
diff changeset
   275
void TestHybridHeap::AllocateSomeBuffers(TUint8** aBfrs, TInt aMinLth, TInt MaxLth, TInt aCount )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   276
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   277
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   278
	TInt loop = RandomNumber(2, 8);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   279
a5496987b1da 201025_04
hgs
parents:
diff changeset
   280
	while ( loop )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   281
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   282
		// allocate all buffers
a5496987b1da 201025_04
hgs
parents:
diff changeset
   283
		TInt i;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   284
		for (i=0; i<aCount; ++i)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   285
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   286
			if (!aBfrs[i])
a5496987b1da 201025_04
hgs
parents:
diff changeset
   287
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   288
				aBfrs[i] = (TUint8*)Alloc(RandomNumber(aMinLth, MaxLth));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   289
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   290
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   291
a5496987b1da 201025_04
hgs
parents:
diff changeset
   292
		// free some cells
a5496987b1da 201025_04
hgs
parents:
diff changeset
   293
		TInt n = RandomNumber(2, aCount);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   294
		while (--n)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   295
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   296
			i = RandomNumber(2, aCount);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   297
			if (aBfrs[i])
a5496987b1da 201025_04
hgs
parents:
diff changeset
   298
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   299
				Free(aBfrs[i]);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   300
				aBfrs[i] = NULL;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   301
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   302
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   303
a5496987b1da 201025_04
hgs
parents:
diff changeset
   304
		// realloc some cells
a5496987b1da 201025_04
hgs
parents:
diff changeset
   305
		n = RandomNumber(2, aCount);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   306
		while (--n)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   307
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   308
			TInt new_len = RandomNumber(aMinLth, MaxLth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   309
			if (aBfrs[i])
a5496987b1da 201025_04
hgs
parents:
diff changeset
   310
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   311
				TUint8* p = (TUint8*)ReAlloc(aBfrs[i], new_len, Random());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   312
				if (p)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   313
					{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   314
					aBfrs[i] = p;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   315
					}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   316
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   317
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   318
a5496987b1da 201025_04
hgs
parents:
diff changeset
   319
		loop --;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   320
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   321
a5496987b1da 201025_04
hgs
parents:
diff changeset
   322
}	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   323
a5496987b1da 201025_04
hgs
parents:
diff changeset
   324
TBool TestHybridHeap::PrintHeapInitData()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   325
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   326
	TInt total;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   327
	TInt count = AllocSize(total);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   328
	RDebug::Printf("Heap initialised for test, alloc count: %d , alloc size: %d\n", count, total);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   329
	if ( iHybridHeap )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   330
		RDebug::Printf("Heap initialised for test, iCellCount: %d , iTotalAllocSize: %d\n", iHybridHeap->iCellCount, iHybridHeap->iTotalAllocSize);	    	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   331
	return (count != 0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   332
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   333
a5496987b1da 201025_04
hgs
parents:
diff changeset
   334
a5496987b1da 201025_04
hgs
parents:
diff changeset
   335
//  Local Functions
a5496987b1da 201025_04
hgs
parents:
diff changeset
   336
LOCAL_D TInt HeapCheckTestThread(TAny* param)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   337
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   338
	TInt t = *((TInt*)param);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   339
	TUint8* bfrs[256];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   340
	Mem::FillZ(bfrs, sizeof(bfrs));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   341
	TestHybridHeap heap;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   342
	test(heap.Init());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   343
a5496987b1da 201025_04
hgs
parents:
diff changeset
   344
	switch( t )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   345
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   346
		case 1:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   347
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   348
			// Overwrite Doug Lea buffer and check()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   349
			heap.AllocateSomeBuffers(bfrs, 0x40, 0xfff0, 256);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   350
			test(heap.PrintHeapInitData());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   351
			TUint8 *p = heap.Alloc(64);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   352
			test( p != NULL );
a5496987b1da 201025_04
hgs
parents:
diff changeset
   353
			Mem::FillZ(p, 80);  // Heap corrupted
a5496987b1da 201025_04
hgs
parents:
diff changeset
   354
			heap.Check();    // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   355
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   356
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   357
a5496987b1da 201025_04
hgs
parents:
diff changeset
   358
		case 2:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   359
			// Corrupt a smallbin and check
a5496987b1da 201025_04
hgs
parents:
diff changeset
   360
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   361
			TInt i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   362
			TBool smallbin_corrupted = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   363
			while ( !smallbin_corrupted )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   364
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   365
				heap.AllocateSomeBuffers(bfrs, 0x4, 0xff, 256);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   366
				smallbin_corrupted = heap.CorruptSmallBin();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   367
				i ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   368
				if ( i > 9 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   369
					break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   370
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   371
			test(smallbin_corrupted);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   372
    		test(heap.PrintHeapInitData());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   373
			heap.Check();    // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   374
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   375
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   376
a5496987b1da 201025_04
hgs
parents:
diff changeset
   377
		case 3:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   378
			// Corrupt a treebin and check
a5496987b1da 201025_04
hgs
parents:
diff changeset
   379
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   380
			TInt i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   381
			TBool treebin_corrupted = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   382
			while ( !treebin_corrupted )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   383
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   384
				heap.AllocateSomeBuffers(bfrs, 0x100, 0x4000, 256);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   385
				treebin_corrupted = heap.CorruptTreeBin();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   386
				i ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   387
				if ( i > 9 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   388
					break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   389
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   390
			test(treebin_corrupted);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   391
			test(heap.PrintHeapInitData());						
a5496987b1da 201025_04
hgs
parents:
diff changeset
   392
			heap.Check();    // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   393
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   394
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   395
a5496987b1da 201025_04
hgs
parents:
diff changeset
   396
		case 10:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   397
			// Overwrite slab buffer and check
a5496987b1da 201025_04
hgs
parents:
diff changeset
   398
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   399
			TInt i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   400
			TBool slabs_created = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   401
			if ( !heap.SlabAllocatorExists() )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   402
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   403
				User::Panic(KLitHeapCheck, ETHeapDebugUnmatchedCallToCheckHeap);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   404
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   405
a5496987b1da 201025_04
hgs
parents:
diff changeset
   406
			while ( !slabs_created )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   407
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   408
				// Allocate enough buffers to cause slab allocator to be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   409
				// initialised  		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   410
				heap.AllocateSomeBuffers(bfrs, 0x4, 0x2000, 256);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   411
				slabs_created = heap.SlabsCreated();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   412
				i ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   413
				if ( i > 9 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   414
					break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   415
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   416
			test(slabs_created);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   417
			test(heap.PrintHeapInitData());						
a5496987b1da 201025_04
hgs
parents:
diff changeset
   418
			i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   419
			TUint8* p[10];
a5496987b1da 201025_04
hgs
parents:
diff changeset
   420
			while ( i < 10 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   421
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   422
				p[i] = heap.Alloc(24);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   423
				test( p[i] != NULL );
a5496987b1da 201025_04
hgs
parents:
diff changeset
   424
				i ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   425
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   426
			i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   427
			while ( i < 10 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   428
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   429
				heap.Free(p[i]);	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   430
				i +=2;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   431
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   432
			p[0] = heap.Alloc(24);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   433
			test( p[0] != NULL );
a5496987b1da 201025_04
hgs
parents:
diff changeset
   434
			memset((TUint8*)(Floor(p[0], SLABSIZE) + sizeof(slabhdr)), 0xee, KMaxSlabPayload);  // Heap corrupted
a5496987b1da 201025_04
hgs
parents:
diff changeset
   435
			heap.Check();         // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   436
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   437
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   438
a5496987b1da 201025_04
hgs
parents:
diff changeset
   439
		case 11:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   440
			// Corrupt slab header
a5496987b1da 201025_04
hgs
parents:
diff changeset
   441
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   442
			TInt i = 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   443
			TBool slabs_created = EFalse;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   444
			if ( !heap.SlabAllocatorExists() )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   445
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   446
				User::Panic(KLitHeapCheck, ETHeapDebugUnmatchedCallToCheckHeap);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   447
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   448
a5496987b1da 201025_04
hgs
parents:
diff changeset
   449
			while ( !slabs_created )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   450
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   451
				// Allocate enough buffers to cause slab allocator to be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   452
				// initialised  		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   453
				heap.AllocateSomeBuffers(bfrs, 0x4, 0x2000, 256);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   454
				slabs_created = heap.SlabsCreated();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   455
				i ++;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   456
				if ( i > 9 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   457
					break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   458
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   459
			test(slabs_created);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   460
			test(heap.PrintHeapInitData());						
a5496987b1da 201025_04
hgs
parents:
diff changeset
   461
			TUint8* p = heap.Alloc(28);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   462
			test(p != NULL);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   463
			p = Floor(p, SLABSIZE);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   464
			*(TUint32*)p = 0xffeeddcc;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   465
			heap.Check();      // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   466
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   467
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   468
a5496987b1da 201025_04
hgs
parents:
diff changeset
   469
		case 20:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   470
			// Corrupt page bitmap data and check
a5496987b1da 201025_04
hgs
parents:
diff changeset
   471
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   472
			if ( !heap.PageAllocatorExists() )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   473
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   474
				User::Panic(KLitHeapCheck, ETHeapDebugUnmatchedCallToCheckHeap);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   475
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   476
			test(heap.ConfigurePageAllocator());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   477
			// Allocate some buffers to cause slab allocator to be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   478
			// initialised  		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   479
			heap.AllocateSomeBuffers(bfrs, 0x4000, 0x10000, 16);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   480
			test(heap.PrintHeapInitData());						
a5496987b1da 201025_04
hgs
parents:
diff changeset
   481
			TUint8* bitmap = heap.Alloc(128);  // For saved bitmap
a5496987b1da 201025_04
hgs
parents:
diff changeset
   482
			test(bitmap != NULL);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   483
			TInt bit_lth = heap.CopyPageBitmap(bitmap, 128);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   484
			test(bit_lth != 0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   485
			memset(bitmap, 0xee, (bit_lth>>3));  //  corrupt bitmap data
a5496987b1da 201025_04
hgs
parents:
diff changeset
   486
			heap.RestorePageBitmap(bitmap, bit_lth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   487
			heap.Check();      // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   488
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   489
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   490
a5496987b1da 201025_04
hgs
parents:
diff changeset
   491
		case 21:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   492
			// Corrupt page bitmap with a earlier freed "ghost" buffer info
a5496987b1da 201025_04
hgs
parents:
diff changeset
   493
			{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   494
			if ( !heap.PageAllocatorExists() )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   495
				{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   496
				User::Panic(KLitHeapCheck, ETHeapDebugUnmatchedCallToCheckHeap);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   497
				}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   498
			test(heap.ConfigurePageAllocator());
a5496987b1da 201025_04
hgs
parents:
diff changeset
   499
			// Allocate some buffers to cause slab allocator to be
a5496987b1da 201025_04
hgs
parents:
diff changeset
   500
			// initialised  		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   501
			heap.AllocateSomeBuffers(bfrs, 0x4000, 0x10000, 16);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   502
			test(heap.PrintHeapInitData());						
a5496987b1da 201025_04
hgs
parents:
diff changeset
   503
			TUint8* bitmap = heap.Alloc(128);  // For saved bitmap
a5496987b1da 201025_04
hgs
parents:
diff changeset
   504
			test(bitmap != NULL);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   505
			TUint8* p = heap.Alloc(0x8000);     // One more page buffer
a5496987b1da 201025_04
hgs
parents:
diff changeset
   506
			TInt bit_lth = heap.CopyPageBitmap(bitmap, 128);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   507
			test(bit_lth != 0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   508
			heap.Free(p);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   509
			heap.RestorePageBitmap(bitmap, bit_lth);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   510
			heap.Check();      // This should cause panic
a5496987b1da 201025_04
hgs
parents:
diff changeset
   511
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   512
			}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   513
a5496987b1da 201025_04
hgs
parents:
diff changeset
   514
		default:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   515
			break;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   516
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   517
a5496987b1da 201025_04
hgs
parents:
diff changeset
   518
	User::Invariant();	// Should not reach here 
a5496987b1da 201025_04
hgs
parents:
diff changeset
   519
	return 0;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   520
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   521
a5496987b1da 201025_04
hgs
parents:
diff changeset
   522
a5496987b1da 201025_04
hgs
parents:
diff changeset
   523
class TestHeapCheck
a5496987b1da 201025_04
hgs
parents:
diff changeset
   524
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   525
	public:
a5496987b1da 201025_04
hgs
parents:
diff changeset
   526
		void TestCheck(void);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   527
		TInt TestThreadExit(RThread& aThread, TExitType aExitType, TInt aExitReason);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   528
};
a5496987b1da 201025_04
hgs
parents:
diff changeset
   529
a5496987b1da 201025_04
hgs
parents:
diff changeset
   530
a5496987b1da 201025_04
hgs
parents:
diff changeset
   531
TInt TestHeapCheck::TestThreadExit(RThread& aThread, TExitType aExitType, TInt aExitReason)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   532
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   533
	// Disable JIT debugging.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   534
	TBool justInTime=User::JustInTime();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   535
	User::SetJustInTime(EFalse);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   536
a5496987b1da 201025_04
hgs
parents:
diff changeset
   537
	TRequestStatus status;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   538
	aThread.Logon(status); 
a5496987b1da 201025_04
hgs
parents:
diff changeset
   539
	aThread.Resume();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   540
	User::WaitForRequest(status);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   541
	if (aExitType != aThread.ExitType())
a5496987b1da 201025_04
hgs
parents:
diff changeset
   542
		return KErrGeneral;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   543
a5496987b1da 201025_04
hgs
parents:
diff changeset
   544
	if ( (status.Int() == ETHeapDebugUnmatchedCallToCheckHeap) && (aThread.ExitReason() == ETHeapDebugUnmatchedCallToCheckHeap))
a5496987b1da 201025_04
hgs
parents:
diff changeset
   545
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   546
		CLOSE_AND_WAIT(aThread);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   547
		// Put JIT debugging back to previous status.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   548
		User::SetJustInTime(justInTime);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   549
		return KErrNotSupported;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   550
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   551
a5496987b1da 201025_04
hgs
parents:
diff changeset
   552
	if ( status.Int() == ERTestFailed )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   553
		return KErrGeneral;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   554
a5496987b1da 201025_04
hgs
parents:
diff changeset
   555
	if ( aExitReason > 0 )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   556
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   557
		if (aExitReason != status.Int())
a5496987b1da 201025_04
hgs
parents:
diff changeset
   558
			return KErrGeneral;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   559
a5496987b1da 201025_04
hgs
parents:
diff changeset
   560
		if (aExitReason != aThread.ExitReason())
a5496987b1da 201025_04
hgs
parents:
diff changeset
   561
			return KErrGeneral;		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   562
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   563
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   564
	CLOSE_AND_WAIT(aThread);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   565
a5496987b1da 201025_04
hgs
parents:
diff changeset
   566
	// Put JIT debugging back to previous status.
a5496987b1da 201025_04
hgs
parents:
diff changeset
   567
	User::SetJustInTime(justInTime);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   568
	return KErrNone;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   569
a5496987b1da 201025_04
hgs
parents:
diff changeset
   570
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   571
a5496987b1da 201025_04
hgs
parents:
diff changeset
   572
void TestHeapCheck::TestCheck()
a5496987b1da 201025_04
hgs
parents:
diff changeset
   573
{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   574
    TInt type;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   575
	TInt r;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   576
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   577
	test.Next(_L("Testing Doug Lea allocator check"));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   578
	{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   579
	type = 1;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   580
	RThread thread;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   581
	test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x400000,  (TAny*) &type)== KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   582
	test(TestThreadExit(thread, EExitPanic, ETHeapBadCellAddress)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   583
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   584
    type = 2;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   585
    test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x400000,  (TAny*) &type)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   586
    test(TestThreadExit(thread, EExitPanic, ETHeapBadCellAddress)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   587
a5496987b1da 201025_04
hgs
parents:
diff changeset
   588
	type = 3;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   589
	test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x400000,  (TAny*) &type)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   590
	test(TestThreadExit(thread, EExitPanic, ETHeapBadCellAddress)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   591
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   592
	}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   593
a5496987b1da 201025_04
hgs
parents:
diff changeset
   594
	test.Next(_L("Testing Slab allocator check"));	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   595
	{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   596
	type = 10;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   597
	RThread thread;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   598
	test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x400000,  (TAny*) &type)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   599
	r = TestThreadExit(thread, EExitPanic, ETHeapBadCellAddress);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   600
	if ( r != KErrNotSupported )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   601
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   602
		test(r==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   603
		
a5496987b1da 201025_04
hgs
parents:
diff changeset
   604
		type = 11;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   605
		RThread thread;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   606
		test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x400000,  (TAny*) &type)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   607
		test(TestThreadExit(thread, EExitPanic, ETHeapBadCellAddress)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   608
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   609
	else test.Printf(_L("Slab allocator does not exist, testes bypassed\n"));	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   610
	}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   611
a5496987b1da 201025_04
hgs
parents:
diff changeset
   612
	test.Next(_L("Testing Page allocator check"));	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   613
	{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   614
	type = 20;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   615
	RThread thread;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   616
	test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x800000,  (TAny*) &type)==KErrNone);	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   617
	r = TestThreadExit(thread, EExitPanic, KErrNone);   // Accept any panic reason here
a5496987b1da 201025_04
hgs
parents:
diff changeset
   618
	if ( r != KErrNotSupported )
a5496987b1da 201025_04
hgs
parents:
diff changeset
   619
		{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   620
		test(r==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   621
a5496987b1da 201025_04
hgs
parents:
diff changeset
   622
		type = 21;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   623
		RThread thread;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   624
		test(thread.Create(_L("Check UserHeap"),HeapCheckTestThread, KDefaultStackSize, 0x1000, 0x800000,  (TAny*) &type)==KErrNone);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   625
		test(TestThreadExit(thread, EExitPanic,	KErrNone)==KErrNone);  // Accept any panic reason here
a5496987b1da 201025_04
hgs
parents:
diff changeset
   626
		}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   627
    else test.Printf(_L("Page allocator does not exist, testes bypassed\n"));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   628
	}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   629
a5496987b1da 201025_04
hgs
parents:
diff changeset
   630
}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   631
a5496987b1da 201025_04
hgs
parents:
diff changeset
   632
a5496987b1da 201025_04
hgs
parents:
diff changeset
   633
a5496987b1da 201025_04
hgs
parents:
diff changeset
   634
a5496987b1da 201025_04
hgs
parents:
diff changeset
   635
//  Global Functions
a5496987b1da 201025_04
hgs
parents:
diff changeset
   636
a5496987b1da 201025_04
hgs
parents:
diff changeset
   637
GLDEF_C TInt E32Main(void)
a5496987b1da 201025_04
hgs
parents:
diff changeset
   638
	{
a5496987b1da 201025_04
hgs
parents:
diff changeset
   639
a5496987b1da 201025_04
hgs
parents:
diff changeset
   640
	test.Title();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   641
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   642
	test.Start(_L("Testing Heap Check function"));
a5496987b1da 201025_04
hgs
parents:
diff changeset
   643
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   644
	TestHeapCheck T;
a5496987b1da 201025_04
hgs
parents:
diff changeset
   645
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   646
	T.TestCheck();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   647
a5496987b1da 201025_04
hgs
parents:
diff changeset
   648
	test.End();
a5496987b1da 201025_04
hgs
parents:
diff changeset
   649
	
a5496987b1da 201025_04
hgs
parents:
diff changeset
   650
	return(0);
a5496987b1da 201025_04
hgs
parents:
diff changeset
   651
	}
a5496987b1da 201025_04
hgs
parents:
diff changeset
   652