Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# SPDX-FileCopyrightText: Huawei Inc.
#
# SPDX-License-Identifier: Apache-2.0
Patch generated from OpenHarmony-v3.1-Release version, with the following commands:
cp -r porting/linux/user/* .
git add -A
git diff --staged > openharmony-linux-user.patch
The hunks from 0001-Make-dynamic-linker-a-relative-symlink-to-libc.patch in
Yocto was removed manually afterwards.
The hunk for the file sys/capabilty.h has been moved to a seperate patch
file so it can be ignored for some configurations that include components
with a dependency on the libcap package, as this pakage already provide
this header.
Upstream-status: Pending
Signed-off-by: Esben Haabendal <esben.haabendal@huawei.com>
diff --git a/arch/arm/bits/fenv.h b/arch/arm/bits/fenv.h
index d85fc86d7947..b497a9878800 100644
--- a/arch/arm/bits/fenv.h
+++ b/arch/arm/bits/fenv.h
@@ -9,10 +9,10 @@
#define FE_INEXACT 16
#define FE_ALL_EXCEPT 31
#define FE_TONEAREST 0
-#define FE_DOWNWARD 0x800000
-#define FE_UPWARD 0x400000
#define FE_TOWARDZERO 0xc00000
#endif
+#define FE_DOWNWARD 0x800000
+#define FE_UPWARD 0x400000
typedef unsigned long fexcept_t;
diff --git a/arch/generic/bits/shm.h b/arch/generic/bits/shm.h
index 8d19378191ba..da07a2a0cb50 100644
--- a/arch/generic/bits/shm.h
+++ b/arch/generic/bits/shm.h
@@ -1,4 +1,4 @@
-#define SHMLBA 4096
+#define SHMLBA (4*4096)
struct shmid_ds {
struct ipc_perm shm_perm;
diff --git a/include/ctype.h b/include/ctype.h
index 7936536f577c..7b91d3ef6228 100644
--- a/include/ctype.h
+++ b/include/ctype.h
@@ -66,6 +66,11 @@ int toascii(int);
#define _toupper(a) ((a)&0x5f)
#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
+#include <stdint.h>
+
+const int32_t **__ctype_tolower_loc(void);
+const int32_t **__ctype_toupper_loc(void);
+
#endif
#ifdef __cplusplus
diff --git a/include/dlfcn.h b/include/dlfcn.h
index 13ab71dd071e..e87392e70398 100644
--- a/include/dlfcn.h
+++ b/include/dlfcn.h
@@ -24,6 +24,21 @@ char *dlerror(void);
void *dlopen(const char *, int);
void *dlsym(void *__restrict, const char *__restrict);
+/* namespace apis */
+typedef const char* Dl_namespace;
+
+void dlns_init(Dl_namespace *, const char *);
+
+/* open dso in given namespace which has own lib search paths
+ * when namespace is null, it's same to dlopen()
+ * void to use "default" as namespace, which is the default namespace*/
+void *dlopen_ns(Dl_namespace *, const char *, int);
+
+/* create the namespace and set lib search paths of namespace,
+ * the paths should be splited by ':'. When namespace already exist,
+ * return error */
+int dlns_create(Dl_namespace *, const char *);
+
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
typedef struct {
const char *dli_fname;
diff --git a/include/pthread.h b/include/pthread.h
index 984db6806418..b430fbffb656 100644
--- a/include/pthread.h
+++ b/include/pthread.h
@@ -6,6 +6,17 @@ extern "C" {
#include <features.h>
+/* Musl did not provide the "owner" macro directly,
+ * so users can not access the mutex-ower-ID.
+ * Thus we added this macro for getting the owner-ID
+ * of the mutex. */
+
+/* These macros provides macros for accessing inner
+ * attributes of the pthread_mutex_t struct.
+ * It is intended for solving the coompiling failure
+ * of Dopra codes which claims that .__data.* realm
+ * can not be found in pthread_mutex_t. */
+
#define __NEED_time_t
#define __NEED_clockid_t
#define __NEED_struct_timespec
@@ -78,6 +89,7 @@ int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void
int pthread_detach(pthread_t);
_Noreturn void pthread_exit(void *);
int pthread_join(pthread_t, void **);
+pid_t pthread_gettid_np(pthread_t);
#ifdef __GNUC__
__attribute__((const))
diff --git a/include/sys/sysinfo.h b/include/sys/sysinfo.h
index 6a3931e52010..9b095ffb500b 100644
--- a/include/sys/sysinfo.h
+++ b/include/sys/sysinfo.h
@@ -5,6 +5,7 @@
extern "C" {
#endif
+#ifndef _LINUX_KERNEL_H
#define SI_LOAD_SHIFT 16
struct sysinfo {
@@ -22,6 +23,7 @@ struct sysinfo {
unsigned mem_unit;
char __reserved[256];
};
+#endif
int sysinfo (struct sysinfo *);
int get_nprocs_conf (void);
diff --git a/include/unistd.h b/include/unistd.h
index 7bcbff943d4a..c9a87b1a14b6 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -190,6 +190,7 @@ int syncfs(int);
int euidaccess(const char *, int);
int eaccess(const char *, int);
ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned);
+pid_t gettid(void);
#endif
#if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
@@ -462,6 +463,19 @@ ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned);
#define _CS_V6_ENV 1148
#define _CS_V7_ENV 1149
+#ifdef _GNU_SOURCE
+#ifndef TEMP_FAILURE_RETRY
+#define MUSL_TEMP_FAILURE_RETRY(expression) \
+ (__extension__ \
+ ({ long int __result; \
+ do __result = (long int)(expression); \
+ while(__result == -1L&& errno == EINTR); \
+ __result;}))
+
+#define TEMP_FAILURE_RETRY(expression) MUSL_TEMP_FAILURE_RETRY(expression)
+#endif
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index afec985a3dbe..1219ee601a3e 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -32,6 +32,7 @@ static void error(const char *, ...);
#define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
#define countof(a) ((sizeof (a))/(sizeof (a)[0]))
+#define DSO_FLAGS_NODELETE 0x1
struct debug {
int ver;
@@ -56,6 +57,9 @@ struct dso {
size_t *dynv;
struct dso *next, *prev;
+ /* mark the dso status */
+ unsigned int flags;
+
Phdr *phdr;
int phnum;
size_t phentsize;
@@ -76,10 +80,12 @@ struct dso {
char mark;
char bfs_built;
char runtime_loaded;
+ char by_dlopen;
struct dso **deps, *needed_by;
size_t ndeps_direct;
size_t next_dep;
int ctor_visitor;
+ int nr_dlopen;
char *rpath_orig, *rpath;
struct tls_module tls;
size_t tls_id;
@@ -1021,6 +1027,8 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
ldso.prev = tail;
tail = &ldso;
}
+ /* increase libc dlopen refcnt */
+ a_inc(&ldso.nr_dlopen);
return &ldso;
}
if (strchr(name, '/')) {
@@ -1030,6 +1038,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
/* Search for the name to see if it's already loaded */
for (p=head->next; p; p=p->next) {
if (p->shortname && !strcmp(p->shortname, name)) {
+ a_inc(&p->nr_dlopen);
return p;
}
}
@@ -1046,11 +1055,13 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
if (!sys_path) {
char *prefix = 0;
size_t prefix_len;
+ char *filename = ldso.name;
if (ldso.name[0]=='/') {
char *s, *t, *z;
for (s=t=z=ldso.name; *s; s++)
if (*s=='/') z=t, t=s;
prefix_len = z-ldso.name;
+ filename = t+1;
if (prefix_len < PATH_MAX)
prefix = ldso.name;
}
@@ -1058,11 +1069,12 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
prefix = "";
prefix_len = 0;
}
+ size_t name_len = strchrnul(filename, '.') - filename;
char etc_ldso_path[prefix_len + 1
- + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
+ + sizeof "/etc/.path" + name_len];
snprintf(etc_ldso_path, sizeof etc_ldso_path,
- "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
- (int)prefix_len, prefix);
+ "%.*s/etc/%.*s.path",
+ (int)prefix_len, prefix, (int)name_len, filename);
FILE *f = fopen(etc_ldso_path, "rbe");
if (f) {
if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
@@ -1074,7 +1086,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
sys_path = "";
}
}
- if (!sys_path) sys_path = SYS_PATH_DFLT;
+ if (!sys_path) sys_path = strdup(SYS_PATH_DFLT);
fd = path_open(name, sys_path, buf, sizeof buf);
}
pathname = buf;
@@ -1092,6 +1104,8 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
if (!p->shortname && pathname != name)
p->shortname = strrchr(p->name, '/')+1;
close(fd);
+ /* increase dlopen refcnt */
+ a_inc(&p->nr_dlopen);
return p;
}
}
@@ -1137,6 +1151,7 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
p->ino = st.st_ino;
p->needed_by = needed_by;
p->name = p->buf;
+ p->nr_dlopen = 1;
p->runtime_loaded = runtime;
strcpy(p->name, pathname);
/* Add a shortname only if name arg was not an explicit pathname. */
@@ -1166,6 +1181,9 @@ static struct dso *load_library(const char *name, struct dso *needed_by)
p->prev = tail;
tail = p;
+ if (runtime)
+ p->by_dlopen = 1;
+
if (DL_FDPIC) makefuncdescs(p);
if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
@@ -2086,6 +2104,10 @@ void *dlopen(const char *file, int mode)
* relocations resolved to symbol definitions that get removed. */
redo_lazy_relocs();
+ if (mode & RTLD_NODELETE) {
+ p->flags |= DSO_FLAGS_NODELETE;
+ }
+
update_tls_size();
if (tls_cnt != orig_tls_cnt)
install_new_tls();
@@ -2169,6 +2191,102 @@ static void *do_dlsym(struct dso *p, const char *s, void *ra)
return laddr(def.dso, def.sym->st_value);
}
+static int do_dlclose(struct dso *p)
+{
+ int old;
+ size_t n;
+ struct dso *d;
+
+ if (__dl_invalid_handle(p))
+ return -1;
+
+ if (!p->by_dlopen) {
+ error("Library %s is not loaded by dlopen", p->name);
+ return -1;
+ }
+
+ /* dso is marked as RTLD_NODELETE library, do nothing here. */
+ if ((p->flags & DSO_FLAGS_NODELETE) != 0) {
+ return 0;
+ }
+
+ old = a_fetch_add(&p->nr_dlopen, -1);
+ if (old > 1)
+ return 0;
+
+ /* call destructors if needed */
+ if (p->constructed) {
+ size_t dyn[DYN_CNT];
+ decode_vec(p->dynv, dyn, DYN_CNT);
+ if (dyn[0] & (1<<DT_FINI_ARRAY)) {
+ n = dyn[DT_FINI_ARRAYSZ] / sizeof(size_t);
+ size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY]) + n;
+ while (n--)
+ ((void (*)(void))*--fn)();
+ }
+ p->constructed = 0;
+ }
+
+ /* remove dso symbols from global list */
+ if (p->syms_next) {
+ for (d = head; d->syms_next != p; d = d->syms_next)
+ ; /* NOP */
+ d->syms_next = p->syms_next;
+ } else if (p == syms_tail) {
+ for (d = head; d->syms_next != p; d = d->syms_next)
+ ; /* NOP */
+ d->syms_next = NULL;
+ syms_tail = d;
+ }
+
+ /* remove dso from lazy list if needed */
+ if (p == lazy_head) {
+ lazy_head = p->lazy_next;
+ } else if (p->lazy_next) {
+ for (d = lazy_head; d->lazy_next != p; d = d->lazy_next)
+ ; /* NOP */
+ d->lazy_next = p->lazy_next;
+ }
+
+ /* remove dso from fini list */
+ if (p == fini_head) {
+ fini_head = p->fini_next;
+ } else if (p->fini_next) {
+ for (d = fini_head; d->fini_next != p; d = d->fini_next)
+ ; /* NOP */
+ d->fini_next = p->fini_next;
+ }
+
+ /* remove dso from global dso list */
+ if (p == tail) {
+ tail = p->prev;
+ tail->next = NULL;
+ } else {
+ p->next->prev = p->prev;
+ p->prev->next = p->next;
+ }
+
+ if (p->lazy != NULL)
+ free(p->lazy);
+ if (p->deps != no_deps)
+ free(p->deps);
+ unmap_library(p);
+ free(p);
+
+ return 0;
+}
+
+hidden int __dlclose(void *p)
+{
+ int rc;
+ pthread_rwlock_wrlock(&lock);
+ __inhibit_ptc();
+ rc = do_dlclose(p);
+ __release_ptc();
+ pthread_rwlock_unlock(&lock);
+ return rc;
+}
+
int dladdr(const void *addr_arg, Dl_info *info)
{
size_t addr = (size_t)addr_arg;
@@ -2312,3 +2430,46 @@ static void error(const char *fmt, ...)
__dl_vseterr(fmt, ap);
va_end(ap);
}
+void dlns_init(Dl_namespace *ns, const char *name)
+{
+ *ns = name;
+}
+
+void *dlopen_ns(Dl_namespace *ns, const char *file, int mode)
+{
+ if (!ns) return NULL;
+
+ return dlopen(file, mode);
+}
+
+int dlns_create(Dl_namespace *ns,const char *search_path)
+{
+ int sys_length = 0;
+ int search_length = 0;
+ char * new_path = NULL;
+
+ if (!search_path || !ns) return EINVAL;
+
+ sys_length = strlen(sys_path);
+ search_length = strlen(search_path);
+
+ if (search_path[0] != ':'){
+ sys_length += search_length + 1;
+ } else {
+ sys_length += search_length;
+ }
+
+ new_path = (char *)malloc(sys_length + 1);
+ if (!new_path) return ENOMEM;
+
+ strcpy(new_path, sys_path);
+ if (search_path[0] != ':') {
+ strcat(new_path, ":");
+ }
+ strcat(new_path, search_path);
+
+ free(sys_path);
+ sys_path = new_path;
+
+ return 0;
+}
diff --git a/src/hook/common_def.h b/src/hook/common_def.h
new file mode 100755
index 000000000000..7d74b3da0c27
--- /dev/null
+++ b/src/hook/common_def.h
@@ -0,0 +1,10 @@
+#ifndef _COMMON_DEF_H
+#define _COMMON_DEF_H
+
+#include <unistd.h>
+#include <stdlib.h>
+
+#define __predict_true(exp) __builtin_expect((exp) != 0, 1)
+#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
+
+#endif
\ No newline at end of file
diff --git a/src/hook/malloc_common.c b/src/hook/malloc_common.c
new file mode 100755
index 000000000000..50abc4d5e5d5
--- /dev/null
+++ b/src/hook/malloc_common.c
@@ -0,0 +1,33 @@
+#ifdef HOOK_ENABLE
+#include <unistd.h>
+#include <sys/types.h>
+#include "musl_malloc.h"
+#include <malloc.h>
+#include "musl_malloc_dispatch_table.h"
+#include "common_def.h"
+#include "musl_preinit_common.h"
+
+void* malloc(size_t bytes)
+{
+ volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
+ if (__predict_false(dispatch_table != NULL)) {
+ 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 result;
+}
+
+void free(void* mem)
+{
+ volatile const struct MallocDispatchType* dispatch_table = get_current_dispatch_table();
+ if (__predict_false(dispatch_table != NULL)) {
+ dispatch_table->free(mem);
+ } else {
+ MuslMalloc(free)(mem);
+ }
+}
+#endif
diff --git a/src/hook/musl_malloc.h b/src/hook/musl_malloc.h
new file mode 100755
index 000000000000..27f30869d0d8
--- /dev/null
+++ b/src/hook/musl_malloc.h
@@ -0,0 +1,26 @@
+#ifndef _MUSL_MALLOC_H
+#define _MUSL_MALLOC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HOOK_ENABLE
+#define MuslMalloc(func) __libc_ ## func
+#else
+#define MuslMalloc(func) func
+#endif
+
+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 *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/musl_malloc_dispatch.h b/include/musl_malloc_dispatch.h
new file mode 100755
index 000000000000..15b859eea083
--- /dev/null
+++ b/include/musl_malloc_dispatch.h
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
@@ -0,0 +1,37 @@
+#ifndef _MUSL_MALLOC_DISPATCH_H
+#define _MUSL_MALLOC_DISPATCH_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+typedef void* (*MallocMallocType)(size_t);
+typedef void* (*MallocReallocType)(void*, size_t);
+typedef void* (*MallocCallocType)(size_t, size_t);
+typedef void* (*MallocVallocType)(size_t);
+typedef void (*MallocFreeType)(void*);
+typedef void* (*MallocMemalignType)(size_t, size_t);
+typedef size_t (*MallocMallocUsableSizeType)(void*);
+typedef bool (*GetHookFlagType)();
+typedef bool (*SetHookFlagType)(bool);
+
+struct MallocDispatchType {
+ MallocMallocType malloc;
+ MallocCallocType calloc;
+ MallocReallocType realloc;
+ MallocVallocType valloc;
+ MallocFreeType free;
+ MallocMemalignType memalign;
+ MallocMallocUsableSizeType malloc_usable_size;
+ GetHookFlagType get_hook_flag;
+ SetHookFlagType set_hook_flag;
+};
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/include/musl_malloc_dispatch_table.h b/include/musl_malloc_dispatch_table.h
new file mode 100755
index 000000000000..430de744bb50
--- /dev/null
+++ b/include/musl_malloc_dispatch_table.h
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@@ -0,0 +1,13 @@
+#ifndef _MUSL_MALLOC_DISPATCH_TABLE_H
+#define _MUSL_MALLOC_DISPATCH_TABLE_H
+
+#include "musl_malloc_dispatch.h"
+#include <stdatomic.h>
+
+struct musl_libc_globals {
+ volatile atomic_llong current_dispatch_table;
+ volatile atomic_llong so_dispatch_table;
+ struct MallocDispatchType malloc_dispatch_table;
+};
+
+#endif
diff --git a/src/hook/musl_preinit.c b/src/hook/musl_preinit.c
new file mode 100755
index 000000000000..57a0bf12fb84
--- /dev/null
+++ b/src/hook/musl_preinit.c
@@ -0,0 +1,400 @@
+/*
+The hook mode has 3 kinds which can be set by command "param set":
+(1) param set libc.hook_mode "startup:\"prog1 \""
+(2) param set libc.hook_mode startup:program1
+(3) param set libc.hook_mode step
+(4) param set libc.hook_mode direct
+
+Case 1 represents "startup" mode, and the hooked process is "prog1 ",
+which loads hooking shared library when the program starts up.
+The path is added with two quotation marks because a quotation mark is a special charcter,
+which need be escaped.
+(2) is similar with (1), but no escaped character, so quotation marks doesn't need.
+(3) represents "step" mode, which loads hooking shared library by some steps.
+(4) represetnt "direct" mode, which loads hooking shared library by a step.
+*/
+
+#ifdef HOOK_ENABLE
+#include <unistd.h>
+#include <signal.h>
+#include "musl_malloc_dispatch_table.h"
+#include "musl_malloc.h"
+#include "musl_preinit_common.h"
+#include <pthread.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <dlfcn.h>
+#include <stdatomic.h>
+#include <ctype.h>
+#include <assert.h>
+#include <string.h>
+#include <malloc.h>
+
+void* ohos_malloc_hook_init_function(size_t bytes);
+
+static struct MallocDispatchType __ohos_malloc_hook_init_dispatch = {
+ .malloc = ohos_malloc_hook_init_function,
+ .free = MuslMalloc(free),
+};
+#define MAX_SYM_NAME_SIZE 1000
+static char *__malloc_hook_shared_lib = "libnative_hook.z.so";
+static char *__malloc_hook_function_prefix = "ohos_malloc_hook";
+static char *__get_param_shared_Lib = "libparam_client.z.so";
+volatile atomic_llong ohos_malloc_hook_shared_liibrary;
+void* function_of_shared_lib[LAST_FUNCTION];
+static enum EnumHookMode __hook_mode = STEP_HOOK_MODE;
+static char __hook_process_path[PATH_MAX+ 1] = {0};
+static char __progname[PATH_MAX + 1] = {0};
+
+
+static char* get_native_hook_param()
+{
+ int (*getFunction)(const char *name, char *value, unsigned int *len);
+
+ void* shared_library_handle = dlopen(__get_param_shared_Lib, RTLD_NOW | RTLD_LOCAL);
+ if (!shared_library_handle) {
+ return NULL;
+ }
+
+ getFunction = (int (*)(const char *name, char *value, unsigned int *len))dlsym((void*)shared_library_handle, "SystemGetParameter");
+ if (getFunction == NULL) {
+ dlclose(shared_library_handle);
+ return NULL;
+ }
+ const char *key = MUSL_HOOK_PARAM_NAME;
+ char *value = (char *)malloc(OHOS_PARAM_MAX_SIZE);
+ memset(value, 0, OHOS_PARAM_MAX_SIZE);
+ if (value == NULL) {
+ dlclose(shared_library_handle);
+ return NULL;
+ }
+ unsigned int len = OHOS_PARAM_MAX_SIZE;
+ getFunction(key, value, &len);
+ dlclose(shared_library_handle);
+ return value;
+}
+
+static int parse_hook_variable(enum EnumHookMode* mode, char* path, int size)
+{
+ if (!mode || !path || size <= 0) {
+ return -1;
+ }
+
+ bool flag = __set_hook_flag(false);
+ char* hook_param_value = get_native_hook_param();
+ if (!hook_param_value) {
+ *mode = STEP_HOOK_MODE;
+ path[0] = '\0';
+ } else {
+ char* ptr = hook_param_value;
+ char* mode_str = hook_param_value;
+
+ while (*ptr && *ptr != ':') {
+ ++ptr;
+ }
+
+ if (*ptr == ':') {
+ *ptr = '\0';
+ ++ptr;
+ }
+
+ if (strcmp(mode_str, "startup") == 0) {
+ *mode = STATRUP_HOOK_MODE;
+ } else if (strcmp(mode_str, "direct") == 0) {
+ *mode = DIRECT_HOOK_MODE;
+ } else if (strcmp(mode_str, "step") == 0) {
+ *mode = STEP_HOOK_MODE;
+ } else {
+ *mode = STEP_HOOK_MODE;
+ }
+ if (*mode == STATRUP_HOOK_MODE) {
+ if (*ptr == '\"') {
+ ++ptr;
+ int idx = 0;
+ while (idx < size - 1 && *ptr && *ptr != '\"') {
+ path[idx++] = *ptr++;
+ }
+ path[idx] = '\0';
+ } else {
+ int idx = 0;
+ while (idx < size - 1 && *ptr) {
+ path[idx++] = *ptr++;
+ }
+ path[idx] = '\0';
+ }
+ }
+
+ free(hook_param_value);
+ }
+ __set_hook_flag(flag);
+ return 0;
+}
+
+static bool init_malloc_function(void* malloc_shared_library_handler, MallocMallocType* func, const char* prefix)
+{
+ char symbol[MAX_SYM_NAME_SIZE];
+ snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "malloc");
+ *func = (MallocMallocType)(dlsym(malloc_shared_library_handler, symbol));
+ if (*func == NULL) {
+ return false;
+ }
+ return true;
+}
+
+static bool init_free_function(void* malloc_shared_library_handler, MallocFreeType* func, const char* prefix)
+{
+ char symbol[MAX_SYM_NAME_SIZE];
+ snprintf(symbol, sizeof(symbol), "%s_%s", prefix, "free");
+ *func = (MallocFreeType)(dlsym(malloc_shared_library_handler, symbol));
+ if (*func == NULL) {
+ return false;
+ }
+ return true;
+}
+
+static bool init_hook_functions(void* shared_library_handler, struct MallocDispatchType* table, const char* prefix)
+{
+ if (!init_malloc_function(shared_library_handler, &table->malloc, prefix)) {
+ return false;
+ }
+ if (!init_free_function(shared_library_handler, &table->free, prefix)) {
+ return false;
+ }
+ return true;
+}
+
+static void clear_function_table()
+{
+ for (size_t i = 0; i < LAST_FUNCTION; i++) {
+ function_of_shared_lib[i] = NULL;
+ }
+}
+
+bool init_malloc_hook_shared_library(void* shared_library_handle, const char* shared_lib, const char* prefix, struct MallocDispatchType* dispatch_table)
+{
+ static const char* names[] = {
+ "initialize",
+ "finalize",
+ "get_hook_flag",
+ "set_hook_flag",
+ "on_start",
+ "on_end",
+ };
+
+ for (int i = 0; i < LAST_FUNCTION; i++) {
+ char symbol[MAX_SYM_NAME_SIZE];
+ snprintf(symbol, sizeof(symbol), "%s_%s", prefix, names[i]);
+ function_of_shared_lib[i] = dlsym(shared_library_handle, symbol);
+ if (function_of_shared_lib[i] == NULL) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: %s routine not found in %s\n", getprogname(), symbol, shared_lib);
+ clear_function_table();
+ return false;
+ }
+ }
+
+ if (!init_hook_functions(shared_library_handle, dispatch_table, prefix)) {
+ clear_function_table();
+ return false;
+ }
+
+ return true;
+}
+
+void* load_malloc_hook_shared_library(const char* shared_lib, const char* prefix, struct MallocDispatchType* dispatch_table)
+{
+ void* shared_library_handle = NULL;
+
+ shared_library_handle = dlopen(shared_lib, RTLD_NOW | RTLD_LOCAL);
+
+ if (shared_library_handle == NULL) {
+ printf("Unable to open shared library %s: %s\n", shared_lib, dlerror());
+ return NULL;
+ }
+
+ if (!init_malloc_hook_shared_library(shared_library_handle, shared_lib, prefix, dispatch_table)) {
+ dlclose(shared_library_handle);
+ shared_library_handle = NULL;
+ }
+ // printf("load_malloc_hook_shared_library success new version test\n");
+ return shared_library_handle;
+}
+
+typedef void (*finalize_func_t)();
+typedef bool (*init_func_t)(const struct MallocDispatchType*, bool*, const char*);
+typedef bool (*on_start_func_t)();
+typedef bool (*on_end_func_t)();
+
+static void malloc_finalize()
+{
+ __set_hook_flag(false);
+ ((finalize_func_t)function_of_shared_lib[FINALIZE_FUNCTION])();
+
+ fclose(stdin);
+ fclose(stdout);
+ fclose(stderr);
+}
+
+bool finish_install_ohos_malloc_hooks(struct musl_libc_globals* globals, const char* options, const char* prefix)
+{
+ init_func_t init_func = (init_func_t)(function_of_shared_lib[INITIALIZE_FUNCTION]);
+ if (!init_func(&__libc_malloc_default_dispatch, NULL, options)) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: failed to enable malloc %s\n", getprogname(), prefix);
+ clear_function_table();
+ return false;
+ }
+ on_start_func_t start_func = (on_start_func_t)(function_of_shared_lib[ON_START_FUNCTION]);
+ if (!start_func()) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: failed to start %s\n", getprogname(), prefix);
+ clear_function_table();
+ return false;
+ }
+ 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);
+
+ int ret_value = atexit(malloc_finalize);
+ if (ret_value != 0) {
+ // __musl_log(__MUSL_LOG_ERROR, "failed to set atexit cleanup function: %d\n", ret_value);
+ }
+ return true;
+}
+
+static bool is_empty_string(const char* str)
+{
+ while (*str) {
+ if (!isspace((unsigned char)(*str))) {
+ return false;
+ }
+ }
+ return true;
+}
+
+static void install_ohos_malloc_hook(struct musl_libc_globals* globals)
+{
+ volatile void* shared_library_handle = (volatile void *)atomic_load_explicit(&ohos_malloc_hook_shared_liibrary, memory_order_acquire);
+ assert(shared_library_handle == NULL || shared_library_handle == (volatile void*)-1);
+ shared_library_handle = (volatile void*)load_malloc_hook_shared_library(__malloc_hook_shared_lib, __malloc_hook_function_prefix, &globals->malloc_dispatch_table);
+ if (shared_library_handle == NULL) {
+ // __musl_log(__MUSL_LOG_ERROR, "Can't load shared library '%s'\n", __malloc_hook_shared_lib);
+ return;
+ }
+
+ if (finish_install_ohos_malloc_hooks(globals, NULL, __malloc_hook_function_prefix)) {
+ atomic_store_explicit(&ohos_malloc_hook_shared_liibrary, (volatile long long)shared_library_handle, memory_order_seq_cst);
+ } else {
+ // __musl_log(__MUSL_LOG_ERROR, "finish_install_ohos_malloc_hooks failed\n");
+ dlclose((void *)shared_library_handle);
+ atomic_store_explicit(&ohos_malloc_hook_shared_liibrary, (volatile long long)0, memory_order_seq_cst);
+ }
+}
+
+static void* init_ohos_malloc_hook()
+{
+ bool flag = __set_hook_flag(false);
+ install_ohos_malloc_hook(&__musl_libc_globals);
+ __set_hook_flag(flag);
+ return NULL;
+}
+
+void* ohos_malloc_hook_init_function(size_t bytes)
+{
+ if (atomic_exchange(&__musl_libc_globals.current_dispatch_table, (volatile const long long)NULL)) {
+ pthread_t thread_id;
+ if (pthread_create(&thread_id, NULL, init_ohos_malloc_hook, NULL) != 0) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_create\n", getprogname());
+ } else if (pthread_detach(thread_id) != 0) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: ohos_malloc_hook: failed to pthread_detach\n", getprogname());
+ }
+ }
+ void*ptr = MuslMalloc(malloc)(bytes);
+ return ptr;
+
+}
+
+static void __set_default_malloc()
+{
+ atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile const long long)NULL, memory_order_seq_cst);
+}
+
+static void __install_malloc_hook()
+{
+ atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst);
+
+ volatile void* shared_library_handle = (volatile void* )atomic_load_explicit(&ohos_malloc_hook_shared_liibrary, memory_order_acquire);
+ if (shared_library_handle == NULL) {
+ if (__hook_mode == STEP_HOOK_MODE) {
+ atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile const long long)&__ohos_malloc_hook_init_dispatch, memory_order_seq_cst);
+ atomic_store_explicit(&ohos_malloc_hook_shared_liibrary, (volatile long long)-1, memory_order_seq_cst);
+ } else {
+ init_ohos_malloc_hook();
+ }
+ } else if (shared_library_handle != (void*)-1) {
+ bool flag = __set_hook_flag(false);
+ on_start_func_t start_func = (on_start_func_t)(function_of_shared_lib[ON_START_FUNCTION]);
+ if (start_func && !start_func()) {
+ // __musl_log(__MUSL_LOG_ERROR, "%s: failed to enable malloc\n", getprogname());
+ }
+ __set_hook_flag(flag);
+ volatile const struct MallocDispatchType* so_dispatch_value = (volatile const struct MallocDispatchType* )atomic_load_explicit(&__musl_libc_globals.so_dispatch_table, memory_order_acquire);
+ atomic_store_explicit(&__musl_libc_globals.current_dispatch_table, (volatile long long)so_dispatch_value, memory_order_seq_cst);
+ }
+}
+
+static void __uninstal_malloc_hook()
+{
+ atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
+
+ bool flag = __set_hook_flag(false);
+ __set_default_malloc();
+ on_end_func_t end_func = (on_end_func_t)(function_of_shared_lib[ON_END_FUNCTION]);
+ if (end_func) {
+ end_func();
+ }
+ __set_hook_flag(flag);
+}
+
+static void __install_malloc_hook_signal_handler()
+{
+ struct sigaction actionInstallHook = {};
+ actionInstallHook.sa_handler = __install_malloc_hook;
+ sigemptyset(&actionInstallHook.sa_mask);
+ sigaddset(&actionInstallHook.sa_mask, SIGUNHOOK);
+ sigaction(SIGHOOK, &actionInstallHook, NULL);
+
+ struct sigaction actionDef = {};
+ actionDef.sa_handler = __uninstal_malloc_hook;
+ sigemptyset(&actionDef.sa_mask);
+ sigaddset(&actionDef.sa_mask, SIGHOOK);
+ sigaction(SIGUNHOOK, &actionDef, NULL);
+}
+
+static void __initialize_malloc()
+{
+ __install_malloc_hook_signal_handler();
+}
+
+
+__attribute__((constructor(1))) static void __musl_initialize()
+{
+ atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)false, memory_order_seq_cst);
+ __set_default_malloc();
+ //__init_musl_log();
+ parse_hook_variable(&__hook_mode, __hook_process_path, sizeof(__hook_process_path));
+ if (__hook_mode == STATRUP_HOOK_MODE) {
+ readlink("/proc/self/exe", __progname, sizeof(__progname) - 1);
+ const char *pos = strrchr(__progname, '/');
+ const char* filename;
+ if (pos) {
+ filename = pos + 1;
+ } else {
+ filename = __progname;
+ }
+ if (strcmp(filename, __hook_process_path) == 0) {
+ atomic_store_explicit(&__hook_enable_hook_flag, (volatile bool)true, memory_order_seq_cst);
+ init_ohos_malloc_hook();