| 1 | /* |
| 2 | * Copyright (C) 2010-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 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <algorithm> |
| 30 | #include <wtf/StackPointer.h> |
| 31 | #include <wtf/ThreadingPrimitives.h> |
| 32 | |
| 33 | namespace WTF { |
| 34 | |
| 35 | class StackBounds { |
| 36 | // This 64k number was picked because a sampling of stack usage differences |
| 37 | // between consecutive entries into one of the Interpreter::execute...() |
| 38 | // functions was seen to be as high as 27k. Hence, 64k is chosen as a |
| 39 | // conservative availability value that is not too large but comfortably |
| 40 | // exceeds 27k with some buffer for error. |
| 41 | const static size_t s_defaultAvailabilityDelta = 64 * 1024; |
| 42 | |
| 43 | public: |
| 44 | enum class StackDirection { Upward, Downward }; |
| 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 | if (isGrowingDownward()) |
| 74 | return static_cast<char*>(m_origin) - static_cast<char*>(m_bound); |
| 75 | return static_cast<char*>(m_bound) - static_cast<char*>(m_origin); |
| 76 | } |
| 77 | |
| 78 | bool isEmpty() const { return !m_origin; } |
| 79 | |
| 80 | bool contains(void* p) const |
| 81 | { |
| 82 | if (isEmpty()) |
| 83 | return false; |
| 84 | if (isGrowingDownward()) |
| 85 | return (m_origin >= p) && (p > m_bound); |
| 86 | return (m_bound > p) && (p >= m_origin); |
| 87 | } |
| 88 | |
| 89 | void* recursionLimit(size_t minAvailableDelta = s_defaultAvailabilityDelta) const |
| 90 | { |
| 91 | checkConsistency(); |
| 92 | if (isGrowingDownward()) |
| 93 | return static_cast<char*>(m_bound) + minAvailableDelta; |
| 94 | return static_cast<char*>(m_bound) - minAvailableDelta; |
| 95 | } |
| 96 | |
| 97 | void* recursionLimit(char* startOfUserStack, size_t maxUserStack, size_t reservedZoneSize) const |
| 98 | { |
| 99 | checkConsistency(); |
| 100 | if (maxUserStack < reservedZoneSize) |
| 101 | reservedZoneSize = maxUserStack; |
| 102 | size_t maxUserStackWithReservedZone = maxUserStack - reservedZoneSize; |
| 103 | |
| 104 | if (isGrowingDownward()) { |
| 105 | char* endOfStackWithReservedZone = reinterpret_cast<char*>(m_bound) + reservedZoneSize; |
| 106 | if (startOfUserStack < endOfStackWithReservedZone) |
| 107 | return endOfStackWithReservedZone; |
| 108 | size_t availableUserStack = startOfUserStack - endOfStackWithReservedZone; |
| 109 | if (maxUserStackWithReservedZone > availableUserStack) |
| 110 | maxUserStackWithReservedZone = availableUserStack; |
| 111 | return startOfUserStack - maxUserStackWithReservedZone; |
| 112 | } |
| 113 | |
| 114 | char* endOfStackWithReservedZone = reinterpret_cast<char*>(m_bound) - reservedZoneSize; |
| 115 | if (startOfUserStack > endOfStackWithReservedZone) |
| 116 | return endOfStackWithReservedZone; |
| 117 | size_t availableUserStack = endOfStackWithReservedZone - startOfUserStack; |
| 118 | if (maxUserStackWithReservedZone > availableUserStack) |
| 119 | maxUserStackWithReservedZone = availableUserStack; |
| 120 | return startOfUserStack + maxUserStackWithReservedZone; |
| 121 | } |
| 122 | |
| 123 | bool isGrowingDownward() const |
| 124 | { |
| 125 | ASSERT(m_origin && m_bound); |
| 126 | return m_bound <= m_origin; |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | StackBounds(void* origin, void* end) |
| 131 | : m_origin(origin) |
| 132 | , m_bound(end) |
| 133 | { |
| 134 | } |
| 135 | |
| 136 | constexpr StackBounds() |
| 137 | : m_origin(nullptr) |
| 138 | , m_bound(nullptr) |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | static StackDirection stackDirection(); |
| 143 | |
| 144 | WTF_EXPORT_PRIVATE static StackBounds currentThreadStackBoundsInternal(); |
| 145 | |
| 146 | void checkConsistency() const |
| 147 | { |
| 148 | #if !ASSERT_DISABLED |
| 149 | void* currentPosition = currentStackPointer(); |
| 150 | ASSERT(m_origin != m_bound); |
| 151 | ASSERT(isGrowingDownward() |
| 152 | ? (currentPosition < m_origin && currentPosition > m_bound) |
| 153 | : (currentPosition > m_origin && currentPosition < m_bound)); |
| 154 | #endif |
| 155 | } |
| 156 | |
| 157 | void* m_origin; |
| 158 | void* m_bound; |
| 159 | |
| 160 | friend class StackStats; |
| 161 | }; |
| 162 | |
| 163 | } // namespace WTF |
| 164 | |
| 165 | using WTF::StackBounds; |
| 166 | |