提交 9c4f2b2a 编写于 作者: D dhy308 提交者: Gitee

Merge branch 'master' of gitee.com:openharmony/third_party_musl into release_sigchain_0113

Issue: I6AEEI
Test: Build & Boot Devices
Signed-off-by: Ndhy308 <tony.gan@huawei.com>
......@@ -2148,6 +2148,8 @@ musl_src_porting_file = [
"src/process/x86_64/__vfork.s",
"src/linux/cache.c",
"src/sched/sched_getcpu.c",
"src/conf/legacy.c",
"src/conf/sysconf.c",
"src/sigchain/sigchain.c",
]
......
......@@ -730,13 +730,6 @@ template("musl_libs") {
"//build/config/compiler:compiler",
":soft_hook",
]
cflags = [
"-mllvm",
"--instcombine-max-iterations=0",
"-ffp-contract=fast",
"-O3",
]
}
source_set("soft_musl_jemalloc") {
......
#include <sys/sysinfo.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <dirent.h>
int get_nprocs_conf()
{
DIR *d = opendir("/sys/devices/system/cpu");
struct dirent *de;
unsigned int cnt = 0;
if (!d)
return 1;
while ((de = readdir(d))) {
if ((de->d_type == DT_DIR) &&
(strlen(de->d_name) > 3) &&
(de->d_name[0] == 'c') &&
(de->d_name[1] == 'p') &&
(de->d_name[2] == 'u') &&
(isdigit(de->d_name[3])))
cnt++;
}
closedir(d);
return cnt;
}
int get_nprocs()
{
int cnt = 1;
FILE* fp = fopen("/sys/devices/system/cpu/online", "re");
if (fp != NULL) {
char buf[128];
memset(buf, 0, sizeof(buf));
if (!fgets(buf, sizeof(buf), fp)) {
return 1;
}
cnt = 0;
char *tmp, *p;
tmp = strtok_r(buf, ",", &p);
while (tmp != NULL) {
char *tok = strstr(tmp, "-");
if (tok) {
tok++;
cnt += atoi(tok) - atoi(tmp) + 1;
} else {
cnt += 1;
}
tmp = strtok_r(NULL, ",", &p);
}
fclose(fp);
}
return cnt;
}
long get_phys_pages()
{
return sysconf(_SC_PHYS_PAGES);
}
long get_avphys_pages()
{
return sysconf(_SC_AVPHYS_PAGES);
}
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/resource.h>
#include <signal.h>
#include <sys/sysinfo.h>
#include "syscall.h"
#include "libc.h"
#define JT(x) (-256|(x))
#define VER JT(1)
#define JT_ARG_MAX JT(2)
#define JT_MQ_PRIO_MAX JT(3)
#define JT_PAGE_SIZE JT(4)
#define JT_SEM_VALUE_MAX JT(5)
#define JT_NPROCESSORS_CONF JT(6)
#define JT_NPROCESSORS_ONLN JT(7)
#define JT_PHYS_PAGES JT(8)
#define JT_AVPHYS_PAGES JT(9)
#define JT_ZERO JT(10)
#define JT_DELAYTIMER_MAX JT(11)
#define RLIM(x) (-32768|(RLIMIT_ ## x))
long sysconf(int name)
{
static const short values[] = {
[_SC_ARG_MAX] = JT_ARG_MAX,
[_SC_CHILD_MAX] = RLIM(NPROC),
[_SC_CLK_TCK] = 100,
[_SC_NGROUPS_MAX] = 32,
[_SC_OPEN_MAX] = RLIM(NOFILE),
[_SC_STREAM_MAX] = -1,
[_SC_TZNAME_MAX] = TZNAME_MAX,
[_SC_JOB_CONTROL] = 1,
[_SC_SAVED_IDS] = 1,
[_SC_REALTIME_SIGNALS] = VER,
[_SC_PRIORITY_SCHEDULING] = -1,
[_SC_TIMERS] = VER,
[_SC_ASYNCHRONOUS_IO] = VER,
[_SC_PRIORITIZED_IO] = -1,
[_SC_SYNCHRONIZED_IO] = -1,
[_SC_FSYNC] = VER,
[_SC_MAPPED_FILES] = VER,
[_SC_MEMLOCK] = VER,
[_SC_MEMLOCK_RANGE] = VER,
[_SC_MEMORY_PROTECTION] = VER,
[_SC_MESSAGE_PASSING] = VER,
[_SC_SEMAPHORES] = VER,
[_SC_SHARED_MEMORY_OBJECTS] = VER,
[_SC_AIO_LISTIO_MAX] = -1,
[_SC_AIO_MAX] = -1,
[_SC_AIO_PRIO_DELTA_MAX] = JT_ZERO, /* ?? */
[_SC_DELAYTIMER_MAX] = JT_DELAYTIMER_MAX,
[_SC_MQ_OPEN_MAX] = -1,
[_SC_MQ_PRIO_MAX] = JT_MQ_PRIO_MAX,
[_SC_VERSION] = VER,
[_SC_PAGE_SIZE] = JT_PAGE_SIZE,
[_SC_RTSIG_MAX] = _NSIG - 1 - 31 - 3,
[_SC_SEM_NSEMS_MAX] = SEM_NSEMS_MAX,
[_SC_SEM_VALUE_MAX] = JT_SEM_VALUE_MAX,
[_SC_SIGQUEUE_MAX] = -1,
[_SC_TIMER_MAX] = -1,
[_SC_BC_BASE_MAX] = _POSIX2_BC_BASE_MAX,
[_SC_BC_DIM_MAX] = _POSIX2_BC_DIM_MAX,
[_SC_BC_SCALE_MAX] = _POSIX2_BC_SCALE_MAX,
[_SC_BC_STRING_MAX] = _POSIX2_BC_STRING_MAX,
[_SC_COLL_WEIGHTS_MAX] = COLL_WEIGHTS_MAX,
[_SC_EXPR_NEST_MAX] = -1,
[_SC_LINE_MAX] = -1,
[_SC_RE_DUP_MAX] = RE_DUP_MAX,
[_SC_2_VERSION] = VER,
[_SC_2_C_BIND] = VER,
[_SC_2_C_DEV] = -1,
[_SC_2_FORT_DEV] = -1,
[_SC_2_FORT_RUN] = -1,
[_SC_2_SW_DEV] = -1,
[_SC_2_LOCALEDEF] = -1,
[_SC_IOV_MAX] = IOV_MAX,
[_SC_THREADS] = VER,
[_SC_THREAD_SAFE_FUNCTIONS] = VER,
[_SC_GETGR_R_SIZE_MAX] = -1,
[_SC_GETPW_R_SIZE_MAX] = -1,
[_SC_LOGIN_NAME_MAX] = 256,
[_SC_TTY_NAME_MAX] = TTY_NAME_MAX,
[_SC_THREAD_DESTRUCTOR_ITERATIONS] = PTHREAD_DESTRUCTOR_ITERATIONS,
[_SC_THREAD_KEYS_MAX] = PTHREAD_KEYS_MAX,
[_SC_THREAD_STACK_MIN] = PTHREAD_STACK_MIN,
[_SC_THREAD_THREADS_MAX] = -1,
[_SC_THREAD_ATTR_STACKADDR] = VER,
[_SC_THREAD_ATTR_STACKSIZE] = VER,
[_SC_THREAD_PRIORITY_SCHEDULING] = VER,
[_SC_THREAD_PRIO_INHERIT] = -1,
[_SC_THREAD_PRIO_PROTECT] = -1,
[_SC_THREAD_PROCESS_SHARED] = VER,
[_SC_NPROCESSORS_CONF] = JT_NPROCESSORS_CONF,
[_SC_NPROCESSORS_ONLN] = JT_NPROCESSORS_ONLN,
[_SC_PHYS_PAGES] = JT_PHYS_PAGES,
[_SC_AVPHYS_PAGES] = JT_AVPHYS_PAGES,
[_SC_ATEXIT_MAX] = -1,
[_SC_PASS_MAX] = -1,
[_SC_XOPEN_VERSION] = _XOPEN_VERSION,
[_SC_XOPEN_XCU_VERSION] = _XOPEN_VERSION,
[_SC_XOPEN_UNIX] = 1,
[_SC_XOPEN_CRYPT] = -1,
[_SC_XOPEN_ENH_I18N] = 1,
[_SC_XOPEN_SHM] = 1,
[_SC_2_CHAR_TERM] = -1,
[_SC_2_UPE] = -1,
[_SC_XOPEN_XPG2] = -1,
[_SC_XOPEN_XPG3] = -1,
[_SC_XOPEN_XPG4] = -1,
[_SC_NZERO] = NZERO,
[_SC_XBS5_ILP32_OFF32] = -1,
[_SC_XBS5_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
[_SC_XBS5_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
[_SC_XBS5_LPBIG_OFFBIG] = -1,
[_SC_XOPEN_LEGACY] = -1,
[_SC_XOPEN_REALTIME] = -1,
[_SC_XOPEN_REALTIME_THREADS] = -1,
[_SC_ADVISORY_INFO] = VER,
[_SC_BARRIERS] = VER,
[_SC_CLOCK_SELECTION] = VER,
[_SC_CPUTIME] = VER,
[_SC_THREAD_CPUTIME] = VER,
[_SC_MONOTONIC_CLOCK] = VER,
[_SC_READER_WRITER_LOCKS] = VER,
[_SC_SPIN_LOCKS] = VER,
[_SC_REGEXP] = 1,
[_SC_SHELL] = 1,
[_SC_SPAWN] = VER,
[_SC_SPORADIC_SERVER] = -1,
[_SC_THREAD_SPORADIC_SERVER] = -1,
[_SC_TIMEOUTS] = VER,
[_SC_TYPED_MEMORY_OBJECTS] = -1,
[_SC_2_PBS] = -1,
[_SC_2_PBS_ACCOUNTING] = -1,
[_SC_2_PBS_LOCATE] = -1,
[_SC_2_PBS_MESSAGE] = -1,
[_SC_2_PBS_TRACK] = -1,
[_SC_SYMLOOP_MAX] = SYMLOOP_MAX,
[_SC_STREAMS] = JT_ZERO,
[_SC_2_PBS_CHECKPOINT] = -1,
[_SC_V6_ILP32_OFF32] = -1,
[_SC_V6_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
[_SC_V6_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
[_SC_V6_LPBIG_OFFBIG] = -1,
[_SC_HOST_NAME_MAX] = HOST_NAME_MAX,
[_SC_TRACE] = -1,
[_SC_TRACE_EVENT_FILTER] = -1,
[_SC_TRACE_INHERIT] = -1,
[_SC_TRACE_LOG] = -1,
[_SC_IPV6] = VER,
[_SC_RAW_SOCKETS] = VER,
[_SC_V7_ILP32_OFF32] = -1,
[_SC_V7_ILP32_OFFBIG] = sizeof(long)==4 ? 1 : -1,
[_SC_V7_LP64_OFF64] = sizeof(long)==8 ? 1 : -1,
[_SC_V7_LPBIG_OFFBIG] = -1,
[_SC_SS_REPL_MAX] = -1,
[_SC_TRACE_EVENT_NAME_MAX] = -1,
[_SC_TRACE_NAME_MAX] = -1,
[_SC_TRACE_SYS_MAX] = -1,
[_SC_TRACE_USER_EVENT_MAX] = -1,
[_SC_XOPEN_STREAMS] = JT_ZERO,
[_SC_THREAD_ROBUST_PRIO_INHERIT] = -1,
[_SC_THREAD_ROBUST_PRIO_PROTECT] = -1,
};
if (name >= sizeof(values)/sizeof(values[0]) || !values[name]) {
errno = EINVAL;
return -1;
} else if (values[name] >= -1) {
return values[name];
} else if (values[name] < -256) {
struct rlimit lim;
getrlimit(values[name]&16383, &lim);
if (lim.rlim_cur == RLIM_INFINITY)
return -1;
return lim.rlim_cur > LONG_MAX ? LONG_MAX : lim.rlim_cur;
}
switch ((unsigned char)values[name]) {
case VER & 255:
return _POSIX_VERSION;
case JT_ARG_MAX & 255:
return ARG_MAX;
case JT_MQ_PRIO_MAX & 255:
return MQ_PRIO_MAX;
case JT_PAGE_SIZE & 255:
return PAGE_SIZE;
case JT_SEM_VALUE_MAX & 255:
return SEM_VALUE_MAX;
case JT_DELAYTIMER_MAX & 255:
return DELAYTIMER_MAX;
case JT_NPROCESSORS_CONF & 255:
return get_nprocs_conf();
case JT_NPROCESSORS_ONLN & 255:
return get_nprocs();
case JT_PHYS_PAGES & 255:
case JT_AVPHYS_PAGES & 255: ;
unsigned long long mem;
struct sysinfo si;
__lsysinfo(&si);
if (!si.mem_unit) si.mem_unit = 1;
if (name==_SC_PHYS_PAGES) mem = si.totalram;
else mem = si.freeram + si.bufferram;
mem *= si.mem_unit;
mem /= PAGE_SIZE;
return (mem > LONG_MAX) ? LONG_MAX : mem;
case JT_ZERO & 255:
return 0;
}
return values[name];
}
......@@ -9,44 +9,26 @@
void* malloc(size_t bytes)
{
struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit(
&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
if (__get_memleak_hook_flag()) {
return dispatch_table->malloc(bytes);
}
if (!__get_global_hook_flag()) {
return MuslFunc(malloc)(bytes);
}
if (!__get_hook_flag()) {
return MuslFunc(malloc)(bytes);
}
return dispatch_table->malloc(bytes);
void*ret = dispatch_table->malloc(bytes);
return ret;
}
void* result = MuslMalloc(malloc)(bytes);
if (__predict_false(result == NULL)) {
//__musl_log(__MUSL_LOG_WARN, "malloc(%zu) failed: returning null pointer\n", bytes);
}
return MuslFunc(malloc)(bytes);
return result;
}
void free(void* mem)
{
struct MallocDispatchType* dispatch_table = (struct MallocDispatchType *)atomic_load_explicit(
&__musl_libc_globals.current_dispatch_table, memory_order_acquire);
volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
if (__predict_false(dispatch_table != NULL)) {
if (__get_memleak_hook_flag()) {
dispatch_table->free(mem);
return;
}
if (!__get_global_hook_flag()) {
MuslFunc(free)(mem);
return;
}
if (!__get_hook_flag()) {
MuslFunc(free)(mem);
return;
}
dispatch_table->free(mem);
return;
} else {
MuslMalloc(free)(mem);
}
MuslFunc(free)(mem);
}
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset)
......
......@@ -13,19 +13,12 @@ extern "C" {
#define MuslMalloc(func) func
#endif
#ifdef USE_JEMALLOC
#define MuslFunc(func) je_ ## func
#else
#define MuslFunc(func) internal_ ## func
#endif
extern void *internal_malloc(size_t);
extern void internal_free(void*);
void *__libc_mmap(void*, size_t, int, int, int, off_t);
int __libc_munmap(void*, size_t);
void *__libc_malloc(size_t);
void *__libc_calloc(size_t, size_t);
void *__libc_realloc(void *, size_t);
void __libc_free(void *);
void *__libc_valloc(size_t);
void *__libc_memalign(size_t, size_t);
size_t __libc_malloc_usable_size(void *);
......
......@@ -40,7 +40,7 @@ void* ohos_malloc_hook_init_function(size_t bytes);
static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
.malloc = ohos_malloc_hook_init_function,
.free = MuslFunc(free),
.free = MuslMalloc(free),
.mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
......@@ -459,7 +459,7 @@ void* ohos_malloc_hook_init_function(size_t bytes)
// __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname());
}
}
void*ptr = MuslFunc(malloc)(bytes);
void*ptr = MuslMalloc(malloc)(bytes);
return ptr;
}
......
......@@ -8,8 +8,8 @@
struct musl_libc_globals __musl_libc_globals;
struct MallocDispatchType __libc_malloc_default_dispatch = {
.malloc = MuslFunc(malloc),
.free = MuslFunc(free),
.malloc = MuslMalloc(malloc),
.free = MuslMalloc(free),
.mmap = MuslMalloc(mmap),
.munmap = MuslMalloc(munmap),
.calloc = MuslMalloc(calloc),
......
......@@ -30,6 +30,11 @@ extern int je_mallopt(int param, int value);
#define inline inline __attribute__((always_inline))
#endif
#ifdef HOOK_ENABLE
void *__libc_malloc(size_t);
void __libc_free(void *p);
#endif
static struct {
volatile uint64_t binmap;
struct bin bins[BINS_COUNT];
......@@ -623,16 +628,16 @@ static void trim(struct chunk *self, size_t n)
}
#ifdef HOOK_ENABLE
void *__libc_malloc(size_t n)
#else
void *malloc(size_t n)
#endif
{
#ifdef USE_JEMALLOC
return je_malloc(n);
#endif
return internal_malloc(n);
}
#endif
void *internal_malloc(size_t n)
{
......@@ -1240,16 +1245,16 @@ static void quarantine_bin(struct chunk *self)
#endif
#ifdef HOOK_ENABLE
void __libc_free(void *p)
#else
void free(void *p)
#endif
{
#ifdef USE_JEMALLOC
return je_free(p);
#endif
return internal_free(p);
}
#endif
void internal_free(void *p)
{
......
......@@ -130,7 +130,11 @@ Lreturn:
pop {r4, pc}
Lfunc_end:
#if defined(LOSCFG_KERNEL_LMS)
.size __memset, Lfunc_end - __memset
#else
.size memset, Lfunc_end - memset
#endif
.cantunwind
.fnend @ -- End function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册