OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <errno.h> | 5 #include <errno.h> |
6 #include <sys/mman.h> | 6 #include <sys/mman.h> |
7 | 7 |
8 #include "base/logging.h" | |
8 #include "components/nacl/loader/nonsfi/irt_interfaces.h" | 9 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
9 #include "components/nacl/loader/nonsfi/irt_util.h" | 10 #include "components/nacl/loader/nonsfi/irt_util.h" |
10 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" | 11 #include "native_client/src/trusted/service_runtime/include/machine/_types.h" |
11 #include "native_client/src/trusted/service_runtime/include/sys/mman.h" | 12 #include "native_client/src/trusted/service_runtime/include/sys/mman.h" |
12 | 13 |
13 namespace nacl { | 14 namespace nacl { |
14 namespace nonsfi { | 15 namespace nonsfi { |
15 namespace { | 16 namespace { |
16 | 17 |
17 int NaClProtToProt(int nacl_prot) { | 18 int NaClProtToProt(int nacl_prot) { |
(...skipping 21 matching lines...) Expand all Loading... | |
39 flags |= MAP_FIXED; | 40 flags |= MAP_FIXED; |
40 | 41 |
41 // Note: NACL_ABI_MAP_ANON is an alias of NACL_ABI_MAP_ANONYMOUS. | 42 // Note: NACL_ABI_MAP_ANON is an alias of NACL_ABI_MAP_ANONYMOUS. |
42 if (nacl_flags & NACL_ABI_MAP_ANONYMOUS) | 43 if (nacl_flags & NACL_ABI_MAP_ANONYMOUS) |
43 flags |= MAP_ANONYMOUS; | 44 flags |= MAP_ANONYMOUS; |
44 return flags; | 45 return flags; |
45 } | 46 } |
46 | 47 |
47 int IrtMMap(void** addr, size_t len, int prot, int flags, | 48 int IrtMMap(void** addr, size_t len, int prot, int flags, |
48 int fd, nacl_abi_off_t off) { | 49 int fd, nacl_abi_off_t off) { |
49 void* result = | 50 const int host_prot = NaClProtToProt(prot); |
50 mmap(*addr, len, NaClProtToProt(prot), NaClFlagsToFlags(flags), fd, off); | 51 // On Chrome OS, mmap will fail if PROT_EXEC is set in |host_prot|, |
Mark Seaborn
2014/04/15 23:28:03
Nit: "can fail"
mazda
2014/04/16 00:33:36
Done.
| |
52 // but mprotect will allow changing the permissions later. | |
53 // See the comments for NaClHostDescMap in | |
Mark Seaborn
2014/04/15 23:28:03
X-refs in comments are a little bit of an anti-pat
mazda
2014/04/16 00:33:36
Done.
| |
54 // native_client/src/shared/platform/posix/nacl_host_desc.c for details. | |
55 void* result = mmap( | |
56 *addr, len, host_prot & ~PROT_EXEC, NaClFlagsToFlags(flags), fd, off); | |
51 if (result == MAP_FAILED) | 57 if (result == MAP_FAILED) |
52 return errno; | 58 return errno; |
59 if (host_prot & PROT_EXEC) { | |
60 if (mprotect(result, len, host_prot) != 0) { | |
61 LOG(FATAL) << "IrtMMap: mprotect to turn on PROT_EXEC failed, " | |
Mark Seaborn
2014/04/15 23:28:03
I was going to suggest doing "return errno" here,
mazda
2014/04/16 00:33:36
Done.
| |
62 << "errno " << errno; | |
Mark Seaborn
2014/04/15 23:28:03
It's better to use LOG_ERRNO or PLOG -- they repor
mazda
2014/04/16 00:33:36
Done.
| |
63 } | |
64 } | |
53 | 65 |
54 *addr = result; | 66 *addr = result; |
55 return 0; | 67 return 0; |
56 } | 68 } |
57 | 69 |
58 int IrtMUnmap(void* addr, size_t len) { | 70 int IrtMUnmap(void* addr, size_t len) { |
59 return CheckError(munmap(addr, len)); | 71 return CheckError(munmap(addr, len)); |
60 } | 72 } |
61 | 73 |
62 int IrtMProtect(void* addr, size_t len, int prot) { | 74 int IrtMProtect(void* addr, size_t len, int prot) { |
63 return CheckError(mprotect(addr, len, NaClProtToProt(prot))); | 75 return CheckError(mprotect(addr, len, NaClProtToProt(prot))); |
64 } | 76 } |
65 | 77 |
66 } // namespace | 78 } // namespace |
67 | 79 |
68 // For mmap, the argument types should be nacl_abi_off_t rather than off_t. | 80 // For mmap, the argument types should be nacl_abi_off_t rather than off_t. |
69 // However, the definition of nacl_irt_memory uses the host type off_t, so here | 81 // However, the definition of nacl_irt_memory uses the host type off_t, so here |
70 // we need to cast it. | 82 // we need to cast it. |
71 const nacl_irt_memory kIrtMemory = { | 83 const nacl_irt_memory kIrtMemory = { |
72 reinterpret_cast<int(*)(void**, size_t, int, int, int, off_t)>(IrtMMap), | 84 reinterpret_cast<int(*)(void**, size_t, int, int, int, off_t)>(IrtMMap), |
73 IrtMUnmap, | 85 IrtMUnmap, |
74 IrtMProtect, | 86 IrtMProtect, |
75 }; | 87 }; |
76 | 88 |
77 } // namespace nonsfi | 89 } // namespace nonsfi |
78 } // namespace nacl | 90 } // namespace nacl |
OLD | NEW |