1 | /* |
2 | * Copyright (C) 2019 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 | #include "config.h" |
27 | #include "CachedBytecode.h" |
28 | |
29 | #include "CachedTypes.h" |
30 | #include "UnlinkedFunctionExecutable.h" |
31 | #include <wtf/Function.h> |
32 | |
33 | namespace JSC { |
34 | |
35 | void CachedBytecode::addGlobalUpdate(Ref<CachedBytecode> bytecode) |
36 | { |
37 | ASSERT(m_updates.isEmpty()); |
38 | m_leafExecutables.clear(); |
39 | copyLeafExecutables(bytecode.get()); |
40 | m_updates.append(CacheUpdate::GlobalUpdate { WTFMove(bytecode->m_payload) }); |
41 | } |
42 | |
43 | void CachedBytecode::addFunctionUpdate(const UnlinkedFunctionExecutable* executable, CodeSpecializationKind kind, Ref<CachedBytecode> bytecode) |
44 | { |
45 | auto it = m_leafExecutables.find(executable); |
46 | ASSERT(it != m_leafExecutables.end()); |
47 | ptrdiff_t offset = it->value.base(); |
48 | ASSERT(offset); |
49 | copyLeafExecutables(bytecode.get()); |
50 | m_updates.append(CacheUpdate::FunctionUpdate { offset, kind, { executable->features(), executable->hasCapturedVariables() }, WTFMove(bytecode->m_payload) }); |
51 | } |
52 | |
53 | void CachedBytecode::copyLeafExecutables(const CachedBytecode& bytecode) |
54 | { |
55 | for (const auto& it : bytecode.m_leafExecutables) { |
56 | auto addResult = m_leafExecutables.add(it.key, it.value + m_size); |
57 | ASSERT_UNUSED(addResult, addResult.isNewEntry); |
58 | } |
59 | m_size += bytecode.size(); |
60 | } |
61 | |
62 | void CachedBytecode::commitUpdates(const ForEachUpdateCallback& callback) const |
63 | { |
64 | off_t offset = m_payload.size(); |
65 | for (const auto& update : m_updates) { |
66 | const CachePayload* payload = nullptr; |
67 | if (update.isGlobal()) |
68 | payload = &update.asGlobal().m_payload; |
69 | else { |
70 | const CacheUpdate::FunctionUpdate& functionUpdate = update.asFunction(); |
71 | payload = &functionUpdate.m_payload; |
72 | { |
73 | ptrdiff_t kindOffset = functionUpdate.m_kind == CodeForCall ? CachedFunctionExecutableOffsets::codeBlockForCallOffset() : CachedFunctionExecutableOffsets::codeBlockForConstructOffset(); |
74 | ptrdiff_t codeBlockOffset = functionUpdate.m_base + kindOffset + CachedWriteBarrierOffsets::ptrOffset() + CachedPtrOffsets::offsetOffset(); |
75 | ptrdiff_t offsetPayload = static_cast<ptrdiff_t>(offset) - codeBlockOffset; |
76 | static_assert(std::is_same<decltype(VariableLengthObjectBase::m_offset), ptrdiff_t>::value, "" ); |
77 | callback(codeBlockOffset, &offsetPayload, sizeof(ptrdiff_t)); |
78 | } |
79 | |
80 | { |
81 | ptrdiff_t metadataOffset = functionUpdate.m_base + CachedFunctionExecutableOffsets::metadataOffset(); |
82 | callback(metadataOffset, &functionUpdate.m_metadata, sizeof(functionUpdate.m_metadata)); |
83 | } |
84 | } |
85 | |
86 | ASSERT(payload); |
87 | callback(offset, payload->data(), payload->size()); |
88 | offset += payload->size(); |
89 | } |
90 | ASSERT(static_cast<size_t>(offset) == m_size); |
91 | } |
92 | |
93 | } // namespace JSC |
94 | |