Class StackMapUtils

java.lang.Object
org.plumelib.bcelutil.StackMapUtils
Direct Known Subclasses:
InstructionListUtils

public abstract class StackMapUtils extends Object
This class provides utility methods to maintain and modify a method's StackMapTable within a Java class file. It can be thought of as an extension to BCEL.

BCEL ought to automatically build and maintain the StackMapTable in a manner similar to the LineNumberTable and the LocalVariableTable. However, for historical reasons, it does not.

This class cannot be a set of static methods (like BcelUtil) as it maintains state during the client's processing of a method that must be available on a per thread basis. Thus it is an abstract class extended by InstructionListUtils. A client would not normally extend this class directly.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected SimpleLog
    A log to which to print debugging information about program instrumentation.
    private org.apache.bcel.classfile.StackMapEntry @InternedDistinct []
    An empty StackMap used for initialization.
    protected int
    The index of the first 'true' local in the local variable table.
    protected int
    The number of local variables in the current method prior to any modifications.
    protected org.apache.bcel.classfile.StackMapType @MonotonicNonNull []
    Initial state of StackMapTypes for locals on method entry.
    protected org.apache.bcel.generic.InstructionHandle
    The end of a local variable's live range: the first instruction after the range.
    protected int
    The storage size of local variable during its live range.
    protected org.apache.bcel.generic.InstructionHandle
    The start of a local variable's live range: the first instruction in the range.
    protected org.apache.bcel.generic.Type
    The type of a local variable during its live range.
    protected boolean
    Whether or not the current method needs a StackMap; set by set_current_stack_map_table.
    protected @org.checkerframework.checker.index.qual.NonNegative int
    A number of methods in this class search and locate a particular StackMap within the current method.
    protected @Nullable org.apache.bcel.generic.ConstantPoolGen
    The pool for the method currently being processed.
    protected int
    Offset into code that corresponds to the current StackMap of interest.
    protected @Nullable org.apache.bcel.classfile.StackMap
    Original stack map table attribute; set by set_current_stack_map_table.
    protected org.apache.bcel.classfile.StackMapEntry @Nullable []
    Working copy of StackMapTable; set by set_current_stack_map_table.
    protected StackTypes
    The types of elements on the operand stack for current method.
    private Map<org.apache.bcel.generic.InstructionHandle,Integer>
    A map from instructions that create uninitialized NEW objects to the corresponding StackMap entry.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final org.apache.bcel.generic.LocalVariableGen
    add_new_argument(org.apache.bcel.generic.MethodGen mgen, String arg_name, org.apache.bcel.generic.Type arg_type)
    protected final org.apache.bcel.generic.LocalVariableGen
    add_new_parameter(org.apache.bcel.generic.MethodGen mgen, String arg_name, org.apache.bcel.generic.Type arg_type)
    Add a new parameter to the method.
    protected String[]
    add_string(String[] arr, String new_string)
    Returns a String array with new_string added to the end of arr.
    protected final void
    adjust_code_for_locals_change(org.apache.bcel.generic.MethodGen mgen, int index_first_moved_local, int size)
    Process the instruction list, adding size (1 or 2) to the index of each Instruction that references a local that is equal or higher in the local map than index_first_moved_local.
    protected final StackTypes
    bcel_calc_stack_types(org.apache.bcel.generic.MethodGen mg)
    Calculates the types on the stack for each instruction using the BCEL stack verification routines.
    protected final void
    build_unitialized_NEW_map(org.apache.bcel.generic.InstructionList il)
    We need to locate and remember any NEW instructions that create uninitialized objects.
    protected final void
    create_local_from_live_range(org.apache.bcel.generic.MethodGen mgen, int offset)
    Create a new LocalVariable from the live_range data.
    protected final org.apache.bcel.generic.LocalVariableGen
    create_method_scope_local(org.apache.bcel.generic.MethodGen mgen, String local_name, org.apache.bcel.generic.Type local_type)
    Create a new local with a scope of the full method.
    protected final void
    create_new_stack_map_attribute(org.apache.bcel.generic.MethodGen mgen)
    Create a new StackMap code attribute from stack_map_table.
    protected final void
    fetch_current_stack_map_table(org.apache.bcel.generic.MethodGen mgen, int java_class_version)
    protected final org.apache.bcel.classfile.StackMapEntry
    Find the StackMap entry whose offset matches the input argument.
    protected final @org.checkerframework.checker.index.qual.IndexOrLow({"stack_map_table"}) int
    Find the index of the StackMap entry whose offset is the first one after the input argument.
    protected final int
    Find the index of the StackMap entry whose offset is the last one before the input argument.
    protected final void
    fix_local_variable_table(org.apache.bcel.generic.MethodGen mgen)
    Under some circumstances, there may be gaps in the LocalVariable table.
    protected final int
    gen_locals(org.apache.bcel.generic.MethodGen mgen, int offset)
    Find the live range of the compiler temp(s) and/or user declared local(s) at the given offset and create a LocalVariableGen for each.
    protected final int
    gen_locals_from_byte_codes(org.apache.bcel.generic.MethodGen mgen, int offset)
    Calculate the live range of a local variable (or variables).
    protected final int
    gen_locals_from_byte_codes(org.apache.bcel.generic.MethodGen mgen, int offset, org.apache.bcel.generic.InstructionHandle start)
    Calculate the live range of a local variable starting from the given InstructionHandle.
    protected final org.apache.bcel.classfile.StackMapType
    generate_StackMapType_from_Type(org.apache.bcel.generic.Type t)
    Convert a Type to a StackMapType.
    protected final org.apache.bcel.generic.Type
    generate_Type_from_StackMapType(org.apache.bcel.classfile.StackMapType smt)
    Convert a StackMapType to a Type.
    protected final String
    get_attribute_name(org.apache.bcel.classfile.Attribute a)
    Return the attribute name for the specified attribute.
    protected final @Nullable org.apache.bcel.classfile.Attribute
    get_local_variable_type_table_attribute(org.apache.bcel.generic.MethodGen mgen)
    Find the LocalVariableTypeTable attribute for a method.
    protected final @Nullable org.apache.bcel.classfile.Attribute
    get_stack_map_table_attribute(org.apache.bcel.generic.MethodGen mgen)
    Find the StackMapTable attribute for a method.
    protected final int
    getSize(org.apache.bcel.classfile.StackMapType smt)
    Return the operand size of this type (2 for long and double, 1 otherwise).
    protected final boolean
    is_local_variable_type_table(org.apache.bcel.classfile.Attribute a)
    Returns whether or not the specified attribute is a LocalVariableTypeTable.
    protected final boolean
    is_stack_map_table(org.apache.bcel.classfile.Attribute a)
    Returns whether or not the specified attribute is a StackMapTable.
    protected final void
    modify_stack_maps_for_switches(org.apache.bcel.generic.InstructionHandle ih, org.apache.bcel.generic.InstructionList il)
    Check to see if (due to some instruction modifications) there have been any changes in a switch statement's padding bytes.
    protected final void
    Print the contents of the StackMapTable to the debug_instrument.log.
    protected final void
    remove_local_variable_type_table(org.apache.bcel.generic.MethodGen mgen)
    Remove the local variable type table attribute (LVTT) from mgen.
    protected final void
    set_current_stack_map_table(org.apache.bcel.generic.MethodGen mgen, int java_class_version)
    Get existing StackMapTable from the MethodGen argument.
    protected final void
    set_method_stack_types(org.apache.bcel.generic.MethodGen mgen)
    Calculates the stack types for each byte code offset of the current method, and stores them in variable stack_types.
    protected static @ClassGetName String
    typeToClassGetName(org.apache.bcel.generic.Type t)
    Convert a Type name to a Class name.
    protected final void
    update_full_frame_stack_map_entries(int offset, org.apache.bcel.generic.Type type_new_var, org.apache.bcel.generic.LocalVariableGen[] locals)
    Update any FULL_FRAME StackMap entries to include a new local var.
    private final void
    update_NEW_object_stack_map_entries(int old_offset, int new_offset)
    One of uninitialized NEW instructions has moved.
    protected final void
    update_stack_map_offset(int position, int delta)
    We have inserted additional byte(s) into the instruction list; update the StackMaps, if required.
    protected final void
    update_uninitialized_NEW_offsets(org.apache.bcel.generic.InstructionList il)
    Check to see if any of the uninitialized NEW instructions have moved.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • pool

      protected @Nullable org.apache.bcel.generic.ConstantPoolGen pool
      The pool for the method currently being processed. Must be set by the client. See the sample code in InstructionListUtils for when and how to set this value.
    • debug_instrument

      protected SimpleLog debug_instrument
      A log to which to print debugging information about program instrumentation.
    • needStackMap

      protected boolean needStackMap
      Whether or not the current method needs a StackMap; set by set_current_stack_map_table.
    • stack_map_table

      protected org.apache.bcel.classfile.StackMapEntry @Nullable [] stack_map_table
      Working copy of StackMapTable; set by set_current_stack_map_table.
    • smta

      protected @Nullable org.apache.bcel.classfile.StackMap smta
      Original stack map table attribute; set by set_current_stack_map_table.
    • initial_type_list

      protected org.apache.bcel.classfile.StackMapType @MonotonicNonNull [] initial_type_list
      Initial state of StackMapTypes for locals on method entry.
    • initial_locals_count

      protected int initial_locals_count
      The number of local variables in the current method prior to any modifications.
    • number_active_locals

      protected @org.checkerframework.checker.index.qual.NonNegative int number_active_locals
      A number of methods in this class search and locate a particular StackMap within the current method. This variable contains the number of live local variables within the range of byte code instructions covered by this StackMap. Set by update_stack_map_offset, find_stack_map_equal, find_stack_map_index_before, or find_stack_map_index_after.
    • running_offset

      protected int running_offset
      Offset into code that corresponds to the current StackMap of interest. Set by find_stack_map_index_before.
    • first_local_index

      protected int first_local_index
      The index of the first 'true' local in the local variable table. That is, after 'this' and any parameters.
    • empty_stack_map_table

      private org.apache.bcel.classfile.StackMapEntry @InternedDistinct [] empty_stack_map_table
      An empty StackMap used for initialization.
    • uninitialized_NEW_map

      private Map<org.apache.bcel.generic.InstructionHandle,Integer> uninitialized_NEW_map
      A map from instructions that create uninitialized NEW objects to the corresponding StackMap entry. Set by build_unitialized_NEW_map.
    • live_range_start

      protected org.apache.bcel.generic.InstructionHandle live_range_start
      The start of a local variable's live range: the first instruction in the range.
    • live_range_end

      protected org.apache.bcel.generic.InstructionHandle live_range_end
      The end of a local variable's live range: the first instruction after the range.
    • live_range_type

      protected org.apache.bcel.generic.Type live_range_type
      The type of a local variable during its live range.
    • live_range_operand_size

      protected int live_range_operand_size
      The storage size of local variable during its live range.
    • stack_types

      protected StackTypes stack_types
      The types of elements on the operand stack for current method.
  • Constructor Details

    • StackMapUtils

      public StackMapUtils()
  • Method Details

    • add_string

      protected String[] add_string(String[] arr, String new_string)
      Returns a String array with new_string added to the end of arr.
      Parameters:
      arr - original string array
      new_string - string to be added
      Returns:
      the new string array
    • get_attribute_name

      @Pure protected final String get_attribute_name(org.apache.bcel.classfile.Attribute a)
      Return the attribute name for the specified attribute.
      Parameters:
      a - the attribute
      Returns:
      the attribute name for the specified attribute
    • is_local_variable_type_table

      @Pure protected final boolean is_local_variable_type_table(org.apache.bcel.classfile.Attribute a)
      Returns whether or not the specified attribute is a LocalVariableTypeTable.
      Parameters:
      a - the attribute
      Returns:
      true iff the attribute is a LocalVariableTypeTable
    • is_stack_map_table

      @Pure protected final boolean is_stack_map_table(org.apache.bcel.classfile.Attribute a)
      Returns whether or not the specified attribute is a StackMapTable.
      Parameters:
      a - the attribute
      Returns:
      true iff the attribute is a StackMapTable
    • get_stack_map_table_attribute

      @Pure protected final @Nullable org.apache.bcel.classfile.Attribute get_stack_map_table_attribute(org.apache.bcel.generic.MethodGen mgen)
      Find the StackMapTable attribute for a method. Return null if there isn't one.
      Parameters:
      mgen - the method
      Returns:
      the StackMapTable attribute for the method (or null if not present)
    • get_local_variable_type_table_attribute

      @Pure protected final @Nullable org.apache.bcel.classfile.Attribute get_local_variable_type_table_attribute(org.apache.bcel.generic.MethodGen mgen)
      Find the LocalVariableTypeTable attribute for a method. Return null if there isn't one.
      Parameters:
      mgen - the method
      Returns:
      the LocalVariableTypeTable attribute for the method (or null if not present)
    • remove_local_variable_type_table

      protected final void remove_local_variable_type_table(org.apache.bcel.generic.MethodGen mgen)
      Remove the local variable type table attribute (LVTT) from mgen. Some instrumentation changes require this to be updated, but without BCEL support that would be hard to do. It should be safe to just delete it since it is optional and really only of use to a debugger.
      Parameters:
      mgen - the method to clear out
    • update_stack_map_offset

      protected final void update_stack_map_offset(int position, int delta)
      We have inserted additional byte(s) into the instruction list; update the StackMaps, if required. Also sets running_offset.
      Parameters:
      position - the location of insertion
      delta - the size of the insertion
    • find_stack_map_equal

      protected final org.apache.bcel.classfile.StackMapEntry find_stack_map_equal(int offset)
      Find the StackMap entry whose offset matches the input argument. Also sets running_offset.
      Parameters:
      offset - byte code offset
      Returns:
      the corresponding StackMapEntry
    • find_stack_map_index_before

      protected final int find_stack_map_index_before(int offset)
      Find the index of the StackMap entry whose offset is the last one before the input argument. Return -1 if there isn't one. Also sets running_offset and number_active_locals.
      Parameters:
      offset - byte code offset
      Returns:
      the corresponding StackMapEntry index
    • find_stack_map_index_after

      protected final @org.checkerframework.checker.index.qual.IndexOrLow({"stack_map_table"}) int find_stack_map_index_after(int offset)
      Find the index of the StackMap entry whose offset is the first one after the input argument. Return -1 if there isn't one. Also sets running_offset.
      Parameters:
      offset - byte code offset
      Returns:
      the corresponding StackMapEntry index
    • modify_stack_maps_for_switches

      protected final void modify_stack_maps_for_switches(org.apache.bcel.generic.InstructionHandle ih, org.apache.bcel.generic.InstructionList il)
      Check to see if (due to some instruction modifications) there have been any changes in a switch statement's padding bytes. If so, then update the corresponding StackMap to reflect this change.
      Parameters:
      ih - where to start looking for a switch instruction
      il - instruction list to search
    • build_unitialized_NEW_map

      protected final void build_unitialized_NEW_map(org.apache.bcel.generic.InstructionList il)
      We need to locate and remember any NEW instructions that create uninitialized objects. Their offset may be contained in a StackMap entry and will probably need to be updated as we add instrumentation code. Note that these instructions are fairly rare.
      Parameters:
      il - instruction list to search
    • update_NEW_object_stack_map_entries

      private final void update_NEW_object_stack_map_entries(int old_offset, int new_offset)
      One of uninitialized NEW instructions has moved. Update its offset in StackMap entries. Note that more than one entry could refer to the same instruction. This is a helper routine used by update_uninitialized_NEW_offsets.
      Parameters:
      old_offset - original location of NEW instruction
      new_offset - new location of NEW instruction
    • update_uninitialized_NEW_offsets

      protected final void update_uninitialized_NEW_offsets(org.apache.bcel.generic.InstructionList il)
      Check to see if any of the uninitialized NEW instructions have moved. Again, these are rare, so a linear pass is fine.
      Parameters:
      il - instruction list to search
    • adjust_code_for_locals_change

      protected final void adjust_code_for_locals_change(org.apache.bcel.generic.MethodGen mgen, int index_first_moved_local, int size)
      Process the instruction list, adding size (1 or 2) to the index of each Instruction that references a local that is equal or higher in the local map than index_first_moved_local. Size should be the size of the new local that was just inserted at index_first_moved_local.
      Parameters:
      mgen - MethodGen to be modified
      index_first_moved_local - original index of first local moved "up"
      size - size of new local added (1 or 2)
    • fetch_current_stack_map_table

      @Deprecated protected final void fetch_current_stack_map_table(org.apache.bcel.generic.MethodGen mgen, int java_class_version)
      Get existing StackMapTable from the MethodGen argument. If there is none, create a new empty one. Sets both smta and stack_map_table. Must be called prior to any other methods that manipulate the stack_map_table!
      Parameters:
      mgen - MethodGen to search
      java_class_version - Java version for the classfile; stack_map_table is optional before Java 1.7 (= classfile version 51)
    • set_current_stack_map_table

      @EnsuresNonNull("stack_map_table") protected final void set_current_stack_map_table(org.apache.bcel.generic.MethodGen mgen, int java_class_version)
      Get existing StackMapTable from the MethodGen argument. If there is none, create a new empty one. Sets both smta and stack_map_table. Must be called prior to any other methods that manipulate the stack_map_table!
      Parameters:
      mgen - MethodGen to search
      java_class_version - Java version for the classfile; stack_map_table is optional before Java 1.7 (= classfile version 51)
    • create_new_stack_map_attribute

      protected final void create_new_stack_map_attribute(org.apache.bcel.generic.MethodGen mgen) throws IOException
      Create a new StackMap code attribute from stack_map_table.
      Parameters:
      mgen - MethodGen to add attribute to
      Throws:
      IOException - if cannot create the attribute
    • typeToClassGetName

      protected static @ClassGetName String typeToClassGetName(org.apache.bcel.generic.Type t)
      Convert a Type name to a Class name.
      Parameters:
      t - type whose name is to be converted
      Returns:
      a String containing the class name
    • generate_StackMapType_from_Type

      protected final org.apache.bcel.classfile.StackMapType generate_StackMapType_from_Type(org.apache.bcel.generic.Type t)
      Convert a Type to a StackMapType.
      Parameters:
      t - Type to be converted
      Returns:
      result StackMapType
    • generate_Type_from_StackMapType

      protected final org.apache.bcel.generic.Type generate_Type_from_StackMapType(org.apache.bcel.classfile.StackMapType smt)
      Convert a StackMapType to a Type.
      Parameters:
      smt - StackMapType to be converted
      Returns:
      result Type
    • getSize

      protected final int getSize(org.apache.bcel.classfile.StackMapType smt)
      Return the operand size of this type (2 for long and double, 1 otherwise).
      Parameters:
      smt - a StackMapType object
      Returns:
      the operand size of this type
    • update_full_frame_stack_map_entries

      protected final void update_full_frame_stack_map_entries(int offset, org.apache.bcel.generic.Type type_new_var, org.apache.bcel.generic.LocalVariableGen[] locals)
      Update any FULL_FRAME StackMap entries to include a new local var. The locals array is a copy of the local variables PRIOR to the addition of the new local in question.
      Parameters:
      offset - offset into stack of the new variable we are adding
      type_new_var - type of new variable we are adding
      locals - a copy of the local variable table prior to this modification
    • add_new_argument

      @Deprecated protected final org.apache.bcel.generic.LocalVariableGen add_new_argument(org.apache.bcel.generic.MethodGen mgen, String arg_name, org.apache.bcel.generic.Type arg_type)
      Add a new parameter to the method. This will be added after last current parameter and before the first local variable. This might have the side effect of causing us to rewrite the method byte codes to adjust the offsets for the local variables - see below for details.

      Must call fix_local_variable_table (just once per method) before calling this routine.

      Parameters:
      mgen - MethodGen to be modified
      arg_name - name of new parameter
      arg_type - type of new parameter
      Returns:
      a LocalVariableGen for the new parameter
    • add_new_parameter

      protected final org.apache.bcel.generic.LocalVariableGen add_new_parameter(org.apache.bcel.generic.MethodGen mgen, String arg_name, org.apache.bcel.generic.Type arg_type)
      Add a new parameter to the method. This will be added after last current parameter and before the first local variable. This might have the side effect of causing us to rewrite the method byte codes to adjust the offsets for the local variables - see below for details.

      Must call fix_local_variable_table (just once per method) before calling this routine.

      Parameters:
      mgen - MethodGen to be modified
      arg_name - name of new parameter
      arg_type - type of new parameter
      Returns:
      a LocalVariableGen for the new parameter
    • create_method_scope_local

      protected final org.apache.bcel.generic.LocalVariableGen create_method_scope_local(org.apache.bcel.generic.MethodGen mgen, String local_name, org.apache.bcel.generic.Type local_type)
      Create a new local with a scope of the full method. This means we need to search the existing locals to find the proper index for our new local. This might have the side effect of causing us to rewrite the method byte codes to adjust the offsets for the existing local variables - see below for details.

      Must call fix_local_variable_table (just once per method) before calling this routine.

      Parameters:
      mgen - MethodGen to be modified
      local_name - name of new local
      local_type - type of new local
      Returns:
      a LocalVariableGen for the new local
    • fix_local_variable_table

      @EnsuresNonNull("initial_type_list") protected final void fix_local_variable_table(org.apache.bcel.generic.MethodGen mgen)
      Under some circumstances, there may be gaps in the LocalVariable table. These gaps occur when the Java compiler adds unnamed parameters and/or unnamed local variables. A gap may also occur for a local variable declared in the source whose lifetime does not cross a StackMap location. This routine creates LocalVariable entries for these missing items.
      1. The java Compiler allocates a hidden parameter for the constructor of an inner class. These items are given the name $hidden$ appended with their offset.
      2. The Java compiler allocates unnamed local temps for:
        • saving the exception in a finally clause
        • the lock for a synchronized block
        • interators
        • user declared locals that never appear in a StackMap
        • (others?)
        These items are given the name DaIkOnTeMp appended with their offset.
      Parameters:
      mgen - MethodGen to be modified
    • gen_locals

      @RequiresNonNull("initial_type_list") protected final int gen_locals(org.apache.bcel.generic.MethodGen mgen, int offset)
      Find the live range of the compiler temp(s) and/or user declared local(s) at the given offset and create a LocalVariableGen for each. Note the compiler might generate temps of different sizes at the same offset (must have disjoint lifetimes). In general, these variables will not have a live range of the entire method. We try to calculate the true live range so if, at some later point, we need to generate a new StackMap we can include the correct list of active locals.
      Parameters:
      mgen - the method
      offset - compiler assigned local offset of hidden temp(s) or local(s)
      Returns:
      offset incremented by size of smallest variable found at offset
    • gen_locals_from_byte_codes

      protected final int gen_locals_from_byte_codes(org.apache.bcel.generic.MethodGen mgen, int offset)
      Calculate the live range of a local variable (or variables).
      Parameters:
      mgen - MethodGen of method to search
      offset - offset of the local
      Returns:
      minimum size of local(s) found at offset
    • gen_locals_from_byte_codes

      protected final int gen_locals_from_byte_codes(org.apache.bcel.generic.MethodGen mgen, int offset, org.apache.bcel.generic.InstructionHandle start)
      Calculate the live range of a local variable starting from the given InstructionHandle. The following live_range globals must be set:
      • live_range_start
      • live_range_end
      • live_range_type
      • live_range_operand_size
      Parameters:
      mgen - MethodGen of method to search
      offset - offset of the local
      start - search forward from this instruction
      Returns:
      minimum size of local(s) found at offset
    • create_local_from_live_range

      protected final void create_local_from_live_range(org.apache.bcel.generic.MethodGen mgen, int offset)
      Create a new LocalVariable from the live_range data. Does nothing if live_range_start is null.
      Parameters:
      mgen - MethodGen of method to search
      offset - offset of the local
    • set_method_stack_types

      protected final void set_method_stack_types(org.apache.bcel.generic.MethodGen mgen)
      Calculates the stack types for each byte code offset of the current method, and stores them in variable stack_types. Does nothing if stack_types is already set.
      Parameters:
      mgen - MethodGen of method whose stack types to compute
    • bcel_calc_stack_types

      protected final StackTypes bcel_calc_stack_types(org.apache.bcel.generic.MethodGen mg)
      Calculates the types on the stack for each instruction using the BCEL stack verification routines.
      Parameters:
      mg - MethodGen for the method to be analyzed
      Returns:
      a StackTypes object for the method