-
Notifications
You must be signed in to change notification settings - Fork 16
Add auto-generation script for syscall mappings #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
celinehoang177
wants to merge
3
commits into
main
Choose a base branch
from
auto-generate-syscall-mapping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #!/usr/bin/env python3 | ||
| # Auto-generates syscall mapping constants for lind-wasm. | ||
| # | ||
| # Reads syscall definitions from glibc's lind_syscall_num.h and generates: | ||
| # - src/sysdefs/src/constants/syscall_const.rs (full set of syscalls) | ||
| # | ||
| # This script should be run before building lind-boot and glibc. | ||
| # | ||
| # Source of truth: src/glibc/lind_syscall/lind_syscall_num.h | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| def parse_c_header(header_path: str) -> dict[str, int]: | ||
| """Parse syscall constants from glibc's lind_syscall_num.h header file.""" | ||
| syscalls = {} | ||
|
|
||
| try: | ||
| with open(header_path) as f: | ||
| content = f.read() | ||
| except FileNotFoundError: | ||
| print(f"Error: Could not find {header_path}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| # Match #define SYSCALL_NAME number | ||
| pattern = r'#define\s+(\w+)\s+(\d+)' | ||
|
|
||
| for match in re.finditer(pattern, content): | ||
| name, number = match.groups() | ||
| syscalls[name] = int(number) | ||
|
|
||
| if not syscalls: | ||
| print(f"Error: Could not parse any syscalls from {header_path}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| return syscalls | ||
|
|
||
|
|
||
| def generate_rust_constants(syscalls: dict[str, int]) -> str: | ||
| """Generate Rust constants for sysdefs/constants/syscall_const.rs.""" | ||
| lines = [ | ||
| "//! Syscall number constants for the Lind platform.", | ||
| "//!", | ||
| "//! Source of truth: Linux x86_64 syscall table", | ||
| "//! https://github.com/torvalds/linux/blob/v6.16-rc1/arch/x86/entry/syscalls/syscall_64.tbl", | ||
| "//! (Historical overview: https://filippo.io/linux-syscall-table/)", | ||
| "//!", | ||
| "//! Keep these in sync with glibc's lind_syscall_num.h and RawPOSIX dispatcher.", | ||
| "", | ||
| ] | ||
|
|
||
| # Sort by syscall number for readability | ||
| sorted_syscalls = sorted(syscalls.items(), key=lambda x: x[1]) | ||
|
|
||
| for name, number in sorted_syscalls: | ||
| lines.append(f"pub const {name}: i32 = {number};") | ||
|
|
||
| lines.append("") | ||
| return "\n".join(lines) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| def write_file(path: str, content: str) -> None: | ||
| """Write content to file, creating directories if needed.""" | ||
| Path(path).parent.mkdir(parents=True, exist_ok=True) | ||
| with open(path, 'w') as f: | ||
| f.write(content) | ||
| print(f"Generated: {path}") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Generate syscall mapping constants from glibc header.""" | ||
| # Find workspace root (should be run from lind-wasm directory) | ||
| workspace_root = Path(__file__).parent.parent | ||
|
|
||
| c_header = workspace_root / "src/glibc/lind_syscall/lind_syscall_num.h" | ||
| rust_sysdefs_out = workspace_root / "src/sysdefs/src/constants/syscall_const.rs" | ||
|
|
||
| print("Parsing syscall definitions...") | ||
| syscalls = parse_c_header(str(c_header)) | ||
| print(f"Found {len(syscalls)} syscall definitions") | ||
|
|
||
| # Generate Rust constants for sysdefs | ||
| print("\nGenerating sysdefs constants...") | ||
| rust_content = generate_rust_constants(syscalls) | ||
| write_file(str(rust_sysdefs_out), rust_content) | ||
|
|
||
| print("Done!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems not including lind specific syscall num?