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