Skip to content

Commit

Permalink
8344533: CTW: Add option to remove clinits before loading
Browse files Browse the repository at this point in the history
Reviewed-by: thartmann, lmesnik
  • Loading branch information
Evgeny Nikitin authored and lmesnik committed Nov 23, 2024
1 parent 70c4e2c commit effee12
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -92,7 +95,22 @@ private static class PathEntryClassLoader extends java.lang.ClassLoader {
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
Expand Down

1 comment on commit effee12

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.