1 /* 2 * Copyright (c) 2001, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 // -*- C++ -*- 27 // Program for unpacking specially compressed Java packages. 28 // John R. Rose 29 30 /* 31 * When compiling for a 64bit LP64 system (longs and pointers being 64bits), 32 * the printf format %ld is correct and use of %lld will cause warning 33 * errors from some compilers (gcc/g++). 34 * _LP64 can be explicitly set (used on Linux). 35 * Solaris compilers will define __sparcv9 or __x86_64 on 64bit compilations. 36 */ 37 #if defined(_LP64) || defined(__sparcv9) || defined(__x86_64) 38 #define LONG_LONG_FORMAT "%ld" 39 #define LONG_LONG_HEX_FORMAT "%lx" 40 #else 41 #define LONG_LONG_FORMAT "%lld" 42 #define LONG_LONG_HEX_FORMAT "%016llx" 43 #endif 44 45 #include <sys/types.h> 46 47 #include <stdio.h> 48 #include <string.h> 49 #include <stdlib.h> 50 #include <stdarg.h> 51 52 #include <limits.h> 53 #include <time.h> 54 55 56 57 58 #include "defines.h" 59 #include "bytes.h" 60 #include "utils.h" 61 #include "coding.h" 62 #include "bands.h" 63 64 #include "constants.h" 65 66 #include "zip.h" 67 68 #include "unpack.h" 69 70 71 // tags, in canonical order: 72 static const byte TAGS_IN_ORDER[] = { 73 CONSTANT_Utf8, 74 CONSTANT_Integer, 75 CONSTANT_Float, 76 CONSTANT_Long, 77 CONSTANT_Double, 78 CONSTANT_String, 79 CONSTANT_Class, 80 CONSTANT_Signature, 81 CONSTANT_NameandType, 82 CONSTANT_Fieldref, 83 CONSTANT_Methodref, 84 CONSTANT_InterfaceMethodref 85 }; 86 #define N_TAGS_IN_ORDER (sizeof TAGS_IN_ORDER) 87 88 #ifndef PRODUCT 89 static const char* TAG_NAME[] = { 90 "*None", 91 "Utf8", 92 "*Unicode", 93 "Integer", 94 "Float", 95 "Long", 96 "Double", 97 "Class", 98 "String", 99 "Fieldref", 100 "Methodref", 101 "InterfaceMethodref", 102 "NameandType", 103 "*Signature", 104 0 105 }; 106 107 static const char* ATTR_CONTEXT_NAME[] = { // match ATTR_CONTEXT_NAME, etc. 108 "class", "field", "method", "code" 109 }; 110 111 #else 112 113 #define ATTR_CONTEXT_NAME ((const char**)null) 114 115 #endif 116 117 118 // REQUESTED must be -2 for u2 and REQUESTED_LDC must be -1 for u1 119 enum { NOT_REQUESTED = 0, REQUESTED = -2, REQUESTED_LDC = -1 }; 120 121 #define NO_INORD ((uint)-1) 122 123 struct entry { 124 byte tag; 125 126 #if 0 127 byte bits; 128 enum { 129 //EB_EXTRA = 1, 130 EB_SUPER = 2 131 }; 132 #endif 133 unsigned short nrefs; // pack w/ tag 134 135 int outputIndex; 136 uint inord; // &cp.entries[cp.tag_base[this->tag]+this->inord] == this 137 138 entry* *refs; 139 140 // put last to pack best 141 union { 142 bytes b; 143 int i; 144 jlong l; 145 } value; 146 147 void requestOutputIndex(cpool& cp, int req = REQUESTED); 148 int getOutputIndex() { 149 assert(outputIndex > NOT_REQUESTED); 150 return outputIndex; 151 } 152 153 entry* ref(int refnum) { 154 assert((uint)refnum < nrefs); 155 return refs[refnum]; 156 } 157 158 const char* utf8String() { 159 assert(tagMatches(CONSTANT_Utf8)); 160 assert(value.b.len == strlen((const char*)value.b.ptr)); 161 return (const char*)value.b.ptr; 162 } 163 164 entry* className() { 165 assert(tagMatches(CONSTANT_Class)); 166 return ref(0); 167 } 168 169 entry* memberClass() { 170 assert(tagMatches(CONSTANT_Member)); 171 return ref(0); 172 } 173 174 entry* memberDescr() { 175 assert(tagMatches(CONSTANT_Member)); 176 return ref(1); 177 } 178 179 entry* descrName() { 180 assert(tagMatches(CONSTANT_NameandType)); 181 return ref(0); 182 } 183 184 entry* descrType() { 185 assert(tagMatches(CONSTANT_NameandType)); 186 return ref(1); 187 } 188 189 int typeSize(); 190 191 bytes& asUtf8(); 192 int asInteger() { assert(tag == CONSTANT_Integer); return value.i; } 193 194 bool isUtf8(bytes& b) { return tagMatches(CONSTANT_Utf8) && value.b.equals(b); } 195 196 bool isDoubleWord() { return tag == CONSTANT_Double || tag == CONSTANT_Long; } 197 198 bool tagMatches(byte tag2) { 199 return (tag2 == tag) 200 || (tag2 == CONSTANT_Utf8 && tag == CONSTANT_Signature) 201 #ifndef PRODUCT 202 || (tag2 == CONSTANT_Literal 203 && tag >= CONSTANT_Integer && tag <= CONSTANT_String && tag != CONSTANT_Class) 204 || (tag2 == CONSTANT_Member 205 && tag >= CONSTANT_Fieldref && tag <= CONSTANT_InterfaceMethodref) 206 #endif 207 ; 208 } 209 210 #ifdef PRODUCT 211 const char* string() { return NULL; } 212 #else 213 const char* string(); // see far below 214 #endif 215 }; 216 217 entry* cpindex::get(uint i) { 218 if (i >= len) 219 return null; 220 else if (base1 != null) 221 // primary index 222 return &base1[i]; 223 else 224 // secondary index 225 return base2[i]; 226 } 227 228 inline bytes& entry::asUtf8() { 229 assert(tagMatches(CONSTANT_Utf8)); 230 return value.b; 231 } 232 233 int entry::typeSize() { 234 assert(tagMatches(CONSTANT_Utf8)); 235 const char* sigp = (char*) value.b.ptr; 236 switch (*sigp) { 237 case '(': sigp++; break; // skip opening '(' 238 case 'D': 239 case 'J': return 2; // double field 240 default: return 1; // field 241 } 242 int siglen = 0; 243 for (;;) { 244 int ch = *sigp++; 245 switch (ch) { 246 case 'D': case 'J': 247 siglen += 1; 248 break; 249 case '[': 250 // Skip rest of array info. 251 while (ch == '[') { ch = *sigp++; } 252 if (ch != 'L') break; 253 // else fall through 254 case 'L': 255 sigp = strchr(sigp, ';'); 256 if (sigp == null) { 257 unpack_abort("bad data"); 258 return 0; 259 } 260 sigp += 1; 261 break; 262 case ')': // closing ')' 263 return siglen; 264 } 265 siglen += 1; 266 } 267 } 268 269 inline cpindex* cpool::getFieldIndex(entry* classRef) { 270 if (classRef == NULL) { abort("missing class reference"); return NULL; } 271 assert(classRef->tagMatches(CONSTANT_Class)); 272 assert((uint)classRef->inord < (uint)tag_count[CONSTANT_Class]); 273 return &member_indexes[classRef->inord*2+0]; 274 } 275 inline cpindex* cpool::getMethodIndex(entry* classRef) { 276 if (classRef == NULL) { abort("missing class reference"); return NULL; } 277 assert(classRef->tagMatches(CONSTANT_Class)); 278 assert((uint)classRef->inord < (uint)tag_count[CONSTANT_Class]); 279 return &member_indexes[classRef->inord*2+1]; 280 } 281 282 struct inner_class { 283 entry* inner; 284 entry* outer; 285 entry* name; 286 int flags; 287 inner_class* next_sibling; 288 bool requested; 289 }; 290 291 // Here is where everything gets deallocated: 292 void unpacker::free() { 293 int i; 294 assert(jniobj == null); // caller resp. 295 assert(infileptr == null); // caller resp. 296 if (jarout != null) jarout->reset(); 297 if (gzin != null) { gzin->free(); gzin = null; } 298 if (free_input) input.free(); 299 // free everybody ever allocated with U_NEW or (recently) with T_NEW 300 assert(smallbuf.base() == null || mallocs.contains(smallbuf.base())); 301 assert(tsmallbuf.base() == null || tmallocs.contains(tsmallbuf.base())); 302 mallocs.freeAll(); 303 tmallocs.freeAll(); 304 smallbuf.init(); 305 tsmallbuf.init(); 306 bcimap.free(); 307 class_fixup_type.free(); 308 class_fixup_offset.free(); 309 class_fixup_ref.free(); 310 code_fixup_type.free(); 311 code_fixup_offset.free(); 312 code_fixup_source.free(); 313 requested_ics.free(); 314 cur_classfile_head.free(); 315 cur_classfile_tail.free(); 316 for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) 317 attr_defs[i].free(); 318 319 // free CP state 320 cp.outputEntries.free(); 321 for (i = 0; i < CONSTANT_Limit; i++) 322 cp.tag_extras[i].free(); 323 } 324 325 // input handling 326 // Attempts to advance rplimit so that (rplimit-rp) is at least 'more'. 327 // Will eagerly read ahead by larger chunks, if possible. 328 // Returns false if (rplimit-rp) is not at least 'more', 329 // unless rplimit hits input.limit(). 330 bool unpacker::ensure_input(jlong more) { 331 julong want = more - input_remaining(); 332 if ((jlong)want <= 0) return true; // it's already in the buffer 333 if (rplimit == input.limit()) return true; // not expecting any more 334 335 if (read_input_fn == null) { 336 // assume it is already all there 337 bytes_read += input.limit() - rplimit; 338 rplimit = input.limit(); 339 return true; 340 } 341 CHECK_0; 342 343 julong remaining = (input.limit() - rplimit); // how much left to read? 344 byte* rpgoal = (want >= remaining)? input.limit(): rplimit + (size_t)want; 345 enum { CHUNK_SIZE = (1<<14) }; 346 julong fetch = want; 347 if (fetch < CHUNK_SIZE) 348 fetch = CHUNK_SIZE; 349 if (fetch > remaining*3/4) 350 fetch = remaining; 351 // Try to fetch at least "more" bytes. 352 while ((jlong)fetch > 0) { 353 jlong nr = (*read_input_fn)(this, rplimit, fetch, remaining); 354 if (nr <= 0) { 355 return (rplimit >= rpgoal); 356 } 357 remaining -= nr; 358 rplimit += nr; 359 fetch -= nr; 360 bytes_read += nr; 361 assert(remaining == (julong)(input.limit() - rplimit)); 362 } 363 return true; 364 } 365 366 // output handling 367 368 fillbytes* unpacker::close_output(fillbytes* which) { 369 assert(wp != null); 370 if (which == null) { 371 if (wpbase == cur_classfile_head.base()) { 372 which = &cur_classfile_head; 373 } else { 374 which = &cur_classfile_tail; 375 } 376 } 377 assert(wpbase == which->base()); 378 assert(wplimit == which->end()); 379 which->setLimit(wp); 380 wp = null; 381 wplimit = null; 382 //wpbase = null; 383 return which; 384 } 385 386 //maybe_inline 387 void unpacker::ensure_put_space(size_t size) { 388 if (wp + size <= wplimit) return; 389 // Determine which segment needs expanding. 390 fillbytes* which = close_output(); 391 byte* wp0 = which->grow(size); 392 wpbase = which->base(); 393 wplimit = which->end(); 394 wp = wp0; 395 } 396 397 maybe_inline 398 byte* unpacker::put_space(size_t size) { 399 byte* wp0 = wp; 400 byte* wp1 = wp0 + size; 401 if (wp1 > wplimit) { 402 ensure_put_space(size); 403 wp0 = wp; 404 wp1 = wp0 + size; 405 } 406 wp = wp1; 407 return wp0; 408 } 409 410 maybe_inline 411 void unpacker::putu2_at(byte* wp, int n) { 412 if (n != (unsigned short)n) { 413 unpack_abort(ERROR_OVERFLOW); 414 return; 415 } 416 wp[0] = (n) >> 8; 417 wp[1] = (n) >> 0; 418 } 419 420 maybe_inline 421 void unpacker::putu4_at(byte* wp, int n) { 422 wp[0] = (n) >> 24; 423 wp[1] = (n) >> 16; 424 wp[2] = (n) >> 8; 425 wp[3] = (n) >> 0; 426 } 427 428 maybe_inline 429 void unpacker::putu8_at(byte* wp, jlong n) { 430 putu4_at(wp+0, (int)((julong)n >> 32)); 431 putu4_at(wp+4, (int)((julong)n >> 0)); 432 } 433 434 maybe_inline 435 void unpacker::putu2(int n) { 436 putu2_at(put_space(2), n); 437 } 438 439 maybe_inline 440 void unpacker::putu4(int n) { 441 putu4_at(put_space(4), n); 442 } 443 444 maybe_inline 445 void unpacker::putu8(jlong n) { 446 putu8_at(put_space(8), n); 447 } 448 449 maybe_inline 450 int unpacker::putref_index(entry* e, int size) { 451 if (e == null) 452 return 0; 453 else if (e->outputIndex > NOT_REQUESTED) 454 return e->outputIndex; 455 else if (e->tag == CONSTANT_Signature) 456 return putref_index(e->ref(0), size); 457 else { 458 e->requestOutputIndex(cp, -size); 459 // Later on we'll fix the bits. 460 class_fixup_type.addByte(size); 461 class_fixup_offset.add((int)wpoffset()); 462 class_fixup_ref.add(e); 463 #ifdef PRODUCT 464 return 0; 465 #else 466 return 0x20+size; // 0x22 is easy to eyeball 467 #endif 468 } 469 } 470 471 maybe_inline 472 void unpacker::putref(entry* e) { 473 int oidx = putref_index(e, 2); 474 putu2_at(put_space(2), oidx); 475 } 476 477 maybe_inline 478 void unpacker::putu1ref(entry* e) { 479 int oidx = putref_index(e, 1); 480 putu1_at(put_space(1), oidx); 481 } 482 483 484 static int total_cp_size[] = {0, 0}; 485 static int largest_cp_ref[] = {0, 0}; 486 static int hash_probes[] = {0, 0}; 487 488 // Allocation of small and large blocks. 489 490 enum { CHUNK = (1 << 14), SMALL = (1 << 9) }; 491 492 // Call malloc. Try to combine small blocks and free much later. 493 void* unpacker::alloc_heap(size_t size, bool smallOK, bool temp) { 494 if (!smallOK || size > SMALL) { 495 void* res = must_malloc((int)size); 496 (temp ? &tmallocs : &mallocs)->add(res); 497 return res; 498 } 499 fillbytes& xsmallbuf = *(temp ? &tsmallbuf : &smallbuf); 500 if (!xsmallbuf.canAppend(size+1)) { 501 xsmallbuf.init(CHUNK); 502 (temp ? &tmallocs : &mallocs)->add(xsmallbuf.base()); 503 } 504 int growBy = (int)size; 505 growBy += -growBy & 7; // round up mod 8 506 return xsmallbuf.grow(growBy); 507 } 508 509 maybe_inline 510 void unpacker::saveTo(bytes& b, byte* ptr, size_t len) { 511 b.ptr = U_NEW(byte, add_size(len,1)); 512 if (aborting()) { 513 b.len = 0; 514 return; 515 } 516 b.len = len; 517 b.copyFrom(ptr, len); 518 } 519 520 // Read up through band_headers. 521 // Do the archive_size dance to set the size of the input mega-buffer. 522 void unpacker::read_file_header() { 523 // Read file header to determine file type and total size. 524 enum { 525 MAGIC_BYTES = 4, 526 AH_LENGTH_0 = 3, //minver, majver, options are outside of archive_size 527 AH_LENGTH_0_MAX = AH_LENGTH_0 + 1, // options might have 2 bytes 528 AH_LENGTH = 26, //maximum archive header length (w/ all fields) 529 // Length contributions from optional header fields: 530 AH_FILE_HEADER_LEN = 5, // sizehi/lo/next/modtime/files 531 AH_ARCHIVE_SIZE_LEN = 2, // sizehi/lo only; part of AH_FILE_HEADER_LEN 532 AH_CP_NUMBER_LEN = 4, // int/float/long/double 533 AH_SPECIAL_FORMAT_LEN = 2, // layouts/band-headers 534 AH_LENGTH_MIN = AH_LENGTH 535 -(AH_FILE_HEADER_LEN+AH_SPECIAL_FORMAT_LEN+AH_CP_NUMBER_LEN), 536 ARCHIVE_SIZE_MIN = AH_LENGTH_MIN - (AH_LENGTH_0 + AH_ARCHIVE_SIZE_LEN), 537 FIRST_READ = MAGIC_BYTES + AH_LENGTH_MIN 538 }; 539 540 assert(AH_LENGTH_MIN == 15); // # of UNSIGNED5 fields required after archive_magic 541 assert(ARCHIVE_SIZE_MIN == 10); // # of UNSIGNED5 fields required after archive_size 542 // An absolute minimum null archive is magic[4], {minver,majver,options}[3], 543 // archive_size[0], cp_counts[8], class_counts[4], for a total of 19 bytes. 544 // (Note that archive_size is optional; it may be 0..10 bytes in length.) 545 // The first read must capture everything up through the options field. 546 // This happens to work even if {minver,majver,options} is a pathological 547 // 15 bytes long. Legal pack files limit those three fields to 1+1+2 bytes. 548 assert(FIRST_READ >= MAGIC_BYTES + AH_LENGTH_0 * B_MAX); 549 550 // Up through archive_size, the largest possible archive header is 551 // magic[4], {minver,majver,options}[4], archive_size[10]. 552 // (Note only the low 12 bits of options are allowed to be non-zero.) 553 // In order to parse archive_size, we need at least this many bytes 554 // in the first read. Of course, if archive_size_hi is more than 555 // a byte, we probably will fail to allocate the buffer, since it 556 // will be many gigabytes long. This is a practical, not an 557 // architectural limit to Pack200 archive sizes. 558 assert(FIRST_READ >= MAGIC_BYTES + AH_LENGTH_0_MAX + 2*B_MAX); 559 560 bool foreign_buf = (read_input_fn == null); 561 byte initbuf[(int)FIRST_READ + (int)C_SLOP + 200]; // 200 is for JAR I/O 562 if (foreign_buf) { 563 // inbytes is all there is 564 input.set(inbytes); 565 rp = input.base(); 566 rplimit = input.limit(); 567 } else { 568 // inbytes, if not empty, contains some read-ahead we must use first 569 // ensure_input will take care of copying it into initbuf, 570 // then querying read_input_fn for any additional data needed. 571 // However, the caller must assume that we use up all of inbytes. 572 // There is no way to tell the caller that we used only part of them. 573 // Therefore, the caller must use only a bare minimum of read-ahead. 574 if (inbytes.len > FIRST_READ) { 575 abort("too much read-ahead"); 576 return; 577 } 578 input.set(initbuf, sizeof(initbuf)); 579 input.b.clear(); 580 input.b.copyFrom(inbytes); 581 rplimit = rp = input.base(); 582 rplimit += inbytes.len; 583 bytes_read += inbytes.len; 584 } 585 // Read only 19 bytes, which is certain to contain #archive_options fields, 586 // but is certain not to overflow past the archive_header. 587 input.b.len = FIRST_READ; 588 if (!ensure_input(FIRST_READ)) 589 abort("EOF reading archive magic number"); 590 591 if (rp[0] == 'P' && rp[1] == 'K') { 592 #ifdef UNPACK_JNI 593 // Java driver must handle this case before we get this far. 594 abort("encountered a JAR header in unpacker"); 595 #else 596 // In the Unix-style program, we simply simulate a copy command. 597 // Copy until EOF; assume the JAR file is the last segment. 598 fprintf(errstrm, "Copy-mode.\n"); 599 for (;;) { 600 jarout->write_data(rp, (int)input_remaining()); 601 if (foreign_buf) 602 break; // one-time use of a passed in buffer 603 if (input.size() < CHUNK) { 604 // Get some breathing room. 605 input.set(U_NEW(byte, (size_t) CHUNK + C_SLOP), (size_t) CHUNK); 606 CHECK; 607 } 608 rp = rplimit = input.base(); 609 if (!ensure_input(1)) 610 break; 611 } 612 jarout->closeJarFile(false); 613 #endif 614 return; 615 } 616 617 // Read the magic number. 618 magic = 0; 619 for (int i1 = 0; i1 < (int)sizeof(magic); i1++) { 620 magic <<= 8; 621 magic += (*rp++ & 0xFF); 622 } 623 624 // Read the first 3 values from the header. 625 value_stream hdr; 626 int hdrVals = 0; 627 int hdrValsSkipped = 0; // debug only 628 hdr.init(rp, rplimit, UNSIGNED5_spec); 629 minver = hdr.getInt(); 630 majver = hdr.getInt(); 631 hdrVals += 2; 632 633 if (magic != (int)JAVA_PACKAGE_MAGIC || 634 (majver != JAVA5_PACKAGE_MAJOR_VERSION && 635 majver != JAVA6_PACKAGE_MAJOR_VERSION) || 636 (minver != JAVA5_PACKAGE_MINOR_VERSION && 637 minver != JAVA6_PACKAGE_MINOR_VERSION)) { 638 char message[200]; 639 sprintf(message, "@" ERROR_FORMAT ": magic/ver = " 640 "%08X/%d.%d should be %08X/%d.%d OR %08X/%d.%d\n", 641 magic, majver, minver, 642 JAVA_PACKAGE_MAGIC, JAVA5_PACKAGE_MAJOR_VERSION, JAVA5_PACKAGE_MINOR_VERSION, 643 JAVA_PACKAGE_MAGIC, JAVA6_PACKAGE_MAJOR_VERSION, JAVA6_PACKAGE_MINOR_VERSION); 644 abort(message); 645 } 646 CHECK; 647 648 archive_options = hdr.getInt(); 649 hdrVals += 1; 650 assert(hdrVals == AH_LENGTH_0); // first three fields only 651 652 #define ORBIT(bit) |(bit) 653 int OPTION_LIMIT = (0 ARCHIVE_BIT_DO(ORBIT)); 654 #undef ORBIT 655 if ((archive_options & ~OPTION_LIMIT) != 0) { 656 fprintf(errstrm, "Warning: Illegal archive options 0x%x\n", 657 archive_options); 658 abort("illegal archive options"); 659 return; 660 } 661 662 if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { 663 uint hi = hdr.getInt(); 664 uint lo = hdr.getInt(); 665 julong x = band::makeLong(hi, lo); 666 archive_size = (size_t) x; 667 if (archive_size != x) { 668 // Silly size specified; force overflow. 669 archive_size = PSIZE_MAX+1; 670 } 671 hdrVals += 2; 672 } else { 673 hdrValsSkipped += 2; 674 } 675 676 // Now we can size the whole archive. 677 // Read everything else into a mega-buffer. 678 rp = hdr.rp; 679 size_t header_size_0 = (rp - input.base()); // used-up header (4byte + 3int) 680 size_t header_size_1 = (rplimit - rp); // buffered unused initial fragment 681 size_t header_size = header_size_0 + header_size_1; 682 unsized_bytes_read = header_size_0; 683 CHECK; 684 if (foreign_buf) { 685 if (archive_size > header_size_1) { 686 abort("EOF reading fixed input buffer"); 687 return; 688 } 689 } else if (archive_size != 0) { 690 if (archive_size < ARCHIVE_SIZE_MIN) { 691 abort("impossible archive size"); // bad input data 692 return; 693 } 694 if (archive_size < header_size_1) { 695 abort("too much read-ahead"); // somehow we pre-fetched too much? 696 return; 697 } 698 input.set(U_NEW(byte, add_size(header_size_0, archive_size, C_SLOP)), 699 header_size_0 + archive_size); 700 CHECK; 701 assert(input.limit()[0] == 0); 702 // Move all the bytes we read initially into the real buffer. 703 input.b.copyFrom(initbuf, header_size); 704 rp = input.b.ptr + header_size_0; 705 rplimit = input.b.ptr + header_size; 706 } else { 707 // It's more complicated and painful. 708 // A zero archive_size means that we must read until EOF. 709 input.init(CHUNK*2); 710 CHECK; 711 input.b.len = input.allocated; 712 rp = rplimit = input.base(); 713 // Set up input buffer as if we already read the header: 714 input.b.copyFrom(initbuf, header_size); 715 CHECK; 716 rplimit += header_size; 717 while (ensure_input(input.limit() - rp)) { 718 size_t dataSoFar = input_remaining(); 719 size_t nextSize = add_size(dataSoFar, CHUNK); 720 input.ensureSize(nextSize); 721 CHECK; 722 input.b.len = input.allocated; 723 rp = rplimit = input.base(); 724 rplimit += dataSoFar; 725 } 726 size_t dataSize = (rplimit - input.base()); 727 input.b.len = dataSize; 728 input.grow(C_SLOP); 729 CHECK; 730 free_input = true; // free it later 731 input.b.len = dataSize; 732 assert(input.limit()[0] == 0); 733 rp = rplimit = input.base(); 734 rplimit += dataSize; 735 rp += header_size_0; // already scanned these bytes... 736 } 737 live_input = true; // mark as "do not reuse" 738 if (aborting()) { 739 abort("cannot allocate large input buffer for package file"); 740 return; 741 } 742 743 // read the rest of the header fields 744 ensure_input((AH_LENGTH-AH_LENGTH_0) * B_MAX); 745 CHECK; 746 hdr.rp = rp; 747 hdr.rplimit = rplimit; 748 749 if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) { 750 archive_next_count = hdr.getInt(); 751 CHECK_COUNT(archive_next_count); 752 archive_modtime = hdr.getInt(); 753 file_count = hdr.getInt(); 754 CHECK_COUNT(file_count); 755 hdrVals += 3; 756 } else { 757 hdrValsSkipped += 3; 758 } 759 760 if ((archive_options & AO_HAVE_SPECIAL_FORMATS) != 0) { 761 band_headers_size = hdr.getInt(); 762 CHECK_COUNT(band_headers_size); 763 attr_definition_count = hdr.getInt(); 764 CHECK_COUNT(attr_definition_count); 765 hdrVals += 2; 766 } else { 767 hdrValsSkipped += 2; 768 } 769 770 int cp_counts[N_TAGS_IN_ORDER]; 771 for (int k = 0; k < (int)N_TAGS_IN_ORDER; k++) { 772 if (!(archive_options & AO_HAVE_CP_NUMBERS)) { 773 switch (TAGS_IN_ORDER[k]) { 774 case CONSTANT_Integer: 775 case CONSTANT_Float: 776 case CONSTANT_Long: 777 case CONSTANT_Double: 778 cp_counts[k] = 0; 779 hdrValsSkipped += 1; 780 continue; 781 } 782 } 783 cp_counts[k] = hdr.getInt(); 784 CHECK_COUNT(cp_counts[k]); 785 hdrVals += 1; 786 } 787 788 ic_count = hdr.getInt(); 789 CHECK_COUNT(ic_count); 790 default_class_minver = hdr.getInt(); 791 default_class_majver = hdr.getInt(); 792 class_count = hdr.getInt(); 793 CHECK_COUNT(class_count); 794 hdrVals += 4; 795 796 // done with archive_header 797 hdrVals += hdrValsSkipped; 798 assert(hdrVals == AH_LENGTH); 799 #ifndef PRODUCT 800 int assertSkipped = AH_LENGTH - AH_LENGTH_MIN; 801 if ((archive_options & AO_HAVE_FILE_HEADERS) != 0) 802 assertSkipped -= AH_FILE_HEADER_LEN; 803 if ((archive_options & AO_HAVE_SPECIAL_FORMATS) != 0) 804 assertSkipped -= AH_SPECIAL_FORMAT_LEN; 805 if ((archive_options & AO_HAVE_CP_NUMBERS) != 0) 806 assertSkipped -= AH_CP_NUMBER_LEN; 807 assert(hdrValsSkipped == assertSkipped); 808 #endif //PRODUCT 809 810 rp = hdr.rp; 811 if (rp > rplimit) 812 abort("EOF reading archive header"); 813 814 // Now size the CP. 815 #ifndef PRODUCT 816 bool x = (N_TAGS_IN_ORDER == cpool::NUM_COUNTS); 817 assert(x); 818 #endif //PRODUCT 819 cp.init(this, cp_counts); 820 CHECK; 821 822 default_file_modtime = archive_modtime; 823 if (default_file_modtime == 0 && !(archive_options & AO_HAVE_FILE_MODTIME)) 824 default_file_modtime = DEFAULT_ARCHIVE_MODTIME; // taken from driver 825 if ((archive_options & AO_DEFLATE_HINT) != 0) 826 default_file_options |= FO_DEFLATE_HINT; 827 828 // meta-bytes, if any, immediately follow archive header 829 //band_headers.readData(band_headers_size); 830 ensure_input(band_headers_size); 831 if (input_remaining() < (size_t)band_headers_size) { 832 abort("EOF reading band headers"); 833 return; 834 } 835 bytes band_headers; 836 // The "1+" allows an initial byte to be pushed on the front. 837 band_headers.set(1+U_NEW(byte, 1+band_headers_size+C_SLOP), 838 band_headers_size); 839 CHECK; 840 // Start scanning band headers here: 841 band_headers.copyFrom(rp, band_headers.len); 842 rp += band_headers.len; 843 assert(rp <= rplimit); 844 meta_rp = band_headers.ptr; 845 // Put evil meta-codes at the end of the band headers, 846 // so we are sure to throw an error if we run off the end. 847 bytes::of(band_headers.limit(), C_SLOP).clear(_meta_error); 848 } 849 850 void unpacker::finish() { 851 if (verbose >= 1) { 852 fprintf(errstrm, 853 "A total of " 854 LONG_LONG_FORMAT " bytes were read in %d segment(s).\n", 855 (bytes_read_before_reset+bytes_read), 856 segments_read_before_reset+1); 857 fprintf(errstrm, 858 "A total of " 859 LONG_LONG_FORMAT " file content bytes were written.\n", 860 (bytes_written_before_reset+bytes_written)); 861 fprintf(errstrm, 862 "A total of %d files (of which %d are classes) were written to output.\n", 863 files_written_before_reset+files_written, 864 classes_written_before_reset+classes_written); 865 } 866 if (jarout != null) 867 jarout->closeJarFile(true); 868 if (errstrm != null) { 869 if (errstrm == stdout || errstrm == stderr) { 870 fflush(errstrm); 871 } else { 872 fclose(errstrm); 873 } 874 errstrm = null; 875 errstrm_name = null; 876 } 877 } 878 879 880 // Cf. PackageReader.readConstantPoolCounts 881 void cpool::init(unpacker* u_, int counts[NUM_COUNTS]) { 882 this->u = u_; 883 884 // Fill-pointer for CP. 885 int next_entry = 0; 886 887 // Size the constant pool: 888 for (int k = 0; k < (int)N_TAGS_IN_ORDER; k++) { 889 byte tag = TAGS_IN_ORDER[k]; 890 int len = counts[k]; 891 tag_count[tag] = len; 892 tag_base[tag] = next_entry; 893 next_entry += len; 894 // Detect and defend against constant pool size overflow. 895 // (Pack200 forbids the sum of CP counts to exceed 2^29-1.) 896 enum { 897 CP_SIZE_LIMIT = (1<<29), 898 IMPLICIT_ENTRY_COUNT = 1 // empty Utf8 string 899 }; 900 if (len >= (1<<29) || len < 0 901 || next_entry >= CP_SIZE_LIMIT+IMPLICIT_ENTRY_COUNT) { 902 abort("archive too large: constant pool limit exceeded"); 903 return; 904 } 905 } 906 907 // Close off the end of the CP: 908 nentries = next_entry; 909 910 // place a limit on future CP growth: 911 size_t generous = 0; 912 generous = add_size(generous, u->ic_count); // implicit name 913 generous = add_size(generous, u->ic_count); // outer 914 generous = add_size(generous, u->ic_count); // outer.utf8 915 generous = add_size(generous, 40); // WKUs, misc 916 generous = add_size(generous, u->class_count); // implicit SourceFile strings 917 maxentries = (uint)add_size(nentries, generous); 918 919 // Note that this CP does not include "empty" entries 920 // for longs and doubles. Those are introduced when 921 // the entries are renumbered for classfile output. 922 923 entries = U_NEW(entry, maxentries); 924 CHECK; 925 926 first_extra_entry = &entries[nentries]; 927 928 // Initialize the standard indexes. 929 tag_count[CONSTANT_All] = nentries; 930 tag_base[ CONSTANT_All] = 0; 931 for (int tag = 0; tag < CONSTANT_Limit; tag++) { 932 entry* cpMap = &entries[tag_base[tag]]; 933 tag_index[tag].init(tag_count[tag], cpMap, tag); 934 } 935 936 // Initialize hashTab to a generous power-of-two size. 937 uint pow2 = 1; 938 uint target = maxentries + maxentries/2; // 60% full 939 while (pow2 < target) pow2 <<= 1; 940 hashTab = U_NEW(entry*, hashTabLength = pow2); 941 } 942 943 static byte* store_Utf8_char(byte* cp, unsigned short ch) { 944 if (ch >= 0x001 && ch <= 0x007F) { 945 *cp++ = (byte) ch; 946 } else if (ch <= 0x07FF) { 947 *cp++ = (byte) (0xC0 | ((ch >> 6) & 0x1F)); 948 *cp++ = (byte) (0x80 | ((ch >> 0) & 0x3F)); 949 } else { 950 *cp++ = (byte) (0xE0 | ((ch >> 12) & 0x0F)); 951 *cp++ = (byte) (0x80 | ((ch >> 6) & 0x3F)); 952 *cp++ = (byte) (0x80 | ((ch >> 0) & 0x3F)); 953 } 954 return cp; 955 } 956 957 static byte* skip_Utf8_chars(byte* cp, int len) { 958 for (;; cp++) { 959 int ch = *cp & 0xFF; 960 if ((ch & 0xC0) != 0x80) { 961 if (len-- == 0) 962 return cp; 963 if (ch < 0x80 && len == 0) 964 return cp+1; 965 } 966 } 967 } 968 969 static int compare_Utf8_chars(bytes& b1, bytes& b2) { 970 int l1 = (int)b1.len; 971 int l2 = (int)b2.len; 972 int l0 = (l1 < l2) ? l1 : l2; 973 byte* p1 = b1.ptr; 974 byte* p2 = b2.ptr; 975 int c0 = 0; 976 for (int i = 0; i < l0; i++) { 977 int c1 = p1[i] & 0xFF; 978 int c2 = p2[i] & 0xFF; 979 if (c1 != c2) { 980 // Before returning the obvious answer, 981 // check to see if c1 or c2 is part of a 0x0000, 982 // which encodes as {0xC0,0x80}. The 0x0000 is the 983 // lowest-sorting Java char value, and yet it encodes 984 // as if it were the first char after 0x7F, which causes 985 // strings containing nulls to sort too high. All other 986 // comparisons are consistent between Utf8 and Java chars. 987 if (c1 == 0xC0 && (p1[i+1] & 0xFF) == 0x80) c1 = 0; 988 if (c2 == 0xC0 && (p2[i+1] & 0xFF) == 0x80) c2 = 0; 989 if (c0 == 0xC0) { 990 assert(((c1|c2) & 0xC0) == 0x80); // c1 & c2 are extension chars 991 if (c1 == 0x80) c1 = 0; // will sort below c2 992 if (c2 == 0x80) c2 = 0; // will sort below c1 993 } 994 return c1 - c2; 995 } 996 c0 = c1; // save away previous char 997 } 998 // common prefix is identical; return length difference if any 999 return l1 - l2; 1000 } 1001 1002 // Cf. PackageReader.readUtf8Bands 1003 local_inline 1004 void unpacker::read_Utf8_values(entry* cpMap, int len) { 1005 // Implicit first Utf8 string is the empty string. 1006 enum { 1007 // certain bands begin with implicit zeroes 1008 PREFIX_SKIP_2 = 2, 1009 SUFFIX_SKIP_1 = 1 1010 }; 1011 1012 int i; 1013 1014 // First band: Read lengths of shared prefixes. 1015 if (len > PREFIX_SKIP_2) 1016 cp_Utf8_prefix.readData(len - PREFIX_SKIP_2); 1017 NOT_PRODUCT(else cp_Utf8_prefix.readData(0)); // for asserts 1018 1019 // Second band: Read lengths of unshared suffixes: 1020 if (len > SUFFIX_SKIP_1) 1021 cp_Utf8_suffix.readData(len - SUFFIX_SKIP_1); 1022 NOT_PRODUCT(else cp_Utf8_suffix.readData(0)); // for asserts 1023 1024 bytes* allsuffixes = T_NEW(bytes, len); 1025 CHECK; 1026 1027 int nbigsuf = 0; 1028 fillbytes charbuf; // buffer to allocate small strings 1029 charbuf.init(); 1030 1031 // Third band: Read the char values in the unshared suffixes: 1032 cp_Utf8_chars.readData(cp_Utf8_suffix.getIntTotal()); 1033 for (i = 0; i < len; i++) { 1034 int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt(); 1035 if (suffix < 0) { 1036 abort("bad utf8 suffix"); 1037 return; 1038 } 1039 if (suffix == 0 && i >= SUFFIX_SKIP_1) { 1040 // chars are packed in cp_Utf8_big_chars 1041 nbigsuf += 1; 1042 continue; 1043 } 1044 bytes& chars = allsuffixes[i]; 1045 uint size3 = suffix * 3; // max Utf8 length 1046 bool isMalloc = (suffix > SMALL); 1047 if (isMalloc) { 1048 chars.malloc(size3); 1049 } else { 1050 if (!charbuf.canAppend(size3+1)) { 1051 assert(charbuf.allocated == 0 || tmallocs.contains(charbuf.base())); 1052 charbuf.init(CHUNK); // Reset to new buffer. 1053 tmallocs.add(charbuf.base()); 1054 } 1055 chars.set(charbuf.grow(size3+1), size3); 1056 } 1057 CHECK; 1058 byte* chp = chars.ptr; 1059 for (int j = 0; j < suffix; j++) { 1060 unsigned short ch = cp_Utf8_chars.getInt(); 1061 chp = store_Utf8_char(chp, ch); 1062 } 1063 // shrink to fit: 1064 if (isMalloc) { 1065 chars.realloc(chp - chars.ptr); 1066 CHECK; 1067 tmallocs.add(chars.ptr); // free it later 1068 } else { 1069 int shrink = (int)(chars.limit() - chp); 1070 chars.len -= shrink; 1071 charbuf.b.len -= shrink; // ungrow to reclaim buffer space 1072 // Note that we did not reclaim the final '\0'. 1073 assert(chars.limit() == charbuf.limit()-1); 1074 assert(strlen((char*)chars.ptr) == chars.len); 1075 } 1076 } 1077 //cp_Utf8_chars.done(); 1078 #ifndef PRODUCT 1079 charbuf.b.set(null, 0); // tidy 1080 #endif 1081 1082 // Fourth band: Go back and size the specially packed strings. 1083 int maxlen = 0; 1084 cp_Utf8_big_suffix.readData(nbigsuf); 1085 cp_Utf8_suffix.rewind(); 1086 for (i = 0; i < len; i++) { 1087 int suffix = (i < SUFFIX_SKIP_1)? 0: cp_Utf8_suffix.getInt(); 1088 int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt(); 1089 if (prefix < 0 || prefix+suffix < 0) { 1090 abort("bad utf8 prefix"); 1091 return; 1092 } 1093 bytes& chars = allsuffixes[i]; 1094 if (suffix == 0 && i >= SUFFIX_SKIP_1) { 1095 suffix = cp_Utf8_big_suffix.getInt(); 1096 assert(chars.ptr == null); 1097 chars.len = suffix; // just a momentary hack 1098 } else { 1099 assert(chars.ptr != null); 1100 } 1101 if (maxlen < prefix + suffix) { 1102 maxlen = prefix + suffix; 1103 } 1104 } 1105 //cp_Utf8_suffix.done(); // will use allsuffixes[i].len (ptr!=null) 1106 //cp_Utf8_big_suffix.done(); // will use allsuffixes[i].len 1107 1108 // Fifth band(s): Get the specially packed characters. 1109 cp_Utf8_big_suffix.rewind(); 1110 for (i = 0; i < len; i++) { 1111 bytes& chars = allsuffixes[i]; 1112 if (chars.ptr != null) continue; // already input 1113 int suffix = (int)chars.len; // pick up the hack 1114 uint size3 = suffix * 3; 1115 if (suffix == 0) continue; // done with empty string 1116 chars.malloc(size3); 1117 CHECK; 1118 byte* chp = chars.ptr; 1119 band saved_band = cp_Utf8_big_chars; 1120 cp_Utf8_big_chars.readData(suffix); 1121 CHECK; 1122 for (int j = 0; j < suffix; j++) { 1123 unsigned short ch = cp_Utf8_big_chars.getInt(); 1124 CHECK; 1125 chp = store_Utf8_char(chp, ch); 1126 } 1127 chars.realloc(chp - chars.ptr); 1128 CHECK; 1129 tmallocs.add(chars.ptr); // free it later 1130 //cp_Utf8_big_chars.done(); 1131 cp_Utf8_big_chars = saved_band; // reset the band for the next string 1132 } 1133 cp_Utf8_big_chars.readData(0); // zero chars 1134 //cp_Utf8_big_chars.done(); 1135 1136 // Finally, sew together all the prefixes and suffixes. 1137 bytes bigbuf; 1138 bigbuf.malloc(maxlen * 3 + 1); // max Utf8 length, plus slop for null 1139 CHECK; 1140 int prevlen = 0; // previous string length (in chars) 1141 tmallocs.add(bigbuf.ptr); // free after this block 1142 CHECK; 1143 cp_Utf8_prefix.rewind(); 1144 for (i = 0; i < len; i++) { 1145 bytes& chars = allsuffixes[i]; 1146 int prefix = (i < PREFIX_SKIP_2)? 0: cp_Utf8_prefix.getInt(); 1147 CHECK; 1148 int suffix = (int)chars.len; 1149 byte* fillp; 1150 // by induction, the buffer is already filled with the prefix 1151 // make sure the prefix value is not corrupted, though: 1152 if (prefix > prevlen) { 1153 abort("utf8 prefix overflow"); 1154 return; 1155 } 1156 fillp = skip_Utf8_chars(bigbuf.ptr, prefix); 1157 // copy the suffix into the same buffer: 1158 fillp = chars.writeTo(fillp); 1159 assert(bigbuf.inBounds(fillp)); 1160 *fillp = 0; // bigbuf must contain a well-formed Utf8 string 1161 int length = (int)(fillp - bigbuf.ptr); 1162 bytes& value = cpMap[i].value.b; 1163 value.set(U_NEW(byte, add_size(length,1)), length); 1164 value.copyFrom(bigbuf.ptr, length); 1165 CHECK; 1166 // Index all Utf8 strings 1167 entry* &htref = cp.hashTabRef(CONSTANT_Utf8, value); 1168 if (htref == null) { 1169 // Note that if two identical strings are transmitted, 1170 // the first is taken to be the canonical one. 1171 htref = &cpMap[i]; 1172 } 1173 prevlen = prefix + suffix; 1174 } 1175 //cp_Utf8_prefix.done(); 1176 1177 // Free intermediate buffers. 1178 free_temps(); 1179 } 1180 1181 local_inline 1182 void unpacker::read_single_words(band& cp_band, entry* cpMap, int len) { 1183 cp_band.readData(len); 1184 for (int i = 0; i < len; i++) { 1185 cpMap[i].value.i = cp_band.getInt(); // coding handles signs OK 1186 } 1187 } 1188 1189 maybe_inline 1190 void unpacker::read_double_words(band& cp_bands, entry* cpMap, int len) { 1191 band& cp_band_hi = cp_bands; 1192 band& cp_band_lo = cp_bands.nextBand(); 1193 cp_band_hi.readData(len); 1194 cp_band_lo.readData(len); 1195 for (int i = 0; i < len; i++) { 1196 cpMap[i].value.l = cp_band_hi.getLong(cp_band_lo, true); 1197 } 1198 //cp_band_hi.done(); 1199 //cp_band_lo.done(); 1200 } 1201 1202 maybe_inline 1203 void unpacker::read_single_refs(band& cp_band, byte refTag, entry* cpMap, int len) { 1204 assert(refTag == CONSTANT_Utf8); 1205 cp_band.setIndexByTag(refTag); 1206 cp_band.readData(len); 1207 CHECK; 1208 int indexTag = (cp_band.bn == e_cp_Class) ? CONSTANT_Class : 0; 1209 for (int i = 0; i < len; i++) { 1210 entry& e = cpMap[i]; 1211 e.refs = U_NEW(entry*, e.nrefs = 1); 1212 entry* utf = cp_band.getRef(); 1213 CHECK; 1214 e.refs[0] = utf; 1215 e.value.b = utf->value.b; // copy value of Utf8 string to self 1216 if (indexTag != 0) { 1217 // Maintain cross-reference: 1218 entry* &htref = cp.hashTabRef(indexTag, e.value.b); 1219 if (htref == null) { 1220 // Note that if two identical classes are transmitted, 1221 // the first is taken to be the canonical one. 1222 htref = &e; 1223 } 1224 } 1225 } 1226 //cp_band.done(); 1227 } 1228 1229 maybe_inline 1230 void unpacker::read_double_refs(band& cp_band, byte ref1Tag, byte ref2Tag, 1231 entry* cpMap, int len) { 1232 band& cp_band1 = cp_band; 1233 band& cp_band2 = cp_band.nextBand(); 1234 cp_band1.setIndexByTag(ref1Tag); 1235 cp_band2.setIndexByTag(ref2Tag); 1236 cp_band1.readData(len); 1237 cp_band2.readData(len); 1238 CHECK; 1239 for (int i = 0; i < len; i++) { 1240 entry& e = cpMap[i]; 1241 e.refs = U_NEW(entry*, e.nrefs = 2); 1242 e.refs[0] = cp_band1.getRef(); 1243 CHECK; 1244 e.refs[1] = cp_band2.getRef(); 1245 CHECK; 1246 } 1247 //cp_band1.done(); 1248 //cp_band2.done(); 1249 } 1250 1251 // Cf. PackageReader.readSignatureBands 1252 maybe_inline 1253 void unpacker::read_signature_values(entry* cpMap, int len) { 1254 cp_Signature_form.setIndexByTag(CONSTANT_Utf8); 1255 cp_Signature_form.readData(len); 1256 CHECK; 1257 int ncTotal = 0; 1258 int i; 1259 for (i = 0; i < len; i++) { 1260 entry& e = cpMap[i]; 1261 entry& form = *cp_Signature_form.getRef(); 1262 CHECK; 1263 int nc = 0; 1264 1265 for ( const char* ncp = form.utf8String() ; *ncp; ncp++) { 1266 if (*ncp == 'L') nc++; 1267 } 1268 1269 ncTotal += nc; 1270 e.refs = U_NEW(entry*, cpMap[i].nrefs = 1 + nc); 1271 CHECK; 1272 e.refs[0] = &form; 1273 } 1274 //cp_Signature_form.done(); 1275 cp_Signature_classes.setIndexByTag(CONSTANT_Class); 1276 cp_Signature_classes.readData(ncTotal); 1277 for (i = 0; i < len; i++) { 1278 entry& e = cpMap[i]; 1279 for (int j = 1; j < e.nrefs; j++) { 1280 e.refs[j] = cp_Signature_classes.getRef(); 1281 CHECK; 1282 } 1283 } 1284 //cp_Signature_classes.done(); 1285 } 1286 1287 // Cf. PackageReader.readConstantPool 1288 void unpacker::read_cp() { 1289 byte* rp0 = rp; 1290 1291 int i; 1292 1293 for (int k = 0; k < (int)N_TAGS_IN_ORDER; k++) { 1294 byte tag = TAGS_IN_ORDER[k]; 1295 int len = cp.tag_count[tag]; 1296 int base = cp.tag_base[tag]; 1297 1298 PRINTCR((1,"Reading %d %s entries...", len, NOT_PRODUCT(TAG_NAME[tag])+0)); 1299 entry* cpMap = &cp.entries[base]; 1300 for (i = 0; i < len; i++) { 1301 cpMap[i].tag = tag; 1302 cpMap[i].inord = i; 1303 } 1304 1305 switch (tag) { 1306 case CONSTANT_Utf8: 1307 read_Utf8_values(cpMap, len); 1308 break; 1309 case CONSTANT_Integer: 1310 read_single_words(cp_Int, cpMap, len); 1311 break; 1312 case CONSTANT_Float: 1313 read_single_words(cp_Float, cpMap, len); 1314 break; 1315 case CONSTANT_Long: 1316 read_double_words(cp_Long_hi /*& cp_Long_lo*/, cpMap, len); 1317 break; 1318 case CONSTANT_Double: 1319 read_double_words(cp_Double_hi /*& cp_Double_lo*/, cpMap, len); 1320 break; 1321 case CONSTANT_String: 1322 read_single_refs(cp_String, CONSTANT_Utf8, cpMap, len); 1323 break; 1324 case CONSTANT_Class: 1325 read_single_refs(cp_Class, CONSTANT_Utf8, cpMap, len); 1326 break; 1327 case CONSTANT_Signature: 1328 read_signature_values(cpMap, len); 1329 break; 1330 case CONSTANT_NameandType: 1331 read_double_refs(cp_Descr_name /*& cp_Descr_type*/, 1332 CONSTANT_Utf8, CONSTANT_Signature, 1333 cpMap, len); 1334 break; 1335 case CONSTANT_Fieldref: 1336 read_double_refs(cp_Field_class /*& cp_Field_desc*/, 1337 CONSTANT_Class, CONSTANT_NameandType, 1338 cpMap, len); 1339 break; 1340 case CONSTANT_Methodref: 1341 read_double_refs(cp_Method_class /*& cp_Method_desc*/, 1342 CONSTANT_Class, CONSTANT_NameandType, 1343 cpMap, len); 1344 break; 1345 case CONSTANT_InterfaceMethodref: 1346 read_double_refs(cp_Imethod_class /*& cp_Imethod_desc*/, 1347 CONSTANT_Class, CONSTANT_NameandType, 1348 cpMap, len); 1349 break; 1350 default: 1351 assert(false); 1352 break; 1353 } 1354 1355 // Initialize the tag's CP index right away, since it might be needed 1356 // in the next pass to initialize the CP for another tag. 1357 #ifndef PRODUCT 1358 cpindex* ix = &cp.tag_index[tag]; 1359 assert(ix->ixTag == tag); 1360 assert((int)ix->len == len); 1361 assert(ix->base1 == cpMap); 1362 #endif 1363 CHECK; 1364 } 1365 1366 cp.expandSignatures(); 1367 CHECK; 1368 cp.initMemberIndexes(); 1369 CHECK; 1370 1371 PRINTCR((1,"parsed %d constant pool entries in %d bytes", cp.nentries, (rp - rp0))); 1372 1373 #define SNAME(n,s) #s "\0" 1374 const char* symNames = ( 1375 ALL_ATTR_DO(SNAME) 1376 "<init>" 1377 ); 1378 #undef SNAME 1379 1380 for (int sn = 0; sn < cpool::s_LIMIT; sn++) { 1381 assert(symNames[0] >= '0' && symNames[0] <= 'Z'); // sanity 1382 bytes name; name.set(symNames); 1383 if (name.len > 0 && name.ptr[0] != '0') { 1384 cp.sym[sn] = cp.ensureUtf8(name); 1385 PRINTCR((4, "well-known sym %d=%s", sn, cp.sym[sn]->string())); 1386 } 1387 symNames += name.len + 1; // skip trailing null to next name 1388 } 1389 1390 band::initIndexes(this); 1391 } 1392 1393 static band* no_bands[] = { null }; // shared empty body 1394 1395 inline 1396 band& unpacker::attr_definitions::fixed_band(int e_class_xxx) { 1397 return u->all_bands[xxx_flags_hi_bn + (e_class_xxx-e_class_flags_hi)]; 1398 } 1399 inline band& unpacker::attr_definitions::xxx_flags_hi() 1400 { return fixed_band(e_class_flags_hi); } 1401 inline band& unpacker::attr_definitions::xxx_flags_lo() 1402 { return fixed_band(e_class_flags_lo); } 1403 inline band& unpacker::attr_definitions::xxx_attr_count() 1404 { return fixed_band(e_class_attr_count); } 1405 inline band& unpacker::attr_definitions::xxx_attr_indexes() 1406 { return fixed_band(e_class_attr_indexes); } 1407 inline band& unpacker::attr_definitions::xxx_attr_calls() 1408 { return fixed_band(e_class_attr_calls); } 1409 1410 1411 inline 1412 unpacker::layout_definition* 1413 unpacker::attr_definitions::defineLayout(int idx, 1414 entry* nameEntry, 1415 const char* layout) { 1416 const char* name = nameEntry->value.b.strval(); 1417 layout_definition* lo = defineLayout(idx, name, layout); 1418 CHECK_0; 1419 lo->nameEntry = nameEntry; 1420 return lo; 1421 } 1422 1423 unpacker::layout_definition* 1424 unpacker::attr_definitions::defineLayout(int idx, 1425 const char* name, 1426 const char* layout) { 1427 assert(flag_limit != 0); // must be set up already 1428 if (idx >= 0) { 1429 // Fixed attr. 1430 if (idx >= (int)flag_limit) 1431 abort("attribute index too large"); 1432 if (isRedefined(idx)) 1433 abort("redefined attribute index"); 1434 redef |= ((julong)1<<idx); 1435 } else { 1436 idx = flag_limit + overflow_count.length(); 1437 overflow_count.add(0); // make a new counter 1438 } 1439 layout_definition* lo = U_NEW(layout_definition, 1); 1440 CHECK_0; 1441 lo->idx = idx; 1442 lo->name = name; 1443 lo->layout = layout; 1444 for (int adds = (idx+1) - layouts.length(); adds > 0; adds--) { 1445 layouts.add(null); 1446 } 1447 CHECK_0; 1448 layouts.get(idx) = lo; 1449 return lo; 1450 } 1451 1452 band** 1453 unpacker::attr_definitions::buildBands(unpacker::layout_definition* lo) { 1454 int i; 1455 if (lo->elems != null) 1456 return lo->bands(); 1457 if (lo->layout[0] == '\0') { 1458 lo->elems = no_bands; 1459 } else { 1460 // Create bands for this attribute by parsing the layout. 1461 bool hasCallables = lo->hasCallables(); 1462 bands_made = 0x10000; // base number for bands made 1463 const char* lp = lo->layout; 1464 lp = parseLayout(lp, lo->elems, -1); 1465 CHECK_0; 1466 if (lp[0] != '\0' || band_stack.length() > 0) { 1467 abort("garbage at end of layout"); 1468 } 1469 band_stack.popTo(0); 1470 CHECK_0; 1471 1472 // Fix up callables to point at their callees. 1473 band** bands = lo->elems; 1474 assert(bands == lo->bands()); 1475 int num_callables = 0; 1476 if (hasCallables) { 1477 while (bands[num_callables] != null) { 1478 if (bands[num_callables]->le_kind != EK_CBLE) { 1479 abort("garbage mixed with callables"); 1480 break; 1481 } 1482 num_callables += 1; 1483 } 1484 } 1485 for (i = 0; i < calls_to_link.length(); i++) { 1486 band& call = *(band*) calls_to_link.get(i); 1487 assert(call.le_kind == EK_CALL); 1488 // Determine the callee. 1489 int call_num = call.le_len; 1490 if (call_num < 0 || call_num >= num_callables) { 1491 abort("bad call in layout"); 1492 break; 1493 } 1494 band& cble = *bands[call_num]; 1495 // Link the call to it. 1496 call.le_body[0] = &cble; 1497 // Distinguish backward calls and callables: 1498 assert(cble.le_kind == EK_CBLE); 1499 assert(cble.le_len == call_num); 1500 cble.le_back |= call.le_back; 1501 } 1502 calls_to_link.popTo(0); 1503 } 1504 return lo->elems; 1505 } 1506 1507 /* attribute layout language parser 1508 1509 attribute_layout: 1510 ( layout_element )* | ( callable )+ 1511 layout_element: 1512 ( integral | replication | union | call | reference ) 1513 1514 callable: 1515 '[' body ']' 1516 body: 1517 ( layout_element )+ 1518 1519 integral: 1520 ( unsigned_int | signed_int | bc_index | bc_offset | flag ) 1521 unsigned_int: 1522 uint_type 1523 signed_int: 1524 'S' uint_type 1525 any_int: 1526 ( unsigned_int | signed_int ) 1527 bc_index: 1528 ( 'P' uint_type | 'PO' uint_type ) 1529 bc_offset: 1530 'O' any_int 1531 flag: 1532 'F' uint_type 1533 uint_type: 1534 ( 'B' | 'H' | 'I' | 'V' ) 1535 1536 replication: 1537 'N' uint_type '[' body ']' 1538 1539 union: 1540 'T' any_int (union_case)* '(' ')' '[' (body)? ']' 1541 union_case: 1542 '(' union_case_tag (',' union_case_tag)* ')' '[' (body)? ']' 1543 union_case_tag: 1544 ( numeral | numeral '-' numeral ) 1545 call: 1546 '(' numeral ')' 1547 1548 reference: 1549 reference_type ( 'N' )? uint_type 1550 reference_type: 1551 ( constant_ref | schema_ref | utf8_ref | untyped_ref ) 1552 constant_ref: 1553 ( 'KI' | 'KJ' | 'KF' | 'KD' | 'KS' | 'KQ' ) 1554 schema_ref: 1555 ( 'RC' | 'RS' | 'RD' | 'RF' | 'RM' | 'RI' ) 1556 utf8_ref: 1557 'RU' 1558 untyped_ref: 1559 'RQ' 1560 1561 numeral: 1562 '(' ('-')? (digit)+ ')' 1563 digit: 1564 ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ) 1565 1566 */ 1567 1568 const char* 1569 unpacker::attr_definitions::parseIntLayout(const char* lp, band* &res, 1570 byte le_kind, bool can_be_signed) { 1571 const char* lp0 = lp; 1572 band* b = U_NEW(band, 1); 1573 CHECK_(lp); 1574 char le = *lp++; 1575 int spec = UNSIGNED5_spec; 1576 if (le == 'S' && can_be_signed) { 1577 // Note: This is the last use of sign. There is no 'EF_SIGN'. 1578 spec = SIGNED5_spec; 1579 le = *lp++; 1580 } else if (le == 'B') { 1581 spec = BYTE1_spec; // unsigned byte 1582 } 1583 b->init(u, bands_made++, spec); 1584 b->le_kind = le_kind; 1585 int le_len = 0; 1586 switch (le) { 1587 case 'B': le_len = 1; break; 1588 case 'H': le_len = 2; break; 1589 case 'I': le_len = 4; break; 1590 case 'V': le_len = 0; break; 1591 default: abort("bad layout element"); 1592 } 1593 b->le_len = le_len; 1594 band_stack.add(b); 1595 res = b; 1596 return lp; 1597 } 1598 1599 const char* 1600 unpacker::attr_definitions::parseNumeral(const char* lp, int &res) { 1601 const char* lp0 = lp; 1602 bool sgn = false; 1603 if (*lp == '0') { res = 0; return lp+1; } // special case '0' 1604 if (*lp == '-') { sgn = true; lp++; } 1605 const char* dp = lp; 1606 int con = 0; 1607 while (*dp >= '0' && *dp <= '9') { 1608 int con0 = con; 1609 con *= 10; 1610 con += (*dp++) - '0'; 1611 if (con <= con0) { con = -1; break; } // numeral overflow 1612 } 1613 if (lp == dp) { 1614 abort("missing numeral in layout"); 1615 return ""; 1616 } 1617 lp = dp; 1618 if (con < 0 && !(sgn && con == -con)) { 1619 // (Portability note: Misses the error if int is not 32 bits.) 1620 abort("numeral overflow"); 1621 return "" ; 1622 } 1623 if (sgn) con = -con; 1624 res = con; 1625 return lp; 1626 } 1627 1628 band** 1629 unpacker::attr_definitions::popBody(int bs_base) { 1630 // Return everything that was pushed, as a null-terminated pointer array. 1631 int bs_limit = band_stack.length(); 1632 if (bs_base == bs_limit) { 1633 return no_bands; 1634 } else { 1635 int nb = bs_limit - bs_base; 1636 band** res = U_NEW(band*, add_size(nb, 1)); 1637 CHECK_(no_bands); 1638 for (int i = 0; i < nb; i++) { 1639 band* b = (band*) band_stack.get(bs_base + i); 1640 res[i] = b; 1641 } 1642 band_stack.popTo(bs_base); 1643 return res; 1644 } 1645 } 1646 1647 const char* 1648 unpacker::attr_definitions::parseLayout(const char* lp, band** &res, 1649 int curCble) { 1650 const char* lp0 = lp; 1651 int bs_base = band_stack.length(); 1652 bool top_level = (bs_base == 0); 1653 band* b; 1654 enum { can_be_signed = true }; // optional arg to parseIntLayout 1655 1656 for (bool done = false; !done; ) { 1657 switch (*lp++) { 1658 case 'B': case 'H': case 'I': case 'V': // unsigned_int 1659 case 'S': // signed_int 1660 --lp; // reparse 1661 case 'F': 1662 lp = parseIntLayout(lp, b, EK_INT); 1663 break; 1664 case 'P': 1665 { 1666 int le_bci = EK_BCI; 1667 if (*lp == 'O') { 1668 ++lp; 1669 le_bci = EK_BCID; 1670 } 1671 assert(*lp != 'S'); // no PSH, etc. 1672 lp = parseIntLayout(lp, b, EK_INT); 1673 b->le_bci = le_bci; 1674 if (le_bci == EK_BCI) 1675 b->defc = coding::findBySpec(BCI5_spec); 1676 else 1677 b->defc = coding::findBySpec(BRANCH5_spec); 1678 } 1679 break; 1680 case 'O': 1681 lp = parseIntLayout(lp, b, EK_INT, can_be_signed); 1682 b->le_bci = EK_BCO; 1683 b->defc = coding::findBySpec(BRANCH5_spec); 1684 break; 1685 case 'N': // replication: 'N' uint '[' elem ... ']' 1686 lp = parseIntLayout(lp, b, EK_REPL); 1687 assert(*lp == '['); 1688 ++lp; 1689 lp = parseLayout(lp, b->le_body, curCble); 1690 CHECK_(lp); 1691 break; 1692 case 'T': // union: 'T' any_int union_case* '(' ')' '[' body ']' 1693 lp = parseIntLayout(lp, b, EK_UN, can_be_signed); 1694 { 1695 int union_base = band_stack.length(); 1696 for (;;) { // for each case 1697 band& k_case = *U_NEW(band, 1); 1698 CHECK_(lp); 1699 band_stack.add(&k_case); 1700 k_case.le_kind = EK_CASE; 1701 k_case.bn = bands_made++; 1702 if (*lp++ != '(') { 1703 abort("bad union case"); 1704 return ""; 1705 } 1706 if (*lp++ != ')') { 1707 --lp; // reparse 1708 // Read some case values. (Use band_stack for temp. storage.) 1709 int case_base = band_stack.length(); 1710 for (;;) { 1711 int caseval = 0; 1712 lp = parseNumeral(lp, caseval); 1713 band_stack.add((void*)(size_t)caseval); 1714 if (*lp == '-') { 1715 // new in version 160, allow (1-5) for (1,2,3,4,5) 1716 if (u->majver < JAVA6_PACKAGE_MAJOR_VERSION) { 1717 abort("bad range in union case label (old archive format)"); 1718 return ""; 1719 } 1720 int caselimit = caseval; 1721 lp++; 1722 lp = parseNumeral(lp, caselimit); 1723 if (caseval >= caselimit 1724 || (uint)(caselimit - caseval) > 0x10000) { 1725 // Note: 0x10000 is arbitrary implementation restriction. 1726 // We can remove it later if it's important to. 1727 abort("bad range in union case label"); 1728 return ""; 1729 } 1730 for (;;) { 1731 ++caseval; 1732 band_stack.add((void*)(size_t)caseval); 1733 if (caseval == caselimit) break; 1734 } 1735 } 1736 if (*lp != ',') break; 1737 lp++; 1738 } 1739 if (*lp++ != ')') { 1740 abort("bad case label"); 1741 return ""; 1742 } 1743 // save away the case labels 1744 int ntags = band_stack.length() - case_base; 1745 int* tags = U_NEW(int, add_size(ntags, 1)); 1746 CHECK_(lp); 1747 k_case.le_casetags = tags; 1748 *tags++ = ntags; 1749 for (int i = 0; i < ntags; i++) { 1750 *tags++ = ptrlowbits(band_stack.get(case_base+i)); 1751 } 1752 band_stack.popTo(case_base); 1753 CHECK_(lp); 1754 } 1755 // Got le_casetags. Now grab the body. 1756 assert(*lp == '['); 1757 ++lp; 1758 lp = parseLayout(lp, k_case.le_body, curCble); 1759 CHECK_(lp); 1760 if (k_case.le_casetags == null) break; // done 1761 } 1762 b->le_body = popBody(union_base); 1763 } 1764 break; 1765 case '(': // call: '(' -?NN* ')' 1766 { 1767 band& call = *U_NEW(band, 1); 1768 CHECK_(lp); 1769 band_stack.add(&call); 1770 call.le_kind = EK_CALL; 1771 call.bn = bands_made++; 1772 call.le_body = U_NEW(band*, 2); // fill in later 1773 int call_num = 0; 1774 lp = parseNumeral(lp, call_num); 1775 call.le_back = (call_num <= 0); 1776 call_num += curCble; // numeral is self-relative offset 1777 call.le_len = call_num; //use le_len as scratch 1778 calls_to_link.add(&call); 1779 CHECK_(lp); 1780 if (*lp++ != ')') { 1781 abort("bad call label"); 1782 return ""; 1783 } 1784 } 1785 break; 1786 case 'K': // reference_type: constant_ref 1787 case 'R': // reference_type: schema_ref 1788 { 1789 int ixTag = CONSTANT_None; 1790 if (lp[-1] == 'K') { 1791 switch (*lp++) { 1792 case 'I': ixTag = CONSTANT_Integer; break; 1793 case 'J': ixTag = CONSTANT_Long; break; 1794 case 'F': ixTag = CONSTANT_Float; break; 1795 case 'D': ixTag = CONSTANT_Double; break; 1796 case 'S': ixTag = CONSTANT_String; break; 1797 case 'Q': ixTag = CONSTANT_Literal; break; 1798 } 1799 } else { 1800 switch (*lp++) { 1801 case 'C': ixTag = CONSTANT_Class; break; 1802 case 'S': ixTag = CONSTANT_Signature; break; 1803 case 'D': ixTag = CONSTANT_NameandType; break; 1804 case 'F': ixTag = CONSTANT_Fieldref; break; 1805 case 'M': ixTag = CONSTANT_Methodref; break; 1806 case 'I': ixTag = CONSTANT_InterfaceMethodref; break; 1807 case 'U': ixTag = CONSTANT_Utf8; break; //utf8_ref 1808 case 'Q': ixTag = CONSTANT_All; break; //untyped_ref 1809 } 1810 } 1811 if (ixTag == CONSTANT_None) { 1812 abort("bad reference layout"); 1813 break; 1814 } 1815 bool nullOK = false; 1816 if (*lp == 'N') { 1817 nullOK = true; 1818 lp++; 1819 } 1820 lp = parseIntLayout(lp, b, EK_REF); 1821 b->defc = coding::findBySpec(UNSIGNED5_spec); 1822 b->initRef(ixTag, nullOK); 1823 } 1824 break; 1825 case '[': 1826 { 1827 // [callable1][callable2]... 1828 if (!top_level) { 1829 abort("bad nested callable"); 1830 break; 1831 } 1832 curCble += 1; 1833 NOT_PRODUCT(int call_num = band_stack.length() - bs_base); 1834 band& cble = *U_NEW(band, 1); 1835 CHECK_(lp); 1836 band_stack.add(&cble); 1837 cble.le_kind = EK_CBLE; 1838 NOT_PRODUCT(cble.le_len = call_num); 1839 cble.bn = bands_made++; 1840 lp = parseLayout(lp, cble.le_body, curCble); 1841 } 1842 break; 1843 case ']': 1844 // Hit a closing brace. This ends whatever body we were in. 1845 done = true; 1846 break; 1847 case '\0': 1848 // Hit a null. Also ends the (top-level) body. 1849 --lp; // back up, so caller can see the null also 1850 done = true; 1851 break; 1852 default: 1853 abort("bad layout"); 1854 break; 1855 } 1856 CHECK_(lp); 1857 } 1858 1859 // Return the accumulated bands: 1860 res = popBody(bs_base); 1861 return lp; 1862 } 1863 1864 void unpacker::read_attr_defs() { 1865 int i; 1866 1867 // Tell each AD which attrc it is and where its fixed flags are: 1868 attr_defs[ATTR_CONTEXT_CLASS].attrc = ATTR_CONTEXT_CLASS; 1869 attr_defs[ATTR_CONTEXT_CLASS].xxx_flags_hi_bn = e_class_flags_hi; 1870 attr_defs[ATTR_CONTEXT_FIELD].attrc = ATTR_CONTEXT_FIELD; 1871 attr_defs[ATTR_CONTEXT_FIELD].xxx_flags_hi_bn = e_field_flags_hi; 1872 attr_defs[ATTR_CONTEXT_METHOD].attrc = ATTR_CONTEXT_METHOD; 1873 attr_defs[ATTR_CONTEXT_METHOD].xxx_flags_hi_bn = e_method_flags_hi; 1874 attr_defs[ATTR_CONTEXT_CODE].attrc = ATTR_CONTEXT_CODE; 1875 attr_defs[ATTR_CONTEXT_CODE].xxx_flags_hi_bn = e_code_flags_hi; 1876 1877 // Decide whether bands for the optional high flag words are present. 1878 attr_defs[ATTR_CONTEXT_CLASS] 1879 .setHaveLongFlags((archive_options & AO_HAVE_CLASS_FLAGS_HI) != 0); 1880 attr_defs[ATTR_CONTEXT_FIELD] 1881 .setHaveLongFlags((archive_options & AO_HAVE_FIELD_FLAGS_HI) != 0); 1882 attr_defs[ATTR_CONTEXT_METHOD] 1883 .setHaveLongFlags((archive_options & AO_HAVE_METHOD_FLAGS_HI) != 0); 1884 attr_defs[ATTR_CONTEXT_CODE] 1885 .setHaveLongFlags((archive_options & AO_HAVE_CODE_FLAGS_HI) != 0); 1886 1887 // Set up built-in attrs. 1888 // (The simple ones are hard-coded. The metadata layouts are not.) 1889 const char* md_layout = ( 1890 // parameter annotations: 1891 #define MDL0 \ 1892 "[NB[(1)]]" 1893 MDL0 1894 // annotations: 1895 #define MDL1 \ 1896 "[NH[(1)]]" \ 1897 "[RSHNH[RUH(1)]]" 1898 MDL1 1899 // member_value: 1900 "[TB" 1901 "(66,67,73,83,90)[KIH]" 1902 "(68)[KDH]" 1903 "(70)[KFH]" 1904 "(74)[KJH]" 1905 "(99)[RSH]" 1906 "(101)[RSHRUH]" 1907 "(115)[RUH]" 1908 "(91)[NH[(0)]]" 1909 "(64)[" 1910 // nested annotation: 1911 "RSH" 1912 "NH[RUH(0)]" 1913 "]" 1914 "()[]" 1915 "]" 1916 ); 1917 1918 const char* md_layout_P = md_layout; 1919 const char* md_layout_A = md_layout+strlen(MDL0); 1920 const char* md_layout_V = md_layout+strlen(MDL0 MDL1); 1921 assert(0 == strncmp(&md_layout_A[-3], ")]][", 4)); 1922 assert(0 == strncmp(&md_layout_V[-3], ")]][", 4)); 1923 1924 for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) { 1925 attr_definitions& ad = attr_defs[i]; 1926 ad.defineLayout(X_ATTR_RuntimeVisibleAnnotations, 1927 "RuntimeVisibleAnnotations", md_layout_A); 1928 ad.defineLayout(X_ATTR_RuntimeInvisibleAnnotations, 1929 "RuntimeInvisibleAnnotations", md_layout_A); 1930 if (i != ATTR_CONTEXT_METHOD) continue; 1931 ad.defineLayout(METHOD_ATTR_RuntimeVisibleParameterAnnotations, 1932 "RuntimeVisibleParameterAnnotations", md_layout_P); 1933 ad.defineLayout(METHOD_ATTR_RuntimeInvisibleParameterAnnotations, 1934 "RuntimeInvisibleParameterAnnotations", md_layout_P); 1935 ad.defineLayout(METHOD_ATTR_AnnotationDefault, 1936 "AnnotationDefault", md_layout_V); 1937 } 1938 1939 attr_definition_headers.readData(attr_definition_count); 1940 attr_definition_name.readData(attr_definition_count); 1941 attr_definition_layout.readData(attr_definition_count); 1942 1943 CHECK; 1944 1945 // Initialize correct predef bits, to distinguish predefs from new defs. 1946 #define ORBIT(n,s) |((julong)1<<n) 1947 attr_defs[ATTR_CONTEXT_CLASS].predef 1948 = (0 X_ATTR_DO(ORBIT) CLASS_ATTR_DO(ORBIT)); 1949 attr_defs[ATTR_CONTEXT_FIELD].predef 1950 = (0 X_ATTR_DO(ORBIT) FIELD_ATTR_DO(ORBIT)); 1951 attr_defs[ATTR_CONTEXT_METHOD].predef 1952 = (0 X_ATTR_DO(ORBIT) METHOD_ATTR_DO(ORBIT)); 1953 attr_defs[ATTR_CONTEXT_CODE].predef 1954 = (0 O_ATTR_DO(ORBIT) CODE_ATTR_DO(ORBIT)); 1955 #undef ORBIT 1956 // Clear out the redef bits, folding them back into predef. 1957 for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) { 1958 attr_defs[i].predef |= attr_defs[i].redef; 1959 attr_defs[i].redef = 0; 1960 } 1961 1962 // Now read the transmitted locally defined attrs. 1963 // This will set redef bits again. 1964 for (i = 0; i < attr_definition_count; i++) { 1965 int header = attr_definition_headers.getByte(); 1966 int attrc = ADH_BYTE_CONTEXT(header); 1967 int idx = ADH_BYTE_INDEX(header); 1968 entry* name = attr_definition_name.getRef(); 1969 CHECK; 1970 entry* layout = attr_definition_layout.getRef(); 1971 CHECK; 1972 attr_defs[attrc].defineLayout(idx, name, layout->value.b.strval()); 1973 } 1974 } 1975 1976 #define NO_ENTRY_YET ((entry*)-1) 1977 1978 static bool isDigitString(bytes& x, int beg, int end) { 1979 if (beg == end) return false; // null string 1980 byte* xptr = x.ptr; 1981 for (int i = beg; i < end; i++) { 1982 char ch = xptr[i]; 1983 if (!(ch >= '0' && ch <= '9')) return false; 1984 } 1985 return true; 1986 } 1987 1988 enum { // constants for parsing class names 1989 SLASH_MIN = '.', 1990 SLASH_MAX = '/', 1991 DOLLAR_MIN = 0, 1992 DOLLAR_MAX = '-' 1993 }; 1994 1995 static int lastIndexOf(int chmin, int chmax, bytes& x, int pos) { 1996 byte* ptr = x.ptr; 1997 for (byte* cp = ptr + pos; --cp >= ptr; ) { 1998 assert(x.inBounds(cp)); 1999 if (*cp >= chmin && *cp <= chmax) 2000 return (int)(cp - ptr); 2001 } 2002 return -1; 2003 } 2004 2005 maybe_inline 2006 inner_class* cpool::getIC(entry* inner) { 2007 if (inner == null) return null; 2008 assert(inner->tag == CONSTANT_Class); 2009 if (inner->inord == NO_INORD) return null; 2010 inner_class* ic = ic_index[inner->inord]; 2011 assert(ic == null || ic->inner == inner); 2012 return ic; 2013 } 2014 2015 maybe_inline 2016 inner_class* cpool::getFirstChildIC(entry* outer) { 2017 if (outer == null) return null; 2018 assert(outer->tag == CONSTANT_Class); 2019 if (outer->inord == NO_INORD) return null; 2020 inner_class* ic = ic_child_index[outer->inord]; 2021 assert(ic == null || ic->outer == outer); 2022 return ic; 2023 } 2024 2025 maybe_inline 2026 inner_class* cpool::getNextChildIC(inner_class* child) { 2027 inner_class* ic = child->next_sibling; 2028 assert(ic == null || ic->outer == child->outer); 2029 return ic; 2030 } 2031 2032 void unpacker::read_ics() { 2033 int i; 2034 int index_size = cp.tag_count[CONSTANT_Class]; 2035 inner_class** ic_index = U_NEW(inner_class*, index_size); 2036 inner_class** ic_child_index = U_NEW(inner_class*, index_size); 2037 cp.ic_index = ic_index; 2038 cp.ic_child_index = ic_child_index; 2039 ics = U_NEW(inner_class, ic_count); 2040 ic_this_class.readData(ic_count); 2041 ic_flags.readData(ic_count); 2042 CHECK; 2043 // Scan flags to get count of long-form bands. 2044 int long_forms = 0; 2045 for (i = 0; i < ic_count; i++) { 2046 int flags = ic_flags.getInt(); // may be long form! 2047 if ((flags & ACC_IC_LONG_FORM) != 0) { 2048 long_forms += 1; 2049 ics[i].name = NO_ENTRY_YET; 2050 } 2051 flags &= ~ACC_IC_LONG_FORM; 2052 entry* inner = ic_this_class.getRef(); 2053 CHECK; 2054 uint inord = inner->inord; 2055 assert(inord < (uint)cp.tag_count[CONSTANT_Class]); 2056 if (ic_index[inord] != null) { 2057 abort("identical inner class"); 2058 break; 2059 } 2060 ic_index[inord] = &ics[i]; 2061 ics[i].inner = inner; 2062 ics[i].flags = flags; 2063 assert(cp.getIC(inner) == &ics[i]); 2064 } 2065 CHECK; 2066 //ic_this_class.done(); 2067 //ic_flags.done(); 2068 ic_outer_class.readData(long_forms); 2069 ic_name.readData(long_forms); 2070 for (i = 0; i < ic_count; i++) { 2071 if (ics[i].name == NO_ENTRY_YET) { 2072 // Long form. 2073 ics[i].outer = ic_outer_class.getRefN(); 2074 CHECK; 2075 ics[i].name = ic_name.getRefN(); 2076 CHECK; 2077 } else { 2078 // Fill in outer and name based on inner. 2079 bytes& n = ics[i].inner->value.b; 2080 bytes pkgOuter; 2081 bytes number; 2082 bytes name; 2083 // Parse n into pkgOuter and name (and number). 2084 PRINTCR((5, "parse short IC name %s", n.ptr)); 2085 int dollar1, dollar2; // pointers to $ in the pattern 2086 // parse n = (<pkg>/)*<outer>($<number>)?($<name>)? 2087 int nlen = (int)n.len; 2088 int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, nlen) + 1; 2089 dollar2 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, nlen); 2090 if (dollar2 < 0) { 2091 abort(); 2092 return; 2093 } 2094 assert(dollar2 >= pkglen); 2095 if (isDigitString(n, dollar2+1, nlen)) { 2096 // n = (<pkg>/)*<outer>$<number> 2097 number = n.slice(dollar2+1, nlen); 2098 name.set(null,0); 2099 dollar1 = dollar2; 2100 } else if (pkglen < (dollar1 2101 = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, n, dollar2-1)) 2102 && isDigitString(n, dollar1+1, dollar2)) { 2103 // n = (<pkg>/)*<outer>$<number>$<name> 2104 number = n.slice(dollar1+1, dollar2); 2105 name = n.slice(dollar2+1, nlen); 2106 } else { 2107 // n = (<pkg>/)*<outer>$<name> 2108 dollar1 = dollar2; 2109 number.set(null,0); 2110 name = n.slice(dollar2+1, nlen); 2111 } 2112 if (number.ptr == null) 2113 pkgOuter = n.slice(0, dollar1); 2114 else 2115 pkgOuter.set(null,0); 2116 PRINTCR((5,"=> %s$ 0%s $%s", 2117 pkgOuter.string(), number.string(), name.string())); 2118 2119 if (pkgOuter.ptr != null) 2120 ics[i].outer = cp.ensureClass(pkgOuter); 2121 2122 if (name.ptr != null) 2123 ics[i].name = cp.ensureUtf8(name); 2124 } 2125 2126 // update child/sibling list 2127 if (ics[i].outer != null) { 2128 uint outord = ics[i].outer->inord; 2129 if (outord != NO_INORD) { 2130 assert(outord < (uint)cp.tag_count[CONSTANT_Class]); 2131 ics[i].next_sibling = ic_child_index[outord]; 2132 ic_child_index[outord] = &ics[i]; 2133 } 2134 } 2135 } 2136 //ic_outer_class.done(); 2137 //ic_name.done(); 2138 } 2139 2140 void unpacker::read_classes() { 2141 PRINTCR((1," ...scanning %d classes...", class_count)); 2142 class_this.readData(class_count); 2143 class_super.readData(class_count); 2144 class_interface_count.readData(class_count); 2145 class_interface.readData(class_interface_count.getIntTotal()); 2146 2147 CHECK; 2148 2149 #if 0 2150 int i; 2151 // Make a little mark on super-classes. 2152 for (i = 0; i < class_count; i++) { 2153 entry* e = class_super.getRefN(); 2154 if (e != null) e->bits |= entry::EB_SUPER; 2155 } 2156 class_super.rewind(); 2157 #endif 2158 2159 // Members. 2160 class_field_count.readData(class_count); 2161 class_method_count.readData(class_count); 2162 2163 CHECK; 2164 2165 int field_count = class_field_count.getIntTotal(); 2166 int method_count = class_method_count.getIntTotal(); 2167 2168 field_descr.readData(field_count); 2169 read_attrs(ATTR_CONTEXT_FIELD, field_count); 2170 CHECK; 2171 2172 method_descr.readData(method_count); 2173 read_attrs(ATTR_CONTEXT_METHOD, method_count); 2174 CHECK; 2175 2176 read_attrs(ATTR_CONTEXT_CLASS, class_count); 2177 CHECK; 2178 2179 read_code_headers(); 2180 2181 PRINTCR((1,"scanned %d classes, %d fields, %d methods, %d code headers", 2182 class_count, field_count, method_count, code_count)); 2183 } 2184 2185 maybe_inline 2186 int unpacker::attr_definitions::predefCount(uint idx) { 2187 return isPredefined(idx) ? flag_count[idx] : 0; 2188 } 2189 2190 void unpacker::read_attrs(int attrc, int obj_count) { 2191 attr_definitions& ad = attr_defs[attrc]; 2192 assert(ad.attrc == attrc); 2193 2194 int i, idx, count; 2195 2196 CHECK; 2197 2198 bool haveLongFlags = ad.haveLongFlags(); 2199 2200 band& xxx_flags_hi = ad.xxx_flags_hi(); 2201 assert(endsWith(xxx_flags_hi.name, "_flags_hi")); 2202 if (haveLongFlags) 2203 xxx_flags_hi.readData(obj_count); 2204 CHECK; 2205 2206 band& xxx_flags_lo = ad.xxx_flags_lo(); 2207 assert(endsWith(xxx_flags_lo.name, "_flags_lo")); 2208 xxx_flags_lo.readData(obj_count); 2209 CHECK; 2210 2211 // pre-scan flags, counting occurrences of each index bit 2212 julong indexMask = ad.flagIndexMask(); // which flag bits are index bits? 2213 for (i = 0; i < obj_count; i++) { 2214 julong indexBits = xxx_flags_hi.getLong(xxx_flags_lo, haveLongFlags); 2215 if ((indexBits & ~indexMask) > (ushort)-1) { 2216 abort("undefined attribute flag bit"); 2217 return; 2218 } 2219 indexBits &= indexMask; // ignore classfile flag bits 2220 for (idx = 0; indexBits != 0; idx++, indexBits >>= 1) { 2221 ad.flag_count[idx] += (int)(indexBits & 1); 2222 } 2223 } 2224 // we'll scan these again later for output: 2225 xxx_flags_lo.rewind(); 2226 xxx_flags_hi.rewind(); 2227 2228 band& xxx_attr_count = ad.xxx_attr_count(); 2229 assert(endsWith(xxx_attr_count.name, "_attr_count")); 2230 // There is one count element for each 1<<16 bit set in flags: 2231 xxx_attr_count.readData(ad.predefCount(X_ATTR_OVERFLOW)); 2232 CHECK; 2233 2234 band& xxx_attr_indexes = ad.xxx_attr_indexes(); 2235 assert(endsWith(xxx_attr_indexes.name, "_attr_indexes")); 2236 int overflowIndexCount = xxx_attr_count.getIntTotal(); 2237 xxx_attr_indexes.readData(overflowIndexCount); 2238 CHECK; 2239 // pre-scan attr indexes, counting occurrences of each value 2240 for (i = 0; i < overflowIndexCount; i++) { 2241 idx = xxx_attr_indexes.getInt(); 2242 if (!ad.isIndex(idx)) { 2243 abort("attribute index out of bounds"); 2244 return; 2245 } 2246 ad.getCount(idx) += 1; 2247 } 2248 xxx_attr_indexes.rewind(); // we'll scan it again later for output 2249 2250 // We will need a backward call count for each used backward callable. 2251 int backwardCounts = 0; 2252 for (idx = 0; idx < ad.layouts.length(); idx++) { 2253 layout_definition* lo = ad.getLayout(idx); 2254 if (lo != null && ad.getCount(idx) != 0) { 2255 // Build the bands lazily, only when they are used. 2256 band** bands = ad.buildBands(lo); 2257 CHECK; 2258 if (lo->hasCallables()) { 2259 for (i = 0; bands[i] != null; i++) { 2260 if (bands[i]->le_back) { 2261 assert(bands[i]->le_kind == EK_CBLE); 2262 backwardCounts += 1; 2263 } 2264 } 2265 } 2266 } 2267 } 2268 ad.xxx_attr_calls().readData(backwardCounts); 2269 CHECK; 2270 2271 // Read built-in bands. 2272 // Mostly, these are hand-coded equivalents to readBandData(). 2273 switch (attrc) { 2274 case ATTR_CONTEXT_CLASS: 2275 2276 count = ad.predefCount(CLASS_ATTR_SourceFile); 2277 class_SourceFile_RUN.readData(count); 2278 CHECK; 2279 2280 count = ad.predefCount(CLASS_ATTR_EnclosingMethod); 2281 class_EnclosingMethod_RC.readData(count); 2282 class_EnclosingMethod_RDN.readData(count); 2283 CHECK; 2284 2285 count = ad.predefCount(X_ATTR_Signature); 2286 class_Signature_RS.readData(count); 2287 CHECK; 2288 2289 ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); 2290 ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); 2291 2292 count = ad.predefCount(CLASS_ATTR_InnerClasses); 2293 class_InnerClasses_N.readData(count); 2294 CHECK; 2295 2296 count = class_InnerClasses_N.getIntTotal(); 2297 class_InnerClasses_RC.readData(count); 2298 class_InnerClasses_F.readData(count); 2299 CHECK; 2300 // Drop remaining columns wherever flags are zero: 2301 count -= class_InnerClasses_F.getIntCount(0); 2302 class_InnerClasses_outer_RCN.readData(count); 2303 class_InnerClasses_name_RUN.readData(count); 2304 CHECK; 2305 2306 count = ad.predefCount(CLASS_ATTR_ClassFile_version); 2307 class_ClassFile_version_minor_H.readData(count); 2308 class_ClassFile_version_major_H.readData(count); 2309 CHECK; 2310 break; 2311 2312 case ATTR_CONTEXT_FIELD: 2313 2314 count = ad.predefCount(FIELD_ATTR_ConstantValue); 2315 field_ConstantValue_KQ.readData(count); 2316 CHECK; 2317 2318 count = ad.predefCount(X_ATTR_Signature); 2319 field_Signature_RS.readData(count); 2320 CHECK; 2321 2322 ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); 2323 ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); 2324 CHECK; 2325 break; 2326 2327 case ATTR_CONTEXT_METHOD: 2328 2329 code_count = ad.predefCount(METHOD_ATTR_Code); 2330 // Code attrs are handled very specially below... 2331 2332 count = ad.predefCount(METHOD_ATTR_Exceptions); 2333 method_Exceptions_N.readData(count); 2334 count = method_Exceptions_N.getIntTotal(); 2335 method_Exceptions_RC.readData(count); 2336 CHECK; 2337 2338 count = ad.predefCount(X_ATTR_Signature); 2339 method_Signature_RS.readData(count); 2340 CHECK; 2341 2342 ad.readBandData(X_ATTR_RuntimeVisibleAnnotations); 2343 ad.readBandData(X_ATTR_RuntimeInvisibleAnnotations); 2344 ad.readBandData(METHOD_ATTR_RuntimeVisibleParameterAnnotations); 2345 ad.readBandData(METHOD_ATTR_RuntimeInvisibleParameterAnnotations); 2346 ad.readBandData(METHOD_ATTR_AnnotationDefault); 2347 CHECK; 2348 break; 2349 2350 case ATTR_CONTEXT_CODE: 2351 // (keep this code aligned with its brother in unpacker::write_attrs) 2352 count = ad.predefCount(CODE_ATTR_StackMapTable); 2353 // disable this feature in old archives! 2354 if (count != 0 && majver < JAVA6_PACKAGE_MAJOR_VERSION) { 2355 abort("undefined StackMapTable attribute (old archive format)"); 2356 return; 2357 } 2358 code_StackMapTable_N.readData(count); 2359 CHECK; 2360 count = code_StackMapTable_N.getIntTotal(); 2361 code_StackMapTable_frame_T.readData(count); 2362 CHECK; 2363 // the rest of it depends in a complicated way on frame tags 2364 { 2365 int fat_frame_count = 0; 2366 int offset_count = 0; 2367 int type_count = 0; 2368 for (int k = 0; k < count; k++) { 2369 int tag = code_StackMapTable_frame_T.getByte(); 2370 if (tag <= 127) { 2371 // (64-127) [(2)] 2372 if (tag >= 64) type_count++; 2373 } else if (tag <= 251) { 2374 // (247) [(1)(2)] 2375 // (248-251) [(1)] 2376 if (tag >= 247) offset_count++; 2377 if (tag == 247) type_count++; 2378 } else if (tag <= 254) { 2379 // (252) [(1)(2)] 2380 // (253) [(1)(2)(2)] 2381 // (254) [(1)(2)(2)(2)] 2382 offset_count++; 2383 type_count += (tag - 251); 2384 } else { 2385 // (255) [(1)NH[(2)]NH[(2)]] 2386 fat_frame_count++; 2387 } 2388 } 2389 2390 // done pre-scanning frame tags: 2391 code_StackMapTable_frame_T.rewind(); 2392 2393 // deal completely with fat frames: 2394 offset_count += fat_frame_count; 2395 code_StackMapTable_local_N.readData(fat_frame_count); 2396 CHECK; 2397 type_count += code_StackMapTable_local_N.getIntTotal(); 2398 code_StackMapTable_stack_N.readData(fat_frame_count); 2399 type_count += code_StackMapTable_stack_N.getIntTotal(); 2400 CHECK; 2401 // read the rest: 2402 code_StackMapTable_offset.readData(offset_count); 2403 code_StackMapTable_T.readData(type_count); 2404 CHECK; 2405 // (7) [RCH] 2406 count = code_StackMapTable_T.getIntCount(7); 2407 code_StackMapTable_RC.readData(count); 2408 CHECK; 2409 // (8) [PH] 2410 count = code_StackMapTable_T.getIntCount(8); 2411 code_StackMapTable_P.readData(count); 2412 CHECK; 2413 } 2414 2415 count = ad.predefCount(CODE_ATTR_LineNumberTable); 2416 code_LineNumberTable_N.readData(count); 2417 count = code_LineNumberTable_N.getIntTotal(); 2418 code_LineNumberTable_bci_P.readData(count); 2419 code_LineNumberTable_line.readData(count); 2420 2421 count = ad.predefCount(CODE_ATTR_LocalVariableTable); 2422 code_LocalVariableTable_N.readData(count); 2423 count = code_LocalVariableTable_N.getIntTotal(); 2424 code_LocalVariableTable_bci_P.readData(count); 2425 code_LocalVariableTable_span_O.readData(count); 2426 code_LocalVariableTable_name_RU.readData(count); 2427 code_LocalVariableTable_type_RS.readData(count); 2428 code_LocalVariableTable_slot.readData(count); 2429 2430 count = ad.predefCount(CODE_ATTR_LocalVariableTypeTable); 2431 code_LocalVariableTypeTable_N.readData(count); 2432 count = code_LocalVariableTypeTable_N.getIntTotal(); 2433 code_LocalVariableTypeTable_bci_P.readData(count); 2434 code_LocalVariableTypeTable_span_O.readData(count); 2435 code_LocalVariableTypeTable_name_RU.readData(count); 2436 code_LocalVariableTypeTable_type_RS.readData(count); 2437 code_LocalVariableTypeTable_slot.readData(count); 2438 break; 2439 } 2440 2441 // Read compressor-defined bands. 2442 for (idx = 0; idx < ad.layouts.length(); idx++) { 2443 if (ad.getLayout(idx) == null) 2444 continue; // none at this fixed index <32 2445 if (idx < (int)ad.flag_limit && ad.isPredefined(idx)) 2446 continue; // already handled 2447 if (ad.getCount(idx) == 0) 2448 continue; // no attributes of this type (then why transmit layouts?) 2449 ad.readBandData(idx); 2450 } 2451 } 2452 2453 void unpacker::attr_definitions::readBandData(int idx) { 2454 int j; 2455 uint count = getCount(idx); 2456 if (count == 0) return; 2457 layout_definition* lo = getLayout(idx); 2458 if (lo != null) { 2459 PRINTCR((1, "counted %d [redefined = %d predefined = %d] attributes of type %s.%s", 2460 count, isRedefined(idx), isPredefined(idx), 2461 ATTR_CONTEXT_NAME[attrc], lo->name)); 2462 } 2463 bool hasCallables = lo->hasCallables(); 2464 band** bands = lo->bands(); 2465 if (!hasCallables) { 2466 // Read through the rest of the bands in a regular way. 2467 readBandData(bands, count); 2468 } else { 2469 // Deal with the callables. 2470 // First set up the forward entry count for each callable. 2471 // This is stored on band::length of the callable. 2472 bands[0]->expectMoreLength(count); 2473 for (j = 0; bands[j] != null; j++) { 2474 band& j_cble = *bands[j]; 2475 assert(j_cble.le_kind == EK_CBLE); 2476 if (j_cble.le_back) { 2477 // Add in the predicted effects of backward calls, too. 2478 int back_calls = xxx_attr_calls().getInt(); 2479 j_cble.expectMoreLength(back_calls); 2480 // In a moment, more forward calls may increment j_cble.length. 2481 } 2482 } 2483 // Now consult whichever callables have non-zero entry counts. 2484 readBandData(bands, (uint)-1); 2485 } 2486 } 2487 2488 // Recursive helper to the previous function: 2489 void unpacker::attr_definitions::readBandData(band** body, uint count) { 2490 int j, k; 2491 for (j = 0; body[j] != null; j++) { 2492 band& b = *body[j]; 2493 if (b.defc != null) { 2494 // It has data, so read it. 2495 b.readData(count); 2496 } 2497 switch (b.le_kind) { 2498 case EK_REPL: 2499 { 2500 int reps = b.getIntTotal(); 2501 readBandData(b.le_body, reps); 2502 } 2503 break; 2504 case EK_UN: 2505 { 2506 int remaining = count; 2507 for (k = 0; b.le_body[k] != null; k++) { 2508 band& k_case = *b.le_body[k]; 2509 int k_count = 0; 2510 if (k_case.le_casetags == null) { 2511 k_count = remaining; // last (empty) case 2512 } else { 2513 int* tags = k_case.le_casetags; 2514 int ntags = *tags++; // 1st element is length (why not?) 2515 while (ntags-- > 0) { 2516 int tag = *tags++; 2517 k_count += b.getIntCount(tag); 2518 } 2519 } 2520 readBandData(k_case.le_body, k_count); 2521 remaining -= k_count; 2522 } 2523 assert(remaining == 0); 2524 } 2525 break; 2526 case EK_CALL: 2527 // Push the count forward, if it is not a backward call. 2528 if (!b.le_back) { 2529 band& cble = *b.le_body[0]; 2530 assert(cble.le_kind == EK_CBLE); 2531 cble.expectMoreLength(count); 2532 } 2533 break; 2534 case EK_CBLE: 2535 assert((int)count == -1); // incoming count is meaningless 2536 k = b.length; 2537 assert(k >= 0); 2538 // This is intended and required for non production mode. 2539 assert((b.length = -1)); // make it unable to accept more calls now. 2540 readBandData(b.le_body, k); 2541 break; 2542 } 2543 } 2544 } 2545 2546 static inline 2547 band** findMatchingCase(int matchTag, band** cases) { 2548 for (int k = 0; cases[k] != null; k++) { 2549 band& k_case = *cases[k]; 2550 if (k_case.le_casetags != null) { 2551 // If it has tags, it must match a tag. 2552 int* tags = k_case.le_casetags; 2553 int ntags = *tags++; // 1st element is length 2554 for (; ntags > 0; ntags--) { 2555 int tag = *tags++; 2556 if (tag == matchTag) 2557 break; 2558 } 2559 if (ntags == 0) 2560 continue; // does not match 2561 } 2562 return k_case.le_body; 2563 } 2564 return null; 2565 } 2566 2567 // write attribute band data: 2568 void unpacker::putlayout(band** body) { 2569 int i; 2570 int prevBII = -1; 2571 int prevBCI = -1; 2572 if (body == NULL) { 2573 abort("putlayout: unexpected NULL for body"); 2574 return; 2575 } 2576 for (i = 0; body[i] != null; i++) { 2577 band& b = *body[i]; 2578 byte le_kind = b.le_kind; 2579 2580 // Handle scalar part, if any. 2581 int x = 0; 2582 entry* e = null; 2583 if (b.defc != null) { 2584 // It has data, so unparse an element. 2585 if (b.ixTag != CONSTANT_None) { 2586 assert(le_kind == EK_REF); 2587 if (b.ixTag == CONSTANT_Literal) 2588 e = b.getRefUsing(cp.getKQIndex()); 2589 else 2590 e = b.getRefN(); 2591 CHECK; 2592 switch (b.le_len) { 2593 case 0: break; 2594 case 1: putu1ref(e); break; 2595 case 2: putref(e); break; 2596 case 4: putu2(0); putref(e); break; 2597 default: assert(false); 2598 } 2599 } else { 2600 assert(le_kind == EK_INT || le_kind == EK_REPL || le_kind == EK_UN); 2601 x = b.getInt(); 2602 2603 assert(!b.le_bci || prevBCI == (int)to_bci(prevBII)); 2604 switch (b.le_bci) { 2605 case EK_BCI: // PH: transmit R(bci), store bci 2606 x = to_bci(prevBII = x); 2607 prevBCI = x; 2608 break; 2609 case EK_BCID: // POH: transmit D(R(bci)), store bci 2610 x = to_bci(prevBII += x); 2611 prevBCI = x; 2612 break; 2613 case EK_BCO: // OH: transmit D(R(bci)), store D(bci) 2614 x = to_bci(prevBII += x) - prevBCI; 2615 prevBCI += x; 2616 break; 2617 } 2618 assert(!b.le_bci || prevBCI == (int)to_bci(prevBII)); 2619 2620 switch (b.le_len) { 2621 case 0: break; 2622 case 1: putu1(x); break; 2623 case 2: putu2(x); break; 2624 case 4: putu4(x); break; 2625 default: assert(false); 2626 } 2627 } 2628 } 2629 2630 // Handle subparts, if any. 2631 switch (le_kind) { 2632 case EK_REPL: 2633 // x is the repeat count 2634 while (x-- > 0) { 2635 putlayout(b.le_body); 2636 } 2637 break; 2638 case EK_UN: 2639 // x is the tag 2640 putlayout(findMatchingCase(x, b.le_body)); 2641 break; 2642 case EK_CALL: 2643 { 2644 band& cble = *b.le_body[0]; 2645 assert(cble.le_kind == EK_CBLE); 2646 assert(cble.le_len == b.le_len); 2647 putlayout(cble.le_body); 2648 } 2649 break; 2650 2651 #ifndef PRODUCT 2652 case EK_CBLE: 2653 case EK_CASE: 2654 assert(false); // should not reach here 2655 #endif 2656 } 2657 } 2658 } 2659 2660 void unpacker::read_files() { 2661 file_name.readData(file_count); 2662 if ((archive_options & AO_HAVE_FILE_SIZE_HI) != 0) 2663 file_size_hi.readData(file_count); 2664 file_size_lo.readData(file_count); 2665 if ((archive_options & AO_HAVE_FILE_MODTIME) != 0) 2666 file_modtime.readData(file_count); 2667 int allFiles = file_count + class_count; 2668 if ((archive_options & AO_HAVE_FILE_OPTIONS) != 0) { 2669 file_options.readData(file_count); 2670 // FO_IS_CLASS_STUB might be set, causing overlap between classes and files 2671 for (int i = 0; i < file_count; i++) { 2672 if ((file_options.getInt() & FO_IS_CLASS_STUB) != 0) { 2673 allFiles -= 1; // this one counts as both class and file 2674 } 2675 } 2676 file_options.rewind(); 2677 } 2678 assert((default_file_options & FO_IS_CLASS_STUB) == 0); 2679 files_remaining = allFiles; 2680 } 2681 2682 maybe_inline 2683 void unpacker::get_code_header(int& max_stack, 2684 int& max_na_locals, 2685 int& handler_count, 2686 int& cflags) { 2687 int sc = code_headers.getByte(); 2688 if (sc == 0) { 2689 max_stack = max_na_locals = handler_count = cflags = -1; 2690 return; 2691 } 2692 // Short code header is the usual case: 2693 int nh; 2694 int mod; 2695 if (sc < 1 + 12*12) { 2696 sc -= 1; 2697 nh = 0; 2698 mod = 12; 2699 } else if (sc < 1 + 12*12 + 8*8) { 2700 sc -= 1 + 12*12; 2701 nh = 1; 2702 mod = 8; 2703 } else { 2704 assert(sc < 1 + 12*12 + 8*8 + 7*7); 2705 sc -= 1 + 12*12 + 8*8; 2706 nh = 2; 2707 mod = 7; 2708 } 2709 max_stack = sc % mod; 2710 max_na_locals = sc / mod; // caller must add static, siglen 2711 handler_count = nh; 2712 if ((archive_options & AO_HAVE_ALL_CODE_FLAGS) != 0) 2713 cflags = -1; 2714 else 2715 cflags = 0; // this one has no attributes 2716 } 2717 2718 // Cf. PackageReader.readCodeHeaders 2719 void unpacker::read_code_headers() { 2720 code_headers.readData(code_count); 2721 CHECK; 2722 int totalHandlerCount = 0; 2723 int totalFlagsCount = 0; 2724 for (int i = 0; i < code_count; i++) { 2725 int max_stack, max_locals, handler_count, cflags; 2726 get_code_header(max_stack, max_locals, handler_count, cflags); 2727 if (max_stack < 0) code_max_stack.expectMoreLength(1); 2728 if (max_locals < 0) code_max_na_locals.expectMoreLength(1); 2729 if (handler_count < 0) code_handler_count.expectMoreLength(1); 2730 else totalHandlerCount += handler_count; 2731 if (cflags < 0) totalFlagsCount += 1; 2732 } 2733 code_headers.rewind(); // replay later during writing 2734 2735 code_max_stack.readData(); 2736 code_max_na_locals.readData(); 2737 code_handler_count.readData(); 2738 totalHandlerCount += code_handler_count.getIntTotal(); 2739 CHECK; 2740 2741 2742 // Read handler specifications. 2743 // Cf. PackageReader.readCodeHandlers. 2744 code_handler_start_P.readData(totalHandlerCount); 2745 code_handler_end_PO.readData(totalHandlerCount); 2746 code_handler_catch_PO.readData(totalHandlerCount); 2747 code_handler_class_RCN.readData(totalHandlerCount); 2748 CHECK; 2749 2750 read_attrs(ATTR_CONTEXT_CODE, totalFlagsCount); 2751 CHECK; 2752 } 2753 2754 static inline bool is_in_range(uint n, uint min, uint max) { 2755 return n - min <= max - min; // unsigned arithmetic! 2756 } 2757 static inline bool is_field_op(int bc) { 2758 return is_in_range(bc, bc_getstatic, bc_putfield); 2759 } 2760 static inline bool is_invoke_init_op(int bc) { 2761 return is_in_range(bc, _invokeinit_op, _invokeinit_limit-1); 2762 } 2763 static inline bool is_self_linker_op(int bc) { 2764 return is_in_range(bc, _self_linker_op, _self_linker_limit-1); 2765 } 2766 static bool is_branch_op(int bc) { 2767 return is_in_range(bc, bc_ifeq, bc_jsr) 2768 || is_in_range(bc, bc_ifnull, bc_jsr_w); 2769 } 2770 static bool is_local_slot_op(int bc) { 2771 return is_in_range(bc, bc_iload, bc_aload) 2772 || is_in_range(bc, bc_istore, bc_astore) 2773 || bc == bc_iinc || bc == bc_ret; 2774 } 2775 band* unpacker::ref_band_for_op(int bc) { 2776 switch (bc) { 2777 case bc_ildc: 2778 case bc_ildc_w: 2779 return &bc_intref; 2780 case bc_fldc: 2781 case bc_fldc_w: 2782 return &bc_floatref; 2783 case bc_lldc2_w: 2784 return &bc_longref; 2785 case bc_dldc2_w: 2786 return &bc_doubleref; 2787 case bc_aldc: 2788 case bc_aldc_w: 2789 return &bc_stringref; 2790 case bc_cldc: 2791 case bc_cldc_w: 2792 return &bc_classref; 2793 2794 case bc_getstatic: 2795 case bc_putstatic: 2796 case bc_getfield: 2797 case bc_putfield: 2798 return &bc_fieldref; 2799 2800 case bc_invokevirtual: 2801 case bc_invokespecial: 2802 case bc_invokestatic: 2803 return &bc_methodref; 2804 case bc_invokeinterface: 2805 return &bc_imethodref; 2806 2807 case bc_new: 2808 case bc_anewarray: 2809 case bc_checkcast: 2810 case bc_instanceof: 2811 case bc_multianewarray: 2812 return &bc_classref; 2813 } 2814 return null; 2815 } 2816 2817 maybe_inline 2818 band* unpacker::ref_band_for_self_op(int bc, bool& isAloadVar, int& origBCVar) { 2819 if (!is_self_linker_op(bc)) return null; 2820 int idx = (bc - _self_linker_op); 2821 bool isSuper = (idx >= _self_linker_super_flag); 2822 if (isSuper) idx -= _self_linker_super_flag; 2823 bool isAload = (idx >= _self_linker_aload_flag); 2824 if (isAload) idx -= _self_linker_aload_flag; 2825 int origBC = _first_linker_op + idx; 2826 bool isField = is_field_op(origBC); 2827 isAloadVar = isAload; 2828 origBCVar = _first_linker_op + idx; 2829 if (!isSuper) 2830 return isField? &bc_thisfield: &bc_thismethod; 2831 else 2832 return isField? &bc_superfield: &bc_supermethod; 2833 } 2834 2835 // Cf. PackageReader.readByteCodes 2836 inline // called exactly once => inline 2837 void unpacker::read_bcs() { 2838 PRINTCR((3, "reading compressed bytecodes and operands for %d codes...", 2839 code_count)); 2840 2841 // read from bc_codes and bc_case_count 2842 fillbytes all_switch_ops; 2843 all_switch_ops.init(); 2844 CHECK; 2845 2846 // Read directly from rp/rplimit. 2847 //Do this later: bc_codes.readData(...) 2848 byte* rp0 = rp; 2849 2850 band* bc_which; 2851 byte* opptr = rp; 2852 byte* oplimit = rplimit; 2853 2854 bool isAload; // passed by ref and then ignored 2855 int junkBC; // passed by ref and then ignored 2856 for (int k = 0; k < code_count; k++) { 2857 // Scan one method: 2858 for (;;) { 2859 if (opptr+2 > oplimit) { 2860 rp = opptr; 2861 ensure_input(2); 2862 oplimit = rplimit; 2863 rp = rp0; // back up 2864 } 2865 if (opptr == oplimit) { abort(); break; } 2866 int bc = *opptr++ & 0xFF; 2867 bool isWide = false; 2868 if (bc == bc_wide) { 2869 if (opptr == oplimit) { abort(); break; } 2870 bc = *opptr++ & 0xFF; 2871 isWide = true; 2872 } 2873 // Adjust expectations of various band sizes. 2874 switch (bc) { 2875 case bc_tableswitch: 2876 case bc_lookupswitch: 2877 all_switch_ops.addByte(bc); 2878 break; 2879 case bc_iinc: 2880 bc_local.expectMoreLength(1); 2881 bc_which = isWide ? &bc_short : &bc_byte; 2882 bc_which->expectMoreLength(1); 2883 break; 2884 case bc_sipush: 2885 bc_short.expectMoreLength(1); 2886 break; 2887 case bc_bipush: 2888 bc_byte.expectMoreLength(1); 2889 break; 2890 case bc_newarray: 2891 bc_byte.expectMoreLength(1); 2892 break; 2893 case bc_multianewarray: 2894 assert(ref_band_for_op(bc) == &bc_classref); 2895 bc_classref.expectMoreLength(1); 2896 bc_byte.expectMoreLength(1); 2897 break; 2898 case bc_ref_escape: 2899 bc_escrefsize.expectMoreLength(1); 2900 bc_escref.expectMoreLength(1); 2901 break; 2902 case bc_byte_escape: 2903 bc_escsize.expectMoreLength(1); 2904 // bc_escbyte will have to be counted too 2905 break; 2906 default: 2907 if (is_invoke_init_op(bc)) { 2908 bc_initref.expectMoreLength(1); 2909 break; 2910 } 2911 bc_which = ref_band_for_self_op(bc, isAload, junkBC); 2912 if (bc_which != null) { 2913 bc_which->expectMoreLength(1); 2914 break; 2915 } 2916 if (is_branch_op(bc)) { 2917 bc_label.expectMoreLength(1); 2918 break; 2919 } 2920 bc_which = ref_band_for_op(bc); 2921 if (bc_which != null) { 2922 bc_which->expectMoreLength(1); 2923 assert(bc != bc_multianewarray); // handled elsewhere 2924 break; 2925 } 2926 if (is_local_slot_op(bc)) { 2927 bc_local.expectMoreLength(1); 2928 break; 2929 } 2930 break; 2931 case bc_end_marker: 2932 // Increment k and test against code_count. 2933 goto doneScanningMethod; 2934 } 2935 } 2936 doneScanningMethod:{} 2937 if (aborting()) break; 2938 } 2939 2940 // Go through the formality, so we can use it in a regular fashion later: 2941 assert(rp == rp0); 2942 bc_codes.readData((int)(opptr - rp)); 2943 2944 int i = 0; 2945 2946 // To size instruction bands correctly, we need info on switches: 2947 bc_case_count.readData((int)all_switch_ops.size()); 2948 for (i = 0; i < (int)all_switch_ops.size(); i++) { 2949 int caseCount = bc_case_count.getInt(); 2950 int bc = all_switch_ops.getByte(i); 2951 bc_label.expectMoreLength(1+caseCount); // default label + cases 2952 bc_case_value.expectMoreLength(bc == bc_tableswitch ? 1 : caseCount); 2953 PRINTCR((2, "switch bc=%d caseCount=%d", bc, caseCount)); 2954 } 2955 bc_case_count.rewind(); // uses again for output 2956 2957 all_switch_ops.free(); 2958 2959 for (i = e_bc_case_value; i <= e_bc_escsize; i++) { 2960 all_bands[i].readData(); 2961 } 2962 2963 // The bc_escbyte band is counted by the immediately previous band. 2964 bc_escbyte.readData(bc_escsize.getIntTotal()); 2965 2966 PRINTCR((3, "scanned %d opcode and %d operand bytes for %d codes...", 2967 (int)(bc_codes.size()), 2968 (int)(bc_escsize.maxRP() - bc_case_value.minRP()), 2969 code_count)); 2970 } 2971 2972 void unpacker::read_bands() { 2973 byte* rp0 = rp; 2974 CHECK; 2975 read_file_header(); 2976 CHECK; 2977 2978 if (cp.nentries == 0) { 2979 // read_file_header failed to read a CP, because it copied a JAR. 2980 return; 2981 } 2982 2983 // Do this after the file header has been read: 2984 check_options(); 2985 2986 read_cp(); 2987 CHECK; 2988 read_attr_defs(); 2989 CHECK; 2990 read_ics(); 2991 CHECK; 2992 read_classes(); 2993 CHECK; 2994 read_bcs(); 2995 CHECK; 2996 read_files(); 2997 } 2998 2999 /// CP routines 3000 3001 entry*& cpool::hashTabRef(byte tag, bytes& b) { 3002 PRINTCR((5, "hashTabRef tag=%d %s[%d]", tag, b.string(), b.len)); 3003 uint hash = tag + (int)b.len; 3004 for (int i = 0; i < (int)b.len; i++) { 3005 hash = hash * 31 + (0xFF & b.ptr[i]); 3006 } 3007 entry** ht = hashTab; 3008 int hlen = hashTabLength; 3009 assert((hlen & (hlen-1)) == 0); // must be power of 2 3010 uint hash1 = hash & (hlen-1); // == hash % hlen 3011 uint hash2 = 0; // lazily computed (requires mod op.) 3012 int probes = 0; 3013 while (ht[hash1] != null) { 3014 entry& e = *ht[hash1]; 3015 if (e.value.b.equals(b) && e.tag == tag) 3016 break; 3017 if (hash2 == 0) 3018 // Note: hash2 must be relatively prime to hlen, hence the "|1". 3019 hash2 = (((hash % 499) & (hlen-1)) | 1); 3020 hash1 += hash2; 3021 if (hash1 >= (uint)hlen) hash1 -= hlen; 3022 assert(hash1 < (uint)hlen); 3023 assert(++probes < hlen); 3024 } 3025 #ifndef PRODUCT 3026 hash_probes[0] += 1; 3027 hash_probes[1] += probes; 3028 #endif 3029 PRINTCR((5, " => @%d %p", hash1, ht[hash1])); 3030 return ht[hash1]; 3031 } 3032 3033 maybe_inline 3034 static void insert_extra(entry* e, ptrlist& extras) { 3035 // This ordering helps implement the Pack200 requirement 3036 // of a predictable CP order in the class files produced. 3037 e->inord = NO_INORD; // mark as an "extra" 3038 extras.add(e); 3039 // Note: We will sort the list (by string-name) later. 3040 } 3041 3042 entry* cpool::ensureUtf8(bytes& b) { 3043 entry*& ix = hashTabRef(CONSTANT_Utf8, b); 3044 if (ix != null) return ix; 3045 // Make one. 3046 if (nentries == maxentries) { 3047 abort("cp utf8 overflow"); 3048 return &entries[tag_base[CONSTANT_Utf8]]; // return something 3049 } 3050 entry& e = entries[nentries++]; 3051 e.tag = CONSTANT_Utf8; 3052 u->saveTo(e.value.b, b); 3053 assert(&e >= first_extra_entry); 3054 insert_extra(&e, tag_extras[CONSTANT_Utf8]); 3055 PRINTCR((4,"ensureUtf8 miss %s", e.string())); 3056 return ix = &e; 3057 } 3058 3059 entry* cpool::ensureClass(bytes& b) { 3060 entry*& ix = hashTabRef(CONSTANT_Class, b); 3061 if (ix != null) return ix; 3062 // Make one. 3063 if (nentries == maxentries) { 3064 abort("cp class overflow"); 3065 return &entries[tag_base[CONSTANT_Class]]; // return something 3066 } 3067 entry& e = entries[nentries++]; 3068 e.tag = CONSTANT_Class; 3069 e.nrefs = 1; 3070 e.refs = U_NEW(entry*, 1); 3071 ix = &e; // hold my spot in the index 3072 entry* utf = ensureUtf8(b); 3073 e.refs[0] = utf; 3074 e.value.b = utf->value.b; 3075 assert(&e >= first_extra_entry); 3076 insert_extra(&e, tag_extras[CONSTANT_Class]); 3077 PRINTCR((4,"ensureClass miss %s", e.string())); 3078 return &e; 3079 } 3080 3081 void cpool::expandSignatures() { 3082 int i; 3083 int nsigs = 0; 3084 int nreused = 0; 3085 int first_sig = tag_base[CONSTANT_Signature]; 3086 int sig_limit = tag_count[CONSTANT_Signature] + first_sig; 3087 fillbytes buf; 3088 buf.init(1<<10); 3089 CHECK; 3090 for (i = first_sig; i < sig_limit; i++) { 3091 entry& e = entries[i]; 3092 assert(e.tag == CONSTANT_Signature); 3093 int refnum = 0; 3094 bytes form = e.refs[refnum++]->asUtf8(); 3095 buf.empty(); 3096 for (int j = 0; j < (int)form.len; j++) { 3097 int c = form.ptr[j]; 3098 buf.addByte(c); 3099 if (c == 'L') { 3100 entry* cls = e.refs[refnum++]; 3101 buf.append(cls->className()->asUtf8()); 3102 } 3103 } 3104 assert(refnum == e.nrefs); 3105 bytes& sig = buf.b; 3106 PRINTCR((5,"signature %d %s -> %s", i, form.ptr, sig.ptr)); 3107 3108 // try to find a pre-existing Utf8: 3109 entry* &e2 = hashTabRef(CONSTANT_Utf8, sig); 3110 if (e2 != null) { 3111 assert(e2->isUtf8(sig)); 3112 e.value.b = e2->value.b; 3113 e.refs[0] = e2; 3114 e.nrefs = 1; 3115 PRINTCR((5,"signature replaced %d => %s", i, e.string())); 3116 nreused++; 3117 } else { 3118 // there is no other replacement; reuse this CP entry as a Utf8 3119 u->saveTo(e.value.b, sig); 3120 e.tag = CONSTANT_Utf8; 3121 e.nrefs = 0; 3122 e2 = &e; 3123 PRINTCR((5,"signature changed %d => %s", e.inord, e.string())); 3124 } 3125 nsigs++; 3126 } 3127 PRINTCR((1,"expanded %d signatures (reused %d utfs)", nsigs, nreused)); 3128 buf.free(); 3129 3130 // go expunge all references to remaining signatures: 3131 for (i = 0; i < (int)nentries; i++) { 3132 entry& e = entries[i]; 3133 for (int j = 0; j < e.nrefs; j++) { 3134 entry*& e2 = e.refs[j]; 3135 if (e2 != null && e2->tag == CONSTANT_Signature) 3136 e2 = e2->refs[0]; 3137 } 3138 } 3139 } 3140 3141 void cpool::initMemberIndexes() { 3142 // This function does NOT refer to any class schema. 3143 // It is totally internal to the cpool. 3144 int i, j; 3145 3146 // Get the pre-existing indexes: 3147 int nclasses = tag_count[CONSTANT_Class]; 3148 entry* classes = tag_base[CONSTANT_Class] + entries; 3149 int nfields = tag_count[CONSTANT_Fieldref]; 3150 entry* fields = tag_base[CONSTANT_Fieldref] + entries; 3151 int nmethods = tag_count[CONSTANT_Methodref]; 3152 entry* methods = tag_base[CONSTANT_Methodref] + entries; 3153 3154 int* field_counts = T_NEW(int, nclasses); 3155 int* method_counts = T_NEW(int, nclasses); 3156 cpindex* all_indexes = U_NEW(cpindex, nclasses*2); 3157 entry** field_ix = U_NEW(entry*, add_size(nfields, nclasses)); 3158 entry** method_ix = U_NEW(entry*, add_size(nmethods, nclasses)); 3159 3160 for (j = 0; j < nfields; j++) { 3161 entry& f = fields[j]; 3162 i = f.memberClass()->inord; 3163 assert(i < nclasses); 3164 field_counts[i]++; 3165 } 3166 for (j = 0; j < nmethods; j++) { 3167 entry& m = methods[j]; 3168 i = m.memberClass()->inord; 3169 assert(i < nclasses); 3170 method_counts[i]++; 3171 } 3172 3173 int fbase = 0, mbase = 0; 3174 for (i = 0; i < nclasses; i++) { 3175 int fc = field_counts[i]; 3176 int mc = method_counts[i]; 3177 all_indexes[i*2+0].init(fc, field_ix+fbase, 3178 CONSTANT_Fieldref + SUBINDEX_BIT); 3179 all_indexes[i*2+1].init(mc, method_ix+mbase, 3180 CONSTANT_Methodref + SUBINDEX_BIT); 3181 // reuse field_counts and member_counts as fill pointers: 3182 field_counts[i] = fbase; 3183 method_counts[i] = mbase; 3184 PRINTCR((3, "class %d fields @%d[%d] methods @%d[%d]", 3185 i, fbase, fc, mbase, mc)); 3186 fbase += fc+1; 3187 mbase += mc+1; 3188 // (the +1 leaves a space between every subarray) 3189 } 3190 assert(fbase == nfields+nclasses); 3191 assert(mbase == nmethods+nclasses); 3192 3193 for (j = 0; j < nfields; j++) { 3194 entry& f = fields[j]; 3195 i = f.memberClass()->inord; 3196 field_ix[field_counts[i]++] = &f; 3197 } 3198 for (j = 0; j < nmethods; j++) { 3199 entry& m = methods[j]; 3200 i = m.memberClass()->inord; 3201 method_ix[method_counts[i]++] = &m; 3202 } 3203 3204 member_indexes = all_indexes; 3205 3206 #ifndef PRODUCT 3207 // Test the result immediately on every class and field. 3208 int fvisited = 0, mvisited = 0; 3209 int prevord, len; 3210 for (i = 0; i < nclasses; i++) { 3211 entry* cls = &classes[i]; 3212 cpindex* fix = getFieldIndex(cls); 3213 cpindex* mix = getMethodIndex(cls); 3214 PRINTCR((2, "field and method index for %s [%d] [%d]", 3215 cls->string(), mix->len, fix->len)); 3216 prevord = -1; 3217 for (j = 0, len = fix->len; j < len; j++) { 3218 entry* f = fix->get(j); 3219 assert(f != null); 3220 PRINTCR((3, "- field %s", f->string())); 3221 assert(f->memberClass() == cls); 3222 assert(prevord < (int)f->inord); 3223 prevord = f->inord; 3224 fvisited++; 3225 } 3226 assert(fix->base2[j] == null); 3227 prevord = -1; 3228 for (j = 0, len = mix->len; j < len; j++) { 3229 entry* m = mix->get(j); 3230 assert(m != null); 3231 PRINTCR((3, "- method %s", m->string())); 3232 assert(m->memberClass() == cls); 3233 assert(prevord < (int)m->inord); 3234 prevord = m->inord; 3235 mvisited++; 3236 } 3237 assert(mix->base2[j] == null); 3238 } 3239 assert(fvisited == nfields); 3240 assert(mvisited == nmethods); 3241 #endif 3242 3243 // Free intermediate buffers. 3244 u->free_temps(); 3245 } 3246 3247 void entry::requestOutputIndex(cpool& cp, int req) { 3248 assert(outputIndex <= NOT_REQUESTED); // must not have assigned indexes yet 3249 if (tag == CONSTANT_Signature) { 3250 ref(0)->requestOutputIndex(cp, req); 3251 return; 3252 } 3253 assert(req == REQUESTED || req == REQUESTED_LDC); 3254 if (outputIndex != NOT_REQUESTED) { 3255 if (req == REQUESTED_LDC) 3256 outputIndex = req; // this kind has precedence 3257 return; 3258 } 3259 outputIndex = req; 3260 //assert(!cp.outputEntries.contains(this)); 3261 assert(tag != CONSTANT_Signature); 3262 cp.outputEntries.add(this); 3263 for (int j = 0; j < nrefs; j++) { 3264 ref(j)->requestOutputIndex(cp); 3265 } 3266 } 3267 3268 void cpool::resetOutputIndexes() { 3269 int i; 3270 int noes = outputEntries.length(); 3271 entry** oes = (entry**) outputEntries.base(); 3272 for (i = 0; i < noes; i++) { 3273 entry& e = *oes[i]; 3274 e.outputIndex = NOT_REQUESTED; 3275 } 3276 outputIndexLimit = 0; 3277 outputEntries.empty(); 3278 #ifndef PRODUCT 3279 // they must all be clear now 3280 for (i = 0; i < (int)nentries; i++) 3281 assert(entries[i].outputIndex == NOT_REQUESTED); 3282 #endif 3283 } 3284 3285 static const byte TAG_ORDER[CONSTANT_Limit] = { 3286 0, 1, 0, 2, 3, 4, 5, 7, 6, 10, 11, 12, 9, 8 3287 }; 3288 3289 extern "C" 3290 int outputEntry_cmp(const void* e1p, const void* e2p) { 3291 // Sort entries according to the Pack200 rules for deterministic 3292 // constant pool ordering. 3293 // 3294 // The four sort keys as follows, in order of decreasing importance: 3295 // 1. ldc first, then non-ldc guys 3296 // 2. normal cp_All entries by input order (i.e., address order) 3297 // 3. after that, extra entries by lexical order (as in tag_extras[*]) 3298 entry& e1 = *(entry*) *(void**) e1p; 3299 entry& e2 = *(entry*) *(void**) e2p; 3300 int oi1 = e1.outputIndex; 3301 int oi2 = e2.outputIndex; 3302 assert(oi1 == REQUESTED || oi1 == REQUESTED_LDC); 3303 assert(oi2 == REQUESTED || oi2 == REQUESTED_LDC); 3304 if (oi1 != oi2) { 3305 if (oi1 == REQUESTED_LDC) return 0-1; 3306 if (oi2 == REQUESTED_LDC) return 1-0; 3307 // Else fall through; neither is an ldc request. 3308 } 3309 if (e1.inord != NO_INORD || e2.inord != NO_INORD) { 3310 // One or both is normal. Use input order. 3311 if (&e1 > &e2) return 1-0; 3312 if (&e1 < &e2) return 0-1; 3313 return 0; // equal pointers 3314 } 3315 // Both are extras. Sort by tag and then by value. 3316 if (e1.tag != e2.tag) { 3317 return TAG_ORDER[e1.tag] - TAG_ORDER[e2.tag]; 3318 } 3319 // If the tags are the same, use string comparison. 3320 return compare_Utf8_chars(e1.value.b, e2.value.b); 3321 } 3322 3323 void cpool::computeOutputIndexes() { 3324 int i; 3325 3326 #ifndef PRODUCT 3327 // outputEntries must be a complete list of those requested: 3328 static uint checkStart = 0; 3329 int checkStep = 1; 3330 if (nentries > 100) checkStep = nentries / 100; 3331 for (i = (int)(checkStart++ % checkStep); i < (int)nentries; i += checkStep) { 3332 entry& e = entries[i]; 3333 if (e.outputIndex != NOT_REQUESTED) { 3334 assert(outputEntries.contains(&e)); 3335 } else { 3336 assert(!outputEntries.contains(&e)); 3337 } 3338 } 3339 3340 // check hand-initialization of TAG_ORDER 3341 for (i = 0; i < (int)N_TAGS_IN_ORDER; i++) { 3342 byte tag = TAGS_IN_ORDER[i]; 3343 assert(TAG_ORDER[tag] == i+1); 3344 } 3345 #endif 3346 3347 int noes = outputEntries.length(); 3348 entry** oes = (entry**) outputEntries.base(); 3349 3350 // Sort the output constant pool into the order required by Pack200. 3351 PTRLIST_QSORT(outputEntries, outputEntry_cmp); 3352 3353 // Allocate a new index for each entry that needs one. 3354 // We do this in two passes, one for LDC entries and one for the rest. 3355 int nextIndex = 1; // always skip index #0 in output cpool 3356 for (i = 0; i < noes; i++) { 3357 entry& e = *oes[i]; 3358 assert(e.outputIndex == REQUESTED || e.outputIndex == REQUESTED_LDC); 3359 e.outputIndex = nextIndex++; 3360 if (e.isDoubleWord()) nextIndex++; // do not use the next index 3361 } 3362 outputIndexLimit = nextIndex; 3363 PRINTCR((3,"renumbering CP to %d entries", outputIndexLimit)); 3364 } 3365 3366 #ifndef PRODUCT 3367 // debugging goo 3368 3369 unpacker* debug_u; 3370 3371 static bytes& getbuf(size_t len) { // for debugging only! 3372 static int bn = 0; 3373 static bytes bufs[8]; 3374 bytes& buf = bufs[bn++ & 7]; 3375 while (buf.len < len + 10) { 3376 buf.realloc(buf.len ? buf.len * 2 : 1000); 3377 } 3378 buf.ptr[0] = 0; // for the sake of strcat 3379 return buf; 3380 } 3381 3382 const char* entry::string() { 3383 bytes buf; 3384 switch (tag) { 3385 case CONSTANT_None: 3386 return "<empty>"; 3387 case CONSTANT_Signature: 3388 if (value.b.ptr == null) 3389 return ref(0)->string(); 3390 // else fall through: 3391 case CONSTANT_Utf8: 3392 buf = value.b; 3393 break; 3394 case CONSTANT_Integer: 3395 case CONSTANT_Float: 3396 buf = getbuf(12); 3397 sprintf((char*)buf.ptr, "0x%08x", value.i); 3398 break; 3399 case CONSTANT_Long: 3400 case CONSTANT_Double: 3401 buf = getbuf(24); 3402 sprintf((char*)buf.ptr, "0x" LONG_LONG_HEX_FORMAT, value.l); 3403 break; 3404 default: 3405 if (nrefs == 0) { 3406 buf = getbuf(20); 3407 sprintf((char*)buf.ptr, "<tag=%d>", tag); 3408 } else if (nrefs == 1) { 3409 return refs[0]->string(); 3410 } else { 3411 const char* s1 = refs[0]->string(); 3412 const char* s2 = refs[1]->string(); 3413 buf = getbuf(strlen(s1) + 1 + strlen(s2) + 4 + 1); 3414 buf.strcat(s1).strcat(" ").strcat(s2); 3415 if (nrefs > 2) buf.strcat(" ..."); 3416 } 3417 } 3418 return (const char*)buf.ptr; 3419 } 3420 3421 void print_cp_entry(int i) { 3422 entry& e = debug_u->cp.entries[i]; 3423 3424 if ((uint)e.tag < CONSTANT_Limit) { 3425 printf(" %d\t%s %s\n", i, TAG_NAME[e.tag], e.string()); 3426 } else { 3427 printf(" %d\t%d %s\n", i, e.tag, e.string()); 3428 } 3429 } 3430 3431 void print_cp_entries(int beg, int end) { 3432 for (int i = beg; i < end; i++) 3433 print_cp_entry(i); 3434 } 3435 3436 void print_cp() { 3437 print_cp_entries(0, debug_u->cp.nentries); 3438 } 3439 3440 #endif 3441 3442 // Unpacker Start 3443 3444 const char str_tf[] = "true\0false"; 3445 #undef STR_TRUE 3446 #undef STR_FALSE 3447 #define STR_TRUE (&str_tf[0]) 3448 #define STR_FALSE (&str_tf[5]) 3449 3450 const char* unpacker::get_option(const char* prop) { 3451 if (prop == null ) return null; 3452 if (strcmp(prop, UNPACK_DEFLATE_HINT) == 0) { 3453 return deflate_hint_or_zero == 0? null : STR_TF(deflate_hint_or_zero > 0); 3454 #ifdef HAVE_STRIP 3455 } else if (strcmp(prop, UNPACK_STRIP_COMPILE) == 0) { 3456 return STR_TF(strip_compile); 3457 } else if (strcmp(prop, UNPACK_STRIP_DEBUG) == 0) { 3458 return STR_TF(strip_debug); 3459 } else if (strcmp(prop, UNPACK_STRIP_JCOV) == 0) { 3460 return STR_TF(strip_jcov); 3461 #endif /*HAVE_STRIP*/ 3462 } else if (strcmp(prop, UNPACK_REMOVE_PACKFILE) == 0) { 3463 return STR_TF(remove_packfile); 3464 } else if (strcmp(prop, DEBUG_VERBOSE) == 0) { 3465 return saveIntStr(verbose); 3466 } else if (strcmp(prop, UNPACK_MODIFICATION_TIME) == 0) { 3467 return (modification_time_or_zero == 0)? null: 3468 saveIntStr(modification_time_or_zero); 3469 } else if (strcmp(prop, UNPACK_LOG_FILE) == 0) { 3470 return log_file; 3471 } else { 3472 return NULL; // unknown option ignore 3473 } 3474 } 3475 3476 bool unpacker::set_option(const char* prop, const char* value) { 3477 if (prop == NULL) return false; 3478 if (strcmp(prop, UNPACK_DEFLATE_HINT) == 0) { 3479 deflate_hint_or_zero = ( (value == null || strcmp(value, "keep") == 0) 3480 ? 0: BOOL_TF(value) ? +1: -1); 3481 #ifdef HAVE_STRIP 3482 } else if (strcmp(prop, UNPACK_STRIP_COMPILE) == 0) { 3483 strip_compile = STR_TF(value); 3484 } else if (strcmp(prop, UNPACK_STRIP_DEBUG) == 0) { 3485 strip_debug = STR_TF(value); 3486 } else if (strcmp(prop, UNPACK_STRIP_JCOV) == 0) { 3487 strip_jcov = STR_TF(value); 3488 #endif /*HAVE_STRIP*/ 3489 } else if (strcmp(prop, UNPACK_REMOVE_PACKFILE) == 0) { 3490 remove_packfile = STR_TF(value); 3491 } else if (strcmp(prop, DEBUG_VERBOSE) == 0) { 3492 verbose = (value == null)? 0: atoi(value); 3493 } else if (strcmp(prop, DEBUG_VERBOSE ".bands") == 0) { 3494 #ifndef PRODUCT 3495 verbose_bands = (value == null)? 0: atoi(value); 3496 #endif 3497 } else if (strcmp(prop, UNPACK_MODIFICATION_TIME) == 0) { 3498 if (value == null || (strcmp(value, "keep") == 0)) { 3499 modification_time_or_zero = 0; 3500 } else if (strcmp(value, "now") == 0) { 3501 time_t now; 3502 time(&now); 3503 modification_time_or_zero = (int) now; 3504 } else { 3505 modification_time_or_zero = atoi(value); 3506 if (modification_time_or_zero == 0) 3507 modification_time_or_zero = 1; // make non-zero 3508 } 3509 } else if (strcmp(prop, UNPACK_LOG_FILE) == 0) { 3510 log_file = (value == null)? value: saveStr(value); 3511 } else { 3512 return false; // unknown option ignore 3513 } 3514 return true; 3515 } 3516 3517 // Deallocate all internal storage and reset to a clean state. 3518 // Do not disturb any input or output connections, including 3519 // infileptr, infileno, inbytes, read_input_fn, jarout, or errstrm. 3520 // Do not reset any unpack options. 3521 void unpacker::reset() { 3522 bytes_read_before_reset += bytes_read; 3523 bytes_written_before_reset += bytes_written; 3524 files_written_before_reset += files_written; 3525 classes_written_before_reset += classes_written; 3526 segments_read_before_reset += 1; 3527 if (verbose >= 2) { 3528 fprintf(errstrm, 3529 "After segment %d, " 3530 LONG_LONG_FORMAT " bytes read and " 3531 LONG_LONG_FORMAT " bytes written.\n", 3532 segments_read_before_reset-1, 3533 bytes_read_before_reset, bytes_written_before_reset); 3534 fprintf(errstrm, 3535 "After segment %d, %d files (of which %d are classes) written to output.\n", 3536 segments_read_before_reset-1, 3537 files_written_before_reset, classes_written_before_reset); 3538 if (archive_next_count != 0) { 3539 fprintf(errstrm, 3540 "After segment %d, %d segment%s remaining (estimated).\n", 3541 segments_read_before_reset-1, 3542 archive_next_count, archive_next_count==1?"":"s"); 3543 } 3544 } 3545 3546 unpacker save_u = (*this); // save bytewise image 3547 infileptr = null; // make asserts happy 3548 jniobj = null; // make asserts happy 3549 jarout = null; // do not close the output jar 3550 gzin = null; // do not close the input gzip stream 3551 bytes esn; 3552 if (errstrm_name != null) { 3553 esn.saveFrom(errstrm_name); 3554 } else { 3555 esn.set(null, 0); 3556 } 3557 this->free(); 3558 mtrace('s', 0, 0); // note the boundary between segments 3559 this->init(read_input_fn); 3560 3561 // restore selected interface state: 3562 #define SAVE(x) this->x = save_u.x 3563 SAVE(jniobj); 3564 SAVE(jnienv); 3565 SAVE(infileptr); // buffered 3566 SAVE(infileno); // unbuffered 3567 SAVE(inbytes); // direct 3568 SAVE(jarout); 3569 SAVE(gzin); 3570 //SAVE(read_input_fn); 3571 SAVE(errstrm); 3572 SAVE(verbose); // verbose level, 0 means no output 3573 SAVE(strip_compile); 3574 SAVE(strip_debug); 3575 SAVE(strip_jcov); 3576 SAVE(remove_packfile); 3577 SAVE(deflate_hint_or_zero); // ==0 means not set, otherwise -1 or 1 3578 SAVE(modification_time_or_zero); 3579 SAVE(bytes_read_before_reset); 3580 SAVE(bytes_written_before_reset); 3581 SAVE(files_written_before_reset); 3582 SAVE(classes_written_before_reset); 3583 SAVE(segments_read_before_reset); 3584 #undef SAVE 3585 if (esn.len > 0) { 3586 errstrm_name = saveStr(esn.strval()); 3587 esn.free(); 3588 } 3589 log_file = errstrm_name; 3590 // Note: If we use strip_names, watch out: They get nuked here. 3591 } 3592 3593 void unpacker::init(read_input_fn_t input_fn) { 3594 int i; 3595 NOT_PRODUCT(debug_u = this); 3596 BYTES_OF(*this).clear(); 3597 #ifndef PRODUCT 3598 free(); // just to make sure freeing is idempotent 3599 #endif 3600 this->u = this; // self-reference for U_NEW macro 3601 errstrm = stdout; // default error-output 3602 log_file = LOGFILE_STDOUT; 3603 read_input_fn = input_fn; 3604 all_bands = band::makeBands(this); 3605 // Make a default jar buffer; caller may safely overwrite it. 3606 jarout = U_NEW(jar, 1); 3607 jarout->init(this); 3608 for (i = 0; i < ATTR_CONTEXT_LIMIT; i++) 3609 attr_defs[i].u = u; // set up outer ptr 3610 } 3611 3612 const char* unpacker::get_abort_message() { 3613 return abort_message; 3614 } 3615 3616 void unpacker::dump_options() { 3617 static const char* opts[] = { 3618 UNPACK_LOG_FILE, 3619 UNPACK_DEFLATE_HINT, 3620 #ifdef HAVE_STRIP 3621 UNPACK_STRIP_COMPILE, 3622 UNPACK_STRIP_DEBUG, 3623 UNPACK_STRIP_JCOV, 3624 #endif /*HAVE_STRIP*/ 3625 UNPACK_REMOVE_PACKFILE, 3626 DEBUG_VERBOSE, 3627 UNPACK_MODIFICATION_TIME, 3628 null 3629 }; 3630 for (int i = 0; opts[i] != null; i++) { 3631 const char* str = get_option(opts[i]); 3632 if (str == null) { 3633 if (verbose == 0) continue; 3634 str = "(not set)"; 3635 } 3636 fprintf(errstrm, "%s=%s\n", opts[i], str); 3637 } 3638 } 3639 3640 3641 // Usage: unpack a byte buffer 3642 // packptr is a reference to byte buffer containing a 3643 // packed file and len is the length of the buffer. 3644 // If null, the callback is used to fill an internal buffer. 3645 void unpacker::start(void* packptr, size_t len) { 3646 CHECK; 3647 NOT_PRODUCT(debug_u = this); 3648 if (packptr != null && len != 0) { 3649 inbytes.set((byte*) packptr, len); 3650 } 3651 CHECK; 3652 read_bands(); 3653 } 3654 3655 void unpacker::check_options() { 3656 const char* strue = "true"; 3657 const char* sfalse = "false"; 3658 if (deflate_hint_or_zero != 0) { 3659 bool force_deflate_hint = (deflate_hint_or_zero > 0); 3660 if (force_deflate_hint) 3661 default_file_options |= FO_DEFLATE_HINT; 3662 else 3663 default_file_options &= ~FO_DEFLATE_HINT; 3664 // Turn off per-file deflate hint by force. 3665 suppress_file_options |= FO_DEFLATE_HINT; 3666 } 3667 if (modification_time_or_zero != 0) { 3668 default_file_modtime = modification_time_or_zero; 3669 // Turn off per-file modtime by force. 3670 archive_options &= ~AO_HAVE_FILE_MODTIME; 3671 } 3672 // %%% strip_compile, etc... 3673 } 3674 3675 // classfile writing 3676 3677 void unpacker::reset_cur_classfile() { 3678 // set defaults 3679 cur_class_minver = default_class_minver; 3680 cur_class_majver = default_class_majver; 3681 3682 // reset constant pool state 3683 cp.resetOutputIndexes(); 3684 3685 // reset fixups 3686 class_fixup_type.empty(); 3687 class_fixup_offset.empty(); 3688 class_fixup_ref.empty(); 3689 requested_ics.empty(); 3690 } 3691 3692 cpindex* cpool::getKQIndex() { 3693 char ch = '?'; 3694 if (u->cur_descr != null) { 3695 entry* type = u->cur_descr->descrType(); 3696 ch = type->value.b.ptr[0]; 3697 } 3698 byte tag = CONSTANT_Integer; 3699 switch (ch) { 3700 case 'L': tag = CONSTANT_String; break; 3701 case 'I': tag = CONSTANT_Integer; break; 3702 case 'J': tag = CONSTANT_Long; break; 3703 case 'F': tag = CONSTANT_Float; break; 3704 case 'D': tag = CONSTANT_Double; break; 3705 case 'B': case 'S': case 'C': 3706 case 'Z': tag = CONSTANT_Integer; break; 3707 default: abort("bad KQ reference"); break; 3708 } 3709 return getIndex(tag); 3710 } 3711 3712 uint unpacker::to_bci(uint bii) { 3713 uint len = bcimap.length(); 3714 uint* map = (uint*) bcimap.base(); 3715 assert(len > 0); // must be initialized before using to_bci 3716 if (bii < len) 3717 return map[bii]; 3718 // Else it's a fractional or out-of-range BCI. 3719 uint key = bii-len; 3720 for (int i = len; ; i--) { 3721 if (map[i-1]-(i-1) <= key) 3722 break; 3723 else 3724 --bii; 3725 } 3726 return bii; 3727 } 3728 3729 void unpacker::put_stackmap_type() { 3730 int tag = code_StackMapTable_T.getByte(); 3731 putu1(tag); 3732 switch (tag) { 3733 case 7: // (7) [RCH] 3734 putref(code_StackMapTable_RC.getRef()); 3735 break; 3736 case 8: // (8) [PH] 3737 putu2(to_bci(code_StackMapTable_P.getInt())); 3738 break; 3739 } 3740 } 3741 3742 // Functions for writing code. 3743 3744 maybe_inline 3745 void unpacker::put_label(int curIP, int size) { 3746 code_fixup_type.addByte(size); 3747 code_fixup_offset.add((int)put_empty(size)); 3748 code_fixup_source.add(curIP); 3749 } 3750 3751 inline // called exactly once => inline 3752 void unpacker::write_bc_ops() { 3753 bcimap.empty(); 3754 code_fixup_type.empty(); 3755 code_fixup_offset.empty(); 3756 code_fixup_source.empty(); 3757 3758 band* bc_which; 3759 3760 byte* opptr = bc_codes.curRP(); 3761 // No need for oplimit, since the codes are pre-counted. 3762 3763 size_t codeBase = wpoffset(); 3764 3765 bool isAload; // copy-out result 3766 int origBC; 3767 3768 entry* thisClass = cur_class; 3769 entry* superClass = cur_super; 3770 entry* newClass = null; // class of last _new opcode 3771 3772 // overwrite any prior index on these bands; it changes w/ current class: 3773 bc_thisfield.setIndex( cp.getFieldIndex( thisClass)); 3774 bc_thismethod.setIndex( cp.getMethodIndex(thisClass)); 3775 if (superClass != null) { 3776 bc_superfield.setIndex( cp.getFieldIndex( superClass)); 3777 bc_supermethod.setIndex(cp.getMethodIndex(superClass)); 3778 } else { 3779 NOT_PRODUCT(bc_superfield.setIndex(null)); 3780 NOT_PRODUCT(bc_supermethod.setIndex(null)); 3781 } 3782 CHECK; 3783 3784 for (int curIP = 0; ; curIP++) { 3785 int curPC = (int)(wpoffset() - codeBase); 3786 bcimap.add(curPC); 3787 ensure_put_space(10); // covers most instrs w/o further bounds check 3788 int bc = *opptr++ & 0xFF; 3789 3790 putu1_fast(bc); 3791 // Note: See '--wp' below for pseudo-bytecodes like bc_end_marker. 3792 3793 bool isWide = false; 3794 if (bc == bc_wide) { 3795 bc = *opptr++ & 0xFF; 3796 putu1_fast(bc); 3797 isWide = true; 3798 } 3799 switch (bc) { 3800 case bc_end_marker: 3801 --wp; // not really part of the code 3802 assert(opptr <= bc_codes.maxRP()); 3803 bc_codes.curRP() = opptr; // advance over this in bc_codes 3804 goto doneScanningMethod; 3805 case bc_tableswitch: // apc: (df, lo, hi, (hi-lo+1)*(label)) 3806 case bc_lookupswitch: // apc: (df, nc, nc*(case, label)) 3807 { 3808 int caseCount = bc_case_count.getInt(); 3809 while (((wpoffset() - codeBase) % 4) != 0) putu1_fast(0); 3810 ensure_put_space(30 + caseCount*8); 3811 put_label(curIP, 4); //int df = bc_label.getInt(); 3812 if (bc == bc_tableswitch) { 3813 int lo = bc_case_value.getInt(); 3814 int hi = lo + caseCount-1; 3815 putu4(lo); 3816 putu4(hi); 3817 for (int j = 0; j < caseCount; j++) { 3818 put_label(curIP, 4); //int lVal = bc_label.getInt(); 3819 //int cVal = lo + j; 3820 } 3821 } else { 3822 putu4(caseCount); 3823 for (int j = 0; j < caseCount; j++) { 3824 int cVal = bc_case_value.getInt(); 3825 putu4(cVal); 3826 put_label(curIP, 4); //int lVal = bc_label.getInt(); 3827 } 3828 } 3829 assert((int)to_bci(curIP) == curPC); 3830 continue; 3831 } 3832 case bc_iinc: 3833 { 3834 int local = bc_local.getInt(); 3835 int delta = (isWide ? bc_short : bc_byte).getInt(); 3836 if (isWide) { 3837 putu2(local); 3838 putu2(delta); 3839 } else { 3840 putu1_fast(local); 3841 putu1_fast(delta); 3842 } 3843 continue; 3844 } 3845 case bc_sipush: 3846 { 3847 int val = bc_short.getInt(); 3848 putu2(val); 3849 continue; 3850 } 3851 case bc_bipush: 3852 case bc_newarray: 3853 { 3854 int val = bc_byte.getByte(); 3855 putu1_fast(val); 3856 continue; 3857 } 3858 case bc_ref_escape: 3859 { 3860 // Note that insnMap has one entry for this. 3861 --wp; // not really part of the code 3862 int size = bc_escrefsize.getInt(); 3863 entry* ref = bc_escref.getRefN(); 3864 CHECK; 3865 switch (size) { 3866 case 1: putu1ref(ref); break; 3867 case 2: putref(ref); break; 3868 default: assert(false); 3869 } 3870 continue; 3871 } 3872 case bc_byte_escape: 3873 { 3874 // Note that insnMap has one entry for all these bytes. 3875 --wp; // not really part of the code 3876 int size = bc_escsize.getInt(); 3877 ensure_put_space(size); 3878 for (int j = 0; j < size; j++) 3879 putu1_fast(bc_escbyte.getByte()); 3880 continue; 3881 } 3882 default: 3883 if (is_invoke_init_op(bc)) { 3884 origBC = bc_invokespecial; 3885 entry* classRef; 3886 switch (bc - _invokeinit_op) { 3887 case _invokeinit_self_option: classRef = thisClass; break; 3888 case _invokeinit_super_option: classRef = superClass; break; 3889 default: assert(bc == _invokeinit_op+_invokeinit_new_option); 3890 case _invokeinit_new_option: classRef = newClass; break; 3891 } 3892 wp[-1] = origBC; // overwrite with origBC 3893 int coding = bc_initref.getInt(); 3894 // Find the nth overloading of <init> in classRef. 3895 entry* ref = null; 3896 cpindex* ix = cp.getMethodIndex(classRef); 3897 CHECK; 3898 for (int j = 0, which_init = 0; ; j++) { 3899 ref = (ix == null)? null: ix->get(j); 3900 if (ref == null) break; // oops, bad input 3901 assert(ref->tag == CONSTANT_Methodref); 3902 if (ref->memberDescr()->descrName() == cp.sym[cpool::s_lt_init_gt]) { 3903 if (which_init++ == coding) break; 3904 } 3905 } 3906 putref(ref); 3907 continue; 3908 } 3909 bc_which = ref_band_for_self_op(bc, isAload, origBC); 3910 if (bc_which != null) { 3911 if (!isAload) { 3912 wp[-1] = origBC; // overwrite with origBC 3913 } else { 3914 wp[-1] = bc_aload_0; // overwrite with _aload_0 3915 // Note: insnMap keeps the _aload_0 separate. 3916 bcimap.add(++curPC); 3917 ++curIP; 3918 putu1_fast(origBC); 3919 } 3920 entry* ref = bc_which->getRef(); 3921 CHECK; 3922 putref(ref); 3923 continue; 3924 } 3925 if (is_branch_op(bc)) { 3926 //int lVal = bc_label.getInt(); 3927 if (bc < bc_goto_w) { 3928 put_label(curIP, 2); //putu2(lVal & 0xFFFF); 3929 } else { 3930 assert(bc <= bc_jsr_w); 3931 put_label(curIP, 4); //putu4(lVal); 3932 } 3933 assert((int)to_bci(curIP) == curPC); 3934 continue; 3935 } 3936 bc_which = ref_band_for_op(bc); 3937 if (bc_which != null) { 3938 entry* ref = bc_which->getRefCommon(bc_which->ix, bc_which->nullOK); 3939 CHECK; 3940 if (ref == null && bc_which == &bc_classref) { 3941 // Shorthand for class self-references. 3942 ref = thisClass; 3943 } 3944 origBC = bc; 3945 switch (bc) { 3946 case bc_ildc: 3947 case bc_cldc: 3948 case bc_fldc: 3949 case bc_aldc: 3950 origBC = bc_ldc; 3951 break; 3952 case bc_ildc_w: 3953 case bc_cldc_w: 3954 case bc_fldc_w: 3955 case bc_aldc_w: 3956 origBC = bc_ldc_w; 3957 break; 3958 case bc_lldc2_w: 3959 case bc_dldc2_w: 3960 origBC = bc_ldc2_w; 3961 break; 3962 case bc_new: 3963 newClass = ref; 3964 break; 3965 } 3966 wp[-1] = origBC; // overwrite with origBC 3967 if (origBC == bc_ldc) { 3968 putu1ref(ref); 3969 } else { 3970 putref(ref); 3971 } 3972 if (origBC == bc_multianewarray) { 3973 // Copy the trailing byte also. 3974 int val = bc_byte.getByte(); 3975 putu1_fast(val); 3976 } else if (origBC == bc_invokeinterface) { 3977 int argSize = ref->memberDescr()->descrType()->typeSize(); 3978 putu1_fast(1 + argSize); 3979 putu1_fast(0); 3980 } 3981 continue; 3982 } 3983 if (is_local_slot_op(bc)) { 3984 int local = bc_local.getInt(); 3985 if (isWide) { 3986 putu2(local); 3987 if (bc == bc_iinc) { 3988 int iVal = bc_short.getInt(); 3989 putu2(iVal); 3990 } 3991 } else { 3992 putu1_fast(local); 3993 if (bc == bc_iinc) { 3994 int iVal = bc_byte.getByte(); 3995 putu1_fast(iVal); 3996 } 3997 } 3998 continue; 3999 } 4000 // Random bytecode. Just copy it. 4001 assert(bc < bc_bytecode_limit); 4002 } 4003 } 4004 doneScanningMethod:{} 4005 //bcimap.add(curPC); // PC limit is already also in map, from bc_end_marker 4006 4007 // Armed with a bcimap, we can now fix up all the labels. 4008 for (int i = 0; i < (int)code_fixup_type.size(); i++) { 4009 int type = code_fixup_type.getByte(i); 4010 byte* bp = wp_at(code_fixup_offset.get(i)); 4011 int curIP = code_fixup_source.get(i); 4012 int destIP = curIP + bc_label.getInt(); 4013 int span = to_bci(destIP) - to_bci(curIP); 4014 switch (type) { 4015 case 2: putu2_at(bp, (ushort)span); break; 4016 case 4: putu4_at(bp, span); break; 4017 default: assert(false); 4018 } 4019 } 4020 } 4021 4022 inline // called exactly once => inline 4023 void unpacker::write_code() { 4024 int j; 4025 4026 int max_stack, max_locals, handler_count, cflags; 4027 get_code_header(max_stack, max_locals, handler_count, cflags); 4028 4029 if (max_stack < 0) max_stack = code_max_stack.getInt(); 4030 if (max_locals < 0) max_locals = code_max_na_locals.getInt(); 4031 if (handler_count < 0) handler_count = code_handler_count.getInt(); 4032 4033 int siglen = cur_descr->descrType()->typeSize(); 4034 CHECK; 4035 if ((cur_descr_flags & ACC_STATIC) == 0) siglen++; 4036 max_locals += siglen; 4037 4038 putu2(max_stack); 4039 putu2(max_locals); 4040 size_t bcbase = put_empty(4); 4041 4042 // Write the bytecodes themselves. 4043 write_bc_ops(); 4044 CHECK; 4045 4046 byte* bcbasewp = wp_at(bcbase); 4047 putu4_at(bcbasewp, (int)(wp - (bcbasewp+4))); // size of code attr 4048 4049 putu2(handler_count); 4050 for (j = 0; j < handler_count; j++) { 4051 int bii = code_handler_start_P.getInt(); 4052 putu2(to_bci(bii)); 4053 bii += code_handler_end_PO.getInt(); 4054 putu2(to_bci(bii)); 4055 bii += code_handler_catch_PO.getInt(); 4056 putu2(to_bci(bii)); 4057 putref(code_handler_class_RCN.getRefN()); 4058 CHECK; 4059 } 4060 4061 julong indexBits = cflags; 4062 if (cflags < 0) { 4063 bool haveLongFlags = attr_defs[ATTR_CONTEXT_CODE].haveLongFlags(); 4064 indexBits = code_flags_hi.getLong(code_flags_lo, haveLongFlags); 4065 } 4066 write_attrs(ATTR_CONTEXT_CODE, indexBits); 4067 } 4068 4069 int unpacker::write_attrs(int attrc, julong indexBits) { 4070 CHECK_0; 4071 if (indexBits == 0) { 4072 // Quick short-circuit. 4073 putu2(0); 4074 return 0; 4075 } 4076 4077 attr_definitions& ad = attr_defs[attrc]; 4078 4079 int i, j, j2, idx, count; 4080 4081 int oiCount = 0; 4082 if (ad.isPredefined(X_ATTR_OVERFLOW) 4083 && (indexBits & ((julong)1<<X_ATTR_OVERFLOW)) != 0) { 4084 indexBits -= ((julong)1<<X_ATTR_OVERFLOW); 4085 oiCount = ad.xxx_attr_count().getInt(); 4086 } 4087 4088 int bitIndexes[X_ATTR_LIMIT_FLAGS_HI]; 4089 int biCount = 0; 4090 4091 // Fill bitIndexes with index bits, in order. 4092 for (idx = 0; indexBits != 0; idx++, indexBits >>= 1) { 4093 if ((indexBits & 1) != 0) 4094 bitIndexes[biCount++] = idx; 4095 } 4096 assert(biCount <= (int)lengthof(bitIndexes)); 4097 4098 // Write a provisional attribute count, perhaps to be corrected later. 4099 int naOffset = (int)wpoffset(); 4100 int na0 = biCount + oiCount; 4101 putu2(na0); 4102 4103 int na = 0; 4104 for (i = 0; i < na0; i++) { 4105 if (i < biCount) 4106 idx = bitIndexes[i]; 4107 else 4108 idx = ad.xxx_attr_indexes().getInt(); 4109 assert(ad.isIndex(idx)); 4110 entry* aname = null; 4111 entry* ref; // scratch 4112 size_t abase = put_empty(2+4); 4113 CHECK_0; 4114 if (idx < (int)ad.flag_limit && ad.isPredefined(idx)) { 4115 // Switch on the attrc and idx simultaneously. 4116 switch (ADH_BYTE(attrc, idx)) { 4117 4118 case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_OVERFLOW): 4119 case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_OVERFLOW): 4120 case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_OVERFLOW): 4121 case ADH_BYTE(ATTR_CONTEXT_CODE, X_ATTR_OVERFLOW): 4122 // no attribute at all, so back up on this one 4123 wp = wp_at(abase); 4124 continue; 4125 4126 case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_ClassFile_version): 4127 cur_class_minver = class_ClassFile_version_minor_H.getInt(); 4128 cur_class_majver = class_ClassFile_version_major_H.getInt(); 4129 // back up; not a real attribute 4130 wp = wp_at(abase); 4131 continue; 4132 4133 case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_InnerClasses): 4134 // note the existence of this attr, but save for later 4135 if (cur_class_has_local_ics) 4136 abort("too many InnerClasses attrs"); 4137 cur_class_has_local_ics = true; 4138 wp = wp_at(abase); 4139 continue; 4140 4141 case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_SourceFile): 4142 aname = cp.sym[cpool::s_SourceFile]; 4143 ref = class_SourceFile_RUN.getRefN(); 4144 CHECK_0; 4145 if (ref == null) { 4146 bytes& n = cur_class->ref(0)->value.b; 4147 // parse n = (<pkg>/)*<outer>?($<id>)* 4148 int pkglen = lastIndexOf(SLASH_MIN, SLASH_MAX, n, (int)n.len)+1; 4149 bytes prefix = n.slice(pkglen, n.len); 4150 for (;;) { 4151 // Work backwards, finding all '$', '#', etc. 4152 int dollar = lastIndexOf(DOLLAR_MIN, DOLLAR_MAX, prefix, (int)prefix.len); 4153 if (dollar < 0) break; 4154 prefix = prefix.slice(0, dollar); 4155 } 4156 const char* suffix = ".java"; 4157 int len = (int)(prefix.len + strlen(suffix)); 4158 bytes name; name.set(T_NEW(byte, add_size(len, 1)), len); 4159 name.strcat(prefix).strcat(suffix); 4160 ref = cp.ensureUtf8(name); 4161 } 4162 putref(ref); 4163 break; 4164 4165 case ADH_BYTE(ATTR_CONTEXT_CLASS, CLASS_ATTR_EnclosingMethod): 4166 aname = cp.sym[cpool::s_EnclosingMethod]; 4167 putref(class_EnclosingMethod_RC.getRefN()); 4168 CHECK_0; 4169 putref(class_EnclosingMethod_RDN.getRefN()); 4170 break; 4171 4172 case ADH_BYTE(ATTR_CONTEXT_FIELD, FIELD_ATTR_ConstantValue): 4173 aname = cp.sym[cpool::s_ConstantValue]; 4174 putref(field_ConstantValue_KQ.getRefUsing(cp.getKQIndex())); 4175 break; 4176 4177 case ADH_BYTE(ATTR_CONTEXT_METHOD, METHOD_ATTR_Code): 4178 aname = cp.sym[cpool::s_Code]; 4179 write_code(); 4180 break; 4181 4182 case ADH_BYTE(ATTR_CONTEXT_METHOD, METHOD_ATTR_Exceptions): 4183 aname = cp.sym[cpool::s_Exceptions]; 4184 putu2(count = method_Exceptions_N.getInt()); 4185 for (j = 0; j < count; j++) { 4186 putref(method_Exceptions_RC.getRefN()); 4187 CHECK_0; 4188 } 4189 break; 4190 4191 case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_StackMapTable): 4192 aname = cp.sym[cpool::s_StackMapTable]; 4193 // (keep this code aligned with its brother in unpacker::read_attrs) 4194 putu2(count = code_StackMapTable_N.getInt()); 4195 for (j = 0; j < count; j++) { 4196 int tag = code_StackMapTable_frame_T.getByte(); 4197 putu1(tag); 4198 if (tag <= 127) { 4199 // (64-127) [(2)] 4200 if (tag >= 64) put_stackmap_type(); 4201 } else if (tag <= 251) { 4202 // (247) [(1)(2)] 4203 // (248-251) [(1)] 4204 if (tag >= 247) putu2(code_StackMapTable_offset.getInt()); 4205 if (tag == 247) put_stackmap_type(); 4206 } else if (tag <= 254) { 4207 // (252) [(1)(2)] 4208 // (253) [(1)(2)(2)] 4209 // (254) [(1)(2)(2)(2)] 4210 putu2(code_StackMapTable_offset.getInt()); 4211 CHECK_0; 4212 for (int k = (tag - 251); k > 0; k--) { 4213 put_stackmap_type(); 4214 CHECK_0; 4215 } 4216 } else { 4217 // (255) [(1)NH[(2)]NH[(2)]] 4218 putu2(code_StackMapTable_offset.getInt()); 4219 putu2(j2 = code_StackMapTable_local_N.getInt()); 4220 while (j2-- > 0) {put_stackmap_type(); CHECK_0;} 4221 putu2(j2 = code_StackMapTable_stack_N.getInt()); 4222 while (j2-- > 0) {put_stackmap_type(); CHECK_0;} 4223 } 4224 } 4225 break; 4226 4227 case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LineNumberTable): 4228 aname = cp.sym[cpool::s_LineNumberTable]; 4229 putu2(count = code_LineNumberTable_N.getInt()); 4230 for (j = 0; j < count; j++) { 4231 putu2(to_bci(code_LineNumberTable_bci_P.getInt())); 4232 putu2(code_LineNumberTable_line.getInt()); 4233 } 4234 break; 4235 4236 case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LocalVariableTable): 4237 aname = cp.sym[cpool::s_LocalVariableTable]; 4238 putu2(count = code_LocalVariableTable_N.getInt()); 4239 for (j = 0; j < count; j++) { 4240 int bii = code_LocalVariableTable_bci_P.getInt(); 4241 int bci = to_bci(bii); 4242 putu2(bci); 4243 bii += code_LocalVariableTable_span_O.getInt(); 4244 putu2(to_bci(bii) - bci); 4245 putref(code_LocalVariableTable_name_RU.getRefN()); 4246 CHECK_0; 4247 putref(code_LocalVariableTable_type_RS.getRefN()); 4248 CHECK_0; 4249 putu2(code_LocalVariableTable_slot.getInt()); 4250 } 4251 break; 4252 4253 case ADH_BYTE(ATTR_CONTEXT_CODE, CODE_ATTR_LocalVariableTypeTable): 4254 aname = cp.sym[cpool::s_LocalVariableTypeTable]; 4255 putu2(count = code_LocalVariableTypeTable_N.getInt()); 4256 for (j = 0; j < count; j++) { 4257 int bii = code_LocalVariableTypeTable_bci_P.getInt(); 4258 int bci = to_bci(bii); 4259 putu2(bci); 4260 bii += code_LocalVariableTypeTable_span_O.getInt(); 4261 putu2(to_bci(bii) - bci); 4262 putref(code_LocalVariableTypeTable_name_RU.getRefN()); 4263 CHECK_0; 4264 putref(code_LocalVariableTypeTable_type_RS.getRefN()); 4265 CHECK_0; 4266 putu2(code_LocalVariableTypeTable_slot.getInt()); 4267 } 4268 break; 4269 4270 case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_Signature): 4271 aname = cp.sym[cpool::s_Signature]; 4272 putref(class_Signature_RS.getRefN()); 4273 break; 4274 4275 case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_Signature): 4276 aname = cp.sym[cpool::s_Signature]; 4277 putref(field_Signature_RS.getRefN()); 4278 break; 4279 4280 case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_Signature): 4281 aname = cp.sym[cpool::s_Signature]; 4282 putref(method_Signature_RS.getRefN()); 4283 break; 4284 4285 case ADH_BYTE(ATTR_CONTEXT_CLASS, X_ATTR_Deprecated): 4286 case ADH_BYTE(ATTR_CONTEXT_FIELD, X_ATTR_Deprecated): 4287 case ADH_BYTE(ATTR_CONTEXT_METHOD, X_ATTR_Deprecated): 4288 aname = cp.sym[cpool::s_Deprecated]; 4289 // no data 4290 break; 4291 } 4292 } 4293 CHECK_0; 4294 if (aname == null) { 4295 // Unparse a compressor-defined attribute. 4296 layout_definition* lo = ad.getLayout(idx); 4297 if (lo == null) { 4298 abort("bad layout index"); 4299 break; 4300 } 4301 assert((int)lo->idx == idx); 4302 aname = lo->nameEntry; 4303 if (aname == null) { 4304 bytes nameb; nameb.set(lo->name); 4305 aname = cp.ensureUtf8(nameb); 4306 // Cache the name entry for next time. 4307 lo->nameEntry = aname; 4308 } 4309 // Execute all the layout elements. 4310 band** bands = lo->bands(); 4311 if (lo->hasCallables()) { 4312 band& cble = *bands[0]; 4313 assert(cble.le_kind == EK_CBLE); 4314 bands = cble.le_body; 4315 } 4316 putlayout(bands); 4317 } 4318 4319 if (aname == null) 4320 abort("bad attribute index"); 4321 CHECK_0; 4322 4323 byte* wp1 = wp; 4324 wp = wp_at(abase); 4325 4326 // DTRT if this attr is on the strip-list. 4327 // (Note that we emptied the data out of the band first.) 4328 if (ad.strip_names.contains(aname)) { 4329 continue; 4330 } 4331 4332 // patch the name and length 4333 putref(aname); 4334 putu4((int)(wp1 - (wp+4))); // put the attr size 4335 wp = wp1; 4336 na++; // count the attrs actually written 4337 } 4338 4339 if (na != na0) 4340 // Refresh changed count. 4341 putu2_at(wp_at(naOffset), na); 4342 return na; 4343 } 4344 4345 void unpacker::write_members(int num, int attrc) { 4346 CHECK; 4347 attr_definitions& ad = attr_defs[attrc]; 4348 band& member_flags_hi = ad.xxx_flags_hi(); 4349 band& member_flags_lo = ad.xxx_flags_lo(); 4350 band& member_descr = (&member_flags_hi)[e_field_descr-e_field_flags_hi]; 4351 assert(endsWith(member_descr.name, "_descr")); 4352 assert(endsWith(member_flags_lo.name, "_flags_lo")); 4353 assert(endsWith(member_flags_lo.name, "_flags_lo")); 4354 bool haveLongFlags = ad.haveLongFlags(); 4355 4356 putu2(num); 4357 julong indexMask = attr_defs[attrc].flagIndexMask(); 4358 for (int i = 0; i < num; i++) { 4359 julong mflags = member_flags_hi.getLong(member_flags_lo, haveLongFlags); 4360 entry* mdescr = member_descr.getRef(); 4361 cur_descr = mdescr; 4362 putu2(cur_descr_flags = (ushort)(mflags & ~indexMask)); 4363 CHECK; 4364 putref(mdescr->descrName()); 4365 putref(mdescr->descrType()); 4366 write_attrs(attrc, (mflags & indexMask)); 4367 CHECK; 4368 } 4369 cur_descr = null; 4370 } 4371 4372 extern "C" 4373 int raw_address_cmp(const void* p1p, const void* p2p) { 4374 void* p1 = *(void**) p1p; 4375 void* p2 = *(void**) p2p; 4376 return (p1 > p2)? 1: (p1 < p2)? -1: 0; 4377 } 4378 4379 void unpacker::write_classfile_tail() { 4380 cur_classfile_tail.empty(); 4381 set_output(&cur_classfile_tail); 4382 4383 int i, num; 4384 4385 attr_definitions& ad = attr_defs[ATTR_CONTEXT_CLASS]; 4386 4387 bool haveLongFlags = ad.haveLongFlags(); 4388 julong kflags = class_flags_hi.getLong(class_flags_lo, haveLongFlags); 4389 julong indexMask = ad.flagIndexMask(); 4390 4391 cur_class = class_this.getRef(); 4392 CHECK; 4393 cur_super = class_super.getRef(); 4394 CHECK; 4395 4396 if (cur_super == cur_class) cur_super = null; 4397 // special representation for java/lang/Object 4398 4399 putu2((ushort)(kflags & ~indexMask)); 4400 putref(cur_class); 4401 putref(cur_super); 4402 4403 putu2(num = class_interface_count.getInt()); 4404 for (i = 0; i < num; i++) { 4405 putref(class_interface.getRef()); 4406 CHECK; 4407 } 4408 4409 write_members(class_field_count.getInt(), ATTR_CONTEXT_FIELD); 4410 write_members(class_method_count.getInt(), ATTR_CONTEXT_METHOD); 4411 CHECK; 4412 4413 cur_class_has_local_ics = false; // may be set true by write_attrs 4414 4415 4416 int naOffset = (int)wpoffset(); 4417 int na = write_attrs(ATTR_CONTEXT_CLASS, (kflags & indexMask)); 4418 4419 4420 // at the very last, choose which inner classes (if any) pertain to k: 4421 #ifdef ASSERT 4422 for (i = 0; i < ic_count; i++) { 4423 assert(!ics[i].requested); 4424 } 4425 #endif 4426 // First, consult the global table and the local constant pool, 4427 // and decide on the globally implied inner classes. 4428 // (Note that we read the cpool's outputIndex fields, but we 4429 // do not yet write them, since the local IC attribute might 4430 // reverse a global decision to declare an IC.) 4431 assert(requested_ics.length() == 0); // must start out empty 4432 // Always include all members of the current class. 4433 for (inner_class* child = cp.getFirstChildIC(cur_class); 4434 child != null; 4435 child = cp.getNextChildIC(child)) { 4436 child->requested = true; 4437 requested_ics.add(child); 4438 } 4439 // And, for each inner class mentioned in the constant pool, 4440 // include it and all its outers. 4441 int noes = cp.outputEntries.length(); 4442 entry** oes = (entry**) cp.outputEntries.base(); 4443 for (i = 0; i < noes; i++) { 4444 entry& e = *oes[i]; 4445 if (e.tag != CONSTANT_Class) continue; // wrong sort 4446 for (inner_class* ic = cp.getIC(&e); 4447 ic != null; 4448 ic = cp.getIC(ic->outer)) { 4449 if (ic->requested) break; // already processed 4450 ic->requested = true; 4451 requested_ics.add(ic); 4452 } 4453 } 4454 int local_ics = requested_ics.length(); 4455 // Second, consult a local attribute (if any) and adjust the global set. 4456 inner_class* extra_ics = null; 4457 int num_extra_ics = 0; 4458 if (cur_class_has_local_ics) { 4459 // adjust the set of ICs by symmetric set difference w/ the locals 4460 num_extra_ics = class_InnerClasses_N.getInt(); 4461 if (num_extra_ics == 0) { 4462 // Explicit zero count has an irregular meaning: It deletes the attr. 4463 local_ics = 0; // (short-circuit all tests of requested bits) 4464 } else { 4465 extra_ics = T_NEW(inner_class, num_extra_ics); 4466 // Note: extra_ics will be freed up by next call to get_next_file(). 4467 } 4468 } 4469 for (i = 0; i < num_extra_ics; i++) { 4470 inner_class& extra_ic = extra_ics[i]; 4471 extra_ic.inner = class_InnerClasses_RC.getRef(); 4472 CHECK; 4473 // Find the corresponding equivalent global IC: 4474 inner_class* global_ic = cp.getIC(extra_ic.inner); 4475 int flags = class_InnerClasses_F.getInt(); 4476 if (flags == 0) { 4477 // The extra IC is simply a copy of a global IC. 4478 if (global_ic == null) { 4479 abort("bad reference to inner class"); 4480 break; 4481 } 4482 extra_ic = (*global_ic); // fill in rest of fields 4483 } else { 4484 flags &= ~ACC_IC_LONG_FORM; // clear high bit if set to get clean zero 4485 extra_ic.flags = flags; 4486 extra_ic.outer = class_InnerClasses_outer_RCN.getRefN(); 4487 CHECK; 4488 extra_ic.name = class_InnerClasses_name_RUN.getRefN(); 4489 CHECK; 4490 // Detect if this is an exact copy of the global tuple. 4491 if (global_ic != null) { 4492 if (global_ic->flags != extra_ic.flags || 4493 global_ic->outer != extra_ic.outer || 4494 global_ic->name != extra_ic.name) { 4495 global_ic = null; // not really the same, so break the link 4496 } 4497 } 4498 } 4499 if (global_ic != null && global_ic->requested) { 4500 // This local repetition reverses the globally implied request. 4501 global_ic->requested = false; 4502 extra_ic.requested = false; 4503 local_ics -= 1; 4504 } else { 4505 // The global either does not exist, or is not yet requested. 4506 extra_ic.requested = true; 4507 local_ics += 1; 4508 } 4509 } 4510 // Finally, if there are any that survived, put them into an attribute. 4511 // (Note that a zero-count attribute is always deleted.) 4512 // The putref calls below will tell the constant pool to add any 4513 // necessary local CP references to support the InnerClasses attribute. 4514 // This step must be the last round of additions to the local CP. 4515 if (local_ics > 0) { 4516 // append the new attribute: 4517 putref(cp.sym[cpool::s_InnerClasses]); 4518 putu4(2 + 2*4*local_ics); 4519 putu2(local_ics); 4520 PTRLIST_QSORT(requested_ics, raw_address_cmp); 4521 int num_global_ics = requested_ics.length(); 4522 for (i = -num_global_ics; i < num_extra_ics; i++) { 4523 inner_class* ic; 4524 if (i < 0) 4525 ic = (inner_class*) requested_ics.get(num_global_ics+i); 4526 else 4527 ic = &extra_ics[i]; 4528 if (ic->requested) { 4529 putref(ic->inner); 4530 putref(ic->outer); 4531 putref(ic->name); 4532 putu2(ic->flags); 4533 NOT_PRODUCT(local_ics--); 4534 } 4535 } 4536 assert(local_ics == 0); // must balance 4537 putu2_at(wp_at(naOffset), ++na); // increment class attr count 4538 } 4539 4540 // Tidy up global 'requested' bits: 4541 for (i = requested_ics.length(); --i >= 0; ) { 4542 inner_class* ic = (inner_class*) requested_ics.get(i); 4543 ic->requested = false; 4544 } 4545 requested_ics.empty(); 4546 4547 CHECK; 4548 close_output(); 4549 4550 // rewrite CP references in the tail 4551 cp.computeOutputIndexes(); 4552 int nextref = 0; 4553 for (i = 0; i < (int)class_fixup_type.size(); i++) { 4554 int type = class_fixup_type.getByte(i); 4555 byte* fixp = wp_at(class_fixup_offset.get(i)); 4556 entry* e = (entry*)class_fixup_ref.get(nextref++); 4557 int idx = e->getOutputIndex(); 4558 switch (type) { 4559 case 1: putu1_at(fixp, idx); break; 4560 case 2: putu2_at(fixp, idx); break; 4561 default: assert(false); // should not reach here 4562 } 4563 } 4564 CHECK; 4565 } 4566 4567 void unpacker::write_classfile_head() { 4568 cur_classfile_head.empty(); 4569 set_output(&cur_classfile_head); 4570 4571 putu4(JAVA_MAGIC); 4572 putu2(cur_class_minver); 4573 putu2(cur_class_majver); 4574 putu2(cp.outputIndexLimit); 4575 4576 int checkIndex = 1; 4577 int noes = cp.outputEntries.length(); 4578 entry** oes = (entry**) cp.outputEntries.base(); 4579 for (int i = 0; i < noes; i++) { 4580 entry& e = *oes[i]; 4581 assert(e.getOutputIndex() == checkIndex++); 4582 byte tag = e.tag; 4583 assert(tag != CONSTANT_Signature); 4584 putu1(tag); 4585 switch (tag) { 4586 case CONSTANT_Utf8: 4587 putu2((int)e.value.b.len); 4588 put_bytes(e.value.b); 4589 break; 4590 case CONSTANT_Integer: 4591 case CONSTANT_Float: 4592 putu4(e.value.i); 4593 break; 4594 case CONSTANT_Long: 4595 case CONSTANT_Double: 4596 putu8(e.value.l); 4597 assert(checkIndex++); 4598 break; 4599 case CONSTANT_Class: 4600 case CONSTANT_String: 4601 // just write the ref 4602 putu2(e.refs[0]->getOutputIndex()); 4603 break; 4604 case CONSTANT_Fieldref: 4605 case CONSTANT_Methodref: 4606 case CONSTANT_InterfaceMethodref: 4607 case CONSTANT_NameandType: 4608 putu2(e.refs[0]->getOutputIndex()); 4609 putu2(e.refs[1]->getOutputIndex()); 4610 break; 4611 default: 4612 abort(ERROR_INTERNAL); 4613 } 4614 } 4615 4616 #ifndef PRODUCT 4617 total_cp_size[0] += cp.outputIndexLimit; 4618 total_cp_size[1] += (int)cur_classfile_head.size(); 4619 #endif 4620 close_output(); 4621 } 4622 4623 unpacker::file* unpacker::get_next_file() { 4624 CHECK_0; 4625 free_temps(); 4626 if (files_remaining == 0) { 4627 // Leave a clue that we're exhausted. 4628 cur_file.name = null; 4629 cur_file.size = null; 4630 if (archive_size != 0) { 4631 julong predicted_size = unsized_bytes_read + archive_size; 4632 if (predicted_size != bytes_read) 4633 abort("archive header had incorrect size"); 4634 } 4635 return null; 4636 } 4637 files_remaining -= 1; 4638 assert(files_written < file_count || classes_written < class_count); 4639 cur_file.name = ""; 4640 cur_file.size = 0; 4641 cur_file.modtime = default_file_modtime; 4642 cur_file.options = default_file_options; 4643 cur_file.data[0].set(null, 0); 4644 cur_file.data[1].set(null, 0); 4645 if (files_written < file_count) { 4646 entry* e = file_name.getRef(); 4647 CHECK_0; 4648 cur_file.name = e->utf8String(); 4649 bool haveLongSize = ((archive_options & AO_HAVE_FILE_SIZE_HI) != 0); 4650 cur_file.size = file_size_hi.getLong(file_size_lo, haveLongSize); 4651 if ((archive_options & AO_HAVE_FILE_MODTIME) != 0) 4652 cur_file.modtime += file_modtime.getInt(); //relative to archive modtime 4653 if ((archive_options & AO_HAVE_FILE_OPTIONS) != 0) 4654 cur_file.options |= file_options.getInt() & ~suppress_file_options; 4655 } else if (classes_written < class_count) { 4656 // there is a class for a missing file record 4657 cur_file.options |= FO_IS_CLASS_STUB; 4658 } 4659 if ((cur_file.options & FO_IS_CLASS_STUB) != 0) { 4660 assert(classes_written < class_count); 4661 classes_written += 1; 4662 if (cur_file.size != 0) { 4663 abort("class file size transmitted"); 4664 return null; 4665 } 4666 reset_cur_classfile(); 4667 4668 // write the meat of the classfile: 4669 write_classfile_tail(); 4670 cur_file.data[1] = cur_classfile_tail.b; 4671 CHECK_0; 4672 4673 // write the CP of the classfile, second: 4674 write_classfile_head(); 4675 cur_file.data[0] = cur_classfile_head.b; 4676 CHECK_0; 4677 4678 cur_file.size += cur_file.data[0].len; 4679 cur_file.size += cur_file.data[1].len; 4680 if (cur_file.name[0] == '\0') { 4681 bytes& prefix = cur_class->ref(0)->value.b; 4682 const char* suffix = ".class"; 4683 int len = (int)(prefix.len + strlen(suffix)); 4684 bytes name; name.set(T_NEW(byte, add_size(len, 1)), len); 4685 cur_file.name = name.strcat(prefix).strcat(suffix).strval(); 4686 } 4687 } else { 4688 // If there is buffered file data, produce a pointer to it. 4689 if (cur_file.size != (size_t) cur_file.size) { 4690 // Silly size specified. 4691 abort("resource file too large"); 4692 return null; 4693 } 4694 size_t rpleft = input_remaining(); 4695 if (rpleft > 0) { 4696 if (rpleft > cur_file.size) 4697 rpleft = (size_t) cur_file.size; 4698 cur_file.data[0].set(rp, rpleft); 4699 rp += rpleft; 4700 } 4701 if (rpleft < cur_file.size) { 4702 // Caller must read the rest. 4703 size_t fleft = (size_t)cur_file.size - rpleft; 4704 bytes_read += fleft; // Credit it to the overall archive size. 4705 } 4706 } 4707 CHECK_0; 4708 bytes_written += cur_file.size; 4709 files_written += 1; 4710 return &cur_file; 4711 } 4712 4713 // Write a file to jarout. 4714 void unpacker::write_file_to_jar(unpacker::file* f) { 4715 size_t htsize = f->data[0].len + f->data[1].len; 4716 julong fsize = f->size; 4717 #ifndef PRODUCT 4718 if (nowrite NOT_PRODUCT(|| skipfiles-- > 0)) { 4719 PRINTCR((2,"would write %d bytes to %s", (int) fsize, f->name)); 4720 return; 4721 } 4722 #endif 4723 if (htsize == fsize) { 4724 jarout->addJarEntry(f->name, f->deflate_hint(), f->modtime, 4725 f->data[0], f->data[1]); 4726 } else { 4727 assert(input_remaining() == 0); 4728 bytes part1, part2; 4729 part1.len = f->data[0].len; 4730 part1.set(T_NEW(byte, part1.len), part1.len); 4731 part1.copyFrom(f->data[0]); 4732 assert(f->data[1].len == 0); 4733 part2.set(null, 0); 4734 size_t fleft = (size_t) fsize - part1.len; 4735 assert(bytes_read > fleft); // part2 already credited by get_next_file 4736 bytes_read -= fleft; 4737 if (fleft > 0) { 4738 // Must read some more. 4739 if (live_input) { 4740 // Stop using the input buffer. Make a new one: 4741 if (free_input) input.free(); 4742 input.init(fleft > (1<<12) ? fleft : (1<<12)); 4743 free_input = true; 4744 live_input = false; 4745 } else { 4746 // Make it large enough. 4747 assert(free_input); // must be reallocable 4748 input.ensureSize(fleft); 4749 } 4750 rplimit = rp = input.base(); 4751 CHECK; 4752 input.setLimit(rp + fleft); 4753 if (!ensure_input(fleft)) 4754 abort("EOF reading resource file"); 4755 part2.ptr = input_scan(); 4756 part2.len = input_remaining(); 4757 rplimit = rp = input.base(); 4758 } 4759 jarout->addJarEntry(f->name, f->deflate_hint(), f->modtime, 4760 part1, part2); 4761 } 4762 if (verbose >= 3) { 4763 fprintf(errstrm, "Wrote " 4764 LONG_LONG_FORMAT " bytes to: %s\n", fsize, f->name); 4765 } 4766 } 4767 4768 // Redirect the stdio to the specified file in the unpack.log.file option 4769 void unpacker::redirect_stdio() { 4770 if (log_file == null) { 4771 log_file = LOGFILE_STDOUT; 4772 } 4773 if (log_file == errstrm_name) 4774 // Nothing more to be done. 4775 return; 4776 errstrm_name = log_file; 4777 if (strcmp(log_file, LOGFILE_STDERR) == 0) { 4778 errstrm = stderr; 4779 return; 4780 } else if (strcmp(log_file, LOGFILE_STDOUT) == 0) { 4781 errstrm = stdout; 4782 return; 4783 } else if (log_file[0] != '\0' && (errstrm = fopen(log_file,"a+")) != NULL) { 4784 return; 4785 } else { 4786 fprintf(stderr, "Can not open log file %s\n", log_file); 4787 // Last resort 4788 // (Do not use stdout, since it might be jarout->jarfp.) 4789 errstrm = stderr; 4790 log_file = errstrm_name = LOGFILE_STDERR; 4791 } 4792 } 4793 4794 #ifndef PRODUCT 4795 int unpacker::printcr_if_verbose(int level, const char* fmt ...) { 4796 if (verbose < level+10) return 0; 4797 va_list vl; 4798 va_start(vl, fmt); 4799 char fmtbuf[300]; 4800 strcpy(fmtbuf+100, fmt); 4801 strcat(fmtbuf+100, "\n"); 4802 char* fmt2 = fmtbuf+100; 4803 while (level-- > 0) *--fmt2 = ' '; 4804 vfprintf(errstrm, fmt2, vl); 4805 return 1; // for ?: usage 4806 } 4807 #endif 4808 4809 void unpacker::abort(const char* message) { 4810 if (message == null) message = "error unpacking archive"; 4811 #ifdef UNPACK_JNI 4812 if (message[0] == '@') { // secret convention for sprintf 4813 bytes saved; 4814 saved.saveFrom(message+1); 4815 mallocs.add(message = saved.strval()); 4816 } 4817 abort_message = message; 4818 return; 4819 #else 4820 if (message[0] == '@') ++message; 4821 fprintf(errstrm, "%s\n", message); 4822 #ifndef PRODUCT 4823 fflush(errstrm); 4824 ::abort(); 4825 #else 4826 exit(-1); 4827 #endif 4828 #endif // JNI 4829 } 4830 4831