Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/icontext.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
/* Context state operations */
18
#include "ghost.h"
19
#include "gsstruct.h"   /* for gxalloc.h */
20
#include "gxalloc.h"
21
#include "ierrors.h"
22
#include "stream.h"   /* for files.h */
23
#include "files.h"
24
#include "idict.h"
25
#include "igstate.h"
26
#include "icontext.h"
27
#include "interp.h"
28
#include "isave.h"
29
#include "dstack.h"
30
#include "estack.h"
31
#include "store.h"
32
33
/* Declare the GC descriptors for embedded objects. */
34
extern_st(st_gs_dual_memory);
35
extern_st(st_ref_stack);
36
extern_st(st_dict_stack);
37
extern_st(st_exec_stack);
38
extern_st(st_op_stack);
39
40
/* GC descriptors */
41
static
42
CLEAR_MARKS_PROC(context_state_clear_marks)
43
342k
{
44
342k
    gs_context_state_t *const pcst = vptr;
45
46
342k
    r_clear_attrs(&pcst->stdio[0], l_mark);
47
342k
    r_clear_attrs(&pcst->stdio[1], l_mark);
48
342k
    r_clear_attrs(&pcst->stdio[2], l_mark);
49
342k
    r_clear_attrs(&pcst->error_object, l_mark);
50
342k
    r_clear_attrs(&pcst->userparams, l_mark);
51
342k
    r_clear_attrs(&pcst->LocalFontDirectory, l_mark);
52
342k
    r_clear_attrs(&pcst->GlobalFontDirectory, l_mark);
53
342k
    r_clear_attrs(&pcst->internaldict, l_mark);
54
342k
    r_clear_attrs(&pcst->op_array_table_global.table, l_mark);
55
342k
    r_clear_attrs(&pcst->op_array_table_local.table, l_mark);
56
342k
}
57
static
58
14.3M
ENUM_PTRS_WITH(context_state_enum_ptrs, gs_context_state_t *pcst) {
59
4.79M
    index -= 14;
60
4.79M
    if (index < st_gs_dual_memory_num_ptrs)
61
0
        return ENUM_USING(st_gs_dual_memory, &pcst->memory,
62
4.79M
                          sizeof(pcst->memory), index);
63
4.79M
    index -= st_gs_dual_memory_num_ptrs;
64
4.79M
    if (index < st_dict_stack_num_ptrs)
65
1.37M
        return ENUM_USING(st_dict_stack, &pcst->dict_stack,
66
4.79M
                          sizeof(pcst->dict_stack), index);
67
3.42M
    index -= st_dict_stack_num_ptrs;
68
3.42M
    if (index < st_exec_stack_num_ptrs)
69
1.37M
        return ENUM_USING(st_exec_stack, &pcst->exec_stack,
70
3.42M
                          sizeof(pcst->exec_stack), index);
71
2.05M
    index -= st_exec_stack_num_ptrs;
72
2.05M
    return ENUM_USING(st_op_stack, &pcst->op_stack,
73
3.42M
                      sizeof(pcst->op_stack), index);
74
3.42M
    }
75
3.42M
    ENUM_PTR(0, gs_context_state_t, pgs);
76
685k
    case 1: ENUM_RETURN_REF(&pcst->stdio[0]);
77
685k
    case 2: ENUM_RETURN_REF(&pcst->stdio[1]);
78
685k
    case 3: ENUM_RETURN_REF(&pcst->stdio[2]);
79
685k
    case 4: ENUM_RETURN_REF(&pcst->error_object);
80
3.42M
    ENUM_PTR(5, gs_context_state_t, invalid_file_stream);
81
685k
    case 6: ENUM_RETURN_REF(&pcst->userparams);
82
685k
    case 7: ENUM_RETURN_REF(&pcst->internaldict);
83
685k
    case 8: ENUM_RETURN_REF(&pcst->LocalFontDirectory);
