]> Repositorios git - scryer-prolog.git/commitdiff
fix UB in ffi tests
authorBennet Bleßmann <[email protected]>
Mon, 27 Jan 2025 20:55:34 +0000 (21:55 +0100)
committerBennet Bleßmann <[email protected]>
Fri, 1 Aug 2025 17:08:24 +0000 (19:08 +0200)
tests-pl/ffi_f64_minus_zero.pl
tests-pl/ffi_f64_nan.pl
tests-pl/ffi_invalid_type.pl
tests-pl/ffi_return_values.pl
tests/scryer/ffi.rs
tests/scryer/helper.rs

index 974b6e926d4a1cfb5f25d8131021e467c9868353..6ca2b4b376cd7a1b918b011fe139e8387752d7b2 100644 (file)
@@ -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),
index a80dbd90990452cb43960743c51aaa1c5d96e8fe..42dcd587a5be47626c5c928a8802a072fa091e2f 100644 (file)
@@ -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).
index c78668dfa158d87b3737add567205ea2a722a3ef..6e687240f87b053784fa999f9e50bf7fe6c8c7cb 100644 (file)
@@ -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)
     ]).
index 3365262c0364ac2a8d500988029b219ffd2b1131..ee1612598637b87b4029b1363542cf589ed6cc66 100644 (file)
@@ -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), 
index 13a9c3cae6014dd81f5cd163dc329397a57e42bf..a98f85a29e92d8999b3be44c10a1d9350011be74 100644 (file)
@@ -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",
     );
 }
index 155040d3b63dc7fd7f9b666ed7937cb36bbcb392..910c025838cb29ec48e045403eac906a8e14d690 100644 (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>(
     file: &str,
-    input: Cow<'static, str>,
+    input: impl Into<Cow<'static, str>>,
     expected: T,
 ) {
     use scryer_prolog::MachineBuilder;