From 611c274b2ee9a564ff1716490df756e3a2b04423 Mon Sep 17 00:00:00 2001 From: wallds Date: Mon, 10 Jan 2022 18:16:56 +0800 Subject: [PATCH 1/7] Fix test_access --- VTIL-Architecture/routine/call_convention.hpp | 2 +- VTIL-Architecture/symex/variable.cpp | 30 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/VTIL-Architecture/routine/call_convention.hpp b/VTIL-Architecture/routine/call_convention.hpp index a9c5b18..ae30be0 100644 --- a/VTIL-Architecture/routine/call_convention.hpp +++ b/VTIL-Architecture/routine/call_convention.hpp @@ -202,7 +202,7 @@ namespace vtil { register_physical, X86_REG_EBP, 32 }, /*.shadow_space =*/ - 0x20, + 0x0, /*.purge_stack =*/ true, diff --git a/VTIL-Architecture/symex/variable.cpp b/VTIL-Architecture/symex/variable.cpp index 593fd00..7e8a357 100644 --- a/VTIL-Architecture/symex/variable.cpp +++ b/VTIL-Architecture/symex/variable.cpp @@ -380,23 +380,23 @@ namespace vtil::symbolic // If vmexit, declared trashed if below or at the shadow space: // - if ( cwrite ) + if ( cwrite && cc.purge_stack) { - if ( it->base == &ins::vexit ? it.block->owner->routine_convention.purge_stack : cc.purge_stack ) - { - // Determine the limit of the stack memory owned by this routine. - // - expression limit = - tracer->trace( { it, REG_SP } ) + - it.block->sp_offset + - cc.shadow_space; + // Determine the limit of the stack memory owned by this routine. + // + expression limit = + tracer->trace( { it, REG_SP } ) + + it.block->sp_offset + + ( it->base == &ins::vexit ? 0 : cc.shadow_space ); - // Calculate the displacement, if constant below 0, declare trashed. - // - access_details details; - fill_displacement( &details, mem.base, pointer{ std::move( limit ) }, tracer, xblock ); - if ( !details.is_unknown() && ( details.bit_offset + var.bit_count() ) <= 0 ) - result += { .bit_offset = 0, .bit_count = var.bit_count(), .read = false, .write = true }; + // Calculate the displacement, if constant below 0, declare trashed. + // + access_details details; + fill_displacement( &details, mem.base, pointer{ std::move( limit ) }, tracer, xblock ); + if ( !details.is_unknown() && ( details.bit_offset + var.bit_count() ) <= 0 ) + { + result += { .bit_offset = 0, .bit_count = var.bit_count(), .read = false, .write = true }; + return result; } } From afa927f1bbc800f41728adac8e0278e111d34dfd Mon Sep 17 00:00:00 2001 From: windy <19060@qq.com> Date: Sun, 5 Dec 2021 00:06:58 +0800 Subject: [PATCH 2/7] Fix typo --- VTIL-Tests/dummy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VTIL-Tests/dummy.cpp b/VTIL-Tests/dummy.cpp index d45ede5..47898f6 100644 --- a/VTIL-Tests/dummy.cpp +++ b/VTIL-Tests/dummy.cpp @@ -384,7 +384,7 @@ DOCTEST_TEST_CASE("Optimization dead_code_elimination_pass") auto block3 = block1->fork( 0x3000 ); { - // mov ecx, [esp - 8] + // mov eax, [esp - 8] block3->ldd( reg_eax, vtil::REG_SP, -8 ); // sp -= 0x10 block3->shift_sp( 0x10 ); From 6f56cc3b00fc94862a745d2b7d93b54e487becbe Mon Sep 17 00:00:00 2001 From: windy <19060@qq.com> Date: Thu, 9 Dec 2021 01:19:30 +0800 Subject: [PATCH 3/7] The broken test in hash --- VTIL-SymEx/directives/transformer.hpp | 2 +- VTIL-SymEx/simplifier/simplifier.cpp | 2 +- VTIL-Tests/dummy.cpp | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/VTIL-SymEx/directives/transformer.hpp b/VTIL-SymEx/directives/transformer.hpp index 98b0b64..3083853 100644 --- a/VTIL-SymEx/directives/transformer.hpp +++ b/VTIL-SymEx/directives/transformer.hpp @@ -69,7 +69,7 @@ namespace vtil::symbolic // Log the translation. // log( "Translating [%s] => [%s]:\n", *from, *to ); - from->enum_variables( [ & ] ( const instance& ins ) + from->enum_variables( [ & ] ( const directive::instance& ins ) { log( " %s: %s\n", ins.id, *match.translate( ins ) ); } ); diff --git a/VTIL-SymEx/simplifier/simplifier.cpp b/VTIL-SymEx/simplifier/simplifier.cpp index c45c3f3..7879555 100644 --- a/VTIL-SymEx/simplifier/simplifier.cpp +++ b/VTIL-SymEx/simplifier/simplifier.cpp @@ -639,7 +639,7 @@ namespace vtil::symbolic // Log the input. // scope_padding _p( 1 ); - if ( !state::get()->padding ) log( "\n" ); + log( "\n" ); log( "[Input] = %s ", *exp ); log( "(Hash: %s)\n", exp->hash() ); #endif diff --git a/VTIL-Tests/dummy.cpp b/VTIL-Tests/dummy.cpp index 47898f6..afe787b 100644 --- a/VTIL-Tests/dummy.cpp +++ b/VTIL-Tests/dummy.cpp @@ -28,6 +28,29 @@ DOCTEST_TEST_CASE("dummy") CHECK(1 == 1); } +DOCTEST_TEST_CASE("Expression hash") +{ + vtil::logger::log("\n\n>> %s \n", __FUNCTION__); + auto const_a = vtil::symbolic::expression{ 123 }; + auto const_b = (vtil::symbolic::expression{ 123 } + 1 - 1).simplify( true ); + CHECK( const_a.hash() == const_b.hash() ); + + auto block = vtil::basic_block::begin( 0x1234 ); + block->push( 0 ); + auto variable_a = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }.to_expression(); + auto variable_b = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }.to_expression(); + // vtil::logger::log( "variable_a: %s \n", variable_a.to_string().c_str() ); + CHECK( variable_a.hash() == variable_b.hash() ); + + auto exp_a = __bt( variable_a, (uint32_t)0x6 ).simplify(true); + auto exp_b = __bt( variable_b, (uint8_t)0x6 ).simplify(true); + vtil::logger::log( "exp_a: %s \n", exp_a.to_string().c_str() ); + vtil::logger::log( "exp_b: %s \n", exp_b.to_string().c_str() ); + CHECK( exp_a.hash() == exp_b.hash() ); + + CHECK( 1 == 1 ); +} + DOCTEST_TEST_CASE("Optimization vtil file") { vtil::logger::log("\n\n>> %s \n", __FUNCTION__); From 06c268424b6fb07ec68126e7a9c4a97141cff5d4 Mon Sep 17 00:00:00 2001 From: windy <19060@qq.com> Date: Sun, 12 Dec 2021 17:20:31 +0800 Subject: [PATCH 4/7] Fix https://github.com/vtil-project/VTIL-Core/issues/61 --- VTIL-SymEx/expressions/expression.cpp | 13 ++++++ VTIL-Tests/dummy.cpp | 60 +++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/VTIL-SymEx/expressions/expression.cpp b/VTIL-SymEx/expressions/expression.cpp index e376fdf..296bbd9 100644 --- a/VTIL-SymEx/expressions/expression.cpp +++ b/VTIL-SymEx/expressions/expression.cpp @@ -546,6 +546,19 @@ namespace vtil::symbolic rhs.resize( value.size(), false ); break; } + case math::operator_id::shift_left: + case math::operator_id::shift_right: + { + rhs.resize( sizeof(uint8_t), false ); + break; + } + + case math::operator_id::rotate_left: + case math::operator_id::rotate_right: + { + rhs.resize( sizeof(uintptr_t), false ); + break; + } case math::operator_id::multiply_high: case math::operator_id::multiply: case math::operator_id::divide: diff --git a/VTIL-Tests/dummy.cpp b/VTIL-Tests/dummy.cpp index afe787b..4376d78 100644 --- a/VTIL-Tests/dummy.cpp +++ b/VTIL-Tests/dummy.cpp @@ -37,16 +37,62 @@ DOCTEST_TEST_CASE("Expression hash") auto block = vtil::basic_block::begin( 0x1234 ); block->push( 0 ); - auto variable_a = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }.to_expression(); - auto variable_b = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }.to_expression(); + auto variable_a = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }; + auto variable_b = vtil::symbolic::variable{ block->begin(), vtil::REG_FLAGS }; // vtil::logger::log( "variable_a: %s \n", variable_a.to_string().c_str() ); CHECK( variable_a.hash() == variable_b.hash() ); - auto exp_a = __bt( variable_a, (uint32_t)0x6 ).simplify(true); - auto exp_b = __bt( variable_b, (uint8_t)0x6 ).simplify(true); - vtil::logger::log( "exp_a: %s \n", exp_a.to_string().c_str() ); - vtil::logger::log( "exp_b: %s \n", exp_b.to_string().c_str() ); - CHECK( exp_a.hash() == exp_b.hash() ); + + // simple shift_right + { + auto exp_a = vtil::symbolic::expression{ (uint32_t)123 } >> (uint8_t)6; + auto exp_b = vtil::symbolic::expression{ (uint32_t)123 } >> (uint32_t)6; + + exp_a = exp_a.simplify( true ); + exp_b = exp_b.simplify( true ); + + vtil::logger::log("exp_a: %s \n", exp_a.to_string().c_str()); + vtil::logger::log("exp_b: %s \n", exp_b.to_string().c_str()); + CHECK(exp_a.hash() == exp_b.hash()); + } + + // Simple const shift_right + { + auto exp_a = variable_a.to_expression() >> (uint8_t)6; + auto exp_b = variable_a.to_expression() >> (uint32_t)6; + + exp_a = exp_a.simplify(true); + exp_b = exp_b.simplify(true); + + vtil::logger::log("exp_a: %s \n", exp_a.to_string().c_str()); + vtil::logger::log("exp_b: %s \n", exp_b.to_string().c_str()); + CHECK(exp_a.hash() == exp_b.hash()); + } + + // advanced shift_right + { + // eax@6:1 + vtil::register_desc temp_6(vtil::register_local, 1, 1, 6); + auto exp_a = vtil::symbolic::variable{ block->begin(), temp_6 }.to_expression(); + exp_a += (uint32_t)1; + exp_a = exp_a.simplify( true ); + vtil::logger::log( "exp_a.size: %d \n", exp_a.value.size() ); + vtil::logger::log( "exp_a: %s \n", exp_a.to_string().c_str() ); + + + // eax >> 6 && 1 + vtil::register_desc temp(vtil::register_local, 1, vtil::arch::bit_count, 0); + auto exp_b = vtil::symbolic::variable{ block->begin(), temp }.to_expression(); + exp_b >>= (uint8_t)6; + exp_b &= (uint64_t)1; + exp_b += (uint32_t)1; + exp_b = exp_b.simplify( true ); + vtil::logger::log( "exp_b.size: %d \n", exp_b.value.size() ); + vtil::logger::log( "exp_b: %s \n", exp_b.to_string().c_str() ); + // CHECK(exp_a.hash() == exp_b.hash()); + } + + CHECK( 1 == 1 ); } From e5f800e5ff9ee9de1e901033df5c41790bac0136 Mon Sep 17 00:00:00 2001 From: windy <19060@qq.com> Date: Sun, 19 Dec 2021 16:47:21 +0800 Subject: [PATCH 5/7] Fix https://github.com/vtil-project/VTIL-Core/issues/61 --- VTIL-SymEx/expressions/expression.cpp | 2 +- VTIL-Tests/dummy.cpp | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/VTIL-SymEx/expressions/expression.cpp b/VTIL-SymEx/expressions/expression.cpp index 296bbd9..4f68ec2 100644 --- a/VTIL-SymEx/expressions/expression.cpp +++ b/VTIL-SymEx/expressions/expression.cpp @@ -549,7 +549,7 @@ namespace vtil::symbolic case math::operator_id::shift_left: case math::operator_id::shift_right: { - rhs.resize( sizeof(uint8_t), false ); + rhs.resize( sizeof(uintptr_t), false ); break; } diff --git a/VTIL-Tests/dummy.cpp b/VTIL-Tests/dummy.cpp index 4376d78..548fb47 100644 --- a/VTIL-Tests/dummy.cpp +++ b/VTIL-Tests/dummy.cpp @@ -74,27 +74,21 @@ DOCTEST_TEST_CASE("Expression hash") // eax@6:1 vtil::register_desc temp_6(vtil::register_local, 1, 1, 6); auto exp_a = vtil::symbolic::variable{ block->begin(), temp_6 }.to_expression(); - exp_a += (uint32_t)1; + exp_a.resize( vtil::arch::bit_count ); exp_a = exp_a.simplify( true ); vtil::logger::log( "exp_a.size: %d \n", exp_a.value.size() ); vtil::logger::log( "exp_a: %s \n", exp_a.to_string().c_str() ); - - // eax >> 6 && 1 + // eax >> 6 & 1 vtil::register_desc temp(vtil::register_local, 1, vtil::arch::bit_count, 0); auto exp_b = vtil::symbolic::variable{ block->begin(), temp }.to_expression(); exp_b >>= (uint8_t)6; - exp_b &= (uint64_t)1; - exp_b += (uint32_t)1; + exp_b &= (uint8_t)1; exp_b = exp_b.simplify( true ); vtil::logger::log( "exp_b.size: %d \n", exp_b.value.size() ); vtil::logger::log( "exp_b: %s \n", exp_b.to_string().c_str() ); - // CHECK(exp_a.hash() == exp_b.hash()); + CHECK(exp_a.hash() == exp_b.hash()); } - - - - CHECK( 1 == 1 ); } DOCTEST_TEST_CASE("Optimization vtil file") From b92c3f736fccaa3bf976682a47504c0321c8c0e0 Mon Sep 17 00:00:00 2001 From: windy <19060@qq.com> Date: Sun, 19 Dec 2021 16:47:59 +0800 Subject: [PATCH 6/7] make compatible (x86) --- VTIL-Architecture/arch/operands.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/VTIL-Architecture/arch/operands.hpp b/VTIL-Architecture/arch/operands.hpp index f6f586a..6325301 100644 --- a/VTIL-Architecture/arch/operands.hpp +++ b/VTIL-Architecture/arch/operands.hpp @@ -54,6 +54,10 @@ namespace vtil { intptr_t ival; uintptr_t uval; +#if _M_X64 || __x86_64__ + int64_t i64; + uint64_t u64; +#endif }; // Number of bits it is expressed in. From 033c7577ceb086df25f25eb3e843652030263721 Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Fri, 14 Jan 2022 16:32:31 +0100 Subject: [PATCH 7/7] Improve CMake and add instructions to README --- CMakeLists.txt | 12 +++++++++--- README.md | 15 +++++++++++++++ VTIL-Architecture/CMakeLists.txt | 4 +++- VTIL-Common/CMakeLists.txt | 4 +++- VTIL-Compiler/CMakeLists.txt | 4 +++- VTIL-SymEx/CMakeLists.txt | 4 +++- VTIL-Tests/CMakeLists.txt | 4 +++- VTIL/CMakeLists.txt | 4 +++- 8 files changed, 42 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 106f388..c87b62f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,16 @@ cmake_minimum_required(VERSION 3.14.5) # Define the VTIL project project(VTIL-Core) -option(VTIL_BUILD_TESTS "Build tests" OFF) +# Detect if VTIL-Core is compiled as the root project +set(VTIL_ROOT_PROJECT OFF) +if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(VTIL_ROOT_PROJECT ON) -# Enable solution folder support -set_property(GLOBAL PROPERTY USE_FOLDERS ON) + # Enable solution folder support + set_property(GLOBAL PROPERTY USE_FOLDERS ON) +endif() + +option(VTIL_BUILD_TESTS "Build tests" ${VTIL_ROOT_PROJECT}) # Load the dependencies set(CMAKE_FOLDER "VTIL-Core/Dependencies") diff --git a/README.md b/README.md index 888e76a..c464ee3 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,18 @@ This repository contains the core components of the VTIL Project used across the It is currently incomplete as the initial release is not done yet, and documentation and FAQ will be within this repository and the organization website once they're done. Until the initial release, you can keep up to date with the VTIL project by checking my [personal twitter account](https://twitter.com/_can1357) or the VTIL website [vtil.org](https://vtil.org/). + +## Building (Windows) + +``` +cmake -B build +``` + +Then open `build\VTIL-Core.sln`. You can also open this folder in a CMake-compatible IDE (Visual Studio, CLion, Qt Creator, VS Code). + +## Building (Linux/Mac) + +``` +cmake -G Ninja -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build +``` diff --git a/VTIL-Architecture/CMakeLists.txt b/VTIL-Architecture/CMakeLists.txt index a884ea3..9207f67 100644 --- a/VTIL-Architecture/CMakeLists.txt +++ b/VTIL-Architecture/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL-Architecture) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.cpp *.hpp) file(GLOB_RECURSE INCLUDES CONFIGURE_DEPENDS includes/*) diff --git a/VTIL-Common/CMakeLists.txt b/VTIL-Common/CMakeLists.txt index b4e6b28..46840f0 100644 --- a/VTIL-Common/CMakeLists.txt +++ b/VTIL-Common/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL-Common) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.cpp *.hpp) file(GLOB_RECURSE INCLUDES CONFIGURE_DEPENDS includes/*) diff --git a/VTIL-Compiler/CMakeLists.txt b/VTIL-Compiler/CMakeLists.txt index a94e92b..07abbb3 100644 --- a/VTIL-Compiler/CMakeLists.txt +++ b/VTIL-Compiler/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL-Compiler) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.cpp *.hpp) file(GLOB_RECURSE INCLUDES CONFIGURE_DEPENDS includes/*) diff --git a/VTIL-SymEx/CMakeLists.txt b/VTIL-SymEx/CMakeLists.txt index d2106db..b5da114 100644 --- a/VTIL-SymEx/CMakeLists.txt +++ b/VTIL-SymEx/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL-SymEx) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.cpp *.hpp) file(GLOB_RECURSE INCLUDES CONFIGURE_DEPENDS includes/*) diff --git a/VTIL-Tests/CMakeLists.txt b/VTIL-Tests/CMakeLists.txt index de0b4e9..92cd82e 100644 --- a/VTIL-Tests/CMakeLists.txt +++ b/VTIL-Tests/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL-Tests) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS *.cpp *.hpp *.h) diff --git a/VTIL/CMakeLists.txt b/VTIL/CMakeLists.txt index 25ba23f..b5f6b4f 100644 --- a/VTIL/CMakeLists.txt +++ b/VTIL/CMakeLists.txt @@ -1,4 +1,6 @@ -project(VTIL) +# Extract project name from folder +get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) +string(REPLACE " " "_" PROJECT_NAME "${PROJECT_NAME}") # This builds no sources -- it simply aliases a collection of all other targets created by VTIL #