Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Milestone 4.5: Stack Frame Recovery #16

Merged
merged 16 commits into from
Apr 2, 2018
Merged

Milestone 4.5: Stack Frame Recovery #16

merged 16 commits into from
Apr 2, 2018

Conversation

surovic
Copy link
Collaborator

@surovic surovic commented Mar 27, 2018

This PR replaces the original stack frame recovery pass with a simpler one, tailored to remill-lifted IR. The pass attempts to recover local variables of functions and parameters passed into the function via stack.

This PR requires PR #13

The algorithm first attempts to find stack objects (local variables and stack parameters) based on the usage of pointers derived from the stack pointer register argument of functions (which was recovered previously via the register argument recovery pass). These pointers are then promoted to alloca instructions, which in the case of local variables is the final transformation.

The next phase of recovery deals with stack parameters. The algorithm analyzes a range of instructions between two calls to lifted (and argument recovered) functions and looks for store instructions to stack objects (alloca created values) that correspond to writes of stack parameters. The whole range is constrained to be in a single basic block. After all calls are processed a canonical call is decided upon (at the moment the call with least parameters) and the called function prototype is derived from it.

If the function has no call to it, stack parameters are inferred from stack objects used in the body of the function.

Finally parameters are loaded into calls and stored into stack objects (alloca insts) in the called functions.

…ysis

Argument recovery is now aware of register aliasing (eax, rax, ...)
and recovers argument types and names accordingly. Return type is
recovered from the last user of a return value register specified
by a calling convention, in a basic block with a `llvm::ReturnInst`.
If it's an `llvm::StoreInst` (aka a write) we take the type and
use it as the function return type. If no such write is found,
the function returns `void`. If more than one return type candidate
is found, we terminate. This should be enhanced in the future.
Since we don't actually use the pass anymore
@@ -405,7 +405,7 @@ bool ParameterRegistry::doInitialization(Module& m)

bool ParameterRegistry::runOnModule(Module& m)
{
aaHack.reset(new ProgramMemoryAAResult);
aaHack.reset(new fcd::AddressSpaceAAResult);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent using 2 spaces, not tabs.

Copy link
Collaborator Author

@surovic surovic Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an original fcd source that didn't see any change besides this one, so I thought it would be better not to reformat it or make any big changes. Should I reformat it completely?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm in that case leave it for now. Same for below.

@@ -11,7 +11,7 @@
#define fcd__callconv_params_registry_h

#include "targetinfo.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please adjust all includes to be relative to the root of the source tree, so I assume in this case: #include "fcd/callconv/targetinfo.h".

Copy link
Collaborator Author

@surovic surovic Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Actually I don't know why this even shows up as a change. Maybe because it was changed at one point and then reverted?

fcd/main.cpp Outdated
@@ -80,6 +80,8 @@ DEFINE_string(frameworks, "",
"Apple framework dirs to be used for declarations");
DEFINE_string(includes, "", "Directories to search headers in");

DECLARE_string(callconv);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this calling_convention, or maybe default_calling_convention. I like command-line arguments to be, as much as possible, self-describing, kind of like variable names.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And lol about me using arch and os.. which are terse. Humans are inconsistent!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually another leftover from my experiments with the original fcd code. Deleted in
7effdc6. But comment noted for future.

@@ -457,33 +457,33 @@ class Main {
// Default passes
vector<string> passNames = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::

Copy link
Collaborator Author

@surovic surovic Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I sound like a broken record at this point. But the main.cpp refactor is coming! Issue #4 for more.

return true;
}

llvm::ModulePass *createRemillArgumentRecoveryPass() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make empty parameter lists take void.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 01a78f6

void RemillArgumentRecovery::getAnalysisUsage(
llvm::AnalysisUsage &usage) const {}

bool RemillArgumentRecovery::runOnModule(llvm::Module &module) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a bit more commenting in this file to describe the gist of what's going on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 01a78f6. Also added comments to RemillStackRecovery to clarify the steps of the algorithm.

@surovic surovic merged commit b1f0e1c into master Apr 2, 2018
@surovic surovic deleted the dev-remill-stackrec branch April 3, 2018 09:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants