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

Resolve 2 UBSAN warnings about uninitialized vars; establish allocation/type check along with it #574

Merged
merged 11 commits into from
Sep 18, 2024

Conversation

jengelh
Copy link
Contributor

@jengelh jengelh commented Sep 8, 2024

Pull Request Type

  • Runtime changes
    • Other changes

Description

UBSAN evaluates use of undefined variable contents at runtime; it's more effective against unintialized heap allocations, whereas compile-time is more effective to mark code paths that are perhaps not exercised during gameplay at all.
mem_malloc is rewritten to catch potentially allocating nontrivial class types, the latter of which is a result of adding initializers. This supersedes 7777720 which only fixed a few struct members.

Related Issues

#568

Checklist

  • I have tested my changes locally and verified that they work as intended.
  • I understand that by submitting this pull request, I am agreeing to license my contributions under the project's license.

@Lgt2x Lgt2x mentioned this pull request Sep 9, 2024
13 tasks
Copy link
Member

@Lgt2x Lgt2x left a comment

Choose a reason for hiding this comment

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

Very clean!

mem/mem.h Outdated Show resolved Hide resolved
@Lgt2x
Copy link
Member

Lgt2x commented Sep 16, 2024

Ok to merge when trivial conflict with logging module is solved

UBSAN is reporting that some uninitialized variables. To that end, I
would like to add member default initializers to e.g. ``struct
object``. Doing that makes classes nontrivial.

Allocations throughout the code occur with e.g.
``mem_malloc(sizeof(T))``, which is, tersely speaking, incompatible
with triviality. Therefore, mem_malloc call sites will be rewritten
to invoke ``mem_rmalloc<T>`` instead, to enforce a compile-time check
that ``T`` is indeed trivial.

``mem_rmalloc<T>`` is modeled to look like ``make_unique<T>``, i.e.
can be called with no arguments to allocate space for a single thing,
or called with a size_t argument that specifies the number of
elements (automatic byte sizing is applied internally).
git grep -l mem_malloc | xargs perl -i -lpe 's{\((\w+) \*\)mem_malloc\(sizeof\(\1\)\)}{mem_rmalloc<$1>()}'
Automated replacement with

```
git grep -l mem_malloc |
xargs perl -i -lpe 's{\((\w+) \*\)mem_malloc\(sizeof\(\1\) \* (\S+)\)}{mem_rmalloc<$1>($2)}'
```
```
git grep -l mem_malloc | xargs perl -i -lpe 's{\((\w+) \*\*\)mem_malloc\(sizeof\(\1 \*\) \* (\S+)\)}{mem_rmalloc<$1>($2)}'
```
```
git grep -l mem_malloc | xargs perl -i -lpe 's{\((\w+) \*\)mem_malloc\((\S+) \* sizeof\(\1\)\)}{mem_rmalloc<$1 *>($2)}'
```
```
git grep -l mem_malloc | xargs perl -i -lpe 's{\((\w+) \*\*\)mem_malloc\((\S+) \* sizeof\(\1 \*\)\)}{mem_rmalloc<$1 *>($2)}'
```
```
git grep -l mem_malloc | xargs perl -i -lpe 's{\((uint8_t) \*\)mem_malloc\((\S+)\)}{mem_rmalloc<$1>($2)}'
```
```
git grep -l mem_malloc | xargs perl -i -lpe 's{\((char) \*\)mem_malloc\((\S+)\)}{mem_rmalloc<$1>($2)}'
```
(Manual modification.) Change mem_malloc callsites with
non-fundamental types to mem_rmalloc.
lnxgameController::flush accesses this->m_MouseActive before that
member being initialized with something sensible, making ASAN report:

linux/lnxcontroller.cpp:259:8: runtime error: load of value 190, which is not a valid value for type 'bool'
linux/lnxcontroller.cpp:259:33: runtime error: load of value 190, which is not a valid value for type 'bool'

Thanks to the introduction of mem_rmalloc, it has been established
that struct lnxgameController is not malloc'd anywhere, so any
instantiation runs its constructor properly.
UBSAN warns:

$GIT/Descent3/object.cpp:2452:91: runtime error: load of value 2, which is not a valid value for type 'bool'
@Lgt2x Lgt2x merged commit a33c79c into DescentDevelopers:main Sep 18, 2024
10 checks passed
@jengelh jengelh deleted the uninit_vars branch October 5, 2024 22:29
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.

2 participants