Loading...
Searching...
No Matches
ReadconDbMirror.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#include "ReadconDbMirror.h"
13
14#include "eon/EonLogger.h"
15
16#include <cstdint>
17#include <cstdlib>
18#include <filesystem>
19#include <fstream>
20#include <mutex>
21#include <optional>
22#include <sstream>
23#include <string>
24#include <unordered_map>
25
26#ifndef EON_READCON_DB_VERSION
27#define EON_READCON_DB_VERSION ""
28#endif
29
30#if defined(_WIN32)
31#ifndef WIN32_LEAN_AND_MEAN
32#define WIN32_LEAN_AND_MEAN
33#endif
34#ifndef NOMINMAX
35#define NOMINMAX
36#endif
37#include <windows.h>
38using LibHandle = HMODULE;
39#else
40#include <dlfcn.h>
41using LibHandle = void *;
42
43extern "C" int rkrdb_open(const char *, std::size_t *);
44// The mirror looks the symbol up with dlsym. Naming it here keeps the
45// linked libreadcon_db.so on the link line under --as-needed.
46[[maybe_unused]] static int (*const kLinkedOpen)(const char *,
47 std::size_t *) = rkrdb_open;
48#endif
49
50namespace eonc::io {
51namespace {
52
53constexpr std::uint64_t kFnvOffset = 14695981039346656037ull;
54constexpr std::uint64_t kFnvPrime = 1099511628211ull;
55
56std::uint64_t fnv1a(const std::string &bytes) {
57 std::uint64_t h = kFnvOffset;
58 for (unsigned char c : bytes) {
59 h ^= static_cast<std::uint64_t>(c);
60 h *= kFnvPrime;
61 }
62 return h == 0 ? 1 : h;
63}
64
65std::optional<std::filesystem::path>
66corpus_dir(const std::filesystem::path &con_path) {
67 namespace fs = std::filesystem;
68 std::error_code ec;
69 fs::path cur = fs::weakly_canonical(con_path, ec);
70 if (ec) {
71 cur = fs::absolute(con_path);
72 }
73 cur = cur.parent_path();
74 for (int i = 0; i < 8; ++i) {
75 if (fs::is_regular_file(cur / "config.ini")) {
76 return cur / "readcon.db";
77 }
78 const fs::path parent = cur.parent_path();
79 if (parent == cur) {
80 break;
81 }
82 cur = parent;
83 }
84 // A stray .con is not a job. Do not mint readcon.db beside it.
85 return std::nullopt;
86}
87
88using OpenFn = int (*)(const char *, std::size_t *);
89using AppendFn = int (*)(std::size_t, std::uint64_t, const char *, const char *,
90 std::uint32_t *);
91
92struct Api {
93 LibHandle lib = nullptr;
94 OpenFn open = nullptr;
95 AppendFn append_str = nullptr;
96 bool missing = false;
97 bool from_process = false;
98 int epoch = -1;
99};
100
101int &mirror_epoch() {
102 static int epoch = 0;
103 return epoch;
104}
105
106void release_lib(LibHandle lib, bool from_process) {
107 if (lib == nullptr || from_process) {
108 return;
109 }
110#if defined(_WIN32)
111 FreeLibrary(lib);
112#else
113 dlclose(lib);
114#endif
115}
116
117void *symbol(LibHandle handle, const char *name) {
118#if defined(_WIN32)
119 return reinterpret_cast<void *>(GetProcAddress(handle, name));
120#else
121 return dlsym(handle, name);
122#endif
123}
124
125LibHandle open_named(const char *name) {
126#if defined(_WIN32)
127 return LoadLibraryA(name);
128#else
129 return dlopen(name, RTLD_LAZY | RTLD_LOCAL);
130#endif
131}
132
133const char *open_error() {
134#if defined(_WIN32)
135 return "LoadLibrary failed";
136#else
137 const char *err = dlerror();
138 return err != nullptr ? err : "dlopen returned null";
139#endif
140}
141
142void clear_api(Api &out) {
143 release_lib(out.lib, out.from_process);
144 out = Api{};
145}
146
147bool resolve_symbols(Api &out, LibHandle handle) {
148 out.open = reinterpret_cast<OpenFn>(symbol(handle, "rkrdb_open"));
149 out.append_str =
150 reinterpret_cast<AppendFn>(symbol(handle, "rkrdb_append_trajectory_str"));
151 return out.open != nullptr && out.append_str != nullptr;
152}
153
154Api &api() {
155 static Api out;
156 if (out.epoch == mirror_epoch() &&
157 (out.lib != nullptr || out.from_process || out.missing)) {
158 return out;
159 }
160 clear_api(out);
161 out.epoch = mirror_epoch();
162 const char *forced = std::getenv("EON_READCON_DB_LIBRARY");
163 if (forced != nullptr) {
164 namespace fs = std::filesystem;
165 std::error_code ec;
166 if (forced[0] == '\0' || !fs::is_regular_file(forced, ec) || ec) {
167 EONC_LOG_WARNING("[readcon-db] libreadcon_db.so failed to load: {}",
168 forced[0] == '\0' ? "empty path" : forced);
169 out.missing = true;
170 return out;
171 }
172 out.lib = open_named(forced);
173 } else {
174#if !defined(_WIN32)
175 if (resolve_symbols(out, RTLD_DEFAULT)) {
176 out.from_process = true;
177 return out;
178 }
179 out.open = nullptr;
180 out.append_str = nullptr;
181 const char *names[] = {"libreadcon_db.so", "libreadcon_db.so.0",
182 "libreadcon_db.dylib"};
183#else
184 // The import library does not pull the DLL in unless some object
185 // references rkrdb_*. Load it from PATH (the test runner puts the
186 // build directory there) or from an earlier LoadLibrary.
187 HMODULE loaded = GetModuleHandleA("readcon_db.dll");
188 if (loaded != nullptr && resolve_symbols(out, loaded)) {
189 out.lib = loaded;
190 out.from_process = true;
191 return out;
192 }
193 out.open = nullptr;
194 out.append_str = nullptr;
195 const char *names[] = {"readcon_db.dll"};
196#endif
197 for (const char *name : names) {
198 out.lib = open_named(name);
199 if (out.lib != nullptr) {
200 break;
201 }
202 }
203 }
204 if (out.lib == nullptr) {
205 EONC_LOG_WARNING("[readcon-db] libreadcon_db.so failed to load: {}",
206 open_error());
207 out.missing = true;
208 return out;
209 }
210 if (!resolve_symbols(out, out.lib)) {
211 release_lib(out.lib, false);
212 out.lib = nullptr;
213 out.open = nullptr;
214 out.append_str = nullptr;
215 out.missing = true;
216 }
217 return out;
218}
219
220std::mutex &handles_mu() {
221 static std::mutex mu;
222 return mu;
223}
224
225std::unordered_map<std::string, std::size_t> &handles() {
226 static std::unordered_map<std::string, std::size_t> map;
227 return map;
228}
229
230std::size_t corpus_handle(const std::filesystem::path &dir) {
231 Api &fns = api();
232 if (fns.missing) {
233 return static_cast<std::size_t>(-1);
234 }
235 const std::string key = dir.string();
236 std::lock_guard<std::mutex> guard(handles_mu());
237 const auto found = handles().find(key);
238 if (found != handles().end()) {
239 return found->second;
240 }
241 std::size_t id = 0;
242 if (fns.open(key.c_str(), &id) != 0) {
243 return static_cast<std::size_t>(-1);
244 }
245 handles().emplace(key, id);
246 return id;
247}
248
249} // namespace
250
251void mirror_con_corpus(const std::string &path) {
252 namespace fs = std::filesystem;
253 std::error_code ec;
254 if (!fs::is_regular_file(path, ec) || ec) {
255 return;
256 }
257 std::ifstream in(path);
258 if (!in) {
259 return;
260 }
261 std::ostringstream buf;
262 buf << in.rdbuf();
263 const std::string text = buf.str();
264 if (text.find_first_not_of(" \t\r\n") == std::string::npos) {
265 return;
266 }
267 fs::path canonical = fs::weakly_canonical(path, ec);
268 if (ec) {
269 canonical = fs::absolute(path);
270 }
271 const std::string resolved = canonical.string();
272 const auto corpus = corpus_dir(canonical);
273 if (!corpus) {
274 return;
275 }
276 const std::size_t id = corpus_handle(*corpus);
277 if (id == static_cast<std::size_t>(-1)) {
278 return;
279 }
280 const std::uint64_t tid = fnv1a(resolved + '\0' + text);
281 std::uint32_t nframes = 0;
282 api().append_str(id, tid, text.c_str(), resolved.c_str(), &nframes);
283}
284
286 const Api &fns = api();
287 return !fns.missing && fns.open != nullptr && fns.append_str != nullptr;
288}
289
290void readcon_db_mirror_reset() { mirror_epoch()++; }
291
293 if (!readcon_db_mirror_ok()) {
294 return "";
295 }
297}
298
299} // namespace eonc::io
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
#define EON_READCON_DB_VERSION
static int(*const kLinkedOpen)(const char *, std::size_t *)
int rkrdb_open(const char *, std::size_t *)
void * LibHandle
bool readcon_db_mirror_ok()
True when the loaded rkrdb_open belongs to the linked readcon-db.
void readcon_db_mirror_reset()
Drop a cached load so a later call reads EON_READCON_DB_LIBRARY again.
const char * readcon_db_loaded_version()
Version of the loaded library. Empty when the load failed.
void mirror_con_corpus(const std::string &path)
Copy a con file into the run readcon-db corpus when libreadcon_db loads.