summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/debug_api/WriteProcessMemory/test4/test4.cpp
blob: 51db23499bbf43361f9214c344dbe0a8e1c3b072 (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
// 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: test4.c
**
** Purpose: Create a child process and debug it.  When the child
** raises an exception, it sends back a memory location.  Call
** WriteProcessMemory on a restricted memory location and ensure that
** it fails.
**
**
**============================================================*/

#include <palsuite.h>
const int MY_EXCEPTION=999;

int __cdecl main(int argc, char *argv[])
{
    
    PROCESS_INFORMATION pi;
    STARTUPINFO si;
    DEBUG_EVENT DebugEv;
    DWORD dwContinueStatus = DBG_CONTINUE; 
    int Count, ret;
    char* DataBuffer[4096];
    char* Memory;

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
    
    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );
    
    memset(DataBuffer, 'z', 4096);

    /* Create a new process.  This is the process to be Debugged */
    if(!CreateProcess( NULL, "helper", NULL, NULL, 
                       FALSE, 0, NULL, NULL, &si, &pi)) 
    {
        Fail("ERROR: CreateProcess failed to load executable 'helper'.  "
             "GetLastError() returned %d.\n",GetLastError());
    }

    /* Call DebugActiveProcess, because the process wasn't created as a 
       debug process.
    */
    if(DebugActiveProcess(pi.dwProcessId) == 0)
    {
        Fail("ERROR: Failed calling DebugActiveProcess on the process "
             "which was created to debug.  GetLastError() returned %d.\n",
             GetLastError());
    }

   
    /* Call WaitForDebugEvent, which will wait until the helper process
       raises an exception.
    */
 
    while(1)
    {
        if(WaitForDebugEvent(&DebugEv, INFINITE) == 0)
        {
            Fail("ERROR: WaitForDebugEvent returned 0, indicating failure.  "
                 "GetLastError() returned %d.\n",GetLastError());
        }
        
        /* We're waiting for the helper process to send this exception.
           When it does, we call WriteProcess.  If it gets called more than
           once, it is ignored.
        */
        
        if(DebugEv.u.Exception.ExceptionRecord.ExceptionCode == MY_EXCEPTION)
        {

            Memory = (LPVOID)
                DebugEv.u.Exception.ExceptionRecord.ExceptionInformation[0];
            
            /* Write to this memory which we have no access to. */

            ret = WriteProcessMemory(pi.hProcess, 
                                     Memory,
                                     DataBuffer,
                                     4096,
                                     &Count);
            
            if(ret != 0)
            {
                Fail("ERROR: WriteProcessMemory should have failed, as "
                     "it attempted to write to a range of memory which was "
                     "not accessible.\n");
            }
            
            if(GetLastError() != ERROR_NOACCESS)
            {
                Fail("ERROR: GetLastError() should have returned "
                     "ERROR_NOACCESS , but intead it returned "
                     "%d.\n",GetLastError());
            }
        }
        
        if(DebugEv.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT)
        {
            break;
        }
        
        if(ContinueDebugEvent(DebugEv.dwProcessId, 
                              DebugEv.dwThreadId, dwContinueStatus) == 0)
        {
            Fail("ERROR: ContinueDebugEvent failed to continue the thread "
                 "which had a debug event.  GetLastError() returned %d.\n",
                 GetLastError());
        }            
    }


    PAL_Terminate();
    return PASS;
}