Coder Social home page Coder Social logo

examon / apex Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 831 KB

APEX is a tool that can extract active code path from the C program source code and recompile this path into new, standalone executable.

License: Apache License 2.0

CMake 0.12% Makefile 0.11% Shell 2.18% C 2.71% C++ 67.09% Python 0.35% Objective-C 0.01% HTML 22.02% CSS 1.91% JavaScript 0.43% TeX 2.82% PostScript 0.26%

apex's People

Contributors

examon avatar

Watchers

 avatar  avatar

apex's Issues

call to exit() fails


===================
# module: <stdin> #
===================
; ModuleID = '<stdin>'
source_filename = "c-code/test_dependencies_minimal.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define void @test() #0 {
  ret void
}

; Function Attrs: nounwind uwtable
define i32 @x() #0 {
  ret i32 10
}

; Function Attrs: nounwind uwtable
define i32 @y(i32) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, i32* %2, align 4
  %4 = load i32, i32* %2, align 4
  %5 = load i32, i32* %2, align 4
  %6 = add nsw i32 %4, %5
  store i32 %6, i32* %3, align 4
  %7 = load i32, i32* %3, align 4
  ret i32 %7
}

; Function Attrs: nounwind uwtable
define i32 @z() #0 {
  %1 = alloca i32, align 4
  store i32 1, i32* %1, align 4
  %2 = load i32, i32* %1, align 4
  ret i32 %2
}

; Function Attrs: nounwind uwtable
define i32 @n() #0 {
  %1 = alloca i32, align 4
  %2 = call i32 @y(i32 10)
  store i32 %2, i32* %1, align 4
  %3 = load i32, i32* %1, align 4
  ret i32 %3
}

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %9 = call i32 @x()
  store i32 %9, i32* %2, align 4
  %10 = load i32, i32* %2, align 4
  %11 = call i32 @y(i32 %10)
  store i32 %11, i32* %3, align 4
  %12 = load i32, i32* %3, align 4
  store i32 %12, i32* %4, align 4
  %13 = call i32 @z()
  store i32 %13, i32* %5, align 4
  %14 = load i32, i32* %5, align 4
  store i32 %14, i32* %6, align 4
  %15 = call i32 @n()
  store i32 %15, i32* %7, align 4
  %16 = load i32, i32* %7, align 4
  store i32 %16, i32* %8, align 4
  ret i32 0
}

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.9.1 (tags/RELEASE_391/final)"}


=======================================
# dgInit(): Building dependence graph #
=======================================
- done

================================================
# apexDgInit(): Building apex dependency graph #
================================================
- done

=======================================================
# apexDgMakeGraph(): building data dependencies graph #
=======================================================
- done

==========================================
# createCallGraph(): Building call graph #
==========================================
- done

=============================================
# findPath(): Finding path from @main to @y #
=============================================
Searching for @main function in callgraph:
- done

Running BFS to find path from @main to @y:
- done

Reconstructing and storing @main -> @y path to the @final_path:
- done

==========================
# printPath(): main -> y #
==========================
main
n
y

=======================================================================================
# updatePathAddDependencies(): Calculating and possibly adding dependencies to @path. #
=======================================================================================
path: main, n, y, 
investigation_list: main, n, y, 
investigating: main
- main is calling: x
- main is calling: y
-- y is in @path
- main is calling: z
- main is calling: n
-- n is in @path

path: main, n, y, x, 
investigation_list: n, y, 
investigating: n
- n is calling: y
-- y is in @path

path: main, n, y, x, 
investigation_list: y, 
investigating: y


==================================================================================
# moduleRemoveFunctionsNotInPath(): Removing functions that do not affect @path. #
==================================================================================
Collecting functions that are going to be removed:
- path: main, n, y, x, 
- functions_to_be_removed: test, z, 

Collecting instruction dependencies for each function that has been marked for removal:
- function: test
-- instruction dependencies count: 0
-- unique instruction dependencies count: 0
- function: z
-- instruction dependencies count: 4
-- unique instruction dependencies count: 4

Removing collected instruction dependencies:
- removed instructions count: 4

Removing marked function bodies:
- removed functions count: 2

===================================
# Getting/picking callsite for @y #
===================================
Collected function callers:
- main
- n

Picking one caller:
- chosen caller id: n

Searching call instruction to @y inside @n:
- call instruction to @y found

