# Std::process::abort should be implemented through exit(3) on Win32 mode

**URL:** <https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059>\
**Category:** libs\
**Created:** [January 31, 2022, 7:09pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059 "2022-01-31T19:09:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [January 31, 2022, 7:09pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/1 "2022-01-31T19:09:12Z")

</div>

According to this issue: [std::process::abort from FFI does not abort. · Issue #93276 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/issues/93276)

std::process::abort now implemented throught \_\_fastfail, which dont terminate program on Win7 and earlier.  
On Win7 \_\_fastfail throws an exception, and WndProc on Win32 will... suppress it! And panicked program will enter WndProc with brocken state and print error message to console infinite times. So if you compile program in "panic=abort" mode, you will no abort.  
Currently the only way to terminate program with std is use unwinding mode and catch\_unwind the panic on the top of WndProc and write exit(3) in panic handler. But unwinding mode is a lot of overhead and boilerplate inside executable. So on target i686-windows-\* panic should be implemented through exit(3)

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [February 1, 2022, 8:42am UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/2 "2022-02-01T08:42:55Z")

</div>

What about trying fastfail first and if it unwinds doing `exit(3)`?

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [February 1, 2022, 2:26pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/3 "2022-02-01T14:26:25Z")

</div>

> [@T4r4sB](#):
>
> So on target i686-windows-\* panic should be implemented through exit(3)

1. Do you mean `exit(3)` as in `man 3 exit` or as in "call the C library function `exit` with argument 3"? If the former, what exit code should be used and why? If the latter, why exit code 3 specifically (and not, for instance, 0xC0000409 as is documented for `__fastfail`?)

2. As best I can tell from skimming MSDN, the situation you describe can only occur

Is there perhaps, instead, something Rust's runtime could do to make old versions of Windows _not_ suppress SEH exceptions thrown out of window procedures by 32-bit processes? Or maybe it doesn't even go in `std`, but instead, in the hypothetical crate that helps you write Win32 GUI programs without having to use as much `unsafe` as you did in the test program you showed in the bug you linked to.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [February 1, 2022, 6:44pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/4 "2022-02-01T18:44:06Z")

</div>

> What about trying fastfail first and if it unwinds doing `exit(3)` ?

Rust catch\_unwind can catch only Rust's exception, so I dont know the way to check other exceptions. So I dont see the way to implement this variant.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [February 1, 2022, 6:44pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/5 "2022-02-01T18:44:36Z")

</div>

> [@zackw](#):
>
> Do you mean `exit(3)` as in `man 3 exit` or as in "call the C library function `exit` with argument 3"?

I mean std::process::exit(3). The number 3 was got from here: [https://github.com/huangqinjin/ucrt/blob/faa22ac4e1e94c2832d48ff2edab4da3e4cfea31/startup/abort.cpp#L89](https://github.com/huangqinjin/ucrt/blob/faa22ac4e1e94c2832d48ff2edab4da3e4cfea31/startup/abort.cpp#L89)

But the exact value of exit code does not matter for me. Any value (except 0) is good, if program will terminate.

> [@zackw](#):
>
> As best I can tell from skimming MSDN, the situation you describe can only occur
> 
> - when `__fastfail` is used inside a window procedure
> - and the process is 32-bit but the OS is 64-bit
> - and the OS is Windows 7 or earlier

Yes, it is. This situation is not rare. I compile programs in x32 mode, because some users has old computers, and I want to make the target audience wider. Also if I publish x32 and x64 versions, some users will run x32 version on x64 system, and there is no way to prevent it.

> [@zackw](#):
>
> s there perhaps, instead, something Rust's runtime could do to make old versions of Windows _not_ suppress SEH exceptions thrown out of window procedures by 32-bit processes?

On library level the easiest way is to handle windows32 case here: [https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/mod.rs#L291](https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/mod.rs#L291), I can make PR if it is necessary. On user level I cant catch exceptions, I can catch only Rust panics in unwinding mode, but "panic=abort" mode is still broken.

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [February 1, 2022, 7:49pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/6 "2022-02-01T19:49:56Z")

</div>

I think the suggestion is to implement something like this: [rust/stack\_overflow.rs at 686663a49e57870c78a4cd047f23a44175fc67a4 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/blob/686663a49e57870c78a4cd047f23a44175fc67a4/library/std/src/sys/windows/stack_overflow.rs#L21-L39)

But for `EXCEPTION_ACCESS_VIOLATION`. So that it would call [`TerminateProcess`](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-terminateprocess) when the application is 32bit and the OS is 64bit Windows 7.

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [February 1, 2022, 9:24pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/7 "2022-02-01T21:24:36Z")

</div>

> [@T4r4sB](#):
>
> On user level I cant catch exceptions, I can catch only Rust panics in unwinding mode

"Rust on Win32 needs some way to catch arbitrary SEH exceptions" does seem like a valid feature request.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [February 2, 2022, 12:06pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/8 "2022-02-02T12:06:09Z")

</div>

> [@T4r4sB](#):
>
> Rust catch\_unwind can catch only Rust's exception, so I dont know the way to check other exceptions. So I dont see the way to implement this variant.

1. There could be a compiler intrinsic for this used by libstd.
2. I think you can set a vectored exception handler which exits when invoked. (edit: already suggested by chrisd)

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [February 2, 2022, 5:50pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/9 "2022-02-02T17:50:53Z")

</div>

> [@zackw](#):
>
> "Rust on Win32 needs some way to catch arbitrary SEH exceptions"

It will be ok, but I think, this way will be something like very platform-specific stuff, so I prefer to see it inside std lib abort implementation.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [February 2, 2022, 7:51pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/10 "2022-02-02T19:51:32Z")

</div>

> [@bjorn3](#):
>
> - There could be a compiler intrinsic for this used by libstd.
> - I think you can set a vectored exception handler which exits when invoked. (edit: already suggested by chrisd)

I think, first variant (when intrinsic used inside abort's implementation) will be better, because this intrinsic should be very platform-specific. It would be better to allow user to use the minimal possible count of platform-specific intrinsics.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [February 3, 2022, 1:13pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/11 "2022-02-03T13:13:07Z")

</div>

The vectored exception handler can set by `std::process::abort` just before trying to raise a fastfail exception. No need for the user to do this.

---

<div class="post-metadata">

**Author:** ![T4r4sB](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/t4r4sb/32/10037_2.png) [@T4r4sB](https://internals.rust-lang.org/u/T4r4sB)\
**Post date:** [February 3, 2022, 9:05pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/12 "2022-02-03T21:05:08Z")

</div>

I think, I can agree with this option

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 4, 2022, 9:05pm UTC](https://internals.rust-lang.org/t/std-abort-should-be-implemented-through-exit-3-on-win32-mode/16059/13 "2022-05-04T21:05:56Z")

</div>

Closed on May 4, 2022, 9:05pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
