diff --git a/.gitattributes b/.gitattributes index 2770518a5419ee17843d29bdb96361131c6c3ee0..7e714e68d58f28a77c163505e9bcec9a5631edfb 100644 --- a/.gitattributes +++ b/.gitattributes @@ -29,6 +29,7 @@ src/Makefile -text src/main.cpp -text src/zipr.cpp -text src/zipr_options.cpp -text +src/zipr_stats.cpp -text third_party/ELFIO/elfio-2.2/AUTHORS -text third_party/ELFIO/elfio-2.2/COPYING -text third_party/ELFIO/elfio-2.2/ChangeLog -text diff --git a/include/zipr.h b/include/zipr.h index 108ec2ed190ee11223477e6bb570652c5926febe..7cdb1c1cb260c39f2f5b8c303b82c6a1e31b7f89 100644 --- a/include/zipr.h +++ b/include/zipr.h @@ -42,16 +42,7 @@ class Zipr_t Zipr_t(libIRDB::FileIR_t* p_firp, Options_t &p_opts) : m_firp(p_firp), m_opts(p_opts) { - total_dollops=0; - total_dollop_space=0; - total_dollop_instructions=0; - total_trampolines=0; - total_2byte_pins=0; - total_5byte_pins=0; - total_tramp_space=0; - total_other_space=0; - truncated_dollops=0; - }; + }; void CreateBinaryFile(const std::string &name); @@ -149,17 +140,6 @@ class Zipr_t // way to read elf headers, etc. ELFIO::elfio* elfiop; - // stats - int total_dollops; - int total_dollop_space; - int total_dollop_instructions; - int total_trampolines; - int total_2byte_pins; - int total_5byte_pins; - int total_tramp_space; - int total_other_space; - int truncated_dollops; - // records where we will insert extra bytes into the program. RangeAddress_t start_of_new_space; diff --git a/include/zipr_stats.h b/include/zipr_stats.h index 938863de77042b26edea909c4c05e1efdf7e44b6..2dfb2774008a68aee12bbe6ac666f003605491e1 100644 --- a/include/zipr_stats.h +++ b/include/zipr_stats.h @@ -9,7 +9,36 @@ class Stats_t { Hits[i] = Misses[i] = 0; } + total_dollops = 0; + total_dollop_space = 0; + total_dollop_instructions = 0; + truncated_dollops = 0; + total_trampolines = 0; + total_2byte_pins = 0; + total_5byte_pins = 0; + total_tramp_space = 0; + total_other_space = 0; }; + + void PrintStats(Options_t opts, std::ostream &out); + + /* + * General stats tracking. + */ + + int total_dollops; + int total_dollop_space; + int total_dollop_instructions; + int truncated_dollops; + int total_trampolines; + int total_2byte_pins; + int total_5byte_pins; + int total_tramp_space; + int total_other_space; + + /* + * Optimization stats tracking. + */ int Hits[Optimizations_t::NumberOfOptimizations]; int Misses[Optimizations_t::NumberOfOptimizations]; void Hit(Optimizations_t::OptimizationName_t opt) { Hits[opt]++; }; diff --git a/src/Makefile b/src/Makefile index 8d82759084433c605858397ad9f8fdcaef742005..e6283f15943b9ce5ed72d7c01e5a77e8c6e9c442 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,7 +3,7 @@ INC=-I../include -I../third_party/ELFIO/elfio-2.2 -I$(SECURITY_TRANSFORMS_HOME)/ -SRCS=zipr.cpp zipr_options.cpp main.cpp +SRCS=zipr.cpp zipr_options.cpp zipr_stats.cpp main.cpp OBJS=$(subst .cpp,.o, $(SRCS)) HDRS=../include/*.h EXE=zipr.exe diff --git a/src/zipr.cpp b/src/zipr.cpp index b003e3e46d45ff8f1a878a130bd4f7e049f713d0..cae6b167acd6a3b857aa6d52b7088759d52ad22b 100644 --- a/src/zipr.cpp +++ b/src/zipr.cpp @@ -589,7 +589,7 @@ void Zipr_t::ReservePinnedInstructions() { byte_map[addr+i]=upinsn->GetDataBits()[i]; SplitFreeRange(addr+i); - total_other_space++; + m_stats->total_other_space++; } continue; } @@ -647,17 +647,17 @@ void Zipr_t::ExpandPinnedInstructions() five_byte_pins[up]=addr; two_byte_pins.erase(it++); - total_5byte_pins++; - total_trampolines++; + m_stats->total_5byte_pins++; + m_stats->total_trampolines++; } else { ++it; if(m_opts.GetVerbose()) printf("Found %p can NOT be updated to 5-byte jmp\n", (void*)addr); - total_2byte_pins++; - total_trampolines++; - total_tramp_space+=2; + m_stats->total_2byte_pins++; + m_stats->total_trampolines++; + m_stats->total_tramp_space+=2; } } @@ -812,7 +812,7 @@ void Zipr_t::OptimizePinnedInstructions() if(m_opts.GetVerbose()) printf("Converting 5-byte pinned jump at %p-%p to patch to %d:%s\n", (void*)addr,(void*)(addr+4), uu.GetInstruction()->GetBaseID(), d.CompleteInstr); - total_tramp_space+=5; + m_stats->total_tramp_space+=5; } // remove and move to next pin @@ -1029,14 +1029,14 @@ void Zipr_t::ProcessUnpinnedInstruction(const UnresolvedUnpinned_t &uu, const Pa patch_list.insert(pair<UnresolvedUnpinned_t,Patch_t>(uu,thepatch)); PlopJump(cur_addr); truncated="truncated due to lack of space."; - total_tramp_space+=5; - truncated_dollops++; - total_trampolines++; + m_stats->total_tramp_space+=5; + m_stats->truncated_dollops++; + m_stats->total_trampolines++; } - total_dollops++; - total_dollop_instructions+=insn_count; - total_dollop_space+=(cur_addr-fr_start); + m_stats->total_dollops++; + m_stats->total_dollop_instructions+=insn_count; + m_stats->total_dollop_space+=(cur_addr-fr_start); if(m_opts.GetVerbose()) printf("Ending dollop. size=%d, %s. space_remaining=%lld, req'd=%d\n", insn_count, truncated, @@ -1408,20 +1408,7 @@ void Zipr_t::OutputBinaryFile(const string &name) void Zipr_t::PrintStats() { - cout<<"Total dollops: "<<std::dec << total_dollops <<endl; - cout<<"Total dollop size: "<<std::dec << total_dollop_space <<endl; - cout<<"Total dollop instructions: "<<std::dec << total_dollop_instructions <<endl; - cout<<"Truncated dollops: "<<std::dec << truncated_dollops <<endl; - cout<<"Ave dollop size: "<<std::dec << (double)total_dollop_space/(double)total_dollops <<endl; - cout<<"Ave dollop instructions: "<<std::dec << (double)total_dollop_instructions/(double)total_dollops <<endl; - cout<<"Truncated dollop fraction: "<<std::dec << (double)truncated_dollops/(double)total_dollops <<endl; - - cout<<"Total trampolines: "<<std::dec << total_trampolines <<endl; - cout<<"Total 2-byte pin trampolines: "<<std::dec << total_2byte_pins <<endl; - cout<<"Total 5-byte pin trampolines: "<<std::dec << total_5byte_pins <<endl; - cout<<"Total trampoline space pins: "<<std::dec << total_tramp_space <<endl; - - cout<<"Other space: "<<total_other_space<<endl; + m_stats->PrintStats(m_opts, cout); } template < typename T > std::string to_hex_string( const T& n ) diff --git a/src/zipr_stats.cpp b/src/zipr_stats.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3254ff516f1f3d7bd43e24dfe207372b0222a69f --- /dev/null +++ b/src/zipr_stats.cpp @@ -0,0 +1,48 @@ +#include <zipr_all.h> +#include <unistd.h> +#include <stdlib.h> +#include <getopt.h> +#include <iostream> + +using namespace zipr; + +static void PrintStat(std::ostream &out, std::string description, int value) +{ + out << description << ": " << std::dec << value << std::endl; +} +static void PrintStat(std::ostream &out, std::string description, double value) +{ + out << description << ": " << std::dec << value << std::endl; +} + +void Stats_t::PrintStats(Options_t opts, std::ostream &out) +{ + PrintStat(out, "Total dollops", total_dollops); + PrintStat(out, "Total dollop size", total_dollop_space); + PrintStat(out, "Total dollop instructions",total_dollop_instructions); + PrintStat(out, "Truncated dollops",truncated_dollops); + PrintStat(out, "Ave dollop size", + (double)total_dollop_space/(double)total_dollops); + PrintStat(out, "Ave dollop instructions", + (double)total_dollop_instructions/(double)total_dollops); + PrintStat(out, "Truncated dollop fraction", + (double)truncated_dollops/(double)total_dollops); + + /* + * Optimizations + */ + if(opts.IsEnabledOptimization(Optimizations_t::OptimizationFallthroughPinned)) + { + PrintStat(out, "Optimization: FallthroughPinned hit rate", + (double)Hits[Optimizations_t::OptimizationFallthroughPinned]/ + (Hits[Optimizations_t::OptimizationFallthroughPinned] + + Misses[Optimizations_t::OptimizationFallthroughPinned])); + } + + PrintStat(out, "Total trampolines", total_trampolines); + PrintStat(out, "Total 2-byte pin trampolines", total_2byte_pins); + PrintStat(out, "Total 5-byte pin trampolines", total_5byte_pins); + PrintStat(out, "Total trampoline space pins", total_tramp_space); + PrintStat(out, "Other space", total_other_space); + PrintStat(out, "Total free ranges", total_free_ranges); +}