Skip to content

Commit

Permalink
fix(radare): Fix Radare Support
Browse files Browse the repository at this point in the history
Version 5.8.0 of Radare2 introduced breaking API changes. This commit
fixes them and bumps the minimum Radare2 version requirement.

Furthermore our homegrown Radare CMake Script did not work with newer
Radare versions, so use pkg-config instead.
  • Loading branch information
cvonelm committed Sep 27, 2023
1 parent cc4bb53 commit fcb2f96
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 63 deletions.
13 changes: 5 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ IfUpdatedUnsetAll(lo2s_USE_STATIC_LIBS
Binutils_USE_STATIC_LIBS
OTF2_USE_STATIC_LIBS
OTF2XX_USE_STATIC_LIBS
Radare_USE_STATIC_LIBS
X86Adapt_STATIC
x86_energy_STATIC
)
Expand All @@ -40,7 +39,6 @@ if(lo2s_USE_STATIC_LIBS STREQUAL "OFF")
set(Binutils_USE_STATIC_LIBS OFF CACHE BOOL "")
set(OTF2_USE_STATIC_LIBS OFF CACHE BOOL "")
set(OTF2XX_USE_STATIC_LIBS OFF CACHE BOOL "")
set(Radare_USE_STATIC_LIBS OFF CACHE BOOL "")
set(X86Adapt_STATIC OFF CACHE BOOL "")
set(x86_energy_STATIC OFF CACHE BOOL "")
set(Sensors_USE_STATIC_LIBS OFF CACHE BOOL "")
Expand All @@ -51,7 +49,6 @@ if(lo2s_USE_STATIC_LIBS STREQUAL "MOSTLY")
set(Binutils_USE_STATIC_LIBS ON CACHE BOOL "")
set(OTF2_USE_STATIC_LIBS ON CACHE BOOL "")
set(OTF2XX_USE_STATIC_LIBS ON CACHE BOOL "")
set(Radare_USE_STATIC_LIBS ON CACHE BOOL "")
set(X86Adapt_STATIC ON CACHE BOOL "")
set(x86_energy_STATIC ON CACHE BOOL "")
set(Sensors_USE_STATIC_LIBS ON CACHE BOOL "")
Expand All @@ -63,7 +60,6 @@ if(lo2s_USE_STATIC_LIBS STREQUAL "ALL")
set(Binutils_USE_STATIC_LIBS ON CACHE BOOL "")
set(OTF2_USE_STATIC_LIBS ON CACHE BOOL "")
set(OTF2XX_USE_STATIC_LIBS ON CACHE BOOL "")
set(Radare_USE_STATIC_LIBS ON CACHE BOOL "")
set(X86Adapt_STATIC ON CACHE BOOL "")
set(x86_energy_STATIC ON CACHE BOOL "")
set(Sensors_USE_STATIC_LIBS ON CACHE BOOL "")
Expand Down Expand Up @@ -97,7 +93,6 @@ include(lib/x86_adapt/x86_adapt.cmake)
# find external dependencies
find_package(Git)
find_package(Binutils REQUIRED)
find_package(Radare)
set(THREADS_PREFER_PTHREAD_FLAG true)
find_package(Threads REQUIRED)
find_package(Doxygen COMPONENTS dot)
Expand All @@ -108,11 +103,12 @@ find_package(PkgConfig)

if(PkgConfig_FOUND)
pkg_check_modules(Audit audit)
pkg_check_modules(Radare IMPORTED_TARGET r_main>=5.8.0)
endif()


