inkwell 0.9.0

Inkwell aims to help you pen your own programming languages by safely wrapping llvm-sys.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use std::cell::RefCell;
use std::rc::Rc;

use inkwell::context::Context;
use inkwell::execution_engine::FunctionLookupError;
use inkwell::memory_manager::McjitMemoryManager;
use inkwell::module::Linkage;
use inkwell::targets::{CodeModel, InitializationConfig, Target};
use inkwell::{AddressSpace, IntPredicate, OptimizationLevel};

#[cfg(target_os = "windows")]
use windows::Win32::System::{
    Memory::{
        MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READ, PAGE_READWRITE, VirtualAlloc, VirtualFree,
        VirtualProtect,
    },
    SystemInformation::{GetSystemInfo, SYSTEM_INFO},
};

type Thunk = unsafe extern "C" fn();

#[test]
fn test_get_function_address() {
    let context = Context::create();
    // let module = context.create_module("errors_abound");
    let builder = context.create_builder();
    let void_type = context.void_type();
    let fn_type = void_type.fn_type(&[], false);

    // FIXME: LLVM's global state is leaking, causing this to fail in `cargo test` but not `cargo test test_get_function_address`
    // nor (most of the time) with `cargo test -- --test-threads LARGE_NUM`
    // assert_eq!(module.create_jit_execution_engine(OptimizationLevel::None), Err("Unable to find target for this triple (no targets are registered)".into()));
    let module = context.create_module("errors_abound");

    Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

    let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

    unsafe {
        assert_eq!(
            execution_engine.get_function::<Thunk>("errors").unwrap_err(),
            FunctionLookupError::FunctionNotFound
        );
    }

    let module = context.create_module("errors_abound");
    let fn_value = module.add_function("func", fn_type, None);
    let basic_block = context.append_basic_block(fn_value, "entry");

    builder.position_at_end(basic_block);
    builder.build_return(None).unwrap();

    let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

    unsafe {
        assert_eq!(
            execution_engine.get_function::<Thunk>("errors").unwrap_err(),
            FunctionLookupError::FunctionNotFound
        );

        assert!(execution_engine.get_function::<Thunk>("func").is_ok());
    }
}

#[test]
fn test_jit_execution_engine() {
    let context = Context::create();
    let module = context.create_module("main_module");
    let builder = context.create_builder();
    let i32_type = context.i32_type();
    #[cfg(feature = "typed-pointers")]
    let i8_ptr_ptr_type = context
        .i8_type()
        .ptr_type(AddressSpace::default())
        .ptr_type(AddressSpace::default());
    #[cfg(not(feature = "typed-pointers"))]
    let i8_ptr_ptr_type = context.ptr_type(AddressSpace::default());
    let one_i32 = i32_type.const_int(1, false);
    let three_i32 = i32_type.const_int(3, false);
    let fourtytwo_i32 = i32_type.const_int(42, false);
    let fn_type = i32_type.fn_type(&[i32_type.into(), i8_ptr_ptr_type.into()], false);
    let fn_value = module.add_function("main", fn_type, None);
    let main_argc = fn_value.get_first_param().unwrap().into_int_value();
    // let main_argv = fn_value.get_nth_param(1).unwrap();
    let check_argc = context.append_basic_block(fn_value, "check_argc");
    let check_arg3 = context.append_basic_block(fn_value, "check_arg3");
    let error1 = context.append_basic_block(fn_value, "error1");
    let success = context.append_basic_block(fn_value, "success");

    main_argc.set_name("argc");

    // If anything goes wrong, jump to returning 1
    builder.position_at_end(error1);
    builder.build_return(Some(&one_i32)).unwrap();

    // If successful, jump to returning 42
    builder.position_at_end(success);
    builder.build_return(Some(&fourtytwo_i32)).unwrap();

    // See if argc == 3
    builder.position_at_end(check_argc);

    let eq = IntPredicate::EQ;
    let argc_check = builder.build_int_compare(eq, main_argc, three_i32, "argc_cmp").unwrap();

    builder
        .build_conditional_branch(argc_check, check_arg3, error1)
        .unwrap();

    builder.position_at_end(check_arg3);
    builder.build_unconditional_branch(success).unwrap();

    Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

    let execution_engine = module
        .create_jit_execution_engine(OptimizationLevel::None)
        .expect("Could not create Execution Engine");

    let main = execution_engine
        .get_function_value("main")
        .expect("Could not find main in ExecutionEngine");

    let ret = unsafe { execution_engine.run_function_as_main(main, &["input", "bar"]) };

    assert_eq!(ret, 1, "unexpected main return code: {ret}");

    let ret = unsafe { execution_engine.run_function_as_main(main, &["input", "bar", "baz"]) };

    assert_eq!(ret, 42, "unexpected main return code: {ret}");
}

