Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests config for serialization of arrays #279

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/for-serialization/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.10
WORKDIR /work/
RUN chown 100 /work \
&& chmod "g+rwX" /work \
&& chown 1000:root /work
39 changes: 39 additions & 0 deletions apps/for-serialization/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>for-serialization</groupId>
<artifactId>for-serialization</artifactId>
<version>1</version>

<name>for-serialization</name>

<parent>
<groupId>org.graalvm.tests.integration</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<properties>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
</properties>

<build>
<finalName>for-serialization</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<mainClass>for_serialization.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
165 changes: 165 additions & 0 deletions apps/for-serialization/src/main/java/for_serialization/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2024, Red Hat Inc. All rights reserved.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package for_serialization;

import sun.reflect.ReflectionFactory;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serial;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* @author Michal Karm Babacek <[email protected]>
* Inspired by https://github.com/oracle/graal/issues/8509
* Tests https://github.com/oracle/graal/issues/9581
*/
public class Main {

static class Muhehehe implements Serializable {
@Serial
private static final long serialVersionUID = 5197858094838069415L;
}

class MuheheheNested implements Serializable {
@Serial
private static final long serialVersionUID = 6197858094838069415L;

class MuheheheNestedNested implements Serializable {
@Serial
private static final long serialVersionUID = 7197858094838069415L;
}
}

public static void main(String[] args) throws Exception {
final Class<?>[] arrayTypes = new Class[] {
boolean[].class,
boolean[][].class,
byte[].class,
byte[][].class,
char[].class,
char[][].class,
double[].class,
double[][].class,
float[].class,
float[][].class,
int[].class,
int[][].class,
long[].class,
long[][].class,
short[].class,
short[][].class,
Boolean[].class,
Boolean[][].class,
Byte[].class,
Byte[][].class,
Character[].class,
Character[][].class,
Double[].class,
Double[][].class,
Float[].class,
Float[][].class,
Integer[].class,
Integer[][].class,
Long[].class,
Long[][].class,
Short[].class,
Short[][].class,
// https://github.com/oracle/graal/issues/9581
String[].class,
String[][].class,
Void[].class,
Void[][].class,
Class[].class,
Class[][].class,
Object[].class,
Object[][].class,
Number[].class,
Number[][].class,
Comparable[].class,
Comparable[][].class,
Serializable[].class,
Serializable[][].class,
Cloneable[].class,
Cloneable[][].class,
Readable[].class,
Readable[][].class,
AutoCloseable[].class,
AutoCloseable[][].class,
Closeable[].class,
Closeable[][].class,
Appendable[].class,
Appendable[][].class,
PrintStream[].class,
PrintStream[][].class,
PrintWriter[].class,
PrintWriter[][].class,
Record[].class,
Record[][].class,
Enum[].class,
Enum[][].class,
Enum[][][].class,
Enum[][][][].class,
Enum[][][][][].class,
Muhehehe[].class,
Muhehehe[][].class,
Muhehehe[][][].class,
MuheheheNested[].class,
MuheheheNested[][].class,
MuheheheNested[][][].class,
MuheheheNested.MuheheheNestedNested[].class,
MuheheheNested.MuheheheNestedNested[][].class,
MuheheheNested.MuheheheNestedNested[][][].class
};

final File f = Files.createTempFile(Path.of("."), "tmp", ".ser").toFile();
f.deleteOnExit();
for (int i = 0; i < arrayTypes.length; i++) {
final Constructor<?> cons = ReflectionFactory.getReflectionFactory()
.newConstructorForSerialization(arrayTypes[i],
java.lang.Object.class.getDeclaredConstructor((Class<?>[]) null));
final Object o = Array.newInstance(arrayTypes[i].getComponentType(), 2);
try (final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));
final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
oos.writeObject(o);
oos.flush();
final Object o2 = ois.readObject();
System.out.printf("%d %d %s %s %b\n",
i, cons.getParameterCount(), o.getClass().descriptorString(), o2.getClass().descriptorString(),
o2.getClass().equals(o.getClass()));
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("\nDone.");
}
}

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<module>apps/timezones</module>
<module>apps/versions</module>
<module>apps/monitor-field-offset</module>
<module>apps/for-serialization</module>
<module>testsuite</module>
</modules>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -497,7 +496,7 @@ public void imageioAWTContainerTest(TestInfo testInfo) throws IOException, Inter

