3232
3333#include "ccompat.h"
3434
35- //!TOKSTYLE-
36- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
37- #include "../testing/fuzzing/fuzz_adapter.h"
38- #endif
39- //!TOKSTYLE+
40-
4135/** don't call into system billions of times for no reason */
4236struct Mono_Time {
4337 uint64_t cur_time ;
@@ -110,6 +104,11 @@ static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_
110104non_null (1 ) nullable (2 )
111105static uint64_t current_time_monotonic_default (Mono_Time * mono_time , void * user_data )
112106{
107+ #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
108+ // This assert should always fail. If it does, the fuzzing harness didn't
109+ // override the mono time callback.
110+ assert (mono_time == nullptr );
111+ #endif
113112 struct timespec clock_mono ;
114113 clock_gettime (CLOCK_MONOTONIC , & clock_mono );
115114 return timespec_to_u64 (clock_mono );
@@ -118,14 +117,6 @@ static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_
118117#endif // !OS_WIN32
119118
120119
121- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
122- non_null (1 ) nullable (2 )
123- static uint64_t current_time_monotonic_dummy (Mono_Time * mono_time , void * user_data )
124- {
125- return fuzz_get_count ();
126- }
127- #endif
128-
129120Mono_Time * mono_time_new (void )
130121{
131122 Mono_Time * mono_time = (Mono_Time * )calloc (1 , sizeof (Mono_Time ));
@@ -147,11 +138,7 @@ Mono_Time *mono_time_new(void)
147138 return nullptr ;
148139 }
149140
150- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
151- mono_time -> current_time_callback = current_time_monotonic_dummy ;
152- #else
153141 mono_time -> current_time_callback = current_time_monotonic_default ;
154- #endif
155142 mono_time -> user_data = nullptr ;
156143
157144#ifdef OS_WIN32
@@ -169,12 +156,15 @@ Mono_Time *mono_time_new(void)
169156
170157 mono_time -> cur_time = 0 ;
171158#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
172- mono_time -> base_time = 0 ; // Maximum reproducibility
159+ // Maximum reproducibility and don't update mono_time before the harness has
160+ // had a chance to set the callback.
161+ // TODO(iphydf): Put mono time callback into Tox_Options with accessors only
162+ // in tox_private.h.
163+ mono_time -> base_time = 0 ;
173164#else
174165 mono_time -> base_time = (uint64_t )time (nullptr ) - (current_time_monotonic (mono_time ) / 1000ULL );
175- #endif
176-
177166 mono_time_update (mono_time );
167+ #endif
178168
179169 return mono_time ;
180170}
0 commit comments