os/kernelhwsrv/kerneltest/e32test/system/t_prot2.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-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\system\t_prot2.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32test.h>
sl@0
    19
#include "u32std.h"
sl@0
    20
sl@0
    21
const TInt KHeapSize=0x1000;
sl@0
    22
sl@0
    23
_LIT(KSecondProcessName,"T_PROT2A");
sl@0
    24
_LIT(KSecondProcessDataChunkName,"T_PROT2A*$DAT");
sl@0
    25
sl@0
    26
RTest test(_L("T_PROT2"));
sl@0
    27
sl@0
    28
LOCAL_C TInt WatcherThread(TAny* aSemaphoreHandle)
sl@0
    29
	{
sl@0
    30
	RTest wtest(_L("Watcher Thread"));
sl@0
    31
	wtest.Title();
sl@0
    32
	RThread().SetPriority(EPriorityMuchMore);
sl@0
    33
	wtest.Start(_L("Create undertaker"));
sl@0
    34
	RUndertaker u;
sl@0
    35
	TInt r=u.Create();
sl@0
    36
	if (r!=KErrNone)
sl@0
    37
		{
sl@0
    38
		RProcess me;
sl@0
    39
		me.Panic(_L("UNDERTAKER"),r);
sl@0
    40
		}
sl@0
    41
	RSemaphore sem;
sl@0
    42
	sem.SetHandle((TInt)aSemaphoreHandle);
sl@0
    43
	sem.Signal();
sl@0
    44
	FOREVER
sl@0
    45
		{
sl@0
    46
		TRequestStatus s;
sl@0
    47
		TInt h;
sl@0
    48
		u.Logon(s,h);
sl@0
    49
		User::WaitForRequest(s);
sl@0
    50
		RThread t;
sl@0
    51
		t.SetHandle(h);
sl@0
    52
		const TDesC& fn=t.FullName();
sl@0
    53
		wtest.Printf(_L("Thread %S exited\n"),&fn);
sl@0
    54
		const TDesC& cat=t.ExitCategory();
sl@0
    55
		wtest.Printf(_L("Exit type %d,%d,%S\n"),t.ExitType(),t.ExitReason(),&cat);
sl@0
    56
		t.Close();
sl@0
    57
		}
sl@0
    58
	}
sl@0
    59
sl@0
    60
LOCAL_C void StartWatcherThread()
sl@0
    61
	{
sl@0
    62
	test.Next(_L("Start watcher thread"));
sl@0
    63
	RSemaphore s;
sl@0
    64
	TInt r=s.CreateLocal(0);
sl@0
    65
	test(r==KErrNone);
sl@0
    66
	RThread t;
sl@0
    67
	r=t.Create(_L("WatcherThread"),WatcherThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)s.Handle());
sl@0
    68
	test(r==KErrNone);
sl@0
    69
	t.Resume();
sl@0
    70
	s.Wait();
sl@0
    71
	s.Close();
sl@0
    72
	}
sl@0
    73
sl@0
    74
void ExceptionHandler(TExcType)
sl@0
    75
	{
sl@0
    76
	User::Leave(KErrAbort);
sl@0
    77
	}
sl@0
    78
sl@0
    79
LOCAL_C TInt RogueThread1(TAny*)
sl@0
    80
	{
sl@0
    81
	TInt n;
sl@0
    82
	TInt m=0;
sl@0
    83
	TUint* p=NULL;
sl@0
    84
	RChunk c;
sl@0
    85
	User::SetExceptionHandler(ExceptionHandler,KExceptionFault);
sl@0
    86
	for (n=0; n<100; n++)
sl@0
    87
		{
sl@0
    88
		User::AfterHighRes(1000);	// wait 1ms
sl@0
    89
		if (m==0 && !p)
sl@0
    90
			{
sl@0
    91
			TFindChunk fc(KSecondProcessDataChunkName);
sl@0
    92
			TFullName fn;
sl@0
    93
			TInt r=fc.Next(fn);
sl@0
    94
			if (r!=KErrNone)
sl@0
    95
				continue;
sl@0
    96
			r=c.Open(fc);
sl@0
    97
			if (r!=KErrNone)
sl@0
    98
				continue;
sl@0
    99
			p=(TUint*)c.Base();	// second process is fixed
sl@0
   100
			continue;
sl@0
   101
			}
sl@0
   102
		if (m==0 && p)
sl@0
   103
			{
sl@0
   104
			if (c.Size()==0)
sl@0
   105
				continue;
sl@0
   106
			m++;
sl@0
   107
			c.Close();
sl@0
   108
			}
sl@0
   109
		TRAPD(r,Mem::Fill(p,0x1000,0xc9));
sl@0
   110
		if (r==KErrNone)
sl@0
   111
			return KErrNone;
sl@0
   112
		}
sl@0
   113
	User::Panic(_L("NOTFOUND"),m);
sl@0
   114
	return KErrNone;
sl@0
   115
	}
sl@0
   116
sl@0
   117
LOCAL_C void TestLoaderProtection()
sl@0
   118
	{
sl@0
   119
	test.Next(_L("Create rogue thread"));
sl@0
   120
	RThread t;
sl@0
   121
	TInt r=t.Create(_L("RogueThread1"),RogueThread1,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   122
	test(r==KErrNone);
sl@0
   123
	t.SetPriority(EPriorityRealTime);	// this should be above the loader
sl@0
   124
	TRequestStatus s;
sl@0
   125
	t.Logon(s);
sl@0
   126
	test(s==KRequestPending);
sl@0
   127
	test.Next(_L("Resume rogue thread"));
sl@0
   128
	t.Resume();
sl@0
   129
sl@0
   130
	test.Next(_L("Create second process"));
sl@0
   131
	RProcess p;
sl@0
   132
	r=p.Create(KSecondProcessName,KNullDesC);
sl@0
   133
	test(r==KErrNone);
sl@0
   134
	TRequestStatus s2;
sl@0
   135
	p.Logon(s2);
sl@0
   136
	test(s2==KRequestPending);
sl@0
   137
	test.Next(_L("Resume second process"));
sl@0
   138
	p.Resume();
sl@0
   139
	
sl@0
   140
	test.Next(_L("Wait for second process to exit"));
sl@0
   141
	User::WaitForRequest(s2);
sl@0
   142
	const TDesC& pcat=p.ExitCategory();
sl@0
   143
	test.Printf(_L("Exit type %d,%d,%S\n"),p.ExitType(),p.ExitReason(),&pcat);
sl@0
   144
	test.Getch();
sl@0
   145
sl@0
   146
	test.Next(_L("Wait for rogue thread to exit"));
sl@0
   147
	User::WaitForRequest(s);
sl@0
   148
	const TDesC& tcat=t.ExitCategory();
sl@0
   149
	test.Printf(_L("Exit type %d,%d,%S\n"),t.ExitType(),t.ExitReason(),&tcat);
sl@0
   150
	test.Getch();
sl@0
   151
	}
sl@0
   152
sl@0
   153
GLDEF_C TInt E32Main()
sl@0
   154
	{
sl@0
   155
	test.Title();
sl@0
   156
	test.Start(_L("Testing protection against errant user threads"));
sl@0
   157
	RProcess().SetPriority(EPriorityHigh);
sl@0
   158
sl@0
   159
	StartWatcherThread();
sl@0
   160
	TestLoaderProtection();
sl@0
   161
sl@0
   162
	test.End();
sl@0
   163
	return KErrNone;
sl@0
   164
	}