1//===- Win32/Memory.cpp - Win32 Memory Implementation -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the Win32 specific implementation of various Memory
11// management utilities
12//
13//===----------------------------------------------------------------------===//
14
15#include "Windows.h"
16#include "llvm/Support/DataTypes.h"
17#include "llvm/Support/Process.h"
18
19namespace llvm {
20using namespace sys;
21
22//===----------------------------------------------------------------------===//
23//=== WARNING: Implementation here must contain only Win32 specific code
24//===          and must not be UNIX code
25//===----------------------------------------------------------------------===//
26
27MemoryBlock Memory::AllocateRWX(size_t NumBytes,
28                                const MemoryBlock *NearBlock,
29                                std::string *ErrMsg) {
30  if (NumBytes == 0) return MemoryBlock();
31
32  static const size_t pageSize = Process::GetPageSize();
33  size_t NumPages = (NumBytes+pageSize-1)/pageSize;
34
35  PVOID start = NearBlock ? static_cast<unsigned char *>(NearBlock->base()) +
36                                NearBlock->size() : NULL;
37
38  void *pa = VirtualAlloc(start, NumPages*pageSize, MEM_RESERVE | MEM_COMMIT,
39                  PAGE_EXECUTE_READWRITE);
40  if (pa == NULL) {
41    if (NearBlock) {
42      // Try again without the NearBlock hint
43      return AllocateRWX(NumBytes, NULL, ErrMsg);
44    }
45    MakeErrMsg(ErrMsg, "Can't allocate RWX Memory: ");
46    return MemoryBlock();
47  }
48
49  MemoryBlock result;
50  result.Address = pa;
51  result.Size = NumPages*pageSize;
52  return result;
53}
54
55bool Memory::ReleaseRWX(MemoryBlock &M, std::string *ErrMsg) {
56  if (M.Address == 0 || M.Size == 0) return false;
57  if (!VirtualFree(M.Address, 0, MEM_RELEASE))
58    return MakeErrMsg(ErrMsg, "Can't release RWX Memory: ");
59  return false;
60}
61
62static DWORD getProtection(const void *addr) {
63  MEMORY_BASIC_INFORMATION info;
64  if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
65    return info.Protect;
66  }
67  return 0;
68}
69
70bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
71  if (!setRangeWritable(M.Address, M.Size)) {
72    return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
73  }
74  return true;
75}
76
77bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
78  if (!setRangeExecutable(M.Address, M.Size)) {
79    return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
80  }
81  return true;
82}
83
84bool Memory::setRangeWritable(const void *Addr, size_t Size) {
85  DWORD prot = getProtection(Addr);
86  if (!prot)
87    return false;
88
89  if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
90    prot = PAGE_EXECUTE_READWRITE;
91  } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
92    prot = PAGE_READWRITE;
93  }
94
95  DWORD oldProt;
96  sys::Memory::InvalidateInstructionCache(Addr, Size);
97  return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
98            == TRUE;
99}
100
101bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
102  DWORD prot = getProtection(Addr);
103  if (!prot)
104    return false;
105
106  if (prot == PAGE_NOACCESS) {
107    prot = PAGE_EXECUTE;
108  } else if (prot == PAGE_READONLY) {
109    prot = PAGE_EXECUTE_READ;
110  } else if (prot == PAGE_READWRITE) {
111    prot = PAGE_EXECUTE_READWRITE;
112  }
113
114  DWORD oldProt;
115  sys::Memory::InvalidateInstructionCache(Addr, Size);
116  return ::VirtualProtect(const_cast<LPVOID>(Addr), Size, prot, &oldProt)
117            == TRUE;
118}
119
120}
121