Skip to content

Cleanup two ASAN initialization order issues #2330

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

Merged
merged 2 commits into from
Mar 12, 2023
Merged

Cleanup two ASAN initialization order issues #2330

merged 2 commits into from
Mar 12, 2023

Conversation

kcgen
Copy link
Member

@kcgen kcgen commented Mar 12, 2023

Render pacer

Previously the pacer was a global object initialized on startup, however it accessed timer values even before they had a chance to initialize:

2023-03-11_19-38

The first commit moves the pacer's construction to when its conf setting is parsed.

Mouse interface objects

Similar to the pacer, previously the mouse interfaces were initialized on startup, however they accessed members of the two mouse state variables (mouse_info and mouse_shared) even before they had a chance to initialize:

2023-03-11_19-39

The second commit moves the interface construction into the mouse interface initialization function.

In both commits, the objects are now managed using std::unique_ptrs.

kcgen added 2 commits March 11, 2023 18:51
Constructing the pacer along with the other SDL conf
items fixes an initialization-order-fiasco caught by ASAN.

The issue is that the pacer tracks elapsed time and takes
a measurement at construction time - however, because
it (was previously) a global, it was reading timer values
before they were assigned.
Constructing the mouse interfaces during the mouse init
phase fixes an initialization-order-fiasco caught by ASAN:

==1147016==ERROR: AddressSanitizer:
initialization-order-fiasco on address 0x559608682e88 at pc
0x559602ee8108 bp 0x7ffc4c848510 sp 0x7ffc4c848500 READ of
size 8 at 0x559608682e88 thread T0

An initialization race previously occured: the interfaces
were previously being initializaed before the MouseInfo and
MouseShared objects (both also globals) members, however the
interfaces need to use their members.

Test with ASAN
~~~~~~~~~~~~~~

Setup:

  meson setup -Dbuildtype=debug -Db_sanitize=address build/asan

Compile:

  meson compile -C build/asan

Binary:

  build/asan/dosbox

Run with:

  export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:detect_leaks=1
@kcgen kcgen added the cleanup Non-functional changes that simplify, improve maintainability, or squash warnings label Mar 12, 2023
@kcgen kcgen requested review from johnnovak and FeralChild64 March 12, 2023 05:39
@kcgen kcgen self-assigned this Mar 12, 2023
Copy link
Collaborator

@FeralChild64 FeralChild64 left a comment

Choose a reason for hiding this comment

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

Looks OK to me.

@kcgen kcgen merged commit 2559f83 into main Mar 12, 2023
@kcgen kcgen deleted the kc/init-order-1 branch March 16, 2023 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleanup Non-functional changes that simplify, improve maintainability, or squash warnings
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants