Skip to content

Do the bus a bus

The bus error is gone but I’m not really comfortable with the reason why I don’t have that error anymore. Let’s explain a bit:

mmap() is a 6 argument function, all of them required.

Thomas Heller discovered under FreeBSD 6.0 with ktrace that it’s called with 8 parameters, not 6. The two argument in addition are all 0 if called with the 6-arg mmap from plain C.

If you call mmap() from ctypes the 7th parameter is zeroed, the 8th is a random hexadecimal. So it doesn’t work, because C calls it with 0.

A FreeBSD committer friend of mine told me instead that the syscall under mmap() is a 7 argument function, not 8.

Thomas Heller tried with 8 parameters and it worked. I, accordingly to my friend, tried with 7 instead and it worked well anyway.

So the mistery is still here but the error is gone.

You can discover odd things also under Unix, not only under Win32 :-D

Eventually, this is the correct code:

[code lang="python"] from ctypes import * import ctypes.util

_libc = cdll.LoadLibrary(ctypes.util.find_library("c"))

f = open("foo", "w+") f.write("foo\0") f.flush()

_libc.mmap.restype = c_void_p m = _libc.mmap(0, 4, 3, 1, f.fileno(), 0, 0) print m if m != c_void_p(-1).value: c = cast(m, POINTER(c_char)) print [c[i] for i in range(4)] f.close() [/code]

ps. the title is taken from a Busta Rhymes old song ;-)

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • Google Buzz
  • HackerNews
  • Posterous
  • Reddit
  • Slashdot
  • Tumblr
  • Anton

    The last parameter to mmap is of type off_t which is a 64 bit integer. I haven’t checked how ctypes works but it would not surprise me if the parameters you give to a function are passed as 32 bit units to the respective C function. I.e. to effectively pass the off_t argument as zero to mmap you have to supply an extra zero argument.