Skip to content

GNATCOLL.Mmap: Attempting to mmap character devices always results in zero-length immutable regions #97

@Tevo45

Description

@Tevo45

Attempting to use GNATCOLL.Mmap to interact with character devices ends up with the program always receiving a zero-length region pointing to Empty_String'Address, rather than any actual mapping, since character devices (such as those under /dev) are reported as being zero in length. GNATCOLL clamps mapping lengths and offsets to the reported file bounds, and attempts to sidestep cases in which mmap would return either failure or a NULL mapping, but I figure it might be simpler to just attempt the map straight-away and then react to failure as appropriate.

Consider the following:

with GNATCOLL.Mmap; use GNATCOLL.Mmap;
with Ada.Text_IO;   use Ada.Text_IO;
with System;

procedure GNATCOLL_Bug is
   File    : Mapped_File    := Open_Write ("/dev/zero");
   Region  : Mapped_Region  := Read (File, Length => 128, Mutable => True);
   Address : System.Address := Data_Address (Region);

   type My_Data is array (0 .. 127) of Character
     with Component_Size => 8;
   Data : My_Data
     with Address => Address;
begin
   Put_Line (Data_Size (Region)'Image);
   Data (64) := 'Y'; -- oops!
end GNATCOLL_Bug;

One might expect Data to point to a 128-byte region of memory mapped from the device at /dev/zero (we can pretend it is something more useful), and that writing into it would replicate the write to the device itself, as it would have been had we been using raw mmap. Instead, Data points to an arbitrary chunk of read-only memory starting at Empty_String'Address, and writing to it would get a STORAGE_ERROR raised, or worse.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions