When parser reuses constants, only floats can collide

Ensure that float constants never use integer keys, so that only
floats can collide in 'k2proto'.
This commit is contained in:
Roberto Ierusalimschy
2024-12-28 15:03:48 -03:00
parent f81d0bbd4f
commit 2a307f898b

54
lcode.c
View File

@@ -557,8 +557,6 @@ static int addk (FuncState *fs, Proto *f, TValue *v) {
** and try to reuse constants. Because some values should not be used ** and try to reuse constants. Because some values should not be used
** as keys (nil cannot be a key, integer keys can collapse with float ** as keys (nil cannot be a key, integer keys can collapse with float
** keys), the caller must provide a useful 'key' for indexing the cache. ** keys), the caller must provide a useful 'key' for indexing the cache.
** Note that all functions share the same table, so entering or exiting
** a function can make some indices wrong.
*/ */
static int k2proto (FuncState *fs, TValue *key, TValue *v) { static int k2proto (FuncState *fs, TValue *key, TValue *v) {
TValue val; TValue val;
@@ -567,15 +565,14 @@ static int k2proto (FuncState *fs, TValue *key, TValue *v) {
int k; int k;
if (!tagisempty(tag)) { /* is there an index there? */ if (!tagisempty(tag)) { /* is there an index there? */
k = cast_int(ivalue(&val)); k = cast_int(ivalue(&val));
lua_assert(k < fs->nk); /* collisions can happen only for float keys */
/* correct value? (warning: must distinguish floats from integers!) */ lua_assert(ttisfloat(key) || luaV_rawequalobj(&f->k[k], v));
if (ttypetag(&f->k[k]) == ttypetag(v) && luaV_rawequalobj(&f->k[k], v)) return k; /* reuse index */
return k; /* reuse index */
} }
/* constant not found; create a new entry */ /* constant not found; create a new entry */
k = addk(fs, f, v); k = addk(fs, f, v);
/* cache for reuse; numerical value does not need GC barrier; /* cache it for reuse; numerical value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */ table is not a metatable, so it does not need to invalidate cache */
setivalue(&val, k); setivalue(&val, k);
luaH_set(fs->ls->L, fs->kcache, key, &val); luaH_set(fs->ls->L, fs->kcache, key, &val);
return k; return k;
@@ -607,27 +604,32 @@ static int luaK_intK (FuncState *fs, lua_Integer n) {
** with actual integers. To that, we add to the number its smaller ** with actual integers. To that, we add to the number its smaller
** power-of-two fraction that is still significant in its scale. ** power-of-two fraction that is still significant in its scale.
** For doubles, that would be 1/2^52. ** For doubles, that would be 1/2^52.
** (This method is not bulletproof: there may be another float ** This method is not bulletproof: different numbers may generate the
** with that value, and for floats larger than 2^53 the result is ** same key (e.g., very large numbers will overflow to 'inf') and for
** still an integer. At worst, this only wastes an entry with ** floats larger than 2^53 the result is still an integer. At worst,
** a duplicate.) ** this only wastes an entry with a duplicate.
*/ */
static int luaK_numberK (FuncState *fs, lua_Number r) { static int luaK_numberK (FuncState *fs, lua_Number r) {
TValue o; TValue o, kv;
lua_Integer ik; setfltvalue(&o, r); /* value as a TValue */
setfltvalue(&o, r); if (r == 0) { /* handle zero as a special case */
if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */ setpvalue(&kv, fs); /* use FuncState as index */
return k2proto(fs, &o, &o); /* use number itself as key */ return k2proto(fs, &kv, &o); /* cannot collide */
else { /* must build an alternative key */ }
else {
const int nbm = l_floatatt(MANT_DIG); const int nbm = l_floatatt(MANT_DIG);
const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1); const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */ const lua_Number k = r * (1 + q); /* key */
TValue kv; lua_Integer ik;
setfltvalue(&kv, k); setfltvalue(&kv, k); /* key as a TValue */
/* result is not an integral value, unless value is too large */ if (!luaV_flttointeger(k, &ik, F2Ieq)) { /* not an integral value? */
lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) || int n = k2proto(fs, &kv, &o); /* use key */
l_mathop(fabs)(r) >= l_mathop(1e6)); if (luaV_rawequalobj(&fs->f->k[n], &o)) /* correct value? */
return k2proto(fs, &kv, &o); return n;
}
/* else, either key is still an integer or there was a collision;
anyway, do not try to reuse constant; instead, create a new one */
return addk(fs, fs->f, &o);
} }
} }
@@ -658,7 +660,7 @@ static int boolT (FuncState *fs) {
static int nilK (FuncState *fs) { static int nilK (FuncState *fs) {
TValue k, v; TValue k, v;
setnilvalue(&v); setnilvalue(&v);
/* cannot use nil as key; instead use table itself to represent nil */ /* cannot use nil as key; instead use table itself */
sethvalue(fs->ls->L, &k, fs->kcache); sethvalue(fs->ls->L, &k, fs->kcache);
return k2proto(fs, &k, &v); return k2proto(fs, &k, &v);
} }