Windows native build with ASan - Claude AI notes: ASan found a heap-use-after-free bug

FYI

CMake uses the nul device as a signal that a build is active. If you attempt to delete the entire build directory while this is present, you may encounter an error such as:

C:\dev-MSVC-PR\swipl-devel>rmdir /s /q build
build\src\Debug\nul - Access is denied.

nul is not a regular file but a Windows device, so it cannot be deleted like a normal file. For this reason, deletion attempts will fail even when run with administrative privileges.

On Windows, the correct way to remove the nul device entry is:

C:\dev-MSVC-PR\swipl-devel>del \\.\C:\dev-MSVC-PR\swipl-devel\build\src\Debug\nul

Peter’s changes seem fine and they pass all tricky tests on other platforms. So, for now I suspect some MSVC specific issue.

It never did. I suspect this could be an artifact produced by the MS IDE? I didn’t see this when building on the commandline. But then, again, this was using older MSVC tools.

I do not know the exact use of nul. When the build works cleanly nul is removed at the end of the process so if one looks for it then it will not exist.

Learning the command to delete it took days before getting a reliable and easy way to remove it, was even using full system reboots at first.

EDIT

Per Google AI

The NUL device file is used with MSVC and CMake on Windows to discard output, often in build scripts or commands like >NUL . It acts as a black hole for standard output or error redirection, ensuring that command-line output from compilers or tools does not clutter the build logs.


These warnings are showing up.

Wondering

  • Do you want to fix them
  • Want me to work on them and send PRs as needed
  • You pick some and have me do some
  • Ignore them for now

I am in no rush to fix them at present but would like to fix them at some point and you seem to like seeing more of the details as it seems I am the only one in the entire universe who tries to build SWI-Prolog for Windows on Windows using MSVC.

	Line  168: C:\dev-MSVC-PR\swipl-devel\src\pl-write.c(216,7): warning C4013: 'ms_snprintf' undefined; assuming extern returning int [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  170: C:\dev-MSVC-PR\swipl-devel\src\pl-thread.c(1969,3): warning C4013: 'ms_snprintf' undefined; assuming extern returning int [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  194: C:\dev-MSVC-PR\swipl-devel\src\pl-dict.c(274,3): warning C4133: 'function': incompatible types - from 'cmp_t (__cdecl *)(const void *,const void *,void *)' to 'int (__cdecl *)(const void *,const void *,void *)' [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  195: C:\dev-MSVC-PR\swipl-devel\src\pl-dict.c(319,39): warning C4133: 'function': incompatible types - from 'cmp_t (__cdecl *)(const void *,const void *,void *)' to 'int (__cdecl *)(const void *,const void *,void *)' [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  213: C:\dev-MSVC-PR\swipl-devel\src\os\pl-buffer.c(161,46): warning C4334: '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?) [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  222: C:\dev-MSVC-PR\swipl-devel\src\os\pl-text.c(1103,5): warning C4013: 'ms_snprintf' undefined; assuming extern returning int [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  256: C:\dev-MSVC-PR\swipl-devel\src\minizip\zip.c(995,9): warning C4267: 'function': conversion from 'size_t' to 'uLong', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  257: C:\dev-MSVC-PR\swipl-devel\src\minizip\zip.c(1117,92): warning C4267: '=': conversion from 'size_t' to 'uLong', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line  258: C:\dev-MSVC-PR\swipl-devel\src\minizip\zip.c(1122,32): warning C4267: '=': conversion from 'size_t' to 'uLong', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\src\swiplobjs.vcxproj]
	Line 4096: C:\dev-MSVC-PR\swipl-devel\packages\clib\form.c(370,7): warning C4267: '-=': conversion from 'size_t' to 'long', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\packages\clib\plugin_cgi.vcxproj]
	Line 4604: C:\dev-MSVC-PR\swipl-devel\packages\clib\nonblockio.c(1527,12): warning C4018: '<': signed/unsigned mismatch [C:\dev-MSVC-PR\swipl-devel\build\packages\clib\plugin_socket.vcxproj]
	Line 5321: LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library [C:\dev-MSVC-PR\swipl-devel\build\packages\nlp\plugin_snowball.vcxproj]
	Line 6003: C:\dev-MSVC-PR\swipl-devel\packages\jpl\src\main\c\jpl.c(1901,5): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\dev-MSVC-PR\swipl-devel\build\packages\jpl\plugin_jpl.vcxproj]
	Line 6004: C:\dev-MSVC-PR\swipl-devel\packages\jpl\src\main\c\jpl.c(1902,5): warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\dev-MSVC-PR\swipl-devel\build\packages\jpl\plugin_jpl.vcxproj]
	Line 6005: C:\dev-MSVC-PR\swipl-devel\packages\jpl\src\main\c\jpl.c(3165,5): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\dev-MSVC-PR\swipl-devel\build\packages\jpl\plugin_jpl.vcxproj]
	Line 6006: C:\dev-MSVC-PR\swipl-devel\packages\jpl\src\main\c\jpl.c(4927,7): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. [C:\dev-MSVC-PR\swipl-devel\build\packages\jpl\plugin_jpl.vcxproj]
	Line 6224: LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library [C:\dev-MSVC-PR\swipl-devel\build\packages\libedit\plugin_libedit4pl.vcxproj]
	Line 6853: C:\dev-MSVC-PR\swipl-devel\packages\semweb\murmur.c(74,25): warning C4267: 'initializing': conversion from 'size_t' to 'unsigned int', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\packages\semweb\plugin_rdf_db.vcxproj]
	Line 6920: C:\dev-MSVC-PR\swipl-devel\packages\semweb\murmur.c(74,25): warning C4267: 'initializing': conversion from 'size_t' to 'unsigned int', possible loss of data [C:\dev-MSVC-PR\swipl-devel\build\packages\semweb\plugin_turtle.vcxproj]
	Line 7448: C:\dev-MSVC-PR\swipl-devel\packages\cpp\test_cpp.cpp(2376,17): warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _strdup. See online help for details. [C:\dev-MSVC-PR\swipl-devel\build\packages\cpp\plugin_test_cpp.vcxproj]

Status update

The number 009 is the 9th build, the number is used to track with the capture of the output.

009 Copy PCRE DLLs from vcpkg to SWI-Prolog build using CMake. 
Using x64 Native Tools Command Prompt for VS Terminal
   cd C:\dev-MSVC-PR\swipl-devel
   del \\.\C:\dev-MSVC-PR\swipl-devel\build\src\Debug\nul  (If needed)
   rmdir /s /q build
   mkdir build 
   cd build
   cmake .. -G "Visual Studio 18 2026" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DSWIPL_PACKAGES_GUI=OFF -DSWIPL_PACKAGES_ARCHIVE=OFF -DSWIPL_PACKAGES_YAML=OFF -DSWIPL_PACKAGES_PYTHON=OFF > "C:\Users\Eric\Projects\SWI-Prolog-PR\output\cmake-config (009).txt" 2>&1
   echo %ERRORLEVEL% -> 0
   cmake --build . --config Debug --verbose > "C:\Users\Eric\Projects\SWI-Prolog-PR\output\cmake-build (009).txt" 2>&1
   echo %ERRORLEVEL% -> 0
   C:\Users\Eric>dir /B C:\dev-MSVC-PR\swipl-devel\build\src\Debug\pcre*
      pcre2-8d.dll
      pcre4pl.dll
      pcre4pl.pdb
   PR accepted: https://github.com/SWI-Prolog/swipl-devel/commit/05067d98ba76750d54001442405a6c2078c81b84
   ctest -C Debug --output-on-failure --timeout 300 --verbose > "C:\Users\Eric\Projects\SWI-Prolog-PR\output\ctest (009).txt" 2>&1
      Windows dialog - Debug Error! - The variable `dts` is being used without being initialized.
   echo %ERRORLEVEL% -> 8

CTest capture


4: % [132/221] bigint:bf_trig_alloc ..
4/83 Test #4: swipl:core …***Exception: SegFault 4.25 sec
Running scripts from core
% [1/52] acyclic_terms:var_is_acyclic … passed (0.020 sec)

Notice that the rest of the tests for bigint were not run because of a hard crash


29: % [5/5] rwlocks:invalid_mode … passed (0.002 sec)
29: .failed(129948)
29: Script c:/dev-msvc-pr/swipl-devel/tests/thread/test_shared_dynamic.pl failed
29: ..
29: % [1/24] thread_create:alias … passed (0.001 sec)


