C and C++ Quickstart

archsem-capi exposes Architectura's lifting, CFG discovery, SSA analysis, concrete execution, and optional symbolic execution to C and C++ consumers via:

  • C ABI header: archsem-capi/include/archsem.h (ABI version 4, minor version 9)
  • Header-only C++17 wrapper: archsem-capi/include/archsem.hpp
  • CMake target: archsem::archsem

archsem-capi links archsem-x86 (iced), archsem-arm64 (farm64), archsem-evm, archsem-cil, archsem-wasm, archsem-lua, and archsem-ref (arm64-guest) into a single native library (libarchsem / archsem.lib / archsem.dll).

Building with CMake

In your CMakeLists.txt:

cmake_minimum_required(VERSION 3.19)
project(my_tool LANGUAGES C CXX)

add_subdirectory(path/to/architectura/archsem-capi archsem)
target_link_libraries(my_tool PRIVATE archsem::archsem)

CMake invokes cargo automatically (Rust 1.88+ must be on PATH) and propagates include directories, C++17 requirements, and platform system libraries.

CMake Options

OptionDefaultDescription
ARCHSEM_SHAREDOFFBuild and link a shared library (libarchsem.so / .dylib / archsem.dll) instead of static
ARCHSEM_SYMEXOFFEnable bounded symbolic execution (archsem_symex_*). Links bitwright (PolyForm-Noncommercial-1.0.0)
ARCHSEM_CARGO_PROFILEreleaseCargo build profile (release, dev, etc.)
ARCHSEM_CARGO_TARGET_DIRBuild treeOverride Cargo's target directory
ARCHSEM_BUILD_EXAMPLESON when top-levelBuild C++ examples and register ctest targets

Building Directly with Cargo

cargo build --release -p archsem-capi
c++ -std=c++17 -I archsem-capi/include my_tool.cpp target/release/libarchsem.a -lpthread -ldl -lm -lrt -lutil

On Windows (MSVC), link archsem.lib along with kernel32, advapi32, ntdll, userenv, ws2_32, bcrypt, and dbghelp (handled automatically by CMake).

C++17 Quickstart (archsem.hpp)

The C++17 wrapper provides RAII ownership (Lifter, Fragment, Function, Machine), range-based views (Node, Block, CfgTarget), and throws archsem::Error on failure (with non-throwing try_lift / try_discover / try_lift_function alternatives returning archsem::Result).

#include "archsem.hpp"

#include <cstdint>
#include <iostream>
#include <vector>

int main() {
    // 1. Lift a single x86-64 instruction: add rax, rbx (48 01 d8)
    archsem::Lifter lifter(archsem::Arch::X86_64);
    archsem::Fragment add = lifter.lift({0x48, 0x01, 0xd8}, 0x401000);

    std::cout << add.text();
    for (archsem::Node node : add.nodes()) {
        if (node.kind() == archsem::NodeKind::WriteReg) {
            std::cout << "writes: " << node.reg().name() << '\n';
        }
    }

    // 2. Discover and optimize a function CFG from an in-memory buffer.
    const std::uint8_t code[] = {
        0x85, 0xff,                   // test edi, edi
        0x74, 0x05,                   // je +5
        0xb8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1
        0xc3,                         // ret
    };
    archsem::Lifter opt_lifter(archsem::Arch::X86_64, {/*optimize=*/true});
    archsem::Function fn = opt_lifter.lift_function(code, 0x1000, 0x1000);

    for (archsem::Block block : fn.blocks()) {
        std::cout << "block " << block.id() << " @ 0x" << std::hex << block.start_address() << std::dec << '\n';
    }

    // Re-optimize with ABI exit liveness and fault-boundary assumptions.
    auto abi_opts = archsem::OptimizeOptions::abi().assume_fault_state_unobserved();
    archsem::Function lean = fn.optimize(abi_opts);

    // 3. Concrete execution with real register aliasing.
    archsem::Machine machine(archsem::Arch::X86_64);
    machine.load(0x1000, code);
    machine.set("edi", 0);
    machine.set("rsp", 0x8000);
    machine.write(0x8000, std::array<std::uint64_t, 1>{0xdead0000});

    archsem::ExecutionResult res = machine.run(lifter, 1000, {0xdead0000});
    std::cout << "exit=" << archsem::to_string(res.exit)
              << " rax=" << machine.get("rax") << '\n';

    return 0;
}

