Skip to content

Commit

Permalink
support ognl strict. #2955
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Nov 21, 2024
1 parent 9f6cc8e commit 375220c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
24 changes: 23 additions & 1 deletion core/src/main/java/com/taobao/arthas/core/GlobalOptions.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* 全局开关
Expand Down Expand Up @@ -128,12 +133,29 @@ public class GlobalOptions {
public static volatile boolean verbose = false;

/**
* 是否打开strict 开关
* 是否打开strict 开关。更新时注意 ognl 里的配置需要同步修改
* @see ognl.OgnlRuntime#getUseStricterInvocationValue()
*/
@Option(level = 1,
name = "strict",
summary = "Option to strict mode",
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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ private ExitStatus processChangeNameValue(CommandProcess process) throws Illegal
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()));
}
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/com/taobao/arthas/core/GlobalOptionsTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 375220c

Please sign in to comment.