@Test
@Tag("imageio")
@DisabledOnOs({OS.WINDOWS, OS.MAC}) // AWT support is not there yet
@DisabledOnOs({ OS.WINDOWS, OS.MAC }) // AWT support is not there yet
@IfMandrelVersion(min = "21.1")
public void imageioAWTTest(TestInfo testInfo) throws IOException, InterruptedException {
imageioAWT(testInfo, Apps.IMAGEIO);
Expand Down Expand Up @@ -818,6 +817,68 @@ public void monitorFieldOffset(TestInfo testInfo, Apps app) throws IOException,
}
}

@Test
@Tag("builder-image")
@IfMandrelVersion(min = "23.1.5", inContainer = true)
public void forSerializationContainerTest(TestInfo testInfo) throws IOException, InterruptedException {
forSerialization(testInfo, Apps.FOR_SERIALIZATION_BUILDER_IMAGE);
}

@Test
@IfMandrelVersion(minJDK = "23.1.5")
Karm marked this conversation as resolved.
Show resolved Hide resolved
public void forSerializationTest(TestInfo testInfo) throws IOException, InterruptedException {
forSerialization(testInfo, Apps.FOR_SERIALIZATION);
}

public void forSerialization(TestInfo testInfo, Apps app) throws IOException, InterruptedException {
LOGGER.info("Testing app: " + app);
File processLog = null;
final StringBuilder report = new StringBuilder();
final File appDir = Path.of(BASE_DIR, app.dir).toFile();
final File metaINF = Path.of(BASE_DIR, app.dir, "src", "main", "resources", "META-INF", "native-image").toFile();
final String cn = testInfo.getTestClass().get().getCanonicalName();
final String mn = testInfo.getTestMethod().get().getName();
final boolean inContainer = app == Apps.FOR_SERIALIZATION_BUILDER_IMAGE;
try {
// Cleanup
cleanTarget(app);
if (metaINF.exists()) {
FileUtils.cleanDirectory(metaINF);
}
if (inContainer) {
removeContainers(app.runtimeContainer.name);
}
Files.createDirectories(Paths.get(appDir.getAbsolutePath() + File.separator + "logs"));
processLog = Path.of(appDir.getAbsolutePath(), "logs", "build-and-run.log").toFile();
builderRoutine(inContainer ? 4 : 3, app, report, cn, mn, appDir, processLog);
LOGGER.info("Running...");

final List<String> cmdHotSpot = getRunCommand(app.buildAndRunCmds.cmds[app.buildAndRunCmds.cmds.length - 2]);
final List<String> cmdNative = getRunCommand(app.buildAndRunCmds.cmds[app.buildAndRunCmds.cmds.length - 1]);
final String hotSpotOutput = runCommand(cmdHotSpot, appDir);
final String nativeOutput = runCommand(cmdNative, appDir);
Logs.appendln(report, appDir.getAbsolutePath());
Logs.appendlnSection(report, String.join(" ", cmdHotSpot));
Logs.appendlnSection(report, hotSpotOutput);
Logs.appendlnSection(report, String.join(" ", cmdNative));
Logs.appendlnSection(report, nativeOutput);

assertEquals(hotSpotOutput, nativeOutput, "The output of the HotSpot and native-image runs must be the same.");

Logs.checkLog(cn, mn, app, processLog);

if (inContainer) {
removeContainers(app.runtimeContainer.name);
}

} finally {
cleanup(null, cn, mn, report, app, processLog);
if (metaINF.exists()) {
FileUtils.cleanDirectory(metaINF);
}
}
}

@Test
@Tag("calendars")
@IfMandrelVersion(min = "22.3.5") // The fix for this test is in 22.3.5 and better
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.graalvm.tests.integration.RuntimesSmokeTest.BASE_DIR;
import static org.graalvm.tests.integration.utils.thresholds.Thresholds.parseProperties;
Expand Down Expand Up @@ -186,7 +183,17 @@ public enum Apps {
URLContent.NONE,
WhitelistLogLines.MONITOR_OFFSET,
BuildAndRunCmds.MONITOR_OFFSET_BUILDER_IMAGE,
ContainerNames.MONITOR_OFFSET_BUILDER_IMAGE);
ContainerNames.MONITOR_OFFSET_BUILDER_IMAGE),
FOR_SERIALIZATION("apps" + File.separator + "for-serialization",
URLContent.NONE,
WhitelistLogLines.FOR_SERIALIZATION,
BuildAndRunCmds.FOR_SERIALIZATION,
ContainerNames.NONE),
FOR_SERIALIZATION_BUILDER_IMAGE("apps" + File.separator + "for-serialization",
URLContent.NONE,
WhitelistLogLines.FOR_SERIALIZATION,
BuildAndRunCmds.FOR_SERIALIZATION_BUILDER_IMAGE,
ContainerNames.FOR_SERIALIZATION_BUILDER_IMAGE);

