Fail when calling the function from the Haskell shared library (that uses FFI and GHC API) on Visual studio 64-bit project
Summary
I work on the Visual Studio 2015/2017 C++ project, which uses the Haskell library. There are some metadata, processed by the Haskell library functions in the following way: the data are translated to Haskell code, compiled by the GHC 8.10.4, then some 'root' function is calculated and the result is returned to the C++ code. We had been working with the 32-bit architecture, and everything was fine, until we moved to the 64-bit version. After that I started to face different misbevaior, depending on the Windows version:
Exception “hPutChar: invalid argument (Bad file descriptor)”
on Windows 7
Error: :”ghcDLL: internal error: IMAGE_REL_AMD64_ADDR32[NB]: High bits are set in 133505844b0 for .text$.Lsow_info”
on Windows 10
I also came along with this issue: Internal error running FFI interpreter on Visual Studio, it illustrates the similar problem.Anyway, I decided to illustrate the issue with my own sample (see "Steps to reproduce") that includes also Haskell sources and probably may help to investigate deeper into the problem. Unfortunately, it was very hard to reproduce exactly the same behavior in the much less sample than the project we work on, but still I managed to reproduce a failure of the program.
Brief source description
Here we have the Haskell library, which exports, in particular, the following functions:
-
c_create_context
: creates the contexts and imports some standard modules (Prelude and Control.DeepSeq in this case) -
c_bring_decls_to_context
: takes Haskell source code, interprets it and bring the names to the given context -
c_execute
: takes the function name, evaluates it and returns the string representation of the result . -
c_ghc_has_exception
: checks, if the previous manipulation with the Context cause an exception. -
c_has_pref_error
: checks, if the function evaluation caused an error and extracts partial evaluated result. -
c_show_result
: print the result of c_execute.
For minimizing purposes modules names, passed to c_create_context
and the Haskell source code, passed to c_bring_decls_to_context
are hard-coded.
The HSCodeGen.cpp
inter-layer is needed, because the MSVC doesn't support some language constructions that are used in Rts.h.
Steps to reproduce
- Clone Haskell_FFI_GHC_API_64-bit_fail.
- Build the project, Debug x64 configuration.
- Run the program.
Expected behavior
Should print "Hello, world!"
Actual behavior
Fails on c_execute.
Environment
- GHC version used: 8.10.4
- OS Windows 10
- System architecture 64-bit
- Visual studio 2015