98% tests passed, 2 tests failed out of 83

Total Test time (real) = 222.08 sec

The following tests FAILED:
4 - swipl:core (SEGFAULT)
29 - swipl:thread (Failed)
Errors while running CTest

In order to allow the rest of the test suite to run, I blocked the test that was causing the SEGFAULT:

File: tests/core/test_arith.pl
Line: 602: test(bf_trig_alloc, [blocked(ā€˜Memory management issue on Windows MSVC’), Len == 7634])

However, the other tests mentioned earlier—which had appeared after applying the fix for bigint:bf_trig_alloc and then vanished when the fix was removed —have now reappeared by just blocking the test.

4: % [16/19] format:large_float_1 .................... **FAILED (0.000 sec)
4: ERROR: [Thread main] c:/dev-msvc-pr/swipl-devel/tests/core/test_format.pl:84:
4: ERROR: [Thread main]     test format:large_float_1: 
4: ERROR: [Thread main] wrong answer for Len (compared using ==)
4: ERROR: [Thread main]     Expected: 108
4: ERROR: [Thread main]     Got:      516
4: % [17/19] format:oncodes_1 .......................... passed (0.000 sec)
4: % [15/20] term_hash2:simple_3 ..................... **FAILED (0.011 sec)
4: ERROR: [Thread main] c:/dev-msvc-pr/swipl-devel/tests/core/test_hash.pl:130:
4: ERROR: [Thread main]     test term_hash2:simple_3: assertion
4: e[1;31mERROR: [Thread main] c:/dev-msvc-pr/swipl-devel/tests/core/test_hash.pl:130:
4: ERROR: [Thread main]     test e[34mterm_hash2:simple_3e[0me[0me[1;31m: assertion at c:/dev-msvc-pr/swipl-devel/build/home/boot/init.pl:685 failed
4: 	Assertion: memberchk(1902251786,[347171279,1214499792,3784382378,3289800483,-510584918])
4: e[0m

FYI

When using VS Code with a local copy of the SWI-Prolog source tree for building—without it being synced to any GitHub repository—the Claude Code for VS Code extension worked reliably.

However, when using VS Code with Git repositories set up to create pull requests (PRs) for SWI-Prolog, including the roughly 40 packages (Git Submodules) involved in the build, the Claude Code for VS Code extension would frequently fail to process prompts. In contrast, switching to the Claude Code CLI proved to be significantly more consistent.

Status update

Using x64 Native Tools Command Prompt for VS Terminal


   C:\dev-MSVC-PR\swipl-devel\build>swipl --version
      Expected result: 'swipl' is not recognized as an internal or external command, operable program or batch file.
      Note: If swipl is on the Windows PATH it could potentially cause problems as there will be two executables on the same machine. By removing swipl from the Windows Path, absolute paths are needed.
   set PATH=%PATH:C:\Program Files\swipl\bin;=%                           (If needed)
   python --version
      Expected result: Python 3.13.5
   set PATH=C:\Users\Groot\AppData\Local\Programs\Python\Python313;%PATH%  (If needed)
   cd C:\dev-MSVC-PR\swipl-devel
   del \\.\C:\dev-MSVC-PR\swipl-devel\build\src\Debug\nul                (If needed)
   rmdir /s /q build
   mkdir build 
   cd build
   cmake .. -G "Visual Studio 18 2026" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DPython_EXECUTABLE="C:/Users/Groot/AppData/Local/Programs/Python/Python313/python.exe" -DBDB_LIBRARY=C:/dev/vcpkg/installed/x64-windows/lib/libdb48.lib 
   echo %ERRORLEVEL%
      Expected result: 0
   cmake --build . --config Debug --verbose
   echo %ERRORLEVEL%
      Expected result: 0
   ctest -C Debug --output-on-failure --timeout 300 --verbose 
   echo %ERRORLEVEL%
      Expected result: 0
   "C:\dev-MSVC-PR\swipl-devel\build\src\Debug\swipl.exe"
   ?- check_installation.

CTest results

29: .failed(150113)
29: Script c:/dev-msvc-pr/swipl-devel/tests/thread/test_shared_dynamic.pl failed
29: ..

99% tests passed, 1 tests failed out of 89

Total Test time (real) = 192.49 sec

The following tests FAILED:
	 29 - swipl:thread (Failed)
Errors while running CTest

Skipped tests

Line 4634: 66: No W3C test files present; skipping Turtle tests
Line 5388: 84: Cannot find JSON Schema test data; skipping tests

Blocked tests

dif:cyclic - C:\dev-MSVC-PR\swipl-devel\tests\attvar\test_dif.pl
bigint:bf_trig_alloc - C:\dev-MSVC-PR\swipl-devel\tests\core\test_arith.pl
phrase:iso_8_1_1_3 - C:\dev-MSVC-PR\swipl-devel\tests\core\test_dcg.pl
ieee754:ieee_div - C:\dev-MSVC-PR\swipl-devel\tests\rational\test_ieee754.pl
bomb_compiler:call - C:\dev-MSVC-PR\swipl-devel\tests\signals\test_bomb.pl

C:\dev-MSVC-PR\swipl-devel\build>"C:\dev-MSVC-PR\swipl-devel\build\src\Debug\swipl.exe"
Welcome to SWI-Prolog (threaded, 64 bits, version 10.1.2-20-ge220f2be8)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

    CMake built from "c:/dev-MSVC-PR/swipl-devel/build"

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

1 ?- check_installation.
% Checking your SWI-Prolog kit for common issues ...
%
% Version: ............. 10.1.2-20-ge220f2be8
% Address bits: ........ 64
% Architecture: ........ x64-win64
% Installed at: ........ c:/dev-msvc-pr/swipl-devel/build/home
% Cores: ............... 8
%
% Checking gmp ................................. ok
% Loading library(archive) ..................... ok
%   Supported filters: bzip2, compress, gzip, grzip, lrzip, lzip, lzma, lzop, none, rpm, uu, xz
%   Supported formats: 7zip, ar, cab, cpio, empty, gnutar, iso9660, lha, mtree, rar, raw, tar, xar, zip
% Loading library(cgi) ......................... ok
% Loading library(crypt) ....................... ok
% Loading library(bdb) ......................... ok
% Loading library(double_metaphone) ............ ok
% Loading library(editline) .................... ok
% Loading library(filesex) ..................... ok
% Loading library(http/http_stream) ............ ok
% Loading library(json) ........................ ok
% Loading library(http/jquery) ................. ok
%   jQuery from c:/dev-msvc-pr/swipl-devel/build/home/library/http/web/js/jquery-3.6.0.min.js
% Loading library(isub) ........................ ok
% Loading library(janus) ....................... ok
% Interactive session; added `.` to Python `sys.path`
%   Python version 3.13.5 (tags/v3.13.5:6cb20a2, Jun 11 2025, 16:15:46) [MSC v.1943 64 bit (AMD64)]
% Loading library(jpl) ...
% Extended DLL search path with
%   'C:\\Program Files\\Microsoft\\jdk-25.0.1.8-hotspot/bin/server'
................................................ ok
% Loading library(memfile) ..................... ok
% Loading library(odbc) ........................ ok
% Loading library(pce) ......................... ok
% Loading library(pcre) ........................ ok
% Loading library(pdt_console) ................. ok
% Loading library(porter_stem) ................. ok
% Loading library(process) ..................... ok
% Loading library(protobufs) ................... ok
% Loading library(readutil) .................... ok
% Loading library(semweb/rdf_db) ............... ok
% Loading library(semweb/rdf_ntriples) ......... ok
% Loading library(semweb/turtle) ............... ok
% Loading library(sgml) ........................ ok
% Loading library(sha) ......................... ok
% Loading library(snowball) .................... ok
% Loading library(socket) ...................... ok
% Loading library(ssl) ......................... ok
% Loading library(sweep_link) .................. ok
%   GNU-Emacs plugin loads
%     M c:/dev-MSVC-PR/swipl-devel/build/src/Debug/sweep-module.dll
% Loading library(crypto) ...................... ok
% Loading library(table) ....................... ok
% Loading library(time) ........................ ok
% Loading library(unicode) ..................... ok
% Loading library(uri) ......................... ok
% Loading library(uuid) ........................ ok
% Loading library(yaml) ........................ ok
% Loading library(zlib) ........................ ok
%
% Congratulations, your kit seems sound and complete!
true.

2 ?- 

Build is currently not using ASan.

Nice progress :hugs:

Is this deterministic or does it fail sometimes? There seems to be something wrong with this, at least on some platforms. A (mostly) deterministic failure may help debugging this. On Linux it can run for days and then occasionally fails. That makes it hard to debug :frowning:

Some of these are already blocked. Did you block some more?

1 Like

If fails sometimes.


The only one I manually blocked was

bigint:bf_trig_alloc

which is from the first table.

As noted, Claude came up with a fix but I think of it more of as a hack.

I can add the fix/hack, see if it works and if so send it as a PR, but I would not be surprised if you agree it is not something that should be used. Let me know.


If you’d like the full output from cmake -G, the cmake build, and ctest, I can email them to you. The logs are quite large, so they can’t be included directly in a reply here.

I can also try to make them available for download here if that’s preferable, though I’m not sure whether that will work. Just let me know how you’d like to proceed.


I’m mostly interested in the details of the asan crash message. That might give a clue. Claude’s ā€œfixā€ may have a clue as well. As additional info to the crash message though.

1 Like

These are the next cmake build and ctest instructions which include ASan options that I’ll run.
bigint:bf_trig_alloc test is unblocked.

   cmake .. -G "Visual Studio 18 2026" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_C_FLAGS="/fsanitize=address" -DCMAKE_CXX_FLAGS="/fsanitize=address" -DCMAKE_EXE_LINKER_FLAGS="/DEBUG:FULL" -DCMAKE_SHARED_LINKER_FLAGS="/DEBUG:FULL" -DPython_EXECUTABLE="C:/Users/Eric/AppData/Local/Programs/Python/Python313/python.exe" -DBDB_LIBRARY=C:/dev/vcpkg/installed/x64-windows/lib/libdb48.lib 
   cmake --build . --config Debug --verbose 
   set ASAN_SAVE_DUMPS=C:\dev-MSVC-PR\swipl-devel\build\asan_crash.dmp
   ctest -C Debug --output-on-failure --timeout 300 --verbose 

During the cmake build step swipl was used to build the documentation and ASan triggered.

C:\dev-MSVC-PR\swipl-devel\build>src\Debug\swipl.exe -f none --no-packs --home=home -g "use_module(library(pldoc/man_index))" -g "prolog_manual_index:index_man_file(manual, swi('/doc/Manual'), 'c:/dev-msvc-pr/swipl-devel/build/man/manual/arith.html'), writeln(done)" -g halt

done

C:\dev-MSVC-PR\swipl-devel\build>echo Run 1: %ERRORLEVEL%
Run 1: -1073741819

C:\dev-MSVC-PR\swipl-devel\build>src\Debug\swipl.exe -f none --no-packs --home=home -g "use_module(library(pldoc/man_index))" -g "prolog_manual_index:index_man_file(manual, swi('/doc/Manual'), 'c:/dev-msvc-pr/swipl-devel/build/man/manual/arith.html'), writeln(done)" -g halt

done

C:\dev-MSVC-PR\swipl-devel\build>echo Run 2: %ERRORLEVEL%
Run 2: -1073741819

C:\dev-MSVC-PR\swipl-devel\build>src\Debug\swipl.exe -f none --no-packs --home=home -g "use_module(library(pldoc/man_index))" -g "prolog_manual_index:index_man_file(manual, swi('/doc/Manual'), 'c:/dev-msvc-pr/swipl-devel/build/man/manual/arith.html'), writeln(done)" -g halt

done

C:\dev-MSVC-PR\swipl-devel\build>echo Run 3: %ERRORLEVEL%
Run 3: -1073741819

The error code -1073741819 (hexadecimal 0xC0000005) is the standard Windows exit code for an Access Violation.

Confirmed Bug Summary

Finding Detail
Trigger arith.html indexing via pldoc/man_index
Crash point During halt / PL_cleanup()
Reproducibility 100% deterministic
Not GC-related Consistent across runs

Minimal Reproduction

swipl.exe -f none --no-packs --home=home ^
  -g "use_module(library(pldoc/man_index))" ^
  -g "prolog_manual_index:index_man_file(manual, swi('/doc/Manual'), 'arith.html')" ^
  -g halt

This is a clean bug report for upstream. The crash is likely in the SGML parser cleanup or atom/term table cleanup after parsing arith.html.


Will also be sending a PR related to __SANITIZE_ADDRESS__ .
MSVC ASan does not include LSan.

For example:

/* LeakSanitizer is only available on Linux and macOS, not Windows MSVC */
  char *s;

  if ( (s=getenv("ASAN_OPTIONS")) && strstr(s,"detect_leaks=1") )
  { fprintf(stderr, "Running LSAN memory leak check (reclaim_memory=%d)\n",
	   reclaim_memory);
    if ( __lsan_do_recoverable_leak_check() )
    { fprintf(stderr, "Leaks detected; sleeping 60 sec.  Attach using\n"
	     "   gdb -p %d\n", getpid());
      sleep(60);
    } else
    { fprintf(stderr, "No leaks detected\n");
    }
  }

There are several changes.

C:\dev-MSVC-PR\swipl-devel\build>dumpbin /headers src\Debug\swipl.exe
Microsoft (R) COFF/PE Dumper Version 14.50.35723.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file src\Debug\swipl.exe

...

OPTIONAL HEADER VALUES
   ...
          400000 size of stack reserve
            1000 size of stack commit
          100000 size of heap reserve
            1000 size of heap commit
   ...

Based on the dumpbin /headers output, here are the stack sizes for swipl.exe:

Stack Sizes (from OPTIONAL HEADER VALUES)

Setting Hex Value Decimal Human Readable
Stack Reserve 0x400000 4,194,304 bytes 4 MB
Stack Commit 0x1000 4,096 bytes 4 KB

Heap Sizes (for comparison)

Setting Hex Value Decimal Human Readable
Heap Reserve 0x100000 1,048,576 bytes 1 MB
Heap Commit 0x1000 4,096 bytes 4 KB

What This Means

  • Stack Reserve (4 MB): The maximum virtual address space reserved for the stack. This is the upper limit the stack can grow to.
  • Stack Commit (4 KB): The initial physical memory committed for the stack. Windows commits additional pages as the stack grows (up to the reserve limit).

The 4 MB stack reserve is the default for MSVC x64 builds. If SWI-Prolog needs a larger stack for deep recursion, it would need to be increased via:

  • Linker option: /STACK:reserve[,commit]
  • In CMake: set_target_properties(swipl PROPERTIES LINK_FLAGS "/STACK:8388608") for 8 MB

Last night, during the cmake configure, ASan reported an Access Violation. I was able to reproduce the issue and create a minimal reproducible example.

Details in earlier reply.

I attempted to increase the stack size using /STACK:8388608, that did not complete as expected, will have to investigate further; will also try the way Jan suggested,

-DSWIPL_C_STACK_SIZE=2097152 to CMake for 2Mb

The build triggering the Access Violation was configured with the following CMake command:

cmake .. -G "Visual Studio 18 2026" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_C_FLAGS="/fsanitize=address" -DCMAKE_CXX_FLAGS="/fsanitize=address" -DCMAKE_EXE_LINKER_FLAGS="/DEBUG:FULL /STACK:8388608" -DCMAKE_SHARED_LINKER_FLAGS="/DEBUG:FULL" -DPython_EXECUTABLE="C:/Users/Eric/AppData/Local/Programs/Python/Python313/python.exe" -DBDB_LIBRARY=C:/dev/vcpkg/installed/x64-windows/lib/libdb48.lib

After deleting the build directory and running the cmake configuration, the Access Violation occurred again, as expected.

Since the failure happens during documentation generation—and a minimal reproducible example exists—it may be useful to temporarily disable documentation builds. This could help to get to other ASan errors that can produce a more informative report (e.g., a usable stack trace or additional diagnostics).

Thoughts?

You need -DSWIPL_C_STACK_SIZE=8388608 to overrule the default 4Mb. 4Mb should be plenty for a Release build, but Debug and Asan may need more.

Most interesting seems to be the arithmetic crash though. Please, using the Asan version, run in a console (not sure about the correct path to swipl.exe; adjust as necessary)

src\Debug\swipl.exe ../tests/core/test_arith.pl
?- run_tests(bigint:bf_trig_alloc).

That should hopefully dump a proper address sanitizer error report into the console.

1 Like

Thanks, did not know this.

C:\dev-MSVC-PR\swipl-devel\build>src\Debug\swipl.exe ../tests/core/test_arith.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 10.1.2-21-gc8e4b2a69-DIRTY)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

    CMake built from "c:/dev-MSVC-PR/swipl-devel/build"

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

3 ?- run_tests(bigint:bf_trig_alloc).
[1/1] bigint:bf_trig_alloc ..=================================================================
==170884==ERROR: AddressSanitizer: access-violation on unknown address 0xffffffffffffffff (pc 0x7ff84245d8e1 bp 0x000000000000 sp 0x00deaaff4020 T0)
==170884==The signal is caused by a READ memory access.
    #0 0x7ff84245d8e0 in bf_realloc C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.h:209
    #1 0x7ff84245d94e in bf_malloc C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.h:215
    #2 0x7ff84248e2b6 in ntt_malloc C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.c:7697
    #3 0x7ff842470ab7 in fft_mul C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.c:8435
    #4 0x7ff842461379 in bf_mul C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.c:1624
    #5 0x7ff841fcce39 in mpz_mul C:\dev-MSVC-PR\swipl-devel\src\libbf\bf_gmp.h:399
    #6 0x7ff841fcffbd in ar_mul C:\dev-MSVC-PR\swipl-devel\src\pl-arith.c:3535
    #7 0x7ff841fd2bec in evalExpression___LD C:\dev-MSVC-PR\swipl-devel\src\pl-arith.c:1115
    #8 0x7ff841fd16f8 in valueExpression___LD C:\dev-MSVC-PR\swipl-devel\src\pl-arith.c:1212
    #9 0x7ff841ff4969 in pl_is2_va C:\dev-MSVC-PR\swipl-devel\src\pl-arith.c:4774
    #10 0x7ff841f84598 in instr_I_FCALLDETVA C:\dev-MSVC-PR\swipl-devel\src\pl-vmi.c:4343
    #11 0x7ff841fca648 in PL_next_solution_guarded___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3760
    #12 0x7ff841f353e7 in PL_next_solution___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3570
    #13 0x7ff8420e9a98 in callProlog C:\dev-MSVC-PR\swipl-devel\src\pl-pro.c:529
    #14 0x7ff84238b00b in pl_with_output_to2_va C:\dev-MSVC-PR\swipl-devel\src\os\pl-file.c:1814
    #15 0x7ff841f84598 in instr_I_FCALLDETVA C:\dev-MSVC-PR\swipl-devel\src\pl-vmi.c:4343
    #16 0x7ff841fca648 in PL_next_solution_guarded___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3760
    #17 0x7ff841f353e7 in PL_next_solution___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3570
    #18 0x7ff8420e9a98 in callProlog C:\dev-MSVC-PR\swipl-devel\src\pl-pro.c:529
    #19 0x7ff8422fb620 in pl_with_mutex C:\dev-MSVC-PR\swipl-devel\src\pl-mutex.c:769
    #20 0x7ff841f85571 in instr_I_FCALLDET2 C:\dev-MSVC-PR\swipl-devel\src\pl-vmi.c:4378
    #21 0x7ff841fca648 in PL_next_solution_guarded___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3760
    #22 0x7ff841f353e7 in PL_next_solution___LD C:\dev-MSVC-PR\swipl-devel\src\pl-wam.c:3570
    #23 0x7ff8420ea3c3 in query_loop C:\dev-MSVC-PR\swipl-devel\src\pl-pro.c:171
    #24 0x7ff8420ea1ac in prologToplevel C:\dev-MSVC-PR\swipl-devel\src\pl-pro.c:661
    #25 0x7ff8423520de in PL_toplevel C:\dev-MSVC-PR\swipl-devel\src\pl-fli.c:4998
    #26 0x7ff76d0e10e4 in wmain C:\dev-MSVC-PR\swipl-devel\src\pl-main.c:142
    #27 0x7ff76d0e2888 in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:90
    #28 0x7ff76d0e27a1 in __scrt_common_main_seh D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #29 0x7ff76d0e265d in __scrt_common_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:330
    #30 0x7ff76d0e28fd in wmainCRTStartup D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_wmain.cpp:16
    #31 0x7ff8d7b7e8d6  (C:\WINDOWS\System32\KERNEL32.DLL+0x18002e8d6)
    #32 0x7ff8d958c40b  (C:\WINDOWS\SYSTEM32\ntdll.dll+0x18008c40b)

==170884==Register values:
rax = 18e156feb5360000  rbx = 0  rcx = 2259ae80000  rdx = 0
rdi = 0  rsi = 0  rbp = 0  rsp = deaaff4020
r8  = 1000  r9  = deaaff5398  r10 = 2417047ec73  r11 = 3c
r12 = 0  r13 = 0  r14 = 0  r15 = 0
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: access-violation C:\dev-MSVC-PR\swipl-devel\src\libbf\libbf.h:209 in bf_realloc

Succesfully saved dump to "C:\dev-MSVC-PR\swipl-devel\build\asan_crash.dmp"
==170884==ABORTING

C:\dev-MSVC-PR\swipl-devel\build>

Note: This was not rebuilt with a larger stack size, might be just a simple increase in the stack size to fix.

Would you like me to try a larger stack sizes using the -DSWIPL_C_STACK_SIZE=8388608?

This has nothing to do with stack sizes. It seems to relate to an invalid pointer to the LibBF context (0xffffffffffffffff). I’m having a quick look, but that might take more time than I can afford today.

P.s. This crash seems unrelated to Asan, no? I.e., it should also happen in non-asan builds.

The noted test fails for a non-asan build.


C:\dev-MSVC-PR\swipl-devel\build>cmake .. -G "Visual Studio 18 2026" -A x64 -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_EXE_LINKER_FLAGS="/DEBUG:FULL" -DCMAKE_SHARED_LINKER_FLAGS="/DEBUG:FULL" -DPython_EXECUTABLE="C:/Users/Eric/AppData/Local/Programs/Python/Python313/python.exe" -DBDB_LIBRARY=C:/dev/vcpkg/installed/x64-windows/lib/libdb48.lib 

echo %ERRORLEVEL%
0

C:\dev-MSVC-PR\swipl-devel\build>cmake --build . --config Debug --verbose

C:\dev-MSVC-PR\swipl-devel\build>echo %ERRORLEVEL%
0

C:\dev-MSVC-PR\swipl-devel\build>src\Debug\swipl.exe ../tests/core/test_arith.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 10.1.2-21-gc8e4b2a69-DIRTY)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

    CMake built from "c:/dev-MSVC-PR/swipl-devel/build"

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

3 ?- run_tests(bigint:bf_trig_alloc).
[1/1] bigint:bf_trig_alloc ..
C:\dev-MSVC-PR\swipl-devel\build>echo %ERRORLEVEL%                                                                                        
-1073741819                                                                                                                               


Using the SWI-Prolog downloaded version for Windows for the noted test works


C:\Users\Eric>"C:\Program Files\swipl\bin\swipl.exe" --version
SWI-Prolog version 10.1.1 for x64-win64

C:\Users\Eric>"C:\Program Files\swipl\bin\swipl.exe" C:\dev-MSVC-PR\swipl-devel\tests\core\test_arith.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 10.1.1)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

2 ?- run_tests(bigint:bf_trig_alloc).
[1/1] bigint:bf_trig_alloc ........................................................................................................................................................ passed (0.078 sec)
% test passed in 0.102 seconds (0.016 cpu)
true.

I thought I try this on MSYS2/clang64, but I don’t see any problematic behavior (neither on swipl 10 nor on the git version with ASAN). I switched USE_GMP on and off.

Thanks!

Not totally surprised.

I did send Jan W. an analysis report that Claude was able to produce using the output from the ASan-enabled terminal run and the crash dump. I then used the Visual Studio IDE after the crash to inspect the stack, function calls, data structures, pointers, and related state. With Claude’s help, I was able to confirm the findings against concrete evidence, rather than relying on speculative or hallucinated AI reasoning.

The next step is for me to allow Claude to propose code changes and then rerun everything with ASan enabled to see how it behaves.

Jan W. will be doing a fix so will be waiting for that.

If Jan wants to include you in the private email thread, I have no objection. I’m mainly trying to avoid posting details publicly that may turn out to be incorrect, as once indexed by search engines they can create misleading trails later on. :slightly_smiling_face: