未验证 提交 ce262772 编写于 作者: O openharmony_ci 提交者: Gitee

!462 封装internal_malloc、internal_free、internal_realloc、internal_calloc接口

Merge pull request !462 from dhy308/fix_asan_branch
......@@ -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);
......
......@@ -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;
}
......
/*
* 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;
}
......
/*
* 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
......
/*-------------------------------------------------------------------------*/
/**
@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
/*
* 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
......
/*
* 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
/*
* 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
}
......
......@@ -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
......
......@@ -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
......@@ -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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册