os/kernelhwsrv/kerneltest/e32test/dll/t_tdll12.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) 1995-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\dll\t_tdll12.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test DLL Thread Local Storage data and DLL Global data access.
sl@0
    17
// API Information:
sl@0
    18
// Dll
sl@0
    19
// Details:
sl@0
    20
// - Test that the local storage of two different DLLs, when accessed from
sl@0
    21
// two different threads is unique. Verify that results are as expected.
sl@0
    22
// - Test the access of DLL Global data including Alloc, Read and Write. Test
sl@0
    23
// the protection of the global data. Verify results are as expected.
sl@0
    24
// Platforms/Drives/Compatibility:
sl@0
    25
// All.
sl@0
    26
// Assumptions/Requirement/Pre-requisites:
sl@0
    27
// Failures and causes:
sl@0
    28
// Base Port information:
sl@0
    29
// 
sl@0
    30
//
sl@0
    31
sl@0
    32
#include "t_dll.h"
sl@0
    33
#include "../mmu/mmudetect.h"
sl@0
    34
sl@0
    35
const TInt KHeapSize=0x2000;
sl@0
    36
sl@0
    37
LOCAL_D RTest test(_L("T_TDLL12"));
sl@0
    38
sl@0
    39
TBool KernProt=EFalse;
sl@0
    40
TUint8* Kern;
sl@0
    41
TUint8* Garbage;
sl@0
    42
sl@0
    43
void SetupAddresses()
sl@0
    44
	{
sl@0
    45
	KernProt=HaveDirectKernProt();
sl@0
    46
	Kern=KernData();
sl@0
    47
	TUint32 mm_attr=MemModelAttributes();
sl@0
    48
	TUint32 mm_type=mm_attr & EMemModelTypeMask;
sl@0
    49
	switch (mm_type)
sl@0
    50
		{
sl@0
    51
		case EMemModelTypeDirect:
sl@0
    52
			Garbage=(TUint8*)0xa8000000;
sl@0
    53
			break;
sl@0
    54
		case EMemModelTypeMoving:
sl@0
    55
			Garbage=(TUint8*)0x60f00000;
sl@0
    56
			break;
sl@0
    57
		case EMemModelTypeMultiple:
sl@0
    58
			Garbage=(TUint8*)0xfe000000;
sl@0
    59
			break;
sl@0
    60
		case EMemModelTypeFlexible:
sl@0
    61
			Garbage=(TUint8*)0x8ff00000;
sl@0
    62
			break;
sl@0
    63
		case EMemModelTypeEmul:
sl@0
    64
			Garbage=(TUint8*)0xf0000000;
sl@0
    65
			break;
sl@0
    66
		default:
sl@0
    67
			test(0);
sl@0
    68
			break;
sl@0
    69
		}
sl@0
    70
	}
sl@0
    71
sl@0
    72
void RunTestInThread(TThreadFunction aFn, TAny* aParameter, const TDesC* aPanicCat, TInt aExitCode)
sl@0
    73
	{
sl@0
    74
	RThread t;
sl@0
    75
	TInt r=t.Create(KNullDesC(),aFn,0x2000,NULL,aParameter);
sl@0
    76
	test(r==KErrNone);
sl@0
    77
	TRequestStatus s;
sl@0
    78
	t.Logon(s);
sl@0
    79
	t.Resume();
sl@0
    80
	User::WaitForRequest(s);
sl@0
    81
	if (aPanicCat)
sl@0
    82
		{
sl@0
    83
		test(t.ExitType()==EExitPanic);
sl@0
    84
		test(t.ExitCategory()==*aPanicCat);
sl@0
    85
		test(t.ExitReason()==aExitCode);
sl@0
    86
		}
sl@0
    87
	else
sl@0
    88
		{
sl@0
    89
		test(t.ExitType()==EExitKill);
sl@0
    90
		test(t.ExitReason()==aExitCode);
sl@0
    91
		}
sl@0
    92
	CLOSE_AND_WAIT(t);
sl@0
    93
	}
sl@0
    94
sl@0
    95
