summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/miscellaneous/IsBadReadPtr/test1/test.cpp
blob: 24b7ceb7e6a2b6da90fde06065d15e29141852dd (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
// 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: test.c
**
** Purpose: IsBadReadPtr() function
**
**
**=========================================================*/

#include <palsuite.h>

#define MEMORY_AMOUNT 16

int __cdecl main(int argc, char *argv[]) 
{
    LPVOID TestingPointer = NULL;
    BOOL ResultValue = 0;
  
    /*
     * Initialize the PAL and return FAILURE if this fails
     */

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
  
    TestingPointer = malloc(MEMORY_AMOUNT); 
    if ( TestingPointer == NULL )
    {
        Fail("ERROR: Failed to allocate memory for TestingPointer pointer. "
           "Can't properly exec test case without this.\n");
    }
 

    /* This should be readable, and return 0 */
    ResultValue = IsBadReadPtr(TestingPointer,MEMORY_AMOUNT);
    if(ResultValue != 0) 
    {
        free(TestingPointer);

        Fail("ERROR: The function returned %d instead of 0, when pointing "
             "at readable memory.\n",ResultValue);    
    }

    /* If we pass 0, the result should be 0 as well */
    ResultValue = IsBadReadPtr(TestingPointer,0);
    if(ResultValue != 0) 
    {
        free(TestingPointer);

        Fail("ERROR: The function returned %d instead of 0, when the "
             "function was passed a range of 0 bytes.\n",ResultValue);
    }
    free(TestingPointer); /* we are done with this */

    /* create a READABLE address */
    TestingPointer =  VirtualAlloc(
        NULL,               /* system selects address */
        80,                 /* size of allocation*/
        MEM_COMMIT,         /* commit */
        PAGE_READONLY);     /* protection = read only */

    if (TestingPointer == NULL )
    {
        Fail("ERROR: call to VirtualAlloc failed\n");
    }

    ResultValue = IsBadReadPtr(TestingPointer,16);
    if(ResultValue != 0)    /* if no access */
    {
        if(!VirtualFree(TestingPointer, 0, MEM_RELEASE))
        {
            Trace("ERROR: Call to VirtualFree failed with error"
                " code[ %u ]\n",GetLastError());  
        }

        Fail("ERROR: The function returned %d instead of 1 when checking "
            "on unreadable memory.\n",ResultValue);
    }

    if(!VirtualFree(TestingPointer,0, MEM_RELEASE))
    {
        Fail("ERROR: Call to VirtualFree failed with error"
            " code[ %u ]\n",GetLastError());  
    }

    /* create an unreadable address */
    TestingPointer =  VirtualAlloc(
        NULL,                 /* system selects address */
        80,                   /* size of allocation */
        MEM_COMMIT,           /* commit */
        PAGE_NOACCESS);       /* protection = no access */

    if (TestingPointer == NULL )
    {
        Fail("ERROR: call to VirtualAlloc failed\n");
    }

    ResultValue = IsBadReadPtr(TestingPointer,16);

    if(ResultValue == 0) /* if access */ 
    {
        if(!VirtualFree(TestingPointer, 0, MEM_RELEASE))
        {
            Trace("ERROR: Call to VirtualFree failed with error"
                " code[ %u ]\n",GetLastError());  
        }

        Fail("ERROR: The function returned %d instead of 1 when checking "
             "on unreadable memory.\n",ResultValue);
    }

    if(!VirtualFree(TestingPointer,0, MEM_RELEASE))
    {
        Fail("ERROR: Call to VirtualFree failed with error"
            " code[ %u ]\n",GetLastError());  
    }


    /* This should be unreadable and return 1 */
    ResultValue = IsBadReadPtr(NULL,16);
    if(ResultValue != 1) 
    {
        Fail("ERROR: The function returned %d instead of 1 when checking "
             "to see if NULL was readable.\n",ResultValue);
    }

    PAL_Terminate();
    return PASS;
}