84
685k
    case 9: ENUM_RETURN_REF(&pcst->GlobalFontDirectory);
85
3.42M
    ENUM_PTR(10, gs_context_state_t, op_array_table_global.nx_table);
86
3.42M
    ENUM_PTR(11, gs_context_state_t, op_array_table_local.nx_table);
87
685k
    case 12: ENUM_RETURN_REF(&pcst->op_array_table_global.table);
88
685k
    case 13: ENUM_RETURN_REF(&pcst->op_array_table_local.table);
89
14.3M
ENUM_PTRS_END
90
342k
static RELOC_PTRS_WITH(context_state_reloc_ptrs, gs_context_state_t *pcst);
91
342k
    RELOC_PTR(gs_context_state_t, pgs);
92
342k
    RELOC_USING(st_gs_dual_memory, &pcst->memory, sizeof(pcst->memory));
93
    /******* WHY DON'T WE CLEAR THE l_mark OF stdio? ******/
94
342k
    RELOC_REF_VAR(pcst->stdio[0]);
95
342k
    RELOC_REF_VAR(pcst->stdio[1]);
96
342k
    RELOC_REF_VAR(pcst->stdio[2]);
97
342k
    RELOC_PTR(gs_context_state_t, invalid_file_stream);
98
342k
    RELOC_REF_VAR(pcst->error_object);
99
342k
    r_clear_attrs(&pcst->error_object, l_mark);
100
342k
    RELOC_REF_VAR(pcst->userparams);
101
342k
    r_clear_attrs(&pcst->userparams, l_mark);
102
342k
    RELOC_REF_VAR(pcst->internaldict);
103
342k
    r_clear_attrs(&pcst->internaldict, l_mark);
104
105
342k
    RELOC_REF_VAR(pcst->LocalFontDirectory);
106
342k
    r_clear_attrs(&pcst->LocalFontDirectory, l_mark);
107
342k
    RELOC_REF_VAR(pcst->GlobalFontDirectory);
108
342k
    r_clear_attrs(&pcst->GlobalFontDirectory, l_mark);
109
110
342k
    RELOC_PTR(gs_context_state_t, op_array_table_global.nx_table);
111
342k
    RELOC_PTR(gs_context_state_t, op_array_table_local.nx_table);
112
342k
    RELOC_REF_VAR(pcst->op_array_table_global.table);
113
342k
    r_clear_attrs(&pcst->op_array_table_global.table, l_mark);
114
342k
    RELOC_REF_VAR(pcst->op_array_table_local.table);
115
342k
    r_clear_attrs(&pcst->op_array_table_local.table, l_mark);
116
342k
    RELOC_USING(st_dict_stack, &pcst->dict_stack, sizeof(pcst->dict_stack));
117
342k
    RELOC_USING(st_exec_stack, &pcst->exec_stack, sizeof(pcst->exec_stack));
118
342k
    RELOC_USING(st_op_stack, &pcst->op_stack, sizeof(pcst->op_stack));
119
342k
RELOC_PTRS_END
120
public_st_context_state();
121
122
/* Allocate the state of a context. */
123
int
124
context_state_alloc(gs_context_state_t ** ppcst,
125
                    const ref *psystem_dict,
126
                    const gs_dual_memory_t * dmem)
