WSL: mmap(NULL, ...) on zero-length file fails with ENOEXEC

  • Your Windows build number: (Type ver at a Windows Command Prompt)

April Update, 10.0.17134.191 Also tested on 19H1, build 18204

  • What you’re doing and what’s happening: (Copy&paste specific commands and their output, or include screen shots)

The package “liblmdb” does not work on WSL. This impacts any projects that use lmdb. I’ve narrowed this down to mmap(NULL,…) on a 0-length file failing with ENOEXEC. This is likely related to #658 , yet distinct enough that the @therealkenc suggested I file a new issue, to avoid confusion.

To reproduce with lmdb:

git clone https://github.com/LMDB/lmdb.git cd lmdb/libraries/liblmdb make test

mtest.c:50: mdb_env_open(env, “./testdb”, MDB_FIXEDMAP , 0664): Exec format error Aborted (core dumped) Makefile:61: recipe for target ‘test’ failed make: *** [test] Error 134

strace shows:

mmap(NULL, 10485760, PROT_READ, MAP_SHARED, 4, 0) = -1 ENOEXEC (Exec format error)

Just before it cores.

Sample code showing just the ENOEXEC, but not going on long enough after that to core:

#include <stddef.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

int main (void) {
  void *map;
  int fd;

// mmap an empty file, fails with ENOEXEC
// Test case to recreate an issue with lmdb and WSL
// mmap(NULL, 10485760, PROT_READ, MAP_SHARED, 4, 0) = -1 ENOEXEC (Exec format error)
  remove("./testfile");
  fd = open("./testfile", O_RDWR | O_CREAT, 0664);

  map = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    printf("mmap(NULL,...) of empty file failed, strace shows ENOEXEC\n");
  else {
    printf("mmap(NULL,...) of empty file succeeded, issue fixed\n");
    munmap(map, 1024);
  }

  close(fd);
  remove("./testfile");

// By contrast, with a file that contains some data
  fd = open("./testfile", O_RDWR | O_CREAT, 0664);
  write(fd,"Content",7);

  map = mmap(NULL, 1024, PROT_READ, MAP_SHARED, fd, 0);
  if (map == MAP_FAILED)
    printf("mmap(NULL,...) of non-empty file failed, that's unexpected\n");
  else {
    printf("mmap(NULL,...) of non-empty file succeeded, as expected\n");
    munmap(map, 1024);
  }

  close(fd);
  remove("./testfile");

  return 0;
}
  • Strace of the failing command, if applicable:

strace of the sample code: mmap-example.strace.txt

strace of liblmdb itself: liblmdb.strace.txt

User voice for this issue lives here: https://wpdev.uservoice.com/forums/266908-command-prompt-console-windows-subsystem-for-l/suggestions/35053207-support-oracle-berkeley-db-and-lmdb

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 16
  • Comments: 19 (3 by maintainers)

Most upvoted comments

The major use case for “mmap’s problems” is BerkeleyDB. Instead of deep diving lmdb (or boltdb #2304 #3162 or foo-db) you may consider:

sudo apt install tcl tcl-dev   # plus the usual suspects
mkdir bdb && cd bdb
wget http://download.oracle.com/berkeley-db/db-5.3.21.tar.gz
tar xf db-5.3.21.tar.gz
cd db-5.3.21/build_unix
../dist/configure --prefix=/usr/local --enable-compat185 --enable-dbm \
    --disable-static --enable-test --enable-debug --enable-cxx \
    --enable-tcl --with-tcl=/usr/lib
make -j4
tclsh  # in tickle shell after this, most unfortunately
source ../test/tcl/test.tcl  
# run_std     # worthy goal but it's a big test
# r btree     # more reasonable but still a big ask
test042 btree # first random fail (segfault) I hit on WSL, fine on Real Linux

There is some information on running the individual tests here. If you get the BerkeleyDB tests to pass it will squash #3149 #2627 #2852 #2871 (and everything else in the BDB universe). The other foo-db will almost certainly follow suit.

This issue has been automatically closed since it has not had any activity for the past year. If you’re still experiencing this issue please re-file this as a new issue or feature request.

Thank you!