robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 1 | /* |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 2 | * |
robert.swiecki@gmail.com | 97c7733 | 2015-02-14 23:06:58 +0000 | [diff] [blame^] | 3 | * honggfuzz - utilities |
| 4 | * ----------------------------------------- |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * Author: Robert Swiecki <swiecki@google.com> |
| 7 | * |
| 8 | * Copyright 2010-2015 by Google Inc. All Rights Reserved. |
| 9 | * |
| 10 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 11 | * not use this file except in compliance with the License. You may obtain |
| 12 | * a copy of the License at |
| 13 | * |
| 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | * |
| 16 | * Unless required by applicable law or agreed to in writing, software |
| 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 19 | * implied. See the License for the specific language governing |
| 20 | * permissions and limitations under the License. |
| 21 | * |
| 22 | */ |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 23 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 25 | #include <math.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 26 | #include <stdarg.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 27 | #include <stdint.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 30 | #include <string.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/time.h> |
| 33 | #include <sys/types.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 34 | #include <time.h> |
robert.swiecki@gmail.com | ba85c3e | 2015-02-02 14:55:16 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 36 | |
| 37 | #include "common.h" |
| 38 | #include "log.h" |
| 39 | |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 40 | static int util_urandomFD = -1; |
| 41 | |
robert.swiecki@gmail.com | 4be12dc | 2015-02-13 14:03:53 +0000 | [diff] [blame] | 42 | uint32_t util_rndGet(uint32_t min, uint32_t max) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 43 | { |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 44 | if (util_urandomFD == -1) { |
| 45 | if ((util_urandomFD = open("/dev/urandom", O_RDONLY)) == -1) { |
| 46 | LOGMSG_P(l_FATAL, "Couldn't open /dev/urandom"); |
| 47 | } |
| 48 | } |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 49 | |
robert.swiecki@gmail.com | b038337 | 2015-02-13 14:17:27 +0000 | [diff] [blame] | 50 | unsigned short seed16v[3]; |
| 51 | |
| 52 | if (read(util_urandomFD, seed16v, sizeof(seed16v)) != sizeof(seed16v)) { |
robert.swiecki@gmail.com | 4be12dc | 2015-02-13 14:03:53 +0000 | [diff] [blame] | 53 | struct timeval tv; |
| 54 | gettimeofday(&tv, NULL); |
robert.swiecki@gmail.com | b038337 | 2015-02-13 14:17:27 +0000 | [diff] [blame] | 55 | seed16v[0] = ((unsigned short)tv.tv_usec); |
| 56 | gettimeofday(&tv, NULL); |
| 57 | seed16v[1] = ((unsigned short)tv.tv_usec); |
| 58 | gettimeofday(&tv, NULL); |
| 59 | seed16v[2] = ((unsigned short)tv.tv_usec); |
robert.swiecki@gmail.com | 4be12dc | 2015-02-13 14:03:53 +0000 | [diff] [blame] | 60 | } |
robert.swiecki@gmail.com | 180263f | 2015-02-13 13:57:15 +0000 | [diff] [blame] | 61 | |
robert.swiecki@gmail.com | b038337 | 2015-02-13 14:17:27 +0000 | [diff] [blame] | 62 | seed48(seed16v); |
| 63 | uint32_t rnd1 = (uint32_t) lrand48(); |
| 64 | uint32_t rnd2 = (uint32_t) lrand48(); |
| 65 | uint32_t rnd = (rnd1 << 16) ^ rnd2; |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 66 | |
robert.swiecki@gmail.com | bce825a | 2015-02-13 23:26:07 +0000 | [diff] [blame] | 67 | if (min > max) { |
| 68 | LOGMSG(l_FATAL, "min:%d > max:%d", min, max); |
| 69 | } |
| 70 | |
robert.swiecki@gmail.com | b038337 | 2015-02-13 14:17:27 +0000 | [diff] [blame] | 71 | return ((rnd % (max - min + 1)) + min); |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
robert.swiecki@gmail.com | e7190b9 | 2015-02-14 23:05:42 +0000 | [diff] [blame] | 74 | void util_ssnprintf(char *str, size_t size, const char *format, ...) |
| 75 | { |
| 76 | char buf1[size]; |
| 77 | char buf2[size]; |
| 78 | |
| 79 | strncpy(buf1, str, size); |
| 80 | |
| 81 | va_list args; |
| 82 | va_start(args, format); |
| 83 | vsnprintf(buf2, size, format, args); |
| 84 | va_end(args); |
| 85 | |
| 86 | snprintf(str, size, "%s%s", buf1, buf2); |
| 87 | } |
| 88 | |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 89 | void util_getLocalTime(const char *fmt, char *buf, size_t len) |
| 90 | { |
| 91 | struct tm ltime; |
| 92 | |
| 93 | time_t t = time(NULL); |
| 94 | |
| 95 | localtime_r(&t, <ime); |
| 96 | strftime(buf, len, fmt, <ime); |
| 97 | } |
| 98 | |
robert.swiecki | 40499ff | 2010-12-13 19:47:08 +0000 | [diff] [blame] | 99 | void util_nullifyStdio(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 100 | { |
| 101 | int fd = open("/dev/null", O_RDWR); |
| 102 | |
| 103 | if (fd == -1) { |
| 104 | LOGMSG_P(l_ERROR, "Couldn't open '/dev/null'"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | dup2(fd, 0); |
| 109 | dup2(fd, 1); |
| 110 | dup2(fd, 2); |
| 111 | |
| 112 | if (fd > 2) { |
| 113 | close(fd); |
| 114 | } |
| 115 | |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | bool util_redirectStdin(char *inputFile) |
| 120 | { |
| 121 | int fd = open(inputFile, O_RDONLY); |
| 122 | |
| 123 | if (fd == -1) { |
| 124 | LOGMSG_P(l_ERROR, "Couldn't open '%s'", inputFile); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | dup2(fd, 0); |
| 129 | if (fd != 0) { |
| 130 | close(fd); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
robert.swiecki | 40499ff | 2010-12-13 19:47:08 +0000 | [diff] [blame] | 136 | void util_recoverStdio(void) |
robert.swiecki | 3bb518c | 2010-10-14 00:48:24 +0000 | [diff] [blame] | 137 | { |
| 138 | int fd = open("/dev/tty", O_RDWR); |
| 139 | |
| 140 | if (fd == -1) { |
| 141 | LOGMSG_P(l_ERROR, "Couldn't open '/dev/tty'"); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | dup2(fd, 0); |
| 146 | dup2(fd, 1); |
| 147 | dup2(fd, 2); |
| 148 | |
| 149 | if (fd > 2) { |
| 150 | close(fd); |
| 151 | } |
| 152 | |
| 153 | return; |
| 154 | } |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 155 | |
robert.swiecki@gmail.com | 772b33d | 2015-02-14 20:35:00 +0000 | [diff] [blame] | 156 | /* |
| 157 | * This is not a cryptographically secure hash |
| 158 | */ |
groebert@google.com | 1bd4c21 | 2013-06-19 11:13:56 +0000 | [diff] [blame] | 159 | extern uint64_t util_hash(const char *buf, size_t len) |
| 160 | { |
| 161 | uint64_t ret = 0; |
| 162 | |
| 163 | for (size_t i = 0; i < len; i++) { |
| 164 | ret += buf[i]; |
| 165 | ret += (ret << 10); |
| 166 | ret ^= (ret >> 6); |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |