Skip to content

Commit

Permalink
Add dyninstAPI/src/mapped_object.C
Browse files Browse the repository at this point in the history
  • Loading branch information
hainest committed Apr 3, 2024
1 parent 80a128c commit ac0bfff
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 77 deletions.
77 changes: 75 additions & 2 deletions docs/dyninstAPI/developer/mapped_object.h.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ by the dynamic linker into the applications address space at runtime.
Used for codeRange ONLY! DON'T USE THIS! BAD USER!

.. cpp:function:: void *get_local_ptr() const

Mapped objects may contain multiple :cpp:class:`Symtab::Region`\ s, this function should not be used, but must be
included in the class because this function is a subclass of codeRange.

.. cpp:function:: unsigned get_size() const
.. cpp:function:: AddressSpace *proc() const
.. cpp:function:: mapped_module *findModule(string m_name, bool wildcard = false)
Expand All @@ -82,7 +86,13 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: func_instance *findFuncByEntry(const Dyninst::Address addr)
.. cpp:function:: func_instance *findFuncByEntry(const block_instance *blk)
.. cpp:function:: bool getInfHeapList(std::vector<heapDescriptor> &infHeaps)

Search an object for heapage

.. cpp:function:: void getInferiorHeaps(vector<pair<string, Dyninst::Address>> &infHeaps)

This gets called once per image. Poke through to the internals; all we care about is symbol table information.

.. cpp:function:: bool findFuncsByAddr(const Dyninst::Address addr, std::set<func_instance *> &funcs)
.. cpp:function:: bool findBlocksByAddr(const Dyninst::Address addr, std::set<block_instance *> &blocks)
.. cpp:function:: block_instance *findBlockByEntry(const Dyninst::Address addr)
Expand All @@ -108,12 +118,38 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: void enableDefensiveMode(bool on = true)
.. cpp:function:: bool isExploratoryModeOn()
.. cpp:function:: bool parseNewEdges(const std::vector<edgeStub> &sources)

- The target and source must be in the same mapped region, make sure memory for the target is up to date
- Parse from target address, add new edge at image layer
- Register all newly created functions as a result of new edge parsing
- Add image blocks as block_instances
- Fix up mapping of split blocks with points
- Add image points, as instPoints

.. cpp:function:: bool parseNewFunctions(std::vector<Dyninst::Address> &funcEntryAddrs)

Re-trigger parsing in the object.

This function should only be invoked if all funcEntryAddrs lie within the boundaries of the object.
Copies over the raw data if a funcEntryAddr lies in between the region's disk size and memory size,
also copies raw data if the memory around the entry point has changed.

A true return value means that new functions were parsed.

.. cpp:function:: bool updateCodeBytesIfNeeded(Dyninst::Address entryAddr)

ret true if was needed
Updates the raw code bytes by fetching from memory, if needed

updates if we haven't updated since the last time code could have changed, and if the entry address
is on an unprotected code page, or if the address is in an uninitialized memory.

Returns ``true`` if was needed.

.. cpp:function:: void updateCodeBytes(const std::list<std::pair<Dyninst::Address, Dyninst::Address>> &owRanges)

- Use other update functions to update non-code areas of mapped files, expanding them if we overwrote into unmapped areas
- Copy overwritten regions into the mapped objects

.. cpp:function:: void setCodeBytesUpdated(bool)
.. cpp:function:: void addProtectedPage(Dyninst::Address pageAddr)

Expand All @@ -125,6 +161,9 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: void remove(instPoint *p)
.. cpp:function:: void splitBlock(block_instance *first, block_instance *second)
.. cpp:function:: bool findBlocksByRange(Dyninst::Address startAddr, Dyninst::Address endAddr, std::list<block_instance *> &pageBlocks)

Grabs all block_instances corresponding to the region (horribly inefficient)

.. cpp:function:: void findFuncsByRange(Dyninst::Address startAddr, Dyninst::Address endAddr, std::set<func_instance *> &pageFuncs)
.. cpp:function:: void addEmulInsn(Dyninst::Address insnAddr, Register effective_addr)
.. cpp:function:: bool isEmulInsn(Dyninst::Address insnAddr)
Expand All @@ -134,11 +173,26 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: void replacePLTStub(Dyninst::SymtabAPI::Symbol *PLTsym, func_instance *func, Dyninst::Address newAddr)
.. cpp:function:: private void updateCodeBytes(Dyninst::SymtabAPI::Region *reg)

helper functions
Update mapped data for whole object, or just one region, if specified

This is a helper function. Read unprotected pages into the mapped file (not analyzed code regions so we don't get
instrumentation in our parse).

.. cpp:function:: private bool isUpdateNeeded(Dyninst::Address entryAddr)

Checks if update is needed by looking in the gap between the previous and next block for changes to
the underlying bytes.

Should only be called if we've already checked that we're not on an analyzed
page that's been protected from overwrites, as this check would not be needed.

.. cpp:function:: private bool isExpansionNeeded(Dyninst::Address entryAddr)
.. cpp:function:: private void expandCodeBytes(Dyninst::SymtabAPI::Region *reg)

