Skip to content

Commit

Permalink
Merge branch 'sc_main' into dependabot/github_actions/github/codeql-a…
Browse files Browse the repository at this point in the history
…ction-3.26.10
  • Loading branch information
aqnuep authored Oct 3, 2024
2 parents 59b2b1c + c59c24a commit 35af4ef
Show file tree
Hide file tree
Showing 30 changed files with 1,566 additions and 155 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ VKConfig.h
*.files
*.includes
.vscode/
.cache
.DS_Store
_out64
out32/*
Expand Down
65 changes: 60 additions & 5 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ if (is_win) {
}

config("vulkan_internal_config") {
defines = [
"VK_ENABLE_BETA_EXTENSIONS",
]
defines = [ "VK_ENABLE_BETA_EXTENSIONS" ]
if (is_clang || !is_win) {
cflags = [
"-Wno-conversion",
Expand Down Expand Up @@ -99,6 +97,52 @@ if (!is_android) {
} else {
library_type = "static_library"
}
support_unknown_function_handling = false
if (defined(ar_path) && ar_path != "" && !is_win && (current_cpu == "arm64" || current_cpu == "x86_64")) {
support_unknown_function_handling = true
static_library("asm_offset") {
sources = [ "loader/asm_offset.c" ]
deps = [
"$vulkan_headers_dir:vulkan_headers",
]

if (is_fuchsia) {
deps += [
":dlopen_fuchsia",
]
}

# Output raw assembly instead of compiled object file. The assembly will be included as a member of the output ar file.
cflags = [ "-S" ]
configs += [ ":vulkan_internal_config" ]
configs += [ ":vulkan_loader_config" ]
}

action("gen_defines") {
script = "scripts/parse_asm_values.py"
deps = [ ":asm_offset" ]

inputs = [
"$target_out_dir/libasm_offset.a",
ar_path,
]
if (current_cpu == "arm64") {
cpu = "aarch64"
} else {
cpu = "x86_64"
}
args = [
rebase_path("$target_gen_dir/gen_defines.asm", root_build_dir),
rebase_path("$target_out_dir/libasm_offset.a", root_build_dir),
"GAS",
"Clang",
cpu,
rebase_path(ar_path, root_build_dir),
"libasm_offset.asm_offset.c.o",
]
outputs = [ "$target_gen_dir/gen_defines.asm" ]
}
}

target(library_type, "libvulkan") {
sources = [
Expand Down Expand Up @@ -135,8 +179,6 @@ if (!is_android) {
"loader/unknown_function_handling.h",
"loader/unknown_function_handling.c",
"loader/vk_loader_layer.h",

# TODO(jmadill): Use assembler where available.
"loader/vk_loader_platform.h",
"loader/wsi.c",
"loader/wsi.h",
Expand Down Expand Up @@ -217,6 +259,19 @@ if (!is_android) {

runtime_deps = [ "//sdk/lib/fdio:fdio_sdk" ]
}
if (support_unknown_function_handling) {
if (current_cpu == "arm64") {
sources += [ "loader/unknown_ext_chain_gas_aarch.S" ]
} else if (current_cpu == "x86_64") {
sources += [ "loader/unknown_ext_chain_gas_x86.S" ]
} else {
assert(false, "Unexpected CPU $current_cpu")
}

defines += ["UNKNOWN_FUNCTIONS_SUPPORTED=1"]
deps += [ ":gen_defines" ]
include_dirs = [ "$target_gen_dir" ]
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Instructions for building this repository on Linux, Windows, and MacOS.
- [Cross Compilation](#cross-compilation)
- [Unknown function handling which requires explicit assembly implementations](#unknown-function-handling-which-requires-explicit-assembly-implementations)
- [Platforms which fully support unknown function handling](#platforms-which-fully-support-unknown-function-handling)
- [Link Time Optimization](#link-time-optimization)
- [Tests](#tests)


Expand Down Expand Up @@ -640,6 +639,8 @@ can be manually disabled by setting `USE_GAS` or `USE_MASM` to `OFF`.
* 64 bit Linux (x64)
* 32 bit Linux (x86)
* 64 bit Arm (aarch64)
* 32 bit Arm (aarch32)


Platforms not listed will use a fallback C Code path that relies on tail-call optimization to work.
No guarantees are made about the use of the fallback code paths.
Expand Down
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ set(VULKANSC ON CACHE BOOL "User defined variable for VULKANSC mode to be passed
if(VULKANSC)
project(VULKAN_LOADER VERSION 1.0.15 LANGUAGES C)
else()
project(VULKAN_LOADER VERSION 1.3.286 LANGUAGES C)
project(VULKAN_LOADER VERSION 1.3.295 LANGUAGES C)
endif()

option(CODE_COVERAGE "Enable Code Coverage" OFF)
Expand Down Expand Up @@ -191,6 +191,8 @@ target_link_libraries(loader_common_options INTERFACE platform_wsi)
# Enable beta Vulkan extensions
target_compile_definitions(loader_common_options INTERFACE VK_ENABLE_BETA_EXTENSIONS)

string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" SYSTEM_PROCESSOR)

option(BUILD_WERROR "Enable warnings as errors")

# Set warnings as errors and the main diagnostic flags
Expand All @@ -213,7 +215,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(loader_common_options INTERFACE -Wno-missing-field-initializers)

# need to prepend /clang: to compiler arguments when using clang-cl
if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND "${CMAKE_CXX_SIMULATE_ID}" MATCHES "MSVC")
if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND "${CMAKE_C_COMPILER_FRONTEND_VARIANT}" MATCHES "MSVC")
target_compile_options(loader_common_options INTERFACE /clang:-fno-strict-aliasing)
else()
target_compile_options(loader_common_options INTERFACE -fno-strict-aliasing)
Expand Down Expand Up @@ -242,6 +244,11 @@ if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR (CMAKE_C_COMPILER_ID STREQUAL "Clang" A

# Prevent <windows.h> from polluting the code. guards against things like MIN and MAX
target_compile_definitions(loader_common_options INTERFACE WIN32_LEAN_AND_MEAN)

# For some reason Advapi32.lib needs to be explicitely linked to when building for Arm (32 bit) on Windows, but isn't required on any other architecture
if (SYSTEM_PROCESSOR MATCHES "arm" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
target_link_libraries(loader_common_options INTERFACE Advapi32)
endif()
endif()

# DEBUG enables runtime loader ICD verification
Expand Down
2 changes: 1 addition & 1 deletion docs/LoaderInterfaceArchitecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ These files are found in different locations based on your platform:
</tr>
<tr>
<td>Vulkan Layer Settings</td>
<td>(registry) HKEY_CURRENT_USER\Software\Khronos\Vulkan\Settings</td>
<td>(registry) HKEY_CURRENT_USER\Software\Khronos\Vulkan\LoaderSettings</td>
</tr>
<tr>
<td>VkConfig Configuration Settings</td>
Expand Down
105 changes: 80 additions & 25 deletions loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ set(OPT_LOADER_SRCS dev_ext_trampoline.c phys_dev_ext.c)

set(ASM_FAILURE_MSG "Support for unknown physical device and device functions is disabled due to missing the required assembly support code. \
To support unknown functions, assembly must be added for the platform.\n")
set(ARMASM_CMAKE_FAILURE_MSG "Support for unknown physical device and device functions is disabled due to the CMake version ${CMAKE_VERSION} \
being older than 3.26. Please update CMake to version 3.26 or newer.\n")

# Check for assembler support
if(WIN32 AND NOT USE_GAS)
Expand All @@ -135,49 +137,91 @@ if(WIN32 AND NOT USE_GAS)
endif()
endif()
if (USE_MASM)
enable_language(ASM_MASM)
if(SYSTEM_PROCESSOR MATCHES "arm")
if(CMAKE_VERSION VERSION_LESS "3.26.0")
set(ASM_FAILURE_MSG ${ARMASM_CMAKE_FAILURE_MSG})
else()
enable_language(ASM_MARMASM)
set(LOADER_ASM_DIALECT "MARMASM")
endif()
else()
enable_language(ASM_MASM)
set(LOADER_ASM_DIALECT "MASM")
endif()
endif()

# Test if the detected compiler actually works.
# Unfortunately, CMake's detection of ASM_MASM is not reliable, so we need to do this ourselves.
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
if(SYSTEM_PROCESSOR MATCHES "arm")
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
test_func FUNCTION
mov r0, #0
bx lr
ENDFUNC
END
]=])
else()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
test_func FUNCTION
mov x1, 26
ldr x0, [x0, x1]
ENDFUNC
END
]=])
endif()
else()
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
.model flat
.code
extrn _start:near
xor eax, eax
ret
end
]=])
else()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
else()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm [=[
.code
extrn start:near
xor rax, rax
ret
end
]=])
endif()
endif ()

if(MINGW)
set(CMAKE_ASM_MASM_FLAGS ${CMAKE_ASM_MASM_FLAGS} ${JWASM_FLAGS})
elseif(NOT CMAKE_CL_64 AND NOT JWASM_FOUND AND CMAKE_SIZEOF_VOID_P EQUAL 4) # /safeseh is only needed in x86
set(CMAKE_ASM_MASM_FLAGS ${CMAKE_ASM_MASM_FLAGS} /safeseh)
set(CMAKE_ASM_MARMASM_FLAGS ${CMAKE_ASM_MARMASM_FLAGS} ${JWASM_FLAGS})
elseif(NOT CMAKE_CL_64 AND NOT JWASM_FOUND AND CMAKE_SIZEOF_VOID_P EQUAL 4)
set(CMAKE_ASM_MASM_FLAGS ${CMAKE_ASM_MASM_FLAGS} /safeseh) # /safeseh is only needed in x86
endif()

# try_compile does not work here due to the same reasons as static above.
execute_process(COMMAND ${CMAKE_ASM_MASM_COMPILER} ${CMAKE_ASM_MASM_FLAGS} -c -Fo ${CMAKE_CURRENT_BINARY_DIR}/masm_check.obj ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm
RESULT_VARIABLE CMAKE_ASM_MASM_COMPILER_WORKS
OUTPUT_QUIET ERROR_QUIET)
if(SYSTEM_PROCESSOR MATCHES "arm")
execute_process(COMMAND ${CMAKE_ASM_MARMASM_COMPILER} ${CMAKE_ASM_MARMASM_FLAGS} ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm
RESULT_VARIABLE ASM_COMPILER_WORKS
OUTPUT_QUIET ERROR_QUIET)
else()
execute_process(COMMAND ${CMAKE_ASM_MASM_COMPILER} ${CMAKE_ASM_MASM_FLAGS} -c -Fo ${CMAKE_CURRENT_BINARY_DIR}/masm_check.obj ${CMAKE_CURRENT_BINARY_DIR}/masm_check.asm
RESULT_VARIABLE ASM_COMPILER_WORKS
OUTPUT_QUIET ERROR_QUIET)
endif()

# Convert the return code to a boolean
if(CMAKE_ASM_MASM_COMPILER_WORKS EQUAL 0)
set(CMAKE_ASM_MASM_COMPILER_WORKS true)
if(ASM_COMPILER_WORKS EQUAL 0)
set(ASM_COMPILER_WORKS true)
else()
set(CMAKE_ASM_MASM_COMPILER_WORKS false)
set(ASM_COMPILER_WORKS false)
endif()
if(CMAKE_ASM_MASM_COMPILER_WORKS)

if(ASM_COMPILER_WORKS)
add_executable(asm_offset asm_offset.c)
target_link_libraries(asm_offset PRIVATE loader_specific_options)
# If am emulator is provided (Like Wine), or running on native, run asm_offset to generate gen_defines.asm
if (CMAKE_CROSSCOMPILING_EMULATOR OR NOT CMAKE_CROSSCOMPILING)
add_custom_command(OUTPUT gen_defines.asm DEPENDS asm_offset COMMAND asm_offset MASM)
add_custom_command(OUTPUT gen_defines.asm DEPENDS asm_offset COMMAND asm_offset ${LOADER_ASM_DIALECT})
else()
# Forces compiler to write the intermediate asm file, needed so that we can get sizeof/offset of info out of it.
target_compile_options(asm_offset PRIVATE "/Fa$<TARGET_FILE_DIR:asm_offset>/asm_offset.asm" /FA)
Expand All @@ -188,14 +232,18 @@ end
# Run parse_asm_values.py on asm_offset's assembly file to generate the gen_defines.asm, which the asm code depends on
add_custom_command(TARGET asm_offset POST_BUILD
COMMAND Python3::Interpreter ${PROJECT_SOURCE_DIR}/scripts/parse_asm_values.py "${CMAKE_CURRENT_BINARY_DIR}/gen_defines.asm"
"$<TARGET_FILE_DIR:asm_offset>/asm_offset.asm" "MASM" "${CMAKE_C_COMPILER_ID}" "${CMAKE_SYSTEM_PROCESSOR}"
"$<TARGET_FILE_DIR:asm_offset>/asm_offset.asm" "${LOADER_ASM_DIALECT}" "${CMAKE_C_COMPILER_ID}" "${SYSTEM_PROCESSOR}"
BYPRODUCTS gen_defines.asm
)
endif()
add_custom_target(loader_asm_gen_files DEPENDS gen_defines.asm)
set_target_properties(loader_asm_gen_files PROPERTIES FOLDER ${LOADER_HELPER_FOLDER})

add_library(loader-unknown-chain STATIC unknown_ext_chain_masm.asm)
if(SYSTEM_PROCESSOR MATCHES "arm")
add_library(loader-unknown-chain STATIC unknown_ext_chain_marmasm.asm)
else()
add_library(loader-unknown-chain STATIC unknown_ext_chain_masm.asm)
endif()
target_include_directories(loader-unknown-chain PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
add_dependencies(loader-unknown-chain loader_asm_gen_files)
set(UNKNOWN_FUNCTIONS_SUPPORTED ON)
Expand All @@ -205,12 +253,10 @@ end
set(CMAKE_ASM_MASM_CREATE_STATIC_LIBRARY "<CMAKE_AR> /OUT:<TARGET> <LINK_FLAGS> <OBJECTS>")
endif()
else()
message(WARNING "Could not find working MASM assembler\n${ASM_FAILURE_MSG}")
message(WARNING "Could not find working ${} assembler\n${ASM_FAILURE_MSG}")
endif()
elseif(UNIX OR MINGW OR (WIN32 AND USE_GAS)) # i.e.: Linux & Apple & MinGW & Windows using Clang-CL

string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" SYSTEM_PROCESSOR)

option(USE_GAS "Use GAS" ON)
if(USE_GAS)
if (APPLE_UNIVERSAL_BINARY)
Expand All @@ -233,12 +279,17 @@ elseif(UNIX OR MINGW OR (WIN32 AND USE_GAS)) # i.e.: Linux & Apple & MinGW & Win
endif()

if(ASSEMBLER_WORKS)
set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain_gas_aarch64.S)
set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain_gas_aarch.S)
endif()
elseif (${SYSTEM_PROCESSOR} MATCHES "aarch64|arm64")
try_compile(ASSEMBLER_WORKS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/asm_test_aarch64.S OUTPUT_VARIABLE TRY_COMPILE_OUTPUT)
if(ASSEMBLER_WORKS)
set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain_gas_aarch64.S)
set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain_gas_aarch.S)
endif()
elseif (${SYSTEM_PROCESSOR} MATCHES "aarch32|armhf")
try_compile(ASSEMBLER_WORKS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/asm_test_aarch32.S OUTPUT_VARIABLE TRY_COMPILE_OUTPUT)
if(ASSEMBLER_WORKS)
set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain_gas_aarch.S)
endif()
# Covers x86_64, amd64, x86, i386, i686, I386, I686
elseif(${SYSTEM_PROCESSOR} MATCHES "amd64|86")
Expand All @@ -255,13 +306,16 @@ elseif(UNIX OR MINGW OR (WIN32 AND USE_GAS)) # i.e.: Linux & Apple & MinGW & Win
endif()

# When compiling for x86 on x64, we can't use CMAKE_SYSTEM_PROCESSOR to determine which architecture to use,
# Instead, check the size of void* and if its 4, set ASM_OFFSET_SYSTEM_PROCESSOR to x86
# Note - there is no 32 bit arm assembly code, so this only applies to x86 currently.
# Instead, check the size of void* and if its 4, set ASM_OFFSET_SYSTEM_PROCESSOR to x86 if we aren't on arm
if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
set(ASM_OFFSET_SYSTEM_PROCESSOR ${SYSTEM_PROCESSOR}) # x86_64 or aarch64/arm64
string(REPLACE amd64 x86_64 ASM_OFFSET_SYSTEM_PROCESSOR "${ASM_OFFSET_SYSTEM_PROCESSOR}")
else()
set(ASM_OFFSET_SYSTEM_PROCESSOR "x86")
if(${SYSTEM_PROCESSOR} MATCHES "86")
set(ASM_OFFSET_SYSTEM_PROCESSOR "x86")
else()
set(ASM_OFFSET_SYSTEM_PROCESSOR ${SYSTEM_PROCESSOR})
endif()
endif()

if(ASSEMBLER_WORKS)
Expand Down Expand Up @@ -293,6 +347,7 @@ elseif(UNIX OR MINGW OR (WIN32 AND USE_GAS)) # i.e.: Linux & Apple & MinGW & Win
else()
message(FATAL_ERROR "C_COMPILER_ID not supported!")
endif()
message(STATUS "CMAKE_CROSSCOMPILING FALSE")

find_package(Python3 REQUIRED QUIET)
# Run parse_asm_values.py on asm_offset's assembly file to generate the gen_defines.asm, which the asm code depends on
Expand Down
Loading

0 comments on commit 35af4ef

Please sign in to comment.