• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 9792155454

04 Jul 2024 09:39AM UTC coverage: 92.719% (+0.1%) from 92.612%
9792155454

push

github

Buristan
Avoid negation of signed integers in C that may hold INT*_MIN.

Reported by minoki.
Recent C compilers 'take advantage' of the undefined behavior.
This completely changes the meaning of expressions like (k == -k).

(cherry picked from commit 8a5e398c5)

This patch changes all possibly dangerous -x operations on integers to
the corresponding two's complement. Also, it removes all related UBSAN
suppressions, since they are fixed.

Also, this patch limits the `bit.tohex()` result by 254 characters.

There is no testcase for `strscan_oct()`, `strscan_dec()` or/and
`STRSCAN_U32` format since first the unary minus is parsed first and
only after the number itself is parsed during parsing C syntax. So the
error is raised in `cp_expr_prefix()` instead. For parsing the exponent
header, there is no testcase, since the power is limited by
`STRSCAN_MAXEXP`.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#9924
Relates to tarantool/tarantool#8473

Reviewed-by: Maxim Kokryashkin <m.kokryashkin@tarantool.org>
Reviewed-by: Sergey Bronnikov <sergeyb@tarantool.org>
Signed-off-by: Sergey Kaplun <skaplun@tarantool.org>

5673 of 6025 branches covered (94.16%)

Branch coverage included in aggregate %.

24 of 31 new or added lines in 10 files covered. (77.42%)

6 existing lines in 1 file now uncovered.

21641 of 23434 relevant lines covered (92.35%)

2953217.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.36
/src/lj_snap.c
1
/*
2
** Snapshot handling.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
*/
5

6
#define lj_snap_c
7
#define LUA_CORE
8

9
#include "lj_obj.h"
10

11
#if LJ_HASJIT
12

13
#include "lj_gc.h"
14
#include "lj_tab.h"
15
#include "lj_state.h"
16
#include "lj_frame.h"
17
#include "lj_bc.h"
18
#include "lj_ir.h"
19
#include "lj_jit.h"
20
#include "lj_iropt.h"
21
#include "lj_trace.h"
22
#include "lj_snap.h"
23
#include "lj_target.h"
24
#if LJ_HASFFI
25
#include "lj_ctype.h"
26
#include "lj_cdata.h"
27
#endif
28

29
/* Pass IR on to next optimization in chain (FOLD). */
30
#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
31

32
/* Emit raw IR without passing through optimizations. */
33
#define emitir_raw(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J))
34

35
/* -- Snapshot buffer allocation ------------------------------------------ */
36

37
/* Grow snapshot buffer. */
38
void lj_snap_grow_buf_(jit_State *J, MSize need)
231✔
39
{
40
  MSize maxsnap = (MSize)J->param[JIT_P_maxsnap];
231✔
41
  if (need > maxsnap)
231✔
42
    lj_trace_err(J, LJ_TRERR_SNAPOV);
1✔
43
  lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot);
230✔
44
  J->cur.snap = J->snapbuf;
229✔
45
}
229✔
46

47
/* Grow snapshot map buffer. */
48
void lj_snap_grow_map_(jit_State *J, MSize need)
225✔
49
{
50
  if (need < 2*J->sizesnapmap)
225✔
51
    need = 2*J->sizesnapmap;
52
  else if (need < 64)
130✔
53
    need = 64;
54
  J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf,
450✔
55
                    J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry));
225✔
56
  J->cur.snapmap = J->snapmapbuf;
225✔
57
  J->sizesnapmap = need;
225✔
58
}
225✔
59

60
/* -- Snapshot generation ------------------------------------------------- */
61

62
/* Add all modified slots to the snapshot. */
63
static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots)
2,104,817✔
64
{
65
  IRRef retf = J->chain[IR_RETF];  /* Limits SLOAD restore elimination. */
2,104,817✔
66
  BCReg s;
2,104,817✔
67
  MSize n = 0;
2,104,817✔
68
  for (s = 0; s < nslots; s++) {
22,075,768✔
69
    TRef tr = J->slot[s];
19,970,951✔
70
    IRRef ref = tref_ref(tr);
19,970,951✔
71
#if LJ_FR2
72
    if (s == 1) {  /* Ignore slot 1 in LJ_FR2 mode, except if tailcalled. */
19,970,951✔
73
      if ((tr & TREF_FRAME))
2,104,817✔
74
        map[n++] = SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL);
1,104,509✔
75
      continue;
2,104,817✔
76
    }
77
    if ((tr & (TREF_FRAME | TREF_CONT)) && !ref) {
17,866,134✔
78
      cTValue *base = J->L->base - J->baseslot;
667,721✔
79
      tr = J->slot[s] = (tr & 0xff0000) | lj_ir_k64(J, IR_KNUM, base[s].u64);
667,721✔
80
      ref = tref_ref(tr);
667,721✔
81
    }
82
#endif
83
    if (ref) {
17,866,134✔
84
      SnapEntry sn = SNAP_TR(s, tr);
13,497,448✔
85
      IRIns *ir = &J->cur.ir[ref];
13,497,448✔
86
      if ((LJ_FR2 || !(sn & (SNAP_CONT|SNAP_FRAME))) &&
13,497,448✔
87
          ir->o == IR_SLOAD && ir->op1 == s && ref > retf) {
13,497,448✔
88
        /*
89
        ** No need to snapshot unmodified non-inherited slots.
90
        ** But always snapshot the function below a frame in LJ_FR2 mode.
91
        */
92
        if (!(ir->op2 & IRSLOAD_INHERIT) &&
3,070,786✔
93
            (!LJ_FR2 || s == 0 || s+1 == nslots ||
2,979,848✔
94
             !(J->slot[s+1] & (TREF_CONT|TREF_FRAME))))
2,976,218✔
95
          continue;
2,988,763✔
96
        /* No need to restore readonly slots and unmodified non-parent slots. */
97
        if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) &&
82,023✔
98
            (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT)
99
          sn |= SNAP_NORESTORE;
41,817✔
100
      }
101
      if (LJ_SOFTFP32 && irt_isnum(ir->t))
10,508,685✔
102
        sn |= SNAP_SOFTFPNUM;
103
      map[n++] = sn;
10,508,685✔
104
    }
105
  }
106
  return n;
2,104,817✔
107
}
108

