Switch the bindings to nanobind (and update the CI)

This commit is contained in:
Romain Thomas
2023-07-09 21:33:43 +02:00
parent 7e387675ac
commit e37ffa9ec8
377 changed files with 12540 additions and 15730 deletions
@@ -0,0 +1,9 @@
set(LIEF_PYTHON_ART_OBJ_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/pyHeader.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/pyParser.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/pyFile.cpp"
)
target_sources(pyLIEF PRIVATE ${LIEF_PYTHON_ART_OBJ_SRC})
+10 -41
View File
@@ -14,54 +14,23 @@
* limitations under the License.
*/
#include "LIEF/ART/File.hpp"
#include "LIEF/ART/hash.hpp"
#include "pyART.hpp"
#include "ART/pyART.hpp"
#include <sstream>
#include <nanobind/stl/string.h>
namespace LIEF {
namespace ART {
template<class T>
using no_const_getter = T (File::*)(void);
template<class T, class P>
using no_const_func = T (File::*)(P);
template<class T>
using getter_t = T (File::*)(void) const;
template<class T>
using setter_t = void (File::*)(T);
namespace LIEF::ART::py {
template<>
void create<File>(py::module& m) {
void create<File>(nb::module_& m) {
nb::class_<File, LIEF::Object>(m, "File", "ART File representation"_doc)
// File object
py::class_<File, LIEF::Object>(m, "File", "ART File representation")
.def_property_readonly("header",
static_cast<no_const_getter<Header&>>(&File::header),
"Return the ART " RST_CLASS_REF(lief.ART.Header) "",
py::return_value_policy::reference)
.def("__eq__", &File::operator==)
.def("__ne__", &File::operator!=)
.def("__hash__",
[] (const File& file) {
return Hash::hash(file);
})
.def("__str__",
[] (const File& file)
{
std::ostringstream stream;
stream << file;
std::string str = stream.str();
return str;
});
.def_prop_ro("header",
nb::overload_cast<>(&File::header),
"Return the ART " RST_CLASS_REF(lief.ART.Header) ""_doc,
nb::rv_policy::reference_internal)
LIEF_DEFAULT_STR(Header);
}
}
}
+47 -68
View File
@@ -14,102 +14,81 @@
* limitations under the License.
*/
#include "LIEF/ART/Header.hpp"
#include "LIEF/ART/hash.hpp"
#include "pyART.hpp"
#include "ART/pyART.hpp"
#include <sstream>
#include <nanobind/stl/string.h>
#include <nanobind/stl/array.h>
namespace LIEF {
namespace ART {
template<class T>
using getter_t = T (Header::*)(void) const;
template<class T>
using setter_t = void (Header::*)(T);
namespace LIEF::ART::py {
template<>
void create<Header>(py::module& m) {
void create<Header>(nb::module_& m) {
py::class_<Header, LIEF::Object>(m, "Header", "ART Header representation")
.def_property_readonly("magic",
static_cast<getter_t<Header::magic_t>>(&Header::magic)
nb::class_<Header, Object>(m, "Header", "ART Header representation"_doc)
.def_prop_ro("magic",
nb::overload_cast<>(&Header::magic, nb::const_)
)
.def_property_readonly("version",
static_cast<getter_t<art_version_t>>(&Header::version)
.def_prop_ro("version",
nb::overload_cast<>(&Header::version, nb::const_)
)
.def_property_readonly("image_begin",
static_cast<getter_t<uint32_t>>(&Header::image_begin)
.def_prop_ro("image_begin",
nb::overload_cast<>(&Header::image_begin, nb::const_)
)
.def_property_readonly("image_size",
static_cast<getter_t<uint32_t>>(&Header::image_size)
.def_prop_ro("image_size",
nb::overload_cast<>(&Header::image_size, nb::const_)
)
.def_property_readonly("oat_checksum",
static_cast<getter_t<uint32_t>>(&Header::oat_checksum)
.def_prop_ro("oat_checksum",
nb::overload_cast<>(&Header::oat_checksum, nb::const_)
)
.def_property_readonly("oat_file_begin",
static_cast<getter_t<uint32_t>>(&Header::oat_file_begin)
.def_prop_ro("oat_file_begin",
nb::overload_cast<>(&Header::oat_file_begin, nb::const_)
)
.def_property_readonly("oat_file_end",
static_cast<getter_t<uint32_t>>(&Header::oat_file_end)
.def_prop_ro("oat_file_end",
nb::overload_cast<>(&Header::oat_file_end, nb::const_)
)
.def_property_readonly("oat_data_end",
static_cast<getter_t<uint32_t>>(&Header::oat_data_end)
.def_prop_ro("oat_data_end",
nb::overload_cast<>(&Header::oat_data_end, nb::const_)
)
.def_property_readonly("patch_delta",
static_cast<getter_t<int32_t>>(&Header::patch_delta)
.def_prop_ro("patch_delta",
nb::overload_cast<>(&Header::patch_delta, nb::const_)
)
.def_property_readonly("image_roots",
static_cast<getter_t<uint32_t>>(&Header::image_roots)
.def_prop_ro("image_roots",
nb::overload_cast<>(&Header::image_roots, nb::const_)
)
.def_property_readonly("pointer_size",
static_cast<getter_t<uint32_t>>(&Header::pointer_size)
.def_prop_ro("pointer_size",
nb::overload_cast<>(&Header::pointer_size, nb::const_)
)
.def_property_readonly("compile_pic",
static_cast<getter_t<bool>>(&Header::compile_pic)
.def_prop_ro("compile_pic",
nb::overload_cast<>(&Header::compile_pic, nb::const_)
)
.def_property_readonly("nb_sections",
static_cast<getter_t<uint32_t>>(&Header::nb_sections)
.def_prop_ro("nb_sections",
nb::overload_cast<>(&Header::nb_sections, nb::const_)
)
.def_property_readonly("nb_methods",
static_cast<getter_t<uint32_t>>(&Header::nb_methods)
.def_prop_ro("nb_methods",
nb::overload_cast<>(&Header::nb_methods, nb::const_)
)
.def_property_readonly("boot_image_begin",
static_cast<getter_t<uint32_t>>(&Header::boot_image_begin)
.def_prop_ro("boot_image_begin",
nb::overload_cast<>(&Header::boot_image_begin, nb::const_)
)
.def_property_readonly("boot_image_size",
static_cast<getter_t<uint32_t>>(&Header::boot_image_size)
.def_prop_ro("boot_image_size",
nb::overload_cast<>(&Header::boot_image_size, nb::const_)
)
.def_property_readonly("boot_oat_begin",
static_cast<getter_t<uint32_t>>(&Header::boot_oat_begin)
.def_prop_ro("boot_oat_begin",
nb::overload_cast<>(&Header::boot_oat_begin, nb::const_)
)
.def_property_readonly("boot_oat_size",
static_cast<getter_t<uint32_t>>(&Header::boot_oat_size)
.def_prop_ro("boot_oat_size",
nb::overload_cast<>(&Header::boot_oat_size, nb::const_)
)
.def_property_readonly("storage_mode",
static_cast<getter_t<STORAGE_MODES>>(&Header::storage_mode)
.def_prop_ro("storage_mode",
nb::overload_cast<>(&Header::storage_mode, nb::const_)
)
.def_property_readonly("data_size",
static_cast<getter_t<uint32_t>>(&Header::data_size)
.def_prop_ro("data_size",
nb::overload_cast<>(&Header::data_size, nb::const_)
)
.def("__eq__", &Header::operator==)
.def("__ne__", &Header::operator!=)
.def("__hash__",
[] (const Header& header) {
return Hash::hash(header);
})
.def("__str__",
[] (const Header& header)
{
std::ostringstream stream;
stream << header;
std::string str = stream.str();
return str;
});
}
LIEF_DEFAULT_STR(Header);
}
}
+28 -48
View File
@@ -13,68 +13,48 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "pyART.hpp"
#include "LIEF/ART/Parser.hpp"
#include "LIEF/ART/File.hpp"
#include "LIEF/logging.hpp"
#include "ART/pyART.hpp"
#include "pyIOStream.hpp"
#include <string>
#include <memory>
namespace LIEF {
namespace ART {
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/unique_ptr.h>
namespace LIEF::ART::py {
template<>
void create<Parser>(py::module& m) {
void create<Parser>(nb::module_& m) {
using namespace LIEF::py;
// Parser (Parser)
m.def("parse",
static_cast<std::unique_ptr<File> (*) (const std::string&)>(&Parser::parse),
"Parse the given filename and return an " RST_CLASS_REF(lief.ART.File) " object",
nb::overload_cast<const std::string&>(&Parser::parse),
"Parse the given filename and return an " RST_CLASS_REF(lief.ART.File) " object"_doc,
"filename"_a,
py::return_value_policy::take_ownership);
nb::rv_policy::take_ownership);
m.def("parse",
static_cast<std::unique_ptr<File> (*) (std::vector<uint8_t>, const std::string&)>(&Parser::parse),
"Parse the given raw data and return an " RST_CLASS_REF(lief.ART.File) " object",
nb::overload_cast<std::vector<uint8_t>, const std::string&>(&Parser::parse),
"Parse the given raw data and return an " RST_CLASS_REF(lief.ART.File) " object"_doc,
"raw"_a, "name"_a = "",
py::return_value_policy::take_ownership);
nb::rv_policy::take_ownership);
m.def("parse",
[] (py::object byteio, const std::string& name) {
const auto& io = py::module::import("io");
const auto& RawIOBase = io.attr("RawIOBase");
const auto& BufferedIOBase = io.attr("BufferedIOBase");
const auto& TextIOBase = io.attr("TextIOBase");
py::object rawio;
if (py::isinstance(byteio, RawIOBase)) {
rawio = byteio;
}
else if (py::isinstance(byteio, BufferedIOBase)) {
rawio = byteio.attr("raw");
}
else if (py::isinstance(byteio, TextIOBase)) {
rawio = byteio.attr("buffer").attr("raw");
}
else {
throw py::type_error(py::repr(byteio).cast<std::string>().c_str());
}
std::string raw_str = static_cast<py::bytes>(rawio.attr("readall")());
std::vector<uint8_t> raw = {
std::make_move_iterator(std::begin(raw_str)),
std::make_move_iterator(std::end(raw_str))};
return LIEF::ART::Parser::parse(std::move(raw), name);
},
"io"_a, "name"_a = "",
py::return_value_policy::take_ownership);
}
[] (nb::object byteio, const std::string& name) -> nb::object {
if (auto stream = PyIOStream::from_python(std::move(byteio))) {
auto ptr = std::make_unique<PyIOStream>(std::move(*stream));
return nb::cast(Parser::parse(stream->content(), name));
}
logging::log(logging::LOG_ERR, "Can't create a LIEF stream interface over the provided io");
return nb::none();
}, "io"_a, "name"_a = "", nb::rv_policy::take_ownership);
}
}