1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39#ifndef MASM_EXECUTABLEALLOCATOR_H
40#define MASM_EXECUTABLEALLOCATOR_H
41
42#include <RefPtr.h>
43#include <RefCounted.h>
44#include <wtf/PageBlock.h>
45
46#include <private/qv4executableallocator_p.h>
47
48#if OS(INTEGRITY)
49#include "OSAllocator.h"
50#endif
51
52#if OS(WINDOWS)
53#include <windows.h>
54#else
55#include <sys/mman.h>
56#include <unistd.h>
57#endif
58
59#ifdef __QNXNTO__
60using std::perror;
61#endif
62
63namespace JSC {
64
65class JSGlobalData;
66
67struct ExecutableMemoryHandle : public RefCounted<ExecutableMemoryHandle> {
68 ExecutableMemoryHandle(QV4::ExecutableAllocator *allocator, size_t size)
69 : m_allocator(allocator)
70 , m_size(size)
71 {
72 m_allocation = allocator->allocate(size);
73 }
74 ~ExecutableMemoryHandle()
75 {
76 m_allocation->deallocate(allocator: m_allocator);
77 }
78
79 inline void shrink(size_t) {
80 // ### TODO.
81 }
82
83 inline bool isManaged() const { return true; }
84
85 void *memoryStart() { return m_allocation->memoryStart(); }
86 size_t memorySize() { return m_allocation->memorySize(); }
87
88 void *exceptionHandlerStart() { return m_allocation->exceptionHandlerStart(); }
89 size_t exceptionHandlerSize() { return m_allocation->exceptionHandlerSize(); }
90
91 void *codeStart() { return m_allocation->codeStart(); }
92 size_t codeSize() { return m_size; }
93
94 QV4::ExecutableAllocator::ChunkOfPages *chunk() const
95 { return m_allocator->chunkForAllocation(allocation: m_allocation); }
96
97 QV4::ExecutableAllocator *m_allocator;
98 QV4::ExecutableAllocator::Allocation *m_allocation;
99 size_t m_size;
100};
101
102struct ExecutableAllocator {
103 ExecutableAllocator(QV4::ExecutableAllocator *alloc)
104 : realAllocator(alloc)
105 {}
106
107 Ref<ExecutableMemoryHandle> allocate(JSGlobalData&, size_t size, void*, int)
108 {
109 return adoptRef(ptr: new ExecutableMemoryHandle(realAllocator, size));
110 }
111
112 static bool makeWritable(void* addr, size_t size)
113 {
114 quintptr pageSize = WTF::pageSize();
115 quintptr iaddr = reinterpret_cast<quintptr>(addr);
116 quintptr roundAddr = iaddr & ~(pageSize - 1);
117 size = size + (iaddr - roundAddr);
118 addr = reinterpret_cast<void*>(roundAddr);
119
120#if ENABLE(ASSEMBLER_WX_EXCLUSIVE) && !defined(V4_BOOTSTRAP)
121# if OS(WINDOWS)
122 DWORD oldProtect;
123# if !OS(WINRT)
124 VirtualProtect(addr, size, PAGE_READWRITE, &oldProtect);
125# else
126 bool hr = VirtualProtectFromApp(addr, size, PAGE_READWRITE, &oldProtect);
127 if (!hr) {
128 return false;
129 }
130# endif
131# elif OS(INTEGRITY)
132 OSAllocator::setMemoryAttributes(addr, size, /*writable*/ true, /*executable*/ false);
133# else
134 int mode = PROT_READ | PROT_WRITE;
135 if (mprotect(addr: addr, len: size, prot: mode) != 0) {
136 perror(s: "mprotect failed in ExecutableAllocator::makeWritable");
137 return false;
138 }
139# endif
140#else
141 // We assume we already have RWX
142 (void)addr; // suppress unused parameter warning
143 (void)size; // suppress unused parameter warning
144#endif
145 return true;
146 }
147
148 static bool makeExecutable(void* addr, size_t size)
149 {
150 quintptr pageSize = WTF::pageSize();
151 quintptr iaddr = reinterpret_cast<quintptr>(addr);
152 quintptr roundAddr = iaddr & ~(pageSize - 1);
153 size = size + (iaddr - roundAddr);
154 addr = reinterpret_cast<void*>(roundAddr);
155
156#if !defined(V4_BOOTSTRAP)
157#if ENABLE(ASSEMBLER_WX_EXCLUSIVE)
158# if OS(WINDOWS)
159 DWORD oldProtect;
160# if !OS(WINRT)
161 VirtualProtect(addr, size, PAGE_EXECUTE_READ, &oldProtect);
162# else
163 bool hr = VirtualProtectFromApp(addr, size, PAGE_EXECUTE_READ, &oldProtect);
164 if (!hr) {
165 return false;
166 }
167# endif
168# elif OS(INTEGRITY)
169 OSAllocator::setMemoryAttributes(addr, size, /*writable*/ false, /*executable*/ true);
170# else
171 int mode = PROT_READ | PROT_EXEC;
172 if (mprotect(addr: addr, len: size, prot: mode) != 0) {
173 perror(s: "mprotect failed in ExecutableAllocator::makeExecutable");
174 return false;
175 }
176# endif
177#else
178# error "Only W^X is supported"
179#endif
180#else
181 (void)addr; // suppress unused parameter warning
182 (void)size; // suppress unused parameter warning
183#endif
184 return true;
185 }
186
187 QV4::ExecutableAllocator *realAllocator;
188};
189
190}
191
192#endif // MASM_EXECUTABLEALLOCATOR_H
193

source code of qtdeclarative/src/3rdparty/masm/stubs/ExecutableAllocator.h