I am yet again stuck with a problem that pops up sometimes but quite often nevertheless. I suspect I am misusing the foreign language interface so this might be just a code review.
What I am doing wrong is probably how I deal with options that I have declared in PL_option_t but were not provided by the Prolog call.
I’m using: SWI-Prolog version
threaded, 64 bits, version 9.3.7-8-gc292c73b1
I want the code to: parse an option list in a foreign function implemented in C using PL_option_t
and PL_scan_options()
. It kinda works, most of the time.
But what I’m getting is:
Here are three consecutive executions of the same code on my command line:
First run
$ make test
swipl -g run_tests -t halt swiplite.pl
[10/10] sqlite3:bad_mode ..
ERROR: Received fatal signal 11 (segv)
Time: Tue May 28 18:09:28 2024
Inferences: 557649
Thread: 1 (main)
C-stack trace labeled "crash":
[0] save_backtrace() at :? [0x77e61794174e]
[1] sigCrashHandler() at :? [0x77e617941839]
[2] __sigaction() at ??:? [0x77e617655ae0]
[3] PL_is_variable___LD() at :? [0x77e61790a3fc]
[4] PL_error() at :? [0x77e617851835]
[5] PL_domain_error() at ??:? [0x77e617852fa9]
[6] swiplite(pl_sqlite3_open+0x18b) [0x77e617bb937b]
[7] PL_next_solution___LD() at :? [0x77e617840e94]
[8] callProlog() at :? [0x77e617889b62]
[9] pl_with_output_to2_va() at pl-file.c:? [0x77e617921636]
[10] PL_next_solution___LD() at :? [0x77e6178410a9]
[11] callProlog() at :? [0x77e617889b62]
[12] pl_with_mutex() at :? [0x77e6178fe019]
[13] PL_next_solution___LD() at :? [0x77e617840f10]
[14] query_loop() at :? [0x77e6178895eb]
[15] prologToplevel() at :? [0x77e617889f4d]
[16] swipl(+0x1096) [0x5f5f1e819096]
[17] __libc_init_first() at ??:? [0x77e61763ec88]
[18] __libc_start_main() at ??:? [0x77e61763ed4c]
[19] swipl(+0x10e5) [0x5f5f1e8190e5]
PROLOG STACK:
make: *** [Makefile:12: test] Segmentation fault (core dumped)
Second run
$ make test
swipl -g run_tests -t halt swiplite.pl
[10/10] sqlite3:bad_mode .................................................... **FAILED (0.000 sec)
ERROR: /home/boris/code/swiplite/swiplite.pl:53:
ERROR: test sqlite3:bad_mode: wrong error
ERROR: Expected: error(domain_error(A,B),C)
ERROR: Got: error(instantiation_error,context(swiplite:sqlite3_open/3,D))
ERROR: 1 test failed
% 9 tests passed
% Test run completed in 0.060 seconds (0.059 cpu)
ERROR: -g run_tests: false
make: *** [Makefile:12: test] Error 1
Third run
$ make test
swipl -g run_tests -t halt swiplite.pl
[10/10] sqlite3:bad_mode ...................................................... passed (0.000 sec)
% All 10 tests passed in 0.060 seconds (0.059 cpu)
I have not changed anything at all between the runs, they are some seconds apart. I was not able to reproduce it with a smaller example but I am attaching the one C file and Prolog file that do that. Discourse wouldn’t let me attach the Makefile so here it is:
CPPFLAGS += -I/your/own/path/to/include
CFLAGS += -fpic -Wall -Wextra -O2
LDFLAGS += -shared
LDLIBS += -l sqlite3
obj = swiplite
$(obj): $(obj).o
.PHONY: test
test: $(obj)
swipl -g run_tests -t halt $(obj).pl
.PHONY: clean
clean:
-rm $(obj).o
-rm $(obj)
swiplite.c (2.8 KB)
swiplite.pl (1.7 KB)
PS: if anyone is wondering, I ended up here because I thought it was gonna be quite easy