LibJSGCPluginAction.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LibJSGCPluginAction.h"
  7. #include <clang/ASTMatchers/ASTMatchFinder.h>
  8. #include <clang/ASTMatchers/ASTMatchers.h>
  9. #include <clang/Basic/SourceManager.h>
  10. #include <clang/Frontend/CompilerInstance.h>
  11. #include <clang/Frontend/FrontendPluginRegistry.h>
  12. #include <clang/Lex/MacroArgs.h>
  13. #include <unordered_set>
  14. template<typename T>
  15. class SimpleCollectMatchesCallback : public clang::ast_matchers::MatchFinder::MatchCallback {
  16. public:
  17. explicit SimpleCollectMatchesCallback(std::string name)
  18. : m_name(std::move(name))
  19. {
  20. }
  21. void run(clang::ast_matchers::MatchFinder::MatchResult const& result) override
  22. {
  23. if (auto const* node = result.Nodes.getNodeAs<T>(m_name))
  24. m_matches.push_back(node);
  25. }
  26. auto const& matches() const { return m_matches; }
  27. private:
  28. std::string m_name;
  29. std::vector<T const*> m_matches;
  30. };
  31. bool record_inherits_from_cell(clang::CXXRecordDecl const& record)
  32. {
  33. if (!record.isCompleteDefinition())
  34. return false;
  35. bool inherits_from_cell = record.getQualifiedNameAsString() == "JS::Cell";
  36. record.forallBases([&](clang::CXXRecordDecl const* base) -> bool {
  37. if (base->getQualifiedNameAsString() == "JS::Cell") {
  38. inherits_from_cell = true;
  39. return false;
  40. }
  41. return true;
  42. });
  43. return inherits_from_cell;
  44. }
  45. std::vector<clang::QualType> get_all_qualified_types(clang::QualType const& type)
  46. {
  47. std::vector<clang::QualType> qualified_types;
  48. if (auto const* template_specialization = type->getAs<clang::TemplateSpecializationType>()) {
  49. auto specialization_name = template_specialization->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();
  50. // Do not unwrap GCPtr/NonnullGCPtr/MarkedVector
  51. if (specialization_name == "JS::GCPtr" || specialization_name == "JS::NonnullGCPtr" || specialization_name == "JS::RawGCPtr" || specialization_name == "JS::MarkedVector") {
  52. qualified_types.push_back(type);
  53. } else {
  54. auto const template_arguments = template_specialization->template_arguments();
  55. for (size_t i = 0; i < template_arguments.size(); i++) {
  56. auto const& template_arg = template_arguments[i];
  57. if (template_arg.getKind() == clang::TemplateArgument::Type) {
  58. auto template_qualified_types = get_all_qualified_types(template_arg.getAsType());
  59. std::move(template_qualified_types.begin(), template_qualified_types.end(), std::back_inserter(qualified_types));
  60. }
  61. }
  62. }
  63. } else {
  64. qualified_types.push_back(type);
  65. }
  66. return qualified_types;
  67. }
  68. struct FieldValidationResult {
  69. bool is_valid { false };
  70. bool is_wrapped_in_gcptr { false };
  71. bool needs_visiting { false };
  72. };
  73. FieldValidationResult validate_field(clang::FieldDecl const* field_decl)
  74. {
  75. auto type = field_decl->getType();
  76. if (auto const* elaborated_type = llvm::dyn_cast<clang::ElaboratedType>(type.getTypePtr()))
  77. type = elaborated_type->desugar();
  78. FieldValidationResult result { .is_valid = true };
  79. for (auto const& qualified_type : get_all_qualified_types(type)) {
  80. if (auto const* pointer_decl = qualified_type->getAs<clang::PointerType>()) {
  81. if (auto const* pointee = pointer_decl->getPointeeCXXRecordDecl()) {
  82. if (record_inherits_from_cell(*pointee)) {
  83. result.is_valid = false;
  84. result.is_wrapped_in_gcptr = false;
  85. result.needs_visiting = true;
  86. return result;
  87. }
  88. }
  89. } else if (auto const* reference_decl = qualified_type->getAs<clang::ReferenceType>()) {
  90. if (auto const* pointee = reference_decl->getPointeeCXXRecordDecl()) {
  91. if (record_inherits_from_cell(*pointee)) {
  92. result.is_valid = false;
  93. result.is_wrapped_in_gcptr = false;
  94. result.needs_visiting = true;
  95. return result;
  96. }
  97. }
  98. } else if (auto const* specialization = qualified_type->getAs<clang::TemplateSpecializationType>()) {
  99. auto template_type_name = specialization->getTemplateName().getAsTemplateDecl()->getName();
  100. if (template_type_name != "GCPtr" && template_type_name != "NonnullGCPtr" && template_type_name != "RawGCPtr")
  101. return result;
  102. auto const template_args = specialization->template_arguments();
  103. if (template_args.size() != 1)
  104. return result; // Not really valid, but will produce a compilation error anyway
  105. auto const& type_arg = template_args[0];
  106. auto const* record_type = type_arg.getAsType()->getAs<clang::RecordType>();
  107. if (!record_type)
  108. return result;
  109. auto const* record_decl = record_type->getAsCXXRecordDecl();
  110. if (!record_decl->hasDefinition())
  111. return result;
  112. result.is_wrapped_in_gcptr = true;
  113. result.is_valid = record_inherits_from_cell(*record_decl);
  114. result.needs_visiting = template_type_name != "RawGCPtr";
  115. }
  116. }
  117. return result;
  118. }
  119. bool LibJSGCVisitor::VisitCXXRecordDecl(clang::CXXRecordDecl* record)
  120. {
  121. using namespace clang::ast_matchers;
  122. if (!record || !record->isCompleteDefinition() || (!record->isClass() && !record->isStruct()))
  123. return true;
  124. // Cell triggers a bunch of warnings for its empty visit_edges implementation, but
  125. // it doesn't have any members anyways so it's fine to just ignore.
  126. auto qualified_name = record->getQualifiedNameAsString();
  127. if (qualified_name == "JS::Cell")
  128. return true;
  129. auto& diag_engine = m_context.getDiagnostics();
  130. std::vector<clang::FieldDecl const*> fields_that_need_visiting;
  131. for (clang::FieldDecl const* field : record->fields()) {
  132. auto validation_results = validate_field(field);
  133. if (!validation_results.is_valid) {
  134. if (validation_results.is_wrapped_in_gcptr) {
  135. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Specialization type must inherit from JS::Cell");
  136. diag_engine.Report(field->getLocation(), diag_id);
  137. } else {
  138. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 to JS::Cell type should be wrapped in %1");
  139. auto builder = diag_engine.Report(field->getLocation(), diag_id);
  140. if (field->getType()->isReferenceType()) {
  141. builder << "reference"
  142. << "JS::NonnullGCPtr";
  143. } else {
  144. builder << "pointer"
  145. << "JS::GCPtr";
  146. }
  147. }
  148. } else if (validation_results.needs_visiting) {
  149. fields_that_need_visiting.push_back(field);
  150. }
  151. }
  152. if (!record_inherits_from_cell(*record))
  153. return true;
  154. validate_record_macros(*record);
  155. clang::DeclarationName name = &m_context.Idents.get("visit_edges");
  156. auto const* visit_edges_method = record->lookup(name).find_first<clang::CXXMethodDecl>();
  157. if (!visit_edges_method && !fields_that_need_visiting.empty()) {
  158. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "JS::Cell-inheriting class %0 contains a GC-allocated member %1 but has no visit_edges method");
  159. auto builder = diag_engine.Report(record->getLocation(), diag_id);
  160. builder << record->getName()
  161. << fields_that_need_visiting[0];
  162. }
  163. if (!visit_edges_method || !visit_edges_method->getBody())
  164. return true;
  165. // Search for a call to Base::visit_edges. Note that this also has the nice side effect of
  166. // ensuring the classes use JS_CELL/JS_OBJECT, as Base will not be defined if they do not.
  167. MatchFinder base_visit_edges_finder;
  168. SimpleCollectMatchesCallback<clang::MemberExpr> base_visit_edges_callback("member-call");
  169. auto base_visit_edges_matcher = cxxMethodDecl(
  170. ofClass(hasName(qualified_name)),
  171. functionDecl(hasName("visit_edges")),
  172. isOverride(),
  173. hasDescendant(memberExpr(member(hasName("visit_edges"))).bind("member-call")));
  174. base_visit_edges_finder.addMatcher(base_visit_edges_matcher, &base_visit_edges_callback);
  175. base_visit_edges_finder.matchAST(m_context);
  176. bool call_to_base_visit_edges_found = false;
  177. for (auto const* call_expr : base_visit_edges_callback.matches()) {
  178. // FIXME: Can we constrain the matcher above to avoid looking directly at the source code?
  179. auto const* source_chars = m_context.getSourceManager().getCharacterData(call_expr->getBeginLoc());
  180. if (strncmp(source_chars, "Base::", 6) == 0) {
  181. call_to_base_visit_edges_found = true;
  182. break;
  183. }
  184. }
  185. if (!call_to_base_visit_edges_found) {
  186. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Missing call to Base::visit_edges");
  187. diag_engine.Report(visit_edges_method->getBeginLoc(), diag_id);
  188. }
  189. // Search for uses of all fields that need visiting. We don't ensure they are _actually_ visited
  190. // with a call to visitor.visit(...), as that is too complex. Instead, we just assume that if the
  191. // field is accessed at all, then it is visited.
  192. if (fields_that_need_visiting.empty())
  193. return true;
  194. MatchFinder field_access_finder;
  195. SimpleCollectMatchesCallback<clang::MemberExpr> field_access_callback("member-expr");
  196. auto field_access_matcher = memberExpr(
  197. hasAncestor(cxxMethodDecl(hasName("visit_edges"))),
  198. hasObjectExpression(hasType(pointsTo(cxxRecordDecl(hasName(record->getName()))))))
  199. .bind("member-expr");
  200. field_access_finder.addMatcher(field_access_matcher, &field_access_callback);
  201. field_access_finder.matchAST(visit_edges_method->getASTContext());
  202. std::unordered_set<std::string> fields_that_are_visited;
  203. for (auto const* member_expr : field_access_callback.matches())
  204. fields_that_are_visited.insert(member_expr->getMemberNameInfo().getAsString());
  205. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "GC-allocated member is not visited in %0::visit_edges");
  206. for (auto const* field : fields_that_need_visiting) {
  207. if (!fields_that_are_visited.contains(field->getNameAsString())) {
  208. auto builder = diag_engine.Report(field->getBeginLoc(), diag_id);
  209. builder << record->getName();
  210. }
  211. }
  212. return true;
  213. }
  214. struct CellTypeWithOrigin {
  215. clang::CXXRecordDecl const& base_origin;
  216. LibJSCellMacro::Type type;
  217. };
  218. std::optional<CellTypeWithOrigin> find_cell_type_with_origin(clang::CXXRecordDecl const& record)
  219. {
  220. for (auto const& base : record.bases()) {
  221. if (auto const* base_record = base.getType()->getAsCXXRecordDecl()) {
  222. auto base_name = base_record->getQualifiedNameAsString();
  223. if (base_name == "JS::Cell")
  224. return CellTypeWithOrigin { *base_record, LibJSCellMacro::Type::JSCell };
  225. if (base_name == "JS::Object")
  226. return CellTypeWithOrigin { *base_record, LibJSCellMacro::Type::JSObject };
  227. if (base_name == "JS::Environment")
  228. return CellTypeWithOrigin { *base_record, LibJSCellMacro::Type::JSEnvironment };
  229. if (base_name == "JS::PrototypeObject")
  230. return CellTypeWithOrigin { *base_record, LibJSCellMacro::Type::JSPrototypeObject };
  231. if (base_name == "Web::Bindings::PlatformObject")
  232. return CellTypeWithOrigin { *base_record, LibJSCellMacro::Type::WebPlatformObject };
  233. if (auto origin = find_cell_type_with_origin(*base_record))
  234. return CellTypeWithOrigin { *base_record, origin->type };
  235. }
  236. }
  237. return {};
  238. }
  239. LibJSGCVisitor::CellMacroExpectation LibJSGCVisitor::get_record_cell_macro_expectation(clang::CXXRecordDecl const& record)
  240. {
  241. auto origin = find_cell_type_with_origin(record);
  242. assert(origin.has_value());
  243. // Need to iterate the bases again to turn the record into the exact text that the user used as
  244. // the class base, since it doesn't have to be qualified (but might be).
  245. for (auto const& base : record.bases()) {
  246. if (auto const* base_record = base.getType()->getAsCXXRecordDecl()) {
  247. if (base_record == &origin->base_origin) {
  248. auto& source_manager = m_context.getSourceManager();
  249. auto char_range = source_manager.getExpansionRange({ base.getBaseTypeLoc(), base.getEndLoc() });
  250. auto exact_text = clang::Lexer::getSourceText(char_range, source_manager, m_context.getLangOpts());
  251. return { origin->type, exact_text.str() };
  252. }
  253. }
  254. }
  255. assert(false);
  256. }
  257. void LibJSGCVisitor::validate_record_macros(clang::CXXRecordDecl const& record)
  258. {
  259. auto& source_manager = m_context.getSourceManager();
  260. auto record_range = record.getSourceRange();
  261. // FIXME: The current macro detection doesn't recursively search through macro expansion,
  262. // so if the record itself is defined in a macro, the JS_CELL/etc won't be found
  263. if (source_manager.isMacroBodyExpansion(record_range.getBegin()))
  264. return;
  265. auto [expected_cell_macro_type, expected_base_name] = get_record_cell_macro_expectation(record);
  266. auto file_id = m_context.getSourceManager().getFileID(record.getLocation());
  267. auto it = m_macro_map.find(file_id.getHashValue());
  268. auto& diag_engine = m_context.getDiagnostics();
  269. auto report_missing_macro = [&] {
  270. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Expected record to have a %0 macro invocation");
  271. auto builder = diag_engine.Report(record.getLocation(), diag_id);
  272. builder << LibJSCellMacro::type_name(expected_cell_macro_type);
  273. };
  274. if (it == m_macro_map.end()) {
  275. report_missing_macro();
  276. return;
  277. }
  278. std::vector<clang::SourceRange> sub_ranges;
  279. for (auto const& sub_decl : record.decls()) {
  280. if (auto const* sub_record = llvm::dyn_cast<clang::CXXRecordDecl>(sub_decl))
  281. sub_ranges.push_back(sub_record->getSourceRange());
  282. }
  283. bool found_macro = false;
  284. auto record_name = record.getDeclName().getAsString();
  285. if (record.getQualifier()) {
  286. // FIXME: There has to be a better way to get this info. getQualifiedNameAsString() gets too much info
  287. // (outer namespaces that aren't part of the class identifier), and getNameAsString() doesn't get
  288. // enough info (doesn't include parts before the namespace specifier).
  289. auto loc = record.getQualifierLoc();
  290. auto& sm = m_context.getSourceManager();
  291. auto begin_offset = sm.getFileOffset(loc.getBeginLoc());
  292. auto end_offset = sm.getFileOffset(loc.getEndLoc());
  293. auto const* file_buf = sm.getCharacterData(loc.getBeginLoc());
  294. auto prefix = std::string { file_buf, end_offset - begin_offset };
  295. record_name = prefix + "::" + record_name;
  296. }
  297. for (auto const& macro : it->second) {
  298. if (record_range.fullyContains(macro.range)) {
  299. bool macro_is_in_sub_decl = false;
  300. for (auto const& sub_range : sub_ranges) {
  301. if (sub_range.fullyContains(macro.range)) {
  302. macro_is_in_sub_decl = true;
  303. break;
  304. }
  305. }
  306. if (macro_is_in_sub_decl)
  307. continue;
  308. if (found_macro) {
  309. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Record has multiple JS_CELL-like macro invocations");
  310. diag_engine.Report(record_range.getBegin(), diag_id);
  311. }
  312. found_macro = true;
  313. if (macro.type != expected_cell_macro_type) {
  314. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Invalid JS-CELL-like macro invocation; expected %0");
  315. auto builder = diag_engine.Report(macro.range.getBegin(), diag_id);
  316. builder << LibJSCellMacro::type_name(expected_cell_macro_type);
  317. }
  318. // This is a compile error, no diagnostic needed
  319. if (macro.args.size() < 2)
  320. return;
  321. if (macro.args[0].text != record_name) {
  322. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Expected first argument of %0 macro invocation to be %1");
  323. auto builder = diag_engine.Report(macro.args[0].location, diag_id);
  324. builder << LibJSCellMacro::type_name(expected_cell_macro_type) << record_name;
  325. }
  326. if (expected_cell_macro_type == LibJSCellMacro::Type::JSPrototypeObject) {
  327. // FIXME: Validate the args for this macro
  328. } else if (macro.args[1].text != expected_base_name) {
  329. auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Expected second argument of %0 macro invocation to be %1");
  330. auto builder = diag_engine.Report(macro.args[1].location, diag_id);
  331. builder << LibJSCellMacro::type_name(expected_cell_macro_type) << expected_base_name;
  332. }
  333. }
  334. }
  335. if (!found_macro)
  336. report_missing_macro();
  337. }
  338. LibJSGCASTConsumer::LibJSGCASTConsumer(clang::CompilerInstance& compiler)
  339. : m_compiler(compiler)
  340. {
  341. auto& preprocessor = compiler.getPreprocessor();
  342. preprocessor.addPPCallbacks(std::make_unique<LibJSPPCallbacks>(preprocessor, m_macro_map));
  343. }
  344. void LibJSGCASTConsumer::HandleTranslationUnit(clang::ASTContext& context)
  345. {
  346. LibJSGCVisitor visitor { context, m_macro_map };
  347. visitor.TraverseDecl(context.getTranslationUnitDecl());
  348. }
  349. char const* LibJSCellMacro::type_name(Type type)
  350. {
  351. switch (type) {
  352. case Type::JSCell:
  353. return "JS_CELL";
  354. case Type::JSObject:
  355. return "JS_OBJECT";
  356. case Type::JSEnvironment:
  357. return "JS_ENVIRONMENT";
  358. case Type::JSPrototypeObject:
  359. return "JS_PROTOTYPE_OBJECT";
  360. case Type::WebPlatformObject:
  361. return "WEB_PLATFORM_OBJECT";
  362. default:
  363. __builtin_unreachable();
  364. }
  365. }
  366. void LibJSPPCallbacks::LexedFileChanged(clang::FileID curr_fid, LexedFileChangeReason reason, clang::SrcMgr::CharacteristicKind, clang::FileID, clang::SourceLocation)
  367. {
  368. if (reason == LexedFileChangeReason::EnterFile) {
  369. m_curr_fid_hash_stack.push_back(curr_fid.getHashValue());
  370. } else {
  371. assert(!m_curr_fid_hash_stack.empty());
  372. m_curr_fid_hash_stack.pop_back();
  373. }
  374. }
  375. void LibJSPPCallbacks::MacroExpands(clang::Token const& name_token, clang::MacroDefinition const&, clang::SourceRange range, clang::MacroArgs const* args)
  376. {
  377. if (auto* ident_info = name_token.getIdentifierInfo()) {
  378. static llvm::StringMap<LibJSCellMacro::Type> libjs_macro_types {
  379. { "JS_CELL", LibJSCellMacro::Type::JSCell },
  380. { "JS_OBJECT", LibJSCellMacro::Type::JSObject },
  381. { "JS_ENVIRONMENT", LibJSCellMacro::Type::JSEnvironment },
  382. { "JS_PROTOTYPE_OBJECT", LibJSCellMacro::Type::JSPrototypeObject },
  383. { "WEB_PLATFORM_OBJECT", LibJSCellMacro::Type::WebPlatformObject },
  384. };
  385. auto name = ident_info->getName();
  386. if (auto it = libjs_macro_types.find(name); it != libjs_macro_types.end()) {
  387. LibJSCellMacro macro { range, it->second, {} };
  388. for (size_t arg_index = 0; arg_index < args->getNumMacroArguments(); arg_index++) {
  389. auto const* first_token = args->getUnexpArgument(arg_index);
  390. auto stringified_token = clang::MacroArgs::StringifyArgument(first_token, m_preprocessor, false, range.getBegin(), range.getEnd());
  391. // The token includes leading and trailing quotes
  392. auto len = strlen(stringified_token.getLiteralData());
  393. std::string arg_text { stringified_token.getLiteralData() + 1, len - 2 };
  394. macro.args.push_back({ arg_text, first_token->getLocation() });
  395. }
  396. assert(!m_curr_fid_hash_stack.empty());
  397. auto curr_fid_hash = m_curr_fid_hash_stack.back();
  398. if (m_macro_map.find(curr_fid_hash) == m_macro_map.end())
  399. m_macro_map[curr_fid_hash] = {};
  400. m_macro_map[curr_fid_hash].push_back(macro);
  401. }
  402. }
  403. }
  404. static clang::FrontendPluginRegistry::Add<LibJSGCPluginAction> X("libjs_gc_scanner", "analyze LibJS GC usage");