| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/**************************************************************************/ |
| 22 |
|
| 23 |
#ifndef __FC_TIME_H |
| 24 |
#define __FC_TIME_H |
| 25 |
#include "features.h" |
| 26 |
__PUSH_FC_STDLIB |
| 27 |
#include "__fc_define_clockid_t.h" |
| 28 |
#include "__fc_define_null.h" |
| 29 |
#include "__fc_define_size_t.h" |
| 30 |
#include "__fc_define_time_t.h" |
| 31 |
#include "__fc_define_timer_t.h" |
| 32 |
#include "__fc_define_timespec.h" |
| 33 |
#include "__fc_string_axiomatic.h" |
| 34 |
#include "errno.h" |
| 35 |
#include "signal.h" |
| 36 |
|
| 37 |
/* |
| 38 |
* Names of the interval timers, and structure |
| 39 |
* defining a timer setting: |
| 40 |
*/ |
| 41 |
#define ITIMER_REAL 0 |
| 42 |
#define ITIMER_VIRTUAL 1 |
| 43 |
#define ITIMER_PROF 2 |
| 44 |
|
| 45 |
|
| 46 |
__BEGIN_DECLS |
| 47 |
|
| 48 |
#ifndef __clock_t_defined |
| 49 |
typedef unsigned int clock_t; |
| 50 |
#define __clock_t_defined |
| 51 |
#endif |
| 52 |
// From POSIX.1-2008: "The value of CLOCKS_PER_SEC shall be 1 million on |
| 53 |
// XSI-conformant systems. [...]" |
| 54 |
#define CLOCKS_PER_SEC ((time_t)1000000) |
| 55 |
|
| 56 |
struct tm { |
| 57 |
int tm_sec; // seconds after the minute [0, 60] |
| 58 |
int tm_min; // minutes after the hour [0, 59] |
| 59 |
int tm_hour; // hours since midnight [0, 23] |
| 60 |
int tm_mday; // day of the month [1, 31] |
| 61 |
int tm_mon; // months since January [0, 11] |
| 62 |
int tm_year; // years since 1900 |
| 63 |
int tm_wday; // days since Sunday [0, 6] |
| 64 |
int tm_yday; // days since January 1 [0, 365] |
| 65 |
int tm_isdst; // Daylight Saving Time flag |
| 66 |
}; |
| 67 |
|
| 68 |
struct itimerspec { |
| 69 |
struct timespec it_interval; |
| 70 |
struct timespec it_value; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
// Note: macros and specifications in this file consider that no |
| 75 |
// other clocks exist (CLOCK_*_CPUTIME_ID and Linux-specific clocks) |
| 76 |
#define CLOCK_REALTIME 666 |
| 77 |
#define CLOCK_MONOTONIC 1 |
| 78 |
#define TIMER_ABSTIME 1 |
| 79 |
|
| 80 |
//@ ghost volatile unsigned int __fc_time; |
| 81 |
|
| 82 |
/*@ assigns \result \from __fc_time; */ |
| 83 |
extern clock_t clock(void); |
| 84 |
|
| 85 |
/*@ assigns \result \from time1, time0; */ |
| 86 |
extern double difftime(time_t time1, time_t time0); |
| 87 |
|
| 88 |
/*@ |
| 89 |
requires valid_timeptr: \valid(timeptr); |
| 90 |
assigns *timeptr \from *timeptr; |
| 91 |
assigns \result \from indirect:*timeptr; |
| 92 |
*/ |
| 93 |
extern time_t mktime(struct tm *timeptr); |
| 94 |
|
| 95 |
/*@ |
| 96 |
assigns *timer, \result \from __fc_time; |
| 97 |
behavior null: |
| 98 |
assumes timer_null: timer == \null; |
| 99 |
assigns \result \from __fc_time; |
| 100 |
behavior not_null: |
| 101 |
assumes timer_non_null: timer != \null; |
| 102 |
requires valid_timer: \valid(timer); |
| 103 |
assigns *timer, \result \from __fc_time; |
| 104 |
ensures initialization:timer: \initialized(timer); |
| 105 |
complete behaviors; |
| 106 |
disjoint behaviors; |
| 107 |
*/ |
| 108 |
extern time_t time(time_t *timer); |
| 109 |
|
| 110 |
char __fc_ctime[26]; |
| 111 |
char * const __fc_p_ctime = __fc_ctime; |
| 112 |
|
| 113 |
/*@ |
| 114 |
requires valid_timeptr: \valid_read(timeptr); |
| 115 |
requires initialization:init_timeptr: \initialized(timeptr); |
| 116 |
assigns __fc_ctime[0..25] \from indirect:*timeptr, indirect:__fc_time; |
| 117 |
assigns \result \from indirect:*timeptr, indirect:__fc_time, __fc_p_ctime; |
| 118 |
ensures result_points_to_ctime: \result == __fc_p_ctime; |
| 119 |
ensures result_valid_string: valid_read_string(__fc_p_ctime); |
| 120 |
*/ |
| 121 |
extern char *asctime(const struct tm *timeptr); |
| 122 |
|
| 123 |
/*@ |
| 124 |
requires valid_timer: \valid_read(timer); |
| 125 |
requires initialization:init_timer: \initialized(timer); |
| 126 |
assigns __fc_ctime[0..25] \from indirect:*timer, indirect:__fc_time; |
| 127 |
assigns \result \from indirect:*timer, indirect:__fc_time, __fc_p_ctime; |
| 128 |
ensures result_points_to_ctime: \result == __fc_p_ctime; |
| 129 |
ensures result_valid_string: valid_read_string(__fc_p_ctime); |
| 130 |
*/ |
| 131 |
extern char *ctime(const time_t *timer); |
| 132 |
|
| 133 |
struct tm __fc_time_tm; |
| 134 |
struct tm * const __fc_p_time_tm = &__fc_time_tm; |
| 135 |
|
| 136 |
/*@ |
| 137 |
requires valid_timer: \valid_read(timer); |
| 138 |
assigns \result \from __fc_p_time_tm; |
| 139 |
assigns __fc_time_tm \from *timer; |
| 140 |
ensures result_null_or_internal_tm: |
| 141 |
\result == &__fc_time_tm || \result == \null ; |
| 142 |
*/ |
| 143 |
extern struct tm *gmtime(const time_t *timer); |
| 144 |
|
| 145 |
/*@ |
| 146 |
requires valid_timer: \valid_read(timer); |
| 147 |
assigns \result \from __fc_p_time_tm; |
| 148 |
assigns __fc_time_tm \from *timer; |
| 149 |
ensures result_null_or_internal_tm: |
| 150 |
\result == &__fc_time_tm || \result == \null; |
| 151 |
ensures maybe_error: errno == \old(errno) || errno == EOVERFLOW; |
| 152 |
*/ |
| 153 |
extern struct tm *localtime(const time_t *timer); |
| 154 |
|
| 155 |
/*@ |
| 156 |
requires dst_has_room: \valid(s+(0 .. max-1)); |
| 157 |
requires valid_format: valid_read_string(format); |
| 158 |
requires valid_tm: \valid_read(tm); |
| 159 |
assigns s[0 .. max-1] \from indirect:max, indirect:format[0..], indirect:*tm; |
| 160 |
assigns \result \from indirect:max, indirect:format[0..], indirect:*tm; |
| 161 |
ensures result_bounded: \result <= max; |
| 162 |
*/ |
| 163 |
extern size_t strftime(char * restrict s, |
| 164 |
size_t max, |
| 165 |
const char * restrict format, |
| 166 |
const struct tm * restrict tm); |
| 167 |
|
| 168 |
/* POSIX */ |
| 169 |
extern char *asctime_r(const struct tm *restrict, char *restrict); |
| 170 |
|
| 171 |
extern int clock_getres(clockid_t, struct timespec *); |
| 172 |
|
| 173 |
/*@ |
| 174 |
requires tp: \valid(tp); |
| 175 |
assigns \result, *tp, __fc_time \from __fc_time; |
| 176 |
behavior realtime_clock: |
| 177 |
assumes realtime: clk_id == CLOCK_REALTIME; |
| 178 |
ensures success: \result == 0; |
| 179 |
ensures initialization: \initialized(tp); |
| 180 |
behavior monotonic_clock: |
| 181 |
assumes monotonic: clk_id == CLOCK_MONOTONIC; |
| 182 |
#ifndef __FC_NO_MONOTONIC_CLOCK |
| 183 |
ensures success: \result == 0; |
| 184 |
ensures initialization: \initialized(tp); |
| 185 |
#else |
| 186 |
// simulates a system without monotonic clock |
| 187 |
assigns \result\from clk_id; |
| 188 |
ensures error: \result == EINVAL; |
| 189 |
#endif |
| 190 |
behavior bad_clock_id: |
| 191 |
assumes bad_id: clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC; |
| 192 |
assigns \result \from clk_id; |
| 193 |
ensures error: \result == EINVAL; |
| 194 |
|
| 195 |
complete behaviors; |
| 196 |
disjoint behaviors; |
| 197 |
*/ |
| 198 |
extern int clock_gettime(clockid_t clk_id, struct timespec *tp); |
| 199 |
|
| 200 |
/*@ axiomatic nanosleep_predicates { |
| 201 |
@ predicate abs_clock_in_range{L}(clockid_t id, struct timespec *tm) |
| 202 |
@ reads __fc_time; |
| 203 |
@ |
| 204 |
@ predicate valid_clock_id{L}(clockid_t id) // id is a known clock and not |
| 205 |
@ reads __fc_time; // the CPU-time clock of the |
| 206 |
@ } // calling thread |
| 207 |
*/ |
| 208 |
|
| 209 |
/*@ ghost volatile int __fc_interrupted; */ |
| 210 |
|
| 211 |
/*@ // missing: may assign to errno: EINTR, EINVAL, ENOTSUP |
| 212 |
// missing: assigns \result, rmtp \from 'clock having id clock_id' |
| 213 |
requires valid_request: \valid_read(rqtp); |
| 214 |
requires initialization:initialized_request: |
| 215 |
\initialized(&rqtp->tv_sec) && \initialized(&rqtp->tv_nsec); |
| 216 |
requires valid_nanosecs: 0 <= rqtp->tv_nsec < 1000000000; |
| 217 |
requires valid_remaining_or_null: rmtp == \null || \valid(rmtp); |
| 218 |
assigns \result \from indirect:__fc_time, indirect:__fc_interrupted, |
| 219 |
indirect:clock_id, indirect:flags, indirect:rqtp, |
| 220 |
indirect:*rqtp; |
| 221 |
behavior absolute: |
| 222 |
assumes absolute_time: (flags & TIMER_ABSTIME) != 0; |
| 223 |
assumes no_einval: abs_clock_in_range(clock_id, rqtp) && |
| 224 |
valid_clock_id(clock_id); |
| 225 |
assigns \result \from indirect:__fc_time, indirect:__fc_interrupted, |
| 226 |
indirect:clock_id, indirect:rqtp, indirect:*rqtp; |
| 227 |
ensures result_ok_or_error: \result == 0 || \result == EINTR || |
| 228 |
\result == EINVAL || \result == ENOTSUP; |
| 229 |
behavior relative_interrupted: |
| 230 |
assumes relative_time: (flags & TIMER_ABSTIME) == 0; |
| 231 |
assumes interrupted: __fc_interrupted != 0; |
| 232 |
assumes no_einval: valid_clock_id(clock_id); |
| 233 |
assigns \result \from indirect:__fc_time, indirect:clock_id, indirect:rqtp, |
| 234 |
indirect:*rqtp; |
| 235 |
assigns *rmtp \from __fc_time, indirect:clock_id, indirect:rqtp, |
| 236 |
indirect:*rqtp, indirect:rmtp; |
| 237 |
ensures result_interrupted: \result == EINTR; |
| 238 |
ensures initialization:interrupted_remaining: |
| 239 |
rmtp != \null ==> \initialized(&rmtp->tv_sec) && \initialized(&rmtp->tv_nsec); |
| 240 |
ensures interrupted_remaining_decreases: |
| 241 |
rmtp != \null ==> |
| 242 |
rqtp->tv_sec * 1000000000 + rqtp->tv_nsec >= |
| 243 |
rmtp->tv_sec * 1000000000 + rmtp->tv_nsec; |
| 244 |
ensures remaining_valid: |
| 245 |
rmtp != \null ==> 0 <= rmtp->tv_nsec < 1000000000; |
| 246 |
behavior relative_no_error: |
| 247 |
assumes relative_time: (flags & TIMER_ABSTIME) == 0; |
| 248 |
assumes not_interrupted: __fc_interrupted == 0; |
| 249 |
assumes no_einval: valid_clock_id(clock_id); |
| 250 |
assigns \result \from indirect:__fc_time, indirect:clock_id, |
| 251 |
indirect:rqtp, indirect:*rqtp; |
| 252 |
ensures result_ok: \result == 0; |
| 253 |
behavior relative_invalid_clock_id: |
| 254 |
assumes relative_time: (flags & TIMER_ABSTIME) == 0; |
| 255 |
assumes not_interrupted: __fc_interrupted == 0; |
| 256 |
assumes einval: !valid_clock_id(clock_id); |
| 257 |
assigns \result \from indirect:__fc_time, indirect:clock_id, |
| 258 |
indirect:rqtp, indirect:*rqtp; |
| 259 |
ensures result_einval: \result == EINVAL; |
| 260 |
complete behaviors; |
| 261 |
disjoint behaviors; |
| 262 |
*/ |
| 263 |
extern int clock_nanosleep(clockid_t clock_id, int flags, |
| 264 |
const struct timespec *rqtp, struct timespec *rmtp); |
| 265 |
|
| 266 |
extern int clock_settime(clockid_t, const struct timespec *); |
| 267 |
extern char *ctime_r(const time_t *timep, char *buf); |
| 268 |
extern struct tm *getdate(const char *string); |
| 269 |
|
| 270 |
/*@ |
| 271 |
requires valid_timer: \valid_read(timer); |
| 272 |
requires valid_result: \valid(result); |
| 273 |
assigns \result \from indirect:*timer, result; |
| 274 |
assigns *result \from indirect:*timer; |
| 275 |
ensures result_null_or_result: |
| 276 |
\result == result || \result == \null; |
| 277 |
*/ |
| 278 |
extern struct tm *gmtime_r(const time_t *restrict timer, |
| 279 |
struct tm *restrict result); |
| 280 |
|
| 281 |
/*@ |
| 282 |
requires valid_timer: \valid_read(timep); |
| 283 |
requires valid_result: \valid(result); |
| 284 |
assigns \result \from indirect:*timep, result; |
| 285 |
assigns *result \from indirect:*timep; |
| 286 |
assigns errno \from indirect:*timep; |
| 287 |
ensures result_null_or_initialized:initialization: |
| 288 |
(\result == result && \initialized(result)) || \result == \null; |
| 289 |
ensures maybe_error: errno == \old(errno) || errno == EOVERFLOW; |
| 290 |
*/ |
| 291 |
extern struct tm *localtime_r(const time_t *restrict timep, |
| 292 |
struct tm *restrict result); |
| 293 |
|
| 294 |
/*@ // missing: errno may be set to EINTR (EINVAL prevented by precondition) |
| 295 |
requires valid_request: \valid_read(rqtp); |
| 296 |
requires initialization:initialized_request: |
| 297 |
\initialized(&rqtp->tv_sec) && \initialized(&rqtp->tv_nsec); |
| 298 |
requires valid_nanosecs: 0 <= rqtp->tv_nsec < 1000000000; |
| 299 |
requires valid_remaining_or_null: rmtp == \null || \valid(rmtp); |
| 300 |
assigns \result \from indirect:__fc_time, indirect:rqtp, indirect:*rqtp; |
| 301 |
assigns *rmtp \from indirect:__fc_time, indirect:rqtp, indirect:*rqtp, |
| 302 |
indirect:rmtp; |
| 303 |
ensures result_elapsed_or_interrupted: \result == 0 || \result == -1; |
| 304 |
ensures initialization:interrupted_remaining: |
| 305 |
rmtp != \null && \result == -1 ==> |
| 306 |
\initialized(&rmtp->tv_sec) && \initialized(&rmtp->tv_nsec); |
| 307 |
ensures interrupted_remaining_decreases: |
| 308 |
rmtp != \null && \result == -1 ==> |
| 309 |
rqtp->tv_sec * 1000000000 + rqtp->tv_nsec >= |
| 310 |
rmtp->tv_sec * 1000000000 + rmtp->tv_nsec; |
| 311 |
ensures interrupted_remaining_valid: |
| 312 |
rmtp != \null && \result == -1 ==> 0 <= rmtp->tv_nsec < 1000000000; |
| 313 |
*/ |
| 314 |
extern int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); |
| 315 |
|
| 316 |
//Note: uncomment functions below when the necessary types will be defined: |
| 317 |
// locale_t, timer_t |
| 318 |
//extern size_t strftime_l(char *restrict, size_t, const char *restrict, |
| 319 |
// const struct tm *restrict, locale_t); |
| 320 |
extern char *strptime(const char *restrict s, const char *restrict format, |
| 321 |
struct tm *restrict tm); |
| 322 |
extern int timer_create(clockid_t, struct sigevent *restrict, |
| 323 |
timer_t *restrict); |
| 324 |
extern int timer_delete(timer_t); |
| 325 |
extern int timer_getoverrun(timer_t); |
| 326 |
extern int timer_gettime(timer_t, struct itimerspec *); |
| 327 |
extern int timer_settime(timer_t, int, const struct itimerspec *restrict, |
| 328 |
struct itimerspec *restrict); |
| 329 |
|
| 330 |
extern int daylight; |
| 331 |
extern long timezone; |
| 332 |
extern char *tzname[2]; |
| 333 |
|
| 334 |
/*@ assigns tzname[0..1][0..] \from \nothing ;*/ |
| 335 |
extern void tzset(void); |
| 336 |
|
| 337 |
__END_DECLS |
| 338 |
|
| 339 |
__POP_FC_STDLIB |
| 340 |
#endif |