Inserting call instruction to exit() before chosen instruction.
- loaded function: exit

declare void @exit(i32)

- exit call instruction created:   call void @exit(i32 0)

=======================
# 
Final Module Dump: #
=======================
; ModuleID = '<stdin>'
source_filename = "c-code/test_dependencies_minimal.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: nounwind uwtable
define i32 @x() #0 {
  ret i32 10
}

; Function Attrs: nounwind uwtable
define i32 @y(i32) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, i32* %2, align 4
  %4 = load i32, i32* %2, align 4
  %5 = load i32, i32* %2, align 4
  %6 = add nsw i32 %4, %5
  store i32 %6, i32* %3, align 4
  %7 = load i32, i32* %3, align 4
  ret i32 %7
}

; Function Attrs: nounwind uwtable
define i32 @n() #0 {
  %1 = alloca i32, align 4
  %2 = call i32 @y(i32 10)
  store i32 %2, i32* %1, align 4
  %3 = load i32, i32* %1, align 4
  ret i32 %3
}

; Function Attrs: nounwind uwtable
define i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, i32* %1, align 4
  %9 = call i32 @x()
  store i32 %9, i32* %2, align 4
  %10 = load i32, i32* %2, align 4
  %11 = call i32 @y(i32 %10)
  store i32 %11, i32* %3, align 4
  %12 = load i32, i32* %3, align 4
  store i32 %12, i32* %4, align 4
  %13 = call i32 @n()
  store i32 %13, i32* %7, align 4
  %14 = load i32, i32* %7, align 4
  store i32 %14, i32* %8, align 4
  ret i32 0
}

declare void @exit(i32)

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.9.1 (tags/RELEASE_391/final)"}

================
# APEXPass END #
================
Global is referenced by parentless instruction!
void (i32)* @exit
; ModuleID = '<stdin>'
  call void @exit(i32 0)
LLVM ERROR: Broken module found, compilation aborted!
========= Exporting callgraphs.
Writing 'callgraph.dot'...
Writing 'callgraph.dot'...
opt: ../bytecode_from_input_basic_opts_and_apex.bc: error: Could not open input file: No such file or directory
mv: cannot stat 'callgraph.dot': No such file or directory
Error: dot: can't open callgraph_apex_opt.dot
========= Running dg tools.
INFO: Points-to analysis took 0 sec 0 ms
INFO: Reaching defs analysis took 0 sec 0 ms
INFO: Adding Def-Use edges took 0 sec 0 ms
INFO: computing control dependencies took 0 sec 0 ms
Warning: cluster named cluster_bb_0x286c4c0 not found
Warning: cluster named cluster_bb_0x286d0c0 not found
Warning: cluster named cluster_bb_0x286a440 not found
Warning: cluster named cluster_bb_0x2868620 not found
Warning: cluster named cluster_bb_0x2866510 not found
========= Disassembling llvm bytecode.
llvm-dis: Could not open build/bytecode_from_input_basic_opts_and_apex.bc: No such file or directory

========= Running bytecode_from_input.bc
========= Running bytecode_from_input_basic_opts.bc
========= Running bytecode_from_input_basic_opts_and_apex.bc
lli: build/bytecode_from_input_basic_opts_and_apex.bc: error: Could not open input file: No such file or directory
Makefile:2: recipe for target 'run' failed
make: *** [run] Error 1







How to create function call with variable argument.

    std::vector<Type *> params_tmp = {Type::getInt32Ty(M.getContext())};
    ArrayRef<Type *> params = makeArrayRef(params_tmp);

    FunctionType *fType = FunctionType::get(Type::getVoidTy(M.getContext()), params, false);
    Constant *temp = M.getOrInsertFunction("lib_dump_int", fType);
    Function *dump_fcn = cast<Function>(temp);

    Value *dump_arg_val = ConstantInt::get(Type::getInt32Ty(M.getContext()), 123456);
    ArrayRef<Value *> dump_params = makeArrayRef(dump_arg_val);

    CallInst *dump_call_inst = CallInst::Create(dump_fcn, dump_params, "");

Code above creates the following instruction:

  call void @lib_dump_int(i32 123456)

But what if I want to set argument to %f instead of 123456?
Like this:

  store i32 %call, i32* %f, align 4
  call void @lib_dump_int(i32 %f)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.