AdaCore / AdaCore/gnatcoll-core

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

Open
#97 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ada
Stars
55
Forks
36
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the GNATCOLL_Bug example with GNATCOLL.Mmap and /dev/zero, then inspect how Read handles reported zero-length character devices. Compare the resulting region and failure behavior with the issue's raw mmap expectation. Done means a requested mapping is not replaced by a zero-length immutable region and mapping failures remain handled appropriately.

Written by the indexing model from the issue text.

Assessment

Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.