kerneltest/f32test/loader/security/exet.cpp
changeset 0 a41df078684a
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1999-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 // f32test\loader\exet.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #define __INCLUDE_DEPENDENCY_GRAPH
       
    19 
       
    20 #include <e32svr.h>
       
    21 #include "dllt.h"
       
    22 #include "exetifc.h"
       
    23 #include "dlltree.h"
       
    24 #include <d_ldrtst.h>
       
    25 
       
    26 #ifdef __VC32__
       
    27 #pragma warning(disable:4706)
       
    28 #endif
       
    29 
       
    30 const TInt KMaxHandlesPerDll=4;
       
    31 const TInt KMaxHandles=KMaxHandlesPerDll*KNumModules;
       
    32 
       
    33 extern "C" TInt _E32Startup();
       
    34 
       
    35 extern "C" IMPORT_C void RegisterConstructorCall(TInt aDllNum);
       
    36 extern "C" IMPORT_C void RegisterInitCall(TInt aDllNum);
       
    37 extern "C" IMPORT_C void RegisterDestructorCall(TInt aDllNum);
       
    38 
       
    39 #define PANIC()		ExeTPanic(__LINE__)
       
    40 #define EXET_ASSERT(c)	((void)((c)||(PANIC(),0)))
       
    41 
       
    42 void ExeTPanic(TInt aLine)
       
    43 	{
       
    44 	User::Panic(_L("EXET"),aLine);
       
    45 	}
       
    46 
       
    47 /******************************************************************************
       
    48  * Class Definitions
       
    49  ******************************************************************************/
       
    50 class CDllInfo;
       
    51 class TDllList : public MDllList
       
    52 	{
       
    53 public:
       
    54 	TDllList();
       
    55 	virtual TBool IsPresent(const SDllInfo& aInfo);
       
    56 	virtual TInt Add(const SDllInfo& aInfo);
       
    57 	virtual void MoveToEnd(TInt aPos);
       
    58 public:
       
    59 	TInt iCount;
       
    60 	SDllInfo iInfo[KNumModules];
       
    61 	};
       
    62 
       
    63 class CTestServer : public CServer2
       
    64 	{
       
    65 public:
       
    66 	CTestServer();
       
    67 	virtual ~CTestServer();
       
    68 	virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
       
    69 	virtual TInt RunError(TInt aError);
       
    70 public:
       
    71 	CDllInfo* iInfo;
       
    72 	};
       
    73 
       
    74 class CTestSession : public CSession2
       
    75 	{
       
    76 public:
       
    77 	virtual ~CTestSession();
       
    78 	virtual void CreateL();
       
    79 	virtual void ServiceL(const RMessage2& aMessage);
       
    80 public:
       
    81 	TInt GetExeDepList(const RMessage2& aMessage);
       
    82 	TInt GetCDList(const RMessage2& aMessage);
       
    83 	TInt LoadDll(const RMessage2& aMessage);
       
    84 	TInt CloseDll(TInt aHandle);
       
    85 	TInt CallBlkI(TInt aHandle, TInt aIn);
       
    86 	TInt CallRBlkI(TInt aHandle, TInt aIn);
       
    87 public:
       
    88 	CDllInfo* iInfo;
       
    89 	};
       
    90 
       
    91 class CDllInfo : public CBase
       
    92 	{
       
    93 public:
       
    94 	CDllInfo();
       
    95 	virtual ~CDllInfo();
       
    96 	TInt Create();
       
    97 	TInt StoreHandle(TInt aHandle);
       
    98 	void RemoveHandle(TInt aIndex);
       
    99 	void SetupCDList();
       
   100 public:
       
   101 	void RegisterConstructorCall(TInt aDllNum);
       
   102 	void RegisterInitCall(TInt aDllNum);
       
   103 	void RegisterDestructorCall(TInt aDllNum);
       
   104 public:
       
   105 	TInt iNextGen;
       
   106 	TInt iHandleCount;
       
   107 	TInt iNextHandle;
       
   108 	TInt iHandles[KMaxHandles];
       
   109 	TInt iModuleNum[KMaxHandles];
       
   110 	TDllList iExeDepList;
       
   111 	TDllList iDllConstructList;
       
   112 	};
       
   113 
       
   114 /******************************************************************************
       
   115  * Static data
       
   116  ******************************************************************************/
       
   117 #ifdef __MODULE_HAS_DATA
       
   118 class TExeData
       
   119 	{
       
   120 public:
       
   121 	TExeData();
       
   122 public:
       
   123 	TTime iStartTime;
       
   124 	TTime iInitTime;
       
   125 	TInt iTest1;
       
   126 	TFileName iFileName;
       
   127 	};
       
   128 
       
   129 TInt Bss[16];
       
   130 TInt ExeNum=EXENUM;
       
   131 TInt Generation=0;
       
   132 TInt InitFlag=0;
       
   133 TFullName StartThread=RThread().FullName();
       
   134 TName StartProcess=RProcess().Name();
       
   135 TExeData TheExeDataObject;
       
   136 
       
   137 TExeData::TExeData()
       
   138 	:	iFileName(RProcess().FileName())
       
   139 	{
       
   140 	TInt r;
       
   141 	CHKDEPS(r);		// Check our dependencies are initialised
       
   142 	if (r!=KErrNone)
       
   143 		User::Panic(_L("CHKDEPS"),r);
       
   144 	iStartTime.HomeTime();
       
   145 	iTest1=299792458;
       
   146 	RegisterConstructorCall(EXENUM);
       
   147 	}
       
   148 #endif
       
   149 
       
   150 /******************************************************************************
       
   151  * Class TDllList
       
   152  ******************************************************************************/
       
   153 TDllList::TDllList()
       
   154 	{
       
   155 	iCount=0;
       
   156 	Mem::Fill(iInfo, KNumModules*sizeof(SDllInfo), 0xff);
       
   157 	}
       
   158 
       
   159 TBool TDllList::IsPresent(const SDllInfo& aInfo)
       
   160 	{
       
   161 	TInt i;
       
   162 	for (i=0; i<iCount; ++i)
       
   163 		{
       
   164 		if (iInfo[i].iDllNum==aInfo.iDllNum)
       
   165 			return ETrue;
       
   166 		}
       
   167 	return EFalse;
       
   168 	}
       
   169 
       
   170 TInt TDllList::Add(const SDllInfo& aInfo)
       
   171 	{
       
   172 	EXET_ASSERT(iCount<KNumModules);
       
   173 	TInt pos=iCount;
       
   174 	iInfo[iCount++]=aInfo;
       
   175 	return pos;
       
   176 	}
       
   177 
       
   178 void TDllList::MoveToEnd(TInt aPos)
       
   179 	{
       
   180 	if (aPos<iCount-1)
       
   181 		{
       
   182 		SDllInfo x(iInfo[aPos]);
       
   183 		Mem::Move(iInfo+aPos, iInfo+aPos+1, (iCount-aPos-1)*sizeof(SDllInfo));
       
   184 		iInfo[iCount-1]=x;
       
   185 		}
       
   186 	}
       
   187 
       
   188 /******************************************************************************
       
   189  * Class CTestSession/CTestServer
       
   190  ******************************************************************************/
       
   191 void ExceptionHandler(TExcType)
       
   192 	{
       
   193 	User::Leave(KErrGeneral);
       
   194 	}
       
   195 
       
   196 TUint32 Read(TLinAddr a)
       
   197 	{
       
   198 	return *(volatile TUint32*)a;
       
   199 	}
       
   200 
       
   201 TInt CheckReadable(TLinAddr a)
       
   202 	{
       
   203 	User::SetExceptionHandler(ExceptionHandler,KExceptionFault);
       
   204 	TRAPD(r,Read(a));
       
   205 	User::SetExceptionHandler(ExceptionHandler,0);
       
   206 	return r;
       
   207 	}
       
   208 
       
   209 CTestSession::~CTestSession()
       
   210 	{
       
   211 	}
       
   212 
       
   213 void CTestSession::CreateL()
       
   214 	{
       
   215 //	CSession2::CreateL();
       
   216 	}
       
   217 
       
   218 void CTestSession::ServiceL(const RMessage2& aMessage)
       
   219 	{
       
   220 	TInt r=KErrNotSupported;
       
   221 	TInt mid=aMessage.Function();
       
   222 	switch(mid)
       
   223 		{
       
   224 		case RLoaderTest::EMsgGetExeDepList:
       
   225 			r=GetExeDepList(aMessage);
       
   226 			break;
       
   227 		case RLoaderTest::EMsgLoadDll:
       
   228 			r=LoadDll(aMessage);
       
   229 			break;
       
   230 		case RLoaderTest::EMsgCallBlkI:
       
   231 			r=CallBlkI(aMessage.Int0(), aMessage.Int1());
       
   232 			break;
       
   233 		case RLoaderTest::EMsgCallRBlkI:
       
   234 			r=CallRBlkI(aMessage.Int0(), aMessage.Int1());
       
   235 			break;
       
   236 		case RLoaderTest::EMsgCloseDll:
       
   237 			r=CloseDll(aMessage.Int0());
       
   238 			break;
       
   239 		case RLoaderTest::EMsgGetCDList:
       
   240 			r=GetCDList(aMessage);
       
   241 			break;
       
   242 		case RLoaderTest::EMsgCheckReadable:
       
   243 			r=CheckReadable((TLinAddr)aMessage.Int0());
       
   244 			break;
       
   245 		case RLoaderTest::EMsgExit:
       
   246 			r=KErrNone;
       
   247 			CActiveScheduler::Stop();
       
   248 			break;
       
   249 		default:
       
   250 			break;
       
   251 		}
       
   252 	aMessage.Complete(r);
       
   253 	}
       
   254 
       
   255 TInt CTestSession::GetExeDepList(const RMessage2& aMsg)
       
   256 	{
       
   257 	TPtrC8 dep_list_ptr((const TUint8*)&iInfo->iExeDepList.iInfo, KNumModules*sizeof(SDllInfo));
       
   258 	aMsg.WriteL(0, dep_list_ptr, 0);
       
   259 	return KErrNone;
       
   260 	}
       
   261 
       
   262 TInt CTestSession::GetCDList(const RMessage2& aMsg)
       
   263 	{
       
   264 	TPtrC8 list_ptr((const TUint8*)&iInfo->iDllConstructList.iInfo, KNumModules*sizeof(SDllInfo));
       
   265 	aMsg.WriteL(0, list_ptr, 0);
       
   266 	return KErrNone;
       
   267 	}
       
   268 
       
   269 TInt CTestSession::LoadDll(const RMessage2& aMsg)
       
   270 	{
       
   271 	TInt module=aMsg.Int0();
       
   272 	TDllList dll_list;
       
   273 	TPtrC8 dll_list_ptr((const TUint8*)dll_list.iInfo, KNumModules*sizeof(SDllInfo));
       
   274 	TPtrC dllname=MODULE_FILENAME(module);
       
   275 	iInfo->SetupCDList();
       
   276 	RLibrary l;
       
   277 	TInt r=l.Load(dllname, TUidType());
       
   278 	if (r!=KErrNone)
       
   279 		return r;
       
   280 	TInitFunction f=(TInitFunction)l.Lookup(INIT_ORDINAL);
       
   281 	EXET_ASSERT(f);
       
   282 	r=(*f)(dll_list);
       
   283 	if (r!=KErrNone)
       
   284 		return r;
       
   285 	TBlkIFunction bf=(TBlkIFunction)l.Lookup(BLOCK_INC_ORDINAL);
       
   286 	EXET_ASSERT(bf);
       
   287 	TInt result=(*bf)(531441);
       
   288 	EXET_ASSERT(result==BlkIValue(module, 531441));
       
   289 	TInt h=iInfo->StoreHandle(l.Handle());
       
   290 	EXET_ASSERT(h>=0);
       
   291 	iInfo->iModuleNum[h]=module;
       
   292 	aMsg.WriteL(1, dll_list_ptr, 0);
       
   293 	return h;
       
   294 	}
       
   295 
       
   296 TInt CTestSession::CallBlkI(TInt aHandle, TInt aIn)
       
   297 	{
       
   298 	TInt h=iInfo->iHandles[aHandle];
       
   299 	EXET_ASSERT(h!=0);
       
   300 	RLibrary l;
       
   301 	l.SetHandle(h);
       
   302 	TBlkIFunction bf=(TBlkIFunction)l.Lookup(BLOCK_INC_ORDINAL);
       
   303 	EXET_ASSERT(bf);
       
   304 	return (*bf)(aIn);
       
   305 	}
       
   306 
       
   307 TInt CTestSession::CallRBlkI(TInt aHandle, TInt aIn)
       
   308 	{
       
   309 	TInt h=iInfo->iHandles[aHandle];
       
   310 	EXET_ASSERT(h!=0);
       
   311 	RLibrary l;
       
   312 	l.SetHandle(h);
       
   313 	TRBlkIFunction rbf=(TRBlkIFunction)l.Lookup(REC_BLOCK_INC_ORDINAL);
       
   314 	EXET_ASSERT(rbf);
       
   315 	++iInfo->iNextGen;
       
   316 	return (*rbf)(aIn,iInfo->iNextGen);
       
   317 	}
       
   318 
       
   319 TInt CTestSession::CloseDll(TInt aHandle)
       
   320 	{
       
   321 	TInt h=iInfo->iHandles[aHandle];
       
   322 	TInt m=iInfo->iModuleNum[aHandle];
       
   323 	EXET_ASSERT(h!=0);
       
   324 	EXET_ASSERT(m>=0);
       
   325 	iInfo->iHandles[aHandle]=0;
       
   326 	iInfo->iModuleNum[aHandle]=-1;
       
   327 	iInfo->SetupCDList();
       
   328 	RLibrary l;
       
   329 	l.SetHandle(h);
       
   330 	l.Close();
       
   331 	return KErrNone;
       
   332 	}
       
   333 
       
   334 CTestServer::CTestServer()
       
   335 	: CServer2(0,ESharableSessions)
       
   336 	{
       
   337 	}
       
   338 
       
   339 CTestServer::~CTestServer()
       
   340 	{
       
   341 	}
       
   342 
       
   343 CSession2* CTestServer::NewSessionL(const TVersion& aVersion, const RMessage2&) const
       
   344 	{
       
   345 	(void)aVersion;
       
   346 	CTestSession* s = new (ELeave) CTestSession;
       
   347 	s->iInfo=iInfo;
       
   348 	return s;
       
   349 	}
       
   350 
       
   351 _LIT(KExetErr,"EXETERR");
       
   352 TInt CTestServer::RunError(TInt aError)
       
   353 	{
       
   354 	User::Panic(KExetErr,aError);
       
   355 	return 0;
       
   356 	}
       
   357 
       
   358 /******************************************************************************
       
   359  * Class CDllInfo
       
   360  ******************************************************************************/
       
   361 TInt ChkC()
       
   362 	{
       
   363 #ifdef __MODULE_HAS_DATA
       
   364 	TInt init_mark=~((EXENUM+DLLNUMOFFSET)*(EXENUM+DLLNUMOFFSET));
       
   365 	if (InitFlag==init_mark)
       
   366 		return KErrNone;
       
   367 	if (InitFlag!=0)
       
   368 		return 0x494e4946;
       
   369 	TInt i;
       
   370 	TInt x=0;
       
   371 	for (i=0; i<16; ++i) x|=Bss[i];
       
   372 	if (x)
       
   373 		return 0x425353;
       
   374 	if (ExeNum!=EXENUM)
       
   375 		return 0x44415441;
       
   376 	if (Generation!=0)
       
   377 		return 0x47454e;
       
   378 	if (StartProcess!=RProcess().Name())
       
   379 		return 0x535450;
       
   380 	if (TheExeDataObject.iTest1!=299792458)
       
   381 		return 0x54455354;
       
   382 	if (TheExeDataObject.iFileName != RProcess().FileName())
       
   383 		return 0x464e414d;
       
   384 	InitFlag=init_mark;
       
   385 	RDebug::Print(_L("ChkC %S OK"),&TheExeDataObject.iFileName);
       
   386 #endif
       
   387 	return KErrNone;
       
   388 	}
       
   389 
       
   390 TInt Init(MDllList& aList)
       
   391 	{
       
   392 	TInt r=KErrNone;
       
   393 	SDllInfo info;
       
   394 	info.iDllNum=DLLNUM;
       
   395 	info.iEntryPointAddress=((TInt)&_E32Startup);
       
   396 	RLdrTest ldd;
       
   397 	ldd.Open();
       
   398 	info.iModuleHandle=ldd.ModuleHandleFromAddr((TInt)&_E32Startup);
       
   399 	ldd.Close();
       
   400 	if (!aList.IsPresent(info))
       
   401 		{
       
   402 		TInt pos=aList.Add(info);
       
   403 		INITDEPS(r,aList);		// Call Init on our dependencies
       
   404 		aList.MoveToEnd(pos);
       
   405 #ifdef __MODULE_HAS_DATA
       
   406 		if (r==KErrNone)
       
   407 			r=ChkC();		// Check initial values for .data/.bss and check constructors have been called
       
   408 #endif
       
   409 		RegisterInitCall(DLLNUM);
       
   410 		}
       
   411 	return r;
       
   412 	}
       
   413 
       
   414 CDllInfo::CDllInfo()
       
   415 	{
       
   416 	Mem::Fill(iModuleNum, sizeof(iModuleNum), 0xff);
       
   417 	}
       
   418 
       
   419 CDllInfo::~CDllInfo()
       
   420 	{
       
   421 	}
       
   422 
       
   423 TInt CDllInfo::Create()
       
   424 	{
       
   425 	TInt r;
       
   426 	r=UserSvr::DllSetTls(0, this);
       
   427 	if (r==KErrNone)
       
   428 		r=UserSvr::DllSetTls(TLS_INDEX, NULL);
       
   429 	if (r==KErrNone)
       
   430 		{
       
   431 		r=Init(iExeDepList);
       
   432 		}
       
   433 	return r;
       
   434 	}
       
   435 
       
   436 void CDllInfo::RegisterConstructorCall(TInt aDllNum)
       
   437 	{
       
   438 	(void)aDllNum;
       
   439 	}
       
   440 
       
   441 void CDllInfo::RegisterInitCall(TInt aDllNum)
       
   442 	{
       
   443 	(void)aDllNum;
       
   444 	}
       
   445 
       
   446 void CDllInfo::RegisterDestructorCall(TInt aDllNum)
       
   447 	{
       
   448 	(void)aDllNum;
       
   449 	}
       
   450 
       
   451 TInt CDllInfo::StoreHandle(TInt aHandle)
       
   452 	{
       
   453 	if (iHandleCount==KMaxHandles)
       
   454 		return KErrOverflow;
       
   455 	TInt i=iNextHandle;
       
   456 	for (; i<KMaxHandles && iHandles[i]!=0; ++i) {}
       
   457 	if (i==KMaxHandles)
       
   458 		{
       
   459 		for (i=0; i<iNextHandle && iHandles[i]!=0; ++i) {}
       
   460 		EXET_ASSERT(i!=iNextHandle);
       
   461 		}
       
   462 	iNextHandle=i;
       
   463 	iHandles[i]=aHandle;
       
   464 	++iHandleCount;
       
   465 	return i;
       
   466 	}
       
   467 
       
   468 void CDllInfo::RemoveHandle(TInt aIndex)
       
   469 	{
       
   470 	iHandles[aIndex]=0;
       
   471 	--iHandleCount;
       
   472 	}
       
   473 
       
   474 void CDllInfo::SetupCDList()
       
   475 	{
       
   476 	new (&iDllConstructList) TDllList;
       
   477 	EXET_ASSERT(UserSvr::DllSetTls(TLS_INDEX, &iDllConstructList)==KErrNone);
       
   478 	}
       
   479 
       
   480 /******************************************************************************
       
   481  * Exports
       
   482  ******************************************************************************/
       
   483 extern "C" __MODULE_EXPORT void RegisterConstructorCall(TInt aDllNum)
       
   484 	{
       
   485 	CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
       
   486 	if (p)
       
   487 		p->RegisterConstructorCall(aDllNum);
       
   488 	}
       
   489 
       
   490 extern "C" __MODULE_EXPORT void RegisterInitCall(TInt aDllNum)
       
   491 	{
       
   492 	CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
       
   493 	if (p)
       
   494 		p->RegisterInitCall(aDllNum);
       
   495 	}
       
   496 
       
   497 extern "C" __MODULE_EXPORT void RegisterDestructorCall(TInt aDllNum)
       
   498 	{
       
   499 	CDllInfo* p=(CDllInfo*)UserSvr::DllTls(0);
       
   500 	if (p)
       
   501 		p->RegisterDestructorCall(aDllNum);
       
   502 	}
       
   503 
       
   504 GLDEF_C TInt E32Main()
       
   505 	{
       
   506 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   507 	EXET_ASSERT(cleanup);
       
   508 	CActiveScheduler* sched=new CActiveScheduler;
       
   509 	EXET_ASSERT(sched);
       
   510 	CActiveScheduler::Install(sched);
       
   511 	CTestServer* svr=new CTestServer;
       
   512 	EXET_ASSERT(svr);
       
   513 	TBuf<16> suffix;
       
   514 	User::CommandLine(suffix);
       
   515 	TName svr_name=KServerName();
       
   516 	if (suffix.Length())
       
   517 		{
       
   518 		svr_name.Append('.');
       
   519 		svr_name+=suffix;
       
   520 		}
       
   521 	EXET_ASSERT(svr->Start(svr_name)==KErrNone);
       
   522 	CDllInfo* dllinfo=new CDllInfo;
       
   523 	EXET_ASSERT(dllinfo);
       
   524 	EXET_ASSERT(dllinfo->Create()==KErrNone);
       
   525 	svr->iInfo=dllinfo;
       
   526 
       
   527 	CActiveScheduler::Start();
       
   528 
       
   529 	UserSvr::DllFreeTls(0);
       
   530 	UserSvr::DllFreeTls(TLS_INDEX);
       
   531 	delete dllinfo;
       
   532 	delete svr;
       
   533 	delete sched;
       
   534 	delete cleanup;
       
   535 
       
   536 	return 0;
       
   537 	}