|
| 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 | +} |
0 commit comments