Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/keiichiw/padding-detector
Detect implicit paddings in C structs and unions
https://github.com/keiichiw/padding-detector
Last synced: 12 days ago
JSON representation
Detect implicit paddings in C structs and unions
- Host: GitHub
- URL: https://github.com/keiichiw/padding-detector
- Owner: keiichiw
- License: mit
- Created: 2020-04-25T00:46:10.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-09T08:09:12.000Z (over 2 years ago)
- Last Synced: 2024-11-08T01:14:26.288Z (2 months ago)
- Language: Rust
- Homepage:
- Size: 15.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# padding-detector
A command line tool to detect implicit paddings that will be added in C structs and unions.
## Usage
```sh
$ cargo build --release
$ cat ./examples/simple.h
#includestruct test1 {
uint8_t a;
// <3-byte padding>
uint32_t b;
uint64_t c;
uint8_t d;
// <7-byte padding>
};union test2 {
uint64_t a;
char b[9]; // followed by 7-byte padding
};
$ ./target/release/padding-detector ./examples/simple.h
Checking `struct test1` (size=24)...
Warning: Implicit padding was found: sum of fields: 14, struct size: 24
Found: 3-byte padding before "b"
Found: 7-byte padding at the end
Checking `union test2` (size=16)...
Found: 7-byte padding is inserted
```## How it works
The work flow consists of the following three steps:
1. Generates Rust FFI bindings to C structs in the given header by [bindgen].
2. Adds functions in the generated Rust file to check paddings.
3. Executes the generated Rust program in 2.You can see the generated Rust codes by the following command:
```
# You'll find './out/bindings.rs' and './out/generated.rs' for step 1 and 2, respectively.
$ ./target/release/padding-detector ./examples/simple.h -o ./out/
```The reason why it generates Rust bindings instead of processing C directly is it's easy to process
Rust's ASTs thanks to [syn].[bindgen]: https://github.com/rust-lang/rust-bindgen
[syn]: https://github.com/dtolnay/syn## Requirements
This tool depends on `libclang`, on which `bindgen` depends.
See [bindgen's doc](https://rust-lang.github.io/rust-bindgen/requirements.html).