diff --git a/src/kpatch_gensrc.c b/src/kpatch_gensrc.c index 32c7afcfc50886d39d368aefb80a7ac73fe85bd5..98b947eae66d58e798dd4ff7ebe44c1f256eb6dc 100644 --- a/src/kpatch_gensrc.c +++ b/src/kpatch_gensrc.c @@ -7,6 +7,9 @@ * * 2021.09.23 - kpatch_gensrc.c: support ignoring functions which we don't need * Huawei Technologies Co., Ltd. + * + * 2022.02.11 - kpatch_gensrc.c: fix add new var bug + * China Telecom. ******************************************************************************/ #include @@ -432,6 +435,20 @@ out: /* ------------------------------------------ helpers -------------------------------------------- */ +static inline int page_shift(int n) { + int res = -1; + + while(n) { + res++; + n >>= 1; + } + + return res; +} + +#define PAGE_SIZE getpagesize() +#define PAGE_SHIFT page_shift(PAGE_SIZE) + static void change_section(struct kp_file *fout, struct section_desc *sect, int flags) { static int init_data_section = 0; @@ -448,15 +465,15 @@ static void change_section(struct kp_file *fout, struct section_desc *sect, int s = ".kpatch.text,\"ax\",@progbits"; else { s = ".kpatch.data,\"aw\",@progbits"; - if (!init_data_section && !(flags & FLAG_PUSH_SECTION)) { + if (!init_data_section) { init_data_section = 1; - align = ".p2align\t12"; + align = ".p2align"; } } fprintf(fout->f, "\t.%ssection %s\n", (flags & FLAG_PUSH_SECTION) ? "push" : "", s); if (align) - fprintf(fout->f, "\t%s\n", align); + fprintf(fout->f, "\t%s\t%d\n", align, PAGE_SHIFT); } void get_comm_args(struct kp_file *f, int l, kpstr_t *xname, int *sz, int *align) diff --git a/src/kpatch_patch.c b/src/kpatch_patch.c index d74299d8a7bbbd3dd363d90dfbea38999d22acd7..177fefb9e16bb77be039d3ca7c627147216392d0 100644 --- a/src/kpatch_patch.c +++ b/src/kpatch_patch.c @@ -28,6 +28,9 @@ * * 2021.09.23 - libcare-ctl: implement applied patch list * Huawei Technologies Co., Ltd. + * + * 2022.02.11 - libcare-ctl: fix add new var bug + * China Telecom. ******************************************************************************/ #include @@ -372,9 +375,9 @@ object_apply_patch(struct object_file *o) kp->jmp_offset = sz; kpdebug("Jump table %d bytes for %d syms at offset 0x%x\n", o->jmp_table->size, undef, kp->jmp_offset); - sz = ROUND_UP(sz + o->jmp_table->size, 4096); + sz = ROUND_UP(sz + o->jmp_table->size, PAGE_SIZE); } - sz = ROUND_UP(sz, 4096); + sz = ROUND_UP(sz, PAGE_SIZE); /* kpatch elf */ kp->elf_offset = sz; @@ -386,7 +389,7 @@ object_apply_patch(struct object_file *o) kp->user_undo = sz; sz = ROUND_UP(sz + HUNK_SIZE * o->ninfo, 16); - sz = ROUND_UP(sz, 4096); + sz = ROUND_UP(sz, PAGE_SIZE); kp->kpatch_total_mem_sz = sz; /* diff --git a/tests/new_var/Makefile b/tests/new_var/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..6dd4b693574de6c1717d7c887d650e00af2b7466 --- /dev/null +++ b/tests/new_var/Makefile @@ -0,0 +1,2 @@ + +include ../makefile.inc diff --git a/tests/new_var/desc b/tests/new_var/desc new file mode 100644 index 0000000000000000000000000000000000000000..4f8cd31e25c3d5fef9b480064c65aaf9626dfd7f --- /dev/null +++ b/tests/new_var/desc @@ -0,0 +1 @@ +patch adds a new var diff --git a/tests/new_var/new_var.c b/tests/new_var/new_var.c new file mode 100644 index 0000000000000000000000000000000000000000..3ed116ad6b8517b079de57b7d0e7b3d4e2a8d847 --- /dev/null +++ b/tests/new_var/new_var.c @@ -0,0 +1,23 @@ +#include +#include + +void print_greetings_patched(int var) +{ + printf("Hello. This is a PATCHED version\n"); + printf("Hello. \n", var); +} + +void print_greetings(void) +{ + printf("Hello. This is an UNPATCHED version\n"); +} + +int main() +{ + while (1) { + print_greetings(); + sleep(1); + } + + return 0; +} diff --git a/tests/new_var/new_var.diff b/tests/new_var/new_var.diff new file mode 100644 index 0000000000000000000000000000000000000000..c61753563f7dd812b518984bef00ecb65da738d0 --- /dev/null +++ b/tests/new_var/new_var.diff @@ -0,0 +1,15 @@ +--- ./new_var.c 2022-02-10 19:40:17.948981115 +0800 ++++ ./new_var.c 2022-02-10 20:02:38.774536002 +0800 +@@ -7,9 +7,11 @@ + printf("Hello. \n", var); + } + ++int newly_added_var = 0x20220210; + void print_greetings(void) + { +- printf("Hello. This is an UNPATCHED version\n"); ++ newly_added_var = 0x2022 << 16 | 0x2202; ++ print_greetings_patched(newly_added_var); + } + + int main()