1 /* 2 * Copyright (c) 2018, 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 import java.awt.Graphics; 25 import java.awt.image.BufferedImage; 26 import java.awt.image.DataBuffer; 27 import java.awt.image.DataBufferByte; 28 import java.awt.image.IndexColorModel; 29 import java.awt.image.MultiPixelPackedSampleModel; 30 import java.awt.image.Raster; 31 import java.awt.image.SampleModel; 32 import java.awt.image.WritableRaster; 33 34 /* 35 * @test 36 * @bug 8201433 37 * @summary This test may throw OOME or NPE from native code, 38 * it should not crash 39 * @requires os.maxMemory >= 2G 40 * @run main/othervm/timeout=300000 -Xms1000m -Xmx1000m ICMColorDataTest 41 */ 42 public class ICMColorDataTest { 43 private static final int WIDTH = 90; 44 private static final int HEIGHT = 90; 45 private static final int BITS_PER_PIXEL = 1; 46 private static final int PIXELS_IN_BYTE = 8; 47 48 // Color model components 49 private static final byte[] RED = { (byte) 255, 0 }; 50 private static final byte[] GREEN = { (byte) 255, 0 }; 51 private static final byte[] BLUE = { (byte) 255, 0 }; 52 53 public static void main(String[] args) { 54 try { 55 for (long i = 0; i < 300_000; i++) { 56 makeImage(); 57 } 58 } catch (OutOfMemoryError | NullPointerException e) { 59 System.err.println("Caught expected exception:\n" + 60 e.getClass() + ": " + e.getMessage()); 61 } 62 System.err.println("Test passed"); 63 } 64 65 private static void makeImage() { 66 int scanLineBytes = WIDTH / PIXELS_IN_BYTE; 67 if ((WIDTH & (PIXELS_IN_BYTE - 1)) != 0) { 68 // Make sure all the pixels in a scan line fit 69 scanLineBytes += 1; 70 } 71 72 byte[] bits = new byte[scanLineBytes * HEIGHT]; 73 DataBuffer dataBuf = new DataBufferByte(bits, bits.length, 0); 74 SampleModel sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 75 WIDTH, HEIGHT, BITS_PER_PIXEL); 76 WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuf, null); 77 IndexColorModel indexModel = new IndexColorModel(2, 2, RED, GREEN, BLUE); 78 BufferedImage bufImage = new BufferedImage(indexModel, raster, 79 indexModel.isAlphaPremultiplied(), null); 80 81 Graphics g = bufImage.getGraphics(); 82 g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1); 83 g.dispose(); 84 } 85 }