Skip to content

Check target function size to avoid undefined behavior #95

Description

@mazong1123

The size of target function to fake could be less than the patching size. Patching such function could cause undefined behavior at runtime.

One option is checking the size of the target function before performing patching. Panic if the function size is smaller than the required patching size.

For example, aarch64 has a hard coded size. This could cause undefined behavior if the function to fake is smaller.

fn replace_function_with_other_function(
    src: FuncPtrInternal,
    target: FuncPtrInternal,
) -> PatchGuard {
    const PATCH_SIZE: usize = 12;
    const JIT_SIZE: usize = 20;


    let original_bytes = unsafe { read_bytes(src.as_ptr() as *mut u8, PATCH_SIZE) };
    let jit_memory = allocate_jit_memory(&src, JIT_SIZE);
    generate_will_execute_jit_code_abs(jit_memory, target.as_ptr());


    apply_branch_patch(src, jit_memory, JIT_SIZE, &original_bytes)
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions