Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: src/trusted/validator_ragel/unreviewed/validator-test.c

Issue 10031039: Add ragel validator to SCONS (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 /* This is a copy of NaClLog_Function from shared/platform/nacl_log.c to avoid
19 #define FALSE 0 19 * linking in code in NaCl shared code in the unreviewed/Makefile and be able to
20 * use CHECK().
20 21
21 /* This may help with portability but makes code less readable. */ 22 * TODO(khim): remove the copy of NaClLog_Function implementation as soon as
22 #pragma GCC diagnostic ignored "-Wdeclaration-after-statement" 23 * unreviewed/Makefile is eliminated.
24 */
25 void NaClLog_Function(int detail_level, char const *fmt, ...) {
26 va_list ap;
27
28 UNREFERENCED_PARAMETER(detail_level);
29 va_start(ap, fmt);
30 vfprintf(stderr, fmt, ap);
31 exit(1);
32 }
23 33
24 static void CheckBounds(unsigned char *data, size_t data_size, 34 static void CheckBounds(unsigned char *data, size_t data_size,
25 void *ptr, size_t inside_size) { 35 void *ptr, size_t inside_size) {
26 assert(data <= (unsigned char *) ptr); 36 CHECK(data <= (unsigned char *) ptr);
27 assert((unsigned char *) ptr + inside_size <= data + data_size); 37 CHECK((unsigned char *) ptr + inside_size <= data + data_size);
28 } 38 }
29 39
30 void ReadFile(const char *filename, uint8_t **result, size_t *result_size) { 40 void ReadImage(const char *filename, uint8_t **result, size_t *result_size) {
31 FILE *fp; 41 FILE *fp;
32 uint8_t *data; 42 uint8_t *data;
33 size_t file_size; 43 size_t file_size;
34 size_t got; 44 size_t got;
35 45
36 fp = fopen(filename, "rb"); 46 fp = fopen(filename, "rb");
37 if (fp == NULL) { 47 if (fp == NULL) {
38 fprintf(stderr, "Failed to open input file: %s\n", filename); 48 fprintf(stderr, "Failed to open input file: %s\n", filename);
39 exit(1); 49 exit(1);
40 } 50 }
(...skipping 18 matching lines...) Expand all
59 *result = data; 69 *result = data;
60 *result_size = file_size; 70 *result_size = file_size;
61 } 71 }
62 72
63 struct ValidateState { 73 struct ValidateState {
64 uint8_t width; 74 uint8_t width;
65 const uint8_t *offset; 75 const uint8_t *offset;
66 }; 76 };
67 77
68 void ProcessError (const uint8_t *ptr, void *userdata) { 78 void ProcessError (const uint8_t *ptr, void *userdata) {
69 printf("offset 0x%zx: DFA error in validator\n", 79 printf("offset 0x%"NACL_PRIxS": DFA error in validator\n",
70 ptr - (((struct ValidateState *)userdata)->offset)); 80 (size_t)(ptr - (((struct ValidateState *)userdata)->offset)));
71 } 81 }
72 82
73 int ValidateFile(const char *filename, int repeat_count) { 83 int ValidateFile(const char *filename, int repeat_count) {
74 size_t data_size; 84 size_t data_size;
75 uint8_t *data; 85 uint8_t *data;
76 ReadFile(filename, &data, &data_size); 86 int count;
77 87
78 int count; 88 ReadImage(filename, &data, &data_size);
89
79 if (data[4] == 1) { 90 if (data[4] == 1) {
80 for (count = 0; count < repeat_count; ++count) { 91 for (count = 0; count < repeat_count; ++count) {
81 Elf32_Ehdr *header; 92 Elf32_Ehdr *header;
82 int index; 93 int index;
83 94
84 header = (Elf32_Ehdr *) data; 95 header = (Elf32_Ehdr *) data;
85 CheckBounds(data, data_size, header, sizeof(*header)); 96 CheckBounds(data, data_size, header, sizeof(*header));
86 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); 97 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0);
87 98
88 for (index = 0; index < header->e_shnum; ++index) { 99 for (index = 0; index < header->e_shnum; ++index) {
89 Elf32_Shdr *section = (Elf32_Shdr *) (data + header->e_shoff + 100 Elf32_Shdr *section = (Elf32_Shdr *) (data + header->e_shoff +
90 header->e_shentsize * index); 101 header->e_shentsize * index);
91 CheckBounds(data, data_size, section, sizeof(*section)); 102 CheckBounds(data, data_size, section, sizeof(*section));
92 103
93 if ((section->sh_flags & SHF_EXECINSTR) != 0) { 104 if ((section->sh_flags & SHF_EXECINSTR) != 0) {
94 struct ValidateState state; 105 struct ValidateState state;
106 int res;
107
95 state.offset = data + section->sh_offset - section->sh_addr; 108 state.offset = data + section->sh_offset - section->sh_addr;
96 if (section->sh_size <= 0xfff) { 109 if (section->sh_size <= 0xfff) {
97 state.width = 4; 110 state.width = 4;
98 } else if (section->sh_size <= 0xfffffff) { 111 } else if (section->sh_size <= 0xfffffff) {
99 state.width = 8; 112 state.width = 8;
100 } else { 113 } else {
101 state.width = 12; 114 state.width = 12;
102 } 115 }
103 CheckBounds(data, data_size, 116 CheckBounds(data, data_size,
104 data + section->sh_offset, section->sh_size); 117 data + section->sh_offset, section->sh_size);
105 int res = ValidateChunkIA32(data + section->sh_offset, 118 res = ValidateChunkIA32(data + section->sh_offset,
106 section->sh_size, ProcessError, &state); 119 section->sh_size, ProcessError, &state);
107 if (res != 0) { 120 if (res != 0) {
108 return res; 121 return res;
109 } 122 }
110 } 123 }
111 } 124 }
112 } 125 }
113 } else if (data[4] == 2) { 126 } else if (data[4] == 2) {
114 for (count = 0; count < repeat_count; ++count) { 127 for (count = 0; count < repeat_count; ++count) {
115 Elf64_Ehdr *header; 128 Elf64_Ehdr *header;
116 int index; 129 int index;
117 130
118 header = (Elf64_Ehdr *) data; 131 header = (Elf64_Ehdr *) data;
119 CheckBounds(data, data_size, header, sizeof(*header)); 132 CheckBounds(data, data_size, header, sizeof(*header));
120 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0); 133 assert(memcmp(header->e_ident, ELFMAG, strlen(ELFMAG)) == 0);
121 134
122 for (index = 0; index < header->e_shnum; ++index) { 135 for (index = 0; index < header->e_shnum; ++index) {
123 Elf64_Shdr *section = (Elf64_Shdr *) (data + header->e_shoff + 136 Elf64_Shdr *section = (Elf64_Shdr *) (data + header->e_shoff +
124 header->e_shentsize * index); 137 header->e_shentsize * index);
125 CheckBounds(data, data_size, section, sizeof(*section)); 138 CheckBounds(data, data_size, section, sizeof(*section));
126 139
127 if ((section->sh_flags & SHF_EXECINSTR) != 0) { 140 if ((section->sh_flags & SHF_EXECINSTR) != 0) {
128 struct ValidateState state; 141 struct ValidateState state;
142 int res;
143
129 state.offset = data + section->sh_offset - section->sh_addr; 144 state.offset = data + section->sh_offset - section->sh_addr;
130 if (section->sh_size <= 0xfff) { 145 if (section->sh_size <= 0xfff) {
131 state.width = 4; 146 state.width = 4;
132 } else if (section->sh_size <= 0xfffffff) { 147 } else if (section->sh_size <= 0xfffffff) {
133 state.width = 8; 148 state.width = 8;
134 } else if (section->sh_size <= 0xfffffffffffLL) { 149 } else if (section->sh_size <= 0xfffffffffffLL) {
135 state.width = 12; 150 state.width = 12;
136 } else { 151 } else {
137 state.width = 16; 152 state.width = 16;
138 } 153 }
139 CheckBounds(data, data_size, 154 CheckBounds(data, data_size,
140 data + section->sh_offset, section->sh_size); 155 data + section->sh_offset, (size_t)section->sh_size);
141 int res = ValidateChunkAMD64(data + section->sh_offset, 156 res = ValidateChunkAMD64(data + section->sh_offset,
142 section->sh_size, ProcessError, &state); 157 (size_t)section->sh_size,
158 ProcessError, &state);
143 if (res != 0) { 159 if (res != 0) {
144 return res; 160 return res;
145 } 161 }
146 } 162 }
147 } 163 }
148 } 164 }
149 } else { 165 } else {
150 printf("Unknown ELF class: %s\n", filename); 166 printf("Unknown ELF class: %s\n", filename);
151 exit(1); 167 exit(1);
152 } 168 }
(...skipping 11 matching lines...) Expand all
164 for (index = initial_index; index < argc; ++index) { 180 for (index = initial_index; index < argc; ++index) {
165 const char *filename = argv[index]; 181 const char *filename = argv[index];
166 int rc = ValidateFile(filename, repeat_count); 182 int rc = ValidateFile(filename, repeat_count);
167 if (rc != 0) { 183 if (rc != 0) {
168 printf("file '%s' can not be fully validated\n", filename); 184 printf("file '%s' can not be fully validated\n", filename);
169 return 1; 185 return 1;
170 } 186 }
171 } 187 }
172 return 0; 188 return 0;
173 } 189 }
OLDNEW
« no previous file with comments | « src/trusted/validator_ragel/unreviewed/validator.h ('k') | src/trusted/validator_ragel/unreviewed/validator-x86_32.rl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698