TInt GlobalReadThread(TAny* a)
sl@0
    96
	{
sl@0
    97
	return TestDll1::GlobalRead(122,*(TDes8*)a);
sl@0
    98
	}
sl@0
    99
sl@0
   100
TInt GlobalWriteThread(TAny* a)
sl@0
   101
	{
sl@0
   102
	return TestDll1::GlobalWrite(0,*(TDes8*)a);
sl@0
   103
	}
sl@0
   104
sl@0
   105
_LIT(KLitKernExec,"KERN-EXEC");
sl@0
   106
void TestProtection()
sl@0
   107
	{
sl@0
   108
	test.Next(_L("Test protection"));
sl@0
   109
	TBool jit=User::JustInTime();
sl@0
   110
	User::SetJustInTime(EFalse);
sl@0
   111
	TUint x=0xffffffff;
sl@0
   112
	TBuf8<64> ubuf;
sl@0
   113
	TPtrC8 uptrc(ubuf.Ptr(),11);
sl@0
   114
	TPtr8 uptr((TUint8*)ubuf.Ptr(),1,20);
sl@0
   115
	TPtrC8 kptrc(Kern,1);
sl@0
   116
	TPtr8 kptr(Kern,10,256);
sl@0
   117
	TPtrC8 gptrc(Garbage,1);
sl@0
   118
	TPtr8 gptr(Garbage,10,256);
sl@0
   119
	RunTestInThread(GlobalReadThread,&x,&KLitKernExec,EKUDesInfoInvalidType);
sl@0
   120
	RunTestInThread(GlobalReadThread,&ubuf,NULL,KErrNone);
sl@0
   121
	RunTestInThread(GlobalReadThread,&uptr,NULL,KErrNone);
sl@0
   122
	RunTestInThread(GlobalReadThread,&uptrc,&KLitKernExec,EKUDesInfoInvalidType);
sl@0
   123
	RunTestInThread(GlobalReadThread,&kptrc,&KLitKernExec,EKUDesInfoInvalidType);
sl@0
   124
	RunTestInThread(GlobalReadThread,&gptrc,&KLitKernExec,EKUDesInfoInvalidType);
sl@0
   125
	RunTestInThread(GlobalReadThread,&gptr,&KLitKernExec,ECausedException);
sl@0
   126
	if (KernProt)
sl@0
   127
		{
sl@0
   128
		RunTestInThread(GlobalReadThread,Kern,&KLitKernExec,ECausedException);
sl@0
   129
		RunTestInThread(GlobalReadThread,&kptr,&KLitKernExec,ECausedException);
sl@0
   130
		}
sl@0
   131
	RunTestInThread(GlobalWriteThread,&x,&KLitKernExec,EKUDesInfoInvalidType);
sl@0
   132
	RunTestInThread(GlobalWriteThread,&ubuf,NULL,KErrNone);
sl@0
   133
	RunTestInThread(GlobalWriteThread,&uptr,NULL,KErrNone);
sl@0
   134
	RunTestInThread(GlobalWriteThread,&uptrc,NULL,KErrNone);
sl@0
   135
	RunTestInThread(GlobalWriteThread,&gptrc,&KLitKernExec,ECausedException);
sl@0
   136
	RunTestInThread(GlobalWriteThread,&gptr,&KLitKernExec,ECausedException);
sl@0
   137
	if (KernProt)
sl@0
   138
		{
sl@0
   139
		RunTestInThread(GlobalWriteThread,Kern,&KLitKernExec,ECausedException);
sl@0
   140
		RunTestInThread(GlobalWriteThread,&kptrc,&KLitKernExec,ECausedException);
sl@0
   141
		RunTestInThread(GlobalWriteThread,&kptr,&KLitKernExec,ECausedException);
sl@0
   142
		}
sl@0
   143
	User::SetJustInTime(jit);
sl@0
   144
	}
sl@0
   145
sl@0
   146
LOCAL_C TInt Dll1Thread2(TAny* /*anArg*/)
sl@0
   147
//
sl@0
   148
// The entry point for thread2.
sl@0
   149
