Skip to content

Commit 3d7e92b

Browse files
committed
luzer: refactoring search_module_path()
The luzer module relies on other shared libraries: library with custom mutator and sanitizer libraries built with libFuzzer. These libraries are located in the directory with luzer_impl.so and on module loading we search a path to this shared library to found other shared libraries. It is done by the function `search_module_path()` - it iterates through the directories specified in the environment variable LUA_CPATH and tries to find the directory with luzer_impl.so. However, there's a simpler way to find a directory path with luzer_impl.so - using the `dladdr(3)` function. The patch refactors the function `search_module_path()` to make its implementation easier for support. Related to #67
1 parent 28b23b2 commit 3d7e92b

File tree

1 file changed

+16
-30
lines changed

1 file changed

+16
-30
lines changed

luzer/luzer.c

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,20 @@ get_coverage_symbols_location(void) {
141141
const char *dso_path_lf_asan;
142142
const char *dso_path_lf_ubsan;
143143
const char *dso_path_libcustom_mutator;
144-
/* struct paths luzer_paths; */
145144

146-
NO_SANITIZE static int
147-
search_module_path(char *so_path, const char *so_name, size_t len) {
148-
/* Create a copy, because `strsep()` below mutates a string. */
149-
char *lua_cpath = strdup(getenv("LUA_CPATH"));
150-
if (!lua_cpath)
151-
lua_cpath = "./";
152-
char *stringp = lua_cpath;
153-
int rc = -1;
154-
char *cpath = NULL;
155-
while ((cpath = strsep(&stringp, ";")) != NULL) {
156-
const char *dir = dirname(cpath);
157-
snprintf(so_path, len, "%s/%s", dir, so_name);
158-
if (access(so_path, F_OK) == 0) {
159-
rc = 0;
160-
strcpy(so_path, cpath);
161-
free(lua_cpath);
162-
break;
163-
}
145+
NO_SANITIZE const char *
146+
search_module_path(char *base_path) {
147+
Dl_info dlinfo;
148+
int rc = dladdr((void *)&get_coverage_symbols_location, &dlinfo);
149+
if (!rc)
150+
return NULL;
151+
char *path = realpath(dlinfo.dli_fname, base_path);
152+
if (!path) {
153+
perror("realpath");
154+
return NULL;
164155
}
165-
return rc;
156+
const char *dir = dirname(path);
157+
return dir;
166158
}
167159

168160
NO_SANITIZE void
@@ -183,16 +175,12 @@ init(void)
183175
"from this library: %s\n", get_coverage_symbols_location());
184176
}
185177

186-
char path[PATH_MAX];
187-
int rc = search_module_path(path, CUSTOM_MUTATOR_LIB, PATH_MAX);
188-
if (rc) {
189-
fprintf(stderr, "%s is not found\n", CUSTOM_MUTATOR_LIB);
178+
char base_path[PATH_MAX], path[PATH_MAX];
179+
const char *base_so_path = search_module_path(base_path);
180+
if (!base_so_path) {
181+
fprintf(stderr, "something goes wrong\n");
190182
return;
191183
}
192-
char *base_so_path = realpath((const char *)&path, NULL);
193-
if (!base_so_path)
194-
perror("realpath");
195-
memset(&path, 0, PATH_MAX);
196184

197185
snprintf(path, PATH_MAX, "%s/%s", base_so_path, CUSTOM_MUTATOR_LIB);
198186
dso_path_libcustom_mutator = strdup(path);
@@ -208,8 +196,6 @@ init(void)
208196
dso_path_lf_ubsan = strdup(path);
209197
if (access(dso_path_lf_ubsan, F_OK))
210198
perror("access");
211-
212-
free(base_so_path);
213199
}
214200

215201
NO_SANITIZE static void

0 commit comments

Comments
 (0)