Skip to content

Commit

Permalink
This commit cleans and modernizes the code base of LIEF.
Browse files Browse the repository at this point in the history
Among the changes:

- It removes the ``and, or, not`` keywords
- It updates the Documentation
- It moves the internal structures in the ``details::`` namespace
  • Loading branch information
rthomas authored and romainthomas committed Jan 27, 2022
1 parent 540049d commit a615adb
Show file tree
Hide file tree
Showing 625 changed files with 15,740 additions and 15,574 deletions.
36 changes: 36 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Checks: >
-*,
bugprone-*,
google-*,
misc-*,
modernize-*,
performance-*,
portability-*,
readability-*,
-bugprone-easily-swappable-parameters,
-bugprone-narrowing-conversions,
-google-readability-namespace-comments,
-google-runtime-int,
-google-runtime-references,
-misc-non-private-member-variables-in-classes,
-modernize-redundant-void-arg,
-modernize-use-trailing-return-type,
-readability-braces-around-statements,
-readability-function-cognitive-complexity,
-readability-magic-numbers,
-readability-named-parameter,
-readability-uppercase-literal-suffix,
-readability-else-after-return,
-google-readability-todo,
-readability-inconsistent-declaration-parameter-name,
-performance-noexcept-move-constructor,
-readability-redundant-string-cstr
# Turn all the warnings from the checks above into errors.
#WarningsAsErrors: "*"
UseColor: false

CheckOptions:
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Authors:

Romain Thomas <[email protected]>
Romain Thomas <[email protected]> - Quarkslab
1 change: 0 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Changelog
=========


see doc/sphinx/changelog.rst

60 changes: 27 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -393,28 +393,24 @@ add_definitions(-DMBEDTLS_FS_IO -DMBEDTLS_PEM_PARSE_C -DMBEDTLS_BIGNUM_C
-DMBEDTLS_X509_CRT_PARSE_C -DMBEDTLS_PEM_WRITE_C
-DMBEDTLS_PKCS1_V15 -DMBEDTLS_PKCS1_V21)

