-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinterflop_valgrind_stdlib.c
More file actions
216 lines (180 loc) · 7.07 KB
/
interflop_valgrind_stdlib.c
File metadata and controls
216 lines (180 loc) · 7.07 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
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
#include "interflop_valgrind_stdlib.h"
#include "pub_tool_libcassert.h"
#include "pub_tool_libcbase.h"
#include "pub_tool_libcfile.h"
#include "pub_tool_libcprint.h"
#include "pub_tool_libcproc.h"
#include "pub_tool_mallocfree.h"
#include "pub_tool_clreq.h"
#define VGFILE_BUFSIZE 8192
struct _VgFile {
HChar buf[VGFILE_BUFSIZE];
UInt num_chars; // number of characters in buf
Int fd; // file descriptor to write to
};
VgFile *_interflop_stderr(void) {
VgFile *fp = (VgFile *)VG_(malloc)("fdopen", sizeof(struct _VgFile));
fp->fd = 2;
fp->num_chars = 0;
return fp;
}
void *_interflop_valgrind_malloc(SizeT size) { return VG_(malloc)("", size); }
VgFile *_interflop_valgrind_fopen(const String pathname, const String mode,
Int *error) {
/* This function does not check for rw|rw+ case */
/* Neither other flags x,F,e and c that are extensions */
// ┌─────────────┬───────────────────────────────┐
// │fopen() mode │ open() flags │
// ├─────────────┼───────────────────────────────┤
// │ r │ O_RDONLY │
// ├─────────────┼───────────────────────────────┤
// │ w │ O_WRONLY | O_CREAT | O_TRUNC │
// ├─────────────┼───────────────────────────────┤
// │ a │ O_WRONLY | O_CREAT | O_APPEND │
// ├─────────────┼───────────────────────────────┤
// │ r+ │ O_RDWR │
// ├─────────────┼───────────────────────────────┤
// │ w+ │ O_RDWR | O_CREAT | O_TRUNC │
// ├─────────────┼───────────────────────────────┤
// │ a+ │ O_RDWR | O_CREAT | O_APPEND │
// └─────────────┴───────────────────────────────┘
Int flags = 0;
if (VG_(strcmp)(mode, "r")) {
flags = VKI_O_RDONLY;
} else if (VG_(strcmp)(mode, "r+")) {
flags = VKI_O_RDWR;
} else if (VG_(strcmp)(mode, "w")) {
flags = VKI_O_WRONLY | VKI_O_CREAT | VKI_O_TRUNC;
} else if (VG_(strcmp)(mode, "w+")) {
flags = VKI_O_RDWR | VKI_O_CREAT | VKI_O_TRUNC;
} else if (VG_(strcmp)(mode, "a")) {
flags = VKI_O_WRONLY | VKI_O_CREAT | VKI_O_APPEND;
} else if (VG_(strcmp(mode, "a+"))) {
flags = VKI_O_RDWR | VKI_O_CREAT | VKI_O_APPEND;
}
// flags = mode linux
// mode = permission
Int permissions = VKI_S_IRUSR | VKI_S_IWUSR | VKI_S_IRGRP | VKI_S_IROTH;
return VG_(fopen)(pathname, flags, permissions);
}
Int _interflop_valgrind_strcmp(const String s1, const String s2) {
return VG_(strcmp)(s1, s2);
}
Int _interflop_valgrind_strcasecmp(const String s1, const String s2) {
return VG_(strcasecmp)(s1, s2);
}
/* Do not follow libc API, use error pointer to pass errno result instead */
/* error = 0 if success */
Long _interflop_valgrind_strtol(const String nptr, String *endptr, Int *error) {
// VG_(strtoll10) returns 0 if no number could be converted,
// and 'endptr' is set to the start of the string.
*error = 0;
long val = VG_(strtoll10)(nptr, endptr);
if (val == 0 && *endptr == nptr) {
*error = 1;
}
return val;
}
/* Do not follow libc API, use error pointer to pass errno result instead */
/* error = 0 if success */
double _interflop_valgrind_strtod(const String nptr, String *endptr,
Int *error) {
// VG_(strtod) returns 0 if no number could be converted,
// and 'endptr' is set to the start of the string.
*error = 0;
double val = VG_(strtod)(nptr, endptr);
if (val == 0 && *endptr == nptr) {
*error = 1;
}
return val;
}
String _interflop_valgrind_getenv(const String name) {
return VG_(getenv)(name);
}
static void __fflush(VgFile *stream) {
VG_(write)(stream->fd, stream->buf, stream->num_chars);
stream->num_chars = 0;
}
Int _interflop_valgrind_fprintf(VgFile *stream, const String format, ...) {
va_list ap;
va_start(ap, format);
UInt res = VG_(vfprintf)(stream, format, ap);
__fflush(stream);
va_end(ap);
return res;
}
String _interflop_valgrind_strcpy(String dest, const String src) {
return VG_(strcpy)(dest, src);
}
String _interflop_valgrind_strncpy(String dest, const String src, SizeT size) {
return VG_(strncpy)(dest, src, size);
}
/* VG_(fclose) does not return error code */
Int _interflop_valgrind_fclose(VgFile *stream) {
VG_(fclose)(stream);
return 0;
}
Int _interflop_valgrind_gettid(void) { return VG_(gettid)(); }
const String _interflop_valgrind_strerror(Int error) {
return "internal error\n";
}
Int _interflop_valgrind_sprintf(String str, const String format, ...) {
va_list ap;
va_start(ap, format);
UInt res = VG_(sprintf)(str, format, ap);
va_end(ap);
return res;
}
/* Valgrind does not have vwarnx equivalent, so just use fprintf */
void _interflop_valgrind_vwarnx(const String fmt, va_list args) {
VG_(vprintf)(fmt, args);
}
Int _interflop_valgrind_vfprintf(VgFile *stream, const String format,
va_list ap) {
Int res = VG_(vfprintf)(stream, format, ap);
__fflush(stream);
return res;
}
void _interflop_valgrind_exit(Int status) { VG_(exit)(status); }
String _interflop_valgrind_strtok_r(String str, const String delim,
String *saveptr) {
return VG_(strtok_r)(str, delim, saveptr);
}
/* Valgrind does not have function to read file so we have to reimplement it */
String _interflop_valgrind_fgets(String s, Int size, VgFile *stream) {
// fgets() reads in at most one less than size characters from stream and
// stores them into the buffer pointed to by s. Reading stops after an EOF or
// a newline. If a newline is read, it is stored into the buffer. A
// terminating null byte ('\0') is stored after the last character in the
// buffer.
if (size <= 0) {
return NULL;
}
if (size == 1) {
s[0] = '\0';
return s;
}
Int fd = stream->fd;
Int _read = 0;
Int _nb_read = 0;
HChar c;
_read = VG_(read)(fd, (void *)&c, 1);
while (_read != 0 && c != '\n' && _nb_read < size - 2) {
s[_nb_read] = c;
_nb_read++;
_read = VG_(read)(fd, (void *)&c, 1);
}
s[_nb_read] = c;
s[_nb_read + 1] = '\0';
if (_read == 0) {
return NULL;
}
return s;
}
void _interflop_valgrind_free(void *ptr) { VG_(free)(ptr); }
void *_interflop_valgrind_calloc(SizeT nmemb, SizeT size) {
return VG_(calloc)("", nmemb, size);
}
Int _interflop_valgrind_gettimeofday(timeval_t *tv, timezone_t *tz) {
return VG_(gettimeofday)(tv, tz);
}