-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathcore.h
More file actions
284 lines (244 loc) · 10.3 KB
/
core.h
File metadata and controls
284 lines (244 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Core DLL interop - loads Microsoft.AI.Foundry.Local.Core.dll at runtime.
// Internal header, not part of the public API.
#pragma once
#include <string>
#include <stdexcept>
#include <filesystem>
#include <memory>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#include <climits>
#include <unistd.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#endif
#include "foundry_local_internal_core.h"
#include "foundry_local_exception.h"
#include "flcore_native.h"
#include "logger.h"
namespace foundry_local {
namespace {
// RAII wrapper for a dynamically loaded shared library handle.
struct SharedLibHandle {
void* handle = nullptr;
SharedLibHandle() = default;
explicit SharedLibHandle(void* h) : handle(h) {}
SharedLibHandle(const SharedLibHandle&) = delete;
SharedLibHandle& operator=(const SharedLibHandle&) = delete;
SharedLibHandle(SharedLibHandle&& o) noexcept : handle(o.handle) { o.handle = nullptr; }
SharedLibHandle& operator=(SharedLibHandle&& o) noexcept {
reset();
handle = o.handle;
o.handle = nullptr;
return *this;
}
~SharedLibHandle() { reset(); }
void reset() noexcept {
if (!handle) return;
#ifdef _WIN32
::FreeLibrary(static_cast<HMODULE>(handle));
#else
::dlclose(handle);
#endif
handle = nullptr;
}
explicit operator bool() const noexcept { return handle != nullptr; }
};
inline std::filesystem::path GetExecutableDir() {
#ifdef _WIN32
std::wstring buf(MAX_PATH, L'\0');
for (;;) {
DWORD len = ::GetModuleFileNameW(nullptr, buf.data(), static_cast<DWORD>(buf.size()));
if (len == 0)
throw std::runtime_error("GetModuleFileNameW failed");
if (len < static_cast<DWORD>(buf.size()))
return std::filesystem::path(buf.c_str()).parent_path();
buf.resize(buf.size() * 2);
}
#elif defined(__APPLE__)
char buf[PATH_MAX];
uint32_t size = sizeof(buf);
if (_NSGetExecutablePath(buf, &size) != 0)
throw std::runtime_error("_NSGetExecutablePath failed");
return std::filesystem::canonical(buf).parent_path();
#else
return std::filesystem::read_symlink("/proc/self/exe").parent_path();
#endif
}
inline std::string GetLoaderError() {
#ifdef _WIN32
DWORD err = ::GetLastError();
if (err == 0) return {};
LPSTR buf = nullptr;
DWORD len = ::FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&buf), 0, nullptr);
std::string msg(buf, len);
::LocalFree(buf);
// Trim trailing newline
while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r'))
msg.pop_back();
return msg;
#else
const char* err = ::dlerror();
return err ? std::string(err) : std::string{};
#endif
}
inline void* LoadSharedLib(const std::filesystem::path& path) {
#ifdef _WIN32
return static_cast<void*>(::LoadLibraryW(path.c_str()));
#else
return ::dlopen(path.c_str(), RTLD_NOW);
#endif
}
inline void* RequireProc(void* mod, const char* name) {
#ifdef _WIN32
void* p = reinterpret_cast<void*>(::GetProcAddress(static_cast<HMODULE>(mod), name));
#else
void* p = ::dlsym(mod, name);
#endif
if (!p) {
std::string msg = std::string("Symbol not found: ") + name;
std::string detail = GetLoaderError();
if (!detail.empty())
msg += " (" + detail + ")";
throw std::runtime_error(msg);
}
return p;
}
inline void* OptionalProc(void* mod, const char* name) noexcept {
#ifdef _WIN32
return reinterpret_cast<void*>(::GetProcAddress(static_cast<HMODULE>(mod), name));
#else
return ::dlsym(mod, name);
#endif
}
} // namespace
struct Core : Internal::IFoundryLocalCore {
using ResponseHandle = std::unique_ptr<ResponseBuffer, void (*)(ResponseBuffer*)>;
Core() = default;
~Core() = default;
void LoadEmbedded() {
constexpr const char* kCoreLibName =
#ifdef _WIN32
"Microsoft.AI.Foundry.Local.Core.dll";
#elif defined(__APPLE__)
"Microsoft.AI.Foundry.Local.Core.dylib";
#else
"Microsoft.AI.Foundry.Local.Core.so";
#endif
LoadFromPath(GetExecutableDir() / kCoreLibName);
}
void unload() override {
module_.reset();
execCmd_ = nullptr;
execCbCmd_ = nullptr;
execBinaryCmd_ = nullptr;
freeResCmd_ = nullptr;
}
CoreResponse call(std::string_view command, ILogger& logger, const std::string* dataArgument = nullptr,
NativeCallbackFn callback = nullptr, void* data = nullptr) const override {
if (!static_cast<bool>(module_) || !execCmd_ || !execCbCmd_ || !freeResCmd_) {
throw Exception("Core is not loaded. Cannot call command: " + std::string(command), logger);
}
RequestBuffer request{};
request.Command = command.empty() ? nullptr : command.data();
request.CommandLength = static_cast<int32_t>(command.size());
if (dataArgument && !dataArgument->empty()) {
request.Data = dataArgument->data();
request.DataLength = static_cast<int32_t>(dataArgument->size());
}
ResponseBuffer response{};
auto safeDeleter = [fn = freeResCmd_](ResponseBuffer* buf) {
if (fn)
fn(buf);
};
std::unique_ptr<ResponseBuffer, decltype(safeDeleter)> responseGuard(&response, safeDeleter);
if (callback != nullptr) {
execCbCmd_(&request, &response, callback, data);
}
else {
execCmd_(&request, &response);
}
CoreResponse result;
if (response.Error && response.ErrorLength > 0) {
result.error.assign(static_cast<const char*>(response.Error), response.ErrorLength);
return result;
}
if (response.Data && response.DataLength > 0) {
result.data.assign(static_cast<const char*>(response.Data), response.DataLength);
}
return result;
}
CoreResponse callWithBinary(std::string_view command, ILogger& logger,
const std::string* dataArgument,
const uint8_t* binaryData, size_t binaryDataLength) const override {
if (!static_cast<bool>(module_) || !freeResCmd_) {
throw Exception("Core is not loaded. Cannot call command: " + std::string(command), logger);
}
if (!execBinaryCmd_) {
throw Exception("execute_command_with_binary is not available in this version of the Core library.", logger);
}
StreamingRequestBuffer request{};
request.Command = command.empty() ? nullptr : command.data();
request.CommandLength = static_cast<int32_t>(command.size());
if (dataArgument && !dataArgument->empty()) {
request.Data = dataArgument->data();
request.DataLength = static_cast<int32_t>(dataArgument->size());
}
if (binaryData && binaryDataLength > 0) {
if (binaryDataLength > static_cast<size_t>(INT32_MAX)) {
throw Exception("Binary data length exceeds maximum supported size (INT32_MAX).", logger);
}
request.BinaryData = binaryData;
request.BinaryDataLength = static_cast<int32_t>(binaryDataLength);
}
ResponseBuffer response{};
auto safeDeleter = [fn = freeResCmd_](ResponseBuffer* buf) {
if (fn)
fn(buf);
};
std::unique_ptr<ResponseBuffer, decltype(safeDeleter)> responseGuard(&response, safeDeleter);
execBinaryCmd_(&request, &response);
CoreResponse result;
if (response.Error && response.ErrorLength > 0) {
result.error.assign(static_cast<const char*>(response.Error), response.ErrorLength);
return result;
}
if (response.Data && response.DataLength > 0) {
result.data.assign(static_cast<const char*>(response.Data), response.DataLength);
}
return result;
}
private:
SharedLibHandle module_;
execute_command_fn execCmd_{};
execute_command_with_callback_fn execCbCmd_{};
execute_command_with_binary_fn execBinaryCmd_{};
free_response_fn freeResCmd_{};
void LoadFromPath(const std::filesystem::path& path) {
SharedLibHandle m(LoadSharedLib(path));
if (!m) {
std::string msg = "Failed to load shared library: " + path.string();
std::string detail = GetLoaderError();
if (!detail.empty())
msg += " (" + detail + ")";
throw std::runtime_error(msg);
}
execCmd_ = reinterpret_cast<execute_command_fn>(RequireProc(m.handle, "execute_command"));
execCbCmd_ = reinterpret_cast<execute_command_with_callback_fn>(
RequireProc(m.handle, "execute_command_with_callback"));
execBinaryCmd_ = reinterpret_cast<execute_command_with_binary_fn>(
OptionalProc(m.handle, "execute_command_with_binary"));
freeResCmd_ = reinterpret_cast<free_response_fn>(RequireProc(m.handle, "free_response"));
module_ = std::move(m);
}
};
} // namespace foundry_local