| name | acsl-annotation-assistant |
| description | Create ACSL (ANSI/ISO C Specification Language) formal annotations for C/C++ programs. Use this skill when working with formal verification, adding function contracts (requires/ensures), loop invariants, assertions, memory safety annotations, or any ACSL specifications. Supports Frama-C verification and generates comprehensive formal specifications for C/C++ code. |
ACSL Annotation Assistant
Generate comprehensive ACSL (ANSI/ISO C Specification Language) annotations for C/C++ programs to support formal verification with tools like Frama-C.
Core Capabilities
1. Function Contracts
Add complete function specifications with preconditions and postconditions:
int find_max_index(int *array, int n);
2. Loop Annotations
Generate loop invariants, variants, and assigns clauses:
for (i = 0; i < n; i++) {
sum += array[i];
}
3. Memory Safety Specifications
Add pointer validity and separation annotations:
void memcpy_safe(char *dest, const char *src, size_t n);
4. Assertions and Assumptions
Insert runtime and verification assertions:
5. Axiomatic Definitions and Predicates
Define reusable logical predicates and axioms:
Annotation Workflow
Step 1: Analyze the Function
Before annotating:
- Identify inputs, outputs, and side effects
- Determine memory access patterns
- Understand algorithmic properties (sorting, searching, etc.)
- Note any implicit assumptions
Step 2: Add Function Contract
Start with the function-level specification:
- Preconditions (
requires): What must be true when function is called
- Postconditions (
ensures): What will be true when function returns
- Assigns clause: What memory locations may be modified
- Behavioral specification: Normal and exceptional behaviors if applicable
Step 3: Annotate Loops
For each loop, specify:
- Loop invariant: Properties that hold before and after each iteration
- Loop variant: Decreasing measure proving termination
- Loop assigns: Memory modified within the loop
Step 4: Add Assertions
Insert intermediate assertions to:
- Document algorithmic properties
- Help verification tools
- Clarify complex logic
Step 5: Define Helper Predicates
Create reusable logical definitions for:
- Common patterns (sorted arrays, valid ranges)
- Domain-specific properties
- Complex mathematical relationships
Common ACSL Constructs
Memory Validity
\valid(ptr)
\valid(ptr + (low..high))
\valid_read(ptr)
\separated(ptr1, ptr2)
Quantifiers
\forall type var; condition ==> property
\exists type var; condition && property
Logic Functions
\old(expr)
\at(expr, Label)
\result
\nothing
Integer Ranges
\forall integer i; low <= i < high ==> array[i] >= 0
Behaviors
Verification Considerations
For Frama-C WP Plugin
When generating annotations for WP verification:
- Use
assigns clauses to specify frame conditions
- Prefer
\valid over raw pointer checks
- Use
\separated for pointer disjointness
- Add
loop assigns for all loops
- Include
loop variant for termination proofs
Common Verification Patterns
Array bounds safety:
Null pointer checks:
Overflow prevention:
Output Format
Generate annotations in standard ACSL comment syntax:
- Multi-line contracts:
/*@ ... */
- Single-line assertions:
//@ assertion
- Place contracts immediately before function declarations
- Place loop annotations immediately before loop headers
- Include explanatory comments when annotations are complex
Best Practices
- Start simple: Begin with basic contracts, then refine
- Be precise: Avoid over-specification or under-specification
- Document assumptions: Make implicit assumptions explicit
- Use predicates: Factor out common patterns
- Test incrementally: Verify annotations with Frama-C as you go
- Include rationale: Add comments explaining non-obvious specifications
Example: Complete Annotated Function
int find_max_index(int *array, int n) {
int max_idx = 0;
for (int i = 1; i < n; i++) {
if (array[i] > array[max_idx]) {
max_idx = i;
}
}
return max_idx;
}
Resources
This skill includes reference materials for ACSL:
references/
acsl_reference.md - Comprehensive ACSL syntax reference
common_patterns.md - Frequently used annotation patterns
frama_c_integration.md - Tips for using with Frama-C
Load these references as needed for detailed syntax information or advanced patterns.