LCOV - code coverage report
Current view: directory - src/trusted/service_runtime/linux - mmap_test_check.cc (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 30 28 93.3 %
Date: 2014-07-02 Functions: 0 0 -

       1                 : /*
       2                 :  * Copyright (c) 2012 The Native Client Authors. All rights reserved.
       3                 :  * Use of this source code is governed by a BSD-style license that can be
       4                 :  * found in the LICENSE file.
       5                 :  */
       6                 : 
       7                 : #include "native_client/src/include/nacl_assert.h"
       8                 : #include "native_client/src/trusted/service_runtime/mmap_test_check.h"
       9                 : 
      10                 : #include <stdio.h>
      11                 : #include <stdlib.h>
      12                 : #include <sys/mman.h>
      13                 : 
      14                 : #define MAX_INPUT_LINE_LENGTH   4096
      15                 : 
      16              12 : void CheckMapping(uintptr_t addr, size_t size, int protect, int map_type) {
      17              12 :   const char *maps_path = "/proc/self/maps";
      18                 :   FILE *maps_file;
      19                 :   char buf[MAX_INPUT_LINE_LENGTH];
      20                 :   uint64_t start;
      21                 :   uint64_t end;
      22                 :   int64_t inode;
      23                 :   char flags[5];
      24              12 :   int prot = 0;
      25              12 :   int type = 0;
      26                 :   int num;
      27              12 :   int found = 0;
      28                 : 
      29                 :   // Open /proc/self/maps to read process memory mapping
      30              12 :   maps_file = fopen(maps_path, "r");
      31              12 :   ASSERT(maps_file != NULL);
      32                 : 
      33               9 :   for (;;) {
      34                 :     // Read each line from /proc/self/maps file
      35              21 :     if (!fgets(buf, sizeof(buf), maps_file)) {
      36               0 :       break;
      37                 :     }
      38                 :     // Parse the line
      39              21 :     if (3 < sscanf(buf, "%" SCNx64 "-%" SCNx64 " %4s %*d %*x:%*x %" SCNd64
      40              21 :                    " %n", &start, &end, flags, &inode, &num)) {
      41                 :       // The maps file will merge regions with the same attributes.  Thus,
      42                 :       // we allow the starting address to be before the expected starting
      43                 :       // address, and we allow the ending address to be after the expected
      44                 :       // end address (addr + size).
      45              21 :       if (start <= addr && end >= addr + size) {
      46              12 :         if (flags[0] == 'r')
      47               6 :           prot |= PROT_READ;
      48              12 :         if (flags[1] == 'w')
      49               6 :           prot |= PROT_WRITE;
      50              12 :         if (flags[2] == 'x')
      51               0 :           prot |= PROT_EXEC;
      52                 : 
      53              12 :         if (flags[3] == 'p')
      54               6 :           type |= MAP_PRIVATE;
      55               6 :         else if (flags[3] == 's')
      56               6 :           type |= MAP_SHARED;
      57                 : 
      58              12 :         found = 1;
      59              12 :         break;
      60                 :       }
      61                 :     }
      62                 :   }
      63                 : 
      64              12 :   ASSERT(found);
      65              12 :   ASSERT_EQ(prot, protect);
      66              12 :   ASSERT_EQ(type, map_type);
      67              12 :   ASSERT_EQ(fclose(maps_file), 0);
      68              12 : }

Generated by: LCOV version 1.7