Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: potential crash caused by dlopen different gtk libraries #33812

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 34 additions & 25 deletions BUILD.gn
Expand Up @@ -43,6 +43,7 @@ if (is_mac) {

if (is_linux) {
import("//build/config/linux/pkg_config.gni")
import("//tools/generate_stubs/rules.gni")

pkg_config("gio_unix") {
packages = [ "gio-unix-2.0" ]
Expand All @@ -54,6 +55,38 @@ if (is_linux) {
"gdk-pixbuf-2.0",
]
}

generate_library_loader("libnotify_loader") {
name = "LibNotifyLoader"
output_h = "libnotify_loader.h"
output_cc = "libnotify_loader.cc"
header = "<libnotify/notify.h>"
config = ":libnotify_config"

functions = [
"notify_is_initted",
"notify_init",
"notify_get_server_caps",
"notify_get_server_info",
"notify_notification_new",
"notify_notification_add_action",
"notify_notification_set_image_from_pixbuf",
"notify_notification_set_timeout",
"notify_notification_set_urgency",
"notify_notification_set_hint_string",
"notify_notification_show",
"notify_notification_close",
]
}

generate_stubs("electron_gtk_stubs") {
sigs = [ "shell/browser/ui/electron_gtk.sigs" ]
extra_header = "shell/browser/ui/electron_gtk.fragment"
output_name = "electron_gtk_stubs"
public_deps = [ "//ui/gtk:gtk_config" ]
logging_function = "LogNoop()"
logging_include = "ui/gtk/log_noop.h"
}
}

declare_args() {
Expand Down Expand Up @@ -253,31 +286,6 @@ copy("copy_shell_devtools_discovery_page") {
outputs = [ "$target_gen_dir/shell_devtools_discovery_page.html" ]
}

if (is_linux) {
generate_library_loader("libnotify_loader") {
name = "LibNotifyLoader"
output_h = "libnotify_loader.h"
output_cc = "libnotify_loader.cc"
header = "<libnotify/notify.h>"
config = ":libnotify_config"

functions = [
"notify_is_initted",
"notify_init",
"notify_get_server_caps",
"notify_get_server_info",
"notify_notification_new",
"notify_notification_add_action",
"notify_notification_set_image_from_pixbuf",
"notify_notification_set_timeout",
"notify_notification_set_urgency",
"notify_notification_set_hint_string",
"notify_notification_show",
"notify_notification_close",
]
}
}

npm_action("electron_version_args") {
script = "generate-version-json"

Expand Down Expand Up @@ -535,6 +543,7 @@ source_set("electron_lib") {
if (is_linux) {
libs = [ "xshmfence" ]
deps += [
":electron_gtk_stubs",
":libnotify_loader",
"//build/config/linux/gtk",
"//dbus",
Expand Down
2 changes: 1 addition & 1 deletion patches/chromium/.patches
Expand Up @@ -66,7 +66,6 @@ feat_enable_offscreen_rendering_with_viz_compositor.patch
gpu_notify_when_dxdiag_request_fails.patch
feat_allow_embedders_to_add_observers_on_created_hunspell.patch
feat_add_onclose_to_messageport.patch
ui_gtk_public_header.patch
allow_in-process_windows_to_have_different_web_prefs.patch
refactor_expose_cursor_changes_to_the_webcontentsobserver.patch
crash_allow_setting_more_options.patch
Expand Down Expand Up @@ -115,3 +114,4 @@ build_disable_partition_alloc_on_mac.patch
fix_non-client_mouse_tracking_and_message_bubbling_on_windows.patch
remove_incorrect_width_height_adjustments.patch
introduce_ozoneplatform_electron_can_call_x11_property.patch
make_gtk_getlibgtk_public.patch
52 changes: 52 additions & 0 deletions patches/chromium/make_gtk_getlibgtk_public.patch
@@ -0,0 +1,52 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: deepak1556 <hop2deep@gmail.com>
Date: Thu, 7 Apr 2022 20:30:16 +0900
Subject: Make gtk::GetLibGtk public

Allows embedders to get a handle to the gtk library
already loaded in the process.

diff --git a/ui/gtk/gtk_compat.cc b/ui/gtk/gtk_compat.cc
index 7104a0d5f4489c687f3cb9e63bc7cbef59d2fa62..f0b9ed834a3f7310da09377f0b2105cf635ffeb7 100644
--- a/ui/gtk/gtk_compat.cc
+++ b/ui/gtk/gtk_compat.cc
@@ -86,12 +86,6 @@ void* GetLibGtk4(bool check = true) {
return libgtk4;
}

-void* GetLibGtk() {
- if (GtkCheckVersion(4))
- return GetLibGtk4();
- return GetLibGtk3();
-}
-
bool LoadGtk3() {
if (!GetLibGtk3(false))
return false;
@@ -133,6 +127,12 @@ gfx::Insets InsetsFromGtkBorder(const GtkBorder& border) {

} // namespace

+void* GetLibGtk() {
+ if (GtkCheckVersion(4))
+ return GetLibGtk4();
+ return GetLibGtk3();
+}
+
bool LoadGtk() {
static bool loaded = LoadGtkImpl();
return loaded;
diff --git a/ui/gtk/gtk_compat.h b/ui/gtk/gtk_compat.h
index 72981270fe26579211afcaf3c596a412f69f5fac..b5dbfde5b011d57d26960d245e0dc61cac9341e4 100644
--- a/ui/gtk/gtk_compat.h
+++ b/ui/gtk/gtk_compat.h
@@ -37,6 +37,9 @@ using SkColor = uint32_t;

namespace gtk {

+// Get handle to the currently loaded gtk library in the process.
+void* GetLibGtk();
+
// Loads libgtk and related libraries and returns true on success.
bool LoadGtk();

38 changes: 0 additions & 38 deletions patches/chromium/ui_gtk_public_header.patch

This file was deleted.

2 changes: 1 addition & 1 deletion shell/browser/browser_linux.cc
Expand Up @@ -17,7 +17,7 @@

#if BUILDFLAG(IS_LINUX)
#include "shell/browser/linux/unity_service.h"
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_util.h" // nogncheck
#endif

namespace electron {
Expand Down
12 changes: 10 additions & 2 deletions shell/browser/electron_browser_main_parts.cc
Expand Up @@ -69,11 +69,13 @@
#include "base/threading/thread_task_runner_handle.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/dbus/dbus_bluez_manager_wrapper_linux.h"
#include "electron/electron_gtk_stubs.h"
#include "ui/base/cursor/cursor_factory.h"
#include "ui/base/ime/linux/linux_input_method_context_factory.h"
#include "ui/gfx/color_utils.h"
#include "ui/gtk/gtk_ui_factory.h"
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_compat.h" // nogncheck
#include "ui/gtk/gtk_ui_factory.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
#include "ui/ozone/public/ozone_platform.h"
#include "ui/views/linux_ui/linux_ui.h"
#endif
Expand Down Expand Up @@ -367,6 +369,12 @@ void ElectronBrowserMainParts::ToolkitInitialized() {
linux_ui->Initialize();
DCHECK(ui::LinuxInputMethodContextFactory::instance());

// Try loading gtk symbols used by Electron.
electron::InitializeElectron_gtk(gtk::GetLibGtk());
if (!electron::IsElectron_gtkInitialized()) {
electron::UninitializeElectron_gtk();
}

// Chromium does not respect GTK dark theme setting, but they may change
// in future and this code might be no longer needed. Check the Chromium
// issue to keep updated:
Expand Down
Expand Up @@ -16,7 +16,7 @@
#include "shell/common/application_info.h"
#include "shell/common/platform_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_util.h" // nogncheck

namespace electron {

Expand Down
1 change: 1 addition & 0 deletions shell/browser/ui/electron_gtk.fragment
@@ -0,0 +1 @@
#include <gtk/gtk.h>
6 changes: 6 additions & 0 deletions shell/browser/ui/electron_gtk.sigs
@@ -0,0 +1,6 @@
GtkFileChooserNative* gtk_file_chooser_native_new(const gchar* title, GtkWindow* parent, GtkFileChooserAction action, const gchar* accept_label, const gchar* cancel_label);
void gtk_native_dialog_set_modal(GtkNativeDialog* self, gboolean modal);
void gtk_native_dialog_show(GtkNativeDialog* self);
void gtk_native_dialog_hide(GtkNativeDialog* self);
gint gtk_native_dialog_run(GtkNativeDialog* self);
void gtk_native_dialog_destroy(GtkNativeDialog* self);