Skip to content

Commit 32dd3fc

Browse files
authored
fix: disable spark async profiler on non-arm based systems (#1533)
### Motivation The async profiler version bundled with spark (which itself is bundled in modern paper versions) does not support java 23 and causes a seqfault when being executed which crashes at least all modern paper services running on amd64 systems: ``` # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x000079c43ba06ecf, pid=39327, tid=39528 # # JRE version: OpenJDK Runtime Environment (23.0+37) (build 23+37-2369) # Java VM: OpenJDK 64-Bit Server VM (23+37-2369, mixed mode, sharing, tiered, compressed class ptrs, g1 gc, linux-amd64) # Problematic frame: # C [spark-502cce8be50-libasyncProfiler.so.tmp+0x6ecf] NMethod::isNMethod()+0x1f ``` ### Modification Disable the async profiler integration in spark when an old version of the async profiler is used. The detection process of the async profiler version relies on a pull request to spark which is not yet merged (so we might need to change the detection process again when that happened). Additionally the issue does not happen on arm systems, therefore the async profiler is left enabled on these systems. The `load` method of `AsyncProfilerAccess` on matching spark versions is changed so that it always throws an exception that the async profiler is not available. The message of the exception is printed into the console when starting the profiler so that the user is informed why the profiler is disabled: ``` [23:22:44 INFO]: [spark] Starting background profiler... [23:22:44 WARN]: [spark] Unable to initialise the async-profiler engine: this version of spark uses a version of async-profiler which does not support java 23+ [23:22:44 WARN]: [spark] Please see here for more information: https://spark.lucko.me/docs/misc/Using-async-profiler ``` ### Result Non-arm servers that run modern paper versions and all servers running spark with an old version of async-profiler will no longer segfault when starting up/the plugin is enabled.
1 parent 8652ceb commit 32dd3fc

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2019-2024 CloudNetService team & contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package eu.cloudnetservice.wrapper.transform.spark;
18+
19+
import eu.cloudnetservice.common.util.StringUtil;
20+
import eu.cloudnetservice.wrapper.transform.ClassTransformer;
21+
import java.lang.classfile.ClassBuilder;
22+
import java.lang.classfile.ClassElement;
23+
import java.lang.classfile.ClassTransform;
24+
import java.lang.classfile.MethodModel;
25+
import java.lang.constant.ClassDesc;
26+
import java.lang.constant.ConstantDescs;
27+
import java.lang.constant.MethodTypeDesc;
28+
import java.lang.reflect.AccessFlag;
29+
import lombok.NonNull;
30+
import org.jetbrains.annotations.ApiStatus;
31+
32+
/**
33+
* A transformer that explicitly disables the spark async profiler integration on old spark versions.
34+
*
35+
* @since 4.0
36+
*/
37+
@ApiStatus.Internal
38+
public final class OldAsyncProfilerDisableTransformer implements ClassTransformer {
39+
40+
private static final String MN_LOAD = "load";
41+
private static final String MN_IS_LINUX_MUSL = "isLinuxMusl";
42+
private static final String CNI_ASYNC_PROFILER_ACC_PREFIX = "me/lucko/spark/";
43+
private static final String CNI_ASYNC_PROFILER_ACC_SUFFIX = "/common/sampler/async/AsyncProfilerAccess";
44+
private static final ClassDesc CD_UNSUPPORTED_OP_EX = ClassDesc.of(UnsupportedOperationException.class.getName());
45+
private static final MethodTypeDesc MTD_UNSUPPORTED_OP_EX_NEW =
46+
MethodTypeDesc.of(ConstantDescs.CD_void, ConstantDescs.CD_String);
47+
48+
/**
49+
* Constructs a new instance of this transformer, usually done via SPI.
50+
*/
51+
public OldAsyncProfilerDisableTransformer() {
52+
// used by SPI
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
@Override
59+
public @NonNull ClassTransform provideClassTransform() {
60+
return new AsyncProfilerAccessClassTransform();
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
@Override
67+
public @NonNull TransformWillingness classTransformWillingness(@NonNull String internalClassName) {
68+
var isAsyncProfilerAccessClass = internalClassName.startsWith(CNI_ASYNC_PROFILER_ACC_PREFIX)
69+
&& internalClassName.endsWith(CNI_ASYNC_PROFILER_ACC_SUFFIX);
70+
return isAsyncProfilerAccessClass ? TransformWillingness.ACCEPT_ONCE : TransformWillingness.REJECT;
71+
}
72+
73+
/**
74+
* A transformer which replaces the {@code load} method to always throw an exception on {@code AsyncProfilerAccess} in
75+
* case the async profiler is not supported.
76+
*
77+
* @since 4.0
78+
*/
79+
private static final class AsyncProfilerAccessClassTransform implements ClassTransform {
80+
81+
// holds if the "isLinuxMusl" method exists in AsyncProfilerAccess - the method was removed
82+
// alongside the async profiler 3 support which added the required java 23 support
83+
private boolean isLinuxMuslExists = false;
84+
// the bug only happens on amd64 systems, so on aarch system we can leave the profiler
85+
// enabled even when running on the old version of it
86+
private boolean isLinuxAarch64 = false;
87+
88+
/**
89+
* {@inheritDoc}
90+
*/
91+
@Override
92+
public void atStart(@NonNull ClassBuilder builder) {
93+
var classModel = builder.original().orElseThrow(() -> new IllegalStateException("original not preset on remap"));
94+
this.isLinuxMuslExists = classModel.methods().stream().anyMatch(methodModel -> {
95+
var isStatic = methodModel.flags().has(AccessFlag.STATIC);
96+
return isStatic && methodModel.methodName().equalsString(MN_IS_LINUX_MUSL);
97+
});
98+
99+
var arch = StringUtil.toLower(System.getProperty("os.arch"));
100+
var osName = StringUtil.toLower(System.getProperty("os.name"));
101+
this.isLinuxAarch64 = osName.equals("linux") && arch.equals("aarch64");
102+
}
103+
104+
/**
105+
* {@inheritDoc}
106+
*/
107+
@Override
108+
public void accept(@NonNull ClassBuilder builder, @NonNull ClassElement element) {
109+
if (element instanceof MethodModel mm
110+
&& !this.isLinuxAarch64
111+
&& this.isLinuxMuslExists
112+
&& mm.flags().has(AccessFlag.STATIC)
113+
&& mm.methodName().equalsString(MN_LOAD)) {
114+
builder.withMethodBody(mm.methodName(), mm.methodType(), mm.flags().flagsMask(), code -> code
115+
.new_(CD_UNSUPPORTED_OP_EX)
116+
.dup()
117+
.ldc("this version of spark uses a version of async-profiler which does not support java 23+")
118+
.invokespecial(CD_UNSUPPORTED_OP_EX, ConstantDescs.INIT_NAME, MTD_UNSUPPORTED_OP_EX_NEW)
119+
.athrow());
120+
} else {
121+
builder.with(element);
122+
}
123+
}
124+
}
125+
}

wrapper-jvm/src/main/resources/META-INF/services/eu.cloudnetservice.wrapper.transform.ClassTransformer

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ eu.cloudnetservice.wrapper.transform.bukkit.WorldEditJava8DetectorTransformer
2323
eu.cloudnetservice.wrapper.transform.bukkit.FAWEWorldEditDownloadURLTransformer
2424
eu.cloudnetservice.wrapper.transform.minestom.MinestomStopCleanlyTransformer
2525
eu.cloudnetservice.wrapper.transform.netty.OldEpollDisableTransformer
26+
eu.cloudnetservice.wrapper.transform.spark.OldAsyncProfilerDisableTransformer

0 commit comments

Comments
 (0)