| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 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 | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 #include <sys/mman.h> | 10 #include <sys/mman.h> |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 } | 60 } |
| 61 CHECK(close(pipe_fds[0]) == 0); | 61 CHECK(close(pipe_fds[0]) == 0); |
| 62 CHECK(close(pipe_fds[1]) == 0); | 62 CHECK(close(pipe_fds[1]) == 0); |
| 63 return success; | 63 return success; |
| 64 } | 64 } |
| 65 | 65 |
| 66 bool IPlatform::GetMemory(uint64_t virt, uint32_t len, void *dst) { | 66 bool IPlatform::GetMemory(uint64_t virt, uint32_t len, void *dst) { |
| 67 return SafeMemoryCopy(dst, reinterpret_cast<void*>(virt), len); | 67 return SafeMemoryCopy(dst, reinterpret_cast<void*>(virt), len); |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool IPlatform::SetMemory(uint64_t virt, uint32_t len, void *src) { | 70 bool IPlatform::SetMemory(struct NaClApp *nap, uint64_t virt, uint32_t len, |
| 71 void *src) { |
| 71 uintptr_t page_mask = NACL_PAGESIZE - 1; | 72 uintptr_t page_mask = NACL_PAGESIZE - 1; |
| 72 uintptr_t page = virt & ~page_mask; | 73 uintptr_t page = virt & ~page_mask; |
| 73 uintptr_t mapping_size = ((virt + len + page_mask) & ~page_mask) - page; | 74 uintptr_t mapping_size = ((virt + len + page_mask) & ~page_mask) - page; |
| 74 if (mprotect(reinterpret_cast<void*>(page), mapping_size, | 75 bool is_code = virt + len <= nap->mem_start + nap->dynamic_text_end; |
| 75 PROT_READ | PROT_WRITE) != 0) { | 76 if (is_code) { |
| 76 return false; | 77 if (mprotect(reinterpret_cast<void*>(page), mapping_size, |
| 78 PROT_READ | PROT_WRITE) != 0) { |
| 79 return false; |
| 80 } |
| 77 } | 81 } |
| 78 bool succeeded = SafeMemoryCopy(reinterpret_cast<void*>(virt), src, len); | 82 bool succeeded = SafeMemoryCopy(reinterpret_cast<void*>(virt), src, len); |
| 79 // TODO(mseaborn): We assume here that SetMemory() is being used to | 83 // We use mprotect() only to modify code area, so PROT_READ | PROT_EXEC are |
| 80 // set or remove a breakpoint in the code area, so that PROT_READ | | 84 // the correct flags to restore the mapping to in most cases. However, this |
| 81 // PROT_EXEC are the correct flags to restore the mapping to. | 85 // does not behave correctly for non-allocated pages in code area (where |
| 82 // The earlier mprotect() does not tell us what the original flags | 86 // we will make zeroed bytes executable) and zero page (where we additionally |
| 83 // were. To find this out we could either: | 87 // prevent some null pointer exceptions). |
| 84 // * read /proc/self/maps (not available inside outer sandbox); or | 88 // |
| 85 // * use service_runtime's own mapping tables. | 89 // TODO(mseaborn): Handle those cases correctly. We might do that by modifying |
| 86 // Alternatively, we could modify code the same way nacl_text.c does. | 90 // code via the dynamic code area the same way nacl_text.c does. |
| 87 if (mprotect(reinterpret_cast<void*>(page), mapping_size, | 91 if (is_code) { |
| 88 PROT_READ | PROT_EXEC) != 0) { | 92 if (mprotect(reinterpret_cast<void*>(page), mapping_size, |
| 89 return false; | 93 PROT_READ | PROT_EXEC) != 0) { |
| 94 return false; |
| 95 } |
| 90 } | 96 } |
| 91 // Flush the instruction cache in case we just modified code to add | 97 // Flush the instruction cache in case we just modified code to add |
| 92 // or remove a breakpoint, otherwise breakpoints will not behave | 98 // or remove a breakpoint, otherwise breakpoints will not behave |
| 93 // reliably on ARM. | 99 // reliably on ARM. |
| 94 NaClFlushCacheForDoublyMappedCode((uint8_t *) virt, (uint8_t *) virt, len); | 100 NaClFlushCacheForDoublyMappedCode((uint8_t *) virt, (uint8_t *) virt, len); |
| 95 return succeeded; | 101 return succeeded; |
| 96 } | 102 } |
| 97 | 103 |
| 98 } // End of port namespace | 104 } // End of port namespace |
| 99 | 105 |
| OLD | NEW |