109
/* Add frame links at the end of the snapshot. */
110
static MSize snapshot_framelinks(jit_State *J, SnapEntry *map, uint8_t *topslot)
2,104,817✔
111
{
112
  cTValue *frame = J->L->base - 1;
2,104,817✔
113
  cTValue *lim = J->L->base - J->baseslot + LJ_FR2;
2,104,817✔
114
  GCfunc *fn = frame_func(frame);
2,104,817✔
115
  cTValue *ftop = isluafunc(fn) ? (frame+funcproto(fn)->framesize) : J->L->top;
2,104,817✔
116
#if LJ_FR2
117
  uint64_t pcbase = (u64ptr(J->pc) << 8) | (J->baseslot - 2);
2,104,817✔
118
  lj_assertJ(2 <= J->baseslot && J->baseslot <= 257, "bad baseslot");
2,104,817✔
119
  memcpy(map, &pcbase, sizeof(uint64_t));
2,104,817✔
120
#else
121
  MSize f = 0;
122
  map[f++] = SNAP_MKPC(J->pc);  /* The current PC is always the first entry. */
123
  lj_assertJ(!J->pt ||
124
             (J->pc >= proto_bc(J->pt) &&
125
              J->pc < proto_bc(J->pt) + J->pt->sizebc), "bad snapshot PC");
126
#endif
127
  while (frame > lim) {  /* Backwards traversal of all frames above base. */
4,306,747✔
128
    if (frame_islua(frame)) {
2,201,930✔
129
#if !LJ_FR2
130
      map[f++] = SNAP_MKPC(frame_pc(frame));
131
#endif
132
      frame = frame_prevl(frame);
2,201,248✔
133
    } else if (frame_iscont(frame)) {
682✔
134
#if !LJ_FR2
135
      map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
136
      map[f++] = SNAP_MKPC(frame_contpc(frame));
137
#endif
138
      frame = frame_prevd(frame);
231✔
139
    } else {
140
      lj_assertJ(!frame_isc(frame), "broken frame chain");
451✔
141
#if !LJ_FR2
142
      map[f++] = SNAP_MKFTSZ(frame_ftsz(frame));
143
#endif
144
      frame = frame_prevd(frame);
451✔
145
      continue;
451✔
146
    }
147
    if (frame + funcproto(frame_func(frame))->framesize > ftop)
2,201,479✔
148
      ftop = frame + funcproto(frame_func(frame))->framesize;
149
  }
150
  *topslot = (uint8_t)(ftop - lim);
2,104,817✔
151
#if LJ_FR2
152
  lj_assertJ(sizeof(SnapEntry) * 2 == sizeof(uint64_t), "bad SnapEntry def");
2,104,817✔
153
  return 2;
2,104,817✔
154
#else
155
  lj_assertJ(f == (MSize)(1 + J->framedepth), "miscalculated snapshot size");
156
  return f;
157
#endif
158
}
159

160
/* Take a snapshot of the current stack. */
161
static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap)
2,104,817✔
162
{
163
  BCReg nslots = J->baseslot + J->maxslot;
2,104,817✔
164
  MSize nent;
2,104,817✔
165
  SnapEntry *p;
2,104,817✔
166
  /* Conservative estimate. */
167
  lj_snap_grow_map(J, nsnapmap + nslots + (MSize)(LJ_FR2?2:J->framedepth+1));
2,104,817✔
168
  p = &J->cur.snapmap[nsnapmap];
2,104,817✔
169
  nent = snapshot_slots(J, p, nslots);
2,104,817✔
170
  snap->nent = (uint8_t)nent;
2,104,817✔
171
  nent += snapshot_framelinks(J, p + nent, &snap->topslot);
2,104,817✔
172
  snap->mapofs = (uint32_t)nsnapmap;
2,104,817✔
173
  snap->ref = (IRRef1)J->cur.nins;
2,104,817✔
174
  snap->mcofs = 0;
2,104,817✔
175
  snap->nslots = (uint8_t)nslots;
2,104,817✔
176
  snap->count = 0;
2,104,817✔
177
  J->cur.nsnapmap = (uint32_t)(nsnapmap + nent);
2,104,817✔
178
}
2,104,817✔
179

180
/* Add or merge a snapshot. */
181
void lj_snap_add(jit_State *J)
2,104,819✔
182
{
183
  MSize nsnap = J->cur.nsnap;
2,104,819✔
184
  MSize nsnapmap = J->cur.nsnapmap;
2,104,819✔
185
  /* Merge if no ins. inbetween or if requested and no guard inbetween. */
186
  if ((nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins) ||
2,104,819✔
187
      (J->mergesnap && !irt_isguard(J->guardemit))) {
1,272,854✔
188
    if (nsnap == 1) {  /* But preserve snap #0 PC. */
852,718✔
189
      emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0);
1,196✔
190
      goto nomerge;
1,196✔
191
    }
192
    nsnapmap = J->cur.snap[--nsnap].mapofs;
851,522✔
193
  } else {
194
  nomerge:
1,252,101✔
195
    lj_snap_grow_buf(J, nsnap+1);
1,253,297✔
196
    J->cur.nsnap = (uint16_t)(nsnap+1);
1,253,295✔
197
  }
198
  J->mergesnap = 0;
2,104,817✔
199
  J->guardemit.irt = 0;
2,104,817✔
200
  snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap);
2,104,817✔
201
}
2,104,817✔
202

203
/* -- Snapshot modification ----------------------------------------------- */
204

205
#define SNAP_USEDEF_SLOTS        (LJ_MAX_JSLOTS+LJ_STACK_EXTRA)
206

207
/* Find unused slots with reaching-definitions bytecode data-flow analysis. */
208
static BCReg snap_usedef(jit_State *J, uint8_t *udf,
209
                         const BCIns *pc, BCReg maxslot)
210
{
211
  BCReg s;
212
  GCobj *o;
213

214
  if (maxslot == 0) return 0;
215
#ifdef LUAJIT_USE_VALGRIND
216
  /* Avoid errors for harmless reads beyond maxslot. */
217
  memset(udf, 1, SNAP_USEDEF_SLOTS);
218
#else
219
  memset(udf, 1, maxslot);
220
#endif
221

222
  /* Treat open upvalues as used. */
223
  o = gcref(J->L->openupval);
224
  while (o) {
225
    if (uvval(gco2uv(o)) < J->L->base) break;
226
    udf[uvval(gco2uv(o)) - J->L->base] = 0;
227
    o = gcref(o->gch.nextgc);
228
  }
229

230
#define USE_SLOT(s)                udf[(s)] &= ~1
231
#define DEF_SLOT(s)                udf[(s)] *= 3
232

233
  /* Scan through following bytecode and check for uses/defs. */
234
  lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc,
235
             "snapshot PC out of range");
