jdk/src/share/native/com/sun/java/util/jar/pack/utils.cpp
author ksrini
Mon, 14 Jan 2013 15:46:54 -0800
changeset 15261 c5b882836677
parent 10907 ea32ea5b72d7
permissions -rw-r--r--
8005252: pack200 should support MethodParameters Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10907
ea32ea5b72d7 7057857: SIGSEGV [libunpack.so] store_Utf8_char(signed char*, unsigned short) in java.util.jar.pack200
ksrini
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdarg.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#ifdef _MSC_VER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <direct.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <process.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "constants.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "defines.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "bytes.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "utils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "unpack.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
2602
5b394a4b6ce1 6755943: Java JAR Pack200 Decompression should enforce stricter header checks
ksrini
parents: 2
diff changeset
    49
void* must_malloc(size_t size) {
5b394a4b6ce1 6755943: Java JAR Pack200 Decompression should enforce stricter header checks
ksrini
parents: 2
diff changeset
    50
  size_t msize = size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  #ifdef USE_MTRACE
2602
5b394a4b6ce1 6755943: Java JAR Pack200 Decompression should enforce stricter header checks
ksrini
parents: 2
diff changeset
    52
  if (msize >= 0 && msize < sizeof(int))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    msize = sizeof(int);  // see 0xbaadf00d below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  #endif
10907
ea32ea5b72d7 7057857: SIGSEGV [libunpack.so] store_Utf8_char(signed char*, unsigned short) in java.util.jar.pack200
ksrini
parents: 5506
diff changeset
    55
  void* ptr = (msize > PSIZE_MAX || msize <= 0) ? null : malloc(msize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  if (ptr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    memset(ptr, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    unpack_abort(ERROR_ENOMEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  mtrace('m', ptr, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  return ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
void mkdirs(int oklen, char* path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    67
  if (strlen(path) <= (size_t)oklen)  return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  char dir[PATH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  strcpy(dir, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  char* slash = strrchr(dir, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  if (slash == 0)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  *slash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  mkdirs(oklen, dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  MKDIR(dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#ifndef PRODUCT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
void breakpoint() { }  // hook for debugger
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    81
int assert_failed(const char* p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  char message[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  sprintf(message, "@assert failed: %s\n", p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  fprintf(stdout, 1+message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  breakpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
  unpack_abort(message);
1082
53833ff90c45 6496269: Many warnings generated from com/sun/java/util/jar/pack/*.cpp when compiled on Linux
ohair
parents: 2
diff changeset
    87
  return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
void unpack_abort(const char* msg, unpacker* u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  if (msg == null)  msg = "corrupt pack file or internal error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
  if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    u = unpacker::current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  if (u == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    fprintf(stderr, "Error: unpacker: %s\n", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    ::abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  u->abort(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
bool unpack_aborting(unpacker* u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    u = unpacker::current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  if (u == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    fprintf(stderr, "Error: unpacker: no current instance\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    ::abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  return u->aborting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
#ifdef USE_MTRACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
// Use this occasionally for detecting storage leaks in unpack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
void mtrace(char c, void* ptr, size_t size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  if (c == 'f')  *(int*)ptr = 0xbaadf00d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  static FILE* mtfp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  if (mtfp == (FILE*)-1)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  if (mtfp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (getenv("USE_MTRACE") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      mtfp = (FILE*)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    char fname[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    sprintf(fname, "mtr%d.txt", getpid());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    mtfp = fopen(fname, "w");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if (mtfp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
      mtfp = stdout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  fprintf(mtfp, "%c %p %p\n", c, ptr, (void*)size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/* # Script for processing memory traces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
   # It should report only a limited number (2) of "suspended" blocks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
   # even if a large number of archive segments are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
   # It should report no "leaked" blocks at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   nawk < mtr*.txt '
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   function checkleaks(what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     nd = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     for (ptr in allocated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
       if (allocated[ptr] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         print NR ": " what " " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         #allocated[ptr] = 0  # stop the dangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
         nd++
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     if (nd > 0)  print NR ": count " what " " nd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
   /^[mfr]/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       ptr = $2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
       a1 = ($1 == "m")? 1: 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
       a0 = 0+allocated[ptr]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
       allocated[ptr] = a1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
       if (a0 + a1 != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         if (a0 == 0 && a1 == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
           print NR ": double free " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         else if (a0 == 1 && a1 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
           print NR ": double malloc " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
           print NR ": oddity " $0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
       next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
   /^s/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     checkleaks("suspended")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     print NR ": unrecognized " $0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
   END {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     checkleaks("leaked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
#endif // USE_MTRACE