1

I'm trying to test clang's -fsanitize=array-bounds compiler flag with some c99 code on Windows but I can't compile due to a linker error. My test code is this:

int index = 11;

int arr[10] = { 0 };
arr[index] = 33;

And when I compile I get this error:

----- Build Debug x64 -----
Executing build command:
clang_build.bat
lld-link: error: undefined symbol: __ubsan_handle_out_of_bounds
>>> referenced by D:\User Program Files_Data\software_projects\bgz_engine\source\win64_main.c:223
>>>               win64_main.o:(WinMainCRTStartup)
===== Success : 1 errors, 0 warnings =====

So it seems the compiler is inserting a __ubsan_handle_out_of_bounds symbol that it can't resolve. Is there some sort of library or linker flag I need to pass to lld-link to get this to work?

Edit:

This is my current clang_build.bat file:

@echo off

REM This represents the full directory path to your batch file
set cwd=%~dp0

call "C:\msvc\setup.bat"

REM -fno-stack-protector tells compiler to not worry about security calls (like __security_cookie) that would typically be inserted with c libs. -mstack-probe-size=99999999999 basically says don't worry about __chkdsk for checking stack overruns
set compiler_flags=-g -fdebug-macro -O0 -fno-stack-protector -Wall -Wpadded -mstack-probe-size=9999999 -fno-builtin -std=c99 -fdiagnostics-absolute-paths -fsanitize=array-bounds

REM -STACK:0x100000,0x100000 is setting function stack default to 1MB. Can increase this if we need to. Keep in mind though this also increases default stack size for all threads created
set linker_flags=-subsystem:windows -machine:x64 -incremental:no -opt:ref -debug:full -ignore:4099 -NODEFAULTLIB -STACK:0x100000,0x100000

set include_paths=-I"%cwd%source" -I"C:\msvc\Windows Kits\10\Include\10.0.22621.0\um"
set library_paths=-LIBPATH:"C:\msvc\Windows Kits\10\Lib\10.0.22621.0\um\x64" -LIBPATH:"C:\Program Files\LLVM\lib\clang\14.0.6\lib\windows"
set import_libraries="kernel32.lib" "user32.lib" "glew32s.lib" "opengl32.lib" "gdi32.lib" "clang_rt.ubsan_standalone-x86_64.lib"

IF NOT EXIST bin mkdir bin
pushd bin

clang --compile ..\source\editor.c %compiler_flags%
lld-link editor.o -dll %linker_flags% -export:editor_update -noentry

clang --compile ..\source\win64_main.c %compiler_flags% %include_paths% -I"%cwd%dep\glew-2.1.0\include" -DBGZ_WIN64 -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-null-dereference
lld-link win64_main.o -OUT:win64_main.exe %linker_flags% %library_paths% %import_libraries% -LIBPATH:"%cwd%dep\glew-2.1.0\lib\win64-release"

popd

I also did a quick search in the installed LLVM directory (have clang 14.0.6 installed by the way) and found 'clang_rt.ubsan_standalone-x86_64.lib' in the windows folder so I'm assumer the sanitizer supports windows

Jason
  • 2,198
  • 3
  • 23
  • 59
  • I assumed it did. I did a quick search in the LLVM install directory (installed clang version 14.0.6 by the way) and did a search for ubsan and found 'clang_rt.ubsan_standalone-x86_64.lib' – Jason Jan 25 '23 at 00:44

0 Answers0