Skip to content

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Jun 13, 2021

The flags are cast to unsigned in the interpreter anyway, so avoid the confusion (and fuzz crashes) by just passing them as unsigned from the beginning.

Also, the flags are often inverted bit-wise with the ~ operator, which also works on signed integers, but might cause confusion as the sign bit is flipped.

Fixes #22233

@practicalswift
Copy link
Contributor

Concept ACK

@fanquake
Copy link
Member

Sanitizer CI failure:

�[0;39;49m�[1;34;49mtest/sigopcount_tests.cpp(26): Entering test suite "sigopcount_tests"
�[0;39;49m�[1;34;49mtest/sigopcount_tests.cpp(108): Entering test case "GetTxSigOpCost"
test/sigopcount_tests.cpp:167:9: runtime error: implicit conversion from type 'int' of value -2049 (32-bit, signed) to type 'unsigned int' changed the value to 4294965247 (32-bit, unsigned)
    #0 0x5567920a3a53 in sigopcount_tests::GetTxSigOpCost::test_method() test/sigopcount_tests.cpp:167:9
    #1 0x5567920a1ee5 in sigopcount_tests::GetTxSigOpCost_invoker() test/sigopcount_tests.cpp:108:1
    #2 0x556791886eff in boost::detail::function::void_function_invoker0<void (*)(), void>::invoke(boost::detail::function::function_buffer&) /usr/include/boost/function/function_template.hpp:117:11
    #3 0x7f1708c4f521  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x31521)
    #4 0x7f1708c4dcc4 in boost::execution_monitor::catch_signals(boost::function<int ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x2fcc4)
    #5 0x7f1708c4dd47 in boost::execution_monitor::execute(boost::function<int ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x2fd47)
    #6 0x7f1708c4ddf4 in boost::execution_monitor::vexecute(boost::function<void ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x2fdf4)
    #7 0x7f1708c7cca5 in boost::unit_test::unit_test_monitor_t::execute_and_translate(boost::function<void ()> const&, unsigned long) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x5eca5)
    #8 0x7f1708c5524c  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x3724c)
    #9 0x7f1708c55769  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x37769)
    #10 0x7f1708c55769  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x37769)
    #11 0x7f1708c595bf in boost::unit_test::framework::run(unsigned long, bool) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x3b5bf)
    #12 0x7f1708c7b81d in boost::unit_test::unit_test_main(bool (*)(), int, char**) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.74.0+0x5d81d)
    #13 0x5567917de70d in main /usr/include/boost/test/unit_test.hpp:64:12
    #14 0x7f170827b564 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x28564)
    #15 0x55679172fa7d in _start (/tmp/cirrus-ci-build/ci/scratch/build/bitcoin-x86_64-pc-linux-gnu/src/test/test_bitcoin+0x1bdfa7d)

SUMMARY: UndefinedBehaviorSanitizer: implicit-integer-sign-change test/sigopcount_tests.cpp:167:9 in 
make[3]: *** [Makefile:18040: test/sigopcount_tests.cpp.test] Error 1

@maflcko maflcko force-pushed the 2106-refactorFlags branch from fa480cd to faa0591 Compare June 14, 2021 06:00
They are cast to unsigned anyway when calling VerifyScript,
bitcoinconsensus_verify_script*, or CountWitnessSigOps.
@maflcko maflcko force-pushed the 2106-refactorFlags branch from faa0591 to fa621ed Compare June 14, 2021 06:03
@maflcko
Copy link
Member Author

maflcko commented Jun 14, 2021

Fixed by pinning the underlying type to uint32_t

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept and code review ACK fa621ed

IMHO there is really not much point in ever using signed datatypes for flags (though e.g. in CheckFinalTx, the flags for the nSequence/nLockTime locks have a special meaning if negative 👀... could probably be replaced by std::optional<uint32_t>)

@maflcko
Copy link
Member Author

maflcko commented Jun 16, 2021

though e.g. in CheckFinalTx, the flags for the nSequence/nLockTime locks have a special meaning if negative eyes... could probably be replaced by std::optional<uint32_t>

Agree, though no sanitizer complains about them right now. Also, those aren't script verify flags in the same enum, so should be done in a separate pull request.

@jonatack
Copy link
Member

Concept ACK

Copy link
Contributor

@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

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

ACK fa621ed

Copy link
Member

@jonatack jonatack left a comment

Choose a reason for hiding this comment

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

ACK fa621ed

@maflcko maflcko merged commit 9faa4b6 into bitcoin:master Jul 20, 2021
@maflcko maflcko deleted the 2106-refactorFlags branch July 20, 2021 13:45
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 23, 2021
hebasto pushed a commit to hebasto/bitcoin that referenced this pull request Aug 7, 2021
They are cast to unsigned anyway when calling VerifyScript,
bitcoinconsensus_verify_script*, or CountWitnessSigOps.

Github-Pull: bitcoin#22232
Rebased-From: fa621ed
@hebasto hebasto mentioned this pull request Aug 7, 2021
@hebasto
Copy link
Member

hebasto commented Aug 7, 2021

Backported in #22629.

UPDATE: #22629 (comment)

@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fuzz: UndefinedBehaviorSanitizer warnings in consensus/tx_verify.cpp
8 participants