forked from GrapheneOS/hardened_malloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
38 lines (32 loc) · 939 Bytes
/
util.c
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __ANDROID__
#include <async_safe/log.h>
#endif
#include "util.h"
static int write_full(int fd, const char *buf, size_t length) {
do {
ssize_t bytes_written = write(fd, buf, length);
if (bytes_written == -1) {
if (errno == EINTR) {
continue;
}
return -1;
}
buf += bytes_written;
length -= bytes_written;
} while (length);
return 0;
}
COLD noreturn void fatal_error(const char *s) {
const char *prefix = "fatal allocator error: ";
(void)(write_full(STDERR_FILENO, prefix, strlen(prefix)) != -1 &&
write_full(STDERR_FILENO, s, strlen(s)) != -1 &&
write_full(STDERR_FILENO, "\n", 1));
#ifdef __ANDROID__
async_safe_format_log(ANDROID_LOG_FATAL, "hardened_malloc", "fatal allocator error: %s", s);
#endif
abort();
}