summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/newlib01/init.c
blob: 0bb3c4628bcfb048dfa29c332d562125fcb4c68e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* SPDX-License-Identifier: BSD-2-Clause */

/*
 * Copyright (c) 2014, 2022 embedded brains GmbH.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <errno.h>

#include <sys/reent.h>
#include <sys/stat.h>

#include <rtems.h>
#include <rtems/console.h>
#include <rtems/imfs.h>
#include <rtems/libcsupport.h>
#include <rtems/sysinit.h>

#include "tmacros.h"

const char rtems_test_name[] = "NEWLIB 1";

static const char stdio_file_path[] = "/stdio-file";

static const char non_stdio_file_path[] = "/non-stdio-file";

typedef enum {
  INIT,
  OPEN,
  WRITTEN,
  CLOSED
} test_state;

typedef struct {
  rtems_id main_task_id;
  rtems_id worker_task_id;
  test_state current;
  FILE *non_stdio_file;
  int non_stdio_fd;
} test_context;

static test_context test_instance;

static void wake_up_main(const test_context *ctx)
{
  rtems_status_code sc;

  sc = rtems_event_transient_send(ctx->main_task_id);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

static void wait(void)
{
  rtems_status_code sc;

  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

/*
 * Check that rand() is properly initialized and returns the expected sequence
 * for default seed values. A call to rand() without any previous call to
 * srand() generates the same sequence as when srand() is first called with a
 * seed value of 1.
 */
static void test_rand(void)
{
  int rv;

  rv = rand();
  rtems_test_assert(rv == 1481765933);
  rv = rand();
  rtems_test_assert(rv == 1085377743);
  rv = rand();
  rtems_test_assert(rv == 1270216262);

  srand(1);
  rv = rand();
  rtems_test_assert(rv == 1481765933);
  rv = rand();
  rtems_test_assert(rv == 1085377743);
  rv = rand();
  rtems_test_assert(rv == 1270216262);
}

/*
 * Check that lrand48() is properly initialized and returns the expected
 * sequence for default seed values. A call to lrand48() without any previous
 * call to srand48() uses default constant initializer values set in the _seed
 * member of struct _rand48.
 */
static void test_lrand48(void)
{
  long rv;

  rv = lrand48();
  rtems_test_assert(rv == 851401618);
  rv = lrand48();
  rtems_test_assert(rv == 1804928587);
  rv = lrand48();
  rtems_test_assert(rv == 758783491);
}

static void stdio_file_worker(rtems_task_argument arg)
{
  test_context *ctx = &test_instance;
  struct _reent *reent = _REENT;
  FILE *output;
  char buf[1] = { 'x' };
  size_t n;

  test_rand();
  test_lrand48();

  rtems_test_assert(reent->__cleanup == NULL);

  output = stdout = fopen(&stdio_file_path[0], "r+");
  rtems_test_assert(stdout != NULL);

  /*
   * Check newlib's __sinit does not touch our assigned file pointer.
   */
  rtems_test_assert(reent->__cleanup == NULL);
  rtems_test_assert(fflush(stdout) == 0);
  rtems_test_assert(reent->__cleanup != NULL);
  rtems_test_assert(stdout == output);

  n = fwrite(&buf[0], sizeof(buf), 1, stdout);
  rtems_test_assert(n == 1);

  rtems_test_assert(ctx->current == OPEN);

  wake_up_main(ctx);

  rtems_test_assert(0);
}

static void non_stdio_file_worker(rtems_task_argument arg)
{
  test_context *ctx = &test_instance;
  FILE *fp;
  char buf[1] = { 'y' };
  size_t n;
  int fd;

  fp = ctx->non_stdio_file = fopen(&non_stdio_file_path[0], "w");
  rtems_test_assert(fp != NULL);

  /* Get file descriptor of new global file stream, store it in text context */
  fd = fileno(fp);
  rtems_test_assert(fd != -1);
  ctx->non_stdio_fd = fd;

  n = fwrite(&buf[0], sizeof(buf), 1, fp);
  rtems_test_assert(n == 1);

  wake_up_main(ctx);

  rtems_test_assert(0);
}

static int handler_open(
  rtems_libio_t *iop,
  const char *path,
  int oflag,
  mode_t mode
)
{
  test_context *ctx = &test_instance;

  rtems_test_assert(ctx->current == INIT);
  ctx->current = OPEN;

  return 0;
}

static int handler_close(
  rtems_libio_t *iop
)
{
  test_context *ctx = &test_instance;

  rtems_test_assert(ctx->current == WRITTEN);
  ctx->current = CLOSED;

  return 0;
}

