diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/stacker | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/stacker')
-rw-r--r-- | vendor/stacker/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | vendor/stacker/Cargo.toml | 41 | ||||
-rw-r--r-- | vendor/stacker/Cross.toml | 2 | ||||
-rw-r--r-- | vendor/stacker/LICENSE-APACHE | 201 | ||||
-rw-r--r-- | vendor/stacker/LICENSE-MIT | 25 | ||||
-rw-r--r-- | vendor/stacker/README.md | 44 | ||||
-rw-r--r-- | vendor/stacker/build.rs | 13 | ||||
-rw-r--r-- | vendor/stacker/src/arch/asm.h | 5 | ||||
-rw-r--r-- | vendor/stacker/src/arch/windows.c | 5 | ||||
-rw-r--r-- | vendor/stacker/src/lib.rs | 457 | ||||
-rw-r--r-- | vendor/stacker/tests/simple.rs | 27 | ||||
-rw-r--r-- | vendor/stacker/tests/smoke.rs | 95 |
12 files changed, 916 insertions, 0 deletions
diff --git a/vendor/stacker/.cargo-checksum.json b/vendor/stacker/.cargo-checksum.json new file mode 100644 index 000000000..8ba471a9c --- /dev/null +++ b/vendor/stacker/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"2f4bee8a3114353b7123969903b52d6b7c04412a848bf607f784bc8cb56a3b80","Cross.toml":"b5f300c31f4522caba733582f4957f693a4017f4728a69805390b63ad69eff67","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"e938c263b76ff66671013f0f77755aa289e807d91b7d39471ba92dea6782929e","build.rs":"8a9274309128a737566386f2534a93c69d2991da19a4543113790bc819448a53","src/arch/asm.h":"4c4db945e854e4ce3f0b7ba8da3755613c0e4513a7aab0604bd6a67c0ff2192d","src/arch/windows.c":"e98e08f6b5102480b8fa4dfa7ee13441845202f5ba81c60b13da2800e0a8630c","src/lib.rs":"cee329550bad2ed1182abb71db9c24558a91b12e1eab8d9a48e23b997bb8f04e","tests/simple.rs":"18fbb3e891ab486c58aa011698f755945818db8799c564f2fa8cfe5ac8d8f0dc","tests/smoke.rs":"db4fd5b210123d9643aefd703be324d626d4b0f9398d7cae7f30871fba71f65b"},"package":"90939d5171a4420b3ff5fbc8954d641e7377335454c259dcb80786f3f21dc9b4"}
\ No newline at end of file diff --git a/vendor/stacker/Cargo.toml b/vendor/stacker/Cargo.toml new file mode 100644 index 000000000..f61a0315b --- /dev/null +++ b/vendor/stacker/Cargo.toml @@ -0,0 +1,41 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "stacker" +version = "0.1.14" +authors = ["Alex Crichton <alex@alexcrichton.com>", "Simonas Kazlauskas <git@kazlauskas.me>"] +build = "build.rs" +description = "A stack growth library useful when implementing deeply recursive algorithms that\nmay accidentally blow the stack.\n" +homepage = "https://github.com/rust-lang/stacker" +documentation = "https://docs.rs/stacker/0.1.14" +readme = "README.md" +license = "MIT/Apache-2.0" +repository = "https://github.com/rust-lang/stacker" + +[lib] +name = "stacker" +test = false +doctest = false +[dependencies.cfg-if] +version = "1.0.0" + +[dependencies.libc] +version = "0.2.45" + +[dependencies.psm] +version = "0.1.7" +[build-dependencies.cc] +version = "1.0.2" +[target."cfg(windows)".dependencies.winapi] +version = "0.3.6" +features = ["memoryapi", "winbase", "fibersapi", "processthreadsapi", "minwindef"] diff --git a/vendor/stacker/Cross.toml b/vendor/stacker/Cross.toml new file mode 100644 index 000000000..7e330ab97 --- /dev/null +++ b/vendor/stacker/Cross.toml @@ -0,0 +1,2 @@ +[target.x86_64-linux-android] +image = "rustembedded/cross:x86_64-linux-android" diff --git a/vendor/stacker/LICENSE-APACHE b/vendor/stacker/LICENSE-APACHE new file mode 100644 index 000000000..16fe87b06 --- /dev/null +++ b/vendor/stacker/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/vendor/stacker/LICENSE-MIT b/vendor/stacker/LICENSE-MIT new file mode 100644 index 000000000..39e0ed660 --- /dev/null +++ b/vendor/stacker/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2014 Alex Crichton + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/vendor/stacker/README.md b/vendor/stacker/README.md new file mode 100644 index 000000000..c8d39a6f6 --- /dev/null +++ b/vendor/stacker/README.md @@ -0,0 +1,44 @@ +# stacker + +[![Build Status](https://github.com/rust-lang/stacker/workflows/Test%20stacker/badge.svg)](https://github.com/rust-lang/stacker/actions) + +[Documentation](https://docs.rs/stacker) + +A stack-growth library for Rust. Enables annotating fixed points in programs +where the stack may want to grow larger. Spills over to the heap if the stack +has hit its limit. + +This library is intended on helping implement recursive algorithms. + +```toml +# Cargo.toml +[dependencies] +stacker = "0.1" +``` + +## Platform Support + +This library currently uses psm for its cross platform capabilities, with a notable exception of +Windows, which uses an implementation based on Fibers. See the README for psm for the support +table. + +On all unsupported platforms this library is a noop. It should compile and run, but it +won't actually grow the stack and code will continue to hit the guard pages +typically in place. + +# License + +This project is licensed under either of + + * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or + http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or + http://opensource.org/licenses/MIT) + +at your option. + +### Contribution + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in this project by you, as defined in the Apache-2.0 license, +shall be dual licensed as above, without any additional terms or conditions. diff --git a/vendor/stacker/build.rs b/vendor/stacker/build.rs new file mode 100644 index 000000000..abaefe586 --- /dev/null +++ b/vendor/stacker/build.rs @@ -0,0 +1,13 @@ +use std::env; + +fn main() { + let target = env::var("TARGET").unwrap(); + let mut cfg = cc::Build::new(); + if target.contains("windows") { + cfg.define("WINDOWS", None); + cfg.file("src/arch/windows.c"); + cfg.include("src/arch").compile("libstacker.a"); + } else { + return; + } +} diff --git a/vendor/stacker/src/arch/asm.h b/vendor/stacker/src/arch/asm.h new file mode 100644 index 000000000..56c9d2890 --- /dev/null +++ b/vendor/stacker/src/arch/asm.h @@ -0,0 +1,5 @@ +#if defined(APPLE) || (defined(WINDOWS) && defined(X86)) +#define GLOBAL(name) .globl _ ## name; _ ## name +#else +#define GLOBAL(name) .globl name; name +#endif diff --git a/vendor/stacker/src/arch/windows.c b/vendor/stacker/src/arch/windows.c new file mode 100644 index 000000000..89485a0cc --- /dev/null +++ b/vendor/stacker/src/arch/windows.c @@ -0,0 +1,5 @@ +#include <windows.h>
+
+PVOID __stacker_get_current_fiber() {
+ return GetCurrentFiber();
+}
diff --git a/vendor/stacker/src/lib.rs b/vendor/stacker/src/lib.rs new file mode 100644 index 000000000..3c1d8891e --- /dev/null +++ b/vendor/stacker/src/lib.rs @@ -0,0 +1,457 @@ +//! A library to help grow the stack when it runs out of space. +//! +//! This is an implementation of manually instrumented segmented stacks where points in a program's +//! control flow are annotated with "maybe grow the stack here". Each point of annotation indicates +//! how far away from the end of the stack it's allowed to be, plus the amount of stack to allocate +//! if it does reach the end. +//! +//! Once a program has reached the end of its stack, a temporary stack on the heap is allocated and +//! is switched to for the duration of a closure. +//! +//! For a set of lower-level primitives, consider the `psm` crate. +//! +//! # Examples +//! +//! ``` +//! // Grow the stack if we are within the "red zone" of 32K, and if we allocate +//! // a new stack allocate 1MB of stack space. +//! // +//! // If we're already in bounds, just run the provided closure on current stack. +//! stacker::maybe_grow(32 * 1024, 1024 * 1024, || { +//! // guaranteed to have at least 32K of stack +//! }); +//! ``` + +#![allow(improper_ctypes)] + +#[macro_use] +extern crate cfg_if; +extern crate libc; +#[cfg(windows)] +extern crate winapi; +#[macro_use] +extern crate psm; + +use std::cell::Cell; + +/// Grows the call stack if necessary. +/// +/// This function is intended to be called at manually instrumented points in a program where +/// recursion is known to happen quite a bit. This function will check to see if we're within +/// `red_zone` bytes of the end of the stack, and if so it will allocate a new stack of at least +/// `stack_size` bytes. +/// +/// The closure `f` is guaranteed to run on a stack with at least `red_zone` bytes, and it will be +/// run on the current stack if there's space available. +#[inline(always)] +pub fn maybe_grow<R, F: FnOnce() -> R>(red_zone: usize, stack_size: usize, callback: F) -> R { + // if we can't guess the remaining stack (unsupported on some platforms) we immediately grow + // the stack and then cache the new stack size (which we do know now because we allocated it. + let enough_space = match remaining_stack() { + Some(remaining) => remaining >= red_zone, + None => false, + }; + if enough_space { + callback() + } else { + grow(stack_size, callback) + } +} + +/// Always creates a new stack for the passed closure to run on. +/// The closure will still be on the same thread as the caller of `grow`. +/// This will allocate a new stack with at least `stack_size` bytes. +pub fn grow<R, F: FnOnce() -> R>(stack_size: usize, callback: F) -> R { + // To avoid monomorphizing `_grow()` and everything it calls, + // we convert the generic callback to a dynamic one. + let mut opt_callback = Some(callback); + let mut ret = None; + let ret_ref = &mut ret; + + // This wrapper around `callback` achieves two things: + // * It converts the `impl FnOnce` to a `dyn FnMut`. + // `dyn` because we want it to not be generic, and + // `FnMut` because we can't pass a `dyn FnOnce` around without boxing it. + // * It eliminates the generic return value, by writing it to the stack of this function. + // Otherwise the closure would have to return an unsized value, which isn't possible. + let dyn_callback: &mut dyn FnMut() = &mut || { + let taken_callback = opt_callback.take().unwrap(); + *ret_ref = Some(taken_callback()); + }; + + _grow(stack_size, dyn_callback); + ret.unwrap() +} + +/// Queries the amount of remaining stack as interpreted by this library. +/// +/// This function will return the amount of stack space left which will be used +/// to determine whether a stack switch should be made or not. +pub fn remaining_stack() -> Option<usize> { + let current_ptr = current_stack_ptr(); + get_stack_limit().map(|limit| current_ptr - limit) +} + +psm_stack_information! ( + yes { + fn current_stack_ptr() -> usize { + psm::stack_pointer() as usize + } + } + no { + #[inline(always)] + fn current_stack_ptr() -> usize { + unsafe { + let mut x = std::mem::MaybeUninit::<u8>::uninit(); + // Unlikely to be ever exercised. As a fallback we execute a volatile read to a + // local (to hopefully defeat the optimisations that would make this local a static + // global) and take its address. This way we get a very approximate address of the + // current frame. + x.as_mut_ptr().write_volatile(42); + x.as_ptr() as usize + } + } + } +); + +thread_local! { + static STACK_LIMIT: Cell<Option<usize>> = Cell::new(unsafe { + guess_os_stack_limit() + }) +} + +#[inline(always)] +fn get_stack_limit() -> Option<usize> { + STACK_LIMIT.with(|s| s.get()) +} + +#[inline(always)] +#[allow(unused)] +fn set_stack_limit(l: Option<usize>) { + STACK_LIMIT.with(|s| s.set(l)) +} + +psm_stack_manipulation! { + yes { + struct StackRestoreGuard { + new_stack: *mut std::ffi::c_void, + stack_bytes: usize, + old_stack_limit: Option<usize>, + } + + impl StackRestoreGuard { + #[cfg(target_arch = "wasm32")] + unsafe fn new(stack_bytes: usize, _page_size: usize) -> StackRestoreGuard { + let layout = std::alloc::Layout::from_size_align(stack_bytes, 16).unwrap(); + let ptr = std::alloc::alloc(layout); + assert!(!ptr.is_null(), "unable to allocate stack"); + StackRestoreGuard { + new_stack: ptr as *mut _, + stack_bytes, + old_stack_limit: get_stack_limit(), + } + } + + #[cfg(not(target_arch = "wasm32"))] + unsafe fn new(stack_bytes: usize, page_size: usize) -> StackRestoreGuard { + let new_stack = libc::mmap( + std::ptr::null_mut(), + stack_bytes, + libc::PROT_NONE, + libc::MAP_PRIVATE | + libc::MAP_ANON, + -1, // Some implementations assert fd = -1 if MAP_ANON is specified + 0 + ); + if new_stack == libc::MAP_FAILED { + panic!("unable to allocate stack") + } + let guard = StackRestoreGuard { + new_stack, + stack_bytes, + old_stack_limit: get_stack_limit(), + }; + let above_guard_page = new_stack.add(page_size); + #[cfg(not(target_os = "openbsd"))] + let result = libc::mprotect( + above_guard_page, + stack_bytes - page_size, + libc::PROT_READ | libc::PROT_WRITE + ); + #[cfg(target_os = "openbsd")] + let result = if libc::mmap( + above_guard_page, + stack_bytes - page_size, + libc::PROT_READ | libc::PROT_WRITE, + libc::MAP_FIXED | libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_STACK, + -1, + 0) == above_guard_page { + 0 + } else { + -1 + }; + if result == -1 { + drop(guard); + panic!("unable to set stack permissions") + } + guard + } + } + + impl Drop for StackRestoreGuard { + fn drop(&mut self) { + #[cfg(target_arch = "wasm32")] + unsafe { + std::alloc::dealloc( + self.new_stack as *mut u8, + std::alloc::Layout::from_size_align_unchecked(self.stack_bytes, 16), + ); + } + #[cfg(not(target_arch = "wasm32"))] + unsafe { + // FIXME: check the error code and decide what to do with it. + // Perhaps a debug_assertion? + libc::munmap(self.new_stack, self.stack_bytes); + } + set_stack_limit(self.old_stack_limit); + } + } + + fn _grow(stack_size: usize, callback: &mut dyn FnMut()) { + // Calculate a number of pages we want to allocate for the new stack. + // For maximum portability we want to produce a stack that is aligned to a page and has + // a size that’s a multiple of page size. Furthermore we want to allocate two extras pages + // for the stack guard. To achieve that we do our calculations in number of pages and + // convert to bytes last. + let page_size = page_size(); + let requested_pages = stack_size + .checked_add(page_size - 1) + .expect("unreasonably large stack requested") / page_size; + let stack_pages = std::cmp::max(1, requested_pages) + 2; + let stack_bytes = stack_pages.checked_mul(page_size) + .expect("unreasonably large stack requesteed"); + + // Next, there are a couple of approaches to how we allocate the new stack. We take the + // most obvious path and use `mmap`. We also `mprotect` a guard page into our + // allocation. + // + // We use a guard pattern to ensure we deallocate the allocated stack when we leave + // this function and also try to uphold various safety invariants required by `psm` + // (such as not unwinding from the callback we pass to it). + // + // Other than that this code has no meaningful gotchas. + unsafe { + let guard = StackRestoreGuard::new(stack_bytes, page_size); + let above_guard_page = guard.new_stack.add(page_size); + set_stack_limit(Some(above_guard_page as usize)); + let panic = psm::on_stack(above_guard_page as *mut _, stack_size, move || { + std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback)).err() + }); + drop(guard); + if let Some(p) = panic { + std::panic::resume_unwind(p); + } + } + } + + fn page_size() -> usize { + // FIXME: consider caching the page size. + #[cfg(not(target_arch = "wasm32"))] + unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) as usize } + #[cfg(target_arch = "wasm32")] + { 65536 } + } + } + + no { + #[cfg(not(windows))] + fn _grow(stack_size: usize, callback: &mut dyn FnMut()) { + drop(stack_size); + callback(); + } + } +} + +cfg_if! { + if #[cfg(windows)] { + use std::ptr; + use std::io; + + use winapi::shared::basetsd::*; + use winapi::shared::minwindef::{LPVOID, BOOL}; + use winapi::shared::ntdef::*; + use winapi::um::fibersapi::*; + use winapi::um::memoryapi::*; + use winapi::um::processthreadsapi::*; + use winapi::um::winbase::*; + + // Make sure the libstacker.a (implemented in C) is linked. + // See https://github.com/rust-lang/rust/issues/65610 + #[link(name="stacker")] + extern { + fn __stacker_get_current_fiber() -> PVOID; + } + + struct FiberInfo<F> { + callback: std::mem::MaybeUninit<F>, + panic: Option<Box<dyn std::any::Any + Send + 'static>>, + parent_fiber: LPVOID, + } + + unsafe extern "system" fn fiber_proc<F: FnOnce()>(data: LPVOID) { + // This function is the entry point to our inner fiber, and as argument we get an + // instance of `FiberInfo`. We will set-up the "runtime" for the callback and execute + // it. + let data = &mut *(data as *mut FiberInfo<F>); + let old_stack_limit = get_stack_limit(); + set_stack_limit(guess_os_stack_limit()); + let callback = data.callback.as_ptr(); + data.panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback.read())).err(); + + // Restore to the previous Fiber + set_stack_limit(old_stack_limit); + SwitchToFiber(data.parent_fiber); + return; + } + + fn _grow(stack_size: usize, callback: &mut dyn FnMut()) { + // Fibers (or stackful coroutines) is the only official way to create new stacks on the + // same thread on Windows. So in order to extend the stack we create fiber and switch + // to it so we can use it's stack. After running `callback` within our fiber, we switch + // back to the current stack and destroy the fiber and its associated stack. + unsafe { + let was_fiber = IsThreadAFiber() == TRUE as BOOL; + let mut data = FiberInfo { + callback: std::mem::MaybeUninit::new(callback), + panic: None, + parent_fiber: { + if was_fiber { + // Get a handle to the current fiber. We need to use a C implementation + // for this as GetCurrentFiber is an header only function. + __stacker_get_current_fiber() + } else { + // Convert the current thread to a fiber, so we are able to switch back + // to the current stack. Threads coverted to fibers still act like + // regular threads, but they have associated fiber data. We later + // convert it back to a regular thread and free the fiber data. + ConvertThreadToFiber(ptr::null_mut()) + } + }, + }; + + if data.parent_fiber.is_null() { + panic!("unable to convert thread to fiber: {}", io::Error::last_os_error()); + } + + let fiber = CreateFiber( + stack_size as SIZE_T, + Some(fiber_proc::<&mut dyn FnMut()>), + &mut data as *mut FiberInfo<&mut dyn FnMut()> as *mut _, + ); + if fiber.is_null() { + panic!("unable to allocate fiber: {}", io::Error::last_os_error()); + } + + // Switch to the fiber we created. This changes stacks and starts executing + // fiber_proc on it. fiber_proc will run `callback` and then switch back to run the + // next statement. + SwitchToFiber(fiber); + DeleteFiber(fiber); + + // Clean-up. + if !was_fiber { + if ConvertFiberToThread() == 0 { + // FIXME: Perhaps should not panic here? + panic!("unable to convert back to thread: {}", io::Error::last_os_error()); + } + } + if let Some(p) = data.panic { + std::panic::resume_unwind(p); + } + } + } + + #[inline(always)] + fn get_thread_stack_guarantee() -> usize { + let min_guarantee = if cfg!(target_pointer_width = "32") { + 0x1000 + } else { + 0x2000 + }; + let mut stack_guarantee = 0; + unsafe { + // Read the current thread stack guarantee + // This is the stack reserved for stack overflow + // exception handling. + // This doesn't return the true value so we need + // some further logic to calculate the real stack + // guarantee. This logic is what is used on x86-32 and + // x86-64 Windows 10. Other versions and platforms may differ + SetThreadStackGuarantee(&mut stack_guarantee) + }; + std::cmp::max(stack_guarantee, min_guarantee) as usize + 0x1000 + } + + #[inline(always)] + unsafe fn guess_os_stack_limit() -> Option<usize> { + // Query the allocation which contains our stack pointer in order + // to discover the size of the stack + // + // FIXME: we could read stack base from the TIB, specifically the 3rd element of it. + type QueryT = winapi::um::winnt::MEMORY_BASIC_INFORMATION; + let mut mi = std::mem::MaybeUninit::<QueryT>::uninit(); + VirtualQuery( + psm::stack_pointer() as *const _, + mi.as_mut_ptr(), + std::mem::size_of::<QueryT>() as SIZE_T, + ); + Some(mi.assume_init().AllocationBase as usize + get_thread_stack_guarantee() + 0x1000) + } + } else if #[cfg(any(target_os = "linux", target_os="solaris", target_os = "netbsd"))] { + unsafe fn guess_os_stack_limit() -> Option<usize> { + let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit(); + assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0); + assert_eq!(libc::pthread_getattr_np(libc::pthread_self(), + attr.as_mut_ptr()), 0); + let mut stackaddr = std::ptr::null_mut(); + let mut stacksize = 0; + assert_eq!(libc::pthread_attr_getstack( + attr.as_ptr(), &mut stackaddr, &mut stacksize + ), 0); + assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0); + Some(stackaddr as usize) + } + } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] { + unsafe fn guess_os_stack_limit() -> Option<usize> { + let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit(); + assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0); + assert_eq!(libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr()), 0); + let mut stackaddr = std::ptr::null_mut(); + let mut stacksize = 0; + assert_eq!(libc::pthread_attr_getstack( + attr.as_ptr(), &mut stackaddr, &mut stacksize + ), 0); + assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0); + Some(stackaddr as usize) + } + } else if #[cfg(target_os = "openbsd")] { + unsafe fn guess_os_stack_limit() -> Option<usize> { + let mut stackinfo = std::mem::MaybeUninit::<libc::stack_t>::uninit(); + assert_eq!(libc::pthread_stackseg_np(libc::pthread_self(), stackinfo.as_mut_ptr()), 0); + Some(stackinfo.assume_init().ss_sp as usize - stackinfo.assume_init().ss_size) + } + } else if #[cfg(target_os = "macos")] { + unsafe fn guess_os_stack_limit() -> Option<usize> { + Some(libc::pthread_get_stackaddr_np(libc::pthread_self()) as usize - + libc::pthread_get_stacksize_np(libc::pthread_self()) as usize) + } + } else { + // fallback for other platforms is to always increase the stack if we're on + // the root stack. After we increased the stack once, we know the new stack + // size and don't need this pessimization anymore + #[inline(always)] + unsafe fn guess_os_stack_limit() -> Option<usize> { + None + } + } +} diff --git a/vendor/stacker/tests/simple.rs b/vendor/stacker/tests/simple.rs new file mode 100644 index 000000000..d4c7820d6 --- /dev/null +++ b/vendor/stacker/tests/simple.rs @@ -0,0 +1,27 @@ +extern crate stacker; + +const RED_ZONE: usize = 100 * 1024; // 100k +const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB + +pub fn ensure_sufficient_stack<R, F: FnOnce() -> R + std::panic::UnwindSafe>(f: F) -> R { + stacker::maybe_grow(RED_ZONE, STACK_PER_RECURSION, f) +} + +#[inline(never)] +fn recurse(n: usize) { + let x = [42u8; 50000]; + if n != 0 { + ensure_sufficient_stack(|| recurse(n - 1)); + } + drop(x); +} + +#[test] +fn foo() { + let limit = if cfg!(target_arch = "wasm32") { + 2000 + } else { + 10000 + }; + recurse(limit); +} diff --git a/vendor/stacker/tests/smoke.rs b/vendor/stacker/tests/smoke.rs new file mode 100644 index 000000000..2ec0de24d --- /dev/null +++ b/vendor/stacker/tests/smoke.rs @@ -0,0 +1,95 @@ +extern crate stacker; + +use std::sync::mpsc; +use std::thread; + +#[inline(never)] +fn __stacker_black_box(_: *const u8) {} + +#[test] +fn deep() { + fn foo(n: usize, s: &mut [u8]) { + __stacker_black_box(s.as_ptr()); + if n > 0 { + stacker::maybe_grow(64 * 1024, 1024 * 1024, || { + let mut s = [0u8; 1024]; + foo(n - 1, &mut s); + __stacker_black_box(s.as_ptr()); + }) + } else { + println!("bottom"); + } + } + + let limit = if cfg!(target_arch = "wasm32") { + 2000 + } else { + 256 * 1024 + }; + foo(limit, &mut []); +} + +#[test] +#[cfg_attr(target_arch = "wasm32", ignore)] +fn panic() { + fn foo(n: usize, s: &mut [u8]) { + __stacker_black_box(s.as_ptr()); + if n > 0 { + stacker::maybe_grow(64 * 1024, 1024 * 1024, || { + let mut s = [0u8; 1024]; + foo(n - 1, &mut s); + __stacker_black_box(s.as_ptr()); + }) + } else { + panic!("bottom"); + } + } + + let (tx, rx) = mpsc::channel::<()>(); + thread::spawn(move || { + foo(64 * 1024, &mut []); + drop(tx); + }) + .join() + .unwrap_err(); + + assert!(rx.recv().is_err()); +} + +fn recursive<F: FnOnce()>(n: usize, f: F) -> usize { + if n > 0 { + stacker::grow(64 * 1024, || recursive(n - 1, f) + 1) + } else { + f(); + 0 + } +} + +#[test] +#[cfg_attr(target_arch = "wasm32", ignore)] +fn catch_panic() { + let panic_result = std::panic::catch_unwind(|| { + recursive(100, || panic!()); + }); + assert!(panic_result.is_err()); +} + +#[test] +#[cfg_attr(target_arch = "wasm32", ignore)] +fn catch_panic_inside() { + stacker::grow(64 * 1024, || { + let panic_result = std::panic::catch_unwind(|| { + recursive(100, || panic!()); + }); + assert!(panic_result.is_err()); + }); +} + +#[test] +#[cfg_attr(target_arch = "wasm32", ignore)] +fn catch_panic_leaf() { + stacker::grow(64 * 1024, || { + let panic_result = std::panic::catch_unwind(|| panic!()); + assert!(panic_result.is_err()); + }); +} |