|
Download docs/ARCHITECTURE.md from Snapkitty/forge-code: direct link, hf CLI and curl.
- Browser
- Download file 19.7 kB
-
https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/ARCHITECTURE.md
- Command line
-
hf download hf://datasets/Snapkitty/forge-code/docs/ARCHITECTURE.md
-
curl -L -o ARCHITECTURE.md https://huggingface.co/datasets/Snapkitty/forge-code/resolve/main/docs/ARCHITECTURE.md
19.7 kB
| # FORGE CODE Architecture | |
| **Reverse-Engineering and Benchmarking Laboratory for Elixir Programs** | |
| ## Overview | |
| FORGE CODE is designed as a modular, extensible framework for analyzing Elixir programs through their complete transformation pipeline. The system is organized around several core principles: | |
| 1. **Complete Pipeline Coverage**: From source code to BEAM binary | |
| 2. **Provenance Tracking**: Cryptographic chain of custody for all artifacts | |
| 3. **Deterministic Analysis**: Reproducible results across runs | |
| 4. **Safety**: Sandboxed execution and fail-closed design | |
| ## System Architecture | |
| ``` | |
| ┌─────────────────────────────────────────────────────────────────┐ | |
| │ FORGE CODE │ | |
| ├─────────────────────────────────────────────────────────────────┤ | |
| │ │ | |
| │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │ | |
| │ │ Compiler │ │ BEAM │ │ Binary │ │ | |
| │ │ Pipeline │───▶│ Inspector │───▶│ Parser │ │ | |
| │ └─────────────────┘ └─────────────────┘ └─────────────┘ │ | |
| │ │ │ │ │ | |
| │ ▼ ▼ ▼ │ | |
| │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │ | |
| │ │ Elixir AST │ │ Function │ │ Binary │ │ | |
| │ │ Extraction │ │ Analysis │ │ Fields │ │ | |
| │ └─────────────────┘ └─────────────────┘ └─────────────┘ │ | |
| │ │ │ │ │ | |
| │ ▼ ▼ ▼ │ | |
| │ ┌─────────────────────────────────────────────────────────────┐ │ | |
| │ │ Binary IR │ │ | |
| │ │ Normalized Intermediate Representation │ │ | |
| │ └─────────────────────────────────────────────────────────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────────────────────────────────────────────────┐ │ | |
| │ │ Semantic Analysis │ │ | |
| │ │ Pattern Recognition │ │ | |
| │ └─────────────────────────────────────────────────────────────┘ │ | |
| │ │ │ | |
| │ ▼ │ | |
| │ ┌─────────────────────────────────────────────────────────────┐ │ | |
| │ │ Provenance Chain │ │ | |
| │ │ SOURCE -> AST -> FORMS -> BEAM -> IR │ │ | |
| │ └─────────────────────────────────────────────────────────────┘ │ | |
| │ │ │ | |
| │ ┌─────────────────▼─────────────────┐ │ | |
| │ │ │ │ | |
| │ ┌─────────▼─────────┐ ┌─────────▼─────────┐ │ | |
| │ │ Tournament │ │ CLI │ │ | |
| │ │ Orchestrator │ │ Interface │ │ | |
| │ │ │ │ │ │ | |
| │ │ - Contestants │ │ - Compile │ │ | |
| │ │ - Challenges │ │ - Inspect │ │ | |
| │ │ - Scoring │ │ - Disassemble │ │ | |
| │ │ - Judging │ │ - Hash │ │ | |
| │ │ - Runner │ │ - Tournament │ │ | |
| │ └─────────────────┘ └───────────────────┘ │ | |
| │ │ | |
| └─────────────────────────────────────────────────────────────────┘ | |
| ``` | |
| ## Module Hierarchy | |
| ``` | |
| forge/ | |
| ├── application.ex # OTP Application entry point | |
| │ | |
| ├── compiler.ex # Source code compilation pipeline | |
| ├── elixir_ast.ex # AST extraction and analysis | |
| ├── erlang_forms.ex # Erlang forms capture | |
| │ | |
| ├── beam/ | |
| │ ├── beam.ex # BEAM file inspection | |
| │ ├── function.ex # Function analysis | |
| │ └── metadata.ex # Metadata extraction | |
| │ | |
| ├── binary/ | |
| │ ├── parser.ex # Binary parsing with offset preservation | |
| │ ├── field.ex # Binary field representation | |
| │ └── sections.ex # Section handling | |
| │ | |
| ├── binary_ir.ex # Normalized Binary Intermediate Representation | |
| │ | |
| ├── semantics.ex # Semantic reconstruction from bytecode | |
| │ | |
| ├── hash.ex # SHA-256 hashing utilities | |
| ├── provenance.ex # Provenance chain tracking | |
| │ | |
| ├── tournament/ | |
| │ ├── tournament.ex # Tournament orchestrator | |
| │ ├── runner.ex # Contestant execution | |
| │ ├── score.ex # Scoring engine | |
| │ ├── judge.ex # Judge output and comparison | |
| │ └── challenge.ex # Challenge definitions | |
| │ | |
| └── cli/ | |
| └── cli.ex # Command-line interface | |
| ``` | |
| ## Data Flow | |
| ### Compilation Pipeline | |
| ``` | |
| Elixir Source | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Compiler.compile/2 │ ← Main entry point | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ compile_to_ast/2 │ → AST representation | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ compile_to_quoted/2 │ → Quoted representation | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ compile_to_forms/2 │ → Erlang forms | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ compile_to_beam/2 │ → BEAM binary + metadata | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| Compile Result | |
| ``` | |
| ### Analysis Pipeline | |
| ``` | |
| BEAM File | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Beam.read/1 │ → Metadata extraction | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ BinaryIR.from_beam/2│ → Normalized IR | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Semantics.infer/1 │ → Semantic operations | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| Analysis Result | |
| ``` | |
| ### Tournament Pipeline | |
| ``` | |
| Tournament Definition | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Tournament.run/1 │ ← Load and validate | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ compile_contestants │ → Compile all submissions | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ run_challenge/3 │ → Execute for each challenge | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Judge.judge/5 │ → Compare outputs | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| ┌─────────────────────┐ | |
| │ Score.calculate_all/2│ → Calculate scores | |
| └─────────────────────┘ | |
| │ | |
| ▼ | |
| Tournament Result | |
| ``` | |
| ## Provenance Chain | |
| Every artifact in the system is tracked through a cryptographic provenance chain: | |
| ``` | |
| ┌─────────────────┐ SHA-256 ┌─────────────────┐ | |
| │ Source Code │ ───────────────▶ │ AST │ | |
| └─────────────────┘ └─────────────────┘ | |
| │ │ | |
| │ SHA-256 │ SHA-256 | |
| ▼ ▼ | |
| ┌─────────────────┐ ┌─────────────────┐ | |
| │ Artifact │ │ Quoted Form │ | |
| │ Hash │ └─────────────────┘ | |
| └─────────────────┘ │ | |
| │ SHA-256 | |
| ▼ ▼ | |
| ┌─────────────────┐ ┌─────────────────┐ | |
| │ Provenance │ ◀─────────────── │ Erlang Forms │ | |
| │ Chain Start │ └─────────────────┘ | |
| └─────────────────┘ │ | |
| │ SHA-256 | |
| ▼ ▼ | |
| ┌─────────────────┐ | |
| │ BEAM File │ | |
| └─────────────────┘ | |
| │ | |
| SHA-256 ▼ | |
| ┌─────────────────┐ | |
| │ Binary IR │ | |
| └─────────────────┘ | |
| │ | |
| SHA-256 ▼ | |
| ┌─────────────────┐ | |
| │ Tournament │ | |
| │ Result │ | |
| └─────────────────┘ | |
| ``` | |
| ## State Representations | |
| FORGE CODE distinguishes between four levels of data representation: | |
| ### 1. RAW State | |
| - **Definition**: Direct binary bytes, uninterpreted | |
| - **Use Case**: Original file data, unknown sections | |
| - **Example**: `<<...>>` (binary data) | |
| ### 2. DECODED State | |
| - **Definition**: Structurally parsed with known formats | |
| - **Use Case**: Known BEAM sections (atoms, literals, functions) | |
| - **Example**: `%{atoms: [:foo, :bar], literals: [1, 2, 3]}` | |
| ### 3. NORMALIZED State | |
| - **Definition**: Implementation-independent representation | |
| - **Use Case**: Stable comparison across OTP versions | |
| - **Example**: Normalized instruction sequences | |
| ### 4. SEMANTIC State | |
| - **Definition**: Inferred higher-level meaning | |
| - **Use Case**: Pattern recognition, function boundaries | |
| - **Example**: `:pattern_match`, `:function_call` | |
| **Important**: The system NEVER represents inferred semantics as if they were directly present in the binary. All semantic reconstructions are clearly marked with confidence levels and provenance. | |
| ## Error Handling | |
| FORGE CODE uses structured error types: | |
| ```elixir | |
| # Compiler errors | |
| :compiler_failure | |
| :ast_error | |
| :quoted_error | |
| :forms_error | |
| :beam_compilation_error | |
| # BEAM analysis errors | |
| :unsupported_beam | |
| :unknown_opcode | |
| :invalid_section | |
| :invalid_offset | |
| :malformed_binary | |
| # Execution errors | |
| :execution_timeout | |
| :memory_limit | |
| :challenge_failure | |
| # System errors | |
| :file_not_found | |
| :permission_error | |
| :configuration_error | |
| ``` | |
| All errors include: | |
| - Error type (atom) | |
| - Human-readable message | |
| - Original context (file, line, offset) | |
| - Stack trace (in debug mode) | |
| ## Safety Principles | |
| ### 1. Parsing ≠ Execution | |
| - BEAM files are parsed using `:beam_lib` and custom parsers | |
| - Parsing NEVER requires executing the code | |
| - Sandboxed execution is available but optional | |
| ### 2. Fail-Closed Design | |
| - Unknown opcodes: `:unknown_opcode` error | |
| - Unknown sections: `:invalid_section` error or skip (configurable) | |
| - Unknown formats: `:unsupported_beam` error | |
| - Never silently interpret unknown structures | |
| ### 3. Resource Limits | |
| - Execution timeouts | |
| - Memory limits | |
| - Recursion depth limits | |
| - Process isolation (when sandboxed) | |
| ### 4. Provenance Integrity | |
| - Every artifact is hashed | |
| - Every transformation is logged | |
| - Chain of custody is maintained | |
| - Tampering is detectable | |
| ## Version Compatibility | |
| BEAM internals vary between OTP releases: | |
| | OTP Version | BEAM Format | Supported | Notes | | |
| |-------------|--------------|-----------|-------| | |
| | OTP 20 | A | Partial | Old format | | |
| | OTP 21 | B | ✅ Yes | Common | | |
| | OTP 22 | B | ✅ Yes | Common | | |
| | OTP 23 | C | ✅ Yes | Common | | |
| | OTP 24 | C | ✅ Yes | Current | | |
| | OTP 25 | C | ✅ Yes | Current | | |
| | OTP 26 | C | ✅ Yes | Current | | |
| The system: | |
| 1. Records exact OTP version for each artifact | |
| 2. Versions the binary parser | |
| 3. Maintains fixtures per OTP family | |
| 4. Explicitly rejects unsupported formats | |
| ## Performance Considerations | |
| ### Time Complexity | |
| - **Compilation**: O(n) where n = source size | |
| - **BEAM Parsing**: O(n) where n = file size | |
| - **Semantic Analysis**: O(n * m) where n = instructions, m = patterns | |
| - **Tournament Execution**: O(c * ch * e) where c = contestants, ch = challenges, e = executions | |
| ### Memory Usage | |
| - **BEAM Files**: Typically small (few KB for simple modules) | |
| - **Binary IR**: ~2-3x the size of BEAM file | |
| - **Semantic Analysis**: Additional overhead for pattern matching | |
| - **Tournament**: Linear in number of contestants and challenges | |
| ### Bottlenecks | |
| 1. **Compilation**: Elixir compilation can be slow for large modules | |
| 2. **BEAM Parsing**: `:beam_lib.read/1` is generally fast | |
| 3. **Semantic Analysis**: Pattern matching can be expensive | |
| 4. **Tournament Execution**: Serial execution of contestants | |
| ## Extensibility | |
| ### Adding New Analyses | |
| 1. Create a new module in `lib/forge/analysis/` | |
| 2. Implement the analysis function | |
| 3. Register it in the `Forge.Semantics` module | |
| 4. Add tests in `test/analysis/` | |
| ### Adding New BEAM Versions | |
| 1. Add version detection in `Forge.Binary.Parser` | |
| 2. Add version-specific parsing logic | |
| 3. Add fixtures for the new version | |
| 4. Update documentation | |
| ### Adding New Tournament Metrics | |
| 1. Add metric to `Forge.Score` module | |
| 2. Add weight to default weights | |
| 3. Add extraction logic in `Forge.Beam` or `Forge.BinaryIR` | |
| 4. Update documentation | |
| ## Directory Structure | |
| ``` | |
| forge-code/ | |
| ├── README.md # Main documentation | |
| ├── LICENSE # MIT License | |
| ├── mix.exs # Project configuration | |
| │ | |
| ├── lib/ | |
| │ └── forge/ | |
| │ ├── application.ex # OTP Application | |
| │ ├── compiler.ex # Compilation pipeline | |
| │ ├── elixir_ast.ex # AST extraction | |
| │ ├── erlang_forms.ex # Erlang forms | |
| │ ├── beam/ # BEAM analysis | |
| │ │ ├── beam.ex | |
| │ │ ├── function.ex | |
| │ │ └── metadata.ex | |
| │ ├── binary/ # Binary parsing | |
| │ │ ├── parser.ex | |
| │ │ ├── field.ex | |
| │ │ └── sections.ex | |
| │ ├── binary_ir.ex # Normalized IR | |
| │ ├── semantics.ex # Semantic reconstruction | |
| │ ├── hash.ex # Hashing utilities | |
| │ ├── provenance.ex # Provenance tracking | |
| │ ├── tournament/ # Tournament system | |
| │ │ ├── tournament.ex | |
| │ │ ├── runner.ex | |
| │ │ ├── score.ex | |
| │ │ ├── judge.ex | |
| │ │ └── challenge.ex | |
| │ └── cli/ # CLI | |
| │ └── cli.ex | |
| │ | |
| ├── config/ | |
| │ ├── config.exs # Configuration | |
| │ └── tournament.exs # Tournament defaults | |
| │ | |
| ├── priv/ | |
| │ └── fixtures/ # Test fixtures | |
| │ | |
| ├── test/ | |
| │ └── *test.exs # Unit tests | |
| │ | |
| ├── examples/ | |
| │ ├── challenges/ # Sample challenges | |
| │ └── contestants/ # Sample submissions | |
| │ | |
| ├── docs/ # Documentation | |
| │ ├── ARCHITECTURE.md | |
| │ ├── BEAM_REVERSE_ENGINEERING.md | |
| │ ├── BINARY_IR.md | |
| │ ├── TOURNAMENT.md | |
| │ ├── SCORING.md | |
| │ ├── PROVENANCE.md | |
| │ ├── SECURITY.md | |
| │ ├── SUPPORTED_OTP.md | |
| │ └── CLI.md | |
| │ | |
| └── scripts/ | |
| └── benchmark.sh | |
| ``` | |