Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding LLVM/Clang support #9

Open
RivenSkaye opened this issue Feb 20, 2024 · 8 comments
Open

Consider adding LLVM/Clang support #9

RivenSkaye opened this issue Feb 20, 2024 · 8 comments

Comments

@RivenSkaye
Copy link
Contributor

So it seems like LLVM supports SEH for both i686 and x86_64 which might even allow for enabling this for use with windows-gnu toolchains, probably by setting the compiler used as clang and linking their libc and libunwind.

I'll do some more digging and experimenting soon and PR if and when I get it to work in the hopes we can get SEH support across toolchains. Some searching does make it seem like GCC has support for at least x86_64 SEH and with the relevant Borland patents for it having expired, we might yet see i686 support as well there!

@sonodima
Copy link
Owner

sonodima commented Feb 20, 2024

This would be a great addition, considering that in the future I would like to explore some way to support exception handling with Posix signals.

MicroSEH could become the only library needed for HW exception handling in Rust, on all the compilers and major OSes.

@RivenSkaye
Copy link
Contributor Author

RivenSkaye commented Jun 6, 2024

Good news from the GCC repos. Seems like both GCC and LLVM support it for at least x86_64 in their standard platform-specific unwind mechanics! At least up to a certain point, but that should also mean they provide the required entries in headers to register our own handler functions.

Do note, though, that "handling" some of these exceptions does not mean the environment is still in a consistent or workable state; so perhaps add a warning to the docs that some should just mean graceful teardown and exit rather than recovery. As for Windows Services, SEH is used for some signaling as well. Including the kind that tells you that your process is about to get nuked.

I think it's reasonable for a crate like this to support only the common use case - that is, for embedded someone will want a much more specific solution and for any desktop/server workload it's reasonable to assume people aren't using 32-bit architectures and OSes anymore

@sonodima
Copy link
Owner

I think it's reasonable for a crate like this to support only the common use case - that is, for embedded someone will want a much more specific solution and for any desktop/server workload it's reasonable to assume people aren't using 32-bit architectures and OSes anymore

I completely agree, this library was originally just meant to be for Win-MSVC-x64 but I'm okay with adding support for multiple architectures and compilers, as long as they don't require too much effort!

@drishal
Copy link

drishal commented Jun 13, 2024

I think it's reasonable for a crate like this to support only the common use case - that is, for embedded someone will want a much more specific solution and for any desktop/server workload it's reasonable to assume people aren't using 32-bit architectures and OSes anymore

I completely agree, this library was originally just meant to be for Win-MSVC-x64 but I'm okay with adding support for multiple architectures and compilers, as long as they don't require too much effort!

is there any possibility for adding support of windows-gnu target?

@sonodima
Copy link
Owner

sonodima commented Oct 6, 2024

is there any possibility for adding support of windows-gnu target?

I think I have seen something about gcc having __try1 and __except1, but honestly I have very little experience with GCC, especially on Windows

@RivenSkaye
Copy link
Contributor Author

The __try1 and __except1 macros produce pretty different code compared to MSVC's. The main issue with cross compilation is that Windows just tacks on an extra page on the stack for the exception struct to do any handler calls with. GCC's closest option is the alt stack, which you might be able to tell it is right behind the current one, but I'm nit quite sure how well that works. At that point, you might as well look into manually doing the whole handler registration so it works from any compiler

@sonodima
Copy link
Owner

sonodima commented Oct 7, 2024

That's interesting. The main reason I went with wrapping the MSVC SEH implementation was to quickly have support for all the archs supported by Windows.

Writing the handler registration ourselves would also make many of the docsrs build.rs issues we had disappear.

@RivenSkaye
Copy link
Contributor Author

RivenSkaye commented Oct 9, 2024

Writing handler registration manually is a lot of work and we'd be stuck manually writing them for every supported exception type. And then we'd be doing this once for every thread (though if we do that inside try_seh it'll always be the current thread). For some more info take a peek at one of the few resources on the topic. It's a 4 part series that also touches upon why the inline asm mentioned in the README here is such a pain to get working. But Wikipedia by way of WINE provides some useful information as well, if you really want to go down that path.

Though if we can assume these exceptions don't come from the calling and handler-registering frame, which should be the case here because we very explicitly register before doing anything, then LLVM seems to support all we need out of the box already! So we should be able to call the LLVM toolchain for this as well, with -fms-compatibility, and have it Just Work™ for most cases. Emit a compiler warning and call it a day I suppose?
Edit: might be worth mentioning that I have no idea how well this works on cross toolchains. This is specifically for Windows native compilation using a non-MSVC toolchain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants