Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zvmem.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* "Virtual memory" operators */
18
#include "stat_.h" /* get system header early to avoid name clash on Cygwin */
19
#include "ghost.h"
20
#include "gsstruct.h"
21
#include "oper.h"
22
#include "estack.h"   /* for checking in restore */
23
#include "ialloc.h"
24
#include "idict.h"    /* ditto */
25
#include "igstate.h"
26
#include "isave.h"
27
#include "dstack.h"
28
#include "stream.h"   /* for files.h */
29
#include "files.h"    /* for e-stack processing */
30
#include "store.h"
31
#include "gsmatrix.h"   /* for gsstate.h */
32
#include "gsstate.h"
33
34
/* Define whether we validate memory before/after save/restore. */
35
/* Note that we only actually do this if DEBUG is set and -Z? is selected. */
36
static const bool I_VALIDATE_BEFORE_SAVE = true;
37
static const bool I_VALIDATE_AFTER_SAVE = true;
38
static const bool I_VALIDATE_BEFORE_RESTORE = true;
39
static const bool I_VALIDATE_AFTER_RESTORE = true;
40
41
gs_private_st_ptrs1(st_vm_save, vm_save_t, "savetype",
42
                    vm_save_enum_ptrs, vm_save_reloc_ptrs, gsave);
43
44
/* Clean up the stacks and validate storage. */
45
void
46
ivalidate_clean_spaces(i_ctx_t *i_ctx_p)
47
2.81M
{
48
2.81M
    if (gs_debug_c('?')) {
49
0
        ref_stack_cleanup(&d_stack);
50
0
        ref_stack_cleanup(&e_stack);
51
0
        ref_stack_cleanup(&o_stack);
52
0
        ivalidate_spaces();
53
0
    }
54
2.81M
}
55
56
/* - save <save> */
57
int
58
zsave(i_ctx_t *i_ctx_p)
59
1.23M
{
60
1.23M
    os_ptr op = osp;
61
1.23M
    uint space = icurrent_space;
62
1.23M
    vm_save_t *vmsave;
63
1.23M
    ulong sid;
64
1.23M
    int code;
65
1.23M
    gs_gstate *prev;
66
67
1.23M
    if (I_VALIDATE_BEFORE_SAVE)
68
1.23M
        ivalidate_clean_spaces(i_ctx_p);
69
1.23M
    ialloc_set_space(idmemory, avm_local);
70
1.23M
    vmsave = ialloc_struct(vm_save_t, &st_vm_save, "zsave");
71
1.23M
    ialloc_set_space(idmemory, space);
72
1.23M
    if (vmsave == 0)
73
0
        return_error(gs_error_VMerror);
74
1.23M
    vmsave->gsave = NULL; /* Ensure constructed enough to destroy safely */
75
1.23M
    code = alloc_save_state(idmemory, vmsave, &sid);
76
77
1.23M
    if (code < 0 || sid == 0) {
78
0
        ifree_object(vmsave, "zsave");
79
0
        if (code < 0)
80
0
            return code;
81
0
        else
82
0
            return_error(gs_error_VMerror);
83
0
    }
84
1.23M
    if_debug2m('u', imemory, "[u]vmsave "PRI_INTPTR", id = %lu\n",
85
1.23M
               (intptr_t) vmsave, (ulong) sid);
86
1.23M
    code = gs_gsave_for_save(igs, &prev);
87
1.23M
    if (code < 0) {
88
3
        alloc_save_t *asave;
89
3
        int code2;
90
        /* dorestore() pops the restore operand off the stack,
91
           despite dorestore() actually having the save state
92
           passed to it as a C function parameter. So push a
93
           sacrificial object.
94
         */
95
3
        push(1);
96
3
        make_null(op);
97
        /* We use dorestore() to discard the save state we
98
           created above.
99
         */
100
3
        asave = alloc_find_save(idmemory, sid);
101
3
        code2 = dorestore(i_ctx_p, asave);
102
3
        if (code2 < 0) /* shouldn't happen! */
103
0
            return_error(gs_error_Fatal);
104
3
        return code;
105
3
    }
106
1.23M
    vmsave->gsave = prev;
107
1.23M
    push(1);
108
1.23M
    make_tav(op, t_save, 0, saveid, sid);
109
1.23M
    if (I_VALIDATE_AFTER_SAVE)
110
1.23M
        ivalidate_clean_spaces(i_ctx_p);
111
1.23M
    return 0;
112
1.23M
}
113
114
/* <save> restore - */
115
static int restore_check_operand(i_ctx_t *i_ctx_p, alloc_save_t **, gs_dual_memory_t *);
116
static int restore_check_stack(const i_ctx_t *i_ctx_p, const ref_stack_t *, const alloc_save_t *, bool);
117
static void restore_fix_stack(i_ctx_t *i_ctx_p, ref_stack_t *, const alloc_save_t *, bool);
118
119
/* Do as many up front checks of the save object as we reasonably can */
120
int
121
restore_check_save(i_ctx_t *i_ctx_p, alloc_save_t **asave)
122
221k
{
123
221k
    int code = restore_check_operand(i_ctx_p, asave, idmemory);
124
125
221k
    if (code < 0)
126
128
        return code;
127
221k
    if_debug2m('u', imemory, "[u]vmrestore "PRI_INTPTR", id = %lu\n",
128
221k
               (intptr_t) alloc_save_client_data(*asave),
129
221k
               (ulong) osp->value.saveid);
130
221k
    if (I_VALIDATE_BEFORE_RESTORE)
131
221k
        ivalidate_clean_spaces(i_ctx_p);
132
    /* Check the contents of the stacks. */
133
221k
    osp--;
134
221k
    {
135
221k
        int code;
136
137
221k
        if ((code = restore_check_stack(i_ctx_p, &o_stack, *asave, false)) < 0 ||
138
221k
            (code = restore_check_stack(i_ctx_p, &e_stack, *asave, true)) < 0 ||
139
221k
            (code = restore_check_stack(i_ctx_p, &d_stack, *asave, false)) < 0
140
221k
            ) {
141
35
            osp++;
142
35
            return code;
143
35
        }
144
221k
    }
145
221k
    osp++;
146
221k
    return 0;
147
221k
}
148
149
/* the semantics of restore differ slightly between Level 1 and
150
   Level 2 and later - the latter includes restoring the device
151
   state (whilst Level 1 didn't have "page devices" as such).
152
   Hence we have two restore operators - one here (Level 1)
153
   and one in zdevice2.c (Level 2+). For that reason, the
154
   operand checking and guts of the restore operation are
155
   separated so both implementations can use them to best
156
   effect.
157
 */