static ssize_t handler_read(
  rtems_libio_t *iop,
  void *buffer,
  size_t count
)
{
  rtems_test_assert(0);

  return 0;
}

static ssize_t handler_write(
  rtems_libio_t *iop,
  const void *buffer,
  size_t count
)
{
  test_context *ctx = &test_instance;

  rtems_test_assert(ctx->current == OPEN);
  ctx->current = WRITTEN;

  iop->offset += count;

  return (ssize_t) count;
}

static int handler_ioctl(
  rtems_libio_t *iop,
  ioctl_command_t request,
  void *buffer
)
{
  rtems_test_assert(0);

  return 0;
}

static off_t handler_lseek(
  rtems_libio_t *iop,
  off_t length,
  int whence
)
{
  rtems_test_assert(0);

  return 0;
}

static int handler_ftruncate(
  rtems_libio_t *iop,
  off_t length
)
{
  rtems_test_assert(0);

  return 0;
}

static int handler_fsync(
  rtems_libio_t *iop
)
{
  rtems_test_assert(0);

  return 0;
}

static int handler_fdatasync(
  rtems_libio_t *iop
)
{
  rtems_test_assert(0);

  return 0;
}

static int handler_fcntl(
  rtems_libio_t *iop,
  int cmd
)
{
  rtems_test_assert(0);

  return 0;
}

static ssize_t handler_readv(
  rtems_libio_t *iop,
  const struct iovec *iov,
  int iovcnt,
  ssize_t total
)
{
  rtems_test_assert(0);

  return 0;
}

static ssize_t handler_writev(
  rtems_libio_t *iop,
  const struct iovec *iov,
  int iovcnt,
  ssize_t total
)
{
  rtems_test_assert(0);

  return 0;
}

static const rtems_filesystem_file_handlers_r node_handlers = {
  .open_h = handler_open,
  .close_h = handler_close,
  .read_h = handler_read,
  .write_h = handler_write,
  .ioctl_h = handler_ioctl,
  .lseek_h = handler_lseek,
  .fstat_h = rtems_filesystem_default_fstat,
  .ftruncate_h = handler_ftruncate,
  .fsync_h = handler_fsync,
  .fdatasync_h = handler_fdatasync,
  .fcntl_h = handler_fcntl,
  .readv_h = handler_readv,
  .writev_h = handler_writev
};

static const IMFS_node_control node_control = IMFS_GENERIC_INITIALIZER(
  &node_handlers,
  IMFS_node_initialize_default,
  IMFS_node_destroy_default
);

static void create_and_run_worker(test_context *ctx, rtems_task_entry entry)
{
  rtems_status_code sc;

  sc = rtems_task_create(
    rtems_build_name('W', 'O', 'R', 'K'),
    2,
    RTEMS_MINIMUM_STACK_SIZE,
    RTEMS_DEFAULT_MODES,
    RTEMS_DEFAULT_ATTRIBUTES,
    &ctx->worker_task_id
  );
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  sc = rtems_task_start(ctx->worker_task_id, entry, 0);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);

  wait();

  sc = rtems_task_delete(ctx->worker_task_id);
  rtems_test_assert(sc == RTEMS_SUCCESSFUL);
}

/*
 * Check that a FILE opened by a task and assigned to a stdio stream is closed
 * during thread termination. Ensure that resources are returned to the system.
 */
static void test_stdio_file(test_context *ctx)
{
  int rv;
  rtems_resource_snapshot snapshot;

  rtems_resource_snapshot_take(&snapshot);

  rv = IMFS_make_generic_node(
    &stdio_file_path[0],
    S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO,
    &node_control,
    NULL
  );
  rtems_test_assert(rv == 0);

  create_and_run_worker(ctx, stdio_file_worker);

  rv = unlink(&stdio_file_path[0]);
  rtems_test_assert(rv == 0);

  rtems_test_assert(rtems_resource_snapshot_check(&snapshot));
}

/*
 * Open a global FILE object from a task but do not assign it to a stdio
 * stream. The FILE is not closed upon thread termination.
 */
static void test_non_stdio_file(test_context *ctx)
{
  create_and_run_worker(ctx, non_stdio_file_worker);
}

/*
 * This exit handler will be called last among the functions registered with
 * atexit(). Check that stdio file descriptors are closed. The Newlib cleanup
 * handler has not yet run, so the stdio FILE objects themselves are still
 * open.
 */
