Opened 8 years ago

Last modified 3 months ago

#781 new bug

GHCi on x86_64, cannot link to static data in shared libs

Reported by: guest Owned by:
Priority: high Milestone: 7.8.3
Component: Compiler Version: 6.5
Keywords: getEnvironment Cc: pho@…, vandijk.roel@…, anton.nik@…, dterei, lewurm@…, jims@…
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: GHCi crash Difficulty: Unknown
Test Case: getEnvironment01 Blocked By: #3658
Blocking: Related Tickets:

Description

System.Posix.getEnvironment fails on Linux 2.6.16-1.2108_FC4smp
with a segfault. (x86_64)

[aleator@thoth HopenCV]$ ghci
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.5.20060527, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base-1.0 ... linking ... done.
Prelude> :m +System.Posix.Env
Prelude System.Posix.Env> getEnv "USER"
Loading package unix-1.0 ... linking ... done.
Just "aleator"
Prelude System.Posix.Env> getEnvironment
Segmentation fault

Change History (25)

comment:1 Changed 8 years ago by simonmar

  • Architecture changed from Unknown to x86_64 (amd64)
  • Operating System changed from Unknown to Linux
  • Summary changed from getEnvironment segfaults (6.5.20060527 - snapshot) to GHCi on x86_64, cannot link to static data in shared libs

I've fixed part of the problem (getEnvironment was broken), but this still won't work with GHCi on x86_64 due to another problem: for annoying reasons the dynamic linker doesn't support linking to static data in shared libraries, which the 'environ' variable in C is. So I'm leaving the bug open with a revised heading.

comment:2 Changed 8 years ago by igloo

  • Milestone set to 6.6.1
  • Test Case set to getEnvironment01

comment:3 Changed 7 years ago by simonmar

  • Milestone changed from 6.6.1 to 6.8

If we get shared libraries working, this one will be fixed.

comment:4 Changed 6 years ago by simonmar

  • Milestone changed from 6.8 branch to _|_

We've committed Wolfgang's patch to Linker.c that understands the PIC relocations on x86_64. This means that it is possible to compile the Haskell code using -fPIC to workaround this problem. Shared libraries will also make this less of an issue, as it will then only be a problem for modules rather than packages that are loaded into GHCi.

comment:5 Changed 5 years ago by simonmar

  • Milestone changed from _|_ to 6.12.1
  • Priority changed from normal to high

comment:6 Changed 5 years ago by simonmar

  • Milestone changed from 6.12.1 to 6.14.1

Let's look into this for 6.14.1.

comment:7 Changed 5 years ago by simonmar

Ok, to elaborate on this. The solution would be to compile everything with -fPIC on x86-64, ie. enable -fPIC by default. We have to measure the performance impact of doing that, which might not be a problem.

comment:8 Changed 4 years ago by simonmar

  • Type of failure set to GHCi crash

comment:9 Changed 4 years ago by PHO

  • Cc pho@… added

comment:10 follow-up: Changed 4 years ago by igloo

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

The session in the description now works, and getEnvironment01 passes all ways, on amd64/Linux.

comment:11 in reply to: ↑ 10 Changed 4 years ago by simonmar

  • Resolution fixed deleted
  • Status changed from closed to new

Replying to igloo:

The session in the description now works, and getEnvironment01 passes all ways, on amd64/Linux.

This bug definitely still exists. Perhaps this particular test case doesn't trigger it any more?

comment:12 follow-up: Changed 4 years ago by igloo

  • Owner set to simonmar

Simon, do you have a testcase for this?

comment:13 in reply to: ↑ 12 Changed 4 years ago by simonmar

Replying to igloo:

Simon, do you have a testcase for this?

~/scratch/781 > cat a.c
int a = 42;
~/scratch/781 > gcc -shared a.c -o a.so
~/scratch/781 > cat b.hs
{-# LANGUAGE Haskell2010 #-}

import Foreign
import Foreign.C

foreign import ccall "&a" pa :: Ptr CInt

main = peek pa >>= print
~/scratch/781 > ghc -c b.hs
compilation IS NOT required
~/scratch/781 > ghc --interactive b.hs `pwd`/a.so
GHCi, version 6.13.20100826: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading object (dynamic) /home/simonmar/scratch/781/a.so ... done
final link ... done
Ok, modules loaded: Main.
Prelude Main> main
-907777
Prelude Main> 

comment:14 Changed 4 years ago by simonmar

  • Blocked By 3658 added
  • Milestone changed from 7.0.1 to 7.2.1

To fix this we need to use -fPIC by default on x86_64, which is also what we need for #3658. So let's plan to do this in 7.2, when we've thought through the implications of moving to dynamic linking by default.

comment:15 Changed 3 years ago by Roel van Dijk

  • Cc vandijk.roel@… added

comment:16 Changed 3 years ago by lelf

  • Cc anton.nik@… added

comment:17 Changed 3 years ago by dterei

  • Cc dterei added

comment:18 Changed 2 years ago by simonmar

  • Milestone changed from 7.4.1 to 7.6.1

punting; we're not moving to dynamic linking by default for 7.4.1.

comment:19 Changed 2 years ago by lewurm

  • Cc lewurm@… added

comment:20 Changed 20 months ago by igloo

  • Milestone changed from 7.6.1 to 7.6.2

comment:21 Changed 18 months ago by simonmar

  • Milestone changed from 7.6.2 to 7.8.1

Moving this to the same milestone as #3658, the ticket it depends on.

One question I have: is this also an issue on Windows?

comment:22 Changed 17 months ago by igloo

Looks like it only affects Linux:

amd64/Linux:

Prelude Main> main
-907777

amd64/OSX:

Prelude Main> main
42

amd64/Windows:

Prelude Main> main
42

comment:23 Changed 17 months ago by simonmar

  • Owner simonmar deleted

comment:24 Changed 11 months ago by jimstutt

  • Cc jims@… added

comment:25 Changed 3 months ago by Ian Lynagh <igloo@…>

In d64c1b96fb7e6778c421fe4b2d5ee3a5cc433e98/ghc:

Add test getEnvironment01 for trac #781 (check getEnvironment doesn't break)
Note: See TracTickets for help on using tickets.