os/kernelhwsrv/kerneltest/e32test/stack/t_stacksize.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2003-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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\stack\t_stacksize.cpp
    15 // Overview:
    16 // Verifies the correct implementation of CreateStackOverride method of RProcess.
    17 // Meanings of constants:
    18 // KDefaultStackSize: 
    19 // - System wide default stack size in actual version;
    20 // KNumberOfFitIteration: 
    21 // - Number of DummyRecursion which can fit in KDefaultStackSize. 
    22 // Value = KDefaultStackSize / 2KB - 1 
    23 // KNumberOfUnfitIteration: 
    24 // - Number of DummyRecursion which is raised a Panic based on stack overflow.
    25 // Value = KDefaultStackSize / 2KB rounded up to nearest integer
    26 // Details:
    27 // ETestProcess1: Create a process with original RProcess::Create() and start it
    28 // - it passes with KNumberOfFitIteration DummyRecursion .
    29 // ETestProcess2: Create a process with RProcess::CreateWithStackOverride(), default 
    30 // stack size and start it
    31 // - it passes with KNumberOfFitIteration DummyRecursion.
    32 // ETestProcess3: Create a process with original RProcess::Create() and start it
    33 // - it raised Panic on Stack overflow with KNumberOfUnfitIteration DummyRecursion.
    34 // ETestProcess4: Create a process with RProcess::CreateWithStackOverride(), default 
    35 // stack size and start it
    36 // - it raised Panic on Stack overflow with KNumberOfUnfitIteration DummyRecursion.
    37 // ETestProcess5: Create a process with RProcess::CreateWithStackOverride(), 2*KDefaultStackSize 
    38 // stack and start it
    39 // - it passes with KNumberOfUnfitIteration DummyRecursion.
    40 // ETestProcess6: Create a process with RProcess::CreateWithStackOverride(), -2*KDefaultStackSize 
    41 // stack.
    42 // - the process creation interrupted with KErrArgument error code
    43 // ETestProcess7: Create a process with RProcess::CreateWithStackOverride(), KDefaultStackSize/2 
    44 // stack and start it
    45 // - it passes with KNumberOfFitIteration DummyRecursion, because the process will be 
    46 // create with (App. image) default stack size.
    47 // Platforms/Drives/Compatibility:
    48 // It is won't work on emulator, because the unlimited stack size.
    49 // Assumptions/Requirement/Pre-requisites:
    50 // No
    51 // Failures and causes:
    52 // Failures of this test will indicate defects in the implementation of CreateWithStackOverride().
    53 // Base Port information:
    54 // No?
    55 // 
    56 //
    57 
    58 #include <e32test.h>
    59 #include "u32std.h"
    60 #include <u32hal.h>
    61 #include <e32svr.h>
    62 
    63 LOCAL_D RTest test(_L("T_STACKSIZE"));
    64 LOCAL_D RTest test2(_L("*** T_STACKSIZE SLAVE ***"));
    65 
    66 // We are here making the assumption that one quarter of the stack should be sufficient
    67 // to accommodate for the overhead generated by the recursive calls as well as
    68 // unpredictable compiler optimisations.
    69 const TInt KStackGobbleSize = KDefaultStackSize / 4 - 8;
    70 const TInt KNumberOfFitIteration = 3;
    71 const TInt KNumberOfUnfitIteration = 4;
    72 
    73 const TInt KImageStackSize = 0x2000; // Test app image stack size (epocstacksize in MMP file)
    74 
    75 enum TTestProcessFunctions
    76 	{
    77 	ETestProcess1,
    78 	ETestProcess2,
    79 	ETestProcess3,
    80 	ETestProcess4,
    81 	ETestProcess5,
    82 	ETestProcess6,
    83 	ETestProcess7,
    84 	};
    85 
    86 class RTestProcess : public RProcess
    87 	{
    88 public:
    89 	TInt Create(TTestProcessFunctions aFunction, TInt aStackSize=0, TInt aArg1=-1,TInt aArg2=-1);
    90 	};
    91 
    92 TInt RTestProcess::Create(TTestProcessFunctions aFunction, TInt aStackSize, TInt aArg1,TInt aArg2)
    93 	{
    94 	
    95 	test.Printf(_L("RTestProcess::Create started with stack size:%d bytes\n"), aStackSize);
    96 	if(aArg1==-1)
    97 		aArg1 = RProcess().Id();
    98 	TBuf<512> commandLine;
    99 	commandLine.Num((TInt)aFunction);
   100 	commandLine.Append(_L(" "));
   101 	commandLine.AppendNum(aArg1);
   102 	commandLine.Append(_L(" "));
   103 	commandLine.AppendNum(aArg2);
   104 
   105 	TFileName filename(RProcess().FileName());
   106 	TInt pos=filename.LocateReverse(TChar('\\'));
   107 	filename.SetLength(pos+1);
   108 	filename+=_L("T_STACKSIZE.EXE");
   109 	
   110 	TInt r=KErrNone;
   111 	switch (aFunction)
   112 		{
   113 		case ETestProcess1:
   114 			test.Printf(_L("Call original Create.\n"));
   115 			r = RProcess::Create(filename,commandLine); 
   116 			break;
   117 			
   118 		case ETestProcess2:
   119 			test.Printf(_L("Call CreateWithStackOverride\n"));	
   120 			r = RProcess::CreateWithStackOverride(filename, commandLine, TUidType(), aStackSize, EOwnerProcess);
   121 			break;
   122 			
   123 		case ETestProcess3:
   124 			test.Printf(_L("Call original Create.\n"));
   125 			r = RProcess::Create(filename,commandLine); 
   126 			break;
   127 			
   128 		case ETestProcess4:
   129 			test.Printf(_L("Call CreateWithStackOverride\n"));	
   130 			r = RProcess::CreateWithStackOverride(filename, commandLine, TUidType(), aStackSize, EOwnerProcess);
   131 			break;
   132 			
   133 		case ETestProcess5:
   134 			test.Printf(_L("Call CreateWithStackOverride\n"));	
   135 			r = RProcess::CreateWithStackOverride(filename, commandLine, TUidType(), aStackSize, EOwnerProcess);
   136 			break;
   137 			
   138 		case ETestProcess6:
   139 			test.Printf(_L("Call CreateWithStackOverride\n"));	
   140 			r = RProcess::CreateWithStackOverride(filename, commandLine, TUidType(), aStackSize, EOwnerProcess);
   141 			break;
   142 			
   143 		case ETestProcess7:
   144 			test.Printf(_L("Call CreateWithStackOverride\n"));	
   145 			r = RProcess::CreateWithStackOverride(filename, commandLine, TUidType(), aStackSize, EOwnerProcess);
   146 			break;
   147 
   148 		default:
   149 			User::Panic(_L("T_STACKSIZE"),1);
   150 			
   151 		}
   152 
   153 	test.Printf(_L("RTestProcess::Create end.\n"));
   154 	return r;
   155 	}
   156 
   157 
   158 TInt DummyRecursion(TInt aA)
   159 	{
   160 	TBuf8<KStackGobbleSize> gobble;	// gobble 1/4 of the stack
   161 	gobble.Append(TChar(aA));	// stop compiler optimising out gobble buffer
   162 	if (aA <= 1)
   163 		return aA;
   164 	return 1 + DummyRecursion(aA-1);
   165 	}
   166 
   167 // Make these global so we don't push too much stuff on the stack
   168 TThreadStackInfo ThreadStackInfo;
   169 _LIT(KStackSizeText, "Stack size is %d bytes\n");
   170 
   171 TInt DoTestProcess(TInt aTestNum,TInt aArg1,TInt aArg2)
   172 	{
   173 	(void)aArg1;
   174 	(void)aArg2;
   175 	RThread().StackInfo(ThreadStackInfo);
   176 
   177 	switch(aTestNum)
   178 		{
   179 	case ETestProcess1:
   180 		{
   181 		test2.Printf(_L("ETestProcess1 started with %d recursion...\n"), KNumberOfFitIteration);
   182 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   183 #ifndef __WINS__
   184 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KDefaultStackSize);
   185 #endif
   186 		test2(DummyRecursion(KNumberOfFitIteration)==KNumberOfFitIteration);
   187 		break;
   188 		}
   189 		
   190 	case ETestProcess2:
   191 		{
   192 		test2.Printf(_L("ETestProcess2 started with %d recursion...\n"), KNumberOfFitIteration);
   193 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   194 #ifndef __WINS__
   195 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KDefaultStackSize);
   196 #endif
   197 		test2(DummyRecursion(KNumberOfFitIteration)==KNumberOfFitIteration);
   198 		break;
   199 		}
   200 		
   201 	case ETestProcess3:
   202 		{
   203 		test2.Printf(_L("ETestProcess3 started with %d recusion...\n"), KNumberOfUnfitIteration);
   204 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   205 #ifndef __WINS__
   206 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KDefaultStackSize);
   207 #endif
   208 		test2(DummyRecursion(KNumberOfUnfitIteration)==KNumberOfUnfitIteration);
   209 		break;
   210 		}
   211 		
   212 	case ETestProcess4:
   213 		{
   214 		test2.Printf(_L("ETestProcess4 started with %d recursion...\n"), KNumberOfUnfitIteration);
   215 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   216 #ifndef __WINS__
   217 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KDefaultStackSize);
   218 #endif
   219 		test2(DummyRecursion(KNumberOfUnfitIteration)==KNumberOfUnfitIteration);
   220 		break;
   221 		}
   222 		
   223 	case ETestProcess5:
   224 		{
   225 		test2.Printf(_L("ETestProcess5 started with %d recursion...\n"), KNumberOfUnfitIteration);
   226 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   227 #ifndef __WINS__
   228 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KDefaultStackSize * 2);
   229 #endif
   230 		test2(DummyRecursion(KNumberOfUnfitIteration)==KNumberOfUnfitIteration);
   231 		break;
   232 		}
   233 		
   234 	case ETestProcess6:
   235 		{
   236 		test2(EFalse); // Process creation should have failed
   237 		}
   238 
   239 	case ETestProcess7:
   240 		{
   241 		test2.Printf(_L("ETestProcess7 started with %d recursion\n"), KNumberOfFitIteration);
   242 		test2.Printf(KStackSizeText, ThreadStackInfo.iBase - ThreadStackInfo.iLimit);
   243 #ifndef __WINS__
   244 		test2((TInt) (ThreadStackInfo.iBase - ThreadStackInfo.iLimit) == KImageStackSize); // Should default to stack size set in image header
   245 		if (KImageStackSize == KDefaultStackSize) // If this is not the case, results can be a bit unpredictable
   246 			{
   247 			test2(DummyRecursion(KNumberOfFitIteration)==KNumberOfFitIteration);
   248 			}
   249 #endif
   250 		break;
   251 		}
   252 		
   253 	default:
   254 		User::Panic(_L("T_STACKSIZE"),1);
   255 		}
   256 
   257 	test2.Printf(_L("\n\t finished.\n"));
   258 	
   259 	return KErrNone;
   260 	}
   261 
   262 GLDEF_C TInt E32Main()
   263     {
   264     
   265 	
   266 	TBuf16<512> cmd;
   267 	User::CommandLine(cmd);
   268 	if(cmd.Length() && TChar(cmd[0]).IsDigit())
   269 		{
   270 		test2.Title();
   271 		test2.Start(_L("Slave process started..."));
   272 		TInt function = -1;
   273 		TInt arg1 = -1;
   274 		TInt arg2 = -1;
   275 		TLex lex(cmd);
   276 		lex.Val(function);
   277 		lex.SkipSpace();
   278 		lex.Val(arg1);
   279 		lex.SkipSpace();
   280 		lex.Val(arg2);
   281 		int r = DoTestProcess(function,arg1,arg2); 
   282 		test2.End();
   283 		return r;
   284 		}
   285 
   286 	test.Title();
   287 
   288 	RTestProcess rogueP;
   289 	TRequestStatus rendezvous;
   290 
   291 	test.Start(_L("Create process with original Create and default stack size"));
   292 	TInt r = rogueP.Create(ETestProcess1, KDefaultStackSize);
   293 	test(r==KErrNone);
   294 	rogueP.Rendezvous(rendezvous);
   295 	rogueP.Resume();
   296 	User::WaitForRequest(rendezvous);
   297 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType() );
   298 	test(rogueP.ExitType()==EExitKill);
   299 	CLOSE_AND_WAIT(rogueP);
   300 	
   301 	test.Next(_L("Create process with CreateWithStackOverride and default stack size"));
   302 	r = rogueP.Create(ETestProcess2, KDefaultStackSize);
   303 	test(r==KErrNone);
   304 	rogueP.Rendezvous(rendezvous);
   305 	rogueP.Resume();
   306 	User::WaitForRequest(rendezvous);
   307 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType() );
   308 	test(rogueP.ExitType()==EExitKill);
   309 	CLOSE_AND_WAIT(rogueP);
   310 	
   311 	test.Next(_L("Create process with original Create and default stack size"));
   312 	r = rogueP.Create(ETestProcess3, KDefaultStackSize);
   313 	test(r==KErrNone);
   314 	rogueP.Rendezvous(rendezvous);
   315 	rogueP.Resume();
   316 	User::WaitForRequest(rendezvous);
   317 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType() );
   318 #if !defined(__WINS__)
   319 	test(rogueP.ExitType()==EExitPanic);
   320 #else
   321 	test(rogueP.ExitType()==EExitKill);
   322 #endif
   323 	CLOSE_AND_WAIT(rogueP);
   324 	
   325 	test.Next(_L("Create process with CreateWithStackOverride and default stack size"));
   326 	r = rogueP.Create(ETestProcess4, KDefaultStackSize);
   327 	test(r==KErrNone);
   328 	rogueP.Rendezvous(rendezvous);
   329 	rogueP.Resume();
   330 	User::WaitForRequest(rendezvous);
   331 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType());
   332 #if !defined(__WINS__)
   333 	test(rogueP.ExitType()==EExitPanic);
   334 #else
   335 	test(rogueP.ExitType()==EExitKill);
   336 #endif
   337 	CLOSE_AND_WAIT(rogueP);
   338 
   339 	test.Next(_L("Create process with CreateWithStackOverride and 2 * KDefaultStackSize stack size"));
   340 	r = rogueP.Create(ETestProcess5, 2 * KDefaultStackSize );
   341 	test(r==KErrNone);
   342 	rogueP.Rendezvous(rendezvous);
   343 	rogueP.Resume();
   344 	User::WaitForRequest(rendezvous);
   345 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType() );
   346 	test(rogueP.ExitType()==EExitKill);
   347 	CLOSE_AND_WAIT(rogueP);
   348 
   349 #if !defined(__WINS__)
   350 	test.Next(_L("Create process with CreateWithStackOverride and negative stack size"));
   351 	r = rogueP.Create(ETestProcess6, - 2 * KDefaultStackSize );
   352 	test(r==KErrArgument);
   353 #endif
   354 
   355 	test.Next(_L("Create process with CreateWithStackOverride and KImageStackSize/2 stack size"));
   356 	r = rogueP.Create(ETestProcess7, KImageStackSize / 2 );
   357 	test(r==KErrNone);
   358 	rogueP.Rendezvous(rendezvous);
   359 	rogueP.Resume();
   360 	User::WaitForRequest(rendezvous);
   361 	test.Printf(_L("ExitType:%d\n"),rogueP.ExitType() );
   362 	test(rogueP.ExitType()==EExitKill);
   363 	CLOSE_AND_WAIT(rogueP);
   364 	
   365 	test.Printf(_L("Test finished.\n"));
   366 	test.End();
   367 
   368 	return(0);
   369     }
   370