Fixed file descriptor leak in IMemory

Include dup in critical section to avoid multiple dup calls from
different threads.
The leak happens more frequently since patch in frameworks/av -
6773d4777f4ccbbe6377e4ae1b42c117066ae6ba when recording video since it
will access IMemory from CameraUtils::isNativeHandleMetadata

Bug: 28924660
Author: Henrik Antonsson <henrik.antonsson@sonymobile.com>
Change-Id: I769a914c3adc505b522659a0c43719fb2922e6fd
diff --git a/libs/binder/IMemory.cpp b/libs/binder/IMemory.cpp
index fb8d620..5f345cf 100644
--- a/libs/binder/IMemory.cpp
+++ b/libs/binder/IMemory.cpp
@@ -312,17 +312,17 @@
                 IInterface::asBinder(this).get(),
                 parcel_fd, size, err, strerror(-err));
 
-        int fd = dup( parcel_fd );
-        ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%zd, err=%d (%s)",
-                parcel_fd, size, err, strerror(errno));
-
-        int access = PROT_READ;
-        if (!(flags & READ_ONLY)) {
-            access |= PROT_WRITE;
-        }
-
         Mutex::Autolock _l(mLock);
         if (mHeapId == -1) {
+            int fd = dup( parcel_fd );
+            ALOGE_IF(fd==-1, "cannot dup fd=%d, size=%zd, err=%d (%s)",
+                    parcel_fd, size, err, strerror(errno));
+
+            int access = PROT_READ;
+            if (!(flags & READ_ONLY)) {
+                access |= PROT_WRITE;
+            }
+
             mRealHeap = true;
             mBase = mmap(0, size, access, MAP_SHARED, fd, offset);
             if (mBase == MAP_FAILED) {