// #[test]
// fn test_execution_engine_empty_module() {
//     let context = Context::create();
//     let module = context.create_module("fooo");
//     let builder = context.create_builder();

//     let ee = module.create_jit_execution_engine(OptimizationLevel::None); // Segfault?
// }

#[test]
fn test_execution_engine() {
    let context = Context::create();
    let module = context.create_module("main_module");

    assert!(module.create_execution_engine().is_ok());
}

#[test]
fn test_interpreter_execution_engine() {
    let context = Context::create();
    let module = context.create_module("main_module");

    assert!(module.create_interpreter_execution_engine().is_ok());
}

#[test]

fn test_mcjit_execution_engine_with_memory_manager() {
    let mmgr = MockMemoryManager::new();
    let mmgr_for_test = mmgr.clone();

    {
        let context = Context::create();
        let module = context.create_module("main_module");
        let builder = context.create_builder();

        // Define @llvm.experimental.stackmap
        let fn_type = context
            .void_type()
            .fn_type(&[context.i64_type().into(), context.i32_type().into()], true);
        let stackmap_func = module.add_function("llvm.experimental.stackmap", fn_type, Some(Linkage::External));

        // Set up the function
        //
        // `@llvm.experimental.stackmap` a call is present, LLVM will emit a separate stackmap section,
        // causing the `allocate_data_section` callback to be invoked an additional
        // time to handle the stackmap data. Specifically:
        // ```
        // f64 test_fn() {
        // entry:
        //   call void @llvm.experimental.stackmap(i64 12345, i32 0)
        //   ret f64 64.0
        // }
        // ```
        let double = context.f64_type();
        let sig = double.fn_type(&[], false);
        let f = module.add_function("test_fn", sig, None);
        let b = context.append_basic_block(f, "entry");
        builder.position_at_end(b);

        // Create a call to the stackmap intrinsic
        // Stack maps are used by the garbage collector to find roots on the stack
        // See: https://llvm.org/docs/StackMaps.html#intrinsics
        builder
            .build_call(
                stackmap_func,
                &[
                    context.i64_type().const_int(12345, false).into(),
                    context.i32_type().const_int(0, false).into(),
                ],
                "call_stackmap",
            )
            .unwrap();

        // Insert a return statement
        let ret = double.const_float(64.0);
        builder.build_return(Some(&ret)).unwrap();

        module.verify().unwrap();

        let ee = module
            .create_mcjit_execution_engine_with_memory_manager(
                mmgr,
                OptimizationLevel::None,
                CodeModel::Default,
                false,
                false,
            )
            .unwrap();

        unsafe {
            let test_fn = ee.get_function::<unsafe extern "C" fn() -> f64>("test_fn").unwrap();
            let return_value = test_fn.call();
            assert_eq!(return_value, 64.0);
        }
    }
    // ee dropped here. Destroy should be called

    let data = mmgr_for_test.data.borrow();
    assert_eq!(1, data.code_alloc_calls);
    assert!(
        data.data_alloc_calls >= 2,
        "Expected at least 2 calls to allocate_data_section, but got {}. \
         We've observed that LLVM 5 typically calls it 3 times, while LLVM 18 often calls it only 2.",
        data.data_alloc_calls
    );
    assert_eq!(1, data.finalize_calls);
    assert_eq!(1, data.destroy_calls);
}

#[test]
fn test_create_mcjit_engine_when_already_owned() {
    let context = Context::create();
    let module = context.create_module("owned_module");

    // First engine should succeed
    let memory_manager = MockMemoryManager::new();
    let engine_result = module.create_mcjit_execution_engine_with_memory_manager(
        memory_manager,
        OptimizationLevel::None,
        CodeModel::Default,
        false,
        false,
    );
    assert!(engine_result.is_ok());

    // Second engine should fail
    let memory_manager2 = MockMemoryManager::new();
    let second_result = module.create_mcjit_execution_engine_with_memory_manager(
        memory_manager2,
        OptimizationLevel::None,
        CodeModel::Default,
        false,
        false,
    );
    assert!(
        second_result.is_err(),
        "Expected an error when creating a second ExecutionEngine on the same module"
    );
}