236
  for (;;) {
237
    BCIns ins = *pc++;
238
    BCOp op = bc_op(ins);
239
    switch (bcmode_b(op)) {
240
    case BCMvar: USE_SLOT(bc_b(ins)); break;
241
    default: break;
242
    }
243
    switch (bcmode_c(op)) {
244
    case BCMvar: USE_SLOT(bc_c(ins)); break;
245
    case BCMrbase:
246
      lj_assertJ(op == BC_CAT, "unhandled op %d with RC rbase", op);
247
      for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s);
248
      for (; s < maxslot; s++) DEF_SLOT(s);
249
      break;
250
    case BCMjump:
251
    handle_jump: {
252
      BCReg minslot = bc_a(ins);
253
      if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT;
254
      else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1;
255
      else if (op == BC_UCLO) { pc += bc_j(ins); break; }
256
      for (s = minslot; s < maxslot; s++) DEF_SLOT(s);
257
      return minslot < maxslot ? minslot : maxslot;
258
      }
259
    case BCMlit:
260
      if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) {
261
        goto handle_jump;
262
      } else if (bc_isret(op)) {
263
        BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1);
264
        for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);
265
        for (; s < top; s++) USE_SLOT(s);
266
        for (; s < maxslot; s++) DEF_SLOT(s);
267
        return 0;
268
      }
269
      break;
270
    case BCMfunc: return maxslot;  /* NYI: will abort, anyway. */
271
    default: break;
272
    }
273
    switch (bcmode_a(op)) {
274
    case BCMvar: USE_SLOT(bc_a(ins)); break;
275
    case BCMdst:
276
       if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins));
277
       break;
278
    case BCMbase:
279
      if (op >= BC_CALLM && op <= BC_ITERN) {
280
        BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ?
281
                    maxslot : (bc_a(ins) + bc_c(ins)+LJ_FR2);
282
        if (LJ_FR2) DEF_SLOT(bc_a(ins)+1);
283
        s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0);
284
        for (; s < top; s++) USE_SLOT(s);
285
        for (; s < maxslot; s++) DEF_SLOT(s);
286
        if (op == BC_CALLT || op == BC_CALLMT) {
287
          for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s);
288
          return 0;
289
        }
290
      } else if (op == BC_VARG) {
291
        return maxslot;  /* NYI: punt. */
292
      } else if (op == BC_KNIL) {
293
        for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s);
294
      } else if (op == BC_TSETM) {
295
        for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s);
296
      }
297
      break;
298
    default: break;
299
    }
300
    lj_assertJ(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc,
301
               "use/def analysis PC out of range");
302
  }
303

304
#undef USE_SLOT
305
#undef DEF_SLOT
306

307
  return 0;  /* unreachable */
308
}
309

310
/* Mark slots used by upvalues of child prototypes as used. */
311
void snap_useuv(GCproto *pt, uint8_t *udf)
1,533,239✔
312
{
313
  /* This is a coarse check, because it's difficult to correlate the lifetime
314
  ** of slots and closures. But the number of false positives is quite low.
315
  ** A false positive may cause a slot not to be purged, which is just
316
  ** a missed optimization.
317
  */
318
  if ((pt->flags & PROTO_CHILD)) {
1,533,239✔
319
    ptrdiff_t i, j, n = pt->sizekgc;
233✔
320
    GCRef *kr = mref(pt->k, GCRef) - 1;
233✔
321
    for (i = 0; i < n; i++, kr--) {
6,870✔
322
      GCobj *o = gcref(*kr);
6,637✔
323
      if (o->gch.gct == ~LJ_TPROTO) {
6,637✔
324
        for (j = 0; j < gco2pt(o)->sizeuv; j++) {
1,059✔
325
          uint32_t v = proto_uv(gco2pt(o))[j];
440✔
326
          if ((v & PROTO_UV_LOCAL)) {
440✔
327
            udf[(v & 0xff)] = 0;
426✔
328
          }
329
        }
330
      }
331
    }
332
  }
333
}
1,533,239✔
334

335
/* Purge dead slots before the next snapshot. */
336
void lj_snap_purge(jit_State *J)
1,177,903✔
337
{
338
  uint8_t udf[SNAP_USEDEF_SLOTS];
1,177,903✔
339
  BCReg s, maxslot = J->maxslot;
1,177,903✔
340
  if (bc_op(*J->pc) == BC_FUNCV && maxslot > J->pt->numparams)
1,177,903✔
341
    maxslot = J->pt->numparams;
342
  s = snap_usedef(J, udf, J->pc, maxslot);
1,177,903✔
343
  if (s < maxslot) {
1,177,903✔
344
    snap_useuv(J->pt, udf);
1,022,387✔
345
    for (; s < maxslot; s++)
3,717,052✔
346
      if (udf[s] != 0)
1,672,278✔
347
        J->base[s] = 0;  /* Purge dead slots. */
911,406✔
348
  }
349
}
1,177,903✔
350

351
/* Shrink last snapshot. */
352
void lj_snap_shrink(jit_State *J)
756,621✔
353
{
354
  SnapShot *snap = &J->cur.snap[J->cur.nsnap-1];
756,621✔
355
  SnapEntry *map = &J->cur.snapmap[snap->mapofs];
756,621✔
356
  MSize n, m, nlim, nent = snap->nent;
756,621✔
357
  uint8_t udf[SNAP_USEDEF_SLOTS];
756,621✔
358
  BCReg maxslot = J->maxslot;
756,621✔
359
  BCReg baseslot = J->baseslot;
756,621✔
360
  BCReg minslot = snap_usedef(J, udf, snap_pc(&map[nent]), maxslot);
756,621✔
361
  if (minslot < maxslot) snap_useuv(J->pt, udf);
756,621✔
362
  maxslot += baseslot;
756,621✔
363
  minslot += baseslot;
756,621✔
364
  snap->nslots = (uint8_t)maxslot;
756,621✔
365
  for (n = m = 0; n < nent; n++) {  /* Remove unused slots from snapshot. */
6,113,149✔
366
    BCReg s = snap_slot(map[n]);
5,356,528✔
367
    if (s < minslot || (s < maxslot && udf[s-baseslot] == 0))
5,356,528✔
368
      map[m++] = map[n];  /* Only copy used slots. */
4,579,728✔
369
  }
370
  snap->nent = (uint8_t)m;
756,621✔
371
  nlim = J->cur.nsnapmap - snap->mapofs - 1;
756,621✔
372
  while (n <= nlim) map[m++] = map[n++];  /* Move PC + frame links down. */
2,269,863✔
373
  J->cur.nsnapmap = (uint32_t)(snap->mapofs + m);  /* Free up space in map. */
756,621✔
374
}
756,621✔
375

376
/* -- Snapshot access ----------------------------------------------------- */
377

378
/* Initialize a Bloom Filter with all renamed refs.
379
** There are very few renames (often none), so the filter has
380
** very few bits set. This makes it suitable for negative filtering.
381
*/
382
static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim)
223,221✔
383
{
384
  BloomFilter rfilt = 0;
223,221✔
385
  IRIns *ir;
223,221✔
386
  for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)
230,280✔
387
    if (ir->op2 <= lim)
7,059✔
388
      bloomset(rfilt, ir->op1);
3,770✔
389
  return rfilt;
208,868✔
390
}
391

392
/* Process matching renames to find the original RegSP. */
393
static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs)
2,211✔
394
{
395
  IRIns *ir;
2,211✔
396
  for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--)
7,544✔
397
    if (ir->op1 == ref && ir->op2 <= lim)
5,333✔
398
      rs = ir->prev;
2,272✔
399
  return rs;
400
}
401

402
/* Copy RegSP from parent snapshot to the parent links of the IR. */
403
IRIns *lj_snap_regspmap(jit_State *J, GCtrace *T, SnapNo snapno, IRIns *ir)
14,353✔
404
{
405
  SnapShot *snap = &T->snap[snapno];
14,353✔
406
  SnapEntry *map = &T->snapmap[snap->mapofs];
14,353✔
407
  BloomFilter rfilt = snap_renamefilter(T, snapno);
14,353✔
408
  MSize n = 0;
409
  IRRef ref = 0;
32,837✔
410
  UNUSED(J);
32,837✔
411
  for ( ; ; ir++) {
51,321✔
412
    uint32_t rs;
32,837✔
413
    if (ir->o == IR_SLOAD) {
32,837✔
414
      if (!(ir->op2 & IRSLOAD_PARENT)) break;
23,399✔
415
      for ( ; ; n++) {
72,988✔
416
        lj_assertJ(n < snap->nent, "slot %d not found in snapshot", ir->op1);
28,123✔
417
        if (snap_slot(map[n]) == ir->op1) {
44,865✔
418
          ref = snap_ref(map[n++]);
16,742✔
419
          break;
16,742✔
420
        }
421
      }
422
    } else if (LJ_SOFTFP32 && ir->o == IR_HIOP) {
9,438✔
423
      ref++;
424
    } else if (ir->o == IR_PVAL) {
9,438✔
425
      ref = ir->op1 + REF_BIAS;
1,742✔
426
    } else {
427
      break;
428
    }
429
    rs = T->ir[ref].prev;
18,484✔
430
    if (bloomtest(rfilt, ref))
18,484✔
431
      rs = snap_renameref(T, snapno, ref, rs);
171✔
432
    ir->prev = (uint16_t)rs;
18,484✔
433
    lj_assertJ(regsp_used(rs), "unused IR %04d in snapshot", ref - REF_BIAS);
18,484✔
434
  }
435
  return ir;
14,353✔
436
}
437

438
/* -- Snapshot replay ----------------------------------------------------- */
439

440
/* Replay constant from parent trace. */
441
static TRef snap_replay_const(jit_State *J, IRIns *ir)
33,277✔
442
{
443
  /* Only have to deal with constants that can occur in stack slots. */
444
  switch ((IROp)ir->o) {
33,277✔
445
  case IR_KPRI: return TREF_PRI(irt_type(ir->t));
179✔
446
  case IR_KINT: return lj_ir_kint(J, ir->i);
4,190✔
447
  case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t));
19,903✔
448
  case IR_KNUM: case IR_KINT64:
9,005✔
449
    return lj_ir_k64(J, (IROp)ir->o, ir_k64(ir)->u64);
9,005✔
450
  case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir));  /* Continuation. */
×
451
  case IR_KNULL: return lj_ir_knull(J, irt_type(ir->t));
×
452
  default: lj_assertJ(0, "bad IR constant op %d", ir->o); return TREF_NIL;
453
  }
454
}
455

456
/* De-duplicate parent reference. */
457
static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref)
458
{
459
  MSize j;
460
  for (j = 0; j < nmax; j++)
14,795✔
461
    if (snap_ref(map[j]) == ref)
14,271✔
462
      return J->slot[snap_slot(map[j])] & ~(SNAP_CONT|SNAP_FRAME);
2,032✔
463
  return 0;
464
}
465

466
/* Emit parent reference with de-duplication. */
467
static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax,
468
                      BloomFilter seen, IRRef ref)
469
{
470
  IRIns *ir = &T->ir[ref];
471
  TRef tr;
472
  if (irref_isk(ref))
473
    tr = snap_replay_const(J, ir);
474
  else if (!regsp_used(ir->prev))
475
    tr = 0;
476
  else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0)
477
    tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0);
478
  return tr;
479
}
480

481
/* Check whether a sunk store corresponds to an allocation. Slow path. */
482
static int snap_sunk_store2(GCtrace *T, IRIns *ira, IRIns *irs)
483
{
484
  if (irs->o == IR_ASTORE || irs->o == IR_HSTORE ||
485
      irs->o == IR_FSTORE || irs->o == IR_XSTORE) {
486
    IRIns *irk = &T->ir[irs->op1];
487
    if (irk->o == IR_AREF || irk->o == IR_HREFK)
488
      irk = &T->ir[irk->op1];
489
    return (&T->ir[irk->op1] == ira);
490
  }
491
  return 0;
492
}
493

494
/* Check whether a sunk store corresponds to an allocation. Fast path. */
495
static LJ_AINLINE int snap_sunk_store(GCtrace *T, IRIns *ira, IRIns *irs)
90✔
496
{
497
  if (irs->s != 255)
90✔
498
    return (ira + irs->s == irs);  /* Fast check. */
90✔
499
  return snap_sunk_store2(T, ira, irs);
×
500
}
501

502
/* Replay snapshot state to setup side trace. */
503
void lj_snap_replay(jit_State *J, GCtrace *T)
14,986✔
504
{
505
  SnapShot *snap = &T->snap[J->exitno];
14,986✔
506
  SnapEntry *map = &T->snapmap[snap->mapofs];
14,986✔
507
  MSize n, nent = snap->nent;
14,986✔
508
  BloomFilter seen = 0;
14,986✔
509
  int pass23 = 0;
14,986✔
510
  J->framedepth = 0;
14,986✔
511
  /* Emit IR for slots inherited from parent snapshot. */
512
  for (n = 0; n < nent; n++) {
67,782✔
513
    SnapEntry sn = map[n];
52,796✔
514
    BCReg s = snap_slot(sn);
52,796✔
515
    IRRef ref = snap_ref(sn);
52,796✔
516
    IRIns *ir = &T->ir[ref];
52,796✔
517
    TRef tr;
52,796✔
518
    /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */
519
    if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0)
55,352✔
520
      goto setslot;
2,032✔
521
    bloomset(seen, ref);
50,764✔
522
    if (irref_isk(ref)) {
50,764✔
523
      /* See special treatment of LJ_FR2 slot 1 in snapshot_slots() above. */
524
      if (LJ_FR2 && (sn == SNAP(1, SNAP_FRAME | SNAP_NORESTORE, REF_NIL)))
32,163✔
525
        tr = 0;
526
      else
527
        tr = snap_replay_const(J, ir);
29,751✔
528
    } else if (!regsp_used(ir->prev)) {
18,601✔
529
      pass23 = 1;
530
      lj_assertJ(s != 0, "unused slot 0 in snapshot");
531
      tr = s;
532
    } else {
533
      IRType t = irt_type(ir->t);
16,854✔
534
      uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT;
16,854✔
535
      if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM;
16,854✔
536
      if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY);
16,854✔
537
      tr = emitir_raw(IRT(IR_SLOAD, t), s, mode);
16,854✔
538
    }
539
  setslot:
52,796✔
540
    J->slot[s] = tr | (sn&(SNAP_CONT|SNAP_FRAME));  /* Same as TREF_* flags. */
52,796✔
541
    J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && (s != LJ_FR2));
52,796✔
542
    if ((sn & SNAP_FRAME))
52,796✔
543
      J->baseslot = s+1;
11,578✔
544
  }
545
  if (pass23) {
14,986✔
546
    IRIns *irlast = &T->ir[snap->ref];
356✔
547
    pass23 = 0;
356✔
548
    /* Emit dependent PVALs. */
549
    for (n = 0; n < nent; n++) {
3,401✔
550
      SnapEntry sn = map[n];
3,045✔
551
      IRRef refp = snap_ref(sn);
3,045✔
552
      IRIns *ir = &T->ir[refp];
3,045✔
553
      if (regsp_reg(ir->r) == RID_SUNK) {
3,045✔
554
        uint8_t m;
1,746✔
555
        if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue;
1,746✔
556
        pass23 = 1;
1,745✔
557
        lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP ||
1,745✔
558
                   ir->o == IR_CNEW || ir->o == IR_CNEWI,
559
                   "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o);
560
        m = lj_ir_mode[ir->o];
1,745✔
561
        if (irm_op1(m) == IRMref) snap_pref(J, T, map, nent, seen, ir->op1);
1,745✔
562
        if (irm_op2(m) == IRMref) snap_pref(J, T, map, nent, seen, ir->op2);
1,745✔
563
        if (LJ_HASFFI && ir->o == IR_CNEWI) {
1,745✔
564
          if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP)
565
            snap_pref(J, T, map, nent, seen, (ir+1)->op2);
566
        } else {
567
          IRIns *irs;
16✔
568
          for (irs = ir+1; irs < irlast; irs++)
215✔
569
            if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
244✔
570
              if (snap_pref(J, T, map, nent, seen, irs->op2) == 0)
31✔
571
                snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1);
6✔
572
              else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) &&
573
                       irs+1 < irlast && (irs+1)->o == IR_HIOP)
574
                snap_pref(J, T, map, nent, seen, (irs+1)->op2);
575
            }
576
        }
577
      } else if (!irref_isk(refp) && !regsp_used(ir->prev)) {
1,299✔
578
        lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
2✔
579
                   "sunk parent IR %04d has bad op %d", refp - REF_BIAS, ir->o);
580
        J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1);
2✔
581
      }
582
    }
583
    /* Replay sunk instructions. */
584
    for (n = 0; pass23 && n < nent; n++) {
3,398✔
585
      SnapEntry sn = map[n];
3,042✔
586
      IRRef refp = snap_ref(sn);
3,042✔
587
      IRIns *ir = &T->ir[refp];
3,042✔
588
      if (regsp_reg(ir->r) == RID_SUNK) {
3,042✔
589
        TRef op1, op2;
1,746✔
590
        uint8_t m;
1,746✔
591
        if (J->slot[snap_slot(sn)] != snap_slot(sn)) {  /* De-dup allocs. */
1,746✔
592
          J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]];
1✔
593
          continue;
1✔
594
        }
595
        op1 = ir->op1;
1,745✔
596
        m = lj_ir_mode[ir->o];
1,745✔
597
        if (irm_op1(m) == IRMref) op1 = snap_pref(J, T, map, nent, seen, op1);
1,745✔
598
        op2 = ir->op2;
1,745✔
599
        if (irm_op2(m) == IRMref) op2 = snap_pref(J, T, map, nent, seen, op2);
1,745✔
600
        if (LJ_HASFFI && ir->o == IR_CNEWI) {
1,745✔
601
          if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) {
1,729✔
602
            lj_needsplit(J);  /* Emit joining HIOP. */
603
            op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2,
604
                             snap_pref(J, T, map, nent, seen, (ir+1)->op2));
605
          }
606
          J->slot[snap_slot(sn)] = emitir(ir->ot & ~(IRT_MARK|IRT_ISPHI), op1, op2);
1,729✔
607
        } else {
608
          IRIns *irs;
16✔
609
          TRef tr = emitir(ir->ot, op1, op2);
16✔
610
          J->slot[snap_slot(sn)] = tr;
16✔
611
          for (irs = ir+1; irs < irlast; irs++)
215✔
612
            if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
275✔
613
              IRIns *irr = &T->ir[irs->op1];
31✔
614
              TRef val, key = irr->op2, tmp = tr;
31✔
615
              if (irr->o != IR_FREF) {
31✔
616
                IRIns *irk = &T->ir[key];
30✔
617
                if (irr->o == IR_HREFK)
30✔
618
                  key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]),
2✔
619
                                    irk->op2);
2✔
620
                else
621
                  key = snap_replay_const(J, irk);
