Skip to content

Commit

Permalink
Remove one WRITE insn for each function
Browse files Browse the repository at this point in the history
There is no need to insert two WRITEs for each function, as we can
craft the final prologue before inserting it.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Nov 12, 2024
1 parent 7002e0a commit b5be066
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
6 changes: 4 additions & 2 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size)
struct ulp_dependency *dep, *prev_dep = NULL;
struct ulp_reference *ref, *prev_ref = NULL;

DEBUG("reading live patch metadata from memory");

/* read metadata header information */
ulp->objs = NULL;

Expand Down Expand Up @@ -503,6 +501,10 @@ parse_metadata_from_mem(struct ulp_metadata *ulp, void *src, size_t size)
prev_ref = ref;
}

if (ulp->so_filename) {
DEBUG("Patch path: %s", ulp->so_filename);
}

return 0;
}

Expand Down
28 changes: 10 additions & 18 deletions lib/arch/x86_64/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,6 @@ static char ulp_prologue_endbr64[ULP_NOPS_LEN_ENDBR64] = {
};
/* clang-format on */

/** @brief Write new function address into data prologue of `old_fentry`.
*
* This function replaces the `<data>` section in prologue `old_fentry`
* with a pointer to the new function given by `manager`, which will
* replace the to be patched function.
*
* @param old_fentry Pointer to prologue of to be replaced function
* @param manager Address of new function.
*/
void
ulp_patch_addr_absolute(void *old_fentry, void *manager)
{
char *dst = (char *)old_fentry + ULP_DATA_OFFSET;
memwrite(dst, &manager, sizeof(void *));
}

/** @brief Copy the ulp proglogue layout into the function to be patched's
* prologue
*
Expand Down Expand Up @@ -166,10 +150,18 @@ ulp_patch_addr(void *old_faddr, void *new_faddr, int enable)

/* Actually patch the prologue. */
if (enable) {
ulp_patch_prologue_layout(addr, prologue, ulp_nops_len);
ulp_patch_addr_absolute(addr, new_faddr);
char patched_prologue[ULP_NOPS_LEN_ENDBR64];
memcpy(patched_prologue, prologue, ulp_nops_len);

/* Insert the function redirection jump. */
DEBUG("Patching function 0x%lx to 0x%lx", old_faddr, new_faddr);
memcpy(patched_prologue + ULP_DATA_OFFSET, &new_faddr, sizeof(void *));

/* Replace the prologue. */
ulp_patch_prologue_layout(addr, patched_prologue, ulp_nops_len);
}
else {
DEBUG("Removing patch from 0x%lx", old_faddr);
ulp_skip_prologue(old_faddr);
}

Expand Down

0 comments on commit b5be066

Please sign in to comment.