Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,20 @@ jobs:
- name: Python package install smoke
run: |
python -m pip install ./python --target python-pkg-smoke
python -c "import sys; sys.path.insert(0, 'python-pkg-smoke'); import smt_wire; ctx = smt_wire.Context(); assert ctx.bv_var('x', 8).width == 8"
python -c "import sys; sys.path.insert(0, 'python-pkg-smoke'); import bv_wire; ctx = bv_wire.Context(); assert ctx.bv_var('x', 8).width == 8"
- name: Python Z3 server
shell: bash
run: |
# Installs the package's z3 extra into a private directory; the test
# still imports bv_wire and bv_z3_server from python/ itself.
python -m pip install "./python[z3]" --target python-z3-deps
PYTHONPATH=python-z3-deps python python/tests/test_z3_server.py
- name: Live server end-to-end
run: python python/tests/test_live_server.py
- name: C++ client CMake smoke
shell: bash
run: |
cargo build --locked -p smt-server
cargo build --locked -p bv-server
cd cpp
cmake -B build -DCMAKE_BUILD_TYPE=Debug
cmake --build build --config Debug
Expand All @@ -72,16 +79,18 @@ jobs:
build_dir = root / "target" / "debug" / "build"
z3_dirs = [
str(path)
for pattern in ("z3-sys-*/out/z3-*/bin", "z3-sys-*/out/z3-*/lib")
# Older Cargo: build/z3-sys-<hash>/out; newer: build/z3-sys/<hash>/out.
for prefix in ("z3-sys-*", "z3-sys/*")
for pattern in (f"{prefix}/out/z3-*/bin", f"{prefix}/out/z3-*/lib")
for path in build_dir.glob(pattern)
]
env = os.environ.copy()
if z3_dirs:
for var in ("PATH", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"):
env[var] = os.pathsep.join(z3_dirs + [env.get(var, "")])
env["SMT_SERVER_ADDRESS"] = "127.0.0.1:9123"
env["BV_SERVER_ADDRESS"] = "127.0.0.1:9123"

exe = root / "target" / "debug" / ("smt-server.exe" if os.name == "nt" else "smt-server")
exe = root / "target" / "debug" / ("bv-server.exe" if os.name == "nt" else "bv-server")
log_path = root / "target" / "cpp-e2e-server.log"
with log_path.open("wb") as log:
proc = subprocess.Popen([str(exe), "127.0.0.1:9123"], cwd=root, env=env, stdout=log, stderr=log)
Expand All @@ -90,15 +99,15 @@ jobs:
last_error = None
while time.time() < deadline:
if proc.poll() is not None:
raise RuntimeError(f"smt-server exited early with {proc.returncode}")
raise RuntimeError(f"bv-server exited early with {proc.returncode}")
try:
with socket.create_connection(("127.0.0.1", 9123), timeout=0.2):
break
except OSError as exc:
last_error = exc
time.sleep(0.05)
else:
raise TimeoutError(f"smt-server did not start: {last_error}")
raise TimeoutError(f"bv-server did not start: {last_error}")
subprocess.check_call(["ctest", "--test-dir", str(root / "cpp" / "build"), "--output-on-failure", "-C", "Debug"], env=env)
except Exception:
log.flush()
Expand Down
56 changes: 28 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[workspace]
members = [
"crates/smt-wire",
"crates/smt-server",
"crates/bv-wire",
"crates/bv-server",
"crates/qfbvsmtrs",
"crates/smt-qfbv-smtlib",
"crates/bv-smtlib",
]
resolver = "2"

Expand All @@ -16,11 +16,11 @@ license = "MIT OR Apache-2.0"
# Workspace members. `crates/qfbvsmtrs/fuzz` declares its own `[workspace]` and so
# sits outside this one; it keeps a plain path dependency on qfbvsmtrs.
qfbvsmtrs = { path = "crates/qfbvsmtrs" }
smt-qfbv-smtlib = { path = "crates/smt-qfbv-smtlib" }
smt-wire = { path = "crates/smt-wire" }
bv-smtlib = { path = "crates/bv-smtlib" }
bv-wire = { path = "crates/bv-wire" }

# External. z3 has to stay on a single version across the workspace: it is a
# normal dependency of smt-server and a dev-dependency of qfbvsmtrs, and the
# normal dependency of bv-server and a dev-dependency of qfbvsmtrs, and the
# gh-release feature downloads a matching prebuilt solver for each.
binbit = { git = "https://github.com/bint-disasm/binbit", rev = "b4996145c06f39ce5bbc79a959975d7d20b6cf5e" }
blake3 = "1"
Expand Down
75 changes: 45 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SMT Server
# bv-server

SMT Server lets analysis tools build `QF_BV` formulas with small client libraries and delegate solving or simplification to a separate server.
bv-server is a bit-vector expression service for binary analysis. Tools that lift machine code, for symbolic execution, deobfuscation, or IR experiments, send `QF_BV` expressions as a compact DAG, and the server solves, optimizes, or simplifies them.

It targets binary analysis, lifting, symbolic execution, and IR experiments where client projects need bit-vector queries but should avoid embedding solver build systems. A client constructs a flat binary DAG, sends a length-prefixed request, and receives a model, unsat core, optimum value, or simplified expression. The server owns the solver and simplifier integrations.
It targets projects where client projects need bit-vector queries but should avoid embedding solver build systems. A client constructs a flat binary DAG, sends a length-prefixed request, and receives a model, unsat core, optimum value, or simplified expression. The server owns the solver and simplifier integrations.

## Clients

- C++: header-only C++17 package under `cpp/`.
- Python: dependency-free package under `python/`, installable with pip's `#subdirectory=python` support.
- Rust: `smt-wire` crate with an idiomatic `Context`/term/`Client` API; protocol internals are isolated for the server.
- Rust: `bv-wire` crate with an idiomatic `Context`/term/`Client` API; protocol internals are isolated for the server.

## Backends

Expand Down Expand Up @@ -40,10 +40,10 @@ cargo build --workspace
## Run the server

```sh
cargo run -p smt-server -- 127.0.0.1:9123
cargo run -p bv-server -- 127.0.0.1:9123
```

If no address is provided, the server listens on `127.0.0.1:9123`. Clients use `SMT_SERVER_ADDRESS=<host>:<port>` as their default connection target, falling back to `127.0.0.1:9123` when the variable is unset.
If no address is provided, the server listens on `127.0.0.1:9123`. Clients use `BV_SERVER_ADDRESS=<host>:<port>` as their default connection target, falling back to `127.0.0.1:9123` when the variable is unset.

Requests use length-prefixed frames:

Expand All @@ -54,7 +54,7 @@ payload bytes

Payload formats:

- binary `SMTQ` request produced by `smt-wire` or one of the clients;
- binary `BVSQ` request produced by `bv-wire` or one of the clients;
- SMT-LIB script as UTF-8 text.

## Request recording
Expand All @@ -64,47 +64,62 @@ The server records every handled binary request/response pair to a local SQLite
Default database:

```text
~/.smt-server/recordings.db
~/.bv-server/recordings.db
```

Override the database with `SMT_SERVER_RECORD_DB`:
Override the database with `BV_SERVER_RECORD_DB`:

```sh
SMT_SERVER_RECORD_DB=/tmp/smt-recordings.db cargo run -p smt-server -- 127.0.0.1:9123
BV_SERVER_RECORD_DB=/tmp/bv-recordings.db cargo run -p bv-server -- 127.0.0.1:9123
```

Set `SMT_SERVER_RECORD_DB` to an empty value to disable recording:
Set `BV_SERVER_RECORD_DB` to an empty value to disable recording:

```sh
SMT_SERVER_RECORD_DB= cargo run -p smt-server -- 127.0.0.1:9123
BV_SERVER_RECORD_DB= cargo run -p bv-server -- 127.0.0.1:9123
```

Rows are deduplicated by the BLAKE3 hash of the canonical binary request, with request and response `request_id` fields zeroed so equivalent requests share one entry. Existing rows are left unchanged.

The `backend` column names the backend that produced the response: the winner of the solver race (`z3`, `binbit`, `qfbvsmtrs`) or the simplifier (`rumba`). It is NULL for error responses, requests rejected before reaching a backend, and rows recorded before the column existed. For example:

```sh
sqlite3 ~/.bv-server/recordings.db "SELECT backend, count(*) FROM recordings GROUP BY backend"
```

To import the old file-tree corpus from `~/.smt-server/requests` into the SQLite database:

```sh
cargo run -p smt-server -- migrate-recordings
cargo run -p bv-server -- migrate-recordings
```

Custom migration paths are supported:

```sh
cargo run -p smt-server -- migrate-recordings /path/to/requests /tmp/smt-recordings.db
cargo run -p bv-server -- migrate-recordings /path/to/requests /tmp/bv-recordings.db
```

## Upgrading from smt-server

This project used to be called smt-server, and its wire format used the magics `SMTQ` (request), `SMTR` (response) and `SMT\0` (expression buffer). The current magics are `BVSQ`, `BVSR` and `BV\0\0`; the protocol is otherwise unchanged.

- **Old clients keep working.** The server accepts `SMTQ` requests and answers them with `SMTR` responses (and `SMT\0` expression buffers). Clients from this repository send only the new magics, so they need a bv-server, not an old smt-server.
- **Recordings carry over.** On startup, if `~/.bv-server/recordings.db` does not exist but `~/.smt-server/recordings.db` does, the server copies the old database into place; the old file is left unchanged. Any database opened by the server is then upgraded once (tracked by `PRAGMA user_version`): rows are rewritten to the new magics and re-keyed, so old and new clients share recordings and cache entries.
- **Environment variables** were renamed from `SMT_SERVER_*` to `BV_SERVER_*`. `SMT_SERVER_RECORD_DB` is still honoured, with a deprecation warning, when `BV_SERVER_RECORD_DB` is unset; the others (`SMT_SERVER_ADDRESS`, `SMT_SERVER_TEXT_QFBVSMTRS_BUDGET_MS`) must be renamed.
- **Package names:** `smt-wire` → `bv-wire` (Rust crate; Python package and module `bv_wire`; C++ header `<bv_wire/bv_wire.hpp>`, namespace `bv_wire`, CMake package `bv_wire`), `smt-qfbv-smtlib` → `bv-smtlib`, `smt_z3_server.py` → `bv_z3_server.py`. The C++ base exception `smt_error` is now `bv_error`.

## Python client example

The Python client can be installed with `pip install 'git+https://github.com/LLVMParty/smt-server.git#subdirectory=python'` or used directly by adding `python/` to `PYTHONPATH`.
The Python client can be installed with `pip install 'git+https://github.com/LLVMParty/bv-server.git#subdirectory=python'` or used directly by adding `python/` to `PYTHONPATH`.

```python
import smt_wire as smt
import bv_wire as bv

ctx = smt.Context()
ctx = bv.Context()
x = ctx.bv_var("x", 8)
ctx.assert_(ctx.bv_eq(x, 42))

with smt.Client() as client:
with bv.Client() as client:
response = client.solve(ctx) # want_model=True by default

print(response.status) # Status.SAT
Expand All @@ -121,13 +136,13 @@ print(ctx.to_smt2()) # includes check-sat/get-model by default
`SIMPLIFY` requests send one expression root and return a typed term in a fresh result context:

```python
import smt_wire as smt
import bv_wire as bv

ctx = smt.Context()
ctx = bv.Context()
x = ctx.bv_var("x", 64)
target = x + 0

with smt.Client() as client:
with bv.Client() as client:
simplified = client.simplify(target)

if simplified.term is not None:
Expand All @@ -151,7 +166,7 @@ A text frame can contain an SMT-LIB script:
The text frontend supports declarations, assertions, named assertions, `check-sat`, `check-sat-assuming`, `get-model`, `get-value`, `get-unsat-core`, `let`, and common bit-vector operations. It rejects stateful incremental commands such as `push` and `pop`.

```python
with smt.Client() as client:
with bv.Client() as client:
print(client.smt2(script))
```

Expand All @@ -160,7 +175,7 @@ The full Python walkthrough is in `python/example.py`.
## Rust client

```rust
use smt_wire::{Client, Context, Status};
use bv_wire::{Client, Context, Status};

let ctx = Context::new();
let x = ctx.bv_var("x", 8).unwrap();
Expand All @@ -176,20 +191,20 @@ if response.status == Status::Sat {
}
```

A matching Rust walkthrough is in `crates/smt-wire/examples/example.rs`.
A matching Rust walkthrough is in `crates/bv-wire/examples/example.rs`.

## C++ client

The C++ helper is a dependency-free C++17 header:

```cpp
#include <smt_wire/smt_wire.hpp>
#include <bv_wire/bv_wire.hpp>

smt_wire::Context ctx;
bv_wire::Context ctx;
auto x = ctx.bv_var("x", 8);
ctx.assert_(ctx.bv_eq(x, 42u));

smt_wire::Client client;
bv_wire::Client client;
auto response = client.solve(ctx);
```

Expand Down Expand Up @@ -223,14 +238,14 @@ See `docs/qfbvsmtrs-validation.md` for maintainer validation gates and corpus-ru

## Repository layout

- `crates/smt-wire` — Rust high-level client API plus server-side wire-format internals and validators.
- `crates/bv-wire` — Rust high-level client API plus server-side wire-format internals and validators.
- `crates/qfbvsmtrs` — standalone pure-Rust `QF_BV` bit-blasting solver crate and CLI.
- `crates/smt-server` — TCP server, Rumba simplifier integration, solver backend integration, SMT-LIB frontend.
- `crates/bv-server` — TCP server, Rumba simplifier integration, solver backend integration, SMT-LIB frontend.
- `python` — Python client package and tests.
- `cpp` — C++17 header-only package, CMake target, and tests.
- `docs/architecture.md` — current crate/server/backend architecture.
- `docs/client-api.md` — Python, C++, and Rust client API notes.
- `docs/smt-wire-protocol.md` — binary wire protocol reference.
- `docs/bv-wire-protocol.md` — binary wire protocol reference.
- `docs/smtlib-frontend.md` — shared QF_BV SMT-LIB frontend behavior.
- `docs/qfbvsmtrs-design.md` — pure-Rust solver design.
- `docs/qfbvsmtrs-validation.md` — validation gates and latest corpus status.
Loading
Loading