Skip to content

Commit

Permalink
Use DEEPBIND flag when loading external modules using dlopen
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`, 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 committed Feb 10, 2025
1 parent 3828936 commit b7b8618
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -12274,7 +12274,16 @@ 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;
#ifndef __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.*/
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 b7b8618

Please sign in to comment.