lean4-htt/src/library/dynlib.cpp
Henrik Böving 52b1b342ab
feat: zero cost BaseIO (#10625)
This PR implements zero cost `BaseIO` by erasing the `IO.RealWorld`
parameter from argument lists and structures. This is a **major breaking
change for FFI**.

Concretely:
- `BaseIO` is defined in terms of `ST IO.RealWorld`
- `EIO` (and thus `IO`) is defined in terms of `EST IO.RealWorld`
- The opaque `Void` type is introduced and the trivial structure
optimization updated to account for it. Furthermore, arguments of type
`Void s` are removed from the argument lists of the C functions.
- `ST` is redefined as `Void s -> ST.Out s a` where `ST.Out` is a pair
of `Void s` and `a`

This together has the following major effects on our generated code:
- Functions that return `BaseIO`/`ST`/`EIO`/`IO`/`EST` now do not take
the dummy world parameter anymore. To account for this FFI code needs to
delete the dummy world parameter from the argument lists.
- Functions that return `BaseIO`/`ST` now return their wrapped value
directly. In particular `BaseIO UInt32` now returns a `uint32_t` instead
of a `lean_object*`. To account for this FFI code might have to change
the return type and does not need to call `lean_io_result_mk_ok` anymore
but can instead just `return` values right away (same with extracting
values from `BaseIO` computations.
- Functions that return `EIO`/`IO`/`EST` now only return the equivalent
of an `Except` node which reduces the allocation size. The
`lean_io_result_mk_ok`/`lean_io_result_mk_error` functions were updated
to account for this already so no change is required.

Besides improving performance by dropping allocation (sizes) we can now
also do fun new things such as:
```lean
@[extern "malloc"]
opaque malloc (size : USize) : BaseIO USize
```
2025-10-22 10:55:12 +02:00

135 lines
4.2 KiB
C++

/*
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura, Mac Malone
*/
#include "util/io.h"
#include "runtime/io.h"
#include "runtime/object.h"
#include "runtime/sstream.h"
#include "runtime/exception.h"
#include "library/dynlib.h"
#ifdef LEAN_WINDOWS
#include <windows.h>
#else
#include <dlfcn.h>
#endif
namespace lean {
static lean_external_class * g_dynlib_external_class = nullptr;
static lean_external_class * g_dynlib_symbol_external_class = nullptr;
static void dynlib_finalizer(void * h) {
// There is no sensible way to handle errors here.
// The same decision was made in Rust's libloading.
#ifdef LEAN_WINDOWS
FreeLibrary(static_cast<HMODULE>(h));
#else
dlclose(h);
#endif
}
static void noop_foreach(void * /* val */, b_obj_arg /* fn */) {
}
static void noop_finalizer(void * h) {
}
void initialize_dynlib() {
g_dynlib_external_class = lean_register_external_class(dynlib_finalizer, noop_foreach);
g_dynlib_symbol_external_class = lean_register_external_class(noop_finalizer, noop_foreach);
}
#ifdef LEAN_WINDOWS
static inline obj_res wrap_dynlib(HMODULE h) {
return alloc_external(g_dynlib_external_class, h);
}
static inline HMODULE dynlib_handle(b_obj_arg dynlib) {
return static_cast<HMODULE>(lean_get_external_data(dynlib));
}
#else
static inline obj_res wrap_dynlib(void * h) {
return alloc_external(g_dynlib_external_class, h);
}
static inline void * dynlib_handle(b_obj_arg dynlib) {
return lean_get_external_data(dynlib);
}
#endif
static inline obj_res wrap_symbol(void * sym) {
return alloc_external(g_dynlib_symbol_external_class, sym);
}
static inline void * symbol_ptr(b_obj_arg sym) {
return lean_get_external_data(sym);
}
/* Dynlib.load : System.FilePath -> IO Dynlib */
extern "C" LEAN_EXPORT obj_res lean_dynlib_load(b_obj_arg path) {
#ifdef LEAN_WINDOWS
HMODULE h = LoadLibrary(string_cstr(path));
if (!h) {
return io_result_mk_error((sstream()
<< "error loading library " << string_cstr(path) << ": " << GetLastError()).str());
}
return io_result_mk_ok(wrap_dynlib(h));
#else
// Both dynlibs and plugins are loaded with RTLD_GLOBAL.
// This ensures the interpreter has access to plugin definitions that are also
// imported (e.g., an environment extension defined with builtin_initialize).
// In either case, loading the same symbol twice (and thus e.g. running initializers
// manipulating global `IO.Ref`s twice) should be avoided; the common module
// should instead be factored out into a separate shared library
void *h = dlopen(string_cstr(path), RTLD_LAZY | RTLD_GLOBAL);
if (!h) {
return io_result_mk_error((sstream()
<< "error loading library, " << dlerror()).str());
}
return io_result_mk_ok(wrap_dynlib(h));
#endif
}
/* Dynlib.get? : (dynlib : Dynlib) -> String -> dynlib.Symbol */
extern "C" LEAN_EXPORT obj_res lean_dynlib_get(b_obj_arg dynlib, b_obj_arg name) {
#ifdef LEAN_WINDOWS
auto sym = reinterpret_cast<void *>(GetProcAddress(dynlib_handle(dynlib), string_cstr(name)));
if (sym) {
return mk_option_some(wrap_symbol(sym));
} else {
return mk_option_none();
}
#else
// The address of a valid Linux symbol can be NULL.
// Thus, this is the recommended way to validate a symbol.
dlerror();
void * sym = dlsym(dynlib_handle(dynlib), string_cstr(name));
if (dlerror()) {
return mk_option_none();
} else {
return mk_option_some(wrap_symbol(sym));
}
#endif
}
/* Dynlib.Symbol.runAsInit : {Dynlib} -> Symbol -> IO Unit */
extern "C" LEAN_EXPORT obj_res lean_dynlib_symbol_run_as_init(b_obj_arg /* dynlib */, b_obj_arg sym) {
auto init_fn = reinterpret_cast<object *(*)(uint8_t)>(symbol_ptr(sym));
return init_fn(1 /* builtin */);
}
/* Lean.loadDynlib : System.FilePath -> IO Unit */
extern "C" obj_res lean_load_dynlib(obj_arg path);
void load_dynlib(std::string path) {
consume_io_result(lean_load_dynlib(mk_string(path)));
}
/* Lean.loadPlugin : System.FilePath -> IO Unit */
extern "C" obj_res lean_load_plugin(obj_arg path);
void load_plugin(std::string path) {
consume_io_result(lean_load_plugin(mk_string(path)));
}
}