Skip to content

Commit 8f9b583

Browse files
SUPERCILEXjamesmth
authored andcommitted
Support LLVM 17
Signed-off-by: Alex Saveau <[email protected]>
1 parent 170baa2 commit 8f9b583

File tree

17 files changed

+120
-83
lines changed

17 files changed

+120
-83
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
2+
resolver = "2"
33
members = [
44
"llvm-plugin",
55
"llvm-plugin-macros",

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Supported versions:
4141
| 14.0.x | llvm14-0 | **&check;** | **&check;** | **&check;** |
4242
| 15.0.x | llvm15-0 | **&check;** | **&check;** | **&check;** |
4343
| 16.0.x | llvm16-0 | **&check;** | **&check;** | **&check;** |
44+
| 17.0.x | llvm17-0 | **&check;** | **&check;** | **&check;** |
4445

4546
## Getting Started
4647

@@ -183,4 +184,4 @@ This [LLVM fork] explains how to do so, and provides LLVM toolchains that will m
183184

184185
Contributions are very welcome, make sure to check out the [Contributing Guide] first!
185186

186-
[Contributing Guide]: ./.github/CONTRIBUTING.md
187+
[Contributing Guide]: ./.github/CONTRIBUTING.md

examples/inject_printf.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,30 @@ impl LlvmModulePass for InjectFuncCallPass {
6767

6868
// create printf args
6969
let func_name = func.get_name().to_str().unwrap();
70-
let func_name_g = builder.build_global_string_ptr(&func_name, "");
70+
let func_name_g = builder.build_global_string_ptr(&func_name, "").unwrap();
7171
let func_argc = cx.i32_type().const_int(func.count_params() as u64, false);
7272

7373
eprintln!(" Injecting call to printf inside {}", func_name);
7474

75-
let format_str_g = builder.build_pointer_cast(
76-
format_str_g.as_pointer_value(),
77-
cx.i8_type().ptr_type(AddressSpace::default()),
78-
"",
79-
);
80-
81-
builder.build_call(
82-
printf,
83-
&[
84-
format_str_g.into(),
85-
func_name_g.as_pointer_value().into(),
86-
func_argc.into(),
87-
],
88-
"",
89-
);
75+
let format_str_g = builder
76+
.build_pointer_cast(
77+
format_str_g.as_pointer_value(),
78+
cx.i8_type().ptr_type(AddressSpace::default()),
79+
"",
80+
)
81+
.unwrap();
82+
83+
builder
84+
.build_call(
85+
printf,
86+
&[
87+
format_str_g.into(),
88+
func_name_g.as_pointer_value().into(),
89+
func_argc.into(),
90+
],
91+
"",
92+
)
93+
.unwrap();
9094

9195
inserted_one_printf = true;
9296
}

examples/strings.rs

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// See https://github.com/tsarpaul/llvm-string-obfuscator
22
// for a more detailed explanation.
33

4-
use either::Either;
5-
64
use llvm_plugin::inkwell::basic_block::BasicBlock;
75
use llvm_plugin::inkwell::module::{Linkage, Module};
86
use llvm_plugin::inkwell::values::{BasicValueEnum, FunctionValue, GlobalValue};
@@ -42,7 +40,7 @@ impl LlvmModulePass for StringObfuscatorModPass {
4240
let cx = module.get_context();
4341
let builder = cx.create_builder();
4442
builder.position_before(&instr);
45-
builder.build_call(decode_stub, &[], "");
43+
builder.build_call(decode_stub, &[], "").unwrap();
4644
};
4745

4846
PreservedAnalyses::None
@@ -81,13 +79,9 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
8179
global_strings.push(GlobalString::Array(global, encoded_str.len() as u32));
8280
}
8381
// Rust-like strings
84-
Some(BasicValueEnum::StructValue(stru)) if stru.get_num_operands() <= 1 => {
85-
let arr = match stru.get_operand(0) {
86-
Some(Either::Left(BasicValueEnum::ArrayValue(arr)))
87-
if arr.is_const_string() =>
88-
{
89-
arr
90-
}
82+
Some(BasicValueEnum::StructValue(stru)) if stru.count_fields() <= 1 => {
83+
let arr = match stru.get_field_at_index(0) {
84+
Some(BasicValueEnum::ArrayValue(arr)) if arr.is_const_string() => arr,
9185
_ => continue,
9286
};
9387
let encoded_str = match arr.get_string_constant() {
@@ -99,7 +93,7 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
9993
None => continue,
10094
};
10195
let new_const = cx.const_string(&encoded_str, false);
102-
stru.set_operand(0, new_const);
96+
stru.set_field_at_index(0, new_const);
10397
global.set_constant(false);
10498
global_strings.push(GlobalString::Struct(global, 0, encoded_str.len() as u32));
10599
}
@@ -129,61 +123,83 @@ fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
129123
let arg1 = decode_fn.get_nth_param(0).unwrap();
130124
let arg2 = decode_fn.get_nth_param(1).unwrap();
131125

132-
let var3 = builder.build_is_not_null(arg1.into_pointer_value(), "");
133-
let var4 = builder.build_int_compare(
134-
IntPredicate::SGT,
135-
arg2.into_int_value(),
136-
cx.i32_type().const_zero(),
137-
"",
138-
);
139-
let var5 = builder.build_and(var4, var3, "");
126+
let var3 = builder
127+
.build_is_not_null(arg1.into_pointer_value(), "")
128+
.unwrap();
129+
let var4 = builder
130+
.build_int_compare(
131+
IntPredicate::SGT,
132+
arg2.into_int_value(),
133+
cx.i32_type().const_zero(),
134+
"",
135+
)
136+
.unwrap();
137+
let var5 = builder.build_and(var4, var3, "").unwrap();
140138

141139
let loop_body_bb = cx.append_basic_block(decode_fn, "");
142140
let end_bb = cx.append_basic_block(decode_fn, "");
143-
builder.build_conditional_branch(var5, loop_body_bb, end_bb);
141+
builder
142+
.build_conditional_branch(var5, loop_body_bb, end_bb)
143+
.unwrap();
144144

145145
builder.position_at_end(loop_body_bb);
146-
let phi1 = builder.build_phi(cx.i8_type().ptr_type(AddressSpace::default()), "");
147-
let phi2 = builder.build_phi(cx.i32_type(), "");
148-
let var9 = builder.build_int_nsw_add(
149-
phi2.as_basic_value().into_int_value(),
150-
cx.i32_type().const_all_ones(),
151-
"",
152-
);
153-
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
146+
let phi1 = builder
147+
.build_phi(cx.i8_type().ptr_type(AddressSpace::default()), "")
148+
.unwrap();
149+
let phi2 = builder.build_phi(cx.i32_type(), "").unwrap();
150+
let var9 = builder
151+
.build_int_nsw_add(
152+
phi2.as_basic_value().into_int_value(),
153+
cx.i32_type().const_all_ones(),
154+
"",
155+
)
156+
.unwrap();
157+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
154158
let var10 = unsafe {
155159
builder.build_gep(
156160
phi1.as_basic_value().into_pointer_value(),
157161
&[cx.i64_type().const_int(1, false)],
158162
"",
159163
)
160-
};
161-
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
164+
}
165+
.unwrap();
166+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
162167
let var10 = unsafe {
163168
builder.build_gep(
164169
cx.i8_type(),
165170
phi1.as_basic_value().into_pointer_value(),
166171
&[cx.i64_type().const_int(1, false)],
167172
"",
168173
)
169-
};
170-
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
174+
}
175+
.unwrap();
176+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
171177
let var11 = builder.build_load(phi1.as_basic_value().into_pointer_value(), "");
172-
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
173-
let var11 = builder.build_load(cx.i8_type(), phi1.as_basic_value().into_pointer_value(), "");
174-
let var12 = builder.build_int_add(var11.into_int_value(), cx.i8_type().const_all_ones(), "");
175-
builder.build_store(phi1.as_basic_value().into_pointer_value(), var12);
176-
let var13 = builder.build_int_compare(
177-
IntPredicate::SGT,
178-
phi2.as_basic_value().into_int_value(),
179-
cx.i32_type().const_int(1, false),
180-
"",
181-
);
182-
183-
builder.build_conditional_branch(var13, loop_body_bb, end_bb);
178+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
179+
let var11 = builder
180+
.build_load(cx.i8_type(), phi1.as_basic_value().into_pointer_value(), "")
181+
.unwrap();
182+
let var12 = builder
183+
.build_int_add(var11.into_int_value(), cx.i8_type().const_all_ones(), "")
184+
.unwrap();
185+
builder
186+
.build_store(phi1.as_basic_value().into_pointer_value(), var12)
187+
.unwrap();
188+
let var13 = builder
189+
.build_int_compare(
190+
IntPredicate::SGT,
191+
phi2.as_basic_value().into_int_value(),
192+
cx.i32_type().const_int(1, false),
193+
"",
194+
)
195+
.unwrap();
196+
197+
builder
198+
.build_conditional_branch(var13, loop_body_bb, end_bb)
199+
.unwrap();
184200

185201
builder.position_at_end(end_bb);
186-
builder.build_return(None);
202+
builder.build_return(None).unwrap();
187203
phi1.add_incoming(&[(&var10, loop_body_bb), (&arg1, start_bb)]);
188204
phi2.add_incoming(&[(&var9, loop_body_bb), (&arg2, start_bb)]);
189205

@@ -206,38 +222,40 @@ fn create_decode_stub<'a>(
206222
for globstr in global_strings {
207223
let (s, len) = match globstr {
208224
GlobalString::Array(gs, len) => {
209-
let s = builder.build_pointer_cast(
210-
gs.as_pointer_value(),
211-
cx.i8_type().ptr_type(AddressSpace::default()),
212-
"",
213-
);
225+
let s = builder
226+
.build_pointer_cast(
227+
gs.as_pointer_value(),
228+
cx.i8_type().ptr_type(AddressSpace::default()),
229+
"",
230+
)
231+
.unwrap();
214232
(s, len)
215233
}
216234
GlobalString::Struct(gs, id, len) => {
217-
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0")))]
235+
#[cfg(not(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0")))]
218236
let s = builder
219237
.build_struct_gep(gs.as_pointer_value(), id, "")
220238
.unwrap();
221-
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0"))]
239+
#[cfg(any(feature = "llvm15-0", feature = "llvm16-0", feature = "llvm17-0"))]
222240
let s = {
223241
let i8_ty_ptr = cx.i8_type().ptr_type(AddressSpace::default());
224242
let struct_ty = cx.struct_type(&[i8_ty_ptr.into()], false);
225243
builder
226244
.build_struct_gep(struct_ty, gs.as_pointer_value(), id, "")
227245
.unwrap()
228246
};
229-
let s = builder.build_pointer_cast(
230-
s,
231-
cx.i8_type().ptr_type(AddressSpace::default()),
232-
"",
233-
);
247+
let s = builder
248+
.build_pointer_cast(s, cx.i8_type().ptr_type(AddressSpace::default()), "")
249+
.unwrap();
234250
(s, len)
235251
}
236252
};
237253
let len = cx.i32_type().const_int(len as u64, false);
238-
builder.build_call(decode_fn, &[s.into(), len.into()], "");
254+
builder
255+
.build_call(decode_fn, &[s.into(), len.into()], "")
256+
.unwrap();
239257
}
240258