# configurable options
CMAKE_DEPENDENT_OPTION(USE_RADARE "Enable Radare support." ON "Radare_FOUND" OFF)
CMAKE_DEPENDENT_OPTION(USE_RADARE "Enable Radare support." ON Radare_FOUND OFF)
option(USE_HW_BREAKPOINT_COMPAT "Time synchronization fallback for old kernels without hardware breakpoint support." OFF)
add_feature_info("USE_HW_BREAKPOINT_COMPAT" USE_HW_BREAKPOINT_COMPAT "Time synchronization fallback for old kernels without hardware breakpoint support.")
option(IWYU "Developer option for include what you use." OFF)
Expand Down Expand Up @@ -250,12 +246,12 @@ add_feature_info("x86_energy" x86_energy_FOUND "Provide additional power measure
if (USE_RADARE)
if (Radare_FOUND)
target_compile_definitions(lo2s PUBLIC HAVE_RADARE)
target_link_libraries(lo2s PRIVATE Radare::Radare)
target_link_libraries(lo2s PRIVATE PkgConfig::Radare)
target_sources(lo2s PRIVATE
src/radare.cpp
)
else()
message(SEND_ERROR "Radare not found but requested.")
message(SEND_ERROR "Radare2 not found but requested.")
endif()
endif()

Expand All @@ -280,6 +276,7 @@ if (USE_LIBAUDIT)
endif()
endif()


# generate version string used in lo2s
if(Git_FOUND)
_is_git(${CMAKE_SOURCE_DIR} IN_GIT)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ In both modes, system-level metrics (e.g. tracepoints), are always grouped by th

* [x86_adapt](https://github.com/tud-zih-energy/x86_adapt) for mircorarchitecture specific metrics
* [x86_energy](https://github.com/tud-zih-energy/x86_energy) for CPU power metrics
* libradare for disassembled instruction strings
* libradare (>= 5.8.0) for disassembled instruction strings
* libsensors for sensor readings
* libaudit to resolve syscall names, otherwise only syscall nrs can be used in syscall tracing
* [pod2man](https://www.eyrie.org/~eagle/software/podlators/) to generate the man pages (typically distributed as part of `perl`)
Expand Down
36 changes: 0 additions & 36 deletions cmake/FindRadare.cmake

This file was deleted.

7 changes: 5 additions & 2 deletions include/lo2s/radare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

extern "C"
{
#include <r_asm.h>
#include <libr/r_asm.h>
#include <libr/r_lib.h>

#include <unistd.h>
}
Expand All @@ -51,7 +52,7 @@ class Radare

Radare();

static std::string single_instruction(std::byte* buf);
static std::string single_instruction(char* buf);

std::string operator()(Address ip, std::istream& obj);

Expand All @@ -62,6 +63,8 @@ class Radare
}

private:
RLib* r_lib_;
RAnal* r_anal_;
RAsm* r_asm_;
};

Expand Down
54 changes: 38 additions & 16 deletions src/radare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,42 @@

namespace lo2s
{
Radare::Radare() : r_asm_(r_asm_new())
Radare::Radare() : r_lib_(r_lib_new(nullptr, nullptr)), r_anal_(r_anal_new()), r_asm_(r_asm_new())
{
r_asm_use(r_asm_, "x86");
r_asm_set_bits(r_asm_, 64);
r_asm_set_syntax(r_asm_, R_ASM_SYNTAX_ATT);
r_asm_set_pc(r_asm_, 0x0);
r_unref(r_anal_->config);

r_asm_->num = r_num_new(nullptr, nullptr, nullptr);
r_anal_->config = r_ref_ptr(r_asm_->config);
r_anal_bind(r_anal_, &r_asm_->analb);

r_asm_use(r_asm_, R_SYS_ARCH);
r_anal_use(r_anal_, R_SYS_ARCH);

int sysbits = (R_SYS_BITS & R_SYS_BITS_64) ? 64 : 32;
r_asm_set_bits(r_asm_, sysbits);
r_anal_set_bits(r_anal_, sysbits);
}

std::string Radare::single_instruction(char* buf)
{
auto len = strlen(buf);
if (len == 0)
if (buf == nullptr)
{
throw Error("empty instruction");
throw Error("code->assembly is NULL");
}
for (size_t i = 0; i < len; i++)

auto it = buf;

while (*it != '\0' && *it != '\n')
{
it++;
}

if (it == buf)
{
if (buf[i] == '\n')
{
buf[i] = '\0';
break;
}
throw Error("empty instruction");
}
return std::string(buf);

return std::string(buf, it - 1);
}

std::string Radare::operator()(Address ip, std::istream& obj)
Expand All @@ -66,9 +78,19 @@ std::string Radare::operator()(Address ip, std::istream& obj)
{
throw Error("instruction pointer at end of file");
}

r_asm_set_pc(r_asm_, offset);
auto code = r_asm_mdisassemble(r_asm_, (unsigned char*)buffer, read_bytes);
auto ret = single_instruction(code->buf_asm);

if (code == nullptr)
{
throw Error("could not disassemble instruction");
}

auto ret = single_instruction(code->assembly);

r_asm_code_free(code);

return ret;
}

Expand Down

0 comments on commit fcb2f96

Please sign in to comment.