1/*
2 * Copyright (C) 2016-2017 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(WEBASSEMBLY)
29
30#include "WasmMemoryMode.h"
31#include "WasmPageCount.h"
32
33#include <wtf/Expected.h>
34#include <wtf/Function.h>
35#include <wtf/RefCounted.h>
36#include <wtf/RefPtr.h>
37#include <wtf/Vector.h>
38#include <wtf/WeakPtr.h>
39
40namespace WTF {
41class PrintStream;
42}
43
44namespace JSC {
45
46namespace Wasm {
47
48class Instance;
49
50class Memory : public RefCounted<Memory> {
51 WTF_MAKE_NONCOPYABLE(Memory);
52 WTF_MAKE_FAST_ALLOCATED;
53public:
54 void dump(WTF::PrintStream&) const;
55
56 explicit operator bool() const { return !!m_memory; }
57
58 enum NotifyPressure { NotifyPressureTag };
59 enum SyncTryToReclaim { SyncTryToReclaimTag };
60 enum GrowSuccess { GrowSuccessTag };
61
62 static Ref<Memory> create();
63 static RefPtr<Memory> tryCreate(PageCount initial, PageCount maximum, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback);
64
65 ~Memory();
66
67 static size_t fastMappedRedzoneBytes();
68 static size_t fastMappedBytes(); // Includes redzone.
69 static bool addressIsInActiveFastMemory(void*);
70
71 void* memory() const { return m_memory; }
72 size_t size() const { return m_size; }
73 PageCount sizeInPages() const { return PageCount::fromBytes(m_size); }
74
75 PageCount initial() const { return m_initial; }
76 PageCount maximum() const { return m_maximum; }
77
78 MemoryMode mode() const { return m_mode; }
79
80 enum class GrowFailReason {
81 InvalidDelta,
82 InvalidGrowSize,
83 WouldExceedMaximum,
84 OutOfMemory,
85 };
86 Expected<PageCount, GrowFailReason> grow(PageCount);
87 void registerInstance(Instance*);
88
89 void check() { ASSERT(!deletionHasBegun()); }
90
91 static ptrdiff_t offsetOfMemory() { return OBJECT_OFFSETOF(Memory, m_memory); }
92 static ptrdiff_t offsetOfSize() { return OBJECT_OFFSETOF(Memory, m_size); }
93
94private:
95 Memory();
96 Memory(void* memory, PageCount initial, PageCount maximum, size_t mappedCapacity, MemoryMode, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback);
97 Memory(PageCount initial, PageCount maximum, WTF::Function<void(NotifyPressure)>&& notifyMemoryPressure, WTF::Function<void(SyncTryToReclaim)>&& syncTryToReclaimMemory, WTF::Function<void(GrowSuccess, PageCount, PageCount)>&& growSuccessCallback);
98
99 void* m_memory { nullptr };
100 size_t m_size { 0 };
101 PageCount m_initial;
102 PageCount m_maximum;
103 size_t m_mappedCapacity { 0 };
104 MemoryMode m_mode { MemoryMode::BoundsChecking };
105 WTF::Function<void(NotifyPressure)> m_notifyMemoryPressure;
106 WTF::Function<void(SyncTryToReclaim)> m_syncTryToReclaimMemory;
107 WTF::Function<void(GrowSuccess, PageCount, PageCount)> m_growSuccessCallback;
108 Vector<WeakPtr<Instance>> m_instances;
109};
110
111} } // namespace JSC::Wasm
112
113#else
114
115namespace JSC { namespace Wasm {
116
117class Memory {
118public:
119 static size_t maxFastMemoryCount() { return 0; }
120 static bool addressIsInActiveFastMemory(void*) { return false; }
121};
122
123} } // namespace JSC::Wasm
124
125#endif // ENABLE(WEBASSEMBLY)
126