28✔
622
                if (irr->o == IR_HREFK || irr->o == IR_AREF) {
30✔
623
                  IRIns *irf = &T->ir[irr->op1];
10✔
624
                  tmp = emitir(irf->ot, tmp, irf->op2);
10✔
625
                } else if (irr->o == IR_NEWREF) {
20✔
626
                  IRRef allocref = tref_ref(tr);
12✔
627
                  IRRef keyref = tref_ref(key);
12✔
628
                  IRRef newref_ref = J->chain[IR_NEWREF];
12✔
629
                  IRIns *newref = &J->cur.ir[newref_ref];
12✔
630
                  lj_assertJ(irref_isk(keyref),
12✔
631
                             "sunk store for parent IR %04d with bad key %04d",
632
                             refp - REF_BIAS, keyref - REF_BIAS);
633
                  if (newref_ref > allocref && newref->op2 == keyref) {
12✔
634
                    lj_assertJ(newref->op1 == allocref,
6✔
635
                               "sunk store for parent IR %04d with bad tab %04d",
636
                               refp - REF_BIAS, allocref - REF_BIAS);
637
                    tmp = newref_ref;
6✔
638
                    goto skip_newref;
6✔
639
                  }
640
                }
641
              }
642
              tmp = emitir(irr->ot, tmp, key);
25✔
643
            skip_newref:
31✔
644
              val = snap_pref(J, T, map, nent, seen, irs->op2);
31✔
645
              if (val == 0) {
31✔
646
                IRIns *irc = &T->ir[irs->op2];
6✔
647
                lj_assertJ(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT,
6✔
648
                           "sunk store for parent IR %04d with bad op %d",
649
                           refp - REF_BIAS, irc->o);
650
                val = snap_pref(J, T, map, nent, seen, irc->op1);
6✔
651
                val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT);
6✔
652
              } else if ((LJ_SOFTFP32 || (LJ_32 && LJ_HASFFI)) &&
653
                         irs+1 < irlast && (irs+1)->o == IR_HIOP) {
654
                IRType t = IRT_I64;
655
                if (LJ_SOFTFP32 && irt_type((irs+1)->t) == IRT_SOFTFP)
656
                  t = IRT_NUM;
657
                lj_needsplit(J);
658
                if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) {
659
                  uint64_t k = (uint32_t)T->ir[irs->op2].i +
660
                               ((uint64_t)T->ir[(irs+1)->op2].i << 32);
661
                  val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM, k);
662
                } else {
663
                  val = emitir_raw(IRT(IR_HIOP, t), val,
664
                          snap_pref(J, T, map, nent, seen, (irs+1)->op2));
665
                }
666
                tmp = emitir(IRT(irs->o, t), tmp, val);
667
                continue;
668
              }
669
              tmp = emitir(irs->ot, tmp, val);
31✔
670
            } else if (LJ_HASFFI && irs->o == IR_XBAR && ir->o == IR_CNEW) {
168✔
671
              emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
×
672
            }
673
        }
674
      }
675
    }
676
  }
677
  J->base = J->slot + J->baseslot;
14,986✔
678
  J->maxslot = snap->nslots - J->baseslot;
14,986✔
679
  lj_snap_add(J);
14,986✔
680
  if (pass23)  /* Need explicit GC step _after_ initial snapshot. */
14,986✔
681
    emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0);
355✔
682
}
14,986✔
683

684
/* -- Snapshot restore ---------------------------------------------------- */
685

686
static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,
687
                        SnapNo snapno, BloomFilter rfilt,
688
                        IRIns *ir, TValue *o);
689

690
/* Restore a value from the trace exit state. */
691
static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex,
763,961✔
692
                            SnapNo snapno, BloomFilter rfilt,
693
                            IRRef ref, TValue *o)
694
{
695
  IRIns *ir = &T->ir[ref];
764,041✔
696
  IRType1 t = ir->t;
764,041✔
697
  RegSP rs = ir->prev;
764,041✔
698
  if (irref_isk(ref)) {  /* Restore constant slot. */
764,041✔
699
    if (ir->o == IR_KPTR) {
499,741✔
700
      o->u64 = (uint64_t)(uintptr_t)ir_kptr(ir);
×
701
    } else {
702
      lj_assertJ(!(ir->o == IR_KKPTR || ir->o == IR_KNULL),
499,741✔
703
                 "restore of const from IR %04d with bad op %d",
704
                 ref - REF_BIAS, ir->o);
705
      lj_ir_kvalue(J->L, o, ir);
499,741✔
706
    }
707
    return;
499,741✔
708
  }
709
  if (LJ_UNLIKELY(bloomtest(rfilt, ref)))
264,300✔
710
    rs = snap_renameref(T, snapno, ref, rs);
2,040✔
711
  if (ra_hasspill(regsp_spill(rs))) {  /* Restore from spill slot. */
264,300✔
712
    int32_t *sps = &ex->spill[regsp_spill(rs)];
38,126✔
713
    if (irt_isinteger(t)) {
38,126✔
714
      setintV(o, *sps);
378✔
715
#if !LJ_SOFTFP32
716
    } else if (irt_isnum(t)) {
37,748✔
717
      o->u64 = *(uint64_t *)sps;
17,073✔
718
#endif
719
#if LJ_64 && !LJ_GC64
720
    } else if (irt_islightud(t)) {
721
      /* 64 bit lightuserdata which may escape already has the tag bits. */
722
      o->u64 = *(uint64_t *)sps;
723
#endif
724
    } else {
725
      lj_assertJ(!irt_ispri(t), "PRI ref with spill slot");
20,675✔
726
      setgcV(J->L, o, (GCobj *)(uintptr_t)*(GCSize *)sps, irt_toitype(t));
20,675✔
727
    }
728
  } else {  /* Restore from register. */
729
    Reg r = regsp_reg(rs);
226,174✔
730
    if (ra_noreg(r)) {
226,174✔
731
      lj_assertJ(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
80✔
732
                 "restore from IR %04d has no reg", ref - REF_BIAS);
733
      snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o);
80✔
734
      if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o));
735
      return;
80✔
736
    } else if (irt_isinteger(t)) {
226,094✔
737
      setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]);
5,255✔
738
#if !LJ_SOFTFP
739
    } else if (irt_isnum(t)) {
220,839✔
740
      setnumV(o, ex->fpr[r-RID_MIN_FPR]);
57,501✔
741
#elif LJ_64  /* && LJ_SOFTFP */
742
    } else if (irt_isnum(t)) {
743
      o->u64 = ex->gpr[r-RID_MIN_GPR];
744
#endif
745
#if LJ_64 && !LJ_GC64
746
    } else if (irt_is64(t)) {
747
      /* 64 bit values that already have the tag bits. */
748
      o->u64 = ex->gpr[r-RID_MIN_GPR];
749
#endif
750
    } else if (irt_ispri(t)) {
163,338✔
751
      setpriV(o, irt_toitype(t));
×
752
    } else {
753
      setgcV(J->L, o, (GCobj *)ex->gpr[r-RID_MIN_GPR], irt_toitype(t));
163,338✔
754
    }
