Compare commits

...

2 Commits

Author SHA1 Message Date
Jaikiran Pai
946c6cc6c7 8334167: Test java/lang/instrument/NativeMethodPrefixApp.java timed out
Reviewed-by: dholmes, sspitsyn
Backport-of: 3babffd400
2024-07-31 10:41:24 +00:00
Alexey Ivanov
6a6591e88c 8335967: "text-decoration: none" does not work with "A" HTML tags
Reviewed-by: prr
Backport-of: 374fca0fcb
2024-07-29 18:39:43 +00:00
5 changed files with 236 additions and 62 deletions

View File

@@ -844,6 +844,10 @@ public class CSS implements Serializable {
}
static Object mergeTextDecoration(String value) {
if (value.startsWith("none")) {
return null;
}
boolean underline = value.contains("underline");
boolean strikeThrough = value.contains("line-through");
if (!underline && !strikeThrough) {

View File

@@ -35,70 +35,69 @@ class NativeMethodPrefixAgent {
static ClassFileTransformer t0, t1, t2;
static Instrumentation inst;
private static Throwable agentError;
private static Throwable agentError; // to be accessed/updated in a synchronized block
public static void checkErrors() {
private static final String CLASS_TO_TRANSFORM = "NativeMethodPrefixApp$Dummy";
public static synchronized void checkErrors() {
if (agentError != null) {
throw new RuntimeException("Agent error", agentError);
}
}
private static synchronized void trackError(final Throwable t) {
if (agentError == null) {
agentError = t;
return;
}
if (agentError != t) {
agentError.addSuppressed(t);
}
}
static class Tr implements ClassFileTransformer {
private static final ClassDesc CD_StringIdCallbackReporter = ClassDesc.ofInternalName("bootreporter/StringIdCallbackReporter");
private static final MethodTypeDesc MTD_void_String_int = MethodTypeDesc.of(CD_void, CD_String, CD_int);
final String trname;
final int transformId;
private final String nativeMethodPrefix;
Tr(int transformId) {
this.trname = "tr" + transformId;
this.transformId = transformId;
this.nativeMethodPrefix = "wrapped_" + trname + "_";
}
public byte[]
transform(
ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer) {
boolean redef = classBeingRedefined != null;
System.out.println(trname + ": " +
(redef? "Retransforming " : "Loading ") + className);
if (className != null) {
try {
byte[] newcf = Instrumentor.instrFor(classfileBuffer)
.addNativeMethodTrackingInjection(
"wrapped_" + trname + "_", (name, h) -> {
h.loadConstant(name);
h.loadConstant(transformId);
h.invokestatic(
CD_StringIdCallbackReporter,
"tracker",
MTD_void_String_int);
})
.apply();
/*** debugging ...
if (newcf != null) {
String fname = trname + (redef?"_redef" : "") + "/" + className;
System.err.println("dumping to: " + fname);
write_buffer(fname + "_before.class", classfileBuffer);
write_buffer(fname + "_instr.class", newcf);
}
***/
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return redef? null : newcf;
} catch (Throwable ex) {
if (agentError == null) {
agentError = ex;
}
System.err.println("ERROR: Injection failure: " + ex);
ex.printStackTrace();
try {
// we only transform a specific application class
if (!className.equals(CLASS_TO_TRANSFORM)) {
return null;
}
if (classBeingRedefined != null) {
return null;
}
// use a byte code generator which creates wrapper methods,
// with a configured native method prefix, for each native method on the
// class being transformed
final Instrumentor byteCodeGenerator = Instrumentor.instrFor(classfileBuffer)
.addNativeMethodTrackingInjection(nativeMethodPrefix,
(name, cb) -> {
cb.loadConstant(name);
cb.loadConstant(transformId);
cb.invokestatic(CD_StringIdCallbackReporter,
"tracker", MTD_void_String_int);
});
// generate the bytecode
return byteCodeGenerator.apply();
} catch (Throwable t) {
trackError(t);
return null;
}
return null;
}
}
// for debugging

View File

@@ -22,9 +22,7 @@
*/
import java.io.File;
import java.nio.file.Path;
import java.lang.management.*;
import bootreporter.*;
import jdk.test.lib.helpers.ClassFileInstaller;
@@ -33,27 +31,24 @@ import jdk.test.lib.process.ProcessTools;
/*
* @test
* @bug 6263319
* @bug 6263319 8334167
* @summary test setNativeMethodPrefix
* @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
* @modules java.management
* java.instrument
* @modules java.instrument
* @library /test/lib
* @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
* asmlib.Instrumentor NativeMethodPrefixAgent
* @enablePreview
* @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature.
* @run driver/timeout=240 NativeMethodPrefixApp roleDriver
* @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548
* @run main/native NativeMethodPrefixApp roleDriver
*/
public class NativeMethodPrefixApp implements StringIdCallback {
// This test is fragile like a golden file test.
// It assumes that a specific non-native library method will call a specific
// native method. The below may need to be updated based on library changes.
static String goldenNativeMethodName = "getStartupTime";
// we expect this native method, which is part of this test's application,
// to be instrumented and invoked
static String goldenNativeMethodName = "fooBarNativeMethod";
static boolean[] gotIt = {false, false, false};
private static final String testLibraryPath = System.getProperty("test.nativepath");
public static void main(String[] args) throws Exception {
if (args.length == 1) {
@@ -68,21 +63,19 @@ public class NativeMethodPrefixApp implements StringIdCallback {
launchApp(agentJar);
} else {
System.err.println("running app");
System.loadLibrary("NativeMethodPrefix"); // load the native library
new NativeMethodPrefixApp().run();
}
}
private static Path createAgentJar() throws Exception {
final String testClassesDir = System.getProperty("test.classes");
final Path agentJar = Path.of("NativeMethodPrefixAgent.jar");
final String manifest = """
Manifest-Version: 1.0
Premain-Class: NativeMethodPrefixAgent
Can-Retransform-Classes: true
Can-Set-Native-Method-Prefix: true
"""
+ "Boot-Class-Path: " + testClassesDir.replace(File.separatorChar, '/') + "/"
+ "\n";
""";
System.out.println("Manifest is:\n" + manifest);
// create the agent jar
ClassFileInstaller.writeJar(agentJar.getFileName().toString(),
@@ -96,6 +89,7 @@ public class NativeMethodPrefixApp implements StringIdCallback {
final OutputAnalyzer oa = ProcessTools.executeTestJava(
"--enable-preview", // due to usage of ClassFile API PreviewFeature in the agent
"-javaagent:" + agentJar.toString(),
"-Djava.library.path=" + testLibraryPath,
NativeMethodPrefixApp.class.getName());
oa.shouldHaveExitValue(0);
// make available stdout/stderr in the logs, even in case of successful completion
@@ -105,10 +99,11 @@ public class NativeMethodPrefixApp implements StringIdCallback {
private void run() throws Exception {
StringIdCallbackReporter.registerCallback(this);
System.err.println("start");
java.lang.reflect.Array.getLength(new short[5]);
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
System.err.println(mxbean.getVmVendor());
final long val = new Dummy().callSomeNativeMethod();
if (val != 42) {
throw new RuntimeException("unexpected return value " + val
+ " from native method, expected 42");
}
NativeMethodPrefixAgent.checkErrors();
@@ -128,4 +123,13 @@ public class NativeMethodPrefixApp implements StringIdCallback {
System.err.println("Tracked #" + id + ": " + name);
}
}
private static class Dummy {
private long callSomeNativeMethod() {
return fooBarNativeMethod();
}
private native long fooBarNativeMethod();
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include <stdio.h>
#include "jni.h"
JNIEXPORT jlong JNICALL
Java_NativeMethodPrefixApp_00024Dummy_fooBarNativeMethod(JNIEnv *env, jclass clazz)
{
fprintf(stderr, "native method called\n");
return 42;
}
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *vm, void *reserved)
{
fprintf(stderr, "native library loaded\n");
return JNI_VERSION_1_1; // this native library needs the very basic JNI support
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.text.View;
import javax.swing.text.html.CSS;
/*
* @test
* @bug 8335967
* @summary Tests 'text-decoration: none' is respected
* @run main HTMLTextDecorationNone
*/
public final class HTMLTextDecorationNone {
private static final String HTML = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>text-decoration: none (&lt;a&gt;)</title>
<style>
a.none { text-decoration: none }
</style>
</head>
<body>
<p><a href="https://openjdk.org/">underlined</a></p>
<p><a href="https://openjdk.org/" style="text-decoration: none">not underlined</a></p>
<p><a href="https://openjdk.org/" class="none">not underlined</a></p>
<p style="text-decoration: underline"><a
href="https://openjdk.org/" style="text-decoration: none">underlined?</a></p>
<p style="text-decoration: underline"><a
href="https://openjdk.org/" class="none">underlined?</a></p>
</body>
</html>
""";
private static final boolean[] underlined = {true, false, false, true, true};
public static void main(String[] args) {
final JEditorPane html = new JEditorPane("text/html", HTML);
html.setEditable(false);
final Dimension size = html.getPreferredSize();
html.setSize(size);
BufferedImage image = new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
// Paint the editor pane to ensure all views are created
html.paint(g);
g.dispose();
int errorCount = 0;
String firstError = null;
System.out.println("----- Views -----");
final View bodyView = html.getUI()
.getRootView(html)
.getView(1)
.getView(1);
for (int i = 0; i < bodyView.getViewCount(); i++) {
View pView = bodyView.getView(i);
View contentView = getContentView(pView);
Object decorationAttr =
contentView.getAttributes()
.getAttribute(CSS.Attribute.TEXT_DECORATION);
String decoration = decorationAttr == null
? "none" : decorationAttr.toString();
System.out.println(i + ": " + decoration);
if (decoration.contains("underline") != underlined[i]) {
errorCount++;
if (firstError == null) {
firstError = "Line " + i + ": " + decoration + " vs "
+ (underlined[i] ? "underline" : "none");
}
}
}
if (errorCount > 0) {
saveImage(image);
throw new RuntimeException(errorCount + " error(s) found, "
+ "the first one: " + firstError);
}
}
private static View getContentView(View parent) {
View view = parent.getView(0);
return view.getViewCount() > 0
? getContentView(view)
: view;
}
private static void saveImage(BufferedImage image) {
try {
ImageIO.write(image, "png",
new File("html.png"));
} catch (IOException ignored) { }
}
}