1 /* Copyright 2021 Google LLC
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5 http://www.apache.org/licenses/LICENSE-2.0
6 Unless required by applicable law or agreed to in writing, software
7 distributed under the License is distributed on an "AS IS" BASIS,
8 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9 See the License for the specific language governing permissions and
10 limitations under the License.
11 */
12
13 #ifndef FUZZ_H
14 #define FUZZ_H
15
16 #include <sys/types.h>
17 #include <sys/socket.h>
18
19 // Forward declared because we want to use FuzzedDataProvider,
20 // which requires CPP.
21 extern ssize_t fuzz_get_random_data(void *buf, size_t len);
22
fuzz_recv(int sockfd,void * buf,size_t len,int flags)23 ssize_t fuzz_recv(int sockfd, void *buf, size_t len, int flags){
24 return fuzz_get_random_data(buf, len);
25 }
26
fuzz_read(int sockfd,void * buf,size_t len)27 ssize_t fuzz_read(int sockfd, void *buf, size_t len){
28 return fuzz_get_random_data(buf, len);
29 }
30
fuzz_write(int fd,const void * buf,size_t count)31 ssize_t fuzz_write(int fd, const void *buf, size_t count) {
32 return count;
33 }
34
fuzz_isatty(int fd)35 int fuzz_isatty(int fd) {
36 return 1;
37 }
38
fuzz_fgets(char * s,int size,FILE * stream)39 char *fuzz_fgets(char *s, int size, FILE *stream) {
40 ssize_t v = fuzz_get_random_data(s, size-1);
41 // We use fgets to get trusted input. As such, assume we have
42 // an ascii printable char at the beginning.
43 printf("Calling into fgets\n");
44 if (s[0] <= 0x21 || s[0] >= 0x7f) {
45 s[0] = 'A';
46 }
47 s[size-1] = '\0';
48 return s;
49 }
50
fuzz_select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)51 int fuzz_select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout) {
52 char val;
53 ssize_t c = fuzz_get_random_data(&val, 1);
54 return c;
55 }
56
fuzz_send(int sockfd,const void * buf,size_t len,int flags)57 ssize_t fuzz_send(int sockfd, const void *buf, size_t len, int flags) {
58 return len;
59 }
60
61 FILE *fp_p = NULL;
fuzz_fopen(const char * pathname,const char * mode)62 FILE *fuzz_fopen(const char *pathname, const char *mode) {
63 if (mode == NULL) return fp_p;
64 return fp_p;
65 }
66
fuzz_fclose(FILE * stream)67 int fuzz_fclose(FILE *stream) {
68 if (stream == NULL) return 1;
69 return 2;
70 }
71
fuzz_sendto(int sockfd,void * buf,size_t len,int flags,struct sockaddr * dest_addr,socklen_t addrlen)72 size_t fuzz_sendto(int sockfd, void *buf, size_t len, int flags, struct sockaddr *dest_addr, socklen_t addrlen) {
73 if (buf == NULL) {
74 return len;
75 }
76 return len;
77 }
78
79 #endif
80