//
sl@0
   150
	{
sl@0
   151
sl@0
   152
    test(TestDll1::Attach(ETrue)==KErrNone);
sl@0
   153
	test((TUint)TestDll1::Data()==0x12345678);
sl@0
   154
	TestDll1::SetData(0xfedcba98);
sl@0
   155
	test((TUint)TestDll1::Data()==0xfedcba98);
sl@0
   156
    test(TestDll1::Attach(EFalse)==KErrNone);
sl@0
   157
	return KErrNone;
sl@0
   158
	}
sl@0
   159
	
sl@0
   160
LOCAL_C TInt Dll2Thread2(TAny* /*anArg*/)
sl@0
   161
//
sl@0
   162
// The entry point for thread2.
sl@0
   163
//
sl@0
   164
	{
sl@0
   165
sl@0
   166
    test(TestDll2::Attach(ETrue)==KErrNone);
sl@0
   167
	test((TUint)TestDll2::Data()==0xABCDABCD);
sl@0
   168
	TestDll2::SetData(0x12341234);
sl@0
   169
	test((TUint)TestDll2::Data()==0x12341234);
sl@0
   170
    test(TestDll2::Attach(EFalse)==KErrNone);
sl@0
   171
	return KErrNone;
sl@0
   172
	}
sl@0
   173
sl@0
   174
void testGlobalAlloc()
sl@0
   175
//
sl@0
   176
//
sl@0
   177
