|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file DetachedThreadLifetimeRule.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2025, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + * |
| 13 | + */ |
| 14 | +#include <vix/cli/errors/runtime/IRuntimeErrorRule.hpp> |
| 15 | +#include <vix/cli/errors/runtime/RuntimeRuleUtils.hpp> |
| 16 | + |
| 17 | +#include <filesystem> |
| 18 | +#include <iostream> |
| 19 | +#include <memory> |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include <vix/cli/Style.hpp> |
| 24 | + |
| 25 | +using namespace vix::cli::style; |
| 26 | + |
| 27 | +namespace vix::cli::errors::runtime |
| 28 | +{ |
| 29 | + class DetachedThreadLifetimeRule final : public IRuntimeErrorRule |
| 30 | + { |
| 31 | + public: |
| 32 | + bool match( |
| 33 | + const std::string &log, |
| 34 | + const std::filesystem::path &sourceFile) const override |
| 35 | + { |
| 36 | + (void)sourceFile; |
| 37 | + |
| 38 | + return (icontains(log, "detach") && |
| 39 | + (icontains(log, "use-after-free") || |
| 40 | + icontains(log, "stack-use-after-scope") || |
| 41 | + icontains(log, "use-after-return"))) || |
| 42 | + (icontains(log, "detached thread") && |
| 43 | + (icontains(log, "dangling") || |
| 44 | + icontains(log, "invalid memory") || |
| 45 | + icontains(log, "lifetime"))) || |
| 46 | + (icontains(log, "lambda") && |
| 47 | + icontains(log, "reference") && |
| 48 | + icontains(log, "detach")); |
| 49 | + } |
| 50 | + |
| 51 | + bool handle( |
| 52 | + const std::string &log, |
| 53 | + const std::filesystem::path &sourceFile) const override |
| 54 | + { |
| 55 | + std::string title = "runtime error: detached thread lifetime bug"; |
| 56 | + std::vector<std::string> hints = { |
| 57 | + "a detached thread likely outlived data it was still using", |
| 58 | + "avoid capturing local variables by reference in detached threads and ensure shared objects outlive the thread", |
| 59 | + }; |
| 60 | + |
| 61 | + if (icontains(log, "stack-use-after-scope") || |
| 62 | + icontains(log, "use-after-return")) |
| 63 | + { |
| 64 | + title = "runtime error: detached thread captured expired stack data"; |
| 65 | + hints = { |
| 66 | + "a detached thread likely kept a reference or pointer to a local variable after the scope ended", |
| 67 | + "capture by value, use shared ownership when needed, or avoid detach() for work tied to local lifetime", |
| 68 | + }; |
| 69 | + } |
| 70 | + else if (icontains(log, "use-after-free")) |
| 71 | + { |
| 72 | + title = "runtime error: detached thread used freed memory"; |
| 73 | + hints = { |
| 74 | + "a detached thread likely accessed memory after it had already been released", |
| 75 | + "ensure detached work owns its data safely, or join the thread before destroying shared state", |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + std::cerr << RED |
| 80 | + << title |
| 81 | + << RESET << "\n"; |
| 82 | + |
| 83 | + print_runtime_hints_and_at( |
| 84 | + hints, |
| 85 | + !sourceFile.empty() ? ("source: " + sourceFile.filename().string()) : ""); |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + std::unique_ptr<IRuntimeErrorRule> makeDetachedThreadLifetimeRule() |
| 92 | + { |
| 93 | + return std::make_unique<DetachedThreadLifetimeRule>(); |
| 94 | + } |
| 95 | +} // namespace vix::cli::errors::runtime |
0 commit comments