src/jdk.pack/share/native/common-unpack/utils.cpp
author jwilhelm
Thu, 27 Jun 2019 03:13:54 +0200
changeset 55515 4c52949a3487
parent 47216 71c04702a3d5
permissions -rw-r--r--
Added tag jdk-13+27 for changeset b7f68ddec66f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29591
diff changeset
     2
 * Copyright (c) 2001, 2015, 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
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29591
diff changeset
    80
#ifndef STATIC_BUILD
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29591
diff changeset
    81
// use the definition in libjvm when building statically
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
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
    83
int assert_failed(const char* p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  char message[1<<12];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
  sprintf(message, "@assert failed: %s\n", p);
29591
51244d1ddffc 8074839: Resolve disabled warnings for libunpack and the unpack200 binary
mikael
parents: 29368
diff changeset
    86
  fprintf(stdout, "%s", 1+message);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  breakpoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
  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
    89
  return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#endif
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 29591
diff changeset
    92
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
void unpack_abort(const char* msg, unpacker* u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
  if (msg == null)  msg = "corrupt pack file or internal error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    u = unpacker::current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
  if (u == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    fprintf(stderr, "Error: unpacker: %s\n", msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    ::abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  u->abort(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
bool unpack_aborting(unpacker* u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  if (u == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    u = unpacker::current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  if (u == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    fprintf(stderr, "Error: unpacker: no current instance\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    ::abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  return u->aborting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#ifdef USE_MTRACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
// Use this occasionally for detecting storage leaks in unpack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
void mtrace(char c, void* ptr, size_t size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  if (c == 'f')  *(int*)ptr = 0xbaadf00d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  static FILE* mtfp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  if (mtfp == (FILE*)-1)  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  if (mtfp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    if (getenv("USE_MTRACE") == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
      mtfp = (FILE*)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
      return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    char fname[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    sprintf(fname, "mtr%d.txt", getpid());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    mtfp = fopen(fname, "w");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (mtfp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      mtfp = stdout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  fprintf(mtfp, "%c %p %p\n", c, ptr, (void*)size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
/* # Script for processing memory traces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
   # It should report only a limited number (2) of "suspended" blocks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
   # even if a large number of archive segments are processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
   # It should report no "leaked" blocks at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
   nawk < mtr*.txt '
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
   function checkleaks(what) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     nd = 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     for (ptr in allocated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
       if (allocated[ptr] == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
         print NR ": " what " " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         #allocated[ptr] = 0  # stop the dangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         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
     if (nd > 0)  print NR ": count " what " " nd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
   /^[mfr]/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
       ptr = $2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
       a1 = ($1 == "m")? 1: 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
       a0 = 0+allocated[ptr]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
       allocated[ptr] = a1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
       if (a0 + a1 != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         if (a0 == 0 && a1 == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
           print NR ": double free " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         else if (a0 == 1 && a1 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
           print NR ": double malloc " ptr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
           print NR ": oddity " $0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
       next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   /^s/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     checkleaks("suspended")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     print NR ": unrecognized " $0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
   END {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     checkleaks("leaked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
#endif // USE_MTRACE