diff --git a/libc-test/src/functional/dlopen.c b/libc-test/src/functional/dlopen.c index 85e0fa4038f39e32c0d354422bfb6cfd77a0ffb1..68e0abf4a46742ca083bc3f518887ffd59afcf98 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 790dd511518dbc66fe4e907534d1ee593f9ece70..05570ffa21488344670c4706d7d6dc978d7744b5 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 0000000000000000000000000000000000000000..d3bf152149d0e104ee15bf6591167c3d2a7f49d3 --- /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 f6b3883c872c79830af079837ceb6c0feb8b460f..4df0810c63e17a2548ce3e1b10c911f915140977 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 3f033cf2b2a5a27a086c1fa3da7034b3d46c69d5..ee58dfe6fc7974940ae8ea65c8262f13bfe8f705 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 c163100dde2e8c161a844dac3b972b77caf5f946..d253c54214b26003daa6398dcaa9c487d8788256 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);