提交 003496e3 编写于 作者: D dhy308

Fix code review issues.

Issue: I6A9R5
Test: libc-test pass
Signed-off-by: Ndhy308 <tony.gan@huawei.com>
上级 9b9fbfb5
# Copyright (c) 2022 Huawei Device Co., Ltd.
# 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
......@@ -22,8 +22,6 @@ group("ldso_cfi_test") {
}
ohos_executable("ldso_cfi_check") {
# cflags_c = ["--coverage"]
# ldflags = ["--coverage"]
subsystem_name = "musl"
part_name = "libc-test"
include_dirs = [
......@@ -41,8 +39,6 @@ ohos_shared_library("ldso_cfi_test_lib") {
include_dirs = [ "." ]
sources = [ "ldso_cfi_test_lib.c" ]
# cflags_c = ["--coverage"]
# ldflags = ["--coverage"]
output_name = "ldso_cfi_test_lib"
output_extension = "so"
subsystem_name = "musl"
......
/**
* Copyright (c) 2022 Huawei Device Co., Ltd.
* 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
......@@ -25,10 +25,12 @@
struct dso {
char *mock;
unsigned char *map;
size_t map_len;
};
extern bool init_cfi_shadow(struct dso *dso_list);
extern bool map_dso_to_cfi_shadow(struct dso *dso);
extern int init_cfi_shadow(struct dso *dso_list);
extern int map_dso_to_cfi_shadow(struct dso *dso);
extern void unmap_dso_from_cfi_shadow(struct dso *dso);
extern void __cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr);
extern void __cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData);
......@@ -60,7 +62,7 @@ static void test_func() {}
void cfi_init_test_0001(void)
{
printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
EXPECT_TRUE("cfi_init_test_0001", init_cfi_shadow(NULL));
EXPECT_EQ("cfi_init_test_0001", init_cfi_shadow(NULL), 0);
}
/**
......@@ -71,7 +73,7 @@ void cfi_init_test_0001(void)
void cfi_init_test_0002(void)
{
printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
EXPECT_TRUE("cfi_init_test_0002", map_dso_to_cfi_shadow(NULL));
EXPECT_EQ("cfi_init_test_0002", map_dso_to_cfi_shadow(NULL), 0);
}
/**
......@@ -320,6 +322,37 @@ void cfi_slowpath_diag_function_test_0001(void)
printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
}
/**
* @tc.name : cfi_unmap_dso_from_cfi_shadow_001
* @tc.desc : If dso map is NULL while unmapping from the CFI shadow, do nothing.
* @tc.level : Level 2
*/
void cfi_unmap_dso_from_cfi_shadow_001(void)
{
printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
struct dso test_dso = {};
test_dso.map = 0;
test_dso.map_len = 1;
unmap_dso_from_cfi_shadow(&test_dso);
printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
}
/**
* @tc.name : cfi_unmap_dso_from_cfi_shadow_002
* @tc.desc : If dso map_len is NULL while unmapping from the CFI shadow, do nothing.
* @tc.level : Level 2
*/
void cfi_unmap_dso_from_cfi_shadow_002(void)
{
printf("["__FILE__"][Line: %d][%s]: entry\n", __LINE__, __func__);
struct dso test_dso = {};
int a = 9;
test_dso.map = (unsigned char *)a;
test_dso.map_len = 0;
unmap_dso_from_cfi_shadow(&test_dso);
printf("["__FILE__"][Line: %d][%s]: end\n", __LINE__, __func__);
}
TEST_FUN G_Fun_Array[] = {
cfi_init_test_0001,
cfi_init_test_0002,
......@@ -332,6 +365,8 @@ TEST_FUN G_Fun_Array[] = {
cfi_slowpath_function_test_0007,
cfi_slowpath_function_test_0008,
cfi_slowpath_diag_function_test_0001,
cfi_unmap_dso_from_cfi_shadow_001,
cfi_unmap_dso_from_cfi_shadow_002,
};
int main(void)
......
/**
* 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 <assert.h>
#include <stdint.h>
#include <stdio.h>
......
此差异已折叠。
/*
* 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 "dynlink.h"
/* alignment bits in memory space for dso */
#define CFI_SUCCESS 0
#define CFI_FAILED -1
/* Define LIBRARY_ALIGNMENT to balance the size of CFI shadow and the number of DSOs that each process can load.
* The LIBRARY_ALIGNMENT is use to calculate the start address of a DSO. Each DSO should be in the different
* LIBRARY_ALIGNMENT memory range so that each DSO can be well mapped to the CFI shadow.
*/
#define LIBRARY_ALIGNMENT_BITS 18
/* minimum unit in memory space for dso */
#define LIBRARY_ALIGNMENT (1UL << LIBRARY_ALIGNMENT_BITS)
/* map all the dso and the dependents to cfi shadow */
bool init_cfi_shadow(struct dso *dso_list);
int init_cfi_shadow(struct dso *dso_list);
/* map a dso and the dependents to cfi shadow */
bool map_dso_to_cfi_shadow(struct dso *dso);
int map_dso_to_cfi_shadow(struct dso *dso);
/* unmap a dso from cfi shadow */
void unmap_dso_from_cfi_shadow(struct dso *dso);
\ No newline at end of file
......@@ -28,10 +28,10 @@
#include <time.h>
#include <sys/prctl.h>
#include "cfi.h"
#include "dlfcn_ext.h"
#include "dynlink_rand.h"
#include "ld_log.h"
#include "cfi.h"
#include "libc.h"
#include "malloc_impl.h"
#include "namespace.h"
......@@ -90,11 +90,6 @@ struct reserved_address_params {
#endif
};
struct sym_info_pair {
uint_fast32_t sym_h;
uint32_t sym_l;
};
typedef void (*stage3_func)(size_t *, size_t *);
static struct builtin_tls {
......@@ -491,7 +486,7 @@ static struct sym_info_pair sysv_hash(const char *s0)
return s_info_p;
}
static struct sym_info_pair gnu_hash(const char *s0)
struct sym_info_pair gnu_hash(const char *s0)
{
struct sym_info_pair s_info_p;
const unsigned char *s = (void *)s0;
......@@ -706,7 +701,61 @@ static void add_can_search_so_list_in_dso(struct dso *dso_relocating, struct dso
#if defined(__GNUC__)
__attribute__((always_inline))
#endif
struct symdef find_sym2(struct dso *dso, struct verinfo *verinfo, int need_def, int use_deps, ns_t *ns)
struct symdef find_sym_impl(
struct dso *dso, struct verinfo *verinfo, struct sym_info_pair s_info_p, int need_def, ns_t *ns)
{
Sym *sym;
uint32_t *ght;
uint32_t h = 0;
uint32_t gh = s_info_p.sym_h;
uint32_t gho = gh / (8 * sizeof(size_t));
size_t ghm = 1ul << gh % (8 * sizeof(size_t));
struct symdef def = {0};
if (ns && !check_sym_accessible(dso, ns))
return def;
if ((ght = dso->ghashtab)) {
const size_t *bloomwords = (const void *)(ght + 4);
size_t f = bloomwords[gho & (ght[2] - 1)];
if (!(f & ghm))
return def;
f >>= (gh >> ght[3]) % (8 * sizeof f);
if (!(f & 1))
return def;
sym = gnu_lookup(s_info_p, ght, dso, verinfo);
} else {
if (!h)
s_info_p = sysv_hash(verinfo->s);
sym = sysv_lookup(verinfo, s_info_p, dso);
}
if (!sym)
return def;
if (!sym->st_shndx)
if (need_def || (sym->st_info & 0xf) == STT_TLS || ARCH_SYM_REJECT_UND(sym))
return def;
if (!sym->st_value)
if ((sym->st_info & 0xf) != STT_TLS)
return def;
if (!(1 << (sym->st_info & 0xf) & OK_TYPES))
return def;
if (!(1 << (sym->st_info >> 4) & OK_BINDS))
return def;
def.sym = sym;
def.dso = dso;
return def;
}
static inline struct symdef find_sym2(struct dso *dso, struct verinfo *verinfo, int need_def, int use_deps, ns_t *ns)
{
struct sym_info_pair s_info_p = gnu_hash(verinfo->s);
uint32_t h = 0, gh = s_info_p.sym_h, gho = gh / (8*sizeof(size_t)), *ght;
......@@ -2835,11 +2884,8 @@ void __dls3(size_t *sp, size_t *auxv)
libc.tls_size = tmp_tls_size;
}
if (!init_cfi_shadow(head)) {
if (init_cfi_shadow(head) == CFI_FAILED) {
error("[%s] init_cfi_shadow failed: %m", __FUNCTION__);
if (runtime) {
longjmp(*rtld_fail, 1);
}
}
if (ldso_fail) _exit(127);
......@@ -3142,11 +3188,9 @@ static void *dlopen_impl(
* relocations resolved to symbol definitions that get removed. */
redo_lazy_relocs();
if (!map_dso_to_cfi_shadow(p)) {
if (map_dso_to_cfi_shadow(p) == CFI_FAILED) {
error("[%s] map_dso_to_cfi_shadow failed: %m", __FUNCTION__);
if (runtime) {
longjmp(*rtld_fail, 1);
}
longjmp(*rtld_fail, 1);
}
if (mode & RTLD_NODELETE) {
......@@ -4138,7 +4182,6 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para
? mmap((void *)start_addr, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
: mmap((void *)start_addr, map_len, prot, map_flags, task->fd, off_start + task->file_offset);
if (map == MAP_FAILED) {
LD_LOGE("Error mapping library %{public}s: failed to map fd", task->name);
goto error;
}
if (reserved_params && map_len < reserved_params->reserved_size) {
......@@ -4150,7 +4193,6 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para
/* use tmp_map_len to mmap enough space for the dso with anonymous mapping */
unsigned char *temp_map = mmap((void *)NULL, tmp_map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (temp_map == MAP_FAILED) {
LD_LOGE("Error mapping library 1 %{public}s: failed to map fd", task->name);
goto error;
}
......@@ -4165,7 +4207,6 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para
/* use map_len to mmap correct space for the dso with file mapping */
: mmap(real_map, map_len, prot, map_flags, task->fd, off_start + task->file_offset);
if (map == MAP_FAILED) {
LD_LOGE("Error mapping library 3 %{public}s: failed to map fd", task->name);
goto error;
}
}
......
......@@ -58,6 +58,23 @@ enum {
REL_FUNCDESC_VAL,
};
struct td_index {
size_t args[2];
struct td_index *next;
};
struct verinfo {
const char *s;
const char *v;
bool use_vna_hash;
uint32_t vna_hash;
};
struct sym_info_pair {
uint_fast32_t sym_h;
uint32_t sym_l;
};
struct dso {
#if DL_FDPIC
struct fdpic_loadmap *loadmap;
......@@ -132,6 +149,7 @@ struct dso {
struct dso **reloc_can_search_dso_list;
size_t reloc_can_search_dso_count;
size_t reloc_can_search_dso_capacity;
bool is_mapped_to_shadow;
char buf[];
};
......@@ -140,18 +158,6 @@ struct symdef {
struct dso *dso;
};
struct verinfo {
const char *s;
const char *v;
bool use_vna_hash;
uint32_t vna_hash;
};
struct td_index {
size_t args[2];
struct td_index *next;
};
struct fdpic_loadseg {
uintptr_t addr, p_vaddr, p_memsz;
};
......@@ -222,7 +228,9 @@ void *laddr(const struct dso *p, size_t v);
#endif
void *addr2dso(size_t a);
struct symdef find_sym2(struct dso *dso, struct verinfo *verinfo, int need_def, int use_deps, ns_t *ns);
struct sym_info_pair gnu_hash(const char *s0);
struct symdef find_sym_impl(
struct dso *dso, struct verinfo *verinfo, struct sym_info_pair s_info_p, int need_def, ns_t *ns);
hidden void *__dlsym(void *restrict, const char *restrict, void *restrict);
hidden void *__dlvsym(void *restrict, const char *restrict, const char *restrict, void *restrict);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册