-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Override IndyBootstrapDispatcher class module (#1468)
- Loading branch information
Showing
11 changed files
with
193 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
53 changes: 53 additions & 0 deletions
53
apm-agent-core/src/test/java/co/elastic/apm/agent/bci/IndyBootstrapTest.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,53 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2020 Elastic and contributors | ||
* %% | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.bci; | ||
|
||
import co.elastic.apm.agent.AbstractInstrumentationTest; | ||
import net.bytebuddy.dynamic.loading.ClassInjector; | ||
import org.junit.jupiter.api.Test; | ||
import org.stagemonitor.util.IOUtils; | ||
|
||
import java.io.InputStream; | ||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class IndyBootstrapTest extends AbstractInstrumentationTest { | ||
|
||
@Test | ||
void testSetJavaBaseModule() throws Throwable { | ||
Module javaBaseModule = Class.class.getModule(); | ||
assertThat(IndyBootstrapTest.class.getModule()).isNotEqualTo(javaBaseModule); | ||
|
||
// In order to test this functionality, IndyBootstrapDispatcherModuleSetter needs to be loaded from the Boot CL. | ||
// We don't mind loading it with the test's class loader as well only to get it's class file | ||
InputStream classFileAsStream = IndyBootstrapDispatcherModuleSetter.class.getResourceAsStream("IndyBootstrapDispatcherModuleSetter.class"); | ||
byte[] bootstrapClass = IOUtils.readToBytes(classFileAsStream); | ||
ClassInjector.UsingUnsafe.ofBootLoader().injectRaw(Collections.singletonMap(IndyBootstrapDispatcherModuleSetter.class.getName(), bootstrapClass)); | ||
|
||
IndyBootstrap.setJavaBaseModule(IndyBootstrapTest.class); | ||
assertThat(IndyBootstrapTest.class.getModule()).isEqualTo(javaBaseModule); | ||
} | ||
} |
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,26 @@ | ||
<?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> | ||
|
||
<parent> | ||
<artifactId>apm-agent-parent</artifactId> | ||
<groupId>co.elastic.apm</groupId> | ||
<version>1.18.2-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>apm-indy-bootstrap-module</artifactId> | ||
<name>${project.groupId}:${project.artifactId}</name> | ||
|
||
<properties> | ||
<apm-agent-parent.base.dir>${project.basedir}/..</apm-agent-parent.base.dir> | ||
|
||
<maven.compiler.target>9</maven.compiler.target> | ||
|
||
<!-- using release to enforce java 9 API (animal sniffer not supported) --> | ||
<maven.compiler.release>${maven.compiler.target}</maven.compiler.release> | ||
|
||
<!-- not supported/relevant beyond java9, see https://github.com/mojohaus/animal-sniffer/issues/62 | ||
now enforced through the maven.compiler.release property --> | ||
<animal.sniffer.skip>true</animal.sniffer.skip> | ||
</properties> | ||
</project> |
50 changes: 50 additions & 0 deletions
50
...ap-module/src/main/java/co/elastic/apm/agent/bci/IndyBootstrapDispatcherModuleSetter.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,50 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2020 Elastic and contributors | ||
* %% | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.bci; | ||
|
||
import sun.misc.Unsafe; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Overrides the module assigned to the class in with the {@code java.base} module. | ||
* <p> | ||
* NOTE: This class is compiled with Java 9 so it must only be loaded though reflection and only when running on Java 9. | ||
* In addition, since it relies on the {@link Unsafe} API, it must be loaded by the bootstrap or platform class loaders. | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class IndyBootstrapDispatcherModuleSetter { | ||
|
||
public static void setJavaBaseModule(Class<?> indyBootstrapDispatcherClass) throws Exception { | ||
Field moduleField = Class.class.getDeclaredField("module"); | ||
if (moduleField.getType() == Module.class) { | ||
Module javaBaseModule = Class.class.getModule(); | ||
Unsafe unsafe = Unsafe.getUnsafe(); | ||
unsafe.putObject(indyBootstrapDispatcherClass, unsafe.objectFieldOffset(moduleField), javaBaseModule); | ||
} else { | ||
throw new IllegalStateException("Unexpected module field type: " + moduleField.getType().getName()); | ||
} | ||
} | ||
} |
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