LCOV - code coverage report
Current view: directory - src/trusted/service_runtime/linux - nacl_bootstrap_prereservation_test.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 34 5 14.7 %
Date: 2014-10-23 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 <errno.h>
       8                 : #include <stdio.h>
       9                 : #include <stdlib.h>
      10                 : #include <string.h>
      11                 : #include <sys/mman.h>
      12                 : 
      13                 : #include "native_client/src/include/nacl_assert.h"
      14                 : #include "native_client/src/shared/platform/nacl_check.h"
      15                 : #include "native_client/src/trusted/service_runtime/sel_addrspace.h"
      16                 : #include "native_client/src/trusted/service_runtime/sel_ldr.h"
      17                 : #include "native_client/src/trusted/service_runtime/sel_util.h"
      18                 : 
      19                 : /*
      20                 :  * Duplicates the definition in the nacl_bootstrap.x linker script.
      21                 :  */
      22                 : #define TEXT_START 0x10000
      23                 : 
      24               0 : int ParseMapsLine(FILE *fp, uintptr_t *start, uintptr_t *end) {
      25                 :   char buf[256];
      26                 : 
      27               0 :   if (!fgets(buf, sizeof(buf), fp)) {
      28               0 :     return 0;
      29                 :   }
      30                 : 
      31               0 :   if (sscanf(buf, "%"SCNxPTR"-%"SCNxPTR" %*4[-rwxp] %*8x %*2x:%*2x %*16x",
      32                 :              start, end) != 2) {
      33               0 :     return 0;
      34                 :   }
      35                 : 
      36               0 :   return 1;
      37                 : }
      38                 : 
      39               0 : void FindLowestMappedRange(uintptr_t *start, uintptr_t *end) {
      40                 :   FILE *fp;
      41                 :   uintptr_t next_start;
      42                 :   uintptr_t next_end;
      43                 : 
      44               0 :   fp = fopen("/proc/self/maps", "r");
      45               0 :   CHECK(fp != NULL);
      46                 : 
      47               0 :   CHECK(ParseMapsLine(fp, start, end));
      48               0 :   while (ParseMapsLine(fp, &next_start, &next_end)) {
      49               0 :     if (next_start != *end) {
      50               0 :       break;
      51                 :     }
      52               0 :     *end = next_end;
      53                 :   }
      54                 : 
      55               0 :   fclose(fp);
      56               0 : }
      57                 : 
      58               0 : int IsPageMappable(uintptr_t addr) {
      59               0 :   void *retval = mmap((void *) addr, NACL_PAGESIZE, PROT_NONE,
      60                 :                       MAP_PRIVATE | MAP_FIXED |
      61                 :                       MAP_ANONYMOUS | MAP_NORESERVE,
      62                 :                       -1, 0);
      63               0 :   if (retval != (void *) addr)   {
      64               0 :     CHECK(MAP_FAILED == retval &&
      65                 :           (EPERM == errno || EACCES == errno ||
      66                 :            (EINVAL == errno && 0 == addr)));
      67               0 :     return 0;
      68                 :   }
      69               0 :   return 1;
      70                 : }
      71                 : 
      72               0 : uintptr_t FindLowestMappableAddress(void) {
      73                 :   uintptr_t mmap_addr;
      74                 :   uintptr_t low_addr;
      75                 : 
      76                 :   /* Find lowest mappable address */
      77               0 :   for (mmap_addr = TEXT_START; mmap_addr > 0; mmap_addr -= NACL_PAGESIZE) {
      78               0 :     if (!IsPageMappable(mmap_addr - NACL_PAGESIZE)) {
      79               0 :       break;
      80                 :     }
      81                 :   }
      82                 :   /* Check that all lower addresses are unmappable */
      83               0 :   for (low_addr = mmap_addr; low_addr > 0; low_addr -= NACL_PAGESIZE) {
      84               0 :     CHECK(!IsPageMappable(low_addr - NACL_PAGESIZE));
      85                 :   }
      86               0 :   return mmap_addr;
      87                 : }
      88                 : 
      89                 : /*
      90                 :  * This test checks that nacl_helper_bootstrap loads a program at
      91                 :  * the correct location when prereserved sandbox memory is used.
      92                 :  * The amount of prereserved memory is passed via the reserved_at_zero
      93                 :  * command line option.
      94                 :  */
      95               1 : int main(int argc, char **argv) {
      96                 :   uintptr_t start;
      97                 :   uintptr_t end;
      98                 :   size_t lowest_mappable_addr;
      99                 : 
     100               1 :   NaClHandleBootstrapArgs(&argc, &argv);
     101                 : 
     102                 :   /* On Linux x86-64, there is no prereserved memory. */
     103                 :   if (NACL_ARCH(NACL_BUILD_ARCH) == NACL_x86 && NACL_BUILD_SUBARCH == 64) {
     104               1 :     printf("We expect there to be no prereserved sandbox memory\n");
     105               1 :     ASSERT_EQ(g_prereserved_sandbox_size, 0);
     106               1 :     return 0;
     107                 :   }
     108                 : 
     109                 :   /*
     110                 :    * Check if g_prereserved_sandbox_size is less than or equal to the
     111                 :    * first address which is not reserved in the address space.  If this
     112                 :    * is the case, then enough memory was reserved.
     113                 :    */
     114                 :   FindLowestMappedRange(&start, &end);
     115                 :   ASSERT_LE(g_prereserved_sandbox_size, end);
     116                 :   ASSERT_LE(start, TEXT_START);
     117                 :   lowest_mappable_addr = FindLowestMappableAddress();
     118                 :   ASSERT_EQ(start, lowest_mappable_addr);
     119                 :   printf("Found the right amount of prereserved memory\n");
     120                 :   return 0;
     121                 : }

Generated by: LCOV version 1.7