Menu

#166 failing to mmap() causes segfault due to wrong cast

Done
None
Low
Patch
2019-07-21
2019-03-21
No

Hi,

In the setup() function, you use:

        gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0);
        if ((uint32_t)gpio_map < 0) {
            return SETUP_MMAP_FAIL;
        } else {
            return SETUP_OK;
        }

The if condition can never be true because of the cast. You want (int32_t), not (uint32_t).

There's the same problem later on, when opening /dev/mem.

I ran into this when trying to run RPi.GPIO on a stock Debian kernel, that ships with CONFIG_DEVMEM_STRICT=y, and thus requires iomem=relaxed to mmap() like that.

Discussion

  • Ben Croston

    Ben Croston - 2019-03-24

    I've changed the if condition in the code. Does it still need iomem=relaxed?
    You can get the development version to test it:
    $ sudo apt install mercurial
    $ sudo pip3 install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO

     
  • Lucas Nussbaum

    Lucas Nussbaum - 2019-03-25

    Hi,

    I confirm that the fix works. Thanks!

    Yes, it still needs iomem=relaxed. Without it, it fails with:

    Traceback (most recent call last):
      File "t3.py", line 5, in <module>
        gpio.setup(18, gpio.OUT)
    RuntimeError: Mmap of GPIO registers failed
    

    But that's expected: the Debian image doesn't have /dev/gpiomem, so the restrictions set by CONFIG_DEVMEM_STRICT=y apply.

    Maybe a note about that should be added to the README.

     
  • Ben Croston

    Ben Croston - 2019-07-21
    • status: New --> Done
    • assigned_to: Ben Croston
     
  • Ben Croston

    Ben Croston - 2019-07-21

    In release 0.7.0

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.