Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlock fails with -1 #6723

Closed
abpISCUE opened this issue Mar 26, 2021 · 2 comments
Closed

mlock fails with -1 #6723

abpISCUE opened this issue Mar 26, 2021 · 2 comments

Comments

@abpISCUE
Copy link

Environment

Windows build number: Microsoft Windows [Version 10.0.19042.870]
Your Distribution version: 10
Whether the issue is on WSL 2 and/or WSL 1: Linux version 5.4.72-microsoft-standard-WSL2 (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Wed Oct 28 23:40:43 UTC 2020

Steps to reproduce

Eclipse project running in WSL, compiling with Eclipse, start executable in console or Eclipse,
the mlock command always returns -1:

#include <iostream>
#include <string.h>
#include <sys/mman.h>

static const size_t ALLOCATION_SIZE = (2 * 1024 * 1024);

int main() {
  std::cout << "Hello Test" << std::endl << std::flush;

  char* buffer = reinterpret_cast<char*>(malloc(ALLOCATION_SIZE));

  if (mlock(buffer, ALLOCATION_SIZE) == -1) {
    std::cout << "Locking memory failed: " << strerror(errno) << std::endl << std::flush;
  }
  return 0;
}

2619 execve("./Test", ["./Test"], 0x7ffe12f53fa8 /* 19 vars */) = 0
2619 brk(NULL) = 0x55d8240b7000
2619 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
2619 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
2619 fstat(3, {st_mode=S_IFREG|0644, st_size=82054, ...}) = 0
2619 mmap(NULL, 82054, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5a4a293000
2619 close(3) = 0
2619 openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
2619 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\304\10\0\0\0\0\0"..., 832) = 832
2619 fstat(3, {st_mode=S_IFREG|0644, st_size=1570256, ...}) = 0
2619 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5a4a291000
2619 mmap(NULL, 1585184, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5a4a10d000
2619 mprotect(0x7f5a4a196000, 962560, PROT_NONE) = 0
2619 mmap(0x7f5a4a196000, 704512, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x89000) = 0x7f5a4a196000
2619 mmap(0x7f5a4a242000, 253952, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x135000) = 0x7f5a4a242000
2619 mmap(0x7f5a4a281000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x173000) = 0x7f5a4a281000
2619 mmap(0x7f5a4a28d000, 12320, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f5a4a28d000
2619 close(3) = 0
2619 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
2619 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\322\0\0\0\0\0\0"..., 832) = 832
2619 fstat(3, {st_mode=S_IFREG|0644, st_size=1579448, ...}) = 0
2619 mmap(NULL, 1581384, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5a49f8a000
2619 mmap(0x7f5a49f97000, 651264, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd000) = 0x7f5a49f97000
2619 mmap(0x7f5a4a036000, 872448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xac000) = 0x7f5a4a036000
2619 mmap(0x7f5a4a10b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x180000) = 0x7f5a4a10b000
2619 close(3) = 0
2619 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
2619 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\3402\0\0\0\0\0\0"..., 832) = 832
2619 fstat(3, {st_mode=S_IFREG|0644, st_size=100712, ...}) = 0
2619 mmap(NULL, 103472, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5a49f70000
2619 mprotect(0x7f5a49f73000, 86016, PROT_NONE) = 0
2619 mmap(0x7f5a49f73000, 69632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f5a49f73000
2619 mmap(0x7f5a49f84000, 12288, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x14000) = 0x7f5a49f84000
2619 mmap(0x7f5a49f88000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x17000) = 0x7f5a49f88000
2619 close(3) = 0
2619 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
2619 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260A\2\0\0\0\0\0"..., 832) = 832
2619 fstat(3, {st_mode=S_IFREG|0755, st_size=1824496, ...}) = 0
2619 mmap(NULL, 1837056, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5a49daf000
2619 mprotect(0x7f5a49dd1000, 1658880, PROT_NONE) = 0
2619 mmap(0x7f5a49dd1000, 1343488, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x22000) = 0x7f5a49dd1000
2619 mmap(0x7f5a49f19000, 311296, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16a000) = 0x7f5a49f19000
2619 mmap(0x7f5a49f66000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b6000) = 0x7f5a49f66000
2619 mmap(0x7f5a49f6c000, 14336, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f5a49f6c000
2619 close(3) = 0
2619 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5a49dad000
2619 arch_prctl(ARCH_SET_FS, 0x7f5a49dadf40) = 0
2619 mprotect(0x7f5a49f66000, 16384, PROT_READ) = 0
2619 mprotect(0x7f5a49f88000, 4096, PROT_READ) = 0
2619 mprotect(0x7f5a4a10b000, 4096, PROT_READ) = 0
2619 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5a49dab000
2619 mprotect(0x7f5a4a281000, 40960, PROT_READ) = 0
2619 mprotect(0x55d823827000, 4096, PROT_READ) = 0
2619 mprotect(0x7f5a4a2cf000, 4096, PROT_READ) = 0
2619 munmap(0x7f5a4a293000, 82054) = 0
2619 brk(NULL) = 0x55d8240b7000
2619 brk(0x55d8240d8000) = 0x55d8240d8000
2619 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0x4), ...}) = 0
2619 write(1, "Hello Test\n", 11) = 11
2619 mmap(NULL, 2101248, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5a49baa000
2619 mlock(0x7f5a49baa010, 2097152) = -1 ENOMEM (Cannot allocate memory)
2619 write(1, "Locking memory failed: Cannot al"..., 46) = 46
2619 exit_group(0) = ?
2619 +++ exited with 0 +++

WSL logs:
wsl_log.zip

Expected behavior

mlock does function as expected, locking memory without failure.

Actual behavior

mlock always fails, returning -1.

@therealkenc
Copy link
Collaborator

On WSL2, it looks like the default max locked memory is 64kb. This is similar in spirit to #4575 but for locked memory instead of open files. On a stock Ubuntu on bare metal, the default limit is higher. The max locked memory can be increased with:

$ limit=8413752832
$ sudo prlimit --memlock=$limit:$limit --pid $$

image

@abpISCUE
Copy link
Author

That did the trick, thank you :)

Remark: you have to add these lines to .bashrc to run program in eclipse; simply starting eclipse in a console after setting the memlock limit, does not suffice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants