|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.dependencies.patches.awsv2sdk; |
| 11 | + |
| 12 | +import org.objectweb.asm.ClassVisitor; |
| 13 | +import org.objectweb.asm.ClassWriter; |
| 14 | +import org.objectweb.asm.MethodVisitor; |
| 15 | +import org.objectweb.asm.Type; |
| 16 | + |
| 17 | +import java.util.Locale; |
| 18 | + |
| 19 | +import static org.objectweb.asm.Opcodes.ASM9; |
| 20 | +import static org.objectweb.asm.Opcodes.GETSTATIC; |
| 21 | +import static org.objectweb.asm.Opcodes.INVOKESTATIC; |
| 22 | + |
| 23 | +class StringFormatInPathResolverPatcher extends ClassVisitor { |
| 24 | + |
| 25 | + StringFormatInPathResolverPatcher(ClassWriter classWriter) { |
| 26 | + super(ASM9, classWriter); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 31 | + return new ReplaceCallMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions)); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Replaces calls to String.format(format, args); with calls to String.format(Locale.ROOT, format, args); |
| 36 | + */ |
| 37 | + private static class ReplaceCallMethodVisitor extends MethodVisitor { |
| 38 | + private static final String CLASS_INTERNAL_NAME = Type.getInternalName(String.class); |
| 39 | + private static final String METHOD_NAME = "format"; |
| 40 | + private static final String OLD_METHOD_DESCRIPTOR = Type.getMethodDescriptor( |
| 41 | + Type.getType(String.class), |
| 42 | + Type.getType(String.class), |
| 43 | + Type.getType(Object[].class) |
| 44 | + ); |
| 45 | + private static final String NEW_METHOD_DESCRIPTOR = Type.getMethodDescriptor( |
| 46 | + Type.getType(String.class), |
| 47 | + Type.getType(Locale.class), |
| 48 | + Type.getType(String.class), |
| 49 | + Type.getType(Object[].class) |
| 50 | + ); |
| 51 | + |
| 52 | + private boolean foundFormatPattern = false; |
| 53 | + |
| 54 | + ReplaceCallMethodVisitor(MethodVisitor methodVisitor) { |
| 55 | + super(ASM9, methodVisitor); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void visitLdcInsn(Object value) { |
| 60 | + if (value instanceof String s && s.startsWith("%s")) { |
| 61 | + if (foundFormatPattern) { |
| 62 | + throw new IllegalStateException( |
| 63 | + "A previous string format constant was not paired with a String.format() call. " |
| 64 | + + "Patching would generate an unbalances stack" |
| 65 | + ); |
| 66 | + } |
| 67 | + // Push the extra arg on the stack |
| 68 | + mv.visitFieldInsn(GETSTATIC, Type.getInternalName(Locale.class), "ROOT", Type.getDescriptor(Locale.class)); |
| 69 | + foundFormatPattern = true; |
| 70 | + } |
| 71 | + super.visitLdcInsn(value); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { |
| 76 | + if (opcode == INVOKESTATIC |
| 77 | + && foundFormatPattern |
| 78 | + && CLASS_INTERNAL_NAME.equals(owner) |
| 79 | + && METHOD_NAME.equals(name) |
| 80 | + && OLD_METHOD_DESCRIPTOR.equals(descriptor)) { |
| 81 | + // Replace the call with String.format(Locale.ROOT, format, args) |
| 82 | + mv.visitMethodInsn(INVOKESTATIC, CLASS_INTERNAL_NAME, METHOD_NAME, NEW_METHOD_DESCRIPTOR, false); |
| 83 | + foundFormatPattern = false; |
| 84 | + } else { |
| 85 | + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments