From a65d9996f1a5d9582d9299c03deb883a35ca4c8c Mon Sep 17 00:00:00 2001
From: Jason Hiser <jdhiser@gmail.com>
Date: Thu, 31 Jan 2019 19:23:19 -0500
Subject: [PATCH] refactoring libirdb-util and libirdb-syscall

---
 include/inc-core/basetypes.hpp      | 87 +++++++++++++++--------------
 include/inc-core/type.hpp           |  2 +-
 include/inc-syscall/syscall.hpp     | 39 +++++++++++++
 include/inc-util/IBT_Provenance.hpp | 24 ++++++++
 include/inc-util/Provenance.hpp     | 20 +++++++
 include/inc-util/insn_preds.hpp     | 21 +++++++
 include/inc-util/params.hpp         |  9 +++
 include/inc-util/utils.hpp          | 55 ++++++++++++++++++
 include/irdb-syscall                | 24 +-------
 include/irdb-util                   |  4 +-
 10 files changed, 218 insertions(+), 67 deletions(-)
 create mode 100644 include/inc-syscall/syscall.hpp
 create mode 100644 include/inc-util/IBT_Provenance.hpp
 create mode 100644 include/inc-util/Provenance.hpp
 create mode 100644 include/inc-util/insn_preds.hpp
 create mode 100644 include/inc-util/params.hpp
 create mode 100644 include/inc-util/utils.hpp

diff --git a/include/inc-core/basetypes.hpp b/include/inc-core/basetypes.hpp
index d42be7e..528b10a 100644
--- a/include/inc-core/basetypes.hpp
+++ b/include/inc-core/basetypes.hpp
@@ -3,53 +3,58 @@ namespace IRDB_SDK
 	using namespace std;
 
 	// enums
-	enum ADFileType           { 
-	                                  adftELF, 
-	                                  adftCGC, 
-	                                  adftPE, 
-	                                  adftNone 
-	                          };
+	using ADFileType_t =  enum ADFileType
+		{ 
+		 adftELF, 
+		 adftCGC, 
+		 adftPE, 
+		 adftNone 
+		};
 
-	enum ADMachineType        { 
-	                                   admtAarch64,  
-	                                   admtX86_64, 
-	                                   admtI386, 
-	                                   admtNone 
-	                          };
+	using ADMachineType_t = enum ADMachineType
+		{ 
+		 admtAarch64,  
+		 admtX86_64, 
+		 admtI386, 
+		 admtNone 
+		};
 
-	enum DatabaseErrorType    {
-	                                   detVariantNotInDatabase, 
-	                                   detVariantTableNotRegistered
-	                          };
+	using DatabaseErrorType_t = enum DatabaseErrorType
+    		{
+		 detVariantNotInDatabase, 
+		 detVariantTableNotRegistered
+		};
 
-	enum ICFSAnalysisStatus   { 
-	                                   iasAnalysisIncomplete, 
-	                                   iasAnalysisModuleComplete, 
-	                                   iasAnalysisComplete 
-	                          };
+	using ICFSAnalysisStatus_t = enum ICFSAnalysisStatus
+		{ 
+		 iasAnalysisIncomplete, 
+		 iasAnalysisModuleComplete, 
+		 iasAnalysisComplete 
+		};
 
-	enum IRDBType             {
-	                                   itUnknown,
-	                                   itNumeric,
-	                                   itPointer,
-	                                   itVoid,
-	                                   itVariadic,
-	                                   itInt,
-	                                   itChar,
-	                                   itFloat,
-	                                   itDouble,
-	                                   itTypedef,
-	                                   itSubtype,
-	                                   itFunc,
-	                                   itAggregate
-	                          };
+	using IRDBType_t = enum IRDBType
+		{
+		 itUnknown,
+		 itNumeric,
+		 itPointer,
+		 itVoid,
+		 itVariadic,
+		 itInt,
+		 itChar,
+		 itFloat,
+		 itDouble,
+		 itTypedef,
+		 itSubtype,
+		 itFunc,
+		 itAggregate
+		};
 
 	// enum renames
-	using ADFileType_t         = enum ADFileType;
-	using ADMachineType_t      = enum ADMachineType;
-	using DatabaseErrorType_t  = enum DatabaseErrorType;
-	using ICFSAnalysisStatus_t = enum ICFSAnalysisStatus; 
-	using IRDBType_t           = enum IRDBType;
+	//using ADFileType_t         = enum ADFileType;
+	//using ADMachineType_t      = enum ADMachineType;
+	//using DatabaseErrorType_t  = enum DatabaseErrorType;
+	//using ICFSAnalysisStatus_t = enum ICFSAnalysisStatus; 
+	//using IRDBType_t           = enum IRDBType;
 
 
 
