提交 5713bb18 编写于 作者: F fangting

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

Signed-off-by: Nfangting <fangting12@huawei.com>
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
static int test_mallopt() static int test_mallopt()
{ {
return mallopt(0, 0) != 0; return mallopt(0, 0) == 0;
} }
int main() int main()
......
...@@ -2168,6 +2168,9 @@ musl_src_porting_file = [ ...@@ -2168,6 +2168,9 @@ musl_src_porting_file = [
"src/stdio/vfprintf.c", "src/stdio/vfprintf.c",
"src/stdio/__stdio_read.c", "src/stdio/__stdio_read.c",
"src/stdio/fread.c", "src/stdio/fread.c",
"src/stdio/fmemopen.c",
"src/stdio/freopen.c",
"src/stdio/stdin.c",
"src/internal/stdio_impl.h", "src/internal/stdio_impl.h",
"src/internal/vdso.c", "src/internal/vdso.c",
"src/time/clock_gettime.c", "src/time/clock_gettime.c",
...@@ -2187,6 +2190,7 @@ musl_src_porting_file = [ ...@@ -2187,6 +2190,7 @@ musl_src_porting_file = [
"src/env/getenv.c", "src/env/getenv.c",
"src/string/stpncpy.c", "src/string/stpncpy.c",
"src/string/strncpy.c", "src/string/strncpy.c",
"src/thread/pthread_once.c",
] ]
musl_inc_hook_files = [ musl_inc_hook_files = [
......
...@@ -393,6 +393,15 @@ template("musl_libs") { ...@@ -393,6 +393,15 @@ template("musl_libs") {
} }
defines = [] defines = []
if (musl_arch == "arm") {
defines += [ "MUSL_ARM_ARCH" ]
}
if (musl_arch == "aarch64") {
defines += [ "MUSL_AARCH64_ARCH" ]
}
if (musl_arch == "x86_64") {
defines += [ "MUSL_X86_64_ARCH" ]
}
if (musl_secure_level > 0) { if (musl_secure_level > 0) {
defines += [ "MALLOC_FREELIST_HARDENED" ] defines += [ "MALLOC_FREELIST_HARDENED" ]
} }
......
...@@ -9,6 +9,16 @@ extern "C" { ...@@ -9,6 +9,16 @@ extern "C" {
#include <bits/alltypes.h> #include <bits/alltypes.h>
#define M_SET_THREAD_CACHE -1001
#define M_THREAD_CACHE_ENABLE 1
#define M_THREAD_CACHE_DISABLE 0
#define M_FLUSH_THREAD_CACHE -1002
#define M_DELAYED_FREE -1003
#define M_DELAYED_FREE_ENABLE 1
#define M_DELAYED_FREE_DISABLE 0
void *malloc (size_t); void *malloc (size_t);
void *calloc (size_t, size_t); void *calloc (size_t, size_t);
void *realloc (void *, size_t); void *realloc (void *, size_t);
...@@ -17,6 +27,7 @@ void *valloc (size_t); ...@@ -17,6 +27,7 @@ void *valloc (size_t);
void *memalign(size_t, size_t); void *memalign(size_t, size_t);
size_t malloc_usable_size(void *); size_t malloc_usable_size(void *);
int mallopt(int param, int value);
struct mallinfo { struct mallinfo {
int arena; int arena;
......
...@@ -736,6 +736,7 @@ ...@@ -736,6 +736,7 @@
{ "name": "lutimes" }, { "name": "lutimes" },
{ "name": "madvise" }, { "name": "madvise" },
{ "name": "malloc" }, { "name": "malloc" },
{ "name": "mallopt" },
{ "name": "malloc_usable_size" }, { "name": "malloc_usable_size" },
{ "name": "mblen" }, { "name": "mblen" },
{ "name": "mbrlen" }, { "name": "mbrlen" },
......
...@@ -380,7 +380,7 @@ static void malloc_finalize() ...@@ -380,7 +380,7 @@ static void malloc_finalize()
fclose(stderr); fclose(stderr);
} }
bool finish_install_ohos_malloc_hooks(struct musl_libc_globals* globals, const char* options, const char* prefix) bool finish_install_ohos_malloc_hooks(struct musl_libc_globals* globals, const char* options, const char* prefix, void* shared_library_handle)
{ {
init_func_t init_func = (init_func_t)(function_of_shared_lib[INITIALIZE_FUNCTION]); init_func_t init_func = (init_func_t)(function_of_shared_lib[INITIALIZE_FUNCTION]);
if (!init_func(&__libc_malloc_default_dispatch, NULL, options)) { if (!init_func(&__libc_malloc_default_dispatch, NULL, options)) {
...@@ -395,6 +395,7 @@ bool finish_install_ohos_malloc_hooks(struct musl_libc_globals* globals, const c ...@@ -395,6 +395,7 @@ bool finish_install_ohos_malloc_hooks(struct musl_libc_globals* globals, const c
clear_function_table(); clear_function_table();
return false; return false;
} }
atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)shared_library_handle, memory_order_seq_cst);
atomic_store_explicit(&__musl_libc_globals.so_dispatch_table, (volatile long long)&globals->malloc_dispatch_table, memory_order_seq_cst); atomic_store_explicit(&__musl_libc_globals.so_dispatch_table, (volatile long long)&globals->malloc_dispatch_table, memory_order_seq_cst);
atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile long long)&globals->malloc_dispatch_table, memory_order_seq_cst); atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile long long)&globals->malloc_dispatch_table, memory_order_seq_cst);
} }
...@@ -437,8 +438,7 @@ static void install_ohos_malloc_hook(struct musl_libc_globals* globals, const ch ...@@ -437,8 +438,7 @@ static void install_ohos_malloc_hook(struct musl_libc_globals* globals, const ch
return; return;
} }
if (finish_install_ohos_malloc_hooks(globals, NULL, prefix)) { if (finish_install_ohos_malloc_hooks(globals, NULL, prefix, shared_library_handle)) {
atomic_store_explicit(&ohos_malloc_hook_shared_library, (volatile long long)shared_library_handle, memory_order_seq_cst);
if (strncmp(__malloc_hook_function_prefix, prefix, strlen(prefix)) == 0) { if (strncmp(__malloc_hook_function_prefix, prefix, strlen(prefix)) == 0) {
atomic_store_explicit(&ohos_malloc_ever_shared_library_handle, (volatile long long)shared_library_handle, memory_order_seq_cst); atomic_store_explicit(&ohos_malloc_ever_shared_library_handle, (volatile long long)shared_library_handle, memory_order_seq_cst);
} else { } else {
......
...@@ -74,6 +74,9 @@ struct pthread { ...@@ -74,6 +74,9 @@ struct pthread {
volatile int killlock[1]; volatile int killlock[1];
char *dlerror_buf; char *dlerror_buf;
void *stdio_locks; void *stdio_locks;
#ifdef RESERVE_SIGNAL_STACK
void *signal_stack;
#endif
/* Part 3 -- the positions of these fields relative to /* Part 3 -- the positions of these fields relative to
* the end of the structure is external and internal ABI. */ * the end of the structure is external and internal ABI. */
......
...@@ -63,6 +63,7 @@ hidden off_t __stdio_seek(FILE *, off_t, int); ...@@ -63,6 +63,7 @@ hidden off_t __stdio_seek(FILE *, off_t, int);
hidden int __stdio_close(FILE *); hidden int __stdio_close(FILE *);
hidden int __fill_buffer(FILE *f); hidden int __fill_buffer(FILE *f);
hidden int __toread(FILE *); hidden int __toread(FILE *);
hidden int __towrite(FILE *); hidden int __towrite(FILE *);
......
#include "stdio_impl.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <inttypes.h>
#include "libc.h"
struct cookie {
size_t pos, len, size;
unsigned char *buf;
int mode;
};
struct mem_FILE {
FILE f;
struct cookie c;
unsigned char buf[UNGET+BUFSIZ], buf2[];
};
static off_t mseek(FILE *f, off_t off, int whence)
{
ssize_t base;
struct cookie *c = f->cookie;
if (whence>2U) {
fail:
errno = EINVAL;
return -1;
}
base = (size_t [3]){0, c->pos, c->len}[whence];
if (off < -base || off > (ssize_t)c->size-base) goto fail;
return c->pos = base+off;
}
static size_t mread(FILE *f, unsigned char *buf, size_t len)
{
struct cookie *c = f->cookie;
size_t rem = c->len - c->pos;
if (c->pos > c->len) rem = 0;
if (len > rem) {
len = rem;
f->flags |= F_EOF;
}
memcpy(buf, c->buf+c->pos, len);
c->pos += len;
rem -= len;
if (rem > f->buf_size) rem = f->buf_size;
f->rpos = f->buf;
f->rend = f->buf + rem;
memcpy(f->rpos, c->buf+c->pos, rem);
c->pos += rem;
return len;
}
static size_t mwrite(FILE *f, const unsigned char *buf, size_t len)
{
struct cookie *c = f->cookie;
size_t rem;
size_t len2 = f->wpos - f->wbase;
if (len2) {
f->wpos = f->wbase;
if (mwrite(f, f->wpos, len2) < len2) return 0;
}
if (c->mode == 'a') c->pos = c->len;
rem = c->size - c->pos;
if (len > rem) len = rem;
memcpy(c->buf+c->pos, buf, len);
c->pos += len;
if (c->pos > c->len) {
c->len = c->pos;
if (c->len < c->size) c->buf[c->len] = 0;
else if ((f->flags&F_NORD) && c->size) c->buf[c->size-1] = 0;
}
return len;
}
static int mclose(FILE *m)
{
return 0;
}
FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
{
struct mem_FILE *f;
int plus = !!strchr(mode, '+');
if (!strchr("rwa", *mode)) {
errno = EINVAL;
return 0;
}
if (!buf && size > PTRDIFF_MAX) {
errno = ENOMEM;
return 0;
}
f = malloc(sizeof *f + (buf?0:size));
if (!f) return 0;
memset(f, 0, offsetof(struct mem_FILE, buf));
f->f.cookie = &f->c;
f->f.fd = -1;
f->f.lbf = EOF;
f->f.buf = f->buf + UNGET;
f->f.buf_size = sizeof f->buf - UNGET;
if (!buf) {
buf = f->buf2;
memset(buf, 0, size);
}
f->c.buf = buf;
f->c.size = size;
f->c.mode = *mode;
if (!plus) f->f.flags = (*mode == 'r') ? F_NOWR : F_NORD;
if (*mode == 'r') f->c.len = size;
else if (*mode == 'a') f->c.len = f->c.pos = strnlen(buf, size);
else if (plus) *f->c.buf = 0;
f->f.read = mread;
f->f.readx = mread;
f->f.write = mwrite;
f->f.seek = mseek;
f->f.close = mclose;
if (!libc.threaded) f->f.lock = -1;
return __ofl_add(&f->f);
}
#include "stdio_impl.h"
#include <fcntl.h>
#include <unistd.h>
/* The basic idea of this implementation is to open a new FILE,
* hack the necessary parts of the new FILE into the old one, then
* close the new FILE. */
/* Locking IS necessary because another thread may provably hold the
* lock, via flockfile or otherwise, when freopen is called, and in that
* case, freopen cannot act until the lock is released. */
FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f)
{
int fl = __fmodeflags(mode);
FILE *f2;
FLOCK(f);
fflush(f);
if (!filename) {
if (fl&O_CLOEXEC)
__syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC);
fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0) {
goto fail;
}
} else {
f2 = fopen(filename, mode);
if (!f2) goto fail;
if (f2->fd == f->fd) {
f2->fd = -1; /* avoid closing in fclose */
}
else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) {
goto fail2;
}
f->flags = (f->flags & F_PERM) | f2->flags;
f->read = f2->read;
f->readx = f2->readx;
f->write = f2->write;
f->seek = f2->seek;
f->close = f2->close;
fclose(f2);
}
FUNLOCK(f);
return f;
fail2:
fclose(f2);
fail:
fclose(f);
return NULL;
}
weak_alias(freopen, freopen64);
#include "stdio_impl.h"
#undef stdin
static unsigned char buf[BUFSIZ+UNGET];
hidden FILE __stdin_FILE = {
.buf = buf+UNGET,
.buf_size = sizeof buf-UNGET,
.fd = 0,
.flags = F_PERM | F_NOWR,
.read = __stdio_read,
.readx = __stdio_readx,
.seek = __stdio_seek,
.close = __stdio_close,
.lock = -1,
};
FILE *const stdin = &__stdin_FILE;
FILE *volatile __stdin_used = &__stdin_FILE;
...@@ -3,17 +3,18 @@ ...@@ -3,17 +3,18 @@
char *__stpncpy(char *dst, const char *src, size_t n) char *__stpncpy(char *dst, const char *src, size_t n)
{ {
char *d = dst, *s = src; char *d = dst, *s = src;
char *flag = dst; dst = &dst[n];
while (n > 0){ while (n > 0){
*d = *src; if((*d++ = *src++) == 0){
if(*s != 0){ dst = d - 1;
s++; while(--n > 0){
flag++; *d++ = 0;
}
break;
} }
d++;
n--; n--;
} }
return (++flag); return (dst);
} }
weak_alias(__stpncpy, stpncpy); weak_alias(__stpncpy, stpncpy);
...@@ -4,12 +4,11 @@ char *strncpy(char *dst, const char *src, size_t n) ...@@ -4,12 +4,11 @@ char *strncpy(char *dst, const char *src, size_t n)
{ {
char *d = dst, *s = src; char *d = dst, *s = src;
while (n > 0){ while (n > 0){
*d = *s; if((*d = *s) != 0){
if(*s != 0){
s++; s++;
} }
d++; d++;
n--; n--;
} }
return (d); return (dst);
} }
\ No newline at end of file
...@@ -61,6 +61,7 @@ void __pthread_reserve_signal_stack() ...@@ -61,6 +61,7 @@ void __pthread_reserve_signal_stack()
sigaltstack(&signal_stack, NULL); sigaltstack(&signal_stack, NULL);
pthread_t self = __pthread_self(); pthread_t self = __pthread_self();
self->signal_stack = stack;
char name[ANON_STACK_NAME_SIZE]; char name[ANON_STACK_NAME_SIZE];
snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid); snprintf(name, ANON_STACK_NAME_SIZE, "signal_stack:%d", __pthread_self()->tid);
prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name); prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, signal_stack.ss_sp, signal_stack.ss_size, name);
...@@ -69,13 +70,20 @@ void __pthread_reserve_signal_stack() ...@@ -69,13 +70,20 @@ void __pthread_reserve_signal_stack()
void __pthread_release_signal_stack() void __pthread_release_signal_stack()
{ {
pthread_t self = __pthread_self();
if (self->signal_stack == NULL) {
return;
}
stack_t signal_stack, old_stack; stack_t signal_stack, old_stack;
memset(&signal_stack, 0, sizeof(signal_stack)); memset(&signal_stack, 0, sizeof(signal_stack));
signal_stack.ss_flags = SS_DISABLE; signal_stack.ss_flags = SS_DISABLE;
sigaltstack(&signal_stack, &old_stack); sigaltstack(&signal_stack, &old_stack);
munmap(self->signal_stack, __default_guardsize);
if (old_stack.ss_flags != SS_DISABLE) { if (old_stack.ss_flags != SS_DISABLE) {
munmap(old_stack.ss_sp, old_stack.ss_size); munmap(old_stack.ss_sp, old_stack.ss_size);
} }
self->signal_stack = NULL;
} }
weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack); weak_alias(__pthread_reserve_signal_stack, pthread_reserve_signal_stack);
......
#include "pthread_impl.h"
static void undo(void *control)
{
/* Wake all waiters, since the waiter status is lost when
* resetting control to the initial state. */
if (a_swap(control, 0) == 3)
__wake(control, -1, 1);
}
hidden int __pthread_once_full(pthread_once_t *control, void (*init)(void))
{
/* Try to enter initializing state. Four possibilities:
* 0 - we're the first or the other cancelled; run init
* 1 - another thread is running init; wait
* 2 - another thread finished running init; just return
* 3 - another thread is running init, waiters present; wait */
for (;;) switch (a_cas(control, 0, 1)) {
case 0:
pthread_cleanup_push(undo, control);
init();
pthread_cleanup_pop(0);
if (a_swap(control, 2) == 3)
__wake(control, -1, 1);
return 0;
case 1:
/* If this fails, so will __wait. */
a_cas(control, 1, 3);
case 3:
__wait(control, 0, 3, 1);
continue;
case 2:
return 0;
}
}
#if defined(MUSL_AARCH64_ARCH) || defined(MUSL_ARM_ARCH)
int __pthread_once(pthread_once_t *control, void (*init)(void))
{
/* Return immediately if init finished before, use load aquire to ensure that
* effects of the init routine are visible to the caller. */
if (a_ll((volatile int *)control) == 2) {
return 0;
}
return __pthread_once_full(control, init);
}
#else
int __pthread_once(pthread_once_t *control, void (*init)(void))
{
/* Return immediately if init finished before, but ensure that
* effects of the init routine are visible to the caller. */
if (*(volatile int *)control == 2) {
a_barrier();
return 0;
}
return __pthread_once_full(control, init);
}
#endif
weak_alias(__pthread_once, pthread_once);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册