| 1 |
//===- unittest/Support/ProgramStackTest.cpp ------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/Support/ProgramStack.h" |
| 10 |
#include "llvm/Support/Process.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
|
| 13 |
using namespace llvm; |
| 14 |
|
| 15 |
// One way of running on a new stack is to start a new thread. When threading |
| 16 |
// is disabled a "new thread" is implemented as simply calling the function. |
| 17 |
#if LLVM_ENABLE_THREADS |
| 18 |
|
| 19 |
static uintptr_t func(int &A) { |
| 20 |
A = 7; |
| 21 |
return getStackPointer(); |
| 22 |
} |
| 23 |
|
| 24 |
static void func2(int &A) { |
| 25 |
A = 5; |
| 26 |
} |
| 27 |
|
| 28 |
TEST(ProgramStackTest, runOnNewStack) { |
| 29 |
int A = 0; |
| 30 |
uintptr_t Stack = runOnNewStack(0, function_ref<uintptr_t(int &)>(func), A); |
| 31 |
EXPECT_EQ(A, 7); |
| 32 |
intptr_t StackDiff = (intptr_t)llvm::getStackPointer() - (intptr_t)Stack; |
| 33 |
size_t StackDistance = (size_t)std::abs(StackDiff); |
| 34 |
// Page size is used as it's large enough to guarantee were not on the same |
| 35 |
// stack but not too large to cause spurious failures. |
| 36 |
EXPECT_GT(StackDistance, llvm::sys::Process::getPageSizeEstimate()); |
| 37 |
runOnNewStack(0, function_ref<void(int &)>(func2), A); |
| 38 |
EXPECT_EQ(A, 5); |
| 39 |
} |
| 40 |
|
| 41 |
#endif /*if LLVM_ENABLE_THREADS */ |