Update contrib.
1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\heap\t_fail.cpp
16 // Test deterministic, random and fail-next heap failure modes.
20 // - Simulate the EFailNext, EDeterministic, ERandom, ETrueRandom,
21 // ENone modes of heap allocation failures and the burst variants.
22 // - Reallocate the size of an existing cell without moving it
23 // and check the result is as expected.
24 // - Check whether heap has been corrupted by all the tests.
25 // Platforms/Drives/Compatibility:
27 // Assumptions/Requirement/Pre-requisites:
28 // Failures and causes:
29 // Base Port information:
33 #define __E32TEST_EXTENSION__
39 #include <e32def_private.h>
41 #include <e32ldr_private.h>
44 LOCAL_D RTest test(_L("T_FAIL"));
45 RKHeapDevice KHeapDevice;
51 Test we fail burst times for EBurstFailNext
52 Defined as a macro so that it is easier to determine which test is failing.
54 @param aCount The number of allocations before it should fail.
55 @param aBurst The number of allocations that should fail.
58 #define __UHEAP_TEST_BURST_FAILNEXT(aCount, aBurst) \
59 __UHEAP_BURSTFAILNEXT(aCount, aBurst); \
60 TEST_BURST_FAILNEXT(__UHEAP_CHECKFAILURE, aCount, aBurst)
63 #define __RHEAP_TEST_BURST_FAILNEXT(aHeap, aCount, aBurst) \
64 __RHEAP_BURSTFAILNEXT(aHeap, aCount, aBurst); \
65 TEST_BURST_FAILNEXT(__RHEAP_CHECKFAILURE(aHeap), aCount, aBurst)
68 #define __KHEAP_TEST_BURST_FAILNEXT(aCount, aBurst) \
69 __KHEAP_BURSTFAILNEXT(aCount, aBurst); \
70 test_Equal(0, __KHEAP_CHECKFAILURE); \
71 test_KErrNone(KHeapDevice.TestBurstFailNext(aCount, aBurst)); \
72 test_Equal(aBurst, __KHEAP_CHECKFAILURE)
75 #define TEST_BURST_FAILNEXT(aCheckFailure, aCount, aBurst) \
77 test_Equal(0, aCheckFailure); \
78 for (i = 0; i < aCount; i++) \
88 for (TUint j = 0; j < aBurst; j++) \
91 test_Equal(NULL, p); \
92 test_Equal(j + 1, aCheckFailure); \
103 Test we fail burst times for EBurstDeterministic
104 Defined as a macro so that it is easier to determine which test is failing.
106 @param aRate The rate of each set of failures.
107 @param aBurst The number of allocations that should fail.
109 #define __UHEAP_TEST_BURST_DETERMINISTIC(aRate, aBurst) \
110 __UHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, aRate, aBurst); \
111 TEST_BURST_DETERMINISTIC(__UHEAP_CHECKFAILURE, aRate, aBurst)
113 #define __RHEAP_TEST_BURST_DETERMINISTIC(aHeap, aRate, aBurst) \
114 __RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstDeterministic, aRate, aBurst); \
115 TEST_BURST_DETERMINISTIC(__RHEAP_CHEAKFAILURE(aHeap), aRate, aBurst)
117 #define __KHEAP_TEST_BURST_DETERMINISTIC(aRate, aBurst) \
118 __KHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, aRate, aBurst); \
119 test_Equal(0, __KHEAP_CHECKFAILURE); \
120 test_KErrNone(KHeapDevice.TestBurstDeterministic(aRate, aBurst)); \
121 test_Equal(aBurst * KHeapFailCycles, __KHEAP_CHECKFAILURE)
123 #define TEST_BURST_DETERMINISTIC(aCheckFailure, aRate, aBurst) \
125 test_Equal(0, aCheckFailure); \
126 TUint failures = 0; \
127 for (i = 1; i <= aRate * KHeapFailCycles; i++) \
129 if (i % aRate == 0) \
131 for (TInt j = 0; j < aBurst; j++) \
134 test_Equal(NULL, p); \
135 test_Equal(++failures, aCheckFailure); \
148 Test we fail burst times for EBurstRandom and EBurstTrueRandom.
149 Even though it is random it should always fail within aRate allocations.
150 Defined as a macro so that it is easier to determine which test is failing.
152 @param aRate The limiting rate of each set of failures.
153 @param aBurst The number of allocations that should fail.
156 #define __UHEAP_TEST_BURST_RANDOM(aRate, aBurst) \
157 __UHEAP_SETBURSTFAIL(RHeap::EBurstRandom, aRate, aBurst); \
158 TEST_BURST_RANDOM(aRate, aBurst)
160 #define __RHEAP_TEST_BURST_RANDOM(aHeap, aRate, aBurst) \
161 __RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstRandom, aRate, aBurst); \
162 TEST_BURST_RANDOM(aRate, aBurst)
164 #define __UHEAP_TEST_BURST_TRUERANDOM(aRate, aBurst) \
165 __UHEAP_SETBURSTFAIL(RHeap::EBurstTrueRandom, aRate, aBurst); \
166 TEST_BURST_RANDOM(aRate, aBurst)
168 #define __RHEAP_TEST_BURST_TRUERANDOM(aHeap, aRate, aBurst) \
169 __RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstTrueRandom, aRate, aBurst); \
170 TEST_BURST_RANDOM(aRate, aBurst)
173 #define TEST_BURST_RANDOM(aRate, aBurst) \
175 for (i = 0; i < aRate * KHeapFailCycles; i++) \
179 {/* we've started failing so check that we fail burst times*/ \
181 for (TInt j = 1; j < aBurst; j++) \
184 test_Equal(NULL, p); \
189 test_NotNull(failed);
191 struct SBurstPanicParams
197 TInt TestBurstPanicThread(TAny* aParams)
199 SBurstPanicParams* burstParams = (SBurstPanicParams*) aParams;
200 __UHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, burstParams->iRate, burstParams->iBurst); \
204 #define __UHEAP_TEST_BURST_PANIC(aRate, aBurst) \
207 TRequestStatus status; \
208 SBurstPanicParams threadParams; \
209 threadParams.iRate = aRate; \
210 threadParams.iBurst = aBurst; \
211 test_KErrNone(thread.Create(_L("TestBurstPanicThread"), TestBurstPanicThread, 0x1000, NULL, (TAny*)&threadParams)); \
212 thread.Logon(status); \
214 User::WaitForRequest(status); \
215 test_Equal(EExitPanic, thread.ExitType()); \
216 test_Equal(ETHeapBadDebugFailParameter, status.Int()); \
217 CLOSE_AND_WAIT(thread); \
220 GLDEF_C TInt E32Main(void)
224 test.Start(_L("Test the heap debug failure mechanisms"));
226 // Prepare for __UHEAP tests
230 // Prepare for __RHEAP tests
232 test_KErrNone(HAL::Get(HAL::EMemoryPageSize, pageSize));
235 test_KErrNone(heapChunk.CreateLocal(pageSize<<1, pageSize<<1));
236 RHeap* rHeap = UserHeap::ChunkHeap(NULL, 0, pageSize);
238 __RHEAP_RESET(rHeap);
242 // Prepare for __KHEAP tests by:
243 // Turning off lazy dll unloading
245 test(l.Connect()==KErrNone);
246 test(l.CancelLazyDllUnload()==KErrNone);
249 // Loading the kernel heap test driver
250 test.Next(_L("Load/open d_kheap test driver"));
251 TInt r = User::LoadLogicalDevice(KHeapTestDriverName);
252 test( r==KErrNone || r==KErrAlreadyExists);
253 if( KErrNone != (r=KHeapDevice.Open()) )
255 User::FreeLogicalDevice(KHeapTestDriverName);
256 test.Printf(_L("Could not open LDD"));
262 //=============================================================================
263 test.Next(_L("Test __UHEAP EFailNext"));
281 __UHEAP_FAILNEXT(10);
298 // Test EFailNext with burst macro should default to burst of 1
299 __UHEAP_SETBURSTFAIL(RAllocator::EFailNext, 5, 5);
300 for (i = 0; i < 4; i++)
313 //=============================================================================
314 test.Next(_L("Test __UHEAP BurstFailNext"));
315 __UHEAP_TEST_BURST_FAILNEXT(2, 1);
316 __UHEAP_TEST_BURST_FAILNEXT(10, 12);
317 __UHEAP_TEST_BURST_FAILNEXT(5, 50);
318 __UHEAP_TEST_BURST_FAILNEXT(50, 5);
320 // test using burst with non-burst macro should default to burst=1
321 __UHEAP_SETFAIL(RHeap::EBurstFailNext, 5);
322 for (i = 0; i < 4; i++)
335 test.Next(_L("Test __RHEAP BurstFailNext"));
336 RHeap* origHeap = User::SwitchHeap(rHeap);
338 __RHEAP_TEST_BURST_FAILNEXT(rHeap, 2, 1);
339 __RHEAP_TEST_BURST_FAILNEXT(rHeap, 10, 12);
340 __RHEAP_TEST_BURST_FAILNEXT(rHeap, 5, 50);
341 __RHEAP_TEST_BURST_FAILNEXT(rHeap, 50, 5);
343 User::SwitchHeap(origHeap);
345 test.Next(_L("Test __KHEAP BurstFailNext"));
346 __KHEAP_TEST_BURST_FAILNEXT(1, 1);
347 __KHEAP_TEST_BURST_FAILNEXT(10, 12);
348 __KHEAP_TEST_BURST_FAILNEXT(5, 50);
349 __KHEAP_TEST_BURST_FAILNEXT(50, 5);
353 //=============================================================================
354 test.Next(_L("Test __UHEAP EDeterministic"));
355 __UHEAP_SETFAIL(RHeap::EDeterministic, 1);
361 __UHEAP_SETFAIL(RHeap::EDeterministic, 2);
370 __UHEAP_SETFAIL(RHeap::EDeterministic, 11);
371 for (i=1; i<=100; i++)
380 // Test using burst macro for non-burst fail type
381 // The burst value will be ignored.
382 __UHEAP_SETBURSTFAIL(RHeap::EDeterministic, 2, 3);
392 //=============================================================================
393 test.Next(_L("Test __UHEAP EBurstDeterministic"));
394 __UHEAP_TEST_BURST_DETERMINISTIC(1, 1);
395 __UHEAP_TEST_BURST_DETERMINISTIC(2, 1);
396 __UHEAP_TEST_BURST_DETERMINISTIC(11, 2);
397 __UHEAP_TEST_BURST_DETERMINISTIC(2, 3); // Test with burst > rate.
399 // Test setting EBurstDeterministic with non-burst MACRO
400 // it should still work but default to a burst rate of 1
401 __UHEAP_SETFAIL(RHeap::EBurstDeterministic, 2);
411 test.Next(_L("Test __RHEAP EBurstDeterministic"));
412 origHeap = User::SwitchHeap(rHeap);
414 __UHEAP_TEST_BURST_DETERMINISTIC(1, 1);
415 __UHEAP_TEST_BURST_DETERMINISTIC(2, 1);
416 __UHEAP_TEST_BURST_DETERMINISTIC(11, 2);
417 __UHEAP_TEST_BURST_DETERMINISTIC(2, 3); // Test with burst > rate.
419 User::SwitchHeap(origHeap);
422 test.Next(_L("Test __KHEAP EBurstDeterministic"));
423 __KHEAP_TEST_BURST_DETERMINISTIC(1, 1);
424 __KHEAP_TEST_BURST_DETERMINISTIC(2, 1);
425 __KHEAP_TEST_BURST_DETERMINISTIC(11, 2);
426 __KHEAP_TEST_BURST_DETERMINISTIC(2, 3); // Test with burst > rate.
429 //=============================================================================
430 test.Next(_L("Test __UHEAP ERandom"));
431 __UHEAP_SETFAIL(RHeap::ERandom, 1);
432 for (i=1; i<=100; i++)
437 __UHEAP_SETFAIL(RHeap::ERandom, 2);
438 for (i=1; i<=100; i++)
442 test(p==NULL || q==NULL);
446 __UHEAP_SETFAIL(RHeap::ERandom, 10);
451 if (p==NULL) failed++;
458 if (p==NULL) failed++;
463 // Test using the burst macro for ERandom.
464 // Can't really check that it only fails once as being random
465 // it may fail again immediately after a previous failure.
466 __UHEAP_SETBURSTFAIL(RHeap::ERandom, 10, 5);
467 TEST_BURST_RANDOM(10, 1);
469 //=============================================================================
470 test.Next(_L("Test __UHEAP EBurstRandom"));
471 __UHEAP_TEST_BURST_RANDOM(10, 2);
472 __UHEAP_TEST_BURST_RANDOM(15, 5);
473 __UHEAP_TEST_BURST_RANDOM(10, 20);
475 // Test using EBurstRandom with non-burst macro.
476 // Can't really check that it only fails once as being random
477 // it may fail again immediately after a previous failure.
478 __UHEAP_SETFAIL(RHeap::EBurstRandom, 10);
479 __UHEAP_TEST_BURST_RANDOM(10, 1);
482 test.Next(_L("Test __RHEAP EBurstRandom"));
483 origHeap = User::SwitchHeap(rHeap);
485 __RHEAP_TEST_BURST_RANDOM(rHeap, 10, 2);
486 __RHEAP_TEST_BURST_RANDOM(rHeap, 15, 5);
487 __RHEAP_TEST_BURST_RANDOM(rHeap, 10, 20);
489 User::SwitchHeap(origHeap);
491 // No random modes for kernel heap
493 //=============================================================================
494 test.Next(_L("Test __UHEAP ETrueRandom"));
495 __UHEAP_SETFAIL(RHeap::ETrueRandom, 10);
500 if (p==NULL) failed++;
507 if (p==NULL) failed++;
512 // Test using ETrueRandom with burst macro.
513 // Can't really check that it only fails once as being random
514 // it may fail again immediately after a previous failure.
515 __UHEAP_SETBURSTFAIL(RHeap::ETrueRandom, 10, 2);
516 TEST_BURST_RANDOM(10, 1);
518 //=============================================================================
519 test.Next(_L("Test __UHEAP EBurstTrueRandom"));
520 __UHEAP_TEST_BURST_TRUERANDOM(10, 2);
521 __UHEAP_TEST_BURST_TRUERANDOM(15, 5);
522 __UHEAP_TEST_BURST_TRUERANDOM(10, 20);
524 // Test using EBurstRandom with non-burst macro.
525 // Can't really check that it only fails once as being random
526 // it may fail again immediately after a previous failure.
527 __UHEAP_SETFAIL(RHeap::EBurstTrueRandom, 10);
528 TEST_BURST_RANDOM(10, 1);
530 test.Next(_L("Test __RHEAP EBurstTrueRandom"));
531 origHeap = User::SwitchHeap(rHeap);
533 __RHEAP_TEST_BURST_TRUERANDOM(rHeap, 10, 2);
534 __RHEAP_TEST_BURST_TRUERANDOM(rHeap, 15, 5);
535 __RHEAP_TEST_BURST_TRUERANDOM(rHeap, 10, 20);
537 User::SwitchHeap(origHeap);
539 // No random modes for kernel heap
541 //=============================================================================
542 test.Next(_L("Test __UHEAP ENone"));
543 __UHEAP_SETFAIL(RHeap::ENone, 0);
544 for (i=0; i<100; i++)
551 // Test using ENone with burst macro.
552 __UHEAP_SETBURSTFAIL(RHeap::ENone, 0, 0);
553 for (i=0; i<100; i++)
560 //=============================================================================
561 test.Next(_L("Test __UHEAP Reset"));
562 __UHEAP_SETFAIL(RHeap::EDeterministic, 1);
574 // Test using EReset with non-burst macro.
575 __UHEAP_SETFAIL(RHeap::EDeterministic, 1);
581 __UHEAP_SETFAIL(RHeap::EReset, 1);
586 // Test using EReset with burst macro.
587 __UHEAP_SETFAIL(RHeap::EDeterministic, 1);
593 __UHEAP_SETBURSTFAIL(RHeap::EReset, 1, 1);
598 //=============================================================================
599 test.Next(_L("Test ETHeapBadDebugFailParameter panics"));
600 __UHEAP_TEST_BURST_PANIC(50, KMaxTUint16 + 1);
601 __UHEAP_TEST_BURST_PANIC(KMaxTUint16 + 1, 2);
602 __UHEAP_TEST_BURST_PANIC(-50, 3);
604 // Test maximum aRate and aBurst values don't panic.
605 __UHEAP_TEST_BURST_FAILNEXT(2, KMaxTUint16); // Use failnext as quicker
606 __UHEAP_TEST_BURST_FAILNEXT(KMaxTUint16, 2);
608 //=============================================================================
609 test.Next(_L("Test __UHEAP User::ReAlloc without cell moving"));
610 TAny* a = User::Alloc(256);
613 TAny* a2 = User::ReAlloc(a,192);
615 a2 = User::ReAlloc(a,128);
617 a2 = User::ReAlloc(a,256);
619 a2 = User::ReAlloc(a,256);
623 //=============================================================================
625 __RHEAP_MARKEND(rHeap);
629 // Ensure all kernel heap debug failures are not active for future tests etc.
632 User::FreeLogicalDevice(KHeapTestDriverName);
640 GLDEF_C TInt E32Main()
642 // __KHEAP_SETFAIL etc. not available in release mode, so don't test
647 test.Start(_L("No tests in release mode"));