Skip to content

Commit 651c330

Browse files
committed
refactor(examples): remove deprecated inkwell api
1 parent 1a897d8 commit 651c330

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

examples/inject_printf.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,29 @@ use llvm_plugin::{
88
LlvmModulePass, ModuleAnalysisManager, PassBuilder, PipelineParsing, PreservedAnalyses,
99
};
1010

11+
#[cfg(not(any(
12+
feature = "llvm15-0",
13+
feature = "llvm16-0",
14+
feature = "llvm17-0",
15+
feature = "llvm18-1",
16+
)))]
17+
macro_rules! ptr_type {
18+
($cx:ident, $ty:ident) => {
19+
$cx.$ty().ptr_type(AddressSpace::default())
20+
};
21+
}
22+
#[cfg(any(
23+
feature = "llvm15-0",
24+
feature = "llvm16-0",
25+
feature = "llvm17-0",
26+
feature = "llvm18-1",
27+
))]
28+
macro_rules! ptr_type {
29+
($cx:ident, $ty:ident) => {
30+
$cx.ptr_type(AddressSpace::default())
31+
};
32+
}
33+
1134
#[llvm_plugin::plugin(name = "inject-func-call", version = "0.1")]
1235
fn plugin_registrar(builder: &mut PassBuilder) {
1336
builder.add_module_pipeline_parsing_callback(|name, pass_manager| {
@@ -29,7 +52,7 @@ impl LlvmModulePass for InjectFuncCallPass {
2952
Some(func) => func,
3053
None => {
3154
// create type `int32 printf(int8*, ...)`
32-
let arg_ty = cx.i8_type().ptr_type(AddressSpace::default());
55+
let arg_ty = ptr_type!(cx, i8_type);
3356
let func_ty = cx.i32_type().fn_type(&[arg_ty.into()], true);
3457
module.add_function("printf", func_ty, None)
3558
}
@@ -73,11 +96,7 @@ impl LlvmModulePass for InjectFuncCallPass {
7396
eprintln!(" Injecting call to printf inside {}", func_name);
7497

7598
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-
)
99+
.build_pointer_cast(format_str_g.as_pointer_value(), ptr_type!(cx, i8_type), "")
81100
.unwrap();
82101

83102
builder

examples/strings.rs

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

4+
use inkwell::values::{ArrayValue, AsValueRef};
45
use llvm_plugin::inkwell::basic_block::BasicBlock;
56
use llvm_plugin::inkwell::module::{Linkage, Module};
67
use llvm_plugin::inkwell::values::{BasicValueEnum, FunctionValue, GlobalValue};
@@ -9,6 +10,29 @@ use llvm_plugin::{
910
LlvmModulePass, ModuleAnalysisManager, PassBuilder, PipelineParsing, PreservedAnalyses,
1011
};
1112

13+
#[cfg(not(any(
14+
feature = "llvm15-0",
15+
feature = "llvm16-0",
16+
feature = "llvm17-0",
17+
feature = "llvm18-1",
18+
)))]
19+
macro_rules! ptr_type {
20+
($cx:ident, $ty:ident) => {
21+
$cx.$ty().ptr_type(AddressSpace::default())
22+
};
23+
}
24+
#[cfg(any(
25+
feature = "llvm15-0",
26+
feature = "llvm16-0",
27+
feature = "llvm17-0",
28+
feature = "llvm18-1",
29+
))]
30+
macro_rules! ptr_type {
31+
($cx:ident, $ty:ident) => {
32+
$cx.ptr_type(AddressSpace::default())
33+
};
34+
}
35+
1236
#[llvm_plugin::plugin(name = "StringObfuscatorPass", version = "v0.1")]
1337
fn plugin_registrar(builder: &mut PassBuilder) {
1438
builder.add_module_pipeline_parsing_callback(|name, pass_manager| {
@@ -71,12 +95,12 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
7195
_ => None,
7296
})
7397
.filter(|(_, _, arr)| {
74-
// needs to be called before `get_string_constant`, otherwise it may crash
98+
// needs to be called before `array_as_const_string`, otherwise it may crash
7599
arr.is_const_string()
76100
})
77101
.filter_map(|(global, stru, arr)| {
78102
// we ignore non-UTF8 strings, since they are probably not human-readable
79-
let s = arr.get_string_constant().and_then(|s| s.to_str().ok())?;
103+
let s = array_as_const_string(&arr).and_then(|s| str::from_utf8(s).ok())?;
80104
let encoded_str = s.bytes().map(|c| c + 1).collect::<Vec<_>>();
81105
Some((global, stru, encoded_str))
82106
})
@@ -101,11 +125,22 @@ fn encode_global_strings<'a>(module: &mut Module<'a>) -> Vec<GlobalString<'a>> {
101125
.collect()
102126
}
103127

