llvm.org GIT mirror llvm / 2dd0cfe
Support/Windows: Add support modifying memory permissions on Windows. Patch by Aaron Ballman! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141910 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
1 changed file(s) with 45 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
5353 return false;
5454 }
5555
56 static DWORD getProtection(const void *addr) {
57 MEMORY_BASIC_INFORMATION info;
58 if (sizeof(info) == ::VirtualQuery(addr, &info, sizeof(info))) {
59 return info.Protect;
60 }
61 return 0;
62 }
63
5664 bool Memory::setWritable(MemoryBlock &M, std::string *ErrMsg) {
65 if (!setRangeWritable(M.Address, M.Size)) {
66 return MakeErrMsg(ErrMsg, "Cannot set memory to writeable: ");
67 }
5768 return true;
5869 }
5970
6071 bool Memory::setExecutable(MemoryBlock &M, std::string *ErrMsg) {
61 return false;
72 if (!setRangeExecutable(M.Address, M.Size)) {
73 return MakeErrMsg(ErrMsg, "Cannot set memory to executable: ");
74 }
75 return true;
6276 }
6377
6478 bool Memory::setRangeWritable(const void *Addr, size_t Size) {
65 return true;
79 DWORD prot = getProtection(Addr);
80 if (!prot)
81 return false;
82
83 if (prot == PAGE_EXECUTE || prot == PAGE_EXECUTE_READ) {
84 prot = PAGE_EXECUTE_READWRITE;
85 } else if (prot == PAGE_NOACCESS || prot == PAGE_READONLY) {
86 prot = PAGE_READWRITE;
87 }
88
89 DWORD oldProt;
90 sys::Memory::InvalidateInstructionCache(Addr, Size);
91 return ::VirtualProtect(const_cast(Addr), Size, prot, &oldProt)
92 == TRUE;
6693 }
6794
6895 bool Memory::setRangeExecutable(const void *Addr, size_t Size) {
69 return false;
96 DWORD prot = getProtection(Addr);
97 if (!prot)
98 return false;
99
100 if (prot == PAGE_NOACCESS) {
101 prot = PAGE_EXECUTE;
102 } else if (prot == PAGE_READONLY) {
103 prot = PAGE_EXECUTE_READ;
104 } else if (prot == PAGE_READWRITE) {
105 prot = PAGE_EXECUTE_READWRITE;
106 }
107
108 DWORD oldProt;
109 sys::Memory::InvalidateInstructionCache(Addr, Size);
110 return ::VirtualProtect(const_cast(Addr), Size, prot, &oldProt)
111 == TRUE;
70112 }
71113
72114 }