fix UB in ffi tests

This commit is contained in:
Bennet Bleßmann
2025-01-27 21:55:34 +01:00
committed by Bennet Bleßmann
parent e1246f0c83
commit 760e1d2aac
6 changed files with 28 additions and 26 deletions

View File

@@ -2,7 +2,9 @@
:- use_module(library(ffi)). :- use_module(library(ffi)).
test :- 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)]), use_foreign_module(LIB, ['ffi_f64_minus_zero'([], f64), 'signum'([f64], f64)]),
ffi:'ffi_f64_minus_zero'(N), ffi:'ffi_f64_minus_zero'(N),
A is max(0.0, N), A is max(0.0, N),

View File

@@ -2,7 +2,9 @@
:- use_module(library(ffi)). :- use_module(library(ffi)).
test :- test :-
getenv("ffi_f64_nan_LIB", LIB), read(Body),
term_variables(Body, [LIB]),
Body,
use_foreign_module(LIB, ['ffi_f64_nan'([], f64)]), use_foreign_module(LIB, ['ffi_f64_nan'([], f64)]),
ffi:'ffi_f64_nan'(N), ffi:'ffi_f64_nan'(N),
_ is round(N). _ is round(N).

View File

@@ -2,7 +2,9 @@
:- use_module(library(ffi)). :- use_module(library(ffi)).
test :- test :-
getenv("ffi_invalid_type_LIB", LIB), read(Body),
term_variables(Body, [LIB]),
Body,
use_foreign_module(LIB, [ use_foreign_module(LIB, [
'ffi_invalid_type'([], c_void) 'ffi_invalid_type'([], c_void)
]). ]).

View File

@@ -2,7 +2,9 @@
:- use_module(library(ffi)). :- use_module(library(ffi)).
test :- test :-
getenv("ffi_return_values_LIB", LIB), read(Body),
term_variables(Body, [LIB]),
Body,
use_foreign_module(LIB, [ use_foreign_module(LIB, [
'ffi_return_values_true'([], bool), 'ffi_return_values_true'([], bool),
'ffi_return_values_false'([], bool), 'ffi_return_values_false'([], bool),

View File

@@ -5,7 +5,7 @@ use std::{
process::Stdio, process::Stdio,
}; };
use crate::helper::load_module_test; use crate::helper::load_module_test_with_input;
use current_platform::CURRENT_PLATFORM; use current_platform::CURRENT_PLATFORM;
@@ -53,12 +53,9 @@ fn ffi_f64_nan() {
"##, "##,
); );
// technically UB as tests are by default multi-threaded, load_module_test_with_input(
// 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(
"tests-pl/ffi_f64_nan.pl", "tests-pl/ffi_f64_nan.pl",
format!("LIB={dynlib_path:?}."),
" error(evaluation_error(undefined),round/1).\n", " 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 // 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] #[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!( let expected = format!(
"i8- {},u8-{},i16- {},u16-{},i32- {},u32-{},i64- {},u64-{},f32-{},f64-{}", "i8- {},u8-{},i16- {},u16-{},i32- {},u32-{},i64- {},u64-{},f32-{},f64-{}",
-42, -42,
@@ -176,7 +169,11 @@ fn ffi_return_values() {
std::f64::consts::TAU 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] #[test]
@@ -191,12 +188,9 @@ fn ffi_invalid_type() {
"##, "##,
); );
// technically UB as tests are by default multi-threaded, load_module_test_with_input(
// 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(
"tests-pl/ffi_invalid_type.pl", "tests-pl/ffi_invalid_type.pl",
format!("LIB={dynlib_path:?}."),
"% Warning: initialization/1 failed for: user:test\n", "% Warning: initialization/1 failed for: user:test\n",
); );
} }

View File

@@ -54,7 +54,7 @@ pub(crate) fn load_module_test_with_tokio_runtime<T: Expectable>(file: &str, exp
pub(crate) fn load_module_test_with_input<T: Expectable>( pub(crate) fn load_module_test_with_input<T: Expectable>(
file: &str, file: &str,
input: Cow<'static, str>, input: impl Into<Cow<'static, str>>,
expected: T, expected: T,
) { ) {
use scryer_prolog::MachineBuilder; use scryer_prolog::MachineBuilder;