Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*-------------------------------------------------------------------------
 *
 * plperl.h
 *    Common include file for PL/Perl files
 *
 * This should be included _AFTER_ postgres.h and system include files, as
 * well as headers that could in turn include system headers.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1995, Regents of the University of California
 *
 * src/pl/plperl/plperl.h
 */
 
#ifndef PL_PERL_H
#define PL_PERL_H
 
/* defines free() by way of system headers, so must be included before perl.h */
#include "mb/pg_wchar.h"
 
/*
 * Pull in Perl headers via a wrapper header, to control the scope of
 * the system_header pragma therein.
 */
#include "plperl_system.h"
 
/* declare routines from plperl.c for access by .xs files */
HV         *plperl_spi_exec(char *, int);
void        plperl_return_next(SV *);
SV         *plperl_spi_query(char *);
SV         *plperl_spi_fetchrow(char *);
SV         *plperl_spi_prepare(char *, int, SV **);
HV         *plperl_spi_exec_prepared(char *, HV *, int, SV **);
SV         *plperl_spi_query_prepared(char *, int, SV **);
void        plperl_spi_freeplan(char *);
void        plperl_spi_cursor_close(char *);
void        plperl_spi_commit(void);
void        plperl_spi_rollback(void);
char       *plperl_sv_to_literal(SV *, char *);
void        plperl_util_elog(int level, SV *msg);
 
 
/* helper functions */
 
/*
 * convert from utf8 to database encoding
 *
 * Returns a palloc'ed copy of the original string
 */
static inline char *
utf_u2e(char *utf8_str, size_t len)
{
    char       *ret;
 
    ret = pg_any_to_server(utf8_str, len, PG_UTF8);
 
    /* ensure we have a copy even if no conversion happened */
    if (ret == utf8_str)
        ret = pstrdup(ret);
 
    return ret;
}
 
/*
 * convert from database encoding to utf8
 *
 * Returns a palloc'ed copy of the original string
 */
static inline char *
utf_e2u(const char *str)
{
    char       *ret;
 
    ret = pg_server_to_any(str, strlen(str), PG_UTF8);
 
    /* ensure we have a copy even if no conversion happened */
    if (ret == str)
        ret = pstrdup(ret);
 
    return ret;
}
 
/*
 * Convert an SV to a char * in the current database encoding
 *
 * Returns a palloc'ed copy of the original string
 */
static inline char *
sv2cstr(SV *sv)
{
    dTHX;
    char       *val,
               *res;
    STRLEN      len;
 
    /*
     * get a utf8 encoded char * out of perl. *note* it may not be valid utf8!
     */
 
    /*
     * SvPVutf8() croaks nastily on certain things, like typeglobs and
     * readonly objects such as $^V. That's a perl bug - it's not supposed to
     * happen. To avoid crashing the backend, we make a copy of the sv before
     * passing it to SvPVutf8(). The copy is garbage collected when we're done
     * with it.
     */
    if (SvREADONLY(sv) ||
        isGV_with_GP(sv) ||
        (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM))
        sv = newSVsv(sv);
    else
    {
        /*
         * increase the reference count so we can just SvREFCNT_dec() it when
         * we are done
         */
        SvREFCNT_inc_simple_void(sv);
    }
 
    /*
     * Request the string from Perl, in UTF-8 encoding; but if we're in a
     * SQL_ASCII database, just request the byte soup without trying to make
     * it UTF8, because that might fail.
     */
    if (GetDatabaseEncoding() == PG_SQL_ASCII)
        val = SvPV(sv, len);
    else
        val = SvPVutf8(sv, len);
 
    /*
     * Now convert to database encoding.  We use perl's length in the event we
     * had an embedded null byte to ensure we error out properly.
     */
    res = utf_u2e(val, len);
 
    /* safe now to garbage collect the new SV */
    SvREFCNT_dec(sv);
 
    return res;
}
 
