Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tevo45 opened this issue Dec 26, 2024 · 0 comments

Comments

@Tevo45
Copy link

Tevo45 commented Dec 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant