1
#line 1 "test-gc-protect-stack.c"
2
/*===========================================================================
3
* Filename : test-gc-protect-stack.c
4
* About : unit test for scm_gc_protect_stack()
6
* Copyright (C) 2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
7
* Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
11
* Redistribution and use in source and binary forms, with or without
12
* modification, are permitted provided that the following conditions
15
* 1. Redistributions of source code must retain the above copyright
16
* notice, this list of conditions and the following disclaimer.
17
* 2. Redistributions in binary form must reproduce the above copyright
18
* notice, this list of conditions and the following disclaimer in the
19
* documentation and/or other materials provided with the distribution.
20
* 3. Neither the name of authors nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
25
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
28
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
===========================================================================*/
38
/* Since introduction of libgcroots to SigScheme, the GC protection interfaces
39
* are withdrawn. These tests does not make sense now. -- YamaKen 2006-12-23 */
40
#define TST_EXCLUDE_THIS
43
#define TST_HAVE_MAIN 1
44
#include "sscm-test.h"
46
#ifndef TST_EXCLUDE_THIS
49
#include <sigscheme/sigscheme.h>
51
#define SCMOBJ_ALIGNEDP(ptr) (!((uintptr_t)(ptr) % sizeof(ScmObj)))
53
#define TEST_STACK_START(protected, actual) \
54
if (stack_dir == STACK_GROWS_DOWNWARDS) { \
55
TST_TN_TRUE(actual <= protected); \
57
TST_TN_TRUE(actual >= protected); \
60
enum stack_growth_dir {
61
STACK_GROWS_DOWNWARDS,
65
static enum stack_growth_dir stack_dir;
66
static void *volatile stack_start_protected;
67
static void *volatile stack_start_actual;
68
static void (*volatile fvv_internal)(void);
69
static int (*volatile fiv_internal)(void);
70
static void (*volatile fvi_internal)(int);
71
static int (*volatile fii_internal)(int);
72
static ScmObj *(*volatile fspsp_internal)(ScmObj *dummy);
73
static ScmObj *(*volatile fspsp2_internal)(ScmObj *dummy);
75
static enum stack_growth_dir probe_stack_growth_dir(void);
76
static enum stack_growth_dir probe_stack_growth_dir2(void *upper_frame);
77
static void fvv(void);
79
static void fvi(int dummy);
80
static int fii(int dummy);
81
static ScmObj *fspsp(ScmObj *dummy);
82
static ScmObj *fspsp2(ScmObj *dummy);
85
static enum stack_growth_dir
86
probe_stack_growth_dir(void)
90
return probe_stack_growth_dir2(&stack_start);
93
static enum stack_growth_dir
94
probe_stack_growth_dir2(void *upper_frame)
98
if ((void *)&stack_start < upper_frame)
99
return STACK_GROWS_DOWNWARDS;
101
return STACK_GROWS_UPWARDS;
109
stack_start_actual = &stack_start;
117
stack_start_actual = &stack_start;
126
stack_start_actual = &stack_start;
134
stack_start_actual = &stack_start;
143
stack_start_actual = &stack_start;
147
/* simulates scm_gc_protect_stack_internal() */
149
fspsp2(ScmObj *designated_stack_start)
153
if (!designated_stack_start)
154
designated_stack_start = &stack_start;
156
stack_start_actual = designated_stack_start;
158
/* enabling this fragment on --enable-debug configuration offsets stack
161
SCM_ASSERT(SCMOBJ_ALIGNEDP(stack_start_actual));
164
/* may intentionally be an invalidated local address */
165
return designated_stack_start;
169
main(int argc, char **argv)
171
tst_suite_info suite = TST_DEFAULT_SUITE_SETUP;
173
scm_initialize(NULL);
175
stack_dir = probe_stack_growth_dir();
180
fspsp_internal = fspsp;
181
fspsp2_internal = fspsp2;
187
TST_DEFAULT_SUITE_CLEANUP(suite);
188
return !!suite.stats.fail;
191
TST_CASE(tst_1, "void (*)(void)")
193
stack_start_protected = scm_gc_current_stack();
194
scm_gc_protect_stack(stack_start_protected);
196
scm_gc_unprotect_stack(stack_start_protected);
198
TEST_STACK_START(stack_start_protected, stack_start_actual);
201
TST_CASE(tst_2, "int (*)(void)")
203
stack_start_protected = scm_gc_current_stack();
204
scm_gc_protect_stack(stack_start_protected);
206
scm_gc_unprotect_stack(stack_start_protected);
208
TEST_STACK_START(stack_start_protected, stack_start_actual);
211
TST_CASE(tst_3, "void (*)(int)")
213
stack_start_protected = scm_gc_current_stack();
214
scm_gc_protect_stack(stack_start_protected);
216
scm_gc_unprotect_stack(stack_start_protected);
218
TEST_STACK_START(stack_start_protected, stack_start_actual);
221
TST_CASE(tst_4, "int (*)(int)")
223
stack_start_protected = scm_gc_current_stack();
224
scm_gc_protect_stack(stack_start_protected);
226
scm_gc_unprotect_stack(stack_start_protected);
228
TEST_STACK_START(stack_start_protected, stack_start_actual);
231
TST_CASE(tst_5, "ScmObj *(*)(ScmObj *)")
233
stack_start_protected = scm_gc_current_stack();
234
scm_gc_protect_stack(stack_start_protected);
235
(*fspsp_internal)(NULL);
236
scm_gc_unprotect_stack(stack_start_protected);
238
TEST_STACK_START(stack_start_protected, stack_start_actual);
241
TST_CASE(tst_6, "ScmObj *(*)(ScmObj *) (2)")
243
stack_start_protected = scm_gc_current_stack();
244
scm_gc_protect_stack(stack_start_protected);
245
(*fspsp2_internal)(NULL);
246
scm_gc_unprotect_stack(stack_start_protected);
248
TEST_STACK_START(stack_start_protected, stack_start_actual);
251
#endif /* TST_EXCLUDE_THIS */