summaryrefslogtreecommitdiff
path: root/tests/test-immutable.c
blob: 04d791d7053b5cac958b4019cc4bf29f16c0866c (plain)
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
/* Test of immutable data.
   Copyright (C) 2021-2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2021.  */

#include <config.h>

#include "immutable.h"

#include <signal.h>
#include <stdlib.h>
#include <string.h>

#include "macros.h"

struct data
{
  int x;
  long y;
};

#if IMMUTABLE_EFFECTIVE

static _GL_ASYNC_SAFE _Noreturn void
segv_handler (int signo)
{
  _Exit (0);
}

static void
install_segv_handler (void)
{
  signal (SIGSEGV, segv_handler);
# if (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__
  signal (SIGBUS, segv_handler);
# endif
}

#endif

int
main (int argc, char *argv[])
{
  if (argc != 2)
    {
      fprintf (stderr, "%s: need 1 argument\n", argv[0]);
      return 1;
    }
  int test = atoi (argv[1]);
  switch (test)
    {
    case 0:
      /* Indicates whether the implementation effectively rejects writes to
         immutable data.  */
      #if !IMMUTABLE_EFFECTIVE
      fputs ("Skipping test: immutability cannot be enforced\n", stderr);
      return 77;
      #else
      break;
      #endif

    case 1:
      /* Correct use of immmalloc.  */
      {
        struct data *wp;
        struct data const *p;

        wp = (struct data *) immmalloc (sizeof (struct data));
        ASSERT (wp != NULL);
        wp->x = 7;
        wp->y = 42;
        p = immfreeze (wp);
        ASSERT (p->x == 7);
        ASSERT (p->y == 42);
        immfree (p);
      }
      break;

    case 2:
      /* Catch invalid write access.  */
      {
        struct data *wp;
        struct data const *p;

        wp = (struct data *) immmalloc (sizeof (struct data));
        ASSERT (wp != NULL);
        wp->x = 7;
        wp->y = 42;
        p = immfreeze (wp);
        #if IMMUTABLE_EFFECTIVE
        install_segv_handler ();
        #endif
        /* This assignment should crash.  */
        ((struct data *) p)->y = 77;
        #if IMMUTABLE_EFFECTIVE
        return 1;
        #endif
      }
      break;

    case 3:
      /* Catch invalid write access while another data object is not frozen.  */
      {
        struct data *wp;
        struct data const *p;
        struct data *wp2;

        wp = (struct data *) immmalloc (sizeof (struct data));
        ASSERT (wp != NULL);
        wp->x = 7;
        wp->y = 42;
        p = immfreeze (wp);
        ASSERT (p->x == 7);
        ASSERT (p->y == 42);

        wp2 = (struct data *) immmalloc (sizeof (struct data));
        ASSERT (wp2 != NULL);
        wp2->x = 7;
        #if IMMUTABLE_EFFECTIVE
        install_segv_handler ();
        #endif
        /* This assignment should crash.  */
        ((struct data *) p)->y = 42;
        #if IMMUTABLE_EFFECTIVE
        return 1;
        #endif
      }
      break;

    case 4:
      /* Correct use of immstrdup.  */
      {
        const char *s = immstrdup ("Hello");
        ASSERT (strlen (s) == 5);
        ASSERT (strcmp (s, "Hello") == 0);
        immfree (s);
      }
      break;

    default:
      ASSERT (false);
    }
  return 0;
}