Bug 1346235 - Part 1: Translate native error to Java exception. r=jchen, a=gchang
authorJohn Lin <jolin@mozilla.com>
Fri, 17 Mar 2017 14:21:27 +0800
changeset 393527 fb9caf2d4e40ef97ac58edd9e6674109469283c3
parent 393526 567d5c33be379ef8d206e3b64afec4a6a92920d6
child 393528 14fd7f066eb943604aa8d43cd8758f208617003d
push id7198
push userjlorenzo@mozilla.com
push dateTue, 18 Apr 2017 12:07:49 +0000
treeherdermozilla-beta@d57aa49c3948 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen, gchang
bugs1346235
milestone54.0a2
Bug 1346235 - Part 1: Translate native error to Java exception. r=jchen, a=gchang MozReview-Commit-ID: BcpfJLSzTvk
mobile/android/base/java/org/mozilla/gecko/mozglue/SharedMemory.java
mozglue/android/SharedMemNatives.cpp
--- a/mobile/android/base/java/org/mozilla/gecko/mozglue/SharedMemory.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mozglue/SharedMemory.java
@@ -128,17 +128,22 @@ public class SharedMemory implements Par
     }
 
     public long getPointer() {
         if (!isValid()) {
             return 0;
         }
 
         if (!mIsMapped) {
-            mHandle = map(getFD(), mSize);
+            try {
+                mHandle = map(getFD(), mSize);
+            } catch (NullPointerException e) {
+                Log.e(LOGTAG, "SharedMemory#" + mId + " error.", e);
+                throw e;
+            }
             if (mHandle != 0) {
                 mIsMapped = true;
             }
         }
         return mHandle;
     }
 
     private native long map(int fd, int size);
--- a/mozglue/android/SharedMemNatives.cpp
+++ b/mozglue/android/SharedMemNatives.cpp
@@ -1,14 +1,16 @@
 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include <errno.h>
 #include <jni.h>
+#include <stdio.h>
 #include <string.h>
 #include <sys/mman.h>
 
 extern "C" {
 
 JNIEXPORT
 void JNICALL
 Java_org_mozilla_gecko_mozglue_SharedMemBuffer_nativeReadFromDirectBuffer(JNIEnv* jenv, jclass, jobject src, jlong dest, jint offset, jint size)
@@ -47,19 +49,25 @@ Java_org_mozilla_gecko_mozglue_SharedMem
   memcpy(to, from + offset, size);
 }
 
 JNIEXPORT
 jlong JNICALL
 Java_org_mozilla_gecko_mozglue_SharedMemory_map(JNIEnv *env, jobject jobj, jint fd, jint length)
 {
   void* address = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+  if (address == MAP_FAILED) {
+    char msg[128];
+    snprintf(msg, sizeof(msg), "mmap failed. errno=%d", errno);
+    env->ThrowNew(env->FindClass("java/lang/NullPointerException"), msg);
+    return 0;
+  }
   return jlong(address);
 }
 
 JNIEXPORT
 void JNICALL
 Java_org_mozilla_gecko_mozglue_SharedMemory_unmap(JNIEnv *env, jobject jobj, jlong address, jint size)
 {
   munmap((void*)address, (size_t)size);
 }
 
-}
\ No newline at end of file
+}