diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-11 14:11:05 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-11 14:11:05 +0200 |
| commit | 672541352e91ce4e2333d45dd8b3808d2d379466 (patch) | |
| tree | 489d7a42a8e753f81fc0d9482d8b91f35e163414 | |
| parent | b4155efdeb3df35080e3e14ab7a618de97164eea (diff) | |
feat(cxxdrv): Begin addressing stack corruption issue causing segmentation fault
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | dev/LibC++/lc_runtime+unreachable.cc | 3 | ||||
| -rw-r--r-- | dev/LibCompiler/UUID.h | 2 | ||||
| -rw-r--r-- | dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc | 25 | ||||
| -rw-r--r-- | dev/LibCompiler/src/DynamicLinkerPEF.cc | 5 | ||||
| -rw-r--r-- | dev/Vendor/Dialogs.h | 41 | ||||
| -rw-r--r-- | tools/asm.cc | 10 | ||||
| -rw-r--r-- | tools/cxxdrv.cc | 9 | ||||
| -rw-r--r-- | tools/cxxdrv.json | 2 |
8 files changed, 46 insertions, 51 deletions
diff --git a/dev/LibC++/lc_runtime+unreachable.cc b/dev/LibC++/lc_runtime+unreachable.cc index 2aaa3c1..39115a1 100644 --- a/dev/LibC++/lc_runtime+unreachable.cc +++ b/dev/LibC++/lc_runtime+unreachable.cc @@ -7,5 +7,6 @@ #include <LibC++/lc_runtime.h> extern "C" void __libcompiler_unreachable(void) { - while (true); + while (true) + ; }
\ No newline at end of file diff --git a/dev/LibCompiler/UUID.h b/dev/LibCompiler/UUID.h index d54eec7..39db276 100644 --- a/dev/LibCompiler/UUID.h +++ b/dev/LibCompiler/UUID.h @@ -172,7 +172,7 @@ namespace Detail { process_byte(static_cast<unsigned char>((bitCount >> 24) & 0xFF)); process_byte(static_cast<unsigned char>((bitCount >> 16) & 0xFF)); process_byte(static_cast<unsigned char>((bitCount >> 8) & 0xFF)); - process_byte(static_cast<unsigned char>((bitCount) & 0xFF)); + process_byte(static_cast<unsigned char>((bitCount) &0xFF)); memcpy(digest, m_digest, 5 * sizeof(uint32_t)); return digest; diff --git a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc index c0dc281..62d51aa 100644 --- a/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc +++ b/dev/LibCompiler/src/CPlusPlusCompilerAMD64.cc @@ -54,9 +54,11 @@ namespace Detail { std::filesystem::path expand_home(const std::filesystem::path& p) { if (!p.empty() && p.string()[0] == '~') { const char* home = std::getenv("HOME"); // For Unix-like systems + if (!home) { home = std::getenv("USERPROFILE"); // For Windows } + if (home) { return std::filesystem::path(home) / p.relative_path().string().substr(1); } else { @@ -86,7 +88,7 @@ struct CompilerStructMap final { struct CompilerState final { std::vector<CompilerRegisterMap> fStackMapVector; std::vector<CompilerStructMap> fStructMapVector; - std::ofstream fOutputAssembly; + std::string fOutputValue; std::string fLastFile; std::string fLastError; Boolean fVerbose; @@ -132,7 +134,6 @@ static std::vector<LibCompiler::CompilerKeyword> kKeywords; ///////////////////////////////////////// -static std::vector<std::string> kFileList; static LibCompiler::AssemblyFactory kFactory; static Boolean kInStruct = false; static Boolean kOnWhileLoop = false; @@ -220,7 +221,7 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { } } - static LibCompiler::SyntaxLeafList::SyntaxLeaf syntax_tree; + LibCompiler::SyntaxLeafList::SyntaxLeaf syntax_tree; for (auto& keyword : keywords_list) { switch (keyword.first.keyword_kind) { @@ -696,12 +697,9 @@ Boolean CompilerFrontendCPlusPlus::Compile(std::string text, std::string file) { continue; } } - - break; } -lc_compile_ok: - kState.fOutputAssembly << syntax_tree.fUserValue; + kState.fOutputValue = syntax_tree.fUserValue; return true; } @@ -735,14 +733,14 @@ class AssemblyCPlusPlusInterface final ASSEMBLY_INTERFACE { std::string line_source; - kState.fOutputAssembly.open(dest); + std::ofstream out(dest); while (std::getline(src_fp, line_source)) { kCompilerFrontend->Compile(line_source, src); + out << kState.fOutputValue; } - kState.fOutputAssembly.flush(); - kState.fOutputAssembly.close(); + out.flush(); if (kAcceptableErrors > 0) return kExitNO; @@ -881,8 +879,6 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { continue; } - kFileList.emplace_back(argv[index]); - std::string argv_i = argv[index]; std::vector exts = kExtListCxx; @@ -903,10 +899,11 @@ LIBCOMPILER_MODULE(CompilerCPlusPlusAMD64) { return kExitNO; } - kFactory.Compile(argv_i, kMachine); + auto ret = kFactory.Compile(argv_i, kMachine); + return ret; } - return kExitOK; + return kExitNO; } // Last rev 8-1-24 diff --git a/dev/LibCompiler/src/DynamicLinkerPEF.cc b/dev/LibCompiler/src/DynamicLinkerPEF.cc index 21572bb..9e3be82 100644 --- a/dev/LibCompiler/src/DynamicLinkerPEF.cc +++ b/dev/LibCompiler/src/DynamicLinkerPEF.cc @@ -64,7 +64,10 @@ /// @brief PEF stack size symbol. #define kLinkerStackSizeSymbol "__PEFSizeOfReserveStack" -#define kOutCon (std::cout << "\e[0;31m" << "ld64: " << "\e[0;97m") +#define kOutCon \ + (std::cout << "\e[0;31m" \ + << "ld64: " \ + << "\e[0;97m") namespace Detail { struct DynamicLinkerBlob final { diff --git a/dev/Vendor/Dialogs.h b/dev/Vendor/Dialogs.h index ce50b81..84e239f 100644 --- a/dev/Vendor/Dialogs.h +++ b/dev/Vendor/Dialogs.h @@ -175,7 +175,7 @@ namespace internal { #elif __EMSCRIPTEN__ void start(int exit_code); #else - void start_process(std::vector<std::string> const& command); + void start_process(std::vector<std::string> const& command); #endif ~executor(); @@ -490,10 +490,10 @@ inline settings::settings(bool resync) { #if _WIN32 flags(flag::is_vista) = internal::is_vista(); #elif !__APPLE__ - flags(flag::has_zenity) = check_program("zenity"); + flags(flag::has_zenity) = check_program("zenity"); flags(flag::has_matedialog) = check_program("matedialog"); - flags(flag::has_qarma) = check_program("qarma"); - flags(flag::has_kdialog) = check_program("kdialog"); + flags(flag::has_qarma) = check_program("qarma"); + flags(flag::has_kdialog) = check_program("kdialog"); // If multiple helpers are available, try to default to the best one if (flags(flag::has_zenity) && flags(flag::has_kdialog)) { @@ -540,7 +540,7 @@ inline bool settings::check_program(std::string const& program) { (void) program; return false; #else - int exit_code = -1; + int exit_code = -1; internal::executor async; async.start_process({"/bin/sh", "-c", "which " + program}); async.result(&exit_code); @@ -604,7 +604,7 @@ inline std::string path::home() { if (size_max != -1) len = size_t(size_max); #endif std::vector<char> buf(len); - struct passwd pwd, *result; + struct passwd pwd, *result; if (getpwuid_r(getuid(), &pwd, buf.data(), buf.size(), &result) == 0) return result->pw_dir; #endif return "/"; @@ -717,7 +717,7 @@ inline void internal::executor::start_process(std::vector<std::string> const& co } close(in[1]); - m_fd = out[0]; + m_fd = out[0]; auto flags = fcntl(m_fd, F_GETFL); fcntl(m_fd, F_SETFL, flags | O_NONBLOCK); @@ -753,7 +753,7 @@ inline bool internal::executor::ready(int timeout /* = default_wait_timeout */) // FIXME: do something (void) timeout; #else - char buf[BUFSIZ]; + char buf[BUFSIZ]; ssize_t received = read(m_fd, buf, BUFSIZ); // Flawfinder: ignore if (received > 0) { m_stdout += std::string(buf, received); @@ -764,7 +764,7 @@ inline bool internal::executor::ready(int timeout /* = default_wait_timeout */) // (this happens when the calling application handles or ignores SIG_CHLD) and results in // waitpid() failing with ECHILD. Otherwise we assume the child is running and we sleep for // a little while. - int status; + int status; pid_t child = waitpid(m_pid, &status, WNOHANG); if (child != m_pid && (child >= 0 || errno != ECHILD)) { // FIXME: this happens almost always at first iteration @@ -782,7 +782,8 @@ inline bool internal::executor::ready(int timeout /* = default_wait_timeout */) inline void internal::executor::stop() { // Loop until the user closes the dialog - while (!ready()); + while (!ready()) + ; } // dll implementation @@ -878,11 +879,11 @@ inline std::vector<std::string> internal::dialog::desktop_helper() const { #if __APPLE__ return {"osascript"}; #else - return {flags(flag::has_zenity) ? "zenity" + return {flags(flag::has_zenity) ? "zenity" : flags(flag::has_matedialog) ? "matedialog" - : flags(flag::has_qarma) ? "qarma" - : flags(flag::has_kdialog) ? "kdialog" - : "echo"}; + : flags(flag::has_qarma) ? "qarma" + : flags(flag::has_kdialog) ? "kdialog" + : "echo"}; #endif } @@ -1124,9 +1125,9 @@ inline internal::file_dialog::file_dialog(type in_type, std::string const& title // Split the pattern list to check whether "*" is in there; if it // is, we have to disable filters because there is no mechanism in // OS X for the user to override the filter. - std::regex sep("\\s+"); - std::string filter_list; - bool has_filter = true; + std::regex sep("\\s+"); + std::string filter_list; + bool has_filter = true; std::sregex_token_iterator iter(patterns.begin(), patterns.end(), sep, -1); std::sregex_token_iterator end; for (; iter != end; ++iter) { @@ -1235,7 +1236,7 @@ inline std::vector<std::string> internal::file_dialog::vector_result() { return m_vector_result; #else std::vector<std::string> ret; - auto result = m_async->result(); + auto result = m_async->result(); for (;;) { // Split result along newline characters auto i = result.find('\n'); @@ -1568,7 +1569,7 @@ inline message::message(std::string const& title, std::string const& text, if_cancel = button::ok; break; } - m_mappings[1] = if_cancel; + m_mappings[1] = if_cancel; m_mappings[256] = if_cancel; // XXX: I think this was never correct script += " with icon "; switch (_icon) { @@ -1655,7 +1656,7 @@ inline message::message(std::string const& title, std::string const& text, if (_choice == choice::yes_no_cancel) flag += "cancel"; command.push_back(flag); if (_choice == choice::yes_no || _choice == choice::yes_no_cancel) { - m_mappings[0] = button::yes; + m_mappings[0] = button::yes; m_mappings[256] = button::no; } } diff --git a/tools/asm.cc b/tools/asm.cc index 36e55cf..8f98543 100644 --- a/tools/asm.cc +++ b/tools/asm.cc @@ -36,7 +36,7 @@ int main(int argc, char const* argv[]) { Int32 asm_type = kInvalidAssembler; for (size_t index_arg = 1; index_arg < argc; ++index_arg) { - if (strstr(argv[index_arg], "--asm:h")) { + if (strstr(argv[index_arg], "-asm:h")) { std::printf("asm: Frontend Assembler (64x0, power64, arm64, x64).\n"); std::printf("asm: Version: %s, Release: %s.\n", kDistVersion, kDistRelease); std::printf( @@ -47,13 +47,13 @@ int main(int argc, char const* argv[]) { "all rights reserved.\n"); return 0; - } else if (strstr(argv[index_arg], "--asm:x64")) { + } else if (strstr(argv[index_arg], "-asm:x64")) { asm_type = kX64Assembler; - } else if (strstr(argv[index_arg], "--asm:aarch64")) { + } else if (strstr(argv[index_arg], "-asm:aarch64")) { asm_type = kARM64Assembler; - } else if (strstr(argv[index_arg], "--asm:64x0")) { + } else if (strstr(argv[index_arg], "-asm:64x0")) { asm_type = k64X0Assembler; - } else if (strstr(argv[index_arg], "--asm:power64")) { + } else if (strstr(argv[index_arg], "-asm:power64")) { asm_type = kPOWER64Assembler; } else { arg_vec_cstr.push_back(argv[index_arg]); diff --git a/tools/cxxdrv.cc b/tools/cxxdrv.cc index a44b51d..f0aee1b 100644 --- a/tools/cxxdrv.cc +++ b/tools/cxxdrv.cc @@ -12,13 +12,6 @@ #include <LibCompiler/Version.h> #include <cstring> -LC_IMPORT_C int CompilerCPlusPlusAMD64(int argc, char const* argv[]); - int main(int argc, char const* argv[]) { - if (auto code = CompilerCPlusPlusAMD64(argc, argv); code > 0) { - std::printf("cxxdrv: compiler exited with code %i.\n", code); - return LIBCOMPILER_EXEC_ERROR; - } - - return LIBCOMPILER_SUCCESS; + return EXIT_FAILURE; } diff --git a/tools/cxxdrv.json b/tools/cxxdrv.json index d16f3b3..ad760c3 100644 --- a/tools/cxxdrv.json +++ b/tools/cxxdrv.json @@ -4,7 +4,7 @@ "headers_path": ["../dev/LibCompiler", "../dev/", "../dev/LibCompiler/src/Detail"], "sources_path": ["cxxdrv.cc"], "output_name": "cxxdrv", - "compiler_flags": ["-L/usr/local/lib", "-lCompiler"], + "compiler_flags": ["-L/usr/local/lib", "-lCompiler", "-Wl,-e,_CompilerCPlusPlusAMD64"], "cpp_macros": [ "__CXXDRV__=202504", "kDistReleaseBranch=$(git rev-parse --abbrev-ref HEAD)-$(uuidgen)" |
