Bug 1346235 - part 1: translate native error to Java exception. r=jchen
authorJohn Lin <jolin@mozilla.com>
Fri, 17 Mar 2017 14:21:27 +0800
changeset 348401 fa8084f4f200ebb0b1649287c583e60cfa9198f3
parent 348400 f7541b903b1565a6274627e2da19e6d780ba2559
child 348402 639a9dcab2bbef7d2ffe8b888a595145242ec1b4
push id39149
push userjolin@mozilla.com
push dateMon, 20 Mar 2017 11:18:00 +0000
treeherderautoland@c4f7d0039351 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjchen
bugs1346235
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1346235 - part 1: translate native error to Java exception. r=jchen 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
+}