//
sl@0
   178
	{
sl@0
   179
sl@0
   180
	__KHEAP_MARK;
sl@0
   181
	test.Start(_L("Test Dll::GlobalAlloc"));
sl@0
   182
	TInt r;
sl@0
   183
	test(TestDll1::GlobalAllocated()==EFalse);
sl@0
   184
	test(TestDll2::GlobalAllocated()==EFalse);
sl@0
   185
	r=TestDll2::GlobalAlloc(0);
sl@0
   186
	test(r==KErrNone);
sl@0
   187
	r=TestDll1::GlobalAlloc(256);
sl@0
   188
	test(r==KErrNone);
sl@0
   189
	test(TestDll1::GlobalAllocated());
sl@0
   190
	test(TestDll2::GlobalAllocated()==EFalse);
sl@0
   191
	r=TestDll2::GlobalAlloc(256);
sl@0
   192
	test(r==KErrNone);
sl@0
   193
	test(TestDll1::GlobalAllocated());
sl@0
   194
	test(TestDll2::GlobalAllocated());
sl@0
   195
sl@0
   196
	test.Next(_L("Write"));
sl@0
   197
	// Write 256 bytes
sl@0
   198
	TBuf8<0x100> buf100;
sl@0
   199
	TInt i;
sl@0
   200
	buf100.SetLength(0x100);
sl@0
   201
	for (i=0; i<256; i++)
sl@0
   202
		buf100[i]=(TText8)('A'+i%26);
sl@0
   203
	r=TestDll1::GlobalWrite(0, buf100);
sl@0
   204
	test(r==KErrNone);
sl@0
   205
	buf100.Fill('X');
sl@0
   206
	r=TestDll2::GlobalWrite(0, buf100);
sl@0
   207
	test(r==KErrNone);
sl@0
   208
sl@0
   209
	test.Next(_L("Read"));
sl@0
   210
	// Read 256 bytes
sl@0
   211
	r=TestDll1::GlobalRead(0, buf100);
sl@0
   212
	test(r==KErrNone);
sl@0
   213
	for (i=0; i<256; i++)
sl@0
   214
		test(buf100[i]=='A'+i%26);
sl@0
   215
	buf100.Fill('D');
sl@0
   216
	r=TestDll2::GlobalRead(0, buf100);
sl@0
   217
	test(r==KErrNone);
sl@0
   218
	for (i=0; i<256; i++)
sl@0
   219
		test(buf100[i]=='X');
sl@0
   220
sl@0
   221
	test.Next(_L("Realloc"));
sl@0
   222
	r=TestDll1::GlobalAlloc(128);
sl@0
   223
	test(r==KErrNone);
sl@0
   224
	test(TestDll1::GlobalAllocated());
sl@0
   225
	test(TestDll2::GlobalAllocated());
sl@0
   226
	test.Next(_L("Read"));
sl@0
   227
	r=TestDll1::GlobalRead(0,buf100);
sl@0
   228
	for (i=0; i<128; i++)
sl@0
   229
		test(buf100[i]=='A'+i%26);
sl@0
   230
	test(buf100.Length()==128);
sl@0
   231
	r=TestDll2::GlobalRead(0,buf100);
sl@0
   232
	test(r==KErrNone);
sl@0
   233
	for (i=0; i<256; i++)
sl@0
   234
		test(buf100[i]=='X');
sl@0
   235
	test(buf100.Length()==256);
sl@0
   236
sl@0
   237
	test.Next(_L("Read @ pos"));
sl@0
   238
	// Read from position
sl@0
   239
	r=TestDll1::GlobalRead(1, buf100);
sl@0
   240
	test(r==KErrNone);
sl@0
   241
	test(buf100.Length()==127);
sl@0
   242
	for (i=0; i<127; i++)
sl@0
   243
		test(buf100[i]=='A'+(i+1)%26);
sl@0
   244
	test.Next(_L("Write @ pos"));
sl@0
   245
	buf100=_L8("LALALALALALA");
sl@0
   246
	r=TestDll1::GlobalWrite(5, buf100);
sl@0
   247
	test(r==KErrNone);
sl@0
   248
	buf100=_L8("POPOPOPOPO");
sl@0
   249
	r=TestDll2::GlobalWrite(4, buf100);
sl@0
   250
	test(r==KErrNone);
sl@0
   251
	r=TestDll1::GlobalRead(0, buf100);
sl@0
   252
	buf100.SetLength(20);
sl@0
   253
	test(buf100==_L8("ABCDELALALALALALARST"));
sl@0
   254
	r=TestDll2::GlobalRead(0, buf100);
sl@0
   255
	buf100.SetLength(20);
sl@0
   256
	test(buf100==_L8("XXXXPOPOPOPOPOXXXXXX"));
sl@0
   257
sl@0
   258
	TestProtection();
sl@0
   259
sl@0
   260
	test.Next(_L("Free Global Alloc"));
sl@0
   261
	r=TestDll1::GlobalAlloc(0);
sl@0
   262
	test(r==KErrNone);
sl@0
   263
	test(TestDll1::GlobalAllocated()==EFalse);
sl@0
   264
	test(TestDll2::GlobalAllocated());
sl@0
   265
sl@0
   266
	r=TestDll2::GlobalWrite(0, _L8("WEEEEEEEEEE"));
sl@0
   267
	test(r==KErrNone);
sl@0
   268
	r=TestDll2::GlobalRead(0, buf100);
sl@0
   269
	buf100.SetLength(11);
sl@0
   270
	test(buf100==_L8("WEEEEEEEEEE"));
sl@0
   271
	r=TestDll1::GlobalAlloc(0);
sl@0
   272
	test(r==KErrNone);
sl@0
   273
	r=TestDll2::GlobalAlloc(0);
sl@0
   274
	test(r==KErrNone);
sl@0
   275
	test(TestDll1::GlobalAllocated()==EFalse);
sl@0
   276
	test(TestDll2::GlobalAllocated()==EFalse);
sl@0
   277
	__KHEAP_MARKEND;
sl@0
   278
	test.End();
sl@0
   279
	}
sl@0
   280
sl@0
   281
GLDEF_C TInt E32Main()
sl@0
   282
//
sl@0
   283
// Test DLL Thread Local Storage data.
sl@0
   284
