Skip to content

Commit 0f8d2d6

Browse files
All tests passing on Windows. (#608)
* test_default_triple fix * All tests passing on Windows. * Formatting. * Formatting. * Formatting. * Forgot to configure windows dep for windows. * I think it should work now. * I don't know what I'm doing. * Hopefully this satisfies clippy. * I could have sworn that I just committed this.
1 parent 075db5c commit 0f8d2d6

5 files changed

Lines changed: 111 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,12 @@ serde = { version = "1.0", default-features = false, features = [
170170
gumdrop = "0.8.1"
171171
regex = "1"
172172

173+
[target.'cfg(windows)'.dev-dependencies.windows]
174+
version = "0.62.2"
175+
features = [
176+
"Win32_System_SystemInformation",
177+
"Win32_System_Memory"
178+
]
179+
173180
[badges]
174181
codecov = { repository = "TheDan64/inkwell" }

tests/all/test_execution_engine.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ use inkwell::module::Linkage;
88
use inkwell::targets::{CodeModel, InitializationConfig, Target};
99
use inkwell::{AddressSpace, IntPredicate, OptimizationLevel};
1010

11+
#[cfg(target_os = "windows")]
12+
use windows::Win32::System::{
13+
Memory::{
14+
VirtualAlloc, VirtualFree, VirtualProtect, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_EXECUTE_READ,
15+
PAGE_READWRITE,
16+
},
17+
SystemInformation::{GetSystemInfo, SYSTEM_INFO},
18+
};
19+
1120
type Thunk = unsafe extern "C" fn();
1221

1322
#[test]
@@ -374,8 +383,20 @@ struct MockMemoryManagerData {
374383
impl MockMemoryManager {
375384
pub fn new() -> Self {
376385
let capacity_bytes = 128 * 1024;
386+
#[cfg(unix)]
377387
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize };
378388

389+
#[cfg(target_os = "windows")]
390+
let page_size = {
391+
let mut info = std::mem::MaybeUninit::<SYSTEM_INFO>::uninit();
392+
unsafe {
393+
GetSystemInfo(info.as_mut_ptr());
394+
}
395+
let info = unsafe { info.assume_init() };
396+
info.dwPageSize as usize
397+
};
398+
399+
#[cfg(unix)]
379400
let code_buff_ptr = unsafe {
380401
std::ptr::NonNull::new_unchecked(libc::mmap(
381402
std::ptr::null_mut(),
@@ -387,6 +408,14 @@ impl MockMemoryManager {
387408
) as *mut u8)
388409
};
389410

411+
#[cfg(target_os = "windows")]
412+
let code_buff_ptr = unsafe {
413+
std::ptr::NonNull::new_unchecked(
414+
VirtualAlloc(None, capacity_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE).cast::<u8>(),
415+
)
416+
};
417+
418+
#[cfg(unix)]
390419
let data_buff_ptr = unsafe {
391420
std::ptr::NonNull::new_unchecked(libc::mmap(
392421
std::ptr::null_mut(),
@@ -398,6 +427,13 @@ impl MockMemoryManager {
398427
) as *mut u8)
399428
};
400429

430+
#[cfg(target_os = "windows")]
431+
let data_buff_ptr = unsafe {
432+
std::ptr::NonNull::new_unchecked(
433+
VirtualAlloc(None, capacity_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE).cast::<u8>(),
434+
)
435+
};
436+
401437
Self {
402438
data: Rc::new(RefCell::new(MockMemoryManagerData {
403439
fixed_capacity_bytes: capacity_bytes,
@@ -460,6 +496,7 @@ impl McjitMemoryManager for MockMemoryManager {
460496

461497
data.finalize_calls += 1;
462498

499+
#[cfg(unix)]
463500
unsafe {
464501
libc::mprotect(
465502
data.code_buff_ptr.as_ptr() as *mut libc::c_void,
@@ -473,6 +510,25 @@ impl McjitMemoryManager for MockMemoryManager {
473510
);
474511
}
475512

513+
#[cfg(windows)]
514+
unsafe {
515+
let mut old_protect = PAGE_READWRITE;
516+
VirtualProtect(
517+
data.code_buff_ptr.as_ptr() as *mut _,
518+
data.fixed_capacity_bytes,
519+
PAGE_EXECUTE_READ,
520+
&mut old_protect,
521+
)
522+
.expect("VirtualProtect failed");
523+
VirtualProtect(
524+
data.data_buff_ptr.as_ptr() as *mut _,
525+
data.fixed_capacity_bytes,
526+
PAGE_READWRITE,
527+
&mut old_protect,
528+
)
529+
.expect("VirtualProtect failed");
530+
}
531+
476532
Ok(())
477533
}
478534

@@ -481,6 +537,7 @@ impl McjitMemoryManager for MockMemoryManager {
481537

482538
data.destroy_calls += 1;
483539

540+
#[cfg(unix)]
484541
unsafe {
485542
libc::munmap(
486543
data.code_buff_ptr.as_ptr() as *mut libc::c_void,
@@ -491,5 +548,11 @@ impl McjitMemoryManager for MockMemoryManager {
491548
data.fixed_capacity_bytes,
492549
);
493550
}
551+
552+
#[cfg(windows)]
553+
unsafe {
554+
VirtualFree(data.code_buff_ptr.as_ptr() as *mut _, 0, MEM_RELEASE).expect("Failed to free memory.");
555+
VirtualFree(data.data_buff_ptr.as_ptr() as *mut _, 0, MEM_RELEASE).expect("Failed to free memory.");
556+
}
494557
}
495558
}

tests/all/test_module.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn test_clone() {
305305
}
306306

307307
#[test]
308-
fn test_print_to_file() {
308+
fn test_print_to_file_good_path() {
309309
let context = Context::create();
310310
let module = context.create_module("mod");
311311
let void_type = context.void_type();
@@ -317,20 +317,38 @@ fn test_print_to_file() {
317317
builder.position_at_end(basic_block);
318318
builder.build_return(None).unwrap();
319319

320-
let bad_path = Path::new("/tmp/some/silly/path/that/sure/doesn't/exist");
321-
322-
assert_eq!(
323-
module.print_to_file(bad_path).unwrap_err().to_str(),
324-
Ok("No such file or directory")
325-
);
326-
327320
let mut temp_path = temp_dir();
328321

329322
temp_path.push("module");
330323

331324
assert!(module.print_to_file(&temp_path).is_ok());
332325
}
333326

327+
#[test]
328+
fn test_print_to_file_bad_path() {
329+
let context = Context::create();
330+
let module = context.create_module("mod");
331+
let void_type = context.void_type();
332+
let fn_type = void_type.fn_type(&[], false);
333+
let f = module.add_function("f", fn_type, None);
334+
let basic_block = context.append_basic_block(f, "entry");
335+
let builder = context.create_builder();
336+
337+
builder.position_at_end(basic_block);
338+
builder.build_return(None).unwrap();
339+
340+
#[cfg(unix)]
341+
let bad_path = Path::new("/tmp/some/silly/path/that/sure/doesn't/exist");
342+
#[cfg(windows)]
343+
let bad_path = Path::new("/does/not/exist/hopefully");
344+
345+
match module.print_to_file(bad_path).unwrap_err().to_str() {
346+
Ok("no such file or directory") | Ok("No such file or directory") => (),
347+
Ok(err) => panic!("Some other error: {err}"),
348+
Err(_) => panic!("Should have failed."),
349+
}
350+
}
351+
334352
#[test]
335353
fn test_get_set_target() {
336354
Target::initialize_x86(&Default::default());

tests/all/test_object_file.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ fn test_symbol_iterator() {
154154
"a" => {
155155
assert!(!has_symbol_a);
156156
has_symbol_a = true;
157+
#[cfg(unix)]
157158
assert_eq!(symbol.size(), 1);
159+
#[cfg(windows)]
160+
assert_eq!(symbol.size(), 0);
158161
},
159162
"b" => {
160163
assert!(!has_symbol_b);

tests/all/test_targets.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,25 @@ fn test_default_triple() {
186186
vec!["pc", "unknown", "redhat"]
187187
} else if cfg!(target_os = "macos") {
188188
vec!["apple"]
189+
} else if cfg!(target_os = "windows") {
190+
vec!["pc", "unknown", "uwp"]
189191
} else {
190192
vec![]
191193
};
192194

193195
let has_known_vendor = vendors.iter().any(|vendor| default_triple.contains(*vendor));
194196
assert!(has_known_vendor, "Target triple '{default_triple}' has unknown vendor");
195197

196-
let os = [
197-
#[cfg(target_os = "linux")]
198-
"linux",
199-
#[cfg(target_os = "macos")]
200-
"darwin",
201-
];
202-
let has_known_os = os.iter().any(|os| default_triple.contains(*os));
198+
let has_known_os = if cfg!(target_os = "linux") {
199+
default_triple.contains("linux")
200+
} else if cfg!(target_os = "macos") {
201+
default_triple.contains("macos")
202+
} else if cfg!(target_os = "windows") {
203+
default_triple.contains("windows")
204+
} else {
205+
false
206+
};
207+
203208
assert!(has_known_os, "Target triple '{default_triple}' has unknown OS");
204209

205210
// TODO: CFG for other supported major OSes

0 commit comments

Comments
 (0)