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

Crash after editing a running public function #1628

Open
hexagonrecursion opened this issue Nov 22, 2023 · 1 comment
Open

Crash after editing a running public function #1628

hexagonrecursion opened this issue Nov 22, 2023 · 1 comment

Comments

@hexagonrecursion
Copy link
Contributor

If I edit a public function on bot A while a program that uses it is running on bot B, this (in some cases) leads to a crash. This is because the function is replaced after I edit it and bot B now tries to run the updated function, but the stack was populated by the previous version of the function. This may lead to a violation of invariants expected by CBot instructions and weird behaviour or even a crash. I constructed one example, but I am sure many more can be constructed - every instruction has it's own invariants.

Example

  1. Load this into bot A:
    public void Foo()
    {
    	{
    		{
    			{}
    			while(true);
    		}
    	}
    }
    
  2. Run this in bot B:
    extern void New()
    {
    	Foo();
    }
    
  3. Replace the program in bot A with:
    public void Foo()
    {
    	message("hello");
    }
    
  4. The game crashes - segmentation fault (null pointer dereference)

Why? Because the code that creates a CBotVar to hold the return value of message() got skipped. We now have a null where a CBotVar was expected.

Thoughts about a fix

  1. We could handle the stack very defensively - don't assume it preserves any invariants. The downside is that this bug becomes a "whack-a-mole" - many edge cases like the example above probably exist - we have no way to know if we fixed them all. Also: this approach may substantially increase the complexity of the already hard-to-understand code in CBot/src/CBot/CBotInstr/
  2. We could make copies of the public functions used by a program when it starts executing. Then the code will not change while it is running. The downside is that we have to somehow handle the game being saved and reloaded - the copies in memory are not enough - we would have to save the copies to disk as well.
  3. We could just stop all CBot programs that use a public function when the function is edited.

Related

  1. A similar bug with updated AI code for Aliens - the code is replaced and the new code does not like the stack left over from the old code
  2. In some cases the game crashes even if the code did not change - just opening the editor and clicking OK is enough
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants