os/kernelhwsrv/kerneltest/e32test/thread/t_smpsafe.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) 2008-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\thread\t_smpsafe.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#define __E32TEST_EXTENSION__
sl@0
    19
#include <e32test.h>
sl@0
    20
#include <e32ldr.h>
sl@0
    21
#include <e32svr.h>
sl@0
    22
#include "u32std.h"
sl@0
    23
#include <u32hal.h>
sl@0
    24
#include <d_ldrtst.h>
sl@0
    25
sl@0
    26
/////////////////////////////////////////////////////////////////////////////
sl@0
    27
//
sl@0
    28
//! @SYMTestCaseID			KBASE-T_SMPSAFE-2700
sl@0
    29
//! @SYMTestType			UT
sl@0
    30
//! @SYMPREQ				PREQ2094
sl@0
    31
//! @SYMTestCaseDesc		SMP compatibility mode test
sl@0
    32
//! @SYMTestActions			
sl@0
    33
//! @SYMTestExpectedResults All tests should pass.
sl@0
    34
//! @SYMTestPriority        Medium
sl@0
    35
//! @SYMTestStatus          Implemented
sl@0
    36
//
sl@0
    37
// The test attempts to prove that the SMPSAFE compatibility mode mechanism
sl@0
    38
// works and correctly forces processes which contain any unsafe code to run
sl@0
    39
// as if they were on a single-cpu machine. This is done by loading and
sl@0
    40
// unloading various combinations of DLLs into the test process itself, and
sl@0
    41
// by spawning and exiting various EXEs.
sl@0
    42
//
sl@0
    43
// Two things are checked for each combination:
sl@0
    44
//
sl@0
    45
// 1) D_LDRTST is used to retrieve the relevant process's SMP unsafe count,
sl@0
    46
//    the number of top-level binaries loaded into that process which are not
sl@0
    47
//    SMP safe. This works on all systems, including uniprocessor, even if
sl@0
    48
//    compatibility mode is not enabled, as this accounting is done
sl@0
    49
//    unconditionally.
sl@0
    50
//
sl@0
    51
// 2) If the system running the test has multiple processors, and one of the
sl@0
    52
//    compatibility modes is actually enabled, the test process runs a loop
sl@0
    53
//    designed to see if concurrent execution of threads actually happens.
sl@0
    54
//    (the loop is in smpsafe.cpp because it is shared between the test and
sl@0
    55
//    the small slave programs used).
sl@0
    56
sl@0
    57
RTest test(_L("T_SMPSAFE"));
sl@0
    58
RLdrTest ldd;
sl@0
    59
sl@0
    60
RProcess pLoaded;
sl@0
    61
TBool SMPPlatform;
sl@0
    62
TBool CompatMode;
sl@0
    63
sl@0
    64
extern TInt CheckAffinity();
sl@0
    65
sl@0
    66
// load an exe and check that it has the expected SMP unsafe count (check 1)
sl@0
    67
// don't resume/delete it yet.
sl@0
    68
void DoStartExe(RProcess& p, const TDesC &aFilename, TInt aExpectedUnsafe)
sl@0
    69
	{
sl@0
    70
	test_KErrNone(p.Create(aFilename, KNullDesC));
sl@0
    71
	test_Equal(aExpectedUnsafe, ldd.ProcessSMPUnsafeCount(p.Handle()));
sl@0
    72
	}
sl@0
    73
sl@0
    74
// resume the exe and if compatibility mode is available, check that the
sl@0
    75
// expected outcome of the test loop was observed (check 2)
sl@0
    76
// delete it afterward.
sl@0
    77
void DoStopExe(RProcess& p, TInt aExpectedUnsafe)
sl@0
    78
	{
sl@0
    79
	TRequestStatus s;
sl@0
    80
	p.Logon(s);
sl@0
    81
	p.Resume();
sl@0
    82
	User::WaitForRequest(s);
sl@0
    83
	if (CompatMode)
sl@0
    84
		test_Equal(aExpectedUnsafe ? 1 : 0, s.Int());
sl@0
    85
	test_Equal(EExitKill, p.ExitType());
sl@0
    86
	p.NotifyDestruction(s);
sl@0
    87
	p.Close();
sl@0
    88
	User::WaitForRequest(s);
sl@0
    89
	}
sl@0
    90
sl@0
    91
void StartExe(const TDesC &aFilename, TInt aExpectedUnsafe)
sl@0
    92
	{
sl@0
    93
	DoStartExe(pLoaded, aFilename, aExpectedUnsafe);
sl@0
    94
	}
sl@0
    95
sl@0
    96
void StopExe(TInt aExpectedUnsafe)
sl@0
    97
	{
sl@0
    98
	DoStopExe(pLoaded, aExpectedUnsafe);
sl@0
    99
	}
sl@0
   100
sl@0
   101
// start and stop an exe, doing both checks 1 and 2.
sl@0
   102
