Opened 2 years ago

Closed 2 years ago

#10462 closed bug (fixed)

GHCi doesn't work Any and missing RealWorld foreign prim imports

Reported by: ezyang Owned by:
Priority: low Milestone: 8.0.1
Component: Compiler (CodeGen) Version: 7.10.1
Keywords: Cc: hvr, hsyl20
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Compile-time crash Test Case: prog014
Blocked By: Blocking:
Related Tickets: Differential Rev(s): D1458
Wiki Page:

Description

Here are two issues with our GHCi support for foreign prim imports:

Here is a program that works:

{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes, UnboxedTuples #-}
module Serum where
import GHC.Exts

foreign import prim "cheneycopy" cheneycopy :: Word# -> State# RealWorld -> (# State# RealWorld, Word# #)

If I remove the world token passing, as in here:

{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes, UnboxedTuples #-}
module Serum where
import GHC.Exts

foreign import prim "cheneycopy" cheneycopy :: Word# -> Word#

I get:

  (GHC version 7.10.1 for x86_64-unknown-linux):
	ByteCodeGen.generateCCall: missing or invalid World token?

Another error is if I try to pass Any as an argument:

{-# LANGUAGE GHCForeignImportPrim, MagicHash, UnliftedFFITypes, UnboxedTuples #-}
module Serum where
import GHC.Exts

foreign import prim "cheneycopy" cheneycopy :: Any -> State# RealWorld -> (# State# RealWorld, Word# #)

Then I get:

ghc: panic! (the 'impossible' happened)
  (GHC version 7.10.1 for x86_64-unknown-linux):
	primRepToFFIType

Note to anyone who is running into this problem: an easy workaround is to use -fobject-code which bypasses bytecode generation.

Attachments (1)

0001-ghci-indicate-that-foreign-primops-are-not-supported.patch (1.9 KB) - added by hsyl20 2 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 2 years ago by ezyang

Priority: normallow

comment:2 Changed 2 years ago by hsyl20

Cc: hsyl20 added

comment:3 Changed 2 years ago by hsyl20

I don't think your first example works as expected. It type checks but the calling convention is silently ignored and replaced by the default CCall one (see #3336 and "convToABI" in compiler/ghci/LibFFI.hsc). The other examples fail because "generateCCall" from compiler/ghci/ByteCodeGen.hs shouldn't be called at all for primops and there are errors when parameters are pushed (your first case) or when parameters are converted into types understandable by libffi (second case).

I attach a patch with an additional check which fails sooner and in a nicer way (similarily to #1257).

comment:4 Changed 2 years ago by hsyl20

Status: newpatch

comment:5 Changed 2 years ago by hsyl20

Milestone: 8.0.1

comment:6 Changed 2 years ago by hsyl20

Component: GHCiCompiler (CodeGen)
Differential Rev(s): D1458
Test Case: prog014

comment:7 Changed 2 years ago by Ben Gamari <ben@…>

In badf5d5/ghc:

Detect invalid foreign imports in bytecode compiler

The bytecode compiler doesn't handle every foreign import calling
convention. Instead of crashing during the generation of the foreign
call, we display an error.

Fix lint warnings

Test Plan: prog014 ghci test added

Reviewers: austin, hvr, bgamari

Reviewed By: bgamari

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D1458

GHC Trac Issues: #10462

comment:8 Changed 2 years ago by ezyang

Resolution: fixed
Status: patchclosed

Marking this as fixed, I guess...

Note: See TracTickets for help on using tickets.