Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfaults during unwinding after throwing exception #774

Closed
rcdailey opened this issue Aug 17, 2018 · 7 comments
Closed

Segfaults during unwinding after throwing exception #774

rcdailey opened this issue Aug 17, 2018 · 7 comments

Comments

@rcdailey
Copy link

rcdailey commented Aug 17, 2018

I'm observing that when I throw exceptions from my code, I get segfaults in destructors of local objects. Example of what I see in Android Studio:

SIGSEGV (signal SIGSEGV: address access protected (fault address: 0x5f023dd9))

In my case, the call stack shows it is in the destructor of one of my local objects (a custom string class). Once the exception is thrown, this string would obviously be destructed as part of the unwinding process.

Here are the details of how I have built my code:

NDK Version: r17b
Minimum API: android-15
ABI: armeabi-v7a
Actual API of the device: android-17
STL: libc++ (shared)
Toolchain: clang

I never had this issue using gnustl (shared). I am attempting to migrate to libc++ which is why I'm hitting this now. Is this a bug? I don't see how it could be my code since this code has worked for years using gnustl. Please let me know what I can do to help. I'm able to set breakpoints and debug on device but it doesn't do me any good since I can't investigate a root cause for this from my code alone.

@rcdailey
Copy link
Author

Looks like #379 might be related?

@rcdailey
Copy link
Author

Per the example I saw in #289, I ran the readelf command on my shared library (the one generating the segfault):

# "C:\android\ndk-standalone\r17b-arm\bin\arm-linux-androideabi-readelf.exe" -sW libzPayService.so | grep _Unwind
    10: 00000000     0 FUNC    GLOBAL DEFAULT  UND _Unwind_Resume
1806268: 00000000     0 FUNC    GLOBAL DEFAULT  UND _Unwind_Resume

According to one of the comments there, these should not be UND right? I'm using Android Studio 3.3 Canary 6 to build this library, and the gradle build tools invoke CMake with the NDK toolchain file as follows:

15:33:10.573 [INFO] [org.gradle.api.Project] External native JSON generation. armDebug|armeabi-v7a : executing cmake Executable : C:\Program Files\CMake\bin\cmake.exe
arguments : 
-HE:\code\frontend2\source
-BE:\code\frontend2\source\Applications\zPayService\.externalNativeBuild\cmake\armDebug\armeabi-v7a
-DANDROID_ABI=armeabi-v7a
-DANDROID_PLATFORM=android-15
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\code\frontend2\source\Applications\zPayService\build\intermediates\cmake\arm\debug\obj\armeabi-v7a
-DCMAKE_BUILD_TYPE=Debug
-DANDROID_NDK=C:\android\android-ndk-r17b
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a
-DCMAKE_SYSTEM_VERSION=15
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_ANDROID_NDK=C:\android\android-ndk-r17b
-DCMAKE_TOOLCHAIN_FILE=C:\android\android-ndk-r17b\build\cmake\android.toolchain.cmake
-G Ninja
-DANDROID_STL=c++_shared
-DZIOSK_BUILD_VERSION_OVERRIDE=7.2.4.0
jvmArgs : 

This all looks correct to me, so why are unwind handlers not defined properly?

@rcdailey
Copy link
Author

Tested on r16b and I'm still seeing the UND on those unwind symbols

@rcdailey
Copy link
Author

rcdailey commented Aug 17, 2018

Example command line invocation of compiler through CMake using ndk r16b:

C:\android\android-ndk-r16b\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi --gcc-toolchain=C:/android/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=C:/android/android-ndk-r16b/sysroot -DANDROID -DBETTER_ENUMS_STRICT_CONVERSION -DBOOST_ALL_NO_LIB -DBOOST_BIND_NO_PLACEHOLDERS -DBOOST_FILESYSTEM_NO_DEPRECATED -DBOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX -DCEREAL_RAPIDJSON_NAMESPACE=cereal_rapidjson -DMAGICKCORE_HDRI_ENABLE=0 -DMAGICKCORE_QUANTUM_DEPTH=8 -DNOAUTOLINK_MAGICK -DSTATIC_MAGICK -DZIOSK_ENABLE_DEVELOPER_LOGS -DZIOSK_ENABLE_ZPAY_DIAGNOSTICS -DZIOSK_MODULE_NAME="zPayServiceInterface" -D_MAGICKLIB_ -IE:/code/frontend2/source/Applications/zPayService/Interface/Source -IE:/code/frontend2/source/Core/UI/Source -ICore/UI/Source -IE:/code/frontend2/source/Core/ThirdParty/PowerVR/sdk/Include -IE:/code/frontend2/source/Core/ThirdParty/PowerVR/tools/include -IE:/code/frontend2/source/Core/ThirdParty/PowerVR/tools/include/OGLES2 -IE:/code/frontend2/source/Core/ThirdParty/boost/include -IE:/code/frontend2/source/Core/ThirdParty/openssl/include -IE:/code/frontend2/source/Core/ThirdParty/sqlite/. -IE:/code/frontend2/source/Core/ThirdParty/cereal/cereal/include -IE:/code/frontend2/source/Core/ThirdParty/cereal/cereal_optional_nvp/include -IE:/code/frontend2/source/Core/ThirdParty/rapidxml/include -IE:/code/frontend2/source/Core/ThirdParty/better-enums/include -IE:/code/frontend2/source/Core/ThirdParty/libpng/include -IE:/code/frontend2/source/Core/ThirdParty/ImageMagick/include/android -IE:/code/frontend2/source/Core/ThirdParty/duktape/src -IE:/code/frontend2/source/Core/ThirdParty/uri/source/include -IE:/code/frontend2/source/Core/ThirdParty/fmt/include -IE:/code/frontend2/source/Core/MsrInterface/Source -IE:/code/frontend2/source/Core/ThirdParty/bsp/msr/include -IC:/android/android-ndk-r16b/sources/android/cpufeatures -IC:/android/android-ndk-r16b/sources/android/native_app_glue -IE:/code/frontend2/source/Services/Source -IE:/code/frontend2/source/Core/WebServices/Source -IE:/code/frontend2/source/Core/ThirdParty/bsp/zpaylib/include -isystem C:/android/android-ndk-r16b/sources/cxx-stl/llvm-libc++/include -isystem C:/android/android-ndk-r16b/sources/android/support/include -isystem C:/android/android-ndk-r16b/sources/cxx-stl/llvm-libc++abi/include -isystem C:/android/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -O0 -fno-limit-debug-info -Wno-inconsistent-missing-override -Werror -std=c++14 -MD -MT Applications/zPayService/Interface/CMakeFiles/zPayServiceInterface.dir/Source/zPayService/Protocol/ZpayTransportLibrary.cpp.o -MF Applications\zPayService\Interface\CMakeFiles\zPayServiceInterface.dir\Source\zPayService\Protocol\ZpayTransportLibrary.cpp.o.d -o Applications/zPayService/Interface/CMakeFiles/zPayServiceInterface.dir/Source/zPayService/Protocol/ZpayTransportLibrary.cpp.o -c E:/code/frontend2/source/Applications/zPayService/Interface/Source/zPayService/Protocol/ZpayTransportLibrary.cpp

And the linker command it prints:

cmd.exe /C "cd . && C:\android\android-ndk-r16b\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi --gcc-toolchain=C:/android/android-ndk-r16b/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 --sysroot=C:/android/android-ndk-r16b/sysroot -fPIC -isystem C:/android/android-ndk-r16b/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot C:/android/android-ndk-r16b/platforms/android-15/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -LC:/android/android-ndk-r16b/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -u ANativeActivity_onCreate -shared -Wl,-soname,libzPayService.so -o output\bin\libzPayService.so @CMakeFiles\zPayService.rsp && cd ."

@rcdailey
Copy link
Author

I rebuilt some libraries and this went away.

@DanAlbert
Copy link
Member

Yeah, that's usually the case with this issue these days. What probably happened above is that the libraries/objects/etc listed in the response file had a shared library that wasn't built with --exclude-libs linked before libgcc/libunwind (if libgcc isn't listed explicitly, it's added to the very end by the computer, which is iirc the usual case for cmake). When that happens the linker finds the public symbols for the unwjnder and loads them for that library instead of including them from the static library.

@rcdailey
Copy link
Author

rcdailey commented Aug 17, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants