Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BasedOnStyle: LLVM
SortIncludes: Never
IndentWidth: 4
TabWidth: 4
UseTab: Never
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignArrayOfStructures: Right
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
BraceWrapping:
AfterFunction: false
AfterStruct: false
AfterEnum: false
AfterControlStatement: false
BeforeElse: true
BeforeWhile: true
ColumnLimit: 120
6 changes: 6 additions & 0 deletions .github/workflows/make.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: LLVM upgrade
shell: bash
run: sudo bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
- name: Prep
shell: bash
run: sudo apt install libcmocka-dev libarchive-dev
- name: Build
shell: bash
run: make -C native
- name: Build
shell: bash
run: make -C native check-format
- name: Test
shell: bash
run: make -C native test
4 changes: 3 additions & 1 deletion native/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ fuzz: $(ODIR)/fuzzmain
mkdir -p test/fuzz_output test/fuzz_queue
AFL_SKIP_CPUFREQ=1 afl-fuzz -i test/fuzz_input -o test/fuzz_output $(ODIR)/fuzzmain test/fuzz_queue -

check-format: $(DEPS) $(wildcard test/test*.c)
clang-format --dry-run --Werror $^

.PHONY: clean grind coverage fuzz test install
.PHONY: clean grind coverage fuzz test install check-format

test: $(tests_ok)

Expand Down
37 changes: 18 additions & 19 deletions native/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
#ifndef FILE_LIBBUFFER_SEEN
#define FILE_LIBBUFFER_SEEN


#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

