aboutsummaryrefslogtreecommitdiff
path: root/src/lj_bcread.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_bcread.c')
-rw-r--r--src/lj_bcread.c88
1 files changed, 39 insertions, 49 deletions
diff --git a/src/lj_bcread.c b/src/lj_bcread.c
index 5be34757..fcc2aa1d 100644
--- a/src/lj_bcread.c
+++ b/src/lj_bcread.c
@@ -43,7 +43,7 @@ static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)
43 lj_err_throw(L, LUA_ERRSYNTAX); 43 lj_err_throw(L, LUA_ERRSYNTAX);
44} 44}
45 45
46/* Refill buffer if needed. */ 46/* Refill buffer. */
47static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) 47static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
48{ 48{
49 lua_assert(len != 0); 49 lua_assert(len != 0);
@@ -51,61 +51,61 @@ static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need)
51 bcread_error(ls, LJ_ERR_BCBAD); 51 bcread_error(ls, LJ_ERR_BCBAD);
52 do { 52 do {
53 const char *buf; 53 const char *buf;
54 size_t size; 54 size_t sz;
55 if (ls->n) { /* Copy remainder to buffer. */ 55 char *p = sbufB(&ls->sb);
56 MSize n = (MSize)(ls->pe - ls->p);
57 if (n) { /* Copy remainder to buffer. */
56 if (sbuflen(&ls->sb)) { /* Move down in buffer. */ 58 if (sbuflen(&ls->sb)) { /* Move down in buffer. */
57 lua_assert(ls->p + ls->n == sbufP(&ls->sb)); 59 lua_assert(ls->pe == sbufP(&ls->sb));
58 if (ls->n != sbuflen(&ls->sb)) 60 if (ls->p != p) memmove(p, ls->p, n);
59 memmove(sbufB(&ls->sb), ls->p, ls->n);
60 } else { /* Copy from buffer provided by reader. */ 61 } else { /* Copy from buffer provided by reader. */
61 memcpy(lj_buf_need(ls->L, &ls->sb, len), ls->p, ls->n); 62 p = lj_buf_need(ls->L, &ls->sb, len);
63 memcpy(p, ls->p, n);
62 } 64 }
63 ls->p = sbufB(&ls->sb); 65 ls->p = p;
66 ls->pe = p + n;
64 } 67 }
65 setsbufP(&ls->sb, sbufB(&ls->sb) + ls->n); 68 setsbufP(&ls->sb, p + n);
66 buf = ls->rfunc(ls->L, ls->rdata, &size); /* Get more data from reader. */ 69 buf = ls->rfunc(ls->L, ls->rdata, &sz); /* Get more data from reader. */
67 if (buf == NULL || size == 0) { /* EOF? */ 70 if (buf == NULL || sz == 0) { /* EOF? */
68 if (need) bcread_error(ls, LJ_ERR_BCBAD); 71 if (need) bcread_error(ls, LJ_ERR_BCBAD);
69 ls->c = -1; /* Only bad if we get called again. */ 72 ls->c = -1; /* Only bad if we get called again. */
70 break; 73 break;
71 } 74 }
72 if (sbuflen(&ls->sb)) { /* Append to buffer. */ 75 if (n) { /* Append to buffer. */
73 MSize n = sbuflen(&ls->sb) + (MSize)size; 76 n += (MSize)sz;
74 char *p = lj_buf_need(ls->L, &ls->sb, n < len ? len : n); 77 p = lj_buf_need(ls->L, &ls->sb, n < len ? len : n);
75 memcpy(sbufP(&ls->sb), buf, size); 78 memcpy(sbufP(&ls->sb), buf, sz);
76 setsbufP(&ls->sb, sbufB(&ls->sb) + n); 79 setsbufP(&ls->sb, p + n);
77 ls->n = n;
78 ls->p = p; 80 ls->p = p;
81 ls->pe = p + n;
79 } else { /* Return buffer provided by reader. */ 82 } else { /* Return buffer provided by reader. */
80 ls->n = (MSize)size;
81 ls->p = buf; 83 ls->p = buf;
84 ls->pe = buf + sz;
82 } 85 }
83 } while (ls->n < len); 86 } while (ls->p + len > ls->pe);
84} 87}
85 88
86/* Need a certain number of bytes. */ 89/* Need a certain number of bytes. */
87static LJ_AINLINE void bcread_need(LexState *ls, MSize len) 90static LJ_AINLINE void bcread_need(LexState *ls, MSize len)
88{ 91{
89 if (LJ_UNLIKELY(ls->n < len)) 92 if (LJ_UNLIKELY(ls->p + len > ls->pe))
90 bcread_fill(ls, len, 1); 93 bcread_fill(ls, len, 1);
91} 94}
92 95
93/* Want to read up to a certain number of bytes, but may need less. */ 96/* Want to read up to a certain number of bytes, but may need less. */
94static LJ_AINLINE void bcread_want(LexState *ls, MSize len) 97static LJ_AINLINE void bcread_want(LexState *ls, MSize len)
95{ 98{
96 if (LJ_UNLIKELY(ls->n < len)) 99 if (LJ_UNLIKELY(ls->p + len > ls->pe))
97 bcread_fill(ls, len, 0); 100 bcread_fill(ls, len, 0);
98} 101}
99 102
100#define bcread_dec(ls) check_exp(ls->n > 0, ls->n--)
101#define bcread_consume(ls, len) check_exp(ls->n >= (len), ls->n -= (len))
102
103/* Return memory block from buffer. */ 103/* Return memory block from buffer. */
104static uint8_t *bcread_mem(LexState *ls, MSize len) 104static LJ_AINLINE uint8_t *bcread_mem(LexState *ls, MSize len)
105{ 105{
106 uint8_t *p = (uint8_t *)ls->p; 106 uint8_t *p = (uint8_t *)ls->p;
107 bcread_consume(ls, len); 107 ls->p += len;
108 ls->p = (char *)p + len; 108 lua_assert(ls->p <= ls->pe);
109 return p; 109 return p;
110} 110}
111 111
@@ -118,25 +118,15 @@ static void bcread_block(LexState *ls, void *q, MSize len)
118/* Read byte from buffer. */ 118/* Read byte from buffer. */
119static LJ_AINLINE uint32_t bcread_byte(LexState *ls) 119static LJ_AINLINE uint32_t bcread_byte(LexState *ls)
120{ 120{
121 bcread_dec(ls); 121 lua_assert(ls->p < ls->pe);
122 return (uint32_t)(uint8_t)*ls->p++; 122 return (uint32_t)(uint8_t)*ls->p++;
123} 123}
124 124
125/* Read ULEB128 value from buffer. */ 125/* Read ULEB128 value from buffer. */
126static uint32_t bcread_uleb128(LexState *ls) 126static LJ_AINLINE uint32_t bcread_uleb128(LexState *ls)
127{ 127{
128 const uint8_t *p = (const uint8_t *)ls->p; 128 uint32_t v = lj_buf_ruleb128(&ls->p);
129 uint32_t v = *p++; 129 lua_assert(ls->p <= ls->pe);
130 if (LJ_UNLIKELY(v >= 0x80)) {
131 int sh = 0;
132 v &= 0x7f;
133 do {
134 v |= ((*p & 0x7f) << (sh += 7));
135 bcread_dec(ls);
136 } while (*p++ >= 0x80);
137 }
138 bcread_dec(ls);
139 ls->p = (char *)p;
140 return v; 130 return v;
141} 131}
142 132
@@ -150,11 +140,10 @@ static uint32_t bcread_uleb128_33(LexState *ls)
150 v &= 0x3f; 140 v &= 0x3f;
151 do { 141 do {
152 v |= ((*p & 0x7f) << (sh += 7)); 142 v |= ((*p & 0x7f) << (sh += 7));
153 bcread_dec(ls);
154 } while (*p++ >= 0x80); 143 } while (*p++ >= 0x80);
155 } 144 }
156 bcread_dec(ls);
157 ls->p = (char *)p; 145 ls->p = (char *)p;
146 lua_assert(ls->p <= ls->pe);
158 return v; 147 return v;
159} 148}
160 149
@@ -438,24 +427,25 @@ GCproto *lj_bcread(LexState *ls)
438 bcread_error(ls, LJ_ERR_BCFMT); 427 bcread_error(ls, LJ_ERR_BCFMT);
439 for (;;) { /* Process all prototypes in the bytecode dump. */ 428 for (;;) { /* Process all prototypes in the bytecode dump. */
440 GCproto *pt; 429 GCproto *pt;
441 MSize len, startn; 430 MSize len;
431 const char *startp;
442 /* Read length. */ 432 /* Read length. */
443 if (ls->n > 0 && ls->p[0] == 0) { /* Shortcut EOF. */ 433 if (ls->p < ls->pe && ls->p[0] == 0) { /* Shortcut EOF. */
444 ls->n--; ls->p++; 434 ls->p++;
445 break; 435 break;
446 } 436 }
447 bcread_want(ls, 5); 437 bcread_want(ls, 5);
448 len = bcread_uleb128(ls); 438 len = bcread_uleb128(ls);
449 if (!len) break; /* EOF */ 439 if (!len) break; /* EOF */
450 bcread_need(ls, len); 440 bcread_need(ls, len);
451 startn = ls->n; 441 startp = ls->p;
452 pt = lj_bcread_proto(ls); 442 pt = lj_bcread_proto(ls);
453 if (len != startn - ls->n) 443 if (ls->p != startp + len)
454 bcread_error(ls, LJ_ERR_BCBAD); 444 bcread_error(ls, LJ_ERR_BCBAD);
455 setprotoV(L, L->top, pt); 445 setprotoV(L, L->top, pt);
456 incr_top(L); 446 incr_top(L);
457 } 447 }
458 if ((int32_t)ls->n > 0 || L->top-1 != bcread_oldtop(L, ls)) 448 if (ls->p < ls->pe || L->top-1 != bcread_oldtop(L, ls))
459 bcread_error(ls, LJ_ERR_BCBAD); 449 bcread_error(ls, LJ_ERR_BCBAD);
460 /* Pop off last prototype. */ 450 /* Pop off last prototype. */
461 L->top--; 451 L->top--;