├── libFlowMatrix ├── readme.md ├── include │ ├── ProtoRuleDB.hpp │ ├── FMCommon.hpp │ ├── matrix_base.hpp │ ├── isa.h │ ├── FMGranularity.hpp │ └── VState.hpp ├── testsuite │ ├── vstate_fmtrace.cpp │ └── vstate_init.cpp ├── src │ ├── acorns_obj.proto │ ├── ProtoRuleDB.cpp │ ├── FMCommon.cpp │ ├── matrix_base.cpp │ └── VState.cpp └── Makefile ├── trace ├── README.md └── FMtrace │ ├── include │ ├── common.hpp │ └── trace.hpp │ ├── Makefile │ └── src │ ├── common.cpp │ ├── FMTraceReader.cpp │ └── trace.cpp ├── FMDynamic ├── include │ ├── cli │ │ ├── readme.md │ │ ├── boostasioscheduler.h │ │ ├── boostasioremotecli.h │ │ ├── standaloneasioscheduler.h │ │ ├── boostasiocliasyncsession.h │ │ ├── standaloneasioremotecli.h │ │ ├── standaloneasiocliasyncsession.h │ │ ├── detail │ │ │ ├── boostasiolib.h │ │ │ ├── standaloneasiolib.h │ │ │ ├── keyboard.h │ │ │ ├── inputdevice.h │ │ │ ├── commonprefix.h │ │ │ ├── oldboostasiolib.h │ │ │ ├── oldstandaloneasiolib.h │ │ │ ├── newstandaloneasiolib.h │ │ │ ├── newboostasiolib.h │ │ │ ├── genericasioscheduler.h │ │ │ ├── genericcliasyncsession.h │ │ │ ├── winkeyboard.h │ │ │ ├── inputhandler.h │ │ │ ├── linuxkeyboard.h │ │ │ ├── server.h │ │ │ ├── history.h │ │ │ ├── terminal.h │ │ │ ├── rang.h │ │ │ ├── split.h │ │ │ └── fromstring.h │ │ ├── historystorage.h │ │ ├── scheduler.h │ │ ├── colorprofile.h │ │ ├── volatilehistorystorage.h │ │ ├── clifilesession.h │ │ ├── clilocalsession.h │ │ ├── filehistorystorage.h │ │ └── loopscheduler.h │ ├── FMQuery.hpp │ ├── FMRule.hpp │ ├── SyscallHook.hpp │ ├── DBCore.hpp │ └── QueryCLI.hpp ├── src │ ├── QueryCLI │ │ └── Worker.cpp │ └── TraceLoader │ │ └── TraceGenerator.cpp └── Makefile ├── examples ├── sample1 │ └── sample1.asm └── sample2 │ └── sample2.c ├── Makefile └── FMCuSparseLinAlg ├── testsuite ├── spgeam_test_gen.py ├── main_diag_test.cu ├── coo_csr_convert_test.cu ├── spgemm_test.cu └── spgeam_test.cu ├── Makefile └── include ├── cuSparseMatrix.cuh ├── cuSparseCore.cuh └── cuCommon.cuh /libFlowMatrix/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /trace/README.md: -------------------------------------------------------------------------------- 1 | # FMTrace 2 | The trace format FlowMatrix supports. 3 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/readme.md: -------------------------------------------------------------------------------- 1 | This is build from CLI library. 2 | See https://github.com/daniele77/cli for details. -------------------------------------------------------------------------------- /examples/sample1/sample1.asm: -------------------------------------------------------------------------------- 1 | ; This is a very simple example for getting started 2 | ; with FlowMatrix Query utilities. You don't have to 3 | ; compile this file as it only contains two move 4 | ; instructions. If you wish to do so, you will need 5 | ; as31 and nasm. 6 | ; Compile with following: 7 | ; nasm -f elf64 sample1.asm 8 | ; ld -s -o sample1 sample1.o 9 | 10 | section .text 11 | global _start 12 | _start: 13 | mov rbx, rdx 14 | mov rcx, rbx 15 | 16 | section .data 17 | -------------------------------------------------------------------------------- /libFlowMatrix/include/ProtoRuleDB.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __LIBFLOWMATRIX_PROTORULEDB_H_ 2 | #define __LIBFLOWMATRIX_PROTORULEDB_H_ 3 | 4 | #include "acorns_obj.pb.h" 5 | #include "FMCommon.hpp" 6 | #include "isa.h" 7 | 8 | namespace FlowMatrix{ 9 | 10 | class ProtoRuleDB 11 | { 12 | private: 13 | std::string path; 14 | std::string arch; 15 | 16 | public: 17 | // Constructor and destructor 18 | ProtoRuleDB(const std::string &dbPath, const std::string arch); 19 | ~ProtoRuleDB(); 20 | 21 | // Rule utilities 22 | int loadRule(const std::string raw_bytes, acorn_obj::TaintRule &rule); 23 | 24 | 25 | }; 26 | 27 | } // End of namespace FlowMatrix 28 | 29 | #endif // __LIBFLOWMATRIX_PROTORULEDB_H_ -------------------------------------------------------------------------------- /libFlowMatrix/testsuite/vstate_fmtrace.cpp: -------------------------------------------------------------------------------- 1 | #include "VState.hpp" 2 | #include "trace.hpp" 3 | 4 | using namespace FlowMatrix; 5 | 6 | int main() 7 | { 8 | // Load FM trace 9 | trace_handle_t *trace_handle = Trace::LoadTrace("../../testsuite/ls_fmtrace/"); 10 | 11 | // Init vstate with default settings 12 | VState *vstate = new VState(FMGranularity::byte); 13 | vstate->init_reg_state(FMRegListConfig_default); 14 | 15 | // Extend vstate with mem accesses in FM trace 16 | mem_ref_t mem_ref; 17 | while (trace_handle->mem_ref_set.ReadNext(mem_ref)) 18 | { 19 | vstate->append(mem_ref.mem_ref_addr, 8); 20 | } 21 | 22 | // Print vstate 23 | vstate->pp(); 24 | 25 | // Close FM trace 26 | Trace::CloseTrace(trace_handle); 27 | } 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | FMDynamic_Path = ./FMDynamic/ 2 | LibFlowMatrix_Path = ./libFlowMatrix/ 3 | FMCuSparseLinAlg_Path = ./FMCuSparseLinAlg/ 4 | FMTrace_path = ./trace/FMtrace/ 5 | 6 | all: FMDynamic 7 | .PHONY: clean FMDynamic LibFlowMatrix 8 | 9 | LibFlowMatrix: 10 | 11 | FMDynamic: 12 | mkdir -p ./bin 13 | (cd $(LibFlowMatrix_Path) && make -j) 14 | (cd $(FMDynamic_Path) && make -j) 15 | sync -f $(FMDynamic_Path)/bin/QueryCLI 16 | cp $(FMDynamic_Path)/bin/QueryCLI ./bin 17 | sync -f $(FMDynamic_Path)/bin/TraceLoader 18 | cp $(FMDynamic_Path)/bin/TraceLoader ./bin 19 | 20 | 21 | clean: 22 | rm -rf ./bin 23 | (cd $(FMDynamic_Path) && make clean) 24 | (cd $(LibFlowMatrix_Path) && make clean) 25 | (cd $(FMCuSparseLinAlg_Path) && make clean) 26 | (cd $(FMTrace_path) && make clean) 27 | 28 | -------------------------------------------------------------------------------- /libFlowMatrix/testsuite/vstate_init.cpp: -------------------------------------------------------------------------------- 1 | #include "VState.hpp" 2 | 3 | using namespace FlowMatrix; 4 | 5 | int main() 6 | { 7 | std::cout << "Bit vstate: " << std::endl; 8 | VState *vstate_bit = new VState(FMGranularity::bit); 9 | vstate_bit->init_reg_state(FMRegListConfig_default); 10 | vstate_bit->pp(); 11 | delete vstate_bit; 12 | 13 | std::cout << "\nByte vstate: " << std::endl; 14 | VState *vstate_byte = new VState(FMGranularity::byte); 15 | vstate_byte->init_reg_state(FMRegListConfig_default); 16 | vstate_byte->pp(); 17 | delete vstate_byte; 18 | 19 | std::cout << "\nQword vstate: " << std::endl; 20 | VState *vstate_qword = new VState(FMGranularity::qword); 21 | vstate_qword->init_reg_state(FMRegListConfig_default); 22 | vstate_qword->pp(); 23 | delete vstate_qword; 24 | } -------------------------------------------------------------------------------- /libFlowMatrix/include/FMCommon.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __LIBFLOWMATRIX_FMCOMMON_H_ 2 | #define __LIBFLOWMATRIX_FMCOMMON_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||" 20 | #define PBWIDTH 60 21 | 22 | #define LOG(...) {printf("%s", currentDateTime().c_str()); printf(__VA_ARGS__); printf("\n");} 23 | #define DIE() {printf("Abort at %s:%d\n", __FILE__, __LINE__); exit(1);} 24 | 25 | const std::string currentDateTime(); 26 | 27 | 28 | 29 | #endif // __LIBFLOWMATRIX_FMCOMMON_H_ -------------------------------------------------------------------------------- /libFlowMatrix/include/matrix_base.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __LIBFLOWMATRIX_MATRIXBASE_H__ 2 | #define __LIBFLOWMATRIX_MATRIXBASE_H__ 3 | 4 | #include 5 | 6 | typedef float value_t; 7 | 8 | class MatrixBase 9 | { 10 | public: 11 | bool isOnDevice; 12 | bool hasVal; 13 | bool isCSR; // True for CSR, False for COO 14 | 15 | int m; // The number of rows in the matrix. 16 | int n; // The number of columns in the matrix. 17 | int nnz; // The number of nonzero elements in the matrix. 18 | 19 | int *row; // CSR: size = m + 1; COO: size = nnz 20 | int *col; // size = nnz 21 | value_t *val; // size = nnz 22 | 23 | MatrixBase(); 24 | 25 | int to_coo(); 26 | int to_csr(); 27 | void print(); 28 | 29 | int store4py(const std::string &filename_prefix) const; 30 | }; 31 | 32 | #endif // __LIBFLOWMATRIX_MATRIXBASE_H__ 33 | -------------------------------------------------------------------------------- /libFlowMatrix/include/isa.h: -------------------------------------------------------------------------------- 1 | /* KH: This is an auto-generated file from Squirrel ISA. Do Not edit. */ 2 | #ifndef _ISA_H_ 3 | #define _ISA_H_ 4 | 5 | #include 6 | 7 | bool isMemRead(const unsigned int memVal); 8 | unsigned int getMemSize(const unsigned int memVal); 9 | unsigned int getMemSlot(const unsigned int memVal); 10 | bool isMemValue(const unsigned int memVal); 11 | std::string mem2str(const unsigned int memVal); 12 | int getRegName(const std::string arch, const unsigned int reg_id, std::string ®_name); 13 | int getRegSize(const std::string arch, const unsigned int reg_id, unsigned int ®_size); 14 | int getRegId(const std::string arch, const std::string reg_name, unsigned int ®_id); 15 | int getEncloseReg(const std::string arch, const unsigned int reg_id, unsigned int &enclose_reg_id); 16 | int getRegId_peekapoo(const std::string arch, const std::string reg_name, unsigned int ®_id); 17 | 18 | #endif -------------------------------------------------------------------------------- /examples/sample2/sample2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Source Code for 'sample2' trace. 3 | 4 | This is a simple example for how FlowMatrix 5 | deals with real execution traces. Sample2 6 | simply read this file by a read syscall 7 | and dump to sample2.out with a write 8 | syscall. 9 | 10 | However, in real execution, there are more 11 | than one read and one write syscall 12 | 13 | Compile with: 14 | $ gcc ./sample2.c -o sample2 15 | */ 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | int main() 22 | { 23 | char buffer[1024]; 24 | int fd = open("sample2.c", O_RDONLY); 25 | int length = read(fd, buffer, 1024); 26 | close(fd); 27 | printf("Read %d bytes.\n", length); 28 | fd = open("sample2.out", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 29 | length = write(fd,buffer,length); 30 | printf("Write %d bytes.\n", length); 31 | close(fd); 32 | } 33 | -------------------------------------------------------------------------------- /libFlowMatrix/include/FMGranularity.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __LIBFLOWMATRIX_FMGRANULARITY_H__ 2 | #define __LIBFLOWMATRIX_FMGRANULARITY_H__ 3 | 4 | #include 5 | 6 | // #define GetSizeWithinGranu(size, granularity) (size / (1 << ((unsigned long) granularity)*3)) 7 | #define PrintGranularity(granularity) {std::cout << "Granularity: "; if (granularity==0) std::cout << "Bit\n"; if (granularity==1) std::cout << "Byte\n"; if (granularity==2) std::cout << "Qword\n";} 8 | namespace FlowMatrix 9 | { 10 | 11 | enum FMGranularity : unsigned char 12 | { 13 | bit=0, 14 | byte=1, 15 | qword=2, /*Currently not supported*/ 16 | }; 17 | 18 | inline uint64_t GetSizeWithinGranu(uint64_t sizeInBit, enum FMGranularity granularity) 19 | { 20 | uint64_t RealSize = sizeInBit / (1 << ((unsigned long) granularity)*3); 21 | if (RealSize==0 && sizeInBit >0) 22 | return 1; 23 | else 24 | return RealSize; 25 | } 26 | 27 | } // namespace FlowMatrix 28 | 29 | #endif // __LIBFLOWMATRIX_FMGRANULARITY_H__ -------------------------------------------------------------------------------- /libFlowMatrix/src/acorns_obj.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package acorn_obj; 4 | 5 | message StateFormat { 6 | string archstring = 1; 7 | repeated uint32 reg_list = 2; 8 | repeated uint32 mem_list = 3; 9 | } 10 | 11 | message Condition { 12 | enum CondOp { 13 | INVALID_COND = 0; 14 | DNF = 1; 15 | LOGIC = 2; 16 | CMP = 3; 17 | } 18 | CondOp cond_op = 1; 19 | oneof criteria { 20 | DNFCriteria dnf_criteria = 2; 21 | LogicCriteria logic_criteria = 3; 22 | CMPCriteria cmp_criteria = 4; 23 | } 24 | } 25 | 26 | message DNFCriteria { 27 | repeated DNFPair dnf_pairs = 1; 28 | } 29 | 30 | message DNFPair { 31 | uint64 mask = 1; 32 | uint64 criteria = 2; 33 | } 34 | 35 | message LogicCriteria { 36 | } 37 | 38 | message CMPCriteria { 39 | } 40 | 41 | message TaintRule { 42 | StateFormat state_format = 1; 43 | Condition condition = 2; 44 | repeated UseDef use_def = 3; 45 | } 46 | 47 | message UseDef { 48 | uint32 use = 1; 49 | repeated uint32 defs = 2; 50 | } 51 | -------------------------------------------------------------------------------- /libFlowMatrix/src/ProtoRuleDB.cpp: -------------------------------------------------------------------------------- 1 | #include "ProtoRuleDB.hpp" 2 | 3 | namespace FlowMatrix 4 | { 5 | 6 | ProtoRuleDB::ProtoRuleDB(const std::string &dbPath, const std::string arch) 7 | { 8 | // Check ProtoBuf Version 9 | GOOGLE_PROTOBUF_VERIFY_VERSION; 10 | 11 | // Set path 12 | this->path = dbPath; 13 | this->arch = arch; 14 | } 15 | 16 | ProtoRuleDB::~ProtoRuleDB() 17 | { 18 | // Shut down ProtoBuf lib 19 | google::protobuf::ShutdownProtobufLibrary(); 20 | } 21 | 22 | int ProtoRuleDB::loadRule(const std::string raw_bytes, acorn_obj::TaintRule &rule) 23 | { 24 | std::string filePath; 25 | filePath = this->path+"/"+this->arch+"/"+raw_bytes+".bin"; 26 | std::fstream input(filePath, std::ios::in | std::ios::binary); 27 | if ( (!input) || !rule.ParseFromIstream(&input)) 28 | { 29 | LOG("[W] Fail to load rule %s with arch %s", raw_bytes.c_str(), arch.c_str()); 30 | input.close(); 31 | return -1; 32 | } 33 | input.close(); 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | } // End of namespace FlowMatrix -------------------------------------------------------------------------------- /FMDynamic/src/QueryCLI/Worker.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace FlowMatrix; 9 | 10 | void printUsage(char **argv) 11 | { 12 | printf("Usage: %s \n", argv[0]); 13 | } 14 | 15 | int main(int argc, char **argv) 16 | { 17 | int pid = getpid(); 18 | 19 | if (argc != 6) 20 | { 21 | printUsage(argv); 22 | exit(0); 23 | } 24 | 25 | // Init DB 26 | std::string traceName(argv[3]); 27 | std::string dBPath(argv[4]); 28 | DBCore *db = new DBCore(dBPath, traceName); 29 | 30 | // Init Query Handle 31 | int start = atoi(argv[1]); 32 | int end = atoi(argv[2]); 33 | int storageLevel = atoi(argv[5]); 34 | 35 | LOG("%d: Prepare query tree on range (%d, %d)", pid, start, end); 36 | FMQuery *query = new FMQuery(dBPath, traceName, end, start, end); 37 | query->BuildTree(storageLevel, 1, true); 38 | 39 | #ifdef TIME_MEASUREMENT 40 | query->sp_core->printTimer(); 41 | db->printTimer(); 42 | #endif 43 | 44 | delete query; 45 | delete db; 46 | } -------------------------------------------------------------------------------- /FMCuSparseLinAlg/testsuite/spgeam_test_gen.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.sparse import csr_matrix, coo_matrix 3 | 4 | m = 4 5 | n = 4 6 | hA_csrOffsets = [ 0, 3, 4, 7, 9 ] 7 | hA_columns = [ 0, 2, 3, 1, 0, 2, 3, 1, 3 ] 8 | hA_values = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0] 9 | matrixA = csr_matrix((hA_values, hA_columns, hA_csrOffsets), shape=(m, n)) 10 | print("Matrix A:") 11 | print(matrixA.toarray()) 12 | matrixA_coo = matrixA.tocoo() 13 | matrixA = matrixA.tocsr() 14 | print(matrixA_coo.row) 15 | print(matrixA_coo.col) 16 | print(matrixA_coo.data) 17 | print("Matrix B:") 18 | hB_csrOffsets = [ 0, 2, 4, 7, 8 ] 19 | hB_columns = [ 0, 3, 1, 3, 0, 1, 2, 1 ] 20 | hB_values = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] 21 | matrixB = csr_matrix((hB_values, hB_columns, hB_csrOffsets), shape=(m, n)) 22 | print(matrixB.toarray()) 23 | matrixC = (matrixA-matrixB).tocsr() 24 | print("Matrix C:") 25 | print(matrixC.toarray()) 26 | print(matrixC.indptr) 27 | print(matrixC.indices) 28 | print(matrixC.data) 29 | hD_cooOffsets = [1, 2, 3, 4] 30 | hD_columns = [1, 2, 3, 4] 31 | hD_values = [1.0, 1.0, 1.0, 1.0] 32 | matrixD = coo_matrix((hD_values, (hD_cooOffsets, hD_columns)), shape=(6, 6)).tocsr() 33 | print("Matrix D:") 34 | print(matrixD.toarray()) 35 | print(matrixD.indptr) 36 | print(matrixD.indices) 37 | print(matrixD.data) -------------------------------------------------------------------------------- /FMCuSparseLinAlg/Makefile: -------------------------------------------------------------------------------- 1 | AR ?= ar 2 | ARFLAGS ?= -rcs 3 | RANLIB ?= ranlib 4 | 5 | CUDA_TOOLKIT := $(shell dirname $$(command -v nvcc))/.. 6 | INC += -I$(CUDA_TOOLKIT)/include -I./include 7 | LIBS += -lcudart -lcusparse 8 | CFLAGS ?= -O3 -g -G -Wno-deprecated-declarations 9 | 10 | SRC_PATH = ./src/ 11 | 12 | OBJ := $(notdir $(patsubst %.cu,%.o,$(wildcard $(SRC_PATH)*.cu))) 13 | TARGET := $(notdir $(patsubst %.cu,%,$(wildcard testsuite/*.cu))) 14 | TESTS := testsuite/spgemm_test testsuite/spgeam_test testsuite/coo_csr_convert_test testsuite/main_diag_test 15 | STATIC_LIB := libfmlinalg.a 16 | all: $(TESTS) $(STATIC_LIB) 17 | 18 | %.o: $(SRC_PATH)/%.cu 19 | nvcc $(INC) $(CFLAGS) -c $< 20 | 21 | testsuite/%.o: testsuite/%.cu 22 | nvcc $(INC) $(CFLAGS) -c $< -o $@ 23 | 24 | testsuite/%: testsuite/%.o $(OBJ) 25 | nvcc $(INC) $^ -o $@ $(LIBS) 26 | 27 | $(STATIC_LIB): $(OBJ) 28 | mkdir -p ./lib 29 | $(AR) $(ARFLAGS) $@ $^ 30 | $(RANLIB) $@ 31 | mv $@ ./lib 32 | 33 | clean: 34 | rm -f $(TESTS) 35 | $(RM) -rf ./lib 36 | rm -f *.o 37 | rm -f testsuite/*.o 38 | 39 | test: 40 | @echo "\n==== 1. SpGEMM Test ====\n" 41 | @./testsuite/spgemm_test 42 | @echo "\n==== 2. SpGEAM Test ====\n" 43 | @./testsuite/spgeam_test 44 | @echo "\n==== 3. COO&CSR Conversion Test ====\n" 45 | @./testsuite/coo_csr_convert_test 46 | @echo "\n==== 4. Set Main Diag Test ====\n" 47 | @./testsuite/main_diag_test 48 | 49 | .PHONY: clean test -------------------------------------------------------------------------------- /FMDynamic/include/FMQuery.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_UTIL_DYNAMIC_QUERY_H_ 2 | #define __FLOWMATRIX_UTIL_DYNAMIC_QUERY_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define THREAD_NUM_LIMIT 8 10 | 11 | int AlignAndMultiply( 12 | FlowMatrix::CuSparseCore *sp_core, 13 | FlowMatrix::cuSparseMatrix &MatrixA, 14 | int num_valid_indices_A, 15 | int *valid_indices_A, 16 | FlowMatrix::cuSparseMatrix &MatrixB, 17 | int num_valid_indices_B, 18 | int *valid_indices_B, 19 | FlowMatrix::cuSparseMatrix *&productMatrix_ptr, 20 | int &superset_num_indices, 21 | int *&superset_indices, 22 | int left = 0, // Only for debugging 23 | int right = 0); 24 | 25 | namespace FlowMatrix 26 | { 27 | 28 | 29 | class FMQuery 30 | { 31 | public: 32 | int thread_num; 33 | int traceLen, startPos, endPos; 34 | std::string traceName, dbPath; 35 | DBCore *db; 36 | CuSparseCore *sp_core; 37 | private: 38 | int GetTaskScale(); 39 | public: 40 | FMQuery(std::string dbPath, std::string traceName, int traceLen, int startPos = 1, int endPos = 0, bool always_use_safe_spgemm = true); 41 | ~FMQuery(); 42 | 43 | cuSparseMatrix *Query(int left, int right); 44 | void BuildTree(int num_not_stored_layer = 0, int thread_num = THREAD_NUM_LIMIT, bool always_use_safe_spgemm = false); 45 | 46 | }; 47 | 48 | } // End of namespace 49 | 50 | #endif -------------------------------------------------------------------------------- /trace/FMtrace/include/common.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_COMMON_H_ 2 | #define __FLOWMATRIX_COMMON_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #ifndef MAX 19 | #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 20 | #endif 21 | 22 | #ifndef MIN 23 | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 24 | #endif 25 | 26 | #ifndef PBSTR 27 | #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||" 28 | #define PBWIDTH 60 29 | #endif 30 | 31 | #ifndef LOG 32 | #define LOG(...) {printf("%s", currentDateTime().c_str()); printf(__VA_ARGS__); printf("\n");} 33 | #endif 34 | #ifndef DIE 35 | #define DIE() {printf("Abort at %s:%d\n", __FILE__, __LINE__); exit(1);} 36 | #endif 37 | 38 | #ifndef isFloatEqual 39 | #define isFloatEqual(a, b) (((a) < (b+0.005)) && ((a) > (b-0.005))) 40 | #endif 41 | 42 | const std::string currentDateTime(); 43 | 44 | void printProgress(double percentage); 45 | 46 | std::string uint8_to_hex_string(const uint8_t *uint8_arrary, const uint64_t size); 47 | int disassemble_one_instr_x86(uint8_t *raw_bytes, uint32_t len, std::string &result_asm, size_t addr = 0x0); 48 | 49 | 50 | namespace FlowMatrix{ 51 | 52 | typedef struct sysent { 53 | unsigned int nargs; 54 | const char *sys_name; 55 | } struct_syscall_info; 56 | 57 | }; 58 | 59 | #endif -------------------------------------------------------------------------------- /FMCuSparseLinAlg/include/cuSparseMatrix.cuh: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_UTILS_CU_SPARSEMATRIX_H_ 2 | #define __FLOWMATRIX_UTILS_CU_SPARSEMATRIX_H_ 3 | 4 | #include 5 | #include 6 | 7 | namespace FlowMatrix{ 8 | 9 | // Sparse Matrix in CSR format. By default it's on device. 10 | class cuSparseMatrix 11 | { 12 | public: 13 | cusparseSpMatDescr_t matDescr; 14 | int64_t num_rows, num_cols, nnz; 15 | index_type_t *d_csrOffsets, *d_cooOffsets, *d_columns; 16 | value_type_t *d_values; 17 | index_type_t *h_csrOffsets, *h_cooOffsets, *h_columns; 18 | value_type_t *h_values; 19 | 20 | cuSparseMatrix(); 21 | cuSparseMatrix(int num_rows, int num_cols); 22 | cuSparseMatrix(cudaStream_t &stream, int num_rows, int num_cols, int nnz, int *csrOffsets, int *columns, float *values); 23 | ~cuSparseMatrix(); 24 | 25 | void fromDB(cusparseHandle_t &handle, int num_rows, int num_cols, int nnz, const int *cooOffsets, const int *columns, const float *values); 26 | 27 | void toCoo(cusparseHandle_t &handle, bool keepCsr = false); 28 | void toCsr(cusparseHandle_t &handle, bool keepCoo = false); 29 | 30 | void setMainDiagonal(cusparseHandle_t &handle, int num_rows, int num_cols, int nnz, int *indices, float value = 1.0f); 31 | void setMatrixByDevicePtrs(int num_rows, int num_cols, int nnz, int *d_csrOffsets, int *d_columns, float *d_values); 32 | void toDevice(cudaStream_t &stream); 33 | void toHost(cudaStream_t &stream, bool keepOnDevice = false); 34 | void print() const; 35 | 36 | void freeDeviceMem(cudaStream_t &stream); 37 | }; 38 | 39 | } // End of namespace 40 | #endif -------------------------------------------------------------------------------- /FMCuSparseLinAlg/testsuite/main_diag_test.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace FlowMatrix; 4 | 5 | int main(){ 6 | const int A_num_rows = 6; 7 | const int A_num_cols = 6; 8 | const int A_nnz = 4; 9 | int hA_csrOffsets[] = { 0, 0, 1, 2, 3, 4, 4 }; 10 | int hA_columns[] = { 1, 2, 3, 4}; 11 | float hA_values[] = { 1.0f, 1.0f, 1.0f, 1.0f,}; 12 | int indices[] = { 1, 2, 3, 4}; 13 | 14 | // Init cusparse handler 15 | auto sp_core = new CuSparseCore(); 16 | 17 | cuSparseMatrix matA; 18 | matA.setMainDiagonal(sp_core->handle, A_num_rows, A_num_cols, A_nnz, indices, 1.0f); 19 | 20 | // To host memory 21 | matA.toHost(sp_core->stream); 22 | sp_core->sync(); 23 | 24 | // Verification 25 | int correct = 1; 26 | if (matA.nnz != A_nnz || matA.num_rows != A_num_rows || matA.num_cols != A_num_cols) 27 | { 28 | correct = 0; 29 | } 30 | for (int i = 0; i < matA.num_rows + 1; i++) { 31 | if (matA.h_csrOffsets[i] != hA_csrOffsets[i]) 32 | { 33 | correct = 0; 34 | break; 35 | } 36 | } 37 | for (int i = 0; i < matA.nnz; i++) { 38 | if (matA.h_columns[i] != hA_columns[i] || 39 | matA.h_values[i] != hA_values[i]) 40 | { 41 | correct = 0; 42 | break; 43 | } 44 | } 45 | if (correct) 46 | printf("Set Main Diag test PASSED\n"); 47 | else { 48 | printf("Set Main Diag test FAILED: Wrong Result\n"); 49 | return EXIT_FAILURE; 50 | } 51 | } -------------------------------------------------------------------------------- /trace/FMtrace/Makefile: -------------------------------------------------------------------------------- 1 | # Command ard arguments 2 | CC ?= gcc 3 | CPPC ?= g++ 4 | RM ?= rm 5 | MKDIR ?= mkdir 6 | AR ?= ar 7 | ARFLAGS ?= -rcs # ar needs the dash on OpenBSD 8 | RANLIB ?= ranlib 9 | 10 | 11 | # Path 12 | PROJ_HOME := . 13 | BIN_PATH := $(PROJ_HOME)/bin 14 | LIB_PATH := $(PROJ_HOME)/lib 15 | SRC_PATH := $(PROJ_HOME)/src 16 | INC_PATH := $(PROJ_HOME)/include 17 | 18 | 19 | # Compilation variables 20 | OPT ?= -O3 21 | WARNINGS = -Wall -Wextra 22 | CPP_STD = -std=c++17 23 | PIC_FLAG ?= -fPIC 24 | CFLAGS ?= $(OPT) $(WARNINGS) $(CPP_STD) $(PIC_FLAG) 25 | INC += -I$(PROJ_HOME)/include 26 | 27 | # Linking variables 28 | LDLIBS += -lcapstone 29 | 30 | # Lib flags 31 | SOLIB_FLAGS ?= -Wl,-soname,libfmtrace.so 32 | 33 | # Target 34 | PROG := $(BIN_PATH)/FMTraceReader 35 | 36 | OBJ := $(notdir $(patsubst %.cpp,%.o,$(wildcard $(SRC_PATH)/*.cpp))) 37 | 38 | all : libfmtrace.so libfmtrace.a $(BIN_PATH)/FMTraceReader clean_tmp 39 | 40 | libfmtrace.so: $(filter-out FMTraceReader.o,$(OBJ)) 41 | @-$(MKDIR) -p $(LIB_PATH) 42 | $(CPPC) -shared $(SOLIB_FLAGS) -o $(LIB_PATH)/$@ $(strip $(CFLAGS) $(LDLIBS) $^) 43 | 44 | libfmtrace.a: $(filter-out FMTraceReader.o,$(OBJ)) 45 | @-$(MKDIR) -p $(LIB_PATH) 46 | $(AR) $(ARFLAGS) $(LIB_PATH)/$@ $^ 47 | $(RANLIB) $(LIB_PATH)/$@ 48 | 49 | $(BIN_PATH)/FMTraceReader: $(OBJ) 50 | @-$(MKDIR) -p $(BIN_PATH) 51 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDLIBS)) 52 | 53 | %.o: $(SRC_PATH)/%.cpp 54 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $< 55 | 56 | 57 | .PHONY: clean create_folder clean_tmp 58 | create_folder: 59 | @-$(MKDIR) -p $(BIN_PATH) 60 | @-$(MKDIR) -p $(LIB_PATH) 61 | clean: 62 | $(RM) *.o *.gch 63 | $(RM) -rf ./bin 64 | $(RM) -rf ./lib 65 | clean_tmp: 66 | @-$(RM) *.o *.gch 67 | 68 | -------------------------------------------------------------------------------- /trace/FMtrace/src/common.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const std::string currentDateTime() { 4 | time_t now = time(0); 5 | struct tm tstruct; 6 | char buf[80]; 7 | tstruct = *localtime(&now); 8 | strftime(buf, sizeof(buf), "%Y-%m-%d.%X ", &tstruct); 9 | 10 | return buf; 11 | } 12 | 13 | void printProgress(double percentage) { 14 | int val = (int) (percentage * 100); 15 | int lpad = (int) (percentage * PBWIDTH); 16 | int rpad = PBWIDTH - lpad; 17 | printf("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, ""); 18 | fflush(stdout); 19 | } 20 | 21 | std::string uint8_to_hex_string(const uint8_t *uint8_arrary, const uint64_t size) 22 | { 23 | std::stringstream ss; 24 | ss << std::hex << std::setfill('0'); 25 | for (uint64_t i = 0; i < size; i++) { 26 | ss << std::hex << std::setw(2) << static_cast(uint8_arrary[i]); 27 | } 28 | return ss.str(); 29 | } 30 | 31 | /* Disassemble and print one instruction from buffer. Return size of this instruction */ 32 | int disassemble_one_instr_x86(uint8_t *raw_bytes, uint32_t len, std::string &result_asm, size_t addr /* = 0x0 */) 33 | { 34 | csh handle; 35 | cs_insn *insn; 36 | size_t count; 37 | std::stringstream ss; 38 | 39 | 40 | if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) 41 | return -1; 42 | count = cs_disasm(handle, raw_bytes, len, addr, 0, &insn); 43 | if (count > 0) { 44 | ss << std::hex< 2 | 3 | const std::string currentDateTime() { 4 | time_t now = time(0); 5 | struct tm tstruct; 6 | char buf[80]; 7 | tstruct = *localtime(&now); 8 | strftime(buf, sizeof(buf), "%Y-%m-%d.%X ", &tstruct); 9 | 10 | return buf; 11 | } 12 | 13 | void printProgress(double percentage) { 14 | int val = (int) (percentage * 100); 15 | int lpad = (int) (percentage * PBWIDTH); 16 | int rpad = PBWIDTH - lpad; 17 | printf("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, ""); 18 | fflush(stdout); 19 | } 20 | 21 | std::string uint8_to_hex_string(const uint8_t *uint8_arrary, const uint64_t size) 22 | { 23 | std::stringstream ss; 24 | ss << std::hex << std::setfill('0'); 25 | for (uint64_t i = 0; i < size; i++) { 26 | ss << std::hex << std::setw(2) << static_cast(uint8_arrary[i]); 27 | } 28 | return ss.str(); 29 | } 30 | 31 | /* Disassemble and print one instruction from buffer. Return size of this instruction */ 32 | int disassemble_one_instr_x86(uint8_t *raw_bytes, uint32_t len, std::string &result_asm, size_t addr /* = 0x0 */) 33 | { 34 | csh handle; 35 | cs_insn *insn; 36 | size_t count; 37 | std::stringstream ss; 38 | 39 | 40 | if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) 41 | return -1; 42 | count = cs_disasm(handle, raw_bytes, len, addr, 0, &insn); 43 | if (count > 0) { 44 | ss << std::hex< 5 | 6 | 7 | namespace FlowMatrix 8 | { 9 | 10 | 11 | class CuSparseCore 12 | { 13 | #ifdef TIME_MEASUREMENT 14 | private: 15 | unsigned long total_nnz; 16 | int spgemm_count, spgemm_safe_count; 17 | std::chrono::duration spgemm_timer, spgemm_safe_timer; 18 | public: 19 | void resetTimer(); 20 | void printTimer(); 21 | #endif // TIME_MEASUREMENT 22 | 23 | // Member variables 24 | private: 25 | // Prepare two buffers for spgemm and add 26 | void *buffer1, *buffer2; 27 | size_t buffer1_size, buffer2_size; 28 | 29 | // Shared Matrix Desc. This is not Cusparse Matrix Desc. 30 | cusparseMatDescr_t matDescr; 31 | 32 | public: 33 | // Provide two streams and attached handles 34 | cudaStream_t stream, associate_stream; 35 | cusparseHandle_t handle, associate_handle; 36 | 37 | 38 | 39 | // SPGEMM function pointer 40 | // Usage: 41 | // rvalue = (this->*(sp_core->spgemm))(__args__); 42 | cuSparseMatrix* (CuSparseCore::*spgemm)(cuSparseMatrix &, cuSparseMatrix &, value_type_t); 43 | 44 | // Member functions: 45 | public: 46 | CuSparseCore(bool always_use_safe = false); 47 | ~CuSparseCore(); 48 | 49 | // Sync both streams 50 | void sync(); 51 | 52 | // Sparse matrix add sparse matrix 53 | // matC = alpha*matA + belta*matB 54 | cuSparseMatrix* add(cuSparseMatrix &matA, cuSparseMatrix &matB, float alpha = 1.0f, float belta = 1.0f, bool use_associate = false); 55 | 56 | // Two implementation of SPGEMM. _spgemm_safe is faster but deprecated :( 57 | cuSparseMatrix* _spgemm(cuSparseMatrix &matA, cuSparseMatrix &matB, value_type_t alpha = 1.0f); 58 | cuSparseMatrix* _spgemm_safe(cuSparseMatrix &matA, cuSparseMatrix &matB, value_type_t alpha = 1.0f); 59 | }; 60 | 61 | } 62 | 63 | 64 | 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /FMDynamic/include/FMRule.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_UTIL_DYNAMIC_FMRULE_H_ 2 | #define __FLOWMATRIX_UTIL_DYNAMIC_FMRULE_H_ 3 | 4 | #include "ProtoRuleDB.hpp" 5 | #include "FMGranularity.hpp" 6 | #include "trace.hpp" 7 | #include "VState.hpp" 8 | #include 9 | 10 | 11 | namespace FlowMatrix 12 | { 13 | 14 | typedef struct _coo_pair_t{ 15 | int col; 16 | int row; 17 | 18 | bool operator<(const struct _coo_pair_t &other) const 19 | { 20 | return ((row < other.row) || (row == other.row && col < other.col) ); 21 | } 22 | } coo_pair_t; 23 | 24 | class FMRule 25 | { 26 | public: 27 | // Arr 28 | int matrix_size, nnz; 29 | int *csr_row, *csr_col; 30 | int *coo_row; 31 | float *value; 32 | bool isCoo; 33 | 34 | // Meta 35 | FMGranularity granu; 36 | instr_t instr_info; 37 | std::set overrided_set; 38 | 39 | public: 40 | /* In dynamic view, we initialize FMRule with: 41 | * 1. TaintInduce Rule in protobuf format 42 | * 2. The instruction info, including eflags and mem addr 43 | * 3. The superset that the rule needs to be mapped to 44 | */ 45 | FMRule(const acorn_obj::TaintRule &rule, 46 | const instr_t &instr_info, 47 | VState &vstate, 48 | bool isCoo, 49 | int &num_valid_indices, 50 | int *&valid_indices); 51 | FMRule(const instr_t &instr_info, VState &vstate); 52 | ~FMRule(); 53 | 54 | static void getSuperSet(const int num_indices_1, const int *indices_1, 55 | const int num_indices_2, const int *indices_2, 56 | int &superset_num_indices, int *&superset_indices, 57 | int &num_missing_indices_1, int *&missing_indices_1, 58 | int &num_missing_indices_2, int *&missing_indices_2); 59 | 60 | void print(VState &vstate); 61 | void store4py(const std::string &dir_path) const; 62 | void coo(int &matrix_size, int &nnz, int* &row, int* &col, float* &value); 63 | }; 64 | 65 | } // End of namespace FlowMatrix 66 | 67 | #endif // __FLOWMATRIX_UTIL_DYNAMIC_FMRULE_H_ 68 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/boostasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOSCHEDULER_H_ 31 | #define CLI_BOOSTASIOSCHEDULER_H_ 32 | 33 | #include "detail/genericasioscheduler.h" 34 | #include "detail/boostasiolib.h" 35 | 36 | namespace cli { using BoostAsioScheduler = detail::GenericAsioScheduler; } 37 | 38 | #endif // CLI_BOOSTASIOSCHEDULER_H_ 39 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/boostasioremotecli.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOREMOTECLI_H_ 31 | #define CLI_BOOSTASIOREMOTECLI_H_ 32 | 33 | #include "detail/boostasiolib.h" 34 | #include "detail/genericasioremotecli.h" 35 | 36 | namespace cli { using BoostAsioCliTelnetServer = detail::CliGenericTelnetServer; } 37 | 38 | #endif // CLI_BOOSTASIOREMOTECLI_H_ 39 | 40 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/standaloneasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOSCHEDULER_H_ 31 | #define CLI_STANDALONEASIOSCHEDULER_H_ 32 | 33 | #include "detail/genericasioscheduler.h" 34 | #include "detail/standaloneasiolib.h" 35 | 36 | namespace cli { using StandaloneAsioScheduler = detail::GenericAsioScheduler; } 37 | 38 | #endif // CLI_STANDALONEASIOSCHEDULER_H_ 39 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/boostasiocliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_BOOSTASIOCLIASYNCSESSION_H_ 31 | #define CLI_BOOSTASIOCLIASYNCSESSION_H_ 32 | 33 | #include "detail/genericcliasyncsession.h" 34 | #include "detail/boostasiolib.h" 35 | 36 | 37 | namespace cli { using BoostAsioCliAsyncSession = detail::GenericCliAsyncSession; } 38 | 39 | #endif // CLI_BOOSTASIOCLIASYNCSESSION_H_ 40 | 41 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/standaloneasioremotecli.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOREMOTECLI_H_ 31 | #define CLI_STANDALONEASIOREMOTECLI_H_ 32 | 33 | #include "detail/standaloneasiolib.h" 34 | #include "detail/genericasioremotecli.h" 35 | 36 | namespace cli { using StandaloneAsioCliTelnetServer = detail::CliGenericTelnetServer; } 37 | 38 | 39 | #endif // CLI_STANDALONEASIOREMOTECLI_H_ 40 | 41 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/standaloneasiocliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_STANDALONEASIOCLIASYNCSESSION_H_ 31 | #define CLI_STANDALONEASIOCLIASYNCSESSION_H_ 32 | 33 | #include "detail/genericcliasyncsession.h" 34 | #include "detail/standaloneasiolib.h" 35 | 36 | 37 | namespace cli { using StandaloneAsioCliAsyncSession = detail::GenericCliAsyncSession; } 38 | 39 | #endif // CLI_STANDALONEASIOCLIASYNCSESSION_H_ 40 | 41 | -------------------------------------------------------------------------------- /FMCuSparseLinAlg/testsuite/coo_csr_convert_test.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace FlowMatrix; 4 | 5 | int main(){ 6 | const int A_num_rows = 4; 7 | const int A_num_cols = 4; 8 | const int A_nnz = 9; 9 | // const int B_num_rows = 4; 10 | // const int B_num_cols = 4; 11 | // const int B_nnz = 9; 12 | int hA_csrOffsets[] = { 0, 3, 4, 7, 9 }; 13 | int hA_columns[] = { 0, 2, 3, 1, 0, 2, 3, 1, 3 }; 14 | float hA_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 15 | 6.0f, 7.0f, 8.0f, 9.0f }; 16 | int hB_cooOffsets[] = { 0, 0, 0, 1, 2, 2, 2, 3, 3}; 17 | int hB_columns[] = { 0, 2, 3, 1, 0, 2, 3, 1, 3}; 18 | float hB_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 19 | 6.0f, 7.0f, 8.0f, 9.0f }; 20 | // const int D_nnz = 8; 21 | 22 | // Init cusparse handler 23 | auto sp_core = new CuSparseCore(); 24 | 25 | // Create matrix 26 | cuSparseMatrix matA(sp_core->stream, A_num_rows, A_num_cols, A_nnz, hA_csrOffsets, hA_columns, hA_values); 27 | 28 | // Convert to coo 29 | matA.toCoo(sp_core->handle, false); 30 | matA.toCsr(sp_core->handle, true); 31 | 32 | // To host memory 33 | matA.toHost(sp_core->stream); 34 | sp_core->sync(); 35 | 36 | // Verification 37 | int correct = 1; 38 | if (matA.nnz != A_nnz || matA.num_rows != A_num_rows || matA.num_cols != A_num_cols) 39 | { 40 | correct = 0; 41 | } 42 | for (int i = 0; i < matA.num_rows + 1; i++) { 43 | if (matA.h_csrOffsets[i] != hA_csrOffsets[i]) 44 | { 45 | correct = 0; 46 | break; 47 | } 48 | } 49 | for (int i = 0; i < matA.nnz; i++) { 50 | if (matA.h_columns[i] != hB_columns[i] || 51 | matA.h_values[i] != hB_values[i] || 52 | matA.h_cooOffsets[i] != hB_cooOffsets[i]) 53 | { 54 | correct = 0; 55 | break; 56 | } 57 | } 58 | if (correct) 59 | printf("COO&CSR Conversion test PASSED\n"); 60 | else { 61 | printf("COO&CSR Conversion test FAILED: Wrong Result\n"); 62 | return EXIT_FAILURE; 63 | } 64 | } -------------------------------------------------------------------------------- /trace/FMtrace/src/FMTraceReader.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool print_instr = false; 4 | bool print_syscall = false; 5 | bool print_mem_ref_set = false; 6 | 7 | void print_usage(const char* program_name) 8 | { 9 | fprintf(stderr, "Usage: %s [Options] trace_name\n", program_name); 10 | fprintf(stderr, "Options:\n"); 11 | fprintf(stderr, " -s \tPrint syscall trace.\n"); 12 | fprintf(stderr, " -i \tPrint instruction trace.\n"); 13 | fprintf(stderr, " -m \tPrint mem access address set.\n"); 14 | fprintf(stderr, " -h \tPrint this help.\n"); 15 | } 16 | 17 | char *parse_input(int argc, char *argv[]) 18 | { 19 | // Argument parsing 20 | int opt; 21 | while ((opt = getopt(argc, argv, "hism")) != -1) { 22 | switch (opt) { 23 | case 's': 24 | print_syscall = true; 25 | break; 26 | case 'i': 27 | print_instr = true; 28 | break; 29 | case 'm': 30 | print_mem_ref_set = true; 31 | break; 32 | case 'h': 33 | print_usage(argv[0]); 34 | exit(EXIT_FAILURE); 35 | break; 36 | } 37 | } 38 | if (optind >= argc) 39 | { 40 | print_usage(argv[0]); 41 | fprintf(stderr, "\nMissing argument: Trace path at the end expected.\n"); 42 | exit(EXIT_FAILURE); 43 | } 44 | 45 | return argv[argc - 1]; 46 | } 47 | 48 | int main(int argc, char *argv[]) 49 | { 50 | std::string trace_name(parse_input(argc, argv)); 51 | FlowMatrix::trace_handle_t *handle = FlowMatrix::Trace::LoadTrace(trace_name); 52 | 53 | if (print_instr) 54 | { 55 | FlowMatrix::instr_t instr; 56 | while (handle->instr_trace.ReadNext(instr)) 57 | { 58 | instr.Print(); 59 | } 60 | } 61 | 62 | if (print_syscall) 63 | { 64 | FlowMatrix::syscall_t syscall; 65 | while (handle->syscall_trace.ReadNext(syscall)) 66 | syscall.Print(); 67 | } 68 | 69 | if (print_mem_ref_set) 70 | { 71 | FlowMatrix::mem_ref_t mem_ref; 72 | while (handle->mem_ref_set.ReadNext(mem_ref)) 73 | mem_ref.Print(); 74 | } 75 | 76 | FlowMatrix::Trace::CloseTrace(handle); 77 | } -------------------------------------------------------------------------------- /trace/FMtrace/include/trace.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_TRACE_H__ 2 | #define __FLOWMATRIX_TRACE_H__ 3 | 4 | #define RAW_BYTE_MAX_LEN 16 5 | #define MEM_REF_MAX_NUM 4 6 | #define SYS_CALL_NAME_MAX_LEN 32 7 | #define BUFFER_SIZE 8192 8 | 9 | #include 10 | 11 | namespace FlowMatrix{ 12 | 13 | typedef struct { 14 | uint32_t instr_id; 15 | uint32_t instr_len; 16 | uint32_t mem_ref_num; 17 | uint64_t rflags; 18 | uint8_t raw_bytes[RAW_BYTE_MAX_LEN]; 19 | uint64_t mem_ref_addr[MEM_REF_MAX_NUM]; 20 | 21 | void Print(); 22 | } instr_t; 23 | 24 | typedef struct { 25 | uint32_t instr_id; 26 | uint32_t syscall_id; 27 | uint64_t rvalue; 28 | uint64_t nargs; 29 | uint64_t args[6]; 30 | char sys_name[SYS_CALL_NAME_MAX_LEN]; 31 | 32 | void Print(); 33 | } syscall_t; 34 | 35 | typedef struct _mem_ref_t { 36 | uint64_t mem_ref_addr; 37 | 38 | bool operator < (const _mem_ref_t &ref) const{ 39 | return (this->mem_ref_addr < ref.mem_ref_addr); 40 | } 41 | 42 | void Print(); 43 | } mem_ref_t; 44 | 45 | template 46 | class TraceHandle 47 | { 48 | private: 49 | FILE *trace_file; 50 | FrameType *buffer, *buffer_head; 51 | size_t buffer_size; 52 | bool is_read_mode; 53 | 54 | size_t FlushBuffer(); 55 | size_t LoadBuffer(); 56 | 57 | public: 58 | void CreateTrace(const std::string &trace_path); 59 | void LoadTrace(const std::string &trace_path); 60 | void CloseTrace(); 61 | 62 | void WriteTrace(const FrameType &frame); 63 | size_t ReadNext(FrameType &frame); 64 | 65 | TraceHandle(); 66 | ~TraceHandle(); 67 | }; 68 | 69 | template class TraceHandle; 70 | template class TraceHandle; 71 | template class TraceHandle; 72 | 73 | 74 | typedef struct { 75 | TraceHandle instr_trace; 76 | TraceHandle syscall_trace; 77 | TraceHandle mem_ref_set; 78 | } trace_handle_t; 79 | 80 | class Trace 81 | { 82 | public: 83 | // TODO: Support mmap-style File I/O to gain further speedup. 84 | static trace_handle_t *CreateTrace(const std::string &trace_name); 85 | static trace_handle_t *LoadTrace(const std::string &trace_name); 86 | static void CloseTrace(trace_handle_t *trace_handle); 87 | }; 88 | 89 | }; // End of FlowMatrix namespace 90 | 91 | #endif -------------------------------------------------------------------------------- /libFlowMatrix/include/VState.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __LIBFLOWMATRIX_VSTATE_H__ 2 | #define __LIBFLOWMATRIX_VSTATE_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define INSERT_TO_VSTATE_BY_REG_NAME(vstate, regname) { \ 10 | unsigned int reg_id, reg_size; \ 11 | assert(getRegId("AMD64", reg_name, reg_id) == 0); \ 12 | assert(getRegSize("AMD64", reg_id, reg_size) == 0); \ 13 | vstate->append(reg_id, reg_size); \ 14 | } 15 | 16 | namespace FlowMatrix 17 | { 18 | 19 | typedef struct _FMRegListConfig 20 | { 21 | unsigned char gpr; // General Purpose Registers 22 | unsigned char fp; // Floating Point Registers 23 | unsigned char st; // ST registers 24 | unsigned char dr; // Debug Registers /* KH: We don't deal with synonyms */ 25 | unsigned char cr; // Control Registers 26 | unsigned char kr; // k0-k7, Opmask registers 27 | unsigned char xmm; // XMM registers 28 | unsigned char ymm; // YMM registers, setting as true overrides xmm 29 | unsigned char zmm; // ZMM registers, setting as true overrides xmm 30 | } FMRegListConfig; 31 | 32 | const FMRegListConfig FMRegListConfig_default = 33 | { 34 | .gpr = 1, 35 | .fp = 1, 36 | .st = 0, 37 | .dr = 0, 38 | .cr = 0, 39 | .kr = 0, 40 | .xmm = 0, 41 | .ymm = 1, 42 | .zmm = 0, 43 | }; 44 | 45 | class VState 46 | { 47 | public: 48 | enum FMGranularity granu; 49 | uint64_t size; 50 | std::map id2offset_map, offset2id_map; 51 | 52 | VState(const enum FMGranularity &granu); 53 | void init_reg_state(const FMRegListConfig ®_config); 54 | void append(uint64_t id, uint64_t size_in_bit); 55 | void pp(); 56 | static std::string GetVStateNameByID(uint64_t id); 57 | 58 | VState &merge(VState &another_state, uint64_t *row, uint64_t *col); 59 | // bool store(const std::string &dir, const std::string &prefix=""); 60 | }; 61 | 62 | } // namespace FlowMatrix 63 | 64 | #endif // __LIBFLOWMATRIX_VSTATE_H__ 65 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/boostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_BOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_BOOSTASIOLIB_H_ 32 | 33 | /** 34 | * This header file provides the class `cli::BoostAsioLib`, using the right 35 | * implementation according to the version of boost libraries included. 36 | */ 37 | 38 | #include 39 | 40 | #if BOOST_VERSION < 106600 41 | #include "oldboostasiolib.h" 42 | namespace cli { namespace detail { using BoostAsioLib = OldBoostAsioLib; } } 43 | #else 44 | #include "newboostasiolib.h" 45 | namespace cli { namespace detail { using BoostAsioLib = NewBoostAsioLib; } } 46 | #endif 47 | 48 | #endif // CLI_DETAIL_BOOSTASIOLIB_H_ 49 | 50 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/standaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_STANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_STANDALONEASIOLIB_H_ 32 | 33 | /** 34 | * This header file provides the class `cli::StandaloneAsioLib`, using the right 35 | * implementation according to the version of asio libraries included. 36 | */ 37 | 38 | #include 39 | 40 | #if ASIO_VERSION < 101300 41 | #include "oldstandaloneasiolib.h" 42 | namespace cli { namespace detail { using StandaloneAsioLib = OldStandaloneAsioLib; } } 43 | #else 44 | #include "newstandaloneasiolib.h" 45 | namespace cli { namespace detail { using StandaloneAsioLib = NewStandaloneAsioLib; } } 46 | #endif 47 | 48 | #endif // CLI_DETAIL_STANDALONEASIOLIB_H_ 49 | 50 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/historystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_HISTORYSTORAGE_H_ 31 | #define CLI_HISTORYSTORAGE_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | 39 | class HistoryStorage 40 | { 41 | public: 42 | virtual ~HistoryStorage() = default; 43 | // Store a vector of commands in the history storage 44 | virtual void Store(const std::vector& commands) = 0; 45 | // Returns all the commands stored 46 | virtual std::vector Commands() const = 0; 47 | // Clear the whole content of the storage 48 | // After calling this method, Commands() returns the empty vector 49 | virtual void Clear() = 0; 50 | }; 51 | 52 | } // namespace cli 53 | 54 | #endif // CLI_HISTORYSTORAGE_H_ 55 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/scheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_SCHEDULER_H_ 31 | #define CLI_SCHEDULER_H_ 32 | 33 | #include 34 | 35 | namespace cli 36 | { 37 | 38 | /** 39 | * A `Scheduler` represents an engine capable of running a task. 40 | * Its method `Post` can be safely called from any thread to submit the task 41 | * that will execute in an unspecified thread of execution as soon as possible 42 | * (but in any case after the call to `Post` is terminated). 43 | */ 44 | class Scheduler 45 | { 46 | public: 47 | virtual ~Scheduler() = default; 48 | 49 | /// Submits a completion token or function object for execution. 50 | virtual void Post(const std::function& f) = 0; 51 | }; 52 | 53 | } // namespace cli 54 | 55 | #endif // CLI_SCHEDULER_H_ 56 | -------------------------------------------------------------------------------- /libFlowMatrix/Makefile: -------------------------------------------------------------------------------- 1 | # Command ard arguments 2 | CC ?= gcc 3 | CPPC ?= g++ 4 | PROTOC ?= protoc 5 | RM ?= rm 6 | CP ?= cp 7 | MV ?= mv 8 | MKDIR ?= mkdir 9 | AR ?= ar 10 | ARFLAGS ?= -rcs # ar needs the dash on OpenBSD 11 | RANLIB ?= ranlib 12 | 13 | # Path 14 | PROJ_HOME := . 15 | LIB_PATH := $(PROJ_HOME)/lib 16 | SRC_PATH := $(PROJ_HOME)/src 17 | INC_PATH := $(PROJ_HOME)/include 18 | TEST_SRC_PATH := $(PROJ_HOME)/testsuite 19 | PROTOBUF_PATH := $(PROJ_HOME)/src/ 20 | FMTRACE_PATH := $(PROJ_HOME)/../trace/FMtrace/ 21 | FMTRACE_INC_PATH := $(FMTRACE_PATH)/include 22 | 23 | # Compilation variables 24 | OPT ?= -O3 -g #-O0 25 | WARNINGS = -Wall -Wextra 26 | CPP_STD = -std=c++17 27 | PIC_FLAG ?= -fPIC 28 | CFLAGS ?= $(OPT) $(WARNINGS) $(CPP_STD) $(PIC_FLAG) 29 | INC += -I$(INC_PATH) -I$(PROJ_HOME) -I$(FMTRACE_INC_PATH) 30 | 31 | # Linking variables 32 | LDLIBS += -lcapstone -lprotobuf 33 | 34 | # Lib flags 35 | # SOLIB_FLAGS ?= -Wl,-soname,libfmtrace.so 36 | 37 | # Target 38 | OBJ := $(notdir $(patsubst %.cpp,%.o,$(wildcard $(SRC_PATH)/*.cpp))) 39 | test_bin := $(TEST_SRC_PATH)/vstate_init $(TEST_SRC_PATH)/vstate_fmtrace 40 | libfm_static_lib := $(LIB_PATH)/libfm.a 41 | libfmtrace_static_lib := $(FMTRACE_PATH)/lib/libfmtrace.a 42 | 43 | test: acorns_obj.pb.o $(test_bin) $(libfm_static_lib) 44 | 45 | all : test 46 | 47 | acorns_obj.pb.o: $(PROTOBUF_PATH)/acorns_obj.pb.cc 48 | $(CPPC) -c $(strip $(CFLAGS) $(INC) $^ $(LDLIBS)) 49 | 50 | $(PROTOBUF_PATH)/acorns_obj.pb.cc: $(PROTOBUF_PATH)/acorns_obj.proto 51 | @-$(CP) $^ ./ 52 | $(PROTOC) *.proto --cpp_out=$(SRC_PATH) 53 | @-$(MV) $(SRC_PATH)/acorns_obj.pb.h $(INC_PATH) 54 | @-$(RM) *.proto 55 | 56 | $(libfm_static_lib): $(OBJ) acorns_obj.pb.o 57 | @-$(MKDIR) -p $(LIB_PATH) 58 | $(AR) $(ARFLAGS) $@ $^ 59 | $(RANLIB) $@ 60 | 61 | $(TEST_SRC_PATH)/vstate_init: $(OBJ) 62 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $(TEST_SRC_PATH)/vstate_init.cpp 63 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ vstate_init.o $(LDLIBS)) 64 | 65 | $(TEST_SRC_PATH)/vstate_fmtrace: $(OBJ) $(libfmtrace_static_lib) 66 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $(TEST_SRC_PATH)/vstate_fmtrace.cpp 67 | $(CPPC) -o $@ $(strip $(CFLAGS) $(OBJ) vstate_fmtrace.o $(libfmtrace_static_lib) $(LDLIBS)) 68 | 69 | %.o: $(SRC_PATH)/%.cpp $(PROTOBUF_PATH)/acorns_obj.pb.cc 70 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $< 71 | 72 | $(libfmtrace_static_lib): 73 | (cd $(FMTRACE_PATH) && make) 74 | 75 | 76 | .PHONY: clean 77 | clean: 78 | $(RM) *.o *.gch 79 | $(RM) -rf ./bin 80 | $(RM) -rf ./lib 81 | $(RM) -rf $(test_bin) 82 | $(RM) -rf $(PROTOBUF_PATH)/acorns_obj.pb.cc 83 | $(RM) -rf $(INC_PATH)/acorns_obj.pb.h 84 | 85 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/keyboard.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_KEYBOARD_H_ 31 | #define CLI_DETAIL_KEYBOARD_H_ 32 | 33 | #if defined(__unix__) || defined(__unix) || defined(__linux__) 34 | #define OS_LINUX 35 | #elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) 36 | #define OS_WIN 37 | #elif defined(__APPLE__) || defined(__MACH__) 38 | #define OS_MAC 39 | #else 40 | #error "Platform not supported (yet)." 41 | #endif 42 | 43 | #if defined(OS_LINUX) || defined(OS_MAC) 44 | #include "linuxkeyboard.h" 45 | namespace cli { namespace detail { using Keyboard = LinuxKeyboard; } } 46 | #elif defined(OS_WIN) 47 | #include "winkeyboard.h" 48 | namespace cli { namespace detail { using Keyboard = WinKeyboard; } } 49 | #else 50 | #error "Platform not supported (yet)." 51 | #endif 52 | 53 | #undef OS_LINUX 54 | #undef OS_WIN 55 | #undef OS_MAC 56 | 57 | #endif // CLI_DETAIL_KEYBOARD_H_ 58 | 59 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/inputdevice.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_INPUTDEVICE_H_ 31 | #define CLI_DETAIL_INPUTDEVICE_H_ 32 | 33 | #include 34 | #include 35 | #include "../scheduler.h" 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | enum class KeyType { ascii, up, down, left, right, backspace, canc, home, end, ret, eof, ignored }; 43 | 44 | class InputDevice 45 | { 46 | public: 47 | using Handler = std::function< void( std::pair ) >; 48 | 49 | explicit InputDevice(Scheduler& _scheduler) : scheduler(_scheduler) {} 50 | virtual ~InputDevice() = default; 51 | 52 | template 53 | void Register(H&& h) { handler = std::forward(h); } 54 | 55 | protected: 56 | 57 | void Notify(std::pair k) 58 | { 59 | scheduler.Post([this,k](){ if (handler) handler(k); }); 60 | } 61 | 62 | private: 63 | 64 | Scheduler& scheduler; 65 | Handler handler; 66 | }; 67 | 68 | } // namespace detail 69 | } // namespace cli 70 | 71 | #endif // CLI_DETAIL_INPUTDEVICE_H_ 72 | 73 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/commonprefix.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_COMMONPREFIX_H_ 31 | #define CLI_DETAIL_COMMONPREFIX_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | namespace cli 39 | { 40 | namespace detail 41 | { 42 | 43 | inline std::string CommonPrefix(const std::vector& v) 44 | { 45 | assert(!v.empty()); 46 | std::string prefix; 47 | 48 | // find the shorter string 49 | auto smin = std::min_element(v.begin(), v.end(), 50 | [] (const std::string& s1, const std::string& s2) 51 | { 52 | return s1.size() < s2.size(); 53 | }); 54 | 55 | for (std::size_t i = 0; i < smin->size(); ++i) 56 | { 57 | // check if i-th element is equal in each input string 58 | const char c = (*smin)[i]; 59 | for (auto& x: v) 60 | if (x[i] != c) return prefix; 61 | prefix += c; 62 | } 63 | 64 | return prefix; 65 | } 66 | 67 | } // namespace detail 68 | } // namespace cli 69 | 70 | #endif // CLI_DETAIL_COMMONPREFIX_H_ 71 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/colorprofile.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_COLORPROFILE_H_ 31 | #define CLI_COLORPROFILE_H_ 32 | 33 | #include "detail/rang.h" 34 | 35 | namespace cli 36 | { 37 | 38 | inline bool& Color() { static bool color; return color; } 39 | 40 | inline void SetColor() { Color() = true; } 41 | inline void SetNoColor() { Color() = false; } 42 | 43 | enum BeforePrompt { beforePrompt }; 44 | enum AfterPrompt { afterPrompt }; 45 | enum BeforeInput { beforeInput }; 46 | enum AfterInput { afterInput }; 47 | 48 | inline std::ostream& operator<<(std::ostream& os, BeforePrompt) 49 | { 50 | if ( Color() ) { os << rang::control::forceColor << rang::fg::green << rang::style::bold; } 51 | return os; 52 | } 53 | 54 | inline std::ostream& operator<<(std::ostream& os, AfterPrompt) 55 | { 56 | os << rang::style::reset; 57 | return os; 58 | } 59 | 60 | inline std::ostream& operator<<(std::ostream& os, BeforeInput) 61 | { 62 | if ( Color() ) { os << rang::control::forceColor << rang::fgB::gray; } 63 | return os; 64 | } 65 | 66 | inline std::ostream& operator<<(std::ostream& os, AfterInput) 67 | { 68 | os << rang::style::reset; 69 | return os; 70 | } 71 | 72 | } // namespace cli 73 | 74 | #endif // CLI_COLORPROFILE_H_ 75 | 76 | 77 | -------------------------------------------------------------------------------- /FMDynamic/include/SyscallHook.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FLOWMATRIX_SYSCALL_HOOK_HPP 2 | #define FLOWMATRIX_SYSCALL_HOOK_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define SYSCALL_READ 0 9 | #define SYSCALL_WRITE 1 10 | #define SYSCALL_MMAP 9 11 | #define SYSCALL_PREAD64 17 12 | #define SYSCALL_PWRITE64 18 13 | #define SYSCALL_SENDTO 44 14 | #define SYSCALL_RECVFROM 45 15 | #define SYSCALL_SENDMSG 46 16 | #define SYSCALL_RECVMSG 47 17 | #define SYSCALL_RECVMMSG 299 18 | #define SYSCALL_SENDMMSG 307 19 | 20 | using FlowMatrix::syscall_t; 21 | namespace FlowMatrix{ 22 | bool get_syscall_buffer( 23 | const syscall_t &this_syscall, 24 | uint64_t &buffer_base, 25 | uint64_t &buffer_size, 26 | bool &isSource) 27 | { 28 | buffer_base = (uint64_t) -1; 29 | buffer_size = (uint64_t) -1; 30 | switch (this_syscall.syscall_id) 31 | { 32 | // Source syscalls 33 | case SYSCALL_READ: 34 | case SYSCALL_RECVFROM: 35 | case SYSCALL_RECVMSG: 36 | case SYSCALL_PREAD64: 37 | isSource = true; 38 | if (this_syscall.rvalue != (uint64_t) -1) 39 | { 40 | buffer_base = this_syscall.args[1]; 41 | buffer_size = this_syscall.rvalue; 42 | } 43 | return true; 44 | case SYSCALL_RECVMMSG: 45 | isSource = true; 46 | if (this_syscall.rvalue != (uint64_t) -1) 47 | { 48 | buffer_base = this_syscall.args[1]; 49 | buffer_size = this_syscall.rvalue * sizeof(struct mmsghdr); 50 | } 51 | return true; 52 | case SYSCALL_MMAP: 53 | isSource = true; 54 | if (this_syscall.rvalue != (uint64_t) -1) 55 | { 56 | buffer_base = this_syscall.rvalue; 57 | buffer_size = this_syscall.args[1]; 58 | } 59 | return true; 60 | 61 | // Sink syscalls 62 | case SYSCALL_WRITE: 63 | case SYSCALL_SENDTO: 64 | case SYSCALL_SENDMSG: 65 | case SYSCALL_PWRITE64: 66 | isSource = false; 67 | if (this_syscall.rvalue != (uint64_t) -1) 68 | { 69 | buffer_base = this_syscall.args[1]; 70 | buffer_size = this_syscall.rvalue; 71 | } 72 | return true; 73 | case SYSCALL_SENDMMSG: 74 | isSource = false; 75 | if (this_syscall.rvalue != (uint64_t) -1) 76 | { 77 | buffer_base = this_syscall.args[1]; 78 | buffer_size = this_syscall.rvalue * sizeof(struct mmsghdr); 79 | } 80 | return true; 81 | 82 | // Not implemented syscalls :( 83 | default: 84 | return false; 85 | } 86 | } 87 | } // Namespace FlowMatrix 88 | #endif // FLOWMATRIX_SYSCALL_HOOK_HPP -------------------------------------------------------------------------------- /FMDynamic/include/DBCore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_UTIL_DYNAMIC_DBCORE_H_ 2 | #define __FLOWMATRIX_UTIL_DYNAMIC_DBCORE_H_ 3 | 4 | #include "cuSparseMatrix.cuh" 5 | #include "FMRule.hpp" 6 | #include "sqlite3.h" 7 | #include 8 | #include 9 | 10 | #define TRANSACTION_BUFFER_SIZE 32 11 | 12 | namespace FlowMatrix 13 | { 14 | 15 | typedef struct _matrix_storage_row_t 16 | { 17 | int left; 18 | int right; 19 | cuSparseMatrix *matrix_ptr; 20 | int num_valid_indices; 21 | const int *valid_indices; 22 | } matrix_storage_row_t; 23 | 24 | class DBCore 25 | { 26 | private: 27 | 28 | #ifdef TIME_MEASUREMENT 29 | // Counter and timer 30 | int load_count, load_fail_count, store_count, offload_count, onload_count; 31 | std::chrono::duration load_timer, load_fail_timer, store_timer, offload_timer, onload_timer; 32 | #endif // TIME_MEASUREMENT 33 | 34 | 35 | public: 36 | // Variables 37 | sqlite3* db; 38 | char* sql_cmd_buffer; 39 | std::string table_name; 40 | cudaStream_t stream; 41 | bool has_stream; 42 | 43 | // Transaction buffer 44 | matrix_storage_row_t transaction_buffer[TRANSACTION_BUFFER_SIZE]; 45 | unsigned int buffer_idx; 46 | 47 | public: 48 | DBCore(const std::string &dbPath, const std::string &traceName); 49 | DBCore(const std::string &dbPath); 50 | 51 | ~DBCore(); 52 | 53 | #ifdef TIME_MEASUREMENT 54 | void resetTimer(); 55 | void printTimer(); 56 | #endif 57 | 58 | void Store(int left, int right, 59 | int matrix_size, int nnz, int* row, int* col, float* value, 60 | int num_valid_indices, const int *valid_indices, 61 | std::string rawbytes="", std::string asm_str="", bool isTranscation = false); 62 | void StoreFromDevice(cusparseHandle_t &handle, 63 | int left, int right, 64 | cuSparseMatrix *matrix_ptr, 65 | int num_valid_indices, const int *valid_indices); 66 | void StoreSyscall(syscall_t &syscall); 67 | void StoreVState(VState &vstate); 68 | cuSparseMatrix *LoadToDevice(cusparseHandle_t &handle, int left, int right, int &num_valid_indices, int *&valid_indices); 69 | 70 | void StartTransaction(); 71 | void EndTransaction(); 72 | void RemoveAllCachedRules(); 73 | void Commit(); 74 | 75 | int GetTraceSize(); 76 | std::string ListAllTraces(); 77 | std::string PrintTrace(const int instr_id, const int len = -1); 78 | 79 | int GetSyscallId(const int instr_id); 80 | std::string PrintSyscallByIdx(const int syscall_idx); 81 | int GetSyscallsByname(std::string syscallName, syscall_t*&syscalls, int start, int end, int num =0); 82 | bool GetInfluenceRangeByInstrIdx(const int instr_id, int &length, int *&indices); 83 | void SetMemInfluence(uint64_t base, uint64_t size, std::set *influence_set_ptr); 84 | std::map *GetOffsetName(std::set&offset); 85 | std::vector GetInstrIdxByASM(std::string asm_str, int start, int end); 86 | }; 87 | 88 | 89 | } 90 | 91 | 92 | #endif -------------------------------------------------------------------------------- /FMDynamic/include/cli/volatilehistorystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_VOLATILEHISTORYSTORAGE_H_ 31 | #define CLI_VOLATILEHISTORYSTORAGE_H_ 32 | 33 | #include "historystorage.h" 34 | #include 35 | 36 | namespace cli 37 | { 38 | 39 | class VolatileHistoryStorage : public HistoryStorage 40 | { 41 | public: 42 | explicit VolatileHistoryStorage(std::size_t size = 1000) : maxSize(size) {} 43 | void Store(const std::vector& cmds) override 44 | { 45 | using dt = std::deque::difference_type; 46 | commands.insert(commands.end(), cmds.begin(), cmds.end()); 47 | if (commands.size() > maxSize) 48 | commands.erase( 49 | commands.begin(), 50 | commands.begin()+static_cast
(commands.size()-maxSize) 51 | ); 52 | } 53 | std::vector Commands() const override 54 | { 55 | return std::vector(commands.begin(), commands.end()); 56 | } 57 | void Clear() override 58 | { 59 | commands.clear(); 60 | } 61 | private: 62 | const std::size_t maxSize; 63 | std::deque commands; 64 | }; 65 | 66 | } // namespace cli 67 | 68 | #endif // CLI_VOLATILEHISTORYSTORAGE_H_ 69 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/oldboostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_OLDBOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_OLDBOOSTASIOLIB_H_ 32 | 33 | #include 34 | 35 | namespace cli 36 | { 37 | namespace detail 38 | { 39 | 40 | namespace asiolib = boost::asio; 41 | namespace asiolibec = boost::system; 42 | 43 | class OldBoostAsioLib 44 | { 45 | public: 46 | using ContextType = boost::asio::io_service; 47 | using WorkGuard = boost::asio::io_service::work; 48 | 49 | class Executor 50 | { 51 | public: 52 | explicit Executor(ContextType& _ios) : 53 | ios(_ios) {} 54 | explicit Executor(boost::asio::ip::tcp::socket& socket) : 55 | ios(socket.get_io_service()) {} 56 | template void Post(T&& t) { ios.post(std::forward(t)); } 57 | private: 58 | ContextType& ios; 59 | }; 60 | 61 | static boost::asio::ip::address IpAddressFromString(const std::string& address) 62 | { 63 | return boost::asio::ip::address::from_string(address); 64 | } 65 | 66 | static auto MakeWorkGuard(ContextType& context) 67 | { 68 | boost::asio::io_service::work work(context); 69 | return work; 70 | } 71 | 72 | }; 73 | 74 | } // namespace detail 75 | } // namespace cli 76 | 77 | #endif // CLI_DETAIL_OLDBOOSTASIOLIB_H_ 78 | 79 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/oldstandaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 32 | 33 | #define ASIO_STANDALONE 1 34 | 35 | #include 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | namespace asiolib = asio; 43 | namespace asiolibec = asio; 44 | 45 | class OldStandaloneAsioLib 46 | { 47 | public: 48 | 49 | using ContextType = asio::io_service; 50 | using WorkGuard = asio::io_service::work; 51 | 52 | class Executor 53 | { 54 | public: 55 | explicit Executor(ContextType& _ios) : 56 | ios(_ios) {} 57 | explicit Executor(asio::ip::tcp::socket& socket) : 58 | ios(socket.get_io_service()) {} 59 | template void Post(T&& t) { ios.post(std::forward(t)); } 60 | private: 61 | ContextType& ios; 62 | }; 63 | 64 | static asio::ip::address IpAddressFromString(const std::string& address) 65 | { 66 | return asio::ip::address::from_string(address); 67 | } 68 | 69 | static auto MakeWorkGuard(ContextType& context) 70 | { 71 | asio::io_service::work work(context); 72 | return work; 73 | } 74 | 75 | }; 76 | 77 | } // namespace detail 78 | } // namespace cli 79 | 80 | #endif // CLI_DETAIL_OLDSTANDALONEASIOLIB_H_ 81 | 82 | -------------------------------------------------------------------------------- /FMCuSparseLinAlg/testsuite/spgemm_test.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace FlowMatrix; 4 | 5 | int main(){ 6 | 7 | const int A_num_rows = 4; 8 | const int A_num_cols = 4; 9 | const int A_nnz = 9; 10 | const int B_num_rows = 4; 11 | const int B_num_cols = 4; 12 | const int B_nnz = 8; 13 | int hA_csrOffsets[] = { 0, 3, 4, 7, 9 }; 14 | int hA_columns[] = { 0, 2, 3, 1, 0, 2, 3, 1, 3 }; 15 | float hA_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 16 | 6.0f, 7.0f, 8.0f, 9.0f }; 17 | int hB_csrOffsets[] = { 0, 2, 4, 7, 8 }; 18 | int hB_columns[] = { 0, 3, 1, 3, 0, 1, 2, 1 }; 19 | float hB_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 20 | 6.0f, 7.0f, 8.0f }; 21 | int hC_csrOffsets[] = { 0, 4, 6, 10, 12 }; 22 | int hC_columns[] = { 0, 1, 2, 3, 1, 3, 0, 1, 2, 3, 1, 3 }; 23 | float hC_values[] = { 11.0f, 36.0f, 14.0f, 2.0f, 12.0f, 24 | 16.0f, 35.0f, 92.0f, 42.0f, 10.0f, 25 | 96.0f, 32.0f }; 26 | const int C_nnz = 12; 27 | 28 | // Init cusparse handler 29 | auto sp_core = new CuSparseCore(); 30 | 31 | // Create matrix 32 | cuSparseMatrix matA(sp_core->stream, A_num_rows, A_num_cols, A_nnz, hA_csrOffsets, hA_columns, hA_values); 33 | cuSparseMatrix matB(sp_core->stream, B_num_rows, B_num_cols, B_nnz, hB_csrOffsets, hB_columns, hB_values); 34 | 35 | // Compute 36 | cuSparseMatrix *matCPtr = sp_core->_spgemm(matA, matB); 37 | cuSparseMatrix *matDPtr = sp_core->_spgemm_safe(matA, matB); 38 | 39 | // To host memory 40 | matCPtr->toHost(sp_core->stream); 41 | matDPtr->toHost(sp_core->stream); 42 | sp_core->sync(); 43 | 44 | // Verification 45 | int correct = 1; 46 | for (int i = 0; i < A_num_rows + 1; i++) { 47 | if (matCPtr->h_csrOffsets[i] != hC_csrOffsets[i]) { 48 | correct = 0; 49 | break; 50 | } 51 | } 52 | for (int i = 0; i < C_nnz; i++) { 53 | if (matCPtr->h_columns[i] != hC_columns[i] || 54 | matCPtr->h_values[i] != hC_values[i]) { // direct floating point 55 | correct = 0; // comparison is not reliable 56 | break; 57 | } 58 | } 59 | for (int i = 0; i < A_num_rows + 1; i++) { 60 | if (matDPtr->h_csrOffsets[i] != hC_csrOffsets[i]) { 61 | correct = 0; 62 | break; 63 | } 64 | } 65 | for (int i = 0; i < C_nnz; i++) { 66 | if (matDPtr->h_columns[i] != hC_columns[i] || 67 | matDPtr->h_values[i] != hC_values[i]) { // direct floating point 68 | correct = 0; // comparison is not reliable 69 | break; 70 | } 71 | } 72 | 73 | if (correct) 74 | printf("SpGEMM test PASSED\n"); 75 | else { 76 | printf("SpGEMM test FAILED: Wrong Result\n"); 77 | return EXIT_FAILURE; 78 | } 79 | 80 | // Free pointer 81 | delete matCPtr; 82 | 83 | } -------------------------------------------------------------------------------- /FMDynamic/include/cli/clifilesession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_CLIFILESESSION_H 31 | #define CLI_CLIFILESESSION_H 32 | 33 | #include 34 | #include 35 | #include // std::invalid_argument 36 | #include "cli.h" // CliSession 37 | 38 | namespace cli 39 | { 40 | 41 | class CliFileSession : public CliSession 42 | { 43 | public: 44 | /// @throw std::invalid_argument if @c _in or @c out are invalid streams 45 | explicit CliFileSession(Cli& _cli, std::istream& _in=std::cin, std::ostream& _out=std::cout) : 46 | CliSession(_cli, _out, 1), 47 | exit(false), 48 | in(_in) 49 | { 50 | if (!_in.good()) throw std::invalid_argument("istream invalid"); 51 | if (!_out.good()) throw std::invalid_argument("ostream invalid"); 52 | ExitAction( 53 | [this](std::ostream&) 54 | { 55 | exit = true; 56 | } 57 | ); 58 | } 59 | void Start() 60 | { 61 | while(!exit) 62 | { 63 | Prompt(); 64 | std::string line; 65 | if (!in.good()) 66 | Exit(); 67 | std::getline(in, line); 68 | if (in.eof()) 69 | Exit(); 70 | else 71 | Feed(line); 72 | } 73 | } 74 | 75 | private: 76 | bool exit; 77 | std::istream& in; 78 | }; 79 | 80 | } // namespace cli 81 | 82 | #endif // CLI_CLIFILESESSION_H 83 | 84 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/newstandaloneasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 31 | #define CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | namespace asiolib = asio; 42 | namespace asiolibec = asio; 43 | 44 | class NewStandaloneAsioLib 45 | { 46 | public: 47 | 48 | using ContextType = asio::io_context; 49 | using WorkGuard = asio::executor_work_guard; 50 | 51 | class Executor 52 | { 53 | public: 54 | explicit Executor(ContextType& ios) : 55 | executor(ios.get_executor()) {} 56 | explicit Executor(asio::ip::tcp::socket& socket) : 57 | executor(socket.get_executor()) {} 58 | template void Post(T&& t) { asio::post(executor, std::forward(t)); } 59 | private: 60 | #if ASIO_VERSION >= 101700 61 | using AsioExecutor = asio::any_io_executor; 62 | #else 63 | using AsioExecutor = asio::executor; 64 | #endif 65 | AsioExecutor executor; 66 | }; 67 | 68 | static asio::ip::address IpAddressFromString(const std::string& address) 69 | { 70 | return asio::ip::make_address(address); 71 | } 72 | 73 | static auto MakeWorkGuard(ContextType& context) 74 | { 75 | return asio::make_work_guard(context); 76 | } 77 | }; 78 | 79 | } // namespace detail 80 | } // namespace cli 81 | 82 | #endif // CLI_DETAIL_NEWSTANDALONEASIOLIB_H_ 83 | 84 | -------------------------------------------------------------------------------- /FMDynamic/include/QueryCLI.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FLOWMATRIX_QUERY_CLI_HPP 2 | #define FLOWMATRIX_QUERY_CLI_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define FLOAT_THRESHOLD (0.5f) 11 | 12 | /********************************/ 13 | /* Structures */ 14 | /********************************/ 15 | typedef enum _vars_type_t : unsigned char 16 | { 17 | SYSCALL = 1, 18 | INSTR = 2, 19 | REGISTER = 3, 20 | MEMORY = 4, 21 | } vars_type_t; 22 | 23 | /********************************/ 24 | /* Utility Functions */ 25 | /********************************/ 26 | void WorkOn(std::ostream& out, std::string &_traceName); 27 | void RemoveCache(std::ostream& out); 28 | void InitQueryRange(std::ostream &out, int start, int end); 29 | void PrepareQuery(std::ostream &out, int level); 30 | void QueryInRange( 31 | std::ostream& out, 32 | std::string _range, 33 | std::string _src_type, 34 | std::string _srcs, 35 | std::string _direction, 36 | std::string _dest_type, 37 | std::string _dests); 38 | void Query( 39 | std::ostream& out, 40 | std::string _src_type, 41 | std::string _srcs, 42 | std::string _direction, 43 | std::string _dest_type, 44 | std::string _dests); 45 | 46 | /********************************/ 47 | /* Trace-Related Functions */ 48 | /********************************/ 49 | void TracePrint(std::ostream &out, const int pos); 50 | void TracePrintRange(std::ostream &out, const int pos, const int length); 51 | void SyscallPrintAll(std::ostream &out); 52 | void SyscallPrint(std::ostream &out, int index); 53 | 54 | /********************************/ 55 | /* Setting Functions */ 56 | /********************************/ 57 | void SetStorageLevel(std::ostream& out, int level); 58 | void SetDB(std::ostream& out, std::string &_dbPath); 59 | 60 | /********************************/ 61 | /* Print Info Functions */ 62 | /********************************/ 63 | void WhichDB(std::ostream& out); 64 | void ListTraces(std::ostream& out); 65 | void TraceSize(std::ostream& out); 66 | void PrintLastResult(std::ostream& out); 67 | void PrintSingleInstr(std::ostream& out, int instr_id); 68 | 69 | /********************************/ 70 | /* Helper Functions */ 71 | /********************************/ 72 | void UsageMenu(std::ostream& out, std::string cmd); 73 | void SubQueryHelper(std::ostream& out); 74 | void QueryHelper(std::ostream& out); 75 | void QueryInRangeHelper(std::ostream& out); 76 | 77 | /********************************/ 78 | /* Query Assist Functions */ 79 | /********************************/ 80 | void MakeSourceSinkSet(std::map*> &source_sink_list, int instr_id, int len, int* indices); 81 | int AddSyscallsToSourceSinkSet(std::map*> &source_sink_list, int num_syscalls, syscall_t* syscalls); 82 | void ParseVars(vars_type_t type, std::string vars, int start, int end, std::map*> &source_sink_list); 83 | 84 | /********************************/ 85 | /* Checker Functions */ 86 | /********************************/ 87 | bool _Check_DB(std::ostream& out); 88 | bool _Check_Trace(std::ostream& out); 89 | bool _Check_Query(std::ostream& out); 90 | 91 | #endif -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/newboostasiolib.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_NEWBOOSTASIOLIB_H_ 31 | #define CLI_DETAIL_NEWBOOSTASIOLIB_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | namespace asiolib = boost::asio; 42 | namespace asiolibec = boost::system; 43 | 44 | class NewBoostAsioLib 45 | { 46 | public: 47 | 48 | using ContextType = boost::asio::io_context; 49 | using WorkGuard = boost::asio::executor_work_guard; 50 | 51 | class Executor 52 | { 53 | public: 54 | explicit Executor(ContextType& ios) : 55 | executor(ios.get_executor()) {} 56 | explicit Executor(boost::asio::ip::tcp::socket& socket) : 57 | executor(socket.get_executor()) {} 58 | template void Post(T&& t) { boost::asio::post(executor, std::forward(t)); } 59 | private: 60 | #if BOOST_VERSION >= 107400 61 | using AsioExecutor = boost::asio::any_io_executor; 62 | #else 63 | using AsioExecutor = boost::asio::executor; 64 | #endif 65 | AsioExecutor executor; 66 | }; 67 | 68 | static boost::asio::ip::address IpAddressFromString(const std::string& address) 69 | { 70 | return boost::asio::ip::make_address(address); 71 | } 72 | 73 | static auto MakeWorkGuard(ContextType& context) 74 | { 75 | return boost::asio::make_work_guard(context); 76 | } 77 | 78 | }; 79 | 80 | } // namespace detail 81 | } // namespace cli 82 | 83 | #endif // CLI_DETAIL_NEWBOOSTASIOLIB_H_ 84 | 85 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/clilocalsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_CLILOCALSESSION_H 31 | #define CLI_CLILOCALSESSION_H 32 | 33 | #include // std::ostream 34 | #include "detail/keyboard.h" 35 | #include "detail/inputhandler.h" 36 | #include "cli.h" // CliSession 37 | 38 | namespace cli 39 | { 40 | 41 | class Scheduler; // forward declaration 42 | 43 | /** 44 | * @brief CliLocalTerminalSession represents a local session. 45 | * You should instantiate it to start an interactive prompt on the standard 46 | * input/output of your application. 47 | * The handlers of the commands will be invoked in the same thread the @c Scheduler runs. 48 | */ 49 | class CliLocalTerminalSession : public CliSession 50 | { 51 | public: 52 | 53 | /** 54 | * @brief Construct a new Cli Local Terminal Session object that uses the specified @c std::ostream 55 | * for output. You can also specify a size for the command history. 56 | * 57 | * @param _cli The cli object that defines the menu hierarchy for this session 58 | * @param scheduler The scheduler that will process the command handlers 59 | * @param _out the output stream where command output will be printed 60 | * @param historySize the size of the command history 61 | */ 62 | CliLocalTerminalSession(Cli& _cli, Scheduler& scheduler, std::ostream& _out, std::size_t historySize = 100) : 63 | CliSession(_cli, _out, historySize), 64 | kb(scheduler), 65 | ih(*this, kb) 66 | { 67 | Prompt(); 68 | } 69 | 70 | private: 71 | detail::Keyboard kb; 72 | detail::InputHandler ih; 73 | }; 74 | 75 | using CliLocalSession = CliLocalTerminalSession; 76 | 77 | } // namespace cli 78 | 79 | #endif // CLI_CLILOCALSESSION_H 80 | 81 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/filehistorystorage.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_FILEHISTORYSTORAGE_H_ 31 | #define CLI_FILEHISTORYSTORAGE_H_ 32 | 33 | #include "historystorage.h" 34 | #include 35 | #include 36 | 37 | namespace cli 38 | { 39 | 40 | class FileHistoryStorage : public HistoryStorage 41 | { 42 | public: 43 | explicit FileHistoryStorage(std::string _fileName, std::size_t size = 1000) : 44 | maxSize(size), 45 | fileName(std::move(_fileName)) 46 | { 47 | } 48 | void Store(const std::vector& cmds) override 49 | { 50 | using dt = std::vector::difference_type; 51 | auto commands = Commands(); 52 | commands.insert(commands.end(), cmds.begin(), cmds.end()); 53 | if (commands.size() > maxSize) 54 | commands.erase( 55 | commands.begin(), 56 | commands.begin() + static_cast
(commands.size() - maxSize) 57 | ); 58 | std::ofstream f(fileName, std::ios_base::out); 59 | for (const auto& line: commands) 60 | f << line << '\n'; 61 | } 62 | std::vector Commands() const override 63 | { 64 | std::vector commands; 65 | std::ifstream in(fileName); 66 | if (in) 67 | { 68 | std::string line; 69 | while (std::getline(in, line)) 70 | commands.push_back(line); 71 | } 72 | return commands; 73 | } 74 | void Clear() override 75 | { 76 | std::ofstream f(fileName, std::ios_base::out | std::ios_base::trunc); 77 | } 78 | 79 | private: 80 | const std::size_t maxSize; 81 | const std::string fileName; 82 | }; 83 | 84 | } // namespace cli 85 | 86 | #endif // CLI_FILEHISTORYSTORAGE_H_ 87 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/genericasioscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_GENERICASIOSCHEDULER_H_ 31 | #define CLI_DETAIL_GENERICASIOSCHEDULER_H_ 32 | 33 | #include "../scheduler.h" 34 | #include // unique_ptr 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | template 42 | class GenericAsioScheduler : public Scheduler 43 | { 44 | public: 45 | 46 | using ContextType = typename ASIOLIB::ContextType; 47 | using WorkGuard = typename ASIOLIB::WorkGuard; 48 | 49 | GenericAsioScheduler() : 50 | owned{true}, 51 | context{new ContextType()}, 52 | executor{*context}, 53 | work{std::make_unique(ASIOLIB::MakeWorkGuard(*context))} 54 | {} 55 | 56 | explicit GenericAsioScheduler(ContextType& _context) : context{&_context}, executor{*context} {} 57 | 58 | ~GenericAsioScheduler() override { if (owned) delete context; } 59 | 60 | // non copyable 61 | GenericAsioScheduler(const GenericAsioScheduler&) = delete; 62 | GenericAsioScheduler& operator=(const GenericAsioScheduler&) = delete; 63 | 64 | void Stop() 65 | { 66 | if (work) 67 | work->reset(); 68 | context->stop(); 69 | } 70 | 71 | void Run() 72 | { 73 | context->run(); 74 | } 75 | 76 | bool Stopped() const 77 | { 78 | return context->stopped(); 79 | } 80 | 81 | void ExecOne() { context->run_one(); } 82 | 83 | void PollOne() { context->poll_one(); } 84 | 85 | void Post(const std::function& f) override 86 | { 87 | executor.Post(f); 88 | } 89 | 90 | ContextType& AsioContext() { return *context; } 91 | 92 | private: 93 | 94 | using ExecutorType = typename ASIOLIB::Executor; 95 | 96 | bool owned = false; 97 | ContextType* context; 98 | ExecutorType executor; 99 | std::unique_ptr work; 100 | }; 101 | 102 | 103 | } // namespace detail 104 | } // namespace cli 105 | 106 | #endif // CLI_DETAIL_GENERICASIOSCHEDULER_H_ 107 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/genericcliasyncsession.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 31 | #define CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 32 | 33 | #include 34 | #include "../cli.h" // CliSession 35 | #include "genericasioscheduler.h" 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | template 43 | class GenericCliAsyncSession : public CliSession 44 | { 45 | public: 46 | GenericCliAsyncSession(GenericAsioScheduler& _scheduler, Cli& _cli) : 47 | CliSession(_cli, std::cout, 1), 48 | input(_scheduler.AsioContext(), ::dup(STDIN_FILENO)) 49 | { 50 | Read(); 51 | } 52 | ~GenericCliAsyncSession() noexcept override 53 | { 54 | try { input.close(); } catch (const std::exception&) { /* do nothing */ } 55 | } 56 | 57 | private: 58 | 59 | void Read() 60 | { 61 | Prompt(); 62 | // Read a line of input entered by the user. 63 | asiolib::async_read_until( 64 | input, 65 | inputBuffer, 66 | '\n', 67 | std::bind( &GenericCliAsyncSession::NewLine, this, 68 | std::placeholders::_1, 69 | std::placeholders::_2 ) 70 | ); 71 | } 72 | 73 | void NewLine(const asiolibec::error_code& error, std::size_t length ) 74 | { 75 | if ( !error || error == asiolib::error::not_found ) 76 | { 77 | auto bufs = inputBuffer.data(); 78 | auto size = static_cast(length); 79 | if ( !error ) --size; // remove \n 80 | std::string s(asiolib::buffers_begin( bufs ), asiolib::buffers_begin( bufs ) + size); 81 | inputBuffer.consume( length ); 82 | 83 | Feed( s ); 84 | Read(); 85 | } 86 | else 87 | { 88 | input.close(); 89 | } 90 | } 91 | 92 | asiolib::streambuf inputBuffer; 93 | asiolib::posix::stream_descriptor input; 94 | }; 95 | 96 | } // namespace detail 97 | } // namespace cli 98 | 99 | #endif // CLI_DETAIL_GENERICCLIASYNCSESSION_H_ 100 | 101 | -------------------------------------------------------------------------------- /FMDynamic/Makefile: -------------------------------------------------------------------------------- 1 | # Command ard arguments 2 | CC ?= gcc 3 | CPPC ?= g++ 4 | PROTOC ?= protoc 5 | RM ?= rm 6 | CP ?= cp 7 | MV ?= mv 8 | MKDIR ?= mkdir 9 | AR ?= ar 10 | ARFLAGS ?= -rcs # ar needs the dash on OpenBSD 11 | RANLIB ?= ranlib 12 | 13 | # Path 14 | PROJ_HOME := ../ 15 | LIBFM_HOME := $(PROJ_HOME)/libFlowMatrix 16 | LIB_PATH := $(LIBFM_HOME)/lib 17 | INC_PATH := $(LIBFM_HOME)/include 18 | FMTRACE_PATH := $(PROJ_HOME)/trace/FMtrace/ 19 | FMTRACE_INC_PATH := $(FMTRACE_PATH)/include/ 20 | FMTRACE_STATIC_LIB_PATH := $(FMTRACE_PATH)/lib/ 21 | FMCUSPARSELINALG_PATH := $(PROJ_HOME)/FMCuSparseLinAlg/ 22 | FMCUSPARSELINALG_INC_PATH := $(FMCUSPARSELINALG_PATH)/include/ 23 | FMCUSPARSELINALG_STATIC_LIB_PATH := $(FMCUSPARSELINALG_PATH)/lib/ 24 | TEST_SRC_PATH := ./testsuite/ 25 | SRC_PATH := ./src 26 | CLI_SRC_PATH := $(SRC_PATH)/QueryCLI/ 27 | TRACE_LOADER_SRC_PATH := $(SRC_PATH)/TraceLoader/ 28 | BIN_PATH := ./bin 29 | 30 | # CUDA Related 31 | CUDA_TOOLKIT := $(shell dirname $$(command -v nvcc))/.. 32 | INC += -I$(CUDA_TOOLKIT)/include 33 | LDPATH += -L$(CUDA_TOOLKIT)/lib64 34 | 35 | # Compilation variables 36 | OPT ?= -O3 -g 37 | WARNINGS = -Wall -Wextra 38 | CPP_STD = -std=c++17 39 | PIC_FLAG ?= #-fPIC 40 | CFLAGS ?= $(OPT) $(WARNINGS) $(CPP_STD) $(PIC_FLAG) 41 | INC += -I$(INC_PATH) -I$(FMTRACE_INC_PATH) -I$(FMCUSPARSELINALG_INC_PATH) -I./ -I./include 42 | 43 | # Linking variables 44 | LDLIBS += -lcudart -lcusparse -lcapstone -lprotobuf -lsqlite3 -pthread 45 | #LDLIBS += $(wildcard $(PROJ_HOME)/libs/*) -pthread -ldl -lrt 46 | 47 | # Target 48 | OBJ := $(notdir $(patsubst %.cpp,%.o,$(wildcard $(SRC_PATH)/*.cpp))) 49 | cli_bin := $(patsubst %.cpp,%,$(wildcard $(CLI_SRC_PATH)/*.cpp)) 50 | traceload_bin := $(patsubst %.cpp,%,$(wildcard $(TRACE_LOADER_SRC_PATH)/*.cpp)) 51 | libfmtrace_static_lib := $(FMTRACE_STATIC_LIB_PATH)/libfmtrace.a 52 | libfm_static_lib := $(LIB_PATH)/libfm.a 53 | libfmlinalg_static_lib := $(FMCUSPARSELINALG_STATIC_LIB_PATH)/libfmlinalg.a 54 | PROG := $(BIN_PATH)/QueryCLI $(BIN_PATH)/TraceLoader $(BIN_PATH)/TraceGenerator $(BIN_PATH)/Worker 55 | 56 | 57 | all : $(PROG) 58 | 59 | $(BIN_PATH)/QueryCLI: $(CLI_SRC_PATH)/QueryCLI.o $(OBJ) $(libfmtrace_static_lib) $(libfm_static_lib) $(libfmlinalg_static_lib) 60 | @-$(MKDIR) -p $(BIN_PATH) 61 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDPATH) $(LDLIBS)) 62 | 63 | $(BIN_PATH)/TraceLoader: $(TRACE_LOADER_SRC_PATH)/TraceLoader.o $(OBJ) $(libfmtrace_static_lib) $(libfm_static_lib) $(libfmlinalg_static_lib) 64 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDPATH) $(LDLIBS)) 65 | 66 | $(BIN_PATH)/TraceGenerator: $(TRACE_LOADER_SRC_PATH)/TraceGenerator.o $(OBJ) $(libfmtrace_static_lib) $(libfm_static_lib) $(libfmlinalg_static_lib) 67 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDPATH) $(LDLIBS)) 68 | 69 | $(BIN_PATH)/Worker: $(CLI_SRC_PATH)/Worker.o $(OBJ) $(libfmtrace_static_lib) $(libfm_static_lib) $(libfmlinalg_static_lib) 70 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDPATH) $(LDLIBS)) 71 | 72 | $(TRACE_LOADER_SRC_PATH)/%: $(TRACE_LOADER_SRC_PATH)/%.o $(OBJ) $(libfmtrace_static_lib) $(libfm_static_lib) $(libfmlinalg_static_lib) 73 | $(CPPC) -o $@ $(strip $(CFLAGS) $^ $(LDPATH) $(LDLIBS)) 74 | 75 | %.o: $(SRC_PATH)/%.cpp 76 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $< -o $@ 77 | 78 | $(CLI_SRC_PATH)/%.o: $(CLI_SRC_PATH)/%.cpp 79 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $< -o $@ 80 | 81 | $(TRACE_LOADER_SRC_PATH)/%.o: $(TRACE_LOADER_SRC_PATH)/%.cpp 82 | $(CPPC) $(strip $(CFLAGS) $(INC) -c) $< -o $@ 83 | 84 | $(libfmtrace_static_lib): 85 | (cd $(FMTRACE_PATH) && make -j) 86 | 87 | $(libfm_static_lib): 88 | (cd $(LIBFM_HOME) && make -j) 89 | 90 | $(libfmlinalg_static_lib): 91 | (cd $(FMCUSPARSELINALG_PATH) && make -j) 92 | 93 | clean: 94 | $(RM) *.o *.gch 95 | $(RM) -rf ./bin 96 | (cd $(TRACE_LOADER_SRC_PATH) && $(RM) *.o) 97 | (cd $(CLI_SRC_PATH) && $(RM) *.o) 98 | (cd $(FMCUSPARSELINALG_PATH) && make clean) 99 | 100 | clean_tmp: 101 | $(RM) *.o *.gch 102 | 103 | .PHONY: clean clean_tmp 104 | -------------------------------------------------------------------------------- /FMCuSparseLinAlg/include/cuCommon.cuh: -------------------------------------------------------------------------------- 1 | #ifndef __FLOWMATRIX_UTILS_CU_COMMON_H_ 2 | #define __FLOWMATRIX_UTILS_CU_COMMON_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // #define DEBUG 14 | // #define TIME_MEASUREMENT 15 | 16 | #define BUFFER_INIT_SIZE 204800000 17 | 18 | #ifndef PYTHON_FLOWMATRIX 19 | typedef int32_t index_type_t; 20 | typedef float value_type_t; 21 | #define COMPUTE_TYPE CUDA_R_32F 22 | #define float2value(f) (f) 23 | #define value2float(h) (h) 24 | #else 25 | // typedef int64_t index_type_t; 26 | // typedef float value_type_t; 27 | // #define COMPUTE_TYPE CUDA_R_32F 28 | // #define float2value(f) (f) 29 | // #define value2float(h) (h) 30 | #endif 31 | 32 | #define LIKELY(x) __builtin_expect(!!(x), 1) 33 | #define UNLIKELY(x) __builtin_expect(!!(x), 0) 34 | #define CHECK_CUDA(call) \ 35 | { \ 36 | const cudaError_t error = call; \ 37 | if (UNLIKELY(error != cudaSuccess)) \ 38 | { \ 39 | fprintf(stderr, "Error: %s:%d, ", __FILE__, __LINE__); \ 40 | fprintf(stderr, "code: %d, reason: %s\n", error, \ 41 | cudaGetErrorString(error)); \ 42 | } \ 43 | } 44 | 45 | 46 | #define CHECK_CUSPARSE(call) \ 47 | { \ 48 | cusparseStatus_t err; \ 49 | if (UNLIKELY((err = (call)) != CUSPARSE_STATUS_SUCCESS)) \ 50 | { \ 51 | fprintf(stderr, "Got error %d (%s) at %s:%d\n", err, \ 52 | cusparseGetErrorName(err), __FILE__, __LINE__); \ 53 | cudaError_t cuda_err = cudaGetLastError(); \ 54 | if (cuda_err != cudaSuccess) \ 55 | { \ 56 | fprintf(stderr, " CUDA error \"%s\" also detected\n", \ 57 | cudaGetErrorString(cuda_err)); \ 58 | } \ 59 | exit(1); \ 60 | } \ 61 | } 62 | 63 | inline bool CHECK_CUSPARSE_NO_EXIT(const cusparseStatus_t &err) 64 | { 65 | if (UNLIKELY((err != CUSPARSE_STATUS_SUCCESS))) 66 | { 67 | fprintf(stderr, "Got error %d (%s) at %s:%d\n", err, 68 | cusparseGetErrorName(err), __FILE__, __LINE__); 69 | cudaError_t cuda_err = cudaGetLastError(); 70 | if (cuda_err != cudaSuccess) 71 | { 72 | fprintf(stderr, " CUDA error \"%s\" also detected\n", 73 | cudaGetErrorString(cuda_err)); 74 | } 75 | return false; 76 | } 77 | return true; 78 | } 79 | 80 | #endif // __FLOWMATRIX_UTILS_CU_COMMON_H_ 81 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/loopscheduler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_LOOPSCHEDULER_H_ 31 | #define CLI_LOOPSCHEDULER_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "scheduler.h" 38 | 39 | namespace cli 40 | { 41 | 42 | /** 43 | * @brief The LoopScheduler is a simple thread-safe scheduler 44 | * 45 | */ 46 | class LoopScheduler : public Scheduler 47 | { 48 | public: 49 | LoopScheduler() = default; 50 | ~LoopScheduler() override 51 | { 52 | Stop(); 53 | } 54 | 55 | // non copyable 56 | LoopScheduler(const LoopScheduler&) = delete; 57 | LoopScheduler& operator=(const LoopScheduler&) = delete; 58 | 59 | void Stop() 60 | { 61 | std::lock_guard lck (mtx); 62 | running = false; 63 | cv.notify_all(); 64 | } 65 | 66 | void Run() 67 | { 68 | while( ExecOne() ) {}; 69 | } 70 | 71 | bool Stopped() const 72 | { 73 | std::lock_guard lck (mtx); 74 | return !running; 75 | } 76 | 77 | void Post(const std::function& f) override 78 | { 79 | std::lock_guard lck (mtx); 80 | tasks.push(f); 81 | cv.notify_all(); 82 | } 83 | 84 | bool ExecOne() 85 | { 86 | std::function task; 87 | { 88 | std::unique_lock lck(mtx); 89 | cv.wait(lck, [this](){ return !running || !tasks.empty(); }); 90 | if (!running) 91 | return false; 92 | task = tasks.front(); 93 | tasks.pop(); 94 | } 95 | 96 | if (task) 97 | task(); 98 | 99 | return true; 100 | } 101 | 102 | bool PollOne() 103 | { 104 | std::function task; 105 | { 106 | std::lock_guard lck(mtx); 107 | if (!running || tasks.empty()) 108 | return false; 109 | task = tasks.front(); 110 | tasks.pop(); 111 | } 112 | 113 | if (task) 114 | task(); 115 | 116 | return true; 117 | } 118 | 119 | private: 120 | std::queue> tasks; 121 | bool running{ true }; 122 | mutable std::mutex mtx; 123 | std::condition_variable cv; 124 | }; 125 | 126 | } // namespace cli 127 | 128 | #endif // CLI_LOOPSCHEDULER_H_ 129 | -------------------------------------------------------------------------------- /FMCuSparseLinAlg/testsuite/spgeam_test.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace FlowMatrix; 4 | 5 | int main(){ 6 | // Test A+B = C 7 | // A-B = D 8 | const int A_num_rows = 4; 9 | const int A_num_cols = 4; 10 | const int A_nnz = 9; 11 | const int B_num_rows = 4; 12 | const int B_num_cols = 4; 13 | const int B_nnz = 8; 14 | int hA_csrOffsets[] = { 0, 3, 4, 7, 9 }; 15 | int hA_columns[] = { 0, 2, 3, 1, 0, 2, 3, 1, 3 }; 16 | float hA_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 17 | 6.0f, 7.0f, 8.0f, 9.0f }; 18 | int hB_csrOffsets[] = { 0, 2, 4, 7, 8 }; 19 | int hB_columns[] = { 0, 3, 1, 3, 0, 1, 2, 1 }; 20 | float hB_values[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 21 | 6.0f, 7.0f, 8.0f }; 22 | int hC_csrOffsets[] = { 0, 3, 5, 9, 11 }; 23 | int hC_columns[] = { 0, 2, 3, 1, 3, 0, 1, 2,3, 1, 3 }; 24 | float hC_values[] = { 2.0f, 2.0f, 5.0f, 7.0f, 4.0f, 25 | 10.0f, 6.0f, 13.0f, 7.0f, 16.0f, 26 | 9.0f}; 27 | const int C_nnz = 11; 28 | int hD_csrOffsets[] = { 0, 3, 5, 9, 11 }; 29 | int hD_columns[] = { 0, 2, 3, 1, 3, 0, 1, 2, 3, 1, 3 }; 30 | float hD_values[] = { 0.0f, 2.0f, 1.0f, 1.0f, -4.0f, 31 | 0.0f, -6.0f, -1.0f, 7.0f, 0.0f, 9.0f}; 32 | // const int D_nnz = 8; 33 | 34 | // Init cusparse handler 35 | auto sp_core_1 = new CuSparseCore(); 36 | auto sp_core_2 = new CuSparseCore(); 37 | 38 | // Create matrix 39 | cuSparseMatrix matA(sp_core_1->stream, A_num_rows, A_num_cols, A_nnz, hA_csrOffsets, hA_columns, hA_values); 40 | cuSparseMatrix matB(sp_core_2->stream, B_num_rows, B_num_cols, B_nnz, hB_csrOffsets, hB_columns, hB_values); 41 | sp_core_1->sync(); 42 | sp_core_2->sync(); 43 | 44 | // Compute 45 | cuSparseMatrix *matCPtr = sp_core_1->add(matA, matB); 46 | cuSparseMatrix *matDPtr = sp_core_2->add(matA, matB, 1.0, -1.0); 47 | 48 | // To host memory 49 | matCPtr->toHost(sp_core_1->stream); 50 | matDPtr->toHost(sp_core_2->stream); 51 | sp_core_1->sync(); 52 | sp_core_2->sync(); 53 | 54 | // Verification 55 | int correct = 1; 56 | for (int i = 0; i < A_num_rows + 1; i++) { 57 | if (matCPtr->h_csrOffsets[i] != hC_csrOffsets[i]) { 58 | correct = 0; 59 | break; 60 | } 61 | } 62 | for (int i = 0; i < C_nnz; i++) { 63 | if (matCPtr->h_columns[i] != hC_columns[i] || 64 | matCPtr->h_values[i] != hC_values[i]) { // direct floating point 65 | correct = 0; // comparison is not reliable 66 | break; 67 | } 68 | } 69 | if (correct) 70 | printf("SpGEAM ADD test PASSED\n"); 71 | else { 72 | printf("SpGEAM ADD test FAILED: Wrong Result\n"); 73 | return EXIT_FAILURE; 74 | } 75 | 76 | // printf("RowPtr: "); 77 | // for (int i = 0; i < A_num_rows + 1; i++) { 78 | // printf("%d ", matDPtr->h_csrOffsets[i]); 79 | // } 80 | // printf("\nCol: "); 81 | // for (int i = 0; i < matDPtr->nnz; i++) { 82 | // printf("%d ", matDPtr->h_columns[i]); 83 | // } 84 | // printf("\nValue: "); 85 | // for (int i = 0; i < matDPtr->nnz; i++) { 86 | // printf("%0.2f ", matDPtr->h_values[i]); 87 | // } 88 | // printf("\n"); 89 | 90 | for (int i = 0; i < A_num_rows + 1; i++) { 91 | if (matDPtr->h_csrOffsets[i] != hD_csrOffsets[i]) { 92 | correct = 0; 93 | break; 94 | } 95 | } 96 | for (int i = 0; i < matDPtr->nnz; i++) { 97 | if (matDPtr->h_columns[i] != hD_columns[i] || 98 | matDPtr->h_values[i] != hD_values[i]) { // direct floating point 99 | correct = 0; // comparison is not reliable 100 | break; 101 | } 102 | } 103 | 104 | if (correct) 105 | printf("SpGEAM Subtraction test PASSED\n"); 106 | else { 107 | printf("SpGEAM Subtraction test FAILED: Wrong Result\n"); 108 | return EXIT_FAILURE; 109 | } 110 | 111 | // Free pointer 112 | delete matCPtr; 113 | delete matDPtr; 114 | } -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/winkeyboard.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_WINKEYBOARD_H_ 31 | #define CLI_DETAIL_WINKEYBOARD_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "inputdevice.h" 41 | 42 | namespace cli 43 | { 44 | namespace detail 45 | { 46 | 47 | class WinKeyboard : public InputDevice 48 | { 49 | public: 50 | explicit WinKeyboard(Scheduler& _scheduler) : InputDevice(_scheduler) 51 | { 52 | servant = std::make_unique( [this](){ Read(); } ); 53 | servant->detach(); 54 | } 55 | ~WinKeyboard() 56 | { 57 | run = false; 58 | } 59 | 60 | private: 61 | void Read() 62 | { 63 | while (run) 64 | { 65 | auto k = Get(); 66 | Notify(k); 67 | } 68 | } 69 | 70 | std::pair Get() 71 | { 72 | int c = _getch(); 73 | switch (c) 74 | { 75 | case EOF: 76 | case 4: // EOT ie CTRL-D 77 | case 26: // CTRL-Z 78 | case 3: // CTRL-C 79 | return std::make_pair(KeyType::eof, ' '); 80 | break; 81 | 82 | case 224: // symbol 83 | { 84 | c = _getch(); 85 | switch (c) 86 | { 87 | case 72: return std::make_pair(KeyType::up, ' '); 88 | case 80: return std::make_pair(KeyType::down, ' '); 89 | case 75: return std::make_pair(KeyType::left, ' '); 90 | case 77: return std::make_pair(KeyType::right, ' '); 91 | case 71: return std::make_pair(KeyType::home, ' '); 92 | case 79: return std::make_pair(KeyType::end, ' '); 93 | case 83: return std::make_pair(KeyType::canc, ' '); 94 | default: return std::make_pair(KeyType::ignored, ' '); 95 | } 96 | } 97 | case 8: 98 | return std::make_pair(KeyType::backspace, c); 99 | break; 100 | case 13: 101 | return std::make_pair(KeyType::ret, c); 102 | break; 103 | default: // hopefully ascii 104 | { 105 | const char ch = static_cast(c); 106 | return std::make_pair(KeyType::ascii, ch); 107 | } 108 | } 109 | return std::make_pair(KeyType::ignored, ' '); 110 | } 111 | 112 | std::atomic run{true}; 113 | std::unique_ptr servant; 114 | }; 115 | 116 | } // namespace detail 117 | } // namespace cli 118 | 119 | #endif // CLI_DETAIL_WINKEYBOARD_H_ 120 | -------------------------------------------------------------------------------- /libFlowMatrix/src/matrix_base.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | MatrixBase::MatrixBase() 4 | { 5 | isOnDevice = false; 6 | 7 | row = nullptr; 8 | col = nullptr; 9 | val = nullptr; 10 | } 11 | 12 | int MatrixBase::to_csr() 13 | { 14 | // Condition check 15 | if (isOnDevice) return -1; 16 | if (isCSR) return -2; 17 | 18 | // Convert! 19 | const int row_ptr_size = m + 1; 20 | int *csr_row = new int[row_ptr_size]; 21 | memset(csr_row, 0, sizeof(int)*row_ptr_size); 22 | for (int i = 0; i < nnz; i++) 23 | csr_row[row[i] + 1]++; 24 | for (int i = 0; i < m; i++) 25 | csr_row[i + 1] += csr_row[i]; 26 | 27 | // Free the old row and override 28 | delete [] row; 29 | row = csr_row; 30 | 31 | // Set propety and normal return 32 | isCSR = true; 33 | return 0; 34 | } 35 | 36 | int MatrixBase::to_coo() 37 | { 38 | // Condition check 39 | if (isOnDevice) return -1; 40 | if (!isCSR) return -2; 41 | 42 | // Convert! 43 | int *coo_row = new int[nnz]; 44 | memset(coo_row, 0, sizeof(int)*nnz); 45 | int row_index = 0; 46 | for (int i = 0; i < nnz; i++) 47 | { 48 | while(row[row_index+1]<=i) row_index++; 49 | coo_row[i] = row_index; 50 | } 51 | 52 | // Assertion check. Should never happen if the input CSR is valid. 53 | if ((row_index+1 != m) || 54 | (row[row_index+1] != nnz)) 55 | return -3; 56 | 57 | // Free the old row and override 58 | delete [] row; 59 | row = coo_row; 60 | 61 | // Set propety and normal return 62 | isCSR = false; 63 | return 0; 64 | } 65 | 66 | void MatrixBase::print() 67 | { 68 | if (isOnDevice) 69 | { 70 | LOG("[E]Cannot print on-device matrix!"); 71 | return; 72 | } 73 | 74 | std::cout << "Matrix Size: " << m << " * " << n << std::endl; 75 | std::cout << "NNZ: " << nnz << std::endl; 76 | 77 | // Print Row 78 | if (isCSR) 79 | { 80 | std::cout << "CSR format:" << std::endl; 81 | std::cout << " Row = ["; 82 | for (int i=0; i \n", argv[0]); 53 | exit(0); 54 | } 55 | std::string trace_name(argv[1]); 56 | std::string dbPath(argv[2]); 57 | 58 | int num_instr = sizeof(memrefs)/sizeof(sample_memref_t); 59 | assert(num_instr == sizeof(rawbytes_list)/sizeof(uint8_t *)); 60 | instr_t *instr_list = forgeInstr(rawbytes_list, memrefs, num_instr); 61 | 62 | 63 | // Init vstate with default settings 64 | VState vstate = VState(FMGranularity::byte); 65 | vstate.init_reg_state(FMRegListConfig_default); 66 | if (vstate.granu == FMGranularity::bit) 67 | trace_name += "_bit"; 68 | 69 | // Init database 70 | DBCore db(dbPath, trace_name); 71 | 72 | // Extend vstate with mem accesses in FM trace 73 | for (int i=0; i 0) 101 | { 102 | int matrix_size, nnz; 103 | int* row, * col; 104 | float* value; 105 | fmrule.coo(matrix_size, nnz, row, col, value); 106 | db.Store(instr.instr_id, instr.instr_id, matrix_size, nnz, row, col, value, num_valid_indices, valid_indices, rawbyte_str, asm_str, USE_TRANSACTION); 107 | } 108 | else 109 | { 110 | if (num_valid_indices > 0) 111 | db.Store(instr.instr_id, instr.instr_id, vstate.size, 0, NULL, NULL, NULL, num_valid_indices, valid_indices, rawbyte_str, asm_str, USE_TRANSACTION); 112 | else 113 | db.Store(instr.instr_id, instr.instr_id, vstate.size, 0, NULL, NULL, NULL, 0, NULL, rawbyte_str, asm_str, USE_TRANSACTION); 114 | } 115 | } 116 | else 117 | { 118 | db.Store(instr.instr_id, instr.instr_id, vstate.size, 0, NULL, NULL, NULL, 0, NULL, rawbyte_str, asm_str, USE_TRANSACTION); 119 | } 120 | 121 | if (valid_indices != NULL) delete [] valid_indices; 122 | 123 | } 124 | 125 | delete [] instr_list; 126 | } -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/inputhandler.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_INPUTHANDLER_H_ 31 | #define CLI_DETAIL_INPUTHANDLER_H_ 32 | 33 | #include 34 | #include 35 | #include "terminal.h" 36 | #include "inputdevice.h" 37 | #include "../cli.h" // CliSession 38 | #include "commonprefix.h" 39 | 40 | namespace cli 41 | { 42 | namespace detail 43 | { 44 | 45 | class InputHandler 46 | { 47 | public: 48 | InputHandler(CliSession& _session, InputDevice& kb) : 49 | session(_session), 50 | terminal(session.OutStream()) 51 | { 52 | kb.Register( [this](auto key){ this->Keypressed(key); } ); 53 | } 54 | 55 | private: 56 | 57 | void Keypressed(std::pair k) 58 | { 59 | const std::pair s = terminal.Keypressed(k); 60 | NewCommand(s); 61 | } 62 | 63 | void NewCommand(const std::pair& s) 64 | { 65 | switch (s.first) 66 | { 67 | case Symbol::nothing: 68 | { 69 | break; 70 | } 71 | case Symbol::eof: 72 | { 73 | session.Exit(); 74 | break; 75 | } 76 | case Symbol::command: 77 | { 78 | session.Feed(s.second); 79 | session.Prompt(); 80 | break; 81 | } 82 | case Symbol::down: 83 | { 84 | terminal.SetLine(session.NextCmd()); 85 | break; 86 | } 87 | case Symbol::up: 88 | { 89 | auto line = terminal.GetLine(); 90 | terminal.SetLine(session.PreviousCmd(line)); 91 | break; 92 | } 93 | case Symbol::tab: 94 | { 95 | auto line = terminal.GetLine(); 96 | auto completions = session.GetCompletions(line); 97 | 98 | if (completions.empty()) 99 | break; 100 | if (completions.size() == 1) 101 | { 102 | terminal.SetLine(completions[0]+' '); 103 | break; 104 | } 105 | 106 | auto commonPrefix = CommonPrefix(completions); 107 | if (commonPrefix.size() > line.size()) 108 | { 109 | terminal.SetLine(commonPrefix); 110 | break; 111 | } 112 | session.OutStream() << '\n'; 113 | std::string items; 114 | std::for_each( completions.begin(), completions.end(), [&items](auto& cmd){ items += '\t' + cmd; } ); 115 | session.OutStream() << items << '\n'; 116 | session.Prompt(); 117 | terminal.ResetCursor(); 118 | terminal.SetLine( line ); 119 | break; 120 | } 121 | } 122 | 123 | } 124 | 125 | CliSession& session; 126 | Terminal terminal; 127 | }; 128 | 129 | } // namespace detail 130 | } // namespace cli 131 | 132 | #endif // CLI_DETAIL_INPUTHANDLER_H_ 133 | 134 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/linuxkeyboard.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_LINUXKEYBOARD_H_ 31 | #define CLI_DETAIL_LINUXKEYBOARD_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | #include "inputdevice.h" 42 | 43 | 44 | namespace cli 45 | { 46 | namespace detail 47 | { 48 | 49 | class LinuxKeyboard : public InputDevice 50 | { 51 | public: 52 | explicit LinuxKeyboard(Scheduler& _scheduler) : 53 | InputDevice(_scheduler) 54 | { 55 | ToManualMode(); 56 | servant = std::make_unique( [this](){ Read(); } ); 57 | servant->detach(); 58 | } 59 | ~LinuxKeyboard() override 60 | { 61 | run = false; 62 | ToStandardMode(); 63 | } 64 | 65 | private: 66 | 67 | void Read() 68 | { 69 | while ( run ) 70 | { 71 | auto k = Get(); 72 | Notify(k); 73 | } 74 | } 75 | 76 | std::pair Get() 77 | { 78 | int ch = std::getchar(); 79 | switch( ch ) 80 | { 81 | case EOF: 82 | case 4: // EOT 83 | return std::make_pair(KeyType::eof,' '); 84 | break; 85 | case 127: return std::make_pair(KeyType::backspace,' '); break; 86 | case 10: return std::make_pair(KeyType::ret,' '); break; 87 | case 27: // symbol 88 | ch = std::getchar(); 89 | if ( ch == 91 ) // arrow keys 90 | { 91 | ch = std::getchar(); 92 | switch( ch ) 93 | { 94 | case 51: 95 | ch = std::getchar(); 96 | if ( ch == 126 ) return std::make_pair(KeyType::canc,' '); 97 | else return std::make_pair(KeyType::ignored,' '); 98 | break; 99 | case 65: return std::make_pair(KeyType::up,' '); 100 | case 66: return std::make_pair(KeyType::down,' '); 101 | case 68: return std::make_pair(KeyType::left,' '); 102 | case 67: return std::make_pair(KeyType::right,' '); 103 | case 70: return std::make_pair(KeyType::end,' '); 104 | case 72: return std::make_pair(KeyType::home,' '); 105 | default: return std::make_pair(KeyType::ignored,' '); 106 | } 107 | } 108 | break; 109 | default: // ascii 110 | { 111 | const char c = static_cast(ch); 112 | return std::make_pair(KeyType::ascii,c); 113 | } 114 | } 115 | return std::make_pair(KeyType::ignored,' '); 116 | } 117 | 118 | void ToManualMode() 119 | { 120 | constexpr tcflag_t ICANON_FLAG = ICANON; 121 | constexpr tcflag_t ECHO_FLAG = ECHO; 122 | 123 | tcgetattr( STDIN_FILENO, &oldt ); 124 | newt = oldt; 125 | newt.c_lflag &= ~( ICANON_FLAG | ECHO_FLAG ); 126 | tcsetattr( STDIN_FILENO, TCSANOW, &newt ); 127 | } 128 | 129 | void ToStandardMode() 130 | { 131 | tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); 132 | } 133 | 134 | termios oldt; 135 | termios newt; 136 | std::atomic run{ true }; 137 | std::unique_ptr servant; 138 | }; 139 | 140 | } // namespace detail 141 | } // namespace cli 142 | 143 | #endif // CLI_DETAIL_LINUXKEYBOARD_H_ 144 | 145 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/server.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_SERVER_H_ 31 | #define CLI_DETAIL_SERVER_H_ 32 | 33 | #include 34 | #include 35 | 36 | namespace cli 37 | { 38 | namespace detail 39 | { 40 | 41 | class Session : public std::enable_shared_from_this, public std::streambuf 42 | { 43 | public: 44 | ~Session() override = default; 45 | virtual void Start() 46 | { 47 | OnConnect(); 48 | Read(); 49 | } 50 | 51 | protected: 52 | 53 | explicit Session(asiolib::ip::tcp::socket _socket) : socket(std::move(_socket)), outStream( this ) {} 54 | 55 | virtual void Disconnect() 56 | { 57 | socket.shutdown(asiolib::ip::tcp::socket::shutdown_both); 58 | socket.close(); 59 | } 60 | 61 | virtual void Read() 62 | { 63 | auto self( shared_from_this() ); 64 | socket.async_read_some(asiolib::buffer( data, max_length ), 65 | [ this, self ]( asiolibec::error_code ec, std::size_t length ) 66 | { 67 | if ( !socket.is_open() || ( ec == asiolib::error::eof ) || ( ec == asiolib::error::connection_reset ) ) 68 | OnDisconnect(); 69 | else if ( ec ) 70 | OnError(); 71 | else 72 | { 73 | OnDataReceived( std::string( data, length )); 74 | Read(); 75 | } 76 | }); 77 | } 78 | 79 | virtual void Send(const std::string& msg) 80 | { 81 | asiolibec::error_code ec; 82 | asiolib::write(socket, asiolib::buffer(msg), ec); 83 | if ((ec == asiolib::error::eof) || (ec == asiolib::error::connection_reset)) 84 | OnDisconnect(); 85 | else if (ec) 86 | OnError(); 87 | } 88 | 89 | virtual std::ostream& OutStream() { return outStream; } 90 | 91 | virtual void OnConnect() = 0; 92 | virtual void OnDisconnect() = 0; 93 | virtual void OnError() = 0; 94 | virtual void OnDataReceived(const std::string& _data) = 0; 95 | 96 | virtual std::string Encode(const std::string& _data) const { return _data; } 97 | 98 | private: 99 | 100 | // std::streambuf 101 | std::streamsize xsputn( const char* s, std::streamsize n ) override 102 | { 103 | Send(Encode(std::string(s, s+n))); 104 | return n; 105 | } 106 | int overflow( int c ) override 107 | { 108 | Send(Encode(std::string(1, static_cast< char >(c)))); 109 | return c; 110 | } 111 | 112 | asiolib::ip::tcp::socket socket; 113 | enum { max_length = 1024 }; 114 | char data[ max_length ]; 115 | std::ostream outStream; 116 | }; 117 | 118 | 119 | template 120 | class Server 121 | { 122 | public: 123 | // disable value semantics 124 | Server( const Server& ) = delete; 125 | Server& operator = ( const Server& ) = delete; 126 | 127 | Server(typename ASIOLIB::ContextType& ios, unsigned short port) : 128 | acceptor(ios, asiolib::ip::tcp::endpoint(asiolib::ip::tcp::v4(), port)), 129 | socket(ios) 130 | { 131 | Accept(); 132 | } 133 | Server(typename ASIOLIB::ContextType& ios, std::string address, unsigned short port) : 134 | acceptor(ios, asiolib::ip::tcp::endpoint(ASIOLIB::IpAddressFromString(address), port)), 135 | socket(ios) 136 | { 137 | Accept(); 138 | } 139 | virtual ~Server() = default; 140 | // returns shared_ptr instead of unique_ptr because Session needs to use enable_shared_from_this 141 | virtual std::shared_ptr CreateSession(asiolib::ip::tcp::socket socket) = 0; 142 | private: 143 | void Accept() 144 | { 145 | acceptor.async_accept(socket, [this](asiolibec::error_code ec) 146 | { 147 | if (!ec) CreateSession(std::move(socket))->Start(); 148 | Accept(); 149 | }); 150 | } 151 | asiolib::ip::tcp::acceptor acceptor; 152 | asiolib::ip::tcp::socket socket; 153 | }; 154 | 155 | } // namespace detail 156 | } // namespace cli 157 | 158 | #endif // CLI_DETAIL_SERVER_H_ 159 | 160 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/history.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_HISTORY_H_ 31 | #define CLI_DETAIL_HISTORY_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | namespace cli 41 | { 42 | namespace detail 43 | { 44 | 45 | class History 46 | { 47 | public: 48 | 49 | explicit History(std::size_t size) : maxSize(size) {} 50 | 51 | // Insert a new item in the buffer, changing the current state to "inserting" 52 | // If we're browsing the history (eg with arrow keys) the new item overwrites 53 | // the current one. 54 | // Otherwise, the item is added to the front of the container 55 | void NewCommand(const std::string& item) 56 | { 57 | ++commands; 58 | current = 0; 59 | if (mode == Mode::browsing) 60 | { 61 | assert(!buffer.empty()); 62 | if (buffer.size() > 1 && buffer[1] == item) // try to insert an element identical to last one 63 | buffer.pop_front(); 64 | else // the item was not identical 65 | buffer[current] = item; 66 | } 67 | else // Mode::inserting 68 | { 69 | if (buffer.empty() || buffer[0] != item) // insert an element not equal to last one 70 | Insert(item); 71 | } 72 | mode = Mode::inserting; 73 | } 74 | 75 | // Return the previous item of the history, updating the current item and 76 | // changing the current state to "browsing" 77 | // If we're already browsing the history (eg with arrow keys) the edit line is inserted 78 | // to the front of the container. 79 | // Otherwise, the line overwrites the current item. 80 | std::string Previous(const std::string& line) 81 | { 82 | if (mode == Mode::inserting) 83 | { 84 | Insert(line); 85 | mode = Mode::browsing; 86 | current = (buffer.size() > 1) ? 1 : 0; 87 | } 88 | else // Mode::browsing 89 | { 90 | assert(!buffer.empty()); 91 | buffer[current] = line; 92 | if (current != buffer.size()-1) 93 | ++current; 94 | } 95 | assert(mode == Mode::browsing); 96 | assert(current < buffer.size()); 97 | return buffer[current]; 98 | } 99 | 100 | // Return the next item of the history, updating the current item. 101 | std::string Next() 102 | { 103 | if (buffer.empty() || current == 0) 104 | return {}; 105 | assert(current != 0); 106 | --current; 107 | assert(current < buffer.size()); 108 | return buffer[current]; 109 | } 110 | 111 | // Show the whole history on the given ostream 112 | void Show(std::ostream& out) const 113 | { 114 | out << '\n'; 115 | for (auto& item: buffer) 116 | out << item << '\n'; 117 | out << '\n' << std::flush; 118 | } 119 | 120 | // cmds[0] is the oldest command, cmds[size-1] the newer 121 | void LoadCommands(const std::vector& cmds) 122 | { 123 | for (const auto& c: cmds) 124 | Insert(c); 125 | } 126 | 127 | // result[0] is the oldest command, result[size-1] the newer 128 | std::vector GetCommands() const 129 | { 130 | auto numCmdsToReturn = std::min(commands, buffer.size()); 131 | auto start = buffer.begin(); 132 | if (mode == Mode::browsing) 133 | { 134 | numCmdsToReturn = std::min(commands, buffer.size()-1); 135 | start = buffer.begin()+1; 136 | } 137 | std::vector result(numCmdsToReturn); 138 | assert(std::distance(start, buffer.end()) >= 0); 139 | assert(numCmdsToReturn <= static_cast(std::distance(buffer.end(), start))); 140 | assert(numCmdsToReturn <= static_cast(std::numeric_limits::max())); 141 | std::reverse_copy(start, start+static_cast(numCmdsToReturn), result.begin()); 142 | return result; 143 | } 144 | 145 | private: 146 | 147 | void Insert(const std::string& item) 148 | { 149 | buffer.push_front(item); 150 | if (buffer.size() > maxSize) 151 | buffer.pop_back(); 152 | } 153 | 154 | const std::size_t maxSize; 155 | std::deque buffer; 156 | std::size_t current = 0; 157 | std::size_t commands = 0; // number of commands issued 158 | enum class Mode { inserting, browsing }; 159 | Mode mode = Mode::inserting; 160 | }; 161 | 162 | } // namespace detail 163 | } // namespace cli 164 | 165 | #endif // CLI_DETAIL_HISTORY_H_ 166 | -------------------------------------------------------------------------------- /trace/FMtrace/src/trace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | template 4 | size_t FlowMatrix::TraceHandle::FlushBuffer() 5 | { 6 | size_t rvalue = fwrite(this->buffer,sizeof(FrameType),this->buffer_size,this->trace_file); 7 | assert(rvalue == this->buffer_size); 8 | this->buffer_head = this->buffer; 9 | this->buffer_size = 0; 10 | return rvalue; 11 | } 12 | 13 | template 14 | size_t FlowMatrix::TraceHandle::LoadBuffer() 15 | { 16 | this->buffer_size = fread(this->buffer,sizeof(FrameType),BUFFER_SIZE,this->trace_file); 17 | this->buffer_head = this->buffer; 18 | return this->buffer_size; 19 | } 20 | 21 | template 22 | void FlowMatrix::TraceHandle::CreateTrace(const std::string &trace_path) 23 | { 24 | this->is_read_mode = false; 25 | this->trace_file = fopen(trace_path.c_str(), "wb"); 26 | if (this->trace_file == NULL) 27 | { 28 | LOG("[E] Unable to open %s. Terminate.", trace_path.c_str()); 29 | DIE(); 30 | } 31 | this->buffer = new FrameType[BUFFER_SIZE]; 32 | this->buffer_head = this->buffer; 33 | this->buffer_size = 0; 34 | } 35 | 36 | template 37 | void FlowMatrix::TraceHandle::LoadTrace(const std::string &trace_path) 38 | { 39 | this->is_read_mode = true; 40 | this->trace_file = fopen(trace_path.c_str(), "rb"); 41 | if (this->trace_file == NULL) 42 | { 43 | LOG("[E] Unable to open %s. Terminate.", trace_path.c_str()); 44 | DIE(); 45 | } 46 | this->buffer = new FrameType[BUFFER_SIZE]; 47 | this->buffer_head = this->buffer; 48 | this->buffer_size = 0; 49 | } 50 | 51 | template 52 | void FlowMatrix::TraceHandle::CloseTrace() 53 | { 54 | if (this->buffer!=NULL) 55 | { 56 | // Flush the write buffer in write mode 57 | if (!this->is_read_mode) this->FlushBuffer(); 58 | // Free buffer 59 | delete[] this->buffer; 60 | } 61 | 62 | // Close file 63 | if (this->trace_file!=NULL) 64 | { 65 | fclose(this->trace_file); 66 | } 67 | 68 | // Set every ptr to NULL 69 | this->buffer = NULL; 70 | this->buffer_head = NULL; 71 | this->trace_file = NULL; 72 | } 73 | 74 | template 75 | void FlowMatrix::TraceHandle::WriteTrace(const FrameType &frame) 76 | { 77 | std::memcpy(this->buffer_head, &frame, sizeof(FrameType)); 78 | this->buffer_head++; 79 | this->buffer_size++; 80 | if (buffer_size >= BUFFER_SIZE) this->FlushBuffer(); 81 | } 82 | 83 | template 84 | size_t FlowMatrix::TraceHandle::ReadNext(FrameType &frame) 85 | { 86 | if (this->buffer + this->buffer_size == this->buffer_head) 87 | { 88 | // Buffer is empty. Load more from file. 89 | this->LoadBuffer(); 90 | if (this->buffer_size == 0) 91 | { 92 | // EOF. Return with 0; 93 | return 0; 94 | } 95 | } 96 | std::memcpy(&frame, this->buffer_head, sizeof(FrameType)); 97 | this->buffer_head++; 98 | return 1; 99 | } 100 | 101 | template 102 | FlowMatrix::TraceHandle::TraceHandle() 103 | { 104 | // do nothing but init with 0 105 | this->buffer = NULL; 106 | this->buffer_head = NULL; 107 | this->trace_file = NULL; 108 | this->buffer_size = 0; 109 | this->is_read_mode = false; 110 | } 111 | 112 | template 113 | FlowMatrix::TraceHandle::~TraceHandle() 114 | { 115 | // Safe deconstruction. Just in case users forgot to call CloseTrace() 116 | this->CloseTrace(); 117 | } 118 | 119 | 120 | FlowMatrix::trace_handle_t *FlowMatrix::Trace::CreateTrace(const std::string &trace_name) 121 | { 122 | mkdir(trace_name.c_str(), 0777); 123 | trace_handle_t *handle = new(trace_handle_t); 124 | std::string instr_trace_name = trace_name + "/instr.fmtrace"; 125 | std::string sys_trace_name = trace_name + "/sys.fmtrace"; 126 | std::string mem_ref_trace_name = trace_name + "/mem_ref.fmtrace"; 127 | std::string log_path = trace_name + "/log.txt"; 128 | handle->instr_trace.CreateTrace(instr_trace_name); 129 | handle->syscall_trace.CreateTrace(sys_trace_name); 130 | handle->mem_ref_set.CreateTrace(mem_ref_trace_name); 131 | assert(freopen(log_path.c_str(),"w", stderr)!=NULL); 132 | return handle; 133 | } 134 | 135 | FlowMatrix::trace_handle_t *FlowMatrix::Trace::LoadTrace(const std::string &trace_name) 136 | { 137 | trace_handle_t *handle = new(trace_handle_t); 138 | std::string instr_trace_name = trace_name + "/instr.fmtrace"; 139 | std::string sys_trace_name = trace_name + "/sys.fmtrace"; 140 | std::string mem_ref_trace_name = trace_name + "/mem_ref.fmtrace"; 141 | std::string log_path = trace_name + "/log.txt"; 142 | handle->instr_trace.LoadTrace(instr_trace_name); 143 | handle->syscall_trace.LoadTrace(sys_trace_name); 144 | handle->mem_ref_set.LoadTrace(mem_ref_trace_name); 145 | assert(freopen(log_path.c_str(),"w", stderr)!=NULL); 146 | return handle; 147 | } 148 | 149 | void FlowMatrix::Trace::CloseTrace(trace_handle_t *trace_handle) 150 | { 151 | trace_handle->instr_trace.CloseTrace(); 152 | trace_handle->syscall_trace.CloseTrace(); 153 | delete trace_handle; 154 | } 155 | 156 | void FlowMatrix::instr_t::Print() 157 | { 158 | std::cout<<"["<instr_id<<"] "; 159 | std::string rawbyte_str = uint8_to_hex_string(this->raw_bytes, this->instr_len); 160 | std::cout<raw_bytes, this->instr_len, asm_str); 163 | std::cout << asm_str <<" (rflags=0x"<rflags<mem_ref_num>0) 165 | { 166 | std::cout<mem_ref_num; idx++) 168 | { 169 | std::cout<<" Mem[" << idx<< "]: 0x"<mem_ref_addr[idx]<instr_id<<"]Syscall "<sys_name 178 | << "(" <syscall_id << ")" << std::endl; 179 | for (uint64_t idx=0; idxnargs; idx++) 180 | { 181 | std::cout<<" Arg " << idx <<": "<args[idx]<mem_ref_addr << std::dec << std::endl; 188 | } 189 | 190 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/terminal.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_TERMINAL_H_ 31 | #define CLI_DETAIL_TERMINAL_H_ 32 | 33 | #include 34 | #include "../colorprofile.h" 35 | #include "inputdevice.h" 36 | 37 | namespace cli 38 | { 39 | namespace detail 40 | { 41 | 42 | enum class Symbol 43 | { 44 | nothing, 45 | command, 46 | up, 47 | down, 48 | tab, 49 | eof 50 | }; 51 | 52 | class Terminal 53 | { 54 | public: 55 | explicit Terminal(std::ostream &_out) : out(_out) {} 56 | 57 | void ResetCursor() { position = 0; } 58 | 59 | void SetLine(const std::string &newLine) 60 | { 61 | out << beforeInput 62 | << std::string(position, '\b') << newLine 63 | << afterInput << std::flush; 64 | 65 | // if newLine is shorter then currentLine, we have 66 | // to clear the rest of the string 67 | if (newLine.size() < currentLine.size()) 68 | { 69 | out << std::string(currentLine.size() - newLine.size(), ' '); 70 | // and go back 71 | out << std::string(currentLine.size() - newLine.size(), '\b') << std::flush; 72 | } 73 | 74 | currentLine = newLine; 75 | position = currentLine.size(); 76 | } 77 | 78 | std::string GetLine() const { return currentLine; } 79 | 80 | std::pair Keypressed(std::pair k) 81 | { 82 | switch (k.first) 83 | { 84 | case KeyType::eof: 85 | return std::make_pair(Symbol::eof, std::string{}); 86 | break; 87 | case KeyType::backspace: 88 | { 89 | if (position == 0) 90 | break; 91 | 92 | --position; 93 | 94 | const auto pos = static_cast(position); 95 | // remove the char from buffer 96 | currentLine.erase(currentLine.begin() + pos); 97 | // go back to the previous char 98 | out << '\b'; 99 | // output the rest of the line 100 | out << std::string(currentLine.begin() + pos, currentLine.end()); 101 | // remove last char 102 | out << ' '; 103 | // go back to the original position 104 | out << std::string(currentLine.size() - position + 1, '\b') << std::flush; 105 | break; 106 | } 107 | case KeyType::up: 108 | return std::make_pair(Symbol::up, std::string{}); 109 | break; 110 | case KeyType::down: 111 | return std::make_pair(Symbol::down, std::string{}); 112 | break; 113 | case KeyType::left: 114 | if (position > 0) 115 | { 116 | out << '\b' << std::flush; 117 | --position; 118 | } 119 | break; 120 | case KeyType::right: 121 | if (position < currentLine.size()) 122 | { 123 | out << beforeInput 124 | << currentLine[position] 125 | << afterInput << std::flush; 126 | ++position; 127 | } 128 | break; 129 | case KeyType::ret: 130 | { 131 | out << "\r\n"; 132 | auto cmd = currentLine; 133 | currentLine.clear(); 134 | position = 0; 135 | return std::make_pair(Symbol::command, cmd); 136 | } 137 | break; 138 | case KeyType::ascii: 139 | { 140 | const char c = static_cast(k.second); 141 | if (c == '\t') 142 | return std::make_pair(Symbol::tab, std::string()); 143 | else 144 | { 145 | const auto pos = static_cast(position); 146 | 147 | // output the new char: 148 | out << beforeInput << c; 149 | // and the rest of the string: 150 | out << std::string(currentLine.begin() + pos, currentLine.end()) 151 | << afterInput; 152 | 153 | // go back to the original position 154 | out << std::string(currentLine.size() - position, '\b') << std::flush; 155 | 156 | // update the buffer and cursor position: 157 | currentLine.insert(currentLine.begin() + pos, c); 158 | ++position; 159 | } 160 | 161 | break; 162 | } 163 | case KeyType::canc: 164 | { 165 | if (position == currentLine.size()) 166 | break; 167 | 168 | const auto pos = static_cast(position); 169 | 170 | // output the rest of the line 171 | out << std::string(currentLine.begin() + pos + 1, currentLine.end()); 172 | // remove last char 173 | out << ' '; 174 | // go back to the original position 175 | out << std::string(currentLine.size() - position, '\b') << std::flush; 176 | // remove the char from buffer 177 | currentLine.erase(currentLine.begin() + pos); 178 | break; 179 | } 180 | case KeyType::end: 181 | { 182 | const auto pos = static_cast(position); 183 | 184 | out << beforeInput 185 | << std::string(currentLine.begin() + pos, currentLine.end()) 186 | << afterInput << std::flush; 187 | position = currentLine.size(); 188 | break; 189 | } 190 | case KeyType::home: 191 | { 192 | out << std::string(position, '\b') << std::flush; 193 | position = 0; 194 | break; 195 | } 196 | case KeyType::ignored: 197 | // TODO 198 | break; 199 | } 200 | 201 | return std::make_pair(Symbol::nothing, std::string()); 202 | } 203 | 204 | private: 205 | std::string currentLine; 206 | std::size_t position = 0; // next writing position in currentLine 207 | std::ostream &out; 208 | }; 209 | 210 | } // namespace detail 211 | } // namespace cli 212 | 213 | #endif // CLI_DETAIL_TERMINAL_H_ 214 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/rang.h: -------------------------------------------------------------------------------- 1 | #ifndef CLI_DETAIL_RANG_H 2 | #define CLI_DETAIL_RANG_H 3 | 4 | #if defined(__unix__) || defined(__unix) || defined(__linux__) 5 | #define OS_LINUX 6 | #elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) 7 | #define OS_WIN 8 | #elif defined(__APPLE__) || defined(__MACH__) 9 | #define OS_MAC 10 | #else 11 | #error Unknown Platform 12 | #endif 13 | 14 | #if defined(OS_LINUX) || defined(OS_MAC) 15 | #include 16 | #include 17 | #elif defined(OS_WIN) 18 | #if !defined(NOMINMAX) 19 | #define NOMINMAX 1 20 | #endif // !defined(NOMINMAX) 21 | #include 22 | #include 23 | #include 24 | #endif 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace rang { 34 | 35 | enum class style { 36 | reset = 0, 37 | bold = 1, 38 | dim = 2, 39 | italic = 3, 40 | underline = 4, 41 | blink = 5, 42 | rblink = 6, 43 | reversed = 7, 44 | conceal = 8, 45 | crossed = 9 46 | }; 47 | 48 | enum class fg { 49 | black = 30, 50 | red = 31, 51 | green = 32, 52 | yellow = 33, 53 | blue = 34, 54 | magenta = 35, 55 | cyan = 36, 56 | gray = 37, 57 | reset = 39 58 | }; 59 | 60 | enum class bg { 61 | black = 40, 62 | red = 41, 63 | green = 42, 64 | yellow = 43, 65 | blue = 44, 66 | magenta = 45, 67 | cyan = 46, 68 | gray = 47, 69 | reset = 49 70 | }; 71 | 72 | enum class fgB { 73 | black = 90, 74 | red = 91, 75 | green = 92, 76 | yellow = 93, 77 | blue = 94, 78 | magenta = 95, 79 | cyan = 96, 80 | gray = 97 81 | }; 82 | 83 | enum class bgB { 84 | black = 100, 85 | red = 101, 86 | green = 102, 87 | yellow = 103, 88 | blue = 104, 89 | magenta = 105, 90 | cyan = 106, 91 | gray = 107 92 | }; 93 | 94 | enum class control { autoColor = 0, forceColor = 1 }; 95 | 96 | 97 | namespace rang_implementation { 98 | 99 | inline std::streambuf const *&RANG_coutbuf() 100 | { 101 | static std::streambuf const *pOutbuff = std::cout.rdbuf(); 102 | return pOutbuff; 103 | } 104 | 105 | inline std::streambuf const *&RANG_cerrbuf() 106 | { 107 | static std::streambuf const *pErrbuff = std::cerr.rdbuf(); 108 | return pErrbuff; 109 | } 110 | 111 | inline std::streambuf const *&RANG_clogbuf() 112 | { 113 | static std::streambuf const *pLogbuff = std::clog.rdbuf(); 114 | return pLogbuff; 115 | } 116 | 117 | inline int getIword() 118 | { 119 | static int i = std::ios_base::xalloc(); 120 | return i; 121 | } 122 | 123 | 124 | inline bool supportsColor() 125 | { 126 | #if defined(OS_LINUX) || defined(OS_MAC) 127 | static constexpr const char* Terms[] = { 128 | "ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", 129 | "linux", "msys", "putty", "rxvt", "screen", "vt100", "xterm" 130 | }; 131 | 132 | const char *env_p = std::getenv("TERM"); 133 | if (env_p == nullptr) { 134 | return false; 135 | } 136 | 137 | static const bool result = std::any_of( 138 | std::begin(Terms), std::end(Terms), [&](const char* term) { 139 | return std::strstr(env_p, term) != nullptr; 140 | }); 141 | 142 | #elif defined(OS_WIN) 143 | static constexpr bool result = true; 144 | #endif 145 | return result; 146 | } 147 | 148 | 149 | inline bool isTerminal(const std::streambuf *osbuf) 150 | { 151 | if (osbuf == RANG_coutbuf()) { 152 | #if defined(OS_LINUX) || defined(OS_MAC) 153 | return isatty(fileno(stdout)) ? true : false; 154 | #elif defined(OS_WIN) 155 | return _isatty(_fileno(stdout)) ? true : false; 156 | #endif 157 | } 158 | 159 | if (osbuf == RANG_cerrbuf() || osbuf == RANG_clogbuf()) { 160 | #if defined(OS_LINUX) || defined(OS_MAC) 161 | return isatty(fileno(stderr)) ? true : false; 162 | #elif defined(OS_WIN) 163 | return _isatty(_fileno(stderr)) ? true : false; 164 | #endif 165 | } 166 | return false; 167 | } 168 | 169 | 170 | template 171 | using enableStd = 172 | typename std::enable_if::value 173 | || std::is_same::value 174 | || std::is_same::value 175 | || std::is_same::value 176 | || std::is_same::value, 177 | std::ostream &>::type; 178 | 179 | 180 | #ifdef OS_WIN 181 | inline HANDLE getVersionDependentHandle() 182 | { 183 | if (IsWindowsVersionOrGreater(10, 0, 0)) return nullptr; 184 | return GetStdHandle(STD_OUTPUT_HANDLE); 185 | } 186 | 187 | inline HANDLE getConsoleHandle() 188 | { 189 | static HANDLE h = getVersionDependentHandle(); 190 | return h; 191 | } 192 | 193 | inline WORD reverseRGB(WORD rgb) 194 | { 195 | static const WORD rev[8] = { 0, 4, 2, 6, 1, 5, 3, 7 }; 196 | return rev[rgb]; 197 | } 198 | 199 | inline void setWinAttribute(rang::bg col, WORD &state) 200 | { 201 | state &= 0xFF0F; 202 | state |= reverseRGB(static_cast(col) - 40) << 4; 203 | } 204 | 205 | inline void setWinAttribute(rang::fg col, WORD &state) 206 | { 207 | state &= 0xFFF0; 208 | state |= reverseRGB(static_cast(col) - 30); 209 | } 210 | 211 | inline void setWinAttribute(rang::bgB col, WORD &state) 212 | { 213 | state &= 0xFF0F; 214 | state |= (0x8 | reverseRGB(static_cast(col) - 100)) << 4; 215 | } 216 | 217 | inline void setWinAttribute(rang::fgB col, WORD &state) 218 | { 219 | state &= 0xFFF0; 220 | state |= (0x8 | reverseRGB(static_cast(col) - 90)); 221 | } 222 | 223 | inline void setWinAttribute(rang::style style, WORD &state) 224 | { 225 | if (style == rang::style::reset) { 226 | state = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); 227 | } 228 | } 229 | 230 | inline WORD ¤t_state() 231 | { 232 | static WORD state 233 | = (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED); 234 | return state; 235 | } 236 | 237 | template 238 | inline enableStd setColor(std::ostream &os, T const value) 239 | { 240 | HANDLE h = getConsoleHandle(); 241 | if (h && isTerminal(os.rdbuf())) { 242 | setWinAttribute(value, current_state()); 243 | SetConsoleTextAttribute(h, current_state()); 244 | return os; 245 | } 246 | return os << "\033[" << static_cast(value) << "m"; 247 | } 248 | #else 249 | template 250 | inline enableStd setColor(std::ostream &os, T const value) 251 | { 252 | return os << "\033[" << static_cast(value) << "m"; 253 | } 254 | #endif 255 | 256 | template 257 | using enableControl = 258 | typename std::enable_if::value, 259 | std::ostream &>::type; 260 | } // namespace rang_implementation 261 | 262 | inline void init() 263 | { 264 | rang_implementation::RANG_coutbuf(); 265 | rang_implementation::RANG_cerrbuf(); 266 | rang_implementation::RANG_clogbuf(); 267 | } 268 | 269 | template 270 | inline rang_implementation::enableStd operator<<( 271 | std::ostream &os, T const value) 272 | { 273 | std::streambuf const *osbuf = os.rdbuf(); 274 | return (os.iword(rang_implementation::getIword()) 275 | || ((rang_implementation::supportsColor()) 276 | && (rang_implementation::isTerminal(osbuf)))) 277 | ? rang_implementation::setColor(os, value) 278 | : os; 279 | } 280 | 281 | template 282 | inline rang_implementation::enableControl operator<<( 283 | std::ostream &os, T const value) 284 | { 285 | if (value == rang::control::forceColor) { 286 | os.iword(rang_implementation::getIword()) = 1; 287 | } else if (value == rang::control::autoColor) { 288 | os.iword(rang_implementation::getIword()) = 0; 289 | } 290 | return os; 291 | } 292 | } // namespace rang 293 | 294 | #undef OS_LINUX 295 | #undef OS_WIN 296 | #undef OS_MAC 297 | 298 | #endif // CLI_DETAIL_RANG_H 299 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/split.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_SPLIT_H_ 31 | #define CLI_DETAIL_SPLIT_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | namespace cli 40 | { 41 | namespace detail 42 | { 43 | 44 | class Text 45 | { 46 | public: 47 | explicit Text(std::string _input) : input(std::move(_input)) 48 | { 49 | } 50 | void SplitInto(std::vector& strs) 51 | { 52 | Reset(); 53 | for (char c: input) 54 | Eval(c); 55 | RemoveEmptyEntries(); 56 | splitResult.swap(strs); // puts the result back in strs 57 | } 58 | private: 59 | void Reset() 60 | { 61 | state = State::space; 62 | prev_state = State::space; 63 | sentence_type = SentenceType::double_quote; 64 | splitResult.clear(); 65 | } 66 | 67 | void Eval(char c) 68 | { 69 | switch(state) 70 | { 71 | case State::space: 72 | EvalSpace(c); 73 | break; 74 | case State::word: 75 | EvalWord(c); 76 | break; 77 | case State::sentence: 78 | EvalSentence(c); 79 | break; 80 | case State::escape: 81 | EvalEscape(c); 82 | break; 83 | } 84 | } 85 | 86 | void EvalSpace(char c) 87 | { 88 | if (c == ' ' || c == '\t' || c == '\n') 89 | { 90 | // do nothing 91 | } 92 | else if (c == '"' || c == '\'') 93 | { 94 | NewSentence(c); 95 | } 96 | else if (c == '\\') 97 | { 98 | // This is the case where the first character of a word is escaped. 99 | // Should come back into the word state after this. 100 | prev_state = State::word; 101 | state = State::escape; 102 | splitResult.emplace_back(""); 103 | } 104 | else 105 | { 106 | state = State::word; 107 | splitResult.emplace_back(1, c); 108 | } 109 | } 110 | 111 | void EvalWord(char c) 112 | { 113 | if (c == ' ' || c == '\t' || c == '\n') 114 | { 115 | state = State::space; 116 | } 117 | else if (c == '"' || c == '\'') 118 | { 119 | NewSentence(c); 120 | } 121 | else if (c == '\\') 122 | { 123 | prev_state = state; 124 | state = State::escape; 125 | } 126 | else 127 | { 128 | assert(!splitResult.empty()); 129 | splitResult.back() += c; 130 | } 131 | } 132 | 133 | void EvalSentence(char c) 134 | { 135 | if (c == '"' || c == '\'') 136 | { 137 | auto new_type = c == '"' ? SentenceType::double_quote : SentenceType::quote; 138 | if (new_type == sentence_type) 139 | state = State::space; 140 | else 141 | { 142 | assert(!splitResult.empty()); 143 | splitResult.back() += c; 144 | } 145 | } 146 | else if (c == '\\') 147 | { 148 | prev_state = state; 149 | state = State::escape; 150 | } 151 | else 152 | { 153 | assert(!splitResult.empty()); 154 | splitResult.back() += c; 155 | } 156 | } 157 | 158 | void EvalEscape(char c) 159 | { 160 | assert(!splitResult.empty()); 161 | if (c != '"' && c != '\'' && c != '\\') 162 | splitResult.back() += "\\"; 163 | splitResult.back() += c; 164 | state = prev_state; 165 | } 166 | 167 | void NewSentence(char c) 168 | { 169 | state = State::sentence; 170 | sentence_type = ( c == '"' ? SentenceType::double_quote : SentenceType::quote); 171 | splitResult.emplace_back(""); 172 | } 173 | 174 | void RemoveEmptyEntries() 175 | { 176 | // remove null entries from the vector: 177 | splitResult.erase( 178 | std::remove_if( 179 | splitResult.begin(), 180 | splitResult.end(), 181 | [](const std::string& s){ return s.empty(); } 182 | ), 183 | splitResult.end() 184 | ); 185 | } 186 | 187 | enum class State { space, word, sentence, escape }; 188 | enum class SentenceType { quote, double_quote }; 189 | State state = State::space; 190 | State prev_state = State::space; 191 | SentenceType sentence_type = SentenceType::double_quote; 192 | const std::string input; 193 | std::vector splitResult; 194 | }; 195 | 196 | // Split the string input into a vector of strings. 197 | // The original string is split where there are spaces. 198 | // Quotes and double quotes can be used to indicate a substring that should not be splitted 199 | // (even if it contains spaces) 200 | 201 | // split(strs, ""); => empty vector 202 | // split(strs, " "); => empty vector 203 | // split(strs, " "); => empty vector 204 | // split(strs, "\t"); => empty vector 205 | // split(strs, " \t \t "); => empty vector 206 | 207 | // split(strs, "1234567890"); => <"1234567890"> 208 | // split(strs, " foo "); => <"foo"> 209 | // split(strs, " foo \t \t bar \t"); => <"foo","bar"> 210 | 211 | // split(strs, "\"\""); => empty vector 212 | // split(strs, "\"foo bar\""); => <"foo bar"> 213 | // split(strs, " \t\t \"foo \tbar\" \t"); => <"foo \tbar"> 214 | // split(strs, " first \t\t \"foo \tbar\" \t last"); => <"first","foo \tbar","last"> 215 | // split(strs, "first\"foo \tbar\""); => <"first","foo \tbar"> 216 | // split(strs, "first \"'second' 'thirdh'\""); => <"first","'second' 'thirdh'"> 217 | 218 | // split(strs, "''"); => empty vector 219 | // split(strs, "'foo bar'"); => <"foo bar"> 220 | // split(strs, " \t\t 'foo \tbar' \t"); => <"foo \tbar"> 221 | // split(strs, " first \t\t 'foo \tbar' \t last"); => <"first","foo \tbar","last"> 222 | // split(strs, "first'foo \tbar'"); => <"first","foo \tbar"> 223 | // split(strs, "first '\"second\" \"thirdh\"'"); => <"first","\"second\" \"thirdh\""> 224 | 225 | // split(strs, R"("foo\"bar")"); // "foo\"bar" => <"foo"bar"> 226 | // split(strs, R"('foo\'bar')"); // 'foo\'bar' => <"foo'bar"> 227 | // split(strs, R"("foo\bar")"); // "foo\bar" => <"foo\bar"> 228 | // split(strs, R"("foo\\"bar")"); // "foo\\"bar" => <"foo\"bar"> 229 | 230 | inline void split(std::vector& strs, const std::string& input) 231 | { 232 | Text sentence(input); 233 | sentence.SplitInto(strs); 234 | } 235 | 236 | } // namespace detail 237 | } // namespace cli 238 | 239 | #endif // CLI_DETAIL_SPLIT_H_ 240 | -------------------------------------------------------------------------------- /FMDynamic/include/cli/detail/fromstring.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * CLI - A simple command line interface. 3 | * Copyright (C) 2016-2021 Daniele Pallastrelli 4 | * 5 | * Boost Software License - Version 1.0 - August 17th, 2003 6 | * 7 | * Permission is hereby granted, free of charge, to any person or organization 8 | * obtaining a copy of the software and accompanying documentation covered by 9 | * this license (the "Software") to use, reproduce, display, distribute, 10 | * execute, and transmit the Software, and to prepare derivative works of the 11 | * Software, and to permit third-parties to whom the Software is furnished to 12 | * do so, all subject to the following: 13 | * 14 | * The copyright notices in the Software and this entire statement, including 15 | * the above license grant, this restriction and the following disclaimer, 16 | * must be included in all copies of the Software, in whole or in part, and 17 | * all derivative works of the Software, unless such copies or derivative 18 | * works are solely in the form of machine-executable object code generated by 19 | * a source language processor. 20 | * 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 24 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 25 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 27 | * DEALINGS IN THE SOFTWARE. 28 | ******************************************************************************/ 29 | 30 | #ifndef CLI_DETAIL_FROMSTRING_H_ 31 | #define CLI_DETAIL_FROMSTRING_H_ 32 | 33 | // #define CLI_FROMSTRING_USE_BOOST 34 | 35 | #ifdef CLI_FROMSTRING_USE_BOOST 36 | 37 | #include 38 | 39 | namespace cli 40 | { 41 | namespace detail 42 | { 43 | 44 | template 45 | inline 46 | T from_string(const std::string& s) 47 | { 48 | return boost::lexical_cast(s); 49 | } 50 | 51 | } // namespace detail 52 | } // namespace cli 53 | 54 | #else 55 | 56 | #include 57 | #include 58 | #include 59 | #include 60 | 61 | namespace cli 62 | { 63 | 64 | namespace detail 65 | { 66 | class bad_conversion : public std::bad_cast 67 | { 68 | public: 69 | const char* what() const noexcept override { 70 | return "bad from_string conversion: " 71 | "source string value could not be interpreted as target"; 72 | } 73 | }; 74 | 75 | template 76 | inline T from_string(const std::string& s); 77 | 78 | template <> 79 | inline std::string from_string(const std::string& s) 80 | { 81 | return s; 82 | } 83 | 84 | template <> 85 | inline std::nullptr_t from_string(const std::string& /*s*/) 86 | { 87 | return nullptr; 88 | } 89 | 90 | namespace detail 91 | { 92 | 93 | template 94 | inline T unsigned_digits_from_string(const std::string& s) 95 | { 96 | if (s.empty()) 97 | throw bad_conversion(); 98 | T result = 0; 99 | for (char c: s) 100 | { 101 | if (!std::isdigit(c)) 102 | throw bad_conversion(); 103 | const T digit = static_cast( c - '0' ); 104 | const T tmp = (result * 10) + digit; 105 | if (result != ((tmp-digit)/10) || (tmp < result)) 106 | throw bad_conversion(); 107 | result = tmp; 108 | } 109 | return result; 110 | } 111 | 112 | template 113 | inline T unsigned_from_string(std::string s) 114 | { 115 | if (s.empty()) 116 | throw bad_conversion(); 117 | if (s[0] == '+') 118 | { 119 | s = s.substr(1); 120 | } 121 | return unsigned_digits_from_string(s); 122 | } 123 | 124 | template 125 | inline T signed_from_string(std::string s) 126 | { 127 | if (s.empty()) 128 | throw bad_conversion(); 129 | using U = std::make_unsigned_t; 130 | if (s[0] == '-') 131 | { 132 | s = s.substr(1); 133 | const U val = unsigned_digits_from_string(s); 134 | if ( val > static_cast( - std::numeric_limits::min() ) ) 135 | throw bad_conversion(); 136 | return (- static_cast(val)); 137 | } 138 | else if (s[0] == '+') 139 | { 140 | s = s.substr(1); 141 | } 142 | const U val = unsigned_digits_from_string(s); 143 | if (val > static_cast( std::numeric_limits::max() )) 144 | throw bad_conversion(); 145 | return static_cast(val); 146 | } 147 | 148 | } // namespace detail 149 | 150 | // signed 151 | 152 | template <> inline signed char 153 | from_string(const std::string& s) { return detail::signed_from_string(s); } 154 | 155 | template <> inline short int 156 | from_string(const std::string& s) { return detail::signed_from_string(s); } 157 | 158 | template <> inline int 159 | from_string(const std::string& s) { return detail::signed_from_string(s); } 160 | 161 | template <> inline long int 162 | from_string(const std::string& s) { return detail::signed_from_string(s); } 163 | 164 | template <> inline long long int 165 | from_string(const std::string& s) { return detail::signed_from_string(s); } 166 | 167 | // unsigned 168 | 169 | template <> inline unsigned char 170 | from_string(const std::string& s) { return detail::unsigned_from_string(s); } 171 | 172 | template <> inline unsigned short int 173 | from_string(const std::string& s) { return detail::unsigned_from_string(s); } 174 | 175 | template <> inline unsigned int 176 | from_string(const std::string& s) { return detail::unsigned_from_string(s); } 177 | 178 | template <> inline unsigned long int 179 | from_string(const std::string& s) { return detail::unsigned_from_string(s); } 180 | 181 | template <> inline unsigned long long int 182 | from_string(const std::string& s) { return detail::unsigned_from_string(s); } 183 | 184 | // bool 185 | 186 | template <> 187 | inline bool from_string(const std::string& s) 188 | { 189 | if (s == "true") return true; 190 | else if (s == "false") return false; 191 | const auto value = detail::signed_from_string(s); 192 | if (value == 1) return true; 193 | else if (value == 0) return false; 194 | throw bad_conversion(); 195 | } 196 | 197 | // chars 198 | 199 | template <> 200 | inline char from_string(const std::string& s) 201 | { 202 | if (s.size() != 1) throw bad_conversion(); 203 | return s[0]; 204 | } 205 | 206 | // floating points 207 | 208 | template <> 209 | inline float from_string(const std::string& s) 210 | { 211 | if ( std::any_of(s.begin(), s.end(), [](char c){return std::isspace(c);} ) ) 212 | throw bad_conversion(); 213 | std::string::size_type sz; 214 | float result = {}; 215 | try { 216 | result = std::stof(s, &sz); 217 | } catch (const std::exception&) { 218 | throw bad_conversion(); 219 | } 220 | if (sz != s.size()) 221 | throw bad_conversion(); 222 | return result; 223 | } 224 | 225 | template <> 226 | inline double from_string(const std::string& s) 227 | { 228 | if ( std::any_of(s.begin(), s.end(), [](char c){return std::isspace(c);} ) ) 229 | throw bad_conversion(); 230 | std::string::size_type sz; 231 | double result = {}; 232 | try { 233 | result = std::stod(s, &sz); 234 | } catch (const std::exception&) { 235 | throw bad_conversion(); 236 | } 237 | if (sz != s.size()) 238 | throw bad_conversion(); 239 | return result; 240 | } 241 | 242 | template <> 243 | inline long double from_string(const std::string& s) 244 | { 245 | if ( std::any_of(s.begin(), s.end(), [](char c){return std::isspace(c);} ) ) 246 | throw bad_conversion(); 247 | std::string::size_type sz; 248 | long double result = {}; 249 | try { 250 | result = std::stold(s, &sz); 251 | } catch (const std::exception&) { 252 | throw bad_conversion(); 253 | } 254 | if (sz != s.size()) 255 | throw bad_conversion(); 256 | return result; 257 | } 258 | 259 | // fallback: operator << 260 | 261 | template 262 | inline T from_string(const std::string& s) 263 | { 264 | std::stringstream interpreter; 265 | T result; 266 | 267 | if(!(interpreter << s) || 268 | !(interpreter >> result) || 269 | !(interpreter >> std::ws).eof()) 270 | throw bad_conversion(); 271 | 272 | return result; 273 | } 274 | 275 | } // namespace detail 276 | 277 | } // namespace cli 278 | 279 | 280 | #endif // CLI_FROMSTRING_USE_BOOST 281 | 282 | #endif // CLI_DETAIL_FROMSTRING_H_ 283 | -------------------------------------------------------------------------------- /libFlowMatrix/src/VState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace 4 | { 5 | 6 | const std::string gpr_strs[] = 7 | { 8 | "RAX", 9 | "RBX", 10 | "RCX", 11 | "RDX", 12 | "RDI", 13 | "RSI", 14 | "RSP", 15 | "RBP", 16 | "R8", 17 | "R9", 18 | "R10", 19 | "R11", 20 | "R12", 21 | "R13", 22 | "R14", 23 | "R15", 24 | "RFLAGS", 25 | "RIP", 26 | }; 27 | 28 | const std::string fp_strs[] = 29 | { 30 | "FP0", 31 | "FP1", 32 | "FP2", 33 | "FP3", 34 | "FP4", 35 | "FP5", 36 | "FP6", 37 | "FP7", 38 | }; 39 | 40 | const std::string st_strs[] = 41 | { 42 | // KH: Not implemented 43 | }; 44 | 45 | const std::string dr_strs[] = 46 | { 47 | // KH: Not implemented 48 | }; 49 | 50 | const std::string cr_strs[] = 51 | { 52 | // KH: Not implemented 53 | }; 54 | 55 | const std::string kr_strs[] = 56 | { 57 | // KH: Not implemented 58 | }; 59 | 60 | const std::string xmm_strs[] = 61 | { 62 | "XMM0", 63 | "XMM1", 64 | "XMM2", 65 | "XMM3", 66 | "XMM4", 67 | "XMM5", 68 | "XMM6", 69 | "XMM7", 70 | "XMM8", 71 | "XMM9", 72 | "XMM10", 73 | "XMM11", 74 | "XMM12", 75 | "XMM13", 76 | "XMM14", 77 | "XMM15", 78 | }; 79 | 80 | const std::string ymm_strs[] = 81 | { 82 | "YMM0", 83 | "YMM1", 84 | "YMM2", 85 | "YMM3", 86 | "YMM4", 87 | "YMM5", 88 | "YMM6", 89 | "YMM7", 90 | "YMM8", 91 | "YMM9", 92 | "YMM10", 93 | "YMM11", 94 | "YMM12", 95 | "YMM13", 96 | "YMM14", 97 | "YMM15", 98 | "YMM16", 99 | "YMM17", 100 | "YMM18", 101 | "YMM19", 102 | "YMM20", 103 | "YMM21", 104 | "YMM22", 105 | "YMM23", 106 | "YMM24", 107 | "YMM25", 108 | "YMM26", 109 | "YMM27", 110 | "YMM28", 111 | "YMM29", 112 | "YMM30", 113 | "YMM31", 114 | }; 115 | 116 | const std::string zmm_strs[] = 117 | { 118 | "ZMM0", 119 | "ZMM1", 120 | "ZMM2", 121 | "ZMM3", 122 | "ZMM4", 123 | "ZMM5", 124 | "ZMM6", 125 | "ZMM7", 126 | "ZMM8", 127 | "ZMM9", 128 | "ZMM10", 129 | "ZMM11", 130 | "ZMM12", 131 | "ZMM13", 132 | "ZMM14", 133 | "ZMM15", 134 | "ZMM16", 135 | "ZMM17", 136 | "ZMM18", 137 | "ZMM19", 138 | "ZMM20", 139 | "ZMM21", 140 | "ZMM22", 141 | "ZMM23", 142 | "ZMM24", 143 | "ZMM25", 144 | "ZMM26", 145 | "ZMM27", 146 | "ZMM28", 147 | "ZMM29", 148 | "ZMM30", 149 | "ZMM31", 150 | }; 151 | 152 | } // anomynos namespace 153 | 154 | namespace FlowMatrix 155 | { 156 | 157 | VState::VState(const enum FMGranularity &granu) 158 | { 159 | this->granu = granu; 160 | this->size = 0; 161 | } 162 | 163 | void VState::init_reg_state(const FMRegListConfig ®_config) 164 | { 165 | if (reg_config.gpr == 1) 166 | { 167 | for (std::string reg_name:gpr_strs) 168 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 169 | } 170 | 171 | if (reg_config.fp == 1) 172 | { 173 | for (std::string reg_name:fp_strs) 174 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 175 | } 176 | 177 | // Not implemented yet 178 | if (reg_config.st == 1) 179 | { 180 | for (std::string reg_name:st_strs) 181 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 182 | } 183 | 184 | // Not implemented yet 185 | if (reg_config.dr == 1) 186 | { 187 | for (std::string reg_name:dr_strs) 188 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 189 | } 190 | 191 | // Not implemented yet 192 | if (reg_config.cr == 1) 193 | { 194 | for (std::string reg_name:cr_strs) 195 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 196 | } 197 | 198 | // Not implemented yet 199 | if (reg_config.kr == 1) 200 | { 201 | for (std::string reg_name:kr_strs) 202 | INSERT_TO_VSTATE_BY_REG_NAME(this, reg_name); 203 | } 204 | 205 | // Not fully implemented yet 206 | if (reg_config.zmm == 1) 207 | { 208 | for (std::string reg_name:zmm_strs) 209 | { 210 | unsigned int reg_id; 211 | assert(getRegId("AMD64", reg_name, reg_id) == 0); 212 | this->append(reg_id, 512); 213 | } 214 | } 215 | else 216 | { 217 | // ~zmm 218 | if (reg_config.ymm == 1) 219 | { 220 | for (std::string reg_name:ymm_strs) 221 | { 222 | unsigned int reg_id; 223 | assert(getRegId("AMD64", reg_name, reg_id) == 0); 224 | this->append(reg_id, 256); 225 | } 226 | 227 | } 228 | else 229 | { 230 | // ~zmm && ~ymm 231 | if (reg_config.xmm == 1) 232 | { 233 | for (std::string reg_name:zmm_strs) 234 | { 235 | unsigned int reg_id; 236 | assert(getRegId("AMD64", reg_name, reg_id) == 0); 237 | this->append(reg_id, 128); 238 | } 239 | } 240 | } 241 | } 242 | 243 | } 244 | 245 | void VState::append(uint64_t id, uint64_t size_in_bit) 246 | { 247 | this->id2offset_map[id] = this->size; 248 | this->offset2id_map[size] = id; 249 | this->size += std::max((uint64_t)1, GetSizeWithinGranu(size_in_bit,this->granu)); 250 | } 251 | 252 | std::string VState::GetVStateNameByID(uint64_t id) 253 | { 254 | std::string name = ""; 255 | if (getRegName("AMD64", id, name) != -1) 256 | { 257 | // This is a regesiter. 258 | return name; 259 | } 260 | else 261 | { 262 | // This is a memory or syscall 263 | std::stringstream ss; 264 | if (id >= 0x1000 && id <= 0x4000) 265 | { 266 | ss << "Syscall_" << id-0x1000; 267 | } 268 | else 269 | { 270 | ss << "0x" << std::hex << id; 271 | } 272 | ss >> name; 273 | return name; 274 | } 275 | 276 | // Should not reach here. 277 | LOG("[E] Should never reach here."); 278 | DIE(); 279 | 280 | // Make Compiler happy 281 | return name; 282 | } 283 | 284 | void VState::pp() 285 | { 286 | // Print Granularity 287 | std::cout << "Granularity: "; 288 | switch(this->granu) 289 | { 290 | case FMGranularity::bit: 291 | std::cout << "Bit-level\n"; 292 | break; 293 | case FMGranularity::byte: 294 | std::cout << "Byte-level\n"; 295 | break; 296 | case FMGranularity::qword: 297 | std::cout << "Qword-level\n"; 298 | break; 299 | default: 300 | // Unknown granularity 301 | std::cout << "Unknown\n"; 302 | } 303 | 304 | // Print Size 305 | std::cout << "Size: " << this->size << std::endl; 306 | 307 | // Prepare for printing continuous memory range 308 | uint64_t last_memory = -1; 309 | uint64_t starting_offset = 0; 310 | uint64_t starting_addr = -1; 311 | uint64_t ending_offset = 0; 312 | 313 | // Print Map 314 | std::cout << "Mapping:" << std::endl; 315 | for (auto it = this->offset2id_map.begin(); it != this->offset2id_map.end(); it++) 316 | { 317 | const uint64_t offset = it->first; 318 | const uint64_t id = it->second; 319 | auto next_it = std::next(it); 320 | uint64_t var_end; 321 | if (next_it == this->offset2id_map.end()) 322 | var_end = this->size; 323 | else 324 | var_end = next_it->first - 1; 325 | 326 | // Also performs an internal cross-check 327 | assert(this->id2offset_map[id] == offset); 328 | 329 | std::string reg_name; 330 | if (getRegName("AMD64", id, reg_name) != -1) 331 | { 332 | // This is a register 333 | std::cout << " [" << offset << "-" << var_end << "]\t"; 334 | std::cout << reg_name << std::endl; 335 | } 336 | else 337 | { 338 | // This is a mem. 339 | if (last_memory + 1 == id) 340 | { 341 | // Continuous memory range 342 | ending_offset = var_end; 343 | last_memory = id; 344 | continue; 345 | } 346 | else 347 | { 348 | if (last_memory != (uint64_t)-1) 349 | { 350 | std::cout << " [" << starting_offset << "-" << ending_offset << "]\t"; 351 | std::cout << "0x" << std::hex << starting_addr << " - 0x" <granu)*3) / 8 353 | << " bytes)"; 354 | std::cout << std::endl; 355 | } 356 | starting_addr = id; 357 | last_memory = id; 358 | starting_offset = offset; 359 | ending_offset = var_end; 360 | } 361 | 362 | } 363 | } 364 | } 365 | 366 | // bool VState::store(const std::string &dir, const std::string &prefix) 367 | // { 368 | // if 369 | // std::string filename = dir + "/" 370 | // fopen(filename.c_str(), "w"); 371 | // } 372 | 373 | 374 | 375 | } // namespace FlowMatrix --------------------------------------------------------------------------------