1 /* 2 * Copyright (c) 2014, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8046516 27 * @summary Segmentation fault in JVM (easily reproducible) 28 * @run main/othervm -XX:-TieredCompilation -Xbatch TestLogSum 29 * @author jackkamm@gmail.com 30 */ 31 32 import java.util.Arrays; 33 import java.util.HashMap; 34 import java.util.List; 35 import java.util.Map; 36 public class TestLogSum { 37 public static void main(String[] args) { 38 double sum; 39 40 for (int i = 0; i < 6; i++) { 41 for (int n = 2; n < 30; n++) { 42 for (int j = 1; j <= n; j++) { 43 for (int k = 1; k <= j; k++) { 44 // System.out.println(computeSum(k, j)); 45 sum = computeSum(k, j); 46 } 47 } 48 } 49 } 50 } 51 52 private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>(); 53 public static double computeSum(int x, int y) { 54 List<Integer> key = Arrays.asList(new Integer[] {x, y}); 55 56 if (!cache.containsKey(key)) { 57 58 // explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error 59 LogSumArray toReturn = new LogSumArray(x); 60 61 // changing loop indices will prevent the error 62 // in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error 63 for (int z = 1; z < x+1; z++) { 64 double logSummand = Math.log(z + x + y); 65 toReturn.addLogSummand(logSummand); 66 } 67 68 // returning the value here without cacheing it will prevent the segfault 69 cache.put(key, toReturn.retrieveLogSum()); 70 } 71 return cache.get(key); 72 } 73 74 /* 75 * Given a bunch of logarithms log(X),log(Y),log(Z),... 76 * This class is used to compute the log of the sum, log(X+Y+Z+...) 77 */ 78 private static class LogSumArray { 79 private double[] logSummandArray; 80 private int currSize; 81 82 private double maxLogSummand; 83 84 public LogSumArray(int maxEntries) { 85 this.logSummandArray = new double[maxEntries]; 86 87 this.currSize = 0; 88 this.maxLogSummand = Double.NEGATIVE_INFINITY; 89 } 90 91 public void addLogSummand(double logSummand) { 92 logSummandArray[currSize] = logSummand; 93 currSize++; 94 // removing this line will prevent the error 95 maxLogSummand = Math.max(maxLogSummand, logSummand); 96 } 97 98 public double retrieveLogSum() { 99 if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY; 100 101 assert currSize <= logSummandArray.length; 102 103 double factorSum = 0; 104 for (int i = 0; i < currSize; i++) { 105 factorSum += Math.exp(logSummandArray[i] - maxLogSummand); 106 } 107 108 return Math.log(factorSum) + maxLogSummand; 109 } 110 } 111 }