mirror of
https://dev.lirent.ru/Vatrog/vm-introspection-engine.git
synced 2026-07-08 22:36:37 +03:00
244511b66e
insndec.h: insn_decode() decodes one instruction into a readable form - concrete registers as canonical roots (al/ah/ax/eax/rax -> one root), memory base/index/scale/disp, immediate values, operand sizes, per-operand read/write, and implicit operands/effects (rsp on push/pop, rdx:rax on mul/div, FLAGS). It is ABI-agnostic: a call reports only its architectural effects. codetrace.h: func_usedef(fn, abi, ...) computes per-instruction register use/def over a function view (reusing cfg_blocks), splitting writes into full-kill / partial / conditional so a reaching-definitions pass can be built on top. With TRACE_ABI_WIN64 it marks the Win64 caller-saved set (rax,rcx,rdx,r8-r11,xmm0-5,flags) clobbered at each call site; the clobber table is confined to codetrace.c and the generic layer stays OS-agnostic (TRACE_ABI_NONE keeps the conservative behaviour). Both ride the existing optional disassembler seam (a second insn_decode_full is added to semsig_backend.h; sem_insn and semsig_hash are untouched), gated behind VMIE_HAVE_DISASM; OFF builds a stub. Adds the vmie_win32_func_usedef wrapper. No new build option - the feature shares VMIE_DISASM with semsig.
204 lines
9.4 KiB
CMake
204 lines
9.4 KiB
CMake
cmake_minimum_required(VERSION 3.18) # find_program(... REQUIRED)
|
|
|
|
set(VMIE_VERSION "0.2.0" CACHE STRING "Library version (MAJOR.MINOR.PATCH); CI passes the tag")
|
|
project(vmi-engine VERSION ${VMIE_VERSION} LANGUAGES C)
|
|
|
|
set(CMAKE_C_STANDARD 17) # generation B uses no C23 feature
|
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
|
set(CMAKE_C_EXTENSIONS ON) # deliberate: strnlen (POSIX) + void* arithmetic (GNU)
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC on all objects: static lib stays linkable into a shared object
|
|
|
|
include(GNUInstallDirs)
|
|
|
|
option(VMIE_LTO "Enable LTO" OFF) # build-only; shipped default is -O2, no LTO
|
|
|
|
# ---- optional semantic-signature backend (operand disassembler) ----------
|
|
# VMIE_DISASM selects the OPTIONAL operand-decode backend for the semantic
|
|
# signature feature (semsig.h). OFF (default) ships the 0.1.6 behaviour: the
|
|
# generic symbol exists via semsig_stub.c, returns 0, VMIE_HAVE_DISASM=0, and no
|
|
# external library is pulled - CI and the .deb packages are unchanged. A backend
|
|
# (zydis|capstone) is NEVER vendored: it is brought from OUTSIDE the tree, either
|
|
# by VMIE_DISASM_SRC (a path to the backend's own source tree, add_subdirectory'd)
|
|
# or, failing that, by find_package of an already-installed library.
|
|
set(VMIE_DISASM "OFF" CACHE STRING "Semantic-signature backend: OFF | zydis | capstone")
|
|
set_property(CACHE VMIE_DISASM PROPERTY STRINGS OFF zydis capstone)
|
|
set(VMIE_DISASM_SRC "" CACHE PATH "Path to the disassembler backend source tree (external; add_subdirectory'd)")
|
|
|
|
# ---- host: VMI core objects (single owner of the source list) -----------
|
|
add_library(vmie_obj OBJECT
|
|
src/core/gpa.c
|
|
src/engine/gva.c
|
|
src/engine/sigphys.c
|
|
src/engine/win32/host.c
|
|
src/engine/win32/pe.c
|
|
src/engine/win32/proc.c
|
|
src/engine/win32/profile.c
|
|
src/engine/win32/text.c
|
|
src/handlers/scan.c
|
|
src/handlers/sigscan.c
|
|
src/handlers/sigset.c
|
|
src/handlers/codescan.c
|
|
src/handlers/codeanalysis.c
|
|
src/handlers/siggen.c
|
|
src/handlers/x86dec.c
|
|
src/handlers/pmap.c
|
|
src/handlers/snapdiff.c)
|
|
|
|
# semantic-signature TUs: OFF compiles ONLY the stub (which carries the whole
|
|
# generic symbol and returns 0); a backend compiles the core (semsig.c) + the one
|
|
# backend TU that talks to the disassembler. semsig.c is never built in OFF (it
|
|
# calls the backend decode, absent there) - so there is no dead/unresolved call.
|
|
if(VMIE_DISASM STREQUAL "OFF")
|
|
target_sources(vmie_obj PRIVATE src/handlers/semsig_stub.c
|
|
src/handlers/insndec_stub.c)
|
|
elseif(VMIE_DISASM STREQUAL "zydis")
|
|
target_sources(vmie_obj PRIVATE src/handlers/semsig.c
|
|
src/handlers/insndec.c
|
|
src/handlers/codetrace.c
|
|
src/handlers/semsig_backend_zydis.c)
|
|
elseif(VMIE_DISASM STREQUAL "capstone")
|
|
target_sources(vmie_obj PRIVATE src/handlers/semsig.c
|
|
src/handlers/insndec.c
|
|
src/handlers/codetrace.c
|
|
src/handlers/semsig_backend_capstone.c)
|
|
else()
|
|
message(FATAL_ERROR "VMIE_DISASM must be OFF, zydis, or capstone (got '${VMIE_DISASM}')")
|
|
endif()
|
|
|
|
target_include_directories(vmie_obj
|
|
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include # public API: include/*.h
|
|
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/core/include # private: core.h
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/engine/include # private: engine-arch.h, pe.h
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/engine/win32) # private: engine-win32.h, contract.h
|
|
target_compile_options(vmie_obj PRIVATE -O2 -Wall -Wextra)
|
|
if(VMIE_LTO)
|
|
target_compile_options(vmie_obj PRIVATE -flto)
|
|
target_link_options(vmie_obj PRIVATE -flto)
|
|
endif()
|
|
|
|
# Resolve the backend target/package name (the actual link/define is applied to
|
|
# vmie_obj here AND re-applied to the static/shared libs below, because
|
|
# $<TARGET_OBJECTS:vmie_obj> carries object files only - not link/define usage
|
|
# requirements). The backend is PRIVATE (an implementation detail), while
|
|
# VMIE_HAVE_DISASM is PUBLIC (visible through semsig.h). The backend comes from
|
|
# VMIE_DISASM_SRC (external sources) or find_package - never from in-tree sources.
|
|
if(NOT VMIE_DISASM STREQUAL "OFF")
|
|
if(VMIE_DISASM STREQUAL "zydis")
|
|
set(VMIE_DISASM_LINK "Zydis::Zydis")
|
|
set(_vmie_disasm_pkg "Zydis")
|
|
else()
|
|
set(VMIE_DISASM_LINK "capstone::capstone")
|
|
set(_vmie_disasm_pkg "capstone")
|
|
endif()
|
|
|
|
if(VMIE_DISASM_SRC)
|
|
# Build the backend from its OWN external source tree (kept out of this
|
|
# repo). Its CMake exports the imported target linked below.
|
|
add_subdirectory(${VMIE_DISASM_SRC} ${CMAKE_CURRENT_BINARY_DIR}/_disasm_backend)
|
|
else()
|
|
find_package(${_vmie_disasm_pkg} REQUIRED)
|
|
endif()
|
|
|
|
target_link_libraries(vmie_obj PRIVATE ${VMIE_DISASM_LINK})
|
|
target_compile_definitions(vmie_obj PUBLIC VMIE_HAVE_DISASM=1)
|
|
else()
|
|
set(VMIE_DISASM_LINK "")
|
|
target_compile_definitions(vmie_obj PUBLIC VMIE_HAVE_DISASM=0)
|
|
endif()
|
|
|
|
# ---- host: static library (in-tree consumers: CLIs + test) --------------
|
|
# $<TARGET_OBJECTS:> carries object files only, not usage requirements:
|
|
# re-declare the PUBLIC include scope so vmie_cli/vmie_scan still see include/.
|
|
add_library(vmie STATIC $<TARGET_OBJECTS:vmie_obj>)
|
|
target_include_directories(vmie PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
# Re-apply the feature macro (PUBLIC: seen by in-tree consumers + the test) and
|
|
# the backend link (PRIVATE), which $<TARGET_OBJECTS> does not carry over.
|
|
if(VMIE_DISASM STREQUAL "OFF")
|
|
target_compile_definitions(vmie PUBLIC VMIE_HAVE_DISASM=0)
|
|
else()
|
|
target_compile_definitions(vmie PUBLIC VMIE_HAVE_DISASM=1)
|
|
target_link_libraries(vmie PRIVATE ${VMIE_DISASM_LINK})
|
|
endif()
|
|
|
|
# ---- host: shared library (external delivery: libvmie.so.*) -------------
|
|
add_library(vmie_shared SHARED $<TARGET_OBJECTS:vmie_obj>)
|
|
target_include_directories(vmie_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
|
set_target_properties(vmie_shared PROPERTIES
|
|
OUTPUT_NAME vmie
|
|
SOVERSION ${PROJECT_VERSION_MAJOR}
|
|
VERSION ${PROJECT_VERSION})
|
|
if(VMIE_LTO)
|
|
target_link_options(vmie_shared PRIVATE -flto)
|
|
endif()
|
|
if(VMIE_DISASM STREQUAL "OFF")
|
|
target_compile_definitions(vmie_shared PUBLIC VMIE_HAVE_DISASM=0)
|
|
else()
|
|
target_compile_definitions(vmie_shared PUBLIC VMIE_HAVE_DISASM=1)
|
|
target_link_libraries(vmie_shared PRIVATE ${VMIE_DISASM_LINK})
|
|
endif()
|
|
|
|
# ---- host: CLI demonstrator over the library ----------------------------
|
|
add_executable(vmie_cli src/cli.c)
|
|
target_link_libraries(vmie_cli PRIVATE vmie) # public include/ comes via vmie (PUBLIC)
|
|
target_compile_options(vmie_cli PRIVATE -Wall -Wextra)
|
|
|
|
# ---- host: dump-scan demonstrator (OS-agnostic, no win32) ----------------
|
|
add_executable(vmie_scan src/scan_cli.c)
|
|
target_link_libraries(vmie_scan PRIVATE vmie)
|
|
target_compile_options(vmie_scan PRIVATE -Wall -Wextra)
|
|
|
|
# ---- guest: cross-compile to Windows x86-64 via mingw-w64 ---------------
|
|
find_program(MINGW_CC NAMES x86_64-w64-mingw32-gcc REQUIRED)
|
|
set(VMIE_STARTUP ${CMAKE_CURRENT_BINARY_DIR}/vmie-startup.exe)
|
|
add_custom_command(
|
|
OUTPUT ${VMIE_STARTUP}
|
|
COMMAND ${MINGW_CC} -O2 -Wall -Wextra -static -s
|
|
-I${CMAKE_CURRENT_SOURCE_DIR}/src/engine/win32
|
|
-o ${VMIE_STARTUP} ${CMAKE_CURRENT_SOURCE_DIR}/src/engine/win32/guest.c
|
|
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/engine/win32/guest.c
|
|
${CMAKE_CURRENT_SOURCE_DIR}/src/engine/win32/contract.h
|
|
COMMENT "Cross-compiling vmie-startup.exe (mingw-w64, x86-64)"
|
|
VERBATIM)
|
|
add_custom_target(vmie-startup ALL DEPENDS ${VMIE_STARTUP})
|
|
|
|
# ---- install: shared library + public headers ---------------------------
|
|
# Versioned file + SONAME symlink -> runtime; unversioned linker symlink -> dev.
|
|
install(TARGETS vmie_shared
|
|
LIBRARY
|
|
DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
|
COMPONENT runtime
|
|
NAMELINK_COMPONENT dev)
|
|
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/
|
|
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/vmie
|
|
COMPONENT dev
|
|
FILES_MATCHING PATTERN "*.h")
|
|
|
|
# ---- package: two Debian packages (runtime + dev) -----------------------
|
|
set(CPACK_GENERATOR "DEB")
|
|
set(CPACK_DEB_COMPONENT_INSTALL ON)
|
|
set(CPACK_COMPONENTS_ALL runtime dev)
|
|
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
|
|
|
|
# component -> Debian package name (Debian shared-library convention)
|
|
set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME "libvmie${PROJECT_VERSION_MAJOR}")
|
|
set(CPACK_DEBIAN_DEV_PACKAGE_NAME "libvmie-dev")
|
|
|
|
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
|
|
set(VMIE_DEB_MAINTAINER "Gregory Lirent <opensource@lirent.ru>"
|
|
CACHE STRING "Debian package Maintainer field")
|
|
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${VMIE_DEB_MAINTAINER}")
|
|
set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
|
|
set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
|
|
set(CPACK_DEBIAN_FILE_NAME "DEB-DEFAULT")
|
|
|
|
set(CPACK_COMPONENT_RUNTIME_DESCRIPTION
|
|
"VM introspection engine (Windows-guest memory introspection) - shared library")
|
|
set(CPACK_COMPONENT_DEV_DESCRIPTION
|
|
"VM introspection engine (Windows-guest memory introspection) - development files")
|
|
|
|
# dev depends on the exact runtime version; runtime libc dependency via shlibdeps
|
|
set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "libvmie${PROJECT_VERSION_MAJOR} (= ${PROJECT_VERSION})")
|
|
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
|
|
|
|
include(CPack)
|