felipepessoto opened a new issue, #13820: URL: https://github.com/apache/gluten/issues/13820
### Backend VL (Velox) ### Bug description In vcpkg (static) builds, `libgluten.so` and `libvelox.so` each contain a private, hidden copy of libgcc's unwinder and of the C++ personality routine, and C++ exceptions routinely cross between the two libraries. Each unwinder copy fills its register-size table only when it performs its first unwind. If an exception from one library reaches a `catch` or cleanup in the other before the other library's copy has ever unwound, that library's personality routine calls its own `_Unwind_SetGR`, which fails a `gcc_assert`. The process then gets SIGABRT instead of handling the exception. Current CI-built libraries avoid this only by accident: glog's `StackTraceInit` static initializer calls `_Unwind_Backtrace` in each library at load time, which initializes that library's copy. Nothing in Gluten's build guarantees it. - **Expected:** an exception thrown in one Gluten native library is caught or cleaned up normally in the other, whatever has run before. - **Actual:** if the receiving library's private unwinder hasn't unwound yet, the process aborts. This is a latent hazard: it hasn't been observed in CI, and no production report is known. Builds without vcpkg shouldn't be affected: only the vcpkg toolchain and triplets pass `-static-libgcc`, and otherwise the G++ driver links the shared libgcc. #### Two private unwinders - [`dev/vcpkg/toolchain.cmake`](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/dev/vcpkg/toolchain.cmake#L60-L61) links every shared library with `-static-libstdc++ -static-libgcc`. - libgluten demotes its bundled runtime to local symbols with `-Wl,--exclude-libs` for `libstdc++.a`, `libgcc.a` and `libgcc_eh.a` (#12557, [cpp/core/CMakeLists.txt](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/core/CMakeLists.txt#L162-L189)). libvelox gets the same isolation from the `local: *` in its [version script](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/velox/symbols.map#L11-L13), applied when `ENABLE_GLUTEN_VCPKG` is set ([cpp/velox/CMakeLists.txt](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/velox/CMakeLists.txt#L278-L282)). - CI's static build is the `build-native-lib-centos-8` job ([velox_backend_x86.yml](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/.github/workflows/velox_backend_x86.yml#L238-L294); `apache/gluten:vcpkg-almalinux-8-gcc13`, GCC 13). The Spark test jobs use its `velox-native-lib-centos-8-<sha>` artifact, and release bundles use the same build script ([build_bundle_package.yml](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/.github/workflows/build_bundle_package.yml#L62)). - In that artifact from the scheduled run on main at 73dae76 ([run 45925918143](https://github.com/apache/gluten/actions/runs/45925918143)), neither library needs `libgcc_s.so.1` or `libstdc++.so.6`, neither imports nor exports any `_Unwind_*` symbol, and each has its own local `_Unwind_RaiseException`, `_Unwind_SetGR`, `_Unwind_Backtrace`, `__gxx_personality_v0` and `dwarf_reg_size_table` (checked with `readelf --dyn-syms` and `nm`). #### Exceptions cross between them - Velox errors thrown in libvelox are caught by libgluten's JNI wrappers, for example in `ColumnarBatchOutIterator.nativeHasNext` ([JniWrapper.cc](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/core/jni/JniWrapper.cc#L450-L463)). - `JavaException` is thrown by `JavaException::check` in libgluten ([JavaException.cc](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/core/jni/JavaException.cc#L201-L224)) and caught in libvelox: by `wrapJavaException` ([VeloxJavaException.h](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/velox/jni/VeloxJavaException.h#L27-L43), used by [VeloxInputIterator.cc](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/velox/jni/VeloxInputIterator.cc#L29)), and by `ListenableArbitrator::growCapacityInternal` when a Spark memory reservation fails ([VeloxMemoryManager.cc](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/cpp/velox/memory/VeloxMemoryManager.cc#L170-L178)). - A cleanup (destructor) in a frame of the other library takes the same path as a `catch`. #### Mechanism In libgcc's `unwind-dw2.c` and libstdc++'s `eh_personality.cc` (GCC 13.3.0): 1. `dwarf_reg_size_table` is a file-static array, so each unwinder copy has its own ([unwind-dw2.c#L148-L149](https://github.com/gcc-mirror/gcc/blob/b71f1de6e9cf7181a288c0f39f9b1ef6580cf5c8/libgcc/unwind-dw2.c#L148-L149)). 2. Only `uw_init_context_1` fills it, when that copy starts an unwind: `_Unwind_RaiseException`, `_Unwind_Resume`, `_Unwind_Backtrace` or a forced unwind ([unwind-dw2.c#L1321-L1348](https://github.com/gcc-mirror/gcc/blob/b71f1de6e9cf7181a288c0f39f9b1ef6580cf5c8/libgcc/unwind-dw2.c#L1321-L1348)). 3. In phase 2, the throwing library's unwinder calls the personality routine linked into each frame's library. At every landing pad, handler or cleanup, `__gxx_personality_v0` calls `_Unwind_SetGR` to hand over the exception ([eh_personality.cc#L724-L727](https://github.com/gcc-mirror/gcc/blob/b71f1de6e9cf7181a288c0f39f9b1ef6580cf5c8/libstdc++-v3/libsupc++/eh_personality.cc#L724-L727)), and that call binds to the frame library's own copy. 4. `_Unwind_SetGR` reads the table and asserts the register size ([unwind-dw2.c#L273-L298](https://github.com/gcc-mirror/gcc/blob/b71f1de6e9cf7181a288c0f39f9b1ef6580cf5c8/libgcc/unwind-dw2.c#L273-L298)). In a copy that has never unwound, the table is all zeros, so the assertion fails and libgcc calls `abort()`. #### Minimal reproducer (no Gluten code) `liba.so` throws, `libb.so` catches, and a C `main` calls `libb`: ```bash cat > liba.cc <<'EOF' extern "C" void a_throw() { throw 42; } EOF cat > libb.cc <<'EOF' extern "C" void a_throw(); extern "C" int b_catch() { try { a_throw(); } catch (int v) { return v; } return -1; } #ifdef WARM_UP // run libb's private unwinder once at load time #include <unwind.h> static _Unwind_Reason_Code noop(_Unwind_Context*, void*) { return _URC_NO_REASON; } __attribute__((constructor)) static void warmUp() { _Unwind_Backtrace(noop, nullptr); } #endif EOF cat > main.c <<'EOF' #include <stdio.h> int b_catch(void); int main(void) { printf("b_catch() returned %d\n", b_catch()); return 0; } EOF echo '{ global: a_throw; b_catch; local: *; };' > libs.map RT="-static-libstdc++ -static-libgcc" MAP="-Wl,--version-script=libs.map" EXCL="-Wl,--exclude-libs,libstdc++.a -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_eh.a" run() { # $1 = link flags for both libraries, $2 = extra flags for libb g++ -O2 -fPIC -shared $1 -o liba.so liba.cc g++ -O2 -fPIC -shared $1 $2 -o libb.so libb.cc -L. -la gcc -O2 -o main main.c -L. -lb -Wl,-rpath-link,. LD_LIBRARY_PATH=. ./main; echo "exit=$?" } run "$RT $MAP" # 1. hidden like libvelox run "$RT $EXCL" # 2. hidden like libgluten run "$RT $MAP" -DWARM_UP # 3. 1 + libb warms its unwinder run "-static-libstdc++ -shared-libgcc $MAP" # 4. 1 with shared libgcc run "$RT" # 5. static runtime, not hidden ``` Results on x86_64, identical with GCC 12.2.1, 13.3.1, 14.2.1 and 15.2.1 (gcc-toolset-12 to 15 in `apache/gluten:centos-9-jdk17`): | # | Variant | Result | |---|---|---| | 1 | Static runtime hidden by a `local: *` version script, like libvelox | SIGABRT (exit 134) | | 2 | Static runtime hidden by `--exclude-libs`, like libgluten | SIGABRT (exit 134) | | 3 | 1, plus a libb constructor that calls `_Unwind_Backtrace` with a no-op callback | `b_catch() returned 42` | | 4 | 1, linked with `-shared-libgcc`: both libraries import `_Unwind_*` from `libgcc_s.so.1` | `b_catch() returned 42` | | 5 | Static runtime, not hidden: the copies interpose into one. Not a fix: #12557 removed exactly this interposition | `b_catch() returned 42` | gdb places the abort in libb's private `_Unwind_SetGR`, called by libb's personality routine while liba's unwinder runs phase 2 (backtrace under "Relevant logs"). #### Why CI is green In both CI-built libraries, the static initializer `_GLOBAL__sub_I_utilities.cc` passes `google::nop_backtrace` to that library's own `_Unwind_Backtrace` (found by disassembling the artifact above). This is glog's `StackTraceInit` ([stacktrace_unwind-inl.h#L49-L68](https://github.com/google/glog/blob/b33e3bad4c46c8a6345525fd822af355e5ef9446/src/stacktrace_unwind-inl.h#L49-L68); glog 0.6.0 is pinned in [vcpkg.json](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/dev/vcpkg/vcpkg.json#L142)), which glog's comment calls a workaround for "the malloc() in _Unwind_Backtrace() issue". It runs when each library is loaded, so both copies are initialized before any exception can cross. That protection lasts only while every library that hides a static libgcc also links glog statically with this stack tracer. It goes away, for example, if: - glog is built with libunwind. glog uses libunwind whenever it finds it ([CMakeLists.txt#L91-L101](https://github.com/google/glog/blob/b33e3bad4c46c8a6345525fd822af355e5ef9446/CMakeLists.txt#L91-L101), [utilities.h#L89-L92](https://github.com/google/glog/blob/b33e3bad4c46c8a6345525fd822af355e5ef9446/src/utilities.h#L89-L92)), and its libunwind tracer has no such initializer. libunwind is already in the vcpkg tree as a Linux dependency of folly ([ports/folly/vcpkg.json](https://github.com/apache/gluten/blob/73dae76e191b10dffa1cf56cdba66a0b5b95cbfa/dev/vcpkg/ports/folly/vcpkg.json#L30-L33)). - glog is linked dynamically or replaced. - another library with a hidden static runtime and no such initializer joins the process. The last case is how this was found, while validating #13563: a test library built like the vcpkg libraries (private libstdc++ and libgcc_eh, hidden symbols) aborted in its own `_Unwind_SetGR` the first time a `JavaException` thrown outside it unwound into it. One throw and catch inside the library beforehand made the same tests pass. #### Possible fixes For maintainers to choose: - **(a) Initialize each private unwinder deliberately.** Call `_Unwind_Backtrace` with a no-op callback from a static initializer or `JNI_OnLoad` in both libgluten and libvelox. This keeps #12557's isolation and makes today's accidental protection explicit (variant 3). - **(b) Link libgcc shared.** Use `-shared-libgcc` while keeping `-static-libstdc++`, so every copy of the personality routine uses the single process-wide unwinder in `libgcc_s.so.1` (variant 4). GCC's manual says that when an application throws and catches exceptions across shared libraries, each library and the application should use the shared libgcc ([Link Options, `-shared-libgcc`](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-shared-libgcc)). The trade-off is a runtime dependency on the system's `libgcc_s.so.1`. This issue was written with the assistance of AI (GitHub Copilot). ### Gluten version main branch ### Spark version N/A (independent of the Spark version) ### Spark configurations N/A ### System information - Reproducer: `apache/gluten:centos-9-jdk17` (CentOS Stream 9, x86_64) with gcc-toolset-12, 13, 14 and 15. - Binary inspection: `velox-native-lib-centos-8-73dae76e191b10dffa1cf56cdba66a0b5b95cbfa` from [run 45925918143](https://github.com/apache/gluten/actions/runs/45925918143) (x86_64, built in `apache/gluten:vcpkg-almalinux-8-gcc13`). ARM64 vcpkg builds use the same toolchain flags but weren't checked. ### Relevant logs ```bash # Reproducer variant 1 under gdb, GCC 13.3.1 (addresses and temporary paths removed) Program received signal SIGABRT, Aborted. #0 __pthread_kill_implementation () from /lib64/libc.so.6 #1 raise () from /lib64/libc.so.6 #2 abort () from /lib64/libc.so.6 #3 _Unwind_SetGR.cold () from libb.so #4 __gxx_personality_v0 () from libb.so #5 _Unwind_RaiseException_Phase2 () from liba.so #6 _Unwind_RaiseException () from liba.so #7 __cxa_throw () from liba.so #8 a_throw () from liba.so #9 b_catch () from libb.so #10 main () ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
