diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lj_bcread.c | 88 | ||||
-rw-r--r-- | src/lj_lex.c | 18 | ||||
-rw-r--r-- | src/lj_lex.h | 4 | ||||
-rw-r--r-- | src/lj_lib.c | 2 |
4 files changed, 50 insertions, 62 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. */ |
47 | static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) | 47 | static 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. */ |
87 | static LJ_AINLINE void bcread_need(LexState *ls, MSize len) | 90 | static 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. */ |
94 | static LJ_AINLINE void bcread_want(LexState *ls, MSize len) | 97 | static 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. */ |
104 | static uint8_t *bcread_mem(LexState *ls, MSize len) | 104 | static 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. */ |
119 | static LJ_AINLINE uint32_t bcread_byte(LexState *ls) | 119 | static 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. */ |
126 | static uint32_t bcread_uleb128(LexState *ls) | 126 | static 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--; |
diff --git a/src/lj_lex.c b/src/lj_lex.c index 2c5128e7..5a8f1003 100644 --- a/src/lj_lex.c +++ b/src/lj_lex.c | |||
@@ -45,17 +45,17 @@ TKDEF(TKSTR1, TKSTR2) | |||
45 | static LJ_NOINLINE LexChar lex_more(LexState *ls) | 45 | static LJ_NOINLINE LexChar lex_more(LexState *ls) |
46 | { | 46 | { |
47 | size_t sz; | 47 | size_t sz; |
48 | const char *buf = ls->rfunc(ls->L, ls->rdata, &sz); | 48 | const char *p = ls->rfunc(ls->L, ls->rdata, &sz); |
49 | if (buf == NULL || sz == 0) return LEX_EOF; | 49 | if (p == NULL || sz == 0) return LEX_EOF; |
50 | ls->n = (MSize)sz - 1; | 50 | ls->pe = p + sz; |
51 | ls->p = buf; | 51 | ls->p = p + 1; |
52 | return (LexChar)(uint8_t)*ls->p++; | 52 | return (LexChar)(uint8_t)p[0]; |
53 | } | 53 | } |
54 | 54 | ||
55 | /* Get next character. */ | 55 | /* Get next character. */ |
56 | static LJ_AINLINE LexChar lex_next(LexState *ls) | 56 | static LJ_AINLINE LexChar lex_next(LexState *ls) |
57 | { | 57 | { |
58 | return (ls->c = ls->n ? (ls->n--,(LexChar)(uint8_t)*ls->p++) : lex_more(ls)); | 58 | return (ls->c = ls->p < ls->pe ? (LexChar)(uint8_t)*ls->p++ : lex_more(ls)); |
59 | } | 59 | } |
60 | 60 | ||
61 | /* Save character. */ | 61 | /* Save character. */ |
@@ -368,8 +368,7 @@ int lj_lex_setup(lua_State *L, LexState *ls) | |||
368 | int header = 0; | 368 | int header = 0; |
369 | ls->L = L; | 369 | ls->L = L; |
370 | ls->fs = NULL; | 370 | ls->fs = NULL; |
371 | ls->n = 0; | 371 | ls->pe = ls->p = NULL; |
372 | ls->p = NULL; | ||
373 | ls->vstack = NULL; | 372 | ls->vstack = NULL; |
374 | ls->sizevstack = 0; | 373 | ls->sizevstack = 0; |
375 | ls->vtop = 0; | 374 | ls->vtop = 0; |
@@ -379,9 +378,8 @@ int lj_lex_setup(lua_State *L, LexState *ls) | |||
379 | ls->linenumber = 1; | 378 | ls->linenumber = 1; |
380 | ls->lastline = 1; | 379 | ls->lastline = 1; |
381 | lex_next(ls); /* Read-ahead first char. */ | 380 | lex_next(ls); /* Read-ahead first char. */ |
382 | if (ls->c == 0xef && ls->n >= 2 && (uint8_t)ls->p[0] == 0xbb && | 381 | if (ls->c == 0xef && ls->p + 2 <= ls->pe && (uint8_t)ls->p[0] == 0xbb && |
383 | (uint8_t)ls->p[1] == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ | 382 | (uint8_t)ls->p[1] == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ |
384 | ls->n -= 2; | ||
385 | ls->p += 2; | 383 | ls->p += 2; |
386 | lex_next(ls); | 384 | lex_next(ls); |
387 | header = 1; | 385 | header = 1; |
diff --git a/src/lj_lex.h b/src/lj_lex.h index 71a7c1de..3e76e72a 100644 --- a/src/lj_lex.h +++ b/src/lj_lex.h | |||
@@ -54,11 +54,11 @@ typedef struct LexState { | |||
54 | struct lua_State *L; /* Lua state. */ | 54 | struct lua_State *L; /* Lua state. */ |
55 | TValue tokval; /* Current token value. */ | 55 | TValue tokval; /* Current token value. */ |
56 | TValue lookaheadval; /* Lookahead token value. */ | 56 | TValue lookaheadval; /* Lookahead token value. */ |
57 | const char *p; /* Current position in input buffer. */ | ||
58 | const char *pe; /* End of input buffer. */ | ||
57 | LexChar c; /* Current character. */ | 59 | LexChar c; /* Current character. */ |
58 | LexToken tok; /* Current token. */ | 60 | LexToken tok; /* Current token. */ |
59 | LexToken lookahead; /* Lookahead token. */ | 61 | LexToken lookahead; /* Lookahead token. */ |
60 | MSize n; /* Bytes left in input buffer. */ | ||
61 | const char *p; /* Current position in input buffer. */ | ||
62 | SBuf sb; /* String buffer for tokens. */ | 62 | SBuf sb; /* String buffer for tokens. */ |
63 | lua_Reader rfunc; /* Reader callback. */ | 63 | lua_Reader rfunc; /* Reader callback. */ |
64 | void *rdata; /* Reader callback data. */ | 64 | void *rdata; /* Reader callback data. */ |
diff --git a/src/lj_lib.c b/src/lj_lib.c index 79c2c99a..b6aa97a0 100644 --- a/src/lj_lib.c +++ b/src/lj_lib.c | |||
@@ -55,7 +55,7 @@ static const uint8_t *lib_read_lfunc(lua_State *L, const uint8_t *p, GCtab *tab) | |||
55 | memset(&ls, 0, sizeof(ls)); | 55 | memset(&ls, 0, sizeof(ls)); |
56 | ls.L = L; | 56 | ls.L = L; |
57 | ls.p = (const char *)(p+len); | 57 | ls.p = (const char *)(p+len); |
58 | ls.n = ~(MSize)0; | 58 | ls.pe = (const char *)~(uintptr_t)0; |
59 | ls.c = -1; | 59 | ls.c = -1; |
60 | ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE)); | 60 | ls.level = (BCDUMP_F_STRIP|(LJ_BE*BCDUMP_F_BE)); |
61 | ls.chunkname = name; | 61 | ls.chunkname = name; |