| 6 |
| 7 | #include "base/compiler_specific.h" |
| 8 |
| 9 | namespace base { |
| 10 |
| 11 | StackCopier::~StackCopier() = default; |
| 12 |
| 13 | // static |
| 14 | uintptr_t StackCopier::RewritePointerIfInOriginalStack( |
| 15 | const uint8_t* original_stack_bottom, |
| 16 | const uintptr_t* original_stack_top, |
| 17 | const uint8_t* stack_copy_bottom, |
| 18 | uintptr_t pointer) { |
| 19 | auto original_stack_bottom_uint = |
| 20 | reinterpret_cast<uintptr_t>(original_stack_bottom); |
| 21 | auto original_stack_top_uint = |
| 22 | reinterpret_cast<uintptr_t>(original_stack_top); |
| 23 | auto stack_copy_bottom_uint = reinterpret_cast<uintptr_t>(stack_copy_bottom); |
| 24 |
| 25 | if (pointer < original_stack_bottom_uint || |
| 26 | pointer >= original_stack_top_uint) |
| 27 | return pointer; |
| 28 |
| 29 | return stack_copy_bottom_uint + (pointer - original_stack_bottom_uint); |
| 30 | } |
| 31 |
| 32 | // static |
| 33 | NO_SANITIZE("address") |
| 34 | const uint8_t* StackCopier::CopyStackContentsAndRewritePointers( |
| 35 | const uint8_t* original_stack_bottom, |
| 36 | const uintptr_t* original_stack_top, |
| 37 | int platform_stack_alignment, |
| 38 | uintptr_t* stack_buffer_bottom) { |
| 39 | const uint8_t* byte_src = original_stack_bottom; |
| 40 | // The first address in the stack with pointer alignment. Pointer-aligned |
| 41 | // values from this point to the end of the stack are possibly rewritten using |
| 42 | // RewritePointerIfInOriginalStack(). Bytes before this cannot be a pointer |
| 43 | // because they occupy less space than a pointer would. |
| 44 | const uint8_t* first_aligned_address = reinterpret_cast<uint8_t*>( |
| 45 | (reinterpret_cast<uintptr_t>(byte_src) + sizeof(uintptr_t) - 1) & |
| 46 | ~(sizeof(uintptr_t) - 1)); |
| 47 |
| 48 | // The stack copy bottom, which is offset from |stack_buffer_bottom| by the |
| 49 | // same alignment as in the original stack. This guarantees identical |
| 50 | // alignment between values in the original stack and the copy. This uses the |
| 51 | // platform stack alignment rather than pointer alignment so that the stack |
| 52 | // copy is aligned to platform expectations. |
| 53 | uint8_t* stack_copy_bottom = |
| 54 | reinterpret_cast<uint8_t*>(stack_buffer_bottom) + |
| 55 | (reinterpret_cast<uintptr_t>(byte_src) & (platform_stack_alignment - 1)); |
| 56 | uint8_t* byte_dst = stack_copy_bottom; |
| 57 |
| 58 | // Copy bytes verbatim up to the first aligned address. |
| 59 | for (; byte_src < first_aligned_address; ++byte_src, ++byte_dst) |
| 60 | *byte_dst = *byte_src; |
| 61 |
| 62 | // Copy the remaining stack by pointer-sized values, rewriting anything that |
| 63 | // looks like a pointer into the stack. |
| 64 | const uintptr_t* src = reinterpret_cast<const uintptr_t*>(byte_src); |
| 65 | uintptr_t* dst = reinterpret_cast<uintptr_t*>(byte_dst); |
| 66 | for (; src < original_stack_top; ++src, ++dst) { |
| 67 | *dst = RewritePointerIfInOriginalStack( |
| 68 | original_stack_bottom, original_stack_top, stack_copy_bottom, *src); |
| 69 | } |
| 70 |
| 71 | return stack_copy_bottom; |
| 72 | } |
| 73 |
| 74 | } // namespace base |
| 75 |