libprocinfo: Introduce MappedFileSize()

A file memory mapping can be created such that it is only partially
backed by the underlying file. i.e. the mapping size is larger than
the file size.

On builds that support larger than 4KB page-size, the common assumption
that a file mapping is entirely backed by the underlying file, is
more likely to be false.

If an access to a region of the mapping beyond the end of the file
occurs, there are 2 situations:
    1) The access is between the end of the file and the next page
       boundary. The kernel will facilitate this although there is
       no file here.
       Note: That writing this region does not persist any data to
       the actual backing file.
    2) The access is beyond the first page boundary after the end
       of the file. This will cause a filemap_fault which does not
       correspond to a valid file offset and the kernel will return
       a SIGBUS. [1]

Userspace programs that parse /proc/<pid>/maps or /proc/<pid>/smaps
to determine the extent of memory mappings which they then use as
arguments to other syscalls or directly access; should be aware of
the second case above (2) and not assume that file mappings are
entirely back by the underlying file.

This is especially important for operations that would cause a
page-fault on the range described in (2). In this case userspace
should either handle the signal or use the range backed by the
underlying file for the desired operation.

Introduce MappedFileSize() to deduce the valid range/size of
the VMA that is backed the underlying file.

Add MappedFileSize(MapInfo...) as a convenience method for use
with procinfo::MapInfo.

[1] SIGBUS at https://man7.org/linux/man-pages/man2/mmap.2.html#RETURN_VALUE
Test: atest -c libprocinfo_test
Test: Incremental OTA
Bug: 324952273
Change-Id: Ifb746ffd771e3d62b663163a7db204493200ecd2
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
2 files changed
tree: a354b02db4ae73dc67c8f71b85ddcdef9bc24d27
  1. include/
  2. testdata/
  3. Android.bp
  4. OWNERS
  5. process.cpp
  6. process_map_benchmark.cpp
  7. process_map_test.cpp
  8. process_test.cpp
  9. TEST_MAPPING