-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add insight for 16kb page ready #517
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
chromy
wants to merge
1
commit into
main
Choose a base branch
from
chromy/2025-12-10-16kb-ready
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
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
103 changes: 103 additions & 0 deletions
103
src/launchpad/size/insights/android/sixteen_kb_page_ready.py
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,103 @@ | ||
| """16KB page ready insight for Android apps.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import lief | ||
|
|
||
| from launchpad.size.insights.insight import Insight, InsightsInput | ||
| from launchpad.size.models.insights import SixteenKBPageReadyInsightResult | ||
| from launchpad.utils.logging import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class SixteenKBPageReadyInsight(Insight[SixteenKBPageReadyInsightResult]): | ||
| """Analyze whether Android app is ready for 16KB page size devices. | ||
| This insight checks ELF section alignment in native libraries (.so files) for | ||
| arm64-v8a and x86_64 architectures. For 16KB page compatibility, all ELF sections | ||
| with alignment requirements must be aligned to at least 16KB (2^16 bytes). | ||
| Only arm64-v8a and x86_64 architectures need to be aligned for 16KB page compatibility. | ||
| """ | ||
|
|
||
| # Architectures that need 16KB alignment | ||
| TARGET_ARCHITECTURES = {"arm64-v8a", "x86_64"} | ||
|
|
||
| def generate(self, input: InsightsInput) -> SixteenKBPageReadyInsightResult | None: | ||
| """Check if the app is ready for 16KB page sizes.""" | ||
| unaligned_files: list[str] = [] | ||
|
|
||
| # Find all .so files in the relevant architectures | ||
| for file_info in input.file_analysis.files: | ||
| if not file_info.path.endswith(".so"): | ||
| continue | ||
|
|
||
| # Extract architecture from path (lib/<arch>/*.so) | ||
| path_parts = Path(file_info.path).parts | ||
| if len(path_parts) < 3 or path_parts[0] != "lib": | ||
| continue | ||
|
|
||
| arch = path_parts[1] | ||
| if arch not in self.TARGET_ARCHITECTURES: | ||
| logger.debug(f"Skipping {file_info.path} - architecture {arch} not in target architectures") | ||
| continue | ||
|
|
||
| # Check ELF alignment using the actual file path | ||
| if self._check_elf_alignment(file_info.full_path, file_info.path): | ||
| unaligned_files.append(file_info.path) | ||
| logger.debug(f"Found unaligned file: {file_info.path}") | ||
|
|
||
| is_ready = len(unaligned_files) == 0 | ||
| logger.info(f"16KB page ready analysis complete: ready={is_ready}, unaligned_files={len(unaligned_files)}") | ||
|
|
||
| return SixteenKBPageReadyInsightResult( | ||
| is_16kb_ready=is_ready, | ||
| unaligned_files=unaligned_files, | ||
| total_unaligned_files=len(unaligned_files), | ||
| ) | ||
|
|
||
| def _check_elf_alignment(self, file_path: Path, relative_path: str) -> bool: | ||
| """Check if an ELF file has proper 16KB alignment. | ||
| Args: | ||
| file_path: Absolute path to the ELF file | ||
| relative_path: Relative path for logging | ||
| Returns: | ||
| True if the file has alignment issues, False if properly aligned | ||
| """ | ||
| try: | ||
| # Parse the ELF file using LIEF | ||
| elf_binary = lief.parse(str(file_path)) | ||
| if not elf_binary: | ||
| logger.warning(f"Could not parse ELF file {relative_path}") | ||
| return False | ||
|
|
||
| # Check each section's alignment | ||
| for section in elf_binary.sections: | ||
| # LIEF sections might not have alignment attribute or it might be None | ||
| alignment = getattr(section, "alignment", None) | ||
| if alignment is None: | ||
| continue | ||
|
|
||
| # 0 or 1 means no alignment restriction | ||
| if alignment <= 1: | ||
| continue | ||
|
|
||
| # Check if alignment is >= 16KB (2^16) | ||
| if alignment >= 16 * 1024: # 16KB = 16384 bytes | ||
| continue | ||
|
|
||
| logger.warning( | ||
| f"Android .so unaligned: {relative_path} section '{getattr(section, 'name', 'unknown')}' " | ||
| f"alignment={alignment} (needs >= 16384)" | ||
| ) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Error analyzing ELF file {relative_path}: {e}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| # Don't consider it unaligned if we can't parse it | ||
| return False | ||
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 |
|---|---|---|
|
|
@@ -243,3 +243,17 @@ class MultipleNativeLibraryArchInsightResult(FilesInsightResult): | |
| """ | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class SixteenKBPageReadyInsightResult(BaseModel): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this extend from |
||
| """Results from 16KB page ready analysis. | ||
|
|
||
| Indicates whether the Android app is ready for 16KB page size devices. | ||
| This checks ELF section alignment in native libraries for arm64-v8a and x86_64 architectures. | ||
| """ | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| is_16kb_ready: bool = Field(..., description="Whether the app is ready for 16KB page sizes") | ||
| unaligned_files: List[str] = Field(default_factory=list, description="List of files with alignment issues") | ||
| total_unaligned_files: int = Field(default=0, ge=0, description="Total number of files with alignment issues") | ||
Oops, something went wrong.
Oops, something went wrong.
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.
nit: using a stable log message +
extraswill make this a bit easier to search