/*
 * Create a new SV from a string assumed to be in the current database's
 * encoding.
 */
static inline SV *
cstr2sv(const char *str)
{
    dTHX;
    SV         *sv;
    char       *utf8_str;
 
    /* no conversion when SQL_ASCII */
    if (GetDatabaseEncoding() == PG_SQL_ASCII)
        return newSVpv(str, 0);
 
    utf8_str = utf_e2u(str);
 
    sv = newSVpv(utf8_str, 0);
    SvUTF8_on(sv);
    pfree(utf8_str);
 
    return sv;
}
 
/*
 * If the SV has get magic, run FETCH to convert it to the intended value.
 *
 * While Perl functions such as SvPV() will handle get magic automatically,
 * we must run this before primitive checks such as SvOK() or SvROK().
 * This must be invoked within the scope of a dTHX declaration.
 */
#define plperl_materialize_sv(sv) \
    do { if (sv) SvGETMAGIC(sv); } while(0)
 
/*
 * croak() with specified message, which is given in the database encoding.
 *
 * Ideally we'd just write croak("%s", str), but plain croak() does not play
 * nice with non-ASCII data.  In modern Perl versions we can call cstr2sv()
 * and pass the result to croak_sv(); in versions that don't have croak_sv(),
 * we have to work harder.
 */
static inline void
croak_cstr(const char *str)
{
    dTHX;
 
#ifdef croak_sv
    /* Use sv_2mortal() to be sure the transient SV gets freed */
    croak_sv(sv_2mortal(cstr2sv(str)));
#else
 
    /*
     * The older way to do this is to assign a UTF8-marked value to ERRSV and
     * then call croak(NULL).  But if we leave it to croak() to append the
     * error location, it does so too late (only after popping the stack) in
     * some Perl versions.  Hence, use mess() to create an SV with the error
     * location info already appended.
     */
    SV         *errsv = get_sv("@", GV_ADD);
    char       *utf8_str = utf_e2u(str);
    SV         *ssv;
 
    ssv = mess("%s", utf8_str);
    SvUTF8_on(ssv);
 
    pfree(utf8_str);
 
    sv_setsv(errsv, ssv);
 
    croak(NULL);
#endif                          /* croak_sv */
}
 
/*
 * Test whether ERRSV indicates that Perl threw an error.
 *
 * The traditional way to write this is SvTRUE(ERRSV), but that is unsafe.
 * If the error is an object whose class overloads "bool", SvTRUE invokes
 * that user-defined Perl code, which is free to throw an error of its own.
 * Since we typically ask this question while unwinding from an error, such
 * a nested throw tends to land right back where we started, giving an
 * infinite loop.  Avoid the problem by not asking Perl at all in the case
 * that matters: any reference is true, whatever its class may claim.  Only
 * non-references need the full test, and overloading cannot apply to those.
 */
static inline bool
errsv_is_true(SV *errsv)
{
    dTHX;
 
    if (!errsv)
        return false;
    if (SvROK(errsv))
        return true;
    return SvTRUE(errsv);
}
 
/*
 * These macros provide a version of the PG_TRY facility that can catch
 * Perl's longjmps as well as our own.  Use these, not plain PG_TRY, in any
 * code that can invoke Perl functions, to ensure that the two jmp_buf stacks
 * remain consistent.  Without this, any Perl function that is called directly
 * (rather than via a runops loop such as call_sv's) will wreak havoc if it
 * throws an error.
 *
 * These must be invoked within the scope of a dTHX declaration.  That
 * declaration controls which interpreter we manipulate the jmp_buf stack of.
 *
 * Like PG_TRY, we accept a __VA_ARGS__ string to append to local variable
 * names, but that's of limited use since we can't modify the "cur_env" name
 * declared by dJMPENV; so you can't nest these without drawing compiler
 * warnings about shadowed variables.  However, it is helpful in some places
 * to nest PG_PERL_TRY within PG_TRY, so we make sure not to conflict with
 * the names that PG_TRY declares.  (Beware of nesting them too closely,
 * for fear of breaking clang.  It does seem to work so long as there's a
 * nontrivial amount of code in between.)
 *
 * Since nothing in life is easy, we need two implementations of this idea.
 * They are equivalent from the outside.
 */
 
#if !(defined(WIN32) && defined(__MINGW64__))
 
/*
 * This is our preferred implementation, which we use everywhere but MinGW-64.
 * It depends on the assumption that Perl is built with the same sigsetjmp()
 * infrastructure that we are.
 *
 * A key design choice here is to use just one sigsetjmp() call and link
 * that jmp_buf structure into both jmp_buf stacks.  That requires being more
 * intimate with both error mechanisms than one could wish, but using two
 * sigsetjmp() calls is expensive, and more importantly it completely breaks
 * clang's optimizer, so much so that clang generates buggy code even at -O0.
 * To provide at least a fig-leaf of separation from Perl innards, we use
 * JMPENV_PUSH/JMPENV_POP to manipulate their jmp_buf stack and modify our
 * own stack by hand.  (Thus, these macros look a good deal like elog.h's.)
 * The main trick to using a single jmp_buf is how to tell whether a caught
 * longjmp was raised by our code or Perl's.  We handle that by detecting
 * whether elog.c has a complete error report that's not been popped.
 * If so, we assume the already-stacked Postgres error is what to report.
 * If not, the error is Perl's, so we convert their error to a Postgres one.
 */
#define PG_PERL_TRY(...)  \
    do { \
        dJMPENV; \
        OP     *_pp_save_op = PL_op; \
        volatile bool _pp_leavetry_needed = true; \
        volatile bool _pp_reporting = false; \
        int     _pp_ret_; \
        sigjmp_buf *_save_exception_stack##__VA_ARGS__ = PG_exception_stack; \
        ErrorContextCallback *_save_context_stack##__VA_ARGS__ = error_context_stack; \
        bool    _do_rethrow##__VA_ARGS__ = false; \
        bool    _cleanup_needed##__VA_ARGS__ = true; \
        JMPENV_PUSH(_pp_ret_); \
        if (_pp_ret_ == 0) \
        { \
            PG_exception_stack = &cur_env.je_buf; \
            plperl_do_entertry(aTHX)
 
#define PG_PERL_CATCH(...)  \
        } \
        else \
        { \
            /* Is the thrown error ours, or Perl's? */ \
            if (!HaveCompleteError()) \
            { \
                /* Perl's, so assume we don't need LEAVETRY any more */ \
                _pp_leavetry_needed = false; \
                /* Reset our error stacks to ensure they're not dangling */ \
                PG_exception_stack = &cur_env.je_buf; \
                error_context_stack = _save_context_stack##__VA_ARGS__; \
                /* This'll longjmp back here, but now with an error stacked */ \
                PG_PERL_REPORT_LONGJMP(_pp_ret_); \
            } \
            PG_PERL_POP_STACKS(__VA_ARGS__)
 
#define PG_PERL_FINALLY(...) \
        } \
        else \
        { \
            /* Is the thrown error ours, or Perl's? */ \
            if (!HaveCompleteError()) \
            { \
                /* Perl's, so assume we don't need LEAVETRY any more */ \
                _pp_leavetry_needed = false; \
                /* Reset our error stacks to ensure they're not dangling */ \
                PG_exception_stack = &cur_env.je_buf; \
                error_context_stack = _save_context_stack##__VA_ARGS__; \
                /* This'll longjmp back here, but now with an error stacked */ \
                PG_PERL_REPORT_LONGJMP(_pp_ret_); \
            } \
            _do_rethrow##__VA_ARGS__ = true; \
        } \
        { \
            PG_PERL_POP_STACKS(__VA_ARGS__)
 
#define PG_PERL_END_TRY(...)  \
        } \
        if (_do_rethrow##__VA_ARGS__) \
                PG_RE_THROW(); \
        if (_cleanup_needed##__VA_ARGS__) \
        { \
            PG_PERL_POP_STACKS(__VA_ARGS__); \
        } \
    } while (0)
 
/*
 * This helper macro is split out just to reduce code duplication.
 * It pops both jmp_buf stacks and does some other end-of-block cleanup.
 *
 * Unlike the case in PG_TRY and friends, we must do this only once,
 * so we need an explicit _cleanup_needed flag to avoid doing it twice
 * during successful exit from a FINALLY block.
 */
#define PG_PERL_POP_STACKS(...) \
        _cleanup_needed##__VA_ARGS__ = false; \
        JMPENV_POP; \
        PG_exception_stack = _save_exception_stack##__VA_ARGS__; \
        error_context_stack = _save_context_stack##__VA_ARGS__; \
        if (_pp_leavetry_needed) \
            plperl_do_leavetry(aTHX); \
        PL_op = _pp_save_op
 
#else                           /* defined(WIN32) && defined(__MINGW64__) */
 
/*
 * We use this implementation on MinGW-64, where we use __builtin_setjmp()
 * (see c.h), but Perl is content with MinGW's setjmp().  Of necessity, we
 * maintain the two jmp_buf stacks separately and pay the price of two setjmp
 * calls per level.
 *
 * Here, the forced updates of PL_top_env are needed to prevent an assertion
 * failure in JMPENV_POP when what we are catching is a Postgres longjmp that
 * has exited past the stack level of some Perl JMPENV_PUSH.
 */
#define PG_PERL_TRY(...)  \
    do { \
        dJMPENV; \
        OP     *_pp_save_op = PL_op; \
        volatile bool _pp_leavetry_needed = true; \
        volatile bool _pp_reporting = false; \
        PG_TRY(__VA_ARGS__); \
        { \
            int     _pp_ret_; \
            JMPENV_PUSH(_pp_ret_); \
            if (_pp_ret_ == 0) \
            { \
                plperl_do_entertry(aTHX);
 
#define PG_PERL_CATCH(...)  \
            } \
            else \
            { \
                _pp_leavetry_needed = false; \
                PG_PERL_REPORT_LONGJMP(_pp_ret_); \
            } \
            PL_top_env = &cur_env; \
            JMPENV_POP; \
            if (_pp_leavetry_needed) \
                plperl_do_leavetry(aTHX); \
            PL_op = _pp_save_op; \
        } \
        PG_CATCH(__VA_ARGS__); \
        { \
            PL_top_env = &cur_env; \
            JMPENV_POP; \
            if (_pp_leavetry_needed) \
                plperl_do_leavetry(aTHX); \
            PL_op = _pp_save_op; \
            {
 
#define PG_PERL_FINALLY(...) \
            } \
            else \
            { \
                _pp_leavetry_needed = false; \
                PG_PERL_REPORT_LONGJMP(_pp_ret_); \
            } \
        } \
        PG_FINALLY(__VA_ARGS__); \
        { \
            PL_top_env = &cur_env; \
            JMPENV_POP; \
            if (_pp_leavetry_needed) \
                plperl_do_leavetry(aTHX); \
            PL_op = _pp_save_op; \
            {
 
#define PG_PERL_END_TRY(...)  \
            } \
        } \
        PG_END_TRY(__VA_ARGS__); \
    } while (0)
 
#endif                          /* !(defined(WIN32) && defined(__MINGW64__)) */
 
/*
 * This helper macro is split out just to reduce code duplication.
 * It converts an error thrown by Perl into a Postgres ereport.
 */
#define PG_PERL_REPORT_LONGJMP(_pp_ret_)    \
    do { \
        SV         *errsv; \
        const char *errmessage; \
        /* \
         * Since we're still within the scope of PG_PERL_TRY's JMPENV_PUSH, \
         * any error thrown by the Perl functions used herein will jump right \
         * back here, risking an infinite loop.  While we try to prevent \
         * that, provide a way to break out of such a loop, just in case. \
         */ \
        CHECK_FOR_INTERRUPTS(); \
        /* \
         * If we already failed while converting Perl's error to a string, \
         * don't try again.  The conversion runs user-supplied Perl code \
         * if the error is an object with an overloaded "" operator, and if \
         * that throws we'll come right back here. \
         */ \
        if (_pp_reporting) \
            ereport(ERROR, \
                    (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), \
                     errmsg("Perl error could not be converted to a string"))); \
        _pp_reporting = true; \
        /* \
         * Convert the error to a string before starting the ereport, not \
         * within it.  A throw from an overloaded "" operator would otherwise \
         * leave an incomplete error stacked, leaking an errordata slot. \
         */ \
        errsv = ERRSV; \
        if (!errsv_is_true(errsv)) \
            elog(ERROR, "unexpected Perl longjmp, code %d", _pp_ret_); \
        errmessage = strip_trailing_ws(sv2cstr(errsv)); \
        _pp_reporting = false; \
        /* This'll longjmp back here, but now with a Postgres error ready */ \
        ereport(ERROR, \
                (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION), \
                 errmsg_internal("%s", errmessage))); \
    } while (0)
 
/*
 * Helper function: invoke OP_ENTERTRY to push a CXt_EVAL context
 *
 * This hackery with ENTERTRY/LEAVETRY ensures that Perl will longjmp rather
 * than exiting, even if we're not presently inside an eval{}.  We explicitly
 * save/restore PL_op rather than trying to get ENTERTRY/LEAVETRY to do that.
 *
 * A side-effect of OP_ENTERTRY that we do *not* want is CLEAR_ERRSV.
 * Hack around that by hiding the entry-time ERRSV in a local.
 */
static inline void
plperl_do_entertry(pTHX)
{
    SV         *old_errsv = GvSV(PL_errgv);
    LOGOP       et_op;
    OP          child_op;
 
    /* Replace ERRSV with a dummy SV, which ENTERTRY will clear */
    GvSV(PL_errgv) = newSV(0);
 
    /* We had better have disabled CATCH_GET, else this does the wrong thing */
    Assert(!CATCH_GET);
    memset(&et_op, 0, sizeof(et_op));
    et_op.op_flags = OPf_WANT_VOID;
    /* We need a child OP, just to avoid a null-pointer dereference */
    memset(&child_op, 0, sizeof(child_op));
    et_op.op_other = &child_op;
    PL_op = (OP *) &et_op;
    (void) (PL_ppaddr[OP_ENTERTRY]) (aTHX);
 
    /* Release the dummy SV's one and only refcnt */
    SvREFCNT_dec(GvSV(PL_errgv));
    /* ... and put back the old ERRSV value */
    GvSV(PL_errgv) = old_errsv;
}
 
/*
 * Helper function: invoke OP_LEAVETRY to pop that context again
 *
 * Just to make things difficult, we must skip this if Perl threw a
 * longjmp, because die_unwind() would have popped the context.
 *
 * As above, don't let it change ERRSV.
 */
static inline void
plperl_do_leavetry(pTHX)
{
    SV         *old_errsv = GvSV(PL_errgv);
    OP          lt_op;
 
    /* Replace ERRSV with a dummy SV, which LEAVETRY will clear */
    GvSV(PL_errgv) = newSV(0);
 
    memset(&lt_op, 0, sizeof(lt_op));
    PL_op = &lt_op;
    (void) (PL_ppaddr[OP_LEAVETRY]) (aTHX);
 
    /* Release the dummy SV's one and only refcnt */
    SvREFCNT_dec(GvSV(PL_errgv));
    /* ... and put back the old ERRSV value */
    GvSV(PL_errgv) = old_errsv;
}
 
#endif                          /* PL_PERL_H */