Uploaded image for project: 'Byteman'
  1. Byteman
  2. BYTEMAN-443

NullPointerException while invoking a Rule for a method having argument as Long

XMLWordPrintable

    • User Experience

      I am getting a NPE in my application while using a Byteman rule. I created a test java code and simulated the same issue, call to method m2 is generating NPE Version: /projects/byteman-download-4.0.23
      Following is the java code:

      import java.util.*;
      
      public class npe {
        public static void main(String args[]) {
          Long l=System.currentTimeMillis();
          Long lx=null;
      
          m1(l);
          m2(lx);
        }
        
        static void m1(Long l1)
        {
          System.out.println("l1 = " + l1);  
        }
        
         static void m2(Long l2)
        {
          System.out.println("l2 new = " + l2);  
        }
      }

      Following is the byteman script:

      RULE catch.npe.m1
      CLASS npe
      METHOD m1
      COMPILE
      AT ENTRY
      IF true
      DO
       System.out.println("[BMAN] Got Method Trigger: m1 [" + $1 + "]");
      ENDRULE
      
      RULE catch.npe.m2
      CLASS npe
      METHOD m2
      COMPILE
      AT ENTRY
      IF true
      DO
       System.out.println("[BMAN] Got Method Trigger: m2 [" + $1 + "]");
      ENDRULE

       
      Now executing the java program:
       

      [root@asif-me btest]# java -Dorg.jboss.byteman.verbose   -Dorg.jboss.byteman.dump.generated.classes   -Dorg.jboss.byteman.dump.generated.classes.directory=/projects/tools/btest/decomp -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=script:npe.btm -Dorg.jboss.byteman.compile.to.bytecode npe
      AccessManager:init Initialising JDK9 AccessManager
      AccessManager:init created module
      AccessManager:init added extraReads
      org.jboss.byteman.agent.Transformer : Saving transformed bytes to /projects/tools/btest/decomp/org/jboss/byteman/jigsaw/JigsawAccessEnabler.class
      AccessManager:init returning JigsawAccessEnabler
      org.jboss.byteman.agent.Transformer : possible trigger for rule catch.npe.m2 in class npe
      RuleTriggerMethodAdapter.injectTriggerPoint : inserting trigger into npe.m2(java.lang.Long) void for rule catch.npe.m2
      org.jboss.byteman.agent.Transformer : inserted trigger for catch.npe.m2 in class npe
      org.jboss.byteman.agent.Transformer : possible trigger for rule catch.npe.m1 in class npe
      RuleTriggerMethodAdapter.injectTriggerPoint : inserting trigger into npe.m1(java.lang.Long) void for rule catch.npe.m1
      org.jboss.byteman.agent.Transformer : inserted trigger for catch.npe.m1 in class npe
      org.jboss.byteman.agent.Transformer : Saving transformed bytes to /projects/tools/btest/decomp/npe.class
      Rule.execute called for catch.npe.m1_0:0
      org.jboss.byteman.agent.Transformer : Saving transformed bytes to /projects/tools/btest/decomp/org/jboss/byteman/rule/helper/Helper_HelperAdapter_Compiled_1.class
      HelperManager.install for helper class org.jboss.byteman.rule.helper.Helper
      calling activated() for helper class org.jboss.byteman.rule.helper.Helper
      Default helper activated
      calling installed(catch.npe.m1) for helper classorg.jboss.byteman.rule.helper.Helper
      Installed rule using default helper : catch.npe.m1
      catch.npe.m1 execute()
      [BMAN] Got Method Trigger: m1 [1727221411198]
      l1 = 1727221411198
      Rule.execute called for catch.npe.m2_1:0
      org.jboss.byteman.agent.Transformer : Saving transformed bytes to /projects/tools/btest/decomp/org/jboss/byteman/rule/helper/Helper_HelperAdapter_Compiled_2.class
      HelperManager.install for helper class org.jboss.byteman.rule.helper.Helper
      calling installed(catch.npe.m2) for helper classorg.jboss.byteman.rule.helper.Helper
      Installed rule using default helper : catch.npe.m2
      catch.npe.m2 execute()
      catch.npe.m2 : java.lang.NullPointerException
      caught ExecuteException
      Exception in thread "main" org.jboss.byteman.rule.exception.ExecuteException: catch.npe.m2  : caught java.lang.NullPointerException
          at org.jboss.byteman.rule.Rule.execute(Rule.java:852)
          at org.jboss.byteman.rule.Rule.execute(Rule.java:789)
          at npe.m2(npe.java)
          at npe.main(npe.java:9)
      Caused by: java.lang.NullPointerException
          at org.jboss.byteman.rule.helper.Helper_HelperAdapter_Compiled_2.execute(npe.btm)
          at org.jboss.byteman.rule.Rule.execute(Rule.java:820)
          ... 3 more
      [root@asif-me btest]# 

      The generated code from dumped class file:
       

      [root@asif-me decomp]# javap -v -p -s -c  npe.class
      Classfile /projects/tools/btest/decomp/npe.class
        Last modified Sep 25, 2024; size 1806 bytes
        MD5 checksum 7160079c97164a866b71ec6497fec007
        Compiled from "npe.java"
      public class npe
        minor version: 0
        major version: 55
        flags: (0x0021) ACC_PUBLIC, ACC_SUPER
        this_class: #2                          // npe
        super_class: #4                         // java/lang/Object
        interfaces: 0, fields: 0, methods: 4, attributes: 3
      Constant pool:
         #1 = Utf8               npe
         #2 = Class              #1             // npe
         #3 = Utf8               java/lang/Object
         #4 = Class              #3             // java/lang/Object
         #5 = Utf8               npe.java
         #6 = Utf8               java/lang/invoke/MethodHandles$Lookup
         #7 = Class              #6             // java/lang/invoke/MethodHandles$Lookup
         #8 = Utf8               java/lang/invoke/MethodHandles
         #9 = Class              #8             // java/lang/invoke/MethodHandles
        #10 = Utf8               Lookup
        #11 = Utf8               <init>
        #12 = Utf8               ()V
        #13 = NameAndType        #11:#12        // "<init>":()V
        #14 = Methodref          #4.#13         // java/lang/Object."<init>":()V
        #15 = Utf8               main
        #16 = Utf8               ([Ljava/lang/String;)V
        #17 = Utf8               java/lang/System
        #18 = Class              #17            // java/lang/System
        #19 = Utf8               currentTimeMillis
        #20 = Utf8               ()J
        #21 = NameAndType        #19:#20        // currentTimeMillis:()J
        #22 = Methodref          #18.#21        // java/lang/System.currentTimeMillis:()J
        #23 = Utf8               java/lang/Long
        #24 = Class              #23            // java/lang/Long
        #25 = Utf8               valueOf
        #26 = Utf8               (J)Ljava/lang/Long;
        #27 = NameAndType        #25:#26        // valueOf:(J)Ljava/lang/Long;
        #28 = Methodref          #24.#27        // java/lang/Long.valueOf:(J)Ljava/lang/Long;
        #29 = Utf8               m1
        #30 = Utf8               (Ljava/lang/Long;)V
        #31 = NameAndType        #29:#30        // m1:(Ljava/lang/Long;)V
        #32 = Methodref          #2.#31         // npe.m1:(Ljava/lang/Long;)V
        #33 = Utf8               m2
        #34 = NameAndType        #33:#30        // m2:(Ljava/lang/Long;)V
        #35 = Methodref          #2.#34         // npe.m2:(Ljava/lang/Long;)V
        #36 = Utf8               catch.npe.m1_0:0
        #37 = String             #36            // catch.npe.m1_0:0
        #38 = Utf8               org/jboss/byteman/rule/Rule
        #39 = Class              #38            // org/jboss/byteman/rule/Rule
        #40 = Utf8               execute
        #41 = Utf8               (Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V
        #42 = NameAndType        #40:#41        // execute:(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V
        #43 = Methodref          #39.#42        // org/jboss/byteman/rule/Rule.execute:(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V
        #44 = Utf8               org/jboss/byteman/rule/exception/EarlyReturnException
        #45 = Class              #44            // org/jboss/byteman/rule/exception/EarlyReturnException
        #46 = Utf8               org/jboss/byteman/rule/exception/ThrowException
        #47 = Class              #46            // org/jboss/byteman/rule/exception/ThrowException
        #48 = Utf8               org/jboss/byteman/rule/exception/ExecuteException
        #49 = Class              #48            // org/jboss/byteman/rule/exception/ExecuteException
        #50 = Utf8               out
        #51 = Utf8               Ljava/io/PrintStream;
        #52 = NameAndType        #50:#51        // out:Ljava/io/PrintStream;
        #53 = Fieldref           #18.#52        // java/lang/System.out:Ljava/io/PrintStream;
        #54 = Utf8               l1 = \u0001
        #55 = String             #54            // l1 = \u0001
        #56 = Utf8               java/lang/invoke/StringConcatFactory
        #57 = Class              #56            // java/lang/invoke/StringConcatFactory
        #58 = Utf8               makeConcatWithConstants
        #59 = Utf8               (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
        #60 = NameAndType        #58:#59        // makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
        #61 = Methodref          #57.#60        // java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
        #62 = MethodHandle       6:#61          // REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
        #63 = Utf8               (Ljava/lang/Long;)Ljava/lang/String;
        #64 = NameAndType        #58:#63        // makeConcatWithConstants:(Ljava/lang/Long;)Ljava/lang/String;
        #65 = InvokeDynamic      #0:#64         // #0:makeConcatWithConstants:(Ljava/lang/Long;)Ljava/lang/String;
        #66 = Utf8               java/io/PrintStream
        #67 = Class              #66            // java/io/PrintStream
        #68 = Utf8               println
        #69 = Utf8               (Ljava/lang/String;)V
        #70 = NameAndType        #68:#69        // println:(Ljava/lang/String;)V
        #71 = Methodref          #67.#70        // java/io/PrintStream.println:(Ljava/lang/String;)V
        #72 = Utf8               caught ReturnException
        #73 = String             #72            // caught ReturnException
        #74 = Utf8               caught ThrowException
        #75 = String             #74            // caught ThrowException
        #76 = Utf8               getThrowable
        #77 = Utf8               ()Ljava/lang/Throwable;
        #78 = NameAndType        #76:#77        // getThrowable:()Ljava/lang/Throwable;
        #79 = Methodref          #47.#78        // org/jboss/byteman/rule/exception/ThrowException.getThrowable:()Ljava/lang/Throwable;
        #80 = Utf8               caught ExecuteException
        #81 = String             #80            // caught ExecuteException
        #82 = Utf8               catch.npe.m2_1:0
        #83 = String             #82            // catch.npe.m2_1:0
        #84 = Utf8               l2 new = \u0001
        #85 = String             #84            // l2 new = \u0001
        #86 = InvokeDynamic      #1:#64         // #1:makeConcatWithConstants:(Ljava/lang/Long;)Ljava/lang/String;
        #87 = Utf8               Code
        #88 = Utf8               LineNumberTable
        #89 = Utf8               StackMapTable
        #90 = Utf8               InnerClasses
        #91 = Utf8               SourceFile
        #92 = Utf8               BootstrapMethods
      {
        public npe();
          descriptor: ()V
          flags: (0x0001) ACC_PUBLIC
          Code:
            stack=1, locals=1, args_size=1
               0: aload_0
               1: invokespecial #14                 // Method java/lang/Object."<init>":()V
               4: return
            LineNumberTable:
              line 3: 0
      
        public static void main(java.lang.String[]);
          descriptor: ([Ljava/lang/String;)V
          flags: (0x0009) ACC_PUBLIC, ACC_STATIC
          Code:
            stack=2, locals=3, args_size=1
               0: invokestatic  #22                 // Method java/lang/System.currentTimeMillis:()J
               3: invokestatic  #28                 // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
               6: astore_1
               7: aconst_null
               8: astore_2
               9: aload_1
              10: invokestatic  #32                 // Method m1:(Ljava/lang/Long;)V
              13: aload_2
              14: invokestatic  #35                 // Method m2:(Ljava/lang/Long;)V
              17: return
            LineNumberTable:
              line 5: 0
              line 6: 7
              line 8: 9
              line 9: 13
              line 10: 17
      
        static void m1(java.lang.Long);
          descriptor: (Ljava/lang/Long;)V
          flags: (0x0008) ACC_STATIC
          Code:
            stack=6, locals=1, args_size=1
               0: ldc           #37                 // String catch.npe.m1_0:0
               2: aconst_null
               3: iconst_1
               4: anewarray     #4                  // class java/lang/Object
               7: dup
               8: iconst_0
               9: aload_0
              10: aastore
              11: invokestatic  #43                 // Method org/jboss/byteman/rule/Rule.execute:(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V
              14: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              17: aload_0
              18: invokedynamic #65,  0             // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/Long;)Ljava/lang/String;
              23: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              26: return
              27: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              30: ldc           #73                 // String caught ReturnException
              32: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              35: pop
              36: return
              37: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              40: ldc           #75                 // String caught ThrowException
              42: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              45: invokevirtual #79                 // Method org/jboss/byteman/rule/exception/ThrowException.getThrowable:()Ljava/lang/Throwable;
              48: athrow
              49: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              52: ldc           #81                 // String caught ExecuteException
              54: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              57: athrow
            Exception table:
               from    to  target type
                   0    14    27   Class org/jboss/byteman/rule/exception/EarlyReturnException
                   0    14    37   Class org/jboss/byteman/rule/exception/ThrowException
                   0    14    49   Class org/jboss/byteman/rule/exception/ExecuteException
            StackMapTable: number_of_entries = 3
              frame_type = 91 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/EarlyReturnException ]
              frame_type = 73 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/ThrowException ]
              frame_type = 75 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/ExecuteException ]
            LineNumberTable:
              line 14: 14
              line 15: 26
      
        static void m2(java.lang.Long);
          descriptor: (Ljava/lang/Long;)V
          flags: (0x0008) ACC_STATIC
          Code:
            stack=6, locals=1, args_size=1
               0: ldc           #83                 // String catch.npe.m2_1:0
               2: aconst_null
               3: iconst_1
               4: anewarray     #4                  // class java/lang/Object
               7: dup
               8: iconst_0
               9: aload_0
              10: aastore
              11: invokestatic  #43                 // Method org/jboss/byteman/rule/Rule.execute:(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/Object;)V
              14: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              17: aload_0
              18: invokedynamic #86,  0             // InvokeDynamic #1:makeConcatWithConstants:(Ljava/lang/Long;)Ljava/lang/String;
              23: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              26: return
              27: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              30: ldc           #73                 // String caught ReturnException
              32: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              35: pop
              36: return
              37: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              40: ldc           #75                 // String caught ThrowException
              42: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              45: invokevirtual #79                 // Method org/jboss/byteman/rule/exception/ThrowException.getThrowable:()Ljava/lang/Throwable;
              48: athrow
              49: getstatic     #53                 // Field java/lang/System.out:Ljava/io/PrintStream;
              52: ldc           #81                 // String caught ExecuteException
              54: invokevirtual #71                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
              57: athrow
            Exception table:
               from    to  target type
                   0    14    27   Class org/jboss/byteman/rule/exception/EarlyReturnException
                   0    14    37   Class org/jboss/byteman/rule/exception/ThrowException
                   0    14    49   Class org/jboss/byteman/rule/exception/ExecuteException
            StackMapTable: number_of_entries = 3
              frame_type = 91 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/EarlyReturnException ]
              frame_type = 73 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/ThrowException ]
              frame_type = 75 /* same_locals_1_stack_item */
                stack = [ class org/jboss/byteman/rule/exception/ExecuteException ]
            LineNumberTable:
              line 19: 14
              line 20: 26
      }
      InnerClasses:
        public static final #10= #7 of #9;      // Lookup=class java/lang/invoke/MethodHandles$Lookup of class java/lang/invoke/MethodHandles
      SourceFile: "npe.java"
      BootstrapMethods:
        0: #62 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
          Method arguments:
            #55 l1 = \u0001
        1: #62 REF_invokeStatic java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
          Method arguments:
            #85 l2 new = \u0001

      Why NPE is generated ? Seems when rule is invoked, it is trying to pass null to native long, instead of Long.
       

              rhn-engineering-adinn Andrew Dinn
              narendrasingh2 Narendra SINGH2
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: