Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:06:38+00:00 2026-06-03T07:06:38+00:00

Toady I updated my developing machine from Ubuntu 10.04 LTS to Ubuntu 12.04 LTS

  • 0

Toady I updated my developing machine from Ubuntu 10.04 LTS to Ubuntu 12.04 LTS (or ghc 6.12.1 to ghc 7.4.1) and I run into a very strange behavior at my currenct project.

After some hours, I reduced it to the following code:

{-# LANGUAGE ForeignFunctionInterface #-}
module Main where

import Data.Word
import Text.Printf
import Foreign

foreign import ccall "dynamic"
   code_void :: FunPtr (IO ()) -> (IO ())

main :: IO ()
main = do
  entryPtr <- (mallocBytes 2)
  poke entryPtr (0xc390 :: Word16) -- nop (0x90); ret(0xc3) (little endian order)

  _ <- printf "entry point: 0x%08x\n" ((fromIntegral $ ptrToIntPtr entryPtr) :: Int)
  _ <- getLine -- for debugging
  code_void $ castPtrToFunPtr entryPtr
  putStrLn "welcome back"

I’m trying to generate some code at run-time, jump to it, and come back again.
Using a Makefile, everything is fine:

$ make 
ghc --make -Wall -O2 Main.hs -o stackoverflow_segv
[1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking stackoverflow_segv ...
./stackoverflow_segv
entry point: 0x098d77e0

welcome back

However, if I call the binary directly from the shell:

$ ./stackoverflow_segv 
entry point: 0x092547e0

Segmentation fault (core dumped)

This behavior is reproducible (luckily?).

Using gdb, objdump and /proc I figured out:

$ gdb -q stackoverflow_segv
Reading symbols from /home/lewurm/stackoverflow/stackoverflow_segv...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/lewurm/stackoverflow/stackoverflow_segv
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
entry point: 0x080fc810

before pressing enter, I switch to a second terminal:

$ cat /proc/`pgrep stackoverflow`/maps
[...]
08048000-080ea000 r-xp 00000000 08:01 2492678    /home/lewurm/stackoverflow/stackoverflow_segv
080ea000-080eb000 r--p 000a2000 08:01 2492678    /home/lewurm/stackoverflow/stackoverflow_segv
080eb000-080f1000 rw-p 000a3000 08:01 2492678    /home/lewurm/stackoverflow/stackoverflow_segv
080f1000-08115000 rw-p 00000000 00:00 0          [heap]
[...]

and back again:

<enter>
Program received signal SIGSEGV, Segmentation fault.
0x0804ce3c in s2aV_info ()

Boo. Let’s see what this code does:

$ objdump -D stackoverflow_segv | grep -C 3 804ce3c
 804ce31:       89 44 24 4c             mov    %eax,0x4c(%esp)
 804ce35:       83 ec 0c                sub    $0xc,%esp
 804ce38:       8b 44 24 4c             mov    0x4c(%esp),%eax
 804ce3c:       ff d0                   call   *%eax
 804ce3e:       83 c4 0c                add    $0xc,%esp
 804ce41:       83 ec 08                sub    $0x8,%esp
 804ce44:       8b 44 24 54             mov    0x54(%esp),%eax

uhm, jumping to *%eax. What was %eax again?

 (gdb) info reg eax
 eax            0x80fc810        135251984

Well, actually it’s just the code buffer. Looking up /proc/*/maps tells us, that this page isn’t executeable (rw-p, right?). But, it’s the same situation when executing it within make.

What is wrong here?

btw, the code is also available via gist

edit: ghc bug report

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:06:39+00:00Added an answer on June 3, 2026 at 7:06 am

    A temporary solution is to use mprotect(3) and set the memory region explicitly as executable. mprotect(3) requires a aligned memory block, therefore memalign(3) is required.

    {-# LANGUAGE ForeignFunctionInterface #-}
    module Main where
    
    import Data.Word
    import Text.Printf
    import Foreign
    import Foreign.C.Types
    
    foreign import ccall "dynamic"
       code_void :: FunPtr (IO ()) -> (IO ())
    
    foreign import ccall "static sys/mman.h"
      mprotect :: CUInt -> CUInt -> Int -> IO ()
    
    foreign import ccall "static stdlib.h"
      memalign :: CUInt -> CUInt -> IO (Ptr a)
    
    
    main :: IO ()
    main = do
      entryPtr <- memalign 0x1000 0x2
      poke entryPtr (0xc390 :: Word16) -- nop (0x90); ret(0xc3) (little endian order)
      let i_entry = (fromIntegral $ ptrToIntPtr entryPtr) :: Int
      -- 0x7 = PROT_{READ,WRITE,EXEC}
      mprotect (fromIntegral i_entry) 2 0x7
    
      _ <- printf "entry point: 0x%08x\n" i_entry
      _ <- getLine -- for debugging
      code_void $ castPtrToFunPtr entryPtr
      putStrLn "welcome back"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently updated my VS2010 website project from .NET 3.5 to 4.0. Everything was
Today I updated MinGW and rebuilt my current C++-project. I've logged timing-info since using
Okay, so today I updated my database with new information from our 'live' database...
I've got some problem about bash. Before today, my VPS (Ubuntu 12.04 LTS) was
I updated ghc today and my small program stopped working: https://github.com/BrisFunctional/misere-oxo/blob/master/OXO/misere.hs GHC is now
today I updated my Mongo.. mongodb-stable (from 10gen repo) but my service has down.
I am developing a wizard for a machine that is to be used as
I am actually running phusion passenger on ubuntu for a while. today i updated
I work in PyDev and quite suddenly, I cannot run my python programs from
since I updated my Android SDK today I get some strange Lint errors in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.