Verification API¶
Formal verification components using Z3 theorem prover.
Bounds Prover¶
multigen.frontend.verifiers.bounds_prover
¶
Memory Safety and Bounds Verification.
This module provides formal verification of memory safety properties, including bounds checking, buffer overflow prevention, and pointer safety.
BoundsProver
¶
Formal verification of memory bounds and safety properties.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
__init__(theorem_prover=None)
¶
Initialize the bounds prover.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theorem_prover
|
Optional[TheoremProver]
|
Z3 theorem prover instance |
None
|
Source code in src/multigen/frontend/verifiers/bounds_prover.py
verify_memory_safety(context)
¶
Verify memory safety for a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
Analysis context with AST and metadata |
required |
Returns:
| Type | Description |
|---|---|
MemorySafetyProof
|
MemorySafetyProof with verification results |
Source code in src/multigen/frontend/verifiers/bounds_prover.py
MemoryAccess
dataclass
¶
Represents a memory access operation.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
MemoryOperationExtractor
¶
Bases: NodeVisitor
Extract memory operations and regions from Python AST.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
visit_Assign(node)
¶
Extract memory region declarations.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
visit_FunctionDef(node)
¶
visit_Subscript(node)
¶
Extract memory access operations.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
MemoryRegion
dataclass
¶
Represents a memory region with bounds and properties.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
MemorySafetyProof
dataclass
¶
Result of memory safety verification.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
summary
property
¶
Human-readable summary of the proof.
MemorySafetyType
¶
Bases: Enum
Types of memory safety properties.
Source code in src/multigen/frontend/verifiers/bounds_prover.py
Theorem Prover¶
multigen.frontend.verifiers.theorem_prover
¶
Z3 Theorem Prover Integration for Formal Verification.
This module provides integration with the Z3 theorem prover for formal verification of code properties, memory safety, and algorithm correctness.
ProofProperty
dataclass
¶
A property to be verified.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
ProofResult
dataclass
¶
Result of a formal verification attempt.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
ProofStatus
¶
PropertyType
¶
Bases: Enum
Types of properties that can be verified.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
SafetyPropertyExtractor
¶
Bases: NodeVisitor
AST visitor to extract safety properties from Python code.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
visit_BinOp(node)
¶
Extract arithmetic operations for overflow checking.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
visit_For(node)
¶
Extract loop information for termination analysis.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
visit_Subscript(node)
¶
Extract array access patterns.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
TheoremProver
¶
Z3-based theorem prover for formal verification.
Source code in src/multigen/frontend/verifiers/theorem_prover.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |
__init__(timeout=30000)
¶
Initialize the theorem prover.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
Timeout in milliseconds for Z3 solver |
30000
|
Source code in src/multigen/frontend/verifiers/theorem_prover.py
analyze_function_safety(context)
¶
Analyze a function for various safety properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
Analysis context with AST and metadata |
required |
Returns:
| Type | Description |
|---|---|
list[ProofResult]
|
List of verification results for safety properties |
Source code in src/multigen/frontend/verifiers/theorem_prover.py
create_bounds_check_property(array_name, index_expr, array_size, context=None)
¶
Create a bounds checking property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_name
|
str
|
Name of the array |
required |
index_expr
|
str
|
Index expression |
required |
array_size
|
int | str
|
Size of the array |
required |
context
|
dict[str, Any] | None
|
Additional context for the property |
None
|
Returns:
| Type | Description |
|---|---|
ProofProperty
|
ProofProperty for bounds checking |
Source code in src/multigen/frontend/verifiers/theorem_prover.py
create_overflow_safety_property(operation, operands, result_type='int', context=None)
¶
Create an overflow safety property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
str
|
Type of operation (add, mul, sub, etc.) |
required |
operands
|
list[str]
|
List of operand names |
required |
result_type
|
str
|
Result type for overflow bounds |
'int'
|
context
|
dict[str, Any] | None
|
Additional context |
None
|
Returns:
| Type | Description |
|---|---|
ProofProperty
|
ProofProperty for overflow safety |
Source code in src/multigen/frontend/verifiers/theorem_prover.py
verify_multiple_properties(properties)
¶
Verify multiple properties efficiently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
properties
|
list[ProofProperty]
|
List of properties to verify |
required |
Returns:
| Type | Description |
|---|---|
list[ProofResult]
|
List of ProofResults |
Source code in src/multigen/frontend/verifiers/theorem_prover.py
verify_property(prop)
¶
Verify a single property using Z3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop
|
ProofProperty
|
Property to verify |
required |
Returns:
| Type | Description |
|---|---|
ProofResult
|
ProofResult with verification outcome |
Source code in src/multigen/frontend/verifiers/theorem_prover.py
Correctness Prover¶
multigen.frontend.verifiers.correctness_prover
¶
Algorithm Correctness Verification.
This module provides formal verification of algorithm correctness using preconditions, postconditions, loop invariants, and functional specifications.
AlgorithmProof
dataclass
¶
Result of algorithm correctness verification.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
summary
property
¶
Human-readable summary of the proof.
AlgorithmStructureExtractor
¶
Bases: NodeVisitor
Extract algorithm structure for correctness verification.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
get_loop_info(line_no)
¶
Get loop information for a specific line number.
visit_Assert(node)
¶
Extract assertion statements.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
visit_Call(node)
¶
Extract function calls (including recursive calls).
Source code in src/multigen/frontend/verifiers/correctness_prover.py
visit_For(node)
¶
Extract for loop information.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
visit_FunctionDef(node)
¶
visit_Name(node)
¶
visit_While(node)
¶
Extract while loop information.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
CorrectnessPropertyType
¶
Bases: Enum
Types of correctness properties.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
CorrectnessProver
¶
Formal verification of algorithm correctness.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
__init__(theorem_prover=None)
¶
Initialize the correctness prover.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theorem_prover
|
Optional[TheoremProver]
|
Z3 theorem prover instance |
None
|
Source code in src/multigen/frontend/verifiers/correctness_prover.py
verify_algorithm_correctness(context, specification=None)
¶
Verify algorithm correctness using formal methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
Analysis context with AST and metadata |
required |
specification
|
Optional[FormalSpecification]
|
Optional formal specification |
None
|
Returns:
| Type | Description |
|---|---|
AlgorithmProof
|
AlgorithmProof with verification results |
Source code in src/multigen/frontend/verifiers/correctness_prover.py
FormalSpecification
dataclass
¶
Formal specification of a function or algorithm.
Source code in src/multigen/frontend/verifiers/correctness_prover.py
Performance Analyzer¶
multigen.frontend.verifiers.performance_analyzer
¶
Performance Bound Analysis and Verification.
This module provides formal analysis of algorithm performance bounds, complexity verification, and resource usage guarantees.
AlgorithmStructureExtractor
¶
Bases: NodeVisitor
Extract algorithm structure for performance analysis.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_Call(node)
¶
Visit a function call node.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_For(node)
¶
Visit a for loop node.
visit_FunctionDef(node)
¶
ComplexityClass
¶
Bases: Enum
Algorithm complexity classes.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
ComplexityPatternAnalyzer
¶
Bases: NodeVisitor
Analyze complexity patterns in code.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_Assign(node)
¶
Visit an assignment node and track array allocations.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_Call(node)
¶
Visit a function call node and track function calls.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_For(node)
¶
Visit a for loop node and track nesting depth.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_Subscript(node)
¶
Visit a subscript node and track array accesses.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
visit_While(node)
¶
Visit a while loop node and track nesting depth.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
PerformanceAnalysisResult
dataclass
¶
Result of performance bound analysis.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
PerformanceAnalyzer
¶
Formal analysis of algorithm performance bounds.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
__init__(theorem_prover=None)
¶
Initialize the performance analyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theorem_prover
|
Optional[TheoremProver]
|
Z3 theorem prover instance |
None
|
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
analyze_performance_bounds(context)
¶
Analyze performance bounds for a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
AnalysisContext
|
Analysis context with AST and metadata |
required |
Returns:
| Type | Description |
|---|---|
PerformanceAnalysisResult
|
PerformanceAnalysisResult with proven bounds |
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
PerformanceBound
dataclass
¶
Represents a performance bound for an algorithm.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
__post_init__()
¶
Initialize constants and conditions dictionaries if not provided.
ResourceType
¶
Bases: Enum
Types of computational resources.
Source code in src/multigen/frontend/verifiers/performance_analyzer.py
Z3 Formula Generator¶
multigen.frontend.verifiers.z3_formula_generator
¶
Z3 Formula Generation from Python AST.
Converts Python AST nodes into Z3 formulas for formal verification.
Z3FormulaGenerator
¶
Generates Z3 formulas from Python AST nodes.
Source code in src/multigen/frontend/verifiers/z3_formula_generator.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
__init__()
¶
Initialize the formula generator.
add_constraint(constraint)
¶
Add a constraint to the formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint
|
Any
|
Z3 constraint |
required |
clear_constraints()
¶
clear_variables()
¶
generate_array_bounds_formula(array_name, index_expr, array_len_name)
¶
Generate Z3 formula for array bounds safety.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_name
|
str
|
Name of the array |
required |
index_expr
|
expr
|
AST expression for the index |
required |
array_len_name
|
str
|
Name of variable holding array length |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Z3 formula asserting bounds safety |
Source code in src/multigen/frontend/verifiers/z3_formula_generator.py
generate_loop_bounds_formula(loop_var, start, end_expr, array_len_name)
¶
Generate Z3 formula for loop bounds safety.
For: for i in range(start, end) Prove: all accesses arr[i] are safe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop_var
|
str
|
Loop variable name (e.g., "i") |
required |
start
|
int
|
Loop start value |
required |
end_expr
|
expr
|
AST expression for loop end |
required |
array_len_name
|
str
|
Name of variable holding array length |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Z3 formula asserting all loop iterations are safe |
Source code in src/multigen/frontend/verifiers/z3_formula_generator.py
generate_preconditions(array_len_name, min_len_expr)
¶
Generate preconditions for array safety.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array_len_name
|
str
|
Name of variable holding array length |
required |
min_len_expr
|
expr
|
AST expression for minimum required length |
required |
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Z3 formula for preconditions |
Source code in src/multigen/frontend/verifiers/z3_formula_generator.py
get_all_constraints()
¶
Get all accumulated constraints.
Returns:
| Type | Description |
|---|---|
list[Any]
|
List of Z3 constraints |
get_or_create_var(name, var_type='int')
¶
Get or create a Z3 variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Variable name |
required |
var_type
|
str
|
Variable type ("int", "bool", etc.) |
'int'
|
Returns:
| Type | Description |
|---|---|
Any
|
Z3 variable or None if Z3 is not available |
Source code in src/multigen/frontend/verifiers/z3_formula_generator.py
Example Usage¶
Verifying array bounds:
from multigen.frontend.verifiers.bounds_prover import BoundsProver
import ast
code = '''
def sum_array(arr: list[int]) -> int:
total: int = 0
for i in range(len(arr)):
total += arr[i]
return total
'''
tree = ast.parse(code)
prover = BoundsProver()
result = prover.verify_memory_safety(tree.body[0])
if result.success:
print("Array access is SAFE")
else:
print("Verification failed:", result.errors)
Using with pipeline:
from multigen.pipeline import MultiGenPipeline, PipelineConfig
config = PipelineConfig(
target_language="c",
enable_formal_verification=True,
strict_verification=True,
)
pipeline = MultiGenPipeline(config=config)
result = pipeline.convert("example.py")
Z3 Availability¶
Check if Z3 is available:
from multigen.frontend.verifiers.bounds_prover import Z3_AVAILABLE
if Z3_AVAILABLE:
print("Z3 is available")
else:
print("Install with: pip install multigen[z3]")
Verification is automatically skipped if Z3 is not installed.
See Also¶
- Verification Guide -- User guide for formal verification
- Frontend API -- Frontend analysis APIs