755
  }
756
}
757

758
#if LJ_HASFFI
759
/* Restore raw data from the trace exit state. */
760
static void snap_restoredata(jit_State *J, GCtrace *T, ExitState *ex,
4,382✔
761
                             SnapNo snapno, BloomFilter rfilt,
762
                             IRRef ref, void *dst, CTSize sz)
763
{
764
  IRIns *ir = &T->ir[ref];
4,382✔
765
  RegSP rs = ir->prev;
4,382✔
766
  int32_t *src;
4,382✔
767
  uint64_t tmp;
4,382✔
768
  UNUSED(J);
4,382✔
769
  if (irref_isk(ref)) {
4,382✔
770
    if (ir_isk64(ir)) {
7✔
771
      src = (int32_t *)&ir[1];
2✔
772
    } else if (sz == 8) {
5✔
UNCOV
773
      tmp = (uint64_t)(uint32_t)ir->i;
×
UNCOV
774
      src = (int32_t *)&tmp;
×
775
    } else {
776
      src = &ir->i;
5✔
777
    }
778
  } else {
779
    if (LJ_UNLIKELY(bloomtest(rfilt, ref)))
4,375✔
780
      rs = snap_renameref(T, snapno, ref, rs);
×
781
    if (ra_hasspill(regsp_spill(rs))) {
4,375✔
782
      src = &ex->spill[regsp_spill(rs)];
818✔
783
      if (sz == 8 && !irt_is64(ir->t)) {
818✔
UNCOV
784
        tmp = (uint64_t)(uint32_t)*src;
×
UNCOV
785
        src = (int32_t *)&tmp;
×
786
      }
787
    } else {
788
      Reg r = regsp_reg(rs);
3,557✔
789
      if (ra_noreg(r)) {
3,557✔
790
        /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */
791
        lj_assertJ(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT,
13✔
792
                   "restore from IR %04d has no reg", ref - REF_BIAS);
793
        snap_restoredata(J, T, ex, snapno, rfilt, ir->op1, dst, 4);
13✔
794
        *(lua_Number *)dst = (lua_Number)*(int32_t *)dst;
13✔
795
        return;
13✔
796
      }
797
#if !LJ_SOFTFP
798
      if (r >= RID_MAX_GPR) {
3,544✔
799
        src = (int32_t *)&ex->fpr[r-RID_MIN_FPR];
17✔
800
#if LJ_TARGET_PPC
801
        if (sz == 4) {  /* PPC FPRs are always doubles. */
802
          *(float *)dst = (float)*(double *)src;
803
          return;
804
        }
805
#else
806
        if (LJ_BE && sz == 4) src++;
17✔
807
#endif
808
      } else
809
#endif
810
      {
811
        src = (int32_t *)&ex->gpr[r-RID_MIN_GPR];
3,527✔
812
        if (LJ_64 && LJ_BE && sz == 4) src++;
3,527✔
813
      }
814
    }
815
  }
816
  lj_assertJ(sz == 1 || sz == 2 || sz == 4 || sz == 8,
4,369✔
817
             "restore from IR %04d with bad size %d", ref - REF_BIAS, sz);
818
  if (sz == 4) *(int32_t *)dst = *src;
4,369✔
819
  else if (sz == 8) *(int64_t *)dst = *(int64_t *)src;
4,311✔
UNCOV
820
  else if (sz == 1) *(int8_t *)dst = (int8_t)*src;
×
UNCOV
821
  else *(int16_t *)dst = (int16_t)*src;
×
822
}
823
#endif
824

825
/* Unsink allocation from the trace exit state. Unsink sunk stores. */
826
static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex,
827
                        SnapNo snapno, BloomFilter rfilt,
828
                        IRIns *ir, TValue *o)
829
{
830
  lj_assertJ(ir->o == IR_TNEW || ir->o == IR_TDUP ||
831
             ir->o == IR_CNEW || ir->o == IR_CNEWI,
832
             "sunk allocation with bad op %d", ir->o);
833
#if LJ_HASFFI
834
  if (ir->o == IR_CNEW || ir->o == IR_CNEWI) {
835
    CTState *cts = ctype_cts(J->L);
836
    CTypeID id = (CTypeID)T->ir[ir->op1].i;
837
    CTSize sz;
838
    CTInfo info = lj_ctype_info(cts, id, &sz);
839
    GCcdata *cd = lj_cdata_newx(cts, id, sz, info);
840
    setcdataV(J->L, o, cd);
841
    if (ir->o == IR_CNEWI) {
842
      uint8_t *p = (uint8_t *)cdataptr(cd);
843
      lj_assertJ(sz == 4 || sz == 8, "sunk cdata with bad size %d", sz);
844
      if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) {
845
        snap_restoredata(J, T, ex, snapno, rfilt, (ir+1)->op2,
846
                         LJ_LE ? p+4 : p, 4);
847
        if (LJ_BE) p += 4;
848
        sz = 4;
849
      }
850
      snap_restoredata(J, T, ex, snapno, rfilt, ir->op2, p, sz);
851
    } else {
852
      IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref];
853
      for (irs = ir+1; irs < irlast; irs++)
854
        if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
855
          IRIns *iro = &T->ir[T->ir[irs->op1].op2];
856
          uint8_t *p = (uint8_t *)cd;
857
          CTSize szs;
858
          lj_assertJ(irs->o == IR_XSTORE, "sunk store with bad op %d", irs->o);
859
          lj_assertJ(T->ir[irs->op1].o == IR_ADD,
860
                     "sunk store with bad add op %d", T->ir[irs->op1].o);
861
          lj_assertJ(iro->o == IR_KINT || iro->o == IR_KINT64,
862
                     "sunk store with bad const offset op %d", iro->o);
863
          if (irt_is64(irs->t)) szs = 8;
864
          else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1;
865
          else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2;
866
          else szs = 4;
867
          if (LJ_64 && iro->o == IR_KINT64)
868
            p += (int64_t)ir_k64(iro)->u64;
869
          else
870
            p += iro->i;
871
          lj_assertJ(p >= (uint8_t *)cdataptr(cd) &&
872
                     p + szs <= (uint8_t *)cdataptr(cd) + sz,
873
                     "sunk store with offset out of range");
874
          if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {
875
            lj_assertJ(szs == 4, "sunk store with bad size %d", szs);
876
            snap_restoredata(J, T, ex, snapno, rfilt, (irs+1)->op2,
877
                             LJ_LE ? p+4 : p, 4);
878
            if (LJ_BE) p += 4;
879
          }
880
          snap_restoredata(J, T, ex, snapno, rfilt, irs->op2, p, szs);
881
        }
