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 14 : void CheckMapping(uintptr_t addr, size_t size, int protect, int map_type) {
17 14 : 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 14 : int prot = 0;
25 14 : int type = 0;
26 : int num;
27 14 : int found = 0;
28 :
29 : // Open /proc/self/maps to read process memory mapping
30 14 : maps_file = fopen(maps_path, "r");
31 14 : ASSERT(maps_file != NULL);
32 :
33 30 : for (;;) {
34 : // Read each line from /proc/self/maps file
35 44 : if (!fgets(buf, sizeof(buf), maps_file)) {
36 0 : break;
37 : }
38 : // Parse the line
39 44 : if (3 < sscanf(buf, "%" SCNx64 "-%" SCNx64 " %4s %*d %*x:%*x %" SCNd64
40 44 : " %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 44 : if (start <= addr && end >= addr + size) {
46 14 : if (flags[0] == 'r')
47 6 : prot |= PROT_READ;
48 14 : if (flags[1] == 'w')
49 6 : prot |= PROT_WRITE;
50 14 : if (flags[2] == 'x')
51 0 : prot |= PROT_EXEC;
52 :
53 14 : if (flags[3] == 'p')
54 8 : type |= MAP_PRIVATE;
55 6 : else if (flags[3] == 's')
56 6 : type |= MAP_SHARED;
57 :
58 14 : found = 1;
59 14 : break;
60 : }
61 : }
62 : }
63 :
64 14 : ASSERT(found);
65 14 : ASSERT_EQ(prot, protect);
66 14 : ASSERT_EQ(type, map_type);
67 14 : ASSERT_EQ(fclose(maps_file), 0);
68 14 : }
|