panda/
lib.rs

1//! panda-rs is a set of Rust bindings for PANDA.
2//!
3//! **The following are provided:**
4//! * Callbacks to various PANDA events in the form of attribute macros
5//! * Callbacks for when guest syscalls happen
6//! * Bindings to various core PANDA plugins (hooks2, osi, etc)
7//! * Safe bindings to the core PANDA API
8//! * An API for driving PANDA via libpanda
9//! * Access to raw PANDA and QEMU API bindings via panda_sys
10//!
11//! ### Feature flags:
12//!
13//! * `libpanda` - enable libpanda mode. This is used to allow for compiling as a binary that links
14//! against libpanda, for pypanda-style use.
15//!
16//! #### Architecture-specific features
17//!
18//! PANDA supports multiple architectures, but requires plugins to be compiled for each
19//! architecture. In order to target a specific guest arch, use exactly one of the following:
20//! `x86_64`, `i386`, `arm`, `aarch64`, `mips`, `mipsel`, `mips64`, `ppc`
21//!
22//! Typically PANDA plugins forward each of these features in their Cargo.toml:
23//!
24//! ```toml
25//! [features]
26//! x86_64 = ["panda/x86_64"]
27//! i386 = ["panda/i386"]
28//! # ...
29//! ```
30//!
31//! ### Callbacks
32//!
33//! `panda-rs` makes extensive use of callbacks for handling analyses on various events. To use
34//! callbacks, you simply apply the callback's attribute to any functions which should be called
35//! for the given callback. In order to use a callback in a PANDA plugin (not to be confused with
36//! an application that uses libpanda), one function must be marked [`#[panda::init]`](init),
37//! otherwise the plugin will not work in PANDA.
38//!
39//! Callbacks come in two forms: free form functions (which use the attribute macros)
40//! mentioned above) and closure callbacks, which use the [`Callback`] API.
41//!
42//! ### libpanda Mode
43//!
44//! PANDA also offers a dynamic library (libpanda). panda-rs allows linking against libpanda
45//! instead of linking as a PANDA plugin. This creates a executable that requires libpanda to run.
46//! To compile in libpanda mode, make sure the `PANDA_PATH` environment variable is set to your
47//! PANDA `build` folder.
48//!
49//! ## Helpful Links
50//!
51//! | Important |    Popular Callbacks    | Popular Plugins |
52//! |:---------:|:-----------------------:|:---------------:|
53//! | [`init`]  | [`before_block_exec`]   | [`osi`](plugins::osi) |
54//! | [`Panda`] | [`virt_mem_after_read`] | [`proc_start_linux`](plugins::proc_start_linux) |
55//! | [`mod@hook`]  | [`virt_mem_after_write`]| [`hooks2`](plugins::hooks2) |
56//! | [`on_sys`]| [`asid_changed`]        | [`guest_plugin_manager`](plugins::guest_plugin_manager) |
57//! | [`uninit`]| [`before_block_exec_invalidate_opt`] ||
58//! | [`regs`]  | [`insn_translate`]      ||
59//! | [`PandaArgs`] | [`insn_exec`] ||
60#![cfg_attr(doc_cfg, feature(doc_cfg))]
61
62/// Raw bindings to the PANDA API
63#[doc(inline)]
64pub use panda_sys as sys;
65//pub use panda_macros::*;
66
67/// PANDA callback macros
68#[doc(hidden)]
69pub use panda_macros as cbs;
70
71#[doc(inline)]
72pub use plugins::hooks::hook;
73
74#[doc(hidden)]
75pub use {inventory, lazy_static, once_cell, paste};
76
77#[doc(hidden)]
78extern crate self as panda;
79
80/// Helpers and constants for interacting with various ABIs
81pub mod abi;
82
83/// Callbacks for linux syscalls (from syscalls2)
84pub mod on_sys;
85
86/// Safe wrappers for the libpanda API for helping create and manage an instance of the PANDA API
87mod library_mode;
88pub use library_mode::*;
89
90mod guest_ptr;
91pub use guest_ptr::*;
92
93/// Safe wrappers for the PANDA API
94mod api;
95pub use api::*;
96
97/// Architecture-specific definitions
98mod arch;
99pub use arch::*;
100
101mod error;
102pub use error::*;
103
104/// Event-based callbacks, for both VM events (e.g. translation of a basic block) and PANDA events (e.g. plugin init)
105mod callbacks;
106pub use callbacks::*;
107
108mod init_return;
109pub use init_return::InitReturn;
110
111/// Helpers for getting plugin arguments from panda
112pub mod panda_arg;
113
114#[doc(inline)]
115pub use panda_arg::PandaArgs;
116
117pub mod enums;
118pub mod plugins;
119pub mod taint;
120
121#[cfg_attr(doc_cfg, doc(cfg(feature = "syscall-injection")))]
122#[cfg(all(feature = "syscall-injection", not(feature = "ppc")))]
123pub mod syscall_injection;
124
125pub use enums::arch::*;
126
127/// A set of types PANDA frequently requires but have a low likelihood of clashing with
128/// other types you import, for use as a wildcard import.
129///
130/// ## Example
131///
132/// ```
133/// use panda::prelude::*;
134/// ```
135pub mod prelude {
136    pub use crate::panda_arg::PandaArgs;
137    pub use crate::regs::SyscallPc;
138    pub use crate::sys::target_long;
139    pub use crate::sys::target_pid_t;
140    pub use crate::sys::target_ptr_t;
141    pub use crate::sys::target_ulong;
142    pub use crate::sys::CPUState;
143    pub use crate::sys::TranslationBlock;
144    pub use crate::Panda;
145    pub use crate::PluginHandle;
146    pub use panda_macros::PandaArgs;
147}
148
149#[cfg(not(feature = "ppc"))]
150pub use panda_macros::{on_all_sys_enter, on_all_sys_return};
151
152// callbacks
153pub use panda_macros::{
154    after_block_exec, after_block_translate, after_cpu_exec_enter, after_insn_exec,
155    after_insn_translate, after_loadvm, after_machine_init, asid_changed, before_block_exec,
156    before_block_exec_invalidate_opt, before_block_translate, before_cpu_exec_exit,
157    before_handle_exception, before_handle_interrupt, before_loadvm, before_tcg_codegen,
158    cpu_restore_state, during_machine_init, end_block_exec, guest_hypercall, hd_read, hd_write,
159    hook, init, insn_exec, insn_translate, main_loop_wait, mmio_after_read, mmio_before_write,
160    monitor, on_mmap_updated, on_process_end, on_process_start, on_rec_auxv, on_thread_end,
161    on_thread_start, phys_mem_after_read, phys_mem_after_write, phys_mem_before_read,
162    phys_mem_before_write, pre_shutdown, replay_after_dma, replay_before_dma, replay_handle_packet,
163    replay_hd_transfer, replay_net_transfer, replay_serial_read, replay_serial_receive,
164    replay_serial_send, replay_serial_write, start_block_exec, top_loop, unassigned_io_read,
165    unassigned_io_write, uninit, virt_mem_after_read, virt_mem_after_write, virt_mem_before_read,
166    virt_mem_before_write, GuestType,
167};