Debian Codegraph @

README.md

raw symbols SCIP
1 # cee-scape
2
3 The cee-scape crate provides Rust access to `setjmp` and `sigsetjmp`
4 functionality, via an interface that ensures LLVM won't miscompile things.
5
6 ## Example
7
8 You might imagine you have some C code that looks like this (vastly over simplified):
9
10 ```c
11 #include <setjmp.h>
12 #include <stdint.h>
13
14 uint32_t subtract_but_longjmp_if_underflow(jmp_buf env, uint32_t a, uint32_t b) {
15 if (b > a) {
16 longjmp(env, b - a);
17 }
18 return a - b;
19 }
20 ```
21
22 If you want to call out to that C code from Rust, you need to establish a jump
23 environment (the `jmp_buf env` parameter); but Rust does not provide `setjmp`.
24
25 Here's how you might use this crate to solve your problem:
26
27 ```rust
28 use cee_scape::call_with_setjmp;
29
30 // This invocation passes parameters that follow normal control flow.
31 assert_eq!(call_with_setjmp(|env| {
32 (unsafe {
33 subtract_but_longjmp_if_underflow(env, 10, 3)
34 }) as c_int
35 }), 7);
36
37 // This invocation passes parameters that cause a non-local jump.
38 assert_eq!(call_with_setjmp(|env| {
39 unsafe {
40 subtract_but_longjmp_if_underflow(env, 3, 10);
41 panic!("should never get here.");
42 }
43 }), 7);
44 ```
45
46 ## Why not just add `setjmp` itself as a function?
47
48 There has been significant discussion of that question amongst the Rust project.
49
50 See in particular [rust-lang/libc PR 1216] and [rust-lang/rfcs Issue 2625].
51
52 [rust-lang/libc PR 1216]: https://github.com/rust-lang/libc/pull/1216
53 [rust-lang/rfcs Issue 2625]: https://github.com/rust-lang/rfcs/issues/2625
54
55 The answer is multifaceted.
56
57 First, adding support for calling `setjmp` would require adding extra support in
58 the Rust compiler. Namely, support for marking functions as potentially
59 returning multiple times, e.g. via a `returns_twice` attribute as discussed in
60 [rust-lang/rfcs Issue 2625][].
61
62 Second, calls to the `setjmp` function would interact in strange ways with the
63 Rust borrow checker; unless the borrow checker were extended to understand the
64 meaning of a `returns_twice` attribute, it would invite immediate unsound
65 behavior, such as moving an owned object multiple times from the same stack
66 slot. This could cause the same value to be dropped multiple times (which would
67 be unsound).
68
69 Third, the `setjmp` function itself only has well-defined behavior in very
70 specific contexts according to the C standard (again discussed in
71 [rust-lang/rfcs Issue 2625]); even just `let x = setjmp(...)` would be undefined
72 behavior in Rust.
73
74 The methods offered by cee-scape, such as `call_with_setjmp`, side-step the above
75 issues by limiting the use of `setjmp` to a specific coding pattern:
76
77 ```rust
78 call_with_setjmp(|env| { ... })
79 ```
80
81 Within the dynamic extent of an invocation to `call_with_setjmp`, one can either
82 return normally, or via a `longjmp` to the given jump environment `env` (which
83 causes a returns from the `call_with_setjmp` invocation). Either way, the outer
84 call returns at most once. The given jump environment is only usable within that
85 dynamic extent (and Rust's lifetime rules help enforce that constraint).
86
87 ## Why is it called cee-scape
88
89 Its a pun: C's jump environments are also known as "escape continuations". This
90 crate enables C escapes.