sl@0
|
1 |
// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32test\emul\t_emul.cpp
|
sl@0
|
15 |
// Overview:
|
sl@0
|
16 |
// Test the mechanism to escape threads from the emulator in order
|
sl@0
|
17 |
// to block on Windows objects
|
sl@0
|
18 |
// Test launching processes doesn't leak windows TLS indices
|
sl@0
|
19 |
// API Information:
|
sl@0
|
20 |
// Emulator
|
sl@0
|
21 |
// Details:
|
sl@0
|
22 |
// - Test the mechanism to escape threads from the emulator in order
|
sl@0
|
23 |
// to block on Windows objects:
|
sl@0
|
24 |
// - test escape and re-enter mechanism.
|
sl@0
|
25 |
// - block on Windows in EPOC thread and escaped EPOC thread.
|
sl@0
|
26 |
// Platforms/Drives/Compatibility:
|
sl@0
|
27 |
// All.
|
sl@0
|
28 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
29 |
// Failures and causes:
|
sl@0
|
30 |
// Base Port information:
|
sl@0
|
31 |
//
|
sl@0
|
32 |
//
|
sl@0
|
33 |
|
sl@0
|
34 |
#define __E32TEST_EXTENSION__
|
sl@0
|
35 |
#include <f32file.h>
|
sl@0
|
36 |
#include <e32atomics.h>
|
sl@0
|
37 |
#include <e32std.h>
|
sl@0
|
38 |
#include <e32std_private.h>
|
sl@0
|
39 |
#include <e32ldr.h>
|
sl@0
|
40 |
#include <e32ldr_private.h>
|
sl@0
|
41 |
#include "e32test.h"
|
sl@0
|
42 |
#include "emulator.h"
|
sl@0
|
43 |
#include "t_emul.h"
|
sl@0
|
44 |
|
sl@0
|
45 |
#define WIN32_LEAN_AND_MEAN
|
sl@0
|
46 |
#pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
|
sl@0
|
47 |
#include <windows.h>
|
sl@0
|
48 |
#pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
|
sl@0
|
49 |
|
sl@0
|
50 |
LOCAL_D RTest test(_L("t_emul"));
|
sl@0
|
51 |
|
sl@0
|
52 |
static TInt ELock;
|
sl@0
|
53 |
static HANDLE ESemaphore;
|
sl@0
|
54 |
static TInt EResult;
|
sl@0
|
55 |
|
sl@0
|
56 |
TInt EscapeTimeoutThread(TAny*)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
User::After(1000000);
|
sl@0
|
59 |
if (__e32_atomic_add_ord32(&ELock, 1) == 0)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
EResult=KErrTimedOut;
|
sl@0
|
62 |
ReleaseSemaphore(ESemaphore,1,NULL);
|
sl@0
|
63 |
}
|
sl@0
|
64 |
return KErrNone;
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
TInt EscapeSignalThread(TAny*)
|
sl@0
|
68 |
{
|
sl@0
|
69 |
if (__e32_atomic_add_ord32(&ELock, 1) == 0)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
EResult=KErrNone;
|
sl@0
|
72 |
ReleaseSemaphore(ESemaphore,1,NULL);
|
sl@0
|
73 |
}
|
sl@0
|
74 |
return KErrNone;
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
TInt DoTestEscape(TBool aDoEscape)
|
sl@0
|
78 |
//
|
sl@0
|
79 |
// Test the mechanism to escape threads from the emulator in order to block on Windows objects
|
sl@0
|
80 |
//
|
sl@0
|
81 |
{
|
sl@0
|
82 |
ELock = 0;
|
sl@0
|
83 |
EResult = KRequestPending;
|
sl@0
|
84 |
ESemaphore = CreateSemaphoreA(NULL,0,1,NULL);
|
sl@0
|
85 |
test (ESemaphore != NULL);
|
sl@0
|
86 |
RThread t1,t2;
|
sl@0
|
87 |
TRequestStatus s1,s2;
|
sl@0
|
88 |
TInt r = t1.Create(KNullDesC,&EscapeSignalThread,0x1000,NULL,NULL);
|
sl@0
|
89 |
test (r == KErrNone);
|
sl@0
|
90 |
t1.SetPriority(EPriorityLess);
|
sl@0
|
91 |
t1.Logon(s1);
|
sl@0
|
92 |
t1.Resume();
|
sl@0
|
93 |
r = t2.Create(KNullDesC,&EscapeTimeoutThread,0x1000,NULL,NULL);
|
sl@0
|
94 |
test (r == KErrNone);
|
sl@0
|
95 |
t2.SetPriority(EPriorityMore);
|
sl@0
|
96 |
t2.Logon(s2);
|
sl@0
|
97 |
t2.Resume();
|
sl@0
|
98 |
//
|
sl@0
|
99 |
if (aDoEscape)
|
sl@0
|
100 |
Emulator::Escape();
|
sl@0
|
101 |
r = WaitForSingleObject(ESemaphore,INFINITE);
|
sl@0
|
102 |
if (aDoEscape)
|
sl@0
|
103 |
Emulator::Reenter();
|
sl@0
|
104 |
test (r==WAIT_OBJECT_0);
|
sl@0
|
105 |
//
|
sl@0
|
106 |
r = EResult;
|
sl@0
|
107 |
t1.Kill(0);
|
sl@0
|
108 |
t2.Kill(0);
|
sl@0
|
109 |
t1.Close();
|
sl@0
|
110 |
t2.Close();
|
sl@0
|
111 |
User::WaitForRequest(s1);
|
sl@0
|
112 |
User::WaitForRequest(s2);
|
sl@0
|
113 |
//
|
sl@0
|
114 |
CloseHandle(ESemaphore);
|
sl@0
|
115 |
return r;
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
void TestEscape()
|
sl@0
|
119 |
//
|
sl@0
|
120 |
// Test the mechanism to escape threads from the emulator in order to block on Windows objects
|
sl@0
|
121 |
//
|
sl@0
|
122 |
{
|
sl@0
|
123 |
test.Start(_L("Test escape and reenter mechanism"));
|
sl@0
|
124 |
for (TInt i = 0;i<10000;++i)
|
sl@0
|
125 |
{
|
sl@0
|
126 |
Emulator::Escape();
|
sl@0
|
127 |
if (i%100 == 0)
|
sl@0
|
128 |
Sleep(10);
|
sl@0
|
129 |
Emulator::Reenter();
|
sl@0
|
130 |
}
|
sl@0
|
131 |
test.Next(_L("Block on Windows in EPOC thread"));
|
sl@0
|
132 |
TInt r = DoTestEscape(EFalse);
|
sl@0
|
133 |
test (r == KErrTimedOut);
|
sl@0
|
134 |
test.Next(_L("Block on Windows in escaped EPOC thread"));
|
sl@0
|
135 |
r = DoTestEscape(ETrue);
|
sl@0
|
136 |
test (r == KErrNone);
|
sl@0
|
137 |
test.End();
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
TInt CountRemainingTlsIndicies()
|
sl@0
|
141 |
{
|
sl@0
|
142 |
const TInt KMax = 2000;
|
sl@0
|
143 |
|
sl@0
|
144 |
TBool allocated[KMax];
|
sl@0
|
145 |
memclr(allocated, sizeof(TBool) * KMax);
|
sl@0
|
146 |
TInt i;
|
sl@0
|
147 |
for (i = 0 ; i < KMax ; ++i)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
TInt index = TlsAlloc();
|
sl@0
|
150 |
if (index == TLS_OUT_OF_INDEXES)
|
sl@0
|
151 |
break;
|
sl@0
|
152 |
test(index >= 0 && index < KMax);
|
sl@0
|
153 |
allocated[index] = ETrue;
|
sl@0
|
154 |
}
|
sl@0
|
155 |
for (TInt j = 0 ; j < KMax ; ++j)
|
sl@0
|
156 |
{
|
sl@0
|
157 |
if (allocated[j])
|
sl@0
|
158 |
test(TlsFree(j));
|
sl@0
|
159 |
}
|
sl@0
|
160 |
return i;
|
sl@0
|
161 |
}
|
sl@0
|
162 |
|
sl@0
|
163 |
void RunSlave(TSlaveAction aAction)
|
sl@0
|
164 |
{
|
sl@0
|
165 |
RProcess p;
|
sl@0
|
166 |
TBuf<8> arg;
|
sl@0
|
167 |
arg.Format(_L("%d"), aAction);
|
sl@0
|
168 |
test_KErrNone(p.Create(KTEmulSlaveName, arg));
|
sl@0
|
169 |
p.Resume();
|
sl@0
|
170 |
TRequestStatus status;
|
sl@0
|
171 |
p.Logon(status);
|
sl@0
|
172 |
User::WaitForRequest(status);
|
sl@0
|
173 |
test_KErrNone(status.Int());
|
sl@0
|
174 |
test_Equal(EExitKill, p.ExitType());
|
sl@0
|
175 |
p.Close();
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
void TestRuntimeCleanup()
|
sl@0
|
179 |
{
|
sl@0
|
180 |
test.Start(_L("Test Codewarrior runtime library is correctly cleaned up"));
|
sl@0
|
181 |
TInt initIndicies = CountRemainingTlsIndicies();
|
sl@0
|
182 |
|
sl@0
|
183 |
test.Next(_L("Test creating a process doesn't leak windows TLS indicies"));
|
sl@0
|
184 |
RunSlave(ESlaveDoNothing);
|
sl@0
|
185 |
test_Equal(initIndicies, CountRemainingTlsIndicies());
|
sl@0
|
186 |
|
sl@0
|
187 |
test.Next(_L("Test leaving in an exe doesn't leak windows TLS indicies"));
|
sl@0
|
188 |
RunSlave(ESlaveTrapExceptionInExe);
|
sl@0
|
189 |
test_Equal(initIndicies, CountRemainingTlsIndicies());
|
sl@0
|
190 |
|
sl@0
|
191 |
test.Next(_L("Test leaving in a linked DLL doesn't leak windows TLS indicies"));
|
sl@0
|
192 |
RunSlave(ESlaveTrapExceptionInLinkedDll);
|
sl@0
|
193 |
test_Equal(initIndicies, CountRemainingTlsIndicies());
|
sl@0
|
194 |
|
sl@0
|
195 |
test.Next(_L("Test leaving in a loaded DLL doesn't leak windows TLS indicies"));
|
sl@0
|
196 |
RunSlave(ESlaveTrapExceptionInLoadedDll);
|
sl@0
|
197 |
test_Equal(initIndicies, CountRemainingTlsIndicies());
|
sl@0
|
198 |
|
sl@0
|
199 |
test.Next(_L("Test cleanup doesn't happen while DLL still loaded"));
|
sl@0
|
200 |
RLibrary l;
|
sl@0
|
201 |
test_KErrNone(l.Load(KTEmulDll2Name));
|
sl@0
|
202 |
RunSlave(ESlaveTrapExceptionInLoadedDll);
|
sl@0
|
203 |
TInt midCount = CountRemainingTlsIndicies();
|
sl@0
|
204 |
test(initIndicies > midCount);
|
sl@0
|
205 |
|
sl@0
|
206 |
test.Next(_L("Test previous detach doesn't cause runtime to be re-initalised"));
|
sl@0
|
207 |
TTrapExceptionInDllFunc func =
|
sl@0
|
208 |
(TTrapExceptionInDllFunc)l.Lookup(KTrapExceptionInDllOrdinal);
|
sl@0
|
209 |
test_NotNull(func);
|
sl@0
|
210 |
func();
|
sl@0
|
211 |
test_Equal(midCount, CountRemainingTlsIndicies());
|
sl@0
|
212 |
l.Close();
|
sl@0
|
213 |
test_Equal(initIndicies, CountRemainingTlsIndicies());
|
sl@0
|
214 |
|
sl@0
|
215 |
test.End();
|
sl@0
|
216 |
}
|
sl@0
|
217 |
|
sl@0
|
218 |
|
sl@0
|
219 |
void DoSomething2L()
|
sl@0
|
220 |
{
|
sl@0
|
221 |
test.Printf(_L("@\n"));
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
|
sl@0
|
225 |
void DoSomething1L()
|
sl@0
|
226 |
{
|
sl@0
|
227 |
TInt i = -1, j = 1;
|
sl@0
|
228 |
for ( ; ; )
|
sl@0
|
229 |
{
|
sl@0
|
230 |
i++;
|
sl@0
|
231 |
//DoSomething2L() must only be called once , else we know that trap mechanism didn't work
|
sl@0
|
232 |
test( i<2);
|
sl@0
|
233 |
if (i == j)
|
sl@0
|
234 |
{
|
sl@0
|
235 |
User::Leave(KErrNotFound);
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
TRAP_IGNORE(DoSomething2L());
|
sl@0
|
239 |
TRAPD(errr, DoSomething2L());
|
sl@0
|
240 |
TInt r;
|
sl@0
|
241 |
TRAP(r, DoSomething2L());
|
sl@0
|
242 |
|
sl@0
|
243 |
}
|
sl@0
|
244 |
}
|
sl@0
|
245 |
|
sl@0
|
246 |
|
sl@0
|
247 |
void LeaveIfArgIsTwo(TInt aCall);
|
sl@0
|
248 |
|
sl@0
|
249 |
class CFred : public CBase
|
sl@0
|
250 |
{
|
sl@0
|
251 |
public:
|
sl@0
|
252 |
static CFred* NewLC();
|
sl@0
|
253 |
CFred();
|
sl@0
|
254 |
~CFred();
|
sl@0
|
255 |
};
|
sl@0
|
256 |
|
sl@0
|
257 |
void DoSomething3L()
|
sl@0
|
258 |
{
|
sl@0
|
259 |
// Push something on the cleanup stack so we can see whyen it gets cleaned up.
|
sl@0
|
260 |
CFred *fred = CFred::NewLC();
|
sl@0
|
261 |
|
sl@0
|
262 |
TInt beforeFunc=0;
|
sl@0
|
263 |
TInt betweenFuncAndTRAPD=0;
|
sl@0
|
264 |
TInt afterTRAPD=0;
|
sl@0
|
265 |
for(TInt loop=1; loop<=2; ++loop)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
++beforeFunc;
|
sl@0
|
268 |
test.Printf(_L("Before LeaveIfArgIsTwo()\n"));
|
sl@0
|
269 |
|
sl@0
|
270 |
// The first time around the loop, this function call works.
|
sl@0
|
271 |
// The second time, it leaves KErrGeneral
|
sl@0
|
272 |
// Then when TRAP mechanism isn't working properly the emulator (correctly)
|
sl@0
|
273 |
// would delete fred, and (incorrectly) jump to the line
|
sl@0
|
274 |
// just after the TRAPD call a few lines further down the file!
|
sl@0
|
275 |
LeaveIfArgIsTwo(loop);
|
sl@0
|
276 |
|
sl@0
|
277 |
++betweenFuncAndTRAPD;
|
sl@0
|
278 |
test.Printf(_L("Between LeaveIfArgIsTwo() and TRAPD\n"));
|
sl@0
|
279 |
|
sl@0
|
280 |
TRAPD(err, test.Printf(_L("Inside TRAPD\n") ) );
|
sl@0
|
281 |
|
sl@0
|
282 |
|
sl@0
|
283 |
// It should only be possible to reach this section of code by executing all the lines
|
sl@0
|
284 |
// between LeaveIfArgIsTwo and here.
|
sl@0
|
285 |
++afterTRAPD;
|
sl@0
|
286 |
|
sl@0
|
287 |
}
|
sl@0
|
288 |
|
sl@0
|
289 |
// Should NEVER get here because LeaveIfArgIsTwo did a leave the second time around the loop
|
sl@0
|
290 |
test.Printf(_L("After loop (should NEVER get here) -\n\
|
sl@0
|
291 |
beforeFunc %d\n\
|
sl@0
|
292 |
betweenFuncAndTRAPD %d\n\
|
sl@0
|
293 |
afterTRAPD %d\n"),
|
sl@0
|
294 |
beforeFunc, betweenFuncAndTRAPD, afterTRAPD);
|
sl@0
|
295 |
|
sl@0
|
296 |
test.Printf(_L("It should be impossible for afterTRAPD to be larger than betweenFuncAndTRAPD\n"));
|
sl@0
|
297 |
test(afterTRAPD <= betweenFuncAndTRAPD);
|
sl@0
|
298 |
|
sl@0
|
299 |
// Cleanup our cleanup stack.
|
sl@0
|
300 |
CleanupStack::PopAndDestroy(&fred);
|
sl@0
|
301 |
}
|
sl@0
|
302 |
|
sl@0
|
303 |
void LeaveIfArgIsTwo(TInt aCall)
|
sl@0
|
304 |
{
|
sl@0
|
305 |
|
sl@0
|
306 |
test.Printf(_L("aCall %d\n"), aCall);
|
sl@0
|
307 |
if(aCall == 2)
|
sl@0
|
308 |
{
|
sl@0
|
309 |
User::Leave(KErrGeneral);
|
sl@0
|
310 |
}
|
sl@0
|
311 |
}
|
sl@0
|
312 |
|
sl@0
|
313 |
CFred *CFred::NewLC()
|
sl@0
|
314 |
{
|
sl@0
|
315 |
CFred *self = new(ELeave) CFred;
|
sl@0
|
316 |
CleanupStack::PushL(self);
|
sl@0
|
317 |
return self;
|
sl@0
|
318 |
}
|
sl@0
|
319 |
|
sl@0
|
320 |
CFred::CFred()
|
sl@0
|
321 |
{
|
sl@0
|
322 |
test.Printf(_L("CFred %x\n"), this);
|
sl@0
|
323 |
}
|
sl@0
|
324 |
|
sl@0
|
325 |
CFred::~CFred()
|
sl@0
|
326 |
{
|
sl@0
|
327 |
test.Printf(_L("~CFred %x\n"), this);
|
sl@0
|
328 |
}
|
sl@0
|
329 |
|
sl@0
|
330 |
|
sl@0
|
331 |
|
sl@0
|
332 |
GLDEF_C TInt E32Main()
|
sl@0
|
333 |
//
|
sl@0
|
334 |
//
|
sl@0
|
335 |
//
|
sl@0
|
336 |
{
|
sl@0
|
337 |
test.Title();
|
sl@0
|
338 |
test.Start(_L("Starting tests ..."));
|
sl@0
|
339 |
|
sl@0
|
340 |
// Turn off evil lazy dll unloading
|
sl@0
|
341 |
RLoader l;
|
sl@0
|
342 |
test(l.Connect()==KErrNone);
|
sl@0
|
343 |
test(l.CancelLazyDllUnload()==KErrNone);
|
sl@0
|
344 |
l.Close();
|
sl@0
|
345 |
|
sl@0
|
346 |
TestEscape();
|
sl@0
|
347 |
#ifdef __CW32__
|
sl@0
|
348 |
TestRuntimeCleanup();
|
sl@0
|
349 |
#endif
|
sl@0
|
350 |
|
sl@0
|
351 |
// The following tests were added to test that the TRAP mechanism works correctly in case of
|
sl@0
|
352 |
// nested TRAP's. A compiler bug (winscw) caused the following tests to fail, since the wrong
|
sl@0
|
353 |
// trap handler would be invoked, when User::Leave() was called.
|
sl@0
|
354 |
|
sl@0
|
355 |
test.Next(_L("Check User::Leave is handled by the correct TRAP handler when nested TRAPs are present - Simple scenario\n"));
|
sl@0
|
356 |
|
sl@0
|
357 |
TRAPD(err, DoSomething1L());
|
sl@0
|
358 |
test(err == KErrNotFound);
|
sl@0
|
359 |
|
sl@0
|
360 |
test.Next(_L("Check User::Leave is handled by the correct TRAP handler when nested TRAPs are present - Cleanup stack scenario\n"));
|
sl@0
|
361 |
__UHEAP_MARK;
|
sl@0
|
362 |
CTrapCleanup* tc = CTrapCleanup::New();
|
sl@0
|
363 |
if (tc == 0) return KErrNoMemory;
|
sl@0
|
364 |
|
sl@0
|
365 |
TRAPD(err2, DoSomething3L());
|
sl@0
|
366 |
test(err2==KErrGeneral);
|
sl@0
|
367 |
|
sl@0
|
368 |
delete tc;
|
sl@0
|
369 |
__UHEAP_MARKEND;
|
sl@0
|
370 |
|
sl@0
|
371 |
|
sl@0
|
372 |
test.End();
|
sl@0
|
373 |
test.Close();
|
sl@0
|
374 |
return KErrNone;
|
sl@0
|
375 |
}
|
sl@0
|
376 |
|