提交 66cc130b 编写于 作者: G ganlan

封装internal_malloc、internal_free、internal_realloc、internal_calloc接口

Signed-off-by: Nganlan <tony.gan@huawei.com>
上级 ed049184
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "namespace.h" #include "namespace.h"
#include "ns_config.h" #include "ns_config.h"
#include "pthread_impl.h" #include "pthread_impl.h"
#include "strops.h"
static void error(const char *, ...); 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 ...@@ -814,7 +815,7 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
case REL_TLSDESC: case REL_TLSDESC:
if (stride<3) addend = reloc_addr[1]; if (stride<3) addend = reloc_addr[1];
if (def.dso->tls_id > static_tls_cnt) { 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) { if (!new) {
error( error(
"Error relocating %s: cannot allocate TLSDESC for %s", "Error relocating %s: cannot allocate TLSDESC for %s",
...@@ -867,7 +868,7 @@ static void redo_lazy_relocs() ...@@ -867,7 +868,7 @@ static void redo_lazy_relocs()
p->lazy_next = lazy_head; p->lazy_next = lazy_head;
lazy_head = p; lazy_head = p;
} else { } else {
free(p->lazy); internal_free(p->lazy);
p->lazy = 0; p->lazy = 0;
p->lazy_next = 0; p->lazy_next = 0;
} }
...@@ -940,7 +941,7 @@ static void unmap_library(struct dso *dso) ...@@ -940,7 +941,7 @@ static void unmap_library(struct dso *dso)
munmap((void *)dso->loadmap->segs[i].addr, munmap((void *)dso->loadmap->segs[i].addr,
dso->loadmap->segs[i].p_memsz); dso->loadmap->segs[i].p_memsz);
} }
free(dso->loadmap); internal_free(dso->loadmap);
} else if (dso->map && dso->map_len) { } else if (dso->map && dso->map_len) {
munmap(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 ...@@ -972,7 +973,7 @@ static void *map_library(int fd, struct dso *dso, struct reserved_address_params
goto noexec; goto noexec;
phsize = eh->e_phentsize * eh->e_phnum; phsize = eh->e_phentsize * eh->e_phnum;
if (phsize > sizeof buf - sizeof *eh) { if (phsize > sizeof buf - sizeof *eh) {
allocated_buf = malloc(phsize); allocated_buf = internal_malloc(phsize);
if (!allocated_buf) return 0; if (!allocated_buf) return 0;
l = pread(fd, allocated_buf, phsize, eh->e_phoff); l = pread(fd, allocated_buf, phsize, eh->e_phoff);
if (l < 0) goto error; if (l < 0) goto error;
...@@ -1019,7 +1020,7 @@ static void *map_library(int fd, struct dso *dso, struct reserved_address_params ...@@ -1019,7 +1020,7 @@ static void *map_library(int fd, struct dso *dso, struct reserved_address_params
} }
if (!dyn) goto noexec; if (!dyn) goto noexec;
if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { 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); + nsegs * sizeof *dso->loadmap->segs);
if (!dso->loadmap) goto error; if (!dso->loadmap) goto error;
dso->loadmap->nsegs = nsegs; dso->loadmap->nsegs = nsegs;
...@@ -1139,13 +1140,13 @@ done_mapping: ...@@ -1139,13 +1140,13 @@ done_mapping:
dso->base = base; dso->base = base;
dso->dynv = laddr(dso, dyn); dso->dynv = laddr(dso, dyn);
if (dso->tls.size) dso->tls.image = laddr(dso, tls_image); if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
free(allocated_buf); internal_free(allocated_buf);
return map; return map;
noexec: noexec:
errno = ENOEXEC; errno = ENOEXEC;
error: error:
if (map!=MAP_FAILED) unmap_library(dso); if (map!=MAP_FAILED) unmap_library(dso);
free(allocated_buf); internal_free(allocated_buf);
return 0; return 0;
} }
...@@ -1234,7 +1235,7 @@ static int fixup_rpath(struct dso *p, char *buf, size_t buf_size) ...@@ -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. */ /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
if (libc.secure && *origin != '/') if (libc.secure && *origin != '/')
return 0; 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; if (!p->rpath) return -1;
d = p->rpath; d = p->rpath;
...@@ -1317,7 +1318,7 @@ static void makefuncdescs(struct dso *p) ...@@ -1317,7 +1318,7 @@ static void makefuncdescs(struct dso *p)
p->funcdescs = dl_mmap(size); p->funcdescs = dl_mmap(size);
self_done = 1; self_done = 1;
} else { } else {
p->funcdescs = malloc(size); p->funcdescs = internal_malloc(size);
} }
if (!p->funcdescs) { if (!p->funcdescs) {
if (!runtime) a_crash(); if (!runtime) a_crash();
...@@ -1350,7 +1351,7 @@ static void get_sys_path(ns_configor *conf) ...@@ -1350,7 +1351,7 @@ static void get_sys_path(ns_configor *conf)
sys_path = sys_path_default; sys_path = sys_path_default;
} else if (sys_path_default) { } else if (sys_path_default) {
size_t newlen = strlen(sys_path) + strlen(sys_path_default) + 2; 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); memset(new_syspath, 0, newlen);
strcpy(new_syspath, sys_path); strcpy(new_syspath, sys_path);
strcat(new_syspath, ":"); strcat(new_syspath, ":");
...@@ -1590,7 +1591,7 @@ struct dso *load_library( ...@@ -1590,7 +1591,7 @@ struct dso *load_library(
if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX; if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
else alloc_size += n_th * per_th; else alloc_size += n_th * per_th;
} }
p = calloc(1, alloc_size); p = internal_calloc(1, alloc_size);
if (!p) { if (!p) {
unmap_library(&temp_dso); unmap_library(&temp_dso);
return 0; return 0;
...@@ -1657,7 +1658,7 @@ static void load_direct_deps(struct dso *p, ns_t *namespace, struct reserved_add ...@@ -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 /* Use builtin buffer for apps with no external deps, to
* preserve property of no runtime failure paths. */ * preserve property of no runtime failure paths. */
p->deps = (p==head && cnt<2) ? builtin_deps : p->deps = (p==head && cnt<2) ? builtin_deps :
calloc(cnt+1, sizeof *p->deps); internal_calloc(cnt+1, sizeof *p->deps);
if (!p->deps) { if (!p->deps) {
error("Error loading dependencies for %s", p->name); error("Error loading dependencies for %s", p->name);
if (runtime) longjmp(*rtld_fail, 1); if (runtime) longjmp(*rtld_fail, 1);
...@@ -1715,8 +1716,8 @@ static void extend_bfs_deps(struct dso *p) ...@@ -1715,8 +1716,8 @@ static void extend_bfs_deps(struct dso *p)
for (j=cnt=0; j<dep->ndeps_direct; j++) for (j=cnt=0; j<dep->ndeps_direct; j++)
if (!dep->deps[j]->mark) cnt++; if (!dep->deps[j]->mark) cnt++;
tmp = no_realloc ? tmp = no_realloc ?
malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) : internal_malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1)); internal_realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
if (!tmp) { if (!tmp) {
error("Error recording dependencies for %s", p->name); error("Error recording dependencies for %s", p->name);
if (runtime) longjmp(*rtld_fail, 1); if (runtime) longjmp(*rtld_fail, 1);
...@@ -1906,7 +1907,7 @@ static struct dso **queue_ctors(struct dso *dso) ...@@ -1906,7 +1907,7 @@ static struct dso **queue_ctors(struct dso *dso)
if (dso==head && cnt <= countof(builtin_ctor_queue)) if (dso==head && cnt <= countof(builtin_ctor_queue))
queue = builtin_ctor_queue; queue = builtin_ctor_queue;
else else
queue = calloc(cnt, sizeof *queue); queue = internal_calloc(cnt, sizeof *queue);
if (!queue) { if (!queue) {
error("Error allocating constructor queue: %m\n"); error("Error allocating constructor queue: %m\n");
...@@ -1989,7 +1990,7 @@ void __libc_start_init(void) ...@@ -1989,7 +1990,7 @@ void __libc_start_init(void)
{ {
do_init_fini(main_ctor_queue); do_init_fini(main_ctor_queue);
if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue) if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue)
free(main_ctor_queue); internal_free(main_ctor_queue);
main_ctor_queue = 0; main_ctor_queue = 0;
} }
...@@ -2405,7 +2406,7 @@ void __dls3(size_t *sp, size_t *auxv) ...@@ -2405,7 +2406,7 @@ void __dls3(size_t *sp, size_t *auxv)
update_tls_size(); update_tls_size();
void *initial_tls = builtin_tls; void *initial_tls = builtin_tls;
if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) { 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) { if (!initial_tls) {
dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
argv[0], libc.tls_size); argv[0], libc.tls_size);
...@@ -2481,7 +2482,7 @@ static void prepare_lazy(struct dso *p) ...@@ -2481,7 +2482,7 @@ static void prepare_lazy(struct dso *p)
size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO); size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
n += i-j; n += i-j;
} }
p->lazy = calloc(n, 3*sizeof(size_t)); p->lazy = internal_calloc(n, 3*sizeof(size_t));
if (!p->lazy) { if (!p->lazy) {
error("Error preparing lazy relocation for %s: %m", p->name); error("Error preparing lazy relocation for %s: %m", p->name);
longjmp(*rtld_fail, 1); longjmp(*rtld_fail, 1);
...@@ -2573,18 +2574,18 @@ static void *dlopen_impl( ...@@ -2573,18 +2574,18 @@ static void *dlopen_impl(
next = p->next; next = p->next;
while (p->td_index) { while (p->td_index) {
void *tmp = p->td_index->next; void *tmp = p->td_index->next;
free(p->td_index); internal_free(p->td_index);
p->td_index = tmp; p->td_index = tmp;
} }
free(p->funcdescs); internal_free(p->funcdescs);
if (p->rpath != p->rpath_orig) if (p->rpath != p->rpath_orig)
free(p->rpath); internal_free(p->rpath);
free(p->deps); internal_free(p->deps);
dlclose_ns(p); dlclose_ns(p);
unmap_library(p); unmap_library(p);
free(p); internal_free(p);
} }
free(ctor_queue); internal_free(ctor_queue);
ctor_queue = 0; ctor_queue = 0;
if (!orig_tls_tail) libc.tls_head = 0; if (!orig_tls_tail) libc.tls_head = 0;
tls_tail = orig_tls_tail; tls_tail = orig_tls_tail;
...@@ -2720,7 +2721,7 @@ end: ...@@ -2720,7 +2721,7 @@ end:
pthread_rwlock_unlock(&lock); pthread_rwlock_unlock(&lock);
if (ctor_queue) { if (ctor_queue) {
do_init_fini(ctor_queue); do_init_fini(ctor_queue);
free(ctor_queue); internal_free(ctor_queue);
} }
pthread_setcancelstate(cs, 0); pthread_setcancelstate(cs, 0);
#ifdef HANDLE_RANDOMIZATION #ifdef HANDLE_RANDOMIZATION
...@@ -3032,11 +3033,11 @@ static int do_dlclose(struct dso *p) ...@@ -3032,11 +3033,11 @@ static int do_dlclose(struct dso *p)
dlclose_ns(p); dlclose_ns(p);
if (p->lazy != NULL) if (p->lazy != NULL)
free(p->lazy); internal_free(p->lazy);
if (p->deps != no_deps) if (p->deps != no_deps)
free(p->deps); internal_free(p->deps);
unmap_library(p); unmap_library(p);
free(p); internal_free(p);
return 0; return 0;
} }
...@@ -3344,14 +3345,14 @@ int handle_asan_path_open(int fd, const char *name, ns_t *namespace, char *buf, ...@@ -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 (fd == -1 && (namespace->asan_lib_paths || namespace->lib_paths)) {
if (namespace->lib_paths && namespace->asan_lib_paths) { if (namespace->lib_paths && namespace->asan_lib_paths) {
size_t newlen = strlen(namespace->asan_lib_paths) + strlen(namespace->lib_paths) + 2; 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); memset(new_lib_paths, 0, newlen);
strcpy(new_lib_paths, namespace->asan_lib_paths); strcpy(new_lib_paths, namespace->asan_lib_paths);
strcat(new_lib_paths, ":"); strcat(new_lib_paths, ":");
strcat(new_lib_paths, namespace->lib_paths); strcat(new_lib_paths, namespace->lib_paths);
fd_tmp = path_open(name, new_lib_paths, buf, buf_size); 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); 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) { } else if (namespace->asan_lib_paths) {
fd_tmp = path_open(name, namespace->asan_lib_paths, buf, buf_size); 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.", 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) ...@@ -3402,7 +3403,7 @@ static bool map_library_header(struct loadtask *task)
} }
task->phsize = task->eh->e_phentsize * task->eh->e_phnum; task->phsize = task->eh->e_phentsize * task->eh->e_phnum;
if (task->phsize > sizeof task->ehdr_buf - sizeof(Ehdr)) { 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) { if (!task->allocated_buf) {
LD_LOGE("Error mapping header %{public}s: failed to alloc memory", task->name); LD_LOGE("Error mapping header %{public}s: failed to alloc memory", task->name);
return false; return false;
...@@ -3487,7 +3488,7 @@ static bool map_library_header(struct loadtask *task) ...@@ -3487,7 +3488,7 @@ static bool map_library_header(struct loadtask *task)
noexec: noexec:
errno = ENOEXEC; errno = ENOEXEC;
error: error:
free(task->allocated_buf); internal_free(task->allocated_buf);
task->allocated_buf = NULL; task->allocated_buf = NULL;
return false; return false;
} }
...@@ -3536,7 +3537,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para ...@@ -3536,7 +3537,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para
goto noexec; goto noexec;
} }
if (DL_FDPIC && !(task->eh->e_flags & FDPIC_CONSTDISP_FLAG)) { 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) { if (!task->p->loadmap) {
goto error; goto error;
} }
...@@ -3681,7 +3682,7 @@ done_mapping: ...@@ -3681,7 +3682,7 @@ done_mapping:
if (task->p->tls.size) { if (task->p->tls.size) {
task->p->tls.image = laddr(task->p, task->tls_image); task->p->tls.image = laddr(task->p, task->tls_image);
} }
free(task->allocated_buf); internal_free(task->allocated_buf);
task->allocated_buf = NULL; task->allocated_buf = NULL;
return true; return true;
noexec: noexec:
...@@ -3690,7 +3691,7 @@ error: ...@@ -3690,7 +3691,7 @@ error:
if (map != MAP_FAILED) { if (map != MAP_FAILED) {
unmap_library(task->p); unmap_library(task->p);
} }
free(task->allocated_buf); internal_free(task->allocated_buf);
task->allocated_buf = NULL; task->allocated_buf = NULL;
return false; return false;
} }
...@@ -3866,7 +3867,7 @@ static bool load_library_header(struct loadtask *task) ...@@ -3866,7 +3867,7 @@ static bool load_library_header(struct loadtask *task)
alloc_size += n_th * per_th; alloc_size += n_th * per_th;
} }
} }
task->p = calloc(1, alloc_size); task->p = internal_calloc(1, alloc_size);
if (!task->p) { if (!task->p) {
LD_LOGE("Error loading header %{public}s: failed to allocate dso", task->name); LD_LOGE("Error loading header %{public}s: failed to allocate dso", task->name);
close(task->fd); close(task->fd);
...@@ -3927,8 +3928,8 @@ static void task_load_library(struct loadtask *task, struct reserved_address_par ...@@ -3927,8 +3928,8 @@ static void task_load_library(struct loadtask *task, struct reserved_address_par
find_sym(task->p, "stdin", 1).sym) { find_sym(task->p, "stdin", 1).sym) {
do_dlclose(task->p); do_dlclose(task->p);
task->p = NULL; task->p = NULL;
free((void*)task->name); internal_free((void*)task->name);
task->name = strdup("libc.so"); task->name = ld_strdup("libc.so");
task->check_inherited = true; task->check_inherited = true;
if (!load_library_header(task)) { if (!load_library_header(task)) {
LD_LOGE("Error loading library %{public}s: failed to load libc.so", task->name); 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 ...@@ -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 /* Use builtin buffer for apps with no external deps, to
* preserve property of no runtime failure paths. */ * preserve property of no runtime failure paths. */
p->deps = (p == head && cnt < MIN_DEPS_COUNT) ? builtin_deps : 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) { if (!p->deps) {
LD_LOGE("Error loading dependencies for %{public}s", p->name); LD_LOGE("Error loading dependencies for %{public}s", p->name);
error("Error loading dependencies for %s", p->name); error("Error loading dependencies for %s", p->name);
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
#include <sys/random.h> #include <sys/random.h>
#include <unistd.h> #include <unistd.h>
#include "malloc_impl.h"
#define HANDLE_INCREASE 2 #define HANDLE_INCREASE 2
#define TASK_BASE_CAPACITY 8 #define TASK_BASE_CAPACITY 8
...@@ -42,7 +44,7 @@ static bool is_first_stage_init(void) ...@@ -42,7 +44,7 @@ static bool is_first_stage_init(void)
void *add_handle_node(void *handle, struct dso *dso) 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) { if (!node) {
return NULL; return NULL;
} }
...@@ -88,7 +90,7 @@ void remove_handle_node(void *handle) ...@@ -88,7 +90,7 @@ void remove_handle_node(void *handle)
} else { } else {
handle_map_list = node->next; handle_map_list = node->next;
} }
free(node); internal_free(node);
return; return;
} else { } else {
pre_node = node; pre_node = node;
...@@ -124,7 +126,7 @@ void *assign_valid_handle(struct dso *p) ...@@ -124,7 +126,7 @@ void *assign_valid_handle(struct dso *p)
struct loadtasks *create_loadtasks(void) struct loadtasks *create_loadtasks(void)
{ {
struct loadtasks *tasks = malloc(sizeof(struct loadtasks)); struct loadtasks *tasks = internal_malloc(sizeof(struct loadtasks));
if (tasks) { if (tasks) {
tasks->array = NULL; tasks->array = NULL;
tasks->capacity = 0; tasks->capacity = 0;
...@@ -141,9 +143,9 @@ bool append_loadtasks(struct loadtasks *tasks, struct loadtask *item) ...@@ -141,9 +143,9 @@ bool append_loadtasks(struct loadtasks *tasks, struct loadtask *item)
new_cap = tasks->capacity + TASK_BASE_CAPACITY; new_cap = tasks->capacity + TASK_BASE_CAPACITY;
void *realloced = NULL; void *realloced = NULL;
if (tasks->array) { if (tasks->array) {
realloced = realloc(tasks->array, new_cap * sizeof(struct loadtask *)); realloced = internal_realloc(tasks->array, new_cap * sizeof(struct loadtask *));
} else { } else {
realloced = malloc(TASK_BASE_CAPACITY * sizeof(struct loadtask *)); realloced = internal_malloc(TASK_BASE_CAPACITY * sizeof(struct loadtask *));
} }
if (realloced) { if (realloced) {
tasks->array = realloced; tasks->array = realloced;
...@@ -163,11 +165,11 @@ void free_task(struct loadtask *task) ...@@ -163,11 +165,11 @@ void free_task(struct loadtask *task)
return; return;
} }
if (task->name) { if (task->name) {
free(task->name); internal_free(task->name);
task->name = NULL; task->name = NULL;
} }
if (task->allocated_buf) { if (task->allocated_buf) {
free(task->allocated_buf); internal_free(task->allocated_buf);
task->allocated_buf = NULL; task->allocated_buf = NULL;
} }
if (task->dyn_map_len) { if (task->dyn_map_len) {
...@@ -184,7 +186,7 @@ void free_task(struct loadtask *task) ...@@ -184,7 +186,7 @@ void free_task(struct loadtask *task)
close(task->fd); close(task->fd);
task->fd = -1; task->fd = -1;
} }
free(task); internal_free(task);
} }
struct loadtask *get_loadtask(struct loadtasks *tasks, size_t index) struct loadtask *get_loadtask(struct loadtasks *tasks, size_t index)
...@@ -206,11 +208,11 @@ void free_loadtasks(struct loadtasks *tasks) ...@@ -206,11 +208,11 @@ void free_loadtasks(struct loadtasks *tasks)
tasks->length = 0; tasks->length = 0;
} }
if (tasks->array) { if (tasks->array) {
free(tasks->array); internal_free(tasks->array);
tasks->array = NULL; tasks->array = NULL;
} }
tasks->capacity = 0; tasks->capacity = 0;
free(tasks); internal_free(tasks);
} }
} }
...@@ -233,11 +235,11 @@ void shuffle_loadtasks(struct loadtasks *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) struct loadtask *create_loadtask(const char *name, struct dso *needed_by, ns_t *ns, bool check_inherited)
{ {
size_t name_len = strlen(name); 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) { if (!name_buf) {
return NULL; return NULL;
} }
struct loadtask *task = calloc(1, sizeof(struct loadtask)); struct loadtask *task = internal_calloc(1, sizeof(struct loadtask));
if (!task) { if (!task) {
return NULL; return NULL;
} }
......
/*
* 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 "namespace.h"
#include "ld_log.h" #include "ld_log.h"
#include "malloc_impl.h"
#include "strops.h" #include "strops.h"
static ns_t g_ns_default; static ns_t g_ns_default;
...@@ -19,14 +35,14 @@ static nslist g_ns_list; ...@@ -19,14 +35,14 @@ static nslist g_ns_list;
static ns_inherit_list *nsinherits_alloc() static ns_inherit_list *nsinherits_alloc()
{ {
ns_inherit_list *nsinl; ns_inherit_list *nsinl;
nsinl = (ns_inherit_list *)calloc(1, sizeof *nsinl); nsinl = (ns_inherit_list *)internal_calloc(1, sizeof *nsinl);
if (nsinl) { if (nsinl) {
nsinl->size = INHERIT_DEFAULT_SIZE; 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) { if (!nsinl->inherits) {
LD_LOGE("nsinherits_alloc failed,return NULL!"); LD_LOGE("nsinherits_alloc failed,return NULL!");
free(nsinl); internal_free(nsinl);
nsinl = NULL; nsinl = NULL;
} }
} }
...@@ -40,10 +56,10 @@ static void nsinherits_free(ns_inherit_list *nsinl) ...@@ -40,10 +56,10 @@ static void nsinherits_free(ns_inherit_list *nsinl)
} }
for (size_t i = 0; i < nsinl->num; i++) { for (size_t i = 0; i < nsinl->num; i++) {
strlist_free(nsinl->inherits[i]->shared_libs); strlist_free(nsinl->inherits[i]->shared_libs);
free(nsinl->inherits[i]); internal_free(nsinl->inherits[i]);
} }
free(nsinl->inherits); internal_free(nsinl->inherits);
free(nsinl); internal_free(nsinl);
} }
static void nsinherits_realloc(ns_inherit_list *nsinl) static void nsinherits_realloc(ns_inherit_list *nsinl)
...@@ -54,7 +70,7 @@ 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; size_t size = 2 * nsinl->size;
if (size) { if (size) {
ns_inherit **inherits; 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) { if (!inherits) {
LD_LOGE("nsinherits_realloc failed!"); LD_LOGE("nsinherits_realloc failed!");
return; return;
...@@ -68,14 +84,14 @@ static void nsinherits_realloc(ns_inherit_list *nsinl) ...@@ -68,14 +84,14 @@ static void nsinherits_realloc(ns_inherit_list *nsinl)
static dsolist *dsolist_alloc() static dsolist *dsolist_alloc()
{ {
dsolist *dsol; dsolist *dsol;
dsol = (dsolist *)calloc(1, sizeof *dsol); dsol = (dsolist *)internal_calloc(1, sizeof *dsol);
if (dsol) { if (dsol) {
dsol->size = DSOLIST_DEFAULT_SIZE; 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) { if (!dsol->dsos) {
LD_LOGE("dsolist_alloc failed,return NULL!"); LD_LOGE("dsolist_alloc failed,return NULL!");
free(dsol); internal_free(dsol);
dsol = NULL; dsol = NULL;
} }
} }
...@@ -90,7 +106,7 @@ static void dsolist_realloc(dsolist *dsol) ...@@ -90,7 +106,7 @@ static void dsolist_realloc(dsolist *dsol)
size_t size = 2 * dsol->size; size_t size = 2 * dsol->size;
if (size) { if (size) {
struct dso **ds; 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) { if (!ds) {
LD_LOGE("dsolist_realloc failed!"); LD_LOGE("dsolist_realloc failed!");
return; return;
...@@ -103,11 +119,11 @@ static void dsolist_realloc(dsolist *dsol) ...@@ -103,11 +119,11 @@ static void dsolist_realloc(dsolist *dsol)
ns_t *ns_alloc() 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(); nst->ns_dsos = dsolist_alloc();
if (!nst->ns_dsos) { if (!nst->ns_dsos) {
LD_LOGE("ns_alloc failed,return NULL!"); LD_LOGE("ns_alloc failed,return NULL!");
free(nst); internal_free(nst);
nst = NULL; nst = NULL;
} }
return nst; return nst;
...@@ -118,15 +134,15 @@ void ns_free(ns_t *ns) ...@@ -118,15 +134,15 @@ void ns_free(ns_t *ns)
if (!ns) { if (!ns) {
return; return;
} }
free(ns->ns_name); internal_free(ns->ns_name);
free(ns->env_paths); internal_free(ns->env_paths);
free(ns->lib_paths); internal_free(ns->lib_paths);
free(ns->asan_lib_paths); internal_free(ns->asan_lib_paths);
strlist_free(ns->permitted_paths); strlist_free(ns->permitted_paths);
strlist_free(ns->asan_permitted_paths); strlist_free(ns->asan_permitted_paths);
strlist_free(ns->allowed_libs); strlist_free(ns->allowed_libs);
nsinherits_free(ns->ns_inherits); nsinherits_free(ns->ns_inherits);
free(ns); internal_free(ns);
} }
void ns_add_dso(ns_t *ns, struct dso *dso) void ns_add_dso(ns_t *ns, struct dso *dso)
...@@ -156,7 +172,7 @@ nslist *nslist_init() ...@@ -156,7 +172,7 @@ nslist *nslist_init()
{ {
g_ns_list.size = NSLIST_DEFAULT_SIZE; g_ns_list.size = NSLIST_DEFAULT_SIZE;
g_ns_list.num = 0; 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) { if (!g_ns_list.nss) {
LD_LOGE("nslist_init failed!"); LD_LOGE("nslist_init failed!");
return NULL; return NULL;
...@@ -169,7 +185,7 @@ static void nslist_realloc() ...@@ -169,7 +185,7 @@ static void nslist_realloc()
size_t size = 2 * g_ns_list.size; size_t size = 2 * g_ns_list.size;
if (size) { if (size) {
ns_t **nss; 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) { if (!nss) {
LD_LOGE("nslist_realloc failed!"); LD_LOGE("nslist_realloc failed!");
return; return;
...@@ -209,8 +225,8 @@ void ns_set_name(ns_t *ns, const char *name) ...@@ -209,8 +225,8 @@ void ns_set_name(ns_t *ns, const char *name)
if (!ns || !name) { if (!ns || !name) {
return; return;
} }
if (ns->ns_name) free(ns->ns_name); if (ns->ns_name) internal_free(ns->ns_name);
ns->ns_name = strdup(name); ns->ns_name = ld_strdup(name);
strtrim(ns->ns_name); strtrim(ns->ns_name);
LD_LOGD("ns_set_name ns_name:%{public}s.", 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) ...@@ -220,9 +236,9 @@ void ns_set_env_paths(ns_t *ns, const char *env_paths)
if (!ns) { if (!ns) {
return; return;
} }
if (ns->env_paths) free(ns->env_paths); if (ns->env_paths) internal_free(ns->env_paths);
if (env_paths) { if (env_paths) {
ns->env_paths = strdup(env_paths); ns->env_paths = ld_strdup(env_paths);
strtrim(ns->env_paths); strtrim(ns->env_paths);
} else { } else {
ns->env_paths = NULL; ns->env_paths = NULL;
...@@ -235,9 +251,9 @@ void ns_set_lib_paths(ns_t *ns, const char *lib_paths) ...@@ -235,9 +251,9 @@ void ns_set_lib_paths(ns_t *ns, const char *lib_paths)
if (!ns) { if (!ns) {
return; return;
} }
if (ns->lib_paths) free(ns->lib_paths); if (ns->lib_paths) internal_free(ns->lib_paths);
if (lib_paths) { if (lib_paths) {
ns->lib_paths = strdup(lib_paths); ns->lib_paths = ld_strdup(lib_paths);
strtrim(ns->lib_paths); strtrim(ns->lib_paths);
} else { } else {
ns->lib_paths = NULL; ns->lib_paths = NULL;
...@@ -251,10 +267,10 @@ void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths) ...@@ -251,10 +267,10 @@ void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths)
return; return;
} }
if (ns->asan_lib_paths) { if (ns->asan_lib_paths) {
free(ns->asan_lib_paths); internal_free(ns->asan_lib_paths);
} }
if (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); strtrim(ns->asan_lib_paths);
} else { } else {
ns->asan_lib_paths = NULL; ns->asan_lib_paths = NULL;
...@@ -305,9 +321,9 @@ void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs) ...@@ -305,9 +321,9 @@ void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs)
ns->allowed_libs = NULL; ns->allowed_libs = NULL;
if (allowed_libs) { if (allowed_libs) {
/* if setted and not empty, split to list. */ /* 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, ":"); 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); 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) ...@@ -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); ns_inherit *inherit = find_ns_inherit(ns, ns_inherited);
if (!inherit) { if (!inherit) {
inherit = calloc(1, sizeof *inherit); inherit = internal_calloc(1, sizeof *inherit);
if (!inherit) { if (!inherit) {
LD_LOGE("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] calloc failed!", LD_LOGE("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] calloc failed!",
ns->ns_name, ns->ns_name,
...@@ -375,9 +391,9 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs) ...@@ -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 setted and not empty, split to list. */
if (shared_libs) { 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, ":"); if (strtrim(s_libs) > 0) inherit->shared_libs = strsplit(shared_libs, ":");
free(s_libs); internal_free(s_libs);
} }
if (!need_add) { if (!need_add) {
...@@ -395,7 +411,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs) ...@@ -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!", LD_LOGD("ns_add_inherit ns[%{public}s] ns_inherited[%{public}s] nsinherits_alloc failed!",
ns->ns_name, ns->ns_name,
ns_inherited->ns_name); ns_inherited->ns_name);
free(inherit); internal_free(inherit);
return; return;
} }
...@@ -420,7 +436,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs) ...@@ -420,7 +436,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs)
ns->ns_name, ns->ns_name,
ns_inherited->ns_name); ns_inherited->ns_name);
if (inherit->shared_libs) strlist_free(inherit->shared_libs); if (inherit->shared_libs) strlist_free(inherit->shared_libs);
free(inherit); internal_free(inherit);
} }
return; return;
} }
......
/*
* 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 #ifndef _NAMESPACE_H
#define _NAMESPACE_H #define _NAMESPACE_H
......
/*-------------------------------------------------------------------------*/ /*
/** * Copyright (c) 2022 Huawei Device Co., Ltd.
@file ns_config.c * Licensed under the Apache License, Version 2.0 (the "License");
@author yangwenjun * you may not use this file except in compliance with the License.
@brief deal with ld-musl-namespace.ini file * 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 "ns_config.h"
#include <ctype.h> #include <ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include "ld_log.h" #include "ld_log.h"
#include "malloc_impl.h"
/*---------------------------- Defines -------------------------------------*/ /*---------------------------- Defines -------------------------------------*/
#define MAX_LINE_SIZE (1024) #define MAX_LINE_SIZE (1024)
#define INI_INVALID_KEY ((char*)-1) #define INI_INVALID_KEY ((char*)-1)
...@@ -102,16 +112,16 @@ static kvlist * kvlist_alloc(size_t size) ...@@ -102,16 +112,16 @@ static kvlist * kvlist_alloc(size_t size)
if (size < KV_DEFAULT_SIZE) size = KV_DEFAULT_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) { if (kvs) {
kvs->key = (char **)calloc(size, sizeof *kvs->key); kvs->key = (char **)internal_calloc(size, sizeof *kvs->key);
kvs->val = (char **)calloc(size, sizeof *kvs->val); kvs->val = (char **)internal_calloc(size, sizeof *kvs->val);
if (kvs->key && kvs->val) { if (kvs->key && kvs->val) {
kvs->size = size; kvs->size = size;
} else { } else {
free(kvs->key); internal_free(kvs->key);
free(kvs->val); internal_free(kvs->val);
free(kvs); internal_free(kvs);
kvs = NULL; kvs = NULL;
} }
} }
...@@ -124,10 +134,10 @@ static void kvlist_realloc(kvlist *kvs) ...@@ -124,10 +134,10 @@ static void kvlist_realloc(kvlist *kvs)
size_t size = 2*kvs->size; size_t size = 2*kvs->size;
if (size) { if (size) {
char **keys, **vals; 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; if (!keys) return;
kvs->key = keys; 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; if (!vals) return;
kvs->val = vals; kvs->val = vals;
kvs->size = size; kvs->size = size;
...@@ -141,12 +151,12 @@ static void kvlist_free(kvlist *kvs) ...@@ -141,12 +151,12 @@ static void kvlist_free(kvlist *kvs)
size_t i; size_t i;
if (!kvs) return; if (!kvs) return;
for (i=0; i<kvs->num; i++) { for (i=0; i<kvs->num; i++) {
free(kvs->key[i]); internal_free(kvs->key[i]);
free(kvs->val[i]); internal_free(kvs->val[i]);
} }
free(kvs->key); internal_free(kvs->key);
free(kvs->val); internal_free(kvs->val);
free(kvs); internal_free(kvs);
} }
static section_list *sections_alloc(size_t size) static section_list *sections_alloc(size_t size)
...@@ -154,17 +164,17 @@ static section_list *sections_alloc(size_t size) ...@@ -154,17 +164,17 @@ static section_list *sections_alloc(size_t size)
section_list *sections; section_list *sections;
if (size < SECTION_DEFAULT_SIZE) size = SECTION_DEFAULT_SIZE; 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) { if (sections) {
sections->names = (char**)calloc(size, sizeof *sections->names); sections->names = (char**)internal_calloc(size, sizeof *sections->names);
sections->kvs = (kvlist**)calloc(size, sizeof *sections->kvs); sections->kvs = (kvlist**)internal_calloc(size, sizeof *sections->kvs);
if (sections->names && sections->kvs) { if (sections->names && sections->kvs) {
sections->size = size; sections->size = size;
} else { } else {
free(sections->names); internal_free(sections->names);
free(sections->kvs); internal_free(sections->kvs);
free(sections); internal_free(sections);
sections = NULL; sections = NULL;
} }
} }
...@@ -178,10 +188,10 @@ static void sections_realloc(section_list *sections) ...@@ -178,10 +188,10 @@ static void sections_realloc(section_list *sections)
if (size) { if (size) {
char **names; char **names;
kvlist **kvs; kvlist **kvs;
names = (char **)realloc(sections->names, size * (sizeof *sections->names)); names = (char **)internal_realloc(sections->names, size * (sizeof *sections->names));
if (!names) return; if (!names) return;
sections->names = names; 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; if (!kvs) return;
sections->kvs = kvs; sections->kvs = kvs;
sections->size = size; sections->size = size;
...@@ -193,12 +203,12 @@ static void sections_free(section_list *sections) ...@@ -193,12 +203,12 @@ static void sections_free(section_list *sections)
{ {
if (!sections) return; if (!sections) return;
for (size_t i=0; i < sections->num; i++) { for (size_t i=0; i < sections->num; i++) {
free(sections->names[i]); internal_free(sections->names[i]);
kvlist_free(sections->kvs[i]); kvlist_free(sections->kvs[i]);
} }
free(sections->names); internal_free(sections->names);
free(sections->kvs); internal_free(sections->kvs);
free(sections); internal_free(sections);
} }
static void kvlist_set(kvlist *kvs, const char *key, const char *val) 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) ...@@ -213,9 +223,9 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val)
} }
if (i < kvs->num) { if (i < kvs->num) {
char * v = strdup(val); char * v = ld_strdup(val);
if (v) { if (v) {
free(kvs->val[i]); internal_free(kvs->val[i]);
kvs->val[i] = v; kvs->val[i] = v;
} }
return; return;
...@@ -224,13 +234,13 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val) ...@@ -224,13 +234,13 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val)
kvlist_realloc(kvs); kvlist_realloc(kvs);
} }
if (kvs->num < kvs->size) { if (kvs->num < kvs->size) {
kvs->key[kvs->num] = strdup(key); kvs->key[kvs->num] = ld_strdup(key);
kvs->val[kvs->num] = strdup(val); kvs->val[kvs->num] = ld_strdup(val);
if (kvs->key[kvs->num] && kvs->val[kvs->num]) { if (kvs->key[kvs->num] && kvs->val[kvs->num]) {
kvs->num++; kvs->num++;
} else { } else {
free(kvs->key[kvs->num]); internal_free(kvs->key[kvs->num]);
free(kvs->val[kvs->num]); internal_free(kvs->val[kvs->num]);
} }
} }
return; return;
...@@ -258,13 +268,13 @@ static void sections_set(section_list *sections, const char *name, const char *k ...@@ -258,13 +268,13 @@ static void sections_set(section_list *sections, const char *name, const char *k
if (sections->num < sections->size) { if (sections->num < sections->size) {
kvs = kvlist_alloc(0); kvs = kvlist_alloc(0);
sections->names[sections->num] = strdup(name); sections->names[sections->num] = ld_strdup(name);
sections->kvs[sections->num] = kvs; sections->kvs[sections->num] = kvs;
if (sections->names[sections->num] && kvs) { if (sections->names[sections->num] && kvs) {
sections->num++; sections->num++;
kvlist_set(kvs,key,val); kvlist_set(kvs,key,val);
} else { } else {
free(sections->names[sections->num]); internal_free(sections->names[sections->num]);
kvlist_free(kvs); kvlist_free(kvs);
} }
} }
...@@ -465,10 +475,10 @@ static int config_parse(const char *file_path, const char *exe_path) ...@@ -465,10 +475,10 @@ static int config_parse(const char *file_path, const char *exe_path)
kvlist* dirkvs; kvlist* dirkvs;
kvlist* acquiescence_kvs; kvlist* acquiescence_kvs;
if (!exe_path) return -1; 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; const char * fpath = CONFIG_DEFAULT_FILE;
if (file_path) fpath = file_path; 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); g_configor.sections = config_load(fpath);
if (!g_configor.sections) { if (!g_configor.sections) {
...@@ -655,11 +665,11 @@ void configor_free() ...@@ -655,11 +665,11 @@ void configor_free()
g_configor.sections = NULL; g_configor.sections = NULL;
} }
if (g_configor.file_path) { if (g_configor.file_path) {
free(g_configor.file_path); internal_free(g_configor.file_path);
g_configor.file_path = NULL; g_configor.file_path = NULL;
} }
if (g_configor.exe_path) { if (g_configor.exe_path) {
free(g_configor.exe_path); internal_free(g_configor.exe_path);
g_configor.exe_path = NULL; g_configor.exe_path = NULL;
} }
} }
\ No newline at end of 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.
*/
#ifndef _NS_CONFIG_H #ifndef _NS_CONFIG_H
#define _NS_CONFIG_H #define _NS_CONFIG_H
......
/*
* 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 <ctype.h>
#include "strops.h" #include "strops.h"
#include "malloc_impl.h"
/* string to lower */ /* string to lower */
void strlwc(char *str) void strlwc(char *str)
...@@ -37,10 +53,10 @@ strlist *strsplit(const char *str, const char *split_s) ...@@ -37,10 +53,10 @@ strlist *strsplit(const char *str, const char *split_s)
if(!str) return NULL; if(!str) return NULL;
strlist *sl = strlist_alloc(0); strlist *sl = strlist_alloc(0);
char *ss = strdup(str); char *ss = ld_strdup(str);
if (!sl || !ss) { if (!sl || !ss) {
strlist_free(sl); strlist_free(sl);
free(ss); internal_free(ss);
return NULL; return NULL;
} }
...@@ -53,7 +69,7 @@ strlist *strsplit(const char *str, const char *split_s) ...@@ -53,7 +69,7 @@ strlist *strsplit(const char *str, const char *split_s)
} }
strtrim(cur); strtrim(cur);
strlist_set(sl, cur); strlist_set(sl, cur);
free(ss); internal_free(ss);
return sl; return sl;
} }
...@@ -64,14 +80,14 @@ strlist *strlist_alloc(size_t size) ...@@ -64,14 +80,14 @@ strlist *strlist_alloc(size_t size)
strlist *strs; strlist *strs;
if (size < STR_DEFAULT_SIZE) size = STR_DEFAULT_SIZE ; if (size < STR_DEFAULT_SIZE) size = STR_DEFAULT_SIZE ;
strs = (strlist *)calloc(1, sizeof *strs) ; strs = (strlist *)internal_calloc(1, sizeof *strs) ;
if (strs) { if (strs) {
strs->strs = (char **)calloc(size, sizeof *strs->strs); strs->strs = (char **)internal_calloc(size, sizeof *strs->strs);
if (strs->strs) { if (strs->strs) {
strs->size = size; strs->size = size;
} else { } else {
free(strs); internal_free(strs);
strs = NULL; strs = NULL;
} }
} }
...@@ -83,7 +99,7 @@ static void strlist_realloc(strlist *strs) ...@@ -83,7 +99,7 @@ static void strlist_realloc(strlist *strs)
if(!strs) return; if(!strs) return;
size_t size = 2*strs->size; size_t size = 2*strs->size;
if (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) { if (ss) {
strs->size = size; strs->size = size;
strs->strs = ss; strs->strs = ss;
...@@ -96,10 +112,10 @@ void strlist_free(strlist *strs) ...@@ -96,10 +112,10 @@ void strlist_free(strlist *strs)
{ {
if (!strs) return; if (!strs) return;
for (size_t i=0; i < strs->num; i++) { for (size_t i=0; i < strs->num; i++) {
free(strs->strs[i]); internal_free(strs->strs[i]);
} }
free(strs->strs); internal_free(strs->strs);
free(strs); internal_free(strs);
} }
void strlist_set(strlist *strs,const char *str) void strlist_set(strlist *strs,const char *str)
...@@ -109,7 +125,15 @@ void strlist_set(strlist *strs,const char *str) ...@@ -109,7 +125,15 @@ void strlist_set(strlist *strs,const char *str)
strlist_realloc(strs); strlist_realloc(strs);
} }
if (strs->num < strs->size) { 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++; 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
/*
* 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 #ifndef _STR_OPS_H
#define _STR_OPS_H #define _STR_OPS_H
...@@ -5,6 +20,7 @@ ...@@ -5,6 +20,7 @@
extern "C" { extern "C" {
#endif #endif
#include <features.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -14,13 +30,15 @@ typedef struct _str_list_ { ...@@ -14,13 +30,15 @@ typedef struct _str_list_ {
char **strs; /** string list */ char **strs; /** string list */
} strlist; } strlist;
strlist *strlist_alloc(size_t size); hidden strlist *strlist_alloc(size_t size);
void strlist_free(strlist *strs); 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); hidden char *ld_strdup(const char *);
size_t strtrim(char *str);
strlist *strsplit(const char *str, const char *split_s);
void strlist_set(strlist *strs, const char *str);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "malloc_impl.h"
/* Define the max length of the string */ /* Define the max length of the string */
#ifndef SECUREC_STRING_MAX_LEN #ifndef SECUREC_STRING_MAX_LEN
#define SECUREC_STRING_MAX_LEN 0x7fffffffUL #define SECUREC_STRING_MAX_LEN 0x7fffffffUL
...@@ -127,11 +129,11 @@ typedef enum { ...@@ -127,11 +129,11 @@ typedef enum {
#ifndef HILOG_PROHIBIT_ALLOCATION #ifndef HILOG_PROHIBIT_ALLOCATION
#ifndef SECUREC_MALLOC #ifndef SECUREC_MALLOC
#define SECUREC_MALLOC(x) malloc((size_t)(x)) #define SECUREC_MALLOC(x) internal_malloc((size_t)(x))
#endif #endif
#ifndef SECUREC_FREE #ifndef SECUREC_FREE
#define SECUREC_FREE(x) free((void *)(x)) #define SECUREC_FREE(x) internal_free((void *)(x))
#endif #endif
#else #else
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#ifndef MUSL_HILOG_ADAPTER_H #ifndef MUSL_HILOG_ADAPTER_H
#define MUSL_HILOG_ADAPTER_H #define MUSL_HILOG_ADAPTER_H
#include <features.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
...@@ -42,8 +43,8 @@ typedef enum { ...@@ -42,8 +43,8 @@ typedef enum {
LOG_LEVEL_MAX, LOG_LEVEL_MAX,
} LogLevel; } 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))); __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 #endif // MUSL_HILOG_ADAPTER_H
...@@ -47,4 +47,12 @@ hidden void __bin_chunk(struct chunk *); ...@@ -47,4 +47,12 @@ hidden void __bin_chunk(struct chunk *);
hidden extern int __malloc_replaced; 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 #endif
...@@ -328,6 +328,11 @@ void *__libc_malloc(size_t n) ...@@ -328,6 +328,11 @@ void *__libc_malloc(size_t n)
#else #else
void *malloc(size_t n) void *malloc(size_t n)
#endif #endif
{
return internal_malloc(n);
}
void *internal_malloc(size_t n)
{ {
struct chunk *c; struct chunk *c;
int i, j; int i, j;
...@@ -403,13 +408,18 @@ static size_t mal0_clear(char *p, size_t pagesz, size_t n) ...@@ -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) 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) { if (n && m > (size_t)-1/n) {
errno = ENOMEM; errno = ENOMEM;
return 0; return 0;
} }
n *= m; n *= m;
void *p = malloc(n); void *p = internal_malloc(n);
if (!p) return p; if (!p) return p;
if (!__malloc_replaced) { if (!__malloc_replaced) {
if (IS_MMAPPED(MEM_TO_CHUNK(p))) if (IS_MMAPPED(MEM_TO_CHUNK(p)))
...@@ -421,14 +431,19 @@ void *calloc(size_t m, size_t n) ...@@ -421,14 +431,19 @@ void *calloc(size_t m, size_t n)
} }
void *realloc(void *p, 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; struct chunk *self, *next;
size_t n0, n1; size_t n0, n1;
void *new; void *new;
if (!p) return malloc(n); if (!p) return internal_malloc(n);
if (!n) { if (!n) {
free(p); internal_free(p);
return NULL; return NULL;
} }
...@@ -444,7 +459,7 @@ void *realloc(void *p, size_t n) ...@@ -444,7 +459,7 @@ void *realloc(void *p, size_t n)
size_t newlen = n + extra; size_t newlen = n + extra;
/* Crash on realloc of freed chunk */ /* Crash on realloc of freed chunk */
if (extra & 1) a_crash(); if (extra & 1) a_crash();
if (newlen < PAGE_SIZE && (new = malloc(n-OVERHEAD))) { if (newlen < PAGE_SIZE && (new = internal_malloc(n-OVERHEAD))) {
n0 = n; n0 = n;
goto copy_free_ret; goto copy_free_ret;
} }
...@@ -487,11 +502,11 @@ void *realloc(void *p, size_t n) ...@@ -487,11 +502,11 @@ void *realloc(void *p, size_t n)
copy_realloc: copy_realloc:
/* As a last resort, allocate a new chunk and copy to it. */ /* 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; if (!new) return 0;
copy_free_ret: copy_free_ret:
memcpy(new, p, n0-OVERHEAD); memcpy(new, p, n0-OVERHEAD);
free(CHUNK_TO_MEM(self)); internal_free(CHUNK_TO_MEM(self));
return new; return new;
} }
...@@ -585,6 +600,11 @@ void __libc_free(void *p) ...@@ -585,6 +600,11 @@ void __libc_free(void *p)
#else #else
void free(void *p) void free(void *p)
#endif #endif
{
return internal_free(p);
}
void internal_free(void *p)
{ {
if (!p) return; if (!p) return;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册