summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/filemapping_memmgt/FreeLibraryAndExitThread/test1/test1.c
blob: 6aacfc83b48babac2ed90e9a6200949557bfc1a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
** 
** Source:  test1.c (FreeLibraryAndExitThread)
**
** Purpose: Tests the PAL implementation of the FreeLibraryAndExitThread
**          function. FreeLibraryAndExitThread when run will exit the 
**          process that it is called within, therefore we create a
**          thread to run the API. Then test for the existance of the
**          thread and access to the library.
**
**
**===================================================================*/

#include <palsuite.h>

/*Define platform specific information*/

/* SHLEXT is defined only for Unix variants */
#if defined(SHLEXT)
#define LibraryName    "dlltest"SHLEXT
#else
#define LibraryName    "dlltest"
#endif

#define TIMEOUT 60000

BOOL  PALAPI StartThreadTest();
DWORD PALAPI CreateTestThread(LPVOID);
BOOL  PALAPI TestDll(HMODULE, int);

int __cdecl main(int argc, char* argv[])
{
    /*Initialize the PAL*/
    if ((PAL_Initialize(argc, argv)) != 0)
    {
        return (FAIL);
    }

    if (!StartThreadTest())
    {
        Fail("ERROR: FreeLibraryAndExitThread test failed.\n");
    }

    /*Terminate the PAL*/
    PAL_Terminate();
    return PASS;

}


BOOL  PALAPI StartThreadTest()
{
    HMODULE hLib;
    HANDLE  hThread;  
    DWORD   dwThreadId;
    LPTHREAD_START_ROUTINE lpStartAddress =  &CreateTestThread;
    LPVOID lpParameter = lpStartAddress;
    DWORD rc = -1;
    /*Load library (DLL).*/
    hLib = LoadLibrary(LibraryName);
    if(hLib == NULL)
    {
        Trace("ERROR: Unable to load library %s\n", LibraryName);
        
        return (FALSE);
    }

    /*Start the test thread*/
    hThread = CreateThread(NULL, 
                            (DWORD)0,
                            lpParameter,
                            hLib,
                            (DWORD)NULL,
                            &dwThreadId);
    if(hThread == NULL)
    {
        Trace("ERROR:%u: Unable to create thread.\n",
                GetLastError());

        FreeLibrary(hLib);
        return (FALSE);
    }

    /*Wait on thread.*/
    rc = WaitForSingleObject(hThread, TIMEOUT);
    if( rc != WAIT_OBJECT_0 )
    {
        Trace("ERROR:%u: hThread=0x%4.4lx not exited by "
            "FreeLibraryAndExitThread, RC[%d]\n",
            GetLastError(),  
            hThread, rc);

// There is a possibility that the other thread might 
// still be using the library VSW:337893
//        FreeLibrary(hLib);
        CloseHandle(hThread);
        return (FALSE);
    }
            
    /*Test access to DLL.*/
    if(!TestDll(hLib, 0))
    {
        Trace("ERROR: TestDll function returned FALSE "
            "expected TRUE\n.");
        
        CloseHandle(hThread);
        return (FALSE);
    }

    FreeLibrary(hLib);
    /*Clean-up thread.*/
    CloseHandle(hThread);

    return (TRUE);
}

BOOL PALAPI TestDll(HMODULE hLib, int testResult)
{
    int     RetVal;
    char    FunctName[] = "DllTest";
    FARPROC DllAddr;    

    /* Attempt to grab the proc address of the dll function.
     * This one should succeed.*/
    if(testResult == 1)
    {
        DllAddr = GetProcAddress(hLib, FunctName);
        if(DllAddr == NULL)
        {
            Trace("ERROR: Unable to load function \"%s\" library \"%s\"\n", 
                    FunctName,
                    LibraryName);
            return (FALSE);
        }
        /* Run the function in the DLL, 
         * to ensure that the DLL was loaded properly.*/
        RetVal = DllAddr();
        if (RetVal != 1)
        {
            Trace("ERROR: Unable to receive correct information from DLL! "
                ":expected \"1\", returned \"%d\"\n",
                RetVal);
            return (FALSE);
        }
    }

    /* Attempt to grab the proc address of the dll function.
     * This one should fail.*/
    if(testResult == 0)
    {
        DllAddr = GetProcAddress(hLib, FunctName);
        if(DllAddr != NULL)
        {
            Trace("ERROR: Able to load function \"%s\" from free'd"
                " library \"%s\"\n", 
                FunctName, 
                LibraryName);
            return (FALSE);
        }
    }
    return (TRUE);
}

DWORD PALAPI CreateTestThread(LPVOID lpParam)
{
    /* Test access to DLL.*/
    TestDll(lpParam, 1);

    /*Free library and exit thread.*/
    FreeLibraryAndExitThread(lpParam, (DWORD)0);

    /* NOT REACHED */

    /*Infinite loop, we should not get here.*/
    while(1);

    return (DWORD)0;
}