fix: potential crash caused by dlopen different gtk libraries (#33650)

This commit is contained in:
Robo 2022-04-18 13:24:32 +09:00 committed by GitHub
parent 9d4aceb940
commit 37b7e347fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 130 additions and 183 deletions

View file

@ -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 {

View file

@ -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
@ -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:

View file

@ -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 {

View file

@ -0,0 +1 @@
#include <gtk/gtk.h>

View file

@ -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);

View file

@ -2,8 +2,6 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <gmodule.h>
#include <memory>
#include <string>
@ -11,6 +9,7 @@
#include "base/files/file_util.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_restrictions.h"
#include "electron/electron_gtk_stubs.h"
#include "shell/browser/javascript_environment.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/file_dialog.h"
@ -18,32 +17,11 @@
#include "shell/browser/unresponsive_suppressor.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "ui/base/glib/glib_signal.h"
#include "ui/gtk/gtk_ui.h"
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_ui.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
namespace file_dialog {
static GModule* gtk_module;
static absl::optional<bool> supports_gtk_file_chooser_native;
using dl_gtk_native_dialog_show_t = void (*)(void*);
using dl_gtk_native_dialog_destroy_t = void (*)(void*);
using dl_gtk_native_dialog_set_modal_t = void (*)(void*, gboolean);
using dl_gtk_native_dialog_run_t = int (*)(void*);
using dl_gtk_native_dialog_hide_t = void (*)(void*);
using dl_gtk_file_chooser_native_new_t = void* (*)(const char*,
GtkWindow*,
GtkFileChooserAction,
const char*,
const char*);
static dl_gtk_native_dialog_show_t dl_gtk_native_dialog_show;
static dl_gtk_native_dialog_destroy_t dl_gtk_native_dialog_destroy;
static dl_gtk_native_dialog_set_modal_t dl_gtk_native_dialog_set_modal;
static dl_gtk_native_dialog_run_t dl_gtk_native_dialog_run;
static dl_gtk_native_dialog_hide_t dl_gtk_native_dialog_hide;
static dl_gtk_file_chooser_native_new_t dl_gtk_file_chooser_native_new;
DialogSettings::DialogSettings() = default;
DialogSettings::DialogSettings(const DialogSettings&) = default;
DialogSettings::~DialogSettings() = default;
@ -53,85 +31,16 @@ namespace {
static const int kPreviewWidth = 256;
static const int kPreviewHeight = 512;
void InitGtkFileChooserNativeSupport() {
// Return early if we have already setup the native functions or we have tried
// once before and failed. Avoid running expensive dynamic library operations.
if (supports_gtk_file_chooser_native) {
return;
}
// Mark that we have attempted to initialize support at least once
supports_gtk_file_chooser_native = false;
if (!g_module_supported()) {
return;
}
gtk_module = g_module_open("libgtk-3.so", G_MODULE_BIND_LAZY);
if (!gtk_module) {
return;
}
// Will never be unloaded
g_module_make_resident(gtk_module);
bool found = g_module_symbol(
gtk_module, "gtk_file_chooser_native_new",
reinterpret_cast<void**>(&dl_gtk_file_chooser_native_new));
if (!found) {
return;
}
found = g_module_symbol(
gtk_module, "gtk_native_dialog_set_modal",
reinterpret_cast<void**>(&dl_gtk_native_dialog_set_modal));
if (!found) {
return;
}
found =
g_module_symbol(gtk_module, "gtk_native_dialog_destroy",
reinterpret_cast<void**>(&dl_gtk_native_dialog_destroy));
if (!found) {
return;
}
found = g_module_symbol(gtk_module, "gtk_native_dialog_show",
reinterpret_cast<void**>(&dl_gtk_native_dialog_show));
if (!found) {
return;
}
found = g_module_symbol(gtk_module, "gtk_native_dialog_hide",
reinterpret_cast<void**>(&dl_gtk_native_dialog_hide));
if (!found) {
return;
}
found = g_module_symbol(gtk_module, "gtk_native_dialog_run",
reinterpret_cast<void**>(&dl_gtk_native_dialog_run));
if (!found) {
return;
}
supports_gtk_file_chooser_native =
dl_gtk_file_chooser_native_new && dl_gtk_native_dialog_set_modal &&
dl_gtk_native_dialog_destroy && dl_gtk_native_dialog_run &&
dl_gtk_native_dialog_show && dl_gtk_native_dialog_hide;
}
class FileChooserDialog {
public:
FileChooserDialog(GtkFileChooserAction action, const DialogSettings& settings)
: parent_(
static_cast<electron::NativeWindowViews*>(settings.parent_window)),
filters_(settings.filters) {
InitGtkFileChooserNativeSupport();
auto label = settings.button_label;
if (*supports_gtk_file_chooser_native) {
dialog_ = GTK_FILE_CHOOSER(dl_gtk_file_chooser_native_new(
if (electron::IsElectron_gtkInitialized()) {
dialog_ = GTK_FILE_CHOOSER(gtk_file_chooser_native_new(
settings.title.c_str(), NULL, action,
label.empty() ? nullptr : label.c_str(), nullptr));
} else {
@ -150,8 +59,8 @@ class FileChooserDialog {
if (parent_) {
parent_->SetEnabled(false);
if (*supports_gtk_file_chooser_native) {
dl_gtk_native_dialog_set_modal(static_cast<void*>(dialog_), TRUE);
if (electron::IsElectron_gtkInitialized()) {
gtk_native_dialog_set_modal(GTK_NATIVE_DIALOG(dialog_), TRUE);
} else {
gtk::SetGtkTransientForAura(GTK_WIDGET(dialog_),
parent_->GetNativeWindow());
@ -188,7 +97,7 @@ class FileChooserDialog {
// org.freedesktop.portal.FileChooser portal. In the case of running through
// the org.freedesktop.portal.FileChooser portal, anything having to do with
// the update-preview signal or the preview widget will just be ignored.
if (!*supports_gtk_file_chooser_native) {
if (!electron::IsElectron_gtkInitialized()) {
preview_ = gtk_image_new();
g_signal_connect(dialog_, "update-preview",
G_CALLBACK(OnUpdatePreviewThunk), this);
@ -197,8 +106,8 @@ class FileChooserDialog {
}
~FileChooserDialog() {
if (*supports_gtk_file_chooser_native) {
dl_gtk_native_dialog_destroy(static_cast<void*>(dialog_));
if (electron::IsElectron_gtkInitialized()) {
gtk_native_dialog_destroy(GTK_NATIVE_DIALOG(dialog_));
} else {
gtk_widget_destroy(GTK_WIDGET(dialog_));
}
@ -236,8 +145,8 @@ class FileChooserDialog {
void RunAsynchronous() {
g_signal_connect(dialog_, "response", G_CALLBACK(OnFileDialogResponseThunk),
this);
if (*supports_gtk_file_chooser_native) {
dl_gtk_native_dialog_show(static_cast<void*>(dialog_));
if (electron::IsElectron_gtkInitialized()) {
gtk_native_dialog_show(GTK_NATIVE_DIALOG(dialog_));
} else {
gtk_widget_show_all(GTK_WIDGET(dialog_));
gtk::GtkUi::GetPlatform()->ShowGtkWindow(GTK_WINDOW(dialog_));
@ -305,8 +214,8 @@ class FileChooserDialog {
};
void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) {
if (*supports_gtk_file_chooser_native) {
dl_gtk_native_dialog_hide(static_cast<void*>(dialog_));
if (electron::IsElectron_gtkInitialized()) {
gtk_native_dialog_hide(GTK_NATIVE_DIALOG(dialog_));
} else {
gtk_widget_hide(GTK_WIDGET(dialog_));
}
@ -371,7 +280,7 @@ bool CanPreview(const struct stat& st) {
}
void FileChooserDialog::OnUpdatePreview(GtkFileChooser* chooser) {
CHECK(!*supports_gtk_file_chooser_native);
CHECK(!electron::IsElectron_gtkInitialized());
gchar* filename = gtk_file_chooser_get_preview_filename(chooser);
if (!filename) {
gtk_file_chooser_set_preview_widget_active(chooser, FALSE);
@ -400,15 +309,15 @@ void FileChooserDialog::OnUpdatePreview(GtkFileChooser* chooser) {
void ShowFileDialog(const FileChooserDialog& dialog) {
// gtk_native_dialog_run() will call gtk_native_dialog_show() for us.
if (!*supports_gtk_file_chooser_native) {
if (!electron::IsElectron_gtkInitialized()) {
gtk_widget_show_all(GTK_WIDGET(dialog.dialog()));
}
}
int RunFileDialog(const FileChooserDialog& dialog) {
int response = 0;
if (*supports_gtk_file_chooser_native) {
response = dl_gtk_native_dialog_run(static_cast<void*>(dialog.dialog()));
if (electron::IsElectron_gtkInitialized()) {
response = gtk_native_dialog_run(GTK_NATIVE_DIALOG(dialog.dialog()));
} else {
response = gtk_dialog_run(GTK_DIALOG(dialog.dialog()));
}

View file

@ -18,8 +18,8 @@
#include "shell/browser/unresponsive_suppressor.h"
#include "ui/base/glib/glib_signal.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gtk/gtk_ui.h"
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_ui.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
#if defined(USE_OZONE)
#include "ui/base/ui_base_features.h"

View file

@ -22,7 +22,7 @@
#include "ui/gfx/skia_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/gtk/gtk_compat.h" // nogncheck
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_util.h" // nogncheck
#include "ui/native_theme/native_theme.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/button/image_button.h"

View file

@ -14,7 +14,7 @@
#include "ui/views/layout/box_layout.h"
#if BUILDFLAG(IS_LINUX)
#include "ui/gtk/gtk_util.h"
#include "ui/gtk/gtk_util.h" // nogncheck
#endif
#if BUILDFLAG(IS_WIN)