~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to sigscheme/test-c/test-gc-protect-stack.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2007-04-21 03:46:09 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070421034609-gpcurkutp8vaysqj
Tags: 1:1.4.1-3
* Switch to dh_gtkmodules for the gtk 2.10 transition (Closes:
  #419318)
  - debian/control: Add ${misc:Depends} and remove libgtk2.0-bin on
    uim-gtk2.0.
  - debian/uim-gtk2.0.post{inst,rm}: Removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
 *  Filename : test-gc-protect-stack.c
 
3
 *  About    : unit test for scm_gc_protect_stack()
 
4
 *
 
5
 *  Copyright (C) 2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
 
6
 *  Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
 
7
 *
 
8
 *  All rights reserved.
 
9
 *
 
10
 *  Redistribution and use in source and binary forms, with or without
 
11
 *  modification, are permitted provided that the following conditions
 
12
 *  are met:
 
13
 *
 
14
 *  1. Redistributions of source code must retain the above copyright
 
15
 *     notice, this list of conditions and the following disclaimer.
 
16
 *  2. Redistributions in binary form must reproduce the above copyright
 
17
 *     notice, this list of conditions and the following disclaimer in the
 
18
 *     documentation and/or other materials provided with the distribution.
 
19
 *  3. Neither the name of authors nor the names of its contributors
 
20
 *     may be used to endorse or promote products derived from this software
 
21
 *     without specific prior written permission.
 
22
 *
 
23
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
 
24
 *  IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
25
 *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
26
 *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
 
27
 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
28
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
29
 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
30
 *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
31
 *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
32
 *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
33
 *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
34
===========================================================================*/
 
35
 
 
36
#if 1
 
37
/* Since introduction of libgcroots to SigScheme, the GC protection interfaces
 
38
 * are withdrawn. These tests does not make sense now. -- YamaKen 2006-12-23 */
 
39
#define TST_EXCLUDE_THIS
 
40
#endif
 
41
 
 
42
#define TST_HAVE_MAIN 1
 
43
#include "sscm-test.h"
 
44
 
 
45
#ifndef TST_EXCLUDE_THIS
 
46
 
 
47
#include <stddef.h>
 
48
#include <sigscheme/sigscheme.h>
 
49
 
 
50
#define SCMOBJ_ALIGNEDP(ptr) (!((uintptr_t)(ptr) % sizeof(ScmObj)))
 
51
 
 
52
#define TEST_STACK_START(protected, actual)                                  \
 
53
    if (stack_dir == STACK_GROWS_DOWNWARDS) {                                \
 
54
        TST_TN_TRUE(actual <= protected);                                    \
 
55
    } else {                                                                 \
 
56
        TST_TN_TRUE(actual >= protected);                                    \
 
57
    }
 
58
 
 
59
enum stack_growth_dir {
 
60
    STACK_GROWS_DOWNWARDS,
 
61
    STACK_GROWS_UPWARDS
 
62
};
 
63
 
 
64
static enum stack_growth_dir stack_dir;
 
65
static void *volatile stack_start_protected;
 
66
static void *volatile stack_start_actual;
 
67
static void (*volatile fvv_internal)(void);
 
68
static int  (*volatile fiv_internal)(void);
 
69
static void (*volatile fvi_internal)(int);
 
70
static int  (*volatile fii_internal)(int);
 
71
static ScmObj *(*volatile fspsp_internal)(ScmObj *dummy);
 
72
static ScmObj *(*volatile fspsp2_internal)(ScmObj *dummy);
 
73
 
 
74
static enum stack_growth_dir probe_stack_growth_dir(void);
 
75
static enum stack_growth_dir probe_stack_growth_dir2(void *upper_frame);
 
76
static void fvv(void);
 
77
static int  fiv(void);
 
78
static void fvi(int dummy);
 
79
static int  fii(int dummy);
 
80
static ScmObj *fspsp(ScmObj *dummy);
 
81
static ScmObj *fspsp2(ScmObj *dummy);
 
82
 
 
83
 
 
84
static enum stack_growth_dir
 
85
probe_stack_growth_dir(void)
 
86
{
 
87
    int stack_start;
 
88
 
 
89
    return probe_stack_growth_dir2(&stack_start);
 
90
}
 
91
 
 
92
static enum stack_growth_dir
 
93
probe_stack_growth_dir2(void *upper_frame)
 
94
{
 
95
    int stack_start;
 
96
 
 
97
    if ((void *)&stack_start < upper_frame)
 
98
        return STACK_GROWS_DOWNWARDS;
 
99
    else
 
100
        return STACK_GROWS_UPWARDS;
 
101
}
 
102
 
 
103
static void
 
104
fvv(void)
 
105
{
 
106
    ScmObj stack_start;
 
107
 
 
108
    stack_start_actual = &stack_start;
 
109
}
 
110
 
 
111
static int
 
112
fiv(void)
 
113
{
 
114
    ScmObj stack_start;
 
115
 
 
116
    stack_start_actual = &stack_start;
 
117
    return 0;
 
118
}
 
119
 
 
120
static void
 
121
fvi(int dummy)
 
122
{
 
123
    ScmObj stack_start;
 
124
 
 
125
    stack_start_actual = &stack_start;
 
126
}
 
127
 
 
128
static int
 
129
fii(int dummy)
 
130
{
 
131
    ScmObj stack_start;
 
132
 
 
133
    stack_start_actual = &stack_start;
 
134
    return 0;
 
135
}
 
136
 
 
137
static ScmObj *
 
138
fspsp(ScmObj *dummy)
 
139
{
 
140
    ScmObj stack_start;
 
141
 
 
142
    stack_start_actual = &stack_start;
 
143
    return dummy;
 
144
}
 
145
 
 
146
/* simulates scm_gc_protect_stack_internal() */
 
147
static ScmObj *
 
148
fspsp2(ScmObj *designated_stack_start)
 
149
{
 
150
    ScmObj stack_start;
 
151
 
 
152
    if (!designated_stack_start)
 
153
        designated_stack_start = &stack_start;
 
154
 
 
155
    stack_start_actual = designated_stack_start;
 
156
 
 
157
    /* enabling this fragment on --enable-debug configuration offsets stack
 
158
     * pointer */
 
159
#if 1
 
160
    SCM_ASSERT(SCMOBJ_ALIGNEDP(stack_start_actual));
 
161
#endif
 
162
 
 
163
    /* may intentionally be an invalidated local address */
 
164
    return designated_stack_start;
 
165
}
 
166
 
 
167
int
 
168
main(int argc, char **argv)
 
169
{
 
170
    tst_suite_info suite = TST_DEFAULT_SUITE_SETUP;
 
171
 
 
172
    scm_initialize(NULL);
 
173
 
 
174
    stack_dir = probe_stack_growth_dir();
 
175
    fvv_internal = fvv;
 
176
    fiv_internal = fiv;
 
177
    fvi_internal = fvi;
 
178
    fii_internal = fii;
 
179
    fspsp_internal = fspsp;
 
180
    fspsp2_internal = fspsp2;
 
181
 
 
182
    tst_main(&suite);
 
183
 
 
184
    scm_finalize();
 
185
 
 
186
    TST_DEFAULT_SUITE_CLEANUP(suite);
 
187
    return !!suite.stats.fail;
 
188
}
 
189
 
 
190
TST_CASE("void (*)(void)")
 
191
{
 
192
    stack_start_protected = scm_gc_current_stack();
 
193
    scm_gc_protect_stack(stack_start_protected);
 
194
    (*fvv_internal)();
 
195
    scm_gc_unprotect_stack(stack_start_protected);
 
196
 
 
197
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
198
}
 
199
 
 
200
TST_CASE("int (*)(void)")
 
201
{
 
202
    stack_start_protected = scm_gc_current_stack();
 
203
    scm_gc_protect_stack(stack_start_protected);
 
204
    (*fiv_internal)();
 
205
    scm_gc_unprotect_stack(stack_start_protected);
 
206
 
 
207
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
208
}
 
209
 
 
210
TST_CASE("void (*)(int)")
 
211
{
 
212
    stack_start_protected = scm_gc_current_stack();
 
213
    scm_gc_protect_stack(stack_start_protected);
 
214
    (*fvi_internal)(0);
 
215
    scm_gc_unprotect_stack(stack_start_protected);
 
216
 
 
217
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
218
}
 
219
 
 
220
TST_CASE("int (*)(int)")
 
221
{
 
222
    stack_start_protected = scm_gc_current_stack();
 
223
    scm_gc_protect_stack(stack_start_protected);
 
224
    (*fii_internal)(0);
 
225
    scm_gc_unprotect_stack(stack_start_protected);
 
226
 
 
227
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
228
}
 
229
 
 
230
TST_CASE("ScmObj *(*)(ScmObj *)")
 
231
{
 
232
    stack_start_protected = scm_gc_current_stack();
 
233
    scm_gc_protect_stack(stack_start_protected);
 
234
    (*fspsp_internal)(NULL);
 
235
    scm_gc_unprotect_stack(stack_start_protected);
 
236
 
 
237
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
238
}
 
239
 
 
240
TST_CASE("ScmObj *(*)(ScmObj *) (2)")
 
241
{
 
242
    stack_start_protected = scm_gc_current_stack();
 
243
    scm_gc_protect_stack(stack_start_protected);
 
244
    (*fspsp2_internal)(NULL);
 
245
    scm_gc_unprotect_stack(stack_start_protected);
 
246
 
 
247
    TEST_STACK_START(stack_start_protected, stack_start_actual);
 
248
}
 
249
 
 
250
#endif /* TST_EXCLUDE_THIS */