• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * This source contains libc overrides for the sake of stable benchmarking. If
18  * building a binary for the purpose of benchmarking, the object compiled from
19  * this source is to be injected into the list of objects-to-be-linked before
20  * the list of libraries-to-be-linked to ensure that the linker picks up these
21  * implementations.
22  */
23 
24 #ifdef __GNUC__
25 /*
26  * Note:
27  *      This is nasty and dangerous. However, we only need the timeval structure
28  *      from sys/time.h. Unfortunately, the same header also declares
29  *      gettimeofday, which has different declarations on different platforms
30  *      (e.g., macOS, Linux). So, instead of #ifdef'ing for platforms, we simply
31  *      tweak the header to declare another function. Don't try this at home.
32  */
33 #define gettimeofday __prevent_conflicting_gettimeofday_declarations__
34 #include <sys/time.h>
35 #undef gettimeofday
36 
37 int gettimeofday (struct timeval *, void *);
38 
39 /**
40  * Useless but stable gettimeofday implementation. Returns Epoch. Ensures that
41  * test cases relying on "now" yield stable results.
42  */
gettimeofday(struct timeval * tv,void * tz)43 int gettimeofday (struct timeval *tv,
44                   void *tz)
45 {
46   (void) tz;
47   tv->tv_sec = 0;
48   tv->tv_usec = 0;
49   return 0;
50 } /* gettimeofday */
51 #endif /* __GNUC__ */
52 
53 int rand (void);
54 
55 /**
56  * Useless but stable rand implementation. Returns 4. Ensures that test cases
57  * relying on randomness yield stable results.
58  */
rand(void)59 int rand (void)
60 {
61   return 4; /* Chosen by fair dice roll. Guaranteed to be random. */
62 } /* rand */
63