Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9fc2e9c9 authored by Bjorn Bringert's avatar Bjorn Bringert
Browse files

MemoryFile constructor and native methods throw IOExceptions.

These native methods in android.os.MemoryFile throw IOException but their
Java declarations did not include "throws IOException":
native_open(),native_mmap(),native_read(),native_write(),native_pin()

The MemoryFile(String,int) constructor calls native_open and
native_mmap, but does not declare that it throws IOException. The other
Java methods that call the native methods do actually declare that they
throw IOException.

This means that any code that created memory files could throw
an IOException, without knowing about it.

This changes adds "throws IOException" to the native methods and to
the constructor. The constructor change changes the public API, but
maintains binary compatibility. There is some precedent for making
source incompatible source API changes for this sort of thing
(see https://mondrian.corp.google.com/changelist/124214-p9).

The change also makes the native methods static, which
they seem to have been intended to be, as indicated by the
second parameter to the native implementations being named
"clazz".

This requires changes to the Compatibility Test Suite to catch the exceptions.
This is done in https://android-git.corp.google.com/g/2617
Unfortunately that change must be submitted together with this one in order
not to break the build.

Fixes http://b/issue?id=1881829
parent 607384d4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -93802,6 +93802,8 @@
</parameter>
<parameter name="length" type="int">
</parameter>
<exception name="IOException" type="java.io.IOException">
</exception>
</constructor>
<method name="allowPurging"
 return="boolean"
+10 −9
Original line number Diff line number Diff line
@@ -37,15 +37,15 @@ public class MemoryFile
    private static String TAG = "MemoryFile";
 
    // returns fd
    private native int native_open(String name, int length);
    private static native int native_open(String name, int length) throws IOException;
    // returns memory address for ashmem region
    private native int native_mmap(int fd, int length);
    private native void native_close(int fd);
    private native int native_read(int fd, int address, byte[] buffer, 
            int srcOffset, int destOffset, int count, boolean isUnpinned);
    private native void native_write(int fd, int address, byte[] buffer, 
            int srcOffset, int destOffset, int count, boolean isUnpinned);
    private native void native_pin(int fd, boolean pin);
    private static native int native_mmap(int fd, int length) throws IOException;
    private static native void native_close(int fd);
    private static native int native_read(int fd, int address, byte[] buffer,
            int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
    private static native void native_write(int fd, int address, byte[] buffer,
            int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
    private static native void native_pin(int fd, boolean pin) throws IOException;

    private int mFD;        // ashmem file descriptor
    private int mAddress;   // address of ashmem memory
@@ -57,8 +57,9 @@ public class MemoryFile
     *
     * @param name optional name for the file (can be null).
     * @param length of the memory file in bytes.
     * @throws IOException if the memory file could not be created.
     */
    public MemoryFile(String name, int length) {
    public MemoryFile(String name, int length) throws IOException {
        mLength = length;
        mFD = native_open(name, length);
        mAddress = native_mmap(mFD, length);