├── .github └── workflows │ └── ci.yml ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── cdylib ├── Cargo.toml └── src │ └── lib.rs ├── rust-toolchain ├── src ├── abi.rs ├── arch.rs ├── lib.rs ├── panic.rs ├── panic_handler.rs ├── panic_handler_dummy.rs ├── panicking.rs ├── personality.rs ├── personality_dummy.rs ├── print.rs ├── system_alloc.rs ├── unwinder │ ├── arch │ │ ├── aarch64.rs │ │ ├── mod.rs │ │ ├── riscv32.rs │ │ ├── riscv64.rs │ │ ├── x86.rs │ │ └── x86_64.rs │ ├── find_fde │ │ ├── custom.rs │ │ ├── fixed.rs │ │ ├── gnu_eh_frame_hdr.rs │ │ ├── mod.rs │ │ ├── phdr.rs │ │ └── registry.rs │ ├── frame.rs │ └── mod.rs └── util.rs ├── test_crates ├── catch_std_exception │ ├── Cargo.toml │ ├── check.sh │ └── src │ │ └── main.rs ├── panic_abort_no_debuginfo │ ├── Cargo.toml │ ├── check.sh │ └── src │ │ └── main.rs ├── std_catch_exception │ ├── Cargo.toml │ ├── check.sh │ └── src │ │ └── main.rs └── throw_and_catch │ ├── Cargo.toml │ ├── check.sh │ └── src │ └── main.rs └── tests └── compile_tests.rs /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: [push, pull_request] 4 | 5 | env: 6 | CARGO_TERM_COLOR: always 7 | 8 | jobs: 9 | test: 10 | strategy: 11 | matrix: 12 | target: 13 | - x86_64-unknown-linux-gnu 14 | - i686-unknown-linux-gnu 15 | - aarch64-unknown-linux-gnu 16 | - riscv64gc-unknown-linux-gnu 17 | - riscv32gc-unknown-linux-gnu 18 | runs-on: ubuntu-latest 19 | 20 | steps: 21 | - uses: actions/checkout@v4 22 | - name: Install Rust 23 | run: | 24 | rustup update nightly 25 | rustup default nightly 26 | - name: Install cross-compilation tools 27 | uses: taiki-e/setup-cross-toolchain-action@v1 28 | with: 29 | target: ${{ matrix.target }} 30 | 31 | - name: Build library 32 | run: cargo build --release $BUILD_STD 33 | env: 34 | RUSTFLAGS: -Zcrate-attr=feature(non_exhaustive_omitted_patterns_lint) 35 | 36 | - name: Run tests 37 | run: cargo test --release $BUILD_STD 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | target 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 4 4 | 5 | [[package]] 6 | name = "catch_std_exception" 7 | version = "0.1.0" 8 | dependencies = [ 9 | "libc", 10 | "unwinding", 11 | ] 12 | 13 | [[package]] 14 | name = "compiler_builtins" 15 | version = "0.1.139" 16 | source = "registry+https://github.com/rust-lang/crates.io-index" 17 | checksum = "7925a77545f37a18e152a4aeaeb9a220309022067cafcfa2ecebe9ec55d36ccb" 18 | 19 | [[package]] 20 | name = "gimli" 21 | version = "0.31.1" 22 | source = "registry+https://github.com/rust-lang/crates.io-index" 23 | checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" 24 | dependencies = [ 25 | "compiler_builtins", 26 | "rustc-std-workspace-alloc", 27 | "rustc-std-workspace-core", 28 | ] 29 | 30 | [[package]] 31 | name = "libc" 32 | version = "0.2.164" 33 | source = "registry+https://github.com/rust-lang/crates.io-index" 34 | checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" 35 | 36 | [[package]] 37 | name = "panic_abort_no_debuginfo" 38 | version = "0.1.0" 39 | dependencies = [ 40 | "unwinding", 41 | ] 42 | 43 | [[package]] 44 | name = "rustc-std-workspace-alloc" 45 | version = "1.0.1" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "f9d441c3b2ebf55cebf796bfdc265d67fa09db17b7bb6bd4be75c509e1e8fec3" 48 | 49 | [[package]] 50 | name = "rustc-std-workspace-core" 51 | version = "1.0.1" 52 | source = "registry+https://github.com/rust-lang/crates.io-index" 53 | checksum = "aa9c45b374136f52f2d6311062c7146bff20fec063c3f5d46a410bd937746955" 54 | 55 | [[package]] 56 | name = "spin" 57 | version = "0.9.8" 58 | source = "registry+https://github.com/rust-lang/crates.io-index" 59 | checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" 60 | 61 | [[package]] 62 | name = "std_catch_exception" 63 | version = "0.1.0" 64 | dependencies = [ 65 | "libc", 66 | "unwinding", 67 | ] 68 | 69 | [[package]] 70 | name = "throw_and_catch" 71 | version = "0.1.0" 72 | dependencies = [ 73 | "libc", 74 | "unwinding", 75 | ] 76 | 77 | [[package]] 78 | name = "unwinding" 79 | version = "0.2.6" 80 | dependencies = [ 81 | "compiler_builtins", 82 | "gimli", 83 | "libc", 84 | "rustc-std-workspace-core", 85 | "spin", 86 | ] 87 | 88 | [[package]] 89 | name = "unwinding_dyn" 90 | version = "0.1.0" 91 | dependencies = [ 92 | "libc", 93 | "unwinding", 94 | ] 95 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "unwinding" 3 | version = "0.2.6" 4 | authors = ["Gary Guo "] 5 | edition = "2024" 6 | license = "MIT OR Apache-2.0" 7 | description = "Unwinding library in Rust and for Rust" 8 | repository = "https://github.com/nbdd0121/unwinding/" 9 | rust-version = "1.88" 10 | 11 | [workspace] 12 | members = [ 13 | "cdylib", 14 | "test_crates/throw_and_catch", 15 | "test_crates/catch_std_exception", 16 | "test_crates/std_catch_exception", 17 | "test_crates/panic_abort_no_debuginfo", 18 | ] 19 | 20 | [dependencies] 21 | gimli = { version = "0.31", default-features = false, features = ["read-core"] } 22 | libc = { version = "0.2", optional = true } 23 | spin = { version = "0.9.8", optional = true, default-features = false, features = ["mutex", "spin_mutex"] } 24 | core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core' } 25 | compiler_builtins = { version = "0.1.2", optional = true } 26 | 27 | [features] 28 | alloc = [] 29 | unwinder = [] 30 | fde-phdr = ["libc"] 31 | fde-phdr-dl = ["fde-phdr"] 32 | fde-phdr-aux = ["fde-phdr"] 33 | fde-registry = ["alloc"] 34 | fde-static = [] 35 | fde-gnu-eh-frame-hdr = [] 36 | fde-custom = [] 37 | dwarf-expr = [] 38 | hide-trace = [] 39 | personality = [] 40 | personality-dummy = [] 41 | print = ["libc"] 42 | panicking = [] 43 | panic = ["panicking", "alloc"] 44 | panic-handler = ["print", "panic"] 45 | panic-handler-dummy = [] 46 | system-alloc = [] 47 | default = ["unwinder", "dwarf-expr", "hide-trace", "fde-phdr-dl", "fde-registry"] 48 | rustc-dep-of-std = ["core", "gimli/rustc-dep-of-std", "compiler_builtins"] 49 | compiler_builtins = ["dep:compiler_builtins"] 50 | core = ["dep:core"] 51 | libc = ["dep:libc"] 52 | spin = ["dep:spin"] 53 | 54 | [profile.release] 55 | debug = true 56 | 57 | [package.metadata.docs.rs] 58 | features = ["panic-handler"] 59 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any 2 | person obtaining a copy of this software and associated 3 | documentation files (the "Software"), to deal in the 4 | Software without restriction, including without 5 | limitation the rights to use, copy, modify, merge, 6 | publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software 8 | is furnished to do so, subject to the following 9 | conditions: 10 | 11 | The above copyright notice and this permission notice 12 | shall be included in all copies or substantial portions 13 | of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 16 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 17 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 18 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 19 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 22 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unwinding library in Rust and for Rust 2 | ====================================== 3 | 4 | [![crates.io](https://img.shields.io/crates/v/unwinding.svg)](https://crates.io/crates/unwinding) 5 | [![docs.rs](https://docs.rs/unwinding/badge.svg)](https://docs.rs/unwinding) 6 | [![license](https://img.shields.io/crates/l/unwinding.svg)](https://crates.io/crates/unwinding) 7 | 8 | This library serves two purposes: 9 | 1. Provide a pure Rust alternative to libgcc_eh or libunwind. 10 | 2. Provide easier unwinding support for `#![no_std]` targets. 11 | 12 | Currently supports x86_64, x86, RV64, RV32 and AArch64. 13 | 14 | ## Unwinder 15 | 16 | The unwinder can be enabled with `unwinder` feature. Here are the feature gates related to the unwinder: 17 | 18 | | Feature | Default | Description | 19 | |--------------------- |---------|-| 20 | | unwinder | Yes | The primary feature gate to enable the unwinder | 21 | | fde-phdr-dl | Yes | Use `dl_iterator_phdr` to retrieve frame unwind table. Depends on libc. | 22 | | fde-phdr-aux | No | Use ELF auxiliary vector to retrieve frame unwind table. Depends on libc. | 23 | | fde-registry | Yes | Provide `__register__frame` and others for dynamic registration. Requires either `libc` or `spin` for a mutex implementation. | 24 | | fde-gnu-eh-frame-hdr | No | Use `__executable_start`, `__etext` and `__GNU_EH_FRAME_HDR` to retrieve frame unwind table. The former two symbols are usually provided by the linker, while the last one is provided if GNU LD is used and --eh-frame-hdr option is enabled. | 25 | | fde-static | No | Use `__executable_start`, `__etext` and `__eh_frame` to retrieve frame unwind table. The former two symbols are usually provided by the linker, while the last one would need to be provided by the user via linker script. | 26 | | fde-custom | No | Allow the program to provide a custom means of retrieving frame unwind table at runtime via the `set_custom_eh_frame_finder` function. | 27 | | dwarf-expr | Yes | Enable the dwarf expression evaluator. Usually not necessary for Rust | 28 | | hide-trace | Yes | Hide unwinder frames in back trace | 29 | 30 | If you want to use the unwinder for other Rust (C++, or any programs that utilize the unwinder), you can build the [`unwinding_dyn`](cdylib) crate provided, and use `LD_PRELOAD` to replace the system unwinder with it. 31 | ```sh 32 | cd cdylib 33 | cargo build --release 34 | # Test the unwinder using rustc. Why not :) 35 | LD_PRELOAD=`../target/release/libunwinding_dyn.so` rustc +nightly -Ztreat-err-as-bug 36 | ``` 37 | 38 | If you want to link to the unwinder in a Rust binary, simply add 39 | ```rust 40 | extern crate unwinding; 41 | ``` 42 | 43 | ## Personality and other utilities 44 | 45 | The library also provides Rust personality function. This can work with the unwinder described above or with a different unwinder. This can be handy if you are working on a `#![no_std]` binary/staticlib/cdylib and you still want unwinding support. 46 | 47 | Here are the feature gates related: 48 | 49 | | Feature | Default | Description | 50 | |---------------|---------|-| 51 | | personality | No | Provides `#[lang = eh_personality]` | 52 | | print | No | Provides `(e)?print(ln)?`. This is really only here because panic handler needs to print things. Depends on libc. | 53 | | panicking | No | Provides a generic `begin_panic` and `catch_unwind`. Only stack unwinding functionality is provided, memory allocation and panic handling is left to the user. | 54 | | panic | No | Provides Rust `begin_panic` and `catch_unwind`. Only stack unwinding functionality is provided and no printing is done, because this feature does not depend on libc. | 55 | | panic-handler | No | Provides `#[panic_handler]`. Provides similar behaviour on panic to std, with `RUST_BACKTRACE` support as well. Stack trace won't have symbols though. Depends on libc. | 56 | | system-alloc | No | Provides a global allocator which calls `malloc` and friends. Provided for convience. | 57 | 58 | If you are writing a `#![no_std]` program, simply enable `personality`, `panic-handler` and `system-alloc` in addition to the defaults, you instantly obtains the ability to do unwinding! An example is given in the [`example/` folder](example). 59 | 60 | ## Baremetal 61 | 62 | To use this library for baremetal projects, disable default features and enable `unwinder`, `fde-static`, `personality`, `panic`. `dwarf-expr` and `hide-trace` are optional. Modify the linker script by 63 | ```ld 64 | /* Inserting these two lines */ 65 | . = ALIGN(8); 66 | PROVIDE(__eh_frame = .); 67 | /* before .eh_frame rule */ 68 | .eh_frame : { KEEP (*(.eh_frame)) *(.eh_frame.*) } 69 | ``` 70 | 71 | And that's it! After you ensured that the global allocator is functional, you can use `unwinding::panic::begin_panic` to initiate an unwing and catch using `unwinding::panic::catch_unwind`, as if you have a `std`. 72 | 73 | If your linker supports `--eh-frame-hdr` you can also try to use `fde-gnu-eh-frame-hdr` instead of `fde-static`. GNU LD will provides a `__GNU_EH_FRAME_HDR` magic symbol so you don't have to provide `__eh_frame` through linker script. 74 | 75 | If you have your own version of `thread_local` and `println!` working, you can port [`panic_handler.rs`](src/panic_handler.rs) for double-panic protection and stack traces! 76 | -------------------------------------------------------------------------------- /cdylib/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "unwinding_dyn" 3 | version = "0.1.0" 4 | authors = ["Gary Guo "] 5 | edition = "2024" 6 | 7 | [lib] 8 | crate-type = ["cdylib", "staticlib"] 9 | 10 | [dependencies] 11 | unwinding = { path = "../", features = ["system-alloc", "personality-dummy", "panic-handler-dummy"] } 12 | libc = "0.2" 13 | -------------------------------------------------------------------------------- /cdylib/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | 3 | // Keep this explicit 4 | #[allow(unused_extern_crates)] 5 | extern crate unwinding; 6 | -------------------------------------------------------------------------------- /rust-toolchain: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /src/abi.rs: -------------------------------------------------------------------------------- 1 | use core::ffi::c_void; 2 | use core::ops; 3 | 4 | use crate::util::*; 5 | 6 | #[cfg(not(feature = "unwinder"))] 7 | use crate::arch::Arch; 8 | #[cfg(feature = "unwinder")] 9 | pub use crate::unwinder::*; 10 | 11 | #[repr(transparent)] 12 | #[derive(Clone, Copy, PartialEq, Eq)] 13 | pub struct UnwindReasonCode(pub c_int); 14 | 15 | #[allow(unused)] 16 | impl UnwindReasonCode { 17 | pub const NO_REASON: Self = Self(0); 18 | pub const FOREIGN_EXCEPTION_CAUGHT: Self = Self(1); 19 | pub const FATAL_PHASE2_ERROR: Self = Self(2); 20 | pub const FATAL_PHASE1_ERROR: Self = Self(3); 21 | pub const NORMAL_STOP: Self = Self(4); 22 | pub const END_OF_STACK: Self = Self(5); 23 | pub const HANDLER_FOUND: Self = Self(6); 24 | pub const INSTALL_CONTEXT: Self = Self(7); 25 | pub const CONTINUE_UNWIND: Self = Self(8); 26 | } 27 | 28 | #[repr(transparent)] 29 | #[derive(Clone, Copy, PartialEq, Eq)] 30 | pub struct UnwindAction(pub c_int); 31 | 32 | impl UnwindAction { 33 | pub const SEARCH_PHASE: Self = Self(1); 34 | pub const CLEANUP_PHASE: Self = Self(2); 35 | pub const HANDLER_FRAME: Self = Self(4); 36 | pub const FORCE_UNWIND: Self = Self(8); 37 | pub const END_OF_STACK: Self = Self(16); 38 | } 39 | 40 | impl ops::BitOr for UnwindAction { 41 | type Output = Self; 42 | 43 | #[inline] 44 | fn bitor(self, rhs: Self) -> Self { 45 | Self(self.0 | rhs.0) 46 | } 47 | } 48 | 49 | impl UnwindAction { 50 | #[inline] 51 | pub const fn empty() -> Self { 52 | Self(0) 53 | } 54 | 55 | #[inline] 56 | pub const fn contains(&self, other: Self) -> bool { 57 | self.0 & other.0 != 0 58 | } 59 | } 60 | 61 | pub type UnwindExceptionCleanupFn = unsafe extern "C" fn(UnwindReasonCode, *mut UnwindException); 62 | 63 | pub type UnwindStopFn = unsafe extern "C" fn( 64 | c_int, 65 | UnwindAction, 66 | u64, 67 | *mut UnwindException, 68 | &mut UnwindContext<'_>, 69 | *mut c_void, 70 | ) -> UnwindReasonCode; 71 | 72 | #[cfg(not(feature = "unwinder"))] 73 | #[repr(C)] 74 | pub struct UnwindException { 75 | pub exception_class: u64, 76 | pub exception_cleanup: Option, 77 | private: [usize; Arch::UNWIND_PRIVATE_DATA_SIZE], 78 | } 79 | 80 | pub type UnwindTraceFn = 81 | extern "C" fn(ctx: &UnwindContext<'_>, arg: *mut c_void) -> UnwindReasonCode; 82 | 83 | #[cfg(not(feature = "unwinder"))] 84 | #[repr(C)] 85 | pub struct UnwindContext<'a> { 86 | opaque: usize, 87 | phantom: core::marker::PhantomData<&'a ()>, 88 | } 89 | 90 | pub type PersonalityRoutine = unsafe extern "C" fn( 91 | c_int, 92 | UnwindAction, 93 | u64, 94 | *mut UnwindException, 95 | &mut UnwindContext<'_>, 96 | ) -> UnwindReasonCode; 97 | 98 | #[cfg(not(feature = "unwinder"))] 99 | macro_rules! binding { 100 | () => {}; 101 | (unsafe extern $abi: literal fn $name: ident ($($arg: ident : $arg_ty: ty),*$(,)?) $(-> $ret: ty)?; $($rest: tt)*) => { 102 | unsafe extern $abi { 103 | pub unsafe fn $name($($arg: $arg_ty),*) $(-> $ret)?; 104 | } 105 | binding!($($rest)*); 106 | }; 107 | 108 | (extern $abi: literal fn $name: ident ($($arg: ident : $arg_ty: ty),*$(,)?) $(-> $ret: ty)?; $($rest: tt)*) => { 109 | unsafe extern $abi { 110 | pub safe fn $name($($arg: $arg_ty),*) $(-> $ret)?; 111 | } 112 | binding!($($rest)*); 113 | }; 114 | } 115 | 116 | #[cfg(feature = "unwinder")] 117 | macro_rules! binding { 118 | () => {}; 119 | (unsafe extern $abi: literal fn $name: ident ($($arg: ident : $arg_ty: ty),*$(,)?) $(-> $ret: ty)?; $($rest: tt)*) => { 120 | const _: unsafe extern $abi fn($($arg_ty),*) $(-> $ret)? = $name; 121 | }; 122 | 123 | (extern $abi: literal fn $name: ident ($($arg: ident : $arg_ty: ty),*$(,)?) $(-> $ret: ty)?; $($rest: tt)*) => { 124 | const _: extern $abi fn($($arg_ty),*) $(-> $ret)? = $name; 125 | }; 126 | } 127 | 128 | binding! { 129 | extern "C" fn _Unwind_GetGR(unwind_ctx: &UnwindContext<'_>, index: c_int) -> usize; 130 | extern "C" fn _Unwind_GetCFA(unwind_ctx: &UnwindContext<'_>) -> usize; 131 | extern "C" fn _Unwind_SetGR( 132 | unwind_ctx: &mut UnwindContext<'_>, 133 | index: c_int, 134 | value: usize, 135 | ); 136 | extern "C" fn _Unwind_GetIP(unwind_ctx: &UnwindContext<'_>) -> usize; 137 | extern "C" fn _Unwind_GetIPInfo( 138 | unwind_ctx: &UnwindContext<'_>, 139 | ip_before_insn: &mut c_int, 140 | ) -> usize; 141 | extern "C" fn _Unwind_SetIP( 142 | unwind_ctx: &mut UnwindContext<'_>, 143 | value: usize, 144 | ); 145 | extern "C" fn _Unwind_GetLanguageSpecificData(unwind_ctx: &UnwindContext<'_>) -> *mut c_void; 146 | extern "C" fn _Unwind_GetRegionStart(unwind_ctx: &UnwindContext<'_>) -> usize; 147 | extern "C" fn _Unwind_GetTextRelBase(unwind_ctx: &UnwindContext<'_>) -> usize; 148 | extern "C" fn _Unwind_GetDataRelBase(unwind_ctx: &UnwindContext<'_>) -> usize; 149 | extern "C" fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void; 150 | unsafe extern "C-unwind" fn _Unwind_RaiseException( 151 | exception: *mut UnwindException, 152 | ) -> UnwindReasonCode; 153 | unsafe extern "C-unwind" fn _Unwind_ForcedUnwind( 154 | exception: *mut UnwindException, 155 | stop: UnwindStopFn, 156 | stop_arg: *mut c_void, 157 | ) -> UnwindReasonCode; 158 | unsafe extern "C-unwind" fn _Unwind_Resume(exception: *mut UnwindException) -> !; 159 | unsafe extern "C-unwind" fn _Unwind_Resume_or_Rethrow( 160 | exception: *mut UnwindException, 161 | ) -> UnwindReasonCode; 162 | unsafe extern "C" fn _Unwind_DeleteException(exception: *mut UnwindException); 163 | extern "C-unwind" fn _Unwind_Backtrace( 164 | trace: UnwindTraceFn, 165 | trace_argument: *mut c_void, 166 | ) -> UnwindReasonCode; 167 | } 168 | -------------------------------------------------------------------------------- /src/arch.rs: -------------------------------------------------------------------------------- 1 | #[cfg(target_arch = "x86_64")] 2 | mod x86_64 { 3 | use gimli::{Register, X86_64}; 4 | 5 | pub struct Arch; 6 | 7 | #[allow(unused)] 8 | impl Arch { 9 | pub const SP: Register = X86_64::RSP; 10 | pub const RA: Register = X86_64::RA; 11 | 12 | pub const UNWIND_DATA_REG: (Register, Register) = (X86_64::RAX, X86_64::RDX); 13 | pub const UNWIND_PRIVATE_DATA_SIZE: usize = 6; 14 | } 15 | } 16 | #[cfg(target_arch = "x86_64")] 17 | pub use x86_64::*; 18 | 19 | #[cfg(target_arch = "x86")] 20 | mod x86 { 21 | use gimli::{Register, X86}; 22 | 23 | pub struct Arch; 24 | 25 | #[allow(unused)] 26 | impl Arch { 27 | pub const SP: Register = X86::ESP; 28 | pub const RA: Register = X86::RA; 29 | 30 | pub const UNWIND_DATA_REG: (Register, Register) = (X86::EAX, X86::EDX); 31 | pub const UNWIND_PRIVATE_DATA_SIZE: usize = 5; 32 | } 33 | } 34 | #[cfg(target_arch = "x86")] 35 | pub use x86::*; 36 | 37 | #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))] 38 | mod riscv { 39 | use gimli::{Register, RiscV}; 40 | 41 | pub struct Arch; 42 | 43 | #[allow(unused)] 44 | impl Arch { 45 | pub const SP: Register = RiscV::SP; 46 | pub const RA: Register = RiscV::RA; 47 | 48 | pub const UNWIND_DATA_REG: (Register, Register) = (RiscV::A0, RiscV::A1); 49 | pub const UNWIND_PRIVATE_DATA_SIZE: usize = 2; 50 | } 51 | } 52 | #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))] 53 | pub use riscv::*; 54 | 55 | #[cfg(target_arch = "aarch64")] 56 | mod aarch64 { 57 | use gimli::{AArch64, Register}; 58 | 59 | pub struct Arch; 60 | 61 | #[allow(unused)] 62 | impl Arch { 63 | pub const SP: Register = AArch64::SP; 64 | pub const RA: Register = AArch64::X30; 65 | 66 | pub const UNWIND_DATA_REG: (Register, Register) = (AArch64::X0, AArch64::X1); 67 | pub const UNWIND_PRIVATE_DATA_SIZE: usize = 2; 68 | } 69 | } 70 | #[cfg(target_arch = "aarch64")] 71 | pub use aarch64::*; 72 | 73 | #[cfg(not(any( 74 | target_arch = "x86_64", 75 | target_arch = "x86", 76 | target_arch = "riscv64", 77 | target_arch = "riscv32", 78 | target_arch = "aarch64" 79 | )))] 80 | compile_error!("Current architecture is not supported"); 81 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | // lang_items is an internal feature. `internal_features` lint is added recently 3 | // so also allow unknown lints to prevent warning in older nightly versions. 4 | #![allow(unknown_lints)] 5 | #![cfg_attr( 6 | any( 7 | feature = "personality", 8 | feature = "personality-dummy", 9 | feature = "panicking", 10 | feature = "panic-handler-dummy" 11 | ), 12 | allow(internal_features) 13 | )] 14 | #![cfg_attr( 15 | any(feature = "personality", feature = "personality-dummy"), 16 | feature(lang_items) 17 | )] 18 | #![cfg_attr( 19 | any(feature = "panicking", feature = "panic-handler-dummy"), 20 | feature(core_intrinsics) 21 | )] 22 | #![cfg_attr(feature = "panic-handler", feature(thread_local))] 23 | #![no_std] 24 | 25 | #[cfg(feature = "alloc")] 26 | extern crate alloc; 27 | 28 | #[cfg(feature = "unwinder")] 29 | mod unwinder; 30 | 31 | #[cfg(all(feature = "unwinder", feature = "fde-custom"))] 32 | pub use unwinder::custom_eh_frame_finder; 33 | 34 | pub mod abi; 35 | 36 | mod arch; 37 | mod util; 38 | 39 | #[cfg(feature = "print")] 40 | pub mod print; 41 | 42 | #[cfg(feature = "personality")] 43 | mod personality; 44 | #[cfg(all(not(feature = "personality"), feature = "personality-dummy"))] 45 | mod personality_dummy; 46 | 47 | #[cfg(feature = "panic")] 48 | pub mod panic; 49 | #[cfg(feature = "panicking")] 50 | pub mod panicking; 51 | 52 | #[cfg(feature = "panic-handler")] 53 | mod panic_handler; 54 | #[cfg(all(not(feature = "panic-handler"), feature = "panic-handler-dummy"))] 55 | mod panic_handler_dummy; 56 | 57 | #[cfg(feature = "system-alloc")] 58 | mod system_alloc; 59 | -------------------------------------------------------------------------------- /src/panic.rs: -------------------------------------------------------------------------------- 1 | use alloc::boxed::Box; 2 | use core::any::Any; 3 | use core::mem::MaybeUninit; 4 | 5 | use crate::abi::*; 6 | #[cfg(feature = "panic-handler")] 7 | pub use crate::panic_handler::*; 8 | use crate::panicking::Exception; 9 | 10 | static CANARY: u8 = 0; 11 | 12 | #[repr(transparent)] 13 | struct RustPanic(Box, DropGuard); 14 | 15 | struct DropGuard; 16 | 17 | impl Drop for DropGuard { 18 | fn drop(&mut self) { 19 | #[cfg(feature = "panic-handler")] 20 | { 21 | drop_panic(); 22 | } 23 | crate::util::abort(); 24 | } 25 | } 26 | 27 | #[repr(C)] 28 | struct ExceptionWithPayload { 29 | exception: MaybeUninit, 30 | // See rust/library/panic_unwind/src/gcc.rs for the canary values 31 | canary: *const u8, 32 | payload: RustPanic, 33 | } 34 | 35 | unsafe impl Exception for RustPanic { 36 | const CLASS: [u8; 8] = *b"MOZ\0RUST"; 37 | 38 | fn wrap(this: Self) -> *mut UnwindException { 39 | Box::into_raw(Box::new(ExceptionWithPayload { 40 | exception: MaybeUninit::uninit(), 41 | canary: &CANARY, 42 | payload: this, 43 | })) as *mut UnwindException 44 | } 45 | 46 | unsafe fn unwrap(ex: *mut UnwindException) -> Self { 47 | let ex = ex as *mut ExceptionWithPayload; 48 | let canary = unsafe { core::ptr::addr_of!((*ex).canary).read() }; 49 | if !core::ptr::eq(canary, &CANARY) { 50 | // This is a Rust exception but not generated by us. 51 | #[cfg(feature = "panic-handler")] 52 | { 53 | foreign_exception(); 54 | } 55 | crate::util::abort(); 56 | } 57 | let ex = unsafe { Box::from_raw(ex) }; 58 | ex.payload 59 | } 60 | } 61 | 62 | pub fn begin_panic(payload: Box) -> UnwindReasonCode { 63 | crate::panicking::begin_panic(RustPanic(payload, DropGuard)) 64 | } 65 | 66 | pub fn catch_unwind R>(f: F) -> Result> { 67 | #[cold] 68 | fn process_panic(p: Option) -> Box { 69 | match p { 70 | None => { 71 | #[cfg(feature = "panic-handler")] 72 | { 73 | foreign_exception(); 74 | } 75 | crate::util::abort(); 76 | } 77 | Some(e) => { 78 | #[cfg(feature = "panic-handler")] 79 | { 80 | panic_caught(); 81 | } 82 | core::mem::forget(e.1); 83 | e.0 84 | } 85 | } 86 | } 87 | crate::panicking::catch_unwind(f).map_err(process_panic) 88 | } 89 | -------------------------------------------------------------------------------- /src/panic_handler.rs: -------------------------------------------------------------------------------- 1 | use crate::abi::*; 2 | use crate::print::*; 3 | use alloc::boxed::Box; 4 | use core::any::Any; 5 | use core::cell::Cell; 6 | use core::ffi::c_void; 7 | use core::panic::{Location, PanicInfo}; 8 | use core::sync::atomic::{AtomicI32, Ordering}; 9 | 10 | #[thread_local] 11 | static PANIC_COUNT: Cell = Cell::new(0); 12 | 13 | #[link(name = "c")] 14 | unsafe extern "C" {} 15 | 16 | pub(crate) fn drop_panic() { 17 | eprintln!("Rust panics must be rethrown"); 18 | } 19 | 20 | pub(crate) fn foreign_exception() { 21 | eprintln!("Rust cannot catch foreign exceptions"); 22 | } 23 | 24 | pub(crate) fn panic_caught() { 25 | PANIC_COUNT.set(0); 26 | } 27 | 28 | fn check_env() -> bool { 29 | static ENV: AtomicI32 = AtomicI32::new(-1); 30 | 31 | let env = ENV.load(Ordering::Relaxed); 32 | if env != -1 { 33 | return env != 0; 34 | } 35 | 36 | let val = unsafe { 37 | let ptr = libc::getenv(b"RUST_BACKTRACE\0".as_ptr() as _); 38 | if ptr.is_null() { 39 | b"" 40 | } else { 41 | let len = libc::strlen(ptr); 42 | core::slice::from_raw_parts(ptr as *const u8, len) 43 | } 44 | }; 45 | let (note, env) = match val { 46 | b"" => (true, false), 47 | b"1" | b"full" => (false, true), 48 | _ => (false, false), 49 | }; 50 | 51 | // Issue a note for the first panic. 52 | if ENV.swap(env as _, Ordering::Relaxed) == -1 && note { 53 | eprintln!("note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"); 54 | } 55 | env 56 | } 57 | 58 | fn stack_trace() { 59 | struct CallbackData { 60 | counter: usize, 61 | } 62 | extern "C" fn callback(unwind_ctx: &UnwindContext<'_>, arg: *mut c_void) -> UnwindReasonCode { 63 | let data = unsafe { &mut *(arg as *mut CallbackData) }; 64 | data.counter += 1; 65 | eprintln!( 66 | "{:4}:{:#19x} - ", 67 | data.counter, 68 | _Unwind_GetIP(unwind_ctx) 69 | ); 70 | UnwindReasonCode::NO_REASON 71 | } 72 | let mut data = CallbackData { counter: 0 }; 73 | _Unwind_Backtrace(callback, &mut data as *mut _ as _); 74 | } 75 | 76 | fn do_panic(msg: Box) -> ! { 77 | if PANIC_COUNT.get() >= 1 { 78 | stack_trace(); 79 | eprintln!("thread panicked while processing panic. aborting."); 80 | crate::util::abort(); 81 | } 82 | PANIC_COUNT.set(1); 83 | if check_env() { 84 | stack_trace(); 85 | } 86 | let code = crate::panic::begin_panic(Box::new(msg)); 87 | eprintln!("failed to initiate panic, error {}", code.0); 88 | crate::util::abort(); 89 | } 90 | 91 | #[panic_handler] 92 | fn panic(info: &PanicInfo<'_>) -> ! { 93 | eprintln!("{}", info); 94 | 95 | struct NoPayload; 96 | do_panic(Box::new(NoPayload)) 97 | } 98 | 99 | #[track_caller] 100 | pub fn panic_any(msg: M) -> ! { 101 | eprintln!("panicked at {}", Location::caller()); 102 | do_panic(Box::new(msg)) 103 | } 104 | -------------------------------------------------------------------------------- /src/panic_handler_dummy.rs: -------------------------------------------------------------------------------- 1 | use core::panic::PanicInfo; 2 | 3 | #[panic_handler] 4 | fn panic(_info: &PanicInfo<'_>) -> ! { 5 | crate::util::abort(); 6 | } 7 | -------------------------------------------------------------------------------- /src/panicking.rs: -------------------------------------------------------------------------------- 1 | use core::mem::ManuallyDrop; 2 | 3 | use crate::abi::*; 4 | 5 | pub unsafe trait Exception { 6 | const CLASS: [u8; 8]; 7 | 8 | fn wrap(this: Self) -> *mut UnwindException; 9 | unsafe fn unwrap(ex: *mut UnwindException) -> Self; 10 | } 11 | 12 | pub fn begin_panic(exception: E) -> UnwindReasonCode { 13 | unsafe extern "C" fn exception_cleanup( 14 | _unwind_code: UnwindReasonCode, 15 | exception: *mut UnwindException, 16 | ) { 17 | unsafe { E::unwrap(exception) }; 18 | } 19 | 20 | let ex = E::wrap(exception); 21 | unsafe { 22 | (*ex).exception_class = u64::from_ne_bytes(E::CLASS); 23 | (*ex).exception_cleanup = Some(exception_cleanup::); 24 | _Unwind_RaiseException(ex) 25 | } 26 | } 27 | 28 | pub fn catch_unwind R>(f: F) -> Result> { 29 | #[repr(C)] 30 | union Data { 31 | f: ManuallyDrop, 32 | r: ManuallyDrop, 33 | p: ManuallyDrop>, 34 | } 35 | 36 | let mut data = Data { 37 | f: ManuallyDrop::new(f), 38 | }; 39 | 40 | let data_ptr = &mut data as *mut _ as *mut u8; 41 | unsafe { 42 | return if core::intrinsics::catch_unwind(do_call::, data_ptr, do_catch::) == 0 { 43 | Ok(ManuallyDrop::into_inner(data.r)) 44 | } else { 45 | Err(ManuallyDrop::into_inner(data.p)) 46 | }; 47 | } 48 | 49 | #[inline] 50 | fn do_call R, R>(data: *mut u8) { 51 | unsafe { 52 | let data = &mut *(data as *mut Data); 53 | let f = ManuallyDrop::take(&mut data.f); 54 | data.r = ManuallyDrop::new(f()); 55 | } 56 | } 57 | 58 | #[cold] 59 | fn do_catch(data: *mut u8, exception: *mut u8) { 60 | unsafe { 61 | let data = &mut *(data as *mut ManuallyDrop>); 62 | let exception = exception as *mut UnwindException; 63 | if (*exception).exception_class != u64::from_ne_bytes(E::CLASS) { 64 | _Unwind_DeleteException(exception); 65 | *data = ManuallyDrop::new(None); 66 | return; 67 | } 68 | *data = ManuallyDrop::new(Some(E::unwrap(exception))); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/personality.rs: -------------------------------------------------------------------------------- 1 | // References: 2 | // https://github.com/rust-lang/rust/blob/c4be230b4a30eb74e3a3908455731ebc2f731d3d/library/panic_unwind/src/gcc.rs 3 | // https://github.com/rust-lang/rust/blob/c4be230b4a30eb74e3a3908455731ebc2f731d3d/library/panic_unwind/src/dwarf/eh.rs 4 | // https://docs.rs/gimli/0.25.0/src/gimli/read/cfi.rs.html 5 | 6 | use core::mem; 7 | use gimli::{constants, NativeEndian}; 8 | use gimli::{EndianSlice, Error, Pointer, Reader}; 9 | 10 | use crate::abi::*; 11 | use crate::arch::*; 12 | use crate::util::*; 13 | 14 | #[derive(Debug)] 15 | enum EHAction { 16 | None, 17 | Cleanup(usize), 18 | Catch(usize), 19 | Filter(usize), 20 | Terminate, 21 | } 22 | 23 | fn parse_pointer_encoding(input: &mut StaticSlice) -> gimli::Result { 24 | let eh_pe = input.read_u8()?; 25 | let eh_pe = constants::DwEhPe(eh_pe); 26 | 27 | if eh_pe.is_valid_encoding() { 28 | Ok(eh_pe) 29 | } else { 30 | Err(gimli::Error::UnknownPointerEncoding(eh_pe)) 31 | } 32 | } 33 | 34 | fn parse_encoded_pointer( 35 | encoding: constants::DwEhPe, 36 | unwind_ctx: &UnwindContext<'_>, 37 | input: &mut StaticSlice, 38 | ) -> gimli::Result { 39 | if encoding == constants::DW_EH_PE_omit { 40 | return Err(Error::CannotParseOmitPointerEncoding); 41 | } 42 | 43 | let base = match encoding.application() { 44 | constants::DW_EH_PE_absptr => 0, 45 | constants::DW_EH_PE_pcrel => input.slice().as_ptr() as u64, 46 | constants::DW_EH_PE_textrel => _Unwind_GetTextRelBase(unwind_ctx) as u64, 47 | constants::DW_EH_PE_datarel => _Unwind_GetDataRelBase(unwind_ctx) as u64, 48 | constants::DW_EH_PE_funcrel => _Unwind_GetRegionStart(unwind_ctx) as u64, 49 | constants::DW_EH_PE_aligned => return Err(Error::UnsupportedPointerEncoding), 50 | _ => unreachable!(), 51 | }; 52 | 53 | let offset = match encoding.format() { 54 | constants::DW_EH_PE_absptr => input.read_address(mem::size_of::() as _), 55 | constants::DW_EH_PE_uleb128 => input.read_uleb128(), 56 | constants::DW_EH_PE_udata2 => input.read_u16().map(u64::from), 57 | constants::DW_EH_PE_udata4 => input.read_u32().map(u64::from), 58 | constants::DW_EH_PE_udata8 => input.read_u64(), 59 | constants::DW_EH_PE_sleb128 => input.read_sleb128().map(|a| a as u64), 60 | constants::DW_EH_PE_sdata2 => input.read_i16().map(|a| a as u64), 61 | constants::DW_EH_PE_sdata4 => input.read_i32().map(|a| a as u64), 62 | constants::DW_EH_PE_sdata8 => input.read_i64().map(|a| a as u64), 63 | _ => unreachable!(), 64 | }?; 65 | 66 | let address = base.wrapping_add(offset); 67 | Ok(if encoding.is_indirect() { 68 | Pointer::Indirect(address) 69 | } else { 70 | Pointer::Direct(address) 71 | }) 72 | } 73 | 74 | fn find_eh_action( 75 | reader: &mut StaticSlice, 76 | unwind_ctx: &UnwindContext<'_>, 77 | ) -> gimli::Result { 78 | let func_start = _Unwind_GetRegionStart(unwind_ctx); 79 | let mut ip_before_instr = 0; 80 | let ip = _Unwind_GetIPInfo(unwind_ctx, &mut ip_before_instr); 81 | let ip = if ip_before_instr != 0 { ip } else { ip - 1 }; 82 | 83 | let start_encoding = parse_pointer_encoding(reader)?; 84 | let lpad_base = if !start_encoding.is_absent() { 85 | unsafe { deref_pointer(parse_encoded_pointer(start_encoding, unwind_ctx, reader)?) } 86 | } else { 87 | func_start 88 | }; 89 | 90 | let ttype_encoding = parse_pointer_encoding(reader)?; 91 | if !ttype_encoding.is_absent() { 92 | reader.read_uleb128()?; 93 | } 94 | 95 | let call_site_encoding = parse_pointer_encoding(reader)?; 96 | let call_site_table_length = reader.read_uleb128()?; 97 | let (mut call_site_table, mut action_table) = reader.split_at(call_site_table_length as _); 98 | 99 | while !call_site_table.is_empty() { 100 | let cs_start = unsafe { 101 | deref_pointer(parse_encoded_pointer( 102 | call_site_encoding, 103 | unwind_ctx, 104 | &mut call_site_table, 105 | )?) 106 | }; 107 | let cs_len = unsafe { 108 | deref_pointer(parse_encoded_pointer( 109 | call_site_encoding, 110 | unwind_ctx, 111 | &mut call_site_table, 112 | )?) 113 | }; 114 | let cs_lpad = unsafe { 115 | deref_pointer(parse_encoded_pointer( 116 | call_site_encoding, 117 | unwind_ctx, 118 | &mut call_site_table, 119 | )?) 120 | }; 121 | let cs_action = call_site_table.read_uleb128()?; 122 | if ip < func_start + cs_start { 123 | break; 124 | } 125 | if ip < func_start + cs_start + cs_len { 126 | if cs_lpad == 0 { 127 | return Ok(EHAction::None); 128 | } else { 129 | let lpad = lpad_base + cs_lpad; 130 | if cs_action == 0 { 131 | return Ok(EHAction::Cleanup(lpad)); 132 | } 133 | 134 | action_table.skip((cs_action - 1) as _)?; 135 | let ttype_index = action_table.read_sleb128()?; 136 | return Ok(if ttype_index == 0 { 137 | EHAction::Cleanup(lpad) 138 | } else if ttype_index > 0 { 139 | EHAction::Catch(lpad) 140 | } else { 141 | EHAction::Filter(lpad) 142 | }); 143 | } 144 | } 145 | } 146 | Ok(EHAction::Terminate) 147 | } 148 | 149 | #[lang = "eh_personality"] 150 | unsafe fn rust_eh_personality( 151 | version: c_int, 152 | actions: UnwindAction, 153 | _exception_class: u64, 154 | exception: *mut UnwindException, 155 | unwind_ctx: &mut UnwindContext<'_>, 156 | ) -> UnwindReasonCode { 157 | if version != 1 { 158 | return UnwindReasonCode::FATAL_PHASE1_ERROR; 159 | } 160 | 161 | let lsda = _Unwind_GetLanguageSpecificData(unwind_ctx); 162 | if lsda.is_null() { 163 | return UnwindReasonCode::CONTINUE_UNWIND; 164 | } 165 | 166 | let mut lsda = EndianSlice::new(unsafe { get_unlimited_slice(lsda as _) }, NativeEndian); 167 | let eh_action = match find_eh_action(&mut lsda, unwind_ctx) { 168 | Ok(v) => v, 169 | Err(_) => return UnwindReasonCode::FATAL_PHASE1_ERROR, 170 | }; 171 | 172 | if actions.contains(UnwindAction::SEARCH_PHASE) { 173 | match eh_action { 174 | EHAction::None | EHAction::Cleanup(_) => UnwindReasonCode::CONTINUE_UNWIND, 175 | EHAction::Catch(_) | EHAction::Filter(_) => UnwindReasonCode::HANDLER_FOUND, 176 | EHAction::Terminate => UnwindReasonCode::FATAL_PHASE1_ERROR, 177 | } 178 | } else { 179 | match eh_action { 180 | EHAction::None => UnwindReasonCode::CONTINUE_UNWIND, 181 | // Forced unwinding hits a terminate action. 182 | EHAction::Filter(_) if actions.contains(UnwindAction::FORCE_UNWIND) => { 183 | UnwindReasonCode::CONTINUE_UNWIND 184 | } 185 | EHAction::Cleanup(lpad) | EHAction::Catch(lpad) | EHAction::Filter(lpad) => { 186 | _Unwind_SetGR( 187 | unwind_ctx, 188 | Arch::UNWIND_DATA_REG.0 .0 as _, 189 | exception as usize, 190 | ); 191 | _Unwind_SetGR(unwind_ctx, Arch::UNWIND_DATA_REG.1 .0 as _, 0); 192 | _Unwind_SetIP(unwind_ctx, lpad); 193 | UnwindReasonCode::INSTALL_CONTEXT 194 | } 195 | EHAction::Terminate => UnwindReasonCode::FATAL_PHASE2_ERROR, 196 | } 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /src/personality_dummy.rs: -------------------------------------------------------------------------------- 1 | use crate::abi::*; 2 | use crate::util::*; 3 | 4 | #[lang = "eh_personality"] 5 | unsafe extern "C" fn personality( 6 | version: c_int, 7 | _actions: UnwindAction, 8 | _exception_class: u64, 9 | _exception: *mut UnwindException, 10 | _ctx: &mut UnwindContext<'_>, 11 | ) -> UnwindReasonCode { 12 | if version != 1 { 13 | return UnwindReasonCode::FATAL_PHASE1_ERROR; 14 | } 15 | UnwindReasonCode::CONTINUE_UNWIND 16 | } 17 | -------------------------------------------------------------------------------- /src/print.rs: -------------------------------------------------------------------------------- 1 | pub use crate::{eprint, eprintln, print, println}; 2 | 3 | #[doc(hidden)] 4 | pub struct StdoutPrinter; 5 | impl core::fmt::Write for StdoutPrinter { 6 | fn write_str(&mut self, s: &str) -> core::fmt::Result { 7 | unsafe { libc::printf(b"%.*s\0".as_ptr() as _, s.len() as i32, s.as_ptr()) }; 8 | Ok(()) 9 | } 10 | } 11 | 12 | #[doc(hidden)] 13 | pub struct StderrPrinter; 14 | impl core::fmt::Write for StderrPrinter { 15 | fn write_str(&mut self, s: &str) -> core::fmt::Result { 16 | unsafe { libc::write(libc::STDERR_FILENO, s.as_ptr() as _, s.len() as _) }; 17 | Ok(()) 18 | } 19 | } 20 | 21 | #[macro_export] 22 | macro_rules! println { 23 | ($($arg:tt)*) => ({ 24 | use core::fmt::Write; 25 | let _ = core::writeln!($crate::print::StdoutPrinter, $($arg)*); 26 | }) 27 | } 28 | 29 | #[macro_export] 30 | macro_rules! print { 31 | ($($arg:tt)*) => ({ 32 | use core::fmt::Write; 33 | let _ = core::write!($crate::print::StdoutPrinter, $($arg)*); 34 | }) 35 | } 36 | 37 | #[macro_export] 38 | macro_rules! eprintln { 39 | ($($arg:tt)*) => ({ 40 | use core::fmt::Write; 41 | let _ = core::writeln!($crate::print::StderrPrinter, $($arg)*); 42 | }) 43 | } 44 | 45 | #[macro_export] 46 | macro_rules! eprint { 47 | ($($arg:tt)*) => ({ 48 | use core::fmt::Write; 49 | let _ = core::write!($crate::print::StderrPrinter, $($arg)*); 50 | }) 51 | } 52 | 53 | #[macro_export] 54 | macro_rules! dbg { 55 | () => { 56 | $crate::eprintln!("[{}:{}]", ::core::file!(), ::core::line!()) 57 | }; 58 | ($val:expr $(,)?) => { 59 | match $val { 60 | tmp => { 61 | $crate::eprintln!("[{}:{}] {} = {:#?}", 62 | ::core::file!(), ::core::line!(), ::core::stringify!($val), &tmp); 63 | tmp 64 | } 65 | } 66 | }; 67 | ($($val:expr),+ $(,)?) => { 68 | ($($crate::dbg!($val)),+,) 69 | }; 70 | } 71 | -------------------------------------------------------------------------------- /src/system_alloc.rs: -------------------------------------------------------------------------------- 1 | use core::alloc::{GlobalAlloc, Layout}; 2 | use core::{cmp, mem, ptr}; 3 | 4 | pub struct System; 5 | 6 | const MIN_ALIGN: usize = mem::size_of::() * 2; 7 | 8 | // Taken std 9 | unsafe impl GlobalAlloc for System { 10 | #[inline] 11 | unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 12 | if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { 13 | unsafe { libc::malloc(layout.size()) as *mut u8 } 14 | } else { 15 | let mut out = ptr::null_mut(); 16 | let align = layout.align().max(mem::size_of::()); 17 | let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) }; 18 | if ret != 0 { 19 | ptr::null_mut() 20 | } else { 21 | out as *mut u8 22 | } 23 | } 24 | } 25 | 26 | #[inline] 27 | unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { 28 | if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { 29 | unsafe { libc::calloc(layout.size(), 1) as *mut u8 } 30 | } else { 31 | let ptr = unsafe { self.alloc(layout) }; 32 | if !ptr.is_null() { 33 | unsafe { ptr::write_bytes(ptr, 0, layout.size()) }; 34 | } 35 | ptr 36 | } 37 | } 38 | 39 | #[inline] 40 | unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { 41 | unsafe { libc::free(ptr as *mut libc::c_void) } 42 | } 43 | 44 | #[inline] 45 | unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { 46 | if layout.align() <= MIN_ALIGN && layout.align() <= new_size { 47 | unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 } 48 | } else { 49 | let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) }; 50 | 51 | let new_ptr = unsafe { self.alloc(new_layout) }; 52 | if !new_ptr.is_null() { 53 | let size = cmp::min(layout.size(), new_size); 54 | unsafe { ptr::copy_nonoverlapping(ptr, new_ptr, size) }; 55 | unsafe { self.dealloc(ptr, layout) }; 56 | } 57 | new_ptr 58 | } 59 | } 60 | } 61 | 62 | #[global_allocator] 63 | pub static GLOBAL: System = System; 64 | -------------------------------------------------------------------------------- /src/unwinder/arch/aarch64.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use core::ops; 3 | use gimli::{AArch64, Register}; 4 | 5 | use super::maybe_cfi; 6 | 7 | // Match DWARF_FRAME_REGISTERS in libgcc 8 | pub const MAX_REG_RULES: usize = 97; 9 | 10 | #[repr(C)] 11 | #[derive(Clone, Default)] 12 | pub struct Context { 13 | pub gp: [usize; 31], 14 | pub sp: usize, 15 | pub fp: [usize; 32], 16 | } 17 | 18 | impl fmt::Debug for Context { 19 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 20 | let mut fmt = fmt.debug_struct("Context"); 21 | for i in 0..=30 { 22 | fmt.field( 23 | AArch64::register_name(Register(i as _)).unwrap(), 24 | &self.gp[i], 25 | ); 26 | } 27 | fmt.field("sp", &self.sp); 28 | for i in 0..=31 { 29 | fmt.field( 30 | AArch64::register_name(Register((i + 64) as _)).unwrap(), 31 | &self.fp[i], 32 | ); 33 | } 34 | fmt.finish() 35 | } 36 | } 37 | 38 | impl ops::Index for Context { 39 | type Output = usize; 40 | 41 | fn index(&self, reg: Register) -> &usize { 42 | match reg { 43 | Register(0..=30) => &self.gp[reg.0 as usize], 44 | AArch64::SP => &self.sp, 45 | Register(64..=95) => &self.fp[(reg.0 - 64) as usize], 46 | _ => unimplemented!(), 47 | } 48 | } 49 | } 50 | 51 | impl ops::IndexMut for Context { 52 | fn index_mut(&mut self, reg: Register) -> &mut usize { 53 | match reg { 54 | Register(0..=30) => &mut self.gp[reg.0 as usize], 55 | AArch64::SP => &mut self.sp, 56 | Register(64..=95) => &mut self.fp[(reg.0 - 64) as usize], 57 | _ => unimplemented!(), 58 | } 59 | } 60 | } 61 | 62 | macro_rules! save { 63 | (gp$(, $fp:ident)?) => { 64 | // No need to save caller-saved registers here. 65 | core::arch::naked_asm!( 66 | maybe_cfi!(".cfi_startproc"), 67 | "stp x29, x30, [sp, -16]!", 68 | maybe_cfi!(" 69 | .cfi_def_cfa_offset 16 70 | .cfi_offset x29, -16 71 | .cfi_offset x30, -8 72 | "), 73 | "sub sp, sp, 512", 74 | maybe_cfi!(".cfi_def_cfa_offset 528"), 75 | " 76 | mov x8, x0 77 | mov x0, sp 78 | ", 79 | save!(maybesavefp($($fp)?)), 80 | " 81 | str x19, [sp, 0x98] 82 | stp x20, x21, [sp, 0xA0] 83 | stp x22, x23, [sp, 0xB0] 84 | stp x24, x25, [sp, 0xC0] 85 | stp x26, x27, [sp, 0xD0] 86 | stp x28, x29, [sp, 0xE0] 87 | add x2, sp, 528 88 | stp x30, x2, [sp, 0xF0] 89 | 90 | blr x8 91 | 92 | add sp, sp, 512 93 | ", 94 | maybe_cfi!(".cfi_def_cfa_offset 16"), 95 | "ldp x29, x30, [sp], 16", 96 | maybe_cfi!(" 97 | .cfi_def_cfa_offset 0 98 | .cfi_restore x29 99 | .cfi_restore x30 100 | "), 101 | "ret", 102 | maybe_cfi!(".cfi_endproc"), 103 | ); 104 | }; 105 | (maybesavefp(fp)) => { 106 | " 107 | stp d8, d9, [sp, 0x140] 108 | stp d10, d11, [sp, 0x150] 109 | stp d12, d13, [sp, 0x160] 110 | stp d14, d15, [sp, 0x170] 111 | " 112 | }; 113 | (maybesavefp()) => { "" }; 114 | } 115 | 116 | #[unsafe(naked)] 117 | pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) { 118 | #[cfg(target_feature = "neon")] 119 | save!(gp, fp); 120 | #[cfg(not(target_feature = "neon"))] 121 | save!(gp); 122 | } 123 | 124 | macro_rules! restore { 125 | ($ctx:expr, gp$(, $fp:ident)?) => { 126 | core::arch::asm!( 127 | restore!(mayberestore($($fp)?)), 128 | " 129 | ldp x2, x3, [x0, 0x10] 130 | ldp x4, x5, [x0, 0x20] 131 | ldp x6, x7, [x0, 0x30] 132 | ldp x8, x9, [x0, 0x40] 133 | ldp x10, x11, [x0, 0x50] 134 | ldp x12, x13, [x0, 0x60] 135 | ldp x14, x15, [x0, 0x70] 136 | ldp x16, x17, [x0, 0x80] 137 | ldp x18, x19, [x0, 0x90] 138 | ldp x20, x21, [x0, 0xA0] 139 | ldp x22, x23, [x0, 0xB0] 140 | ldp x24, x25, [x0, 0xC0] 141 | ldp x26, x27, [x0, 0xD0] 142 | ldp x28, x29, [x0, 0xE0] 143 | ldp x30, x1, [x0, 0xF0] 144 | mov sp, x1 145 | 146 | ldp x0, x1, [x0, 0x00] 147 | ret 148 | ", 149 | in("x0") $ctx, 150 | options(noreturn) 151 | ); 152 | }; 153 | (mayberestore(fp)) => { 154 | " 155 | ldp d0, d1, [x0, 0x100] 156 | ldp d2, d3, [x0, 0x110] 157 | ldp d4, d5, [x0, 0x120] 158 | ldp d6, d7, [x0, 0x130] 159 | ldp d8, d9, [x0, 0x140] 160 | ldp d10, d11, [x0, 0x150] 161 | ldp d12, d13, [x0, 0x160] 162 | ldp d14, d15, [x0, 0x170] 163 | ldp d16, d17, [x0, 0x180] 164 | ldp d18, d19, [x0, 0x190] 165 | ldp d20, d21, [x0, 0x1A0] 166 | ldp d22, d23, [x0, 0x1B0] 167 | ldp d24, d25, [x0, 0x1C0] 168 | ldp d26, d27, [x0, 0x1D0] 169 | ldp d28, d29, [x0, 0x1E0] 170 | ldp d30, d31, [x0, 0x1F0] 171 | " 172 | }; 173 | (mayberestore()) => { "" }; 174 | } 175 | 176 | pub unsafe fn restore_context(ctx: &Context) -> ! { 177 | unsafe { 178 | #[cfg(target_feature = "neon")] 179 | restore!(ctx, gp, fp); 180 | #[cfg(not(target_feature = "neon"))] 181 | restore!(ctx, gp); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /src/unwinder/arch/mod.rs: -------------------------------------------------------------------------------- 1 | #[cfg(target_arch = "x86_64")] 2 | mod x86_64; 3 | #[cfg(target_arch = "x86_64")] 4 | pub use x86_64::*; 5 | 6 | #[cfg(target_arch = "x86")] 7 | mod x86; 8 | #[cfg(target_arch = "x86")] 9 | pub use x86::*; 10 | 11 | #[cfg(target_arch = "riscv64")] 12 | mod riscv64; 13 | #[cfg(target_arch = "riscv64")] 14 | pub use riscv64::*; 15 | 16 | #[cfg(target_arch = "riscv32")] 17 | mod riscv32; 18 | #[cfg(target_arch = "riscv32")] 19 | pub use riscv32::*; 20 | 21 | #[cfg(target_arch = "aarch64")] 22 | mod aarch64; 23 | #[cfg(target_arch = "aarch64")] 24 | pub use aarch64::*; 25 | 26 | #[cfg(not(any( 27 | target_arch = "x86_64", 28 | target_arch = "x86", 29 | target_arch = "riscv64", 30 | target_arch = "riscv32", 31 | target_arch = "aarch64" 32 | )))] 33 | compile_error!("Current architecture is not supported"); 34 | 35 | // CFI directives cannot be used if neither debuginfo nor panic=unwind is enabled. 36 | // We don't have an easy way to check the former, so just check based on panic strategy. 37 | #[cfg(panic = "abort")] 38 | macro_rules! maybe_cfi { 39 | ($x: literal) => { 40 | "" 41 | }; 42 | } 43 | 44 | #[cfg(panic = "unwind")] 45 | macro_rules! maybe_cfi { 46 | ($x: literal) => { 47 | $x 48 | }; 49 | } 50 | 51 | pub(crate) use maybe_cfi; 52 | -------------------------------------------------------------------------------- /src/unwinder/arch/riscv32.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use core::ops; 3 | use gimli::{Register, RiscV}; 4 | 5 | use super::maybe_cfi; 6 | 7 | // Match DWARF_FRAME_REGISTERS in libgcc 8 | pub const MAX_REG_RULES: usize = 65; 9 | 10 | #[cfg(all(target_feature = "f", not(target_feature = "d")))] 11 | compile_error!("RISC-V with only F extension is not supported"); 12 | 13 | #[repr(C)] 14 | #[derive(Clone, Default)] 15 | pub struct Context { 16 | pub gp: [usize; 32], 17 | #[cfg(target_feature = "d")] 18 | pub fp: [u64; 32], 19 | } 20 | 21 | impl fmt::Debug for Context { 22 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 23 | let mut fmt = fmt.debug_struct("Context"); 24 | for i in 0..=31 { 25 | fmt.field(RiscV::register_name(Register(i as _)).unwrap(), &self.gp[i]); 26 | } 27 | #[cfg(target_feature = "d")] 28 | for i in 0..=31 { 29 | fmt.field( 30 | RiscV::register_name(Register((i + 32) as _)).unwrap(), 31 | &self.fp[i], 32 | ); 33 | } 34 | fmt.finish() 35 | } 36 | } 37 | 38 | impl ops::Index for Context { 39 | type Output = usize; 40 | 41 | fn index(&self, reg: Register) -> &usize { 42 | match reg { 43 | Register(0..=31) => &self.gp[reg.0 as usize], 44 | // We cannot support indexing fp here. It is 64-bit if D extension is implemented, 45 | // and 32-bit if only F extension is implemented. 46 | _ => unimplemented!(), 47 | } 48 | } 49 | } 50 | 51 | impl ops::IndexMut for Context { 52 | fn index_mut(&mut self, reg: Register) -> &mut usize { 53 | match reg { 54 | Register(0..=31) => &mut self.gp[reg.0 as usize], 55 | // We cannot support indexing fp here. It is 64-bit if D extension is implemented, 56 | // and 32-bit if only F extension is implemented. 57 | _ => unimplemented!(), 58 | } 59 | } 60 | } 61 | 62 | macro_rules! code { 63 | (save_gp) => { 64 | " 65 | sw x0, 0x00(sp) 66 | sw ra, 0x04(sp) 67 | sw t0, 0x08(sp) 68 | sw gp, 0x0C(sp) 69 | sw tp, 0x10(sp) 70 | sw s0, 0x20(sp) 71 | sw s1, 0x24(sp) 72 | sw s2, 0x48(sp) 73 | sw s3, 0x4C(sp) 74 | sw s4, 0x50(sp) 75 | sw s5, 0x54(sp) 76 | sw s6, 0x58(sp) 77 | sw s7, 0x5C(sp) 78 | sw s8, 0x60(sp) 79 | sw s9, 0x64(sp) 80 | sw s10, 0x68(sp) 81 | sw s11, 0x6C(sp) 82 | " 83 | }; 84 | (save_fp) => { 85 | // arch option manipulation needed due to LLVM/Rust bug, see rust-lang/rust#80608 86 | " 87 | .option push 88 | .option arch, +d 89 | fsd fs0, 0xC0(sp) 90 | fsd fs1, 0xC8(sp) 91 | fsd fs2, 0x110(sp) 92 | fsd fs3, 0x118(sp) 93 | fsd fs4, 0x120(sp) 94 | fsd fs5, 0x128(sp) 95 | fsd fs6, 0x130(sp) 96 | fsd fs7, 0x138(sp) 97 | fsd fs8, 0x140(sp) 98 | fsd fs9, 0x148(sp) 99 | fsd fs10, 0x150(sp) 100 | fsd fs11, 0x158(sp) 101 | .option pop 102 | " 103 | }; 104 | (restore_gp) => { 105 | " 106 | lw ra, 0x04(a0) 107 | lw sp, 0x08(a0) 108 | lw gp, 0x0C(a0) 109 | lw tp, 0x10(a0) 110 | lw t0, 0x14(a0) 111 | lw t1, 0x18(a0) 112 | lw t2, 0x1C(a0) 113 | lw s0, 0x20(a0) 114 | lw s1, 0x24(a0) 115 | lw a1, 0x2C(a0) 116 | lw a2, 0x30(a0) 117 | lw a3, 0x34(a0) 118 | lw a4, 0x38(a0) 119 | lw a5, 0x3C(a0) 120 | lw a6, 0x40(a0) 121 | lw a7, 0x44(a0) 122 | lw s2, 0x48(a0) 123 | lw s3, 0x4C(a0) 124 | lw s4, 0x50(a0) 125 | lw s5, 0x54(a0) 126 | lw s6, 0x58(a0) 127 | lw s7, 0x5C(a0) 128 | lw s8, 0x60(a0) 129 | lw s9, 0x64(a0) 130 | lw s10, 0x68(a0) 131 | lw s11, 0x6C(a0) 132 | lw t3, 0x70(a0) 133 | lw t4, 0x74(a0) 134 | lw t5, 0x78(a0) 135 | lw t6, 0x7C(a0) 136 | " 137 | }; 138 | (restore_fp) => { 139 | " 140 | fld ft0, 0x80(a0) 141 | fld ft1, 0x88(a0) 142 | fld ft2, 0x90(a0) 143 | fld ft3, 0x98(a0) 144 | fld ft4, 0xA0(a0) 145 | fld ft5, 0xA8(a0) 146 | fld ft6, 0xB0(a0) 147 | fld ft7, 0xB8(a0) 148 | fld fs0, 0xC0(a0) 149 | fld fs1, 0xC8(a0) 150 | fld fa0, 0xD0(a0) 151 | fld fa1, 0xD8(a0) 152 | fld fa2, 0xE0(a0) 153 | fld fa3, 0xE8(a0) 154 | fld fa4, 0xF0(a0) 155 | fld fa5, 0xF8(a0) 156 | fld fa6, 0x100(a0) 157 | fld fa7, 0x108(a0) 158 | fld fs2, 0x110(a0) 159 | fld fs3, 0x118(a0) 160 | fld fs4, 0x120(a0) 161 | fld fs5, 0x128(a0) 162 | fld fs6, 0x130(a0) 163 | fld fs7, 0x138(a0) 164 | fld fs8, 0x140(a0) 165 | fld fs9, 0x148(a0) 166 | fld fs10, 0x150(a0) 167 | fld fs11, 0x158(a0) 168 | fld ft8, 0x160(a0) 169 | fld ft9, 0x168(a0) 170 | fld ft10, 0x170(a0) 171 | fld ft11, 0x178(a0) 172 | " 173 | }; 174 | } 175 | 176 | #[unsafe(naked)] 177 | pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) { 178 | // No need to save caller-saved registers here. 179 | #[cfg(target_feature = "d")] 180 | core::arch::naked_asm!( 181 | maybe_cfi!(".cfi_startproc"), 182 | " 183 | mv t0, sp 184 | add sp, sp, -0x190 185 | ", 186 | maybe_cfi!(".cfi_def_cfa_offset 0x190"), 187 | "sw ra, 0x180(sp)", 188 | maybe_cfi!(".cfi_offset ra, -16"), 189 | code!(save_gp), 190 | code!(save_fp), 191 | " 192 | mv t0, a0 193 | mv a0, sp 194 | jalr t0 195 | lw ra, 0x180(sp) 196 | add sp, sp, 0x190 197 | ", 198 | maybe_cfi!(".cfi_def_cfa_offset 0"), 199 | maybe_cfi!(".cfi_restore ra"), 200 | "ret", 201 | maybe_cfi!(".cfi_endproc"), 202 | ); 203 | #[cfg(not(target_feature = "d"))] 204 | core::arch::naked_asm!( 205 | maybe_cfi!(".cfi_startproc"), 206 | " 207 | mv t0, sp 208 | add sp, sp, -0x90 209 | ", 210 | maybe_cfi!(".cfi_def_cfa_offset 0x90"), 211 | "sw ra, 0x80(sp)", 212 | maybe_cfi!(".cfi_offset ra, -16"), 213 | code!(save_gp), 214 | " 215 | mv t0, a0 216 | mv a0, sp 217 | jalr t0 218 | lw ra, 0x80(sp) 219 | add sp, sp, 0x90 220 | ", 221 | maybe_cfi!(".cfi_def_cfa_offset 0"), 222 | maybe_cfi!(".cfi_restore ra"), 223 | "ret", 224 | maybe_cfi!(".cfi_endproc") 225 | ); 226 | } 227 | 228 | pub unsafe fn restore_context(ctx: &Context) -> ! { 229 | #[cfg(target_feature = "d")] 230 | unsafe { 231 | core::arch::asm!( 232 | code!(restore_fp), 233 | code!(restore_gp), 234 | " 235 | lw a0, 0x28(a0) 236 | ret 237 | ", 238 | in("a0") ctx, 239 | options(noreturn) 240 | ); 241 | } 242 | #[cfg(not(target_feature = "d"))] 243 | unsafe { 244 | core::arch::asm!( 245 | code!(restore_gp), 246 | " 247 | lw a0, 0x28(a0) 248 | ret 249 | ", 250 | in("a0") ctx, 251 | options(noreturn) 252 | ); 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /src/unwinder/arch/riscv64.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use core::ops; 3 | use gimli::{Register, RiscV}; 4 | 5 | use super::maybe_cfi; 6 | 7 | // Match DWARF_FRAME_REGISTERS in libgcc 8 | pub const MAX_REG_RULES: usize = 65; 9 | 10 | #[cfg(all(target_feature = "f", not(target_feature = "d")))] 11 | compile_error!("RISC-V with only F extension is not supported"); 12 | 13 | #[repr(C)] 14 | #[derive(Clone, Default)] 15 | pub struct Context { 16 | pub gp: [usize; 32], 17 | #[cfg(target_feature = "d")] 18 | pub fp: [usize; 32], 19 | } 20 | 21 | impl fmt::Debug for Context { 22 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 23 | let mut fmt = fmt.debug_struct("Context"); 24 | for i in 0..=31 { 25 | fmt.field(RiscV::register_name(Register(i as _)).unwrap(), &self.gp[i]); 26 | } 27 | #[cfg(target_feature = "d")] 28 | for i in 0..=31 { 29 | fmt.field( 30 | RiscV::register_name(Register((i + 32) as _)).unwrap(), 31 | &self.fp[i], 32 | ); 33 | } 34 | fmt.finish() 35 | } 36 | } 37 | 38 | impl ops::Index for Context { 39 | type Output = usize; 40 | 41 | fn index(&self, reg: Register) -> &usize { 42 | match reg { 43 | Register(0..=31) => &self.gp[reg.0 as usize], 44 | #[cfg(target_feature = "d")] 45 | Register(32..=63) => &self.fp[(reg.0 - 32) as usize], 46 | _ => unimplemented!(), 47 | } 48 | } 49 | } 50 | 51 | impl ops::IndexMut for Context { 52 | fn index_mut(&mut self, reg: Register) -> &mut usize { 53 | match reg { 54 | Register(0..=31) => &mut self.gp[reg.0 as usize], 55 | #[cfg(target_feature = "d")] 56 | Register(32..=63) => &mut self.fp[(reg.0 - 32) as usize], 57 | _ => unimplemented!(), 58 | } 59 | } 60 | } 61 | 62 | macro_rules! code { 63 | (save_gp) => { 64 | " 65 | sd x0, 0x00(sp) 66 | sd ra, 0x08(sp) 67 | sd t0, 0x10(sp) 68 | sd gp, 0x18(sp) 69 | sd tp, 0x20(sp) 70 | sd s0, 0x40(sp) 71 | sd s1, 0x48(sp) 72 | sd s2, 0x90(sp) 73 | sd s3, 0x98(sp) 74 | sd s4, 0xA0(sp) 75 | sd s5, 0xA8(sp) 76 | sd s6, 0xB0(sp) 77 | sd s7, 0xB8(sp) 78 | sd s8, 0xC0(sp) 79 | sd s9, 0xC8(sp) 80 | sd s10, 0xD0(sp) 81 | sd s11, 0xD8(sp) 82 | " 83 | }; 84 | (save_fp) => { 85 | // arch option manipulation needed due to LLVM/Rust bug, see rust-lang/rust#80608 86 | " 87 | .option push 88 | .option arch, +d 89 | fsd fs0, 0x140(sp) 90 | fsd fs1, 0x148(sp) 91 | fsd fs2, 0x190(sp) 92 | fsd fs3, 0x198(sp) 93 | fsd fs4, 0x1A0(sp) 94 | fsd fs5, 0x1A8(sp) 95 | fsd fs6, 0x1B0(sp) 96 | fsd fs7, 0x1B8(sp) 97 | fsd fs8, 0x1C0(sp) 98 | fsd fs9, 0x1C8(sp) 99 | fsd fs10, 0x1D0(sp) 100 | fsd fs11, 0x1D8(sp) 101 | .option pop 102 | " 103 | }; 104 | (restore_gp) => { 105 | " 106 | ld ra, 0x08(a0) 107 | ld sp, 0x10(a0) 108 | ld gp, 0x18(a0) 109 | ld tp, 0x20(a0) 110 | ld t0, 0x28(a0) 111 | ld t1, 0x30(a0) 112 | ld t2, 0x38(a0) 113 | ld s0, 0x40(a0) 114 | ld s1, 0x48(a0) 115 | ld a1, 0x58(a0) 116 | ld a2, 0x60(a0) 117 | ld a3, 0x68(a0) 118 | ld a4, 0x70(a0) 119 | ld a5, 0x78(a0) 120 | ld a6, 0x80(a0) 121 | ld a7, 0x88(a0) 122 | ld s2, 0x90(a0) 123 | ld s3, 0x98(a0) 124 | ld s4, 0xA0(a0) 125 | ld s5, 0xA8(a0) 126 | ld s6, 0xB0(a0) 127 | ld s7, 0xB8(a0) 128 | ld s8, 0xC0(a0) 129 | ld s9, 0xC8(a0) 130 | ld s10, 0xD0(a0) 131 | ld s11, 0xD8(a0) 132 | ld t3, 0xE0(a0) 133 | ld t4, 0xE8(a0) 134 | ld t5, 0xF0(a0) 135 | ld t6, 0xF8(a0) 136 | " 137 | }; 138 | (restore_fp) => { 139 | " 140 | fld ft0, 0x100(a0) 141 | fld ft1, 0x108(a0) 142 | fld ft2, 0x110(a0) 143 | fld ft3, 0x118(a0) 144 | fld ft4, 0x120(a0) 145 | fld ft5, 0x128(a0) 146 | fld ft6, 0x130(a0) 147 | fld ft7, 0x138(a0) 148 | fld fs0, 0x140(a0) 149 | fld fs1, 0x148(a0) 150 | fld fa0, 0x150(a0) 151 | fld fa1, 0x158(a0) 152 | fld fa2, 0x160(a0) 153 | fld fa3, 0x168(a0) 154 | fld fa4, 0x170(a0) 155 | fld fa5, 0x178(a0) 156 | fld fa6, 0x180(a0) 157 | fld fa7, 0x188(a0) 158 | fld fs2, 0x190(a0) 159 | fld fs3, 0x198(a0) 160 | fld fs4, 0x1A0(a0) 161 | fld fs5, 0x1A8(a0) 162 | fld fs6, 0x1B0(a0) 163 | fld fs7, 0x1B8(a0) 164 | fld fs8, 0x1C0(a0) 165 | fld fs9, 0x1C8(a0) 166 | fld fs10, 0x1D0(a0) 167 | fld fs11, 0x1D8(a0) 168 | fld ft8, 0x1E0(a0) 169 | fld ft9, 0x1E8(a0) 170 | fld ft10, 0x1F0(a0) 171 | fld ft11, 0x1F8(a0) 172 | " 173 | }; 174 | } 175 | 176 | #[unsafe(naked)] 177 | pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) { 178 | // No need to save caller-saved registers here. 179 | #[cfg(target_feature = "d")] 180 | core::arch::naked_asm!( 181 | maybe_cfi!(".cfi_startproc"), 182 | " 183 | mv t0, sp 184 | add sp, sp, -0x210 185 | ", 186 | maybe_cfi!(".cfi_def_cfa_offset 0x210"), 187 | "sd ra, 0x200(sp)", 188 | maybe_cfi!(".cfi_offset ra, -16"), 189 | code!(save_gp), 190 | code!(save_fp), 191 | " 192 | mv t0, a0 193 | mv a0, sp 194 | jalr t0 195 | ld ra, 0x200(sp) 196 | add sp, sp, 0x210 197 | ", 198 | maybe_cfi!(".cfi_def_cfa_offset 0"), 199 | maybe_cfi!(".cfi_restore ra"), 200 | "ret", 201 | maybe_cfi!(".cfi_endproc"), 202 | ); 203 | #[cfg(not(target_feature = "d"))] 204 | core::arch::naked_asm!( 205 | maybe_cfi!(".cfi_startproc"), 206 | " 207 | mv t0, sp 208 | add sp, sp, -0x110 209 | ", 210 | maybe_cfi!(".cfi_def_cfa_offset 0x110"), 211 | "sd ra, 0x100(sp)", 212 | maybe_cfi!(".cfi_offset ra, -16"), 213 | code!(save_gp), 214 | " 215 | mv t0, a0 216 | mv a0, sp 217 | jalr t0 218 | ld ra, 0x100(sp) 219 | add sp, sp, 0x110 220 | ", 221 | maybe_cfi!(".cfi_def_cfa_offset 0"), 222 | maybe_cfi!(".cfi_restore ra"), 223 | "ret", 224 | maybe_cfi!(".cfi_endproc"), 225 | ); 226 | } 227 | 228 | pub unsafe fn restore_context(ctx: &Context) -> ! { 229 | #[cfg(target_feature = "d")] 230 | unsafe { 231 | core::arch::asm!( 232 | code!(restore_fp), 233 | code!(restore_gp), 234 | " 235 | ld a0, 0x50(a0) 236 | ret 237 | ", 238 | in("a0") ctx, 239 | options(noreturn) 240 | ); 241 | } 242 | #[cfg(not(target_feature = "d"))] 243 | unsafe { 244 | core::arch::asm!( 245 | code!(restore_gp), 246 | " 247 | ld a0, 0x50(a0) 248 | ret 249 | ", 250 | in("a0") ctx, 251 | options(noreturn) 252 | ); 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /src/unwinder/arch/x86.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use core::ops; 3 | use gimli::{Register, X86}; 4 | 5 | use super::maybe_cfi; 6 | 7 | // Match DWARF_FRAME_REGISTERS in libgcc 8 | pub const MAX_REG_RULES: usize = 17; 9 | 10 | #[repr(C)] 11 | #[derive(Clone, Default)] 12 | pub struct Context { 13 | pub registers: [usize; 8], 14 | pub ra: usize, 15 | pub mcxsr: usize, 16 | pub fcw: usize, 17 | } 18 | 19 | impl fmt::Debug for Context { 20 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 21 | let mut fmt = fmt.debug_struct("Context"); 22 | for i in 0..=7 { 23 | fmt.field( 24 | X86::register_name(Register(i as _)).unwrap(), 25 | &self.registers[i], 26 | ); 27 | } 28 | fmt.field("ra", &self.ra) 29 | .field("mcxsr", &self.mcxsr) 30 | .field("fcw", &self.fcw) 31 | .finish() 32 | } 33 | } 34 | 35 | impl ops::Index for Context { 36 | type Output = usize; 37 | 38 | fn index(&self, reg: Register) -> &usize { 39 | match reg { 40 | Register(0..=7) => &self.registers[reg.0 as usize], 41 | X86::RA => &self.ra, 42 | X86::MXCSR => &self.mcxsr, 43 | _ => unimplemented!(), 44 | } 45 | } 46 | } 47 | 48 | impl ops::IndexMut for Context { 49 | fn index_mut(&mut self, reg: Register) -> &mut usize { 50 | match reg { 51 | Register(0..=7) => &mut self.registers[reg.0 as usize], 52 | X86::RA => &mut self.ra, 53 | X86::MXCSR => &mut self.mcxsr, 54 | _ => unimplemented!(), 55 | } 56 | } 57 | } 58 | 59 | #[unsafe(naked)] 60 | pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) { 61 | // No need to save caller-saved registers here. 62 | core::arch::naked_asm!( 63 | maybe_cfi!(".cfi_startproc"), 64 | "sub esp, 52", 65 | maybe_cfi!(".cfi_def_cfa_offset 56"), 66 | " 67 | mov [esp + 4], ecx 68 | mov [esp + 8], edx 69 | mov [esp + 12], ebx 70 | 71 | /* Adjust the stack to account for the return address */ 72 | lea eax, [esp + 56] 73 | mov [esp + 16], eax 74 | 75 | mov [esp + 20], ebp 76 | mov [esp + 24], esi 77 | mov [esp + 28], edi 78 | 79 | /* Return address */ 80 | mov eax, [esp + 52] 81 | mov [esp + 32], eax 82 | 83 | stmxcsr [esp + 36] 84 | fnstcw [esp + 40] 85 | 86 | mov eax, [esp + 60] 87 | mov ecx, esp 88 | push eax 89 | ", 90 | maybe_cfi!(".cfi_adjust_cfa_offset 4"), 91 | "push ecx", 92 | maybe_cfi!(".cfi_adjust_cfa_offset 4"), 93 | " 94 | call [esp + 64] 95 | 96 | add esp, 60 97 | ", 98 | maybe_cfi!(".cfi_def_cfa_offset 4"), 99 | "ret", 100 | maybe_cfi!(".cfi_endproc"), 101 | ); 102 | } 103 | 104 | pub unsafe fn restore_context(ctx: &Context) -> ! { 105 | unsafe { 106 | core::arch::asm!( 107 | " 108 | /* Restore stack */ 109 | mov esp, [edx + 16] 110 | 111 | /* Restore callee-saved control registers */ 112 | ldmxcsr [edx + 36] 113 | fldcw [edx + 40] 114 | 115 | /* Restore return address */ 116 | mov eax, [edx + 32] 117 | push eax 118 | 119 | /* 120 | * Restore general-purpose registers. Non-callee-saved registers are 121 | * also restored because sometimes it's used to pass unwind arguments. 122 | */ 123 | mov eax, [edx + 0] 124 | mov ecx, [edx + 4] 125 | mov ebx, [edx + 12] 126 | mov ebp, [edx + 20] 127 | mov esi, [edx + 24] 128 | mov edi, [edx + 28] 129 | 130 | /* EDX restored last */ 131 | mov edx, [edx + 8] 132 | 133 | ret 134 | ", 135 | in("edx") ctx, 136 | options(noreturn) 137 | ); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/unwinder/arch/x86_64.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use core::ops; 3 | use gimli::{Register, X86_64}; 4 | 5 | use super::maybe_cfi; 6 | 7 | // Match DWARF_FRAME_REGISTERS in libgcc 8 | pub const MAX_REG_RULES: usize = 17; 9 | 10 | #[repr(C)] 11 | #[derive(Clone, Default)] 12 | pub struct Context { 13 | pub registers: [usize; 16], 14 | pub ra: usize, 15 | pub mcxsr: usize, 16 | pub fcw: usize, 17 | } 18 | 19 | impl fmt::Debug for Context { 20 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 21 | let mut fmt = fmt.debug_struct("Context"); 22 | for i in 0..=15 { 23 | fmt.field( 24 | X86_64::register_name(Register(i as _)).unwrap(), 25 | &self.registers[i], 26 | ); 27 | } 28 | fmt.field("ra", &self.ra) 29 | .field("mcxsr", &self.mcxsr) 30 | .field("fcw", &self.fcw) 31 | .finish() 32 | } 33 | } 34 | 35 | impl ops::Index for Context { 36 | type Output = usize; 37 | 38 | fn index(&self, reg: Register) -> &usize { 39 | match reg { 40 | Register(0..=15) => &self.registers[reg.0 as usize], 41 | X86_64::RA => &self.ra, 42 | X86_64::MXCSR => &self.mcxsr, 43 | X86_64::FCW => &self.fcw, 44 | _ => unimplemented!(), 45 | } 46 | } 47 | } 48 | 49 | impl ops::IndexMut for Context { 50 | fn index_mut(&mut self, reg: Register) -> &mut usize { 51 | match reg { 52 | Register(0..=15) => &mut self.registers[reg.0 as usize], 53 | X86_64::RA => &mut self.ra, 54 | X86_64::MXCSR => &mut self.mcxsr, 55 | X86_64::FCW => &mut self.fcw, 56 | _ => unimplemented!(), 57 | } 58 | } 59 | } 60 | 61 | #[unsafe(naked)] 62 | pub extern "C-unwind" fn save_context(f: extern "C" fn(&mut Context, *mut ()), ptr: *mut ()) { 63 | // No need to save caller-saved registers here. 64 | core::arch::naked_asm!( 65 | maybe_cfi!(".cfi_startproc"), 66 | "sub rsp, 0x98", 67 | maybe_cfi!(".cfi_def_cfa_offset 0xA0"), 68 | " 69 | mov [rsp + 0x18], rbx 70 | mov [rsp + 0x30], rbp 71 | 72 | /* Adjust the stack to account for the return address */ 73 | lea rax, [rsp + 0xA0] 74 | mov [rsp + 0x38], rax 75 | 76 | mov [rsp + 0x60], r12 77 | mov [rsp + 0x68], r13 78 | mov [rsp + 0x70], r14 79 | mov [rsp + 0x78], r15 80 | 81 | /* Return address */ 82 | mov rax, [rsp + 0x98] 83 | mov [rsp + 0x80], rax 84 | 85 | stmxcsr [rsp + 0x88] 86 | fnstcw [rsp + 0x90] 87 | 88 | mov rax, rdi 89 | mov rdi, rsp 90 | call rax 91 | add rsp, 0x98 92 | ", 93 | maybe_cfi!(".cfi_def_cfa_offset 8"), 94 | "ret", 95 | maybe_cfi!(".cfi_endproc"), 96 | ); 97 | } 98 | 99 | pub unsafe fn restore_context(ctx: &Context) -> ! { 100 | unsafe { 101 | core::arch::asm!( 102 | " 103 | /* Restore stack */ 104 | mov rsp, [rdi + 0x38] 105 | 106 | /* Restore callee-saved control registers */ 107 | ldmxcsr [rdi + 0x88] 108 | fldcw [rdi + 0x90] 109 | 110 | /* Restore return address */ 111 | mov rax, [rdi + 0x80] 112 | push rax 113 | 114 | /* 115 | * Restore general-purpose registers. Non-callee-saved registers are 116 | * also restored because sometimes it's used to pass unwind arguments. 117 | */ 118 | mov rax, [rdi + 0x00] 119 | mov rdx, [rdi + 0x08] 120 | mov rcx, [rdi + 0x10] 121 | mov rbx, [rdi + 0x18] 122 | mov rsi, [rdi + 0x20] 123 | mov rbp, [rdi + 0x30] 124 | mov r8 , [rdi + 0x40] 125 | mov r9 , [rdi + 0x48] 126 | mov r10, [rdi + 0x50] 127 | mov r11, [rdi + 0x58] 128 | mov r12, [rdi + 0x60] 129 | mov r13, [rdi + 0x68] 130 | mov r14, [rdi + 0x70] 131 | mov r15, [rdi + 0x78] 132 | 133 | /* RDI restored last */ 134 | mov rdi, [rdi + 0x28] 135 | 136 | ret 137 | ", 138 | in("rdi") ctx, 139 | options(noreturn) 140 | ); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/custom.rs: -------------------------------------------------------------------------------- 1 | use super::{FDEFinder, FDESearchResult}; 2 | use crate::util::{deref_pointer, get_unlimited_slice}; 3 | 4 | use core::sync::atomic::{AtomicU32, Ordering}; 5 | use gimli::{BaseAddresses, EhFrame, EhFrameHdr, NativeEndian, UnwindSection}; 6 | 7 | pub(crate) struct CustomFinder(()); 8 | 9 | pub(crate) fn get_finder() -> &'static CustomFinder { 10 | &CustomFinder(()) 11 | } 12 | 13 | impl FDEFinder for CustomFinder { 14 | fn find_fde(&self, pc: usize) -> Option { 15 | get_custom_eh_frame_finder().and_then(|eh_frame_finder| find_fde(eh_frame_finder, pc)) 16 | } 17 | } 18 | 19 | /// A trait for types whose values can be used as the global EH frame finder set by [`set_custom_eh_frame_finder`]. 20 | pub unsafe trait EhFrameFinder { 21 | fn find(&self, pc: usize) -> Option; 22 | } 23 | 24 | pub struct FrameInfo { 25 | pub text_base: Option, 26 | pub kind: FrameInfoKind, 27 | } 28 | 29 | pub enum FrameInfoKind { 30 | EhFrameHdr(usize), 31 | EhFrame(usize), 32 | } 33 | 34 | static mut CUSTOM_EH_FRAME_FINDER: Option<&(dyn EhFrameFinder + Sync)> = None; 35 | 36 | static CUSTOM_EH_FRAME_FINDER_STATE: AtomicU32 = AtomicU32::new(UNINITIALIZED); 37 | 38 | const UNINITIALIZED: u32 = 0; 39 | const INITIALIZING: u32 = 1; 40 | const INITIALIZED: u32 = 2; 41 | 42 | /// The type returned by [`set_custom_eh_frame_finder`] if [`set_custom_eh_frame_finder`] has 43 | /// already been called. 44 | #[derive(Debug)] 45 | pub struct SetCustomEhFrameFinderError(()); 46 | 47 | /// Sets the global EH frame finder. 48 | /// 49 | /// This function should only be called once during the lifetime of the program. 50 | /// 51 | /// # Errors 52 | /// 53 | /// An error is returned if this function has already been called during the lifetime of the 54 | /// program. 55 | pub fn set_custom_eh_frame_finder( 56 | fde_finder: &'static (dyn EhFrameFinder + Sync), 57 | ) -> Result<(), SetCustomEhFrameFinderError> { 58 | match CUSTOM_EH_FRAME_FINDER_STATE.compare_exchange( 59 | UNINITIALIZED, 60 | INITIALIZING, 61 | Ordering::SeqCst, 62 | Ordering::SeqCst, 63 | ) { 64 | Ok(UNINITIALIZED) => { 65 | unsafe { 66 | CUSTOM_EH_FRAME_FINDER = Some(fde_finder); 67 | } 68 | CUSTOM_EH_FRAME_FINDER_STATE.store(INITIALIZED, Ordering::SeqCst); 69 | Ok(()) 70 | } 71 | Err(INITIALIZING) => { 72 | while CUSTOM_EH_FRAME_FINDER_STATE.load(Ordering::SeqCst) == INITIALIZING { 73 | core::hint::spin_loop(); 74 | } 75 | Err(SetCustomEhFrameFinderError(())) 76 | } 77 | Err(INITIALIZED) => Err(SetCustomEhFrameFinderError(())), 78 | _ => { 79 | unreachable!() 80 | } 81 | } 82 | } 83 | 84 | fn get_custom_eh_frame_finder() -> Option<&'static dyn EhFrameFinder> { 85 | if CUSTOM_EH_FRAME_FINDER_STATE.load(Ordering::SeqCst) == INITIALIZED { 86 | Some(unsafe { CUSTOM_EH_FRAME_FINDER.unwrap() }) 87 | } else { 88 | None 89 | } 90 | } 91 | 92 | fn find_fde(eh_frame_finder: &T, pc: usize) -> Option { 93 | let info = eh_frame_finder.find(pc)?; 94 | let text_base = info.text_base; 95 | match info.kind { 96 | FrameInfoKind::EhFrameHdr(eh_frame_hdr) => { 97 | find_fde_with_eh_frame_hdr(pc, text_base, eh_frame_hdr) 98 | } 99 | FrameInfoKind::EhFrame(eh_frame) => find_fde_with_eh_frame(pc, text_base, eh_frame), 100 | } 101 | } 102 | 103 | fn find_fde_with_eh_frame_hdr( 104 | pc: usize, 105 | text_base: Option, 106 | eh_frame_hdr: usize, 107 | ) -> Option { 108 | unsafe { 109 | let mut bases = BaseAddresses::default().set_eh_frame_hdr(eh_frame_hdr as _); 110 | if let Some(text_base) = text_base { 111 | bases = bases.set_text(text_base as _); 112 | } 113 | let eh_frame_hdr = EhFrameHdr::new( 114 | get_unlimited_slice(eh_frame_hdr as usize as _), 115 | NativeEndian, 116 | ) 117 | .parse(&bases, core::mem::size_of::() as _) 118 | .ok()?; 119 | let eh_frame = deref_pointer(eh_frame_hdr.eh_frame_ptr()); 120 | let bases = bases.set_eh_frame(eh_frame as _); 121 | let eh_frame = EhFrame::new(get_unlimited_slice(eh_frame as _), NativeEndian); 122 | 123 | // Use binary search table for address if available. 124 | if let Some(table) = eh_frame_hdr.table() { 125 | if let Ok(fde) = 126 | table.fde_for_address(&eh_frame, &bases, pc as _, EhFrame::cie_from_offset) 127 | { 128 | return Some(FDESearchResult { 129 | fde, 130 | bases, 131 | eh_frame, 132 | }); 133 | } 134 | } 135 | 136 | // Otherwise do the linear search. 137 | if let Ok(fde) = eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) { 138 | return Some(FDESearchResult { 139 | fde, 140 | bases, 141 | eh_frame, 142 | }); 143 | } 144 | 145 | None 146 | } 147 | } 148 | 149 | fn find_fde_with_eh_frame( 150 | pc: usize, 151 | text_base: Option, 152 | eh_frame: usize, 153 | ) -> Option { 154 | unsafe { 155 | let mut bases = BaseAddresses::default().set_eh_frame(eh_frame as _); 156 | if let Some(text_base) = text_base { 157 | bases = bases.set_text(text_base as _); 158 | } 159 | let eh_frame = EhFrame::new(get_unlimited_slice(eh_frame as _), NativeEndian); 160 | 161 | if let Ok(fde) = eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) { 162 | return Some(FDESearchResult { 163 | fde, 164 | bases, 165 | eh_frame, 166 | }); 167 | } 168 | 169 | None 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/fixed.rs: -------------------------------------------------------------------------------- 1 | use super::FDESearchResult; 2 | use crate::util::*; 3 | 4 | use gimli::{BaseAddresses, EhFrame, NativeEndian, UnwindSection}; 5 | 6 | pub struct StaticFinder(()); 7 | 8 | pub fn get_finder() -> &'static StaticFinder { 9 | &StaticFinder(()) 10 | } 11 | 12 | unsafe extern "C" { 13 | static __executable_start: u8; 14 | static __etext: u8; 15 | static __eh_frame: u8; 16 | } 17 | 18 | impl super::FDEFinder for StaticFinder { 19 | fn find_fde(&self, pc: usize) -> Option { 20 | unsafe { 21 | let text_start = &__executable_start as *const u8 as usize; 22 | let text_end = &__etext as *const u8 as usize; 23 | if !(text_start..text_end).contains(&pc) { 24 | return None; 25 | } 26 | 27 | let eh_frame = &__eh_frame as *const u8 as usize; 28 | let bases = BaseAddresses::default() 29 | .set_eh_frame(eh_frame as _) 30 | .set_text(text_start as _); 31 | let eh_frame = EhFrame::new(get_unlimited_slice(eh_frame as _), NativeEndian); 32 | 33 | if let Ok(fde) = eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) { 34 | return Some(FDESearchResult { 35 | fde, 36 | bases, 37 | eh_frame, 38 | }); 39 | } 40 | 41 | None 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/gnu_eh_frame_hdr.rs: -------------------------------------------------------------------------------- 1 | use super::FDESearchResult; 2 | use crate::util::*; 3 | 4 | use gimli::{BaseAddresses, EhFrame, EhFrameHdr, NativeEndian, UnwindSection}; 5 | 6 | pub struct StaticFinder(()); 7 | 8 | pub fn get_finder() -> &'static StaticFinder { 9 | &StaticFinder(()) 10 | } 11 | 12 | unsafe extern "C" { 13 | static __executable_start: u8; 14 | static __etext: u8; 15 | static __GNU_EH_FRAME_HDR: u8; 16 | } 17 | 18 | impl super::FDEFinder for StaticFinder { 19 | fn find_fde(&self, pc: usize) -> Option { 20 | unsafe { 21 | let text_start = &__executable_start as *const u8 as usize; 22 | let text_end = &__etext as *const u8 as usize; 23 | if !(text_start..text_end).contains(&pc) { 24 | return None; 25 | } 26 | 27 | let eh_frame_hdr = &__GNU_EH_FRAME_HDR as *const u8 as usize; 28 | let bases = BaseAddresses::default() 29 | .set_text(text_start as _) 30 | .set_eh_frame_hdr(eh_frame_hdr as _); 31 | let eh_frame_hdr = EhFrameHdr::new( 32 | get_unlimited_slice(eh_frame_hdr as usize as _), 33 | NativeEndian, 34 | ) 35 | .parse(&bases, core::mem::size_of::() as _) 36 | .ok()?; 37 | let eh_frame = deref_pointer(eh_frame_hdr.eh_frame_ptr()); 38 | let bases = bases.set_eh_frame(eh_frame as _); 39 | let eh_frame = EhFrame::new(get_unlimited_slice(eh_frame as _), NativeEndian); 40 | 41 | // Use binary search table for address if available. 42 | if let Some(table) = eh_frame_hdr.table() { 43 | if let Ok(fde) = 44 | table.fde_for_address(&eh_frame, &bases, pc as _, EhFrame::cie_from_offset) 45 | { 46 | return Some(FDESearchResult { 47 | fde, 48 | bases, 49 | eh_frame, 50 | }); 51 | } 52 | } 53 | 54 | // Otherwise do the linear search. 55 | if let Ok(fde) = eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) { 56 | return Some(FDESearchResult { 57 | fde, 58 | bases, 59 | eh_frame, 60 | }); 61 | } 62 | 63 | None 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/mod.rs: -------------------------------------------------------------------------------- 1 | #[cfg(feature = "fde-custom")] 2 | mod custom; 3 | #[cfg(feature = "fde-static")] 4 | mod fixed; 5 | #[cfg(feature = "fde-gnu-eh-frame-hdr")] 6 | mod gnu_eh_frame_hdr; 7 | #[cfg(feature = "fde-phdr")] 8 | mod phdr; 9 | #[cfg(feature = "fde-registry")] 10 | mod registry; 11 | 12 | use crate::util::*; 13 | use gimli::{BaseAddresses, EhFrame, FrameDescriptionEntry}; 14 | 15 | #[cfg(feature = "fde-custom")] 16 | pub mod custom_eh_frame_finder { 17 | pub use super::custom::{ 18 | set_custom_eh_frame_finder, EhFrameFinder, FrameInfo, FrameInfoKind, 19 | SetCustomEhFrameFinderError, 20 | }; 21 | } 22 | 23 | #[derive(Debug)] 24 | pub struct FDESearchResult { 25 | pub fde: FrameDescriptionEntry, 26 | pub bases: BaseAddresses, 27 | pub eh_frame: EhFrame, 28 | } 29 | 30 | pub trait FDEFinder { 31 | fn find_fde(&self, pc: usize) -> Option; 32 | } 33 | 34 | pub struct GlobalFinder(()); 35 | 36 | impl FDEFinder for GlobalFinder { 37 | fn find_fde(&self, pc: usize) -> Option { 38 | #[cfg(feature = "fde-custom")] 39 | if let Some(v) = custom::get_finder().find_fde(pc) { 40 | return Some(v); 41 | } 42 | #[cfg(feature = "fde-registry")] 43 | if let Some(v) = registry::get_finder().find_fde(pc) { 44 | return Some(v); 45 | } 46 | #[cfg(feature = "fde-gnu-eh-frame-hdr")] 47 | if let Some(v) = gnu_eh_frame_hdr::get_finder().find_fde(pc) { 48 | return Some(v); 49 | } 50 | #[cfg(feature = "fde-phdr")] 51 | if let Some(v) = phdr::get_finder().find_fde(pc) { 52 | return Some(v); 53 | } 54 | #[cfg(feature = "fde-static")] 55 | if let Some(v) = fixed::get_finder().find_fde(pc) { 56 | return Some(v); 57 | } 58 | None 59 | } 60 | } 61 | 62 | pub fn get_finder() -> &'static GlobalFinder { 63 | &GlobalFinder(()) 64 | } 65 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/phdr.rs: -------------------------------------------------------------------------------- 1 | use super::FDESearchResult; 2 | use crate::util::*; 3 | 4 | use core::mem; 5 | use core::slice; 6 | use gimli::{BaseAddresses, EhFrame, EhFrameHdr, NativeEndian, UnwindSection}; 7 | use libc::{PT_DYNAMIC, PT_GNU_EH_FRAME, PT_LOAD}; 8 | 9 | #[cfg(target_pointer_width = "32")] 10 | use libc::Elf32_Phdr as Elf_Phdr; 11 | #[cfg(target_pointer_width = "64")] 12 | use libc::Elf64_Phdr as Elf_Phdr; 13 | 14 | pub struct PhdrFinder(()); 15 | 16 | pub fn get_finder() -> &'static PhdrFinder { 17 | &PhdrFinder(()) 18 | } 19 | 20 | impl super::FDEFinder for PhdrFinder { 21 | fn find_fde(&self, pc: usize) -> Option { 22 | #[cfg(feature = "fde-phdr-aux")] 23 | if let Some(v) = search_aux_phdr(pc) { 24 | return Some(v); 25 | } 26 | #[cfg(feature = "fde-phdr-dl")] 27 | if let Some(v) = search_dl_phdr(pc) { 28 | return Some(v); 29 | } 30 | None 31 | } 32 | } 33 | 34 | #[cfg(feature = "fde-phdr-aux")] 35 | fn search_aux_phdr(pc: usize) -> Option { 36 | use libc::{getauxval, AT_PHDR, AT_PHNUM, PT_PHDR}; 37 | 38 | unsafe { 39 | let phdr = getauxval(AT_PHDR) as *const Elf_Phdr; 40 | let phnum = getauxval(AT_PHNUM) as usize; 41 | let phdrs = slice::from_raw_parts(phdr, phnum); 42 | // With known address of PHDR, we can calculate the base address in reverse. 43 | let base = 44 | phdrs.as_ptr() as usize - phdrs.iter().find(|x| x.p_type == PT_PHDR)?.p_vaddr as usize; 45 | search_phdr(phdrs, base, pc) 46 | } 47 | } 48 | 49 | #[cfg(feature = "fde-phdr-dl")] 50 | fn search_dl_phdr(pc: usize) -> Option { 51 | use core::ffi::c_void; 52 | use libc::{dl_iterate_phdr, dl_phdr_info}; 53 | 54 | struct CallbackData { 55 | pc: usize, 56 | result: Option, 57 | } 58 | 59 | unsafe extern "C" fn phdr_callback( 60 | info: *mut dl_phdr_info, 61 | _size: usize, 62 | data: *mut c_void, 63 | ) -> c_int { 64 | unsafe { 65 | let data = &mut *(data as *mut CallbackData); 66 | let phdrs = slice::from_raw_parts((*info).dlpi_phdr, (*info).dlpi_phnum as usize); 67 | if let Some(v) = search_phdr(phdrs, (*info).dlpi_addr as _, data.pc) { 68 | data.result = Some(v); 69 | return 1; 70 | } 71 | 0 72 | } 73 | } 74 | 75 | let mut data = CallbackData { pc, result: None }; 76 | unsafe { dl_iterate_phdr(Some(phdr_callback), &mut data as *mut CallbackData as _) }; 77 | data.result 78 | } 79 | 80 | fn search_phdr(phdrs: &[Elf_Phdr], base: usize, pc: usize) -> Option { 81 | unsafe { 82 | let mut text = None; 83 | let mut eh_frame_hdr = None; 84 | let mut dynamic = None; 85 | 86 | for phdr in phdrs { 87 | let start = base + phdr.p_vaddr as usize; 88 | match phdr.p_type { 89 | PT_LOAD => { 90 | let end = start + phdr.p_memsz as usize; 91 | let range = start..end; 92 | if range.contains(&pc) { 93 | text = Some(range); 94 | } 95 | } 96 | PT_GNU_EH_FRAME => { 97 | eh_frame_hdr = Some(start); 98 | } 99 | PT_DYNAMIC => { 100 | dynamic = Some(start); 101 | } 102 | _ => (), 103 | } 104 | } 105 | 106 | let text = text?; 107 | let eh_frame_hdr = eh_frame_hdr?; 108 | 109 | let mut bases = BaseAddresses::default() 110 | .set_eh_frame_hdr(eh_frame_hdr as _) 111 | .set_text(text.start as _); 112 | 113 | // Find the GOT section. 114 | if let Some(start) = dynamic { 115 | const DT_NULL: usize = 0; 116 | const DT_PLTGOT: usize = 3; 117 | 118 | let mut tags = start as *const [usize; 2]; 119 | let mut tag = *tags; 120 | while tag[0] != DT_NULL { 121 | if tag[0] == DT_PLTGOT { 122 | bases = bases.set_got(tag[1] as _); 123 | break; 124 | } 125 | tags = tags.add(1); 126 | tag = *tags; 127 | } 128 | } 129 | 130 | // Parse .eh_frame_hdr section. 131 | let eh_frame_hdr = EhFrameHdr::new( 132 | get_unlimited_slice(eh_frame_hdr as usize as _), 133 | NativeEndian, 134 | ) 135 | .parse(&bases, mem::size_of::() as _) 136 | .ok()?; 137 | 138 | let eh_frame = deref_pointer(eh_frame_hdr.eh_frame_ptr()); 139 | bases = bases.set_eh_frame(eh_frame as _); 140 | let eh_frame = EhFrame::new(get_unlimited_slice(eh_frame as usize as _), NativeEndian); 141 | 142 | // Use binary search table for address if available. 143 | if let Some(table) = eh_frame_hdr.table() { 144 | if let Ok(fde) = 145 | table.fde_for_address(&eh_frame, &bases, pc as _, EhFrame::cie_from_offset) 146 | { 147 | return Some(FDESearchResult { 148 | fde, 149 | bases, 150 | eh_frame, 151 | }); 152 | } 153 | } 154 | 155 | // Otherwise do the linear search. 156 | if let Ok(fde) = eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) { 157 | return Some(FDESearchResult { 158 | fde, 159 | bases, 160 | eh_frame, 161 | }); 162 | } 163 | 164 | None 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/unwinder/find_fde/registry.rs: -------------------------------------------------------------------------------- 1 | use super::FDESearchResult; 2 | use crate::util::get_unlimited_slice; 3 | use alloc::boxed::Box; 4 | use core::ffi::c_void; 5 | use core::mem::MaybeUninit; 6 | use core::ops; 7 | use core::ptr; 8 | use gimli::{BaseAddresses, EhFrame, NativeEndian, UnwindSection}; 9 | 10 | enum Table { 11 | Single(*const c_void), 12 | Multiple(*const *const c_void), 13 | } 14 | 15 | struct Object { 16 | next: *mut Object, 17 | tbase: usize, 18 | dbase: usize, 19 | table: Table, 20 | } 21 | 22 | struct GlobalState { 23 | object: *mut Object, 24 | } 25 | 26 | unsafe impl Send for GlobalState {} 27 | 28 | pub struct Registry(()); 29 | 30 | // `unsafe` because there is no protection for reentrance. 31 | unsafe fn lock_global_state() -> impl ops::DerefMut { 32 | #[cfg(feature = "libc")] 33 | { 34 | static mut MUTEX: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; 35 | unsafe { libc::pthread_mutex_lock(core::ptr::addr_of_mut!(MUTEX)) }; 36 | 37 | static mut STATE: GlobalState = GlobalState { 38 | object: ptr::null_mut(), 39 | }; 40 | 41 | struct LockGuard; 42 | impl Drop for LockGuard { 43 | fn drop(&mut self) { 44 | unsafe { libc::pthread_mutex_unlock(core::ptr::addr_of_mut!(MUTEX)) }; 45 | } 46 | } 47 | 48 | impl ops::Deref for LockGuard { 49 | type Target = GlobalState; 50 | 51 | #[allow(static_mut_refs)] 52 | fn deref(&self) -> &GlobalState { 53 | unsafe { &*core::ptr::addr_of!(STATE) } 54 | } 55 | } 56 | 57 | impl ops::DerefMut for LockGuard { 58 | fn deref_mut(&mut self) -> &mut GlobalState { 59 | unsafe { &mut *core::ptr::addr_of_mut!(STATE) } 60 | } 61 | } 62 | 63 | LockGuard 64 | } 65 | #[cfg(not(feature = "libc"))] 66 | { 67 | static MUTEX: spin::Mutex = spin::Mutex::new(GlobalState { 68 | object: ptr::null_mut(), 69 | }); 70 | MUTEX.lock() 71 | } 72 | #[cfg(not(any(feature = "libc", feature = "spin")))] 73 | compile_error!("Either feature \"libc\" or \"spin\" must be enabled to use \"fde-registry\"."); 74 | } 75 | 76 | pub fn get_finder() -> &'static Registry { 77 | &Registry(()) 78 | } 79 | 80 | impl super::FDEFinder for Registry { 81 | fn find_fde(&self, pc: usize) -> Option { 82 | unsafe { 83 | let guard = lock_global_state(); 84 | let mut cur = guard.object; 85 | 86 | while !cur.is_null() { 87 | let bases = BaseAddresses::default() 88 | .set_text((*cur).tbase as _) 89 | .set_got((*cur).dbase as _); 90 | match (*cur).table { 91 | Table::Single(addr) => { 92 | let eh_frame = EhFrame::new(get_unlimited_slice(addr as _), NativeEndian); 93 | let bases = bases.clone().set_eh_frame(addr as usize as _); 94 | if let Ok(fde) = 95 | eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) 96 | { 97 | return Some(FDESearchResult { 98 | fde, 99 | bases, 100 | eh_frame, 101 | }); 102 | } 103 | } 104 | Table::Multiple(mut addrs) => { 105 | let mut addr = *addrs; 106 | while !addr.is_null() { 107 | let eh_frame = 108 | EhFrame::new(get_unlimited_slice(addr as _), NativeEndian); 109 | let bases = bases.clone().set_eh_frame(addr as usize as _); 110 | if let Ok(fde) = 111 | eh_frame.fde_for_address(&bases, pc as _, EhFrame::cie_from_offset) 112 | { 113 | return Some(FDESearchResult { 114 | fde, 115 | bases, 116 | eh_frame, 117 | }); 118 | } 119 | 120 | addrs = addrs.add(1); 121 | addr = *addrs; 122 | } 123 | } 124 | } 125 | 126 | cur = (*cur).next; 127 | } 128 | } 129 | 130 | None 131 | } 132 | } 133 | 134 | #[unsafe(no_mangle)] 135 | unsafe extern "C" fn __register_frame_info_bases( 136 | begin: *const c_void, 137 | ob: *mut Object, 138 | tbase: *const c_void, 139 | dbase: *const c_void, 140 | ) { 141 | if begin.is_null() { 142 | return; 143 | } 144 | 145 | unsafe { 146 | ob.write(Object { 147 | next: core::ptr::null_mut(), 148 | tbase: tbase as _, 149 | dbase: dbase as _, 150 | table: Table::Single(begin), 151 | }); 152 | 153 | let mut guard = lock_global_state(); 154 | (*ob).next = guard.object; 155 | guard.object = ob; 156 | } 157 | } 158 | 159 | #[unsafe(no_mangle)] 160 | unsafe extern "C" fn __register_frame_info(begin: *const c_void, ob: *mut Object) { 161 | unsafe { __register_frame_info_bases(begin, ob, core::ptr::null_mut(), core::ptr::null_mut()) } 162 | } 163 | 164 | #[unsafe(no_mangle)] 165 | unsafe extern "C" fn __register_frame(begin: *const c_void) { 166 | if begin.is_null() { 167 | return; 168 | } 169 | 170 | let storage = Box::into_raw(Box::new(MaybeUninit::::uninit())) as *mut Object; 171 | unsafe { __register_frame_info(begin, storage) } 172 | } 173 | 174 | #[unsafe(no_mangle)] 175 | unsafe extern "C" fn __register_frame_info_table_bases( 176 | begin: *const c_void, 177 | ob: *mut Object, 178 | tbase: *const c_void, 179 | dbase: *const c_void, 180 | ) { 181 | unsafe { 182 | ob.write(Object { 183 | next: core::ptr::null_mut(), 184 | tbase: tbase as _, 185 | dbase: dbase as _, 186 | table: Table::Multiple(begin as _), 187 | }); 188 | 189 | let mut guard = lock_global_state(); 190 | (*ob).next = guard.object; 191 | guard.object = ob; 192 | } 193 | } 194 | 195 | #[unsafe(no_mangle)] 196 | unsafe extern "C" fn __register_frame_info_table(begin: *const c_void, ob: *mut Object) { 197 | unsafe { 198 | __register_frame_info_table_bases(begin, ob, core::ptr::null_mut(), core::ptr::null_mut()) 199 | } 200 | } 201 | 202 | #[unsafe(no_mangle)] 203 | unsafe extern "C" fn __register_frame_table(begin: *const c_void) { 204 | if begin.is_null() { 205 | return; 206 | } 207 | 208 | let storage = Box::into_raw(Box::new(MaybeUninit::::uninit())) as *mut Object; 209 | unsafe { __register_frame_info_table(begin, storage) } 210 | } 211 | 212 | #[unsafe(no_mangle)] 213 | extern "C" fn __deregister_frame_info_bases(begin: *const c_void) -> *mut Object { 214 | if begin.is_null() { 215 | return core::ptr::null_mut(); 216 | } 217 | 218 | let mut guard = unsafe { lock_global_state() }; 219 | unsafe { 220 | let mut prev = &mut guard.object; 221 | let mut cur = *prev; 222 | 223 | while !cur.is_null() { 224 | let found = match (*cur).table { 225 | Table::Single(addr) => addr == begin, 226 | _ => false, 227 | }; 228 | if found { 229 | *prev = (*cur).next; 230 | return cur; 231 | } 232 | prev = &mut (*cur).next; 233 | cur = *prev; 234 | } 235 | } 236 | 237 | core::ptr::null_mut() 238 | } 239 | 240 | #[unsafe(no_mangle)] 241 | extern "C" fn __deregister_frame_info(begin: *const c_void) -> *mut Object { 242 | __deregister_frame_info_bases(begin) 243 | } 244 | 245 | #[unsafe(no_mangle)] 246 | unsafe extern "C" fn __deregister_frame(begin: *const c_void) { 247 | if begin.is_null() { 248 | return; 249 | } 250 | let storage = __deregister_frame_info(begin); 251 | drop(unsafe { Box::from_raw(storage as *mut MaybeUninit) }) 252 | } 253 | -------------------------------------------------------------------------------- /src/unwinder/frame.rs: -------------------------------------------------------------------------------- 1 | use gimli::{ 2 | BaseAddresses, CfaRule, Register, RegisterRule, UnwindContext, UnwindExpression, UnwindTableRow, 3 | }; 4 | #[cfg(feature = "dwarf-expr")] 5 | use gimli::{Evaluation, EvaluationResult, Location, Value}; 6 | 7 | use super::arch::*; 8 | use super::find_fde::{self, FDEFinder, FDESearchResult}; 9 | use crate::abi::PersonalityRoutine; 10 | use crate::arch::*; 11 | use crate::util::*; 12 | 13 | struct StoreOnStack; 14 | 15 | // gimli's MSRV doesn't allow const generics, so we need to pick a supported array size. 16 | const fn next_value(x: usize) -> usize { 17 | let supported = [0, 1, 2, 3, 4, 8, 16, 32, 64, 128]; 18 | let mut i = 0; 19 | while i < supported.len() { 20 | if supported[i] >= x { 21 | return supported[i]; 22 | } 23 | i += 1; 24 | } 25 | 192 26 | } 27 | 28 | impl gimli::UnwindContextStorage for StoreOnStack { 29 | type Rules = [(Register, RegisterRule); next_value(MAX_REG_RULES)]; 30 | type Stack = [UnwindTableRow; 2]; 31 | } 32 | 33 | #[cfg(feature = "dwarf-expr")] 34 | impl gimli::EvaluationStorage for StoreOnStack { 35 | type Stack = [Value; 64]; 36 | type ExpressionStack = [(R, R); 0]; 37 | type Result = [gimli::Piece; 1]; 38 | } 39 | 40 | #[derive(Debug)] 41 | pub struct Frame { 42 | fde_result: FDESearchResult, 43 | row: UnwindTableRow, 44 | } 45 | 46 | impl Frame { 47 | pub fn from_context(ctx: &Context, signal: bool) -> Result, gimli::Error> { 48 | let mut ra = ctx[Arch::RA]; 49 | 50 | // Reached end of stack 51 | if ra == 0 { 52 | return Ok(None); 53 | } 54 | 55 | // RA points to the *next* instruction, so move it back 1 byte for the call instruction. 56 | if !signal { 57 | ra -= 1; 58 | } 59 | 60 | let fde_result = match find_fde::get_finder().find_fde(ra as _) { 61 | Some(v) => v, 62 | None => return Ok(None), 63 | }; 64 | let mut unwinder = UnwindContext::<_, StoreOnStack>::new_in(); 65 | let row = fde_result 66 | .fde 67 | .unwind_info_for_address( 68 | &fde_result.eh_frame, 69 | &fde_result.bases, 70 | &mut unwinder, 71 | ra as _, 72 | )? 73 | .clone(); 74 | 75 | Ok(Some(Self { fde_result, row })) 76 | } 77 | 78 | #[cfg(feature = "dwarf-expr")] 79 | fn evaluate_expression( 80 | &self, 81 | ctx: &Context, 82 | expr: UnwindExpression, 83 | ) -> Result { 84 | let expr = expr.get(&self.fde_result.eh_frame).unwrap(); 85 | let mut eval = 86 | Evaluation::<_, StoreOnStack>::new_in(expr.0, self.fde_result.fde.cie().encoding()); 87 | let mut result = eval.evaluate()?; 88 | loop { 89 | match result { 90 | EvaluationResult::Complete => break, 91 | EvaluationResult::RequiresMemory { address, .. } => { 92 | let value = unsafe { (address as usize as *const usize).read_unaligned() }; 93 | result = eval.resume_with_memory(Value::Generic(value as _))?; 94 | } 95 | EvaluationResult::RequiresRegister { register, .. } => { 96 | let value = ctx[register]; 97 | result = eval.resume_with_register(Value::Generic(value as _))?; 98 | } 99 | EvaluationResult::RequiresRelocatedAddress(address) => { 100 | let value = unsafe { (address as usize as *const usize).read_unaligned() }; 101 | result = eval.resume_with_memory(Value::Generic(value as _))?; 102 | } 103 | _ => unreachable!(), 104 | } 105 | } 106 | 107 | Ok( 108 | match eval 109 | .as_result() 110 | .last() 111 | .ok_or(gimli::Error::PopWithEmptyStack)? 112 | .location 113 | { 114 | Location::Address { address } => address as usize, 115 | _ => unreachable!(), 116 | }, 117 | ) 118 | } 119 | 120 | #[cfg(not(feature = "dwarf-expr"))] 121 | fn evaluate_expression( 122 | &self, 123 | _ctx: &Context, 124 | _expr: UnwindExpression, 125 | ) -> Result { 126 | Err(gimli::Error::UnsupportedEvaluation) 127 | } 128 | 129 | pub fn adjust_stack_for_args(&self, ctx: &mut Context) { 130 | let size = self.row.saved_args_size(); 131 | ctx[Arch::SP] = ctx[Arch::SP].wrapping_add(size as usize); 132 | } 133 | 134 | pub fn unwind(&self, ctx: &Context) -> Result { 135 | let row = &self.row; 136 | let mut new_ctx = ctx.clone(); 137 | 138 | let cfa = match *row.cfa() { 139 | CfaRule::RegisterAndOffset { register, offset } => { 140 | ctx[register].wrapping_add(offset as usize) 141 | } 142 | CfaRule::Expression(expr) => self.evaluate_expression(ctx, expr)?, 143 | }; 144 | 145 | new_ctx[Arch::SP] = cfa as _; 146 | new_ctx[Arch::RA] = 0; 147 | 148 | #[warn(non_exhaustive_omitted_patterns)] 149 | for (reg, rule) in row.registers() { 150 | let value = match *rule { 151 | RegisterRule::Undefined | RegisterRule::SameValue => ctx[*reg], 152 | RegisterRule::Offset(offset) => unsafe { 153 | *((cfa.wrapping_add(offset as usize)) as *const usize) 154 | }, 155 | RegisterRule::ValOffset(offset) => cfa.wrapping_add(offset as usize), 156 | RegisterRule::Register(r) => ctx[r], 157 | RegisterRule::Expression(expr) => { 158 | let addr = self.evaluate_expression(ctx, expr)?; 159 | unsafe { *(addr as *const usize) } 160 | } 161 | RegisterRule::ValExpression(expr) => self.evaluate_expression(ctx, expr)?, 162 | RegisterRule::Architectural => unreachable!(), 163 | RegisterRule::Constant(value) => value as usize, 164 | _ => unreachable!(), 165 | }; 166 | new_ctx[*reg] = value; 167 | } 168 | 169 | Ok(new_ctx) 170 | } 171 | 172 | pub fn bases(&self) -> &BaseAddresses { 173 | &self.fde_result.bases 174 | } 175 | 176 | pub fn personality(&self) -> Option { 177 | self.fde_result 178 | .fde 179 | .personality() 180 | .map(|x| unsafe { deref_pointer(x) }) 181 | .map(|x| unsafe { core::mem::transmute(x) }) 182 | } 183 | 184 | pub fn lsda(&self) -> usize { 185 | self.fde_result 186 | .fde 187 | .lsda() 188 | .map(|x| unsafe { deref_pointer(x) }) 189 | .unwrap_or(0) 190 | } 191 | 192 | pub fn initial_address(&self) -> usize { 193 | self.fde_result.fde.initial_address() as _ 194 | } 195 | 196 | pub fn is_signal_trampoline(&self) -> bool { 197 | self.fde_result.fde.is_signal_trampoline() 198 | } 199 | } 200 | -------------------------------------------------------------------------------- /src/unwinder/mod.rs: -------------------------------------------------------------------------------- 1 | mod arch; 2 | mod find_fde; 3 | mod frame; 4 | 5 | use core::ffi::c_void; 6 | use core::ptr; 7 | use gimli::Register; 8 | 9 | use crate::abi::*; 10 | use crate::arch::*; 11 | use crate::util::*; 12 | use arch::*; 13 | use find_fde::FDEFinder; 14 | use frame::Frame; 15 | 16 | #[cfg(feature = "fde-custom")] 17 | pub use find_fde::custom_eh_frame_finder; 18 | 19 | // Helper function to turn `save_context` which takes function pointer to a closure-taking function. 20 | fn with_context T>(f: F) -> T { 21 | use core::mem::ManuallyDrop; 22 | 23 | union Data { 24 | f: ManuallyDrop, 25 | t: ManuallyDrop, 26 | } 27 | 28 | extern "C" fn delegate T>(ctx: &mut Context, ptr: *mut ()) { 29 | // SAFETY: This function is called exactly once; it extracts the function, call it and 30 | // store the return value. This function is `extern "C"` so we don't need to worry about 31 | // unwinding past it. 32 | unsafe { 33 | let data = &mut *ptr.cast::>(); 34 | let t = ManuallyDrop::take(&mut data.f)(ctx); 35 | data.t = ManuallyDrop::new(t); 36 | } 37 | } 38 | 39 | let mut data = Data { 40 | f: ManuallyDrop::new(f), 41 | }; 42 | save_context(delegate::, ptr::addr_of_mut!(data).cast()); 43 | unsafe { ManuallyDrop::into_inner(data.t) } 44 | } 45 | 46 | #[repr(C)] 47 | pub struct UnwindException { 48 | pub exception_class: u64, 49 | pub exception_cleanup: Option, 50 | private_1: Option, 51 | private_2: usize, 52 | private_unused: [usize; Arch::UNWIND_PRIVATE_DATA_SIZE - 2], 53 | } 54 | 55 | pub struct UnwindContext<'a> { 56 | frame: Option<&'a Frame>, 57 | ctx: &'a mut Context, 58 | signal: bool, 59 | } 60 | 61 | #[unsafe(no_mangle)] 62 | pub extern "C" fn _Unwind_GetGR(unwind_ctx: &UnwindContext<'_>, index: c_int) -> usize { 63 | unwind_ctx.ctx[Register(index as u16)] 64 | } 65 | 66 | #[unsafe(no_mangle)] 67 | pub extern "C" fn _Unwind_GetCFA(unwind_ctx: &UnwindContext<'_>) -> usize { 68 | unwind_ctx.ctx[Arch::SP] 69 | } 70 | 71 | #[unsafe(no_mangle)] 72 | pub extern "C" fn _Unwind_SetGR(unwind_ctx: &mut UnwindContext<'_>, index: c_int, value: usize) { 73 | unwind_ctx.ctx[Register(index as u16)] = value; 74 | } 75 | 76 | #[unsafe(no_mangle)] 77 | pub extern "C" fn _Unwind_GetIP(unwind_ctx: &UnwindContext<'_>) -> usize { 78 | unwind_ctx.ctx[Arch::RA] 79 | } 80 | 81 | #[unsafe(no_mangle)] 82 | pub extern "C" fn _Unwind_GetIPInfo( 83 | unwind_ctx: &UnwindContext<'_>, 84 | ip_before_insn: &mut c_int, 85 | ) -> usize { 86 | *ip_before_insn = unwind_ctx.signal as _; 87 | unwind_ctx.ctx[Arch::RA] 88 | } 89 | 90 | #[unsafe(no_mangle)] 91 | pub extern "C" fn _Unwind_SetIP(unwind_ctx: &mut UnwindContext<'_>, value: usize) { 92 | unwind_ctx.ctx[Arch::RA] = value; 93 | } 94 | 95 | #[unsafe(no_mangle)] 96 | pub extern "C" fn _Unwind_GetLanguageSpecificData(unwind_ctx: &UnwindContext<'_>) -> *mut c_void { 97 | unwind_ctx 98 | .frame 99 | .map(|f| f.lsda() as *mut c_void) 100 | .unwrap_or(ptr::null_mut()) 101 | } 102 | 103 | #[unsafe(no_mangle)] 104 | pub extern "C" fn _Unwind_GetRegionStart(unwind_ctx: &UnwindContext<'_>) -> usize { 105 | unwind_ctx.frame.map(|f| f.initial_address()).unwrap_or(0) 106 | } 107 | 108 | #[unsafe(no_mangle)] 109 | pub extern "C" fn _Unwind_GetTextRelBase(unwind_ctx: &UnwindContext<'_>) -> usize { 110 | unwind_ctx 111 | .frame 112 | .map(|f| f.bases().eh_frame.text.unwrap() as _) 113 | .unwrap_or(0) 114 | } 115 | 116 | #[unsafe(no_mangle)] 117 | pub extern "C" fn _Unwind_GetDataRelBase(unwind_ctx: &UnwindContext<'_>) -> usize { 118 | unwind_ctx 119 | .frame 120 | .map(|f| f.bases().eh_frame.data.unwrap() as _) 121 | .unwrap_or(0) 122 | } 123 | 124 | #[unsafe(no_mangle)] 125 | pub extern "C" fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void { 126 | find_fde::get_finder() 127 | .find_fde(pc as usize - 1) 128 | .map(|r| r.fde.initial_address() as usize as _) 129 | .unwrap_or(ptr::null_mut()) 130 | } 131 | 132 | macro_rules! try1 { 133 | ($e: expr) => {{ 134 | match $e { 135 | Ok(v) => v, 136 | Err(_) => return UnwindReasonCode::FATAL_PHASE1_ERROR, 137 | } 138 | }}; 139 | } 140 | 141 | macro_rules! try2 { 142 | ($e: expr) => {{ 143 | match $e { 144 | Ok(v) => v, 145 | Err(_) => return UnwindReasonCode::FATAL_PHASE2_ERROR, 146 | } 147 | }}; 148 | } 149 | 150 | #[inline(never)] 151 | #[unsafe(no_mangle)] 152 | pub unsafe extern "C-unwind" fn _Unwind_RaiseException( 153 | exception: *mut UnwindException, 154 | ) -> UnwindReasonCode { 155 | with_context(|saved_ctx| { 156 | // Phase 1: Search for handler 157 | let mut ctx = saved_ctx.clone(); 158 | let mut signal = false; 159 | loop { 160 | if let Some(frame) = try1!(Frame::from_context(&ctx, signal)) { 161 | if let Some(personality) = frame.personality() { 162 | let result = unsafe { 163 | personality( 164 | 1, 165 | UnwindAction::SEARCH_PHASE, 166 | (*exception).exception_class, 167 | exception, 168 | &mut UnwindContext { 169 | frame: Some(&frame), 170 | ctx: &mut ctx, 171 | signal, 172 | }, 173 | ) 174 | }; 175 | 176 | match result { 177 | UnwindReasonCode::CONTINUE_UNWIND => (), 178 | UnwindReasonCode::HANDLER_FOUND => { 179 | break; 180 | } 181 | _ => return UnwindReasonCode::FATAL_PHASE1_ERROR, 182 | } 183 | } 184 | 185 | ctx = try1!(frame.unwind(&ctx)); 186 | signal = frame.is_signal_trampoline(); 187 | } else { 188 | return UnwindReasonCode::END_OF_STACK; 189 | } 190 | } 191 | 192 | // Disambiguate normal frame and signal frame. 193 | let handler_cfa = ctx[Arch::SP] - signal as usize; 194 | unsafe { 195 | (*exception).private_1 = None; 196 | (*exception).private_2 = handler_cfa; 197 | } 198 | 199 | let code = raise_exception_phase2(exception, saved_ctx, handler_cfa); 200 | match code { 201 | UnwindReasonCode::INSTALL_CONTEXT => unsafe { restore_context(saved_ctx) }, 202 | _ => code, 203 | } 204 | }) 205 | } 206 | 207 | fn raise_exception_phase2( 208 | exception: *mut UnwindException, 209 | ctx: &mut Context, 210 | handler_cfa: usize, 211 | ) -> UnwindReasonCode { 212 | let mut signal = false; 213 | loop { 214 | if let Some(frame) = try2!(Frame::from_context(ctx, signal)) { 215 | let frame_cfa = ctx[Arch::SP] - signal as usize; 216 | if let Some(personality) = frame.personality() { 217 | let code = unsafe { 218 | personality( 219 | 1, 220 | UnwindAction::CLEANUP_PHASE 221 | | if frame_cfa == handler_cfa { 222 | UnwindAction::HANDLER_FRAME 223 | } else { 224 | UnwindAction::empty() 225 | }, 226 | (*exception).exception_class, 227 | exception, 228 | &mut UnwindContext { 229 | frame: Some(&frame), 230 | ctx, 231 | signal, 232 | }, 233 | ) 234 | }; 235 | 236 | match code { 237 | UnwindReasonCode::CONTINUE_UNWIND => (), 238 | UnwindReasonCode::INSTALL_CONTEXT => { 239 | frame.adjust_stack_for_args(ctx); 240 | return UnwindReasonCode::INSTALL_CONTEXT; 241 | } 242 | _ => return UnwindReasonCode::FATAL_PHASE2_ERROR, 243 | } 244 | } 245 | 246 | *ctx = try2!(frame.unwind(ctx)); 247 | signal = frame.is_signal_trampoline(); 248 | } else { 249 | return UnwindReasonCode::FATAL_PHASE2_ERROR; 250 | } 251 | } 252 | } 253 | 254 | #[inline(never)] 255 | #[unsafe(no_mangle)] 256 | pub unsafe extern "C-unwind" fn _Unwind_ForcedUnwind( 257 | exception: *mut UnwindException, 258 | stop: UnwindStopFn, 259 | stop_arg: *mut c_void, 260 | ) -> UnwindReasonCode { 261 | with_context(|ctx| { 262 | unsafe { 263 | (*exception).private_1 = Some(stop); 264 | (*exception).private_2 = stop_arg as _; 265 | } 266 | 267 | let code = force_unwind_phase2(exception, ctx, stop, stop_arg); 268 | match code { 269 | UnwindReasonCode::INSTALL_CONTEXT => unsafe { restore_context(ctx) }, 270 | _ => code, 271 | } 272 | }) 273 | } 274 | 275 | fn force_unwind_phase2( 276 | exception: *mut UnwindException, 277 | ctx: &mut Context, 278 | stop: UnwindStopFn, 279 | stop_arg: *mut c_void, 280 | ) -> UnwindReasonCode { 281 | let mut signal = false; 282 | loop { 283 | let frame = try2!(Frame::from_context(ctx, signal)); 284 | 285 | let code = unsafe { 286 | stop( 287 | 1, 288 | UnwindAction::FORCE_UNWIND 289 | | UnwindAction::END_OF_STACK 290 | | if frame.is_none() { 291 | UnwindAction::END_OF_STACK 292 | } else { 293 | UnwindAction::empty() 294 | }, 295 | (*exception).exception_class, 296 | exception, 297 | &mut UnwindContext { 298 | frame: frame.as_ref(), 299 | ctx, 300 | signal, 301 | }, 302 | stop_arg, 303 | ) 304 | }; 305 | match code { 306 | UnwindReasonCode::NO_REASON => (), 307 | _ => return UnwindReasonCode::FATAL_PHASE2_ERROR, 308 | } 309 | 310 | if let Some(frame) = frame { 311 | if let Some(personality) = frame.personality() { 312 | let code = unsafe { 313 | personality( 314 | 1, 315 | UnwindAction::FORCE_UNWIND | UnwindAction::CLEANUP_PHASE, 316 | (*exception).exception_class, 317 | exception, 318 | &mut UnwindContext { 319 | frame: Some(&frame), 320 | ctx, 321 | signal, 322 | }, 323 | ) 324 | }; 325 | 326 | match code { 327 | UnwindReasonCode::CONTINUE_UNWIND => (), 328 | UnwindReasonCode::INSTALL_CONTEXT => { 329 | frame.adjust_stack_for_args(ctx); 330 | return UnwindReasonCode::INSTALL_CONTEXT; 331 | } 332 | _ => return UnwindReasonCode::FATAL_PHASE2_ERROR, 333 | } 334 | } 335 | 336 | *ctx = try2!(frame.unwind(ctx)); 337 | signal = frame.is_signal_trampoline(); 338 | } else { 339 | return UnwindReasonCode::END_OF_STACK; 340 | } 341 | } 342 | } 343 | 344 | #[inline(never)] 345 | #[unsafe(no_mangle)] 346 | pub unsafe extern "C-unwind" fn _Unwind_Resume(exception: *mut UnwindException) -> ! { 347 | with_context(|ctx| { 348 | let code = match unsafe { (*exception).private_1 } { 349 | None => { 350 | let handler_cfa = unsafe { (*exception).private_2 }; 351 | raise_exception_phase2(exception, ctx, handler_cfa) 352 | } 353 | Some(stop) => { 354 | let stop_arg = unsafe { (*exception).private_2 as _ }; 355 | force_unwind_phase2(exception, ctx, stop, stop_arg) 356 | } 357 | }; 358 | assert!(code == UnwindReasonCode::INSTALL_CONTEXT); 359 | 360 | unsafe { restore_context(ctx) } 361 | }) 362 | } 363 | 364 | #[inline(never)] 365 | #[unsafe(no_mangle)] 366 | pub unsafe extern "C-unwind" fn _Unwind_Resume_or_Rethrow( 367 | exception: *mut UnwindException, 368 | ) -> UnwindReasonCode { 369 | let stop = match unsafe { (*exception).private_1 } { 370 | None => return unsafe { _Unwind_RaiseException(exception) }, 371 | Some(v) => v, 372 | }; 373 | 374 | with_context(|ctx| { 375 | let stop_arg = unsafe { (*exception).private_2 as _ }; 376 | let code = force_unwind_phase2(exception, ctx, stop, stop_arg); 377 | assert!(code == UnwindReasonCode::INSTALL_CONTEXT); 378 | 379 | unsafe { restore_context(ctx) } 380 | }) 381 | } 382 | 383 | #[unsafe(no_mangle)] 384 | pub unsafe extern "C" fn _Unwind_DeleteException(exception: *mut UnwindException) { 385 | if let Some(cleanup) = unsafe { (*exception).exception_cleanup } { 386 | unsafe { cleanup(UnwindReasonCode::FOREIGN_EXCEPTION_CAUGHT, exception) }; 387 | } 388 | } 389 | 390 | #[inline(never)] 391 | #[unsafe(no_mangle)] 392 | pub extern "C-unwind" fn _Unwind_Backtrace( 393 | trace: UnwindTraceFn, 394 | trace_argument: *mut c_void, 395 | ) -> UnwindReasonCode { 396 | with_context(|ctx| { 397 | let mut ctx = ctx.clone(); 398 | let mut signal = false; 399 | let mut skipping = cfg!(feature = "hide-trace"); 400 | 401 | loop { 402 | let frame = try1!(Frame::from_context(&ctx, signal)); 403 | if !skipping { 404 | let code = trace( 405 | &UnwindContext { 406 | frame: frame.as_ref(), 407 | ctx: &mut ctx, 408 | signal, 409 | }, 410 | trace_argument, 411 | ); 412 | match code { 413 | UnwindReasonCode::NO_REASON => (), 414 | _ => return UnwindReasonCode::FATAL_PHASE1_ERROR, 415 | } 416 | } 417 | if let Some(frame) = frame { 418 | if skipping { 419 | if frame.initial_address() == _Unwind_Backtrace as usize { 420 | skipping = false; 421 | } 422 | } 423 | ctx = try1!(frame.unwind(&ctx)); 424 | signal = frame.is_signal_trampoline(); 425 | } else { 426 | return UnwindReasonCode::END_OF_STACK; 427 | } 428 | } 429 | }) 430 | } 431 | -------------------------------------------------------------------------------- /src/util.rs: -------------------------------------------------------------------------------- 1 | use gimli::{EndianSlice, NativeEndian, Pointer}; 2 | 3 | pub type StaticSlice = EndianSlice<'static, NativeEndian>; 4 | 5 | pub unsafe fn get_unlimited_slice<'a>(start: *const u8) -> &'a [u8] { 6 | // Create the largest possible slice for this address. 7 | let start = start as usize; 8 | let end = start.saturating_add(isize::MAX as _); 9 | let len = end - start; 10 | unsafe { core::slice::from_raw_parts(start as *const _, len) } 11 | } 12 | 13 | pub unsafe fn deref_pointer(ptr: Pointer) -> usize { 14 | match ptr { 15 | Pointer::Direct(x) => x as _, 16 | Pointer::Indirect(x) => unsafe { *(x as *const _) }, 17 | } 18 | } 19 | 20 | #[cfg(feature = "libc")] 21 | pub use libc::c_int; 22 | 23 | #[cfg(not(feature = "libc"))] 24 | #[allow(non_camel_case_types)] 25 | pub type c_int = i32; 26 | 27 | #[cfg(all( 28 | any(feature = "panic", feature = "panic-handler-dummy"), 29 | feature = "libc" 30 | ))] 31 | pub fn abort() -> ! { 32 | unsafe { libc::abort() }; 33 | } 34 | 35 | #[cfg(all( 36 | any(feature = "panic", feature = "panic-handler-dummy"), 37 | not(feature = "libc") 38 | ))] 39 | pub fn abort() -> ! { 40 | core::intrinsics::abort(); 41 | } 42 | -------------------------------------------------------------------------------- /test_crates/catch_std_exception/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "catch_std_exception" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | [dependencies] 7 | unwinding = { path = "../../", features = ["panic"] } 8 | libc = "0.2" 9 | -------------------------------------------------------------------------------- /test_crates/catch_std_exception/check.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -o pipefail 3 | trap "rm -f run.log" EXIT 4 | ${CARGO:-cargo} run --release $BUILD_STD 2>&1 | tee run.log 5 | if [ $? -ne 134 ]; then 6 | echo process is not aborted 7 | exit 1 8 | fi 9 | grep -Pz 'panicked at test_crates/catch_std_exception/src/main.rs:5:9:\nexplicit panic\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace' run.log 10 | -------------------------------------------------------------------------------- /test_crates/catch_std_exception/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate unwinding; 2 | 3 | fn main() { 4 | let _ = unwinding::panic::catch_unwind(|| { 5 | panic!(); 6 | }); 7 | } 8 | -------------------------------------------------------------------------------- /test_crates/panic_abort_no_debuginfo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "panic_abort_no_debuginfo" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | [dependencies] 7 | unwinding = { path = "../../", features = ["panic"] } 8 | -------------------------------------------------------------------------------- /test_crates/panic_abort_no_debuginfo/check.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -o pipefail 3 | 4 | # Skip the test if need -Zbuild-std, it somehow doesn't work. 5 | if [ -n "$BUILD_STD" ]; then 6 | exit 0 7 | fi 8 | 9 | export CARGO_TARGET_DIR=$(mktemp -d) 10 | trap "rm -rf $CARGO_TARGET_DIR" EXIT 11 | RUSTFLAGS="-Cpanic=abort -Cdebuginfo=0" ${CARGO:-cargo} build --release $BUILD_STD 2>&1 12 | -------------------------------------------------------------------------------- /test_crates/panic_abort_no_debuginfo/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate unwinding; 2 | 3 | fn main() {} 4 | -------------------------------------------------------------------------------- /test_crates/std_catch_exception/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "std_catch_exception" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | [dependencies] 7 | unwinding = { path = "../../", features = ["panic"] } 8 | libc = "0.2" 9 | -------------------------------------------------------------------------------- /test_crates/std_catch_exception/check.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -o pipefail 3 | trap "rm -f run.log" EXIT 4 | ${CARGO:-cargo} run --release $BUILD_STD 2>&1 | tee run.log 5 | if [ $? -ne 134 ]; then 6 | echo process is not aborted 7 | exit 1 8 | fi 9 | grep -Pz 'fatal runtime error: Rust cannot catch foreign exceptions' run.log 10 | -------------------------------------------------------------------------------- /test_crates/std_catch_exception/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate unwinding; 2 | 3 | fn main() { 4 | let _ = std::panic::catch_unwind(|| { 5 | unwinding::panic::begin_panic(Box::new("test")); 6 | }); 7 | } 8 | -------------------------------------------------------------------------------- /test_crates/throw_and_catch/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "throw_and_catch" 3 | version = "0.1.0" 4 | edition = "2024" 5 | 6 | [dependencies] 7 | unwinding = { path = "../..", features = ["system-alloc", "personality", "panic-handler"] } 8 | libc = "0.2" 9 | -------------------------------------------------------------------------------- /test_crates/throw_and_catch/check.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -o pipefail 3 | trap "rm -f run.log" EXIT 4 | ${CARGO:-cargo} run --release $BUILD_STD 2>&1 | tee run.log 5 | if [ $? -ne 134 ]; then 6 | echo process is not aborted 7 | exit 1 8 | fi 9 | grep -Pz 'panicked at test_crates/throw_and_catch/src/main.rs:36:5:\npanic\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\ndropped: "string"\ncaught\npanicked at test_crates/throw_and_catch/src/main.rs:46:5:\npanic\npanicked at test_crates/throw_and_catch/src/main.rs:25:9:\npanic on drop\n( *\d+:.*\n)+thread panicked while processing panic\. aborting\.' run.log 10 | 11 | -------------------------------------------------------------------------------- /test_crates/throw_and_catch/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | extern crate alloc; 5 | extern crate unwinding; 6 | 7 | use alloc::{borrow::ToOwned, string::String}; 8 | use unwinding::print::*; 9 | 10 | #[link(name = "c")] 11 | unsafe extern "C" {} 12 | 13 | struct PrintOnDrop(String); 14 | 15 | impl Drop for PrintOnDrop { 16 | fn drop(&mut self) { 17 | eprintln!("dropped: {:?}", self.0); 18 | } 19 | } 20 | 21 | struct PanicOnDrop; 22 | 23 | impl Drop for PanicOnDrop { 24 | fn drop(&mut self) { 25 | panic!("panic on drop"); 26 | } 27 | } 28 | 29 | #[track_caller] 30 | fn foo() { 31 | panic!("panic"); 32 | } 33 | 34 | fn bar() { 35 | let _p = PrintOnDrop("string".to_owned()); 36 | foo() 37 | } 38 | 39 | fn main() { 40 | let _ = unwinding::panic::catch_unwind(|| { 41 | bar(); 42 | eprintln!("done"); 43 | }); 44 | eprintln!("caught"); 45 | let _p = PanicOnDrop; 46 | foo(); 47 | } 48 | 49 | #[unsafe(export_name = "main")] 50 | extern "C" fn start(_argc: isize, _argv: *const *const u8) -> isize { 51 | unwinding::panic::catch_unwind(|| { 52 | main(); 53 | 0 54 | }) 55 | .unwrap_or(101) 56 | } 57 | -------------------------------------------------------------------------------- /tests/compile_tests.rs: -------------------------------------------------------------------------------- 1 | use std::process::Command; 2 | 3 | #[test] 4 | fn main() { 5 | let dir = env!("CARGO_MANIFEST_DIR"); 6 | 7 | let tests = [ 8 | "throw_and_catch", 9 | "catch_std_exception", 10 | "std_catch_exception", 11 | "panic_abort_no_debuginfo", 12 | ]; 13 | 14 | for test in tests { 15 | let status = Command::new("./check.sh") 16 | .current_dir(format!("{dir}/test_crates/{test}")) 17 | .status() 18 | .unwrap(); 19 | assert!(status.success()); 20 | } 21 | } 22 | --------------------------------------------------------------------------------