static void check_after_libio_exit(void)
{
  test_context *ctx = &test_instance;
  struct stat unused;
  int rv;

  errno = 0;
  rv = fstat(0, &unused);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EBADF);

  errno = 0;
  rv = fstat(1, &unused);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EBADF);

  errno = 0;
  rv = fstat(2, &unused);
  rtems_test_assert(rv == -1);
  rtems_test_assert(errno == EBADF);

  rtems_test_assert(stdin->_flags != 0);
  rtems_test_assert(stdout->_flags != 0);
  rtems_test_assert(stderr->_flags != 0);

  /*
   * The non-stdio file and its file descriptor should be still open at this
   * point.
   */
  rv = fstat(ctx->non_stdio_fd, &unused);
  rtems_test_assert(rv == 0);
  rtems_test_assert(ctx->non_stdio_file->_flags != 0);
}

static void register_exit_handler_before_libio_exit(void)
{
  int rv;

  rv = atexit(check_after_libio_exit);
  rtems_test_assert(rv == 0);
}

/*
 * Register the exit handler before rtems_libio_exit() so that
 * check_after_libio_exit() is called after rtems_libio_exit().  The exit()
 * handlers are in a LIFO list.
 */
RTEMS_SYSINIT_ITEM(register_exit_handler_before_libio_exit,
    RTEMS_SYSINIT_STD_FILE_DESCRIPTORS, RTEMS_SYSINIT_ORDER_FIRST);

/*
 * At this point, neither the functions registered with atexit() nor the Newlib
 * cleanup procedures have been called. Therefore, stdio file descriptors
 * should be open and stdio FILE object flags should be non-zero.
 */
static void test_exit_handling(test_context *ctx)
{
  struct stat unused;
  int rv;

  rv = fstat(0, &unused);
  rtems_test_assert(rv == 0);

  rv = fstat(1, &unused);
  rtems_test_assert(rv == 0);

  rv = fstat(2, &unused);
  rtems_test_assert(rv == 0);

  rtems_test_assert(stdin->_flags != 0);
  rtems_test_assert(stdout->_flags != 0);
  rtems_test_assert(stderr->_flags != 0);

  /*
   * The file descriptor of the non-stdio file should still be open; the FILE
   * object flags should still be non-zero.
   */
  rv = fstat(ctx->non_stdio_fd, &unused);
  rtems_test_assert(rv == 0);
  rtems_test_assert(ctx->non_stdio_file->_flags != 0);

  /* Run exit handlers and Newlib cleanup procedures */
  exit(0);
}

static void Init(rtems_task_argument arg)
{
  test_context *ctx = &test_instance;
  FILE *file;
  int rv;

  TEST_BEGIN();
  test_rand();
  test_lrand48();

  ctx->main_task_id = rtems_task_self();

  /* Fill dynamic file pool in Newlib */
  file = fopen(CONSOLE_DEVICE_NAME, "r+");
  rtems_test_assert(file != NULL);
  rv = fclose(file);
  rtems_test_assert(rv == 0);

  test_stdio_file(ctx);
  test_non_stdio_file(ctx);
  test_exit_handling(ctx);
}

static void fatal_extension(
  rtems_fatal_source source,
  bool always_set_to_false,
  rtems_fatal_code error
)
{
  if (
    source == RTEMS_FATAL_SOURCE_EXIT
      && !always_set_to_false
      && error == 0
  ) {
    /*
     * Final conditions check after exit handlers and Newlib cleanup procedures
     * have run. File descriptors and FILE objects themselves are closed.
     */
    struct stat unused;
    int rv;
    test_context *ctx = &test_instance;

    errno = 0;
    rv = fstat(0, &unused);
    rtems_test_assert(rv == -1);
    rtems_test_assert(errno == EBADF);

    errno = 0;
    rv = fstat(1, &unused);
    rtems_test_assert(rv == -1);
    rtems_test_assert(errno == EBADF);

    errno = 0;
    rv = fstat(2, &unused);
    rtems_test_assert(rv == -1);
    rtems_test_assert(errno == EBADF);

    rtems_test_assert(stdin->_flags == 0);
    rtems_test_assert(stdout->_flags == 0);
    rtems_test_assert(stderr->_flags == 0);

    /* The non-stdio file and its file descriptor should be closed */
    errno = 0;
    rv = fstat(ctx->non_stdio_fd, &unused);
    rtems_test_assert(rv == -1);
    rtems_test_assert(errno == EBADF);
    rtems_test_assert(ctx->non_stdio_file->_flags == 0);

    TEST_END();
  }
}

#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER

#define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 5

#define CONFIGURE_MAXIMUM_TASKS 2

#define CONFIGURE_INITIAL_EXTENSIONS \
  { .fatal = fatal_extension }, \
  RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_RTEMS_INIT_TASKS_TABLE

#define CONFIGURE_INIT

#include <rtems/confdefs.h>