241-
builder.build_return(None);
259+
builder.build_return(None).unwrap();
242260
decode_stub
243261
}

llvm-plugin-macros/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use syn::{AttributeArgs, Error};
2323
///
2424
/// # Example
2525
///
26-
/// ```
26+
/// ```ignore
2727
/// # use llvm_plugin::PassBuilder;
2828
/// #[llvm_plugin::plugin(name = "plugin_name", version = "0.1")]
2929
/// fn plugin_registrar(builder: &mut PassBuilder) {

llvm-plugin/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ llvm13-0 = ["inkwell/llvm13-0-no-llvm-linking"]
3131
llvm14-0 = ["inkwell/llvm14-0-no-llvm-linking"]
3232
llvm15-0 = ["inkwell/llvm15-0-no-llvm-linking"]
3333
llvm16-0 = ["inkwell/llvm16-0-no-llvm-linking"]
34+
llvm17-0 = ["inkwell/llvm17-0-no-llvm-linking"]
3435

3536
target-x86 = ["inkwell/target-x86"]
3637
target-arm = ["inkwell/target-arm"]
@@ -51,8 +52,8 @@ target-riscv = ["inkwell/target-riscv"]
5152
target-all = ["inkwell/target-all"]
5253

5354
[dependencies]
54-
inkwell = { package = "llvm-plugin-inkwell", version = "0.3" }
55-
inkwell-internals = { package = "llvm-plugin-inkwell-internals", version = "0.7" }
55+
inkwell = "0.4"
56+
inkwell_internals = "0.9.0"
5657
llvm-plugin-macros = { path = "../llvm-plugin-macros", version = "0.2", optional = true }
5758

5859
[build-dependencies]

llvm-plugin/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ mod llvm_sys {
214214
(15, 0)
215215
} else if cfg!(feature = "llvm16-0") {
216216
(16, 0)
217+
} else if cfg!(feature = "llvm17-0") {
218+
(17, 0)
217219
} else {
218220
panic!("Missing llvm* feature")
219221
}

tests/plugins/plugin1-lld/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ llvm13-0 = ["llvm-plugin/llvm13-0"]
1616
llvm14-0 = ["llvm-plugin/llvm14-0"]
1717
llvm15-0 = ["llvm-plugin/llvm15-0"]
1818
llvm16-0 = ["llvm-plugin/llvm16-0"]
19+
llvm17-0 = ["llvm-plugin/llvm17-0"]
1920

2021
target-x86 = ["llvm-plugin/target-x86"]
2122
target-arm = ["llvm-plugin/target-arm"]

tests/plugins/plugin1/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ llvm13-0 = ["llvm-plugin/llvm13-0"]
1717
llvm14-0 = ["llvm-plugin/llvm14-0"]
1818
llvm15-0 = ["llvm-plugin/llvm15-0"]
1919
llvm16-0 = ["llvm-plugin/llvm16-0"]
20+
llvm17-0 = ["llvm-plugin/llvm17-0"]
2021

2122
target-x86 = ["llvm-plugin/target-x86"]
2223
target-arm = ["llvm-plugin/target-arm"]

tests/plugins/plugin2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ llvm13-0 = ["llvm-plugin/llvm13-0"]
1717
llvm14-0 = ["llvm-plugin/llvm14-0"]
1818
llvm15-0 = ["llvm-plugin/llvm15-0"]
1919
llvm16-0 = ["llvm-plugin/llvm16-0"]
20+
llvm17-0 = ["llvm-plugin/llvm17-0"]
2021

2122
target-x86 = ["llvm-plugin/target-x86"]
2223
target-arm = ["llvm-plugin/target-arm"]

0 commit comments

Comments
 (0)