-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #986 from mandiant/feature-981
add Address abstraction
- Loading branch information
Showing
67 changed files
with
3,484 additions
and
2,310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import abc | ||
|
||
from dncil.clr.token import Token | ||
|
||
|
||
class Address(abc.ABC): | ||
@abc.abstractmethod | ||
def __eq__(self, other): | ||
... | ||
|
||
@abc.abstractmethod | ||
def __lt__(self, other): | ||
# implement < so that addresses can be sorted from low to high | ||
... | ||
|
||
@abc.abstractmethod | ||
def __hash__(self): | ||
# implement hash so that addresses can be used in sets and dicts | ||
... | ||
|
||
@abc.abstractmethod | ||
def __repr__(self): | ||
# implement repr to help during debugging | ||
... | ||
|
||
|
||
class AbsoluteVirtualAddress(int, Address): | ||
"""an absolute memory address""" | ||
|
||
def __new__(cls, v): | ||
assert v >= 0 | ||
return int.__new__(cls, v) | ||
|
||
def __repr__(self): | ||
return f"absolute(0x{self:x})" | ||
|
||
|
||
class RelativeVirtualAddress(int, Address): | ||
"""a memory address relative to a base address""" | ||
|
||
def __repr__(self): | ||
return f"relative(0x{self:x})" | ||
|
||
|
||
class FileOffsetAddress(int, Address): | ||
"""an address relative to the start of a file""" | ||
|
||
def __new__(cls, v): | ||
assert v >= 0 | ||
return int.__new__(cls, v) | ||
|
||
def __repr__(self): | ||
return f"file(0x{self:x})" | ||
|
||
|
||
class DNTokenAddress(Address): | ||
"""a .NET token""" | ||
|
||
def __init__(self, token: Token): | ||
self.token = token | ||
|
||
def __eq__(self, other): | ||
return self.token.value == other.token.value | ||
|
||
def __lt__(self, other): | ||
return self.token.value < other.token.value | ||
|
||
def __hash__(self): | ||
return hash(self.token.value) | ||
|
||
def __repr__(self): | ||
return f"token(0x{self.token.value:x})" | ||
|
||
|
||
class DNTokenOffsetAddress(Address): | ||
"""an offset into an object specified by a .NET token""" | ||
|
||
def __init__(self, token: Token, offset: int): | ||
assert offset >= 0 | ||
self.token = token | ||
self.offset = offset | ||
|
||
def __eq__(self, other): | ||
return (self.token.value, self.offset) == (other.token.value, other.offset) | ||
|
||
def __lt__(self, other): | ||
return (self.token.value, self.offset) < (other.token.value, other.offset) | ||
|
||
def __hash__(self): | ||
return hash((self.token.value, self.offset)) | ||
|
||
def __repr__(self): | ||
return f"token(0x{self.token.value:x})+(0x{self.offset:x})" | ||
|
||
|
||
class _NoAddress(Address): | ||
def __eq__(self, other): | ||
return True | ||
|
||
def __lt__(self, other): | ||
return False | ||
|
||
def __hash__(self): | ||
return hash(0) | ||
|
||
def __repr__(self): | ||
return "no address" | ||
|
||
|
||
NO_ADDRESS = _NoAddress() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.