//
sl@0
   285
    {
sl@0
   286
sl@0
   287
	test.Title();
sl@0
   288
	SetupAddresses();
sl@0
   289
//
sl@0
   290
	test.Start(_L("Dll1 Thread 1"));
sl@0
   291
    test(TestDll1::Attach(ETrue)==KErrNone);
sl@0
   292
	test((TUint)TestDll1::Data()==0x12345678);
sl@0
   293
	TestDll1::SetData(0x87654321);
sl@0
   294
	test((TUint)TestDll1::Data()==0x87654321);
sl@0
   295
//
sl@0
   296
	test.Next(_L("Dll1 Thread 2"));
sl@0
   297
	RThread t;
sl@0
   298
	TInt r=t.Create(_L("Dll1 Thread2"),Dll1Thread2,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   299
	test(r==KErrNone);
sl@0
   300
	TRequestStatus tStat;
sl@0
   301
	t.Logon(tStat);
sl@0
   302
	test(tStat==KRequestPending);
sl@0
   303
	t.Resume();
sl@0
   304
	User::WaitForRequest(tStat);
sl@0
   305
	test(tStat==KErrNone);
sl@0
   306
//
sl@0
   307
	test.Next(_L("Dll1 Thread 1 again"));
sl@0
   308
	test((TUint)TestDll1::Data()==0x87654321);
sl@0
   309
	TestDll1::SetData(0x12345678);
sl@0
   310
	test((TUint)TestDll1::Data()==0x12345678);
sl@0
   311
//
sl@0
   312
    test(TestDll1::Attach(EFalse)==KErrNone);
sl@0
   313
//
sl@0
   314
	test.Next(_L("Dll2 Thread 1"));
sl@0
   315
    test(TestDll2::Attach(ETrue)==KErrNone);
sl@0
   316
	test((TUint)TestDll2::Data()==0xABCDABCD);
sl@0
   317
	TestDll2::SetData(0xDCBADCBA);
sl@0
   318
	test((TUint)TestDll2::Data()==0xDCBADCBA);
sl@0
   319
//
sl@0
   320
	test.Next(_L("Dll2 Thread 2"));
sl@0
   321
	r=t.Create(_L("Dll2 Thread2"),Dll2Thread2,KDefaultStackSize,KHeapSize,KHeapSize,NULL);
sl@0
   322
	test(r==KErrNone);
sl@0
   323
	t.Logon(tStat);
sl@0
   324
	test(tStat==KRequestPending);
sl@0
   325
	t.Resume();
sl@0
   326
	User::WaitForRequest(tStat);
sl@0
   327
	test(tStat==KErrNone);
sl@0
   328
//
sl@0
   329
	test.Next(_L("Dll2 Thread 1 again"));
sl@0
   330
	test((TUint)TestDll2::Data()==0xDCBADCBA);
sl@0
   331
	TestDll2::SetData(0xABCDABCD);
sl@0
   332
	test((TUint)TestDll2::Data()==0xABCDABCD);
sl@0
   333
//
sl@0
   334
    test(TestDll2::Attach(EFalse)==KErrNone);
sl@0
   335
//
sl@0
   336
	test.Next(_L("Dll1 Thread 1"));
sl@0
   337
    test(TestDll1::Attach(ETrue)==KErrNone);
sl@0
   338
	test((TUint)TestDll1::Data()==0x12345678);
sl@0
   339
	TestDll1::SetData(0x87654321);
sl@0
   340
	test((TUint)TestDll1::Data()==0x87654321);
sl@0
   341
//
sl@0
   342
	test.Next(_L("Dll2 Thread 1"));
sl@0
   343
    test(TestDll2::Attach(ETrue)==KErrNone);
sl@0
   344
	test((TUint)TestDll2::Data()==0xABCDABCD);
sl@0
   345
	TestDll2::SetData(0xDCBADCBA);
sl@0
   346
	test((TUint)TestDll2::Data()==0xDCBADCBA);
sl@0
   347
//
sl@0
   348
	test((TUint)TestDll1::Data()==0x87654321);
sl@0
   349
	TestDll1::SetData(0x12345678);
sl@0
   350
	test((TUint)TestDll1::Data()==0x12345678);
sl@0
   351
//
sl@0
   352
	test((TUint)TestDll2::Data()==0xDCBADCBA);
sl@0
   353
	TestDll2::SetData(0xABCDABCD);
sl@0
   354
	test((TUint)TestDll2::Data()==0xABCDABCD);
sl@0
   355
//
sl@0
   356
    test(TestDll1::Attach(EFalse)==KErrNone);
sl@0
   357
    test(TestDll2::Attach(EFalse)==KErrNone);
sl@0
   358
//
sl@0
   359
sl@0
   360
	test.End();
sl@0
   361
	return(0);
sl@0
   362
    }
sl@0
   363