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

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