Ticket #706 (closed bug: fixed)

Opened 7 years ago

Last modified 2 years ago

GHC uses _stub.c files regardless of whether any 'foreign import' decls remain in a .hs file

Reported by: ncalexan@… Owned by: simonmar
Priority: high Milestone: 7.4.1
Component: Compiler (FFI) Version: 6.4.1
Keywords: ffi, link Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time crash Difficulty: Moderate (less than a day)
Test Case: Blocked By: #3687
Blocking: Related Tickets:

Description

It appears GHC links any _stub.o files it can find, which is not correct (although usually it only leads to duplicate symbols.) To duplicate, have two modules A and B, and a foreign import in A. A_stub.o will be built as usual. Copy A_stub.o to B_stub.o, relink and have duplicate symbols.

Attachments

Win32TestStubBug.zip Download (80.3 KB) - added by jcab@… 7 years ago.
Repro for the issue with stub.c files

Change History

Changed 7 years ago by simonmar

  • os changed from MacOS X to Multiple
  • architecture changed from powerpc to Multiple

I believe this only happens if you are using --make or GHCi, and GHC decides the file does not need compiling. In this case it picks up any _stub.o file it finds. We should really check the source file for foreign export / foreign import wrapper before using the _stub.o file.

Changed 7 years ago by simonmar

  • difficulty changed from Unknown to Easy (1 hr)

Changed 7 years ago by simonmar

  • owner set to simonmar
  • milestone set to 6.6

Changed 7 years ago by simonmar

  • milestone changed from 6.6 to 6.6.1

Changed 7 years ago by simonpj

  • owner changed from simonmar to igloo
  • difficulty changed from Easy (1 hr) to Moderate (1 day)

Changed 7 years ago by simonpj

  • milestone changed from 6.6.1 to 6.8

Changed 7 years ago by jcab@…

  • summary changed from GHC links _stub.o files regardless of whether any 'foreign import' decls remain in a .hs file to GHC uses _stub.c files regardless of whether any 'foreign import' decls remain in a .hs file

The description of the bug is incorrect. It picks up the _stub.c files, not the _stub.o files, and it picks them up even if the file is recompiled (and didn't re-generate the _stub.c file).

Verified with GHC 6.6. not even -fforce-recomp works, only manually deleting the _stub.c files works around the problem.

Problem with 6.6 is that it won't generate _stub.c files for modules where previous versions did (for modules that _use_ other modules which have the FFI that causes _stub.c files), so after upgrading you may encounter this problem.

Changed 7 years ago by jcab@…

Repro for the issue with stub.c files

Changed 7 years ago by jcab@…

In order to repro, just install GHC in a Windows machine, unzip the attached file in some directory, and from that directory run the command:

ghc -fglasgow-exts -fallow-undecidable-instances -fallow-overlapping-instances -fno-monomorphism-restriction -threaded -lKernel32 -lUser32 -lGdi32 -lOle32 -lComctl32 -optc-D_WIN32_IE=0x700 -optc-D_WIN32_WINNT=0x600 Win32Test.hs -fforce-recomp

There's a stub.c (renamed as stubBugBug.c) in the Win32 directory. There's another stub.c file in the root directory (Win32Test_stub.c). Both (the one in Win32 directory needs to be renamed back to stub.c, of course) will cause compile errors if present. Deleting them (or renaming them) will work around the issue. Both came from GHC 6.4.2.

Let me know if you have any trouble reproing this.

Changed 6 years ago by simonmar

  • owner igloo deleted
  • milestone changed from 6.8 branch to _|_

We should really fix, but it's not trivial, doesn't affect many people, and there's an easy workaround (just delete the file). So, _|_ for now.

Changed 5 years ago by simonmar

  • architecture changed from Multiple to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Multiple to Unknown/Multiple

Changed 4 years ago by simonmar

  • difficulty changed from Moderate (1 day) to Moderate (less than a day)

Changed 3 years ago by igloo

  • failure set to None/Unknown
  • milestone changed from _|_ to 6.14.1

Can't/shouldn't we just record whether or not there's a stub file in the .hi file?

Changed 3 years ago by simonpj

Simon and I looked briefly at this:

  • The mg_foreign field of a ModGuts records the foreign-stub information
  • It would be straightforward to add a Bool to ModIface to record whether there were stubs
  • The only tricky bit is making use of that ModIface field to construct the right hm_linkable list of Linkables, recorded in HomeModInfo.

So this is probably not hard to fix; just a bit fiddly to poke around and see what needs to be done. Any volunteers?

Simon

Changed 3 years ago by igloo

See also #3687

Changed 2 years ago by igloo

  • milestone changed from 7.0.1 to 7.0.2

Changed 2 years ago by simonmar

  • owner set to simonmar
  • priority changed from normal to high
  • blockedby 3687 added
  • milestone changed from 7.0.2 to 7.2.1

Changed 2 years ago by batterseapower

  • failure changed from None/Unknown to Compile-time crash

Changed 2 years ago by simonmar

  • status changed from new to closed
  • resolution set to fixed

Fixed: changeset:7b0ff1792d699ff02a604163c9ccf4a98a1ca3eb

Note: See TracTickets for help on using tickets.