8000 [GEN][ZH] Fix rebalanceChildSleepyUpdate debug runtime exceptions by Mauller · Pull Request #563 · TheSuperHackers/GeneralsGameCode · GitHub
[go: up one dir, main page]

Skip to content

[GEN][ZH] Fix rebalanceChildSleepyUpdate debug runtime exceptions#563

Closed
Mauller wants to merge 1 commit intoTheSuperHackers:mainfrom
Mauller:fix-debug-runtime-errors-genzh
Closed

[GEN][ZH] Fix rebalanceChildSleepyUpdate debug runtime exceptions#563
Mauller wants to merge 1 commit intoTheSuperHackers:mainfrom
Mauller:fix-debug-runtime-errors-genzh

Conversation

@Mauller
Copy link
@Mauller Mauller commented Apr 1, 2025

Previously known as: [GEN][ZH] Fix debug critical runtime errors

Fix critical runtime errors in generals and zero hour that are preventing debug from working.

Edit: I have split out a lot of the fixes that were within this PR into smaller PR's to make them easier to review individualy.
This PR now only contains fixes to the sleepy updates loop within GameLogic.

The issue with the sleep updates loop is that the passed in value i can create a child index that is beyond the end of the container it is used with. This will cause unexpected behaviour but is caught by the check on the while loop, the child pointer must be smaller than the end of container pointer.

This behaviour also occurs within the loop when the next i value and child index are calculated, so must be guarded within the loop as well.

The out of bounds access exception is only thrown during debug and prevents the use of debug. under normal gameplay the pointer returned should never be acted upon if it is beyond the bounds of the container.

This fix only concerns Debug and doesn't affect normal runtime, but it does prevent us from running debug at all.

Current known issues:
- Depends on: #464
* #489
~~* #536 ~~
* #537
* #538

@tintinhamans tintinhamans changed the title [GEN][ZH] Fix debut critical runtime errors [GEN][ZH] Fix debug critical runtime errors Apr 1, 2025
@xezon xezon added this to the Code foundation build up milestone Apr 1, 2025
@xezon xezon added Bug Something is not working right, typically is user facing Critical Severity: Minor < Major < Critical < Blocker Debug Is mostly debug functionality labels Apr 1, 2025
@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from 1de0acb to a1b8b1e Compare April 1, 2025 21:56
@Mauller
Copy link
Author
Mauller commented Apr 1, 2025

I believe i have sussed out the main issues in zero hour causing problems, there are others i have not pushed but they are asserts being hit due to issues with the main menus shell map.

Will sort out generals tomorrow.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from a1b8b1e to 3780304 Compare April 2, 2025 18:00
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the way this was meant to be written is

UpdateModulePtr* pSZ = &m_sleepyUpdates + m_sleepyUpdates.size();

Then all other changes here can be reverted.

Copy link
Author
@Mauller Mauller Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work, i already tried these.
The issue at this point is with the value of child going out of range of the container on line 2841.

I did it this way so i could use a local variable of container size to catch when child exceeded the size of the container, to minimise any performance penalty. ( What i took from the comments is that this gets hit a lot. )

In normal operation it runs fine, it's mostly debug that the issues occur, but we probably shouldn't be letting arrays return things out of bounds.

Copy link
@xezon xezon Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you seen this child being larger than the size? If not, then this check should be an assert or removed.

Copy link
Author
@Mauller Mauller Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes multiple times and it is why i put this guard here. m_sleepyUpdates is a std::list, which throws asserts under debug when you pass it an index outside of the arrays bounds.

child = ((i+1)<<1)-1;
// TheSuperHackers @fix Mauller 02/04/2025 Prevent invalid index into container throwing errors during debug
if(child >= containerSize) break;
pChild = &m_sleepyUpdates[child];

i can become greater than half the size of the container, which results in child becoming greater than the size of the container at this point.

This then triggers assert in the std library.

Under normal conditions since pChild becomes greater than pSZ, it doesn't casue a problem during normal gameplay.

while (pChild <= pSZ)

The original loop bellow on lines 2878, uses the iterator Child value and the container size as the test in the while loop, so this loop doesn't casue issues like the pointer based "unrolled loop" since everything gets checked for the child size against the container size before the container is accessed.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from f78229a to c28a408 Compare April 2, 2025 21:35
@Mauller Mauller self-assigned this Apr 3, 2025
@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch 2 times, most recently from 721ea34 to aa3b2b8 Compare April 4, 2025 17:15
@Mauller
Copy link
Author
Mauller commented Apr 4, 2025

This should be up to date now with changes and Generals inclusions.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from aa3b2b8 to b444274 Compare April 5, 2025 08:22
@Mauller
Copy link
Author
Mauller commented Apr 5, 2025

Rebased with main 05/04/2025

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from b444274 to 37aa1e5 Compare April 5, 2025 19:19
@Mauller
Copy link
Author
Mauller commented Apr 5, 2025

Rebased with main after recent cleanups and null manager.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from 37aa1e5 to 1ac75af Compare April 6, 2025 10:29
@Mauller Mauller marked this pull request as ready for review April 6, 2025 14:03
@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from 1ac75af to c474fbb Compare April 6, 2025 19:54
@Mauller
Copy link
Author
Mauller commented Apr 6, 2025

Updated with critical fixes to the script engine.

The change from STL port to STD library meant that iterators were being used in a way that was invalid in STD library.
A previous iterator was being compared against in a loop, but this would become invalidated if the container was resized due to an erase or insert. This then throws an assert due to a comparisson with an invalidated iterator.

The changed behaviour compares a pointer to the data within the previously seen iterator, which is then compared to the current iterators data pointer.

This change should match the behaviour that would have been seen when using STL port type iterators.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from d005531 to 877083a Compare April 7, 2025 17:46
@Mauller
Copy link
Author
Mauller commented Apr 7, 20 4D24 25

I think this is at a point now where it is pretty stable after adding further fixes for the miles audio and also fixing the script engine.

I have rebased it with latest Main, BFME shader additions, so it should be an easy merge.

The Miles audio issue turned out to be that it was trying to erase an iterator on the wrong container. So i have reverted some of the just recent changes.

For the script engine, i replicated what the original STL port iterators would have been doing by copying the address of the script the previous iterator was pointing to, then comparing that address with the address of the script the current iterator points to.

In theory this should replicate what the original iterators were doing and preserve the original behaviour of the script engine.
Moving Itadvanced bool out of the loop was just a small cleanup, better to not be constantly making a bool within a loop.

This build has been tested with the golden sample replay on a VC6 build and it does not mismatch and returns the expected score at the end of the game.

@Mauller
Copy link
Author
Mauller commented Apr 7, 2025

The supply center dock update also appears to have an issue related to it, there was also incorrectly cast pointers.

@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from 877083a to c915d0a Compare April 7, 2025 18:14
@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from c915d0a to 279db18 Compare April 7, 2025 18:39
@Mauller Mauller force-pushed the fix-debug-runtime-errors-genzh branch from 0c3d4b2 to 80ebb4b Compare April 8, 2025 17:51
@Mauller
Copy link
Author
Mauller commented Apr 8, 2025

Updated this PR to just contain the GameLogic sleepy updates commit. All other fixes have been pulled out into seperate PR's of related issues.

@Mauller Mauller changed the title [GEN][ZH] Fix debug critical runtime errors [GEN][ZH] Fix rebalanceChildSleepyUpdate debug runtime exceptions Apr 9, 2025
@Mauller Mauller added Fix Is fixing something, but is not user facing Gen Relates to Generals ZH Relates to Zero Hour labels Apr 9, 2025
@xezon
Copy link
xezon commented Apr 10, 2025

How can this issue be reproduced?

@Mauller
Copy link
Author
Mauller commented Apr 10, 2025

How can this issue be reproduced?

Pull #632 then run under debug, it will fall into the exception in sleepy updates pretty quick.

@xezon
Copy link
xezon commented Apr 10, 2025

Yes. Ran into this swiftly in shell map. I have put proper fix in new pull request #644 which makes it work the way it was intended. Your fix is correct in principle, but it adds 2 new branches, 1 in the loop, which is not necessary, and therefore is not the preferred fix for this.

@Mauller
Copy link
Author
Mauller commented Apr 10, 2025

Yes. Ran into this swiftly in shell map. I have put proper fix in new pull request #644 which makes it work the way it was intended. Your fix is correct in principle, but it adds 2 new branches, 1 in the loop, which is not necessary, and therefore is not the preferred fix for this.

Yeah that's all fair, was just looking over your change and it looks good to me, using pointer arithmetic like the rest of the loop instead.

The while then catches any issues if pChld goes out of bounds when entering and ending a loop iteration.
This PR can be closed for now then.

@xezon xezon closed this in #644 Apr 11, 2025
@Mauller Mauller deleted the fix-debug-runtime-errors-genzh branch April 11, 2025 15:56
@xezon xezon removed the Fix Is fixing something, but is not user facing label Jun 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something is not working right, typically is user facing Critical Severity: Minor < Major < Critical < Blocker Debug Is mostly debug functionality Gen Relates to Generals ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0