From 0eda01182baf1d7add85abef6c92514a40d61d87 Mon Sep 17 00:00:00 2001 From: Ao Li Date: Wed, 1 May 2024 09:06:52 -0400 Subject: [PATCH] Fix verification error caused by instrumentation. --- .../visitors/VolatileFieldsInstrumenter.kt | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/VolatileFieldsInstrumenter.kt b/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/VolatileFieldsInstrumenter.kt index f776da9d..0519978d 100644 --- a/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/VolatileFieldsInstrumenter.kt +++ b/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/VolatileFieldsInstrumenter.kt @@ -7,6 +7,8 @@ import org.objectweb.asm.FieldVisitor import org.objectweb.asm.MethodVisitor import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes.ASM9 +import org.objectweb.asm.Type +import org.objectweb.asm.commons.AdviceAdapter class VolatileFieldsInstrumenter(cv: ClassVisitor, val instrumentJDK: Boolean) : ClassVisitor(ASM9, cv) { @@ -54,13 +56,23 @@ class VolatileFieldsInstrumenter(cv: ClassVisitor, val instrumentJDK: Boolean) : if (name == "" || name == "" || !shouldInstrument) { return mv } - return object : MethodVisitor(ASM9, mv) { + return object : AdviceAdapter(ASM9, mv, access, name, descriptor) { override fun visitFieldInsn(opcode: Int, owner: String, name: String, descriptor: String) { if (recursiveVisitClass(owner) || !instrumentJDK || volatileManager.isVolatile(owner, name)) { - if (opcode == Opcodes.GETFIELD || opcode == Opcodes.PUTFIELD) { - visitVarInsn(Opcodes.ALOAD, 0) + if (opcode == Opcodes.GETFIELD) { + dup() + } + if (opcode == Opcodes.PUTFIELD) { + if (descriptor == "J" || descriptor == "D") { + dup2X1() + pop2() + } else { + dupX1() + pop() + } + dup() } visitLdcInsn(owner) visitLdcInsn(name) @@ -96,6 +108,13 @@ class VolatileFieldsInstrumenter(cv: ClassVisitor, val instrumentJDK: Boolean) : false) } } + if (opcode == Opcodes.PUTFIELD) { + if (descriptor == "J" || descriptor == "D") { + swap(Type.LONG_TYPE, Type.INT_TYPE) + } else { + swap() + } + } super.visitFieldInsn(opcode, owner, name, descriptor) } }