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

tests/libc_newlib: fix pointers comparison for llvm #10014

Merged
merged 1 commit into from
Sep 25, 2018

Conversation

cladmi
Copy link
Contributor

@cladmi cladmi commented Sep 21, 2018

Contribution description

With llvm and samr21-xpro, I could not directly do 'printf == iprintf'.
And I could not cast them to an integer value in a portable way.
So I use real function pointers for this.
However comparing printf_addr with iprintf_addr does not work if
there is not the memcmp to somehow 'force them' to be pointers...
So I used the 'memcmp' for the comparison.

Testing procedure

Run make clean flash test BOARD=samr21-xpro TOOLCHAIN=llvm and it should work.

I tested with pic32-wifire and also tested to disable newlib-nano or nano.specs for the samr21-xpro to test the error cases.

Issues/PRs references

Found by #9809

@cladmi cladmi added Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR Area: toolchain Area: toolchains; everything related to compilation, libc, linking, … labels Sep 21, 2018
@cladmi
Copy link
Contributor Author

cladmi commented Sep 21, 2018

Maybe not being able to to the comparison is a real problem, but I cannot really say. Let's already see the result of murdock in #9809

@cladmi cladmi added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Sep 21, 2018
@RIOT-OS RIOT-OS deleted a comment Sep 21, 2018
@kaspar030
Copy link
Contributor

This is indeed weird. The assembly looks like clang decides at compile time that the comparison is wrong.

Replacing the comparison with TEST_ASSERT((printf - iprintf) == 0); also fixes the issue.

@kaspar030
Copy link
Contributor

@cladmi here's the diff against master for the shorter fix: https://gist.github.com/1acb9d37be0ef35057309a6ee2d4e5da

@cladmi
Copy link
Contributor Author

cladmi commented Sep 24, 2018

Replacing the comparison with TEST_ASSERT((printf - iprintf) == 0); also fixes the issue.

Nice, I will replace to use this.

miri64
miri64 previously requested changes Sep 25, 2018
Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

Except for static tests, Murdock is happy with this PR here and and when run with #9809.

I also ran the test on both native and samr21-xpro, both compiled with either GCC and clang.

I have however some comments regarding the output messages.

You may squash when addressed (and if you like mark @kaspar030 as a co-author).

*/

#ifdef MODULE_NEWLIB
#ifdef MODULE_NEWLIB_NANO
/* Nano maps iprintf to printf */
TEST_ASSERT(iprintf == printf);
TEST_ASSERT_MESSAGE((printf - iprintf) == 0, "iprintf == printf");
Copy link
Member

Choose a reason for hiding this comment

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

I think this output might be confusing. The message is given when iprintf != printf, so this is what should be printed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, intuitively I agreed, but a plain TEST_ASSERT(<expression>) also prints just the failed expression, not the opposite. So all the TEST_ASSERT* have an implicit assertion "<msg>" failed.

Copy link
Member

Choose a reason for hiding this comment

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

True, but with e.g. TEST_ASSERT_EQUAL_INT it tells you "This is not as expected, this is what I expect" (faked with changing an expected value in tests-core:

.......................................
core_cib_tests.test_cib_get__overflow (tests/unittests/tests-core/tests-core-cib.c 48) exp 4 was 3
....................................................
run 91 failures 1

Copy link
Contributor

Choose a reason for hiding this comment

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

True. Thing is, we have

TEST_ASSERT(1==0) -> core_cib_tests.test_cib_get__overflow (tests/unittests/tests-core/tests-core-cib.c 48) 1==0,

but then want to tweak a custom message to essentially say the opposite, e.g.,

TEST_ASSERT_MESSAGE(1==0, "1 != 0") -> core_cib_tests.test_cib_get__overflow (tests/unittests/tests-core/tests-core-cib.c 48) 1 != 0?

(do I make sense? 😉)

Copy link
Member

Choose a reason for hiding this comment

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

(do I make sense? 😉)

Yes, at least I see (from the beginning btw) where you are coming from. I'm just arguing that for TEST_ASSERT() we can't really change what is happening. The CPP is just printing #exp, because it's the sane (and most efficient) thing to do. However, for TEST_ASSERT_MESSAGE we have more freedom, so we should use it and print a more intuitive output. But in the end it is purely based on taste of how to do it IMHO, so let's @cladmi decide, what he thinks is best for his test ;-).

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer keeping the "default" embunit behavior, show the condition that failed in the message on assert.

In master with the error I had:

018-09-25 14:45:58,501 - INFO # main(): This is RIOT! (Version: 2018.10-devel-855-g028bc-hal-HEAD)
2018-09-25 14:45:58,503 - INFO # Newlib/nano test
2018-09-25 14:45:58,503 - INFO # .
2018-09-25 14:45:58,509 - INFO # tests.test_newlib (tests/libc_newlib/main.c 66) iprintf == printf
2018-09-25 14:45:58,509 - INFO # 
2018-09-25 14:45:58,511 - INFO # run 1 failures 1

And I only changed the condition to (iprintf- printf)==0 because it made it work for llvm but wanted to keep the same message.
With only TEST_ASSERT we would also have (iprintf - printf) == 0 so for me would feel strange to do the opposite of the normal behavior.

#else
/* Normal newlib does not */
TEST_ASSERT(iprintf != printf);
TEST_ASSERT_MESSAGE((printf - iprintf) != 0, "iprintf != printf");
Copy link
Member

Choose a reason for hiding this comment

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

Dito (just with iprintf == printf)

@miri64 miri64 dismissed their stale review September 25, 2018 08:59

See #10014 (comment): "[…] in the end it is purely based on taste of how to do it IMHO, so let's @cladmi decide, what he thinks is best for his test ;-)."

@cladmi
Copy link
Contributor Author

cladmi commented Sep 25, 2018

@miri64 thanks for the co-author trick, I will do it.

Copy link
Member

@miri64 miri64 left a comment

Choose a reason for hiding this comment

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

ACK

@miri64
Copy link
Member

miri64 commented Sep 25, 2018

(please squash ;-))

With llvm and samr21-xpro, I could not directly do 'printf == iprintf'.
But doing `(printf - iprintf) == 0` correctly checked if they are equal.

Co-authored-by: Kaspar Schleiser <kaspar@schleiser.de>
@cladmi cladmi force-pushed the pr/tests_libc/fix_for_llvm branch from 3e62363 to ba2a8df Compare September 25, 2018 13:03
@cladmi
Copy link
Contributor Author

cladmi commented Sep 25, 2018

Squashed now waiting for the "CI: run tests".

@miri64 miri64 merged commit f1529b8 into RIOT-OS:master Sep 25, 2018
@cladmi cladmi deleted the pr/tests_libc/fix_for_llvm branch September 25, 2018 17:24
@jia200x jia200x added this to the Release 2018.10 milestone Oct 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: tests Area: tests and testing framework Area: toolchain Area: toolchains; everything related to compilation, libc, linking, … CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants