-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch ImmutableCollections for tests (#109271)
ImmutableCollections uses a seed, set early during JVM startup, which affects the iteration order of collections. Although we do not want to rely on the iteration order of Map and Set collections, bugs do sometimes occur. In order to reproduce those bugs to fix them, it is important the test seed for Elasticsearch matches the seed used in ImmutableCollections. Unfortunately ImmutableCollections is internal to the JDK, and the seed used is private and final. This commit works around these limitations by creating a patched version of ImmutableCollections which allows access to the seed member. ESTestCase is then able to reflectively set the seed at runtime based on the Elasticsearch seed. Note that this only affects tests. ImmutableCollections remains is unchanged for production code. relates #94946
- Loading branch information
Showing
7 changed files
with
180 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import org.elasticsearch.gradle.OS | ||
import org.elasticsearch.gradle.VersionProperties | ||
import org.elasticsearch.gradle.internal.info.BuildParams | ||
|
||
apply plugin: 'elasticsearch.java' | ||
|
||
configurations { | ||
patch | ||
} | ||
|
||
dependencies { | ||
implementation 'org.ow2.asm:asm:9.7' | ||
implementation 'org.ow2.asm:asm-tree:9.7' | ||
} | ||
|
||
def outputDir = layout.buildDirectory.dir("jdk-patches") | ||
def generatePatch = tasks.register("generatePatch", JavaExec) | ||
generatePatch.configure { | ||
dependsOn tasks.named("compileJava") | ||
inputs.property("java-home-set", BuildParams.getIsRuntimeJavaHomeSet()) | ||
inputs.property("java-version", BuildParams.runtimeJavaVersion) | ||
outputs.dir(outputDir) | ||
|
||
classpath = sourceSets.main.runtimeClasspath | ||
mainClass = 'org.elasticsearch.jdk.patch.ImmutableCollectionsPatcher' | ||
if (BuildParams.getIsRuntimeJavaHomeSet()) { | ||
executable = "${BuildParams.runtimeJavaHome}/bin/java" + (OS.current() == OS.WINDOWS ? '.exe' : '') | ||
} else { | ||
javaLauncher = javaToolchains.launcherFor { | ||
languageVersion = JavaLanguageVersion.of(BuildParams.runtimeJavaVersion.majorVersion) | ||
vendor = VersionProperties.bundledJdkVendor == "openjdk" ? | ||
JvmVendorSpec.ORACLE : | ||
JvmVendorSpec.matching(VersionProperties.bundledJdkVendor) | ||
} | ||
} | ||
doFirst { | ||
args outputDir.get().getAsFile().toString() | ||
} | ||
} | ||
|
||
artifacts.add("patch", generatePatch); |
58 changes: 58 additions & 0 deletions
58
...lections-patch/src/main/java/org/elasticsearch/jdk/patch/ImmutableCollectionsPatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.jdk.patch; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.FieldVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Loads ImmutableCollections.class from the current jdk and writes it out | ||
* as a public class with SALT32L and REVERSE as public, non-final static fields. | ||
* | ||
* By exposing ImmutableCollections, tests run with this patched version can | ||
* hook in the existing test seed to ensure consistent iteration of immutable collections. | ||
* Note that the consistency is for <i>reproducing</i> dependencies on iteration | ||
* order, so that the code can be fixed. | ||
*/ | ||
public class ImmutableCollectionsPatcher { | ||
private static final String CLASSFILE = "java.base/java/util/ImmutableCollections.class"; | ||
|
||
public static void main(String[] args) throws Exception { | ||
Path outputDir = Paths.get(args[0]); | ||
byte[] originalClassFile = Files.readAllBytes(Paths.get(URI.create("jrt:/" + CLASSFILE))); | ||
|
||
ClassReader classReader = new ClassReader(originalClassFile); | ||
ClassWriter classWriter = new ClassWriter(classReader, 0); | ||
classReader.accept(new ClassVisitor(Opcodes.ASM9, classWriter) { | ||
@Override | ||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { | ||
super.visit(version, Opcodes.ACC_PUBLIC, name, signature, superName, interfaces); | ||
} | ||
|
||
@Override | ||
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { | ||
if (name.equals("SALT32L") || name.equals("REVERSE")) { | ||
access = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; | ||
} | ||
return super.visitField(access, name, descriptor, signature, value); | ||
} | ||
}, 0); | ||
Path outputFile = outputDir.resolve(CLASSFILE); | ||
Files.createDirectories(outputFile.getParent()); | ||
Files.write(outputFile, classWriter.toByteArray()); | ||
} | ||
} |