C – Test my software with a file system that does not support mmap

Test my software with a file system that does not support mmap… here is a solution to the problem.

Test my software with a file system that does not support mmap

I have a piece of code trying to mmap some files. If it can mmap files, it will do some things, if it can’t mmap, it will do other things.

The code works in both cases, but I’d like to do some testing in some filesystems that don’t support mmap. The problem is that I didn’t find any filesystems that couldn’t do mmap.

Can anyone point to some file systems that don’t support mmap?

Solution

You can use library insertion to simulate systems that do not support mmap. Just take this C file

#include <errno.h>
#include <sys/mman.h>
void* mmap(void*, size_t, int, int, int, off_t) {
  errno = ENODEV;
  return NULL;
}

and compile it into a shared library. Name the path to the library in the LD_PRELOAD environment variable, which should take precedence over real mmap, simulating a system where mmap always fails. This way, you can test your code without super user rights, without creating a specific file system, without using the corresponding kernel modules, userspace tools, etc.

Theoretically, you might run into situations where some libraries beyond your control depend on a particular type of mmap to always work. A mapping with MAP_ANONYMOUS is a prime example because it is not supported by the file system and therefore does not depend on the FS type. If you encounter any issues in the library that fail due to violating mmap assumptions, you may have to modify the inserter to take a closer look at its parameters and forward some calls to the libc implementation while rejecting the others themselves. But I only do this when I actually need to.

Related Problems and Solutions