From: Bennet Bleßmann Date: Mon, 27 Jan 2025 20:55:34 +0000 (+0100) Subject: fix UB in ffi tests X-Git-Tag: v0.10.0~29^2~12 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=760e1d2aac9273f6903454541428890b0cecd870;p=scryer-prolog.git fix UB in ffi tests --- diff --git a/tests-pl/ffi_f64_minus_zero.pl b/tests-pl/ffi_f64_minus_zero.pl index 974b6e92..6ca2b4b3 100644 --- a/tests-pl/ffi_f64_minus_zero.pl +++ b/tests-pl/ffi_f64_minus_zero.pl @@ -2,7 +2,9 @@ :- use_module(library(ffi)). test :- - getenv("ffi_f64_minus_zero_LIB", LIB), + read(Body), + term_variables(Body, [LIB]), + Body, use_foreign_module(LIB, ['ffi_f64_minus_zero'([], f64), 'signum'([f64], f64)]), ffi:'ffi_f64_minus_zero'(N), A is max(0.0, N), diff --git a/tests-pl/ffi_f64_nan.pl b/tests-pl/ffi_f64_nan.pl index a80dbd90..42dcd587 100644 --- a/tests-pl/ffi_f64_nan.pl +++ b/tests-pl/ffi_f64_nan.pl @@ -2,7 +2,9 @@ :- use_module(library(ffi)). test :- - getenv("ffi_f64_nan_LIB", LIB), + read(Body), + term_variables(Body, [LIB]), + Body, use_foreign_module(LIB, ['ffi_f64_nan'([], f64)]), ffi:'ffi_f64_nan'(N), _ is round(N). diff --git a/tests-pl/ffi_invalid_type.pl b/tests-pl/ffi_invalid_type.pl index c78668df..6e687240 100644 --- a/tests-pl/ffi_invalid_type.pl +++ b/tests-pl/ffi_invalid_type.pl @@ -2,7 +2,9 @@ :- use_module(library(ffi)). test :- - getenv("ffi_invalid_type_LIB", LIB), + read(Body), + term_variables(Body, [LIB]), + Body, use_foreign_module(LIB, [ 'ffi_invalid_type'([], c_void) ]). diff --git a/tests-pl/ffi_return_values.pl b/tests-pl/ffi_return_values.pl index 3365262c..ee161259 100644 --- a/tests-pl/ffi_return_values.pl +++ b/tests-pl/ffi_return_values.pl @@ -2,7 +2,9 @@ :- use_module(library(ffi)). test :- - getenv("ffi_return_values_LIB", LIB), + read(Body), + term_variables(Body, [LIB]), + Body, use_foreign_module(LIB, [ 'ffi_return_values_true'([], bool), 'ffi_return_values_false'([], bool), diff --git a/tests/scryer/ffi.rs b/tests/scryer/ffi.rs index 13a9c3ca..a98f85a2 100644 --- a/tests/scryer/ffi.rs +++ b/tests/scryer/ffi.rs @@ -5,7 +5,7 @@ use std::{ process::Stdio, }; -use crate::helper::load_module_test; +use crate::helper::load_module_test_with_input; use current_platform::CURRENT_PLATFORM; @@ -53,12 +53,9 @@ fn ffi_f64_nan() { "##, ); - // technically UB as tests are by default multi-threaded, - // but there is currently no other easy way to get the dynamic library file path as an input into a load_module_test test - std::env::set_var("ffi_f64_nan_LIB", dynlib_path); - - load_module_test( + load_module_test_with_input( "tests-pl/ffi_f64_nan.pl", + format!("LIB={dynlib_path:?}."), " error(evaluation_error(undefined),round/1).\n", ); } @@ -81,12 +78,12 @@ fn ffi_f64_minus_zero() { "##, ); - // technically UB as tests are by default multi-threaded, - // but there is currently no other easy way to get the dynamic library file path as an input into a load_module_test test - std::env::set_var("ffi_f64_minus_zero_LIB", dynlib_path); - // note: ouput is currently wrong correct would be 1.0,1.0 - load_module_test("tests-pl/ffi_f64_minus_zero.pl", "-1.0,1.0"); + load_module_test_with_input( + "tests-pl/ffi_f64_minus_zero.pl", + format!("LIB={dynlib_path:?}."), + "-1.0,1.0", + ); } #[test] @@ -158,10 +155,6 @@ fn ffi_return_values() { "##, ); - // technically UB as tests are by default multi-threaded, - // but there is currently no other easy way to get the dynamic library file path as an input into a load_module_test test - std::env::set_var("ffi_return_values_LIB", dynlib_path); - let expected = format!( "i8- {},u8-{},i16- {},u16-{},i32- {},u32-{},i64- {},u64-{},f32-{},f64-{}", -42, @@ -176,7 +169,11 @@ fn ffi_return_values() { std::f64::consts::TAU ); - load_module_test("tests-pl/ffi_return_values.pl", expected.as_str()); + load_module_test_with_input( + "tests-pl/ffi_return_values.pl", + format!("LIB={dynlib_path:?}."), + expected.as_str(), + ); } #[test] @@ -191,12 +188,9 @@ fn ffi_invalid_type() { "##, ); - // technically UB as tests are by default multi-threaded, - // but there is currently no other easy way to get the dynamic library file path as an input into a load_module_test test - std::env::set_var("ffi_invalid_type_LIB", dynlib_path); - - load_module_test( + load_module_test_with_input( "tests-pl/ffi_invalid_type.pl", + format!("LIB={dynlib_path:?}."), "% Warning: initialization/1 failed for: user:test\n", ); } diff --git a/tests/scryer/helper.rs b/tests/scryer/helper.rs index 155040d3..910c0258 100644 --- a/tests/scryer/helper.rs +++ b/tests/scryer/helper.rs @@ -54,7 +54,7 @@ pub(crate) fn load_module_test_with_tokio_runtime(file: &str, exp pub(crate) fn load_module_test_with_input( file: &str, - input: Cow<'static, str>, + input: impl Into>, expected: T, ) { use scryer_prolog::MachineBuilder;