prometheus-lua / Prometheus

Lua Obfuscator written in pure Lua
https://levno-710.gitbook.io/prometheus/
GNU Affero General Public License v3.0
212 stars 65 forks source link

[BUG] VMify "Unresolved Upvalue" error during special repeat until case #171

Open 9382 opened 1 month ago

9382 commented 1 month ago

VMify will fail if there's a repeat until statement that references a local in the until condition that was first defined in the repeat's body

Code to reproduce:

repeat
    local x = 5
until x == 5 -- The condition here should see the `local x = 5` above, as they are actually contained in the same scope

The actual code that parses the repeat until (src/prometheus/parser.lua#L225-L233) is parsing the condition with the return's scope, which should be good, but for some reason something later is breaking because of this, and I have no idea what

Zaenalos commented 1 month ago

VMIFY Scoping problem.

Zaenalos commented 1 month ago

that

There is a possible fix for this. 

Possible fix #1: 

Add a boolean reference in Repeat Statement AST named "hasUpvalue", set it to false if one of the condition variables only refers inside the Repeat loop or true by default; all of this can be done during parsing, and then add a check like this in the compiler.

if statement.hasUpvalue then

    -- Handle the condition first, which means no local variable refers from body to condition, which is safe.

else

   -- Handle the loop body first (this is where the fix should be applied).

end

before the code:

local conditionReg = self:compileExpression(statement.condition, funcDepth, 1)[1];

Possible fix #2:

Add a function in the compiler that safely traverses expressions, so that you can check if there is a local variable inside the Repeat loop that is being used in the condition. 

levno-710 commented 3 weeks ago

If anyone is willing to fix this issue, I am willing to accept a PR, but I currently do not have time, to fix this myself due to personal reasons.