os/graphics/graphicstest/graphicstestharness/inc/singletontestexithelper.inl
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2010 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 "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
// Test shutdown cleanup functions that should be called to destroy singletons
sl@0
    15
// before the heap is destroyed, in particular when using TEF steps.
sl@0
    16
// These methods are implemented and called from Window Server - WServ, CWsTop
sl@0
    17
// but this copy can be called by any low-level unit tests that bypass WServ   
sl@0
    18
// but need to provide the same clean-up behaviour.
sl@0
    19
// @test
sl@0
    20
// @internalTechnology
sl@0
    21
// 
sl@0
    22
// Currently, three things are cleaned up:
sl@0
    23
// 1) Singleton API mutex in OpenWF implementation
sl@0
    24
// 2) Singleton Find/Acquire mutext in Native Stream implementation
sl@0
    25
// 3) EGL thread status 
sl@0
    26
//
sl@0
    27
// Note that while these cleanups are only required by OWF-C implementations,
sl@0
    28
// the methods called should be harmless if called 
sl@0
    29
// when some other composition solution is in action.
sl@0
    30
sl@0
    31
#include <e32property.h>
sl@0
    32
#include <EGL/egl.h>
sl@0
    33
sl@0
    34
// IDs of p&s properties that optionally contain onexit callbacks
sl@0
    35
// that may be used to release singletons owned by libraries at shutdown in order to make 
sl@0
    36
// the memory leak tests work.
sl@0
    37
// By convention, the ID is the same as the UID3 of the libary.
sl@0
    38
static TBool gReleaseSingletonsOnExit = EFalse;
sl@0
    39
static const TUid KOpenWfcImplCleanupKey = {0x10286FC4};     
sl@0
    40
static const TUid KOpenWfcInteropCleanupKey = {0x10286FC5};
sl@0
    41
sl@0
    42
sl@0
    43
static void DefineOwfSingletonKey(const TUid& aSingletonKey)
sl@0
    44
/**
sl@0
    45
 * Defines a new property for a singleton key. WServ must only process 
sl@0
    46
 * singleton keys that it created to prevent a malicious process with the 
sl@0
    47
 * WriteDeviceData capability causing arbitrary functions to be executed.
sl@0
    48
 * 
sl@0
    49
 * @param   aSingeltonKey   The UID of the singleton key to define.
sl@0
    50
 */
sl@0
    51
    {
sl@0
    52
    RThread t;      
sl@0
    53
    TUid category = { t.SecureId().iId };
sl@0
    54
    RProperty prop;
sl@0
    55
    
sl@0
    56
    // Write access is restricted to THIS process
sl@0
    57
    TInt err = prop.Define( category, aSingletonKey.iUid, 
sl@0
    58
            RProperty::EByteArray, TSecurityPolicy( t.SecureId() ), 
sl@0
    59
            TSecurityPolicy( t.SecureId() ), sizeof( TCallBack ) );
sl@0
    60
    
sl@0
    61
    if ( err == KErrNone || err == KErrAlreadyExists)
sl@0
    62
        {
sl@0
    63
        TCallBack cb( NULL, NULL );
sl@0
    64
        TPckgC<TCallBack> cbPckg( cb );
sl@0
    65
        
sl@0
    66
        // Any error should cause the properties to be ignored
sl@0
    67
        err = prop.Set( category, aSingletonKey.iUid, cbPckg );    
sl@0
    68
        }      
sl@0
    69
    //We presume that if property already exists it was previously set by this test code.
sl@0
    70
    if ( err != KErrNone )
sl@0
    71
        {
sl@0
    72
        // A problem occured / the property already existed so for safety
sl@0
    73
        // the release code should be skipped.
sl@0
    74
        gReleaseSingletonsOnExit = EFalse;
sl@0
    75
        }
sl@0
    76
    
sl@0
    77
    prop.Close();
sl@0
    78
    t.Close();
sl@0
    79
    }
sl@0
    80
#define DefineOwfSingletonKeys DefineOwfSingletonKeys
sl@0
    81
/** Call this method before starting the compositor.    
sl@0
    82
 * 
sl@0
    83
 */
sl@0
    84
static void DefineOwfSingletonKeys()
sl@0
    85
    {
sl@0
    86
    // Define properties for singleton callbacks. This must only be done ONCE
sl@0
    87
    // to ensure the properties can't be hijacked.
sl@0
    88
    gReleaseSingletonsOnExit = ETrue;
sl@0
    89
    DefineOwfSingletonKey(KOpenWfcInteropCleanupKey);
sl@0
    90
    DefineOwfSingletonKey(KOpenWfcImplCleanupKey);
sl@0
    91
    }
sl@0
    92
sl@0
    93
static void DeleteOwfSingleton( const TUid& aSingletonKey )
sl@0
    94
/**
sl@0
    95
 * Deletes a singleton object that was created on WServ's main heap.
sl@0
    96
 * 
sl@0
    97
 * @pre     The ws plugins have not been unloaded.
sl@0
    98
 * @param   aSingletonKey   The UID of the singleton which correponds to an
sl@0
    99
 *                          RProperty within WServ's category.                       
sl@0
   100
 */
sl@0
   101
    {   
sl@0
   102
    if ( gReleaseSingletonsOnExit )
sl@0
   103
        {
sl@0
   104
        RThread t;
sl@0
   105
        TPckgBuf<TCallBack> cb;
sl@0
   106
        RProperty prop; 
sl@0
   107
        TInt err = prop.Get(TUid::Uid(t.SecureId().iId), aSingletonKey.iUid, cb);
sl@0
   108
        if (err == KErrNone && cb.Length() == sizeof(TCallBack) && 
sl@0
   109
                cb().iFunction && cb().iPtr == &User::Heap())
sl@0
   110
            {
sl@0
   111
            // Callback is only invoked if the heap for the singleton was the 
sl@0
   112
            // WServ heap because the WServ memory leak tests only check this
sl@0
   113
            // heap.
sl@0
   114
            cb().CallBack();
sl@0
   115
            }
sl@0
   116
        // Errors are ignored because the purpose of this function is to free
sl@0
   117
        // singletons in order top make memory leak checks pass.
sl@0
   118
        prop.Close();
sl@0
   119
        t.Close();
sl@0
   120
        }
sl@0
   121
    }
sl@0
   122
/** Call this method to destroy OWF-C singletons on shut down
sl@0
   123
 * 
sl@0
   124
 */
sl@0
   125
#define DeleteOwfSingletons DeleteOwfSingletons
sl@0
   126
static void DeleteOwfSingletons()
sl@0
   127
    {
sl@0
   128
    // Free singletons on WServ heap created by libraries. Must be called
sl@0
   129
    // BEFORE iPluginManager is deleted otherwise the library code could have
sl@0
   130
    // been unloaded.
sl@0
   131
    DeleteOwfSingleton(KOpenWfcImplCleanupKey);
sl@0
   132
    DeleteOwfSingleton(KOpenWfcInteropCleanupKey);
sl@0
   133
    /* Release any use of EGL by this thread. */
sl@0
   134
    eglReleaseThread();
sl@0
   135
    }
sl@0
   136