Skip to content

Commit

Permalink
Use DEEPBIND flag when loading external modules using dlopen (#1703)
Browse files Browse the repository at this point in the history
The current flags used in `dlopen` to load external modules don't allow
modules to define symbols that are already defined by the valkey server
binary because the symbol resolution first looks in the server memory
and only if it does not find anything, it looks in the module (shared
library) memory.

This might become a problem if, for instance, we try to implement a new
scripting engine based on a newer version of Lua. The Lua interpreter
library shares many symbol names with the Lua interpreter included in
the Valkey server binary.

To fix the above problem, this PR adds the flag `RTLD_DEEPBIND` to the
flags used in `dlopen` on systems that support it, which changes the
symbol resolution strategy to look for the symbol in the module memory
first, if the code executing is from the module.

---------

Signed-off-by: Ricardo Dias <ricardo.dias@percona.com>
  • Loading branch information
rjd15372 authored Feb 10, 2025
1 parent 99e2dc9 commit 244c570
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12284,7 +12284,18 @@ int moduleLoad(const char *path, void **module_argv, int module_argc, int is_loa
}
}

handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
int dlopen_flags = RTLD_NOW | RTLD_LOCAL;
#if (defined(__linux__) || defined(__FreeBSD__)) && !defined(__SANITIZE_ADDRESS__)
/* RTLD_DEEPBIND, which is required for loading modules that contains the
* same symbols, does not work with ASAN. Therefore, we exclude
* RTLD_DEEPBIND when doing test builds with ASAN.
* See https://github.com/google/sanitizers/issues/611 for more details.
*
* This flag is also currently only available in Linux and FreeBSD. */
dlopen_flags |= RTLD_DEEPBIND;
#endif

handle = dlopen(path, dlopen_flags);
if (handle == NULL) {
serverLog(LL_WARNING, "Module %s failed to load: %s", path, dlerror());
return C_ERR;
Expand Down

0 comments on commit 244c570

Please sign in to comment.