Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/3rdparty/masm/stubs/ExecutableAllocator.h |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
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__ | - | ||||||
60 | using std::perror; | - | ||||||
61 | #endif | - | ||||||
62 | - | |||||||
63 | namespace JSC { | - | ||||||
64 | - | |||||||
65 | class JSGlobalData; | - | ||||||
66 | - | |||||||
67 | struct 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 | } executed 1163946 times by 153 tests: end of block Executed by:
| 1163946 | ||||||
74 | ~ExecutableMemoryHandle() | - | ||||||
75 | { | - | ||||||
76 | m_allocation->deallocate(m_allocator); | - | ||||||
77 | } executed 1164520 times by 153 tests: end of block Executed by:
| 1164520 | ||||||
78 | - | |||||||
79 | inline void shrink(size_t) { | - | ||||||
80 | // ### TODO. | - | ||||||
81 | } | - | ||||||
82 | - | |||||||
83 | inline bool isManaged() const { return true; } executed 1164590 times by 153 tests: return true; Executed by:
| 1164590 | ||||||
84 | - | |||||||
85 | void* start() { return m_allocation->start(); } executed 5821358 times by 153 tests: return m_allocation->start(); Executed by:
| 5821358 | ||||||
86 | size_t sizeInBytes() { return m_size; } executed 2337898 times by 153 tests: return m_size; Executed by:
| 2337898 | ||||||
87 | - | |||||||
88 | QV4::ExecutableAllocator::ChunkOfPages *chunk() const | - | ||||||
89 | { return m_allocator->chunkForAllocation(m_allocation); } never executed: return m_allocator->chunkForAllocation(m_allocation); | 0 | ||||||
90 | - | |||||||
91 | QV4::ExecutableAllocator *m_allocator; | - | ||||||
92 | QV4::ExecutableAllocator::Allocation *m_allocation; | - | ||||||
93 | size_t m_size; | - | ||||||
94 | }; | - | ||||||
95 | - | |||||||
96 | struct ExecutableAllocator { | - | ||||||
97 | ExecutableAllocator(QV4::ExecutableAllocator *alloc) | - | ||||||
98 | : realAllocator(alloc) | - | ||||||
99 | {} executed 1162840 times by 153 tests: end of block Executed by:
| 1162840 | ||||||
100 | - | |||||||
101 | PassRefPtr<ExecutableMemoryHandle> allocate(JSGlobalData&, size_t size, void*, int) | - | ||||||
102 | { | - | ||||||
103 | return adoptRef(new ExecutableMemoryHandle(realAllocator, size)); executed 1160949 times by 153 tests: return adoptRef(new ExecutableMemoryHandle(realAllocator, size)); Executed by:
| 1160949 | ||||||
104 | } | - | ||||||
105 | - | |||||||
106 | static void makeWritable(void* addr, size_t size) | - | ||||||
107 | { | - | ||||||
108 | quintptr pageSize = WTF::pageSize(); | - | ||||||
109 | quintptr iaddr = reinterpret_cast<quintptr>(addr); | - | ||||||
110 | quintptr roundAddr = iaddr & ~(pageSize - 1); | - | ||||||
111 | size = size + (iaddr - roundAddr); | - | ||||||
112 | addr = reinterpret_cast<void*>(roundAddr); | - | ||||||
113 | - | |||||||
114 | #if ENABLE(ASSEMBLER_WX_EXCLUSIVE) && !defined(V4_BOOTSTRAP) | - | ||||||
115 | # if OS(WINDOWS) | - | ||||||
116 | DWORD oldProtect; | - | ||||||
117 | # if !OS(WINRT) | - | ||||||
118 | VirtualProtect(addr, size, PAGE_READWRITE, &oldProtect); | - | ||||||
119 | # else | - | ||||||
120 | bool hr = VirtualProtectFromApp(addr, size, PAGE_READWRITE, &oldProtect); | - | ||||||
121 | if (!hr) { | - | ||||||
122 | Q_UNREACHABLE(); | - | ||||||
123 | } | - | ||||||
124 | # endif | - | ||||||
125 | # elif OS(INTEGRITY) | - | ||||||
126 | OSAllocator::setMemoryAttributes(addr, /*writable*/ true, /*executable*/ false); | - | ||||||
127 | # else | - | ||||||
128 | int mode = PROT_READ | PROT_WRITE; | - | ||||||
129 | if (mprotect(addr, size, mode) != 0) {
| 0-1164377 | ||||||
130 | perror("mprotect failed in ExecutableAllocator::makeWritable"); | - | ||||||
131 | Q_UNREACHABLE(); | - | ||||||
132 | } never executed: end of block | 0 | ||||||
133 | # endif | - | ||||||
134 | #else | - | ||||||
135 | // We assume we already have RWX | - | ||||||
136 | (void)addr; // suppress unused parameter warning | - | ||||||
137 | (void)size; // suppress unused parameter warning | - | ||||||
138 | #endif | - | ||||||
139 | } executed 1164392 times by 153 tests: end of block Executed by:
| 1164392 | ||||||
140 | - | |||||||
141 | static void makeExecutable(void* addr, size_t size) | - | ||||||
142 | { | - | ||||||
143 | quintptr pageSize = WTF::pageSize(); | - | ||||||
144 | quintptr iaddr = reinterpret_cast<quintptr>(addr); | - | ||||||
145 | quintptr roundAddr = iaddr & ~(pageSize - 1); | - | ||||||
146 | size = size + (iaddr - roundAddr); | - | ||||||
147 | addr = reinterpret_cast<void*>(roundAddr); | - | ||||||
148 | - | |||||||
149 | #if !defined(V4_BOOTSTRAP) | - | ||||||
150 | #if ENABLE(ASSEMBLER_WX_EXCLUSIVE) | - | ||||||
151 | # if OS(WINDOWS) | - | ||||||
152 | DWORD oldProtect; | - | ||||||
153 | # if !OS(WINRT) | - | ||||||
154 | VirtualProtect(addr, size, PAGE_EXECUTE_READ, &oldProtect); | - | ||||||
155 | # else | - | ||||||
156 | bool hr = VirtualProtectFromApp(addr, size, PAGE_EXECUTE_READ, &oldProtect); | - | ||||||
157 | if (!hr) { | - | ||||||
158 | Q_UNREACHABLE(); | - | ||||||
159 | } | - | ||||||
160 | # endif | - | ||||||
161 | # elif OS(INTEGRITY) | - | ||||||
162 | OSAllocator::setMemoryAttributes(addr, /*writable*/ false, /*executable*/ true); | - | ||||||
163 | # else | - | ||||||
164 | int mode = PROT_READ | PROT_EXEC; | - | ||||||
165 | if (mprotect(addr, size, mode) != 0) {
| 0-1164555 | ||||||
166 | perror("mprotect failed in ExecutableAllocator::makeExecutable"); | - | ||||||
167 | Q_UNREACHABLE(); | - | ||||||
168 | } never executed: end of block | 0 | ||||||
169 | # endif | - | ||||||
170 | #else | - | ||||||
171 | # error "Only W^X is supported" | - | ||||||
172 | #endif | - | ||||||
173 | #else | - | ||||||
174 | (void)addr; // suppress unused parameter warning | - | ||||||
175 | (void)size; // suppress unused parameter warning | - | ||||||
176 | #endif | - | ||||||
177 | } executed 1164564 times by 153 tests: end of block Executed by:
| 1164564 | ||||||
178 | - | |||||||
179 | QV4::ExecutableAllocator *realAllocator; | - | ||||||
180 | }; | - | ||||||
181 | - | |||||||
182 | } | - | ||||||
183 | - | |||||||
184 | #endif // MASM_EXECUTABLEALLOCATOR_H | - | ||||||
Source code | Switch to Preprocessed file |