|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 021MALLOC_SIZE-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 | + * @summary Check the allocation-site stack trace of a corrupted memory at free() time |
| 27 | + * @modules java.base/jdk.internal.misc |
| 28 | + * @library /test/lib |
| 29 | + * @build jdk.test.whitebox.WhiteBox |
| 30 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 31 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:NativeMemoryTracking=detail NMTPrintMallocSiteOfCorruptedMemory |
| 32 | + */ |
| 33 | + |
| 34 | +import jdk.test.lib.Utils; |
| 35 | +import jdk.test.lib.process.ProcessTools; |
| 36 | +import jdk.test.lib.process.OutputAnalyzer; |
| 37 | +import jdk.test.whitebox.WhiteBox; |
| 38 | + |
| 39 | +public class NMTPrintMallocSiteOfCorruptedMemory { |
| 40 | + private static final String HEADER_ARG = "header"; |
| 41 | + private static final String FOOTER_ARG = "footer"; |
| 42 | + private static final String HEADER_AND_SITE_ARG = "header-and-site"; |
| 43 | + private static final String FOOTER_AND_SITE_ARG = "footer-and-site"; |
| 44 | + private static final int MALLOC_SIZE = 10; |
| 45 | + private static WhiteBox wb = WhiteBox.getWhiteBox(); |
| 46 | + |
| 47 | + static { |
| 48 | + System.loadLibrary("MallocHeaderModifier"); |
| 49 | + } |
| 50 | + |
| 51 | + public static native byte modifyHeaderCanary(long malloc_memory); |
| 52 | + public static native byte modifyFooterCanary(long malloc_memory, long size); |
| 53 | + public static native byte modifyHeaderCanaryAndSiteMarker(long malloc_memory); |
| 54 | + public static native byte modifyFooterCanaryAndSiteMarker(long malloc_memory, long size); |
| 55 | + |
| 56 | + private static void runThisTestWith(String arg) throws Exception { |
| 57 | + ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(new String[] {"-Xbootclasspath/a:.", |
| 58 | + "-XX:+UnlockDiagnosticVMOptions", |
| 59 | + "-XX:+WhiteBoxAPI", |
| 60 | + "-XX:NativeMemoryTracking=detail", |
| 61 | + "-Djava.library.path=" + Utils.TEST_NATIVE_PATH, |
| 62 | + "NMTPrintMallocSiteOfCorruptedMemory", |
| 63 | + arg}); |
| 64 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 65 | + output.shouldMatch("NMT Block at .*, corruption at: "); |
| 66 | + switch(arg) { |
| 67 | + case HEADER_AND_SITE_ARG, FOOTER_AND_SITE_ARG -> output.shouldContain("allocation-site cannot be shown since the marker is also corrupted."); |
| 68 | + case HEADER_ARG, FOOTER_ARG -> { |
| 69 | + output.shouldContain("allocated from:"); |
| 70 | + output.shouldMatch("\\[.*\\]WB_NMTMalloc\\+0x.*"); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static void testModifyHeaderCanary() { |
| 76 | + long addr = wb.NMTMalloc(MALLOC_SIZE); |
| 77 | + modifyHeaderCanary(addr); |
| 78 | + wb.NMTFree(addr); |
| 79 | + } |
| 80 | + |
| 81 | + private static void testModifyFooterCanary() { |
| 82 | + long addr = wb.NMTMalloc(MALLOC_SIZE); |
| 83 | + modifyFooterCanary(addr, MALLOC_SIZE); |
| 84 | + wb.NMTFree(addr); |
| 85 | + } |
| 86 | + |
| 87 | + private static void testModifyHeaderCanaryAndSiteMarker() { |
| 88 | + long addr = wb.NMTMalloc(MALLOC_SIZE); |
| 89 | + modifyHeaderCanaryAndSiteMarker(addr); |
| 90 | + wb.NMTFree(addr); |
| 91 | + } |
| 92 | + |
| 93 | + private static void testModifyFooterCanaryAndSiteMarker() { |
| 94 | + long addr = wb.NMTMalloc(MALLOC_SIZE); |
| 95 | + modifyFooterCanaryAndSiteMarker(addr, MALLOC_SIZE); |
| 96 | + wb.NMTFree(addr); |
| 97 | + } |
| 98 | + |
| 99 | + public static void main(String args[]) throws Exception { |
| 100 | + if (args != null && args.length == 1) { |
| 101 | + switch (args[0]) { |
| 102 | + case HEADER_ARG -> testModifyHeaderCanary(); |
| 103 | + case FOOTER_ARG -> testModifyFooterCanary(); |
| 104 | + case HEADER_AND_SITE_ARG -> testModifyHeaderCanaryAndSiteMarker(); |
| 105 | + case FOOTER_AND_SITE_ARG -> testModifyFooterCanaryAndSiteMarker(); |
| 106 | + default -> throw new RuntimeException("Invalid argument for NMTPrintMallocSiteOfCorruptedMemory (" + args[0] + ")"); |
| 107 | + } |
| 108 | + } else { |
| 109 | + runThisTestWith(HEADER_ARG); |
| 110 | + runThisTestWith(FOOTER_ARG); |
| 111 | + runThisTestWith(HEADER_AND_SITE_ARG); |
| 112 | + runThisTestWith(FOOTER_AND_SITE_ARG); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments