source: webkit/trunk/JavaScriptCore/assembler/AssemblerBuffer.h@ 44341

Last change on this file since 44341 was 44341, checked in by [email protected], 16 years ago

2009-06-01 Gavin Barraclough <[email protected]>

Reviewed by Sam "WX" Weinig.

Allow the JIT to operate without relying on use of RWX memory, on platforms where this is supported.

This patch adds a switch to Platform.h (ENABLE_ASSEMBLER_WX_EXCLUSIVE) which enables this mode of operation.
When this flag is set, all executable memory will be allocated RX, and switched to RW only whilst being
modified. Upon completion of code generation the protection is switched back to RX to allow execution.

Further optimization will be required before it is desirable to enable this mode of operation by default;
enabling this presently incurs a 5%-10% regression.

(Submitting disabled - no performance impact).

  • assembler/AbstractMacroAssembler.h: (JSC::AbstractMacroAssembler::CodeLocationInstruction::repatchLoadToLEA): (JSC::AbstractMacroAssembler::CodeLocationLabel::fromFunctionPointer): (JSC::AbstractMacroAssembler::CodeLocationJump::relink): (JSC::AbstractMacroAssembler::CodeLocationCall::relink): (JSC::AbstractMacroAssembler::CodeLocationNearCall::relink): (JSC::AbstractMacroAssembler::CodeLocationDataLabel32::repatch): (JSC::AbstractMacroAssembler::CodeLocationDataLabelPtr::repatch): (JSC::AbstractMacroAssembler::ProcessorReturnAddress::relinkCallerToTrampoline): (JSC::AbstractMacroAssembler::ProcessorReturnAddress::relinkCallerToFunction): (JSC::AbstractMacroAssembler::ProcessorReturnAddress::relinkNearCallerToTrampoline): (JSC::AbstractMacroAssembler::ProcessorReturnAddress::relinkNearCallerToFunction): (JSC::AbstractMacroAssembler::PatchBuffer::PatchBuffer): (JSC::AbstractMacroAssembler::PatchBuffer::~PatchBuffer): (JSC::AbstractMacroAssembler::PatchBuffer::link): (JSC::AbstractMacroAssembler::PatchBuffer::patch): (JSC::AbstractMacroAssembler::PatchBuffer::performFinalization): (JSC::::CodeLocationCommon::nearCallAtOffset): (JSC::::CodeLocationCall::CodeLocationCall): (JSC::::CodeLocationNearCall::CodeLocationNearCall):
  • assembler/AssemblerBuffer.h: (JSC::AssemblerBuffer::executableCopy):
  • assembler/X86Assembler.h: (JSC::CAN_SIGN_EXTEND_U32_64): (JSC::X86Assembler::linkJump): (JSC::X86Assembler::linkCall): (JSC::X86Assembler::patchPointer): (JSC::X86Assembler::relinkJump): (JSC::X86Assembler::relinkCall): (JSC::X86Assembler::repatchInt32): (JSC::X86Assembler::repatchPointer): (JSC::X86Assembler::repatchLoadToLEA): (JSC::X86Assembler::patchInt32): (JSC::X86Assembler::patchRel32):
  • jit/ExecutableAllocator.h: (JSC::ExecutableAllocator::): (JSC::ExecutableAllocator::makeWritable): (JSC::ExecutableAllocator::makeExecutable):
  • jit/ExecutableAllocatorFixedVMPool.cpp: (JSC::FixedVMPoolAllocator::FixedVMPoolAllocator):
  • jit/ExecutableAllocatorPosix.cpp: (JSC::ExecutablePool::systemAlloc): (JSC::ExecutablePool::systemRelease): (JSC::ExecutableAllocator::reprotectRegion):
  • jit/ExecutableAllocatorWin.cpp:
  • jit/JITPropertyAccess.cpp: (JSC::JIT::patchGetByIdSelf): (JSC::JIT::patchPutByIdReplace):
  • wtf/Platform.h:
File size: 4.3 KB
Line 
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef AssemblerBuffer_h
27#define AssemblerBuffer_h
28
29#include <wtf/Platform.h>
30
31#if ENABLE(ASSEMBLER)
32
33#include "stdint.h"
34#include <string.h>
35#include <jit/ExecutableAllocator.h>
36#include <wtf/Assertions.h>
37#include <wtf/FastMalloc.h>
38
39namespace JSC {
40
41 class AssemblerBuffer {
42 static const int inlineCapacity = 256;
43 public:
44 AssemblerBuffer()
45 : m_buffer(m_inlineBuffer)
46 , m_capacity(inlineCapacity)
47 , m_size(0)
48 {
49 }
50
51 ~AssemblerBuffer()
52 {
53 if (m_buffer != m_inlineBuffer)
54 fastFree(m_buffer);
55 }
56
57 void ensureSpace(int space)
58 {
59 if (m_size > m_capacity - space)
60 grow();
61 }
62
63 bool isAligned(int alignment) const
64 {
65 return !(m_size & (alignment - 1));
66 }
67
68 void putByteUnchecked(int value)
69 {
70 ASSERT(!(m_size > m_capacity - 4));
71 m_buffer[m_size] = value;
72 m_size++;
73 }
74
75 void putByte(int value)
76 {
77 if (m_size > m_capacity - 4)
78 grow();
79 putByteUnchecked(value);
80 }
81
82 void putShortUnchecked(int value)
83 {
84 ASSERT(!(m_size > m_capacity - 4));
85 *reinterpret_cast<short*>(&m_buffer[m_size]) = value;
86 m_size += 2;
87 }
88
89 void putShort(int value)
90 {
91 if (m_size > m_capacity - 4)
92 grow();
93 putShortUnchecked(value);
94 }
95
96 void putIntUnchecked(int value)
97 {
98 *reinterpret_cast<int*>(&m_buffer[m_size]) = value;
99 m_size += 4;
100 }
101
102 void putInt64Unchecked(int64_t value)
103 {
104 *reinterpret_cast<int64_t*>(&m_buffer[m_size]) = value;
105 m_size += 8;
106 }
107
108 void putInt(int value)
109 {
110 if (m_size > m_capacity - 4)
111 grow();
112 putIntUnchecked(value);
113 }
114
115 void* data() const
116 {
117 return m_buffer;
118 }
119
120 int size() const
121 {
122 return m_size;
123 }
124
125 void* executableCopy(ExecutablePool* allocator)
126 {
127 if (!m_size)
128 return 0;
129
130 void* result = allocator->alloc(m_size);
131
132 if (!result)
133 return 0;
134
135 ExecutableAllocator::makeWritable(result, m_size);
136
137 return memcpy(result, m_buffer, m_size);
138 }
139
140 private:
141 void grow()
142 {
143 m_capacity += m_capacity / 2;
144
145 if (m_buffer == m_inlineBuffer) {
146 char* newBuffer = static_cast<char*>(fastMalloc(m_capacity));
147 m_buffer = static_cast<char*>(memcpy(newBuffer, m_buffer, m_size));
148 } else
149 m_buffer = static_cast<char*>(fastRealloc(m_buffer, m_capacity));
150 }
151
152 char m_inlineBuffer[inlineCapacity];
153 char* m_buffer;
154 int m_capacity;
155 int m_size;
156 };
157
158} // namespace JSC
159
160#endif // ENABLE(ASSEMBLER)
161
162#endif // AssemblerBuffer_h
Note: See TracBrowser for help on using the repository browser.