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

Commit

Permalink
Use LLVM libunwind for stack unwinding on Unix
Browse files Browse the repository at this point in the history
This change replaces external libunwind usage on Unix by a copy of the LLVM libunwind.
It also modifies libunwind to support reporting register location and implements
the unw_get_save_loc API to get this location.
For now, only memory locations are supported (register locations are not).
  • Loading branch information
janvorli committed Dec 1, 2016
1 parent a48e1a6 commit c6571c6
Show file tree
Hide file tree
Showing 17 changed files with 472 additions and 245 deletions.
2 changes: 1 addition & 1 deletion Documentation/prerequisites-for-building.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PowerShell also needs to be available from the PATH environment variable (it's t
Install basic dependency packages:

```
sudo apt-get install cmake llvm-3.5 clang-3.5 lldb-3.6 lldb-3.6-dev libunwind8 libunwind8-dev liblttng-ust liblttng-ust-dev uuid uuid-dev
sudo apt-get install cmake llvm-3.5 clang-3.5 lldb-3.6 lldb-3.6-dev liblttng-ust liblttng-ust-dev uuid uuid-dev
```

# Mac OSX (10.10+)
Expand Down
59 changes: 59 additions & 0 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.NET Core uses third-party libraries or other resources that may be
distributed under licenses different than the .NET Core software.

Attributions and licence notices for test cases originally authored by
third parties can be found in the respective test directories.

In the event that we accidentally failed to list a required notice, please
bring it to our attention. Post an issue or email us:

[email protected]

The attached notices are provided for information only.

License notice for The LLVM Compiler Infrastructure
---------------------------------------------------
==============================================================================
LLVM Release License
==============================================================================
University of Illinois/NCSA
Open Source License

Copyright (c) 2003-2015 University of Illinois at Urbana-Champaign.
All rights reserved.

Developed by:

LLVM Team

University of Illinois at Urbana-Champaign

http://llvm.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.

* Neither the names of the LLVM Team, University of Illinois at
Urbana-Champaign, nor the names of its contributors may be used to
endorse or promote products derived from this Software without specific
prior written permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
SOFTWARE.

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
27 changes: 26 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,14 +91,32 @@ if(WIN32)
else()

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

# Disable building _Unwind_XXX style APIs of libunwind, since we don't use them.
add_definitions(-D_LIBUNWIND_DISABLE_ZERO_COST_APIS=1)

# Compile unwinding only for the current compilation target architecture
add_definitions(-D_LIBUNWIND_IS_NATIVE_ONLY)

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_DARWIN)
list(APPEND FULL_RUNTIME_SOURCES
../libunwind/src/Unwind_AppleExtras.cpp
)
endif()

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

list(APPEND RUNTIME_SOURCES_ARCH_ASM
../libunwind/src/UnwindRegistersRestore.S
../libunwind/src/UnwindRegistersSave.S
)
endif()

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

#ifndef APP_LOCAL_RUNTIME
#if !defined(APP_LOCAL_RUNTIME) && !defined(USE_PORTABLE_HELPERS)
#ifndef PLATFORM_UNIX
PalAddVectoredExceptionHandler(1, RhpVectoredExceptionHandler);
#else
PalSetHardwareExceptionHandler(RhpHardwareExceptionHandler);
#endif
#endif
#endif // !APP_LOCAL_RUNTIME && !USE_PORTABLE_HELPERS

//
// init per-instance state
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 // !USE_PORTABLE_HELPERS
int status = pthread_key_create(&g_threadKey, TlsObjectDestructor);
if (status != 0)
{
Expand Down
68 changes: 0 additions & 68 deletions src/Native/Runtime/unix/UnixContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "regdisplay.h"
#include "config.h"

#if !HAVE_LIBUNWIND_H
#error Don't know how to unwind on this platform
#endif

#include <libunwind.h>

#if HAVE_UCONTEXT_T
Expand Down Expand Up @@ -201,56 +197,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 +255,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 +275,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 +293,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 +536,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 +545,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 +555,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;
}
4 changes: 0 additions & 4 deletions src/Native/Runtime/unix/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#cmakedefine01 HAVE_SYS_VMPARAM_H
#cmakedefine01 HAVE_MACH_VM_TYPES_H
#cmakedefine01 HAVE_MACH_VM_PARAM_H
#cmakedefine01 HAVE_LIBUNWIND_H

#cmakedefine01 HAVE_PTHREAD_ATTR_GET_NP
#cmakedefine01 HAVE_PTHREAD_GETATTR_NP
Expand All @@ -15,14 +14,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
33 changes: 0 additions & 33 deletions src/Native/Runtime/unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_FILE_OFFSET_BITS=64)
check_include_files(sys/vmparam.h HAVE_SYS_VMPARAM_H)
check_include_files(mach/vm_types.h HAVE_MACH_VM_TYPES_H)
check_include_files(mach/vm_param.h HAVE_MACH_VM_PARAM_H)
check_include_files(libunwind.h HAVE_LIBUNWIND_H)

check_library_exists(pthread pthread_attr_get_np "" HAVE_PTHREAD_ATTR_GET_NP)
check_library_exists(pthread pthread_getattr_np "" HAVE_PTHREAD_GETATTR_NP)
Expand All @@ -29,20 +28,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 +38,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 Expand Up @@ -133,9 +105,4 @@ int main()
exit(ret);
}" HAVE_MACH_ABSOLUTE_TIME)

if(NOT HAVE_LIBUNWIND_H)
unset(HAVE_LIBUNWIND_H CACHE)
message(FATAL_ERROR "Cannot find libunwind. Try installing libunwind8 and libunwind8-dev (or the appropriate packages for your platform)")
endif()

configure_file(${CMAKE_CURRENT_LIST_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
Loading

0 comments on commit c6571c6

Please sign in to comment.