Coverage Report

Created: 2026-04-09 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/SockFuzzer/fuzz/fakes/fake_impls.c
Line
Count
Source
1
/*
2
 * Copyright 2021 Google LLC
3
 *
4
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5
 *
6
 * This file contains Original Code and/or Modifications of Original Code
7
 * as defined in and that are subject to the Apple Public Source License
8
 * Version 2.0 (the 'License'). You may not use this file except in
9
 * compliance with the License. The rights granted to you under the License
10
 * may not be used to create, or enable the creation or redistribution of,
11
 * unlawful or unlicensed copies of an Apple operating system, or to
12
 * circumvent, violate, or enable the circumvention or violation of, any
13
 * terms of an Apple operating system software license agreement.
14
 *
15
 * Please obtain a copy of the License at
16
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17
 *
18
 * The Original Code and all software distributed under the License are
19
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23
 * Please see the License for the specific language governing rights and
24
 * limitations under the License.
25
 *
26
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27
 */
28
29
// Trivial implementations belong here. More substantial faked
30
// subsystems should live in their own file.
31
32
#include <stdbool.h>
33
#include <stdint.h>
34
#include <string.h>
35
36
#include <kern/assert.h>
37
#include <libkern/libkern.h>
38
39
#include "bsd/net/nwk_wq.h"
40
#include "bsd/sys/_types/_timeval.h"
41
#include "bsd/sys/conf.h"
42
#include "bsd/sys/kdebug_kernel.h"
43
#include "bsd/sys/kernel_types.h"
44
#include "bsd/sys/malloc.h"
45
#include "bsd/sys/resource.h"
46
#include "bsd/uuid/uuid.h"
47
48
extern void get_fuzzed_bytes(void* addr, size_t bytes);
49
extern bool get_fuzzed_bool(void);
50
51
int snprintf(char*, size_t, const char*, ...) __printflike(3, 4);
52
53
int maxfilesperproc = 10;
54
55
36
bool PE_parse_boot_argn(const char* arg_string, void* arg_ptr, int max_arg) {
56
36
  if (!strcmp(arg_string, "ifa_debug")) {
57
9
    *(int*)arg_ptr = 0;
58
9
    return true;
59
9
  }
60
61
27
  if (!strcmp(arg_string, "inaddr_nhash")) {
62
1
    *(uint32_t*)arg_ptr = 0;
63
1
    return true;
64
1
  }
65
66
26
  if (!strcmp(arg_string, "mcache_flags")) {
67
0
    *(uint32_t*)arg_ptr = 0;
68
0
    return true;
69
0
  }
70
71
26
  if (!strcmp(arg_string, "mbuf_debug")) {
72
1
    *(uint32_t*)arg_ptr = 0;
73
1
    return true;
74
1
  }
75
76
25
  if (!strcmp(arg_string, "mleak_sample_factor")) {
77
1
    *(uint32_t*)arg_ptr = 0;
78
1
    return true;
79
1
  }
80
81
  // Just return 0 by default.
82
24
  memset(arg_ptr, 0, max_arg);
83
84
  // assert(false);
85
24
  return false;
86
25
}
87
88
4
void* os_log_create() { return (void*)1; }
89
90
0
void pflog_packet() {}
91
92
// TODO(nedwill): return a real vfs context
93
1.26M
void* vfs_context_current() { return NULL; }
94
95
0
int csproc_get_platform_binary(void* p) { return 0; }
96
97
2.13M
void uuid_clear(uuid_t uu) { memset(uu, 0, sizeof(uuid_t)); }
98
99
2.03M
int uuid_is_null(const uuid_t uu) {
100
2.03M
  return !memcmp(uu, UUID_NULL, sizeof(uuid_t));
101
2.03M
}
102
103
25
int uuid_compare(const uuid_t uu1, const uuid_t uu2) {
104
25
  return memcmp(uu1, uu2, sizeof(uuid_t));
105
25
}
106
107
// TODO(nedwill): this shouldn't return the same value
108
// within the same fuzz session (use a counter and reset)
109
0
void uuid_generate_random(uuid_t out) {
110
0
  if (get_fuzzed_bool()) {
111
0
    memcpy(out, "0000000000000000", 16);
112
0
    return;
113
0
  }
114
0
  if (get_fuzzed_bool()) {
115
0
    memcpy(out, "1111111111111111", 16);
116
0
    return;
117
0
  }
118
0
  memcpy(out, "2222222222222222", 16);
119
0
}
120
121
4
void uuid_copy(uuid_t dst, const uuid_t src) {
122
4
  memcpy(dst, src, sizeof(uuid_t));
123
4
}
124
125
0
void uuid_unparse_upper(const uuid_t uu, uuid_string_t out) {
126
0
  snprintf(out, sizeof(uuid_string_t),
127
0
           "%c%c%c%c-"
128
0
           "%c%c-"
129
0
           "%c%c-"
130
0
           "%c%c-"
131
0
           "%c%c%c%c%c%c",
132
0
           uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], uu[8], uu[9],
133
0
           uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
134
0
}
135
136
0
void uuid_unparse(const uuid_t uu, uuid_string_t out) {
137
0
  uuid_unparse_upper(uu, out);
138
0
}
139
140
extern void* kernproc;
141
142
1.42M
void* vfs_context_proc() { return kernproc; }
143
144
// TODO(Ned): better timekeeping here
145
970k
uint64_t mach_continuous_time(void) { return 0; }
146
147
// TODO: handle timer scheduling
148
0
void timeout() { assert(false); }
149
150
371k
void microtime(struct timeval* tvp) { memset(&tvp, 0, sizeof(tvp)); }
151
152
3.70M
void microuptime(struct timeval* tvp) { memset(&tvp, 0, sizeof(tvp)); }
153
154
0
int mac_socket_check_accepted() { return 0; }
155
156
20.6k
int mac_socket_check_setsockopt() { return 0; }
157
158
2.94k
int mac_socket_check_bind() { return 0; }
159
160
622k
int mac_file_check_ioctl() { return 0; }
161
162
0
int deflateInit2_() { return 1; }
163
0
int inflateInit2_() { return 1; }
164
165
0
bool kauth_cred_issuser() { return true; }
166
167
181k
unsigned long RandomULong() {
168
  // returning 0 here would be a failure
169
181k
  return 1;
170
181k
}
171
172
// TODO: threading
173
10
int kernel_thread_start() { return 0; }
174
175
1
int cdevsw_add(int major, const struct cdevsw *cdevsw) {
176
1
  return 0;
177
1
}
178
179
2
void devfs_make_node() {}
180
181
1.38M
bool lck_mtx_try_lock() { return true; }
182
183
2
void kprintf() { return; }
184
185
3
void thread_deallocate() {}
186
187
// we are root
188
57.0k
int proc_suser() { return 0; }
189
190
270k
void _os_log_internal() {}
191
192
0
void hw_atomic_add() {}
193
194
0
void hw_atomic_sub() {}
195
196
1.30M
void lck_mtx_destroy() {}
197
198
183k
int mac_socket_check_ioctl() { return 0; }
199
200
659k
bool proc_is64bit() { return true; }
201
202
580
int priv_check_cred() { return 0; }
203
204
393k
bool lck_rw_try_lock_exclusive() { return true; }
205
206
void* malloc(size_t size);
207
void free(void* ptr);
208
209
// TODO(nedwill): fix this hack
210
__attribute__((visibility("default"))) bool real_copyout = true;
211
212
115k
int copyout(const void* kaddr, user_addr_t udaddr, size_t len) {
213
  // randomly fail
214
115k
  if (get_fuzzed_bool()) {
215
6.27k
    return 1;
216
6.27k
  }
217
218
109k
  if (!udaddr || udaddr == 1 || !real_copyout) {
219
15.8k
    void* buf = malloc(len);
220
15.8k
    memcpy(buf, kaddr, len);
221
15.8k
    free(buf);
222
15.8k
    return 0;
223
15.8k
  }
224
225
93.8k
  memcpy((void*)udaddr, kaddr, len);
226
93.8k
  return 0;
227
109k
}
228
229
998k
void* __MALLOC(size_t size, int type, int flags, vm_allocation_site_t* site) {
230
998k
  void* addr = NULL;
231
998k
  assert(type < M_LAST);
232
233
998k
  if (size == 0) {
234
0
    return NULL;
235
0
  }
236
237
998k
  addr = malloc(size);
238
998k
  if (!addr) {
239
0
    return NULL;
240
0
  }
241
242
998k
  if (flags & M_ZERO) {
243
759k
    bzero(addr, size);
244
759k
  }
245
246
998k
  return (addr);
247
998k
}
248
249
757k
void read_frandom(void* buffer, unsigned int numBytes) {
250
757k
  get_fuzzed_bytes(buffer, numBytes);
251
757k
}
252
253
0
void read_random(void* buffer, unsigned int numBytes) {
254
0
  get_fuzzed_bytes(buffer, numBytes);
255
0
}
256
257
0
int ml_get_max_cpus(void) { return 1; }
258
259
void clock_interval_to_deadline(uint32_t interval, uint32_t scale_factor,
260
2.37M
                                uint64_t* result) {
261
2.37M
  *result = 0;
262
2.37M
}
263
264
void clock_interval_to_absolutetime_interval(uint32_t interval,
265
                                             uint32_t scale_factor,
266
288k
                                             uint64_t* result) {
267
288k
  *result = 0;
268
288k
}
269
270
7
void* thread_call_allocate_with_options() { return (void*)1; }
271
272
288k
bool thread_call_enter_delayed_with_leeway() { return true; }
273
274
1.61M
void lck_rw_assert() {}
275
276
1
uint32_t IOMapperIOVMAlloc() { return 0; }
277
278
2.13M
int proc_uniqueid() { return 0; }
279
280
0
uint64_t mach_absolute_time() { return 0; }
281
282
3.17M
int proc_pid() { return 0; }
283
284
void proc_getexecutableuuid(void* p, unsigned char* uuidbuf,
285
2.13M
                            unsigned long size) {
286
2.13M
  memset(uuidbuf, 0, size);
287
2.13M
}
288
289
2.13M
void proc_pidoriginatoruuid(void* buffer, size_t size) {
290
2.13M
  memset(buffer, 0, size);
291
2.13M
}
292
293
2.13M
void* kauth_cred_proc_ref() { return (void*)1; }
294
295
3.01M
void* kauth_cred_get() { return (void*)1; }
296
297
0
void* proc_ucred() { return (void*)1; }
298
299
2.13M
int suser(void* arg1, void* arg2) {
300
2.13M
  (void)arg1;
301
2.13M
  (void)arg2;
302
2.13M
  return 0;
303
2.13M
}
304
305
3.88M
void lck_rw_lock_shared() {}
306
307
5.84M
void lck_rw_done() {}
308
309
2.05M
bool proc_get_effective_thread_policy() {
310
  // TODO: more options
311
2.05M
  return false;
312
2.05M
}
313
314
5.78M
void* current_proc() { return kernproc; }
315
316
69.1k
int proc_selfpid() { return 1; }
317
318
0
void tvtohz() {}
319
320
0
int kauth_cred_getuid() {
321
  // UUID: root
322
0
  return get_fuzzed_bool() ? 1 : 0;
323
0
}
324
325
996k
char* proc_best_name() { return "kernproc"; }
326
327
16
void* proc_find() { return kernproc; }
328
329
69.1k
int mac_socket_check_create() { return 0; }
330
331
53.6k
int mac_socket_check_accept() { return 0; }
332
333
277
void ovbcopy(const char* from, char* to, size_t nbytes) {
334
277
  memmove(to, from, nbytes);
335
277
}
336
337
int __attribute__((warn_unused_result))
338
1.22M
copyin(const user_addr_t uaddr, void *kaddr, size_t len) {
339
  // Address 1 means use fuzzed bytes, otherwise use real bytes.
340
  // NOTE: this does not support nested useraddr.
341
1.22M
  if (uaddr != 1) {
342
681k
    memcpy(kaddr, (void*)uaddr, len);
343
681k
    return 0;
344
681k
  }
345
346
547k
  if (get_fuzzed_bool()) {
347
32.3k
    return -1;
348
32.3k
  }
349
350
515k
  get_fuzzed_bytes(kaddr, len);
351
515k
  return 0;
352
547k
}
353
354
3
void SHA1Final() {}
355
356
3
void SHA1Init() {}
357
358
5
void SHA1Update() {}
359
360
2
void* thread_call_allocate_with_priority() { return (void*)1; }
361
362
9
void lck_grp_attr_free() {}
363
364
2
void lck_grp_free() {}
365
366
3.61M
void lck_rw_lock_exclusive() {}
367
368
1.19M
void timevaladd() {}
369
0
void timevalsub() {}
370
371
23.8k
void thread_call_enter_delayed() {}
372
373
0
void MD5Init() {}
374
0
void MD5Update() {}
375
0
void MD5Final(unsigned char* digest, void* ctx) {
376
0
  memset(digest, 0, 4);
377
0
}
378
379
16
void proc_rele() {}
380
2.79M
void wakeup(void* chan) {}
381
382
0
void lck_spin_lock() {}
383
0
void lck_spin_unlock() {}
384
2.05M
void kauth_cred_unref(void* cred) {}
385
2.05M
void lck_rw_unlock_exclusive() {}
386
387
479k
bool IS_64BIT_PROCESS() { return true; }
388
389
21.9k
int mac_socket_check_listen() { return 0; }
390
391
0
void kauth_cred_ref() {}
392
393
0
void in_stat_set_activity_bitmap() {}
394
395
60.5k
int mac_socket_check_getsockopt() { return 0; }
396
397
439k
int mac_pipe_check_ioctl() { return 0; }
398
399
0
int mac_pipe_check_write() { return 0; }
400
401
0
int mac_pipe_check_kqfilter() { return 0; }
402
403
2
int mac_pipe_label_init() { return 0; }
404
405
0
int mac_pipe_label_destroy() { return 0; }
406
407
0
int mac_pipe_check_read() { return 0; }
408
409
0
int mac_pipe_check_stat() { return 0; }
410
411
2
int mac_pipe_label_associate() { return 0; }
412
413
0
int kauth_getuid() { return 0; }
414
415
0
int kauth_getgid() { return 0; }
416
417
0
int mac_pipe_check_select() { return 0; }
418
419
0
void _aio_close() {}
420
0
void unlink1() {}
421
422
16.4k
int mac_socket_check_connect() { return 0; }
423
424
0
void ml_thread_policy() {}
425
426
1
void aes_encrypt_key128() {}
427
428
6
void OSBacktrace() {}
429
430
0
void lck_grp_attr_setdefault() {}
431
432
8.32k
void nanouptime() {}
433
434
8.32k
void wakeup_one() {}
435
436
762k
int lck_mtx_try_lock_spin() { return 1; }
437
438
358k
void absolutetime_to_nanoseconds(uint64_t in, uint64_t* out) { *out = 0; }
439
440
void free(void* ptr);
441
442
5.31k
void nwk_wq_enqueue(struct nwk_wq_entry* nwk_item) {
443
5.31k
  nwk_item->func(nwk_item->arg);
444
5.31k
  free(nwk_item);
445
5.31k
}
446
447
35.9k
int ppsratecheck() { return 1; }
448
449
16
bool ratecheck() { return true; }
450
451
0
void fulong() {}
452
0
void ubc_cs_blob_deallocate() {}
453
0
void proc_thread() {}
454
0
void munge_user32_stat64() {}
455
0
int mac_file_check_lock() { return 0; }
456
0
void vnode_setsize() {}
457
0
void vnode_setnocache() {}
458
0
void kauth_authorize_fileop() {}
459
0
void VNOP_FSYNC() {}
460
0
void tablefull() {}
461
0
void vnode_recycle() {}
462
0
void ipc_object_copyin() {}
463
0
int mac_file_check_inherit() { return 0; }
464
0
void vnode_vid() {}
465
0
void munge_user32_stat() {}
466
0
void VNOP_OFFTOBLK() {}
467
52
int mac_file_check_create() { return 0; }
468
0
void fileport_port_to_fileglob() {}
469
0
void VNOP_SETATTR() {}
470
0
void vfs_devblocksize() {}
471
0
int mac_file_check_library_validation() { return 0; }
472
0
void ubc_cs_blob_add() {}
473
0
void vn_getpath() {}
474
0
void ipc_port_release_send() {}
475
0
void proc_kqhashlock_grp() {}
476
0
void vn_path_package_check() {}
477
0
void VNOP_GETATTR() {}
478
0
void ubc_cs_blob_allocate() {}
479
0
void audit_sysclose() {}
480
0
void vnode_is_openevt() {}
481
0
void audit_arg_vnpath_withref() {}
482
0
int mac_file_check_fcntl() { return 0; }
483
0
void VNOP_ALLOCATE() {}
484
0
void fg_vn_data_free() {}
485
0
void VNOP_BLKTOOFF() {}
486
0
void vnode_islnk() {}
487
0
void VNOP_IOCTL() {}
488
0
int mac_vnode_check_truncate() { return 0; }
489
0
int mac_file_check_dup() { return 0; }
490
0
void ubc_cs_blob_get() {}
491
0
void audit_arg_vnpath() {}
492
0
void get_task_ipcspace() {}
493
0
void vn_rdwr() {}
494
42
int mac_file_label_destroy() { return 0; }
495
0
void fileport_alloc() {}
496
0
void vnode_getwithref() {}
497
52
int mac_file_label_associate() { return 0; }
498
0
void sulong() {}
499
0
void proc_lck_attr() {}
500
0
int mac_vnode_check_write() { return 0; }
501
0
void ipc_port_copyout_send() {}
502
0
void kauth_filesec_free() {}
503
0
void munge_user64_stat64() {}
504
0
void munge_user64_stat() {}
505
0
void VNOP_EXCHANGE() {}
506
0
void vnode_set_openevt() {}
507
0
void vn_stat_noauth() {}
508
0
void vnode_mount() {}
509
0
void open1() {}
510
0
void kauth_authorize_fileop_has_listeners() {}
511
143k
void fp_isguarded() {}
512
0
void audit_arg_fflags() {}
513
0
int mac_vnode_notify_truncate() { return 0; }
514
143k
void fp_guard_exception() {}
515
0
void vnode_clear_openevt() {}
516
0
void pshm_stat() {}
517
0
void proc_knhashlock_grp() {}
518
0
void VNOP_BLOCKMAP() {}
519
0
void vnode_clearnocache() {}
520
0
void VNOP_ADVLOCK() {}
521
0
void ubc_cs_blob_revalidate() {}
522
0
void guarded_fileproc_free() {}
523
0
void audit_arg_text() {}
524
0
void vnode_isnocache() {}
525
0
void mach_port_deallocate() {}
526
52
int mac_file_label_init() { return 0; }
527
0
void vn_pathconf() {}
528
0
void audit_arg_mode() {}
529
10.7k
long boottime_sec() { return 0; }
530
9.51k
void mac_socket_check_receive() {}
531
1.64k
void mac_socket_check_send() {}
532
533
0
void kernel_debug(uint32_t debugid, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4, uintptr_t arg5) {}
534
535
69
void lck_rw_unlock_shared() {}
536
0
kern_return_t kmem_alloc_contig() { assert(false); }
537
uint32_t ipc_control_port_options;
538
539
0
bool current_task_can_use_restricted_in_port() { return true; }
540
541
unsigned int
542
ml_wait_max_cpus(void)
543
1
{
544
1
  return 1;
545
1
}
546
547
int
548
fls(unsigned int mask)
549
7
{
550
7
  if (mask == 0) {
551
0
    return 0;
552
0
  }
553
554
7
  return (sizeof(mask) << 3) - __builtin_clz(mask);
555
7
}
556
557
6
int scnprintf(char *buf, size_t size, const char *fmt, ...) {
558
6
  return 0;
559
6
}
560
561
rlim_t
562
1.26M
proc_limitgetcur(proc_t p, int which, boolean_t to_lock_proc) {
563
1.26M
  if (which == RLIMIT_NOFILE) {
564
1.26M
    return 10;
565
1.26M
  }
566
0
  assert(false);
567
0
}
568
569
4
task_t proc_task() { return TASK_NULL; }
570
571
722k
vm_offset_t current_percpu_base(void) {
572
722k
  return 0;
573
722k
}
574
575
0
int proc_pidversion(proc_t p) {
576
0
  assert(false);
577
0
  return 0;
578
0
}
579
580
unsigned int kdebug_enable = 0;
581
6
void kernel_debug_string_early(const char *message) {
582
6
  printf("kernel_debug_string_early: %s\n", message);
583
6
}