158
int
159
dorestore(i_ctx_t *i_ctx_p, alloc_save_t *asave)
160
124k
{
161
124k
    os_ptr op = osp;
162
124k
    bool last;
163
124k
    vm_save_t *vmsave;
164
124k
    int code;
165
166
124k
    check_op(1);
167
124k
    osp--;
168
169
    /* Reset l_new in all stack entries if the new save level is zero. */
170
    /* Also do some special fixing on the e-stack. */
171
124k
    restore_fix_stack(i_ctx_p, &o_stack, asave, false);
172
124k
    restore_fix_stack(i_ctx_p, &e_stack, asave, true);
173
124k
    restore_fix_stack(i_ctx_p, &d_stack, asave, false);
174
    /* Iteratively restore the state of memory, */
175
    /* also doing a grestoreall at each step. */
176
124k
    do {
177
124k
        vmsave = alloc_save_client_data(alloc_save_current(idmemory));
178
        /* Restore the graphics state. */
179
        /* The only time vmsave->gsave should be NULL is if we are
180
           cleaning up after a VMerror during a save operation.
181
         */
182
124k
        if (vmsave->gsave != NULL)
183
124k
            gs_grestoreall_for_restore(igs, vmsave->gsave);
184
        /*
185
         * If alloc_save_space decided to do a second save, the vmsave
186
         * object was allocated one save level less deep than the
187
         * current level, so ifree_object won't actually free it;
188
         * however, it points to a gsave object that definitely
189
         * *has* been freed.  In order not to trip up the garbage
190
         * collector, we clear the gsave pointer now.
191
         */
192
124k
        vmsave->gsave = 0;
193
        /* Now it's safe to restore the state of memory. */
194
124k
        code = alloc_restore_step_in(idmemory, asave);
195
124k
        if (code < 0)
196
0
            return code;
197
124k
        last = code;
198
124k
    }
199
124k
    while (!last);
200
124k
    {
201
124k
        uint space = icurrent_space;
202
203
124k
        ialloc_set_space(idmemory, avm_local);
204
124k
        ifree_object(vmsave, "zrestore");
205
124k
        ialloc_set_space(idmemory, space);
206
124k
    }
207
124k
    dict_set_top();   /* reload dict stack cache */
208
124k
    if (I_VALIDATE_AFTER_RESTORE)
209
124k
        ivalidate_clean_spaces(i_ctx_p);
210
    /* If the i_ctx_p LockFilePermissions is true, but the userparams */
211
    /* we just restored is false, we need to make sure that we do not */
212
    /* cause an 'invalidaccess' in setuserparams. Temporarily set     */
213
    /* LockFilePermissions false until the gs_lev2.ps can do a        */
214
    /* setuserparams from the restored userparam dictionary.          */
215
    /* NOTE: This is safe to do here, since the restore has           */
216
    /* successfully completed - this should never come before any     */
217
    /* operation that can trigger an error                            */
218
124k
    i_ctx_p->LockFilePermissions = false;
219
124k
    return 0;
220
124k
}
221
222
int
223
zrestore(i_ctx_t *i_ctx_p)
224
0
{
225
0
    alloc_save_t *asave;
226
0
    int code = restore_check_save(i_ctx_p, &asave);
227
0
    if (code < 0)
228
0
        return code;
229
230
0
    return dorestore(i_ctx_p, asave);
231
0
}
232
233
/* Check the operand of a restore. */
234
static int
235
restore_check_operand(i_ctx_t *i_ctx_p, alloc_save_t ** pasave,
236
                      gs_dual_memory_t *idmem)
237
221k
{
238
221k
    os_ptr op = osp;
239
221k
    vm_save_t *vmsave;
240
221k
    ulong sid;
241
221k
    alloc_save_t *asave;
242
243
221k
    check_op(1);
244
221k
    *pasave = NULL;
245
221k
    check_type(*op, t_save);
246
221k
    vmsave = r_ptr(op, vm_save_t);
247
221k
    if (vmsave == 0)    /* invalidated save */
248
0
        return_error(gs_error_invalidrestore);
249
221k
    sid = op->value.saveid;
250
221k
    asave = alloc_find_save(idmem, sid);
251
221k
    if (asave == 0)
252
0
        return_error(gs_error_invalidrestore);
253
221k
    *pasave = asave;
254
221k
    return 0;
255
221k
}
256
257
/* Check a stack to make sure all its elements are older than a save. */
258
static int
259
restore_check_stack(const i_ctx_t *i_ctx_p, const ref_stack_t * pstack,
260
                    const alloc_save_t * asave, bool is_estack)
261
663k
{
262
663k
    ref_stack_enum_t rsenum;
263
264
663k
    ref_stack_enum_begin(&rsenum, pstack);
265
663k
    do {
266
663k
        const ref *stkp = rsenum.ptr;
267
663k
        uint size = rsenum.size;
268
269
18.9M
        for (; size; stkp++, size--) {
270
18.3M
            const void *ptr;
271
272
18.3M
            switch (r_type(stkp)) {
273
2.09k
                case t_array:
274
                    /*
275
                     * Zero-length arrays are a special case: see the
276
                     * t_*array case (label rr:) in igc.c:gc_trace.
277
                     */
278
2.09k
                    if (r_size(stkp) == 0) {
279
                        /*stkp->value.refs = (void *)0;*/
280
553
                        continue;
281
553
                    }
282
1.53k
                    ptr = stkp->value.refs;
283
1.53k
                    break;
284
843k
                case t_dictionary:
285
843k
                    ptr = stkp->value.pdict;
286
843k
                    break;
287
882k
                case t_file:
288
                    /* Don't check executable or closed literal */
289
                    /* files on the e-stack. */
290
882k
                    {
291
882k
                        stream *s;
292
293
882k
                        if (is_estack &&
294
535k
                            (r_has_attr(stkp, a_executable) ||
295
267k
                             file_is_invalid(s, stkp))
296
882k
                            )
297
488k
                            continue;
298
882k
                    }
299
393k
                    ptr = stkp->value.pfile;
300
393k
                    break;
301
327k
                case t_name:
302
                    /* Names are special because of how they are allocated. */
303
327k
                    if (alloc_name_is_since_save((const gs_memory_t *)pstack->memory,
304
327k
                                                 stkp, asave))
305
0
                        return_error(gs_error_invalidrestore);
306
327k
                    continue;
307
327k
                case t_string:
308
                    /* Don't check empty executable strings */
309
                    /* on the e-stack. */
310
174k
                    if (r_size(stkp) == 0 &&
311
299
                        r_has_attr(stkp, a_executable) && is_estack
312
174k
                        )
313
0
                        continue;
314
174k
                    ptr = stkp->value.bytes;
315
174k
                    break;
316
1.81M
                case t_mixedarray:
317
2.13M
                case t_shortarray:
318
                    /* See the t_array case above. */
319
2.13M
                    if (r_size(stkp) == 0) {
320
                        /*stkp->value.packed = (void *)0;*/
321
0
                        continue;
322
0
                    }
323
2.13M
                    ptr = stkp->value.packed;
324
2.13M
                    break;
325
0
                case t_device:
326
0
                    ptr = stkp->value.pdevice;
327
0
                    break;
328
0
                case t_fontID:
329
0
                case t_struct:
330
0
                case t_astruct:
331
0
                case t_pdfctx:
332
0
                    ptr = stkp->value.pstruct;
333
0
                    break;
334
1.71M
                case t_save:
335
                    /* See the comment in isave.h regarding the following. */
336
1.71M
                    if (i_ctx_p->language_level <= 2)
337
0
                        continue;
338
1.71M
                    ptr = alloc_find_save(&gs_imemory, stkp->value.saveid);
339
                    /*
340
                     * Invalid save objects aren't supposed to be possible
341
                     * in LL3, but just in case....
342
                     */
343
1.71M
                    if (ptr == 0)
344
0
                        return_error(gs_error_invalidrestore);
345
1.71M
                    if (ptr == asave)
346
318k
                        continue;
347
1.40M
                    break;
348
12.2M
                default:
349
12.2M
                    continue;
350
18.3M
            }
351
4.95M
            if (alloc_is_since_save(ptr, asave))
352
35
                return_error(gs_error_invalidrestore);
353
4.95M
        }
354
663k
    } while (ref_stack_enum_next(&rsenum));
355
663k
    return 0;   /* OK */
356
663k
}
357
/*
358
 * If the new save level is zero, fix up the contents of a stack
359
 * by clearing the l_new bit in all the entries (since we can't tolerate
360
 * values with l_new set if the save level is zero).
361
 * Also, in any case, fix up the e-stack by replacing empty executable
362
 * strings and closed executable files that are newer than the save
363
 * with canonical ones that aren't.
364
 *
365
 * Note that this procedure is only called if restore_check_stack succeeded.
366
 */
