Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Start using LLVM libunwind
Browse files Browse the repository at this point in the history
  • Loading branch information
janvorli committed Nov 24, 2016
1 parent cac5d0e commit 9317993
Show file tree
Hide file tree
Showing 41 changed files with 12,824 additions and 98 deletions.
1 change: 0 additions & 1 deletion src/BuildIntegration/Microsoft.NETCore.Native.Unix.targets
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ See the LICENSE file in the project root for more information.
<LinkerArg Include="-ldl" />
<LinkerArg Include="-lm" />
<LinkerArg Include="-lrt" Condition="'$(TargetOS)' != 'OSX'" />
<LinkerArg Include="-lunwind-x86_64" Condition="'$(TargetOS)' != 'OSX'" />
</ItemGroup>

<MakeDir Directories="$([System.IO.Path]::GetDirectoryName($(NativeBinary)))" />
Expand Down
15 changes: 14 additions & 1 deletion src/Native/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ if(WIN32)
list(APPEND COMMON_RUNTIME_SOURCES
windows/PalRedhawkCommon.cpp
windows/PalRedhawkMinWin.cpp
)

list(APPEND FULL_RUNTIME_SOURCES
windows/CoffNativeCodeManager.cpp
)

Expand All @@ -88,12 +91,18 @@ if(WIN32)
else()

include_directories(unix)
include_directories(../libunwind/include)

list(APPEND COMMON_RUNTIME_SOURCES
unix/HardwareExceptions.cpp
unix/PalRedhawkUnix.cpp
)

list(APPEND FULL_RUNTIME_SOURCES
unix/HardwareExceptions.cpp
unix/UnixContext.cpp
unix/UnixNativeCodeManager.cpp
../libunwind/src/Unwind-EHABI.cpp
../libunwind/src/libunwind.cpp
)

if(CLR_CMAKE_PLATFORM_ARCH_AMD64)
Expand All @@ -107,6 +116,10 @@ else()
set(ASM_SUFFIX S)
endif()

list(APPEND RUNTIME_SOURCES_ARCH_ASM
../libunwind/src/UnwindRegistersRestore.${ASM_SUFFIX}
../libunwind/src/UnwindRegistersSave.${ASM_SUFFIX}
)
endif()

list(APPEND RUNTIME_SOURCES_ARCH_ASM
Expand Down
2 changes: 2 additions & 0 deletions src/Native/Runtime/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ bool InitDLL(HANDLE hPalInstance)
if (!RestrictedCallouts::Initialize())
return false;

#ifndef USE_PORTABLE_HELPERS
#ifndef APP_LOCAL_RUNTIME
#ifndef PLATFORM_UNIX
PalAddVectoredExceptionHandler(1, RhpVectoredExceptionHandler);
#else
PalSetHardwareExceptionHandler(RhpHardwareExceptionHandler);
#endif
#endif
#endif

//
Expand Down
4 changes: 2 additions & 2 deletions src/Native/Runtime/unix/PalRedhawkUnix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,12 +443,12 @@ REDHAWK_PALEXPORT bool REDHAWK_PALAPI PalInit()
{
return false;
}

#ifndef USE_PORTABLE_HELPERS
if (!InitializeHardwareExceptionHandling())
{
return false;
}

#endif
int status = pthread_key_create(&g_threadKey, TlsObjectDestructor);
if (status != 0)
{
Expand Down
64 changes: 0 additions & 64 deletions src/Native/Runtime/unix/UnixContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,56 +201,6 @@

#endif // __APPLE__

#if UNWIND_CONTEXT_IS_UCONTEXT_T

#if defined(_AMD64_)
#define ASSIGN_UNWIND_REGS \
ASSIGN_REG(Rip, IP) \
ASSIGN_REG(Rsp, SP) \
ASSIGN_REG_PTR(Rbp, Rbp) \
ASSIGN_REG_PTR(Rbx, Rbx) \
ASSIGN_REG_PTR(R12, R12) \
ASSIGN_REG_PTR(R13, R13) \
ASSIGN_REG_PTR(R14, R14) \
ASSIGN_REG_PTR(R15, R15)
#elif defined(_ARM64_)
#define ASSIGN_UNWIND_REGS \
ASSIGN_REG(Pc, IP)
// ASSIGN_REG(Sp, SP) \
// ASSIGN_REG_PTR(Fp, FP) \
// ASSIGN_REG_PTR(Lr, LR) \
// ASSIGN_REG_PTR(X19, X19) \
// ASSIGN_REG_PTR(X20, X20) \
// ASSIGN_REG_PTR(X21, X21) \
// ASSIGN_REG_PTR(X22, X22) \
// ASSIGN_REG_PTR(X23, X23) \
// ASSIGN_REG_PTR(X24, X24) \
// ASSIGN_REG_PTR(X25, X25) \
// ASSIGN_REG_PTR(X26, X26) \
// ASSIGN_REG_PTR(X27, X27) \
// ASSIGN_REG_PTR(X28, X28)
#else
#error unsupported architecture
#endif

// Convert REGDISPLAY to unw_context_t
static void RegDisplayToUnwindContext(REGDISPLAY* regDisplay, unw_context_t *unwContext)
{
#define ASSIGN_REG(regName1, regName2) \
MCREG_##regName1(unwContext->uc_mcontext) = regDisplay->regName2;

#define ASSIGN_REG_PTR(regName1, regName2) \
if (regDisplay->p##regName2 != NULL) \
MCREG_##regName1(unwContext->uc_mcontext) = *(regDisplay->p##regName2);

ASSIGN_UNWIND_REGS

#undef ASSIGN_REG
#undef ASSIGN_REG_PTR
}

#else // UNWIND_CONTEXT_IS_UCONTEXT_T

// Update unw_context_t from REGDISPLAY
static void RegDisplayToUnwindContext(REGDISPLAY* regDisplay, unw_context_t *unwContext)
{
Expand Down Expand Up @@ -309,20 +259,17 @@ static void RegDisplayToUnwindCursor(REGDISPLAY* regDisplay, unw_cursor_t *curso
#undef ASSIGN_REG_PTR
#endif // _AMD64_
}
#endif // UNWIND_CONTEXT_IS_UCONTEXT_T

// Initialize unw_cursor_t and unw_context_t from REGDISPLAY
bool InitializeUnwindContextAndCursor(REGDISPLAY* regDisplay, unw_cursor_t* cursor, unw_context_t* unwContext)
{
int st;

#if !UNWIND_CONTEXT_IS_UCONTEXT_T
st = unw_getcontext(unwContext);
if (st < 0)
{
return false;
}
#endif

RegDisplayToUnwindContext(regDisplay, unwContext);

Expand All @@ -332,18 +279,15 @@ bool InitializeUnwindContextAndCursor(REGDISPLAY* regDisplay, unw_cursor_t* curs
return false;
}

#if !UNWIND_CONTEXT_IS_UCONTEXT_T
// Set the unwind context to the specified windows context
RegDisplayToUnwindCursor(regDisplay, cursor);
#endif

return true;
}

// Update context pointer for a register from the unw_cursor_t.
static void GetContextPointer(unw_cursor_t *cursor, unw_context_t *unwContext, int reg, PTR_UIntNative *contextPointer)
{
#if defined(HAVE_UNW_GET_SAVE_LOC)
unw_save_loc_t saveLoc;
unw_get_save_loc(cursor, reg, &saveLoc);
if (saveLoc.type == UNW_SLT_MEMORY)
Expand All @@ -353,10 +297,6 @@ static void GetContextPointer(unw_cursor_t *cursor, unw_context_t *unwContext, i
if (unwContext == NULL || (pLoc < (PTR_UIntNative)unwContext) || ((PTR_UIntNative)(unwContext + 1) <= pLoc))
*contextPointer = (PTR_UIntNative)saveLoc.u.addr;
}
#else
// Returning NULL indicates that we don't have context pointers available
*contextPointer = NULL;
#endif
}

#if defined(_AMD64_)
Expand Down Expand Up @@ -600,7 +540,6 @@ bool VirtualUnwind(REGDISPLAY* pRegisterSet)
return false;
}

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_ARM64_) || defined(_ARM_)
// FreeBSD, NetBSD and OSX appear to do two different things when unwinding
// 1: If it reaches where it cannot unwind anymore, say a
// managed frame. It wil return 0, but also update the $pc
Expand All @@ -610,7 +549,6 @@ bool VirtualUnwind(REGDISPLAY* pRegisterSet)
// So we bank the original PC here, so we can compare it after
// the step
uintptr_t curPc = pRegisterSet->GetIP();
#endif

int st = unw_step(&cursor);
if (st < 0)
Expand All @@ -621,13 +559,11 @@ bool VirtualUnwind(REGDISPLAY* pRegisterSet)
// Update the REGDISPLAY to reflect the unwind
UnwindCursorToRegDisplay(&cursor, &unwContext, pRegisterSet);

#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(_ARM64_) || defined(_ARM_)
if (st == 0 && pRegisterSet->GetIP() == curPc)
{
// TODO: is this correct for CoreRT? Should we return false instead?
pRegisterSet->SetIP(0);
}
#endif

return true;
}
3 changes: 0 additions & 3 deletions src/Native/Runtime/unix/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
#cmakedefine01 HAVE_SYSCTL
#cmakedefine01 HAVE_SYSCONF

#cmakedefine HAVE_UNW_GET_SAVE_LOC

#cmakedefine01 HAVE_GREGSET_T
#cmakedefine01 HAVE___GREGSET_T

#cmakedefine01 HAVE_SIGINFO_T
#cmakedefine01 HAVE_UCONTEXT_T
#cmakedefine01 UNWIND_CONTEXT_IS_UCONTEXT_T

#cmakedefine01 HAVE__SC_PHYS_PAGES
#cmakedefine01 HAVE__SC_AVPHYS_PAGES
Expand Down
27 changes: 0 additions & 27 deletions src/Native/Runtime/unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ check_function_exists(clock_nanosleep HAVE_CLOCK_NANOSLEEP)
check_function_exists(sysctl HAVE_SYSCTL)
check_function_exists(sysconf HAVE_SYSCONF)

set(CMAKE_REQUIRED_LIBRARIES unwind unwind-generic)
check_cxx_source_compiles("
#include <libunwind.h>
int main(int argc, char **argv) {
unw_cursor_t cursor;
unw_save_loc_t saveLoc;
int reg = UNW_REG_IP;
unw_get_save_loc(&cursor, reg, &saveLoc);
return 0;
}" HAVE_UNW_GET_SAVE_LOC)
set(CMAKE_REQUIRED_LIBRARIES)

check_struct_has_member ("ucontext_t" uc_mcontext.gregs[0] ucontext.h HAVE_GREGSET_T)
check_struct_has_member ("ucontext_t" uc_mcontext.__gregs[0] ucontext.h HAVE___GREGSET_T)

Expand All @@ -53,19 +39,6 @@ set(CMAKE_EXTRA_INCLUDE_FILES)
set(CMAKE_EXTRA_INCLUDE_FILES ucontext.h)
check_type_size(ucontext_t UCONTEXT_T)

check_cxx_source_compiles("
#include <libunwind.h>
#include <ucontext.h>
int main(int argc, char **argv)
{
unw_context_t libUnwindContext;
ucontext_t uContext;
libUnwindContext = uContext;
return 0;
}" UNWIND_CONTEXT_IS_UCONTEXT_T)

check_cxx_symbol_exists(_SC_PHYS_PAGES unistd.h HAVE__SC_PHYS_PAGES)
check_cxx_symbol_exists(_SC_AVPHYS_PAGES unistd.h HAVE__SC_AVPHYS_PAGES)

Expand Down
4 changes: 4 additions & 0 deletions src/Native/libunwind/.arcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"project_id" : "libunwind",
"conduit_uri" : "https://reviews.llvm.org/"
}
2 changes: 2 additions & 0 deletions src/Native/libunwind/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: LLVM

Loading

0 comments on commit 9317993

Please sign in to comment.