8344533: CTW: Add option to remove clinits before loading

Reviewed-by: thartmann, lmesnik
This commit is contained in:
Evgeny Nikitin 2024-11-23 03:55:58 +00:00 committed by Leonid Mesnik
parent 70c4e2c0cc
commit effee122dd

View File

@ -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<String, byte[]> findByteCode;
private PathEntryClassLoader(Function<String, byte[]> 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("<clinit>")));
}
@Override