Repository navigation
Expand file tree
/
Copy pathinfrastructure.c
More file actions
124 lines (110 loc) · 3.32 KB
/
Copy pathinfrastructure.c
File metadata and controls
124 lines (110 loc) · 3.32 KB
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
/*
* Basic implementations of common functions.
*
* Copyright (c) Antmicro
* Copyright (c) Realtime Embedded
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#ifdef __GLIBC__
#include <execinfo.h>
#endif
#include "callbacks.h"
#include "infrastructure.h"
void *global_retaddr = 0;
// clang-format off
#if defined(__linux__) && defined(__x86_64__) && defined(__GLIBC__)
// Wrappers downgrading GLIBC v2.34 functions.
#include <pthread.h>
asm(".symver pthread_mutexattr_destroy, pthread_mutexattr_destroy@GLIBC_2.2.5");
int __wrap_pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
{
return pthread_mutexattr_destroy(attr);
}
asm(".symver pthread_mutexattr_init, pthread_mutexattr_init@GLIBC_2.2.5");
int __wrap_pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
return pthread_mutexattr_init(attr);
}
asm(".symver pthread_mutexattr_setrobust, pthread_mutexattr_setrobust@GLIBC_2.12");
int __wrap_pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robustness)
{
return pthread_mutexattr_setrobust(attr, robustness);
}
// Wrappers downgrading GLIBC v2.29 functions.
#if defined(TARGET_I386)
#include <math.h>
asm(".symver pow, pow@GLIBC_2.2.5");
double __wrap_pow(double x, double y)
{
return pow(x, y);
}
asm(".symver log, log@GLIBC_2.2.5");
double __wrap_log(double x)
{
return log(x);
}
#endif
#endif
// clang-format on
void *tlib_mallocz(size_t size)
{
void *ret = tlib_malloc(size);
memset(ret, 0, size);
return ret;
}
char *tlib_strdup(const char *str)
{
int length = strlen(str);
char *ret = tlib_malloc(length + 1);
strcpy(ret, str);
return ret;
}
void tlib_printf(enum log_level level, char *fmt, ...)
{
char s[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(s, 1024, fmt, ap);
tlib_log((int32_t)level, s);
va_end(ap);
}
void tlib_abortf(char *fmt, ...)
{
char result[1024];
va_list ap;
va_start(ap, fmt);
vsnprintf(result, 1024, fmt, ap);
#if DEBUG && defined(__GLIBC__)
#define TRACE_MAX_SIZE 20
void *array[TRACE_MAX_SIZE];
char **strings;
int size, i;
size = backtrace(array, TRACE_MAX_SIZE);
strings = backtrace_symbols(array, size);
if(strings != NULL) {
tlib_printf(LOG_LEVEL_ERROR, "Stack: [%d frames]", size);
// The last frame is meaningless, and the first one is the tlib_abort itself
for(i = 1; i < size - 1; i++) {
tlib_printf(LOG_LEVEL_ERROR, "%s\n", strings[i]);
}
}
free(strings);
#endif
tlib_abort(result);
va_end(ap);
}