Skip to content

Commit

Permalink
SPTCH-3534: Use already loaded version of JSCoreGTK if available
Browse files Browse the repository at this point in the history
  • Loading branch information
nmoinvaz committed Dec 9, 2024
1 parent 6c6f61c commit 7b8fabf
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions execute_jscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <string.h>

#include <dlfcn.h>
#ifndef __APPLE__
# include <link.h>
#endif

#include <JavaScriptCore/JavaScript.h>

Expand Down Expand Up @@ -376,13 +379,32 @@ bool proxy_execute_jscore_global_init(void) {
g_proxy_execute_jscore.module = dlopen(
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/JavaScriptCore", RTLD_LAZY | RTLD_LOCAL);
#else
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-4.1.so.0", RTLD_LAZY | RTLD_LOCAL);
if (!g_proxy_execute_jscore.module)
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-4.0.so.18", RTLD_LAZY | RTLD_LOCAL);
if (!g_proxy_execute_jscore.module)
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-3.0.so.0", RTLD_LAZY | RTLD_LOCAL);
if (!g_proxy_execute_jscore.module)
g_proxy_execute_jscore.module = dlopen("libjavascriptcoregtk-1.0.so.0", RTLD_LAZY | RTLD_LOCAL);
const char *library_names[] = {"libjavascriptcoregtk-4.1.so.0", "libjavascriptcoregtk-4.0.so.18",
"libjavascriptcoregtk-3.0.so.0", "libjavascriptcoregtk-1.0.so.0"};
const size_t library_names_size = sizeof(library_names) / sizeof(library_names[0]);

// Use existing JavaScriptCoreGTK if already loaded
struct link_map *map = NULL;
void *current_process = dlopen(0, RTLD_LAZY);
if (!current_process)
return false;

Check warning on line 390 in execute_jscore.c

View check run for this annotation

Codecov / codecov/patch

execute_jscore.c#L390

Added line #L390 was not covered by tests
if (dlinfo(current_process, RTLD_DI_LINKMAP, &map) == 0) {
while (map && !g_proxy_execute_jscore.module) {
for (size_t i = 0; i < library_names_size; i++) {
if (strstr(map->l_name, library_names[i])) {
g_proxy_execute_jscore.module = dlopen(map->l_name, RTLD_NOLOAD | RTLD_LAZY | RTLD_LOCAL);
break;

Check warning on line 396 in execute_jscore.c

View check run for this annotation

Codecov / codecov/patch

execute_jscore.c#L395-L396

Added lines #L395 - L396 were not covered by tests
}
}
map = map->l_next;
}
}
dlclose(current_process);

// Load the first available version of the JavaScriptCoreGTK
for (size_t i = 0; !g_proxy_execute_jscore.module && i < library_names_size; i++) {
g_proxy_execute_jscore.module = dlopen(library_names[i], RTLD_LAZY | RTLD_LOCAL);
}
#endif

if (!g_proxy_execute_jscore.module)
Expand Down

0 comments on commit 7b8fabf

Please sign in to comment.