# ASAN - LSAN - TSAN - USAN
# ~~~
# LIEF Sanitizer options
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
set(SANITIZER_FLAGS -fno-omit-frame-pointer -g -O1)
if(LIEF_ASAN)
message(STATUS "Address sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=address)

set_property(
TARGET LIB_LIEF
APPEND
PROPERTY LINK_FLAGS -fsanitize=address)
set_property(TARGET LIB_LIEF
APPEND PROPERTY LINK_FLAGS -fsanitize=address)
target_link_libraries(LIB_LIEF INTERFACE asan)
endif()

if(LIEF_LSAN)
message(STATUS "Leak sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=leak)

set_property(
TARGET LIB_LIEF
APPEND
PROPERTY LINK_FLAGS -fsanitize=leak)
set_property(TARGET LIB_LIEF
APPEND PROPERTY LINK_FLAGS -fsanitize=leak)

target_link_libraries(LIB_LIEF INTERFACE lsan)
endif()
Expand All @@ -423,45 +419,43 @@ if(LIEF_TSAN)
message(STATUS "Thread sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=thread)

set_property(
TARGET LIB_LIEF
APPEND
PROPERTY LINK_FLAGS -fsanitize=thread)
set_property(TARGET LIB_LIEF
APPEND PROPERTY LINK_FLAGS -fsanitize=thread)

target_link_libraries(LIB_LIEF INTERFACE tsan)
endif()

if(LIEF_USAN)
message(STATUS "Undefined sanitizer enabled")
list(APPEND SANITIZER_FLAGS -fsanitize=undefined)
list(APPEND SANITIZER_FLAGS -fsanitize=undefined,null,alignment)

set_property(
TARGET LIB_LIEF
APPEND
PROPERTY LINK_FLAGS -fsanitize=undefined)
set_property(TARGET LIB_LIEF
APPEND PROPERTY LINK_FLAGS -fsanitize=undefined,null,alignment)
target_link_options(LIB_LIEF PUBLIC -fsanitize=undefined,null,alignment)

target_link_libraries(LIB_LIEF INTERFACE ubsan)
target_link_libraries(LIB_LIEF PUBLIC ubsan -fsanitize=undefined,null,alignment)
endif()

if(LIEF_ASAN
OR LIEF_LSAN
OR LIEF_TSAN
OR LIEF_USAN)
target_compile_options(LIB_LIEF PRIVATE ${SANITIZER_FLAGS})
set(LIEF_SANITIZER OFF)
if(LIEF_ASAN OR LIEF_LSAN OR LIEF_TSAN OR LIEF_USAN)
set(LIEF_SANITIZER ON)
endif()

# Fuzzing
# ~~~
if (LIEF_SANITIZER)
message("LIEF Sanitizer enabled: ${SANITIZER_FLAGS}")
target_compile_options(LIB_LIEF PUBLIC ${SANITIZER_FLAGS})
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/tests/sanitizer")
endif()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Fuzzing options
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(LIEF_FUZZING)
set_property(
TARGET LIB_LIEF
APPEND
PROPERTY LINK_FLAGS ${FUZZING_LINKER_FLAGS})
set_property(TARGET LIB_LIEF
APPEND PROPERTY LINK_FLAGS ${FUZZING_LINKER_FLAGS})

target_compile_options(LIB_LIEF PRIVATE ${FUZZING_FLAGS})

target_link_libraries(LIB_LIEF asan pthread)

add_dependencies(LIB_LIEF lief_libfuzzer)
endif()

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ int main(int argc, char** argv) {

## Contact

* **Mail**: lief at quarkslab com
* **Mail**: contact at lief re
* **Gitter**: [lief-project](https://gitter.im/lief-project)

## About
Expand Down
6 changes: 3 additions & 3 deletions api/c/ELF/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void init_c_binary(Elf_Binary_t* c_binary, Binary* binary) {
c_binary->type = static_cast<enum LIEF_ELF_ELF_CLASS>(binary->type());
c_binary->interpreter = nullptr;
if (binary->has_interpreter()) {
std::string interp = binary->interpreter();
const std::string& interp = binary->interpreter();
c_binary->interpreter = static_cast<char*>(malloc((interp.size() + 1) * sizeof(char)));
std::memcpy(
reinterpret_cast<void*>(const_cast<char*>(c_binary->interpreter)),
Expand All @@ -65,7 +65,7 @@ void init_c_binary(Elf_Binary_t* c_binary, Binary* binary) {

Elf_Binary_t* elf_parse(const char *file) {
Binary* binary = Parser::parse(file).release();
Elf_Binary_t* c_binary = static_cast<Elf_Binary_t*>(malloc(sizeof(Elf_Binary_t)));
auto* c_binary = static_cast<Elf_Binary_t*>(malloc(sizeof(Elf_Binary_t)));
memset(c_binary, 0, sizeof(Elf_Binary_t));
init_c_binary(c_binary, binary);
return c_binary;
Expand All @@ -92,7 +92,7 @@ int elf_binary_save_header(Elf_Binary_t* binary) {
hdr.entrypoint(binary->header.entrypoint);

//TODO: identity
return true;
return 1;
}


Expand Down
18 changes: 9 additions & 9 deletions api/c/ELF/DynamicEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
case DYNAMIC_TAGS::DT_NEEDED:
{

Elf_DynamicEntry_Library_t* e = static_cast<Elf_DynamicEntry_Library_t*>(
auto* e = static_cast<Elf_DynamicEntry_Library_t*>(
malloc(sizeof(Elf_DynamicEntry_Library_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -50,7 +50,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {

case DYNAMIC_TAGS::DT_SONAME:
{
Elf_DynamicEntry_SharedObject_t* e = static_cast<Elf_DynamicEntry_SharedObject_t*>(
auto* e = static_cast<Elf_DynamicEntry_SharedObject_t*>(
malloc(sizeof(Elf_DynamicEntry_SharedObject_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -63,7 +63,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {

case DYNAMIC_TAGS::DT_RPATH:
{
Elf_DynamicEntry_Rpath_t* e = static_cast<Elf_DynamicEntry_Rpath_t*>(
auto* e = static_cast<Elf_DynamicEntry_Rpath_t*>(
malloc(sizeof(Elf_DynamicEntry_Rpath_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -77,7 +77,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {

case DYNAMIC_TAGS::DT_RUNPATH:
{
Elf_DynamicEntry_RunPath_t* e = static_cast<Elf_DynamicEntry_RunPath_t*>(
auto* e = static_cast<Elf_DynamicEntry_RunPath_t*>(
malloc(sizeof(Elf_DynamicEntry_RunPath_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -93,7 +93,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
case DYNAMIC_TAGS::DT_FINI_ARRAY:
case DYNAMIC_TAGS::DT_PREINIT_ARRAY:
{
Elf_DynamicEntry_Array_t* e = static_cast<Elf_DynamicEntry_Array_t*>(
auto* e = static_cast<Elf_DynamicEntry_Array_t*>(
malloc(sizeof(Elf_DynamicEntry_Array_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -111,7 +111,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {

case DYNAMIC_TAGS::DT_FLAGS:
{
Elf_DynamicEntry_Flags_t* e = static_cast<Elf_DynamicEntry_Flags_t*>(
auto* e = static_cast<Elf_DynamicEntry_Flags_t*>(
malloc(sizeof(Elf_DynamicEntry_Flags_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -120,7 +120,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
e->flags = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS*>(malloc((flags.size() + 1) * sizeof(enum LIEF_ELF_DYNAMIC_FLAGS)));
e->flags_1 = nullptr;

auto&& it = std::begin(flags);
auto it = std::begin(flags);

for (size_t i = 0; it != std::end(flags); ++i, ++it) {
e->flags[i] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS>(*it);
Expand All @@ -134,7 +134,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {

case DYNAMIC_TAGS::DT_FLAGS_1:
{
Elf_DynamicEntry_Flags_t* e = static_cast<Elf_DynamicEntry_Flags_t*>(
auto* e = static_cast<Elf_DynamicEntry_Flags_t*>(
malloc(sizeof(Elf_DynamicEntry_Flags_t)));

e->tag = static_cast<enum LIEF_ELF_DYNAMIC_TAGS>(entry.tag());
Expand All @@ -143,7 +143,7 @@ void init_c_dynamic_entries(Elf_Binary_t* c_binary, Binary* binary) {
e->flags_1 = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS_1*>(malloc((flags.size() + 1) * sizeof(enum LIEF_ELF_DYNAMIC_FLAGS_1)));
e->flags = nullptr;

auto&& it = std::begin(flags);
auto it = std::begin(flags);

for (size_t i = 0; it != std::end(flags); ++i, ++it) {
e->flags_1[i] = static_cast<enum LIEF_ELF_DYNAMIC_FLAGS_1>(*it);
Expand Down
2 changes: 1 addition & 1 deletion api/c/ELF/Section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void init_c_sections(Elf_Binary_t* c_binary, Binary* binary) {
Section& b_section = sections[i];
c_binary->sections[i] = static_cast<Elf_Section_t*>(malloc(sizeof(Elf_Section_t)));
const std::vector<uint8_t>& section_content = b_section.content();
uint8_t* content = static_cast<uint8_t*>(malloc(section_content.size() * sizeof(uint8_t)));
auto* content = static_cast<uint8_t*>(malloc(section_content.size() * sizeof(uint8_t)));
std::copy(
std::begin(section_content),
std::end(section_content),
Expand Down
2 changes: 1 addition & 1 deletion api/c/ELF/Segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void init_c_segments(Elf_Binary_t* c_binary, Binary* binary) {
Segment& segment = segments[i];

const std::vector<uint8_t>& segment_content = segment.content();
uint8_t* content = static_cast<uint8_t*>(malloc(segment_content.size() * sizeof(uint8_t)));
auto* content = static_cast<uint8_t*>(malloc(segment_content.size() * sizeof(uint8_t)));
std::copy(
std::begin(segment_content),
std::end(segment_content),
Expand Down
2 changes: 1 addition & 1 deletion api/c/MachO/LoadCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void init_c_commands(Macho_Binary_t* c_binary, Binary* binary) {

c_binary->commands[i] = static_cast<Macho_Command_t*>(malloc(sizeof(Macho_Command_t)));
const std::vector<uint8_t>& cmd_content = cmd.data();
uint8_t* content = static_cast<uint8_t*>(malloc(cmd_content.size() * sizeof(uint8_t)));
auto* content = static_cast<uint8_t*>(malloc(cmd_content.size() * sizeof(uint8_t)));
std::copy(
std::begin(cmd_content),
std::end(cmd_content),
Expand Down
2 changes: 1 addition & 1 deletion api/c/MachO/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace LIEF::MachO;
Macho_Binary_t** macho_parse(const char *file) {
FatBinary* macho_binaries = Parser::parse(file).release();

Macho_Binary_t** c_macho_binaries = static_cast<Macho_Binary_t**>(
auto** c_macho_binaries = static_cast<Macho_Binary_t**>(
malloc((macho_binaries->size() + 1) * sizeof(Macho_Binary_t**)));

for (size_t i = 0; i < macho_binaries->size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion api/c/MachO/Section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void init_c_sections(Macho_Binary_t* c_binary, Binary* binary) {

c_binary->sections[i] = static_cast<Macho_Section_t*>(malloc(sizeof(Macho_Section_t)));
const std::vector<uint8_t>& section_content = section.content();
uint8_t* content = static_cast<uint8_t*>(malloc(section_content.size() * sizeof(uint8_t)));
auto* content = static_cast<uint8_t*>(malloc(section_content.size() * sizeof(uint8_t)));
std::copy(
std::begin(section_content),
std::end(section_content),
Expand Down
2 changes: 1 addition & 1 deletion api/c/MachO/Segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void init_c_segments(Macho_Binary_t* c_binary, Binary* binary) {

c_binary->segments[i] = static_cast<Macho_Segment_t*>(malloc(sizeof(Macho_Segment_t)));
const std::vector<uint8_t>& segment_content = segment.content();
uint8_t* content = static_cast<uint8_t*>(malloc(segment_content.size() * sizeof(uint8_t)));
auto* content = static_cast<uint8_t*>(malloc(segment_content.size() * sizeof(uint8_t)));
std::copy(
std::begin(segment_content),
std::end(segment_content),
Expand Down
2 changes: 1 addition & 1 deletion api/c/PE/Binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void init_c_binary(Pe_Binary_t* c_binary, Binary* binary) {

Pe_Binary_t* pe_parse(const char *file) {
Binary* binary = Parser::parse(file).release();
Pe_Binary_t* c_binary = static_cast<Pe_Binary_t*>(malloc(sizeof(Pe_Binary_t)));
auto* c_binary = static_cast<Pe_Binary_t*>(malloc(sizeof(Pe_Binary_t)));
std::memset(c_binary, 0, sizeof(Pe_Binary_t));
init_c_binary(c_binary, binary);

Expand Down
2 changes: 1 addition & 1 deletion api/c/PE/Import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace PE {

void init_c_imports(Pe_Binary_t* c_binary, Binary* binary) {

if (not binary->has_imports()) {
if (!binary->has_imports()) {
c_binary->imports = nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion api/c/PE/Section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void init_c_sections(Pe_Binary_t* c_binary, Binary* binary) {
std::vector<uint8_t> section_content = b_section.content();
uint8_t* content = nullptr;

if (section_content.size() > 0) {
if (!section_content.empty()) {
content = static_cast<uint8_t*>(malloc(section_content.size() * sizeof(uint8_t)));
std::move(
std::begin(section_content),
Expand Down
8 changes: 4 additions & 4 deletions api/python/ART/objects/pyParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void create<Parser>(py::module& m) {

m.def("parse",
[] (py::object byteio, const std::string& name) {
auto&& io = py::module::import("io");
auto&& RawIOBase = io.attr("RawIOBase");
auto&& BufferedIOBase = io.attr("BufferedIOBase");
auto&& TextIOBase = io.attr("TextIOBase");
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;

Expand Down
Loading

0 comments on commit a615adb

Please sign in to comment.