|
| 1 | +// Copyright 2020 the V8 project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "src/heap/cppgc/stack.h" |
| 6 | + |
| 7 | +#include <limits> |
| 8 | + |
| 9 | +#include "src/base/platform/platform.h" |
| 10 | +#include "src/heap/cppgc/globals.h" |
| 11 | +#include "src/heap/cppgc/sanitizers.h" |
| 12 | + |
| 13 | +namespace cppgc { |
| 14 | +namespace internal { |
| 15 | + |
| 16 | +using IterateStackCallback = void (Stack::*)(StackVisitor*, intptr_t*) const; |
| 17 | +extern "C" void PushAllRegistersAndIterateStack(const Stack*, StackVisitor*, |
| 18 | + IterateStackCallback); |
| 19 | + |
| 20 | +Stack::Stack(const void* stack_start) : stack_start_(stack_start) {} |
| 21 | + |
| 22 | +bool Stack::IsOnStack(void* slot) const { |
| 23 | + void* raw_slot = v8::base::Stack::GetStackSlot(slot); |
| 24 | + return v8::base::Stack::GetCurrentStackPosition() <= raw_slot && |
| 25 | + raw_slot <= stack_start_; |
| 26 | +} |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +#ifdef V8_USE_ADDRESS_SANITIZER |
| 31 | + |
| 32 | +// No ASAN support as accessing fake frames otherwise results in |
| 33 | +// "stack-use-after-scope" warnings. |
| 34 | +NO_SANITIZE_ADDRESS |
| 35 | +void IterateAsanFakeFrameIfNecessary(StackVisitor* visitor, |
| 36 | + void* asan_fake_stack, |
| 37 | + const void* stack_start, |
| 38 | + const void* stack_end, void* address) { |
| 39 | + // When using ASAN fake stack a pointer to the fake frame is kept on the |
| 40 | + // native frame. In case |addr| points to a fake frame of the current stack |
| 41 | + // iterate the fake frame. Frame layout see |
| 42 | + // https://github.com/google/sanitizers/wiki/AddressSanitizerUseAfterReturn |
| 43 | + if (asan_fake_stack) { |
| 44 | + void* fake_frame_begin; |
| 45 | + void* fake_frame_end; |
| 46 | + void* real_stack_frame = __asan_addr_is_in_fake_stack( |
| 47 | + asan_fake_stack, address, &fake_frame_begin, &fake_frame_end); |
| 48 | + if (real_stack_frame) { |
| 49 | + // |address| points to a fake frame. Check that the fake frame is part |
| 50 | + // of this stack. |
| 51 | + if (stack_start >= real_stack_frame && real_stack_frame >= stack_end) { |
| 52 | + // Iterate the fake frame. |
| 53 | + for (void** current = reinterpret_cast<void**>(fake_frame_begin); |
| 54 | + current < fake_frame_end; ++current) { |
| 55 | + void* addr = *current; |
| 56 | + if (addr == nullptr) continue; |
| 57 | + visitor->VisitPointer(addr); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#endif // V8_USE_ADDRESS_SANITIZER |
| 65 | + |
| 66 | +#ifdef V8_TARGET_ARCH_X64 |
| 67 | + |
| 68 | +void IterateSafeStackIfNecessary(StackVisitor* visitor) { |
| 69 | +#if defined(__has_feature) |
| 70 | +#if __has_feature(safe_stack) |
| 71 | + // Source: |
| 72 | + // https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/safestack/safestack.cpp |
| 73 | + constexpr size_t kSafeStackAlignmentBytes = 16; |
| 74 | + void* stack_end = __builtin___get_unsafe_stack_ptr(); |
| 75 | + void* stack_start = __builtin___get_unsafe_stack_top(); |
| 76 | + CHECK_GT(stack_start, stack_end); |
| 77 | + CHECK_EQ(0u, reinterpret_cast<uintptr_t>(stack_end) & |
| 78 | + (kSafeStackAlignmentBytes - 1)); |
| 79 | + CHECK_EQ(0u, reinterpret_cast<uintptr_t>(stack_start) & |
| 80 | + (kSafeStackAlignmentBytes - 1)); |
| 81 | + void** current = reinterpret_cast<void**>(stack_end); |
| 82 | + for (; current < stack_start; ++current) { |
| 83 | + void* address = *current; |
| 84 | + if (address == nullptr) continue; |
| 85 | + visitor->VisitPointer(address); |
| 86 | + } |
| 87 | +#endif // __has_feature(safe_stack) |
| 88 | +#endif // defined(__has_feature) |
| 89 | +} |
| 90 | + |
| 91 | +#endif // V8_TARGET_ARCH_X64 |
| 92 | + |
| 93 | +} // namespace |
| 94 | + |
| 95 | +#ifdef V8_TARGET_ARCH_X64 |
| 96 | +void Stack::IteratePointers(StackVisitor* visitor) const { |
| 97 | + PushAllRegistersAndIterateStack(this, visitor, &Stack::IteratePointersImpl); |
| 98 | + // No need to deal with callee-saved registers as they will be kept alive by |
| 99 | + // the regular conservative stack iteration. |
| 100 | + IterateSafeStackIfNecessary(visitor); |
| 101 | +} |
| 102 | +#endif // V8_TARGET_ARCH_X64 |
| 103 | + |
| 104 | +// No ASAN support as method accesses redzones while walking the stack. |
| 105 | +NO_SANITIZE_ADDRESS |
| 106 | +void Stack::IteratePointersImpl(StackVisitor* visitor, |
| 107 | + intptr_t* stack_end) const { |
| 108 | +#ifdef V8_USE_ADDRESS_SANITIZER |
| 109 | + void* asan_fake_stack = __asan_get_current_fake_stack(); |
| 110 | +#endif // V8_USE_ADDRESS_SANITIZER |
| 111 | + // All supported platforms should have their stack aligned to at least |
| 112 | + // sizeof(void*). |
| 113 | + constexpr size_t kMinStackAlignment = sizeof(void*); |
| 114 | + // Redzone should not contain any pointers as the iteration is always called |
| 115 | + // from the assembly trampoline. If inline assembly is ever inlined through |
| 116 | + // LTO this may become necessary. |
| 117 | + constexpr size_t kRedZoneBytes = 128; |
| 118 | + void** current = reinterpret_cast<void**>( |
| 119 | + reinterpret_cast<uintptr_t>(stack_end - kRedZoneBytes)); |
| 120 | + CHECK_EQ(0u, reinterpret_cast<uintptr_t>(current) & (kMinStackAlignment - 1)); |
| 121 | + for (; current < stack_start_; ++current) { |
| 122 | + // MSAN: Instead of unpoisoning the whole stack, the slot's value is copied |
| 123 | + // into a local which is unpoisoned. |
| 124 | + void* address = *current; |
| 125 | + MSAN_UNPOISON(address, sizeof(address)); |
| 126 | + if (address == nullptr) continue; |
| 127 | + visitor->VisitPointer(address); |
| 128 | +#ifdef V8_USE_ADDRESS_SANITIZER |
| 129 | + IterateAsanFakeFrameIfNecessary(visitor, asan_fake_stack, stack_start_, |
| 130 | + stack_end, address); |
| 131 | +#endif // V8_USE_ADDRESS_SANITIZER |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace internal |
| 136 | +} // namespace cppgc |
0 commit comments