Bug 20338

Summary: Program compiled with gcj crashes when accessing private static method from nested class
Product: gcc Reporter: Zbigniew Chyla <zbigniew>
Component: javaAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs, java-prs
Priority: P2 Keywords: ice-on-valid-code, patch, wrong-code
Version: 4.0.0   
Target Milestone: 4.0.3   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2005-03-09 12:39:24

Description Zbigniew Chyla 2005-03-05 23:15:33 UTC
Here's very simple example of class that compiles fine but crashes during
execution. It has something to do with "private" modifier in method of nested
class - after removing "private" the problem disappears.

$ cat Outer.java
public class Outer {
    public static void main(String[] args) {
        Nested.test();
    }

    static class Nested {
        private static void test() {
            System.out.println("test");
        }
    }
}
$ gcj-4.0 -o outer --main=Outer Outer.java
$ ./outer
Exception in thread "main" java.lang.NullPointerException
   at gnu.gcj.convert.Output_8859_1.write(java.lang.String, int, int, char[])
(/usr/lib/libgcj.so.6.0.0)
   at java.io.PrintStream.writeChars(java.lang.String, int, int)
(/usr/lib/libgcj.so.6.0.0)
   at java.io.PrintStream.print(java.lang.String, boolean)
(/usr/lib/libgcj.so.6.0.0)
   at java.io.PrintStream.println(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
   at Outer$Nested.test() (Unknown Source)
   at Outer.main(java.lang.String[]) (Unknown Source)
Comment 1 Andrew Pinski 2005-03-05 23:20:14 UTC
Confirmed, note with -fdump-tree-all, we ICE also which seems wrong.
Comment 2 Ranjit Mathew 2005-03-09 12:39:22 UTC
The problem is that we leave out a call to _Jv_InitClass for a static
private method thinking that it is unreachable. This is not the case
for a private static inner class method. A simple pessimistic
fix is:

Index: decl.c
===================================================================
--- decl.c      2005-03-09 17:16:36.000000000 +0530
+++ decl.c      2005-03-09 17:17:18.000000000 +0530
@@ -2039,5 +2039,5 @@ finish_method (tree fndecl)
   /* Prepend class initialization for static methods reachable from
      other classes.  */
-  if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
+  if (METHOD_STATIC (fndecl)
       && ! DECL_CLINIT_P (fndecl)
       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
Comment 3 Ranjit Mathew 2005-03-09 14:51:26 UTC
A proposed patch is here:

  http://gcc.gnu.org/ml/java-patches/2005-q1/msg00684.html

As noted there, there is still a problem with either 
GCJ or gij for this testcase.
Comment 4 Bryce McKinlay 2005-03-09 18:06:55 UTC
Other Java compilers generate "accessor" methods for calls to private methods in
a nested class, the real bug here is that GCJ doesn't do this - thats why the
javac bytecode works fine on gij but gcj-produced bytecode does not.
Comment 5 CVS Commits 2005-03-09 19:05:03 UTC
Subject: Bug 20338

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	rmathew@gcc.gnu.org	2005-03-09 19:04:55

Modified files:
	gcc/java       : ChangeLog decl.c 

Log message:
	PR java/20338
	* decl.c (finish_method): Emit _Jv_InitClass for private static
	methods inside inner classes as well.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/ChangeLog.diff?cvsroot=gcc&r1=1.1569&r2=1.1570
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/decl.c.diff?cvsroot=gcc&r1=1.211&r2=1.212

Comment 6 Ranjit Mathew 2005-03-10 11:00:37 UTC
While the main problem reported by the filer has now been fixed on
mainline, I think we should not close this PR just yet - not until
we check in a testcase for our testsuite for this, which is not 
possible till PR19870 is fixed.

Otherwise, we can close this one and add a note to PR19870
to remember to add in the testcase for this PR when that is
fixed.
Comment 7 Bryce McKinlay 2005-03-10 11:30:59 UTC
Why not check in the test case and XFAIL it? 
Comment 8 Ranjit Mathew 2005-03-10 12:00:19 UTC
(In reply to comment #7)
> Why not check in the test case and XFAIL it? 

I feel dumb for not having thought of it myself...

I am closing this bug and will submit the testcase as
a separate patch. Thanks.
Comment 9 CVS Commits 2005-10-05 19:35:51 UTC
Subject: Bug 20338

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	bryce@gcc.gnu.org	2005-10-05 19:35:46

Modified files:
	gcc/java       : ChangeLog decl.c java-tree.h jcf-write.c 
	                 parse.y 
	libjava        : ChangeLog 
	libjava/testsuite/libjava.jacks: jacks.xfail 
Added files:
	libjava/testsuite/libjava.lang: PR19870.java PR19870.out 
	                                PR19870_2.java PR19870_2.out 

Log message:
	gcc/java:
	
	2005-10-05  Ranjit Mathew  <rmathew@hotmail.com>
	
	PR java/20338
	* decl.c (finish_method): Emit _Jv_InitClass for private static
	methods inside inner classes as well.
	
	2005-10-05  Ranjit Mathew  <rmathew@hotmail.com>
	
	PR java/19870
	* parse.y (nested_field_access_p): Rename to nested_member_access_p
	and expand to handle method accesses across nested classes.
	(build_outer_method_access_method): Rename to
	build_nested_method_access_method.  Minor adjustments to comments.
	(resolve_expression_name): Use the newly-renamed
	nested_member_access_p method.
	(resolve_qualified_expression_name): Likewise.
	(patch_method_invocation): Also consider static methods for access
	method generation.  Minor adjustments to comments.
	(maybe_use_access_method): Use the more general
	nested_memeber_access_p to determine access across nested class
	boundaries.  Allow THIS_ARG to be NULL (for static methods).
	
	2005-10-05  Tom Tromey  <tromey@redhat.com>
	
	PR java/21844:
	* parse.y (nested_field_access_p): Handle case where outer field
	is inherited by enclosing class.
	
	2005-10-05  Ranjit Mathew  <rmathew@hotmail.com>
	
	PR java/19870.
	* java-tree.h (OUTER_FIELD_ACCESS_IDENTIFIER_P): Rename to
	NESTED_FIELD_ACCESS_IDENTIFIER_P.
	(FIELD_INNER_ACCESS): Rename to FIELD_NESTED_ACCESS.
	(FIELD_INNER_ACCESS_P): Rename to FIELD_NESTED_ACCESS_P.
	* jcf-write.c (generate_classfile): Use
	NESTED_FIELD_ACCESS_IDENTIFIER_P instead of
	OUTER_FIELD_ACCESS_IDENTIFIER_P.
	* parse.y (build_outer_field_access): Rename to
	build_nested_field_access. Support static fields and outer-to-inner
	class accesses.
	(outer_field_access_p): Rename to nested_field_access_p. Support
	static fields and generalise to outer-to-inner class and sibling
	inner class accesses.
	(outer_field_expanded_access_p): Rename to
	nested_field_expanded_access_p and support static fields.
	(outer_field_access_fix): Rename to nested_field_access_fix and
	support static fields.
	(build_outer_field_access_expr): Rename to
	build_nested_field_access_expr and support static fields.
	(build_outer_field_access_methods): Rename to
	build_nested_field_access_methods and support static fields. For
	static fields, generate accessors without class instance parameters.
	(build_outer_field_access_method): Rename to
	build_nested_field_access_method and support static fields.
	(build_outer_method_access_method): Use
	NESTED_FIELD_ACCESS_IDENTIFIER_P instead of
	OUTER_FIELD_ACCESS_IDENTIFIER_P.
	(resolve_expression_name): Consider static field accesses across
	nested classes.
	(resolve_qualified_expression_name): Likewise.
	(java_complete_lhs): Use nested_field_access_fix instead of
	outer_field_access_fix.
	(patch_unary_op): Rename outer_field_flag to nested_field_flag.
	Use nested_field_expanded_access_p instead of
	outer_field_expanded_access_p. Use nested_field_access_fix instead
	of outer_field_access_fix.
	(check_thrown_exceptions): Use NESTED_FIELD_ACCESS_IDENTIFIER_P
	instead of OUTER_FIELD_ACCESS_IDENTIFIER_P.
	
	libjava:
	
	2005-10-05  Ranjit Mathew  <rmathew@hotmail.com>
	
	More testsuite adjustments for PR java/19870.
	* testsuite/libjava.lang/PR19870_2.java: New testcase.
	* testsuite/libjava.lang/PR19870_2.out: Expected output for the
	new testcase.
	* testsuite/libjava.jacks/jacks.xfail: Remove
	8.5.2-non-static-member-usage-2 and add
	15.12.3-explicit-constructor-9.
	
	2005-10-05  Tom Tromey  <tromey@redhat.com>
	
	PR java/21844:
	* testsuite/libjava.lang/pr21844.java: New file.
	* testsuite/libjava.lang/pr21844.out: New file.
	
	2005-10-05  Ranjit Mathew  <rmathew@hotmail.com>
	
	Testsuite adjustments for PR java/19870.
	* testsuite/libjava.lang/PR19870.java: New testcase.
	* testsuite/libjava.lang/PR19870.out: Expected output for the
	testcase.
	* testsuite/libjava.jacks/jacks.xfail: Add
	8.5.2-accessible-static-member-usage-3 and 15.8.4-static-2

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.1556.2.34&r2=1.1556.2.35
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/decl.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.209.4.4&r2=1.209.4.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/java-tree.h.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.226.8.3&r2=1.226.8.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/jcf-write.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.162.4.1&r2=1.162.4.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/java/parse.y.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.528.6.4&r2=1.528.6.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.3391.2.103&r2=1.3391.2.104
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/libjava.jacks/jacks.xfail.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.19.8.2&r2=1.19.8.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/libjava.lang/PR19870.java.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.24.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/libjava.lang/PR19870.out.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.24.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/libjava.lang/PR19870_2.java.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.18.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libjava/testsuite/libjava.lang/PR19870_2.out.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.18.1

Comment 10 Andrew Pinski 2005-10-05 19:40:34 UTC
Also fixed in 4.0.3.