Skip to content

Commit

Permalink
Make mmap optional in Fcitx5Utils (#1252)
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt authored Feb 2, 2025
1 parent cc1c9d8 commit de44ba8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include(WriteBasicConfigVersionFile)
include(ECMUninstallTarget)
include(src/lib/fcitx-utils/Fcitx5Macros.cmake)
include(CheckSymbolExists)
include(CheckIncludeFile)

#######################################################################
# Options
Expand Down Expand Up @@ -225,6 +226,7 @@ else()
endif()

check_function_exists(pipe2 HAVE_PIPE2)
check_include_file(sys/mman.h HAVE_SYS_MMAN_H)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
Expand Down
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define DBUS_SYSTEM_BUS_DEFAULT_ADDRESS "@DBUS_SYSTEM_BUS_DEFAULT_ADDRESS@"

#cmakedefine HAVE_PIPE2
#cmakedefine HAVE_SYS_MMAN_H

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
Expand Down
44 changes: 38 additions & 6 deletions src/lib/fcitx-utils/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "library.h"
#include <dlfcn.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cerrno>
Expand All @@ -23,8 +22,22 @@
#include "misc.h"
#include "stringutils.h"

#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

namespace fcitx {

namespace {

#ifdef RTLD_NODELETE
constexpr bool hasRTLDNoDelete = true;
#else
constexpr bool hasRTLDNoDelete = false;
#endif

} // namespace

class LibraryPrivate {
public:
LibraryPrivate(std::string path) : path_(std::move(path)) {}
Expand All @@ -34,9 +47,15 @@ class LibraryPrivate {
if (!handle_) {
return false;
}
if (dlclose(handle_)) {
error_ = dlerror();
return false;

// If there is no RTLD_NODELETE and we don't want to unload, we leak
// it on purpose.
if (hasRTLDNoDelete ||
!loadFlag_.test(LibraryLoadHint::PreventUnloadHint)) {
if (dlclose(handle_)) {
error_ = dlerror();
return false;
}
}

handle_ = nullptr;
Expand All @@ -46,6 +65,7 @@ class LibraryPrivate {
std::string path_;
void *handle_ = nullptr;
std::string error_;
Flags<fcitx::LibraryLoadHint> loadFlag_;
};

Library::Library(const std::string &path)
Expand All @@ -54,6 +74,9 @@ Library::Library(const std::string &path)
FCITX_DEFINE_DEFAULT_DTOR_AND_MOVE(Library)

bool Library::load(Flags<fcitx::LibraryLoadHint> hint) {
if (loaded()) {
return true;
}
FCITX_D();
int flag = 0;
if (hint & LibraryLoadHint::ResolveAllSymbolsHint) {
Expand All @@ -62,9 +85,11 @@ bool Library::load(Flags<fcitx::LibraryLoadHint> hint) {
flag |= RTLD_LAZY;
}

#ifdef RTLD_NODELETE
if (hint & LibraryLoadHint::PreventUnloadHint) {
flag |= RTLD_NODELETE;
}
#endif

if (hint & LibraryLoadHint::ExportExternalSymbolsHint) {
flag |= RTLD_GLOBAL;
Expand All @@ -86,6 +111,8 @@ bool Library::load(Flags<fcitx::LibraryLoadHint> hint) {
return false;
}

d->loadFlag_ = hint;

return true;
}

Expand Down Expand Up @@ -141,9 +168,12 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
d->error_ = strerror(errno);
break;
}
void *data = nullptr;
#ifdef HAVE_SYS_MMAN_H
data = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
void *needunmap = nullptr;
void *data = needunmap =
mmap(nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
needunmap = data;
#endif
if (!data) {
data = malloc(statbuf.st_size);
needfree.reset(data);
Expand All @@ -163,9 +193,11 @@ bool Library::findData(const char *slug, const char *magic, size_t lenOfMagic,
parser(pos);
}
result = true;
#ifdef HAVE_SYS_MMAN_H
if (needunmap) {
munmap(needunmap, statbuf.st_size);
}
#endif
} while (false);

close(fd);
Expand Down

0 comments on commit de44ba8

Please sign in to comment.