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

fix(core,developer): use NDEBUG flag to disable assertions in release build #12715

Merged

Conversation

mcdurdin
Copy link
Member

@mcdurdin mcdurdin commented Nov 27, 2024

This disables assertions in release builds for all meson-built projects.

See d71cb56 for deeper discussion on the yak shave that impacted all the unit tests.

image

Fixes: #12619
Fixes: #12453 (by implication)

@keymanapp-test-bot skip

…or VC++

This disables assertions in release builds in Windows only. Other
platforms may have different outcomes.

Fixes: #12619
@keymanapp-test-bot
Copy link

keymanapp-test-bot bot commented Nov 27, 2024

@keymanapp-test-bot keymanapp-test-bot bot added this to the A18S16 milestone Nov 27, 2024
@github-actions github-actions bot added common/ common/resources/ Build infrastructure core/ Keyman Core fix labels Nov 27, 2024
Comment on lines +445 to +450
{$IFDEF DEBUG}
configuration := 'debug';
{$ELSE}
configuration := 'release';
{$ENDIF}
Exit(keyman_root + 'core\build\x86\'+configuration+'\src\' + Filename);
Copy link
Member Author

Choose a reason for hiding this comment

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

Allows us to test both release and debug builds of Core in the Debugger when running within the keymanapp/keyman repo

@mcdurdin
Copy link
Member Author

Hmm, unit tests have very unexpectedly failed for Test: Keyman Core - Windows (Common) with this minimal change. More digging needed, tomorrow.

@ermshiperete
Copy link
Contributor

Ah, you made the change in resources/build/meson/standard.meson.build and so it gets picked up by all the platform builds. I don't think anything else is needed on the Linux side.

ermshiperete
ermshiperete previously approved these changes Nov 27, 2024
Copy link
Contributor

@ermshiperete ermshiperete left a comment

Choose a reason for hiding this comment

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

LGTM

@mcdurdin
Copy link
Member Author

Ah, you made the change in resources/build/meson/standard.meson.build and so it gets picked up by all the platform builds. I don't think anything else is needed on the Linux side.

I constrained the check to msvc though

@ermshiperete
Copy link
Contributor

Ah, right. For Linux at least it's the same, so I think we should move it outside of the msvc section.

rc-swag
rc-swag previously approved these changes Nov 28, 2024
Copy link
Contributor

@rc-swag rc-swag left a comment

Choose a reason for hiding this comment

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

LGTM

Had a real yak shave this morning with disabling assertions in release
builds in our C/C++ code. It turns out that our unit tests use
`assert()` which we intended to use from `test_assert.h`, but in some
cases `cassert` or `assert.h` had been #included after `test_assert.h`,
overriding our special `assert()` macro. The chain of includes is
somewhat hard to puzzle out -- it's often buried several levels deep.
This meant that a release build would drop all test assertions, meaning
most tests passed, unsurprisingly, as there were no assertions left to
fail ... but some tests failed with crashes because we optimized out
important lines such as `assert(some_important_function())`.

I was quite unhappy with this fragility, so I have opted to rename
`assert()` to `test_assert()` in all of our home-grown C/C++ unit tests,
which further highlighted unit tests which were only using the C/C++
`assert()` and not ours, so then had to figure out which unit test
executables needed to have `test_assert` added, and then ... then ...
discovered a bug in `test_color.h`, where we were #including
`io.h`/`unistd.h` inside a `namespace console_color {}` block, which
just happened to be the first ref to those beautiful headers, and thus
(because `#pragma once`) meant that useful little functions like
`access()` were no longer accessible to us in the global namespace.

I have also audited Every Single Call to `assert()` to verify that we do
not do Important Work inside the parentheses, and, apart from those
offending unit tests, now resolved with `test_assert()`, it looks like
all is good.

I would like to present one very well-shaved yak in this commit.

Fixes: #12619
@@ -7,7 +7,7 @@
#

# Note: this version of cmpfiles ignores line endings, which is better for platform independence
cmpfiles = ['-c', 'import sys; a = open(sys.argv[1], \'r\').read(); b = open(sys.argv[2], \'r\').read(); exit(not (a==b))']
cmpfiles = ['-c', 'import sys; a = open(sys.argv[1], \'r\').read(); b = open(sys.argv[2], \'r\').read(); sys.exit(not (a==b))']
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated ... but maybe due to my python version? In any case sys.exit is probably safer

@mcdurdin mcdurdin changed the title fix(core): use NDEBUG flag to disable assertions in release build for VC++ fix(core,developer): use NDEBUG flag to disable assertions in release build Nov 28, 2024
@mcdurdin mcdurdin dismissed stale reviews from ermshiperete and rc-swag November 28, 2024 02:57

big changes due to assert() in unit tests

@mcdurdin
Copy link
Member Author

The yak shave isn't over:

  ../../../src/action.cpp: In function ‘bool km::core::action_item_list_to_actions_object(const km_core_action_item*, km_core_actions*)’:
  ../../../src/action.cpp:64:20: error: variable ‘last_context_item’ set but not used [-Werror=unused-but-set-variable]
     64 |               auto last_context_item = output.back();
        |                    ^~~~~~~~~~~~~~~~~
  ../../../src/action.cpp:74:20: error: variable ‘last_context_item’ set but not used [-Werror=unused-but-set-variable]
     74 |               auto last_context_item = output.back();
        |                    ^~~~~~~~~~~~~~~~~
  cc1plus: all warnings being treated as errors

Presumably last_context_item is only used in an assertion ... so need to #ifdef it away.

@SabineSIL
Copy link
Contributor

..changing of assert_failed to test_assert_failed looks good in all files;
... can`t say much about_ ( but it seem OK)

  • removing libraries in common/include/test_color.h
  • changes in common/windows/delphi/general/KeymanPaths.pas
  • using sys.exit instead of exit in core/tests/meson.build

@darcywong00
Copy link
Contributor

Is the discussion on d71cb56 the kind we want to summarize into /core/docs/internal/ ?

Particularly the fragility of assert() and tests

@mcdurdin
Copy link
Member Author

mcdurdin commented Nov 28, 2024

Is the discussion on d71cb56 the kind we want to summarize into /core/docs/internal/ ?

Particularly the fragility of assert() and tests

We could add something to our test wiki; it's not really internal Keyman-relevant documentation, more a trap-for-young-players-in-c++ (i.e. how to use tools properly)

Copy link
Contributor

@darcywong00 darcywong00 left a comment

Choose a reason for hiding this comment

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

questions about ldml_*.cpp

core/src/ldml/ldml_markers.cpp Show resolved Hide resolved
Copy link
Contributor

@darcywong00 darcywong00 left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@rc-swag rc-swag left a comment

Choose a reason for hiding this comment

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

lgtm
re-review

@mcdurdin mcdurdin merged commit f747984 into master Dec 2, 2024
28 checks passed
@mcdurdin mcdurdin deleted the fix/core/12619-disable-assertions-vcwin-release-build branch December 2, 2024 01:02
@keyman-server
Copy link
Collaborator

Changes in this pull request will be available for download in Keyman version 18.0.150-alpha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
7 participants