-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Enhance Java Agent to intercept Runtime::halt #17757
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.javaagent; | ||
|
|
||
| import org.opensearch.javaagent.bootstrap.AgentPolicy; | ||
|
|
||
| import java.lang.StackWalker.Option; | ||
| import java.security.Policy; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import net.bytebuddy.asm.Advice; | ||
|
|
||
| /** | ||
| * {@link Runtime#halt} interceptor | ||
| */ | ||
| public class RuntimeHaltInterceptor { | ||
| /** | ||
| * RuntimeHaltInterceptor | ||
| */ | ||
| public RuntimeHaltInterceptor() {} | ||
|
Check warning on line 26 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
|
|
||
| /** | ||
| * Interceptor | ||
| * @param code exit code | ||
| * @throws Exception exceptions | ||
| */ | ||
| @Advice.OnMethodEnter | ||
| @SuppressWarnings("removal") | ||
| public static void intercept(int code) throws Exception { | ||
| final Policy policy = AgentPolicy.getPolicy(); | ||
|
Check warning on line 36 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
| if (policy == null) { | ||
| return; /* noop */ | ||
|
Check warning on line 38 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
| } | ||
|
|
||
| final StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE); | ||
| final Class<?> caller = walker.getCallerClass(); | ||
| final Stream<Class<?>> chain = walker.walk(StackCallerClassChainExtractor.INSTANCE); | ||
|
Check warning on line 43 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
|
|
||
| if (AgentPolicy.isChainThatCanExit(caller, chain) == false) { | ||
| throw new SecurityException("The class " + caller + " is not allowed to call Runtime::halt(" + code + ")"); | ||
|
Check warning on line 46 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
| } | ||
| } | ||
|
Check warning on line 48 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/RuntimeHaltInterceptor.java
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.javaagent; | ||
|
|
||
| import java.lang.StackWalker.StackFrame; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Stream; | ||
|
|
||
| /** | ||
| * Stack Caller Class Chain Extractor | ||
| */ | ||
| public final class StackCallerClassChainExtractor implements Function<Stream<StackFrame>, Stream<Class<?>>> { | ||
| /** | ||
| * Single instance of stateless class. | ||
| */ | ||
| public static final StackCallerClassChainExtractor INSTANCE = new StackCallerClassChainExtractor(); | ||
|
Check warning on line 22 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/StackCallerClassChainExtractor.java
|
||
|
|
||
| /** | ||
| * Constructor | ||
| */ | ||
| private StackCallerClassChainExtractor() {} | ||
|
|
||
| /** | ||
| * Folds the stack | ||
| * @param frames stack frames | ||
| */ | ||
| @Override | ||
| public Stream<Class<?>> apply(Stream<StackFrame> frames) { | ||
| return cast(frames); | ||
|
Check warning on line 35 in libs/agent-sm/agent/src/main/java/org/opensearch/javaagent/StackCallerClassChainExtractor.java
|
||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static <A> Stream<A> cast(Stream<StackFrame> frames) { | ||
| return (Stream<A>) frames.map(StackFrame::getDeclaringClass).filter(c -> !c.isHidden()).distinct(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.