|
1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "precompiled.hpp" |
|
26 #include "classfile/imageFile.hpp" |
|
27 #include "runtime/os.inline.hpp" |
|
28 #include "utilities/bytes.hpp" |
|
29 |
|
30 |
|
31 // Compute the Perfect Hashing hash code for the supplied string. |
|
32 u4 ImageStrings::hash_code(const char* string, u4 seed) { |
|
33 u1* bytes = (u1*)string; |
|
34 |
|
35 // Compute hash code. |
|
36 for (u1 byte = *bytes++; byte; byte = *bytes++) { |
|
37 seed = (seed * HASH_MULTIPLIER) ^ byte; |
|
38 } |
|
39 |
|
40 // Ensure the result is unsigned. |
|
41 return seed & 0x7FFFFFFF; |
|
42 } |
|
43 |
|
44 // Test to see if string begins with start. If so returns remaining portion |
|
45 // of string. Otherwise, NULL. |
|
46 const char* ImageStrings::starts_with(const char* string, const char* start) { |
|
47 char ch1, ch2; |
|
48 |
|
49 // Match up the strings the best we can. |
|
50 while ((ch1 = *string) && (ch2 = *start)) { |
|
51 if (ch1 != ch2) { |
|
52 // Mismatch, return NULL. |
|
53 return NULL; |
|
54 } |
|
55 |
|
56 string++, start++; |
|
57 } |
|
58 |
|
59 // Return remainder of string. |
|
60 return string; |
|
61 } |
|
62 |
|
63 ImageLocation::ImageLocation(u1* data) { |
|
64 // Deflate the attribute stream into an array of attributes. |
|
65 memset(_attributes, 0, sizeof(_attributes)); |
|
66 u1 byte; |
|
67 |
|
68 while ((byte = *data) != ATTRIBUTE_END) { |
|
69 u1 kind = attribute_kind(byte); |
|
70 u1 n = attribute_length(byte); |
|
71 assert(kind < ATTRIBUTE_COUNT, "invalid image location attribute"); |
|
72 _attributes[kind] = attribute_value(data + 1, n); |
|
73 data += n + 1; |
|
74 } |
|
75 } |
|
76 |
|
77 ImageFile::ImageFile(const char* name) { |
|
78 // Copy the image file name. |
|
79 _name = NEW_C_HEAP_ARRAY(char, strlen(name)+1, mtClass); |
|
80 strcpy(_name, name); |
|
81 |
|
82 // Initialize for a closed file. |
|
83 _fd = -1; |
|
84 _memory_mapped = true; |
|
85 _index_data = NULL; |
|
86 } |
|
87 |
|
88 ImageFile::~ImageFile() { |
|
89 // Ensure file is closed. |
|
90 close(); |
|
91 |
|
92 // Free up name. |
|
93 FREE_C_HEAP_ARRAY(char, _name, mtClass); |
|
94 } |
|
95 |
|
96 bool ImageFile::open() { |
|
97 // If file exists open for reading. |
|
98 struct stat st; |
|
99 if (os::stat(_name, &st) != 0 || |
|
100 (st.st_mode & S_IFREG) != S_IFREG || |
|
101 (_fd = os::open(_name, 0, O_RDONLY)) == -1) { |
|
102 return false; |
|
103 } |
|
104 |
|
105 // Read image file header and verify. |
|
106 u8 header_size = sizeof(ImageHeader); |
|
107 if (os::read(_fd, &_header, header_size) != header_size || |
|
108 _header._magic != IMAGE_MAGIC || |
|
109 _header._major_version != MAJOR_VERSION || |
|
110 _header._minor_version != MINOR_VERSION) { |
|
111 close(); |
|
112 return false; |
|
113 } |
|
114 |
|
115 // Memory map index. |
|
116 _index_size = index_size(); |
|
117 _index_data = (u1*)os::map_memory(_fd, _name, 0, NULL, _index_size, true, false); |
|
118 |
|
119 // Failing that, read index into C memory. |
|
120 if (_index_data == NULL) { |
|
121 _memory_mapped = false; |
|
122 _index_data = NEW_RESOURCE_ARRAY(u1, _index_size); |
|
123 |
|
124 if (os::seek_to_file_offset(_fd, 0) == -1) { |
|
125 close(); |
|
126 return false; |
|
127 } |
|
128 |
|
129 if (os::read(_fd, _index_data, _index_size) != _index_size) { |
|
130 close(); |
|
131 return false; |
|
132 } |
|
133 |
|
134 return true; |
|
135 } |
|
136 |
|
137 // Used to advance a pointer, unstructured. |
|
138 #undef nextPtr |
|
139 #define nextPtr(base, fromType, count, toType) (toType*)((fromType*)(base) + (count)) |
|
140 // Pull tables out from the index. |
|
141 _redirect_table = nextPtr(_index_data, u1, header_size, s4); |
|
142 _offsets_table = nextPtr(_redirect_table, s4, _header._location_count, u4); |
|
143 _location_bytes = nextPtr(_offsets_table, u4, _header._location_count, u1); |
|
144 _string_bytes = nextPtr(_location_bytes, u1, _header._locations_size, u1); |
|
145 #undef nextPtr |
|
146 |
|
147 // Successful open. |
|
148 return true; |
|
149 } |
|
150 |
|
151 void ImageFile::close() { |
|
152 // Dealllocate the index. |
|
153 if (_index_data) { |
|
154 if (_memory_mapped) { |
|
155 os::unmap_memory((char*)_index_data, _index_size); |
|
156 } else { |
|
157 FREE_RESOURCE_ARRAY(u1, _index_data, _index_size); |
|
158 } |
|
159 |
|
160 _index_data = NULL; |
|
161 } |
|
162 |
|
163 // close file. |
|
164 if (_fd != -1) { |
|
165 os::close(_fd); |
|
166 _fd = -1; |
|
167 } |
|
168 |
|
169 } |
|
170 |
|
171 // Return the attribute stream for a named resourced. |
|
172 u1* ImageFile::find_location_data(const char* path) const { |
|
173 // Compute hash. |
|
174 u4 hash = ImageStrings::hash_code(path) % _header._location_count; |
|
175 s4 redirect = _redirect_table[hash]; |
|
176 |
|
177 if (!redirect) { |
|
178 return NULL; |
|
179 } |
|
180 |
|
181 u4 index; |
|
182 |
|
183 if (redirect < 0) { |
|
184 // If no collision. |
|
185 index = -redirect - 1; |
|
186 } else { |
|
187 // If collision, recompute hash code. |
|
188 index = ImageStrings::hash_code(path, redirect) % _header._location_count; |
|
189 } |
|
190 |
|
191 assert(index < _header._location_count, "index exceeds location count"); |
|
192 u4 offset = _offsets_table[index]; |
|
193 assert(offset < _header._locations_size, "offset exceeds location attributes size"); |
|
194 |
|
195 if (offset == 0) { |
|
196 return NULL; |
|
197 } |
|
198 |
|
199 return _location_bytes + offset; |
|
200 } |
|
201 |
|
202 // Verify that a found location matches the supplied path. |
|
203 bool ImageFile::verify_location(ImageLocation& location, const char* path) const { |
|
204 // Retrieve each path component string. |
|
205 ImageStrings strings(_string_bytes, _header._strings_size); |
|
206 // Match a path with each subcomponent without concatenation (copy). |
|
207 // Match up path parent. |
|
208 const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings); |
|
209 const char* next = ImageStrings::starts_with(path, parent); |
|
210 // Continue only if a complete match. |
|
211 if (!next) return false; |
|
212 // Match up path base. |
|
213 const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings); |
|
214 next = ImageStrings::starts_with(next, base); |
|
215 // Continue only if a complete match. |
|
216 if (!next) return false; |
|
217 // Match up path extension. |
|
218 const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings); |
|
219 next = ImageStrings::starts_with(next, extension); |
|
220 |
|
221 // True only if complete match and no more characters. |
|
222 return next && *next == '\0'; |
|
223 } |
|
224 |
|
225 // Return the resource for the supplied location. |
|
226 u1* ImageFile::get_resource(ImageLocation& location) const { |
|
227 // Retrieve the byte offset and size of the resource. |
|
228 u8 offset = _index_size + location.get_attribute(ImageLocation::ATTRIBUTE_OFFSET); |
|
229 u8 size = location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED); |
|
230 u8 compressed_size = location.get_attribute(ImageLocation::ATTRIBUTE_COMPRESSED); |
|
231 u8 read_size = compressed_size ? compressed_size : size; |
|
232 |
|
233 // Allocate space for the resource. |
|
234 u1* data = NEW_RESOURCE_ARRAY(u1, read_size); |
|
235 |
|
236 bool is_read = os::read_at(_fd, data, read_size, offset) == read_size; |
|
237 guarantee(is_read, "error reading from image or short read"); |
|
238 |
|
239 // If not compressed, just return the data. |
|
240 if (!compressed_size) { |
|
241 return data; |
|
242 } |
|
243 |
|
244 u1* uncompressed = NEW_RESOURCE_ARRAY(u1, size); |
|
245 char* msg = NULL; |
|
246 jboolean res = ClassLoader::decompress(data, compressed_size, uncompressed, size, &msg); |
|
247 if (!res) warning("decompression failed due to %s\n", msg); |
|
248 guarantee(res, "decompression failed"); |
|
249 |
|
250 return uncompressed; |
|
251 } |
|
252 |
|
253 void ImageFile::get_resource(const char* path, u1*& buffer, u8& size) const { |
|
254 buffer = NULL; |
|
255 size = 0; |
|
256 u1* data = find_location_data(path); |
|
257 if (data) { |
|
258 ImageLocation location(data); |
|
259 if (verify_location(location, path)) { |
|
260 size = location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED); |
|
261 buffer = get_resource(location); |
|
262 } |
|
263 } |
|
264 } |
|
265 |
|
266 GrowableArray<const char*>* ImageFile::packages(const char* name) { |
|
267 char entry[JVM_MAXPATHLEN]; |
|
268 bool overflow = jio_snprintf(entry, sizeof(entry), "%s/packages.offsets", name) == -1; |
|
269 guarantee(!overflow, "package name overflow"); |
|
270 |
|
271 u1* buffer; |
|
272 u8 size; |
|
273 |
|
274 get_resource(entry, buffer, size); |
|
275 guarantee(buffer, "missing module packages reource"); |
|
276 ImageStrings strings(_string_bytes, _header._strings_size); |
|
277 GrowableArray<const char*>* pkgs = new GrowableArray<const char*>(); |
|
278 int count = size / 4; |
|
279 for (int i = 0; i < count; i++) { |
|
280 u4 offset = Bytes::get_Java_u4(buffer + (i*4)); |
|
281 const char* p = strings.get(offset); |
|
282 pkgs->append(p); |
|
283 } |
|
284 |
|
285 return pkgs; |
|
286 } |