diff --git a/test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/PathHandler.java b/test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/PathHandler.java index 5ab94b0a356..5e36cf0f7f9 100644 --- a/test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/PathHandler.java +++ b/test/hotspot/jtreg/testlibrary/ctw/src/sun/hotspot/tools/ctw/PathHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -24,6 +24,9 @@ package sun.hotspot.tools.ctw; import java.io.Closeable; +import java.lang.classfile.ClassFile; +import java.lang.classfile.ClassTransform; +import java.lang.classfile.MethodModel; import java.net.URI; import java.nio.file.FileSystems; import java.nio.file.Files; @@ -92,7 +95,22 @@ public class PathHandler implements Closeable { private final Function findByteCode; private PathEntryClassLoader(Function findByteCode) { - this.findByteCode = findByteCode; + boolean allowClinits = "true".equals( + System.getProperty("sun.hotspot.tools.ctwrunner.allow_clinits", "true")); + + this.findByteCode = allowClinits ? findByteCode + : findByteCode.andThen(PathEntryClassLoader::sterilizeClinits); + } + + /** + * Removes 'clinit' methods to prevent code execution + */ + private static byte[] sterilizeClinits(byte[] src) { + ClassFile classFile = ClassFile.of(); + return classFile.transformClass(classFile.parse(src), + ClassTransform.dropping( + element -> element instanceof MethodModel mm + && mm.methodName().stringValue().equals(""))); } @Override