| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#define _GNU_SOURCE |
| 17 |
#define BUILDING_LIBC_WRAPPERS |
| 18 |
|
| 19 |
#include "libc_wrappers.h" |
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
#include <signal.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <sys/syscall.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <syscall.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#include "gotcha_auxv.h" |
| 32 |
|
| 33 |
typedef struct malloc_header_t { |
| 34 |
size_t size; |
| 35 |
} malloc_header_t; |
| 36 |
|
| 37 |
typedef struct malloc_link_t { |
| 38 |
malloc_header_t header; |
| 39 |
struct malloc_link_t *next; |
| 40 |
} malloc_link_t; |
| 41 |
|
| 42 |
#define MIN_SIZE (sizeof(malloc_link_t) - sizeof(malloc_header_t)) |
| 43 |
#define MIN_BLOCK_SIZE (1024 * 32) |
| 44 |
|
| 45 |
static malloc_link_t *free_list = NULL; |
| 46 |
|
| 47 |
static void split_allocation(malloc_link_t *allocation, size_t new_size) { |
| 48 |
size_t orig_size = allocation->header.size; |
| 49 |
malloc_link_t *newalloc; |
| 50 |
|
| 51 |
if (orig_size - new_size <= sizeof(malloc_link_t)) return; |
| 52 |
|
| 53 |
allocation->header.size = new_size; |
| 54 |
newalloc = (malloc_link_t *)(((unsigned char *)&allocation->next) + new_size); |
| 55 |
newalloc->header.size = (orig_size - new_size) - sizeof(malloc_header_t); |
| 56 |
newalloc->next = free_list; |
| 57 |
free_list = newalloc; |
| 58 |
} |
| 59 |
|
| 60 |
void *gotcha_malloc(size_t size) { |
| 61 |
malloc_link_t *cur, *prev, *newalloc; |
| 62 |
malloc_link_t *best_fit = NULL, *best_fit_prev; |
| 63 |
ssize_t best_fit_diff = SIZE_MAX, diff, block_size; |
| 64 |
void *result; |
| 65 |
|
| 66 |
if (size < MIN_SIZE) size = MIN_SIZE; |
| 67 |
if (size % 8) size += 8 - (size % 8); |
| 68 |
|
| 69 |
|
| 70 |
for (prev = NULL, cur = free_list; cur; cur = cur->next) { |
| 71 |
diff = cur->header.size - size; |
| 72 |
if (diff >= 0 && (!best_fit || diff < best_fit_diff)) { |
| 73 |
best_fit = cur; |
| 74 |
best_fit_prev = prev; |
| 75 |
best_fit_diff = diff; |
| 76 |
if (!diff) break; |
| 77 |
} |
| 78 |
prev = cur; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (best_fit) { |
| 83 |
if (best_fit_prev) |
| 84 |
best_fit_prev->next = best_fit->next; |
| 85 |
else |
| 86 |
free_list = best_fit->next; |
| 87 |
split_allocation(best_fit, size); |
| 88 |
return (void *)&best_fit->next; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (size + sizeof(malloc_header_t) > MIN_BLOCK_SIZE) { |
| 93 |
block_size = size + sizeof(malloc_header_t); |
| 94 |
diff = block_size % gotcha_getpagesize(); |
| 95 |
if (diff) block_size += gotcha_getpagesize() - diff; |
| 96 |
} else { |
| 97 |
block_size = MIN_BLOCK_SIZE; |
| 98 |
} |
| 99 |
|
| 100 |
result = gotcha_mmap(NULL, block_size, PROT_READ | PROT_WRITE, |
| 101 |
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 102 |
if (result == MAP_FAILED) return NULL; |
| 103 |
newalloc = (malloc_link_t *)result; |
| 104 |
newalloc->header.size = block_size - sizeof(malloc_header_t); |
| 105 |
split_allocation(newalloc, size); |
| 106 |
return (void *)&newalloc->next; |
| 107 |
} |
| 108 |
|
| 109 |
void *gotcha_realloc(void *buffer, size_t size) { |
| 110 |
void *newbuffer; |
| 111 |
malloc_link_t *alloc; |
| 112 |
|
| 113 |
alloc = (malloc_link_t *)(((malloc_header_t *)buffer) - 1); |
| 114 |
|
| 115 |
if (size <= alloc->header.size) return buffer; |
| 116 |
|
| 117 |
newbuffer = gotcha_malloc(size); |
| 118 |
if (!newbuffer) return NULL; |
| 119 |
gotcha_memcpy(newbuffer, buffer, alloc->header.size); |
| 120 |
|
| 121 |
gotcha_free(buffer); |
| 122 |
|
| 123 |
return newbuffer; |
| 124 |
} |
| 125 |
|
| 126 |
void gotcha_free(void *buffer) { |
| 127 |
malloc_link_t *alloc; |
| 128 |
alloc = (malloc_link_t *)(((malloc_header_t *)buffer) - 1); |
| 129 |
|
| 130 |
alloc->next = free_list; |
| 131 |
free_list = alloc; |
| 132 |
} |
| 133 |
|
| 134 |
void gotcha_memcpy(void *dest, void *src, size_t size) { |
| 135 |
size_t i; |
| 136 |
for (i = 0; i < size; i++) { |
| 137 |
((unsigned char *)dest)[i] = ((unsigned char *)src)[i]; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
int gotcha_strncmp(const char *in_one, const char *in_two, int max_length) { |
| 142 |
int i = 0; |
| 143 |
for (; i < max_length; i++) { |
| 144 |
if (in_one[i] == '\0') { |
| 145 |
return (in_two[i] == '\0') ? 0 : 1; |
| 146 |
} |
| 147 |
if (in_one[i] != in_two[i]) { |
| 148 |
return in_one[i] - in_two[i]; |
| 149 |
} |
| 150 |
} |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int gotcha_strcmp(const char *in_one, const char *in_two) { |
| 155 |
int i = 0; |
| 156 |
for (;; i++) { |
| 157 |
if (in_one[i] == '\0') { |
| 158 |
return (in_two[i] == '\0') ? 0 : 1; |
| 159 |
} |
| 160 |
if (in_one[i] != in_two[i]) { |
| 161 |
return in_one[i] - in_two[i]; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
char *gotcha_strstr(const char *searchIn, const char *searchFor) { |
| 167 |
int i, j; |
| 168 |
if (!searchFor[0]) return NULL; |
| 169 |
|
| 170 |
for (i = 0; searchIn[i]; i++) { |
| 171 |
if (searchIn[i] != searchFor[0]) continue; |
| 172 |
for (j = 1;; j++) { |
| 173 |
if (!searchFor[j]) return (char *)(searchFor + i); |
| 174 |
if (!searchIn[i + j]) return NULL; |
| 175 |
if (searchFor[j] != searchIn[i + j]) break; |
| 176 |
} |
| 177 |
} |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
ssize_t gotcha_write(int fd, const void *buf, size_t count) { |
| 182 |
return syscall(SYS_write, fd, buf, count); |
| 183 |
} |
| 184 |
|
| 185 |
size_t gotcha_strlen(const char *s) { |
| 186 |
size_t i; |
| 187 |
for (i = 0; s[i]; i++) |
| 188 |
; |
| 189 |
return i; |
| 190 |
} |
| 191 |
|
| 192 |
size_t gotcha_strnlen(const char *s, size_t max_length) { |
| 193 |
size_t i; |
| 194 |
for (i = 0; s[i] && i < max_length; i++) |
| 195 |
; |
| 196 |
return i; |
| 197 |
} |
| 198 |
|
| 199 |
static int ulong_to_hexstr(unsigned long num, char *str, int strlen, |
| 200 |
int uppercase) { |
| 201 |
int len, i; |
| 202 |
unsigned long val; |
| 203 |
char base_char = uppercase ? 'A' : 'a'; |
| 204 |
|
| 205 |
if (num == 0) { |
| 206 |
if (strlen < 2) return -1; |
| 207 |
str[0] = '0'; |
| 208 |
str[1] = '\0'; |
| 209 |
return 1; |
| 210 |
} |
| 211 |
|
| 212 |
for (len = 0, val = num; val; val = val / 16, len++) |
| 213 |
; |
| 214 |
if (len + 1 >= strlen) return -1; |
| 215 |
|
| 216 |
str[len] = '\0'; |
| 217 |
val = num; |
| 218 |
for (i = 1; i <= len; i++) { |
| 219 |
str[len - i] = |
| 220 |
(val % 16 <= 9) ? ('0' + val % 16) : (base_char + (val % 16 - 10)); |
| 221 |
val = val / 16; |
| 222 |
} |
| 223 |
return len; |
| 224 |
} |
| 225 |
|
| 226 |
static int ulong_to_str(unsigned long num, char *str, int strlen) { |
| 227 |
int len, i; |
| 228 |
unsigned long val; |
| 229 |
|
| 230 |
if (num == 0) { |
| 231 |
if (strlen < 2) return -1; |
| 232 |
str[0] = '0'; |
| 233 |
str[1] = '\0'; |
| 234 |
return 1; |
| 235 |
} |
| 236 |
|
| 237 |
for (len = 0, val = num; val; val = val / 10, len++) |
| 238 |
; |
| 239 |
if (len + 1 >= strlen) return -1; |
| 240 |
|
| 241 |
str[len] = '\0'; |
| 242 |
val = num; |
| 243 |
for (i = 1; i <= len; i++) { |
| 244 |
str[len - i] = '0' + val % 10; |
| 245 |
val = val / 10; |
| 246 |
} |
| 247 |
return len; |
| 248 |
} |
| 249 |
|
| 250 |
static int slong_to_str(signed long num, char *str, int strlen) { |
| 251 |
int result; |
| 252 |
if (num >= 0) return ulong_to_str((unsigned long)num, str, strlen); |
| 253 |
|
| 254 |
result = ulong_to_str((unsigned long)(num * -1), str + 1, strlen - 1); |
| 255 |
if (result == -1) return -1; |
| 256 |
str[0] = '-'; |
| 257 |
return result + 1; |
| 258 |
} |
| 259 |
|
| 260 |
void gotcha_assert_fail(const char *s, const char *file, unsigned int line, |
| 261 |
const char *function) { |
| 262 |
char linestr[64]; |
| 263 |
int result; |
| 264 |
|
| 265 |
result = ulong_to_str(line, linestr, sizeof(linestr) - 1); |
| 266 |
if (result == -1) linestr[0] = '\0'; |
| 267 |
|
| 268 |
gotcha_write(2, file, gotcha_strlen(file)); |
| 269 |
gotcha_write(2, ":", 1); |
| 270 |
gotcha_write(2, linestr, gotcha_strlen(linestr)); |
| 271 |
gotcha_write(2, ": ", 2); |
| 272 |
gotcha_write(2, function, gotcha_strlen(function)); |
| 273 |
gotcha_write(2, ": Assertion `", 13); |
| 274 |
gotcha_write(2, s, gotcha_strlen(s)); |
| 275 |
gotcha_write(2, "' failed.\n", 10); |
| 276 |
syscall(SYS_kill, gotcha_getpid(), SIGABRT); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
extern char **__environ; |
| 281 |
char *gotcha_getenv(const char *name) { |
| 282 |
char **s; |
| 283 |
int name_len; |
| 284 |
|
| 285 |
name_len = gotcha_strlen(name); |
| 286 |
for (s = __environ; *s; s++) { |
| 287 |
if (gotcha_strncmp(name, *s, name_len) != 0) continue; |
| 288 |
if ((*s)[name_len] != '=') continue; |
| 289 |
return (*s) + name_len + 1; |
| 290 |
} |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
|
| 294 |
pid_t gotcha_getpid() { return syscall(SYS_getpid); } |
| 295 |
|
| 296 |
pid_t gotcha_gettid() { return syscall(SYS_gettid); } |
| 297 |
|
| 298 |
unsigned int gotcha_getpagesize() { |
| 299 |
static unsigned int pagesz = 0; |
| 300 |
if (pagesz) return pagesz; |
| 301 |
|
| 302 |
pagesz = get_auxv_pagesize(); |
| 303 |
if (!pagesz) pagesz = 4096; |
| 304 |
return pagesz; |
| 305 |
} |
| 306 |
|
| 307 |
int gotcha_open(const char *pathname, int flags, ...) { |
| 308 |
mode_t mode; |
| 309 |
va_list args; |
| 310 |
long result; |
| 311 |
|
| 312 |
va_start(args, flags); |
| 313 |
if (flags & O_CREAT) { |
| 314 |
mode = va_arg(args, mode_t); |
| 315 |
} else { |
| 316 |
mode = 0; |
| 317 |
} |
| 318 |
va_end(args); |
| 319 |
|
| 320 |
#if defined(SYS_open) |
| 321 |
result = syscall(SYS_open, pathname, flags, mode); |
| 322 |
#else |
| 323 |
result = syscall(SYS_openat, AT_FDCWD, pathname, flags, mode); |
| 324 |
#endif |
| 325 |
|
| 326 |
if (result >= 0) return (int)result; |
| 327 |
|
| 328 |
return -1; |
| 329 |
} |
| 330 |
|
| 331 |
void *gotcha_mmap(void *addr, size_t length, int prot, int flags, int fd, |
| 332 |
off_t offset) { |
| 333 |
long result; |
| 334 |
|
| 335 |
result = syscall(SYS_mmap, addr, length, prot, flags, fd, offset); |
| 336 |
return (void *)result; |
| 337 |
} |
| 338 |
|
| 339 |
int gotcha_atoi(const char *nptr) { |
| 340 |
int neg = 1, len, val = 0, mult = 1; |
| 341 |
const char *cur; |
| 342 |
|
| 343 |
while (*nptr == '-') { |
| 344 |
neg = neg * -1; |
| 345 |
nptr++; |
| 346 |
} |
| 347 |
|
| 348 |
for (len = 0; nptr[len] >= '0' && nptr[len] <= '9'; len++) |
| 349 |
; |
| 350 |
|
| 351 |
for (cur = nptr + len - 1; cur != nptr - 1; cur--) { |
| 352 |
val += mult * (*cur - '0'); |
| 353 |
mult *= 10; |
| 354 |
} |
| 355 |
|
| 356 |
return val * neg; |
| 357 |
} |
| 358 |
|
| 359 |
int gotcha_close(int fd) { return syscall(SYS_close, fd); } |
| 360 |
|
| 361 |
int gotcha_mprotect(void *addr, size_t len, int prot) { |
| 362 |
return syscall(SYS_mprotect, addr, len, prot); |
| 363 |
} |
| 364 |
|
| 365 |
ssize_t gotcha_read(int fd, void *buf, size_t count) { |
| 366 |
return syscall(SYS_read, fd, buf, count); |
| 367 |
} |
| 368 |
|
| 369 |
static const char *add_to_buffer(const char *str, int fd, int *pos, |
| 370 |
char *buffer, int buffer_size, |
| 371 |
int *num_printed, int print_percent) { |
| 372 |
for (; *str && (print_percent || *str != '%'); str++) { |
| 373 |
if (*pos >= buffer_size) { |
| 374 |
gotcha_write(fd, buffer, buffer_size); |
| 375 |
*num_printed += buffer_size; |
| 376 |
*pos = 0; |
| 377 |
} |
| 378 |
else { |
| 379 |
buffer[*pos] = *str; |
| 380 |
} |
| 381 |
*pos = *pos + 1; |
| 382 |
} |
| 383 |
return str; |
| 384 |
} |
| 385 |
|
| 386 |
int gotcha_int_printf(int fd, const char *format, ...) { |
| 387 |
#define inc(S) \ |
| 388 |
do { \ |
| 389 |
S++; \ |
| 390 |
if (*(S) == '\0') goto done; \ |
| 391 |
} while (0) |
| 392 |
va_list args; |
| 393 |
const char *str = format; |
| 394 |
int buffer_pos = 0; |
| 395 |
int char_width, short_width, long_width, long_long_width, size_width; |
| 396 |
int num_printed = 0; |
| 397 |
char buffer[4096] = {'\0'}; |
| 398 |
|
| 399 |
va_start(args, format); |
| 400 |
while (*str) { |
| 401 |
str = add_to_buffer(str, fd, &buffer_pos, buffer, sizeof(buffer), |
| 402 |
&num_printed, 0); |
| 403 |
if (!*str) break; |
| 404 |
|
| 405 |
gotcha_assert(*str == '%'); |
| 406 |
inc(str); |
| 407 |
|
| 408 |
char_width = short_width = long_width = long_long_width = size_width = 0; |
| 409 |
if (*str == 'h' && *(str + 1) == 'h') { |
| 410 |
char_width = 1; |
| 411 |
inc(str); |
| 412 |
inc(str); |
| 413 |
} else if (*str == 'h') { |
| 414 |
short_width = 1; |
| 415 |
inc(str); |
| 416 |
} else if (*str == 'l' && *(str + 1) == 'l') { |
| 417 |
long_long_width = 1; |
| 418 |
inc(str); |
| 419 |
inc(str); |
| 420 |
} else if (*str == 'l') { |
| 421 |
long_width = 1; |
| 422 |
inc(str); |
| 423 |
} else if (*str == 'z') { |
| 424 |
size_width = 1; |
| 425 |
inc(str); |
| 426 |
} |
| 427 |
|
| 428 |
if (*str == 'd' || *str == 'i') { |
| 429 |
signed long val = 0; |
| 430 |
char numstr[64] = {'\0'}; |
| 431 |
if (char_width) |
| 432 |
val = (signed long)(signed char)va_arg(args, signed int); |
| 433 |
else if (short_width) |
| 434 |
val = (signed long)(signed short)va_arg(args, signed int); |
| 435 |
else if (long_width) |
| 436 |
val = (signed long)va_arg(args, signed long); |
| 437 |
else if (long_long_width) |
| 438 |
val = (signed long)va_arg(args, signed long long); |
| 439 |
else if (size_width) |
| 440 |
val = (signed long)va_arg(args, ssize_t); |
| 441 |
else |
| 442 |
val = (signed long)va_arg(args, signed int); |
| 443 |
slong_to_str(val, numstr, 64); |
| 444 |
add_to_buffer(numstr, fd, &buffer_pos, buffer, sizeof(buffer), |
| 445 |
&num_printed, 1); |
| 446 |
} else if (*str == 'u') { |
| 447 |
unsigned long val = 0; |
| 448 |
char numstr[64] = {'\0'}; |
| 449 |
if (char_width) |
| 450 |
val = (unsigned long)(unsigned char)va_arg(args, unsigned int); |
| 451 |
else if (short_width) |
| 452 |
val = (unsigned long)(unsigned short)va_arg(args, unsigned int); |
| 453 |
else if (long_width) |
| 454 |
val = (unsigned long)va_arg(args, unsigned long); |
| 455 |
else if (long_long_width) |
| 456 |
val = (unsigned long)va_arg(args, unsigned long long); |
| 457 |
else if (size_width) |
| 458 |
val = (unsigned long)va_arg(args, ssize_t); |
| 459 |
else |
| 460 |
val = (unsigned long)va_arg(args, unsigned int); |
| 461 |
ulong_to_str(val, numstr, 64); |
| 462 |
add_to_buffer(numstr, fd, &buffer_pos, buffer, sizeof(buffer), |
| 463 |
&num_printed, 1); |
| 464 |
} else if (*str == 'x' || *str == 'X' || *str == 'p') { |
| 465 |
unsigned long val = 0; |
| 466 |
char numstr[64] = {'\0'}; |
| 467 |
if (*str != 'p') { |
| 468 |
if (char_width) |
| 469 |
val = (unsigned long)(unsigned char)va_arg(args, unsigned int); |
| 470 |
else if (short_width) |
| 471 |
val = (unsigned long)(unsigned short)va_arg(args, unsigned int); |
| 472 |
else if (long_width) |
| 473 |
val = (unsigned long)va_arg(args, unsigned long); |
| 474 |
else if (long_long_width) |
| 475 |
val = (unsigned long)va_arg(args, unsigned long long); |
| 476 |
else if (size_width) |
| 477 |
val = (unsigned long)va_arg(args, ssize_t); |
| 478 |
else |
| 479 |
val = (unsigned long)va_arg(args, unsigned int); |
| 480 |
} else { |
| 481 |
val = (unsigned long)va_arg(args, void *); |
| 482 |
add_to_buffer("0x", fd, &buffer_pos, buffer, sizeof(buffer), |
| 483 |
&num_printed, 1); |
| 484 |
} |
| 485 |
ulong_to_hexstr(val, numstr, 64, (*str == 'X')); |
| 486 |
add_to_buffer(numstr, fd, &buffer_pos, buffer, sizeof(buffer), |
| 487 |
&num_printed, 1); |
| 488 |
} else if (*str == 'c') { |
| 489 |
char cbuf[2] = {'\0'}; |
| 490 |
cbuf[0] = (unsigned char)va_arg(args, unsigned int); |
| 491 |
cbuf[1] = '\0'; |
| 492 |
add_to_buffer(cbuf, fd, &buffer_pos, buffer, sizeof(buffer), &num_printed, |
| 493 |
1); |
| 494 |
} else if (*str == 's') { |
| 495 |
char *s = (char *)va_arg(args, char *); |
| 496 |
add_to_buffer(s, fd, &buffer_pos, buffer, sizeof(buffer), &num_printed, |
| 497 |
1); |
| 498 |
} else if (*str == '%') { |
| 499 |
add_to_buffer("%", fd, &buffer_pos, buffer, sizeof(buffer), &num_printed, |
| 500 |
1); |
| 501 |
} else { |
| 502 |
char s[3] = {'\0'}; |
| 503 |
s[0] = '%'; |
| 504 |
s[1] = *str; |
| 505 |
s[2] = '\0'; |
| 506 |
add_to_buffer(s, fd, &buffer_pos, buffer, sizeof(buffer), &num_printed, |
| 507 |
1); |
| 508 |
} |
| 509 |
inc(str); |
| 510 |
} |
| 511 |
|
| 512 |
done: |
| 513 |
gotcha_write(fd, buffer, buffer_pos); |
| 514 |
num_printed += buffer_pos; |
| 515 |
va_end(args); |
| 516 |
return num_printed; |
| 517 |
} |
| 518 |
|
| 519 |
void *gotcha_memset(void *s, int c, size_t n) { |
| 520 |
size_t i; |
| 521 |
unsigned char byte = (unsigned char)c; |
| 522 |
for (i = 0; i < n; i++) { |
| 523 |
((unsigned char *)s)[i] = byte; |
| 524 |
} |
| 525 |
return s; |
| 526 |
} |
| 527 |
|
| 528 |
char *gotcha_strncat(char *dest, const char *src, size_t n) { |
| 529 |
char *dest_begin = dest; |
| 530 |
dest = dest + gotcha_strlen(dest); |
| 531 |
size_t dest_stop = gotcha_strnlen(src, n); |
| 532 |
dest[dest_stop] = '\0'; |
| 533 |
gotcha_memcpy(dest, (void *)src, n); |
| 534 |
return dest_begin; |
| 535 |
} |