diff --git a/include/inc-core/type.hpp b/include/inc-core/type.hpp
index df9ea1e..bd1aaed 100644
--- a/include/inc-core/type.hpp
+++ b/include/inc-core/type.hpp
@@ -21,7 +21,7 @@ namespace IRDB_SDK
 			virtual bool       isPointerType()   const = 0;
 			virtual bool       isNumericType()   const = 0;
 
-			virtual void       setTypeID(IRDBType t)          = 0;
+			virtual void       setTypeID(IRDBType_t ty)       = 0;
 			virtual void       setName(const string& newname) = 0;
 
 	};
diff --git a/include/inc-syscall/syscall.hpp b/include/inc-syscall/syscall.hpp
new file mode 100644
index 0000000..bd35112
--- /dev/null
+++ b/include/inc-syscall/syscall.hpp
@@ -0,0 +1,39 @@
+
+
+namespace IRDB_SDK
+{
+	using namespace std;
+	using  SyscallNumber_t = enum SyscallNumber
+		{
+			sntUnknown=-1,
+		};
+
+	class  SyscallSite_t
+	{
+		protected:
+			SyscallSite_t() { }
+			SyscallSite_t(const SyscallSite_t& copy) = delete;
+		public:
+			virtual ~SyscallSite_t() { }
+			virtual Instruction_t*  getSyscallSite()   const = 0 ;
+			virtual Instruction_t*  getSite()          const = 0 ;
+			virtual SyscallNumber_t getSyscallNumber() const = 0 ;
+	};
+
+	using SyscallSiteSet_t = set<SyscallSite_t*>;
+
+	class Syscalls_t
+	{
+		protected:
+			Syscalls_t() { }
+			Syscalls_t(const Syscalls_t& copy) = delete;
+		public:
+			virtual ~Syscalls_t() { }
+		// getters
+			virtual const SyscallSiteSet_t& getSyscalls() const = 0;
+		// factories
+			static unique_ptr<Syscalls_t> factory(FileIR_t *the_firp); 
+	};
+
+}
+
diff --git a/include/inc-util/IBT_Provenance.hpp b/include/inc-util/IBT_Provenance.hpp
new file mode 100644
index 0000000..d306e7c
--- /dev/null
+++ b/include/inc-util/IBT_Provenance.hpp
@@ -0,0 +1,24 @@
+
+namespace IRDB_SDK
+{
+
+	class IBTProvenance_t
+	{
+
+		public:
+			IBTProvenance_t() {}
+			IBTProvenance_t(const IBTProvenance_t& copy) = delete;
+		public:
+
+			virtual ~IBTProvenance_t() {} 	
+
+			static unique_ptr<IBTProvenance_t> factory(const FileIR_t* f=NULL); 
+
+			virtual const Provenance_t& getProvenance(const Instruction_t* i)  const = 0 ;
+			virtual const Provenance_t& operator[] (const Instruction_t* i)    const = 0 ;
+	 
+
+	};
+
+}
+
diff --git a/include/inc-util/Provenance.hpp b/include/inc-util/Provenance.hpp
new file mode 100644
index 0000000..cb5b9ff
--- /dev/null
+++ b/include/inc-util/Provenance.hpp
@@ -0,0 +1,20 @@
+
+namespace IRDB_SDK
+{
+	using namespace std;
+
+	class Provenance_t
+	{
+		protected:
+			Provenance_t() {} 
+			Provenance_t(const Provenance_t& copy) = delete;
+
+		public:
+			virtual ~Provenance_t() {}
+
+			virtual bool hasReturn()       const = 0;
+			virtual bool hasIndirectJump() const = 0;
+			virtual bool hasIndirectCall() const = 0;
+	};		
+
+}
diff --git a/include/inc-util/insn_preds.hpp b/include/inc-util/insn_preds.hpp
new file mode 100644
index 0000000..a95ca6b
--- /dev/null
+++ b/include/inc-util/insn_preds.hpp
@@ -0,0 +1,21 @@
+namespace IRDB_SDK
+{
+
+	class InstructionPredecessors_t
+	{
+		protected:
+			InstructionPredecessors_t() { }
+			InstructionPredecessors_t(const InstructionPredecessors_t& copy) = delete;
+		public:
+			virtual ~InstructionPredecessors_t() { } 
+
+		// getters
+			virtual const InstructionSet_t& getPredecessors(const Instruction_t* i)  const = 0;
+			virtual const InstructionSet_t& operator[] (const Instruction_t* i)     const = 0;
+
+		// factories
+			static unique_ptr<InstructionPredecessors_t> factory(FileIR_t* firp=nullptr);
+
+	};
+
+}
diff --git a/include/inc-util/params.hpp b/include/inc-util/params.hpp
new file mode 100644
index 0000000..69703cb
--- /dev/null
+++ b/include/inc-util/params.hpp
@@ -0,0 +1,9 @@
+
+namespace IRDB_SDK
+{
+	extern bool isParameterWrite(const FileIR_t *firp, Instruction_t* insn, std::string& output_dst);
+	extern bool callFollows(const FileIR_t *firp, Instruction_t* insn, const std::string& arg_str, const std::string & = "");
+	extern bool leaFlowsIntoCall(const FileIR_t *firp, Instruction_t* insn);
+	extern bool leaFlowsIntoPrintf(const FileIR_t *firp, Instruction_t* insn);
+	extern bool flowsIntoCall(const FileIR_t *firp, Instruction_t* insn);
+}
diff --git a/include/inc-util/utils.hpp b/include/inc-util/utils.hpp
new file mode 100644
index 0000000..1c84ea2
--- /dev/null
+++ b/include/inc-util/utils.hpp
@@ -0,0 +1,55 @@
+
+namespace IRDB_SDK
+{
+
+	using namespace std;
+
+	template <class T>
+	inline string to_hex_string (const T& t)
+	{
+		auto ss=stringstream();
+		ss << hex << t;
+		return ss.str();
+	}
+
+	 
+
+	template<class T> inline T strtoint(const string& s)
+	{
+		auto str=stringstream(s);
+		auto off=T();
+
+		str >> off;
+
+		return off;
+	}
+
+	// nb: relies on srand() being previously set 
+	template <typename IterType, typename Funct>
+	inline Funct for_randomOrder_each(const IterType &b, const IterType & e, const Funct &callback)
+	{
+		auto m=map<int,const typename iterator_traits<IterType>::value_type *>();
+		for_each(b,e, [&](const typename iterator_traits<IterType>::value_type & o)
+		{
+		       while(true)
+		       {
+			       auto rn=rand();
+			       if(m.find(rn)==m.end())
+			       {
+				       m[rn]=&o;
+				       break;
+			       }
+		       }
+		});
+		for_each(m.begin(), m.end(), [&](const pair<int,const typename iterator_traits<IterType>::value_type *> &p)
+		{
+		       callback(*p.second);
+		});
+		return callback;
+	}
+
+
+
+}
+
+
diff --git a/include/irdb-syscall b/include/irdb-syscall
index 8710b77..7724d86 100644
--- a/include/irdb-syscall
+++ b/include/irdb-syscall
@@ -1,30 +1,8 @@
-/*
- * Copyright (c) 2014 - Zephyr Software LLC
- *
- * This file may be used and modified for non-commercial purposes as long as
- * all copyright, permission, and nonwarranty notices are preserved.
- * Redistribution is prohibited without prior written consent from Zephyr
- * Software.
- *
- * Please contact the authors for restrictions applying to commercial use.
- *
- * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Author: Zephyr Software
- * e-mail: jwd@zephyr-software.com
- * URL   : http://www.zephyr-software.com/
- *
- */
 
 #ifndef IRDB_SDK_syscall
 #define IRDB_SDK_syscall
 
-
-/* Building a CFG depends on core functionality */
-#include <IRDB_SDK-core.hpp>
-#include <IRDB_SDK-util.hpp>
+#include <irdb-core>
 
 #include <vector>
 #include <set>
diff --git a/include/irdb-util b/include/irdb-util
index 17def90..a658784 100644
--- a/include/irdb-util
+++ b/include/irdb-util
@@ -22,13 +22,13 @@
 #define IRDB_SDK_util
 
 /* Building a CFG depends on core functionality */
-#include <IRDB_SDK-core.hpp>
-#include <vector>
 #include <set>
 #include <map>
 #include <ostream>
 #include <inc-util/insn_preds.hpp>
+#include <inc-util/Provenance.hpp>
 #include <inc-util/IBT_Provenance.hpp>
 #include <inc-util/params.hpp>
+#include <inc-util/utils.hpp>
 
 #endif
-- 
GitLab