// Outputs non-printable characters as octal, which allows the resulting
// string to be a valid C-string constant.
void printbuf(char* c, int n) {
void printbuf(char *c, int n) {
printf("unsigned char* buf=\"");
for (int i = 0; i < n; i++) {
switch (c[i]) {
switch (c[i]) {

case '\n':
printf("\\n");
Expand All @@ -44,14 +43,13 @@ void printbuf(char* c, int n) {
} else {
printf("%c", c[i]);
}
break;
}
break;
}
}
printf("\"\n");
}


char* formatbuf(char* buf, int sz) {
char *formatbuf(char *buf, int sz) {
// nicely format a buffer to hex/ascii with a length offset. each 16 bytes
// or part thereof of the input take 76 bytes include newline character. caller
// must free() the buffer.
Expand All @@ -62,24 +60,25 @@ char* formatbuf(char* buf, int sz) {
// 00000030 69 63 65 90 00 00 28 41 ice···(A
//
// ----8--- -------------------------48--------------------- --------17-------\n
const char* hexd = "0123456789abcdef";
int lines = (sz + (16-1)) / 16; // sz/16 rounded up!
if (lines == 0) lines++;
int osz = lines * 76 + 1; // +trailing null
char* r = malloc(osz);
const char *hexd = "0123456789abcdef";
int lines = (sz + (16 - 1)) / 16; // sz/16 rounded up!
if (lines == 0)
lines++;
int osz = lines * 76 + 1; // +trailing null
char *r = malloc(osz);
for (int i = 0; i < lines; i++) {
// this provides our trailing null!
sprintf(r+i*76, "%08x %48s %17s\n", i*16, "", "");
for (int j = i*16; j < sz && j < (i+1)*16; j++) {
sprintf(r + i * 76, "%08x %48s %17s\n", i * 16, "", "");
for (int j = i * 16; j < sz && j < (i + 1) * 16; j++) {
int c = j % 16;
int e = c > 7 ? 1 : 0;
r[i*76+9 +c*3+e] = hexd[(buf[j] >> 4) & 0x0F]; // upper nibble
r[i*76+10+c*3+e] = hexd[buf[j] & 0x0F]; // lower nibble
r[i * 76 + 9 + c * 3 + e] = hexd[(buf[j] >> 4) & 0x0F]; // upper nibble
r[i * 76 + 10 + c * 3 + e] = hexd[buf[j] & 0x0F]; // lower nibble
// printable ascii?
if (buf[j] >= 32 && buf[j] < 127) {
r[i*76+9+49+c+e] = buf[j];
r[i * 76 + 9 + 49 + c + e] = buf[j];
} else {
r[i*76+9+49+c+e] = '.';
r[i * 76 + 9 + 49 + c + e] = '.';
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions native/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef FILE_LIBBUFFER_SEEN
#define FILE_LIBBUFFER_SEEN

void printbuf(char* c, int n);
char* formatbuf(char* buf, int sz);
void printbuf(char *c, int n);
char *formatbuf(char *buf, int sz);

#endif
99 changes: 53 additions & 46 deletions native/fuzzmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.


#define _GNU_SOURCE

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#define KXVER 3
#include "k.h"
#include "libchronicle.h"
#include "mock_k.h"
#include "serdes_k.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include "k.h"
#include "mock_k.h"
#include "serdes_k.h"
#include "libchronicle.h"

// This is a stand-alone tool for replaying a queue for use with fuzzer
int print_data = 0;
int print_meta = 0;

int print_msg(void* ctx, uint64_t index, void* msg) {
int print_msg(void *ctx, uint64_t index, void *msg) {
K x = (K)msg;
if (print_data) {
printf("[%" PRIu64 "] KC ", index);
Expand All @@ -53,7 +52,9 @@ uint32_t xorshift128(uint32_t state[static 4]) {
uint32_t s, t = state[3];
t ^= t << 11;
t ^= t >> 8;
state[3] = state[2]; state[2] = state[1]; state[1] = s = state[0];
state[3] = state[2];
state[2] = state[1];
state[1] = s = state[0];
t ^= s;
t ^= s >> 19;
state[0] = t;
Expand All @@ -68,13 +69,13 @@ uint32_t xorshift128(uint32_t state[static 4]) {
// Reported but unresolved https://www.mail-archive.com/[email protected]/msg209613.html
static inline uint64_t rdtsc(void) {
uint32_t a, d;
__asm__ __volatile__("rdtscp" : "=a" (a), "=d" (d));
return (((uint64_t) d << 32) | a);
__asm__ __volatile__("rdtscp" : "=a"(a), "=d"(d));
return (((uint64_t)d << 32) | a);
}
int c_mkstemp(char* pattern) {
int c_mkstemp(char *pattern) {
uint32_t xor_state[4];
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)rdtsc();
char* rep = strstr(pattern, "XXXXXX");
char *rep = strstr(pattern, "XXXXXX");
if (rep) {
for (int i = 0; i < 6; i++) {
uint8_t incre = xorshift128(xor_state) & 0x1F;
Expand All @@ -93,11 +94,11 @@ int c_mkstemp(char* pattern) {
int main(const int argc, char **argv) {
int c;
opterr = 0;
int verboseflag = 0;
FILE * fuzzfid = NULL;
int verboseflag = 0;
FILE *fuzzfid = NULL;

while ((c = getopt(argc, argv, "dmv:")) != -1)
switch (c) {
switch (c) {
case 'd':
print_data = 1;
break;
Expand All @@ -113,7 +114,7 @@ int main(const int argc, char **argv) {
default:
fprintf(stderr, "%c??", c);
exit(3);
}
}

if (optind + 2 > argc) {
printf("Missing mandatory argument.\n Expected: %s [-d] [-m] [-v] QUEUE FUZZFILE\n", argv[0]);
Expand All @@ -128,68 +129,70 @@ int main(const int argc, char **argv) {
printf(" the second `bytes` appends that many random bytes as a new entry to the queue\n");
printf(" As the script is played, the random number generator seed, recieved index and byte count\n");
printf(" are written to a log. This is then re-opened to verify the data in the queue matches.\n");

exit(1);
}

// mandatory arguments
char* dir = argv[optind];
char* fuzz = argv[optind+1];
char *dir = argv[optind];
char *fuzz = argv[optind + 1];

fuzzfid = stdin;
if (strcmp(fuzz, "-") == 0) {
fprintf(stderr, "fuzzing from stdin\n");
} else {
fuzzfid = fopen(fuzz, "r");
if (fuzzfid == NULL) {
if (fuzzfid == NULL) {
fprintf(stderr, "Unable to open %s\n", fuzz);
exit(4);
}
}

queue_t* queue = chronicle_init(dir);
queue_t *queue = chronicle_init(dir);
chronicle_set_decoder(queue, &parse_kx, &free_kx);
chronicle_set_encoder(queue, &sizeof_kx, &append_kx);
chronicle_set_version(queue, 5);
chronicle_set_roll_scheme(queue, "FAST_HOURLY");
chronicle_set_create(queue, 1);
if (chronicle_open(queue) != 0) exit(-1);

tailer_t* tailer = chronicle_tailer(queue, print_msg, NULL, 0);
if (chronicle_open(queue) != 0)
exit(-1);

tailer_t *tailer = chronicle_tailer(queue, print_msg, NULL, 0);
chronicle_peek_queue(queue);

if (verboseflag) {
chronicle_debug();
}

size_t linecap = 0;
ssize_t linelen = 0;
char *msgp = NULL;
char *parsep = NULL;

int line = 0;
size_t linecap = 0;
ssize_t linelen = 0;
char *msgp = NULL;
char *parsep = NULL;

int line = 0;
long long bytes;
long long time;
uint64_t clock = 0;
uint64_t index = 0;
uint64_t clock = 0;
uint64_t index = 0;

uint32_t xor_state[4];
uint32_t xor_state[4];

char* tmpfile = strdup("/tmp/shmmain.XXXXXX");
int tmpfid = c_mkstemp(tmpfile);
char *tmpfile = strdup("/tmp/shmmain.XXXXXX");
int tmpfid = c_mkstemp(tmpfile);
printf("logging fuzz expectations to %s fid %d msg %s\n", tmpfile, tmpfid, strerror(errno));
FILE* tmp = fdopen(tmpfid, "w+");
FILE *tmp = fdopen(tmpfid, "w+");
printf(" tmp is %p %s\n", tmp, strerror(errno));
while ((linelen = getline(&msgp, &linecap, fuzzfid)) > 0) {
line++;
parsep = msgp;
time = strtoll(parsep, &parsep, 0);
if (*parsep == ' ') parsep++;
if (*parsep == ' ')
parsep++;
bytes = strtoll(parsep, &parsep, 0);

printf(" FUZ: %lld millis, %lld bytes\n", time, bytes);
clock += time;
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)line+1;
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)line + 1;
K x = ktn(KC, bytes);
for (long long b = 0; b < bytes; b++) {
kG(x)[b] = (uint8_t)xorshift128(xor_state);
Expand All @@ -199,11 +202,14 @@ int main(const int argc, char **argv) {
r0(x);
}
// we're done parsing input, now replay checking using temp file
//fclose(tmp);
//close(tmpfid);
// fclose(tmp);
// close(tmpfid);

fflush(tmp);
if (fseek(tmp, 0L, SEEK_SET) != 0) { printf("abort not fseek"); abort(); };
if (fseek(tmp, 0L, SEEK_SET) != 0) {
printf("abort not fseek");
abort();
};

int rline = 0;
int r;
Expand All @@ -216,7 +222,7 @@ int main(const int argc, char **argv) {

// TODO: verify!!
} else if (rline != line) {
printf ("error, %s r=%d at line %d!\n\n", tmpfile, r, rline);
printf("error, %s r=%d at line %d!\n\n", tmpfile, r, rline);
}
rline++;
} while (r != EOF);
Expand All @@ -225,7 +231,8 @@ int main(const int argc, char **argv) {

// unlink(tmpfile);
free(tmpfile);
if (msgp) free(msgp);
if (msgp)
free(msgp);
printf("parse finished\n");

exit(rline == line ? 0 : 5); // exit with fail
Expand Down
Loading
Loading