padlock-source
Source analysis backend for padlock — a struct memory layout analyzer for C, C++, Rust, Go, and Zig.
This crate parses source files without invoking a compiler, using tree-sitter (C/C++/Go) and syn (Rust) to extract struct definitions and simulate their memory layout.
Supported languages
| Language | Parser | Notes |
|---|---|---|
| C | tree-sitter-c | structs, unions, typedefs, bit-fields |
| C++ | tree-sitter-cpp | classes, inheritance, vtable pointer, typedef alias resolution; stdlib types (std::string, std::vector, std::optional, std::shared_ptr, …); template structs/classes are skipped |
| Rust | syn | repr(C), repr(packed), repr(align(N)), all primitive types, NonZeroXxx, f16/f128, transparent newtypes (Cell<T>, MaybeUninit<T>, etc.); repr(Rust) structs emit a caveat note; generic structs are skipped |
| Go | tree-sitter-go | built-in types, sync.Mutex, slices, maps, locally-declared interfaces (sized as 2-word fat pointers); qualified cross-package types flagged as uncertain |
| Zig | tree-sitter | structs, unions (bare + tagged), packed structs; C interop types, arbitrary-width integers |
Concurrency annotation
Guard annotations are read from source to enable accurate false-sharing detection:
- Rust:
#[lock_protected_by = "mu"],#[guarded_by("mu")] - C/C++:
GUARDED_BY(mu),__attribute__((guarded_by(mu))) - Go:
// padlock:guard=mu,// guarded_by: mu,// +checklocksprotects:mu
Fields without explicit annotations are inferred from their type names (Mutex, std::atomic, sync.Mutex, …).
Source locations
parse_source(path, arch) populates StructLayout.source_file and StructLayout.source_line for every returned layout, enabling line-level navigation in CLI output and SARIF reports.
Usage
padlock-source is an internal library crate. To analyze source files, use the CLI:
Part of padlock
padlock-cli— CLI (padlock+cargo-padlockbinaries)padlock-core— IR, analysis passes, findingspadlock-source— Source analysis (C/C++/Rust/Go) (this crate)padlock-dwarf— Binary analysis (DWARF/PDB)padlock-output— Output formatters (terminal/JSON/SARIF/diff)padlock-macros— Compile-time layout assertions