annotate src/java.base/share/native/libjimage/imageFile.cpp @ 14563:2891d078c753

8141571: jdk/internal/jimage/JImageReadTest.java crashing in msvcr120.dll Summary: add asserts to diagnose Reviewed-by: jlaskey
author rriggs
date Fri, 13 Nov 2015 12:00:23 -0500
parents 40cb3080b3dd
children db40b83b1071 384f4b699387
rev   line source
jfdenise@13824 1 /*
jfdenise@13824 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
jfdenise@13824 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jfdenise@13824 4 *
jfdenise@13824 5 * This code is free software; you can redistribute it and/or modify it
jfdenise@13824 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@13973 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@13973 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@13973 9 * by Oracle in the LICENSE file that accompanied this code.
jfdenise@13824 10 *
jfdenise@13824 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jfdenise@13824 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@13973 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jfdenise@13824 14 * version 2 for more details (a copy is included in the LICENSE file that
jfdenise@13824 15 * accompanied this code).
jfdenise@13824 16 *
jfdenise@13824 17 * You should have received a copy of the GNU General Public License version
jfdenise@13824 18 * 2 along with this work; if not, write to the Free Software Foundation,
jfdenise@13824 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jfdenise@13824 20 *
jfdenise@13824 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jfdenise@13824 22 * or visit www.oracle.com if you need additional information or have any
jfdenise@13824 23 * questions.
jfdenise@13824 24 */
jfdenise@13824 25
jfdenise@13824 26 #include <assert.h>
jfdenise@13824 27 #include <string.h>
jfdenise@13824 28 #include <stdlib.h>
jfdenise@13824 29
jfdenise@13824 30 #include "endian.hpp"
jfdenise@13824 31 #include "imageDecompressor.hpp"
jfdenise@13824 32 #include "imageFile.hpp"
jfdenise@13824 33 #include "inttypes.hpp"
jfdenise@13824 34 #include "jni.h"
jfdenise@13824 35 #include "osSupport.hpp"
jfdenise@13824 36
jfdenise@13824 37 // Map the full jimage, only with 64 bit addressing.
jfdenise@13824 38 bool MemoryMapImage = sizeof(void *) == 8;
jfdenise@13824 39
jfdenise@13824 40 #ifdef WIN32
jfdenise@13824 41 const char FileSeparator = '\\';
jfdenise@13824 42 #else
jfdenise@13824 43 const char FileSeparator = '/';
jfdenise@13824 44 #endif
jfdenise@13824 45
jfdenise@13824 46 // Image files are an alternate file format for storing classes and resources. The
jfdenise@13824 47 // goal is to supply file access which is faster and smaller than the jar format.
jfdenise@13824 48 //
jfdenise@13824 49 // (More detailed nodes in the header.)
jfdenise@13824 50 //
jfdenise@13824 51
jfdenise@13824 52 // Compute the Perfect Hashing hash code for the supplied UTF-8 string.
jfdenise@13824 53 s4 ImageStrings::hash_code(const char* string, s4 seed) {
jlaskey@13973 54 // Access bytes as unsigned.
jlaskey@13973 55 u1* bytes = (u1*)string;
jlaskey@13973 56 // Compute hash code.
jlaskey@13973 57 for (u1 byte = *bytes++; byte; byte = *bytes++) {
jlaskey@13973 58 seed = (seed * HASH_MULTIPLIER) ^ byte;
jlaskey@13973 59 }
jlaskey@13973 60 // Ensure the result is not signed.
jlaskey@13973 61 return seed & 0x7FFFFFFF;
jfdenise@13824 62 }
jfdenise@13824 63
jfdenise@13824 64 // Match up a string in a perfect hash table.
jfdenise@13824 65 // Returns the index where the name should be.
jfdenise@13824 66 // Result still needs validation for precise match (false positive.)
jfdenise@13824 67 s4 ImageStrings::find(Endian* endian, const char* name, s4* redirect, u4 length) {
jlaskey@13973 68 // If the table is empty, then short cut.
jlaskey@13973 69 if (!redirect || !length) {
jlaskey@13973 70 return NOT_FOUND;
jlaskey@13973 71 }
jlaskey@13973 72 // Compute the basic perfect hash for name.
jlaskey@13973 73 s4 hash_code = ImageStrings::hash_code(name);
jlaskey@13973 74 // Modulo table size.
jlaskey@13973 75 s4 index = hash_code % length;
jlaskey@13973 76 // Get redirect entry.
jlaskey@13973 77 // value == 0 then not found
jlaskey@13973 78 // value < 0 then -1 - value is true index
jlaskey@13973 79 // value > 0 then value is seed for recomputing hash.
jlaskey@13973 80 s4 value = endian->get(redirect[index]);
jlaskey@13973 81 // if recompute is required.
jlaskey@13973 82 if (value > 0 ) {
jlaskey@13973 83 // Entry collision value, need to recompute hash.
jlaskey@13973 84 hash_code = ImageStrings::hash_code(name, value);
jlaskey@13973 85 // Modulo table size.
jlaskey@13973 86 return hash_code % length;
jlaskey@13973 87 } else if (value < 0) {
jlaskey@13973 88 // Compute direct index.
jlaskey@13973 89 return -1 - value;
jlaskey@13973 90 }
jlaskey@13973 91 // No entry found.
jfdenise@13824 92 return NOT_FOUND;
jfdenise@13824 93 }
jfdenise@13824 94
jfdenise@13824 95 // Test to see if UTF-8 string begins with the start UTF-8 string. If so,
jfdenise@13824 96 // return non-NULL address of remaining portion of string. Otherwise, return
jlaskey@13973 97 // NULL. Used to test sections of a path without copying from image string
jfdenise@13824 98 // table.
jfdenise@13824 99 const char* ImageStrings::starts_with(const char* string, const char* start) {
jlaskey@13973 100 char ch1, ch2;
jlaskey@13973 101 // Match up the strings the best we can.
jlaskey@13973 102 while ((ch1 = *string) && (ch2 = *start)) {
jlaskey@13973 103 if (ch1 != ch2) {
jlaskey@13973 104 // Mismatch, return NULL.
jlaskey@13973 105 return NULL;
jlaskey@13973 106 }
jlaskey@13973 107 // Next characters.
jlaskey@13973 108 string++, start++;
jfdenise@13824 109 }
jlaskey@13973 110 // Return remainder of string.
jlaskey@13973 111 return string;
jfdenise@13824 112 }
jfdenise@13824 113
jfdenise@13824 114 // Inflates the attribute stream into individual values stored in the long
jfdenise@13824 115 // array _attributes. This allows an attribute value to be quickly accessed by
jfdenise@13824 116 // direct indexing. Unspecified values default to zero (from constructor.)
jfdenise@13824 117 void ImageLocation::set_data(u1* data) {
jlaskey@13973 118 // Deflate the attribute stream into an array of attributes.
jlaskey@13973 119 u1 byte;
jlaskey@13973 120 // Repeat until end header is found.
asmotrak@14522 121 while ((data != NULL) && (byte = *data)) {
jlaskey@13973 122 // Extract kind from header byte.
jlaskey@13973 123 u1 kind = attribute_kind(byte);
jlaskey@13973 124 assert(kind < ATTRIBUTE_COUNT && "invalid image location attribute");
jlaskey@13973 125 // Extract length of data (in bytes).
jlaskey@13973 126 u1 n = attribute_length(byte);
jlaskey@13973 127 // Read value (most significant first.)
jlaskey@13973 128 _attributes[kind] = attribute_value(data + 1, n);
jlaskey@13973 129 // Position to next attribute by skipping attribute header and data bytes.
jlaskey@13973 130 data += n + 1;
jlaskey@13973 131 }
jfdenise@13824 132 }
jfdenise@13824 133
jfdenise@13824 134 // Zero all attribute values.
jfdenise@13824 135 void ImageLocation::clear_data() {
jlaskey@13973 136 // Set defaults to zero.
jlaskey@13973 137 memset(_attributes, 0, sizeof(_attributes));
jfdenise@13824 138 }
jfdenise@13824 139
jfdenise@13824 140 // ImageModuleData constructor maps out sub-tables for faster access.
jfdenise@13824 141 ImageModuleData::ImageModuleData(const ImageFileReader* image_file,
jlaskey@13973 142 const char* module_data_name) :
jlaskey@13973 143 _image_file(image_file),
jlaskey@13973 144 _endian(image_file->endian()),
jlaskey@13973 145 _strings(image_file->get_strings()) {
jlaskey@13973 146 // Retrieve the resource containing the module data for the image file.
jlaskey@13973 147 ImageLocation location;
jlaskey@13973 148 bool found = image_file->find_location(module_data_name, location);
jlaskey@13973 149 if (found) {
jlaskey@13973 150 u8 data_size = location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
jlaskey@13973 151 _data = new u1[(size_t)data_size];
rriggs@14563 152 assert(_data != NULL && "allocation failed");
jlaskey@13973 153 _image_file->get_resource(location, _data);
jlaskey@13973 154 // Map out the header.
jlaskey@13973 155 _header = (Header*)_data;
jlaskey@13973 156 // Get the package to module entry count.
jlaskey@13973 157 u4 ptm_count = _header->ptm_count(_endian);
jlaskey@13973 158 // Get the module to package entry count.
jlaskey@13973 159 u4 mtp_count = _header->mtp_count(_endian);
jlaskey@13973 160 // Compute the offset of the package to module perfect hash redirect.
jlaskey@13973 161 u4 ptm_redirect_offset = sizeof(Header);
jlaskey@13973 162 // Compute the offset of the package to module data.
jlaskey@13973 163 u4 ptm_data_offset = ptm_redirect_offset + ptm_count * sizeof(s4);
jlaskey@13973 164 // Compute the offset of the module to package perfect hash redirect.
jlaskey@13973 165 u4 mtp_redirect_offset = ptm_data_offset + ptm_count * sizeof(PTMData);
jlaskey@13973 166 // Compute the offset of the module to package data.
jlaskey@13973 167 u4 mtp_data_offset = mtp_redirect_offset + mtp_count * sizeof(s4);
jlaskey@13973 168 // Compute the offset of the module to package tables.
jlaskey@13973 169 u4 mtp_packages_offset = mtp_data_offset + mtp_count * sizeof(MTPData);
jlaskey@13973 170 // Compute the address of the package to module perfect hash redirect.
jlaskey@13973 171 _ptm_redirect = (s4*)(_data + ptm_redirect_offset);
jlaskey@13973 172 // Compute the address of the package to module data.
jlaskey@13973 173 _ptm_data = (PTMData*)(_data + ptm_data_offset);
jlaskey@13973 174 // Compute the address of the module to package perfect hash redirect.
jlaskey@13973 175 _mtp_redirect = (s4*)(_data + mtp_redirect_offset);
jlaskey@13973 176 // Compute the address of the module to package data.
jlaskey@13973 177 _mtp_data = (MTPData*)(_data + mtp_data_offset);
jlaskey@13973 178 // Compute the address of the module to package tables.
jlaskey@13973 179 _mtp_packages = (s4*)(_data + mtp_packages_offset);
jlaskey@13973 180 } else {
jlaskey@13973 181 // No module data present.
jlaskey@13973 182 _data = NULL;
jlaskey@13973 183 _header = NULL;
jlaskey@13973 184 _ptm_redirect = NULL;
jlaskey@13973 185 _ptm_data = NULL;
jlaskey@13973 186 _mtp_redirect = NULL;
jlaskey@13973 187 _mtp_data = NULL;
jlaskey@13973 188 _mtp_packages = NULL;
jlaskey@13973 189 }
jfdenise@13824 190 }
jfdenise@13824 191
jfdenise@13824 192 // Release module data resource.
jfdenise@13824 193 ImageModuleData::~ImageModuleData() {
jlaskey@13973 194 if (_data) {
asmotrak@14522 195 delete[] _data;
jlaskey@13973 196 }
jfdenise@13824 197 }
jfdenise@13824 198
jfdenise@13824 199 // Return the name of the module data resource. Ex. "./lib/modules/file.jimage"
jfdenise@13824 200 // yields "file.jdata"
jfdenise@13824 201 void ImageModuleData::module_data_name(char* buffer, const char* image_file_name) {
jlaskey@13973 202 // Locate the last slash in the file name path.
jlaskey@13973 203 const char* slash = strrchr(image_file_name, FileSeparator);
jlaskey@13973 204 // Trim the path to name and extension.
jlaskey@13973 205 const char* name = slash ? slash + 1 : (char *)image_file_name;
jlaskey@13973 206 // Locate the extension period.
jlaskey@13973 207 const char* dot = strrchr(name, '.');
jlaskey@13973 208 assert(dot && "missing extension on jimage name");
jlaskey@13973 209 // Trim to only base name.
jlaskey@13973 210 int length = (int)(dot - name);
jlaskey@13973 211 strncpy(buffer, name, length);
jlaskey@13973 212 buffer[length] = '\0';
jlaskey@13973 213 // Append extension.
jlaskey@13973 214 strcat(buffer, ".jdata");
jfdenise@13824 215 }
jfdenise@13824 216
jlaskey@13973 217 // Return the module in which a package resides. Returns NULL if not found.
jfdenise@13824 218 const char* ImageModuleData::package_to_module(const char* package_name) {
jlaskey@13973 219 // Test files may contain no module data.
jlaskey@13973 220 if (_data != NULL) {
jlaskey@13973 221 // Search the package to module table.
jlaskey@13973 222 s4 index = ImageStrings::find(_endian, package_name, _ptm_redirect,
jlaskey@13973 223 _header->ptm_count(_endian));
jlaskey@13973 224 // If entry is found.
jlaskey@13973 225 if (index != ImageStrings::NOT_FOUND) {
jlaskey@13973 226 // Retrieve the package to module entry.
jlaskey@13973 227 PTMData* data = _ptm_data + index;
jlaskey@13973 228 // Verify that it is the correct data.
jlaskey@13973 229 if (strcmp(package_name, get_string(data->name_offset(_endian))) != 0) {
jlaskey@13973 230 return NULL;
jlaskey@13973 231 }
jlaskey@13973 232 // Return the module name.
jlaskey@13973 233 return get_string(data->module_name_offset(_endian));
jlaskey@13973 234 }
jfdenise@13824 235 }
jlaskey@13973 236 return NULL;
jfdenise@13824 237 }
jfdenise@13824 238
jfdenise@13824 239 // Returns all the package names in a module in a NULL terminated array.
jfdenise@13824 240 // Returns NULL if module not found.
jfdenise@13824 241 const char** ImageModuleData::module_to_packages(const char* module_name) {
jlaskey@13973 242 // Test files may contain no module data.
jlaskey@13973 243 if (_data != NULL) {
jlaskey@13973 244 // Search the module to package table.
jlaskey@13973 245 s4 index = ImageStrings::find(_endian, module_name, _mtp_redirect,
jlaskey@13973 246 _header->mtp_count(_endian));
jlaskey@13973 247 // If entry is found.
jlaskey@13973 248 if (index != ImageStrings::NOT_FOUND) {
jlaskey@13973 249 // Retrieve the module to package entry.
jlaskey@13973 250 MTPData* data = _mtp_data + index;
jlaskey@13973 251 // Verify that it is the correct data.
jlaskey@13973 252 if (strcmp(module_name, get_string(data->name_offset(_endian))) != 0) {
jlaskey@13973 253 return NULL;
jlaskey@13973 254 }
jlaskey@13973 255 // Construct an array of all the package entries.
jlaskey@13973 256 u4 count = data->package_count(_endian);
jlaskey@13973 257 const char** packages = new const char*[count + 1];
rriggs@14563 258 assert(packages != NULL && "allocation failed");
jlaskey@13973 259 s4 package_offset = data->package_offset(_endian);
jlaskey@13973 260 for (u4 i = 0; i < count; i++) {
jlaskey@13973 261 u4 package_name_offset = mtp_package(package_offset + i);
jlaskey@13973 262 const char* package_name = get_string(package_name_offset);
jlaskey@13973 263 packages[i] = package_name;
jlaskey@13973 264 }
jlaskey@13973 265 packages[count] = NULL;
jlaskey@13973 266 return packages;
jlaskey@13973 267 }
jfdenise@13824 268 }
jlaskey@13973 269 return NULL;
jfdenise@13824 270 }
jfdenise@13824 271
jfdenise@13824 272 // Manage a table of open image files. This table allows multiple access points
jfdenise@13824 273 // to share an open image.
jfdenise@13824 274 ImageFileReaderTable::ImageFileReaderTable() : _count(0), _max(_growth) {
jlaskey@13973 275 _table = new ImageFileReader*[_max];
rriggs@14563 276 assert( _table != NULL && "allocation failed");
jfdenise@13824 277 }
jfdenise@13824 278
jfdenise@13824 279 ImageFileReaderTable::~ImageFileReaderTable() {
asmotrak@14522 280 delete[] _table;
jfdenise@13824 281 }
jfdenise@13824 282
jfdenise@13824 283 // Add a new image entry to the table.
jfdenise@13824 284 void ImageFileReaderTable::add(ImageFileReader* image) {
jlaskey@13973 285 if (_count == _max) {
jlaskey@13973 286 _max += _growth;
jlaskey@13973 287 _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
jlaskey@13973 288 }
jlaskey@13973 289 _table[_count++] = image;
jfdenise@13824 290 }
jfdenise@13824 291
jfdenise@13824 292 // Remove an image entry from the table.
jfdenise@13824 293 void ImageFileReaderTable::remove(ImageFileReader* image) {
jlaskey@13973 294 s4 last = _count - 1;
jlaskey@13973 295 for (s4 i = 0; _count; i++) {
jlaskey@13973 296 if (_table[i] == image) {
jlaskey@13973 297 if (i != last) {
jlaskey@13973 298 _table[i] = _table[last];
jlaskey@13973 299 _count = last;
jlaskey@13973 300 }
jlaskey@13973 301 break;
jlaskey@13973 302 }
jfdenise@13824 303 }
jfdenise@13824 304
jlaskey@13973 305 if (_count != 0 && _count == _max - _growth) {
jlaskey@13973 306 _max -= _growth;
jlaskey@13973 307 _table = static_cast<ImageFileReader**>(realloc(_table, _max * sizeof(ImageFileReader*)));
jlaskey@13973 308 }
jfdenise@13824 309 }
jfdenise@13824 310
jfdenise@13824 311 // Determine if image entry is in table.
jfdenise@13824 312 bool ImageFileReaderTable::contains(ImageFileReader* image) {
jlaskey@13973 313 for (s4 i = 0; _count; i++) {
jlaskey@13973 314 if (_table[i] == image) {
jlaskey@13973 315 return true;
jlaskey@13973 316 }
jfdenise@13824 317 }
jlaskey@13973 318 return false;
jfdenise@13824 319 }
jfdenise@13824 320
jfdenise@13824 321 // Table to manage multiple opens of an image file.
jfdenise@13824 322 ImageFileReaderTable ImageFileReader::_reader_table;
jfdenise@13824 323
jfdenise@13824 324 SimpleCriticalSection _reader_table_lock;
jfdenise@13824 325
jfdenise@13824 326 // Open an image file, reuse structure if file already open.
jfdenise@13824 327 ImageFileReader* ImageFileReader::open(const char* name, bool big_endian) {
jlaskey@13973 328 {
jlaskey@13973 329 // Lock out _reader_table.
jlaskey@13973 330 SimpleCriticalSectionLock cs(&_reader_table_lock);
jlaskey@13973 331 // Search for an exist image file.
jlaskey@13973 332 for (u4 i = 0; i < _reader_table.count(); i++) {
jlaskey@13973 333 // Retrieve table entry.
jlaskey@13973 334 ImageFileReader* reader = _reader_table.get(i);
jlaskey@13973 335 // If name matches, then reuse (bump up use count.)
rriggs@14563 336 assert(reader->name() != NULL && "reader->name must not be null");
jlaskey@13973 337 if (strcmp(reader->name(), name) == 0) {
jlaskey@13973 338 reader->inc_use();
jlaskey@13973 339 return reader;
jlaskey@13973 340 }
jlaskey@13973 341 }
jlaskey@13973 342 } // Unlock the mutex
jlaskey@13973 343
jlaskey@13973 344 // Need a new image reader.
jlaskey@13973 345 ImageFileReader* reader = new ImageFileReader(name, big_endian);
rriggs@14563 346 if (reader == NULL || !reader->open()) {
rriggs@14563 347 // Failed to open.
jlaskey@13973 348 delete reader;
jlaskey@13973 349 return NULL;
jlaskey@13973 350 }
jlaskey@13973 351
jlaskey@13973 352 // Lock to update
jfdenise@13824 353 SimpleCriticalSectionLock cs(&_reader_table_lock);
rriggs@14563 354 // Search for an existing image file.
jfdenise@13824 355 for (u4 i = 0; i < _reader_table.count(); i++) {
jlaskey@13973 356 // Retrieve table entry.
jlaskey@13973 357 ImageFileReader* existing_reader = _reader_table.get(i);
jlaskey@13973 358 // If name matches, then reuse (bump up use count.)
rriggs@14563 359 assert(reader->name() != NULL && "reader->name still must not be null");
jlaskey@13973 360 if (strcmp(existing_reader->name(), name) == 0) {
jlaskey@13973 361 existing_reader->inc_use();
jlaskey@13973 362 reader->close();
jlaskey@13973 363 delete reader;
jlaskey@13973 364 return existing_reader;
jlaskey@13973 365 }
jfdenise@13824 366 }
jlaskey@13973 367 // Bump use count and add to table.
jlaskey@13973 368 reader->inc_use();
jlaskey@13973 369 _reader_table.add(reader);
jlaskey@13973 370 return reader;
jfdenise@13824 371 }
jfdenise@13824 372
jfdenise@13824 373 // Close an image file if the file is not in use elsewhere.
jfdenise@13824 374 void ImageFileReader::close(ImageFileReader *reader) {
jlaskey@13973 375 // Lock out _reader_table.
jlaskey@13973 376 SimpleCriticalSectionLock cs(&_reader_table_lock);
jlaskey@13973 377 // If last use then remove from table and then close.
jlaskey@13973 378 if (reader->dec_use()) {
jlaskey@13973 379 _reader_table.remove(reader);
jlaskey@13973 380 delete reader;
jlaskey@13973 381 }
jfdenise@13824 382 }
jfdenise@13824 383
jfdenise@13824 384 // Return an id for the specifed ImageFileReader.
jfdenise@13824 385 u8 ImageFileReader::readerToID(ImageFileReader *reader) {
jlaskey@13973 386 // ID is just the cloaked reader address.
jlaskey@13973 387 return (u8)reader;
jfdenise@13824 388 }
jfdenise@13824 389
jfdenise@13824 390 // Validate the image id.
jfdenise@13824 391 bool ImageFileReader::idCheck(u8 id) {
jlaskey@13973 392 // Make sure the ID is a managed (_reader_table) reader.
jlaskey@13973 393 SimpleCriticalSectionLock cs(&_reader_table_lock);
jlaskey@13973 394 return _reader_table.contains((ImageFileReader*)id);
jfdenise@13824 395 }
jfdenise@13824 396
jfdenise@13824 397 // Return an id for the specifed ImageFileReader.
jfdenise@13824 398 ImageFileReader* ImageFileReader::idToReader(u8 id) {
jlaskey@13973 399 assert(idCheck(id) && "invalid image id");
jlaskey@13973 400 return (ImageFileReader*)id;
jfdenise@13824 401 }
jfdenise@13824 402
jfdenise@13824 403 // Constructor intializes to a closed state.
jfdenise@13824 404 ImageFileReader::ImageFileReader(const char* name, bool big_endian) {
jlaskey@13973 405 // Copy the image file name.
jlaskey@13973 406 int len = (int) strlen(name) + 1;
jlaskey@13973 407 _name = new char[len];
rriggs@14563 408 assert(_name != NULL && "allocation failed");
jlaskey@13973 409 strncpy(_name, name, len);
jlaskey@13973 410 // Initialize for a closed file.
jlaskey@13973 411 _fd = -1;
jlaskey@13973 412 _endian = Endian::get_handler(big_endian);
jlaskey@13973 413 _index_data = NULL;
jfdenise@13824 414 }
jfdenise@13824 415
jfdenise@13824 416 // Close image and free up data structures.
jfdenise@13824 417 ImageFileReader::~ImageFileReader() {
jlaskey@13973 418 // Ensure file is closed.
jlaskey@13973 419 close();
jlaskey@13973 420 // Free up name.
jlaskey@13973 421 if (_name) {
asmotrak@14522 422 delete[] _name;
jlaskey@13973 423 _name = NULL;
jlaskey@13973 424 }
jfdenise@13824 425 }
jfdenise@13824 426
jfdenise@13824 427 // Open image file for read access.
jfdenise@13824 428 bool ImageFileReader::open() {
jlaskey@13973 429 char buffer[IMAGE_MAX_PATH];
jfdenise@13824 430
jlaskey@13973 431 // If file exists open for reading.
jlaskey@13973 432 _fd = osSupport::openReadOnly(_name);
jlaskey@13973 433 if (_fd == -1) {
jlaskey@13973 434 return false;
jlaskey@13973 435 }
jlaskey@13973 436 // Retrieve the file size.
jlaskey@13973 437 _file_size = osSupport::size(_name);
jlaskey@13973 438 // Read image file header and verify it has a valid header.
jlaskey@13973 439 size_t header_size = sizeof(ImageHeader);
jlaskey@13973 440 if (_file_size < header_size ||
jlaskey@13973 441 !read_at((u1*)&_header, header_size, 0) ||
jlaskey@13973 442 _header.magic(_endian) != IMAGE_MAGIC ||
jlaskey@13973 443 _header.major_version(_endian) != MAJOR_VERSION ||
jlaskey@13973 444 _header.minor_version(_endian) != MINOR_VERSION) {
jlaskey@13973 445 close();
jlaskey@13973 446 return false;
jlaskey@13973 447 }
jlaskey@13973 448 // Size of image index.
jlaskey@13973 449 _index_size = index_size();
jlaskey@13973 450 // Make sure file is large enough to contain the index.
jlaskey@13973 451 if (_file_size < _index_size) {
jlaskey@13973 452 return false;
jlaskey@13973 453 }
jlaskey@13973 454 // Determine how much of the image is memory mapped.
jlaskey@13973 455 size_t map_size = (size_t)(MemoryMapImage ? _file_size : _index_size);
jlaskey@13973 456 // Memory map image (minimally the index.)
jlaskey@13973 457 _index_data = (u1*)osSupport::map_memory(_fd, _name, 0, map_size);
jlaskey@13973 458 assert(_index_data && "image file not memory mapped");
jlaskey@13973 459 // Retrieve length of index perfect hash table.
jlaskey@13973 460 u4 length = table_length();
jlaskey@13973 461 // Compute offset of the perfect hash table redirect table.
jlaskey@13973 462 u4 redirect_table_offset = (u4)header_size;
jlaskey@13973 463 // Compute offset of index attribute offsets.
jlaskey@13973 464 u4 offsets_table_offset = redirect_table_offset + length * sizeof(s4);
jlaskey@13973 465 // Compute offset of index location attribute data.
jlaskey@13973 466 u4 location_bytes_offset = offsets_table_offset + length * sizeof(u4);
jlaskey@13973 467 // Compute offset of index string table.
jlaskey@13973 468 u4 string_bytes_offset = location_bytes_offset + locations_size();
jlaskey@13973 469 // Compute address of the perfect hash table redirect table.
jlaskey@13973 470 _redirect_table = (s4*)(_index_data + redirect_table_offset);
jlaskey@13973 471 // Compute address of index attribute offsets.
jlaskey@13973 472 _offsets_table = (u4*)(_index_data + offsets_table_offset);
jlaskey@13973 473 // Compute address of index location attribute data.
jlaskey@13973 474 _location_bytes = _index_data + location_bytes_offset;
jlaskey@13973 475 // Compute address of index string table.
jlaskey@13973 476 _string_bytes = _index_data + string_bytes_offset;
jfdenise@13824 477
jlaskey@13973 478 // Initialize the module data
jlaskey@13973 479 ImageModuleData::module_data_name(buffer, _name);
jlaskey@13973 480 module_data = new ImageModuleData(this, buffer);
rriggs@14563 481 // Successful open (if memory allocation succeeded).
rriggs@14563 482 return module_data != NULL;
jfdenise@13824 483 }
jfdenise@13824 484
jfdenise@13824 485 // Close image file.
jfdenise@13824 486 void ImageFileReader::close() {
jlaskey@13973 487 // Deallocate the index.
jlaskey@13973 488 if (_index_data) {
jlaskey@13973 489 osSupport::unmap_memory((char*)_index_data, _index_size);
jlaskey@13973 490 _index_data = NULL;
jlaskey@13973 491 }
jlaskey@13973 492 // Close file.
jlaskey@13973 493 if (_fd != -1) {
jlaskey@13973 494 osSupport::close(_fd);
jlaskey@13973 495 _fd = -1;
jlaskey@13973 496 }
jfdenise@13824 497 }
jfdenise@13824 498
jfdenise@13824 499 // Read directly from the file.
jfdenise@13824 500 bool ImageFileReader::read_at(u1* data, u8 size, u8 offset) const {
jlaskey@13973 501 return (u8)osSupport::read(_fd, (char*)data, size, offset) == size;
jfdenise@13824 502 }
jfdenise@13824 503
jlaskey@13973 504 // Find the location attributes associated with the path. Returns true if
jfdenise@13824 505 // the location is found, false otherwise.
jfdenise@13824 506 bool ImageFileReader::find_location(const char* path, ImageLocation& location) const {
jlaskey@13973 507 // Locate the entry in the index perfect hash table.
jlaskey@13973 508 s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
jlaskey@13973 509 // If is found.
jlaskey@13973 510 if (index != ImageStrings::NOT_FOUND) {
jlaskey@13973 511 // Get address of first byte of location attribute stream.
jlaskey@13973 512 u1* data = get_location_data(index);
jlaskey@13973 513 // Expand location attributes.
jlaskey@13973 514 location.set_data(data);
jlaskey@13973 515 // Make sure result is not a false positive.
jlaskey@13973 516 return verify_location(location, path);
jlaskey@13973 517 }
jlaskey@13973 518 return false;
jfdenise@13824 519 }
jfdenise@13824 520
jfdenise@13824 521 // Find the location index and size associated with the path.
jfdenise@13824 522 // Returns the location index and size if the location is found, 0 otherwise.
jfdenise@13824 523 u4 ImageFileReader::find_location_index(const char* path, u8 *size) const {
jlaskey@13973 524 // Locate the entry in the index perfect hash table.
jlaskey@13973 525 s4 index = ImageStrings::find(_endian, path, _redirect_table, table_length());
jlaskey@13973 526 // If found.
jlaskey@13973 527 if (index != ImageStrings::NOT_FOUND) {
jlaskey@13973 528 // Get address of first byte of location attribute stream.
jlaskey@13973 529 u4 offset = get_location_offset(index);
jlaskey@13973 530 u1* data = get_location_offset_data(offset);
jlaskey@13973 531 // Expand location attributes.
jlaskey@13973 532 ImageLocation location(data);
jlaskey@13973 533 // Make sure result is not a false positive.
jlaskey@13973 534 if (verify_location(location, path)) {
jlaskey@13973 535 *size = (jlong)location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
jlaskey@13973 536 return offset;
jlaskey@13973 537 }
jfdenise@13824 538 }
jlaskey@13973 539 return 0; // not found
jfdenise@13824 540 }
jfdenise@13824 541
jfdenise@13824 542 // Assemble the location path from the string fragments indicated in the location attributes.
jfdenise@13824 543 void ImageFileReader::location_path(ImageLocation& location, char* path, size_t max) const {
jlaskey@13973 544 // Manage the image string table.
jlaskey@13973 545 ImageStrings strings(_string_bytes, _header.strings_size(_endian));
jlaskey@13973 546 // Position to first character of the path buffer.
jlaskey@13973 547 char* next = path;
jlaskey@13973 548 // Temp for string length.
jlaskey@13973 549 size_t length;
jlaskey@13973 550 // Get module string.
jlaskey@13973 551 const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
jlaskey@13973 552 // If module string is not empty string.
jlaskey@13973 553 if (*module != '\0') {
jlaskey@13973 554 // Get length of module name.
jlaskey@13973 555 length = strlen(module);
jlaskey@13973 556 // Make sure there is no buffer overflow.
jlaskey@13973 557 assert(next - path + length + 2 < max && "buffer overflow");
jlaskey@13973 558 // Append '/module/'.
jlaskey@13973 559 *next++ = '/';
jlaskey@13973 560 strncpy(next, module, length); next += length;
jlaskey@13973 561 *next++ = '/';
jlaskey@13973 562 }
jlaskey@13973 563 // Get parent (package) string.
jlaskey@13973 564 const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
jlaskey@13973 565 // If parent string is not empty string.
jlaskey@13973 566 if (*parent != '\0') {
jlaskey@13973 567 // Get length of module string.
jlaskey@13973 568 length = strlen(parent);
jlaskey@13973 569 // Make sure there is no buffer overflow.
jlaskey@13973 570 assert(next - path + length + 1 < max && "buffer overflow");
jlaskey@13973 571 // Append 'patent/' .
jlaskey@13973 572 strncpy(next, parent, length); next += length;
jlaskey@13973 573 *next++ = '/';
jlaskey@13973 574 }
jlaskey@13973 575 // Get base name string.
jlaskey@13973 576 const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
jlaskey@13973 577 // Get length of base name.
jlaskey@13973 578 length = strlen(base);
jfdenise@13824 579 // Make sure there is no buffer overflow.
jlaskey@13973 580 assert(next - path + length < max && "buffer overflow");
jlaskey@13973 581 // Append base name.
jlaskey@13973 582 strncpy(next, base, length); next += length;
jlaskey@13973 583 // Get extension string.
jlaskey@13973 584 const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
jlaskey@13973 585 // If extension string is not empty string.
jlaskey@13973 586 if (*extension != '\0') {
jlaskey@13973 587 // Get length of extension string.
jlaskey@13973 588 length = strlen(extension);
jlaskey@13973 589 // Make sure there is no buffer overflow.
jlaskey@13973 590 assert(next - path + length + 1 < max && "buffer overflow");
jlaskey@13973 591 // Append '.extension' .
jlaskey@13973 592 *next++ = '.';
jlaskey@13973 593 strncpy(next, extension, length); next += length;
jlaskey@13973 594 }
jfdenise@13824 595 // Make sure there is no buffer overflow.
jlaskey@13973 596 assert((size_t)(next - path) < max && "buffer overflow");
jlaskey@13973 597 // Terminate string.
jlaskey@13973 598 *next = '\0';
jfdenise@13824 599 }
jfdenise@13824 600
jfdenise@13824 601 // Verify that a found location matches the supplied path (without copying.)
jfdenise@13824 602 bool ImageFileReader::verify_location(ImageLocation& location, const char* path) const {
jlaskey@13973 603 // Manage the image string table.
jlaskey@13973 604 ImageStrings strings(_string_bytes, _header.strings_size(_endian));
jlaskey@13973 605 // Position to first character of the path string.
jlaskey@13973 606 const char* next = path;
jlaskey@13973 607 // Get module name string.
jlaskey@13973 608 const char* module = location.get_attribute(ImageLocation::ATTRIBUTE_MODULE, strings);
jlaskey@13973 609 // If module string is not empty.
jlaskey@13973 610 if (*module != '\0') {
jlaskey@13973 611 // Compare '/module/' .
jlaskey@13973 612 if (*next++ != '/') return false;
jlaskey@13973 613 if (!(next = ImageStrings::starts_with(next, module))) return false;
jlaskey@13973 614 if (*next++ != '/') return false;
jlaskey@13973 615 }
jlaskey@13973 616 // Get parent (package) string
jlaskey@13973 617 const char* parent = location.get_attribute(ImageLocation::ATTRIBUTE_PARENT, strings);
jlaskey@13973 618 // If parent string is not empty string.
jlaskey@13973 619 if (*parent != '\0') {
jlaskey@13973 620 // Compare 'parent/' .
jlaskey@13973 621 if (!(next = ImageStrings::starts_with(next, parent))) return false;
jlaskey@13973 622 if (*next++ != '/') return false;
jlaskey@13973 623 }
jlaskey@13973 624 // Get base name string.
jlaskey@13973 625 const char* base = location.get_attribute(ImageLocation::ATTRIBUTE_BASE, strings);
jlaskey@13973 626 // Compare with basne name.
jlaskey@13973 627 if (!(next = ImageStrings::starts_with(next, base))) return false;
jlaskey@13973 628 // Get extension string.
jlaskey@13973 629 const char* extension = location.get_attribute(ImageLocation::ATTRIBUTE_EXTENSION, strings);
jlaskey@13973 630 // If extension is not empty.
jlaskey@13973 631 if (*extension != '\0') {
jlaskey@13973 632 // Compare '.extension' .
jlaskey@13973 633 if (*next++ != '.') return false;
jlaskey@13973 634 if (!(next = ImageStrings::starts_with(next, extension))) return false;
jlaskey@13973 635 }
jlaskey@13973 636 // True only if complete match and no more characters.
jlaskey@13973 637 return *next == '\0';
jfdenise@13824 638 }
jfdenise@13824 639
jfdenise@13824 640 // Return the resource for the supplied location offset.
jfdenise@13824 641 void ImageFileReader::get_resource(u4 offset, u1* uncompressed_data) const {
jlaskey@13973 642 // Get address of first byte of location attribute stream.
jlaskey@13973 643 u1* data = get_location_offset_data(offset);
jlaskey@13973 644 // Expand location attributes.
jlaskey@13973 645 ImageLocation location(data);
jlaskey@13973 646 // Read the data
jlaskey@13973 647 get_resource(location, uncompressed_data);
jfdenise@13824 648 }
jfdenise@13824 649
jfdenise@13824 650 // Return the resource for the supplied location.
jfdenise@13824 651 void ImageFileReader::get_resource(ImageLocation& location, u1* uncompressed_data) const {
jlaskey@13973 652 // Retrieve the byte offset and size of the resource.
jlaskey@13973 653 u8 offset = location.get_attribute(ImageLocation::ATTRIBUTE_OFFSET);
jlaskey@13973 654 u8 uncompressed_size = location.get_attribute(ImageLocation::ATTRIBUTE_UNCOMPRESSED);
jlaskey@13973 655 u8 compressed_size = location.get_attribute(ImageLocation::ATTRIBUTE_COMPRESSED);
jlaskey@13973 656 // If the resource is compressed.
jlaskey@13973 657 if (compressed_size != 0) {
jlaskey@13973 658 u1* compressed_data;
jlaskey@13973 659 // If not memory mapped read in bytes.
jlaskey@13973 660 if (!MemoryMapImage) {
jlaskey@13973 661 // Allocate buffer for compression.
jlaskey@13973 662 compressed_data = new u1[(u4)compressed_size];
rriggs@14563 663 assert (compressed_data != NULL && "allocation failed");
jlaskey@13973 664 // Read bytes from offset beyond the image index.
jlaskey@13973 665 bool is_read = read_at(compressed_data, compressed_size, _index_size + offset);
jlaskey@13973 666 assert(is_read && "error reading from image or short read");
jlaskey@13973 667 } else {
jlaskey@13973 668 compressed_data = get_data_address() + offset;
jlaskey@13973 669 }
jlaskey@13973 670 // Get image string table.
jlaskey@13973 671 const ImageStrings strings = get_strings();
jlaskey@13973 672 // Decompress resource.
jlaskey@13973 673 ImageDecompressor::decompress_resource(compressed_data, uncompressed_data, (u4)uncompressed_size,
jlaskey@13973 674 &strings);
jlaskey@13973 675 // If not memory mapped then release temporary buffer.
jlaskey@13973 676 if (!MemoryMapImage) {
asmotrak@14522 677 delete[] compressed_data;
jlaskey@13973 678 }
jfdenise@13824 679 } else {
jlaskey@13973 680 // Read bytes from offset beyond the image index.
jlaskey@13973 681 bool is_read = read_at(uncompressed_data, uncompressed_size, _index_size + offset);
jlaskey@13973 682 assert(is_read && "error reading from image or short read");
jfdenise@13824 683 }
jfdenise@13824 684 }
jfdenise@13824 685
jfdenise@13824 686 // Return the ImageModuleData for this image
jfdenise@13824 687 ImageModuleData * ImageFileReader::get_image_module_data() {
jlaskey@13973 688 return module_data;
jfdenise@13824 689 }