Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyFactory/TestProviderLeak.java
38867 views
/*1* Copyright (c) 2005, 2013, 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* @test25* @bug 6578538 802762426* @summary com.sun.crypto.provider.SunJCE instance leak using KRB5 and27* LoginContext28* @author Brad Wetmore29*30* @run main/othervm -Xmx20m TestProviderLeak31*32*/3334/*35* We force the leak to become a problem by eating up most JVM free memory.36* In current runs on a server and client machine, it took roughly 50-15037* iterations to have the memory leak or time-out shut down other operations.38* It complained about "JCE cannot authenticate the provider SunJCE" or timed39* out.40*/4142import javax.crypto.*;43import javax.crypto.spec.*;4445import java.util.*;46import java.util.concurrent.*;4748public class TestProviderLeak {49private static final int MB = 1024 * 1024;50// Currently, 3MB heap size is reserved for running testing iterations.51// It is tweaked to make sure the test quickly triggers the memory leak52// or throws out TimeoutException.53private static final int RESERVATION = 3;54// The maximum time, 5 seconds, to wait for each iteration.55private static final int TIME_OUT = 5;5657private static Deque<byte []> eatupMemory() throws Exception {58dumpMemoryStats("Before memory allocation");5960Deque<byte []> data = new ArrayDeque<byte []>();61boolean hasException = false;62while (!hasException) {63byte [] megaByte;64try {65megaByte = new byte [MB];66data.add(megaByte);67} catch (OutOfMemoryError e) {68megaByte = null; // Free memory ASAP6970int size = data.size();7172for (int j = 0; j < RESERVATION && !data.isEmpty(); j++) {73data.removeLast();74}75System.gc();76hasException = true;77System.out.println("OOME is thrown when allocating "78+ size + "MB memory.");79}80}81dumpMemoryStats("After memory allocation");8283return data;84}8586private static void dumpMemoryStats(String s) throws Exception {87Runtime rt = Runtime.getRuntime();88System.out.println(s + ":\t"89+ rt.freeMemory() + " bytes free");90}9192public static void main(String [] args) throws Exception {93// Prepare the test94final SecretKeyFactory skf =95SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1", "SunJCE");96final PBEKeySpec pbeKS = new PBEKeySpec(97"passPhrase".toCharArray(), new byte [] { 0 }, 5, 512);9899ExecutorService executor = Executors.newSingleThreadExecutor();100Callable<SecretKey> task = new Callable<SecretKey>() {101@Override102public SecretKey call() throws Exception {103return skf.generateSecret(pbeKS);104}105};106107// Eat up memory108Deque<byte []> dummyData = eatupMemory();109assert (dummyData != null);110111// Start testing iteration112try {113for (int i = 0; i <= 1000; i++) {114if ((i % 20) == 0) {115// Calling gc() isn't dependable, but doesn't hurt.116// Gives better output in leak cases.117System.gc();118dumpMemoryStats("Iteration " + i);119}120121Future<SecretKey> future = executor.submit(task);122123try {124future.get(TIME_OUT, TimeUnit.SECONDS);125} catch (Exception e) {126dumpMemoryStats("\nException seen at iteration " + i);127throw e;128}129}130} finally {131// JTReg will time out after two minutes. Proactively release132// the memory to avoid JTReg time-out situation.133dummyData = null;134System.gc();135dumpMemoryStats("Memory dereference");136executor.shutdownNow();137}138}139}140141142