Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/6842838/CreateBadJar.java
38841 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Borrowing significantly from Martin Buchholz's CorruptedZipFiles.java25*26* Needed a way of testing the checks for corrupt zip/jar entry in27* inflate_file from file j2se/src/share/bin/parse_manifest.c28* and running them with the 64-bit launcher. e.g.29* sparcv9/bin/java -jar badjar.jar30*31* Run from a script driver Test6842838.sh as we want to specifically run32* bin/sparcv9/java, the 64-bit launcher.33*34* So this program will create a zip file and damage it in the way35* required to tickle this bug.36*37* It will cause a buffer overrun: but that will not always crash.38* Use libumem preloaded by the script driver in order to39* abort quickly when the overrun happens. That makes the test40* Solaris-specific.41*/4243import java.util.*;44import java.util.zip.*;45import java.io.*;46import static java.lang.System.*;47import static java.util.zip.ZipFile.*;4849public class CreateBadJar {5051public static void main(String [] arguments) {5253if (arguments.length != 2) {54throw new RuntimeException("Arguments: jarfilename entryname");55}56String outFile = arguments[0];57String entryName = arguments[1];5859try {60// If the named file doesn't exist, create it.61// If it does, we are expecting it to contain the named entry, for62// alteration.63if (!new File(outFile).exists()) {64System.out.println("Creating file " + outFile);6566// Create the requested zip/jar file.67ZipOutputStream zos = null;68zos = new ZipOutputStream(69new FileOutputStream(outFile));7071ZipEntry e = new ZipEntry(entryName);72zos.putNextEntry(e);73for (int j=0; j<50000; j++) {74zos.write((int)'a');75}76zos.closeEntry();77zos.close();78zos = null;79}8081// Read it.82int len = (int)(new File(outFile).length());83byte[] good = new byte[len];84FileInputStream fis = new FileInputStream(outFile);85fis.read(good);86fis.close();87fis = null;8889int endpos = len - ENDHDR;90int cenpos = u16(good, endpos+ENDOFF);91if (u32(good, cenpos) != CENSIG) throw new RuntimeException("Where's CENSIG?");9293byte[] bad;94bad = good.clone();9596// Corrupt it...97int pos = findInCEN(bad, cenpos, entryName);9899// What bad stuff are we doing to it?100// Store a 32-bit -1 in uncomp size.101bad[pos+0x18]=(byte)0xff;102bad[pos+0x19]=(byte)0xff;103bad[pos+0x1a]=(byte)0xff;104bad[pos+0x1b]=(byte)0xff;105106// Bad work complete, delete the original.107new File(outFile).delete();108109// Write it.110FileOutputStream fos = new FileOutputStream(outFile);111fos.write(bad);112fos.close();113fos = null;114115} catch (Exception e) {116e.printStackTrace();117}118119}120121/*122* Scan Central Directory File Headers looking for the named entry.123*/124125static int findInCEN(byte[] bytes, int cenpos, String entryName) {126int pos = cenpos;127int nextPos = 0;128String filename = null;129do {130if (nextPos != 0) {131pos = nextPos;132}133System.out.println("entry at pos = " + pos);134if (u32(bytes, pos) != CENSIG) throw new RuntimeException ("entry not found in CEN or premature end...");135136int csize = u32(bytes, pos+0x14); // +0x14 1 dword csize137int uncompsize = u32(bytes, pos+0x18); // +0x18 1 dword uncomp size138int filenameLength = u16(bytes, pos+0x1c); // +0x1c 1 word length of filename139int extraLength = u16(bytes, pos+0x1e); // +0x1e 1 world length of extra field140int commentLength = u16(bytes, pos+0x20); // +0x20 1 world length of file comment141filename = new String(bytes, pos+0x2e, filenameLength); // +0x2e chars of filename142int offset = u32(bytes, pos+0x2a); // +0x2a chars of filename143144System.out.println("filename = " + filename + "\ncsize = " + csize +145" uncomp.size = " + uncompsize +" file offset = " + offset);146nextPos = pos + 0x2e + filenameLength + extraLength + commentLength;147148} while (!filename.equals(entryName));149150System.out.println("entry found at pos = " + pos);151return pos;152}153154static int u8(byte[] data, int offset) {155return data[offset]&0xff;156}157158static int u16(byte[] data, int offset) {159return u8(data,offset) + (u8(data,offset+1)<<8);160}161162static int u32(byte[] data, int offset) {163return u16(data,offset) + (u16(data,offset+2)<<16);164}165166}167168169170