[GHC] #12186: GHC compiled executable incompatible with 3rd party software

#12186: GHC compiled executable incompatible with 3rd party software -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Keywords: | Operating System: Windows Architecture: x86_64 | Type of failure: Incorrect result (amd64) | at runtime Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I've been trying to work on a library that allows Haskell to call into .Net code, but there's a major show stopper in that the .Net runtime starting with version 4.0, doesn't like the executable files that GHC produces. I've managed to reduce a test case to not actually depend on using Haskell, but just using GHC to compile a C file, and that C file being nothing but a dumb wrapper around a small dll. The resulting executable exhibits the incorrect behaviour: {{{
stack exec ghc -- main.c -no-hs-main main 1 2 3 4 5 6 ICLRRuntimeHost Start failed w/hr 0x80004005 }}}
Then I can also use GCC on the intermediate object file that was created, and produce an executable that exhibits the correct behaviour: {{{
stack exec gcc -- main.o -o main2.exe main2 1 2 3 4 5 6 7 8 }}}
I've put a copy of the test case here: https://gitlab.com/tim-m89/DotNetHostingTest -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: x86_64 Type of failure: Incorrect result | (amd64) at runtime | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by tim-m89): Further investigation reveals that this is directly caused by how the linker is invoked with the option: {{{ --stack=0x800000,0x800000 }}} To set the reserve & commit space of the stack respectively, but the issue goes away if the stack commit is <= 0x7E000 {{{ --stack=0x800000,0x7E000 }}} This change was introduced on #8870 as a 'temporary' fix on 7.8.1 where a 'proper' fix was apparently supposed to happen on 7.8.2. So my next questions would be: * Do we need still need the temporary fix? and if so, * How much is involved / ETA of the proper solution (easy enough to emit [#chkstk __chkstk] calls?) * Is 0x800000 commit overkill / would a smaller value work? [=#chkstk !__chkstk] [https://support.microsoft.com/en-us/kb/100775] -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * cc: Phyx- (added) * architecture: x86_64 (amd64) => Unknown/Multiple * related: => #8870 Comment: Hi @tim-m89, Thanks for the report and for looking into this. How did you settle on 504k bytes? Was that just a number you tried that worked? I don't know the code generators very well, so I'm afraid I can't give a good estimate without looking into this. But I do agree that 8mb of initial committed memory seems rather overkill. If the tests for #8870 still pass with the lowered commit space then I guess there's no problem lowering it for now. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by tim-m89): * cc: tim-m89 (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by awson): While to not simply add the specific options to '''your particular''' project? `-optl-Xlinker -optl--stack=0x800000,0x7E000` would do the right things for you then. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by tim-m89): @Phyx- The specific number 0x7E000 was found by finding a number that worked and 1 that didn't, trying the half way point, and repeating until I found a number where no 1 byte, 4 byte or whole page increment would work. Why 126 pages? I just don't know. @awson The original hard coded stack size is still getting passed to gcc in the linker stage though, and it appears that the hard coded argument is taking precedence (perhaps because it occurs afterwards in the gcc invocation?). So I can't just specify it as an argument ghc should pass to the linker. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Phyx-): Hmm, when I pass it to the linker as an argument to GHC I get {{{ gcc.exe "-fno-stack-protector" "-DTABLES_NEXT_TO_CODE" "-Wl,--hash- size=31" "-Wl,--reduce-memory-overheads" "-Xlinker" "-- stack=0x800000,0x800000" "-static-libgcc" "-Xlinker" "-- stack=0x800000,0x7E000" }}} so it should work. Unless you can only set it once? I'm keen on seeing a proper fix, but for now, if passing it to GHC doesn't work, maybe we can make it a flag to GHC to control the reserved and commit space. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by awson): I tested my suggestion and it worked. OTOH, I tested only a simple `ghc` invocation. Perhaps, `cabal` can mess things up, though. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by tim-m89): When passing this specific linker option, it needs to come after the hard coded 1 to to take precedence apparently, but from my limited tests that only seems to be the case for simple GHC invocations. Specifying that option in either the cabal file or passed to stack as an option to pass to GHC via 'stack build' doesn't work, though passing it to GHC directly works fine (including when that same GHC invocation was via 'stack exec'). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Phyx-): Ok, If I'm reading this correctly, GCC can already emit stack checks. https://gcc.gnu.org/onlinedocs/gnat_ugn/Stack-Overflow-Checking.html So isn't adding `-fstack-check` enough? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Phyx-): Right, so using `-fstack-check` seems to emit calls to `___chkstk_ms` before stack alignments. {{{ 633de0: 48 83 e0 f0 and $0xfffffffffffffff0,%rax 633de4: e8 07 0c 0d 00 callq 7049f0 <___chkstk_ms> 633de9: 48 29 c4 sub %rax,%rsp }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Phyx- Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Phab:D2535 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * owner: => Phyx- * differential: => Phab:D2535 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Phyx- Type: bug | Status: patch Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Phab:D2535 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * status: new => patch -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:12 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues
-------------------------------------+-------------------------------------
Reporter: tim-m89 | Owner: Phyx-
Type: bug | Status: patch
Priority: normal | Milestone:
Component: Compiler | Version: 8.0.1
(Linking) |
Resolution: | Keywords:
Operating System: Windows | Architecture:
Type of failure: Incorrect result | Unknown/Multiple
at runtime | Test Case:
Blocked By: | Blocking:
Related Tickets: #8870 | Differential Rev(s): Phab:D2535
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Tamar Christina

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Phyx- Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 (Linking) | Resolution: fixed | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Phab:D2535 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * status: patch => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:14 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12186: Windows linker stack commit setting causing issues -------------------------------------+------------------------------------- Reporter: tim-m89 | Owner: Phyx- Type: bug | Status: closed Priority: normal | Milestone: 8.2.1 Component: Compiler | Version: 8.0.1 (Linking) | Resolution: fixed | Keywords: Operating System: Windows | Architecture: Type of failure: Incorrect result | Unknown/Multiple at runtime | Test Case: Blocked By: | Blocking: Related Tickets: #8870 | Differential Rev(s): Phab:D2535 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * milestone: => 8.2.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12186#comment:15 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC