Opened 14 years ago

Closed 9 years ago

#706 closed bug (fixed)

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 Test Case:
Blocked By: #3687 Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

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 (1)

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

Download all attachments as: .zip

Change History (20)

comment:1 Changed 14 years ago by simonmar

Architecture: powerpcMultiple
Operating System: MacOS XMultiple

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.

comment:2 Changed 14 years ago by simonmar

difficulty: UnknownEasy (1 hr)

comment:3 Changed 14 years ago by simonmar

Milestone: 6.6
Owner: set to simonmar

comment:4 Changed 13 years ago by simonmar

Milestone: 6.66.6.1

comment:5 Changed 13 years ago by simonpj

difficulty: Easy (1 hr)Moderate (1 day)
Owner: changed from simonmar to igloo

comment:6 Changed 13 years ago by simonpj

Milestone: 6.6.16.8

comment:7 Changed 13 years ago by jcab@…

Summary: GHC links _stub.o files regardless of whether any 'foreign import' decls remain in a .hs fileGHC 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 13 years ago by jcab@…

Attachment: Win32TestStubBug.zip added

Repro for the issue with stub.c files

comment:8 Changed 13 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.

comment:9 Changed 12 years ago by simonmar

Milestone: 6.8 branch_|_
Owner: igloo deleted

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.

comment:10 Changed 11 years ago by simonmar

Architecture: MultipleUnknown/Multiple

comment:11 Changed 11 years ago by simonmar

Operating System: MultipleUnknown/Multiple

comment:12 Changed 10 years ago by simonmar

difficulty: Moderate (1 day)Moderate (less than a day)

comment:13 Changed 10 years ago by igloo

Milestone: _|_6.14.1
Type of failure: None/Unknown

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

comment:14 Changed 10 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

comment:15 Changed 10 years ago by igloo

See also #3687

comment:16 Changed 9 years ago by igloo

Milestone: 7.0.17.0.2

comment:17 Changed 9 years ago by simonmar

Blocked By: 3687 added
Milestone: 7.0.27.2.1
Owner: set to simonmar
Priority: normalhigh

comment:18 Changed 9 years ago by batterseapower

Type of failure: None/UnknownCompile-time crash

comment:19 Changed 9 years ago by simonmar

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.