1 /* Test of immutable data.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2021. */
21 #include "immutable.h"
35 #if IMMUTABLE_EFFECTIVE
37 static _GL_ASYNC_SAFE _Noreturn
void
38 segv_handler (int signo
)
44 install_segv_handler (void)
46 signal (SIGSEGV
, segv_handler
);
47 # if (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__
48 signal (SIGBUS
, segv_handler
);
55 main (int argc
, char *argv
[])
59 fprintf (stderr
, "%s: need 1 argument\n", argv
[0]);
62 int test
= atoi (argv
[1]);
66 /* Indicates whether the implementation effectively rejects writes to
68 #if !IMMUTABLE_EFFECTIVE
69 fputs ("Skipping test: immutability cannot be enforced\n", stderr
);
76 /* Correct use of immmalloc. */
81 wp
= (struct data
*) immmalloc (sizeof (struct data
));
93 /* Catch invalid write access. */
98 wp
= (struct data
*) immmalloc (sizeof (struct data
));
103 #if IMMUTABLE_EFFECTIVE
104 install_segv_handler ();
106 /* This assignment should crash. */
107 ((struct data
*) p
)->y
= 77;
108 #if IMMUTABLE_EFFECTIVE
115 /* Catch invalid write access while another data object is not frozen. */
118 struct data
const *p
;
121 wp
= (struct data
*) immmalloc (sizeof (struct data
));
129 wp2
= (struct data
*) immmalloc (sizeof (struct data
));
130 ASSERT (wp2
!= NULL
);
132 #if IMMUTABLE_EFFECTIVE
133 install_segv_handler ();
135 /* This assignment should crash. */
136 ((struct data
*) p
)->y
= 42;
137 #if IMMUTABLE_EFFECTIVE
144 /* Correct use of immstrdup. */
146 const char *s
= immstrdup ("Hello");
147 ASSERT (strlen (s
) == 5);
148 ASSERT (strcmp (s
, "Hello") == 0);
156 return test_exit_status
;