Skip to content

Commit af384a3

Browse files
Merge pull request #42 from rusty1968/hash-owned
Implementation of OpenProt owned digest API for ASPEED HACE controller
2 parents 91bb9c6 + 07af3af commit af384a3

File tree

17 files changed

+1514
-81
lines changed

17 files changed

+1514
-81
lines changed

.github/copilot-instructions.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Copilot Instructions for aspeed-ddk
2+
3+
## Project Overview
4+
aspeed-ddk is a Rust-based driver development kit for ASPEED SoCs, focusing on no_std environments and efficient resource usage.
5+
6+
## Pull Request Review Checklist
7+
8+
- [ ] Code is completely panic-free (no unwrap/expect/panic/indexing)
9+
- [ ] All fallible operations return Result or Option
10+
- [ ] Integer operations use checked/saturating/wrapping methods where needed
11+
- [ ] Array/slice access uses get() or pattern matching, not direct indexing
12+
- [ ] Error cases are well documented and handled appropriately
13+
- [ ] Tests verify error handling paths, not just happy paths
14+
15+
## Quick Reference: Forbidden Patterns
16+
17+
| Forbidden Pattern | Required Alternative |
18+
|-------------------|----------------------|
19+
| `value.unwrap()` | `match value { Some(v) => v, None => return Err(...) }` |
20+
| `result.expect("msg")` | `match result { Ok(v) => v, Err(e) => return Err(e.into()) }` |
21+
| `collection[index]` | `collection.get(index).ok_or(Error::OutOfBounds)?` |
22+
23+
24+
## Code Style
25+
26+
### no_std and Memory Allocation Guidelines
27+
28+
- This project is strictly **no_std** and **no_alloc** in production code
29+
- All production paths must be allocation-free and compatible with bare-metal targets
30+
31+
#### Production Code Requirements
32+
33+
- **NEVER** use crates or features that require heap allocation in production code
34+
- **DO NOT** use the `heapless` crate in production paths despite its name suggesting compatibility
35+
- **DO NOT** use any crate that depends on the `alloc` crate without feature gating
36+
- **ALWAYS** use fixed-size arrays, slices, or static memory allocation
37+
- **ALWAYS** design APIs to accept and return memory provided by the caller
38+
39+
#### Memory Management in Production Code
40+
41+
- Buffers must be pre-allocated by the caller and passed as slices
42+
- Collection types must have fixed, compile-time sizes
43+
- All data structures must have predictable, static memory footprints
44+
- No dynamic memory growth patterns are allowed
45+
46+
#### Test Code Exceptions
47+
48+
- Test code (annotated with `#[cfg(test)]`) may use allocation if needed
49+
- The `heapless` crate and other no_std compatible collections are permitted in tests
50+
- Standard library features may be used in tests when the `std` feature is enabled
51+
- Test helpers can use more ergonomic APIs that wouldn't be appropriate for production
52+
53+
#### Example: Production vs. Test Code
54+
55+
```rust
56+
// Production code - strict no_alloc approach
57+
pub fn process_data(data: &[u8], output: &mut [u8]) -> Result<usize, Error> {
58+
if output.len() < data.len() * 2 {
59+
return Err(Error::BufferTooSmall);
60+
}
61+
62+
// Process data into output buffer
63+
// ...
64+
65+
Ok(processed_bytes)
66+
}
67+
68+
// Test code - can use more ergonomic approaches
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
73+
#[test]
74+
fn test_process_data() {
75+
// It's fine to use Vec in tests
76+
let input = vec![1, 2, 3, 4];
77+
let mut output = vec![0; 16];
78+
79+
let result = process_data(&input, &mut output);
80+
assert!(result.is_ok());
81+
// ...
82+
}
83+
84+
#[test]
85+
fn test_with_heapless() {
86+
// Heapless is also fine in tests
87+
use heapless::Vec;
88+
let mut input: Vec<u8, 8> = Vec::new();
89+
input.extend_from_slice(&[1, 2, 3]).unwrap();
90+
91+
let mut output = [0u8; 16];
92+
let result = process_data(&input, &mut output);
93+
assert!(result.is_ok());
94+
// ...
95+
}
96+
}
97+
98+
99+
### Unsafe Code
100+
101+
- Minimize unsafe code; isolate in well-documented functions
102+
- Document all safety preconditions in unsafe functions
103+
- Add safety comments explaining why unsafe is necessary
104+
- Unit test unsafe code thoroughly
105+
106+
## Common Patterns
107+
108+
### Static vs. Dynamic Dispatch
109+
110+
This project strongly prefers static dispatch over dynamic dispatch to optimize for binary size, performance, and no_std compatibility.
111+
112+
#### Static Dispatch Requirements
113+
114+
- Use generic parameters instead of trait objects (`dyn Trait`) whenever possible
115+
- Leverage impl Trait in return positions rather than Box<dyn Trait>
116+
- Monomorphize code at compile time rather than using virtual dispatch at runtime
117+
- Avoid heap allocations associated with typical dyn Trait usage
118+
119+
120+
## Workflows
121+
122+
### Pre-commit
123+
cargo xtask precommit

.github/workflows/build-test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
run: |
2929
sudo apt-get update -qy
3030
sudo apt-get install -qy build-essential curl gcc-multilib gcc-riscv64-unknown-elf git
31+
32+
- name: Install cargo-bloat
33+
run: cargo install cargo-bloat
3134

3235
- name: Verify Cargo.lock is up to date
3336
run: |
@@ -41,3 +44,11 @@ jobs:
4144
- name: Run precommit checks (build/format/lint)
4245
run: |
4346
cargo --config "$EXTRA_CARGO_CONFIG" xtask precommit
47+
48+
- name: Upload binary size reports
49+
uses: actions/upload-artifact@v4
50+
if: always() && hashFiles('target/bloat-reports/*') != ''
51+
with:
52+
name: bloat-reports
53+
path: target/bloat-reports/
54+
retention-days: 30
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Licensed under the Apache-2.0 license
2+
3+
name: Binary Size Analysis
4+
5+
on:
6+
pull_request:
7+
branches:
8+
- main
9+
paths:
10+
- 'src/**'
11+
- 'Cargo.toml'
12+
- 'Cargo.lock'
13+
14+
jobs:
15+
size-analysis:
16+
runs-on: ubuntu-22.04
17+
18+
permissions:
19+
contents: read
20+
21+
steps:
22+
- name: Checkout PR
23+
uses: actions/checkout@v4
24+
with:
25+
fetch-depth: 0
26+
27+
- name: Install packages
28+
run: |
29+
sudo apt-get update -qy
30+
sudo apt-get install -qy build-essential curl gcc-multilib gcc-riscv64-unknown-elf git
31+
32+
- name: Install cargo-bloat
33+
run: cargo install cargo-bloat
34+
35+
- name: Build current branch
36+
run: |
37+
cargo build --release --target thumbv7em-none-eabihf
38+
39+
- name: Analyze current branch size
40+
run: |
41+
# Use cargo bloat directly since xtask bloat may not be available
42+
cargo bloat --release --target thumbv7em-none-eabihf > target/pr-bloat-report.txt || echo "Bloat analysis failed"
43+
44+
- name: Checkout main branch
45+
run: |
46+
git checkout main
47+
48+
- name: Build main branch
49+
run: |
50+
cargo build --release --target thumbv7em-none-eabihf
51+
52+
- name: Analyze main branch size
53+
run: |
54+
# Use cargo bloat directly since xtask bloat may not be available
55+
cargo bloat --release --target thumbv7em-none-eabihf > target/main-bloat-report.txt || echo "Bloat analysis failed"
56+
57+
- name: Generate size comparison report
58+
id: size-comparison
59+
run: |
60+
# Create a comprehensive comparison
61+
echo "## 📊 Binary Size Analysis" > size_report.md
62+
echo "" >> size_report.md
63+
64+
# Get binary sizes
65+
PR_SIZE=$(stat -c%s target/thumbv7em-none-eabihf/release/aspeed-ddk 2>/dev/null || echo "0")
66+
git checkout -
67+
MAIN_SIZE=$(stat -c%s target/thumbv7em-none-eabihf/release/aspeed-ddk 2>/dev/null || echo "0")
68+
69+
# Calculate difference
70+
SIZE_DIFF=$((PR_SIZE - MAIN_SIZE))
71+
72+
echo "### Size Comparison" >> size_report.md
73+
echo "- **Main branch**: $(numfmt --to=iec $MAIN_SIZE)" >> size_report.md
74+
echo "- **PR branch**: $(numfmt --to=iec $PR_SIZE)" >> size_report.md
75+
echo "- **Difference**: $(numfmt --to=iec $SIZE_DIFF)" >> size_report.md
76+
echo "" >> size_report.md
77+
78+
if [ $SIZE_DIFF -gt 10240 ]; then
79+
echo "⚠️ **Warning**: Binary size increased by more than 10KB" >> size_report.md
80+
echo "size_warning=true" >> $GITHUB_OUTPUT
81+
elif [ $SIZE_DIFF -gt 0 ]; then
82+
echo "ℹ️ Binary size increased slightly" >> size_report.md
83+
elif [ $SIZE_DIFF -lt 0 ]; then
84+
echo "✅ Binary size decreased!" >> size_report.md
85+
fi
86+
87+
echo "" >> size_report.md
88+
echo "### Top Functions (Current PR)" >> size_report.md
89+
echo '```' >> size_report.md
90+
head -20 target/pr-bloat-report.txt >> size_report.md 2>/dev/null || echo "No function data available" >> size_report.md
91+
echo '```' >> size_report.md
92+
93+
- name: Output size analysis to workflow summary
94+
run: |
95+
echo "## 📊 Binary Size Analysis" >> $GITHUB_STEP_SUMMARY
96+
cat size_report.md >> $GITHUB_STEP_SUMMARY
97+
98+
- name: Output size analysis to console
99+
run: |
100+
echo "=== Binary Size Analysis ==="
101+
cat size_report.md
102+
103+
- name: Check for significant size increase
104+
run: |
105+
# Extract size difference from the report
106+
if grep -q "⚠️ Binary size increased by more than 10KB" size_report.md; then
107+
echo "::error::Binary size increased by more than 10KB! Please review."
108+
exit 1
109+
elif grep -q "🚨 Binary size increased significantly" size_report.md; then
110+
echo "::warning::Binary size increased significantly. Consider optimization."
111+
fi
112+
113+
- name: Upload detailed reports
114+
uses: actions/upload-artifact@v4
115+
with:
116+
name: size-analysis-detailed
117+
path: |
118+
target/pr-bloat-report.txt
119+
target/main-bloat-report.txt
120+
size_report.md
121+
retention-days: 7

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
/target
2+
3+
# Map files generated by linker
4+
*.map
5+
6+
# Bloat analysis reports
7+
target/bloat-reports/
8+
target/*-bloat-reports/

0 commit comments

Comments
 (0)