367
static void
368
restore_fix_stack(i_ctx_t *i_ctx_p, ref_stack_t * pstack,
369
                  const alloc_save_t * asave, bool is_estack)
370
372k
{
371
372k
    ref_stack_enum_t rsenum;
372
373
372k
    ref_stack_enum_begin(&rsenum, pstack);
374
372k
    do {
375
372k
        ref *stkp = rsenum.ptr;
376
372k
        uint size = rsenum.size;
377
378
11.1M
        for (; size; stkp++, size--) {
379
10.7M
            r_clear_attrs(stkp, l_new);    /* always do it, no harm */
380
10.7M
            if (is_estack) {
381
8.14M
                ref ofile;
382
383
8.14M
                ref_assign(&ofile, stkp);
384
8.14M
                switch (r_type(stkp)) {
385
0
                    case t_string:
386
0
                        if (r_size(stkp) == 0 &&
387
0
                            alloc_is_since_save(stkp->value.bytes,
388
0
                                                asave)
389
0
                            ) {
390
0
                            make_empty_const_string(stkp,
391
0
                                                    avm_foreign);
392
0
                            break;
393
0
                        }
394
0
                        continue;
395
296k
                    case t_file:
396
296k
                        if (alloc_is_since_save(stkp->value.pfile,
397
296k
                                                asave)
398
296k
                            ) {
399
0
                            make_invalid_file(i_ctx_p, stkp);
400
0
                            break;
401
0
                        }
402
296k
                        continue;
403
7.84M
                    default:
404
7.84M
                        continue;
405
8.14M
                }
406
0
                r_copy_attrs(stkp, a_all | a_executable,
407
0
                             &ofile);
408
0
            }
409
10.7M
        }
410
372k
    } while (ref_stack_enum_next(&rsenum));
411
372k
}
412
413
/* - vmstatus <save_level> <vm_used> <vm_maximum> */
414
static int
415
zvmstatus(i_ctx_t *i_ctx_p)
416
2.31M
{
417
2.31M
    os_ptr op = osp;
418
2.31M
    gs_memory_status_t mstat, dstat;
419
420
2.31M
    gs_memory_status(imemory, &mstat);
421
2.31M
    if (imemory == imemory_global) {
422
1.33M
        gs_memory_status_t sstat;
423
424
1.33M
        gs_memory_status(imemory_system, &sstat);
425
1.33M
        mstat.allocated += sstat.allocated;
426
1.33M
        mstat.used += sstat.used;
427
1.33M
    }
428
2.31M
    gs_memory_status(imemory->non_gc_memory, &dstat);
429
2.31M
    push(3);
430
2.31M
    make_int(op - 2, imemory_save_level(iimemory_local));
431
2.31M
    make_int(op - 1, mstat.used);
432
2.31M
    make_int(op, mstat.allocated + dstat.allocated - dstat.used);
433
2.31M
    return 0;
434
2.31M
}
435
436
/* ------ Initialization procedure ------ */
437
438
const op_def zvmem_op_defs[] =
439
{
440
    {"1restore", zrestore},
441
    {"0save", zsave},
442
    {"0vmstatus", zvmstatus},
443
    op_def_end(0)
444
};