#[test]
fn test_add_remove_module() {
    let context = Context::create();
    let module = context.create_module("test");
    let ee = module
        .create_jit_execution_engine(OptimizationLevel::default())
        .unwrap();

    assert!(ee.add_module(&module).is_err());

    let module2 = context.create_module("mod2");

    assert!(ee.remove_module(&module2).is_err());
    assert!(ee.add_module(&module2).is_ok());
    assert!(ee.remove_module(&module).is_ok());
    assert!(ee.remove_module(&module2).is_ok());
}

// REVIEW: Global state pollution access tests cause this to pass when run individually
// but fail when multiple tests are run
// #[test]
// fn test_no_longer_segfaults() {
//     Target::initialize_amd_gpu(&InitializationConfig::default());

//     let context = Context::create();
//     let module = context.create_module("test");

//     assert_eq!(*module.create_jit_execution_engine(OptimizationLevel::default()).unwrap_err(), *CString::new("No available targets are compatible with this triple.").unwrap());

//     // REVIEW: Module is being cloned in err case... should test Module still works as expected...
// }

// #[test]
// fn test_get_function_value() {
//     let context = Context::create();
//     let builder = context.create_builder();
//     let module = context.create_module("errors_abound");
//     // let mut execution_engine = ExecutionEngine::create_jit_from_module(module, 0);
//     let mut execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();
//     let void_type = context.void_type();
//     let fn_type = void_type.fn_type(&[], false);
//     let fn_value = module.add_function("func", fn_type, None);
//     let basic_block = context.append_basic_block(&fn_value, "entry");

//     builder.position_at_end(basic_block);
//     builder.build_return(None);

//     assert_eq!(execution_engine.get_function_value("errors"), Err(FunctionLookupError::JITNotEnabled));

//     // Regain ownership of module
//     let module = execution_engine.remove_module(&module).unwrap();

//     Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

//     let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

//     assert_eq!(execution_engine.get_function_value("errors"), Err(FunctionLookupError::FunctionNotFound));

//     assert!(execution_engine.get_function_value("func").is_ok());
// }

// No longer necessary with explicit lifetimes
// #[test]
// fn test_previous_double_free() {
//     Target::initialize_native(&InitializationConfig::default()).unwrap();

//     let context = Context::create();
//     let module = context.create_module("sum");
//     let _ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap();

//     drop(context);
//     drop(module);
// }

// #[test]
// fn test_previous_double_free2() {
//     Target::initialize_native(&InitializationConfig::default()).unwrap();

//     let _execution_engine = {
//         let context = Context::create();
//         let module = context.create_module("sum");

//         module.create_jit_execution_engine(OptimizationLevel::None).unwrap()
//     };
// }

/// A mock memory manager that allocates memory in fixed-size pages for testing.
#[derive(Debug, Clone)]
struct MockMemoryManager {
    data: Rc<RefCell<MockMemoryManagerData>>,
}

#[derive(Debug)]
struct MockMemoryManagerData {
    fixed_capacity_bytes: usize,
    fixed_page_size: usize,

    code_buff_ptr: std::ptr::NonNull<u8>,
    code_offset: usize,

    data_buff_ptr: std::ptr::NonNull<u8>,
    data_offset: usize,

    /// Count call to callbacks for testing
    code_alloc_calls: usize,
    data_alloc_calls: usize,
    finalize_calls: usize,
    destroy_calls: usize,
}

impl MockMemoryManager {
    pub fn new() -> Self {
        let capacity_bytes = 128 * 1024;
        #[cfg(unix)]
        let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize };

        #[cfg(target_os = "windows")]
        let page_size = {
            let mut info = std::mem::MaybeUninit::<SYSTEM_INFO>::uninit();
            unsafe {
                GetSystemInfo(info.as_mut_ptr());
            }
            let info = unsafe { info.assume_init() };
            info.dwPageSize as usize
        };

        #[cfg(unix)]
        let code_buff_ptr = unsafe {
            std::ptr::NonNull::new_unchecked(libc::mmap(
                std::ptr::null_mut(),
                capacity_bytes,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
                -1,
                0,
            ) as *mut u8)
        };

        #[cfg(target_os = "windows")]
        let code_buff_ptr = unsafe {
            std::ptr::NonNull::new_unchecked(
                VirtualAlloc(None, capacity_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE).cast::<u8>(),
            )
        };

        #[cfg(unix)]
        let data_buff_ptr = unsafe {
            std::ptr::NonNull::new_unchecked(libc::mmap(
                std::ptr::null_mut(),
                capacity_bytes,
                libc::PROT_READ | libc::PROT_WRITE,
                libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
                -1,
                0,
            ) as *mut u8)
        };

        #[cfg(target_os = "windows")]
        let data_buff_ptr = unsafe {
            std::ptr::NonNull::new_unchecked(
                VirtualAlloc(None, capacity_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE).cast::<u8>(),
            )
        };

        Self {
            data: Rc::new(RefCell::new(MockMemoryManagerData {
                fixed_capacity_bytes: capacity_bytes,
                fixed_page_size: page_size,

                code_buff_ptr,
                code_offset: 0,

                data_buff_ptr,
                data_offset: 0,

                code_alloc_calls: 0,
                data_alloc_calls: 0,
                finalize_calls: 0,
                destroy_calls: 0,
            })),
        }
    }
}

impl McjitMemoryManager for MockMemoryManager {
    fn allocate_code_section(
        &mut self,
        size: libc::size_t,
        _alignment: libc::c_uint,
        _section_id: libc::c_uint,
        _section_name: &str,
    ) -> *mut u8 {
        let mut data = self.data.borrow_mut();
        data.code_alloc_calls += 1;

        let alloc_size = size.div_ceil(data.fixed_page_size) * data.fixed_page_size;
        let ptr = unsafe { data.code_buff_ptr.as_ptr().add(data.code_offset) };
        data.code_offset += alloc_size;

        ptr
    }

    fn allocate_data_section(
        &mut self,
        size: libc::size_t,
        _alignment: libc::c_uint,
        _section_id: libc::c_uint,
        _section_name: &str,
        _is_read_only: bool,
    ) -> *mut u8 {
        let mut data = self.data.borrow_mut();

        data.data_alloc_calls += 1;

        let alloc_size = size.div_ceil(data.fixed_page_size) * data.fixed_page_size;
        let ptr = unsafe { data.data_buff_ptr.as_ptr().add(data.data_offset) };
        data.data_offset += alloc_size;

        ptr
    }

    fn finalize_memory(&mut self) -> Result<(), String> {
        let mut data = self.data.borrow_mut();

        data.finalize_calls += 1;

        #[cfg(unix)]
        unsafe {
            libc::mprotect(
                data.code_buff_ptr.as_ptr() as *mut libc::c_void,
                data.fixed_capacity_bytes,
                libc::PROT_READ | libc::PROT_EXEC,
            );
            libc::mprotect(
                data.data_buff_ptr.as_ptr() as *mut libc::c_void,
                data.fixed_capacity_bytes,
                libc::PROT_READ | libc::PROT_WRITE,
            );
        }

        #[cfg(windows)]
        unsafe {
            let mut old_protect = PAGE_READWRITE;
            VirtualProtect(
                data.code_buff_ptr.as_ptr() as *mut _,
                data.fixed_capacity_bytes,
                PAGE_EXECUTE_READ,
                &mut old_protect,
            )
            .expect("VirtualProtect failed");
            VirtualProtect(
                data.data_buff_ptr.as_ptr() as *mut _,
                data.fixed_capacity_bytes,
                PAGE_READWRITE,
                &mut old_protect,
            )
            .expect("VirtualProtect failed");
        }

        Ok(())
    }

    fn destroy(&mut self) {
        let mut data = self.data.borrow_mut();

        data.destroy_calls += 1;

        #[cfg(unix)]
        unsafe {
            libc::munmap(
                data.code_buff_ptr.as_ptr() as *mut libc::c_void,
                data.fixed_capacity_bytes,
            );
            libc::munmap(
                data.data_buff_ptr.as_ptr() as *mut libc::c_void,
                data.fixed_capacity_bytes,
            );
        }

        #[cfg(windows)]
        unsafe {
            VirtualFree(data.code_buff_ptr.as_ptr() as *mut _, 0, MEM_RELEASE).expect("Failed to free memory.");
            VirtualFree(data.data_buff_ptr.as_ptr() as *mut _, 0, MEM_RELEASE).expect("Failed to free memory.");
        }
    }
}