Symbolic Execution in C++ (ARCHSEM_SYMEX=ON)

When built with -DARCHSEM_SYMEX=ON (or --features symex):

archsem::SymexOptions options;
options.memory = archsem::SymMemory::Symbolic;
options.registers = {{"rsp", {0x8000}}};

archsem::SymbolicReport report = archsem::symex(lean, options);
for (const archsem_sym_path &path : report.paths()) {
    if (auto rax = report.value(path, "rax")) {
        std::cout << "path rax = " << rax->text() << '\n';
    }
}

[!WARNING] Enabling ARCHSEM_SYMEX=ON links bitwright, which is licensed under PolyForm-Noncommercial-1.0.0. Keep ARCHSEM_SYMEX=OFF if you require a purely MIT-licensed binary.

C ABI Quickstart (archsem.h)

The C ABI uses explicit heap-owned handles (*_free), archsem_status return codes, and optional archsem_error ** out-parameters:

#include "archsem.h"
#include <stdio.h>

int main(void) {
    archsem_error *err = NULL;
    archsem_lifter *lifter = NULL;
    archsem_fragment *frag = NULL;

    if (archsem_lifter_new(ARCHSEM_ARCH_X86_64, 0, &lifter, &err) != ARCHSEM_STATUS_OK) {
        fprintf(stderr, "error: %s\n", archsem_error_message(err));
        archsem_error_free(err);
        return 1;
    }

    const uint8_t insn[] = {0x48, 0x01, 0xd8}; /* add rax, rbx */
    if (archsem_lift_instruction(lifter, insn, sizeof(insn), 0x401000, &frag, &err) != ARCHSEM_STATUS_OK) {
        fprintf(stderr, "lift failed: %s\n", archsem_error_message(err));
        archsem_error_free(err);
        archsem_lifter_free(lifter);
        return 1;
    }

    size_t node_count = 0;
    const archsem_node *nodes = archsem_fragment_nodes(frag, &node_count);
    for (size_t i = 0; i < node_count; ++i) {
        if (nodes[i].kind == ARCHSEM_NODE_WRITE_REG) {
            printf("write reg: %s (%u bits)\n", nodes[i].reg.name, nodes[i].reg.bits);
        }
    }

    archsem_fragment_free(frag);
    archsem_lifter_free(lifter);
    return 0;
}

ABI Versioning, Threading, and Ownership

  • Ownership: Top-level handles (archsem_lifter, archsem_fragment, archsem_function, archsem_machine, archsem_register_ssa, archsem_memory_ssa, archsem_symbolic_report, archsem_*_module, archsem_error) are caller-owned and must be freed via *_free. All returned node/string/slice pointers are borrowed from the owning object.
  • Thread Safety: Lifter, Fragment, Function, SymbolicReport, and bytecode module handles are immutable after creation and safe to read concurrently from multiple threads; one Lifter can lift across multiple threads simultaneously. Machine is single-threaded.
  • ABI Stability: archsem_abi_version() returns the major ABI revision (4) and archsem_abi_minor_version() returns the compatible minor revision (9). Options structs begin with uint32_t struct_size for forward/backward compatibility. Unknown node, control, terminator, or exit constants added in future minor releases should be treated as unknown rather than fatal errors.
  • Panic Safety: No Rust panic crosses the FFI boundary; panics are caught and surfaced as ARCHSEM_STATUS_INTERNAL.