Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#1357 closed feature request (fixed)

warning for possible missing & in foreign import

Reported by: Frederik Owned by:
Priority: normal Milestone: 6.10 branch
Component: Compiler Version: 6.6
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description (last modified by igloo)

I am attaching files to hopefully reproduce a problem I am having. I have not upgraded to 6.6.1 yet so let me know if it has been fixed.

$ touch fpenv_c.c fpenv.hs && cc -g -Wall -c fpenv_c.c && ghc -fasm --make fpenv.hs fpenv_c.o -lc
[1 of 1] Compiling Main             ( fpenv.hs, fpenv.o )
Linking fpenv ...
$ ./fpenv
(x,xFP)=(0x08090628,0x08090628)
fin_test: env=0xa7900040, p=0x808e00c
zsh: segmentation fault  ./fpenv
[139]$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.6.20070420

Running in valgrind shows:

...
fin_test: env=0x437c008, p=0x1
==12777==
==12777== Jump to the invalid address stated on the next line
==12777==    at 0x1F: ???
==12777==  Address 0x1F is not stack'd, malloc'd or (recently) free'd

I think this is when fin_test exits. I am experiencing this problem in a much larger program, so I wrote this test case to figure out what is going wrong. Am I using the newForeignPtrEnv API incorrectly?

Attachments (2)

fpenv_c.c (113 bytes) - added by Frederik 8 years ago.
fpenv.hs (527 bytes) - added by Frederik 8 years ago.

Download all attachments as: .zip

Change History (12)

Changed 8 years ago by Frederik

Changed 8 years ago by Frederik

comment:1 Changed 8 years ago by Frederik

  • Priority changed from normal to high

comment:2 Changed 8 years ago by Frederik

Any word on this? The code I have posted is the simplest example I can think of involving newForeignPtrEnv, and it segmentation faults on my system; either the function doesn't work at all, or I am using it wrongly, or this bug has been fixed in 6.6.1 - does anyone know the which it is? Thanks in advance...

comment:3 Changed 8 years ago by igloo

  • Description modified (diff)

comment:4 Changed 8 years ago by igloo

  • Resolution set to invalid
  • Status changed from new to closed

You need

foreign import ccall "null.h &fin_test" finalizerTest :: FunPtr (Ptr a -> Ptr a -> IO ())

(note the ampersand). If you don't already, you also ought to have a header file null.h with the C prototype for fin_test in.

Thanks

Ian

comment:5 Changed 8 years ago by Frederik

Thank you for catching that mistake!

Perhaps I was confused since 'f' and '&f' have the same meaning with most C compilers, I will remember that GHC treats them differently.

comment:6 Changed 8 years ago by simonmar

  • difficulty changed from Unknown to Easy (1 hr)
  • Milestone set to 6.1
  • Priority changed from high to normal
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Summary changed from trouble using newForeignPtrEnv to warning for possible missing & in foreign import
  • Type changed from bug to feature request

It's an easy mistake to make - I remember doing exactly the same thing myself once, and having to debug it using gdb.

Perhaps we should have a warning for

foreign import ccall "f" f :: FunPtr a

on the grounds that you probably (but not necessarily) wanted "&f".

comment:7 Changed 7 years ago by simonmar

  • Resolution set to fixed
  • Status changed from reopened to closed

Warning added, and documented:

Wed Jul  9 11:21:43 BST 2008  Simon Marlow <[email protected]>
  * add -fwarn-dodgy-foreign-imports (see #1357)

comment:8 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:9 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple

comment:10 Changed 5 years ago by simonmar

  • difficulty changed from Easy (1 hr) to Easy (less than 1 hour)
Note: See TracTickets for help on using tickets.