void TryExe(const TDesC &aFilename, TInt aExpectedUnsafe)
sl@0
   103
	{
sl@0
   104
	RProcess p;
sl@0
   105
	DoStartExe(p, aFilename, aExpectedUnsafe);
sl@0
   106
	DoStopExe(p, aExpectedUnsafe);
sl@0
   107
	}
sl@0
   108
sl@0
   109
// check the main test process, both checks 1 and 2.
sl@0
   110
void CheckSelf(TInt aExpectedUnsafe)
sl@0
   111
	{
sl@0
   112
	test_Equal(aExpectedUnsafe, ldd.ProcessSMPUnsafeCount(RProcess().Handle()));
sl@0
   113
	if (CompatMode)
sl@0
   114
		test_Equal(aExpectedUnsafe ? 1 : 0, CheckAffinity());
sl@0
   115
	}
sl@0
   116
sl@0
   117
GLDEF_C TInt E32Main()
sl@0
   118
	{
sl@0
   119
	RLibrary l, l2;
sl@0
   120
sl@0
   121
	// Turn off evil lazy dll unloading
sl@0
   122
	RLoader ldr;
sl@0
   123
	test(ldr.Connect()==KErrNone);
sl@0
   124
	test(ldr.CancelLazyDllUnload()==KErrNone);
sl@0
   125
	ldr.Close();
sl@0
   126
sl@0
   127
	test.Title();
sl@0
   128
	test.Start(_L("Test SMP safe binary flag"));
sl@0
   129
sl@0
   130
	test.Next(_L("Get number of CPUs"));
sl@0
   131
	TInt cpus = UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0);
sl@0
   132
	test_Compare(cpus, >, 0);
sl@0
   133
	SMPPlatform = cpus > 1;
sl@0
   134
	if (!SMPPlatform)
sl@0
   135
		{
sl@0
   136
		CompatMode = EFalse;
sl@0
   137
		test.Printf(_L("*****************************************************\n"));
sl@0
   138
		test.Printf(_L("Uniprocessor system, not actually testing compat mode\n"));
sl@0
   139
		test.Printf(_L("*****************************************************\n"));
sl@0
   140
		}
sl@0
   141
	else
sl@0
   142
		{
sl@0
   143
		test.Next(_L("Get compatibility mode setting"));
sl@0
   144
		TInt flags = UserSvr::HalFunction(EHalGroupKernel, EKernelHalConfigFlags, 0, 0);
sl@0
   145
		test_Compare(flags, >=, 0);
sl@0
   146
		CompatMode = flags & (EKernelConfigSMPUnsafeCompat | EKernelConfigSMPUnsafeCPU0);
sl@0
   147
		if (!CompatMode)
sl@0
   148
			{
sl@0
   149
			test.Printf(_L("*************************************************\n"));
sl@0
   150
			test.Printf(_L("Compatibility mode is not enabled, not testing it\n"));
sl@0
   151
			test.Printf(_L("*************************************************\n"));
sl@0
   152
			}
sl@0
   153
		}
sl@0
   154
sl@0
   155
	test.Next(_L("Load test LDD"));
sl@0
   156
	TInt r = User::LoadLogicalDevice(_L("d_ldrtst.ldd"));
sl@0
   157
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   158
	test_KErrNone(ldd.Open());
sl@0
   159
sl@0
   160
	test.Next(_L("Check we are safe ourselves"));
sl@0
   161
	CheckSelf(0);
sl@0
   162
sl@0
   163
	test.Next(_L("Check safe exe"));
sl@0
   164
	StartExe(_L("smpsafe0.exe"), 0);
sl@0
   165
	test.Next(_L("Load already loaded safe exe (self)"));
sl@0
   166
	TryExe(_L("smpsafe0.exe"), 0);
sl@0
   167
	StopExe(0);
sl@0
   168
sl@0
   169
	test.Next(_L("Check safe XIP exe"));
sl@0
   170
	StartExe(_L("smpsafex0.exe"), 0);
sl@0
   171
	test.Next(_L("Load already loaded safe XIP exe (self)"));
sl@0
   172
	TryExe(_L("smpsafex0.exe"), 0);
sl@0
   173
	StopExe(0);
sl@0
   174
sl@0
   175
	test.Next(_L("Load unsafe exe"));
sl@0
   176
	StartExe(_L("smpsafe1.exe"), 1);
sl@0
   177
	test.Next(_L("Load already loaded unsafe exe"));
sl@0
   178
	TryExe(_L("smpsafe1.exe"), 1);
sl@0
   179
	StopExe(1);
sl@0
   180
sl@0
   181
	test.Next(_L("Load safe exe directly linked to unsafe dll"));
sl@0
   182
	TryExe(_L("smpsafe2.exe"), 1);
sl@0
   183
sl@0
   184
	test.Next(_L("Dynamically load unsafe dll"));
sl@0
   185
	test_KErrNone(l.Load(_L("smpsafea.dll")));
sl@0
   186
	CheckSelf(1);
sl@0
   187
	test.Next(_L("Load safe exe directly linked to loaded unsafe dll"));
sl@0
   188
	TryExe(_L("smpsafe2.exe"), 1);
sl@0
   189
	test.Next(_L("Dynamically unload unsafe dll"));
sl@0
   190
	l.Close();
sl@0
   191
	CheckSelf(0);
sl@0
   192
sl@0
   193
	test.Next(_L("Load safe XIP exe directly linked to unsafe XIP dll"));
sl@0
   194
	TryExe(_L("smpsafex2.exe"), 1);
sl@0
   195
sl@0
   196
	test.Next(_L("Dynamically load unsafe XIP dll"));
sl@0
   197
	test_KErrNone(l.Load(_L("smpsafexa.dll")));
sl@0
   198
	CheckSelf(1);
sl@0
   199
	test.Next(_L("Load safe XIP exe directly linked to loaded unsafe XIP dll"));
sl@0
   200
	TryExe(_L("smpsafex2.exe"), 1);
sl@0
   201
	test.Next(_L("Dynamically unload unsafe XIP dll"));
sl@0
   202
	l.Close();
sl@0
   203
	CheckSelf(0);
sl@0
   204
sl@0
   205
	test.Next(_L("Load safe exe indirectly linked to unsafe dll"));
sl@0
   206
	TryExe(_L("smpsafe3.exe"), 1);
sl@0
   207
sl@0
   208
	test.Next(_L("Dynamically load unsafe dll"));
sl@0
   209
	test_KErrNone(l.Load(_L("smpsafea.dll")));
sl@0
   210
	CheckSelf(1);
sl@0
   211
	test.Next(_L("Load safe exe indirectly linked to loaded unsafe dll"));
sl@0
   212
	TryExe(_L("smpsafe3.exe"), 1);
sl@0
   213
	test.Next(_L("Dynamically unload unsafe dll"));
sl@0
   214
	l.Close();
sl@0
   215
	CheckSelf(0);
sl@0
   216
sl@0
   217
	test.Next(_L("Dynamically load safe dll linked to unsafe dll"));
sl@0
   218
	test_KErrNone(l.Load(_L("smpsafeb.dll")));
sl@0
   219
	CheckSelf(1);
sl@0
   220
	test.Next(_L("Load safe exe indirectly linked to unsafe dll, inbetween loaded"));
sl@0
   221
	TryExe(_L("smpsafe3.exe"), 1);
sl@0
   222
	test.Next(_L("Dynamically load unsafe dll as well"));
sl@0
   223
	test_KErrNone(l2.Load(_L("smpsafea.dll")));
sl@0
   224
	CheckSelf(2);
sl@0
   225
	test.Next(_L("Dynamically unload safe dll linked to unsafe dll"));
sl@0
   226
	l.Close();
sl@0
   227
	CheckSelf(1);
sl@0
   228
	test.Next(_L("Dynamically unload unsafe dll as well"));
sl@0
   229
	l2.Close();
sl@0
   230
	CheckSelf(0);
sl@0
   231
sl@0
   232
	test.Next(_L("Load safe exe directly linked to unsafe XIP dll"));
sl@0
   233
	TryExe(_L("smpsafe4.exe"), 1);
sl@0
   234
sl@0
   235
	test.Next(_L("Dynamically load unsafe XIP dll"));
sl@0
   236
	test_KErrNone(l.Load(_L("smpsafexa.dll")));
sl@0
   237
	CheckSelf(1);
sl@0
   238
	test.Next(_L("Load safe exe directly linked to loaded unsafe XIP dll"));
sl@0
   239
	TryExe(_L("smpsafe4.exe"), 1);
sl@0
   240
	test.Next(_L("Dynamically unload unsafe XIP dll"));
sl@0
   241
	l.Close();
sl@0
   242
	CheckSelf(0);
sl@0
   243
sl@0
   244
	test.Next(_L("Dynamically load figure-eight dll cycle"));
sl@0
   245
	test_KErrNone(l.Load(_L("smpsafec.dll")));
sl@0
   246
	CheckSelf(1);
sl@0
   247
	test.Next(_L("Load figure-eight from a different point"));
sl@0
   248
	test_KErrNone(l2.Load(_L("smpsafed.dll")));
sl@0
   249
	CheckSelf(2);
sl@0
   250
	test.Next(_L("Unload original point"));
sl@0
   251
	l.Close();
sl@0
   252
	CheckSelf(1);
sl@0
   253
	test.Next(_L("Unload second point"));
sl@0
   254
	l2.Close();
sl@0
   255
	CheckSelf(0);
sl@0
   256
sl@0
   257
	test.Next(_L("Close test LDD"));
sl@0
   258
	ldd.Close();
sl@0
   259
	test_KErrNone(User::FreeLogicalDevice(KLdrTestLddName));
sl@0
   260
sl@0
   261
	test.End();
sl@0
   262
	return KErrNone;
sl@0
   263
	}