128+
pub fn array_as_const_string<'a>(arr: &'a ArrayValue) -> Option<&'a [u8]> {
129+
let mut len = 0;
130+
let ptr = unsafe { inkwell::llvm_sys::core::LLVMGetAsString(arr.as_value_ref(), &mut len) };
131+
132+
if ptr.is_null() {
133+
None
134+
} else {
135+
unsafe { Some(std::slice::from_raw_parts(ptr.cast(), len)) }
136+
}
137+
}
138+
104139
fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
105140
let cx = module.get_context();
106141

107142
// create type `void decode(int8*, int32)`
108-
let arg1_ty = cx.i8_type().ptr_type(AddressSpace::default());
143+
let arg1_ty = ptr_type!(cx, i8_type);
109144
let arg2_ty = cx.i32_type();
110145
let fn_ty = cx
111146
.void_type()
@@ -140,9 +175,7 @@ fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
140175
.unwrap();
141176

142177
builder.position_at_end(loop_body_bb);
143-
let phi1 = builder
144-
.build_phi(cx.i8_type().ptr_type(AddressSpace::default()), "")
145-
.unwrap();
178+
let phi1 = builder.build_phi(ptr_type!(cx, i8_type), "").unwrap();
146179
let phi2 = builder.build_phi(cx.i32_type(), "").unwrap();
147180
let var9 = builder
148181
.build_int_nsw_add(
@@ -186,7 +219,9 @@ fn create_decode_fn<'a>(module: &mut Module<'a>) -> FunctionValue<'a> {
186219
feature = "llvm17-0",
187220
feature = "llvm18-1",
188221
)))]
189-
let var11 = builder.build_load(phi1.as_basic_value().into_pointer_value(), "");
222+
let var11 = builder
223+
.build_load(phi1.as_basic_value().into_pointer_value(), "")
224+
.unwrap();
190225
#[cfg(any(
191226
feature = "llvm15-0",
192227
feature = "llvm16-0",
@@ -240,11 +275,7 @@ fn create_decode_stub<'a>(
240275
let (s, len) = match globstr {
241276
GlobalString::Array(gs, len) => {
242277
let s = builder
243-
.build_pointer_cast(
244-
gs.as_pointer_value(),
245-
cx.i8_type().ptr_type(AddressSpace::default()),
246-
"",
247-
)
278+
.build_pointer_cast(gs.as_pointer_value(), ptr_type!(cx, i8_type), "")
248279
.unwrap();
249280
(s, len)
250281
}
@@ -265,14 +296,14 @@ fn create_decode_stub<'a>(
265296
feature = "llvm18-1",
266297
))]
267298
let s = {
268-
let i8_ty_ptr = cx.i8_type().ptr_type(AddressSpace::default());
299+
let i8_ty_ptr = ptr_type!(cx, i8_type);
269300
let struct_ty = cx.struct_type(&[i8_ty_ptr.into()], false);
270301
builder
271302
.build_struct_gep(struct_ty, gs.as_pointer_value(), id, "")
272303
.unwrap()
273304
};
274305
let s = builder
275-
.build_pointer_cast(s, cx.i8_type().ptr_type(AddressSpace::default()), "")
306+
.build_pointer_cast(s, ptr_type!(cx, i8_type), "")
276307
.unwrap();
277308
(s, len)
278309
}

0 commit comments

Comments
 (0)