Skip to content

Commit

Permalink
create new serialization/deserialization library
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMatula committed Nov 7, 2019
1 parent 4f277a0 commit e74bf6b
Show file tree
Hide file tree
Showing 23 changed files with 613 additions and 280 deletions.
15 changes: 14 additions & 1 deletion cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ option(RETDEC_ENABLE_PDBPARSER "" OFF)
option(RETDEC_ENABLE_RETDEC "" OFF)
option(RETDEC_ENABLE_RETDECTOOL "" OFF)
option(RETDEC_ENABLE_RTTI_FINDER "" OFF)
option(RETDEC_ENABLE_SERDES "" OFF)
option(RETDEC_ENABLE_STACOFIN "" OFF)
option(RETDEC_ENABLE_STACOFINTOOL "" OFF)
option(RETDEC_ENABLE_UNPACKER "" OFF)
Expand Down Expand Up @@ -98,6 +99,7 @@ foreach(t ${RETDEC_ENABLE})
set_if_equal(${t} "retdec" RETDEC_ENABLE_RETDEC)
set_if_equal(${t} "retdectool" RETDEC_ENABLE_RETDEC)
set_if_equal(${t} "rtti-finder" RETDEC_ENABLE_RTTI_FINDER)
set_if_equal(${t} "serdes" RETDEC_ENABLE_SERDES)
set_if_equal(${t} "stacofin" RETDEC_ENABLE_STACOFIN)
set_if_equal(${t} "stacofintool" RETDEC_ENABLE_STACOFINTOOL)
set_if_equal(${t} "unpacker" RETDEC_ENABLE_UNPACKER)
Expand Down Expand Up @@ -142,6 +144,7 @@ if (RETDEC_ENABLE_AR_EXTRACTOR
OR RETDEC_ENABLE_RETDEC
OR RETDEC_ENABLE_RETDECTOOL
OR RETDEC_ENABLE_RTTI_FINDER
OR RETDEC_ENABLE_SERDES
OR RETDEC_ENABLE_STACOFIN
OR RETDEC_ENABLE_STACOFINTOOL
OR RETDEC_ENABLE_UNPACKER
Expand Down Expand Up @@ -357,6 +360,10 @@ set_if_at_least_one_set(RETDEC_ENABLE_CONFIG
RETDEC_ENABLE_LLVMIR2HLL
RETDEC_ENABLE_STACOFIN)

set_if_at_least_one_set(RETDEC_ENABLE_SERDES
RETDEC_ENABLE_ALL
RETDEC_ENABLE_CONFIG)

set_if_at_least_one_set(RETDEC_ENABLE_COMMON
RETDEC_ENABLE_ALL
RETDEC_ENABLE_BIN2LLVMIR
Expand All @@ -366,6 +373,7 @@ set_if_at_least_one_set(RETDEC_ENABLE_COMMON
RETDEC_ENABLE_FILEFORMAT
RETDEC_ENABLE_LOADER
RETDEC_ENABLE_RTTI_FINDER
RETDEC_ENABLE_SERDES
RETDEC_ENABLE_STACOFIN)

set_if_at_least_one_set(RETDEC_ENABLE_UTILS
Expand Down Expand Up @@ -425,6 +433,9 @@ set_if_all_set(RETDEC_ENABLE_LLVMIR2HLL_TESTS
set_if_all_set(RETDEC_ENABLE_LOADER_TESTS
RETDEC_TESTS
RETDEC_ENABLE_LOADER)
set_if_all_set(RETDEC_ENABLE_SERDES_TESTS
RETDEC_TESTS
RETDEC_ENABLE_SERDES)
set_if_all_set(RETDEC_ENABLE_UNPACKER_TESTS
RETDEC_TESTS
RETDEC_ENABLE_UNPACKER)
Expand Down Expand Up @@ -459,12 +470,14 @@ set_if_at_least_one_set(RETDEC_ENABLE_GOOGLETEST
RETDEC_ENABLE_LLVMIR_EMUL_TESTS
RETDEC_ENABLE_LLVMIR2HLL_TESTS
RETDEC_ENABLE_LOADER_TESTS
RETDEC_ENABLE_SERDES_TESTS
RETDEC_ENABLE_UNPACKER_TESTS
RETDEC_ENABLE_UTILS_TESTS)

set_if_at_least_one_set(RETDEC_ENABLE_JSONCPP
RETDEC_ENABLE_CONFIG
RETDEC_ENABLE_FILEINFO)
RETDEC_ENABLE_FILEINFO
RETDEC_ENABLE_SERDES)

set_if_at_least_one_set(RETDEC_ENABLE_KEYSTONE
RETDEC_ENABLE_CAPSTONE2LLVMIRTOOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ParamReturn : public llvm::ModulePass
void collectExtraData(CallEntry* ce) const;

void collectCallSpecificTypes(CallEntry* ce) const;
config::CallingConventionID toCallConv(const std::string &cc) const;
common::CallingConventionID toCallConv(const std::string &cc) const;

// Collection of functions usage data.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <llvm/IR/Module.h>

#include "retdec/config/calling_convention.h"
#include "retdec/common/calling_convention.h"

namespace retdec {
namespace bin2llvmir {
Expand All @@ -24,7 +24,7 @@ class CallingConvention
//
public:
typedef std::unique_ptr<CallingConvention> Ptr;
typedef retdec::config::CallingConventionID ID;
typedef retdec::common::CallingConventionID ID;

typedef Ptr (*ConstructorMethod)(const Abi*);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* @file include/retdec/config/calling_convention.h
* @brief Decompilation configuration manipulation: calling convention.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
* @file include/retdec/common/calling_convention.h
* @brief Calling convention representation.
* @copyright (c) 2019 Avast Software, licensed under the MIT license
*/

#ifndef RETDEC_CONFIG_CALLING_CONVENTION_H
#define RETDEC_CONFIG_CALLING_CONVENTION_H
#ifndef RETDEC_COMMON_CALLING_CONVENTION_H
#define RETDEC_COMMON_CALLING_CONVENTION_H

#include <string>

#include "retdec/config/base.h"

namespace retdec {
namespace config {
namespace common {

/**
* Represents functions' calling conventions.
Expand All @@ -21,8 +21,43 @@ namespace config {
*/
class CallingConvention
{
public:
enum class eCC
{
CC_UNKNOWN = 0,
CC_VOIDARG,
CC_CDECL,
CC_ELLIPSIS,
CC_STDCALL,
CC_PASCAL,
CC_FASTCALL,
CC_THISCALL,
CC_MANUAL,
CC_SPOILED,
CC_SPECIALE,
CC_SPECIALP,
CC_SPECIAL,
CC_WATCOM,
CC_X64,
CC_ARM,
CC_ARM64,
CC_MIPS,
CC_MIPS64,
CC_POWERPC,
CC_POWERPC64,
CC_PIC32,
CC_ENDING,
};

friend std::ostream& operator<<(
std::ostream& out,
const eCC& cc);

public:
CallingConvention();
CallingConvention(eCC cc);

CallingConvention& operator=(const eCC& cc);

/// @name Calling convention named constructors.
/// @{
Expand Down Expand Up @@ -56,6 +91,7 @@ class CallingConvention
bool isSpecialE() const;
bool isSpecialP() const;
bool isSpecial() const;
eCC getID() const;
/// @}

/// @name Calling convention set methods.
Expand All @@ -73,57 +109,21 @@ class CallingConvention
void setIsSpecialE();
void setIsSpecialP();
void setIsSpecial();
void set(eCC cc);
/// @}

bool operator<(const CallingConvention& cc) const;
friend std::ostream& operator<< (std::ostream &out, const CallingConvention& cc);

Json::Value getJsonValue() const;
void readJsonValue(const Json::Value& val);

public:
enum class eCallingConvention
{
CC_UNKNOWN = 0,
CC_VOIDARG,
CC_CDECL,
CC_ELLIPSIS,
CC_STDCALL,
CC_PASCAL,
CC_FASTCALL,
CC_THISCALL,
CC_MANUAL,
CC_SPOILED,
CC_SPECIALE,
CC_SPECIALP,
CC_SPECIAL,
CC_WATCOM,
CC_X64,
CC_ARM,
CC_ARM64,
CC_MIPS,
CC_MIPS64,
CC_POWERPC,
CC_POWERPC64,
CC_PIC32,
CC_ENDING,
};

eCallingConvention getID() const;

friend std::ostream& operator<< (std::ostream &out, const eCallingConvention& cc);
CallingConvention& operator=(const eCallingConvention& cc);

private:
CallingConvention(eCallingConvention cc);
friend std::ostream& operator<<(
std::ostream &out,
const CallingConvention& cc);

private:
eCallingConvention _callingConvention = eCallingConvention::CC_UNKNOWN;
eCC _cc = eCC::CC_UNKNOWN;
};

typedef CallingConvention::eCallingConvention CallingConventionID;
typedef CallingConvention::eCC CallingConventionID;

} // namespace config
} // namespace common
} // namespace retdec

#endif
2 changes: 1 addition & 1 deletion include/retdec/common/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class RangeContainer
const RangeElementType& s,
const RangeElementType& e)
{
return insert(s, e);
return insert(RangeType(s, e));
}

const RangeType* getRange(const RangeElementType& e) const
Expand Down
4 changes: 2 additions & 2 deletions include/retdec/config/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include <string>

#include "retdec/common/calling_convention.h"
#include "retdec/config/base.h"
#include "retdec/config/calling_convention.h"
#include "retdec/config/objects.h"
#include "retdec/config/storage.h"
#include "retdec/config/types.h"
Expand Down Expand Up @@ -103,7 +103,7 @@ class Function : public retdec::common::AddressRange
bool operator!=(const Function& o) const;

public:
CallingConvention callingConvention;
common::CallingConvention callingConvention;
Storage returnStorage;
Storage frameBaseStorage; // TODO - serialization
Type returnType;
Expand Down
26 changes: 26 additions & 0 deletions include/retdec/serdes/calling_convention.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file include/retdec/serdes/calling_convention.h
* @brief Calling convention (de)serialization.
* @copyright (c) 2019 Avast Software, licensed under the MIT license
*/

#ifndef RETDEC_SERDES_CALLING_CONVENTION_H
#define RETDEC_SERDES_CALLING_CONVENTION_H

#include <json/json.h>

namespace retdec {

namespace common {
class CallingConvention;
} // namespace common

namespace serdes {

Json::Value serialize(const common::CallingConvention& cc);
common::CallingConvention deserialize(const Json::Value& val);

} // namespace serdes
} // namespace retdec

#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cond_add_subdirectory(pdbparser RETDEC_ENABLE_PDBPARSER)
cond_add_subdirectory(retdec RETDEC_ENABLE_RETDEC)
cond_add_subdirectory(retdectool RETDEC_ENABLE_RETDECTOOL)
cond_add_subdirectory(rtti-finder RETDEC_ENABLE_RTTI_FINDER)
cond_add_subdirectory(serdes RETDEC_ENABLE_SERDES)
cond_add_subdirectory(stacofin RETDEC_ENABLE_STACOFIN)
cond_add_subdirectory(stacofintool RETDEC_ENABLE_STACOFINTOOL)
cond_add_subdirectory(unpacker RETDEC_ENABLE_UNPACKER)
Expand Down
18 changes: 9 additions & 9 deletions src/bin2llvmir/optimizations/param_return/param_return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,18 @@ DataFlowEntry ParamReturn::createDataFlowEntry(Value* calledValue) const
return dataflow;
}

config::CallingConventionID ParamReturn::toCallConv(const std::string &cc) const
common::CallingConventionID ParamReturn::toCallConv(const std::string &cc) const
{
std::map<std::string, config::CallingConventionID> ccMap {
{"cdecl", config::CallingConventionID::CC_CDECL},
{"pascal", config::CallingConventionID::CC_PASCAL},
{"thiscall", config::CallingConventionID::CC_THISCALL},
{"stdcall", config::CallingConventionID::CC_STDCALL},
{"fastcall", config::CallingConventionID::CC_FASTCALL},
{"eabi", config::CallingConventionID::CC_ARM}
std::map<std::string, common::CallingConventionID> ccMap {
{"cdecl", common::CallingConventionID::CC_CDECL},
{"pascal", common::CallingConventionID::CC_PASCAL},
{"thiscall", common::CallingConventionID::CC_THISCALL},
{"stdcall", common::CallingConventionID::CC_STDCALL},
{"fastcall", common::CallingConventionID::CC_FASTCALL},
{"eabi", common::CallingConventionID::CC_ARM}
}; // TODO add vectorcall and regcall

return utils::mapGetValueOrDefault(ccMap, cc, config::CallingConventionID::CC_UNKNOWN);
return utils::mapGetValueOrDefault(ccMap, cc, common::CallingConventionID::CC_UNKNOWN);
}

void ParamReturn::collectExtraData(DataFlowEntry* dataflow) const
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

add_library(retdec-common STATIC
address.cpp
calling_convention.cpp
)
target_include_directories(retdec-common PUBLIC ${PROJECT_SOURCE_DIR}/include/)
Loading

0 comments on commit e74bf6b

Please sign in to comment.