Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Musl
提交
66cc130b
T
Third Party Musl
项目概览
OpenHarmony
/
Third Party Musl
1 年多 前同步成功
通知
37
Star
125
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Musl
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
66cc130b
编写于
8月 16, 2022
作者:
G
ganlan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
封装internal_malloc、internal_free、internal_realloc、internal_calloc接口
Signed-off-by:
N
ganlan
<
tony.gan@huawei.com
>
上级
ed049184
变更
12
隐藏空白更改
内联
并排
Showing
12 changed file
with
288 addition
and
156 deletion
+288
-156
porting/linux/user/ldso/dynlink.c
porting/linux/user/ldso/dynlink.c
+40
-39
porting/linux/user/ldso/dynlink_rand.c
porting/linux/user/ldso/dynlink_rand.c
+14
-12
porting/linux/user/ldso/namespace.c
porting/linux/user/ldso/namespace.c
+51
-35
porting/linux/user/ldso/namespace.h
porting/linux/user/ldso/namespace.h
+15
-0
porting/linux/user/ldso/ns_config.c
porting/linux/user/ldso/ns_config.c
+53
-43
porting/linux/user/ldso/ns_config.h
porting/linux/user/ldso/ns_config.h
+15
-0
porting/linux/user/ldso/strops.c
porting/linux/user/ldso/strops.c
+35
-11
porting/linux/user/ldso/strops.h
porting/linux/user/ldso/strops.h
+24
-6
porting/linux/user/src/hilog/vsnprintf_s_p.c
porting/linux/user/src/hilog/vsnprintf_s_p.c
+4
-2
porting/linux/user/src/internal/hilog_adapter.h
porting/linux/user/src/internal/hilog_adapter.h
+3
-2
porting/linux/user/src/internal/malloc_impl.h
porting/linux/user/src/internal/malloc_impl.h
+8
-0
porting/linux/user/src/malloc/malloc.c
porting/linux/user/src/malloc/malloc.c
+26
-6
未找到文件。
porting/linux/user/ldso/dynlink.c
浏览文件 @
66cc130b
...
...
@@ -33,6 +33,7 @@
#include "namespace.h"
#include "ns_config.h"
#include "pthread_impl.h"
#include "strops.h"
static
void
error
(
const
char
*
,
...);
...
...
@@ -814,7 +815,7 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
case
REL_TLSDESC
:
if
(
stride
<
3
)
addend
=
reloc_addr
[
1
];
if
(
def
.
dso
->
tls_id
>
static_tls_cnt
)
{
struct
td_index
*
new
=
malloc
(
sizeof
*
new
);
struct
td_index
*
new
=
internal_
malloc
(
sizeof
*
new
);
if
(
!
new
)
{
error
(
"Error relocating %s: cannot allocate TLSDESC for %s"
,
...
...
@@ -867,7 +868,7 @@ static void redo_lazy_relocs()
p
->
lazy_next
=
lazy_head
;
lazy_head
=
p
;
}
else
{
free
(
p
->
lazy
);
internal_
free
(
p
->
lazy
);
p
->
lazy
=
0
;
p
->
lazy_next
=
0
;
}
...
...
@@ -940,7 +941,7 @@ static void unmap_library(struct dso *dso)
munmap
((
void
*
)
dso
->
loadmap
->
segs
[
i
].
addr
,
dso
->
loadmap
->
segs
[
i
].
p_memsz
);
}
free
(
dso
->
loadmap
);
internal_
free
(
dso
->
loadmap
);
}
else
if
(
dso
->
map
&&
dso
->
map_len
)
{
munmap
(
dso
->
map
,
dso
->
map_len
);
}
...
...
@@ -972,7 +973,7 @@ static void *map_library(int fd, struct dso *dso, struct reserved_address_params
goto
noexec
;
phsize
=
eh
->
e_phentsize
*
eh
->
e_phnum
;
if
(
phsize
>
sizeof
buf
-
sizeof
*
eh
)
{
allocated_buf
=
malloc
(
phsize
);
allocated_buf
=
internal_
malloc
(
phsize
);
if
(
!
allocated_buf
)
return
0
;
l
=
pread
(
fd
,
allocated_buf
,
phsize
,
eh
->
e_phoff
);
if
(
l
<
0
)
goto
error
;
...
...
@@ -1019,7 +1020,7 @@ static void *map_library(int fd, struct dso *dso, struct reserved_address_params
}
if
(
!
dyn
)
goto
noexec
;
if
(
DL_FDPIC
&&
!
(
eh
->
e_flags
&
FDPIC_CONSTDISP_FLAG
))
{
dso
->
loadmap
=
calloc
(
1
,
sizeof
*
dso
->
loadmap
dso
->
loadmap
=
internal_
calloc
(
1
,
sizeof
*
dso
->
loadmap
+
nsegs
*
sizeof
*
dso
->
loadmap
->
segs
);
if
(
!
dso
->
loadmap
)
goto
error
;
dso
->
loadmap
->
nsegs
=
nsegs
;
...
...
@@ -1139,13 +1140,13 @@ done_mapping:
dso
->
base
=
base
;
dso
->
dynv
=
laddr
(
dso
,
dyn
);
if
(
dso
->
tls
.
size
)
dso
->
tls
.
image
=
laddr
(
dso
,
tls_image
);
free
(
allocated_buf
);
internal_
free
(
allocated_buf
);
return
map
;
noexec:
errno
=
ENOEXEC
;
error:
if
(
map
!=
MAP_FAILED
)
unmap_library
(
dso
);
free
(
allocated_buf
);
internal_
free
(
allocated_buf
);
return
0
;
}
...
...
@@ -1234,7 +1235,7 @@ static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
/* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
if
(
libc
.
secure
&&
*
origin
!=
'/'
)
return
0
;
p
->
rpath
=
malloc
(
strlen
(
p
->
rpath_orig
)
+
n
*
l
+
1
);
p
->
rpath
=
internal_
malloc
(
strlen
(
p
->
rpath_orig
)
+
n
*
l
+
1
);
if
(
!
p
->
rpath
)
return
-
1
;
d
=
p
->
rpath
;
...
...
@@ -1317,7 +1318,7 @@ static void makefuncdescs(struct dso *p)
p
->
funcdescs
=
dl_mmap
(
size
);
self_done
=
1
;
}
else
{
p
->
funcdescs
=
malloc
(
size
);
p
->
funcdescs
=
internal_
malloc
(
size
);
}
if
(
!
p
->
funcdescs
)
{
if
(
!
runtime
)
a_crash
();
...
...
@@ -1350,7 +1351,7 @@ static void get_sys_path(ns_configor *conf)
sys_path
=
sys_path_default
;
}
else
if
(
sys_path_default
)
{
size_t
newlen
=
strlen
(
sys_path
)
+
strlen
(
sys_path_default
)
+
2
;
char
*
new_syspath
=
malloc
(
newlen
);
char
*
new_syspath
=
internal_
malloc
(
newlen
);
memset
(
new_syspath
,
0
,
newlen
);
strcpy
(
new_syspath
,
sys_path
);
strcat
(
new_syspath
,
":"
);
...
...
@@ -1590,7 +1591,7 @@ struct dso *load_library(
if
(
n_th
>
SSIZE_MAX
/
per_th
)
alloc_size
=
SIZE_MAX
;
else
alloc_size
+=
n_th
*
per_th
;
}
p
=
calloc
(
1
,
alloc_size
);
p
=
internal_
calloc
(
1
,
alloc_size
);
if
(
!
p
)
{
unmap_library
(
&
temp_dso
);
return
0
;
...
...
@@ -1657,7 +1658,7 @@ static void load_direct_deps(struct dso *p, ns_t *namespace, struct reserved_add
/* Use builtin buffer for apps with no external deps, to
* preserve property of no runtime failure paths. */
p
->
deps
=
(
p
==
head
&&
cnt
<
2
)
?
builtin_deps
:
calloc
(
cnt
+
1
,
sizeof
*
p
->
deps
);
internal_
calloc
(
cnt
+
1
,
sizeof
*
p
->
deps
);
if
(
!
p
->
deps
)
{
error
(
"Error loading dependencies for %s"
,
p
->
name
);
if
(
runtime
)
longjmp
(
*
rtld_fail
,
1
);
...
...
@@ -1715,8 +1716,8 @@ static void extend_bfs_deps(struct dso *p)
for
(
j
=
cnt
=
0
;
j
<
dep
->
ndeps_direct
;
j
++
)
if
(
!
dep
->
deps
[
j
]
->
mark
)
cnt
++
;
tmp
=
no_realloc
?
malloc
(
sizeof
(
*
tmp
)
*
(
ndeps_all
+
cnt
+
1
))
:
realloc
(
p
->
deps
,
sizeof
(
*
tmp
)
*
(
ndeps_all
+
cnt
+
1
));
internal_
malloc
(
sizeof
(
*
tmp
)
*
(
ndeps_all
+
cnt
+
1
))
:
internal_
realloc
(
p
->
deps
,
sizeof
(
*
tmp
)
*
(
ndeps_all
+
cnt
+
1
));
if
(
!
tmp
)
{
error
(
"Error recording dependencies for %s"
,
p
->
name
);
if
(
runtime
)
longjmp
(
*
rtld_fail
,
1
);
...
...
@@ -1906,7 +1907,7 @@ static struct dso **queue_ctors(struct dso *dso)
if
(
dso
==
head
&&
cnt
<=
countof
(
builtin_ctor_queue
))
queue
=
builtin_ctor_queue
;
else
queue
=
calloc
(
cnt
,
sizeof
*
queue
);
queue
=
internal_
calloc
(
cnt
,
sizeof
*
queue
);
if
(
!
queue
)
{
error
(
"Error allocating constructor queue: %m
\n
"
);
...
...
@@ -1989,7 +1990,7 @@ void __libc_start_init(void)
{
do_init_fini
(
main_ctor_queue
);
if
(
!
__malloc_replaced
&&
main_ctor_queue
!=
builtin_ctor_queue
)
free
(
main_ctor_queue
);
internal_
free
(
main_ctor_queue
);
main_ctor_queue
=
0
;
}
...
...
@@ -2405,7 +2406,7 @@ void __dls3(size_t *sp, size_t *auxv)
update_tls_size
();
void
*
initial_tls
=
builtin_tls
;
if
(
libc
.
tls_size
>
sizeof
builtin_tls
||
tls_align
>
MIN_TLS_ALIGN
)
{
initial_tls
=
calloc
(
libc
.
tls_size
,
1
);
initial_tls
=
internal_
calloc
(
libc
.
tls_size
,
1
);
if
(
!
initial_tls
)
{
dprintf
(
2
,
"%s: Error getting %zu bytes thread-local storage: %m
\n
"
,
argv
[
0
],
libc
.
tls_size
);
...
...
@@ -2481,7 +2482,7 @@ static void prepare_lazy(struct dso *p)
size_t
i
=
0
;
search_vec
(
p
->
dynv
,
&
i
,
DT_MIPS_SYMTABNO
);
n
+=
i
-
j
;
}
p
->
lazy
=
calloc
(
n
,
3
*
sizeof
(
size_t
));
p
->
lazy
=
internal_
calloc
(
n
,
3
*
sizeof
(
size_t
));
if
(
!
p
->
lazy
)
{
error
(
"Error preparing lazy relocation for %s: %m"
,
p
->
name
);
longjmp
(
*
rtld_fail
,
1
);
...
...
@@ -2573,18 +2574,18 @@ static void *dlopen_impl(
next
=
p
->
next
;
while
(
p
->
td_index
)
{
void
*
tmp
=
p
->
td_index
->
next
;
free
(
p
->
td_index
);
internal_
free
(
p
->
td_index
);
p
->
td_index
=
tmp
;
}
free
(
p
->
funcdescs
);
internal_
free
(
p
->
funcdescs
);
if
(
p
->
rpath
!=
p
->
rpath_orig
)
free
(
p
->
rpath
);
free
(
p
->
deps
);
internal_
free
(
p
->
rpath
);
internal_
free
(
p
->
deps
);
dlclose_ns
(
p
);
unmap_library
(
p
);
free
(
p
);
internal_
free
(
p
);
}
free
(
ctor_queue
);
internal_
free
(
ctor_queue
);
ctor_queue
=
0
;
if
(
!
orig_tls_tail
)
libc
.
tls_head
=
0
;
tls_tail
=
orig_tls_tail
;
...
...
@@ -2720,7 +2721,7 @@ end:
pthread_rwlock_unlock
(
&
lock
);
if
(
ctor_queue
)
{
do_init_fini
(
ctor_queue
);
free
(
ctor_queue
);
internal_
free
(
ctor_queue
);
}
pthread_setcancelstate
(
cs
,
0
);
#ifdef HANDLE_RANDOMIZATION
...
...
@@ -3032,11 +3033,11 @@ static int do_dlclose(struct dso *p)
dlclose_ns
(
p
);
if
(
p
->
lazy
!=
NULL
)
free
(
p
->
lazy
);
internal_
free
(
p
->
lazy
);
if
(
p
->
deps
!=
no_deps
)
free
(
p
->
deps
);
internal_
free
(
p
->
deps
);
unmap_library
(
p
);
free
(
p
);
internal_
free
(
p
);
return
0
;
}
...
...
@@ -3344,14 +3345,14 @@ int handle_asan_path_open(int fd, const char *name, ns_t *namespace, char *buf,
if
(
fd
==
-
1
&&
(
namespace
->
asan_lib_paths
||
namespace
->
lib_paths
))
{
if
(
namespace
->
lib_paths
&&
namespace
->
asan_lib_paths
)
{
size_t
newlen
=
strlen
(
namespace
->
asan_lib_paths
)
+
strlen
(
namespace
->
lib_paths
)
+
2
;
char
*
new_lib_paths
=
malloc
(
newlen
);
char
*
new_lib_paths
=
internal_
malloc
(
newlen
);
memset
(
new_lib_paths
,
0
,
newlen
);
strcpy
(
new_lib_paths
,
namespace
->
asan_lib_paths
);
strcat
(
new_lib_paths
,
":"
);
strcat
(
new_lib_paths
,
namespace
->
lib_paths
);
fd_tmp
=
path_open
(
name
,
new_lib_paths
,
buf
,
buf_size
);
LD_LOGD
(
"handle_asan_path_open path_open new_lib_paths:%{public}s ,fd: %{public}d."
,
new_lib_paths
,
fd_tmp
);
free
(
new_lib_paths
);
internal_
free
(
new_lib_paths
);
}
else
if
(
namespace
->
asan_lib_paths
)
{
fd_tmp
=
path_open
(
name
,
namespace
->
asan_lib_paths
,
buf
,
buf_size
);
LD_LOGD
(
"handle_asan_path_open path_open asan_lib_paths:%{public}s ,fd: %{public}d."
,
...
...
@@ -3402,7 +3403,7 @@ static bool map_library_header(struct loadtask *task)
}
task
->
phsize
=
task
->
eh
->
e_phentsize
*
task
->
eh
->
e_phnum
;
if
(
task
->
phsize
>
sizeof
task
->
ehdr_buf
-
sizeof
(
Ehdr
))
{
task
->
allocated_buf
=
malloc
(
task
->
phsize
);
task
->
allocated_buf
=
internal_
malloc
(
task
->
phsize
);
if
(
!
task
->
allocated_buf
)
{
LD_LOGE
(
"Error mapping header %{public}s: failed to alloc memory"
,
task
->
name
);
return
false
;
...
...
@@ -3487,7 +3488,7 @@ static bool map_library_header(struct loadtask *task)
noexec:
errno
=
ENOEXEC
;
error:
free
(
task
->
allocated_buf
);
internal_
free
(
task
->
allocated_buf
);
task
->
allocated_buf
=
NULL
;
return
false
;
}
...
...
@@ -3536,7 +3537,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para
goto
noexec
;
}
if
(
DL_FDPIC
&&
!
(
task
->
eh
->
e_flags
&
FDPIC_CONSTDISP_FLAG
))
{
task
->
p
->
loadmap
=
calloc
(
1
,
sizeof
(
struct
fdpic_loadmap
)
+
nsegs
*
sizeof
(
struct
fdpic_loadseg
));
task
->
p
->
loadmap
=
internal_
calloc
(
1
,
sizeof
(
struct
fdpic_loadmap
)
+
nsegs
*
sizeof
(
struct
fdpic_loadseg
));
if
(
!
task
->
p
->
loadmap
)
{
goto
error
;
}
...
...
@@ -3681,7 +3682,7 @@ done_mapping:
if
(
task
->
p
->
tls
.
size
)
{
task
->
p
->
tls
.
image
=
laddr
(
task
->
p
,
task
->
tls_image
);
}
free
(
task
->
allocated_buf
);
internal_
free
(
task
->
allocated_buf
);
task
->
allocated_buf
=
NULL
;
return
true
;
noexec:
...
...
@@ -3690,7 +3691,7 @@ error:
if
(
map
!=
MAP_FAILED
)
{
unmap_library
(
task
->
p
);
}
free
(
task
->
allocated_buf
);
internal_
free
(
task
->
allocated_buf
);
task
->
allocated_buf
=
NULL
;
return
false
;
}
...
...
@@ -3866,7 +3867,7 @@ static bool load_library_header(struct loadtask *task)
alloc_size
+=
n_th
*
per_th
;
}
}
task
->
p
=
calloc
(
1
,
alloc_size
);
task
->
p
=
internal_
calloc
(
1
,
alloc_size
);
if
(
!
task
->
p
)
{
LD_LOGE
(
"Error loading header %{public}s: failed to allocate dso"
,
task
->
name
);
close
(
task
->
fd
);
...
...
@@ -3927,8 +3928,8 @@ static void task_load_library(struct loadtask *task, struct reserved_address_par
find_sym
(
task
->
p
,
"stdin"
,
1
).
sym
)
{
do_dlclose
(
task
->
p
);
task
->
p
=
NULL
;
free
((
void
*
)
task
->
name
);
task
->
name
=
strdup
(
"libc.so"
);
internal_
free
((
void
*
)
task
->
name
);
task
->
name
=
ld_
strdup
(
"libc.so"
);
task
->
check_inherited
=
true
;
if
(
!
load_library_header
(
task
))
{
LD_LOGE
(
"Error loading library %{public}s: failed to load libc.so"
,
task
->
name
);
...
...
@@ -3980,7 +3981,7 @@ static void preload_direct_deps(struct dso *p, ns_t *namespace, struct loadtasks
/* Use builtin buffer for apps with no external deps, to
* preserve property of no runtime failure paths. */
p
->
deps
=
(
p
==
head
&&
cnt
<
MIN_DEPS_COUNT
)
?
builtin_deps
:
calloc
(
cnt
+
1
,
sizeof
*
p
->
deps
);
internal_
calloc
(
cnt
+
1
,
sizeof
*
p
->
deps
);
if
(
!
p
->
deps
)
{
LD_LOGE
(
"Error loading dependencies for %{public}s"
,
p
->
name
);
error
(
"Error loading dependencies for %s"
,
p
->
name
);
...
...
porting/linux/user/ldso/dynlink_rand.c
浏览文件 @
66cc130b
...
...
@@ -20,6 +20,8 @@
#include <sys/random.h>
#include <unistd.h>
#include "malloc_impl.h"
#define HANDLE_INCREASE 2
#define TASK_BASE_CAPACITY 8
...
...
@@ -42,7 +44,7 @@ static bool is_first_stage_init(void)
void
*
add_handle_node
(
void
*
handle
,
struct
dso
*
dso
)
{
struct
handle_node
*
node
=
malloc
(
sizeof
(
*
node
));
struct
handle_node
*
node
=
internal_
malloc
(
sizeof
(
*
node
));
if
(
!
node
)
{
return
NULL
;
}
...
...
@@ -88,7 +90,7 @@ void remove_handle_node(void *handle)
}
else
{
handle_map_list
=
node
->
next
;
}
free
(
node
);
internal_
free
(
node
);
return
;
}
else
{
pre_node
=
node
;
...
...
@@ -124,7 +126,7 @@ void *assign_valid_handle(struct dso *p)
struct
loadtasks
*
create_loadtasks
(
void
)
{
struct
loadtasks
*
tasks
=
malloc
(
sizeof
(
struct
loadtasks
));
struct
loadtasks
*
tasks
=
internal_
malloc
(
sizeof
(
struct
loadtasks
));
if
(
tasks
)
{
tasks
->
array
=
NULL
;
tasks
->
capacity
=
0
;
...
...
@@ -141,9 +143,9 @@ bool append_loadtasks(struct loadtasks *tasks, struct loadtask *item)
new_cap
=
tasks
->
capacity
+
TASK_BASE_CAPACITY
;
void
*
realloced
=
NULL
;
if
(
tasks
->
array
)
{
realloced
=
realloc
(
tasks
->
array
,
new_cap
*
sizeof
(
struct
loadtask
*
));
realloced
=
internal_
realloc
(
tasks
->
array
,
new_cap
*
sizeof
(
struct
loadtask
*
));
}
else
{
realloced
=
malloc
(
TASK_BASE_CAPACITY
*
sizeof
(
struct
loadtask
*
));
realloced
=
internal_
malloc
(
TASK_BASE_CAPACITY
*
sizeof
(
struct
loadtask
*
));
}
if
(
realloced
)
{
tasks
->
array
=
realloced
;
...
...
@@ -163,11 +165,11 @@ void free_task(struct loadtask *task)
return
;
}
if
(
task
->
name
)
{
free
(
task
->
name
);
internal_
free
(
task
->
name
);
task
->
name
=
NULL
;
}
if
(
task
->
allocated_buf
)
{
free
(
task
->
allocated_buf
);
internal_
free
(
task
->
allocated_buf
);
task
->
allocated_buf
=
NULL
;
}
if
(
task
->
dyn_map_len
)
{
...
...
@@ -184,7 +186,7 @@ void free_task(struct loadtask *task)
close
(
task
->
fd
);
task
->
fd
=
-
1
;
}
free
(
task
);
internal_
free
(
task
);
}
struct
loadtask
*
get_loadtask
(
struct
loadtasks
*
tasks
,
size_t
index
)
...
...
@@ -206,11 +208,11 @@ void free_loadtasks(struct loadtasks *tasks)
tasks
->
length
=
0
;
}
if
(
tasks
->
array
)
{
free
(
tasks
->
array
);
internal_
free
(
tasks
->
array
);
tasks
->
array
=
NULL
;
}
tasks
->
capacity
=
0
;
free
(
tasks
);
internal_
free
(
tasks
);
}
}
...
...
@@ -233,11 +235,11 @@ void shuffle_loadtasks(struct loadtasks *tasks)
struct
loadtask
*
create_loadtask
(
const
char
*
name
,
struct
dso
*
needed_by
,
ns_t
*
ns
,
bool
check_inherited
)
{
size_t
name_len
=
strlen
(
name
);
char
*
name_buf
=
(
char
*
)
malloc
(
name_len
+
1
);
char
*
name_buf
=
(
char
*
)
internal_
malloc
(
name_len
+
1
);
if
(
!
name_buf
)
{
return
NULL
;
}
struct
loadtask
*
task
=
calloc
(
1
,
sizeof
(
struct
loadtask
));
struct
loadtask
*
task
=
internal_
calloc
(
1
,
sizeof
(
struct
loadtask
));
if
(
!
task
)
{
return
NULL
;
}
...
...
porting/linux/user/ldso/namespace.c
浏览文件 @
66cc130b
/*
* Copyright (c) 2022 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 "namespace.h"
#include "ld_log.h"
#include "malloc_impl.h"
#include "strops.h"
static
ns_t
g_ns_default
;
...
...
@@ -19,14 +35,14 @@ static nslist g_ns_list;
static
ns_inherit_list
*
nsinherits_alloc
()
{
ns_inherit_list
*
nsinl
;
nsinl
=
(
ns_inherit_list
*
)
calloc
(
1
,
sizeof
*
nsinl
);
nsinl
=
(
ns_inherit_list
*
)
internal_
calloc
(
1
,
sizeof
*
nsinl
);
if
(
nsinl
)
{
nsinl
->
size
=
INHERIT_DEFAULT_SIZE
;
nsinl
->
inherits
=
(
ns_inherit
**
)
calloc
(
INHERIT_DEFAULT_SIZE
,
sizeof
*
nsinl
->
inherits
);
nsinl
->
inherits
=
(
ns_inherit
**
)
internal_
calloc
(
INHERIT_DEFAULT_SIZE
,
sizeof
*
nsinl
->
inherits
);
if
(
!
nsinl
->
inherits
)
{
LD_LOGE
(
"nsinherits_alloc failed,return NULL!"
);
free
(
nsinl
);
internal_
free
(
nsinl
);
nsinl
=
NULL
;
}
}
...
...
@@ -40,10 +56,10 @@ static void nsinherits_free(ns_inherit_list *nsinl)
}
for
(
size_t
i
=
0
;
i
<
nsinl
->
num
;
i
++
)
{
strlist_free
(
nsinl
->
inherits
[
i
]
->
shared_libs
);
free
(
nsinl
->
inherits
[
i
]);
internal_
free
(
nsinl
->
inherits
[
i
]);
}
free
(
nsinl
->
inherits
);
free
(
nsinl
);
internal_
free
(
nsinl
->
inherits
);
internal_
free
(
nsinl
);
}
static
void
nsinherits_realloc
(
ns_inherit_list
*
nsinl
)
...
...
@@ -54,7 +70,7 @@ static void nsinherits_realloc(ns_inherit_list *nsinl)
size_t
size
=
2
*
nsinl
->
size
;
if
(
size
)
{
ns_inherit
**
inherits
;
inherits
=
(
ns_inherit
**
)
realloc
(
nsinl
->
inherits
,
size
*
(
sizeof
*
nsinl
->
inherits
));
inherits
=
(
ns_inherit
**
)
internal_
realloc
(
nsinl
->
inherits
,
size
*
(
sizeof
*
nsinl
->
inherits
));
if
(
!
inherits
)
{
LD_LOGE
(
"nsinherits_realloc failed!"
);
return
;
...
...
@@ -68,14 +84,14 @@ static void nsinherits_realloc(ns_inherit_list *nsinl)
static
dsolist
*
dsolist_alloc
()
{
dsolist
*
dsol
;
dsol
=
(
dsolist
*
)
calloc
(
1
,
sizeof
*
dsol
);
dsol
=
(
dsolist
*
)
internal_
calloc
(
1
,
sizeof
*
dsol
);
if
(
dsol
)
{
dsol
->
size
=
DSOLIST_DEFAULT_SIZE
;
dsol
->
dsos
=
(
struct
dso
**
)
calloc
(
DSOLIST_DEFAULT_SIZE
,
sizeof
*
dsol
->
dsos
);
dsol
->
dsos
=
(
struct
dso
**
)
internal_
calloc
(
DSOLIST_DEFAULT_SIZE
,
sizeof
*
dsol
->
dsos
);
if
(
!
dsol
->
dsos
)
{
LD_LOGE
(
"dsolist_alloc failed,return NULL!"
);
free
(
dsol
);
internal_
free
(
dsol
);
dsol
=
NULL
;
}
}
...
...
@@ -90,7 +106,7 @@ static void dsolist_realloc(dsolist *dsol)
size_t
size
=
2
*
dsol
->
size
;
if
(
size
)
{
struct
dso
**
ds
;
ds
=
(
struct
dso
**
)
realloc
(
dsol
->
dsos
,
size
*
(
sizeof
*
dsol
->
dsos
));
ds
=
(
struct
dso
**
)
internal_
realloc
(
dsol
->
dsos
,
size
*
(
sizeof
*
dsol
->
dsos
));
if
(
!
ds
)
{
LD_LOGE
(
"dsolist_realloc failed!"
);
return
;
...
...
@@ -103,11 +119,11 @@ static void dsolist_realloc(dsolist *dsol)
ns_t
*
ns_alloc
()
{
ns_t
*
nst
=
(
ns_t
*
)
calloc
(
1
,
sizeof
*
nst
);
ns_t
*
nst
=
(
ns_t
*
)
internal_
calloc
(
1
,
sizeof
*
nst
);
nst
->
ns_dsos
=
dsolist_alloc
();
if
(
!
nst
->
ns_dsos
)
{
LD_LOGE
(
"ns_alloc failed,return NULL!"
);
free
(
nst
);
internal_
free
(
nst
);
nst
=
NULL
;
}
return
nst
;
...
...
@@ -118,15 +134,15 @@ void ns_free(ns_t *ns)
if
(
!
ns
)
{
return
;
}
free
(
ns
->
ns_name
);
free
(
ns
->
env_paths
);
free
(
ns
->
lib_paths
);
free
(
ns
->
asan_lib_paths
);
internal_
free
(
ns
->
ns_name
);
internal_
free
(
ns
->
env_paths
);
internal_
free
(
ns
->
lib_paths
);
internal_
free
(
ns
->
asan_lib_paths
);
strlist_free
(
ns
->
permitted_paths
);
strlist_free
(
ns
->
asan_permitted_paths
);
strlist_free
(
ns
->
allowed_libs
);
nsinherits_free
(
ns
->
ns_inherits
);
free
(
ns
);
internal_
free
(
ns
);
}
void
ns_add_dso
(
ns_t
*
ns
,
struct
dso
*
dso
)
...
...
@@ -156,7 +172,7 @@ nslist *nslist_init()
{
g_ns_list
.
size
=
NSLIST_DEFAULT_SIZE
;
g_ns_list
.
num
=
0
;
g_ns_list
.
nss
=
(
ns_t
**
)
calloc
(
NSLIST_DEFAULT_SIZE
,
sizeof
*
g_ns_list
.
nss
);
g_ns_list
.
nss
=
(
ns_t
**
)
internal_
calloc
(
NSLIST_DEFAULT_SIZE
,
sizeof
*
g_ns_list
.
nss
);
if
(
!
g_ns_list
.
nss
)
{
LD_LOGE
(
"nslist_init failed!"
);
return
NULL
;
...
...
@@ -169,7 +185,7 @@ static void nslist_realloc()
size_t
size
=
2
*
g_ns_list
.
size
;
if
(
size
)
{
ns_t
**
nss
;
nss
=
(
ns_t
**
)
realloc
(
g_ns_list
.
nss
,
size
*
(
sizeof
*
g_ns_list
.
nss
));
nss
=
(
ns_t
**
)
internal_
realloc
(
g_ns_list
.
nss
,
size
*
(
sizeof
*
g_ns_list
.
nss
));
if
(
!
nss
)
{
LD_LOGE
(
"nslist_realloc failed!"
);
return
;
...
...
@@ -209,8 +225,8 @@ void ns_set_name(ns_t *ns, const char *name)
if
(
!
ns
||
!
name
)
{
return
;
}
if
(
ns
->
ns_name
)
free
(
ns
->
ns_name
);
ns
->
ns_name
=
strdup
(
name
);
if
(
ns
->
ns_name
)
internal_
free
(
ns
->
ns_name
);
ns
->
ns_name
=
ld_
strdup
(
name
);
strtrim
(
ns
->
ns_name
);
LD_LOGD
(
"ns_set_name ns_name:%{public}s."
,
ns
->
ns_name
);
}
...
...
@@ -220,9 +236,9 @@ void ns_set_env_paths(ns_t *ns, const char *env_paths)
if
(
!
ns
)
{
return
;
}
if
(
ns
->
env_paths
)
free
(
ns
->
env_paths
);
if
(
ns
->
env_paths
)
internal_
free
(
ns
->
env_paths
);
if
(
env_paths
)
{
ns
->
env_paths
=
strdup
(
env_paths
);
ns
->
env_paths
=
ld_
strdup
(
env_paths
);
strtrim
(
ns
->
env_paths
);
}
else
{
ns
->
env_paths
=
NULL
;
...
...
@@ -235,9 +251,9 @@ void ns_set_lib_paths(ns_t *ns, const char *lib_paths)
if
(
!
ns
)
{
return
;
}
if
(
ns
->
lib_paths
)
free
(
ns
->
lib_paths
);
if
(
ns
->
lib_paths
)
internal_
free
(
ns
->
lib_paths
);
if
(
lib_paths
)
{
ns
->
lib_paths
=
strdup
(
lib_paths
);
ns
->
lib_paths
=
ld_
strdup
(
lib_paths
);
strtrim
(
ns
->
lib_paths
);
}
else
{
ns
->
lib_paths
=
NULL
;
...
...
@@ -251,10 +267,10 @@ void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths)
return
;
}
if
(
ns
->
asan_lib_paths
)
{
free
(
ns
->
asan_lib_paths
);
internal_
free
(
ns
->
asan_lib_paths
);
}
if
(
asan_lib_paths
)
{
ns
->
asan_lib_paths
=
strdup
(
asan_lib_paths
);
ns
->
asan_lib_paths
=
ld_
strdup
(
asan_lib_paths
);
strtrim
(
ns
->
asan_lib_paths
);
}
else
{
ns
->
asan_lib_paths
=
NULL
;
...
...
@@ -305,9 +321,9 @@ void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs)
ns
->
allowed_libs
=
NULL
;
if
(
allowed_libs
)
{
/* if setted and not empty, split to list. */
char
*
a_libs
=
strdup
(
allowed_libs
);
char
*
a_libs
=
ld_
strdup
(
allowed_libs
);
if
(
strtrim
(
a_libs
)
>
0
)
ns
->
allowed_libs
=
strsplit
(
a_libs
,
":"
);
free
(
a_libs
);
internal_
free
(
a_libs
);
}
LD_LOGD
(
"ns_set_allowed_libs ns[%{public}s] allowed_libs:%{public}s."
,
ns
->
ns_name
,
allowed_libs
);
}
...
...
@@ -354,7 +370,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
ns_inherit
*
inherit
=
find_ns_inherit
(
ns
,
ns_inherited
);
if
(
!
inherit
)
{
inherit
=
calloc
(
1
,
sizeof
*
inherit
);
inherit
=
internal_
calloc
(
1
,
sizeof
*
inherit
);
if
(
!
inherit
)
{
LD_LOGE
(
"ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] calloc failed!"
,
ns
->
ns_name
,
...
...
@@ -375,9 +391,9 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
/* if setted and not empty, split to list. */
if
(
shared_libs
)
{
char
*
s_libs
=
strdup
(
shared_libs
);
char
*
s_libs
=
ld_
strdup
(
shared_libs
);
if
(
strtrim
(
s_libs
)
>
0
)
inherit
->
shared_libs
=
strsplit
(
shared_libs
,
":"
);
free
(
s_libs
);
internal_
free
(
s_libs
);
}
if
(
!
need_add
)
{
...
...
@@ -395,7 +411,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
LD_LOGD
(
"ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] nsinherits_alloc failed!"
,
ns
->
ns_name
,
ns_inherited
->
ns_name
);
free
(
inherit
);
internal_
free
(
inherit
);
return
;
}
...
...
@@ -420,7 +436,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
ns
->
ns_name
,
ns_inherited
->
ns_name
);
if
(
inherit
->
shared_libs
)
strlist_free
(
inherit
->
shared_libs
);
free
(
inherit
);
internal_
free
(
inherit
);
}
return
;
}
...
...
porting/linux/user/ldso/namespace.h
浏览文件 @
66cc130b
/*
* Copyright (c) 2022 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 _NAMESPACE_H
#define _NAMESPACE_H
...
...
porting/linux/user/ldso/ns_config.c
浏览文件 @
66cc130b
/*-------------------------------------------------------------------------*/
/**
@file ns_config.c
@author yangwenjun
@brief deal with ld-musl-namespace.ini file
*/
/*
* Copyright (c) 2022 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 "ns_config.h"
#include <ctype.h>
#include <stdarg.h>
#include "ld_log.h"
#include "malloc_impl.h"
/*---------------------------- Defines -------------------------------------*/
#define MAX_LINE_SIZE (1024)
#define INI_INVALID_KEY ((char*)-1)
...
...
@@ -102,16 +112,16 @@ static kvlist * kvlist_alloc(size_t size)
if
(
size
<
KV_DEFAULT_SIZE
)
size
=
KV_DEFAULT_SIZE
;
kvs
=
(
kvlist
*
)
calloc
(
1
,
sizeof
*
kvs
);
kvs
=
(
kvlist
*
)
internal_
calloc
(
1
,
sizeof
*
kvs
);
if
(
kvs
)
{
kvs
->
key
=
(
char
**
)
calloc
(
size
,
sizeof
*
kvs
->
key
);
kvs
->
val
=
(
char
**
)
calloc
(
size
,
sizeof
*
kvs
->
val
);
kvs
->
key
=
(
char
**
)
internal_
calloc
(
size
,
sizeof
*
kvs
->
key
);
kvs
->
val
=
(
char
**
)
internal_
calloc
(
size
,
sizeof
*
kvs
->
val
);
if
(
kvs
->
key
&&
kvs
->
val
)
{
kvs
->
size
=
size
;
}
else
{
free
(
kvs
->
key
);
free
(
kvs
->
val
);
free
(
kvs
);
internal_
free
(
kvs
->
key
);
internal_
free
(
kvs
->
val
);
internal_
free
(
kvs
);
kvs
=
NULL
;
}
}
...
...
@@ -124,10 +134,10 @@ static void kvlist_realloc(kvlist *kvs)
size_t
size
=
2
*
kvs
->
size
;
if
(
size
)
{
char
**
keys
,
**
vals
;
keys
=
(
char
**
)
realloc
(
kvs
->
key
,
size
*
(
sizeof
*
kvs
->
key
));
keys
=
(
char
**
)
internal_
realloc
(
kvs
->
key
,
size
*
(
sizeof
*
kvs
->
key
));
if
(
!
keys
)
return
;
kvs
->
key
=
keys
;
vals
=
(
char
**
)
realloc
(
kvs
->
val
,
size
*
(
sizeof
*
kvs
->
val
));
vals
=
(
char
**
)
internal_
realloc
(
kvs
->
val
,
size
*
(
sizeof
*
kvs
->
val
));
if
(
!
vals
)
return
;
kvs
->
val
=
vals
;
kvs
->
size
=
size
;
...
...
@@ -141,12 +151,12 @@ static void kvlist_free(kvlist *kvs)
size_t
i
;
if
(
!
kvs
)
return
;
for
(
i
=
0
;
i
<
kvs
->
num
;
i
++
)
{
free
(
kvs
->
key
[
i
]);
free
(
kvs
->
val
[
i
]);
internal_
free
(
kvs
->
key
[
i
]);
internal_
free
(
kvs
->
val
[
i
]);
}
free
(
kvs
->
key
);
free
(
kvs
->
val
);
free
(
kvs
);
internal_
free
(
kvs
->
key
);
internal_
free
(
kvs
->
val
);
internal_
free
(
kvs
);
}
static
section_list
*
sections_alloc
(
size_t
size
)
...
...
@@ -154,17 +164,17 @@ static section_list *sections_alloc(size_t size)
section_list
*
sections
;
if
(
size
<
SECTION_DEFAULT_SIZE
)
size
=
SECTION_DEFAULT_SIZE
;
sections
=
(
section_list
*
)
calloc
(
1
,
sizeof
*
sections
);
sections
=
(
section_list
*
)
internal_
calloc
(
1
,
sizeof
*
sections
);
if
(
sections
)
{
sections
->
names
=
(
char
**
)
calloc
(
size
,
sizeof
*
sections
->
names
);
sections
->
kvs
=
(
kvlist
**
)
calloc
(
size
,
sizeof
*
sections
->
kvs
);
sections
->
names
=
(
char
**
)
internal_
calloc
(
size
,
sizeof
*
sections
->
names
);
sections
->
kvs
=
(
kvlist
**
)
internal_
calloc
(
size
,
sizeof
*
sections
->
kvs
);
if
(
sections
->
names
&&
sections
->
kvs
)
{
sections
->
size
=
size
;
}
else
{
free
(
sections
->
names
);
free
(
sections
->
kvs
);
free
(
sections
);
internal_
free
(
sections
->
names
);
internal_
free
(
sections
->
kvs
);
internal_
free
(
sections
);
sections
=
NULL
;
}
}
...
...
@@ -178,10 +188,10 @@ static void sections_realloc(section_list *sections)
if
(
size
)
{
char
**
names
;
kvlist
**
kvs
;
names
=
(
char
**
)
realloc
(
sections
->
names
,
size
*
(
sizeof
*
sections
->
names
));
names
=
(
char
**
)
internal_
realloc
(
sections
->
names
,
size
*
(
sizeof
*
sections
->
names
));
if
(
!
names
)
return
;
sections
->
names
=
names
;
kvs
=
(
kvlist
**
)
realloc
(
sections
->
kvs
,
size
*
(
sizeof
*
sections
->
kvs
));
kvs
=
(
kvlist
**
)
internal_
realloc
(
sections
->
kvs
,
size
*
(
sizeof
*
sections
->
kvs
));
if
(
!
kvs
)
return
;
sections
->
kvs
=
kvs
;
sections
->
size
=
size
;
...
...
@@ -193,12 +203,12 @@ static void sections_free(section_list *sections)
{
if
(
!
sections
)
return
;
for
(
size_t
i
=
0
;
i
<
sections
->
num
;
i
++
)
{
free
(
sections
->
names
[
i
]);
internal_
free
(
sections
->
names
[
i
]);
kvlist_free
(
sections
->
kvs
[
i
]);
}
free
(
sections
->
names
);
free
(
sections
->
kvs
);
free
(
sections
);
internal_
free
(
sections
->
names
);
internal_
free
(
sections
->
kvs
);
internal_
free
(
sections
);
}
static
void
kvlist_set
(
kvlist
*
kvs
,
const
char
*
key
,
const
char
*
val
)
...
...
@@ -213,9 +223,9 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val)
}
if
(
i
<
kvs
->
num
)
{
char
*
v
=
strdup
(
val
);
char
*
v
=
ld_
strdup
(
val
);
if
(
v
)
{
free
(
kvs
->
val
[
i
]);
internal_
free
(
kvs
->
val
[
i
]);
kvs
->
val
[
i
]
=
v
;
}
return
;
...
...
@@ -224,13 +234,13 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val)
kvlist_realloc
(
kvs
);
}
if
(
kvs
->
num
<
kvs
->
size
)
{
kvs
->
key
[
kvs
->
num
]
=
strdup
(
key
);
kvs
->
val
[
kvs
->
num
]
=
strdup
(
val
);
kvs
->
key
[
kvs
->
num
]
=
ld_
strdup
(
key
);
kvs
->
val
[
kvs
->
num
]
=
ld_
strdup
(
val
);
if
(
kvs
->
key
[
kvs
->
num
]
&&
kvs
->
val
[
kvs
->
num
])
{
kvs
->
num
++
;
}
else
{
free
(
kvs
->
key
[
kvs
->
num
]);
free
(
kvs
->
val
[
kvs
->
num
]);
internal_
free
(
kvs
->
key
[
kvs
->
num
]);
internal_
free
(
kvs
->
val
[
kvs
->
num
]);
}
}
return
;
...
...
@@ -258,13 +268,13 @@ static void sections_set(section_list *sections, const char *name, const char *k
if
(
sections
->
num
<
sections
->
size
)
{
kvs
=
kvlist_alloc
(
0
);
sections
->
names
[
sections
->
num
]
=
strdup
(
name
);
sections
->
names
[
sections
->
num
]
=
ld_
strdup
(
name
);
sections
->
kvs
[
sections
->
num
]
=
kvs
;
if
(
sections
->
names
[
sections
->
num
]
&&
kvs
)
{
sections
->
num
++
;
kvlist_set
(
kvs
,
key
,
val
);
}
else
{
free
(
sections
->
names
[
sections
->
num
]);
internal_
free
(
sections
->
names
[
sections
->
num
]);
kvlist_free
(
kvs
);
}
}
...
...
@@ -465,10 +475,10 @@ static int config_parse(const char *file_path, const char *exe_path)
kvlist
*
dirkvs
;
kvlist
*
acquiescence_kvs
;
if
(
!
exe_path
)
return
-
1
;
g_configor
.
exe_path
=
strdup
(
exe_path
);
g_configor
.
exe_path
=
ld_
strdup
(
exe_path
);
const
char
*
fpath
=
CONFIG_DEFAULT_FILE
;
if
(
file_path
)
fpath
=
file_path
;
g_configor
.
file_path
=
strdup
(
fpath
);
g_configor
.
file_path
=
ld_
strdup
(
fpath
);
g_configor
.
sections
=
config_load
(
fpath
);
if
(
!
g_configor
.
sections
)
{
...
...
@@ -655,11 +665,11 @@ void configor_free()
g_configor
.
sections
=
NULL
;
}
if
(
g_configor
.
file_path
)
{
free
(
g_configor
.
file_path
);
internal_
free
(
g_configor
.
file_path
);
g_configor
.
file_path
=
NULL
;
}
if
(
g_configor
.
exe_path
)
{
free
(
g_configor
.
exe_path
);
internal_
free
(
g_configor
.
exe_path
);
g_configor
.
exe_path
=
NULL
;
}
}
\ No newline at end of file
porting/linux/user/ldso/ns_config.h
浏览文件 @
66cc130b
/*
* Copyright (c) 2022 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 _NS_CONFIG_H
#define _NS_CONFIG_H
...
...
porting/linux/user/ldso/strops.c
浏览文件 @
66cc130b
/*
* Copyright (c) 2022 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 <ctype.h>
#include "strops.h"
#include "malloc_impl.h"
/* string to lower */
void
strlwc
(
char
*
str
)
...
...
@@ -37,10 +53,10 @@ strlist *strsplit(const char *str, const char *split_s)
if
(
!
str
)
return
NULL
;
strlist
*
sl
=
strlist_alloc
(
0
);
char
*
ss
=
strdup
(
str
);
char
*
ss
=
ld_
strdup
(
str
);
if
(
!
sl
||
!
ss
)
{
strlist_free
(
sl
);
free
(
ss
);
internal_
free
(
ss
);
return
NULL
;
}
...
...
@@ -53,7 +69,7 @@ strlist *strsplit(const char *str, const char *split_s)
}
strtrim
(
cur
);
strlist_set
(
sl
,
cur
);
free
(
ss
);
internal_
free
(
ss
);
return
sl
;
}
...
...
@@ -64,14 +80,14 @@ strlist *strlist_alloc(size_t size)
strlist
*
strs
;
if
(
size
<
STR_DEFAULT_SIZE
)
size
=
STR_DEFAULT_SIZE
;
strs
=
(
strlist
*
)
calloc
(
1
,
sizeof
*
strs
)
;
strs
=
(
strlist
*
)
internal_
calloc
(
1
,
sizeof
*
strs
)
;
if
(
strs
)
{
strs
->
strs
=
(
char
**
)
calloc
(
size
,
sizeof
*
strs
->
strs
);
strs
->
strs
=
(
char
**
)
internal_
calloc
(
size
,
sizeof
*
strs
->
strs
);
if
(
strs
->
strs
)
{
strs
->
size
=
size
;
}
else
{
free
(
strs
);
internal_
free
(
strs
);
strs
=
NULL
;
}
}
...
...
@@ -83,7 +99,7 @@ static void strlist_realloc(strlist *strs)
if
(
!
strs
)
return
;
size_t
size
=
2
*
strs
->
size
;
if
(
size
)
{
char
**
ss
=
(
char
**
)
realloc
(
strs
->
strs
,
size
*
(
sizeof
*
strs
->
strs
));
char
**
ss
=
(
char
**
)
internal_
realloc
(
strs
->
strs
,
size
*
(
sizeof
*
strs
->
strs
));
if
(
ss
)
{
strs
->
size
=
size
;
strs
->
strs
=
ss
;
...
...
@@ -96,10 +112,10 @@ void strlist_free(strlist *strs)
{
if
(
!
strs
)
return
;
for
(
size_t
i
=
0
;
i
<
strs
->
num
;
i
++
)
{
free
(
strs
->
strs
[
i
]);
internal_
free
(
strs
->
strs
[
i
]);
}
free
(
strs
->
strs
);
free
(
strs
);
internal_
free
(
strs
->
strs
);
internal_
free
(
strs
);
}
void
strlist_set
(
strlist
*
strs
,
const
char
*
str
)
...
...
@@ -109,7 +125,15 @@ void strlist_set(strlist *strs,const char *str)
strlist_realloc
(
strs
);
}
if
(
strs
->
num
<
strs
->
size
)
{
strs
->
strs
[
strs
->
num
]
=
strdup
(
str
);
strs
->
strs
[
strs
->
num
]
=
ld_
strdup
(
str
);
if
(
strs
->
strs
[
strs
->
num
])
strs
->
num
++
;
}
}
char
*
ld_strdup
(
const
char
*
s
)
{
size_t
l
=
strlen
(
s
);
char
*
d
=
internal_malloc
(
l
+
1
);
if
(
!
d
)
return
NULL
;
return
memcpy
(
d
,
s
,
l
+
1
);
}
\ No newline at end of file
porting/linux/user/ldso/strops.h
浏览文件 @
66cc130b
/*
* Copyright (c) 2022 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 _STR_OPS_H
#define _STR_OPS_H
...
...
@@ -5,6 +20,7 @@
extern
"C"
{
#endif
#include <features.h>
#include <stdlib.h>
#include <string.h>
...
...
@@ -14,13 +30,15 @@ typedef struct _str_list_ {
char
**
strs
;
/** string list */
}
strlist
;
strlist
*
strlist_alloc
(
size_t
size
);
void
strlist_free
(
strlist
*
strs
);
hidden
strlist
*
strlist_alloc
(
size_t
size
);
hidden
void
strlist_free
(
strlist
*
strs
);
hidden
void
strlwc
(
char
*
str
);
hidden
size_t
strtrim
(
char
*
str
);
hidden
strlist
*
strsplit
(
const
char
*
str
,
const
char
*
split_s
);
hidden
void
strlist_set
(
strlist
*
strs
,
const
char
*
str
);
void
strlwc
(
char
*
str
);
size_t
strtrim
(
char
*
str
);
strlist
*
strsplit
(
const
char
*
str
,
const
char
*
split_s
);
void
strlist_set
(
strlist
*
strs
,
const
char
*
str
);
hidden
char
*
ld_strdup
(
const
char
*
);
#ifdef __cplusplus
}
...
...
porting/linux/user/src/hilog/vsnprintf_s_p.c
浏览文件 @
66cc130b
...
...
@@ -19,6 +19,8 @@
#include <string.h>
#include <stdio.h>
#include "malloc_impl.h"
/* Define the max length of the string */
#ifndef SECUREC_STRING_MAX_LEN
#define SECUREC_STRING_MAX_LEN 0x7fffffffUL
...
...
@@ -127,11 +129,11 @@ typedef enum {
#ifndef HILOG_PROHIBIT_ALLOCATION
#ifndef SECUREC_MALLOC
#define SECUREC_MALLOC(x) malloc((size_t)(x))
#define SECUREC_MALLOC(x)
internal_
malloc((size_t)(x))
#endif
#ifndef SECUREC_FREE
#define SECUREC_FREE(x) free((void *)(x))
#define SECUREC_FREE(x)
internal_
free((void *)(x))
#endif
#else
...
...
porting/linux/user/src/internal/hilog_adapter.h
浏览文件 @
66cc130b
...
...
@@ -16,6 +16,7 @@
#ifndef MUSL_HILOG_ADAPTER_H
#define MUSL_HILOG_ADAPTER_H
#include <features.h>
#include <stdarg.h>
#include <stdbool.h>
...
...
@@ -42,8 +43,8 @@ typedef enum {
LOG_LEVEL_MAX
,
}
LogLevel
;
int
HiLogAdapterPrint
(
LogType
type
,
LogLevel
level
,
unsigned
int
domain
,
const
char
*
tag
,
const
char
*
fmt
,
...)
hidden
int
HiLogAdapterPrint
(
LogType
type
,
LogLevel
level
,
unsigned
int
domain
,
const
char
*
tag
,
const
char
*
fmt
,
...)
__attribute__
((
__format__
(
os_log
,
5
,
6
)));
bool
HiLogAdapterIsLoggable
(
unsigned
int
domain
,
const
char
*
tag
,
LogLevel
level
);
hidden
bool
HiLogAdapterIsLoggable
(
unsigned
int
domain
,
const
char
*
tag
,
LogLevel
level
);
#endif // MUSL_HILOG_ADAPTER_H
porting/linux/user/src/internal/malloc_impl.h
浏览文件 @
66cc130b
...
...
@@ -47,4 +47,12 @@ hidden void __bin_chunk(struct chunk *);
hidden
extern
int
__malloc_replaced
;
hidden
void
*
internal_malloc
(
size_t
n
);
hidden
void
internal_free
(
void
*
p
);
hidden
void
*
internal_calloc
(
size_t
m
,
size_t
n
);
hidden
void
*
internal_realloc
(
void
*
p
,
size_t
n
);
#endif
porting/linux/user/src/malloc/malloc.c
浏览文件 @
66cc130b
...
...
@@ -328,6 +328,11 @@ void *__libc_malloc(size_t n)
#else
void
*
malloc
(
size_t
n
)
#endif
{
return
internal_malloc
(
n
);
}
void
*
internal_malloc
(
size_t
n
)
{
struct
chunk
*
c
;
int
i
,
j
;
...
...
@@ -403,13 +408,18 @@ static size_t mal0_clear(char *p, size_t pagesz, size_t n)
}
void
*
calloc
(
size_t
m
,
size_t
n
)
{
return
internal_calloc
(
m
,
n
);
}
void
*
internal_calloc
(
size_t
m
,
size_t
n
)
{
if
(
n
&&
m
>
(
size_t
)
-
1
/
n
)
{
errno
=
ENOMEM
;
return
0
;
}
n
*=
m
;
void
*
p
=
malloc
(
n
);
void
*
p
=
internal_
malloc
(
n
);
if
(
!
p
)
return
p
;
if
(
!
__malloc_replaced
)
{
if
(
IS_MMAPPED
(
MEM_TO_CHUNK
(
p
)))
...
...
@@ -421,14 +431,19 @@ void *calloc(size_t m, size_t n)
}
void
*
realloc
(
void
*
p
,
size_t
n
)
{
return
internal_realloc
(
p
,
n
);
}
void
*
internal_realloc
(
void
*
p
,
size_t
n
)
{
struct
chunk
*
self
,
*
next
;
size_t
n0
,
n1
;
void
*
new
;
if
(
!
p
)
return
malloc
(
n
);
if
(
!
p
)
return
internal_
malloc
(
n
);
if
(
!
n
)
{
free
(
p
);
internal_
free
(
p
);
return
NULL
;
}
...
...
@@ -444,7 +459,7 @@ void *realloc(void *p, size_t n)
size_t
newlen
=
n
+
extra
;
/* Crash on realloc of freed chunk */
if
(
extra
&
1
)
a_crash
();
if
(
newlen
<
PAGE_SIZE
&&
(
new
=
malloc
(
n
-
OVERHEAD
)))
{
if
(
newlen
<
PAGE_SIZE
&&
(
new
=
internal_
malloc
(
n
-
OVERHEAD
)))
{
n0
=
n
;
goto
copy_free_ret
;
}
...
...
@@ -487,11 +502,11 @@ void *realloc(void *p, size_t n)
copy_realloc:
/* As a last resort, allocate a new chunk and copy to it. */
new
=
malloc
(
n
-
OVERHEAD
);
new
=
internal_
malloc
(
n
-
OVERHEAD
);
if
(
!
new
)
return
0
;
copy_free_ret:
memcpy
(
new
,
p
,
n0
-
OVERHEAD
);
free
(
CHUNK_TO_MEM
(
self
));
internal_
free
(
CHUNK_TO_MEM
(
self
));
return
new
;
}
...
...
@@ -585,6 +600,11 @@ void __libc_free(void *p)
#else
void
free
(
void
*
p
)
#endif
{
return
internal_free
(
p
);
}
void
internal_free
(
void
*
p
)
{
if
(
!
p
)
return
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录