or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Syncing
xxxxxxxxxx
Unwinding by default on C FFI?
Premise
We have been acting under the assumption that calls to
extern "C"
functions should not permit unwinding. But this is not entirely clear.Presuming that we do the work to define how unwinding and foreign exceptions interact, we can then choose for one of two paths:
extern "C"
, unwinding is UB; withextern "C unwind"
, it is permittedextern "C"
, unwinding is permitted; withextern "C noexcept"
, it is UB(There are other variations, such as a
#[noexcept]
attribute to be placed on function items. However, in prior discussion we opted that ABIs were the preferred way to communicate unwinding information, so I've stuck to that convention. I think the arguments here apply in either case.)Some unknowns that might be important
catch_unwind
allow one to intercept foreign exceptions?-Zpanic=abort
interact with FFI calls that may unwind?Assumptions
We can define unwinding semantics in some reasonable and consistent way across virtually all platforms
For example, there seem to be some unknowns about how longjmp and dtors interact with LLVM. I'm presuming they can be resolved in a satisfactory way.
If this is not the case, then we may want to have people "opt-in" to unwinding because it would allow us to error out on platforms that can't handle exceptions properly.
Arguments for making the default (
"C"
) forbid unwinding, and requiring opt-inMost FFI calls, in practice, will never unwind, but will still use extern "C"
It is believed to be true that the vast majority of FFI calls are to functions (typically implemented in C) that are never expected to unwind. Thus this default aligns better with the reality.
If we did introduce a "C noexcept" ABI, then there are two possibilities:
Smaller code size if the default is that unwinding is UB
Most FFI calls will be
extern "C"
and most FFI calls will never unwind. Aligning those defaults allows the compiler to remove more dead code.In the case of
-Zpanic=abort
, if we define that we will give a hard abort on unwinding, then each call site to a FFI function (at least anextern "C"
function) will require some amount of "shim code" to catch and give a hard error.On the other hand, if we say that unwinding through an extern "C" function with
-Zpanic=abort
is UB, the code size is not a problem, but there are interactions with unsafe code (see next section).Potential for unwinding interacts strongly with unsafe code
Let us first assume that
-Zpanic=abort
means that unwinding is UB. In that case, if you have Rust code which invokes a foreign function that does indeed unwind, that Rust code becomes UB just by changing to-Zpanic=abort
(whereas it was well-defined before).To sidestep that, you can assume that
-Zpanic=abort
will insert an abort shim, but now we are paying a higher price. (We could also make it configurable whether or not a shim is created, perhaps even tied to debug/release builds like overflow, but this is making the feature more complex and less ergonomic.)On the other hand, if unwinding must be explicitly declared (via a "C unwind" ABI), we could make it an error to invoke a "C unwind" function with
-Zpanic=abort
, or we could choose to add an abort shim – but this is expect to occur much less frequently, since most functions do not unwind.People will overlook unwinding, better for it to be made explicit
Because most FFI calls do not unwind, people who make FFI calls are likely not to consider that unwinding is a possible outcome. Thus they are unlikely to help ensure that their code is unwind safe. Using the "C unwind" ABI helps draw attention to the unusual case, making it easier to do code audits.
The danger with a misaligned default, like "C can unwind but usually doesn't", is that it will be hard to tell if someone is using
extern "C"
because their function can unwind or simply because it was the obvious thing to do.People may adopt the folk wisdom of 'just use "C noexcept"' everywhere
e.g., cramertj mentioned that Fuschia would likely prefer to just use "C noexcept" everywhere rather than use "C", given that it would save them on codesize.
However, this is not really "doable" via just a local change: for example, libc exports using the "C" ABI could cause trouble if fuschia is using them.
Arguments for making the default (
"C"
) permit unwindingRust's C ABI would match the system definition
Most system ABIs specify an unwinding standard. This would make Rust's "C" ABI match the underlying system ABI more completely
The default ABI ("C") permits invoking a wider set of functions without UB
This could be seen as leading to less UB overall, although it must be balanced against the interactions with
-Zpanic=abort
.The precedent in C/C++ is to permit unwinding by default
When compiling with
-fexceptions
, at least, the assumption is that most functions can unwind. To rule out unwinding, one must tag a function with anoexcept
attribute (it is UB if anoexcept
function unwinds, though in many cases C++ will also catch the exception and abort). Therefore, users might expect this default.(N.B. "C/C++" is not one language and the C standard has no such thing as
-fexceptions
afaik. // Centril)Existing libc bindings use "C" but sometimes unwind
Because of the potential for pthread-cancelation, a large set of libc bindings would more accurately be "C unwind". The pthreads man page lists them out (search for "cancelation points") but they include many common functions.
This implies a few things:
(Seems like a great service to users to clarify unwinding/not in the
libc
crate by usingextern "C nounwind"
! // Centril)ABI boundaries should not change semantic behavior
This is from an email from Nick Lewycky, a Wasmer dev who worked on LLVM for ~5 years:
("Supposed to" isn't justified here and we are allowed to, and do add shims on e.g. coercing things, including function pointers, afaik. // Centril)
You might like to avoid unwinding of Rust functions, too
We discussed how unsafe code in particular must be careful to be "unwind safe", but this applies not just to "C" functions but really to any Rust function. Therefore, we might like to have a feature that guarantees that some callee cannot unwind. But do we really want to use the ABI string to contrl this in the more general case?
Addendums
Expressive power of a
extern "C"
feature that is guaranteed not to unwindThe C++ language has a
noexcept
keyword that implements a couple of features, one of which is to be able to make whether a function unwinds or not part of a functions' type (e.g. similar to#[unwind(aborts)]
, but being part of the type system).Adding the guarantee to the language that
extern "C"
do not unwind has the same expressive power as C++noexcept
keyword, but it ties that guarantee to foreign functions, whereas it might be something you want more generally (see prior point).Proof: first, by using
extern "C"
on a function declaration, we state that it does not unwind, just likenoexcept
. We can also useextern "C"
in type declarations, to constrain the types that generic code accepts. For example,extern "C"
allows implementing aNeverUnwind
trait that can be used to bound function typesthat never unwind:
This trait can be used to implement APIs that rely on a function never unwinding for correctness, like the ones in the take_mut crate, without using
DropGuard
s to restore invariants thatunsafe
code temporarily violates in case of a panic:With
specialization
, code that usesDropGuards
can be provided in the generic case, and code that does not in the case of+ NeverUnwinds
is satisfied.It also allows implementing a trait that makes sure that calling a function never unwinds, subsuming the use cases of the no_panic crate:
which can be used to make sure that function calls do not unwind:
All
.nounwind_call
s are guaranteed not to unwind. They will benounwind
in LLVM-IR, eliminating panic handling code in the caller.The
-C panic=abort
feature is very effective at applying these optimizations to the whole binary. However, it might be tempting for users using-C panic=uninwd
, or for library writers, to start usingextern "C"
instead of the Rust ABI as an optimization hammer similar to, e.g.,#[inline]
, to work around the cases for which LLVM does not deduce thenounwind
attribute appropiately.By adding a
extern "C"
feature to the language that's guaranteed not to unwind we are actually adding a feature to Rust that's very similar in power tonoexcept
, but with worse ergonomics. It might make more sense to allowextern "C"
and all ABIs to unwind by default, and provide a better language feature for specifying that a function never unwinds (e.g.nounwind fn foo(...) { ... }
). Having some ABIs that unwind by default and some that do not might complicate adding such a feature. (Why would it complicate things? At most it seems likeextern "C" fn
would be subsumed bynopanic extern "C" fn
but the former is still shorter (see arguments above for nounwind as common default). // Centril)