882
    }
883
  } else
884
#endif
885
  {
886
    IRIns *irs, *irlast;
887
    GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) :
888
                                  lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1]));
889
    settabV(J->L, o, t);
890
    irlast = &T->ir[T->snap[snapno].ref];
891
    for (irs = ir+1; irs < irlast; irs++)
892
      if (irs->r == RID_SINK && snap_sunk_store(T, ir, irs)) {
893
        IRIns *irk = &T->ir[irs->op1];
894
        TValue tmp, *val;
895
        lj_assertJ(irs->o == IR_ASTORE || irs->o == IR_HSTORE ||
896
                   irs->o == IR_FSTORE,
897
                   "sunk store with bad op %d", irs->o);
898
        if (irk->o == IR_FREF) {
899
          switch (irk->op2) {
900
          case IRFL_TAB_META:
901
            if (T->ir[irs->op2].o == IR_KNULL) {
902
              setgcrefnull(t->metatable);
903
            } else {
904
              snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp);
905
              /* NOBARRIER: The table is new (marked white). */
906
              setgcref(t->metatable, obj2gco(tabV(&tmp)));
907
            }
908
            break;
909
          case IRFL_TAB_NOMM:
910
            /* Negative metamethod cache invalidated by lj_tab_set() below. */
911
            break;
912
          default:
913
            lj_assertJ(0, "sunk store with bad field %d", irk->op2);
914
            break;
915
          }
916
        } else {
917
          irk = &T->ir[irk->op2];
918
          if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1];
919
          lj_ir_kvalue(J->L, &tmp, irk);
920
          val = lj_tab_set(J->L, t, &tmp);
921
          /* NOBARRIER: The table is new (marked white). */
922
          snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val);
923
          if (LJ_SOFTFP32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) {
924
            snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp);
925
            val->u32.hi = tmp.u32.lo;
926
          }
927
        }
928
      }
929
  }
930
}
931

932
/* Restore interpreter state from exit state with the help of a snapshot. */
933
const BCIns *lj_snap_restore(jit_State *J, void *exptr)
208,868✔
934
{
935
  ExitState *ex = (ExitState *)exptr;
208,868✔
936
  SnapNo snapno = J->exitno;  /* For now, snapno == exitno. */
208,868✔
937
  GCtrace *T = traceref(J, J->parent);
208,868✔
938
  SnapShot *snap = &T->snap[snapno];
208,868✔
939
  MSize n, nent = snap->nent;
208,868✔
940
  SnapEntry *map = &T->snapmap[snap->mapofs];
208,868✔
941
#if !LJ_FR2 || defined(LUA_USE_ASSERT)
942
  SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1-LJ_FR2];
943
#endif
944
#if !LJ_FR2
945
  ptrdiff_t ftsz0;
946
#endif
947
  TValue *frame;
208,868✔
948
  BloomFilter rfilt = snap_renamefilter(T, snapno);
208,868✔
949
  const BCIns *pc = snap_pc(&map[nent]);
208,868✔
950
  lua_State *L = J->L;
208,868✔
951

952
  /* Set interpreter PC to the next PC to get correct error messages. */
953
  setcframe_pc(cframe_raw(L->cframe), pc+1);
208,868✔
954

955
  /* Make sure the stack is big enough for the slots from the snapshot. */
956
  if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) {
208,868✔
957
    L->top = curr_topL(L);
37✔
958
    lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize);
37✔
959
  }
960

961
  /* Fill stack slots with data from the registers and spill slots. */
962
  frame = L->base-1-LJ_FR2;
208,864✔
963
#if !LJ_FR2
964
  ftsz0 = frame_ftsz(frame);  /* Preserve link to previous frame in slot #0. */
965
#endif
966
  for (n = 0; n < nent; n++) {
1,026,674✔
967
    SnapEntry sn = map[n];
817,810✔
968
    if (!(sn & SNAP_NORESTORE)) {
817,810✔
969
      TValue *o = &frame[snap_slot(sn)];
768,180✔
970
      IRRef ref = snap_ref(sn);
768,180✔
971
      IRIns *ir = &T->ir[ref];
768,180✔
972
      if (ir->r == RID_SUNK) {
768,180✔
973
        MSize j;
974
        for (j = 0; j < n; j++)
35,691✔
975
          if (snap_ref(map[j]) == ref) {  /* De-duplicate sunk allocations. */
31,256✔
976
            copyTV(L, o, &frame[snap_slot(map[j])]);
21✔
977
            goto dupslot;
21✔
978
          }
979
        snap_unsink(J, T, ex, snapno, rfilt, ir, o);
4,435✔
980
      dupslot:
4,456✔
981
        continue;
4,456✔
982
      }
983
      snap_restoreval(J, T, ex, snapno, rfilt, ref, o);
763,724✔
984
      if (LJ_SOFTFP32 && (sn & SNAP_SOFTFPNUM) && tvisint(o)) {
763,724✔
985
        TValue tmp;
986
        snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp);
987
        o->u32.hi = tmp.u32.lo;
988
#if !LJ_FR2
989
      } else if ((sn & (SNAP_CONT|SNAP_FRAME))) {
990
        /* Overwrite tag with frame link. */
991
        setframe_ftsz(o, snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0);
992
        L->base = o+1;
993
#endif
994
      }
995
    }
996
  }
997
#if LJ_FR2
998
  L->base += (map[nent+LJ_BE] & 0xff);
208,864✔
999
#endif
1000
  lj_assertJ(map + nent == flinks, "inconsistent frames in snapshot");
208,864✔
1001

1002
  /* Compute current stack top. */
1003
  switch (bc_op(*pc)) {
208,864✔
1004
  default:
207,907✔
1005
    if (bc_op(*pc) < BC_FUNCF) {
207,907✔
1006
      L->top = curr_topL(L);
207,888✔
1007
      break;
207,888✔
1008
    }
1009
    /* fallthrough */
1010
  case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM:
1011
    L->top = frame + snap->nslots;
976✔
1012
    break;
976✔
1013
  }
1014
  J->nsnaprestore++;
208,864✔
1015
  return pc;
208,864✔
1016
}
1017

1018
#undef emitir_raw
1019
#undef emitir
1020

1021
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc