1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=============================================================
6**
7** Source: virtualprotect.c
8**
9** Purpose: Positive test the VirtualProtect API.
10** Call VirtualProtect to set new protect as
11** PAGE_NOACCESS
12**
13**
14**============================================================*/
15#include <palsuite.h>
16
17#define REGIONSIZE 1024
18
19int __cdecl main(int argc, char *argv[])
20{
21 int err;
22 LPVOID lpVirtualAddress;
23 DWORD OldProtect;
24
25 //Initialize the PAL environment
26 err = PAL_Initialize(argc, argv);
27 if(0 != err)
28 {
29 ExitProcess(FAIL);
30 }
31 //Allocate the physical storage in memory or in the paging file on disk
32 lpVirtualAddress = VirtualAlloc(NULL,//determine where to allocate the region
33 REGIONSIZE, //specify the size
34 MEM_COMMIT, //allocation type
35 PAGE_READONLY); //access protection
36 if(NULL == lpVirtualAddress)
37 {
38 Fail("\nFailed to call VirtualAlloc API!\n");
39 }
40
41 OldProtect = PAGE_READONLY;
42 //Set new access protection
43 err = VirtualProtect(lpVirtualAddress,
44 REGIONSIZE, //specify the region size
45 PAGE_NOACCESS,//desied access protection
46 &OldProtect);//old access protection
47 if(0 == err)
48 {
49 Trace("\nFailed to call VirtualProtect API!\n");
50 err = VirtualFree(lpVirtualAddress,REGIONSIZE,MEM_DECOMMIT);
51 if(0 == err)
52 {
53 Fail("\nFailed to call VirtualFree API!\n");
54 }
55 Fail("");
56 }
57
58
59 //decommit the specified region
60 err = VirtualFree(lpVirtualAddress,REGIONSIZE,MEM_DECOMMIT);
61 if(0 == err)
62 {
63 Fail("\nFailed to call VirtualFree API!\n");
64 }
65
66 PAL_Terminate();
67 return PASS;
68}
69