- Copy the entire region in from the mutatee,
- If memory emulation is not on, copy blocks back in from the mapped file, since we
don't want to copy instrumentation into the mutatee.

.. cpp:function:: bool getSymbolInfo(const std::string &n, int_symbol &sym)

......
Expand All @@ -155,6 +209,9 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: const std::vector<int_variable *> *findVarVectorByPretty(const std::string &varname)
.. cpp:function:: const std::vector<int_variable *> *findVarVectorByMangled(const std::string &varname)
.. cpp:function:: const int_variable *getVariable(const std::string &varname)

Returns one variable, doesn't search other mapped_objects. Use carefully.

.. cpp:function:: void setDirty()

this marks the shared object as dirty, mutated so it needs saved back to disk
Expand All @@ -180,7 +237,13 @@ by the dynamic linker into the applications address space at runtime.
.. cpp:function:: void setCallee(const block_instance *, func_instance *)
.. cpp:function:: func_instance *getCallee(const block_instance *) const
.. cpp:function:: void destroy(PatchAPI::PatchFunction *f)

Does not delete

.. cpp:function:: void destroy(PatchAPI::PatchBlock *b)

Does not delete

.. cpp:member:: private fileDescriptor desc_

full file descriptor
Expand All @@ -198,7 +261,13 @@ by the dynamic linker into the applications address space at runtime.
Where the data starts...

.. cpp:member:: private Dyninst::Address tocBase

.. cpp:function:: private void set_short_name()

Fill in "short_name" data member.

Use last component of "name" data member with FS_FIELD_SEPERATOR ("/") as field separator.

.. cpp:member:: private std::vector<mapped_module *> everyModule
.. cpp:type:: private std::unordered_map<std::string, std::vector<func_instance *> *> func_index_t
.. cpp:type:: private std::unordered_map<std::string, std::vector<int_variable *> *> var_index_t
Expand Down Expand Up @@ -330,3 +399,7 @@ by the dynamic linker into the applications address space at runtime.
#define SHAREDOBJECT_ADDED 1
#define SHAREDOBJECT_REMOVED 2
.. cpp:function:: bool codeBytesUpdateCB(void *objCB, Address targ)

Triggered when parsing needs to check if the underlying data has changed

77 changes: 2 additions & 75 deletions dyninstAPI/src/mapped_object.C
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ using namespace Dyninst::ProcControlAPI;
#else
#define FS_FIELD_SEPERATOR '/'
#endif
// Whee hasher...


// triggered when parsing needs to check if the underlying data has changed
bool codeBytesUpdateCB(void *objCB, Address targ)
{
mapped_object *obj = (mapped_object*) objCB;
Expand Down Expand Up @@ -409,8 +407,6 @@ mapped_module *mapped_object::findModule(pdmodule *pdmod)
return NULL;
}

// fill in "short_name" data member. Use last component of "name" data
// member with FS_FIELD_SEPERATOR ("/") as field seperator....
void mapped_object::set_short_name() {
const char *name_string = fullName_.c_str();
const char *ptr = strrchr(name_string, FS_FIELD_SEPERATOR);
Expand Down Expand Up @@ -562,7 +558,6 @@ const std::vector <int_variable *> *mapped_object::findVarVectorByMangled(const
return allVarsByMangledName[varname];
}

//Returns one variable, doesn't search other mapped_objects. Use carefully.
const int_variable *mapped_object::getVariable(const std::string &varname) {
const std::vector<int_variable *> *vars = NULL;
vars = findVarVectorByPretty(varname);
Expand Down Expand Up @@ -742,7 +737,6 @@ void mapped_object::addFunction(func_instance *func) {
func->mod()->addFunction(func);
}

// Enter a function in all the appropriate tables
int_variable *mapped_object::findVariable(image_variable *img_var) {
if (!img_var) return NULL;

Expand Down Expand Up @@ -817,11 +811,6 @@ void mapped_object::addVariable(int_variable *var) {
var->mod()->addVariable(var);
}

/////////// Dinky functions

// This way we don't have to cross-include every header file in the
// world.

AddressSpace *mapped_object::proc() const { return proc_; }

bool mapped_object::isSharedLib() const
Expand All @@ -845,7 +834,6 @@ const std::string mapped_object::debugString() const
return debug;
}

// Search an object for heapage
bool mapped_object::getInfHeapList(std::vector<heapDescriptor> &infHeaps) {
vector<pair<string,Address> > foundHeaps;

Expand Down Expand Up @@ -939,10 +927,6 @@ unsigned mapped_object::memoryEnd()
return memEnd_;
}


// This gets called once per image. Poke through to the internals;
// all we care about, amusingly, is symbol table information.

void mapped_object::getInferiorHeaps(vector<pair<string, Address> > &foundHeaps)
{
vector<pair<string, Address> > code_heaps;
Expand Down Expand Up @@ -990,9 +974,6 @@ void *mapped_object::getPtrToData(Address addr) const
return image_->codeObject()->cs()->getPtrToData(offset);
}

// mapped objects may contain multiple Symtab::Regions, this function
// should not be used, but must be included in the class because this
// function is a subclass of codeRange
void *mapped_object::get_local_ptr() const
{
assert(0);// if you crash here, blame me. -kevin
Expand Down Expand Up @@ -1046,7 +1027,6 @@ mapped_module* mapped_object::getDefaultModule()
}


// Grabs all block_instances corresponding to the region (horribly inefficient)
bool mapped_object::findBlocksByRange(Address startAddr,
Address endAddr,
list<block_instance*> &rangeBlocks)//output
Expand Down Expand Up @@ -1079,16 +1059,7 @@ void mapped_object::findFuncsByRange(Address startAddr,
}
}

/* Re-trigger parsing in the object. This function should
* only be invoked if all funcEntryAddrs lie within the boundaries of
* the object.
*
* Copies over the raw data if a funcEntryAddr lies in between
* the region's disk size and memory size, also copies raw data
* if the memory around the entry point has changed
*
* A true return value means that new functions were parsed
*/

bool mapped_object::parseNewFunctions(vector<Address> &funcEntryAddrs)
{

Expand Down Expand Up @@ -1162,15 +1133,6 @@ bool mapped_object::parseNewFunctions(vector<Address> &funcEntryAddrs)
return reparsedObject;
}


/* 0. The target and source must be in the same mapped region, make sure memory
* for the target is up to date
* 1. Parse from target address, add new edge at image layer
* 2. Register all newly created functions as a result of new edge parsing
* 3. Add image blocks as block_instances
* 4. fix up mapping of split blocks with points
* 5. Add image points, as instPoints
*/
bool mapped_object::parseNewEdges(const std::vector<edgeStub> &stubs)
{
using namespace SymtabAPI;
Expand Down Expand Up @@ -1297,11 +1259,6 @@ bool mapped_object::parseNewEdges(const std::vector<edgeStub> &stubs)
}


/* 1. Copy the entire region in from the mutatee,
* 2. if memory emulation is not on, copy blocks back in from the
* mapped file, since we don't want to copy instrumentation into
* the mutatee.
*/
void mapped_object::expandCodeBytes(SymtabAPI::Region *reg)
{
assert(reg);
Expand Down Expand Up @@ -1391,9 +1348,6 @@ void mapped_object::expandCodeBytes(SymtabAPI::Region *reg)
}
}

