Skip to content

Commit 7ad1a98

Browse files
authored
Remove druntime's libunwind dependency (#4691)
* Reuse Darwin backtracing code for DRuntime_Use_Libunwind and use same code path as libexec-based backtracing. * Remove linking with libunwind * Run two tests manually for musl.
1 parent a0318c2 commit 7ad1a98

File tree

8 files changed

+47
-209
lines changed

8 files changed

+47
-209
lines changed

.github/workflows/alpine_musl.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
uses: jirutka/setup-alpine@v1
2222
with:
2323
branch: v3.20
24-
packages: ldc git g++ cmake ninja llvm-dev llvm-static compiler-rt libunwind-static libxml2-static zstd-static zlib-static bash grep diffutils make
24+
packages: ldc git g++ cmake ninja llvm-dev llvm-static compiler-rt libxml2-static zstd-static zlib-static bash grep diffutils make
2525

2626
- name: Build LDC bootstrap
2727
shell: alpine.sh {0}
@@ -73,7 +73,16 @@ jobs:
7373
- name: Run DMD testsuite
7474
if: success() || failure()
7575
shell: alpine.sh {0}
76-
run: ctest -V -R "dmd-testsuite"
76+
run: |
77+
# These two tests require extra flags "-link-defaultlib-debug -frame-pointer=all", https://github.com/ldc-developers/ldc/issues/4694.
78+
# Run them separately with these flags, and then remove them before running the full testsuite.
79+
bin/ldc2 -g -L-export-dynamic -link-defaultlib-debug -frame-pointer=all -run tests/dmd/runnable/test17559.d
80+
bin/ldc2 -O -g -L-export-dynamic -link-defaultlib-debug -frame-pointer=all -run tests/dmd/runnable/test17559.d
81+
bin/ldc2 -g -L-export-dynamic -link-defaultlib-debug -frame-pointer=all -run tests/dmd/runnable/test19086.d
82+
bin/ldc2 -O -g -L-export-dynamic -link-defaultlib-debug -frame-pointer=all -run tests/dmd/runnable/test19086.d
83+
rm tests/dmd/runnable/test17559.d
84+
rm tests/dmd/runnable/test19086.d
85+
ctest -V -R "dmd-testsuite"
7786
7887
- name: Run defaultlib unittests & druntime integration tests
7988
if: success() || failure()

driver/linker-gcc.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,6 @@ void ArgsBuilder::addDefaultPlatformLibs() {
694694
args.push_back("-lm");
695695
break;
696696
}
697-
if (triple.isMusl() && !global.params.betterC) {
698-
args.push_back("-lunwind"); // for druntime backtrace
699-
}
700697
args.push_back("-lrt");
701698
args.push_back("-ldl");
702699
// fallthrough

runtime/DRuntimeIntegrationTests.cmake

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ set(linkdl "")
4242
if("${TARGET_SYSTEM}" MATCHES "Linux")
4343
set(linkdl "LINKDL=-L-ldl")
4444
endif()
45-
set(linkunwind "")
46-
if("${TARGET_SYSTEM}" MATCHES "musl")
47-
set(linkunwind "LINKUNWIND=-L-lunwind")
48-
endif()
4945

5046
get_subdirs(testnames ${PROJECT_SOURCE_DIR}/druntime/test)
5147
if(${BUILD_SHARED_LIBS} STREQUAL "OFF")
@@ -78,7 +74,7 @@ foreach(name ${testnames})
7874
COMMAND ${GNU_MAKE_BIN} -C ${PROJECT_SOURCE_DIR}/druntime/test/${name}
7975
ROOT=${outdir} DMD=${LDMD_EXE_FULL} BUILD=${build}
8076
DRUNTIME=${druntime_path_build} DRUNTIMESO=${shared_druntime_path_build}
81-
SHARED=1 ${cflags_base} ${linkdl} ${linkunwind}
77+
SHARED=1 ${cflags_base} ${linkdl}
8278
)
8379
set_tests_properties(${fullname} PROPERTIES DEPENDS clean-${fullname})
8480
endforeach()

runtime/druntime/src/core/internal/backtrace/handler.d

Lines changed: 0 additions & 142 deletions
This file was deleted.

runtime/druntime/src/core/internal/backtrace/unwind.d

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ version (LDC) // simplify runtime function forward declaration
150150
else
151151
void _Unwind_Resume(_Unwind_Exception* exception_object);
152152
_Unwind_Reason_Code _Unwind_Resume_or_Rethrow(_Unwind_Exception* exception_object);
153-
_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void*);
153+
_Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void*) nothrow;
154154

155155
version (ARM_EABI_UNWINDER)
156156
{
@@ -160,46 +160,37 @@ version (ARM_EABI_UNWINDER)
160160
// On ARM, these are macros resp. not visible (static inline). To avoid
161161
// an unmaintainable amount of dependencies on implementation details,
162162
// just use a C shim (in ldc/arm_unwind.c).
163-
_Unwind_Word _d_eh_GetGR(_Unwind_Context* context, int index);
163+
_Unwind_Word _d_eh_GetGR(_Unwind_Context* context, int index) nothrow;
164164
alias _Unwind_GetGR = _d_eh_GetGR;
165165

166-
void _d_eh_SetGR(_Unwind_Context* context, int index, _Unwind_Word new_value);
166+
void _d_eh_SetGR(_Unwind_Context* context, int index, _Unwind_Word new_value) nothrow;
167167
alias _Unwind_SetGR = _d_eh_SetGR;
168168

169-
_Unwind_Ptr _d_eh_GetIP(_Unwind_Context* context);
169+
_Unwind_Ptr _d_eh_GetIP(_Unwind_Context* context) nothrow;
170170
alias _Unwind_GetIP = _d_eh_GetIP;
171171

172-
_Unwind_Ptr _d_eh_GetIPInfo(_Unwind_Context* context, int*);
172+
_Unwind_Ptr _d_eh_GetIPInfo(_Unwind_Context* context, int*) nothrow;
173173
alias _Unwind_GetIPInfo = _d_eh_GetIPInfo;
174174

175-
void _d_eh_SetIP(_Unwind_Context* context, _Unwind_Ptr new_value);
175+
void _d_eh_SetIP(_Unwind_Context* context, _Unwind_Ptr new_value) nothrow;
176176
alias _Unwind_SetIP = _d_eh_SetIP;
177177
}
178178
else
179179
{
180-
_Unwind_Word _Unwind_GetGR(_Unwind_Context* context, int index);
181-
void _Unwind_SetGR(_Unwind_Context* context, int index, _Unwind_Word new_value);
182-
_Unwind_Ptr _Unwind_GetIP(_Unwind_Context* context);
183-
_Unwind_Ptr _Unwind_GetIPInfo(_Unwind_Context* context, int*);
184-
void _Unwind_SetIP(_Unwind_Context* context, _Unwind_Ptr new_value);
180+
_Unwind_Word _Unwind_GetGR(_Unwind_Context* context, int index) nothrow;
181+
void _Unwind_SetGR(_Unwind_Context* context, int index, _Unwind_Word new_value) nothrow;
182+
_Unwind_Ptr _Unwind_GetIP(_Unwind_Context* context) nothrow;
183+
_Unwind_Ptr _Unwind_GetIPInfo(_Unwind_Context* context, int*) nothrow;
184+
void _Unwind_SetIP(_Unwind_Context* context, _Unwind_Ptr new_value) nothrow;
185185
}
186-
_Unwind_Word _Unwind_GetCFA(_Unwind_Context*);
187-
_Unwind_Word _Unwind_GetBSP(_Unwind_Context*);
188-
void* _Unwind_GetLanguageSpecificData(_Unwind_Context*);
189-
_Unwind_Ptr _Unwind_GetRegionStart(_Unwind_Context* context);
190-
void* _Unwind_FindEnclosingFunction(void* pc);
186+
_Unwind_Word _Unwind_GetCFA(_Unwind_Context*) nothrow;
187+
_Unwind_Word _Unwind_GetBSP(_Unwind_Context*) nothrow;
188+
void* _Unwind_GetLanguageSpecificData(_Unwind_Context*) nothrow;
189+
_Unwind_Ptr _Unwind_GetRegionStart(_Unwind_Context* context) nothrow;
190+
void* _Unwind_FindEnclosingFunction(void* pc) nothrow;
191191

192-
version (X68_64)
192+
version (X86_64)
193193
{
194-
_Unwind_Ptr _Unwind_GetDataRelBase(_Unwind_Context* context)
195-
{
196-
return _Unwind_GetGR(context, 1);
197-
}
198-
199-
_Unwind_Ptr _Unwind_GetTextRelBase(_Unwind_Context* context)
200-
{
201-
assert(0);
202-
}
203194
}
204195
else
205196
{

runtime/druntime/src/core/runtime.d

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@ else version (TVOS)
1919
else version (WatchOS)
2020
version = Darwin;
2121

22+
version(LDC) version (Darwin)
23+
{
24+
// Use our own backtrace() based on _Unwind_Backtrace(), as the former (from
25+
// execinfo) doesn't seem to handle missing frame pointers too well.
26+
version = DefineBacktrace_using_UnwindBacktrace;
27+
}
28+
2229
version (DRuntime_Use_Libunwind)
2330
{
24-
import core.internal.backtrace.libunwind;
31+
version = DefineBacktrace_using_UnwindBacktrace;
32+
2533
// This shouldn't be necessary but ensure that code doesn't get mixed
2634
// It does however prevent the unittest SEGV handler to be installed,
2735
// which is desireable as it uses backtrace directly.
@@ -670,24 +678,11 @@ extern (C) UnitTestResult runModuleUnitTests()
670678
return results;
671679
}
672680

673-
version (LDC) version (Darwin)
681+
version (DefineBacktrace_using_UnwindBacktrace)
674682
{
675-
nothrow:
676-
677-
extern (C)
678-
{
679-
enum _URC_NO_REASON = 0;
680-
enum _URC_END_OF_STACK = 5;
683+
import core.internal.backtrace.unwind;
681684

682-
alias _Unwind_Context_Ptr = void*;
683-
alias _Unwind_Trace_Fn = int function(_Unwind_Context_Ptr, void*);
684-
int _Unwind_Backtrace(_Unwind_Trace_Fn, void*);
685-
ptrdiff_t _Unwind_GetIP(_Unwind_Context_Ptr context);
686-
}
687-
688-
// Use our own backtrce() based on _Unwind_Backtrace(), as the former (from
689-
// execinfo) doesn't seem to handle missing frame pointers too well.
690-
private int backtrace(void** buffer, int maxSize)
685+
private int backtrace(void** buffer, int maxSize) nothrow
691686
{
692687
if (maxSize < 0) return 0;
693688

@@ -698,7 +693,7 @@ version (LDC) version (Darwin)
698693
int entriesWritten = 0;
699694
}
700695

701-
static extern(C) int handler(_Unwind_Context_Ptr context, void* statePtr)
696+
static extern(C) int handler(_Unwind_Context* context, void* statePtr)
702697
{
703698
auto state = cast(State*)statePtr;
704699
if (state.entriesWritten >= state.maxSize) return _URC_END_OF_STACK;
@@ -819,14 +814,8 @@ void defaultTraceDeallocator(Throwable.TraceInfo info) nothrow
819814
free(cast(void *)obj);
820815
}
821816

822-
version (DRuntime_Use_Libunwind)
823-
{
824-
import core.internal.backtrace.handler;
825-
826-
alias DefaultTraceInfo = LibunwindHandler;
827-
}
828817
/// Default implementation for most POSIX systems
829-
else version (Posix) private class DefaultTraceInfo : Throwable.TraceInfo
818+
version (Posix) private class DefaultTraceInfo : Throwable.TraceInfo
830819
{
831820
import core.demangle;
832821
import core.stdc.stdlib : free;

runtime/druntime/test/common.mak

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ DMD:=
1212
DRUNTIME:=
1313
DRUNTIMESO:=
1414
LINKDL:=
15-
LINKUNWIND:=
1615
QUIET:=
1716
TIMELIMIT:=
1817
PIC:=
@@ -26,7 +25,6 @@ ifeq (,$(findstring ldmd2,$(DMD)))
2625
endif
2726

2827
LDL:=$(subst -L,,$(LINKDL)) # -ldl
29-
LUNWIND:=$(subst -L,,$(LINKUNWIND)) # -lunwind
3028
SRC:=src
3129
GENERATED:=./generated
3230
ROOT:=$(GENERATED)/$(OS)/$(BUILD)/$(MODEL)

runtime/druntime/test/stdcpp/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,22 @@ $(ROOT)/%_old.done: $(ROOT)/%_old$(DOTEXE)
9898
$(ROOT)/%$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
9999
@mkdir -p $(dir $@)
100100
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++98 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_d$(DOTOBJ) $(SRC)/$*_test.d
101-
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++98 -o $@ $< $(ROOT)/$*_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
101+
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++98 -o $@ $< $(ROOT)/$*_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
102102
# build C++11 tests
103103
$(ROOT)/%_11$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
104104
@mkdir -p $(dir $@)
105105
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++11 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_11_d$(DOTOBJ) $(SRC)/$*_test.d
106-
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++11 -o $@ $< $(ROOT)/$*_11_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
106+
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++11 -o $@ $< $(ROOT)/$*_11_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
107107
# build C++17 tests
108108
$(ROOT)/%_17$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
109109
@mkdir -p $(dir $@)
110110
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++17 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_17_d$(DOTOBJ) $(SRC)/$*_test.d
111-
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++17 -o $@ $< $(ROOT)/$*_17_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
111+
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++17 -o $@ $< $(ROOT)/$*_17_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
112112
# build libstdc++ _GLIBCXX_USE_CXX11_ABI=0 tests
113113
$(ROOT)/%_old$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
114114
@mkdir -p $(dir $@)
115115
$(QUIET)$(DMD) $(DFLAGS) -version=_GLIBCXX_USE_CXX98_ABI -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_old_d$(DOTOBJ) $(SRC)/$*_test.d
116-
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -D_GLIBCXX_USE_CXX11_ABI=0 -o $@ $< $(ROOT)/$*_old_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
116+
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -D_GLIBCXX_USE_CXX11_ABI=0 -o $@ $< $(ROOT)/$*_old_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
117117

118118
endif # end Posix
119119

0 commit comments

Comments
 (0)