-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathtest_fuzz_server.c
66 lines (55 loc) · 1.61 KB
/
test_fuzz_server.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "iso14229.h"
#ifdef __cplusplus
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
#else
int LLVMFuzzerTestOneInput(const uint8_t *, size_t);
#endif
typedef struct {
UDSSDU_t sdu_info;
ssize_t msg_len;
int srv_retval;
uint8_t msg[UDS_TP_MTU];
} StuffToFuzz_t;
static StuffToFuzz_t fuzz_buf;
static uint8_t client_recv_buf[UDS_TP_MTU];
static int fn(UDSServer_t *srv, UDSEvent_t ev, const void *arg) {
printf("Whoah, got event %d\n", ev);
return fuzz_buf.srv_retval;
}
static uint32_t g_ms = 0;
uint32_t UDSMillis() { return g_ms; }
static UDSServer_t srv;
static UDSTp_t *mock_client = NULL;
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
static bool initialized = false;
if (!initialized) {
UDSServerInit(&srv);
srv.tp = ISOTPMockNew("server", ISOTPMock_DEFAULT_SERVER_ARGS);
mock_client = ISOTPMockNew("client", ISOTPMock_DEFAULT_CLIENT_ARGS);
initialized = true;
}
if (size < sizeof(fuzz_buf)) {
return -1;
}
memset(&fuzz_buf, 0, sizeof(fuzz_buf));
memmove(&fuzz_buf, data, size);
if (fuzz_buf.msg_len > UDS_TP_MTU || fuzz_buf.msg_len < 0) {
return -1;
}
// ISOTPMockLogToStdout();
UDSTpSend(mock_client, fuzz_buf.msg, fuzz_buf.msg_len, &fuzz_buf.sdu_info);
for (int i = 0; i < 1000; i++) {
UDSServerPoll(&srv);
if(UDSTpGetRecvLen(mock_client)) {
UDSTpAckRecv(mock_client);
}
g_ms++;
}
return 0;
}