From 2528378f4723738070309addaed6671f53417fa7 Mon Sep 17 00:00:00 2001 From: Xuelian Guo Date: Mon, 16 Sep 2024 17:48:09 +0800 Subject: [PATCH] corrected code style after scan code by checkpatch.pl --- BM/lass/lass.c | 372 ++++++++++++++++--------------------- scenario/srf-oe/tests-lass | 2 +- 2 files changed, 156 insertions(+), 218 deletions(-) diff --git a/BM/lass/lass.c b/BM/lass/lass.c index f3e8ae23..703bc756 100644 --- a/BM/lass/lass.c +++ b/BM/lass/lass.c @@ -2,11 +2,13 @@ // Copyright (c) 2024 Intel Corporation. /* - * lass.c: + * lass.c: * - * Author: Pengfei, Xu , Weihong,Zhang , Xuelian, Guo + * Author: Pengfei, Xu , + * Weihong,Zhang , + * Xuelian, Guo ********************************************************* - * Usage: [| m | d | g | t | r | i | v | e | a | h |] + * Usage: m | d | g | t | r | i | v | e | a | h * m Test get vsyscall address maps. * d Test execute vsyscall addr 0xffffffffff600000. * g Test call vsyscall @@ -65,14 +67,14 @@ static sig_atomic_t num_vsyscall_traps; static int instruction_num, pass_num, fail_num; static jmp_buf jmpbuf; -static bool lass_enable = false; +static bool lass_enable; static bool no_vsys_map = true; -static bool vsyscall_none = false; -static bool vsyscall_xonly = false; -static bool vsyscall_emulate = false; -static bool vsyscall_default = false; +static bool vsyscall_none; +static bool vsyscall_xonly; +static bool vsyscall_emulate; +static bool vsyscall_default; /* - * /proc/self/maps, r means readable, x means excutable + * /proc/self/maps, r means readable, x means executable * vsyscall map: ffffffffff600000-ffffffffff601000 */ bool vsyscall_map_r = false, vsyscall_map_x = false; @@ -84,8 +86,7 @@ const gtod_t vgtod = (gtod_t)VSYS(VSYS_START_ADDR); typedef long (*time_func_t)(time_t *t); const time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400); -struct testcases -{ +struct testcases { char param; /* 2: SIGSEGV Error; 1: other errors */ const char *msg; int (*test_func)(); @@ -135,8 +136,8 @@ static bool check_lass_enable(void) bool rv = false; FILE *cmd = popen(command, "r"); - if (cmd) - { + + if (cmd) { while (fgets(buf, sizeof(buf) - 1, cmd) != NULL) ; // printf("Get buff:%s\n", buf); @@ -151,35 +152,31 @@ static bool check_lass_enable(void) /* Get information from /proc/cmdline */ static bool check_vsyscall_status(void) { - char buf[256] = {0}; - char command[256] = "cat /proc/cmdline"; - bool rv = false; - - FILE *cmd = popen(command, "r"); - if (cmd) - { - while (fgets(buf, sizeof(buf) - 1, cmd) != NULL) + char buf[256] = {0}; + char command[256] = "cat /proc/cmdline"; + bool rv = false; + + FILE *cmd = popen(command, "r"); + + if (cmd) { + while (fgets(buf, sizeof(buf) - 1, cmd) != NULL) ; - pclose(cmd); - + pclose(cmd); + rv = (strstr(buf, " vsyscall") != 0); if (!rv) - vsyscall_default = true; - - rv = (strstr(buf, " vsyscall=emulate") != 0); - if (rv) + vsyscall_default = true; + rv = (strstr(buf, " vsyscall=emulate") != 0); + if (rv) vsyscall_emulate = true; - rv = (strstr(buf, " vsyscall=xonly") != 0); - if (rv) - vsyscall_xonly = true; - + if (rv) + vsyscall_xonly = true; rv = (strstr(buf, " vsyscall=none") != 0); - if (rv) - vsyscall_none = true; - } - - return rv; + if (rv) + vsyscall_none = true; + } + return rv; } static int get_vsys_map(void) @@ -188,22 +185,20 @@ static int get_vsys_map(void) char line[MAPS_LINE_LEN]; maps = fopen("/proc/self/maps", "r"); - if (!maps) - { + if (!maps) { printf("[WARN]\tCould not open /proc/self/maps\n"); vsyscall_map_r = false; return 0; } - while (fgets(line, MAPS_LINE_LEN, maps)) - { + while (fgets(line, MAPS_LINE_LEN, maps)) { char r, x; void *start, *end; char name[MAPS_LINE_LEN]; /* sscanf() is safe as strlen(name) >= strlen(line) */ if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s", - &start, &end, &r, &x, name) != 5) + &start, &end, &r, &x, name) != 5) continue; if (strcmp(name, "[vsyscall]")) @@ -212,8 +207,7 @@ static int get_vsys_map(void) printf("\tvsyscall map: %s", line); if (start != (void *)VSYS_START_ADDR || - end != (void *)VSYS_END_ADDR) - { + end != (void *)VSYS_END_ADDR) { fail_case("address range is nonsense\n"); } @@ -221,15 +215,14 @@ static int get_vsys_map(void) vsyscall_map_r = (r == 'r'); vsyscall_map_x = (x == 'x'); printf("vsyscall_map_r:%d, vsyscall_map_x:%d\n", - vsyscall_map_r, vsyscall_map_x); + vsyscall_map_r, vsyscall_map_x); no_vsys_map = false; break; } fclose(maps); - if (no_vsys_map) - { + if (no_vsys_map) { printf("[WARN]\tno vsyscall map in /proc/self/maps\n"); vsyscall_map_r = false; vsyscall_map_x = false; @@ -240,22 +233,16 @@ static int get_vsys_map(void) static int test_read_vsys_map(void) { get_vsys_map(); - if (no_vsys_map) - { - if (lass_enable && vsyscall_none) { - pass_case("lass vsyscall=none, disable vsyscall address map as expected."); - } - else { - fail_case("lass vsyscall=emulate/xonly/default, syscall address map should not be disabled."); - } - } - else { - if (lass_enable && vsyscall_none) { - fail_case("lass vsyscall=none, Vsyscall address map should not be enabled."); - } - else { - pass_case("lass vsyscall=emulate/xonly/default, vsyscall address map is enabled as expected."); - } + if (no_vsys_map) { + if (lass_enable && vsyscall_none) + pass_case("lass vsyscall=none,disable vsyscall address map as expected."); + else + fail_case("lass vsyscall=emulate/xonly/default,vsyscall address map should not be disabled."); + } else { + if (lass_enable && vsyscall_none) + fail_case("lass vsyscall=none,vsyscall address map should not be enabled."); + else + pass_case("lass vsyscall=emulate/xonly/default,vsyscall address map is enabled as expected."); } return 0; @@ -268,8 +255,7 @@ void dump_buffer(unsigned char *buf, int size) printf("-----------------------------------------------------\n"); printf("buf addr:%p size = %d (%03xh)\n", buf, size, size); - for (i = 0; i < size; i += 16) - { + for (i = 0; i < size; i += 16) { printf("%04x: ", i); for (j = i; ((j < i + 16) && (j < size)); j++) @@ -284,43 +270,36 @@ static int test_read_vsys_address(void) bool can_read; int a = 0; - if (sigsetjmp(jmpbuf, 1) == 0) - { + if (sigsetjmp(jmpbuf, 1) == 0) { printf("Access 0x%lx\n", VSYS_START_ADDR); a = *(int *)VSYS_START_ADDR; printf("0x%lx content:0x%x\n", VSYS_START_ADDR, a); can_read = true; - } - else - { + } else { can_read = false; } printf("can_read:%d, vsyscall_map_r:%d\n", can_read, vsyscall_map_r); // when LASS enable, the vsyscall page is unreadable except vsyscall=emulate - if (lass_enable && vsyscall_emulate) { + if (lass_enable && vsyscall_emulate) vsyscall_map_r = true; - } - else { + else vsyscall_map_r = false; - } - // vsyscall_map_r = true; + // vsyscall_map_r = true; // can_read = true; printf("can_read:%d, vsyscall_map_r:%d\n", can_read, vsyscall_map_r); - - if (vsyscall_map_r == can_read) { + + if (vsyscall_map_r == can_read) pass_case("Could read vsyscall addr is as expected"); - } - else { + else fail_case("Could not read vsyscall addr is not as expected"); - } return 0; } static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), - int flags) + int flags) { struct sigaction sa; @@ -332,7 +311,7 @@ static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *), if (sigaction(sig, &sa, 0)) err(1, "sigaction"); - return; + //return; } static void sigsegv(int sig, siginfo_t *info, void *ctx_void) @@ -344,7 +323,7 @@ static void sigsegv(int sig, siginfo_t *info, void *ctx_void) unsigned long bp = ctx->uc_mcontext.gregs[REG_RBP]; printf("Received sig:%d,si_code:%d,ip:0x%lx,bp:0x%lx\n", - sig, info->si_code, ip, bp); + sig, info->si_code, ip, bp); siglongjmp(jmpbuf, 1); } @@ -363,56 +342,40 @@ static int test_process_vm_readv_vsys_address(void) ret = process_vm_readv(getpid(), &local, 1, &remote, 1, 0); printf("After process_vm_readv copy to buf\n"); - // when LASS enable, the vsyscall page is unreadable + //when LASS enable, the vsyscall page is unreadable if (lass_enable == true) vsyscall_map_r = false; - if (ret != READ_LEN) - { + if (ret != READ_LEN) { perror("Get error:"); if (vsyscall_map_r) { - if (lass_enable && vsyscall_emulate) { + if (lass_enable && vsyscall_emulate) fail_case("Readable vsyscall address is as expected when lass vsyscall=emulate, but read date is wrong"); - } - else { - fail_case("Readable vsyscall address is not as expected when vsyscall!=emulate"); - } - } - else { - if (lass_enable && vsyscall_emulate) { + else + fail_case("Readable vsyscall address is not as expected when vsyscall!=emulate"); + } else { + if (lass_enable && vsyscall_emulate) fail_case("Could not process_vm_readv is not as expected when lass vsyscall=emulate"); - } - else { + else pass_case("Could not process_vm_readv as expected when lass vsyscall!=emulate"); - } } - } - else - { + } else { dump_buffer(buf, 0x100); - if (vsyscall_map_r) - { - if (memcmp(buf, (const void *)VSYS_START_ADDR, READ_LEN)) - { + if (vsyscall_map_r) { + if (memcmp(buf, (const void *)VSYS_START_ADDR, READ_LEN)) { printf("[WARN]\t Read incorrect data\n"); fail_case("read vsyscall data is not acceptable in lass"); - } - else { - if (lass_enable && vsyscall_emulate) { + } else { + if (lass_enable && vsyscall_emulate) pass_case("read vsyscall date is as expected when lass vsyscall=emulate."); - } - else { + else fail_case("Read vsyscall data is not as expcted when lass vsyscall!=emulate."); - } } - } - else { - if (lass_enable && vsyscall_emulate) { + } else { + if (lass_enable && vsyscall_emulate) pass_case("readable vsyscall address is as expected when lass vsyscall=emulate, and read data is correct"); - } - else { + else fail_case("Read vsyscall data is unexpected."); - } } } @@ -447,36 +410,28 @@ static int test_vsys_address_executable(void) printf("vsyscall address is not executable!\n"); printf("[RUN]\tMake sure that vsyscalls is executable\n"); - if (sigsetjmp(jmpbuf, 1) == 0) - { + if (sigsetjmp(jmpbuf, 1) == 0) { vgtod(&tv, &tz); executed = true; - } - else + } else executed = false; - if (executed) - { + if (executed) { printf("Get time: tv_sys.sec:%ld usec:%ld\n", tv.tv_sec, tv.tv_usec); if (lass_enable && vsyscall_none) fail_case("lass vsyscall=none,executed vsyscall address is unexpected!"); - else - { + else { if (vsyscall_map_x) pass_case("Executed vsyscall address is expected!"); else fail_case("Should trigger wrong page fault!"); } - } - else - { /* INSTR */ + } else { /* INSTR */ printf("Failed to get time\n"); - if (lass_enable && vsyscall_none) pass_case("lass vsyscall=none,unsupport execute vsyscall address!"); - else - { + else { if (vsyscall_map_x) fail_case("Should be execute vsyscall address!"); else @@ -499,9 +454,9 @@ static unsigned long get_eflags(void) static void set_eflags(unsigned long eflags) { asm volatile("pushq %0\n\tpopfq" - : - : "rm"(eflags) - : "flags"); + : + : "rm"(eflags) + : "flags"); } static void sigtrap(int sig, siginfo_t *info, void *ctx_void) @@ -528,8 +483,7 @@ static int test_vsys_emulation(void) bool is_native; num_vsyscall_traps = 0; - if (!vsyscall_map_x) - { + if (!vsyscall_map_x) { printf("Could not execute vsyscall\n"); pass_case("Sysfile: vsyscall could not be executed\n"); return 1; @@ -578,10 +532,9 @@ int test_vsys_syscall_gtod(void) struct timezone tz_sys; ret_vsys = sys_gtod(&tv_sys, &tz_sys); - if (ret_vsys) + if (ret_vsys) { fail_case("Failed to test syscall gettimeofday!"); - else - { + } else { printf("Sysvall: gettimeofday\n"); printf("Get time. tv_sys.sec:%ld usec:%ld\n", tv_sys.tv_sec, tv_sys.tv_usec); @@ -605,30 +558,24 @@ int test_read_kernel_linear(void) b = rand(); kernel_random_addr = ((a << 32) | 0xffff800000000000ul) | b; - if (kernel_random_addr < KERNEL_START_ADDR) - { + if (kernel_random_addr < KERNEL_START_ADDR) { printf("addr:0x%lx is smaller than 0x%lx\n", kernel_random_addr, KERNEL_START_ADDR); fail_case("Set addr error!"); return 1; } printf("Kernel linear addr:0x%lx\n", kernel_random_addr); - if (sigsetjmp(jmpbuf, 1) == 0) - { + if (sigsetjmp(jmpbuf, 1) == 0) { addr_content = *(const int *)kernel_random_addr; readed = true; - } - else + } else readed = false; - if (readed) - { + if (readed) { printf("Get content:0x%x (0x%lx)\n", addr_content, kernel_random_addr); fail_case("Kernel address could not read from user space!"); - } - else - { /* INSTR */ + } else { /* INSTR */ printf("Failed to read kernel space.\n"); pass_case("LASS unsupport access to read kernel address!"); @@ -638,56 +585,56 @@ int test_read_kernel_linear(void) } static struct testcases lass_cases[] = { - { - .param = 'm', - .test_func = test_read_vsys_map, - .msg = "Test get vsyscall address maps.", - }, - { - .param = 'd', - .test_func = test_vsys_address_executable, - .msg = "Test execute vsyscall addr 0xffffffffff600000.", - }, - { - .param = 'g', - .test_func = test_vsys_syscall_gtod, - .msg = "Test call vsyscall", - }, - { - .param = 't', - .test_func = test_vsys_api_gtod, - .msg = "Test call vsyscall api gettimeofday", - }, - { - .param = 'r', - .test_func = test_read_vsys_address, - .msg = "Test read vsyscall 0xffffffffff600000.", - }, - { - .param = 'i', - .test_func = test_read_kernel_linear, - .msg = "Test read random kernel space.", - }, - { - .param = 'v', - .test_func = test_process_vm_readv_vsys_address, - .msg = "Test process_vm_readv read address 0xffffffffff600000.[negative]", - }, - { - .param = 'e', - .test_func = test_vsys_emulation, - .msg = "Test vsyscall emulation.", - }, - { - .param = 'a', - .test_func = run_all, - .msg = "Test all.", - }, - { - .param = 'h', - .test_func = usage, - .msg = "Help", - }}; + { + .param = 'm', + .test_func = test_read_vsys_map, + .msg = "Test get vsyscall address maps.", + }, + { + .param = 'd', + .test_func = test_vsys_address_executable, + .msg = "Test execute vsyscall addr 0xffffffffff600000.", + }, + { + .param = 'g', + .test_func = test_vsys_syscall_gtod, + .msg = "Test call vsyscall", + }, + { + .param = 't', + .test_func = test_vsys_api_gtod, + .msg = "Test call vsyscall api gettimeofday", + }, + { + .param = 'r', + .test_func = test_read_vsys_address, + .msg = "Test read vsyscall 0xffffffffff600000.", + }, + { + .param = 'i', + .test_func = test_read_kernel_linear, + .msg = "Test read random kernel space.", + }, + { + .param = 'v', + .test_func = test_process_vm_readv_vsys_address, + .msg = "Test process_vm_readv read address 0xffffffffff600000.[negative]", + }, + { + .param = 'e', + .test_func = test_vsys_emulation, + .msg = "Test vsyscall emulation.", + }, + { + .param = 'a', + .test_func = run_all, + .msg = "Test all.", + }, + { + .param = 'h', + .test_func = usage, + .msg = "Help", + }}; int usage(void) { @@ -711,8 +658,7 @@ int run_test_case(char opt) int cnt = ARRAY_SIZE(lass_cases); int ret = 0; - for (int i = 0; i < cnt; i++) - { + for (int i = 0; i < cnt; i++) { if (lass_cases[i].param != opt) continue; @@ -732,8 +678,7 @@ int run_all(void) int cnt = ARRAY_SIZE(lass_cases); int ret = 0; - for (int i = 0; i < cnt; i++) - { + for (int i = 0; i < cnt; i++) { if (lass_cases[i].param == 'h' || lass_cases[i].param == 'a') continue; @@ -752,10 +697,8 @@ int check_param(char opt) int cnt = ARRAY_SIZE(lass_cases); int ret = 0; - for (int i = 0; i < cnt; i++) - { - if (lass_cases[i].param != opt) - { + for (int i = 0; i < cnt; i++) { + if (lass_cases[i].param != opt) { ret = 1; break; } @@ -768,31 +711,26 @@ int main(int argc, char *argv[]) { char parm; - if (!cpu_has_lass()) - { + if (!cpu_has_lass()) { printf("Unsupported LASS feature!\n"); return 1; } - if (!check_lass_enable()) - { + if (!check_lass_enable()) { lass_enable = false; printf("LASS feature is under default mode without lass defined in cmdline.\n"); } lass_enable = true; - check_vsyscall_status(); + check_vsyscall_status(); - if (argc == 2) - { - if (sscanf(argv[1], "%c", &parm) != 1) - { + if (argc == 2) { + if (sscanf(argv[1], "%c", &parm) != 1) { printf("Invalid parm:%c\n", parm); usage(); } printf("parm:%c\n", parm); - } - else + } else usage(); if (!check_param(parm)) diff --git a/scenario/srf-oe/tests-lass b/scenario/srf-oe/tests-lass index 1e40e8f1..0586b67f 100644 --- a/scenario/srf-oe/tests-lass +++ b/scenario/srf-oe/tests-lass @@ -6,7 +6,7 @@ #Test get vsyscall address maps. lass m #Test execute vsyscall addr 0xffffffffff600000. -lass m +lass d #Test call vsyscall lass g #Test call vsyscall api gettimeofday