Allow locking nullptr if the length is 0
authorChristopher Dykes <cdykes@fb.com>
Thu, 25 Aug 2016 21:52:05 +0000 (14:52 -0700)
committerFacebook Github Bot 9 <facebook-github-bot-9-bot@fb.com>
Thu, 25 Aug 2016 21:53:36 +0000 (14:53 -0700)
Summary:
Because apparently this is valid (and we test it).
This also adds a check in mmap to make sure we aren't passing an invalid handle to `MapViewOfFileEx`.

Reviewed By: yfeldblum

Differential Revision: D3772853

fbshipit-source-id: 11593997a3fb12b7b391c5e52661060b71341aef

folly/portability/SysMman.cpp
folly/portability/SysMman.h

index 9827f045dd6731d8999d87937880bce1e18678ad..38d56ae3bf2be6b20e18046302edad804c9c8d69 100755 (executable)
@@ -64,6 +64,13 @@ int madvise(const void* addr, size_t len, int advise) {
 }
 
 int mlock(const void* addr, size_t len) {
+  // For some strange reason, it's allowed to
+  // lock a nullptr as long as length is zero.
+  // VirtualLock doesn't allow it, so handle
+  // it specially.
+  if (addr == nullptr && len == 0) {
+    return 0;
+  }
   if (!VirtualLock((void*)addr, len)) {
     return -1;
   }
@@ -106,6 +113,9 @@ void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t off) {
         (DWORD)((length >> 32) & 0xFFFFFFFF),
         (DWORD)(length & 0xFFFFFFFF),
         nullptr);
+    if (fmh == nullptr) {
+      return MAP_FAILED;
+    }
     ret = MapViewOfFileEx(
         fmh,
         accessFlags,
@@ -148,6 +158,10 @@ int mprotect(void* addr, size_t size, int prot) {
 }
 
 int munlock(const void* addr, size_t length) {
+  // See comment in mlock
+  if (addr == nullptr && length == 0) {
+    return 0;
+  }
   if (!VirtualUnlock((void*)addr, length)) {
     return -1;
   }
index 52dd33c829d3e71ce5894b950ec3d05bd9a980b6..907c6778d8b7abe068b6f2b20d67e0a826ee3488 100755 (executable)
@@ -44,6 +44,7 @@
 #define PROT_WRITE 2
 #define PROT_EXEC 4
 
+#define MADV_NORMAL 0
 #define MADV_DONTNEED 0
 #define MADV_SEQUENTIAL 0