Conversation
| $(OCAMLOPT) -w a -o $$F.native$(EXE) $$f; \ | ||
| fi; \ | ||
| done | ||
| $(if $(findstring win32,$(UNIX_OR_WIN32)),:, \ |
There was a problem hiding this comment.
This doesn't look right - surely config/s-nt.h needs updating to define HAS_STACK_OVERFLOW_DETECTION for Win64?
There was a problem hiding this comment.
no, HAS_STACK_OVERFLOW_DETECTION enables the very Unix-specific SIGSEGV handling code that handles the stack overflow in signals_asm.c ; that just won't compile on Win32.
byterun/win32.c
Outdated
|
|
||
| void caml_win32_overflow_detection() | ||
| { | ||
| SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); |
There was a problem hiding this comment.
Reading, rather than trying it at this stage: is this functionally necessary?
|
This is a great leap towards closing a very tedious piece of behaviour on Windows, thanks! Just to expand on the problem: since Visual Studio 2005, the Microsoft CRT includes various calls (principally via abort) which clear the exception filter in order to guarantee that the runtime is torn down. MinGW in its default configuration is therefore unaffected because the core msvcrt included in Windows is based on .NET 2002/3's implementation. However, given its latest incarnation (the Universal CRT or UCRT) is now a Windows component again, that "stability" is not guaranteed to last and in fact I think mingw can already be configured to use other versions of the CRT if you wish. I'm not (yet) 100% convinced about the equivalence of using a vectored exception handler instead of the unhandled filter - but I'm just putting that out there for now (the alternative is to hook SetUnhandledExceptionFilter and effectively disable the CRT from calling it) |
|
I haven't delved into the history enough - was supporting Windows '9x the original reason for being unable to detect stack overflow? |
|
I had a look at the sources of the CRT, trying to find out why
er what do you mean ? Stack overflow detection was working, just only on 32-bits and not with the recent MSVC CRT that prevent SetUnhandledExceptionFilter from working. |
|
OK, looked at this in a bit more detail (including building it). I did not realise that 32 bit native stack overflow detection actually worked because as it happens on my machine it's masked by the crash dialog (and possibly the fact that the machine I'm sat at has a slightly old OCaml by default) and had always assumed it wasn't available on 32 or 64-bit and, more importantly, the test for it has always been skipped (I'm very familiar with differences in the testsuite between the platforms......) I think that it should be refactored to use Could the relevant section in the manual be updated in this GPR as well (I don't think there's a policy of separating PRs - @gasche, @Octachron?) |
|
On the contrary, having documentation changes in the same PR is better. (It helps reviewer who can read the doc, and also writing the doc can clarify things and prompt changes to the implementation.) |
|
@dra27, OK waiting on some other opinion on this because I'm not too hot about the |
|
Commit fab3b7a adds this line to byterun/win32.c:
+ ctx->Rsp = (uintnat) (alt_rsp - 4 - 1);
I was wondering whether the - 4 and - 1 wouldn't diserve a word of
explanation (in the code?) but perhaps their meaning is obvious
for those familiar with this kind of code.
|
|
I think that it should be refactored to use
`HAS_STACK_OVERFLOW_DETECTION` - the fact that the test was skipped
before was a bug and I think that the bug is that s-nt.h should be
defining that, rather than extra tests being included in the Makefile
itself.
Shouldn't there be a real make variable to let the build system know
whether this feature is actually available?
The code in asmrun should be refactored so that the signals bit is
under a further `#ifdef` and the Windows stuff under another but both
activated by HAS_STACK_OVERFLOW_DETECTION. @shindere may have an
opinion on this too - basically, changes should be moving towards
allowing a clear configure route for the Windows ports where the
features are similarly-named.
I agree in principle, but am not familiar enough with the code to have
an opinion about how this can be achieved, sorry.
|
|
@shindere - it could be made formal inasmuch as the Given that only one (I think?) C file is affected, it doesn't seem too major a thing to need to do. @oandrieu - I'm not clear whether it's that you don't like the idea of the change or don't like the idea of making the change... if the latter, then I'm happy to push a commit showing it! |
|
@shindere - the @dra27 - I don't quite see what you have in mind. |
|
Olivier Andrieu (2016/11/29 08:42 -0800):
@shindere - the `-4 - 1` corresponds to the Win64 calling convention:
caller is supposed to allocate backing space on the stack for the 4
argument-passing registers ; the -1 is the space for the return
address (unused here since we never return).
Okay, thanks. If this is obvious to those familiar with this kind of
code, I'm fine.
|
|
David Allsopp (2016/11/29 06:25 -0800):
@shindere - it could be made formal inasmuch as the `s.h` generated by
`configure` could be converted into a Makefile include as well (or one
could be generated at the same time), or potentially stack overflow
detection should be included in the output of `ocamlc -config`... but
that's possibly a separate matter?
I think so, yes. It will probably be easier to address such issues once
we start moving to autoconf. Then configure can generate as many files
as needed, Makefiles, header files, OCaml files...
|
|
I'm not enough of a Win32 guru to review this code, and I'm not sure we'll find enough experts who can. Still, my feeling is that the new code is probably better than the old one, even if not perfect, so we don't have anything to lose by using the new code. |
|
I agree that Having done more testing, I have three changes to propose:
@xavierleroy, @shindere - what are your thoughts? |
Right, it's not. I just stumbled on the function in the documentation. I'm not sure what good is this crash dialog for an OCaml program, but yes it's unrelated to the whole stack overflow detection — I'll remove it.
OK, makes sense.
Well the modification to the testcase is still useful to test that the system stack overflow detection has been properly re-set. |
|
You are completely right about the new addition to the test case - sorry, I forgot that that there was too! Please do keep that :) |
fb927d4 to
87f6f7f
Compare
|
Could we restart discussion and come to a decision for this PR? Are there any pending issues? If not I'm in favor of using this code. |
|
This is probably too late or too risky at this point for inclusion in 4.05, but I'm still in favor of merging in master soon, so that the code gets some exposure before ending in 4.06. |
|
Does this code operate correctly if the fault happens in a thread that is not the main thread? |
|
@mshinwell Yes I believe so. I found no indication that the protection mechanism is different in non-main threads and tests work as expected. |
|
I think this is ready for merge, except: it looks like @oandrieu needs to sign a CLA first. |
|
I tend to agree with @dra27 on the
Since |
|
@damiendoligez : @oandrieu sent me a signed CLA on March 15th, so everything is OK on the legal front. (My CLA notes that you might have consulted are not up to date, sorry about that, will update today.) |
|
@damiendoligez @dra27 Could you please come to a clear statement as to what you would like to see changed, if anything, in this pull request before it is merged? @oandrieu Please rebase to resolve conflicts. |
9f0df4b to
22abcc1
Compare
|
@mshinwell done — not sure what the AppVeyor failure is about though. |
|
@oandrieu I think if you rebase again now it will be ok. |
22abcc1 to
720989c
Compare
for some reason, SetUnHandledExceptionFilter is overridden by the Visual CRT, so it's not working on the MSVC port (it's working fine with mignw though). The AddVectoredExceptionHandler API seems a better fit for this (requires WinXP)
also modify the test so that it triggers two stack overflows, to make sure that the Windows stack has been properly re-set by our exception handler
Win32 overflow detection hasn't much to do with signals.
This is an adaptation of SVN commit r12159: ,---- | PR#5064, PR#5485: try to ensure that 4K words of stack are available | before calling into C functions, raising a Stack_overflow exception | otherwise. This reduces (but does not eliminate) the risk of | segmentation faults due to stack overflow in C code. `---- Except that the stack probe is only one page size ahead, so as not to overstep the guard page marking the end of the stack.
|
About |
|
OK, I'm going to merge this then; I think there's value in having this in sooner rather than later so as to get more testing. We can always tweak it at a later date. |
|
The MSVC32 port does not compile for the current trunk. boot/ocamlrun ./ocamlopt -g -nostdlib -I stdlib -I otherlibs/dynlink -o ocamlc.opt @oandrieu: can you see where the porblem comes from? Are you able to submit a fix for it? If required I'll be happy to help out with testing on the CI machines. Thanks! |
|
@shindere ah crap, sorry. |
|
Olivier Andrieu (2017/05/12 07:00 -0700):
@shindere ah crap, sorry.
No problem!
Yes, a fix is easy enough: ***@***.***
OK cool, thanks!
Should I submit a new PR ? or is there a way to amend/extend this one
?
Usually I just push a hotfix to trunk and make sure (1) the commit
message for the hotfix contains a reference to the PR it fixes and (2)
the comments of the PR contain a note linking to the commit that
complements the PR.
If this is fine for you I will do that with your commit.
Could you perhaps just elaborate a bit your commit message, please?
Ideally, I'd find it cool if it could sum up what the PR was doing,
explain what was wrong with the way it was done in the original PR and
then give a hint about how it fixes the issue.
Once you have done that I'll cherry pick to trunk and update the PR with
a reference to the commit.
You may also want to make sure your commit is already based on the
latest trunk.
Thanks a lot!
|
|
@shindere OK, thanks. I've updated the commit message and rebased on trunk here: oandrieu/ocaml@1a111e1 . It would be great if you could arrange for CI to run on win32,win64 × msvc,mingw . |
|
Olivier Andrieu (2017/05/12 09:41 -0700):
@shindere OK, thanks. I've updated the commit message and rebased on
trunk here: ***@***.*** . It would be great if you could arrange
for CI to run on win32,win64 × msvc,mingw .
Thanks. Sure, will run CI before merging - next week. ;-)
|
|
Olivier Andrieu (2017/05/12 09:41 -0700):
@shindere OK, thanks. I've updated the commit message and rebased on
trunk here: ***@***.*** . It would be great if you could arrange
for CI to run on win32,win64 × msvc,mingw .
When I said it's fine without a PR yesterday I was motivated by a wish
to keep things simple and not making them un-necessarily bureaucratic. But,
giving it a second thought, I came to realise that it may still be
better to have a PR dedicated to this commit. This PR should also update
the already existing Cahnges entry by listing the two PRs involved in
this feature. That way, if somebody wants to backport it (in 4.05, say),
it will be enough to read the Changes file to know what to cherry-pick.
So, with my aplogies for changing my mind, could you please create a
dedcicated PR, that also updates the Changes file appropriately,
@oandrieu?
It will also make it slightly easier to do the CI tests before merging.
Thanks a lot for your understanding and for your work and
responsiveness!
|
Co-authored-by: Guillaume Bury <[email protected]>
Co-authored-by: Guillaume Bury <[email protected]>
b11eea1 flambda-backend: Introduce Import_info (ocaml#1036) bc5b135 flambda-backend: Fix `ocamlobjinfo` on flambda2 .cmx files (ocaml#1029) c8babbd flambda-backend: Compilation_unit optimisations (ocaml#1035) e8d3e22 flambda-backend: Use 4.14.0 opam switch for building (includes upgrading ocamlformat to 0.24.1) (ocaml#1030) eb14a86 flambda-backend: Port PR81 from ocaml-jst (ocaml#1024) 131bc12 flambda-backend: Merge ocaml-jst 2022-12-13 (ocaml#1022) 06c189a flambda-backend: Make stack allocation the default (ocaml#1013) 98debd5 flambda-backend: Initial support for value slots not of value kind (ocaml#946) deb1714 flambda-backend: Add is_last flag to closinfo words (ocaml#938) d07fce1 flambda-backend: Disable poll insertion in Configure (ocaml#967) 0f1ce0e flambda-backend: Regenerate ocaml/configure autoconf 2.69 (instead of 2.71) (ocaml#1012) 27132d8 flambda-backend: Fix for spurious typing error related to expanding through functor arguments (ocaml#997) 724fb68 flambda-backend: Use `Compilation_unit.t` instead of `Ident.t` for globals (ocaml#871) 396d5b8 flambda-backend: Add a test for frametable setup in natdynlinked libraries (ocaml#983) b73ab12 flambda-backend: Fix invocation of `caml_shared_startup` in native dynlink (ocaml#980) 7c7d75a flambda-backend: Fix split_default_wrapper which did not trigger anymore with flambda2 (ocaml#970) 8fb75bd flambda-backend: Port ocaml#11727 and ocaml#11732 (ocaml#965) fdb7987 flambda-backend: Fix include functor issue after 4.14 merge. (ocaml#948) 9745cdb flambda-backend: Print -dprofile/-dtimings output to stdout like 4.12 (ocaml#943) 5f51f21 flambda-backend: Merge pull request ocaml#932 from mshinwell/4.14-upgrade 841687d flambda-backend: Run make alldepend in ocaml/ (ocaml#936) 72a7658 flambda-backend: Remove reformatting changes only in dynlink/dune (preserving PR889 and adjusting to minimise diff) 6d758cd flambda-backend: Revert whitespace changes in dune files, to match upstream c86bf6e flambda-backend: Remove duplicate tests for polling 971dbeb flambda-backend: Testsuite fixes 32f8356 flambda-backend: Topeval fix for symbols patch befea01 flambda-backend: Compilation fixes / rectify merge faults a84543f flambda-backend: Merge ocaml-jst 8e65056 flambda-backend: Merge ocaml-jst 4d70045 flambda-backend: Remove filename from system frametable (amd64) (ocaml#920) 5e57b7d flambda-backend: Bugfix for runtime frame_descr logic for C frames (ocaml#918) 6423d5e flambda-backend: Merge pull request ocaml#914 from mshinwell/merge-ocaml-jst-2022-10-24 ead605c flambda-backend: Add a missing Extract_exception (ocaml#916) c8f1481 flambda-backend: Resolve conflicts and add specialise/specialised attributes to Builtin_attributes cf4d0d3 flambda-backend: Merge fixes (ocaml#21) c2f742f flambda-backend: Re-enable some tests for Flambda2 (ocaml#881) 3d38d13 flambda-backend: Long frames in frametable (ocaml#797) 85aec7b flambda-backend: Add loop attribute to Builtin_attributes c0f16e3 flambda-backend: Compilation fixes 90dea23 flambda-backend: Merge flambda-backend/main 5acc6ea flambda-backend: Fixes after merge e501946 flambda-backend: Merge ocaml-jst 115083b flambda-backend: Merge ocaml-jst 9943b2e flambda-backend: Revert "Revert "Transform tail-recursive functions into recursive continuations (ocaml#893)"" (ocaml#909) ce339f1 flambda-backend: Fix alloc modes and call kinds for overapplications (ocaml#902) e6a317c flambda-backend: Revert "Transform tail-recursive functions into recursive continuations (ocaml#893)" 853c488 flambda-backend: Transform tail-recursive functions into recursive continuations (ocaml#893) 5a977e4 flambda-backend: Fix missing End_region primitives on switch arms (ocaml#898) 7fa7f9d flambda-backend: Add missing dependencies to Dune files (ocaml#889) 3cd36f0 flambda-backend: Have Lambda `Pgetglobal` and `Psetglobal` take `Compilation_unit.t` (ocaml#896) 7565915 flambda-backend: [@poll error] attribute (ocaml#745) 9eb9448 flambda-backend: Backport the main safepoints PRs (ocaml#740) 689bdda flambda-backend: Add strict mode for ocamldep (ocaml#892) git-subtree-dir: ocaml git-subtree-split: b11eea1
Co-authored-by: Julien Girard <[email protected]>
This branch enables stack overflow detection for native code on Windows 64. There are mainly two changes compared to the existing Win32 code:
SetUnhandledExceptionFilter) wasn't working anymore with recent MSVC CRT ;I'm not too sure about the backtrace stashing mechanism:
caml_last_return_addressandcaml_bottom_of_stackhave not been updated so it seems the backtrace may be incorrect. But the Unix equivalent in signals_asm.c ought to have the same issue and it does not bypass the stashing …