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

Don't access items in empty cache #2332

Merged
merged 4 commits into from
Sep 1, 2024

Conversation

blabber
Copy link
Collaborator

@blabber blabber commented Aug 1, 2024

While debugging an issue reported in LT85, I found that in certain circumstances an empty specialist output cache (introduced in 5c44029) is accessed, what crashed the client.

An obvious fix, contained in this PR, is limiting access to non-empty caches. There may be better ways to handle this though.

While here, I applied the same safety measure to the waste level cache, introduced in ecabf5d.

@blabber blabber requested a review from lmoureaux August 1, 2024 20:46
@jwrober jwrober self-requested a review August 2, 2024 14:53
@jwrober
Copy link
Collaborator

jwrober commented Aug 7, 2024

@lmoureaux Would you like to review this PR? It looks good to me and seems to fix the issue reported.

@blabber
Copy link
Collaborator Author

blabber commented Aug 8, 2024

In particular, I could imagine that a solution along these lines would be better, because it effectively continues to use the cache.

    output_type_iterate(stat_index)
    {
      int amount = 0;

      if (pcsoutputs &&  !pcsoutputs->empty())
      {
        amount = pcsoutputs->at(sp)[stat_index];
      }

      if (!pcsoutputs)
      {              
         amount = get_specialist_output(pcity, sp, stat_index);
      }

      output[stat_index] += count * amount;
    }

However, I was not sure how to interpret the empty cache.

@lmoureaux
Copy link
Contributor

Yes, I want to check where the empty cache comes from. Need to check the code again ^^

@lmoureaux
Copy link
Contributor

lmoureaux commented Aug 30, 2024

The cache is not created when the governor settings forbid specialists.

Stack trace of the crash:

#12 0x000055555585b2b3 in add_specialist_output(city const*, int*, std::vector<std::array<int, 6ul>, std::allocator<std::array<int, 6ul> > > const*) (pcity=0x55555c82c360, output=0x55555c82c484, pcsoutputs=0x55555a02ff10)
    at /home/louis/Personal/freeciv21/common/city.cpp:2249
#13 0x000055555585d4a6 in city_refresh_from_main_map(city*, bool*, std::vector<city*, std::allocator<city*> > const&, std::array<cached_waste, 6ul> const*, std::vector<std::array<int, 6ul>, std::allocator<std::array<int, 6ul> > > const*) (pcity=0x55555c82c360, workers_map=0x55555f476f70, gov_centers=std::vector of length 1, capacity 1 = {...}, pcwaste=0x55555a02ff40, pcsoutputs=0x55555a02ff10) at /home/louis/Personal/freeciv21/common/city.cpp:3050
#14 0x00005555559107d4 in apply_solution(cm_state*, partial_solution const*) (state=0x55555a02fe10, soln=0x55555a02fff8) at /home/louis/Personal/freeciv21/common/aicore/cm.cpp:704
#15 0x0000555555910956 in evaluate_solution(cm_state*, partial_solution const*) (state=0x55555a02fe10, soln=0x55555a02fff8) at /home/louis/Personal/freeciv21/common/aicore/cm.cpp:736
#16 0x000055555591364c in bb_next(cm_state*, bool) (state=0x55555a02fe10, negative_ok=false) at /home/louis/Personal/freeciv21/common/aicore/cm.cpp:1809
#17 0x0000555555914424 in cm_find_best_solution(cm_state*, cm_parameter const*, std::unique_ptr<cm_result, std::default_delete<cm_result> >&, bool)
    (state=0x55555a02fe10, parameter=0x7fffffffcc60, result=std::unique_ptr<cm_result> = {...}, negative_ok=false) at /home/louis/Personal/freeciv21/common/aicore/cm.cpp:2090
#18 0x0000555555914515 in cm_query_result(city*, cm_parameter const*, std::unique_ptr<cm_result, std::default_delete<cm_result> >&, bool)

lmoureaux added a commit to lmoureaux/freeciv21 that referenced this pull request Aug 30, 2024
Governors that forbid specialists do not bother computing how much
specialists would produce and end up with an empty specialist output
cache. When applying solutions to evaluate them, however, we were trying
to use the cached values. Only do it when there specialists are actually
present in the solution.

See longturn#2332.
lmoureaux added a commit to lmoureaux/freeciv21 that referenced this pull request Aug 30, 2024
Governors that forbid specialists do not bother computing how much
specialists would produce and end up with an empty specialist output
cache. When applying solutions to evaluate them, however, we were trying
to use the cached values. Only do it when there specialists are actually
present in the solution.

See longturn#2332.
@lmoureaux
Copy link
Contributor

#2355 addresses the root cause but in a less defensive way.

lmoureaux added a commit that referenced this pull request Aug 30, 2024
Governors that forbid specialists do not bother computing how much
specialists would produce and end up with an empty specialist output
cache. When applying solutions to evaluate them, however, we were trying
to use the cached values. Only do it when there specialists are actually
present in the solution.

See #2332.
@lmoureaux
Copy link
Contributor

Even after #2355 we may want to keep checking that the cache isn't empty. I'm only mildly optimistic that the issue won't appear in a different code path.

Apply the same defensive measure, we applied to specialists output
cache, to the waste cache.
common/city.cpp Outdated
@@ -2247,6 +2247,9 @@ void add_specialist_output(
// This is more than just an optimization. For governors that forbid
// specialists, the cache may not be filled.
if (count > 0) {
// If there is a cache it must not be empty.
fc_assert(!pcsoutputs || !pcsoutputs->empty());
Copy link
Contributor

Choose a reason for hiding this comment

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

fc_ asserts aren't fatal by default. Use fc_assert_action(condition, continue) to skip this specialist, which is probably an ok fallback behavior:

Suggested change
fc_assert(!pcsoutputs || !pcsoutputs->empty());
fc_assert_action(!pcsoutputs || !pcsoutputs->empty(), continue);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I implemented another solution, falling back to the old behaviour by setting the cache to nullptr if the assertion triggers. This makes sure, that the game will behave correctly. What do you think?

common/city.cpp Outdated
@@ -2876,6 +2879,9 @@ void set_city_production(struct city *pcity,
* on, so shield waste will include shield bonuses. */
output_type_iterate(o)
{
// If there is a cache it must not be empty.
fc_assert(!pcwaste || !pcwaste->empty());
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably go before the loop to avoid getting 6 messages every time. I think returning if the assert fails is fine, so use fc_assert_ret(condition).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Same here.

Fallback to the behavior used prior to the introduction of these
caches, if the assertion checking for an empty cache is triggered.

While here, move an assertion outside a loop to make sure it is not
checked for each iteration.
Copy link
Contributor

@lmoureaux lmoureaux left a comment

Choose a reason for hiding this comment

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

That's a nicer solution!

@lmoureaux lmoureaux enabled auto-merge (rebase) September 1, 2024 21:41
@lmoureaux lmoureaux merged commit 625c10c into longturn:master Sep 1, 2024
20 checks passed
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.

3 participants