public final String dir;
public final URLContent urlContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,41 @@ public enum BuildAndRunCmds {
"--name", ContainerNames.MONITOR_OFFSET_BUILDER_IMAGE.name,
BUILDER_IMAGE, "-R:-InstallSegfaultHandler", "-march=native", "--gc=serial", "--no-fallback",
"-jar", "target/monitor-field-offsets-nok.jar", "target/monitor-field-offsets-nok" },
}),
FOR_SERIALIZATION(new String[][] {
new String[] { "mvn", "package" },
new String[] { "java", "-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image",
"-jar", "target/for-serialization.jar" },
new String[] { "native-image", "-ea", "-march=native", "--no-fallback", "--link-at-build-time",
"-H:ConfigurationFileDirectories=src/main/resources/META-INF/native-image",
"-jar", "target/for-serialization.jar", "target/for-serialization" },
new String[] { "java", "-jar", "target/for-serialization.jar" },
new String[] { IS_THIS_WINDOWS ? "target\\for-serialization.exe" : "./target/for-serialization" }
}),
FOR_SERIALIZATION_BUILDER_IMAGE(new String[][] {
// Maven build
new String[] { "mvn", "package" },
// Collect agent info
new String[] { CONTAINER_RUNTIME, "run", IS_THIS_WINDOWS ? "" : "-u", IS_THIS_WINDOWS ? "" : getUnixUIDGID(),
"-t", "--entrypoint", "java", "-v", BASE_DIR + File.separator + "apps" + File.separator + "for-serialization:/project:z",
BUILDER_IMAGE, "-agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image",
"-jar", "target/for-serialization.jar" },
// Native image build
new String[] { CONTAINER_RUNTIME, "run", IS_THIS_WINDOWS ? "" : "-u", IS_THIS_WINDOWS ? "" : getUnixUIDGID(),
"-t", "-v", BASE_DIR + File.separator + "apps" + File.separator + "for-serialization:/project:z",
BUILDER_IMAGE, "-ea", "-march=native", "--no-fallback", "--link-at-build-time",
"-H:ConfigurationFileDirectories=src/main/resources/META-INF/native-image",
"-jar", "target/for-serialization.jar", "target/for-serialization" },
// Build runtime container for native executable
new String[] { CONTAINER_RUNTIME, "build", "--network=host", "-t", ContainerNames.FOR_SERIALIZATION_BUILDER_IMAGE.name, "." },
// Run Java, HotSpot version in container, uses our builder image
new String[] { CONTAINER_RUNTIME, "run", IS_THIS_WINDOWS ? "" : "-u", IS_THIS_WINDOWS ? "" : getUnixUIDGID(),
"-t", "--entrypoint", "java", "-v", BASE_DIR + File.separator + "apps" + File.separator + "for-serialization:/project:z",
BUILDER_IMAGE, "-jar", "target/for-serialization.jar" },
// Run native executable in container, uses a plain UBI image
new String[] { CONTAINER_RUNTIME, "run", IS_THIS_WINDOWS ? "" : "-u", IS_THIS_WINDOWS ? "" : getUnixUIDGID(),
"-t", "-v", BASE_DIR + File.separator + "apps" + File.separator + "for-serialization:/work:z",
ContainerNames.FOR_SERIALIZATION_BUILDER_IMAGE.name, "target/for-serialization" },
});

public final String[][] cmds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum ContainerNames {
JFR_PLAINTEXT_BUILDER_IMAGE("my-jfr-plaintext-runner"),
HYPERFOIL("hyperfoil-container"),
MONITOR_OFFSET_BUILDER_IMAGE("my-monitor-offset-runner"),
FOR_SERIALIZATION_BUILDER_IMAGE("my-for-serialization-runner"),
NONE("NO_CONTAINER");

public final String name;
Expand Down
Loading
Loading