Skip to content

Commit 101dd6a

Browse files
committed
DOC: fix name in readme
1 parent 7baf6e2 commit 101dd6a

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

README.md

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,104 @@
1-
# ADIN LLVM plugin
1+
# ADIN LLVM Pass
22

3-
Introduction:
4-
The ADIN LLVM plugin is Transform Plugin for Runtime Hooking of Memory Operations is a crucial component within the ADIN LLVM fork. Designed to enhance the capabilities of the LLVM compiler infrastructure, this plugin facilitates the dynamic modification of memory operations, such as store and load operations, by replacing them with custom hook functions at runtime. By integrating this powerful plugin into your development workflow, you can gain fine-grained control over memory access and inject custom logic into your programs.
3+
![logo](img/logo.png)
4+
5+
6+
## Introduction:
7+
The **ADIN LLVM pass** is Transform LLVM pass for Runtime Hooking of Memory Operations is a crucial component within the [**ADIN LLVM fork**](https://github.com/remotemcu/adin-llvm). Designed to enhance the capabilities of the LLVM compiler infrastructure, this pass(plugin) facilitates the dynamic modification of memory operations, such as store and load operations, by replacing them with custom hook functions at runtime. By integrating this powerful plugin(pass) into your development workflow, you can gain fine-grained control over memory access and inject custom logic into your programs.
8+
9+
10+
## How to Build
11+
See [**ADIN LLVM fork doc**](https://github.com/remotemcu/adin-llvm)
12+
13+
14+
15+
## Usage
16+
To utilize the memory operation hooking capabilities of the **ADIN LLVM plugin**, you can modify LLVM IR compiled code using the `opt` tool of the [**ADIN LLVM fork**](https://github.com/remotemcu/adin-llvm) with the `-adin` flag. Here's an example to help you understand the process:
17+
18+
Let's assume you have a simple C code file named `example.c`.
19+
```c
20+
int var = 0;
21+
22+
void f(){
23+
*(int*)0x100 = 1;
24+
var = *(int*)0x100;
25+
}
26+
```
27+
28+
To compile it into LLVM IR code using Clang, execute the following command:
29+
30+
```shell
31+
clang -S -emit-llvm example.c -o example.ll
32+
```
33+
34+
This command will generate the LLVM IR code file `example.ll` based on your C code.
35+
```llvm
36+
; Function Attrs: noinline nounwind optnone uwtable
37+
define dso_local void @f() #0 {
38+
store i32 1, i32* inttoptr (i64 256 to i32*), align 4
39+
%1 = load i32, i32* inttoptr (i64 256 to i32*), align 4
40+
store i32 %1, i32* @b, align 4
41+
ret void
42+
}
43+
44+
```
45+
46+
Now, you can use the **ADIN LLVM plugin** to modify the LLVM IR code and add memory operation hooks. Run the following command:
47+
48+
```shell
49+
opt -adin -S example.ll-o adin_modified_example.ll
50+
```
51+
52+
the `-adin` flag indicates that you want to perform memory operation hooking on the input LLVM IR code. The modified LLVM IR code will be written to the `modified.ll` file.
53+
54+
```llvm
55+
define dso_local void @f() #0 {
56+
call void @__adin_store_(i8* inttoptr (i64 256 to i8*), i64 1, i32 32, i32 4)
57+
%load_i32_ = call i64 @__adin_load_(i8* inttoptr (i64 256 to i8*), i32 32, i32 4)
58+
%truncated_i32_ = trunc i64 %load_i32_ to i32
59+
store i32 %truncated_i32_, i32* @b, align 4
60+
ret void
61+
}
62+
63+
```
64+
65+
In the modified LLVM IR code (`modified.ll`), the original store and load operations have been replaced with the `__adin_store_` and `__adin_load_` functions. These functions are the hook functions provided by the ADIN LLVM Fork, which allow you to intercept and modify the behavior of memory operations.
66+
67+
You can define and implement these hook functions in C/C++ code to perform any desired modifications or additional actions before or after the memory operations.
68+
69+
* `__adin_store_` function will be called instead of a regular store operation,
70+
* `__adin_load_` function will be called instead of a regular load operation.
71+
72+
To implement the **__adin_store_** and **__adin_load_** hook functions in your C/C++ code for performing desired modifications or additional actions before memory operations, you can follow a similar approach to what is done in the [Address Interceptor Lib]. Here's an example:
73+
74+
```c
75+
extern "C" void __adin_store_(llvm_pass_addr pointer, llvm_value_type value, llvm_pass_arg TypeSizeArg, llvm_pass_arg AlignmentArg)
76+
{
77+
//...
78+
}
79+
80+
extern "C" llvm_value_type __adin_load_(const llvm_pass_addr pointer, llvm_pass_arg TypeSizeArg, llvm_pass_arg AlignmentArg)
81+
{
82+
//...
83+
}
84+
```
85+
86+
Finally, you can use the LLVM IR code to continue with the compilation process, linking, and generating the final executable or library as needed.
87+
88+
Yes, the `opt` utility provided by the [**ADIN LLVM fork**]() also allows you to hook `memmove`, `memcpy`, and `memset` operations in addition to store and load operations. You can enable the hooking of these memory operations using specific options provided by `opt`. Here are the options you can use:
89+
90+
```sh
91+
$ opt --help | grep adin
92+
-adin-alloca-address-skip - Skip intercept address on alloca frame (Stack var)
93+
-adin-check-normal-address-aligment - Checks normal alignment of address attempt
94+
-adin-mem-function-instructions - if equal true - intercept memmove/memcpy/memset function, else skip
95+
-adin-name-callback-load=<string> - Set name callback of load operation. Default __adin_load_
96+
-adin-name-callback-memcpy=<string> - Set name callback of memcpy operation. Default __adin_memcpy_
97+
-adin-name-callback-memmove=<string> - Set name callback of memmove operation. Default __adin_memmove_
98+
-adin-name-callback-memset=<string> - Set name callback of memset operation. Default __adin_memset_
99+
-adin-name-callback-store=<string> - Set name callback of store operation. Default __adin_store_
100+
-adin-simple-global-skip - Skip intercept address of SIMPLE global var
101+
-adin-skip-unsupported-instructions - if equal true - skip this unsupported instruction, else throw error
102+
-adin-verbose-level=<int> - Set Level of verbose for AddressIntercept Pass
103+
104+
```

0 commit comments

Comments
 (0)