Chromium Code Reviews| 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 <assert.h> | 7 #include <assert.h> |
| 8 #include <elf.h> | |
| 9 #include <inttypes.h> | |
| 10 #include <stdio.h> | 8 #include <stdio.h> |
| 11 #include <stdlib.h> | 9 #include <stdlib.h> |
| 12 #include <string.h> | 10 #include <string.h> |
| 13 #include "validator.h" | |
| 14 | 11 |
| 15 #undef TRUE | 12 #include "native_client/src/include/elf32.h" |
| 16 #define TRUE 1 | 13 #include "native_client/src/include/elf64.h" |
| 14 #include "native_client/src/shared/platform/nacl_check.h" | |
| 15 #include "native_client/src/shared/utils/types.h" | |
| 16 #include "native_client/src/trusted/validator_ragel/unreviewed/validator.h" | |
| 17 | 17 |
| 18 #undef FALSE | 18 void NaClLog_Function(int detail_level, char const *fmt, ...) { |
|
pasko-google - do not use
2012/04/06 15:23:58
same TODO here please
khim
2012/04/06 15:30:04
Done.
| |
| 19 #define FALSE 0 | 19 va_list ap; |
| 20 | 20 |
| 21 /* This may help with portability but makes code less readable. */ | 21 va_start(ap, fmt); |
| 22 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement" | 22 vfprintf(stderr, fmt, ap); |
| 23 exit(1); | |
| 24 NaClLogV_mu(detail_level, fmt, ap); | |
| 25 va_end(ap); | |
| 26 NaClLogUnlock(); | |
| 27 } | |
| 23 | 28 |
| 24 static void CheckBounds(unsigned char *data, size_t data_size, | 29 static void CheckBounds(unsigned char *data, size_t data_size, |
| 25 void *ptr, size_t inside_size) { | 30 void *ptr, size_t inside_size) { |
| 26 assert(data <= (unsigned char *) ptr); | 31 CHECK(data <= (unsigned char *) ptr); |
| 27 assert((unsigned char *) ptr + inside_size <= data + data_size); | 32 CHECK((unsigned char *) ptr + inside_size <= data + data_size); |
| 28 } | 33 } |
| 29 | 34 |
| 30 void ReadFile(const char *filename, uint8_t **result, size_t *result_size) { | 35 void ReadImage(const char *filename, uint8_t **result, size_t *result_size) { |
| 31 FILE *fp; | 36 FILE *fp; |
| 32 uint8_t *data; | 37 uint8_t *data; |
| 33 size_t file_size; | 38 size_t file_size; |
| 34 size_t got; | 39 size_t got; |
| 35 | 40 |
| 36 fp = fopen(filename, "rb"); | 41 fp = fopen(filename, "rb"); |
| 37 if (fp == NULL) { | 42 if (fp == NULL) { |
| 38 fprintf(stderr, "Failed to open input file: %s\n", filename); | 43 fprintf(stderr, "Failed to open input file: %s\n", filename); |
| 39 exit(1); | 44 exit(1); |
| 40 } | 45 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 59 *result = data; | 64 *result = data; |
| 60 *result_size = file_size; | 65 *result_size = file_size; |
| 61 } | 66 } |
| 62 | 67 |
| 63 struct ValidateState { | 68 struct ValidateState { |
| 64 uint8_t width; | 69 uint8_t width; |
| 65 const uint8_t *offset; | 70 const uint8_t *offset; |
| 66 }; | 71 }; |
| 67 | 72 |
| 68 void ProcessError (const uint8_t *ptr, void *userdata) { | 73 void ProcessError (const uint8_t *ptr, void *userdata) { |
| 69 printf("offset 0x%zx: DFA error in validator\n", | 74 printf("offset 0x%"NACL_PRIxS": DFA error in validator\n", |
| 70 ptr - (((struct ValidateState *)userdata)->offset)); | 75 ptr - (((struct ValidateState *)userdata)->offset)); |
| 71 } | 76 } |
| 72 | 77 |
| 73 int ValidateFile(const char *filename, int repeat_count) { | 78 int ValidateFile(const char *filename, int repeat_count) { |
| 74 size_t data_size; | 79 size_t data_size; |
| 75 uint8_t *data; | 80 uint8_t *data; |
| 76 ReadFile(filename, &data, &data_size); | 81 int count; |
| 77 | 82 |
| 78 int count; | 83 ReadImage(filename, &data, &data_size); |
| 84 | |
| 79 if (data[4] == 1) { | 85 if (data[4] == 1) { |
| 80 for (count = 0; count < repeat_count; ++count) { | 86 for (count = 0; count < repeat_count; ++count) { |
| 81 Elf32_Ehdr *header; | 87 Elf32_Ehdr *header; |
| 82 int index; | 88 int index; |
| 83 | 89 |
| 84 header = (Elf32_Ehdr *) data; | 90 header = (Elf32_Ehdr *) data; |
| 85 CheckBounds(data, data_size, header, sizeof(*header)); | 91 CheckBounds(data, data_size, header, sizeof(*header)); |
| 86 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); | 92 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); |
| 87 | 93 |
| 88 for (index = 0; index < header->e_shnum; ++index) { | 94 for (index = 0; index < header->e_shnum; ++index) { |
| 89 Elf32_Shdr *section = (Elf32_Shdr *) (data + header->e_shoff + | 95 Elf32_Shdr *section = (Elf32_Shdr *) (data + header->e_shoff + |
| 90 header->e_shentsize * index); | 96 header->e_shentsize * index); |
| 91 CheckBounds(data, data_size, section, sizeof(*section)); | 97 CheckBounds(data, data_size, section, sizeof(*section)); |
| 92 | 98 |
| 93 if ((section->sh_flags & SHF_EXECINSTR) != 0) { | 99 if ((section->sh_flags & SHF_EXECINSTR) != 0) { |
| 94 struct ValidateState state; | 100 struct ValidateState state; |
| 101 int res; | |
| 102 | |
| 95 state.offset = data + section->sh_offset - section->sh_addr; | 103 state.offset = data + section->sh_offset - section->sh_addr; |
| 96 if (section->sh_size <= 0xfff) { | 104 if (section->sh_size <= 0xfff) { |
| 97 state.width = 4; | 105 state.width = 4; |
| 98 } else if (section->sh_size <= 0xfffffff) { | 106 } else if (section->sh_size <= 0xfffffff) { |
| 99 state.width = 8; | 107 state.width = 8; |
| 100 } else { | 108 } else { |
| 101 state.width = 12; | 109 state.width = 12; |
| 102 } | 110 } |
| 103 CheckBounds(data, data_size, | 111 CheckBounds(data, data_size, |
| 104 data + section->sh_offset, section->sh_size); | 112 data + section->sh_offset, section->sh_size); |
| 105 int res = ValidateChunkIA32(data + section->sh_offset, | 113 res = ValidateChunkIA32(data + section->sh_offset, |
| 106 section->sh_size, ProcessError, &state); | 114 section->sh_size, ProcessError, &state); |
| 107 if (res != 0) { | 115 if (res != 0) { |
| 108 return res; | 116 return res; |
| 109 } | 117 } |
| 110 } | 118 } |
| 111 } | 119 } |
| 112 } | 120 } |
| 113 } else if (data[4] == 2) { | 121 } else if (data[4] == 2) { |
| 114 for (count = 0; count < repeat_count; ++count) { | 122 for (count = 0; count < repeat_count; ++count) { |
| 115 Elf64_Ehdr *header; | 123 Elf64_Ehdr *header; |
| 116 int index; | 124 int index; |
| 117 | 125 |
| 118 header = (Elf64_Ehdr *) data; | 126 header = (Elf64_Ehdr *) data; |
| 119 CheckBounds(data, data_size, header, sizeof(*header)); | 127 CheckBounds(data, data_size, header, sizeof(*header)); |
| 120 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); | 128 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); |
| 121 | 129 |
| 122 for (index = 0; index < header->e_shnum; ++index) { | 130 for (index = 0; index < header->e_shnum; ++index) { |
| 123 Elf64_Shdr *section = (Elf64_Shdr *) (data + header->e_shoff + | 131 Elf64_Shdr *section = (Elf64_Shdr *) (data + header->e_shoff + |
| 124 header->e_shentsize * index); | 132 header->e_shentsize * index); |
| 125 CheckBounds(data, data_size, section, sizeof(*section)); | 133 CheckBounds(data, data_size, section, sizeof(*section)); |
| 126 | 134 |
| 127 if ((section->sh_flags & SHF_EXECINSTR) != 0) { | 135 if ((section->sh_flags & SHF_EXECINSTR) != 0) { |
| 128 struct ValidateState state; | 136 struct ValidateState state; |
| 137 int res; | |
| 138 | |
| 129 state.offset = data + section->sh_offset - section->sh_addr; | 139 state.offset = data + section->sh_offset - section->sh_addr; |
| 130 if (section->sh_size <= 0xfff) { | 140 if (section->sh_size <= 0xfff) { |
| 131 state.width = 4; | 141 state.width = 4; |
| 132 } else if (section->sh_size <= 0xfffffff) { | 142 } else if (section->sh_size <= 0xfffffff) { |
| 133 state.width = 8; | 143 state.width = 8; |
| 134 } else if (section->sh_size <= 0xfffffffffffLL) { | 144 } else if (section->sh_size <= 0xfffffffffffLL) { |
| 135 state.width = 12; | 145 state.width = 12; |
| 136 } else { | 146 } else { |
| 137 state.width = 16; | 147 state.width = 16; |
| 138 } | 148 } |
| 139 CheckBounds(data, data_size, | 149 CheckBounds(data, data_size, |
| 140 data + section->sh_offset, section->sh_size); | 150 data + section->sh_offset, section->sh_size); |
| 141 int res = ValidateChunkAMD64(data + section->sh_offset, | 151 res = ValidateChunkAMD64(data + section->sh_offset, |
| 142 section->sh_size, ProcessError, &state); | 152 section->sh_size, ProcessError, &state); |
| 143 if (res != 0) { | 153 if (res != 0) { |
| 144 return res; | 154 return res; |
| 145 } | 155 } |
| 146 } | 156 } |
| 147 } | 157 } |
| 148 } | 158 } |
| 149 } else { | 159 } else { |
| 150 printf("Unknown ELF class: %s\n", filename); | 160 printf("Unknown ELF class: %s\n", filename); |
| 151 exit(1); | 161 exit(1); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 164 for (index = initial_index; index < argc; ++index) { | 174 for (index = initial_index; index < argc; ++index) { |
| 165 const char *filename = argv[index]; | 175 const char *filename = argv[index]; |
| 166 int rc = ValidateFile(filename, repeat_count); | 176 int rc = ValidateFile(filename, repeat_count); |
| 167 if (rc != 0) { | 177 if (rc != 0) { |
| 168 printf("file '%s' can not be fully validated\n", filename); | 178 printf("file '%s' can not be fully validated\n", filename); |
| 169 return 1; | 179 return 1; |
| 170 } | 180 } |
| 171 } | 181 } |
| 172 return 0; | 182 return 0; |
| 173 } | 183 } |
| OLD | NEW |