1/*
2 * Copyright (C) 2010-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
27#pragma once
28
29#include <algorithm>
30#include <wtf/StackPointer.h>
31#include <wtf/ThreadingPrimitives.h>
32
33namespace WTF {
34
35class StackBounds {
36 WTF_MAKE_FAST_ALLOCATED;
37public:
38
39 // This 64k number was picked because a sampling of stack usage differences
40 // between consecutive entries into one of the Interpreter::execute...()
41 // functions was seen to be as high as 27k. Hence, 64k is chosen as a
42 // conservative availability value that is not too large but comfortably
43 // exceeds 27k with some buffer for error.
44 static constexpr size_t DefaultReservedZone = 64 * 1024;
45
46 static constexpr StackBounds emptyBounds() { return StackBounds(); }
47
48#if HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
49 // This function is only effective for newly created threads. In some platform, it returns a bogus value for the main thread.
50 static StackBounds newThreadStackBounds(PlatformThreadHandle);
51#endif
52 static StackBounds currentThreadStackBounds()
53 {
54 auto result = currentThreadStackBoundsInternal();
55 result.checkConsistency();
56 return result;
57 }
58
59 void* origin() const
60 {
61 ASSERT(m_origin);
62 return m_origin;
63 }
64
65 void* end() const
66 {
67 ASSERT(m_bound);
68 return m_bound;
69 }
70
71 size_t size() const
72 {
73 return static_cast<char*>(m_origin) - static_cast<char*>(m_bound);
74 }
75
76 bool isEmpty() const { return !m_origin; }
77
78 bool contains(void* p) const
79 {
80 if (isEmpty())
81 return false;
82 return (m_origin >= p) && (p > m_bound);
83 }
84
85 void* recursionLimit(size_t minReservedZone = DefaultReservedZone) const
86 {
87 checkConsistency();
88 return static_cast<char*>(m_bound) + minReservedZone;
89 }
90
91 void* recursionLimit(char* startOfUserStack, size_t maxUserStack, size_t reservedZoneSize) const
92 {
93 checkConsistency();
94 if (maxUserStack < reservedZoneSize)
95 reservedZoneSize = maxUserStack;
96 size_t maxUserStackWithReservedZone = maxUserStack - reservedZoneSize;
97
98 char* endOfStackWithReservedZone = reinterpret_cast<char*>(m_bound) + reservedZoneSize;
99 if (startOfUserStack < endOfStackWithReservedZone)
100 return endOfStackWithReservedZone;
101 size_t availableUserStack = startOfUserStack - endOfStackWithReservedZone;
102 if (maxUserStackWithReservedZone > availableUserStack)
103 maxUserStackWithReservedZone = availableUserStack;
104 return startOfUserStack - maxUserStackWithReservedZone;
105 }
106
107private:
108 StackBounds(void* origin, void* end)
109 : m_origin(origin)
110 , m_bound(end)
111 {
112 ASSERT(isGrowingDownwards());
113 }
114
115 constexpr StackBounds()
116 : m_origin(nullptr)
117 , m_bound(nullptr)
118 {
119 }
120
121 inline bool isGrowingDownwards() const
122 {
123 ASSERT(m_origin && m_bound);
124 return m_bound <= m_origin;
125 }
126
127 WTF_EXPORT_PRIVATE static StackBounds currentThreadStackBoundsInternal();
128
129 void checkConsistency() const
130 {
131#if !ASSERT_DISABLED
132 void* currentPosition = currentStackPointer();
133 ASSERT(m_origin != m_bound);
134 ASSERT(currentPosition < m_origin && currentPosition > m_bound);
135#endif
136 }
137
138 void* m_origin;
139 void* m_bound;
140
141 friend class StackStats;
142};
143
144} // namespace WTF
145
146using WTF::StackBounds;
147