From 2d2d0642fcfe57314f71d8a81fdeeb3b2f25059e Mon Sep 17 00:00:00 2001 From: liqixin Date: Thu, 16 May 2024 15:14:29 +0800 Subject: [PATCH] fix: jvm fault injection problem,className not match and javassist not found exception. --- .../tools/jvm/ChaosMetaClassFileTransformer.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/chaosmetad/tools/jvm/ChaosMetaClassFileTransformer.java b/chaosmetad/tools/jvm/ChaosMetaClassFileTransformer.java index cb50374..cfe7992 100644 --- a/chaosmetad/tools/jvm/ChaosMetaClassFileTransformer.java +++ b/chaosmetad/tools/jvm/ChaosMetaClassFileTransformer.java @@ -64,6 +64,9 @@ private void doInject(CtMethod ctMethod, ChaosMetaJVMMethodRule methodRule) thro } } + private static String normalizeClass(String className) { + return className.replace("/", "."); + } public String getMessage() { return message; @@ -80,11 +83,16 @@ public byte[] transform(ClassLoader loader, String className, Class classBein System.out.printf("transform class: %s, targetClassName: %s, status: %s, loader: %s, \n", className, targetClassName, status, loader.toString()); try { - if (!targetClassName.equals(className) || (status.equals("recovered"))) { + //fix className not match + String normalizeClassName = normalizeClass(className); + if (!targetClassName.equals(normalizeClassName) || (status.equals("recovered"))) { return null; } ClassPool pool = ClassPool.getDefault(); + //fix javassist not found exception + ClassClassPath classPath = new ClassClassPath(loader.loadClass(targetClassName)); + pool.insertClassPath(classPath); CtClass ctClass = pool.get(targetClassName); if (status.equals("success") || status.equals("fail")) { // TODO: Need to consider how to clean up the added packages when restoring?