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