OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014 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 #ifndef NATIVE_CLIENT_SRC_INCLUDE_MINSFI_PTR_H_ |
| 8 #define NATIVE_CLIENT_SRC_INCLUDE_MINSFI_PTR_H_ |
| 9 |
| 10 #include <assert.h> |
| 11 |
| 12 #include "native_client/src/include/minsfi_priv.h" |
| 13 |
| 14 /* |
| 15 * This defines what ToMinsfiPtr should do if the given native pointer |
| 16 * is not within the bounds of the allocated memory region. We abort in |
| 17 * production code and return a magic constant when invoked inside tests. |
| 18 */ |
| 19 #ifndef MINSFI_PTR_CONVERSION_TEST |
| 20 #define ToMinsfiPtr_AbortAction abort(); |
| 21 #else // MINSFI_PTR_CONVERSION_TEST |
| 22 #define ToMinsfiPtr_AbortAction return 0xCAFEBABE; |
| 23 #endif // MINSFI_PTR_CONVERSION_TEST |
| 24 |
| 25 /* |
| 26 * Convert an untrusted pointer into a native pointer. Because this is just |
| 27 * some integer provided by the untrusted code, we must sandbox it the same |
| 28 * way the SandboxMemoryAccesses compiler pass does. |
| 29 */ |
| 30 static inline char *FromMinsfiPtr(sfiptr_t sfiptr, const MinsfiSandbox *sb) { |
| 31 uint32_t masked_sfiptr = sfiptr & sb->ptr_mask; |
| 32 return sb->mem_base + masked_sfiptr; |
| 33 } |
| 34 |
| 35 /* |
| 36 * Convert a native pointer into an untrusted pointer. This means simply |
| 37 * subtracting the memory base from the address. |
| 38 */ |
| 39 static inline sfiptr_t ToMinsfiPtr(const char *ptr, const MinsfiSandbox *sb) { |
| 40 uintptr_t ptr_int = (uintptr_t) ptr; |
| 41 uintptr_t base_int = (uintptr_t) sb->mem_base; |
| 42 sfiptr_t sb_ptr = ptr_int - base_int; |
| 43 |
| 44 /* Check that the pointer is in the bounds of the allocated memory region. */ |
| 45 if ((base_int > ptr_int) || ((sb_ptr & (~sb->ptr_mask)) != 0)) { |
| 46 ToMinsfiPtr_AbortAction |
| 47 } |
| 48 |
| 49 return sb_ptr; |
| 50 } |
| 51 |
| 52 #endif // NATIVE_CLIENT_SRC_INCLUDE_MINSFI_PTR_H_ |
OLD | NEW |