From 1ef194d3942aba4c06d2f7d77fb572fc2284affe Mon Sep 17 00:00:00 2001 From: lifeng 71117973 Date: Fri, 11 Feb 2022 13:51:50 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E6=97=B6=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E8=AE=BF=E9=97=AE=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kpatch_gensrc.c | 23 ++++++++++++++++++++--- src/kpatch_patch.c | 9 ++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/kpatch_gensrc.c b/src/kpatch_gensrc.c index 32c7afc..98b947e 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 d74299d..177fefb 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; /* -- Gitee From 1d04fad042678d8c82aa8dc5f851ec67e47f3f03 Mon Sep 17 00:00:00 2001 From: ctyunsystem Date: Fri, 11 Feb 2022 13:51:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E6=97=B6=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E8=AE=BF=E9=97=AE=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kpatch_gensrc.c | 23 ++++++++++++++++++++--- src/kpatch_patch.c | 9 ++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/kpatch_gensrc.c b/src/kpatch_gensrc.c index 32c7afc..98b947e 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 d74299d..177fefb 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; /* -- Gitee From 4449bc61d4268fe68aed93250f14d7821ab559e9 Mon Sep 17 00:00:00 2001 From: lifeng 71117973 Date: Fri, 11 Feb 2022 13:58:13 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E6=97=B6=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E8=AE=BF=E9=97=AE=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/new_var/Makefile | 2 ++ tests/new_var/desc | 1 + tests/new_var/new_var.c | 23 +++++++++++++++++++++++ tests/new_var/new_var.diff | 15 +++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 tests/new_var/Makefile create mode 100644 tests/new_var/desc create mode 100644 tests/new_var/new_var.c create mode 100644 tests/new_var/new_var.diff diff --git a/tests/new_var/Makefile b/tests/new_var/Makefile new file mode 100644 index 0000000..6dd4b69 --- /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 0000000..4f8cd31 --- /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 0000000..3ed116a --- /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 0000000..c617535 --- /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() -- Gitee From 248f97935287853e71afeb5174792a8b57d29a2b Mon Sep 17 00:00:00 2001 From: ctyunsystem Date: Fri, 11 Feb 2022 13:58:13 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E6=97=B6=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=E8=AE=BF=E9=97=AE=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=AF=BC=E8=87=B4=E7=A8=8B=E5=BA=8F=E5=B4=A9=E6=BA=83?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/new_var/Makefile | 2 ++ tests/new_var/desc | 1 + tests/new_var/new_var.c | 23 +++++++++++++++++++++++ tests/new_var/new_var.diff | 15 +++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 tests/new_var/Makefile create mode 100644 tests/new_var/desc create mode 100644 tests/new_var/new_var.c create mode 100644 tests/new_var/new_var.diff diff --git a/tests/new_var/Makefile b/tests/new_var/Makefile new file mode 100644 index 0000000..6dd4b69 --- /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 0000000..4f8cd31 --- /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 0000000..3ed116a --- /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 0000000..c617535 --- /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() -- Gitee