support ognl strict. #2955

pull/2957/head
hengyunabc 3 months ago
parent 9f6cc8e4fc
commit 375220cb92

@ -1,6 +1,11 @@
package com.taobao.arthas.core;
import java.lang.reflect.Field;
import com.taobao.arthas.common.JavaVersionUtils;
import com.taobao.arthas.common.UnsafeUtils;
import ognl.OgnlRuntime;
/**
*
@ -128,7 +133,8 @@ public class GlobalOptions {
public static volatile boolean verbose = false;
/**
* strict
* strict ognl
* @see ognl.OgnlRuntime#getUseStricterInvocationValue()
*/
@Option(level = 1,
name = "strict",
@ -136,4 +142,20 @@ public class GlobalOptions {
description = STRICT_MESSAGE
)
public static volatile boolean strict = true;
public static void updateOnglStrict(boolean strict) {
try {
Field field = OgnlRuntime.class.getDeclaredField("_useStricterInvocation");
field.setAccessible(true);
// 获取字段的内存偏移量和基址
Object staticFieldBase = UnsafeUtils.UNSAFE.staticFieldBase(field);
long staticFieldOffset = UnsafeUtils.UNSAFE.staticFieldOffset(field);
// 修改字段的值
UnsafeUtils.UNSAFE.putBoolean(staticFieldBase, staticFieldOffset, strict);
} catch (NoSuchFieldException | SecurityException e) {
// ignore
}
}
}

@ -155,6 +155,11 @@ public class OptionsCommand extends AnnotatedCommand {
return ExitStatus.failure(-1, format("Options[%s] type[%s] was unsupported.", optionName, type.getSimpleName()));
}
// FIXME hack for ongl strict
if (field.getName().equals("strict")) {
GlobalOptions.updateOnglStrict(Boolean.valueOf(optionValue));
logger.info("update ongl strict to: {}", optionValue);
}
} catch (Throwable t) {
return ExitStatus.failure(-1, format("Cannot cast option value[%s] to type[%s].", optionValue, type.getSimpleName()));
}

@ -0,0 +1,18 @@
package com.taobao.arthas.core;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import ognl.OgnlRuntime;
class GlobalOptionsTest {
@Test
void test() {
GlobalOptions.updateOnglStrict(true);
Assertions.assertThat(OgnlRuntime.getUseStricterInvocationValue()).isTrue();
GlobalOptions.updateOnglStrict(false);
Assertions.assertThat(OgnlRuntime.getUseStricterInvocationValue()).isFalse();
}
}
Loading…
Cancel
Save