JDK-6915677 : Unsafe operations that access invalid memory are not handled gracefully
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 6u7
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_8
  • CPU: x86
  • Submitted: 2010-01-11
  • Updated: 2011-02-16
  • Resolved: 2010-09-23
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b3050)
Java HotSpot(TM) 64-Bit Server VM (build 11.0-b15, mixed mode)


FULL OS VERSION :
Linux lu0016 2.6.16.54-0.2.3-default #1 SMP Thu Nov 22 18:32:07 UTC 2007 ia64 ia64 ia64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
The VM crashes if Unsafe operations access invalid memory!

Notice: this is a copy of Bug 6244515 which has been resolved for Linux/x86, Linux/amd64 and Solaris. Unfortunately, the problem reappeared in the new Linux ia64 version of  the JVM.

For your convenience, I've copied the Error description of 6244515:

The specification of java.nio.MappedByteBuffer says:

    All or part of a mapped byte buffer may become inaccessible at any time,
    for example if the mapped file is truncated.  An attempt to access an
    inaccessible region of a mapped byte buffer will not change the buffer's
    content and will cause an unspecified exception to be thrown either at the
    time of the access or at some later time.  ...

This behavior was intended to be implemented in 1.40-beta2 by RFEs 4454110
(runtime), 4454113 (c1), and 4454115 (c2).  The implementation is incomplete on
solaris-sparc and totally absent on linux-i586; I haven't checked any other
os/arch combinations.

With 1.4.0-beta2 on solaris-sparc, the following program crashes the VM with
-Xint but throws the expected error ("java.lang.InternalError: a fault occurred
in an unsafe memory access operation") with -Xcomp -client and -Xcomp -server.
The same behavior was observed with 1.4.0-beta3, 1.4.0-fcs, 1.4.1, 1.4.2,
1.5.0, and 1.6.0-b28.

With 1.4.0-beta2 on linux-i586, the following program crashes the VM with
-Xint, with -Xcomp -client, and with -Xcomp -server.  The same behavior was
observed with 1.4.0-beta3, 1.4.0-fcs, 1.4.1, 1.4.2, 1.5.0, and 1.6.0-b28.

Note that the program must be edited slightly in order to be compiled with
1.4.0-beta2 due to later API changes.  It must also be recompiled from beta3
to FCS.  For convenience I've attached a tarball containing class files for
each version.


THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: Yes

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the class "Truncate".

EXPECTED VERSUS ACTUAL BEHAVIOR :
The program should print "Test Passed" and complete gracefully. However the VM crashes instead.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Attached seperatly


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/*
 * @test
 * @bug 6244515
 * @summary Unsafe operations that access invalid memory are handled gracefully
 *
 *   This test passes if it doesn't crash.  It should throw an exception.
 */
import java.io.*;
import java.nio.*;
import java.nio.channels.*;


public class Truncate {

    private static final int SIZE = 1 << 16;

    public static void main(String[] args) throws Exception {
      // This test case will not pass on Windows since Windows does not
      // support truncating file while it is still having user-mapped region
      // open. So we just return on Windows.
      if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
        System.out.println("Test Passed. Note that due to some platform dependent behaviours, this
test is not actually executed on Windows platform");
        return;
      }
      File tmpfile = File.createTempFile("zzz", null);
      try {
        FileChannel fc = new RandomAccessFile(tmpfile, "rw").getChannel();
        fc.position(SIZE);
        fc.write(ByteBuffer.allocate(1));
        MappedByteBuffer bb
            // Uncomment this line for 1.4.0 beta2
            //= fc.map(FileChannel.MAP_RW, 0, SIZE);
            // Uncomment this line for 1.4.0 beta3 or later
            = fc.map(FileChannel.MapMode.READ_WRITE, 0, SIZE);

        bb.put((byte)1).put((byte)2).put((byte)3).put((byte)4);
        fc.truncate(0);
        bb.put((byte)5);
        System.out.println("bb.put above got a bus error");
      } catch (InternalError e) {
        System.out.println("Test Passed");
      } finally {
        if (tmpfile != null) {
          tmpfile.delete();
        }
      }
    }

}

---------- END SOURCE ----------

Comments
EVALUATION It looks like a duplicate. Same test case too. I thought I'd fixed this but I guess not.
23-09-2010