os/kernelhwsrv/kerneltest/e32test/demandpaging/t_svrpinning.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/demandpaging/t_svrpinning.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,737 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\demandpaging\t_svrpinning.cpp
    1.18 +// Overview:
    1.19 +// Test the pinning of RMessage descriptor arguments.
    1.20 +// API Information:
    1.21 +// RMessage2, RMessagePtr2, RSessionBase, CSession2, CServer2
    1.22 +// Details:
    1.23 +// Platforms/Drives/Compatibility:
    1.24 +// All.
    1.25 +// Assumptions/Requirement/Pre-requisites:
    1.26 +// Failures and causes:
    1.27 +// Base Port information:
    1.28 +// 
    1.29 +//
    1.30 +
    1.31 +#define __E32TEST_EXTENSION__
    1.32 +
    1.33 +#include <e32std.h>
    1.34 +#include <e32std_private.h>
    1.35 +#include <e32def.h>
    1.36 +#include <e32def_private.h>
    1.37 +#include <e32test.h>
    1.38 +#include <e32ver.h>
    1.39 +#include <e32panic.h>
    1.40 +#include <dptest.h>
    1.41 +#include <u32hal.h>
    1.42 +#include <hal.h>
    1.43 +
    1.44 +const TInt KHeapMinSize=0x1000;
    1.45 +const TInt KHeapMaxSize=0x1000;
    1.46 +
    1.47 +const TUint KPageSize = 0x1000;
    1.48 +TInt gPageSize;
    1.49 +TUint gPageMask;
    1.50 +TBool gDataPagingSupport = EFalse;
    1.51 +TBool gProcessPaged;
    1.52 +enum TServerPinning
    1.53 +	{
    1.54 +	EServerDefault,
    1.55 +	EServerPinning,
    1.56 +	EServerNotPinning,
    1.57 +	EServerSetPinningTooLate,
    1.58 +	EServerPinningCount,
    1.59 +	};
    1.60 +TInt gServerPinningState;
    1.61 +
    1.62 +class CTestServer : public CServer2
    1.63 +	{
    1.64 +public:
    1.65 +	CTestServer(TInt aPriority);
    1.66 +protected:
    1.67 +	//override the pure virtual functions:
    1.68 +	virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const;
    1.69 +	};
    1.70 +
    1.71 +
    1.72 +class CTestSession : public CSession2
    1.73 +	{
    1.74 +public:
    1.75 +	enum TTestMode
    1.76 +		{
    1.77 +		EStop,
    1.78 +		ETestPinAll,
    1.79 +		ETestPinEven,
    1.80 +		ETestPinOdd,
    1.81 +		ETestPin3,
    1.82 +		ETestPin2,
    1.83 +		ETestPin1,
    1.84 +		ETestPin0,
    1.85 +		ETestPinWritable,
    1.86 +		ETestUnpinWritable,
    1.87 +		ETestPinOOM,
    1.88 +		ETestPinDefault,
    1.89 +		ETestDeadServer,
    1.90 +		};
    1.91 +//Override pure virtual
    1.92 +	IMPORT_C virtual void ServiceL(const RMessage2& aMessage);
    1.93 +private:
    1.94 +	TInt CheckDesPresent(const RMessage2& aMessage, TUint aArgIndex, TBool aExpected, TBool aWrite);
    1.95 +	TInt CheckArgsPresent(const RMessage2& aMessage, TBool arg0Present, TBool arg1Present, TBool arg2Present, TBool arg3Present, TBool aWrite);
    1.96 +	TBool iClientDied;
    1.97 +	};
    1.98 +
    1.99 +
   1.100 +class CMyActiveScheduler : public CActiveScheduler
   1.101 +	{
   1.102 +public:
   1.103 +	virtual void Error(TInt anError) const; //override pure virtual error function
   1.104 +	};
   1.105 +
   1.106 +
   1.107 +class RSession : public RSessionBase
   1.108 +	{
   1.109 +public:
   1.110 +	TInt PublicSendReceive(TInt aFunction, const TIpcArgs &aPtr)
   1.111 +		{
   1.112 +		return (SendReceive(aFunction, aPtr));
   1.113 +		}
   1.114 +	TInt PublicCreateSession(const TDesC& aServer,TInt aMessageSlots)
   1.115 +		{
   1.116 +		return (CreateSession(aServer,User::Version(),aMessageSlots));
   1.117 +		}
   1.118 +	};
   1.119 +
   1.120 +
   1.121 +_LIT(KServerName,"CTestServer");
   1.122 +
   1.123 +TBool UpdateExpected(TBool aExpected)
   1.124 +	{
   1.125 +	if (!gDataPagingSupport												// Data paging is not supported so memory should always be present
   1.126 +		|| gServerPinningState == EServerPinning						// The server is a pinning server.
   1.127 +		/*|| (gServerPinningState == EServerDefault && !gProcessPaged)*/// The process isn't paged and default server policy
   1.128 +		)
   1.129 +		{
   1.130 +		aExpected = ETrue;
   1.131 +		}
   1.132 +	return aExpected;
   1.133 +	}
   1.134 +
   1.135 +
   1.136 +CTestServer::CTestServer(TInt aPriority)
   1.137 +//
   1.138 +// Constructor - sets name
   1.139 +//
   1.140 +	: CServer2(aPriority)
   1.141 +	{}
   1.142 +
   1.143 +CSession2* CTestServer::NewSessionL(const TVersion& aVersion,const RMessage2& /*aMessage*/) const
   1.144 +//
   1.145 +// Virtual fn - checks version supported and creates a CTestSession
   1.146 +//
   1.147 +	{
   1.148 +	TVersion version(KE32MajorVersionNumber,KE32MinorVersionNumber,KE32BuildVersionNumber);
   1.149 +	if (User::QueryVersionSupported(version,aVersion)==EFalse)
   1.150 +		User::Leave(KErrNotSupported);
   1.151 +	CTestSession* newCTestSession = new CTestSession;
   1.152 +	if (newCTestSession==NULL)
   1.153 +		User::Panic(_L("NewSessionL failure"), KErrNoMemory);
   1.154 +	return(newCTestSession);
   1.155 +	}
   1.156 +
   1.157 +RSemaphore gSem;
   1.158 +RSemaphore gSem1;
   1.159 +
   1.160 +TInt CTestSession::CheckDesPresent(const RMessage2& aMessage, TUint aArgIndex, TBool aExpected, TBool aWrite)
   1.161 +	{
   1.162 +	if (aExpected)
   1.163 +		RDebug::Printf("  Checking message argument at %d is present", aArgIndex);
   1.164 +	else
   1.165 +		RDebug::Printf("  Checking message argument at %d is not present", aArgIndex);
   1.166 +
   1.167 +	// Get the length of the descriptor and verify it is as expected.
   1.168 +	TInt length = aMessage.GetDesLength(aArgIndex);
   1.169 +	if (length < KErrNone)
   1.170 +		{
   1.171 +		RDebug::Printf("  Error getting descriptor length %d", length);
   1.172 +		return length;
   1.173 +		}
   1.174 +	if (length < 3)
   1.175 +		{// The incorrect descriptor length.
   1.176 +		RDebug::Printf("  Error - Descriptor length too small %d", length);
   1.177 +		return KErrArgument;
   1.178 +		}
   1.179 +	if (!aWrite)
   1.180 +		{// Now read the descriptor and verify that it is present or not.
   1.181 +		TBuf8<5> des;
   1.182 +		TInt r = aMessage.Read(aArgIndex, des);
   1.183 +		TBool pass;
   1.184 +		if (iClientDied)
   1.185 +			pass = r == KErrDied || r == KErrBadDescriptor;
   1.186 +		else
   1.187 +			pass = r == (aExpected ? KErrNone : KErrBadDescriptor);
   1.188 +		if (!pass)
   1.189 +			{
   1.190 +			RDebug::Printf("  Error reading descriptor data r %d", r);
   1.191 +			return KErrGeneral;
   1.192 +			}
   1.193 +		if (r==KErrNone && (des[0] != 'a' || des[1] != 'r' || des[2] != 'g'))
   1.194 +			{// The incorrect descriptor data has been passed.
   1.195 +			RDebug::Printf("  Error in descriptor data is corrupt r %d", r);
   1.196 +			return KErrArgument;
   1.197 +			}
   1.198 +		}
   1.199 +	else
   1.200 +		{// Now write to the maximum length of the descriptor.
   1.201 +		TInt max = aMessage.GetDesMaxLength(aArgIndex);
   1.202 +		if (max < 0)
   1.203 +			{
   1.204 +			RDebug::Printf("  Error getting descriptor max. length %d", max);
   1.205 +			return length;
   1.206 +			}
   1.207 +		HBufC8* argTmp = HBufC8::New(max);
   1.208 +		TPtr8 argPtr = argTmp->Des();
   1.209 +		argPtr.SetLength(max);
   1.210 +		for (TInt i = 0; i < max; i++)
   1.211 +			argPtr[i] = (TUint8)aArgIndex;
   1.212 +		TInt r = aMessage.Write(aArgIndex, argPtr);
   1.213 +		TBool pass;
   1.214 +		if (iClientDied)
   1.215 +			pass = r == KErrDied || r == KErrBadDescriptor;
   1.216 +		else
   1.217 +			pass = r == (aExpected ? KErrNone : KErrBadDescriptor);
   1.218 +		if (!pass)
   1.219 +			{
   1.220 +			RDebug::Printf("  Error writing to the descriptor data r %d", r);
   1.221 +			return KErrGeneral;
   1.222 +			}
   1.223 +		}
   1.224 +
   1.225 +	if (!aExpected)
   1.226 +		{// The client should have been killed as the data wasn't present.
   1.227 +		if(!iClientDied)
   1.228 +			User::After(500000); // allow time for client to die before next test
   1.229 +		iClientDied = ETrue;
   1.230 +		}
   1.231 +	return KErrNone;
   1.232 +	}
   1.233 +
   1.234 +TInt CTestSession::CheckArgsPresent(const RMessage2& aMessage, TBool arg0Present, TBool arg1Present, TBool arg2Present, TBool arg3Present, TBool aWrite=EFalse)
   1.235 +	{
   1.236 +	// Adjust the pinning status expected based on the default policies.
   1.237 +	// (Must do this before anything else as UpdateExpected() accessed paged global data)
   1.238 +	arg0Present = UpdateExpected(arg0Present);
   1.239 +	arg1Present = UpdateExpected(arg1Present);
   1.240 +	arg2Present = UpdateExpected(arg2Present);
   1.241 +	arg3Present = UpdateExpected(arg3Present);
   1.242 +
   1.243 +	// Flush the cache so on paged systems, unpinned paged memory will be discarded.
   1.244 +	DPTest::FlushCache();
   1.245 +
   1.246 +	TInt r = User::SetRealtimeState(User::ERealtimeStateOn);
   1.247 +	if (r != KErrNone)
   1.248 +		{
   1.249 +		RDebug::Printf("Error setting realtime state r = %d", r);
   1.250 +		return r;
   1.251 +		}
   1.252 +
   1.253 +	r = CheckDesPresent(aMessage, 0, arg0Present, aWrite);
   1.254 +	if (r == KErrNone)
   1.255 +		r = CheckDesPresent(aMessage, 1, arg1Present, aWrite);
   1.256 +	if (r == KErrNone)
   1.257 +		r = CheckDesPresent(aMessage, 2, arg2Present, aWrite);
   1.258 +	if (r == KErrNone)
   1.259 +		r = CheckDesPresent(aMessage, 3, arg3Present, aWrite);
   1.260 +
   1.261 +	User::SetRealtimeState(User::ERealtimeStateOff);
   1.262 +
   1.263 +	return r;
   1.264 +	}
   1.265 +
   1.266 +void CTestSession::ServiceL(const RMessage2& aMessage)
   1.267 +//
   1.268 +// Virtual message-handler
   1.269 +//
   1.270 +	{
   1.271 +	TInt r = KErrNone;
   1.272 +	iClientDied = EFalse;
   1.273 +	switch (aMessage.Function())
   1.274 +		{
   1.275 +		case EStop:
   1.276 +			CActiveScheduler::Stop();
   1.277 +			break;
   1.278 +		case ETestPinAll:
   1.279 +			r = CheckArgsPresent(aMessage, ETrue, ETrue, ETrue, ETrue);
   1.280 +			break;
   1.281 +		case ETestPinOdd:
   1.282 +			r = CheckArgsPresent(aMessage, EFalse, ETrue, EFalse, ETrue);
   1.283 +			break;
   1.284 +		case ETestPinEven:
   1.285 +			r = CheckArgsPresent(aMessage, ETrue, EFalse, ETrue, EFalse);
   1.286 +			break;
   1.287 +		case ETestPin3:
   1.288 +			r = CheckArgsPresent(aMessage, ETrue, ETrue, ETrue, EFalse);
   1.289 +			break;
   1.290 +		case ETestPin2:
   1.291 +			r = CheckArgsPresent(aMessage, ETrue, ETrue, EFalse, EFalse);
   1.292 +			break;
   1.293 +		case ETestPin1:
   1.294 +			r = CheckArgsPresent(aMessage, ETrue, EFalse, EFalse, EFalse);
   1.295 +			break;
   1.296 +		case ETestPin0:
   1.297 +		case ETestPinDefault:
   1.298 +			r = CheckArgsPresent(aMessage, EFalse, EFalse, EFalse, EFalse);
   1.299 +			break;
   1.300 +		case ETestPinWritable:
   1.301 +			r = CheckArgsPresent(aMessage, ETrue, ETrue, ETrue, ETrue, ETrue);
   1.302 +			break;
   1.303 +		case ETestUnpinWritable:
   1.304 +			r = CheckArgsPresent(aMessage, EFalse, EFalse, EFalse, EFalse, ETrue);
   1.305 +			break;
   1.306 +		default:
   1.307 +			r = KErrNotSupported;
   1.308 +
   1.309 +		}
   1.310 + 	aMessage.Complete(r);
   1.311 +
   1.312 +	// If descriptors aren't as expected then panic so the test will fail.
   1.313 +	if (r != KErrNone)
   1.314 +		User::Panic(_L("ServiceL failure"), r);
   1.315 +	}
   1.316 +
   1.317 +// CTestSession funtions
   1.318 +
   1.319 +void CMyActiveScheduler::Error(TInt anError) const
   1.320 +//
   1.321 +// Virtual error handler
   1.322 +//
   1.323 +	{
   1.324 +	User::Panic(_L("CMyActiveScheduer::Error"), anError);
   1.325 +	}
   1.326 +
   1.327 +TInt ServerThread(TAny* aPinningAttrib)
   1.328 +//
   1.329 +// Passed as the server thread in 2 tests - sets up and runs CTestServer
   1.330 +//
   1.331 +	{
   1.332 +	RTest test(_L("T_SVRPINNING...server"));
   1.333 +	CMyActiveScheduler* pScheduler = new CMyActiveScheduler;
   1.334 +	if (pScheduler == NULL)
   1.335 +		{
   1.336 +		gSem.Signal();
   1.337 +		test(0);
   1.338 +		}
   1.339 +
   1.340 +	CActiveScheduler::Install(pScheduler);
   1.341 +
   1.342 +	CTestServer* pServer = new CTestServer(0);
   1.343 +	if (pServer == NULL)
   1.344 +		{
   1.345 +		gSem.Signal();
   1.346 +		test(0);
   1.347 +		}
   1.348 +
   1.349 +	// Set the pinning attributes of the server.
   1.350 +	TServerPinning pinningAttrib = (TServerPinning)(TInt)aPinningAttrib;
   1.351 +	switch (pinningAttrib)
   1.352 +		{
   1.353 +		case EServerDefault :
   1.354 +		case EServerSetPinningTooLate :
   1.355 +			break;
   1.356 +		case EServerPinning :
   1.357 +			pServer->SetPinClientDescriptors(ETrue);
   1.358 +			break;
   1.359 +		case EServerNotPinning :
   1.360 +			pServer->SetPinClientDescriptors(EFalse);
   1.361 +			break;
   1.362 +		default :
   1.363 +			break;
   1.364 +		}
   1.365 +
   1.366 +	//Starting a CServer2 also Adds it to the ActiveScheduler
   1.367 +	TInt r = pServer->Start(KServerName);
   1.368 +	if (r != KErrNone)
   1.369 +		{
   1.370 +		gSem.Signal();
   1.371 +		test(0);
   1.372 +		}
   1.373 +
   1.374 +	if (pinningAttrib == EServerSetPinningTooLate)
   1.375 +		{
   1.376 +		pServer->SetPinClientDescriptors(EFalse);
   1.377 +		}
   1.378 +
   1.379 +	test.Next(_L("Start ActiveScheduler and signal to client"));
   1.380 +	test.Printf(_L("        There might be something going on beneath this window\n"));
   1.381 +	gSem.Signal();
   1.382 +	CActiveScheduler::Start();
   1.383 +	test.Next(_L("Destroy ActiveScheduler"));
   1.384 +	delete pScheduler;
   1.385 +	delete pServer;
   1.386 +
   1.387 +	test.Close();
   1.388 +
   1.389 +	return (KErrNone);
   1.390 +	}
   1.391 +
   1.392 +
   1.393 +
   1.394 +#include <e32svr.h>
   1.395 +
   1.396 +void dummyFunction(TUint8* /*a0*/, TUint8* /*a1*/, TUint8* /*a2*/, TUint8* /*a3*/, TUint8* /*a4*/, TUint8* /*a5*/)
   1.397 +	{
   1.398 +	}
   1.399 +
   1.400 +TInt ClientThread(TAny* aTestMode)
   1.401 +//
   1.402 +// Passed as the first client thread - signals the server to do several tests
   1.403 +//
   1.404 +	{
   1.405 +	// Create the message arguments to be pinned.  Should be one of each type of
   1.406 +	// descriptor to test that the pinning code can correctly pin each type 
   1.407 +	// descriptor data.  Each descriptor's data should in a separate page in 
   1.408 +	// thread's stack to ensure that access to each argument will cause a 
   1.409 +	// separate page fault.
   1.410 +
   1.411 +	TUint8 argBufs[KPageSize*(6+2)]; // enough for 6 whole pages
   1.412 +	TUint8* argBuf0 = (TUint8*)(TUintPtr(argBufs+gPageMask)&~gPageMask);
   1.413 +	TUint8* argBuf1 = argBuf0+KPageSize;
   1.414 +	TUint8* argBuf2 = argBuf1+KPageSize;
   1.415 +	TUint8* argBuf3 = argBuf2+KPageSize;
   1.416 +	TUint8* argBuf4 = argBuf3+KPageSize;
   1.417 +	TUint8* argBuf5 = argBuf4+KPageSize;
   1.418 +
   1.419 +	argBuf0[0]='a'; argBuf0[1]='r'; argBuf0[2]='g'; argBuf0[3]='0'; argBuf0[4]='\0';
   1.420 +	TPtr8 arg0(argBuf0, 5, 20);
   1.421 +
   1.422 +	TBufC8<5>& arg1 = *(TBufC8<5>*)argBuf1;
   1.423 +	new (&arg1) TBufC8<5>((const TUint8*)"arg1");
   1.424 +
   1.425 +	argBuf2[0]='a'; argBuf2[1]='r'; argBuf2[2]='g'; argBuf2[3]='1'; argBuf2[4]='\0';
   1.426 +	TPtrC8 arg2((const TUint8*)argBuf2);
   1.427 +
   1.428 +	TBuf8<50>& arg3 = *(TBuf8<50>*)argBuf3;
   1.429 +	new (&arg3) TBuf8<50>((const TUint8*)"arg3");
   1.430 +
   1.431 +	// For some tests use this 5th and final type of descriptor.
   1.432 +	HBufC8* argTmp = HBufC8::New(7);
   1.433 +	*argTmp = (const TUint8*)"argTmp";
   1.434 +	RBuf8 argTmpBuf(argTmp);
   1.435 +
   1.436 +	// Need a couple of extra writable argments
   1.437 +	argBuf4[0]='a'; argBuf4[1]='r'; argBuf4[2]='g'; argBuf4[3]='4'; argBuf4[4]='\0';
   1.438 +	TPtr8 arg4(argBuf4, 5, 20);
   1.439 +	argBuf5[0]='a'; argBuf5[1]='r'; argBuf5[2]='g'; argBuf5[3]='5'; argBuf5[4]='\0';
   1.440 +	TPtr8 arg5(argBuf5, 5, 20);
   1.441 +
   1.442 +	RTest test(_L("T_SVRPINNING...client"));
   1.443 +	RSession session;
   1.444 +	TInt r = session.PublicCreateSession(_L("CTestServer"),5);
   1.445 +	if (r != KErrNone)
   1.446 +		{
   1.447 +		gSem.Signal();
   1.448 +		test(0);
   1.449 +		}
   1.450 +
   1.451 +	switch((TInt)aTestMode)
   1.452 +		{
   1.453 +		case CTestSession::ETestPinAll:
   1.454 +			test.Printf(_L("Test pinning all args\n"));
   1.455 +			r = session.PublicSendReceive(CTestSession::ETestPinAll, TIpcArgs(&arg0, &arg1, &arg2, &arg3).PinArgs());
   1.456 +			break;
   1.457 +
   1.458 +		case CTestSession::ETestPinOdd:
   1.459 +			test.Printf(_L("Test pinning odd args\n"));
   1.460 +			r = session.PublicSendReceive(CTestSession::ETestPinOdd, TIpcArgs(&arg0, &argTmpBuf, &arg2, &arg3).PinArgs(EFalse, ETrue, EFalse, ETrue));
   1.461 +			break;
   1.462 +
   1.463 +		case CTestSession::ETestPinEven:
   1.464 +			test.Printf(_L("Test pinning even args\n"));
   1.465 +			r = session.PublicSendReceive(CTestSession::ETestPinEven, TIpcArgs(&arg0, &arg1, argTmp, &arg3).PinArgs(ETrue, EFalse, ETrue, EFalse));
   1.466 +			break;
   1.467 +
   1.468 +		case CTestSession::ETestPin3:
   1.469 +			test.Printf(_L("Test pinning 3 args\n"));
   1.470 +			r = session.PublicSendReceive(CTestSession::ETestPin3, TIpcArgs(&arg0, &arg1, &arg2, &arg3).PinArgs(ETrue, ETrue, ETrue, EFalse));
   1.471 +			break;
   1.472 +
   1.473 +		case CTestSession::ETestPin2:
   1.474 +			test.Printf(_L("Test pinning 2 args\n"));
   1.475 +			r = session.PublicSendReceive(CTestSession::ETestPin2, TIpcArgs(argTmp, &arg1, &arg2, &arg3).PinArgs(ETrue, ETrue, EFalse, EFalse));
   1.476 +			break;
   1.477 +
   1.478 +		case CTestSession::ETestPin1:
   1.479 +			test.Printf(_L("Test pinning 1 args\n"));
   1.480 +			r = session.PublicSendReceive(CTestSession::ETestPin1, TIpcArgs(&argTmpBuf, &arg1, &arg2, &arg3).PinArgs(ETrue, EFalse, EFalse, EFalse));
   1.481 +			break;
   1.482 +
   1.483 +		case CTestSession::ETestPin0:
   1.484 +			test.Printf(_L("Test pinning 0 args\n"));
   1.485 +			r = session.PublicSendReceive(CTestSession::ETestPin0, TIpcArgs(&arg0, &arg1, &arg2, &arg3).PinArgs(EFalse, EFalse, EFalse, EFalse));
   1.486 +			break;
   1.487 +
   1.488 +		case CTestSession::ETestPinDefault:
   1.489 +			test.Printf(_L("Test the default pinning policy of this server\n"));
   1.490 +			r = session.PublicSendReceive(CTestSession::ETestPinDefault, TIpcArgs(&arg0, &arg1, &arg2, argTmp));
   1.491 +			break;
   1.492 +
   1.493 +		case CTestSession::ETestPinWritable:
   1.494 +			test.Printf(_L("Test writing to pinned descriptors\n"));
   1.495 +			r = session.PublicSendReceive(CTestSession::ETestPinWritable, TIpcArgs(&arg0, &arg3, &arg4, &arg5).PinArgs(ETrue, ETrue, ETrue, ETrue));
   1.496 +			// Verify the index of each argument has been written to each descriptor.
   1.497 +			{
   1.498 +			TUint maxLength = arg0.MaxLength();
   1.499 +			test_Equal(maxLength, arg0.Length());
   1.500 +			TUint j = 0;
   1.501 +			for (; j < maxLength; j++)
   1.502 +				test_Equal(0, arg0[j]);
   1.503 +			maxLength = arg3.MaxLength();
   1.504 +			test_Equal(maxLength, arg3.Length());
   1.505 +			for (j = 0; j < maxLength; j++)
   1.506 +				test_Equal(1, arg3[j]);
   1.507 +			maxLength = arg4.MaxLength();
   1.508 +			test_Equal(maxLength, arg4.Length());
   1.509 +			for (j = 0; j < maxLength; j++)
   1.510 +				test_Equal(2, arg4[j]);
   1.511 +			maxLength = arg5.MaxLength();
   1.512 +			test_Equal(maxLength, arg5.Length());
   1.513 +			for (j = 0; j < maxLength; j++)
   1.514 +				test_Equal(3, arg5[j]);
   1.515 +			}
   1.516 +			break;
   1.517 +
   1.518 +		case CTestSession::ETestUnpinWritable:
   1.519 +			test.Printf(_L("Test writing to unpinned descriptors\n"));
   1.520 +			r = session.PublicSendReceive(CTestSession::ETestUnpinWritable, TIpcArgs(&arg0, &arg3, &arg4, &arg5).PinArgs(EFalse, EFalse, EFalse, EFalse));
   1.521 +			// Verify the index of each argument has been written to each descriptor.
   1.522 +			// Unless this is a pinnning server than the thread will be panicked before we reach there.
   1.523 +			{
   1.524 +			TUint maxLength = arg0.MaxLength();
   1.525 +			test_Equal(maxLength, arg0.Length());
   1.526 +			TUint j = 0;
   1.527 +			for (j = 0; j < maxLength; j++)
   1.528 +				test_Equal(0, arg0[j]);
   1.529 +			maxLength = arg3.MaxLength();
   1.530 +			test_Equal(maxLength, arg3.Length());
   1.531 +			for (j = 0; j < maxLength; j++)
   1.532 +				test_Equal(1, arg3[j]);
   1.533 +			maxLength = arg4.MaxLength();
   1.534 +			test_Equal(maxLength, arg4.Length());
   1.535 +			for (j = 0; j < maxLength; j++)
   1.536 +				test_Equal(2, arg4[j]);
   1.537 +			maxLength = arg5.MaxLength();
   1.538 +			test_Equal(maxLength, arg5.Length());
   1.539 +			for (j = 0; j < maxLength; j++)
   1.540 +				test_Equal(3, arg5[j]);
   1.541 +			}
   1.542 +			break;
   1.543 +
   1.544 +		case CTestSession::ETestDeadServer:
   1.545 +			test.Printf(_L("Test pinning to dead server\n"));
   1.546 +			gSem.Signal();
   1.547 +			gSem1.Wait();
   1.548 +			r = session.PublicSendReceive(CTestSession::ETestPinAll, TIpcArgs(&arg0, &arg1, &arg2, &arg3).PinArgs());
   1.549 +			break;
   1.550 +
   1.551 +		case CTestSession::ETestPinOOM:
   1.552 +			test.Printf(_L("Pinning OOM tests\n"));
   1.553 +			__KHEAP_MARK;
   1.554 +			const TUint KMaxKernelAllocations = 1024;
   1.555 +			TUint i;
   1.556 +			r = KErrNoMemory;
   1.557 +			for (i = 0; i < KMaxKernelAllocations && r == KErrNoMemory; i++)
   1.558 +				{
   1.559 +				__KHEAP_FAILNEXT(i);
   1.560 +				r = session.PublicSendReceive(CTestSession::ETestPinAll, TIpcArgs(&arg0, &arg1, &arg2, &arg3).PinArgs());
   1.561 +				__KHEAP_RESET;
   1.562 +				}
   1.563 +			test.Printf(_L("SendReceive took %d tries\n"),i);
   1.564 +			test_KErrNone(r);
   1.565 +
   1.566 +			__KHEAP_MARKEND;
   1.567 +			break;
   1.568 +		}
   1.569 +
   1.570 +	session.Close();
   1.571 +	test.Close();
   1.572 +	return r;
   1.573 +	}
   1.574 +
   1.575 +
   1.576 +GLDEF_C TInt E32Main()
   1.577 +	{
   1.578 +	RTest test(_L("T_SVRPINNING...main"));
   1.579 +	test.Title();
   1.580 +
   1.581 +
   1.582 +	if (DPTest::Attributes() & DPTest::ERomPaging)
   1.583 +		test.Printf(_L("Rom paging supported\n"));
   1.584 +	if (DPTest::Attributes() & DPTest::ECodePaging)
   1.585 +		test.Printf(_L("Code paging supported\n"));
   1.586 +	if (DPTest::Attributes() & DPTest::EDataPaging)
   1.587 +		{
   1.588 +		test.Printf(_L("Data paging supported\n"));
   1.589 +		gDataPagingSupport = ETrue;
   1.590 +		}
   1.591 +
   1.592 +	// Determine the data paging attribute.
   1.593 +	RProcess process;	// Default to point to current process.
   1.594 +	gProcessPaged = process.DefaultDataPaged();
   1.595 +	test.Printf(_L("Process data paged %x\n"), gProcessPaged);
   1.596 +
   1.597 +	test.Start(_L("Test IPC message arguments pinning"));
   1.598 +	test_KErrNone(HAL::Get(HAL::EMemoryPageSize, gPageSize));
   1.599 +	gPageMask = gPageSize - 1;
   1.600 +	test_Equal(KPageSize, gPageSize);
   1.601 +	// Disable JIT as we are testing panics and don't want the emulator to hang.
   1.602 +	TBool justInTime = User::JustInTime();
   1.603 +	User::SetJustInTime(EFalse);
   1.604 +
   1.605 +	TBool exitFailure = EFalse;
   1.606 +	for (	gServerPinningState = EServerDefault; 
   1.607 +			gServerPinningState < EServerSetPinningTooLate && !exitFailure; 
   1.608 +			gServerPinningState++)
   1.609 +		{
   1.610 +		// Create the server with the specified pinning mode.
   1.611 +		switch (gServerPinningState)
   1.612 +			{
   1.613 +			case EServerDefault : 
   1.614 +				test.Next(_L("Test server with default pinning policy"));
   1.615 +				break;
   1.616 +			case EServerPinning : 
   1.617 +				test.Next(_L("Test server with pinning policy"));
   1.618 +				break;
   1.619 +			case EServerNotPinning : 
   1.620 +				test.Next(_L("Test server with not pinning policy"));
   1.621 +				break;
   1.622 +			}
   1.623 +		test_KErrNone(gSem.CreateLocal(0));
   1.624 +		test_KErrNone(gSem1.CreateLocal(0));
   1.625 +		// Create the server thread it needs to have a unpaged stack and heap.
   1.626 +		TThreadCreateInfo serverInfo(_L("Server Thread"), ServerThread, KDefaultStackSize, (TAny*)gServerPinningState);
   1.627 +		serverInfo.SetPaging(TThreadCreateInfo::EUnpaged);
   1.628 +		serverInfo.SetCreateHeap(KHeapMinSize, KHeapMaxSize);
   1.629 +		RThread serverThread;
   1.630 +		test_KErrNone(serverThread.Create(serverInfo));
   1.631 +		TRequestStatus serverStat;
   1.632 +		serverThread.Logon(serverStat);
   1.633 +		serverThread.Resume();
   1.634 +
   1.635 +		// Wait for the server to start and then create a session to it.
   1.636 +		gSem.Wait();
   1.637 +		RSession session;
   1.638 +		test_KErrNone(session.PublicCreateSession(_L("CTestServer"),5));
   1.639 +		
   1.640 +		for (	TUint clientTest = CTestSession::ETestPinAll; 
   1.641 +				clientTest <= CTestSession::ETestPinDefault && !exitFailure;
   1.642 +				clientTest++)
   1.643 +			{
   1.644 +			// Create the client thread it needs to have a paged stack and heap.
   1.645 +			TThreadCreateInfo clientInfo(_L("Client Thread"), ClientThread, 10 * gPageSize, (TAny*)clientTest);
   1.646 +			clientInfo.SetPaging(TThreadCreateInfo::EPaged);
   1.647 +			clientInfo.SetCreateHeap(KHeapMinSize, KHeapMaxSize);
   1.648 +			RThread clientThread;
   1.649 +			test_KErrNone(clientThread.Create(clientInfo));
   1.650 +
   1.651 +			TRequestStatus clientStat;
   1.652 +			clientThread.Logon(clientStat);
   1.653 +			clientThread.Resume();
   1.654 +
   1.655 +			// Wait for the client thread to end.
   1.656 +			User::WaitForRequest(clientStat);
   1.657 +
   1.658 +			// If all the descriptor arguments were not pinned then the client 
   1.659 +			// thread should have been panicked.
   1.660 +			TBool expectPanic = (clientTest == CTestSession::ETestPinAll || 
   1.661 +								clientTest == CTestSession::ETestPinWritable ||
   1.662 +								clientTest == CTestSession::ETestPinOOM )? 0 : 1;
   1.663 +			expectPanic = !UpdateExpected(!expectPanic);
   1.664 +
   1.665 +			TInt exitReason = clientThread.ExitReason();
   1.666 +			TInt exitType = clientThread.ExitType();
   1.667 +			if (expectPanic)
   1.668 +				{
   1.669 +				if (exitType != EExitPanic || 
   1.670 +					exitReason != EIllegalFunctionForRealtimeThread ||
   1.671 +					clientThread.ExitCategory() != _L("KERN-EXEC"))
   1.672 +					{// Thread didn't panic as expected.
   1.673 +					exitFailure = ETrue;
   1.674 +					}
   1.675 +				}
   1.676 +			else
   1.677 +				{
   1.678 +				if (exitType != EExitKill || exitReason != KErrNone)
   1.679 +					{// Thread didn't exit gracefully as expected.
   1.680 +					exitFailure = ETrue;
   1.681 +					}
   1.682 +				}
   1.683 +			CLOSE_AND_WAIT(clientThread);
   1.684 +			}
   1.685 +
   1.686 +		test.Next(_L("Test client sending message to closed server"));
   1.687 +		TThreadCreateInfo clientInfo(_L("Client Thread"), ClientThread, 10 * gPageSize, (TAny*)CTestSession::ETestDeadServer);
   1.688 +		clientInfo.SetPaging(TThreadCreateInfo::EPaged);
   1.689 +		clientInfo.SetCreateHeap(KHeapMinSize, KHeapMaxSize);
   1.690 +		RThread clientThread;
   1.691 +		test_KErrNone(clientThread.Create(clientInfo));
   1.692 +		TRequestStatus clientStat;
   1.693 +		clientThread.Logon(clientStat);
   1.694 +		clientThread.Resume();
   1.695 +		gSem.Wait();
   1.696 +		
   1.697 +		// Signal to stop ActiveScheduler and wait for server to stop.
   1.698 +		session.PublicSendReceive(CTestSession::EStop, TIpcArgs());
   1.699 +		session.Close();
   1.700 +		User::WaitForRequest(serverStat);
   1.701 +		if (serverThread.ExitType() != EExitKill)
   1.702 +			{
   1.703 +			test.Printf(_L("!!Server thread did something bizarre %d"), serverThread.ExitReason());
   1.704 +			}
   1.705 +
   1.706 +		gSem1.Signal();
   1.707 +		User::WaitForRequest(clientStat);
   1.708 +		test_Equal(EExitKill, clientThread.ExitType());
   1.709 +		test_Equal(KErrServerTerminated, clientThread.ExitReason());
   1.710 +
   1.711 +		CLOSE_AND_WAIT(clientThread);
   1.712 +		CLOSE_AND_WAIT(serverThread);
   1.713 +		CLOSE_AND_WAIT(gSem);
   1.714 +		CLOSE_AND_WAIT(gSem1);
   1.715 +		}
   1.716 +	test(!exitFailure);
   1.717 +
   1.718 +	test.Next(_L("Test server setting pinning policy after server started"));
   1.719 +	RThread serverThread;
   1.720 +	test_KErrNone(serverThread.Create(_L("Server Thread"),ServerThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize, (TAny*)gServerPinningState));
   1.721 +	TRequestStatus serverStat;
   1.722 +	serverThread.Logon(serverStat);
   1.723 +	serverThread.Resume();
   1.724 +	// The server should have panicked with E32USER-CBase 106.
   1.725 +	User::WaitForRequest(serverStat);
   1.726 +	TInt exitReason = serverThread.ExitReason();
   1.727 +	TInt exitType = serverThread.ExitType();
   1.728 +	test_Equal(EExitPanic, exitType);
   1.729 +	test_Equal(ECServer2InvalidSetPin, exitReason);
   1.730 +	if (_L("E32USER-CBase") != serverThread.ExitCategory())
   1.731 +		test(0);
   1.732 +	CLOSE_AND_WAIT(serverThread);
   1.733 +
   1.734 +	test.End();
   1.735 +
   1.736 +	// Set JIT back to original state.
   1.737 +	User::SetJustInTime(justInTime);
   1.738 +
   1.739 +	return (KErrNone);
   1.740 +	}