127
166k
{
128
166k
    gs_ref_memory_t *mem = dmem->space_local;
129
166k
    gs_context_state_t *pcst = *ppcst;
130
166k
    int code;
131
166k
    int i;
132
133
166k
    if (pcst == 0) {
134
166k
        pcst = gs_alloc_struct((gs_memory_t *) mem, gs_context_state_t,
135
166k
                               &st_context_state, "context_state_alloc");
136
166k
        if (pcst == 0)
137
0
            return_error(gs_error_VMerror);
138
166k
    }
139
    /* Make sure pcst->memory is valid, in case we return an error. */
140
166k
    pcst->memory = *dmem;
141
166k
    code = gs_interp_alloc_stacks(mem, pcst);
142
166k
    if (code < 0)
143
0
        goto x0;
144
    /*
145
     * We have to initialize the dictionary stack early,
146
     * for far-off references to systemdict.
147
     */
148
166k
    pcst->dict_stack.system_dict = *psystem_dict;
149
166k
    pcst->dict_stack.min_size = 0;
150
166k
    pcst->dict_stack.userdict_index = 0;
151
166k
    pcst->pgs = int_gstate_alloc(dmem);
152
166k
    if (pcst->pgs == 0) {
153
0
        code = gs_note_error(gs_error_VMerror);
154
0
        goto x1;
155
0
    }
156
166k
    pcst->language_level = 1;
157
166k
    make_false(&pcst->array_packing);
158
166k
    make_int(&pcst->binary_object_format, 0);
159
166k
    pcst->nv_page_count = 0;
160
166k
    pcst->rand_state = rand_state_initial;
161
166k
    pcst->usertime_inited = false;
162
166k
    pcst->plugin_list = 0;
163
166k
    make_t(&pcst->error_object, t__invalid);
164
165
    /* The following get set properly in obj_init(), but
166
       init them so the garbager doesn't barf in the event
167
       of a error during initialisation
168
     */
169
166k
    make_t(&pcst->LocalFontDirectory, t__invalid);
170
166k
    make_t(&pcst->GlobalFontDirectory, t__invalid);
171
172
166k
    { /*
173
         * Create an empty userparams dictionary of the right size.
174
         * If we can't determine the size, pick an arbitrary one.
175
         */
176
166k
        ref *puserparams;
177
166k
        uint size;
178
166k
        ref *system_dict = &pcst->dict_stack.system_dict;
179
180
166k
        if (dict_find_string(system_dict, "userparams", &puserparams) > 0)
181
0
            size = dict_length(puserparams);
182
166k
        else
183
166k
            size = 300;
184
166k
        code = dict_alloc(pcst->memory.space_local, size, &pcst->userparams);
185
166k
        if (code < 0)
186
0
            goto x2;
187
        /* PostScript code initializes the user parameters. */
188
166k
    }
189
166k
    { /*
190
         * Create an empty internaldict dictionary.
191
         */
192
166k
        code = dict_alloc(pcst->memory.space_local, 13, &pcst->internaldict);
193
166k
        if (code < 0)
194
0
            goto x2;
195
        /* PostScript code initializes the user parameters. */
196
166k
    }
197
166k
    pcst->scanner_options = 0;
198
166k
    pcst->LockFilePermissions = false;
199
166k
    pcst->RenderTTNotdef = true;
200
201
166k
    pcst->invalid_file_stream = (stream*)gs_alloc_struct_immovable(mem->stable_memory,
202
166k
                                          stream, &st_stream,
203
166k
                                          "context_state_alloc");
204
166k
    if (pcst->invalid_file_stream == NULL) {
205
0
        code = gs_note_error(gs_error_VMerror);
206
0
        goto x3;
207
0
    }
208
209
166k
    s_init(pcst->invalid_file_stream, mem->stable_memory);
210
166k
    sread_string(pcst->invalid_file_stream, NULL, 0);
211
166k
    s_init_no_id(pcst->invalid_file_stream);
212
213
    /* The initial stdio values are bogus.... */
214
166k
    make_file(&pcst->stdio[0], a_readonly | avm_invalid_file_entry, 1,
215
166k
              pcst->invalid_file_stream);
216
166k
    make_file(&pcst->stdio[1], a_all | avm_invalid_file_entry, 1,
217
166k
              pcst->invalid_file_stream);
218
166k
    make_file(&pcst->stdio[2], a_all | avm_invalid_file_entry, 1,
219
166k
              pcst->invalid_file_stream);
220
833k
    for (i = countof(pcst->memory.spaces_indexed); --i >= 0;)
221
666k
        if (dmem->spaces_indexed[i] != 0)
222
499k
            ++(dmem->spaces_indexed[i]->num_contexts);
223
    /* The number of interpreter "ticks" between calls on the time_slice_proc.
224
     * Currently, the clock ticks before each operator, and at each
225
     * procedure return. */
226
166k
    pcst->time_slice_ticks = 0x7fff;
227
166k
    *ppcst = pcst;
228
229
166k
    pcst->system_params.CurDisplayList = 0;
230
166k
    pcst->system_params.CurFormCache = 0;
231
166k
    pcst->system_params.CurOutlineCache = 0;
232
166k
    pcst->system_params.CurPatternCache = 0;
233
166k
    pcst->system_params.CurScreenStorage = 0;
234
166k
    pcst->system_params.CurSourceList = 0;
235
166k
    pcst->system_params.CurStoredScreenCache = 0;
236
166k
    pcst->system_params.CurUPathCache = 0;
237
166k
    pcst->system_params.MaxDisplayAndSourceList = 140000;
238
166k
    pcst->system_params.MaxDisplayList = 140000;
239
166k
    pcst->system_params.MaxFormCache = 100000;
240
166k
    pcst->system_params.MaxImageBuffer = 524288;
241
166k
    pcst->system_params.MaxOutlineCache = 65000;
242
166k
    pcst->system_params.MaxPatternCache = 100000;
243
166k
    pcst->system_params.MaxUPathCache = 300000;
244
166k
    pcst->system_params.MaxScreenStorage = 84000;
245
166k
    pcst->system_params.MaxSourceList = 25000;
246
166k
    pcst->system_params.RamSize = 4194304;
247
166k
    pcst->system_params.JobTimeout = 0;
248
166k
    pcst->system_params.WaitTimeout = 40;
249
166k
    pcst->system_params.GenericResourceDir = NULL;
250
166k
    pcst->system_params.FontResourceDir = NULL;
251
166k
    pcst->system_params.GenericResourcePathSep = NULL;
252
166k
    pcst->system_params.PercentDiskFontResourceDir = NULL;
253
166k
    pcst->system_params.PercentDiskGenericResourceDir = NULL;
254
166k
    pcst->system_params.SystemParamsPassword = NULL;
255
166k
    pcst->system_params.StartJobPassword = NULL;
256
257
166k
    pcst->user_params.MaxFormItem = 100000;
258
166k
    pcst->user_params.MaxPatternItem = 20000;
259
166k
    pcst->user_params.MaxScreenItem = 48000;
260
166k
    pcst->user_params.MaxUPathItem = 0;
261
166k
    pcst->user_params.MaxSuperScreen = 1016;
262
166k
    pcst->user_params.JobName = NULL;
263
166k
    pcst->user_params.IdiomRecognition = false;
264
166k
    pcst->user_params.HalftoneMode = 0;
265
266
166k
    return 0;
267
0
  x3:/* No need to delete dictionary here, as gc will do it for us. */
268
0
  x2:gs_gstate_free(pcst->pgs);
269
0
  x1:gs_interp_free_stacks(mem, pcst);
270
0
  x0:if (*ppcst == 0)
271
0
        gs_free_object((gs_memory_t *) mem, pcst, "context_state_alloc");
272
0
    return code;
273
0
}
274
275
/* Load the interpreter state from a context. */
276
int
277
context_state_load(gs_context_state_t * i_ctx_p)
278
509k
{
279
509k
    gs_ref_memory_t *lmem = iimemory_local;
280
509k
    ref *system_dict = systemdict;
281
509k
    uint space = r_space(system_dict);
282
509k
    dict_stack_t *dstack = &idict_stack;
283
509k
    int code;
284
285
    /*
286
     * Disable save checking, and space check for systemdict, while
287
     * copying dictionaries.
288
     */
289
509k
    alloc_set_not_in_save(idmemory);
290
509k
    r_set_space(system_dict, avm_max);
291
    /*
292
     * Set systemdict.userparams to the saved copy, and then
293
     * set the actual user parameters.  Note that we must disable both
294
     * space checking and save checking while doing this.  Also,
295
     * we must do this after copying localdicts (if required), because
296
     * userparams also appears in localdicts.
297
     */
298
509k
    code = dict_put_string(system_dict, "userparams", &i_ctx_p->userparams,
299
509k
                           dstack);
300
509k
    if (code >= 0)
301
509k
        code = set_user_params(i_ctx_p, &i_ctx_p->userparams);
302
509k
    r_set_space(system_dict, space);
303
509k
    if (lmem->save_level > 0)
304
176k
        alloc_set_in_save(idmemory);
305
509k
    estack_clear_cache(&iexec_stack);
306
509k
    dstack_set_top(&idict_stack);
307
509k
    return code;
308
509k
}
309
310
/* Store the interpreter state in a context. */
311
int
312
context_state_store(gs_context_state_t * pcst)
313
342k
{
314
342k
    ref_stack_cleanup(&pcst->dict_stack.stack);
315
342k
    ref_stack_cleanup(&pcst->exec_stack.stack);
316
342k
    ref_stack_cleanup(&pcst->op_stack.stack);
317
    /*
318
     * The user parameters in systemdict.userparams are kept
319
     * up to date by PostScript code, but we still need to save
320
     * systemdict.userparams to get the correct l_new flag.
321
     */
322
342k
    {
323
342k
        ref *puserparams;
324
        /* We need i_ctx_p for access to the d_stack. */
325
342k
        i_ctx_t *i_ctx_p = pcst;
326
327
342k
        if (dict_find_string(systemdict, "userparams", &puserparams) <= 0)
328
0
            return_error(gs_error_Fatal);
329
342k
        pcst->userparams = *puserparams;
330
342k
    }
331
0
    return 0;
332
342k
}
333
334
/* Free the contents of the state of a context, always to its local VM. */
335
/* Return a mask of which of its VMs, if any, we freed. */
336
int
337
context_state_free(gs_context_state_t * pcst)
338
0
{
339
0
    gs_ref_memory_t *mem = pcst->memory.space_local;
340
0
    int freed = 0;
341
0
    int i;
342
343
    /*
344
     * If this context is the last one referencing a particular VM
345
     * (local / global / system), free the entire VM space;
346
     * otherwise, just free the context-related structures.
347
     */
348
0
    for (i = countof(pcst->memory.spaces_indexed); --i >= 0;) {
349
0
        if (pcst->memory.spaces_indexed[i] != 0 &&
350
0
            !--(pcst->memory.spaces_indexed[i]->num_contexts)
351
0
            ) {
352
/****** FREE THE ENTIRE SPACE ******/
353
0
            freed |= 1 << i;
354
0
        }
355
0
    }
356
    /*
357
     * If we freed any spaces at all, we must have freed the local
358
     * VM where the context structure and its substructures were
359
     * allocated.
360
     */
361
0
    if (freed)
362
0
        return freed;
363
0
    {
364
0
        gs_gstate *pgs = pcst->pgs;
365
366
0
        gs_grestoreall(pgs);
367
        /* Patch the saved pointer so we can do the last grestore. */
368
0
        {
369
0
            gs_gstate *saved = gs_gstate_saved(pgs);
370
371
0
            gs_gstate_swap_saved(saved, saved);
372
0
        }
373
0
        gs_grestore(pgs);
374
0
        gs_gstate_swap_saved(pgs, (gs_gstate *) 0);
375
0
        gs_gstate_free(pgs);
376
0
    }
377
/****** FREE USERPARAMS ******/
378
0
    gs_interp_free_stacks(mem, pcst);
379
0
    return 0;
380
0
}