os/kernelhwsrv/kerneltest/f32test/loader/security/dllt.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) 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\security\dllt.cpp
    15 // 
    16 //
    17 
    18 #include <e32svr.h>
    19 #include "dllt.h"
    20 #include <d_ldrtst.h>
    21 
    22 #ifdef __VC32__
    23 #pragma warning(disable:4706)
    24 #endif
    25 
    26 extern "C" TInt _E32Dll(TInt);
    27 
    28 extern "C" __MODULE_EXPORT TInt INITFUNC(MDllList&);
    29 extern "C" __MODULE_EXPORT TInt CHKCFUNC();
    30 extern "C" __MODULE_EXPORT TInt RBLKIFUNC(TInt, TInt);
    31 
    32 extern "C" __MODULE_IMPORT TInt BLKIFUNC(TInt);
    33 
    34 #ifdef __DLL_LINK_TO_EXE
    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 #endif
    39 
    40 void GetDllInfo(SDllInfo& aInfo)
    41 	{
    42 	aInfo.iDllNum=DLLNUM;
    43 	aInfo.iEntryPointAddress=((TInt)&_E32Dll);
    44 	RLdrTest ldd;
    45 	ldd.Open();
    46 	aInfo.iModuleHandle=ldd.ModuleHandleFromAddr((TInt)&_E32Dll);
    47 	ldd.Close();
    48 	}
    49 
    50 #ifdef __MODULE_HAS_DATA
    51 class TDllData
    52 	{
    53 public:
    54 	TDllData();
    55 	~TDllData();
    56 public:
    57 	TTime iStartTime;
    58 	TTime iInitTime;
    59 	TInt iTest1;
    60 	TFileName iFileName;
    61 	RLibrary iLib;
    62 	};
    63 
    64 class TDllData2
    65 	{
    66 public:
    67 	TDllData2();
    68 	~TDllData2();
    69 public:
    70 	TInt iTest2;
    71 	};
    72 
    73 TInt Bss[16];
    74 TInt DllNum=DLLNUM;
    75 TInt Generation=0;
    76 TInt InitFlag=0;
    77 TInt CDFlag=0;
    78 TFullName StartThread=RThread().FullName();
    79 TName StartProcess=RProcess().Name();
    80 TDllData TheDllDataObject;
    81 TDllData2 TheDllDataObject2;
    82 
    83 void AddToCDList()
    84 	{
    85 	MDllList* pM=(MDllList*)UserSvr::DllTls(TLS_INDEX);
    86 	if (pM)
    87 		{
    88 		SDllInfo di;
    89 		GetDllInfo(di);
    90 		pM->Add(di);
    91 		}
    92 	}
    93 
    94 TDllData::TDllData()
    95 	{
    96 	CDFlag|=1;
    97 	if (CDFlag==3)
    98 		AddToCDList();
    99 #ifndef __DLL_IN_CYCLE
   100 	TInt r;
   101 	CHKDEPS(r);		// Check our dependencies are initialised
   102 	if (r!=KErrNone)
   103 		User::Panic(_L("CHKDEPS"),r);
   104 #endif
   105 	iStartTime.HomeTime();
   106 	iTest1=299792458;
   107 	Dll::FileName(iFileName);
   108 #ifdef __DLL_LINK_TO_EXE
   109 	RegisterConstructorCall(DLLNUM);
   110 #endif
   111 	}
   112 
   113 TDllData::~TDllData()
   114 	{
   115 	CDFlag|=4;
   116 	if (CDFlag==15)
   117 		AddToCDList();
   118 #ifdef __DLL_LINK_TO_EXE
   119 	RegisterDestructorCall(DLLNUM);
   120 #endif
   121 	iLib.Close();
   122 	}
   123 
   124 TDllData2::TDllData2()
   125 	{
   126 	CDFlag|=2;
   127 	if (CDFlag==3)
   128 		AddToCDList();
   129 	iTest2=DLLNUM^0x3bb;
   130 	iTest2*=iTest2;
   131 	}
   132 
   133 TDllData2::~TDllData2()
   134 	{
   135 	CDFlag|=8;
   136 	if (CDFlag==15)
   137 		AddToCDList();
   138 	}
   139 #endif
   140 
   141 #ifdef __MODULE_HAS_DATA
   142 void RecordInitCall()
   143 	{
   144 	TheDllDataObject.iInitTime.HomeTime();
   145 	}
   146 #endif
   147 
   148 extern "C" __MODULE_EXPORT TInt INITFUNC(MDllList& aList)
   149 	{
   150 	TInt r=KErrNone;
   151 	SDllInfo info;
   152 	GetDllInfo(info);
   153 	if (!aList.IsPresent(info))
   154 		{
   155 		TInt pos=aList.Add(info);
   156 		INITDEPS(r,aList);		// Call Init on our dependencies
   157 		aList.MoveToEnd(pos);
   158 #ifdef __MODULE_HAS_DATA
   159 		if (r==KErrNone)
   160 			r=CHKCFUNC();		// Check initial values for .data/.bss and check constructors have been called
   161 		if (r==KErrNone)
   162 			RecordInitCall();
   163 #endif
   164 #ifdef __DLL_LINK_TO_EXE
   165 		RegisterInitCall(DLLNUM);
   166 #endif
   167 		}
   168 	return r;
   169 	}
   170 
   171 extern "C" __MODULE_EXPORT TInt CHKCFUNC()
   172 	{
   173 #ifdef __MODULE_HAS_DATA
   174 	TInt i;
   175 	TInt x=0;
   176 	for (i=0; i<16; ++i) x|=Bss[i];
   177 	if (x)
   178 		return 0x425353;
   179 	if (DllNum!=DLLNUM)
   180 		return 0x44415441;
   181 	if (TheDllDataObject.iTest1!=299792458)
   182 		return 0x54455354;
   183 	x=DLLNUM^0x3bb;
   184 	x*=x;
   185 	if (x!=TheDllDataObject2.iTest2)
   186 		return 0x54535432;
   187 	TInt init_mark=~((DLLNUM+DLLNUMOFFSET)*(DLLNUM+DLLNUMOFFSET));
   188 	if (InitFlag==init_mark)
   189 		return KErrNone;
   190 	if (InitFlag!=0)
   191 		return 0x494e4946;
   192 	if (Generation!=0)
   193 		return 0x47454e;
   194 	if (StartProcess!=RProcess().Name())
   195 		return 0x535450;
   196 	TFileName fn;
   197 	Dll::FileName(fn);
   198 	if (fn!=TheDllDataObject.iFileName)
   199 		return 0x464e414d;
   200 	InitFlag=init_mark;
   201 	RDebug::Print(_L("ChkC %S OK"),&fn);
   202 #endif
   203 	return KErrNone;
   204 	}
   205 
   206 extern "C" __MODULE_EXPORT TInt GetGeneration()
   207 	{
   208 #ifdef __MODULE_HAS_DATA
   209 	return Generation;
   210 #else
   211 	return 0;
   212 #endif
   213 	}
   214 
   215 extern "C" __MODULE_EXPORT TInt RBLKIFUNC(TInt aInput, TInt aGeneration)
   216 	{
   217 	(void)aGeneration;
   218 #ifdef __MODULE_HAS_DATA
   219 	TInt r=aInput;
   220 	if (aGeneration!=Generation)
   221 		{
   222 		Generation=aGeneration;
   223 		r=BLKIFUNC(aInput);
   224 		RBLKIFUNC_DEPS(r,aGeneration);
   225 		}
   226 	return r;
   227 #else
   228 	return aInput;
   229 #endif
   230 	}
   231 
   232 extern "C" __MODULE_EXPORT void SetCloseLib(TInt aLibHandle)
   233 	{
   234 	(void)aLibHandle;
   235 #ifdef __MODULE_HAS_DATA
   236 	TheDllDataObject.iLib.SetHandle(aLibHandle);
   237 #endif
   238 	}