Update contrib.
1 // Copyright (c) 2002-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\emul\t_emul.cpp
16 // Test the mechanism to escape threads from the emulator in order
17 // to block on Windows objects
18 // Test launching processes doesn't leak windows TLS indices
22 // - Test the mechanism to escape threads from the emulator in order
23 // to block on Windows objects:
24 // - test escape and re-enter mechanism.
25 // - block on Windows in EPOC thread and escaped EPOC thread.
26 // Platforms/Drives/Compatibility:
28 // Assumptions/Requirement/Pre-requisites:
29 // Failures and causes:
30 // Base Port information:
34 #define __E32TEST_EXTENSION__
36 #include <e32atomics.h>
38 #include <e32std_private.h>
40 #include <e32ldr_private.h>
45 #define WIN32_LEAN_AND_MEAN
46 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
48 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
50 LOCAL_D RTest test(_L("t_emul"));
53 static HANDLE ESemaphore;
56 TInt EscapeTimeoutThread(TAny*)
59 if (__e32_atomic_add_ord32(&ELock, 1) == 0)
62 ReleaseSemaphore(ESemaphore,1,NULL);
67 TInt EscapeSignalThread(TAny*)
69 if (__e32_atomic_add_ord32(&ELock, 1) == 0)
72 ReleaseSemaphore(ESemaphore,1,NULL);
77 TInt DoTestEscape(TBool aDoEscape)
79 // Test the mechanism to escape threads from the emulator in order to block on Windows objects
83 EResult = KRequestPending;
84 ESemaphore = CreateSemaphoreA(NULL,0,1,NULL);
85 test (ESemaphore != NULL);
88 TInt r = t1.Create(KNullDesC,&EscapeSignalThread,0x1000,NULL,NULL);
90 t1.SetPriority(EPriorityLess);
93 r = t2.Create(KNullDesC,&EscapeTimeoutThread,0x1000,NULL,NULL);
95 t2.SetPriority(EPriorityMore);
101 r = WaitForSingleObject(ESemaphore,INFINITE);
104 test (r==WAIT_OBJECT_0);
111 User::WaitForRequest(s1);
112 User::WaitForRequest(s2);
114 CloseHandle(ESemaphore);
120 // Test the mechanism to escape threads from the emulator in order to block on Windows objects
123 test.Start(_L("Test escape and reenter mechanism"));
124 for (TInt i = 0;i<10000;++i)
131 test.Next(_L("Block on Windows in EPOC thread"));
132 TInt r = DoTestEscape(EFalse);
133 test (r == KErrTimedOut);
134 test.Next(_L("Block on Windows in escaped EPOC thread"));
135 r = DoTestEscape(ETrue);
136 test (r == KErrNone);
140 TInt CountRemainingTlsIndicies()
142 const TInt KMax = 2000;
144 TBool allocated[KMax];
145 memclr(allocated, sizeof(TBool) * KMax);
147 for (i = 0 ; i < KMax ; ++i)
149 TInt index = TlsAlloc();
150 if (index == TLS_OUT_OF_INDEXES)
152 test(index >= 0 && index < KMax);
153 allocated[index] = ETrue;
155 for (TInt j = 0 ; j < KMax ; ++j)
163 void RunSlave(TSlaveAction aAction)
167 arg.Format(_L("%d"), aAction);
168 test_KErrNone(p.Create(KTEmulSlaveName, arg));
170 TRequestStatus status;
172 User::WaitForRequest(status);
173 test_KErrNone(status.Int());
174 test_Equal(EExitKill, p.ExitType());
178 void TestRuntimeCleanup()
180 test.Start(_L("Test Codewarrior runtime library is correctly cleaned up"));
181 TInt initIndicies = CountRemainingTlsIndicies();
183 test.Next(_L("Test creating a process doesn't leak windows TLS indicies"));
184 RunSlave(ESlaveDoNothing);
185 test_Equal(initIndicies, CountRemainingTlsIndicies());
187 test.Next(_L("Test leaving in an exe doesn't leak windows TLS indicies"));
188 RunSlave(ESlaveTrapExceptionInExe);
189 test_Equal(initIndicies, CountRemainingTlsIndicies());
191 test.Next(_L("Test leaving in a linked DLL doesn't leak windows TLS indicies"));
192 RunSlave(ESlaveTrapExceptionInLinkedDll);
193 test_Equal(initIndicies, CountRemainingTlsIndicies());
195 test.Next(_L("Test leaving in a loaded DLL doesn't leak windows TLS indicies"));
196 RunSlave(ESlaveTrapExceptionInLoadedDll);
197 test_Equal(initIndicies, CountRemainingTlsIndicies());
199 test.Next(_L("Test cleanup doesn't happen while DLL still loaded"));
201 test_KErrNone(l.Load(KTEmulDll2Name));
202 RunSlave(ESlaveTrapExceptionInLoadedDll);
203 TInt midCount = CountRemainingTlsIndicies();
204 test(initIndicies > midCount);
206 test.Next(_L("Test previous detach doesn't cause runtime to be re-initalised"));
207 TTrapExceptionInDllFunc func =
208 (TTrapExceptionInDllFunc)l.Lookup(KTrapExceptionInDllOrdinal);
211 test_Equal(midCount, CountRemainingTlsIndicies());
213 test_Equal(initIndicies, CountRemainingTlsIndicies());
221 test.Printf(_L("@\n"));
231 //DoSomething2L() must only be called once , else we know that trap mechanism didn't work
235 User::Leave(KErrNotFound);
238 TRAP_IGNORE(DoSomething2L());
239 TRAPD(errr, DoSomething2L());
241 TRAP(r, DoSomething2L());
247 void LeaveIfArgIsTwo(TInt aCall);
249 class CFred : public CBase
252 static CFred* NewLC();
259 // Push something on the cleanup stack so we can see whyen it gets cleaned up.
260 CFred *fred = CFred::NewLC();
263 TInt betweenFuncAndTRAPD=0;
265 for(TInt loop=1; loop<=2; ++loop)
268 test.Printf(_L("Before LeaveIfArgIsTwo()\n"));
270 // The first time around the loop, this function call works.
271 // The second time, it leaves KErrGeneral
272 // Then when TRAP mechanism isn't working properly the emulator (correctly)
273 // would delete fred, and (incorrectly) jump to the line
274 // just after the TRAPD call a few lines further down the file!
275 LeaveIfArgIsTwo(loop);
277 ++betweenFuncAndTRAPD;
278 test.Printf(_L("Between LeaveIfArgIsTwo() and TRAPD\n"));
280 TRAPD(err, test.Printf(_L("Inside TRAPD\n") ) );
283 // It should only be possible to reach this section of code by executing all the lines
284 // between LeaveIfArgIsTwo and here.
289 // Should NEVER get here because LeaveIfArgIsTwo did a leave the second time around the loop
290 test.Printf(_L("After loop (should NEVER get here) -\n\
292 betweenFuncAndTRAPD %d\n\
294 beforeFunc, betweenFuncAndTRAPD, afterTRAPD);
296 test.Printf(_L("It should be impossible for afterTRAPD to be larger than betweenFuncAndTRAPD\n"));
297 test(afterTRAPD <= betweenFuncAndTRAPD);
299 // Cleanup our cleanup stack.
300 CleanupStack::PopAndDestroy(&fred);
303 void LeaveIfArgIsTwo(TInt aCall)
306 test.Printf(_L("aCall %d\n"), aCall);
309 User::Leave(KErrGeneral);
313 CFred *CFred::NewLC()
315 CFred *self = new(ELeave) CFred;
316 CleanupStack::PushL(self);
322 test.Printf(_L("CFred %x\n"), this);
327 test.Printf(_L("~CFred %x\n"), this);
332 GLDEF_C TInt E32Main()
338 test.Start(_L("Starting tests ..."));
340 // Turn off evil lazy dll unloading
342 test(l.Connect()==KErrNone);
343 test(l.CancelLazyDllUnload()==KErrNone);
348 TestRuntimeCleanup();
351 // The following tests were added to test that the TRAP mechanism works correctly in case of
352 // nested TRAP's. A compiler bug (winscw) caused the following tests to fail, since the wrong
353 // trap handler would be invoked, when User::Leave() was called.
355 test.Next(_L("Check User::Leave is handled by the correct TRAP handler when nested TRAPs are present - Simple scenario\n"));
357 TRAPD(err, DoSomething1L());
358 test(err == KErrNotFound);
360 test.Next(_L("Check User::Leave is handled by the correct TRAP handler when nested TRAPs are present - Cleanup stack scenario\n"));
362 CTrapCleanup* tc = CTrapCleanup::New();
363 if (tc == 0) return KErrNoMemory;
365 TRAPD(err2, DoSomething3L());
366 test(err2==KErrGeneral);