From 75729e8446cd6dc720c49e7897230d54fa11e2b2 Mon Sep 17 00:00:00 2001 From: flobernd Date: Fri, 15 Sep 2017 00:42:05 +0200 Subject: [PATCH] Fixed compilation of the performance-test tool on linux systems --- CMakeLists.txt | 51 +++++++++++++++++++++------------------- examples/ZydisPerfTest.c | 39 +++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f2529fc..da074a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,11 +73,11 @@ target_compile_definitions("Zydis" PRIVATE "_CRT_SECURE_NO_WARNINGS" "ZYDIS_EXPO generate_export_header("Zydis" BASE_NAME "ZYDIS" EXPORT_FILE_NAME "ZydisExportConfig.h") if (NOT ZYDIS_FEATURE_ENCODER AND NOT ZYDIS_FEATURE_DECODER) - message( - FATAL_ERROR - "\nIt's dangerous to go alone! Take at least one of these:\n" - "[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER" - ) + message( + FATAL_ERROR + "\nIt's dangerous to go alone! Take at least one of these:\n" + "[ ] ZYDIS_FEATURE_ENCODER [ ] ZYDIS_FEATURE_DECODER" + ) endif () if (ZYDIS_FEATURE_EVEX) @@ -160,26 +160,29 @@ install(DIRECTORY "include" DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) # =============================================================================================== # if (ZYDIS_BUILD_EXAMPLES) - if (ZYDIS_FEATURE_DECODER) - add_executable("FormatterHooks" - "examples/FormatterHooks.c" - "examples/FormatHelper.h") - target_link_libraries("FormatterHooks" "Zydis") - set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter") - target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS") + if (ZYDIS_FEATURE_DECODER) + add_executable("FormatterHooks" + "examples/FormatterHooks.c" + "examples/FormatHelper.h") + target_link_libraries("FormatterHooks" "Zydis") + set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples/Formatter") + target_compile_definitions("FormatterHooks" PRIVATE "_CRT_SECURE_NO_WARNINGS") add_executable("ZydisFuzzIn" "examples/ZydisFuzzIn.c") target_link_libraries("ZydisFuzzIn" "Zydis") set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples") target_compile_definitions("ZydisFuzzIn" PRIVATE "_CRT_SECURE_NO_WARNINGS") - if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" OR ${CMAKE_SYSTEM_NAME} STREQUAL "Windows") - add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c") - target_link_libraries("ZydisPerfTest" "Zydis") - set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples") - target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS") + add_executable("ZydisPerfTest" "examples/ZydisPerfTest.c") + target_link_libraries("ZydisPerfTest" "Zydis") + set_target_properties("FormatterHooks" PROPERTIES FOLDER "Examples") + target_compile_definitions("ZydisPerfTest" PRIVATE "_CRT_SECURE_NO_WARNINGS") + if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + target_compile_definitions("ZydisPerfTest" PRIVATE "_GNU_SOURCE") + find_package(Threads REQUIRED) + target_link_libraries("ZydisPerfTest" Threads::Threads) endif () - endif () + endif () endif () # =============================================================================================== # @@ -187,15 +190,15 @@ endif () # =============================================================================================== # if (ZYDIS_BUILD_TOOLS) - if (ZYDIS_FEATURE_DECODER) - add_executable("ZydisDisasm" "tools/ZydisDisasm.c") - target_link_libraries("ZydisDisasm" "Zydis") - set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools") - target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS") + if (ZYDIS_FEATURE_DECODER) + add_executable("ZydisDisasm" "tools/ZydisDisasm.c") + target_link_libraries("ZydisDisasm" "Zydis") + set_target_properties ("ZydisDisasm" PROPERTIES FOLDER "Tools") + target_compile_definitions("ZydisDisasm" PRIVATE "_CRT_SECURE_NO_WARNINGS") add_executable("ZydisInfo" "tools/ZydisInfo.c") target_link_libraries("ZydisInfo" "Zydis") set_target_properties ("ZydisInfo" PROPERTIES FOLDER "Tools") target_compile_definitions("ZydisInfo" PRIVATE "_CRT_SECURE_NO_WARNINGS") - endif () + endif () endif () diff --git a/examples/ZydisPerfTest.c b/examples/ZydisPerfTest.c index f84df7a..2a1452e 100644 --- a/examples/ZydisPerfTest.c +++ b/examples/ZydisPerfTest.c @@ -39,7 +39,7 @@ # include #elif defined(ZYDIS_LINUX) # include -# inlcude +# include #else # error "Unsupported platform detected" #endif @@ -61,7 +61,7 @@ void StartCounter() LARGE_INTEGER li; if (!QueryPerformanceFrequency(&li)) { - fputs("QueryPerformanceFrequency failed!\n", stderr); + fputs("Error: QueryPerformanceFrequency failed!\n", stderr); } CounterFreq = (double)li.QuadPart / 1000.0; QueryPerformanceCounter(&li); @@ -95,7 +95,21 @@ double GetCounter() return (double)elapsed * timebaseInfo.numer / timebaseInfo.denom / 1000000; } #elif defined(ZYDIS_LINUX) -// TODO: +struct timeval t1; + +void StartCounter() +{ + gettimeofday(&t1, NULL); +} + +double GetCounter() +{ + struct timeval t2; + gettimeofday(&t2, NULL); + + double t = (t2.tv_sec - t1.tv_sec) * 1000.0; + return t + (t2.tv_usec - t1.tv_usec) / 1000.0; +} #endif /* ---------------------------------------------------------------------------------------------- */ @@ -127,7 +141,7 @@ void adjustProcessAndThreadPriority() pthread_t thread = pthread_self(); cpu_set_t cpus; CPU_ZERO(&cpus); - CPU_SET(cpu, &cpus); + CPU_SET(0, &cpus); pthread_setaffinity_np(thread, sizeof(cpus), &cpus); #endif } @@ -364,16 +378,29 @@ int main(int argc, char** argv) fseek(file, 0L, SEEK_END); long length = ftell(file); void* buffer = malloc(length); + if (!buffer) + { + fprintf(stderr, "Failed to allocate %" PRIu64 " on the heap", (uint64_t)length); + goto NextFile2; + } + rewind(file); - fread(buffer, 1, length, file); + if (fread(buffer, 1, length, file) != length) + { + fprintf(stderr, + "Could not read %" PRIu64 " bytes from file \"%s\"", (uint64_t)length, &buf[0]); + goto NextFile1; + } printf("Testing %s ...\n", tests[i].encoding); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_MINIMAL, ZYDIS_FALSE); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_FALSE); testPerformance(buffer, length, ZYDIS_DECODE_GRANULARITY_FULL , ZYDIS_TRUE ); - puts(""); + +NextFile1: free(buffer); +NextFile2: fclose(file); } }