1 | /*
|
---|
2 | * Copyright (C) 2012-2021 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 | #pragma once
|
---|
27 |
|
---|
28 | #if ENABLE(JIT)
|
---|
29 |
|
---|
30 | #include "CallData.h"
|
---|
31 | #include "Intrinsic.h"
|
---|
32 | #include "MacroAssemblerCodeRef.h"
|
---|
33 | #include "SlowPathFunction.h"
|
---|
34 | #include "ThunkGenerator.h"
|
---|
35 | #include "Weak.h"
|
---|
36 | #include "WeakHandleOwner.h"
|
---|
37 | #include <tuple>
|
---|
38 | #include <wtf/HashMap.h>
|
---|
39 | #include <wtf/HashSet.h>
|
---|
40 | #include <wtf/PackedRefPtr.h>
|
---|
41 | #include <wtf/RecursiveLockAdapter.h>
|
---|
42 | #include <wtf/text/StringHash.h>
|
---|
43 |
|
---|
44 | namespace JSC {
|
---|
45 | namespace DOMJIT {
|
---|
46 | class Signature;
|
---|
47 | }
|
---|
48 |
|
---|
49 | class VM;
|
---|
50 | class NativeExecutable;
|
---|
51 |
|
---|
52 | class JITThunks final : private WeakHandleOwner {
|
---|
53 | WTF_MAKE_FAST_ALLOCATED;
|
---|
54 | public:
|
---|
55 | JITThunks();
|
---|
56 | ~JITThunks() final;
|
---|
57 |
|
---|
58 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeCall(VM&);
|
---|
59 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeConstruct(VM&);
|
---|
60 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeTailCall(VM&);
|
---|
61 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiNativeTailCallWithoutSavedTags(VM&);
|
---|
62 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiInternalFunctionCall(VM&);
|
---|
63 | MacroAssemblerCodePtr<JITThunkPtrTag> ctiInternalFunctionConstruct(VM&);
|
---|
64 |
|
---|
65 | MacroAssemblerCodeRef<JITThunkPtrTag> ctiStub(VM&, ThunkGenerator);
|
---|
66 | MacroAssemblerCodeRef<JITThunkPtrTag> ctiSlowPathFunctionStub(VM&, SlowPathFunction);
|
---|
67 |
|
---|
68 | NativeExecutable* hostFunctionStub(VM&, TaggedNativeFunction, TaggedNativeFunction constructor, const String& name);
|
---|
69 | NativeExecutable* hostFunctionStub(VM&, TaggedNativeFunction, TaggedNativeFunction constructor, ThunkGenerator, Intrinsic, const DOMJIT::Signature*, const String& name);
|
---|
70 | NativeExecutable* hostFunctionStub(VM&, TaggedNativeFunction, ThunkGenerator, Intrinsic, const String& name);
|
---|
71 |
|
---|
72 | private:
|
---|
73 | template <typename GenerateThunk>
|
---|
74 | MacroAssemblerCodeRef<JITThunkPtrTag> ctiStubImpl(ThunkGenerator key, GenerateThunk);
|
---|
75 |
|
---|
76 | void finalize(Handle<Unknown>, void* context) final;
|
---|
77 |
|
---|
78 | struct Entry {
|
---|
79 | PackedRefPtr<ExecutableMemoryHandle> handle;
|
---|
80 | bool needsCrossModifyingCodeFence;
|
---|
81 | };
|
---|
82 | using CTIStubMap = HashMap<ThunkGenerator, Entry>;
|
---|
83 | CTIStubMap m_ctiStubMap;
|
---|
84 |
|
---|
85 | using HostFunctionKey = std::tuple<TaggedNativeFunction, TaggedNativeFunction, String>;
|
---|
86 |
|
---|
87 | struct WeakNativeExecutableHash {
|
---|
88 | static inline unsigned hash(const Weak<NativeExecutable>&);
|
---|
89 | static inline unsigned hash(NativeExecutable*);
|
---|
90 | static unsigned hash(const HostFunctionKey& key)
|
---|
91 | {
|
---|
92 | return hash(std::get<0>(key), std::get<1>(key), std::get<2>(key));
|
---|
93 | }
|
---|
94 |
|
---|
95 | static inline bool equal(const Weak<NativeExecutable>&, const Weak<NativeExecutable>&);
|
---|
96 | static inline bool equal(const Weak<NativeExecutable>&, const HostFunctionKey&);
|
---|
97 | static inline bool equal(const Weak<NativeExecutable>&, NativeExecutable*);
|
---|
98 | static inline bool equal(NativeExecutable&, NativeExecutable&);
|
---|
99 | static constexpr bool safeToCompareToEmptyOrDeleted = false;
|
---|
100 |
|
---|
101 | private:
|
---|
102 | static inline unsigned hashPointer(TaggedNativeFunction p)
|
---|
103 | {
|
---|
104 | return DefaultHash<TaggedNativeFunction>::hash(p);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static unsigned hash(TaggedNativeFunction function, TaggedNativeFunction constructor, const String& name)
|
---|
108 | {
|
---|
109 | // FIXME: Use WTF::computeHash.
|
---|
110 | // https://bugs.webkit.org/show_bug.cgi?id=207835
|
---|
111 | unsigned hash = WTF::pairIntHash(hashPointer(function), hashPointer(constructor));
|
---|
112 | if (!name.isNull())
|
---|
113 | hash = WTF::pairIntHash(hash, DefaultHash<String>::hash(name));
|
---|
114 | return hash;
|
---|
115 | }
|
---|
116 | };
|
---|
117 | struct HostKeySearcher;
|
---|
118 | struct NativeExecutableTranslator;
|
---|
119 |
|
---|
120 | using WeakNativeExecutableSet = HashSet<Weak<NativeExecutable>, WeakNativeExecutableHash>;
|
---|
121 | WeakNativeExecutableSet m_nativeExecutableSet;
|
---|
122 |
|
---|
123 | WTF::RecursiveLock m_lock;
|
---|
124 | };
|
---|
125 |
|
---|
126 | } // namespace JSC
|
---|
127 |
|
---|
128 | #endif // ENABLE(JIT)
|
---|