SwuduSusuwu / SubStack

Stages blog posts, plus has C++ sources which match posts. Blog about human nervous tissues + autonomous tools
https://SwuduSusuwu.substack.com/
Apache License 2.0
2 stars 0 forks source link

cxx/main.cxx:testHarnesses() segfaults. Should pass (or print which function fails). #5

Closed SwuduSusuwu closed 2 weeks ago

SwuduSusuwu commented 2 weeks ago

Todo: cxx/main.cxx:testHarnesses() passes. https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx#L15 ./make.sh compiles this to a.out But for now, ./a.out segfaults:

~/SubStack $ ./a.out
bash: ./a.out: No such file or directory
~/SubStack $ ./make.sh
/data/data/com.termux/files/usr/bin/ctags
/data/data/com.termux/files/usr/bin/clang++
./cxx//VirusAnalysis.cxx:37:29: warning: expression result unused [-Wunused-value]
   37 |         /* callbackHook("exec", */ [](const PortableExecutable &file) { /* TODO: OS-specific "hook"/"callback" for `exec()`/app-launches */
      |                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   38 |                 switch(virusAnalysis(file)) {
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   39 |                 case virusAnalysisPass:
      |                 ~~~~~~~~~~~~~~~~~~~~~~~
   40 |                         return true; /* launch this */
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   41 |                 case virusAnalysisRequiresReview:
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   42 |                         submitSampleToHosts(file); /* manual review */
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   43 |                         return false;
      |                         ~~~~~~~~~~~~~
   44 |                 default:
      |                 ~~~~~~~~
   45 |                         return false; /* abort */
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~
   46 |                 }
      |                 ~
   47 |         } /* ) */ ;
      |         ~
./cxx//VirusAnalysis.cxx:62:3: warning: label at end of compound statement is a C++23 extension [-Wc++23-extensions]
   62 |                 }
      |                 ^
./cxx//VirusAnalysis.cxx:117:1: warning: non-void function does not return a value [-Wreturn-type]
  117 | }
      | ^
3 warnings generated.
./cxx//ConversationCns.cxx:106:74: warning: non-void function does not return a value [-Wreturn-type]
  106 | const FileBytecode conversationParseQuestion(const FilePath &xhtmlFile) {} /* TODO */
      |                                                                          ^
./cxx//ConversationCns.cxx:107:88: warning: non-void function does not return a value [-Wreturn-type]
  107 | const std::vector<FileBytecode> conversationParseResponses(const FilePath &xhtmlFile) {} /* TODO */
      |                                                                                        ^
2 warnings generated.
~/SubStack $ ./a.out
cxx/Macros.hxx: pass
cxx/VirusAnalysis.hxx: pass
Segmentation fault
~/SubStack $

clang++'s warnings appear unrelated to the segfault.

SwuduSusuwu commented 2 weeks ago
~/SubStack $ pkg install valgrind
...
~/SubStack $ valgrind ./a.out
==6247== Memcheck, a memory error detector
==6247== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==6247== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==6247== Command: ./a.out
==6247==
ARM64 front end: load_store
disInstr(arm64): unhandled instruction 0x38BFC109
disInstr(arm64): 0011'1000 1011'1111 1100'0001 0000'1001
==6247== valgrind: Unrecognised instruction at address 0x4073170.
==6247==    at 0x4073170: __dl__Z26__libc_safe_arc4random_bufPvm (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x4078377: __dl___libc_init_main_thread_late (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x410665F: __dl__ZL29__linker_init_post_relocationR19KernelArgumentBlockR6soinfo (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x41065CB: __dl___linker_init (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x4048007: __dl__start (in /apex/com.android.runtime/bin/linker64)
==6247== Your program just tried to execute an instruction that Valgrind
==6247== did not recognise.  There are two possible reasons for this.
==6247== 1. Your program has a bug and erroneously jumped to a non-code
==6247==    location.  If you are running Memcheck and you just saw a
==6247==    warning about a bad jump, it's probably your program's fault.
==6247== 2. The instruction is legitimate but Valgrind doesn't handle it,
==6247==    i.e. it's Valgrind's fault.  If you think this is the case or
==6247==    you are not sure, please let us know and we'll try to fix it.
==6247== Either way, Valgrind will now raise a SIGILL signal which will
==6247== probably kill your program.
==6247==
==6247== Process terminating with default action of signal 4 (SIGILL)
==6247==  Illegal opcode at address 0x4073170
==6247==    at 0x4073170: __dl__Z26__libc_safe_arc4random_bufPvm (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x4078377: __dl___libc_init_main_thread_late (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x410665F: __dl__ZL29__linker_init_post_relocationR19KernelArgumentBlockR6soinfo (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x41065CB: __dl___linker_init (in /apex/com.android.runtime/bin/linker64)
==6247==    by 0x4048007: __dl__start (in /apex/com.android.runtime/bin/linker64)
==6247==
==6247== HEAP SUMMARY:
==6247==     in use at exit: 0 bytes in 0 blocks
==6247==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6247==
==6247== All heap blocks were freed -- no leaks are possible
==6247==
==6247== For lists of detected and suppressed errors, rerun with: -s
==6247== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Illegal instruction
~/SubStack $
SwuduSusuwu commented 2 weeks ago

Removed conversationCnsTestsThrows() from https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.cxx#L31 ; now ./a.out is success, but valgrind gives the same error, so the error from valgrind must not relate to the segmentation fault from conversationCnsTestsThrows().

~/SubStack $ cat cxx/main.cxx
#include "VirusAnalysis.hxx" /* virusAnalysisTestsThrows */
#include "ConversationCns.hxx" /* conversationCnsTestsThrows */
#include "Macros.hxx" /* ASSUME EXPECTS ENSURES NOEXCEPT NORETURN */
#include <stdio.h> /* printf */
#include <stdlib.h> /* exit */
namespace Susuwu {
void noExcept() NOEXCEPT;
NORETURN void noReturn();
void noExcept() NOEXCEPT {printf("");}
void noReturn()  {exit(0);}
int testHarnesses() EXPECTS(true) ENSURES(true) {
        ASSUME(true);
        noExcept();
        printf("cxx/Macros.hxx: pass\n");
        if(virusAnalysisTestsThrows()) {
                printf("cxx/VirusAnalysis.hxx: pass\n");
        }
        noReturn();
}
}; /* namespace Susuwu */
int main(int argc, const char **args) {
        return Susuwu::testHarnesses();
}

~/SubStack $ ./make.sh
/* Dual licenses: choose "Creative Commons" or "Apache 2" (allows all uses) */
/data/data/com.termux/files/usr/bin/ctags
/data/data/com.termux/files/usr/bin/clang++
~/SubStack $ ./a.out
cxx/Macros.hxx: pass
cxx/VirusAnalysis.hxx: pass
~/SubStack $ valgrind ./a.out
==4271== Memcheck, a memory error detector
==4271== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==4271== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==4271== Command: ./a.out
==4271==
ARM64 front end: load_store
disInstr(arm64): unhandled instruction 0x38BFC109
disInstr(arm64): 0011'1000 1011'1111 1100'0001 0000'1001
==4271== valgrind: Unrecognised instruction at address 0x4073170.
==4271==    at 0x4073170: __dl__Z26__libc_safe_arc4random_bufPvm (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x4078377: __dl___libc_init_main_thread_late (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x410665F: __dl__ZL29__linker_init_post_relocationR19KernelArgumentBlockR6soinfo (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x41065CB: __dl___linker_init (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x4048007: __dl__start (in /apex/com.android.runtime/bin/linker64)
==4271== Your program just tried to execute an instruction that Valgrind
==4271== did not recognise.  There are two possible reasons for this.
==4271== 1. Your program has a bug and erroneously jumped to a non-code
==4271==    location.  If you are running Memcheck and you just saw a
==4271==    warning about a bad jump, it's probably your program's fault.
==4271== 2. The instruction is legitimate but Valgrind doesn't handle it,
==4271==    i.e. it's Valgrind's fault.  If you think this is the case or
==4271==    you are not sure, please let us know and we'll try to fix it.
==4271== Either way, Valgrind will now raise a SIGILL signal which will
==4271== probably kill your program.
==4271==
==4271== Process terminating with default action of signal 4 (SIGILL)
==4271==  Illegal opcode at address 0x4073170
==4271==    at 0x4073170: __dl__Z26__libc_safe_arc4random_bufPvm (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x4078377: __dl___libc_init_main_thread_late (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x410665F: __dl__ZL29__linker_init_post_relocationR19KernelArgumentBlockR6soinfo (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x41065CB: __dl___linker_init (in /apex/com.android.runtime/bin/linker64)
==4271==    by 0x4048007: __dl__start (in /apex/com.android.runtime/bin/linker64)
==4271==
==4271== HEAP SUMMARY:
==4271==     in use at exit: 0 bytes in 0 blocks
==4271==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==4271==
==4271== All heap blocks were freed -- no leaks are possible
==4271==
==4271== For lists of detected and suppressed errors, rerun with: -s
==4271== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Illegal instruction
~/SubStack $

Am not going to commit this, as testHarnesses() is supposed to execute conversationCnsTestsThrows(). Must fix conversationCnsTestsThrows() plus fix what causes valgrind's "Unrecognized instruction" error.

SwuduSusuwu commented 2 weeks ago

Can not use valgrind with Termux: https://github.com/ivosh/valgrind/issues/2 https://bugs.kde.org/show_bug.cgi?id=488514

SwuduSusuwu commented 2 weeks ago

From cxx/ClassCns.cxx:posixExec

const int posixExec(const std::string &executable, const std::string &argsS, const std::string &envVarsS) {
#ifdef _POSIX_VERSION
    char *args[] = {
        const_cast<char *>(executable.c_str()),
        const_cast<char *>(argsS.c_str()),
        NULL
    };
    char *envVars[] = {
        const_cast<char *>(envVarsS.c_str()),
        NULL
    };
    return execve(args[0], args, envVars);
#endif /* def _POSIX_VERSION */
}

, to

const int posixExec(const std::string &executable, const std::string &argsS, const std::string &envVarsS) {
}

, fixes conversationCnsTestsThrows() + testsHarnesses():

~/SubStack $ ./make.sh
/* Dual licenses: choose "Creative Commons" or "Apache 2" (allows all uses) */
/data/data/com.termux/files/usr/bin/ctags
/data/data/com.termux/files/usr/bin/clang++
./~/SubStack $ ./a.out
cxx/Macros.hxx: pass
cxx/VirusAnalysis.hxx: pass
cxx/ConversationCns.hxx: pass
~/SubStack $

, but posixExec() is supposed to exist, thus can not commit this.

SwuduSusuwu commented 2 weeks ago

The last 2 commits ( @make.sh command -v has > /dev/null. set -x + @make.sh CXX_FLAGS="-fsanitize= on g++ / clang++ ) produce this:

~/SubStack $ ./make.sh
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -x c -c ./cxx//../c/rfc6234/sha1.c
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -x c -c ./cxx//../c/rfc6234/sha224-256.c
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -x c -c ./cxx//../c/rfc6234/sha384-512.c
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//ClassSha2.cxx
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//ClassResultList.cxx
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//ClassCns.cxx
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//VirusAnalysis.cxx
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//ConversationCns.cxx
./cxx//ConversationCns.cxx:106:74: warning: non-void function does not return a value [-Wreturn-type]
  106 | const FileBytecode conversationParseQuestion(const FilePath &xhtmlFile) {} /* TODO */
      |                                                                          ^
./cxx//ConversationCns.cxx:107:88: warning: non-void function does not return a value [-Wreturn-type]
  107 | const std::vector<FileBytecode> conversationParseResponses(const FilePath &xhtmlFile) {} /* TODO */
      |                                                                                        ^
2 warnings generated.
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g -c ./cxx//main.cxx
+ clang++ -fsanitize=address -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -fno-omit-frame-pointer -g sha1.o sha224-256.o sha384-512.o ClassSha2.o ClassResultList.o ClassCns.o VirusAnalysis.o ConversationCns.o main.o
~/SubStack $ ./a.out
cxx/Macros.hxx: pass
cxx/VirusAnalysis.hxx: pass
AddressSanitizer:DEADLYSIGNAL
=================================================================
==355==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x007958880050 bp 0x007fed4ad6f0 sp 0x007fed4acee0 T0)
==355==The signal is caused by a READ memory access.
==355==Hint: address points to the zero page.
libunwind: unsupported .eh_frame_hdr version: 127 at 795cd30000
    #0 0x7958880050 in __strlen_aarch64 libc_init_dynamic.cpp
    https://github.com/SwuduSusuwu/SubStack/issues/1 0x795a4be864 in strlen out/lib/compiler-rt-aarch64/out/llvm-project/compiler-rt/lib/asan/../sanitizer_common/sanitizer_common_interceptors.inc
    #2 0x59a56172b0 in std::__ndk1::__constexpr_strlen[abi:v170000](char const*) /data/data/com.termux/files/usr/include/c++/v1/cstring:114:10
    #3 0x59a5617270 in std::__ndk1::char_traits<char>::length(char const*) /data/data/com.termux/files/usr/include/c++/v1/__string/char_traits.h:220:12
    #4 0x59a55e0c8c in std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>::basic_string[abi:v170000]<std::nullptr_t>(char const*) /data/data/com.termux/files/usr/include/c++/v1/string:882:17
    #5 0x59a5639018 in Susuwu::questionsResponsesFromHosts(Susuwu::ResultList&, Susuwu::ResultList&, std::__ndk1::vector<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>, std::__ndk1::allocator<std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>>>> const&) /data/data/com.termux/files/home/SubStack/./cxx/ConversationCns.cxx:54:68
    #6 0x59a5638870 in Susuwu::conversationCnsTestsThrows() /data/data/com.termux/files/home/SubStack/./cxx/ConversationCns.cxx:32:2
    #7 0x59a56431bc in Susuwu::testHarnesses() /data/data/com.termux/files/home/SubStack/./cxx/main.cxx:21:5
    #8 0x59a56431f8 in main /data/data/com.termux/files/home/SubStack/./cxx/main.cxx:28:9
    #9 0x795887ee18 in __libc_init (/apex/com.android.runtime/lib64/bionic/libc.so+0x56e18) (BuildId: 33ad5959e2b38fc822cda3c642e16c94)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV libc_init_dynamic.cpp in __strlen_aarch64

which shows ./cxx/ConversationCns.cxx:54 (posixExec("/bin/wget", "'" + host + "/robots.txt' > robots.txt", NULL);) was the last to execute

SwuduSusuwu commented 2 weeks ago

cxx/* posixExec -> execves + execvex partial fix

~/SubStack $ ./a.out
cxx/Macros.hxx: pass
execves(): pass: unknown program ‘pass’
Try 'pass --help' for more information.
execvex(): '/bin/echo pass': 0: cannot open '/bin/echo pass': No such file
virusAnalysisTestsThrows(): pass
conversationCnsTestsThrows(): : 0: cannot open : No such file
: 0: cannot open : No such file
Trap
~/SubStack $