Runtime Code Modification via Trampoline Mediation
Find Innovative SolutionsGenerate Solutions
Solution Overview
Problem
Existing techniques for modifying mobile application user interfaces at runtime often result in memory access errors, stack violations, and system crashes, especially when just-in-time (JIT) translation occurs in environments like the Android Runtime (ART), due to the disruption of swizzling methods and multithreading issues.
Innovation Solution
The approach involves creating a copy of a code block, modifying its flag to mark it as native, altering metadata to identify alternative code, and redirecting the pointer to a trampoline function, which checks for modifications and invokes the alternative code, thereby avoiding memory errors and ensuring thread safety.
Engineering Contradictions & Design Principles
Engineering Contradiction Analysis
1Adaptability or versatility
If swizzling techniques are used to modify application code at runtime, then the ability to dynamically change user interface and functionality is improved, but memory access errors and system crashes occur due to disruption of JIT translation and multithreading issues
Solution Approach 1:
The patent introduces a trampoline function as an intermediary between the modified code pointer and the actual alternative code. This trampoline acts as a mediator that safely redirects execution flow without directly disrupting the original code structure or JIT translation processes, thereby maintaining system stability while enabling runtime modifications through swizzling techniques
Solution Approach 2:
The patent segments the code modification process into distinct components: the original code block, a copy of the code block with modified metadata, and a trampoline function. This segmentation allows the modification to be applied incrementally and safely, isolating the changes from the main execution flow and preventing system-wide disruptions
2Adaptability or versatility
If pointer modification is used to redirect code execution, then dynamic code replacement is enabled, but memory access errors occur due to disrupted JIT translation
Solution Approach 1:
The patent performs preliminary actions by creating a copy of the code block and modifying its metadata (flag and alternative code identifier) before the actual pointer redirection occurs. This preliminary preparation ensures that when the pointer is modified to point to the trampoline, the target code structure is already ready and valid, preventing memory access errors that would occur with direct pointer modification
Solution Approach 2:
The trampoline function serves as an intermediary that bridges the original code pointer and the alternative code. Instead of directly modifying the pointer to point to alternative code (which would disrupt JIT translation), the pointer is modified to point to the trampoline, which then safely redirects execution to the alternative code, eliminating memory access errors
3Device complexity
If direct code substitution is performed without copies, then modification simplicity is maintained, but thread safety is compromised due to multithreading issues
Solution Approach 1:
The patent creates a copy of the code block rather than modifying the original directly. This copying approach allows the original code to remain intact for other threads while the copy can be safely modified with alternative metadata. The pointer is then redirected to this safe copy through the trampoline, ensuring thread safety without significantly increasing modification process complexity
Solution Approach 2:
The patent segments the code into original and copied versions, allowing concurrent access by multiple threads. The original code block remains unchanged and accessible to threads that haven't triggered the modification, while the copied block handles the modified execution path, thereby maintaining thread safety with minimal added complexity
Data Source
AI summary
Techniques to facilitate modifications to program execution in an application at runtime are disclosed herein. In at least one implementation, a copy of a code block associated with code is created. A flag associated with the copy of the code block is modified to mark the copy of the code block as having a native code version of the code. Metadata associated with the copy of the code block is modified to identify alternative code to run instead of the native code version of the code. A pointer associated with the code block is modified to point to a trampoline function. The trampoline function checks whether the code block is associated with a modification, and when the code block is associated with the modification, then the trampoline function calls the copy of the code block to invoke the alternative code by virtue of the flag and the metadata.


