From dd0c6c66ed48e355eb85f4ce29089681d5ef8b5b Mon Sep 17 00:00:00 2001 From: TXuian <1163589503@qq.com> Date: Wed, 22 May 2024 15:46:36 +0800 Subject: [PATCH] Fix ipc to support null pointer. --- Ubiquitous/XiZi_AIoT/services/app/Makefile | 6 +- .../XiZi_AIoT/services/app/test_ipc_null.c | 85 +++++++++++++++++++ .../XiZi_AIoT/services/lib/ipc/libipc.c | 19 ++++- .../XiZi_AIoT/services/lib/ipc/libipc.h | 11 +++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 Ubiquitous/XiZi_AIoT/services/app/test_ipc_null.c diff --git a/Ubiquitous/XiZi_AIoT/services/app/Makefile b/Ubiquitous/XiZi_AIoT/services/app/Makefile index b3a6c8c02..f3c003c85 100644 --- a/Ubiquitous/XiZi_AIoT/services/app/Makefile +++ b/Ubiquitous/XiZi_AIoT/services/app/Makefile @@ -27,7 +27,7 @@ INC_DIR = -I$(KERNEL_ROOT)/services/shell/letter-shell \ -I$(KERNEL_ROOT)/services/app ifeq ($(BOARD), imx6q-sabrelite) -all: init test_fault simple_client simple_server shell fs_server semaphore_server test_thread test_irq_hdlr test_irq_block test_irq_send eth_driver epit_server readme.txt | bin +all: init test_fault simple_client simple_server shell fs_server semaphore_server test_ipc_null test_thread test_irq_hdlr test_irq_block test_irq_send eth_driver epit_server readme.txt | bin else all: init test_fault simple_client simple_server shell fs_server test_irq_hdlr readme.txt | bin endif @@ -53,6 +53,10 @@ epit_server: timer.o epit.o ccm_pll.o usyscall.o arch_usyscall.o libserial.o pri @${objdump} -S $@ > $@.asm endif +test_ipc_null: test_ipc_null.o libserial.o printf.o usyscall.o arch_usyscall.o libipc.o session.o + @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} + @${objdump} -S $@ > $@.asm + semaphore_server: semaphore_server.o libserial.o printf.o usyscall.o arch_usyscall.o libipc.o session.o @${ld} ${user_ldflags} -e main -o $@ $^ ${board_specs} @${objdump} -S $@ > $@.asm diff --git a/Ubiquitous/XiZi_AIoT/services/app/test_ipc_null.c b/Ubiquitous/XiZi_AIoT/services/app/test_ipc_null.c new file mode 100644 index 000000000..e0c0ccf19 --- /dev/null +++ b/Ubiquitous/XiZi_AIoT/services/app/test_ipc_null.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2020 AIIT XUOS Lab + * XiUOS is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ +#include +#include + +#include "libipc.h" +#include "libserial.h" +#include "usyscall.h" + +IPC_SERVICES(IpcTestNull, Ipc_test_null); +#define TEST_BUF_SIZE 32 + +static char test_null_server_name[] = "TestNullServer"; +static char test_null_client_name[] = "TestNullClient"; + +// client side +static char buf[TEST_BUF_SIZE]; + +IPC_INTERFACE(Ipc_test_null, 1, ptr, sizeof(buf)); +int test_null(struct Session* session, char* ptr) +{ + return IPC_CALL(Ipc_test_null)(session, ptr); +} + +// client thread +int server_thread(int argc, char** argv); +int main(int argc, char** argv) +{ + struct Session session; + bool server_enabled = false; + while (connect_session(&session, test_null_server_name, 4096) < 0) { + if (!server_enabled) { + char* server_param[] = { test_null_server_name, NULL }; + if (thread(server_thread, test_null_server_name, server_param) >= 0) { + server_enabled = true; + } + } + } + + printf("[%s] Call using NULL ptr.\n", test_null_client_name); + test_null(&session, NULL); + printf("[%s] Call using non-NULL ptr.\n", test_null_client_name); + test_null(&session, buf); + + exit(0); + return 0; +} + +// server side +int IPC_DO_SERVE_FUNC(Ipc_test_null)(void* ptr) +{ + if (ptr == NULL) { + printf("[%s]: A NULL ptr ipc call.\n", test_null_server_name); + } else { + printf("[%s]: A non-NULL ptr ipc call.\n", test_null_server_name); + } + + return 0; +} + +IPC_SERVER_INTERFACE(Ipc_test_null, 1); +IPC_SERVER_REGISTER_INTERFACES(IpcTestNull, 1, Ipc_test_null); + +// server threads +int server_thread(int argc, char** argv) +{ + if (register_server(test_null_server_name) < 0) { + printf("[%s] Register %s server failed.\n", test_null_server_name, test_null_server_name); + exit(1); + return 1; + } + ipc_server_loop(&IpcTestNull); + + exit(0); + return 0; +} diff --git a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c index 4428db4f3..e17229523 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c +++ b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.c @@ -83,11 +83,22 @@ bool ipc_msg_set_nth_arg(struct IpcMsg* msg, const int arg_num, const void* cons return false; } struct IpcArgInfo* nth_arg_info = IPCMSG_ARG_INFO(msg, arg_num); - if (len > nth_arg_info->len) { + if (len < 0 || (uint32_t)len > (uint32_t)nth_arg_info->len) { printf("[%s] IPC: size of arg out of buffer range, given len: %d, len %u\n", __func__, len, nth_arg_info->len); return false; } + void* buf = ipc_msg_get_nth_arg_buf(msg, arg_num); + + // handle attributes of different params + if (data == NULL) { + nth_arg_info->null_ptr = 1; + memset(buf, 0x0, len); + return true; + } else { + nth_arg_info->null_ptr = 0; + } + memmove(buf, data, len); return true; } @@ -109,6 +120,12 @@ bool ipc_msg_get_nth_arg(struct IpcMsg* msg, const int arg_num, void* data, cons printf("[%s] IPC: size of arg out of buffer range", __func__); return false; } + + // handle null ptr: do nothing + if (nth_arg_info->null_ptr == 1) { + return true; + } + void* buf = ipc_msg_get_nth_arg_buf(msg, arg_num); memmove(data, buf, len); return true; diff --git a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h index eaa084aad..a10016b48 100644 --- a/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h +++ b/Ubiquitous/XiZi_AIoT/services/lib/ipc/libipc.h @@ -60,6 +60,13 @@ typedef struct { struct IpcArgInfo { uint16_t offset; uint16_t len; + union { + uint16_t attr; + struct { + uint16_t null_ptr : 1; + uint16_t reserved : 15; + }; + }; } __attribute__((packed)); /* [header, ipc_arg_buffer_len[], ipc_arg_buffer[]] */ @@ -104,6 +111,10 @@ struct IpcNode { /// @return __attribute__((__always_inline__)) static inline void* ipc_msg_get_nth_arg_buf(struct IpcMsg* msg, int arg_num) { + if (IPCMSG_ARG_INFO(msg, arg_num)->null_ptr == 1) { + return NULL; + } + return (void*)((char*)msg + IPCMSG_ARG_INFO(msg, arg_num)->offset); }