"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "libhfcommon/util.c" between
honggfuzz-2.1.tar.gz and honggfuzz-2.2.tar.gz

About: honggfuzz is a security oriented, feedback-driven, evolutionary, easy-to-use fuzzer with powerful analysis options.

util.c  (honggfuzz-2.1):util.c  (honggfuzz-2.2)
skipping to change at line 30 skipping to change at line 30
* permissions and limitations under the License. * permissions and limitations under the License.
* *
*/ */
#include "libhfcommon/util.h" #include "libhfcommon/util.h"
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#if !defined(_HF_ARCH_DARWIN) #if !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__)
#include <link.h> #include <link.h>
#endif #endif /* !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__) */
#include <math.h> #include <math.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
skipping to change at line 66 skipping to change at line 66
} }
return p; return p;
} }
void* util_Calloc(size_t sz) { void* util_Calloc(size_t sz) {
void* p = util_Malloc(sz); void* p = util_Malloc(sz);
memset(p, '\0', sz); memset(p, '\0', sz);
return p; return p;
} }
extern void* util_Realloc(void* ptr, size_t sz) { void* util_AllocCopy(const uint8_t* ptr, size_t sz) {
void* p = util_Malloc(sz);
memcpy(p, ptr, sz);
return p;
}
void* util_Realloc(void* ptr, size_t sz) {
void* ret = realloc(ptr, sz); void* ret = realloc(ptr, sz);
if (ret == NULL) { if (ret == NULL) {
PLOG_W("realloc(%p, %zu)", ptr, sz); PLOG_W("realloc(%p, %zu)", ptr, sz);
free(ptr); free(ptr);
return NULL; return NULL;
} }
return ret; return ret;
} }
void* util_MMap(size_t sz) { void* util_MMap(size_t sz) {
skipping to change at line 165 skipping to change at line 171
buf[i] = util_rndPrintable(); buf[i] = util_rndPrintable();
} }
} }
void util_rndBuf(uint8_t* buf, size_t sz) { void util_rndBuf(uint8_t* buf, size_t sz) {
pthread_once(&rndThreadOnce, util_rndInitThread); pthread_once(&rndThreadOnce, util_rndInitThread);
if (sz == 0) { if (sz == 0) {
return; return;
} }
for (size_t i = 0; i < sz; i++) { for (size_t i = 0; i < sz; i++) {
buf[i] = (uint8_t)util_InternalRnd64(); buf[i] = (uint8_t)(util_InternalRnd64() >> 40);
} }
} }
int util_vssnprintf(char* str, size_t size, const char* format, va_list ap) { int util_vssnprintf(char* str, size_t size, const char* format, va_list ap) {
size_t len = strlen(str); size_t len = strlen(str);
if (len >= size) { if (len >= size) {
return len; return len;
} }
size_t left = size - len; size_t left = size - len;
skipping to change at line 240 skipping to change at line 246
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
ret += buf[i]; ret += buf[i];
ret += (ret << 10); ret += (ret << 10);
ret ^= (ret >> 6); ret ^= (ret >> 6);
} }
return ret; return ret;
} }
int64_t util_timeNowMillis(void) { int64_t util_timeNowUSecs(void) {
struct timeval tv; struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) { if (gettimeofday(&tv, NULL) == -1) {
PLOG_F("gettimeofday()"); PLOG_F("gettimeofday()");
} }
return (((int64_t)tv.tv_sec * 1000LL) + ((int64_t)tv.tv_usec / 1000LL)); return (((int64_t)tv.tv_sec * 1000000) + (int64_t)tv.tv_usec);
} }
void util_sleepForMSec(uint64_t msec) { void util_sleepForMSec(uint64_t msec) {
if (msec == 0) { if (msec == 0) {
return; return;
} }
struct timespec ts = { struct timespec ts = {
.tv_sec = msec / 1000U, .tv_sec = msec / 1000U,
.tv_nsec = (msec % 1000U) * 1000000U, .tv_nsec = (msec % 1000U) * 1000000U,
}; };
skipping to change at line 806 skipping to change at line 812
#if defined(SIGRTMIN) && defined(SIGRTMAX) #if defined(SIGRTMIN) && defined(SIGRTMAX)
if (signo >= SIGRTMIN && signo <= SIGRTMAX) { if (signo >= SIGRTMIN && signo <= SIGRTMAX) {
snprintf(signame, sizeof(signame), "SIG%d-RTMIN+%d", signo, signo - SIGR TMIN); snprintf(signame, sizeof(signame), "SIG%d-RTMIN+%d", signo, signo - SIGR TMIN);
return signame; return signame;
} }
#endif /* defined(SIGRTMIN) && defined(SIGRTMAX) */ #endif /* defined(SIGRTMIN) && defined(SIGRTMAX) */
snprintf(signame, sizeof(signame), "UNKNOWN-%d", signo); snprintf(signame, sizeof(signame), "UNKNOWN-%d", signo);
return signame; return signame;
} }
#if !defined(_HF_ARCH_DARWIN) #if !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__)
static int addrRO_cb(struct dl_phdr_info* info, size_t size HF_ATTR_UNUSED, void static int addrStatic_cb(struct dl_phdr_info* info, size_t size HF_ATTR_UNUSED,
* data) { void* data) {
for (size_t i = 0; i < info->dlpi_phnum; i++) { for (size_t i = 0; i < info->dlpi_phnum; i++) {
if (info->dlpi_phdr[i].p_type != PT_LOAD) {
continue;
}
uintptr_t addr_start = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr; uintptr_t addr_start = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr;
uintptr_t addr_end = uintptr_t addr_end =
info->dlpi_addr + info->dlpi_phdr[i].p_vaddr + info->dlpi_phdr[i].p_ memsz; addr_start + HF_MIN(info->dlpi_phdr[i].p_memsz, info->dlpi_phdr[i].p _filesz);
if (((uintptr_t)data >= addr_start) && ((uintptr_t)data < addr_end)) { if (((uintptr_t)data >= addr_start) && ((uintptr_t)data < addr_end)) {
if ((info->dlpi_phdr[i].p_flags & PF_W) == 0) { if ((info->dlpi_phdr[i].p_flags & PF_W) == 0) {
return 1; return LHFC_ADDR_RO;
} else { } else {
return 2; return LHFC_ADDR_RW;
}
}
}
return LHFC_ADDR_NOTFOUND;
}
lhfc_addr_t util_getProgAddr(const void* addr) {
return (lhfc_addr_t)dl_iterate_phdr(addrStatic_cb, (void*)addr);
}
static int check32_cb(struct dl_phdr_info* info, size_t size HF_ATTR_UNUSED, voi
d* data) {
uint32_t v = *(uint32_t*)data;
for (size_t i = 0; i < info->dlpi_phnum; i++) {
/* Look only in the actual binary, and not in libraries */
if (info->dlpi_name[0] != '\0') {
continue;
}
if (info->dlpi_phdr[i].p_type != PT_LOAD) {
continue;
}
if (!(info->dlpi_phdr[i].p_flags & PF_R)) {
continue;
}
uint32_t* start = (uint32_t*)(info->dlpi_addr + info->dlpi_phdr[i].p_vad
dr);
uint32_t* end =
(uint32_t*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr +
HF_MIN(info->dlpi_phdr[i].p_memsz, info->dlpi_phdr[i].p_
filesz));
/* Assume that the 32bit value looked for is also 32bit aligned */
for (; start < end; start++) {
if (*start == v) {
return 1;
}
}
}
return 0;
}
bool util_32bitValInBinary(uint32_t v) {
return (dl_iterate_phdr(check32_cb, &v) == 1);
}
static int check64_cb(struct dl_phdr_info* info, size_t size HF_ATTR_UNUSED, voi
d* data) {
uint64_t v = *(uint64_t*)data;
for (size_t i = 0; i < info->dlpi_phnum; i++) {
/* Look only in the actual binary, and not in libraries */
if (info->dlpi_name[0] != '\0') {
continue;
}
if (info->dlpi_phdr[i].p_type != PT_LOAD) {
continue;
}
if (!(info->dlpi_phdr[i].p_flags & PF_R)) {
continue;
}
uint64_t* start = (uint64_t*)(info->dlpi_addr + info->dlpi_phdr[i].p_vad
dr);
uint64_t* end =
(uint64_t*)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr +
HF_MIN(info->dlpi_phdr[i].p_memsz, info->dlpi_phdr[i].p_
filesz));
/* Assume that the 64bit value looked for is also 64bit aligned */
for (; start < end; start++) {
if (*start == v) {
return 1;
} }
} }
} }
return 0; return 0;
} }
bool util_isAddrRO(const void* addr) { bool util_64bitValInBinary(uint32_t v) {
return (dl_iterate_phdr(addrRO_cb, (void*)addr) == 1); return (dl_iterate_phdr(check64_cb, &v) == 1);
}
#else /* !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__) */
/* Darwin doesn't use ELF file format for binaries, so dl_iterate_phdr() cannot
be used there */
lhfc_addr_t util_getProgAddr(const void* addr HF_ATTR_UNUSED) {
return LHFC_ADDR_NOTFOUND;
}
bool util_32bitValInBinary(uint32_t v HF_ATTR_UNUSED) {
return false;
} }
#else /* !defined(_HF_ARCH_DARWIN) */ bool util_64bitValInBinary(uint32_t v HF_ATTR_UNUSED) {
bool util_isAddrRO(const void* addr) {
return false; return false;
} }
#endif /* !defined(_HF_ARCH_DARWIN) */ #endif /* !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__) */
 End of changes. 14 change blocks. 
16 lines changed or deleted 103 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)