Skip to content

Commit ac8432c

Browse files
committed
luzer: refactoring getting symbols location
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 introduce a function `get_symbol_path()` that replaces the functions `get_libFuzzer_symbols_location()`, `get_coverage_symbols_location()`, `search_module_path()` and makes implementation easier for support. The patch also renames `base_so_path` to `base_dir`. Related to #67 Needed for #59
1 parent f1b521e commit ac8432c

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

luzer/luzer.c

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -120,50 +120,30 @@ __sanitizer_print_stack_trace(void)
120120
} /* extern "C" */
121121
#endif
122122

123-
NO_SANITIZE const char *
124-
get_libFuzzer_symbols_location(void) {
123+
NO_SANITIZE char *
124+
get_symbol_path(void *addr) {
125125
Dl_info dl_info;
126-
if (!dladdr((void*)&LLVMFuzzerRunDriver, &dl_info)) {
127-
return "<Not a shared object>";
126+
if (dladdr(addr, &dl_info)) {
127+
if (!dl_info.dli_sname)
128+
return NULL;
129+
} else {
130+
/*
131+
* The specified address in `addr` could not be matched to
132+
* a shared object.
133+
*/
134+
return NULL;
128135
}
129-
return (dl_info.dli_fname);
130-
}
131-
132-
NO_SANITIZE const char *
133-
get_coverage_symbols_location(void) {
134-
Dl_info dl_info;
135-
if (!dladdr((void*)&__sanitizer_cov_8bit_counters_init, &dl_info)) {
136-
return "<Not a shared object>";
136+
char *path = realpath(dl_info.dli_fname, NULL);
137+
if (!path) {
138+
perror("realpath");
139+
return NULL;
137140
}
138-
return (dl_info.dli_fname);
141+
return path;
139142
}
140143

141144
const char *dso_path_lf_asan;
142145
const char *dso_path_lf_ubsan;
143146
const char *dso_path_libcustom_mutator;
144-
/* struct paths luzer_paths; */
145-
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-
}
164-
}
165-
return rc;
166-
}
167147

168148
NO_SANITIZE void
169149
init(void)
@@ -175,41 +155,44 @@ init(void)
175155
assert(NULL);
176156
}
177157

178-
if (strcmp(get_coverage_symbols_location(), get_libFuzzer_symbols_location()) != 0) {
158+
char *coverage_symbols_location =
159+
get_symbol_path((void*)&__sanitizer_cov_8bit_counters_init);
160+
char *libFuzzer_symbols_location =
161+
get_symbol_path((void*)&LLVMFuzzerRunDriver);
162+
if (libFuzzer_symbols_location && coverage_symbols_location &&
163+
strcmp(libFuzzer_symbols_location, coverage_symbols_location) != 0) {
179164
fprintf(stderr,
180165
"WARNING: Coverage symbols are being provided by a library other than "
181166
"libFuzzer. This will result in a broken Lua code coverage and "
182167
"severely impacted native extension code coverage. Symbols are coming "
183-
"from this library: %s\n", get_coverage_symbols_location());
168+
"from this library: %s\n", coverage_symbols_location);
184169
}
170+
free(coverage_symbols_location);
171+
free(libFuzzer_symbols_location);
185172

186173
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);
174+
char *base_path = get_symbol_path((void *)&get_symbol_path);
175+
if (!base_path) {
190176
return;
191177
}
192-
char *base_so_path = realpath((const char *)&path, NULL);
193-
if (!base_so_path)
194-
perror("realpath");
195-
memset(&path, 0, PATH_MAX);
178+
const char *base_dir = dirname(base_path);
196179

197-
snprintf(path, PATH_MAX, "%s/%s", base_so_path, CUSTOM_MUTATOR_LIB);
180+
snprintf(path, PATH_MAX, "%s/%s", base_dir, CUSTOM_MUTATOR_LIB);
198181
dso_path_libcustom_mutator = strdup(path);
199182
if (access(dso_path_libcustom_mutator, F_OK))
200183
perror("access");
201184

202-
snprintf(path, PATH_MAX, "%s/%s", base_so_path, dso_asan_string());
185+
snprintf(path, PATH_MAX, "%s/%s", base_dir, dso_asan_string());
203186
dso_path_lf_asan = strdup(path);
204187
if (access(dso_path_lf_asan, F_OK))
205188
perror("access");
206189

207-
snprintf(path, PATH_MAX, "%s/%s", base_so_path, dso_ubsan_string());
190+
snprintf(path, PATH_MAX, "%s/%s", base_dir, dso_ubsan_string());
208191
dso_path_lf_ubsan = strdup(path);
209192
if (access(dso_path_lf_ubsan, F_OK))
210193
perror("access");
211194

212-
free(base_so_path);
195+
free(base_path);
213196
}
214197

215198
NO_SANITIZE static void

0 commit comments

Comments
 (0)