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_EXECUTE_READ |
12 | ** |
13 | ** |
14 | **============================================================*/ |
15 | #include <palsuite.h> |
16 | |
17 | #define REGIONSIZE 1024 |
18 | |
19 | int __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 | |
32 | //Allocate the physical storage in memory or in the paging file on disk |
33 | lpVirtualAddress = VirtualAlloc(NULL,//determine where to allocate the region |
34 | REGIONSIZE, //specify the size |
35 | MEM_COMMIT, //allocation type |
36 | PAGE_READONLY); //access protection |
37 | if(NULL == lpVirtualAddress) |
38 | { |
39 | Fail("\nFailed to call VirtualAlloc API!\n" ); |
40 | } |
41 | |
42 | OldProtect = PAGE_READONLY; |
43 | //Set new access protection |
44 | err = VirtualProtect(lpVirtualAddress, |
45 | REGIONSIZE, //specify the region size |
46 | PAGE_EXECUTE_READ,//desied access protection |
47 | &OldProtect);//old access protection |
48 | if(0 == err) |
49 | { |
50 | Trace("\nFailed to call VirtualProtect API!\n" ); |
51 | err = VirtualFree(lpVirtualAddress,REGIONSIZE,MEM_DECOMMIT); |
52 | if(0 == err) |
53 | { |
54 | Fail("\nFailed to call VirtualFree API!\n" ); |
55 | } |
56 | Fail("" ); |
57 | } |
58 | |
59 | |
60 | //decommit the specified region |
61 | err = VirtualFree(lpVirtualAddress,REGIONSIZE,MEM_DECOMMIT); |
62 | if(0 == err) |
63 | { |
64 | Fail("\nFailed to call VirtualFree API!\n" ); |
65 | } |
66 | |
67 | PAL_Terminate(); |
68 | return PASS; |
69 | } |
70 | |