From 9d1d4e28b3a949fafcfd922fc9e040e87c95fe10 Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 16 Aug 2024 13:59:21 +0200 Subject: [PATCH] ScriptOpt: Ensure global statements have non-null scope The ProfileFunc() logic assumed that GetScope() returned a non-nullptr. This holds except for the synthetic global statements function. Fix the latter and add an assert, also add a name to the type so it's easier to recognize in a debugger what's going on, otherwise the name is "". This was found by UBSAN due to it seeing the ->OrderedVars() call on a nullptr. Elsewhere, num_params == 0 shielded from that access and so didn't lead to crashes. --- src/script_opt/ProfileFunc.cc | 2 ++ src/script_opt/ScriptOpt.cc | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index d6d096f374..5d1f07ae09 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -80,6 +80,8 @@ ProfileFunc::ProfileFunc(const Expr* e, bool _abs_rec_fields) { void ProfileFunc::Profile(const FuncType* ft, const StmtPtr& body) { num_params = ft->Params()->NumFields(); + assert(profiled_scope != nullptr); + auto& ov = profiled_scope->OrderedVars(); for ( int i = 0; i < num_params; ++i ) params.insert(ov[i].get()); diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index 28dd288054..56ebaf8f11 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -66,12 +66,14 @@ void analyze_global_stmts(Stmt* stmts) { auto id = install_ID("", GLOBAL_MODULE_NAME, true, false); auto empty_args_t = make_intrusive(nullptr); auto func_t = make_intrusive(empty_args_t, nullptr, FUNC_FLAVOR_FUNCTION); + func_t->SetName(""); id->SetType(func_t); auto sc = current_scope(); std::vector empty_inits; global_stmts = make_intrusive(id); global_stmts->AddBody(stmts->ThisPtr(), empty_inits, sc->Length()); + global_stmts->SetScope(sc); global_stmts_ind = funcs.size(); funcs.emplace_back(global_stmts, sc, stmts->ThisPtr(), 0);