sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\system\t_prot2.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include "u32std.h" sl@0: sl@0: const TInt KHeapSize=0x1000; sl@0: sl@0: _LIT(KSecondProcessName,"T_PROT2A"); sl@0: _LIT(KSecondProcessDataChunkName,"T_PROT2A*$DAT"); sl@0: sl@0: RTest test(_L("T_PROT2")); sl@0: sl@0: LOCAL_C TInt WatcherThread(TAny* aSemaphoreHandle) sl@0: { sl@0: RTest wtest(_L("Watcher Thread")); sl@0: wtest.Title(); sl@0: RThread().SetPriority(EPriorityMuchMore); sl@0: wtest.Start(_L("Create undertaker")); sl@0: RUndertaker u; sl@0: TInt r=u.Create(); sl@0: if (r!=KErrNone) sl@0: { sl@0: RProcess me; sl@0: me.Panic(_L("UNDERTAKER"),r); sl@0: } sl@0: RSemaphore sem; sl@0: sem.SetHandle((TInt)aSemaphoreHandle); sl@0: sem.Signal(); sl@0: FOREVER sl@0: { sl@0: TRequestStatus s; sl@0: TInt h; sl@0: u.Logon(s,h); sl@0: User::WaitForRequest(s); sl@0: RThread t; sl@0: t.SetHandle(h); sl@0: const TDesC& fn=t.FullName(); sl@0: wtest.Printf(_L("Thread %S exited\n"),&fn); sl@0: const TDesC& cat=t.ExitCategory(); sl@0: wtest.Printf(_L("Exit type %d,%d,%S\n"),t.ExitType(),t.ExitReason(),&cat); sl@0: t.Close(); sl@0: } sl@0: } sl@0: sl@0: LOCAL_C void StartWatcherThread() sl@0: { sl@0: test.Next(_L("Start watcher thread")); sl@0: RSemaphore s; sl@0: TInt r=s.CreateLocal(0); sl@0: test(r==KErrNone); sl@0: RThread t; sl@0: r=t.Create(_L("WatcherThread"),WatcherThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)s.Handle()); sl@0: test(r==KErrNone); sl@0: t.Resume(); sl@0: s.Wait(); sl@0: s.Close(); sl@0: } sl@0: sl@0: void ExceptionHandler(TExcType) sl@0: { sl@0: User::Leave(KErrAbort); sl@0: } sl@0: sl@0: LOCAL_C TInt RogueThread1(TAny*) sl@0: { sl@0: TInt n; sl@0: TInt m=0; sl@0: TUint* p=NULL; sl@0: RChunk c; sl@0: User::SetExceptionHandler(ExceptionHandler,KExceptionFault); sl@0: for (n=0; n<100; n++) sl@0: { sl@0: User::AfterHighRes(1000); // wait 1ms sl@0: if (m==0 && !p) sl@0: { sl@0: TFindChunk fc(KSecondProcessDataChunkName); sl@0: TFullName fn; sl@0: TInt r=fc.Next(fn); sl@0: if (r!=KErrNone) sl@0: continue; sl@0: r=c.Open(fc); sl@0: if (r!=KErrNone) sl@0: continue; sl@0: p=(TUint*)c.Base(); // second process is fixed sl@0: continue; sl@0: } sl@0: if (m==0 && p) sl@0: { sl@0: if (c.Size()==0) sl@0: continue; sl@0: m++; sl@0: c.Close(); sl@0: } sl@0: TRAPD(r,Mem::Fill(p,0x1000,0xc9)); sl@0: if (r==KErrNone) sl@0: return KErrNone; sl@0: } sl@0: User::Panic(_L("NOTFOUND"),m); sl@0: return KErrNone; sl@0: } sl@0: sl@0: LOCAL_C void TestLoaderProtection() sl@0: { sl@0: test.Next(_L("Create rogue thread")); sl@0: RThread t; sl@0: TInt r=t.Create(_L("RogueThread1"),RogueThread1,KDefaultStackSize,KHeapSize,KHeapSize,NULL); sl@0: test(r==KErrNone); sl@0: t.SetPriority(EPriorityRealTime); // this should be above the loader sl@0: TRequestStatus s; sl@0: t.Logon(s); sl@0: test(s==KRequestPending); sl@0: test.Next(_L("Resume rogue thread")); sl@0: t.Resume(); sl@0: sl@0: test.Next(_L("Create second process")); sl@0: RProcess p; sl@0: r=p.Create(KSecondProcessName,KNullDesC); sl@0: test(r==KErrNone); sl@0: TRequestStatus s2; sl@0: p.Logon(s2); sl@0: test(s2==KRequestPending); sl@0: test.Next(_L("Resume second process")); sl@0: p.Resume(); sl@0: sl@0: test.Next(_L("Wait for second process to exit")); sl@0: User::WaitForRequest(s2); sl@0: const TDesC& pcat=p.ExitCategory(); sl@0: test.Printf(_L("Exit type %d,%d,%S\n"),p.ExitType(),p.ExitReason(),&pcat); sl@0: test.Getch(); sl@0: sl@0: test.Next(_L("Wait for rogue thread to exit")); sl@0: User::WaitForRequest(s); sl@0: const TDesC& tcat=t.ExitCategory(); sl@0: test.Printf(_L("Exit type %d,%d,%S\n"),t.ExitType(),t.ExitReason(),&tcat); sl@0: test.Getch(); sl@0: } sl@0: sl@0: GLDEF_C TInt E32Main() sl@0: { sl@0: test.Title(); sl@0: test.Start(_L("Testing protection against errant user threads")); sl@0: RProcess().SetPriority(EPriorityHigh); sl@0: sl@0: StartWatcherThread(); sl@0: TestLoaderProtection(); sl@0: sl@0: test.End(); sl@0: return KErrNone; sl@0: }