1/*
2 * Copyright (C) 2018 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#include "MetadataTable.h"
29#include "UnlinkedMetadataTable.h"
30#include <wtf/FastMalloc.h>
31
32namespace JSC {
33
34ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable()
35 : m_hasMetadata(false)
36 , m_isFinalized(false)
37 , m_isLinked(false)
38 , m_is32Bit(false)
39 , m_rawBuffer(static_cast<uint8_t*>(fastZeroedMalloc(sizeof(LinkingData) + s_offset32TableSize)))
40{
41}
42
43ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable(bool is32Bit)
44 : m_hasMetadata(false)
45 , m_isFinalized(false)
46 , m_isLinked(false)
47 , m_is32Bit(is32Bit)
48 , m_rawBuffer(static_cast<uint8_t*>(fastZeroedMalloc(sizeof(LinkingData) + (is32Bit ? s_offset16TableSize + s_offset32TableSize : s_offset16TableSize))))
49{
50}
51
52ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable(EmptyTag)
53 : m_hasMetadata(false)
54 , m_isFinalized(true)
55 , m_isLinked(false)
56 , m_is32Bit(false)
57 , m_rawBuffer(nullptr)
58{
59}
60
61ALWAYS_INLINE UnlinkedMetadataTable::~UnlinkedMetadataTable()
62{
63 ASSERT(!m_isLinked);
64 if (m_hasMetadata || !m_isFinalized)
65 fastFree(m_rawBuffer);
66}
67
68ALWAYS_INLINE unsigned UnlinkedMetadataTable::addEntry(OpcodeID opcodeID)
69{
70 ASSERT(!m_isFinalized && opcodeID < s_offsetTableEntries - 1);
71 m_hasMetadata = true;
72 return preprocessBuffer()[opcodeID]++;
73}
74
75ALWAYS_INLINE size_t UnlinkedMetadataTable::sizeInBytes()
76{
77 if (m_isFinalized && !m_hasMetadata)
78 return 0;
79
80 if (m_is32Bit)
81 return s_offset16TableSize + s_offset32TableSize;
82 return s_offset16TableSize;
83}
84
85ALWAYS_INLINE size_t UnlinkedMetadataTable::sizeInBytes(MetadataTable& metadataTable)
86{
87 ASSERT(m_isFinalized);
88
89 // In this case, we return the size of the table minus the offset table,
90 // which was already accounted for in the UnlinkedCodeBlock.
91
92 // Be careful not to touch m_rawBuffer if this metadataTable is not owning it.
93 // It is possible that, m_rawBuffer is realloced in the other thread while we are accessing here.
94 size_t result = metadataTable.totalSize();
95 if (metadataTable.buffer() == buffer()) {
96 ASSERT(m_isLinked);
97 if (m_is32Bit)
98 return result - (s_offset16TableSize + s_offset32TableSize);
99 return result - s_offset16TableSize;
100 }
101 return result;
102}
103
104ALWAYS_INLINE RefPtr<MetadataTable> UnlinkedMetadataTable::link()
105{
106 ASSERT(m_isFinalized);
107
108 if (!m_hasMetadata)
109 return nullptr;
110
111 unsigned totalSize = this->totalSize();
112 unsigned offsetTableSize = this->offsetTableSize();
113 uint8_t* buffer;
114 if (!m_isLinked) {
115 m_isLinked = true;
116 m_rawBuffer = buffer = reinterpret_cast<uint8_t*>(fastRealloc(m_rawBuffer, sizeof(LinkingData) + totalSize));
117 } else {
118 buffer = reinterpret_cast<uint8_t*>(fastMalloc(sizeof(LinkingData) + totalSize));
119 memcpy(buffer, m_rawBuffer, sizeof(LinkingData) + offsetTableSize);
120 }
121 memset(buffer + sizeof(LinkingData) + offsetTableSize, 0, totalSize - offsetTableSize);
122 return adoptRef(*new (buffer + sizeof(LinkingData)) MetadataTable(*this));
123}
124
125ALWAYS_INLINE void UnlinkedMetadataTable::unlink(MetadataTable& metadataTable)
126{
127 ASSERT(m_isFinalized);
128 if (!m_hasMetadata)
129 return;
130
131 if (metadataTable.buffer() == buffer()) {
132 ASSERT(m_isLinked);
133 m_isLinked = false;
134 m_rawBuffer = static_cast<uint8_t*>(fastRealloc(m_rawBuffer, sizeof(LinkingData) + offsetTableSize()));
135 return;
136 }
137 fastFree(&metadataTable.linkingData());
138}
139
140} // namespace JSC
141