From ec0394a261edcb59831ecde9962bd8a77870409f Mon Sep 17 00:00:00 2001 From: guzhihao4 Date: Tue, 29 Aug 2023 21:23:38 +0800 Subject: [PATCH] Fix dlclose testcase Complete dlclose testcase and optimize implementation Issue: #I7WYVI Signed-off-by: guzhihao4 Change-Id: I6d9cd29aecc4000631b8a7541c531c386b9b3f07 --- libc-test/src/functional/dlopen.c | 10 +++++--- libc-test/src/functional/dlopen_weak.c | 26 +++++++++++++++++--- libc-test/src/functional/dlopen_weak.h | 22 +++++++++++++++++ libc-test/src/functional/dlopen_weak_deps.c | 27 ++++++++++++++++++--- libc-test/src/functional/dlopen_weak_deps.h | 24 ++++++++++++++++-- porting/linux/user/ldso/dynlink.c | 4 ++- 6 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 libc-test/src/functional/dlopen_weak.h diff --git a/libc-test/src/functional/dlopen.c b/libc-test/src/functional/dlopen.c index 85e0fa403..68e0abf4a 100644 --- a/libc-test/src/functional/dlopen.c +++ b/libc-test/src/functional/dlopen.c @@ -122,18 +122,20 @@ void dlopen_dlclose() } #define DLOPEN_WEAK "libdlopen_weak.so" -typedef int (*FuncPtr_TestNumber)(int input); +typedef int (*func_ptr)(int input); void dlopen_dlclose_weak() { void* handle = dlopen(DLOPEN_WEAK, RTLD_LAZY | RTLD_GLOBAL); - if (!handle) + if (!handle) { t_error("dlopen(name=%s, mode=%d) failed: %s\n", DLOPEN_WEAK, RTLD_LAZY | RTLD_GLOBAL, dlerror()); - FuncPtr_TestNumber fn = (FuncPtr_TestNumber)dlsym(handle, "TestNumber"); + } + func_ptr fn = (func_ptr)dlsym(handle, "TestNumber"); if (fn) { int ret = fn(12); - if (ret != 0) + if (ret != 0) { t_error("weak symbol relocation error: so_name: %s, symbol: TestNumber\n", DLOPEN_WEAK); + } } dlclose(handle); } diff --git a/libc-test/src/functional/dlopen_weak.c b/libc-test/src/functional/dlopen_weak.c index 790dd5115..05570ffa2 100644 --- a/libc-test/src/functional/dlopen_weak.c +++ b/libc-test/src/functional/dlopen_weak.c @@ -1,11 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "dlopen_weak_deps.h" +#include "dlopen_weak.h" + +int num = 2; -__attribute__((weak)) int TestFunction(int input) +__attribute__((weak)) int test_function(int input) { - return input % 2; + return input % num; } -int TestNumber(int input) +int test_number(int input) { - return TestNumber2(input); + return test_number2(input); } diff --git a/libc-test/src/functional/dlopen_weak.h b/libc-test/src/functional/dlopen_weak.h new file mode 100644 index 000000000..d3bf15214 --- /dev/null +++ b/libc-test/src/functional/dlopen_weak.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DLOPEN_WEAK_H +#define DLOPEN_WEAK_H + +__attribute__((weak)) int test_function(int input); +int test_number(int input); + +#endif // DLOPEN_WEAK_H diff --git a/libc-test/src/functional/dlopen_weak_deps.c b/libc-test/src/functional/dlopen_weak_deps.c index f6b3883c8..4df0810c6 100644 --- a/libc-test/src/functional/dlopen_weak_deps.c +++ b/libc-test/src/functional/dlopen_weak_deps.c @@ -1,9 +1,28 @@ -__attribute__((weak)) int TestFunction(int input) +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dlopen_weak_deps.h" + +int num = 5; + +__attribute__((weak)) int test_function(int input) { - return input % 5; + return input % num; } -int TestNumber2(int input) +int test_number2(int input) { - return TestFunction(input) == 2; + return test_function(input); } diff --git a/libc-test/src/functional/dlopen_weak_deps.h b/libc-test/src/functional/dlopen_weak_deps.h index 3f033cf2b..ee58dfe6f 100644 --- a/libc-test/src/functional/dlopen_weak_deps.h +++ b/libc-test/src/functional/dlopen_weak_deps.h @@ -1,2 +1,22 @@ -int TestNumber2(int input); -__attribute__((weak)) int TestFunction(int input); +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DLOPEN_WEAK_DEPS_H +#define DLOPEN_WEAK_DEPS_H + +int test_number2(int input); +__attribute__((weak)) int test_function(int input); + +#endif // DLOPEN_WEAK_DEPS_H diff --git a/porting/linux/user/ldso/dynlink.c b/porting/linux/user/ldso/dynlink.c index c163100dd..d253c5421 100644 --- a/porting/linux/user/ldso/dynlink.c +++ b/porting/linux/user/ldso/dynlink.c @@ -3901,7 +3901,9 @@ static int do_dlclose(struct dso *p) } struct dso **dso_close_list = malloc((deps_num + 1) * sizeof(struct dso*)); - memset(dso_close_list, 0, deps_num + 1); + if (dso_close_list != NULL) { + memset(dso_close_list, 0, deps_num + 1); + } int dso_close_list_size = 0; LD_LOGI("do_dlclose name=%{public}s count=%{public}d by_dlopen=%{public}d", p->name, p->nr_dlopen, p->by_dlopen); -- Gitee