// 1. use other update functions to update non-code areas of mapped files,
// expanding them if we overwrote into unmapped areas
// 2. copy overwritten regions into the mapped objects
void mapped_object::updateCodeBytes(const list<pair<Address,Address> > &owRanges)
{
// 1. use other update functions to update non-code areas of mapped files,
Expand Down Expand Up @@ -1456,12 +1410,6 @@ void mapped_object::updateCodeBytes(const list<pair<Address,Address> > &owRanges
pagesUpdated_ = true;
}

// this is a helper function
//
// update mapped data for whole object, or just one region, if specified
//
// Read unprotected pages into the mapped file
// (not analyzed code regions so we don't get instrumentation in our parse)
void mapped_object::updateCodeBytes(SymtabAPI::Region * symReg)
{
assert(NULL != symReg);
Expand Down Expand Up @@ -1548,12 +1496,7 @@ void mapped_object::updateCodeBytes(SymtabAPI::Region * symReg)
}
}

// checks if update is needed by looking in the gap between the previous
// and next block for changes to the underlying bytes
//
// should only be called if we've already checked that we're not on an
// analyzed page that's been protected from overwrites, as this
// check would not be needed

bool mapped_object::isUpdateNeeded(Address entry)
{
using namespace ParseAPI;
Expand Down Expand Up @@ -1621,7 +1564,6 @@ bool mapped_object::isUpdateNeeded(Address entry)
return updateNeeded;
}

// checks to see if expansion is needed
bool mapped_object::isExpansionNeeded(Address entry)
{
using namespace SymtabAPI;
Expand Down Expand Up @@ -1672,11 +1614,6 @@ bool mapped_object::isExpansionNeeded(Address entry)
}
}

// updates the raw code bytes by fetching from memory, if needed
//
// updates if we haven't updated since the last time code could have
// changed, and if the entry address is on an unprotected code page,
// or if the address is in an uninitialized memory,
bool mapped_object::updateCodeBytesIfNeeded(Address entry)
{

Expand Down Expand Up @@ -1794,12 +1731,10 @@ void mapped_object::remove(instPoint *point)
bpmod->remove(point);
}

// does not delete
void mapped_object::destroy(PatchAPI::PatchBlock *b) {
calleeNames_.erase(SCAST_BI(b));
}

// does not delete
void mapped_object::destroy(PatchAPI::PatchFunction *f) {
remove(SCAST_FI(f));
}
Expand Down Expand Up @@ -1984,14 +1919,6 @@ void mapped_object::setCalleeName(block_instance *b, std::string s) {
calleeNames_[b] = s;
}

// Missing
// findEdge
// findBlock
// findOneBlockByAddr
// splitBlock
// findFuncByEntry
// findBlock (again)

edge_instance *mapped_object::findEdge(ParseAPI::Edge *e,
block_instance *src,
block_instance *trg) {
Expand Down

0 comments on commit ac0bfff

Please sign in to comment.