| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 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 | 7 |
| 8 /* | 8 /* |
| 9 * address_sets.c - Implements a bit set of addresses that is used by branch | 9 * address_sets.c - Implements a bit set of addresses that is used by branch |
| 10 * validation to check if branches are safe. | 10 * validation to check if branches are safe. |
| 11 */ | 11 */ |
| 12 | 12 |
| 13 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/address_sets.h" | 13 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/address_sets.h" |
| 14 | 14 |
| 15 #include "native_client/src/shared/platform/nacl_log.h" | 15 #include "native_client/src/shared/platform/nacl_log.h" |
| 16 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter.
h" | 16 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter.
h" |
| 17 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter_
internal.h" | 17 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/ncvalidate_iter_
internal.h" |
| 18 | 18 |
| 19 /* To turn on debugging of instruction decoding, change value of | 19 /* To turn on debugging of instruction decoding, change value of |
| 20 * DEBUGGING to 1. | 20 * DEBUGGING to 1. |
| 21 */ | 21 */ |
| 22 #define DEBUGGING 0 | 22 #define DEBUGGING 0 |
| 23 | 23 |
| 24 #include "native_client/src/shared/utils/debugging.h" | 24 #include "native_client/src/shared/utils/debugging.h" |
| 25 | 25 |
| 26 /* Model the set of possible 3-bit tails of possible PcAddresses. */ | 26 #include "native_client/src/trusted/validator/x86/ncval_reg_sfi/address_sets_inl
.c" |
| 27 static const uint8_t nacl_pc_address_masks[8] = { | |
| 28 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; | |
| 29 | |
| 30 /* Model the offset created by removing the bottom three bits of a PcAddress. */ | |
| 31 typedef NaClPcAddress NaClPcOffset; | |
| 32 | |
| 33 /* Convert an address into the corresponding offset in an address table. | |
| 34 * That is, strip off the last three bits, since these remaining bits | |
| 35 * will be encoded using the union of address masks in the address table. | |
| 36 */ | |
| 37 static INLINE NaClPcOffset NaClPcAddressToOffset(NaClPcAddress address) { | |
| 38 return address >> 3; | |
| 39 } | |
| 40 | |
| 41 /* Convert the 3 lower bits of an address into the corresponding address mask | |
| 42 * to use. | |
| 43 */ | |
| 44 static INLINE uint8_t NaClPcAddressToMask(NaClPcAddress address) { | |
| 45 return nacl_pc_address_masks[(int) (address & (NaClPcAddress)0x7)]; | |
| 46 } | |
| 47 | |
| 48 /* Returns true if the given address is within the code segment. Generates | |
| 49 * error messages if it isn't. | |
| 50 */ | |
| 51 static Bool NaClCheckAddressRange(NaClPcAddress address, | |
| 52 NaClValidatorState* state) { | |
| 53 if (address < state->vbase) { | |
| 54 NaClValidatorPcAddressMessage(LOG_ERROR, state, address, | |
| 55 "Jump to address before code block.\n"); | |
| 56 return FALSE; | |
| 57 } | |
| 58 if (address >= state->vlimit) { | |
| 59 NaClValidatorPcAddressMessage(LOG_ERROR, state, address, | |
| 60 "Jump to address beyond code block limit.\n"); | |
| 61 return FALSE; | |
| 62 } | |
| 63 return TRUE; | |
| 64 } | |
| 65 | 27 |
| 66 uint8_t NaClAddressSetContains(NaClAddressSet set, | 28 uint8_t NaClAddressSetContains(NaClAddressSet set, |
| 67 NaClPcAddress address, | 29 NaClPcAddress address, |
| 68 NaClValidatorState* state) { | 30 NaClValidatorState* state) { |
| 69 if (NaClCheckAddressRange(address, state)) { | 31 if (NaClCheckAddressRange(address, state)) { |
| 70 NaClPcAddress offset = address - state->vbase; | 32 NaClPcAddress offset = address - state->vbase; |
| 71 return set[NaClPcAddressToOffset(offset)] & NaClPcAddressToMask(offset); | 33 return set[NaClPcAddressToOffset(offset)] & NaClPcAddressToMask(offset); |
| 72 } else { | 34 } else { |
| 73 return FALSE; | 35 return FALSE; |
| 74 } | 36 } |
| 75 } | 37 } |
| 76 | 38 |
| 77 void NaClAddressSetAdd(NaClAddressSet set, NaClPcAddress address, | 39 void NaClAddressSetAdd(NaClAddressSet set, NaClPcAddress address, |
| 78 NaClValidatorState* state) { | 40 NaClValidatorState* state) { |
| 79 if (NaClCheckAddressRange(address, state)) { | 41 NaClAddressSetAddInline(set, address, state); |
| 80 NaClPcAddress offset = address - state->vbase; | |
| 81 DEBUG(NaClLog(LOG_INFO, | |
| 82 "Address set add: %"NACL_PRIxNaClPcAddress"\n", address)); | |
| 83 set[NaClPcAddressToOffset(offset)] |= NaClPcAddressToMask(offset); | |
| 84 } | |
| 85 } | 42 } |
| 86 | 43 |
| 87 size_t NaClAddressSetArraySize(NaClMemorySize size) { | 44 size_t NaClAddressSetArraySize(NaClMemorySize size) { |
| 88 /* Be sure to add an element for partial overlaps. */ | 45 /* Be sure to add an element for partial overlaps. */ |
| 89 /* TODO(karl) The cast to size_t for the number of elements may | 46 /* TODO(karl) The cast to size_t for the number of elements may |
| 90 * cause loss of data. We need to fix this. This is a security | 47 * cause loss of data. We need to fix this. This is a security |
| 91 * issue when doing cross-platform (32-64 bit) generation. | 48 * issue when doing cross-platform (32-64 bit) generation. |
| 92 */ | 49 */ |
| 93 return (size_t) NaClPcAddressToOffset(size) + 1; | 50 return (size_t) NaClPcAddressToOffset(size) + 1; |
| 94 } | 51 } |
| 95 | 52 |
| 96 NaClAddressSet NaClAddressSetCreate(NaClMemorySize size) { | 53 NaClAddressSet NaClAddressSetCreate(NaClMemorySize size) { |
| 97 return (NaClAddressSet) calloc(NaClAddressSetArraySize(size), | 54 return (NaClAddressSet) calloc(NaClAddressSetArraySize(size), |
| 98 sizeof(uint8_t)); | 55 sizeof(uint8_t)); |
| 99 } | 56 } |
| 100 | 57 |
| 101 void NaClAddressSetDestroy(NaClAddressSet set) { | 58 void NaClAddressSetDestroy(NaClAddressSet set) { |
| 102 free(set); | 59 free(set); |
| 103 } | 60 } |
| OLD | NEW |