Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pattern cache key collision bug #21

Merged
merged 1 commit into from
Sep 19, 2018
Merged

Conversation

Khatskevich
Copy link
Contributor

This PR closes #10

Complex patterns are cached by this library to
speedup parsing.
In rare cases the parser was initialized with errors and did
not parse a valid string.

Factors which lead to the error:
1. The pattern cache is weak by value.
2. Intermediate syntax constructions are garbagecollected after
   those were used.
3. Key in the cache is a raw address sometimes.

There was possible the following case:
1. User defines grammar: `c = a + b`.
2. `c` is cached by addresses of `a` and `b`
3. `a` and `b` are garbagecollected
4. User defines grammar: `c1 = a1 + b1`.
5. There is a chance that `a1` and `b1` have got the
   same addresses as was `a` and `b`; in that case
   `c1` would be wrongly taken from the cache as its
   caching key is the same as for `c`.
6. Parser works wrong.

This commit introduces unique pattern ids, which are generated
by a counter. Cache key is then calculated based on those
values.
@pygy
Copy link
Owner

pygy commented Sep 18, 2018

Hi @Khatskevich, thanks for the patch.

The changes in API.lua make total sense to me, but I don't understand the ones in constructors.lua. Is it a workaround for a weak tables bug, or do I just need some sleep (I do, it's almost 1am here)?

@Khatskevich
Copy link
Contributor Author

Yes, you are right.
Changes to constructors.lua are redundant.

@Khatskevich
Copy link
Contributor Author

Changing constructors.lua
Pros:

  1. Unified cache key (__ptid%d)
    Cons:
  2. As I understand this prohibits using pattern names like __ptid%d

I can refactor the PR. Should I revert the constructors.lua?
In that case I would augment the pt with id straight in API in case the pt does not have one during nameify.

@pygy
Copy link
Owner

pygy commented Sep 19, 2018

Oh, no, you're right, now I get it.

The collisions stem from the fact that tostring(table) and tostring(proxy) return a stringified version of their memory location. After one pattern is GC'ed, another may be allocated at the same place in memory.

I did need some sleep :-)

Great catch, and great patch!

@pygy pygy merged commit 20aa38a into pygy:master Sep 19, 2018
@Totktonada Totktonada mentioned this pull request Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

instable result for a thirdparty testsuite
2 participants