Skip to content

Commit

Permalink
MachRegister: fix initialization order bug (#1731)
Browse files Browse the repository at this point in the history
This fixes the static initialization ordering issue reported in #1730.
  • Loading branch information
hainest committed Apr 22, 2024
1 parent 6ef6687 commit a29f32a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
4 changes: 2 additions & 2 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ set(_public_headers
h/registers/AMDGPU/amdgpu_gfx90a_regs.h
h/registers/AMDGPU/amdgpu_gfx940_regs.h
h/registers/cuda_regs.h
h/registers/MachRegister.h
h/registers/ppc32_regs.h
h/registers/ppc64_regs.h
h/registers/reg_def.h
Expand Down Expand Up @@ -72,12 +71,12 @@ set(_private_headers
src/lprintf.h
src/lru_cache.h
src/MappedFile.h
src/registers/MachRegisterCache.h
src/NodeIterator.h
src/ntHeaders.h
src/parseauxv.h
src/pathName.h
src/pool_allocators.h
src/registers/MachRegister.C
src/sha1.h
src/singleton_object_pool.h
src/stats.h
Expand Down Expand Up @@ -111,6 +110,7 @@ set(_sources
src/debug_common.C
src/VariableLocation.C
src/Buffer.C
src/registers/MachRegister.C
src/MachSyscall.C)

if(DYNINST_OS_UNIX)
Expand Down
8 changes: 8 additions & 0 deletions common/src/dyn_regs.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "registers/MachRegisterCache.h"

namespace Dyninst { namespace registers {
// These are used in MachRegister.C
name_cache names;
register_cache all_regs;
}}

//clang-format: off
#define DYN_DEFINE_REGS
#include "dyn_regs.h"
Expand Down
19 changes: 12 additions & 7 deletions common/src/registers/MachRegister.C
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common/h/registers/MachRegister.h"
#include "registers/MachRegisterCache.h"
#include "debug_common.h"
#include "dyn_regs.h"
#include "external/rose/amdgpuInstructionEnum.h"
Expand All @@ -12,20 +13,24 @@
#include <vector>

namespace {
std::unordered_map<signed int, std::string> names;
const std::string invalid_reg_name{"<INVALID_REG>"};
std::map<Dyninst::Architecture, std::vector<Dyninst::MachRegister>> all_regs;
}

namespace Dyninst { namespace registers {
// These are defined in dyn_regs.C to ensure global constructor initialization ordering
extern name_cache names;
extern register_cache all_regs;
}}

namespace Dyninst {

MachRegister::MachRegister() : reg(0) {}

MachRegister::MachRegister(signed int r) : reg(r) {}

MachRegister::MachRegister(signed int r, std::string n) : MachRegister(r) {
names.emplace(r, std::move(n));
all_regs[getArchitecture()].push_back(*this);
registers::names.emplace(r, std::move(n));
registers::all_regs[getArchitecture()].push_back(*this);
}

unsigned int MachRegister::regClass() const { return reg & 0x00ff0000; }
Expand Down Expand Up @@ -112,8 +117,8 @@ namespace Dyninst {
bool MachRegister::isValid() const { return (reg != InvalidReg.reg); }

std::string const& MachRegister::name() const {
auto iter = names.find(reg);
if(iter != names.end()) {
auto iter = registers::names.find(reg);
if(iter != registers::names.end()) {
return iter->second;
}
common_parsing_printf("No MachRegister found with value %x\n", static_cast<unsigned int>(reg));
Expand Down Expand Up @@ -2636,6 +2641,6 @@ namespace Dyninst {
}

std::vector<MachRegister> const& MachRegister::getAllRegistersForArch(Dyninst::Architecture arch) {
return all_regs[arch];
return registers::all_regs[arch];
}
}
49 changes: 49 additions & 0 deletions common/src/registers/MachRegisterCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef DYNINST_COMMON_REGISTERS_MACHREGISTERCACHE_H
#define DYNINST_COMMON_REGISTERS_MACHREGISTERCACHE_H

#include "registers/MachRegister.h"

#include <unordered_map>
#include <map>
#include <vector>
#include <string>

namespace Dyninst { namespace registers {

using name_cache = std::unordered_map<signed int, std::string>;
using register_cache = std::map<Dyninst::Architecture, std::vector<Dyninst::MachRegister>>;

}}


#endif

0 comments on commit a29f32a

Please sign in to comment.