fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012-2019 Apple Inc. All rights reserved. |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 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 | #include "config.h" |
| 27 | #include "JITThunks.h" |
| 28 | |
| 29 | #if ENABLE(JIT) |
| 30 | |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 31 | #include "JIT.h" |
fpizlo@apple.com | 595eebd | 2016-08-24 19:00:37 +0000 | [diff] [blame] | 32 | #include "JSCInlines.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 33 | #include "LLIntData.h" |
annulen@yandex.ru | 6712c2d | 2017-06-25 17:40:30 +0000 | [diff] [blame] | 34 | #include "ThunkGenerators.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 35 | #include "VM.h" |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 36 | |
| 37 | namespace JSC { |
| 38 | |
fpizlo@apple.com | 536df7c | 2013-01-07 23:49:29 +0000 | [diff] [blame] | 39 | JITThunks::JITThunks() |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 40 | { |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | JITThunks::~JITThunks() |
| 44 | { |
| 45 | } |
| 46 | |
ysuzuki@apple.com | 5ae1a2c | 2020-02-17 22:58:49 +0000 | [diff] [blame] | 47 | static inline NativeExecutable& getMayBeDyingNativeExecutable(const Weak<NativeExecutable>& weak) |
| 48 | { |
| 49 | // This never gets Deleted / Empty slots. |
| 50 | WeakImpl* impl = weak.unsafeImpl(); |
| 51 | ASSERT(impl); |
| 52 | // We have a callback removing entry when finalizing. This means that we never hold Deallocated entry in HashSet. |
| 53 | ASSERT(impl->state() != WeakImpl::State::Deallocated); |
| 54 | // Never use jsCast here. This is possible that this value is "Dead" but not "Finalized" yet. In this case, |
| 55 | // we can still access to non-JS data, as we are doing in a finalize callback. |
| 56 | auto* executable = static_cast<NativeExecutable*>(impl->jsValue().asCell()); |
| 57 | ASSERT(executable); |
| 58 | return *executable; |
| 59 | } |
| 60 | |
| 61 | inline unsigned JITThunks::WeakNativeExecutableHash::hash(NativeExecutable* executable) |
| 62 | { |
| 63 | return hash(executable->function(), executable->constructor(), executable->name()); |
| 64 | } |
| 65 | |
| 66 | inline unsigned JITThunks::WeakNativeExecutableHash::hash(const Weak<NativeExecutable>& key) |
| 67 | { |
| 68 | return hash(&getMayBeDyingNativeExecutable(key)); |
| 69 | } |
| 70 | |
| 71 | inline bool JITThunks::WeakNativeExecutableHash::equal(NativeExecutable& a, NativeExecutable& b) |
| 72 | { |
| 73 | if (&a == &b) |
| 74 | return true; |
| 75 | return a.function() == b.function() && a.constructor() == b.constructor() && a.name() == b.name(); |
| 76 | } |
| 77 | |
| 78 | inline bool JITThunks::WeakNativeExecutableHash::equal(const Weak<NativeExecutable>& a, const Weak<NativeExecutable>& b) |
| 79 | { |
| 80 | return equal(getMayBeDyingNativeExecutable(a), getMayBeDyingNativeExecutable(b)); |
| 81 | } |
| 82 | |
| 83 | inline bool JITThunks::WeakNativeExecutableHash::equal(const Weak<NativeExecutable>& a, NativeExecutable* bExecutable) |
| 84 | { |
| 85 | return equal(getMayBeDyingNativeExecutable(a), *bExecutable); |
| 86 | } |
| 87 | |
| 88 | inline bool JITThunks::WeakNativeExecutableHash::equal(const Weak<NativeExecutable>& a, const HostFunctionKey& b) |
| 89 | { |
| 90 | auto& aExecutable = getMayBeDyingNativeExecutable(a); |
| 91 | return aExecutable.function() == std::get<0>(b) && aExecutable.constructor() == std::get<1>(b) && aExecutable.name() == std::get<2>(b); |
| 92 | } |
| 93 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 94 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeCall(VM& vm) |
fpizlo@apple.com | 2ac511c | 2012-12-27 23:12:27 +0000 | [diff] [blame] | 95 | { |
mark.lam@apple.com | 2e81af7 | 2018-03-06 03:03:01 +0000 | [diff] [blame] | 96 | ASSERT(VM::canUseJIT()); |
commit-queue@webkit.org | 2cb9c25 | 2016-12-13 19:38:13 +0000 | [diff] [blame] | 97 | return ctiStub(vm, nativeCallGenerator).code(); |
fpizlo@apple.com | 2ac511c | 2012-12-27 23:12:27 +0000 | [diff] [blame] | 98 | } |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 99 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 100 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeConstruct(VM& vm) |
fpizlo@apple.com | 2ac511c | 2012-12-27 23:12:27 +0000 | [diff] [blame] | 101 | { |
mark.lam@apple.com | 2e81af7 | 2018-03-06 03:03:01 +0000 | [diff] [blame] | 102 | ASSERT(VM::canUseJIT()); |
commit-queue@webkit.org | 2cb9c25 | 2016-12-13 19:38:13 +0000 | [diff] [blame] | 103 | return ctiStub(vm, nativeConstructGenerator).code(); |
fpizlo@apple.com | 2ac511c | 2012-12-27 23:12:27 +0000 | [diff] [blame] | 104 | } |
| 105 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 106 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeTailCall(VM& vm) |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 107 | { |
utatane.tea@gmail.com | 6863b23 | 2017-12-17 19:35:38 +0000 | [diff] [blame] | 108 | ASSERT(VM::canUseJIT()); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 109 | return ctiStub(vm, nativeTailCallGenerator).code(); |
| 110 | } |
| 111 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 112 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiNativeTailCallWithoutSavedTags(VM& vm) |
fpizlo@apple.com | f41fc90 | 2016-04-23 02:00:38 +0000 | [diff] [blame] | 113 | { |
utatane.tea@gmail.com | 6863b23 | 2017-12-17 19:35:38 +0000 | [diff] [blame] | 114 | ASSERT(VM::canUseJIT()); |
fpizlo@apple.com | f41fc90 | 2016-04-23 02:00:38 +0000 | [diff] [blame] | 115 | return ctiStub(vm, nativeTailCallWithoutSavedTagsGenerator).code(); |
| 116 | } |
| 117 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 118 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiInternalFunctionCall(VM& vm) |
utatane.tea@gmail.com | 7ab015d | 2017-11-06 14:40:08 +0000 | [diff] [blame] | 119 | { |
mark.lam@apple.com | 2e81af7 | 2018-03-06 03:03:01 +0000 | [diff] [blame] | 120 | ASSERT(VM::canUseJIT()); |
utatane.tea@gmail.com | 7ab015d | 2017-11-06 14:40:08 +0000 | [diff] [blame] | 121 | return ctiStub(vm, internalFunctionCallGenerator).code(); |
| 122 | } |
| 123 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 124 | MacroAssemblerCodePtr<JITThunkPtrTag> JITThunks::ctiInternalFunctionConstruct(VM& vm) |
utatane.tea@gmail.com | 7ab015d | 2017-11-06 14:40:08 +0000 | [diff] [blame] | 125 | { |
mark.lam@apple.com | 2e81af7 | 2018-03-06 03:03:01 +0000 | [diff] [blame] | 126 | ASSERT(VM::canUseJIT()); |
utatane.tea@gmail.com | 7ab015d | 2017-11-06 14:40:08 +0000 | [diff] [blame] | 127 | return ctiStub(vm, internalFunctionConstructGenerator).code(); |
| 128 | } |
| 129 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 130 | MacroAssemblerCodeRef<JITThunkPtrTag> JITThunks::ctiStub(VM& vm, ThunkGenerator generator) |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 131 | { |
fpizlo@apple.com | aa67129 | 2015-08-15 00:14:52 +0000 | [diff] [blame] | 132 | LockHolder locker(m_lock); |
mark.lam@apple.com | de0dba7 | 2018-04-18 03:31:09 +0000 | [diff] [blame] | 133 | CTIStubMap::AddResult entry = m_ctiStubMap.add(generator, MacroAssemblerCodeRef<JITThunkPtrTag>()); |
oliver@apple.com | f5a934a | 2013-07-25 04:00:24 +0000 | [diff] [blame] | 134 | if (entry.isNewEntry) { |
| 135 | // Compilation thread can only retrieve existing entries. |
| 136 | ASSERT(!isCompilationThread()); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 137 | entry.iterator->value = generator(vm); |
oliver@apple.com | f5a934a | 2013-07-25 04:00:24 +0000 | [diff] [blame] | 138 | } |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 139 | return entry.iterator->value; |
| 140 | } |
| 141 | |
mark.lam@apple.com | de0dba7 | 2018-04-18 03:31:09 +0000 | [diff] [blame] | 142 | MacroAssemblerCodeRef<JITThunkPtrTag> JITThunks::existingCTIStub(ThunkGenerator generator) |
keith_miller@apple.com | e3ecd6a | 2017-03-03 22:24:21 +0000 | [diff] [blame] | 143 | { |
| 144 | LockHolder locker(m_lock); |
| 145 | CTIStubMap::iterator entry = m_ctiStubMap.find(generator); |
| 146 | if (entry == m_ctiStubMap.end()) |
mark.lam@apple.com | de0dba7 | 2018-04-18 03:31:09 +0000 | [diff] [blame] | 147 | return MacroAssemblerCodeRef<JITThunkPtrTag>(); |
keith_miller@apple.com | e3ecd6a | 2017-03-03 22:24:21 +0000 | [diff] [blame] | 148 | return entry->value; |
| 149 | } |
| 150 | |
ysuzuki@apple.com | 5ae1a2c | 2020-02-17 22:58:49 +0000 | [diff] [blame] | 151 | struct JITThunks::HostKeySearcher { |
| 152 | static unsigned hash(const HostFunctionKey& key) { return WeakNativeExecutableHash::hash(key); } |
| 153 | static bool equal(const Weak<NativeExecutable>& a, const HostFunctionKey& b) { return WeakNativeExecutableHash::equal(a, b); } |
| 154 | }; |
| 155 | |
| 156 | struct JITThunks::NativeExecutableTranslator { |
| 157 | static unsigned hash(NativeExecutable* key) { return WeakNativeExecutableHash::hash(key); } |
| 158 | static bool equal(const Weak<NativeExecutable>& a, NativeExecutable* b) { return WeakNativeExecutableHash::equal(a, b); } |
| 159 | static void translate(Weak<NativeExecutable>& location, NativeExecutable* executable, unsigned) |
| 160 | { |
| 161 | location = Weak<NativeExecutable>(executable, executable->vm().jitStubs.get()); |
| 162 | } |
| 163 | }; |
| 164 | |
commit-queue@webkit.org | d5d496b | 2015-09-22 12:21:31 +0000 | [diff] [blame] | 165 | void JITThunks::finalize(Handle<Unknown> handle, void*) |
akling@apple.com | 47a5bfa | 2015-03-08 23:58:40 +0000 | [diff] [blame] | 166 | { |
fpizlo@apple.com | e88e598 | 2017-01-17 23:52:55 +0000 | [diff] [blame] | 167 | auto* nativeExecutable = static_cast<NativeExecutable*>(handle.get().asCell()); |
ysuzuki@apple.com | 5ae1a2c | 2020-02-17 22:58:49 +0000 | [diff] [blame] | 168 | auto hostFunctionKey = std::make_tuple(nativeExecutable->function(), nativeExecutable->constructor(), nativeExecutable->name()); |
| 169 | { |
| 170 | DisallowGC disallowGC; |
| 171 | auto iterator = m_nativeExecutableSet.find<HostKeySearcher>(hostFunctionKey); |
| 172 | // Because this finalizer is called, this means that we still have dead Weak<> in m_nativeExecutableSet. |
| 173 | ASSERT(iterator != m_nativeExecutableSet.end()); |
| 174 | ASSERT(iterator->unsafeImpl()->state() == WeakImpl::State::Finalized); |
| 175 | m_nativeExecutableSet.remove(iterator); |
| 176 | } |
akling@apple.com | 47a5bfa | 2015-03-08 23:58:40 +0000 | [diff] [blame] | 177 | } |
| 178 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 179 | NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, TaggedNativeFunction constructor, const String& name) |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 180 | { |
utatane.tea@gmail.com | 0d74c7c | 2016-11-03 03:20:53 +0000 | [diff] [blame] | 181 | return hostFunctionStub(vm, function, constructor, nullptr, NoIntrinsic, nullptr, name); |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 182 | } |
| 183 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 184 | NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, TaggedNativeFunction constructor, ThunkGenerator generator, Intrinsic intrinsic, const DOMJIT::Signature* signature, const String& name) |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 185 | { |
oliver@apple.com | f5a934a | 2013-07-25 04:00:24 +0000 | [diff] [blame] | 186 | ASSERT(!isCompilationThread()); |
utatane.tea@gmail.com | 6863b23 | 2017-12-17 19:35:38 +0000 | [diff] [blame] | 187 | ASSERT(VM::canUseJIT()); |
oliver@apple.com | f5a934a | 2013-07-25 04:00:24 +0000 | [diff] [blame] | 188 | |
ysuzuki@apple.com | 5ae1a2c | 2020-02-17 22:58:49 +0000 | [diff] [blame] | 189 | auto hostFunctionKey = std::make_tuple(function, constructor, name); |
| 190 | { |
| 191 | DisallowGC disallowGC; |
| 192 | auto iterator = m_nativeExecutableSet.find<HostKeySearcher>(hostFunctionKey); |
| 193 | if (iterator != m_nativeExecutableSet.end()) { |
| 194 | // It is possible that this returns Weak<> which is Dead, but not finalized. |
| 195 | // We should not use this reference to store value created in the subsequent sequence, since allocating NativeExecutable can cause GC, which changes this Set. |
| 196 | if (auto* executable = iterator->get()) |
| 197 | return executable; |
| 198 | } |
| 199 | } |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 200 | |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 201 | RefPtr<JITCode> forCall; |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 202 | if (generator) { |
mark.lam@apple.com | de0dba7 | 2018-04-18 03:31:09 +0000 | [diff] [blame] | 203 | MacroAssemblerCodeRef<JSEntryPtrTag> entry = generator(vm).retagged<JSEntryPtrTag>(); |
sbarati@apple.com | 9777f7c | 2019-04-30 03:27:39 +0000 | [diff] [blame] | 204 | forCall = adoptRef(new DirectJITCode(entry, entry.code(), JITType::HostCallThunk, intrinsic)); |
ysuzuki@apple.com | 6189a49 | 2019-02-06 19:49:04 +0000 | [diff] [blame] | 205 | } else if (signature) |
sbarati@apple.com | 9777f7c | 2019-04-30 03:27:39 +0000 | [diff] [blame] | 206 | forCall = adoptRef(new NativeDOMJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeCall(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, intrinsic, signature)); |
ysuzuki@apple.com | 6189a49 | 2019-02-06 19:49:04 +0000 | [diff] [blame] | 207 | else |
sbarati@apple.com | 9777f7c | 2019-04-30 03:27:39 +0000 | [diff] [blame] | 208 | forCall = adoptRef(new NativeJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeCall(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, intrinsic)); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 209 | |
sbarati@apple.com | 9777f7c | 2019-04-30 03:27:39 +0000 | [diff] [blame] | 210 | Ref<JITCode> forConstruct = adoptRef(*new NativeJITCode(MacroAssemblerCodeRef<JSEntryPtrTag>::createSelfManagedCodeRef(ctiNativeConstruct(vm).retagged<JSEntryPtrTag>()), JITType::HostCallThunk, NoIntrinsic)); |
msaboff@apple.com | 9589433 | 2014-01-29 19:18:54 +0000 | [diff] [blame] | 211 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 212 | NativeExecutable* nativeExecutable = NativeExecutable::create(vm, forCall.releaseNonNull(), function, WTFMove(forConstruct), constructor, name); |
ysuzuki@apple.com | 5ae1a2c | 2020-02-17 22:58:49 +0000 | [diff] [blame] | 213 | { |
| 214 | DisallowGC disallowGC; |
| 215 | auto addResult = m_nativeExecutableSet.add<NativeExecutableTranslator>(nativeExecutable); |
| 216 | if (!addResult.isNewEntry) { |
| 217 | // Override the existing Weak<NativeExecutable> with the new one since it is dead. |
| 218 | ASSERT(!*addResult.iterator); |
| 219 | *addResult.iterator = Weak<NativeExecutable>(nativeExecutable, this); |
| 220 | ASSERT(*addResult.iterator); |
| 221 | #if ASSERT_ENABLED |
| 222 | auto iterator = m_nativeExecutableSet.find<HostKeySearcher>(hostFunctionKey); |
| 223 | ASSERT(iterator != m_nativeExecutableSet.end()); |
| 224 | ASSERT(iterator->get() == nativeExecutable); |
| 225 | ASSERT(iterator->unsafeImpl()->state() == WeakImpl::State::Live); |
| 226 | #endif |
| 227 | } |
| 228 | } |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 229 | return nativeExecutable; |
| 230 | } |
| 231 | |
mark.lam@apple.com | 5ba0779 | 2019-08-27 22:14:52 +0000 | [diff] [blame] | 232 | NativeExecutable* JITThunks::hostFunctionStub(VM& vm, TaggedNativeFunction function, ThunkGenerator generator, Intrinsic intrinsic, const String& name) |
fpizlo@apple.com | f41fc90 | 2016-04-23 02:00:38 +0000 | [diff] [blame] | 233 | { |
utatane.tea@gmail.com | 0d74c7c | 2016-11-03 03:20:53 +0000 | [diff] [blame] | 234 | return hostFunctionStub(vm, function, callHostFunctionAsConstructor, generator, intrinsic, nullptr, name); |
fpizlo@apple.com | f41fc90 | 2016-04-23 02:00:38 +0000 | [diff] [blame] | 235 | } |
| 236 | |
fpizlo@apple.com | c8eca23 | 2012-12-25 21:07:53 +0000 | [diff] [blame] | 237 | } // namespace JSC |
| 238 | |
| 239 | #endif // ENABLE(JIT) |