Skip to content

Commit

Permalink
Pad parition-alloc TLS segment
Browse files Browse the repository at this point in the history
  • Loading branch information
rinon committed Mar 13, 2024
1 parent 1c24fb0 commit c479da0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
38 changes: 21 additions & 17 deletions cmake/ia2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,7 @@ function(add_ia2_compartment NAME TYPE)
elseif("${ARG_PKEY}" GREATER "0")
set(UNPADDED_LIB ${NAME}_unpadded)
add_library(${UNPADDED_LIB} SHARED)
# Add command and target to generate the padded, sonamed library
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib${NAME}.so
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${UNPADDED_LIB}> ${CMAKE_CURRENT_BINARY_DIR}/lib${NAME}.so
COMMAND pad-tls --allow-no-tls ${CMAKE_CURRENT_BINARY_DIR}/lib${NAME}.so
DEPENDS pad-tls $<TARGET_FILE:${UNPADDED_LIB}>
COMMENT "Padding TLS segment of wrapped library"
)
add_custom_target(${NAME}-padding DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/lib${NAME}.so")
add_library(${NAME} SHARED IMPORTED)
set_property(TARGET ${NAME} PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lib${NAME}.so")
add_dependencies(${NAME} ${NAME}-padding)

set_target_properties(${UNPADDED_LIB} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/unpadded"
OUTPUT_NAME "${NAME}"
)
pad_tls_library(${UNPADDED_LIB} ${NAME})
set(NAME ${UNPADDED_LIB})
else()
add_library(${NAME} SHARED)
Expand Down Expand Up @@ -105,6 +89,26 @@ function(relative_to_absolute OUTPUT BASEDIR)
set(${OUTPUT} ${paths} PARENT_SCOPE)
endfunction()

function(pad_tls_library INPUT OUTPUT)
# Add command and target to generate the padded, sonamed library
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lib${OUTPUT}.so
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${INPUT}> ${CMAKE_CURRENT_BINARY_DIR}/lib${OUTPUT}.so
COMMAND pad-tls --allow-no-tls ${CMAKE_CURRENT_BINARY_DIR}/lib${OUTPUT}.so
DEPENDS pad-tls $<TARGET_FILE:${INPUT}>
COMMENT "Padding TLS segment of wrapped library"
)
add_custom_target(${OUTPUT}-padding DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/lib${OUTPUT}.so")
add_library(${OUTPUT} SHARED IMPORTED GLOBAL)
set_property(TARGET ${OUTPUT} PROPERTY IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lib${OUTPUT}.so")
add_dependencies(${OUTPUT} ${OUTPUT}-padding)

set_target_properties(${INPUT} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/unpadded"
OUTPUT_NAME "${OUTPUT}"
)
endfunction()

# Create a fake target that builds the given sources
#
# We use this target to provide the input compile_commands.json for the source
Expand Down
20 changes: 13 additions & 7 deletions runtime/partition-alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,37 +65,43 @@ set(PA_SRCS

list(TRANSFORM PA_SRCS PREPEND ${EXTERNAL_DIR}/chromium/src/base/allocator/partition_allocator/)

add_library(partition-alloc SHARED
add_library(partition-alloc_unpadded SHARED
src/allocator_shim_default_dispatch_to_partition_alloc.cc
src/get_pkey.cc
src/shared_allocator.cc
${PA_SRCS})

pad_tls_library(partition-alloc_unpadded partition-alloc)

target_include_directories(partition-alloc BEFORE
INTERFACE
INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
)

target_include_directories(partition-alloc_unpadded BEFORE
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
PRIVATE
${EXTERNAL_DIR}/chromium/src)

target_compile_options(partition-alloc
target_compile_options(partition-alloc_unpadded
PRIVATE
"-fPIC"
"-std=c++17"
"-Wno-invalid-offsetof"
"-Wno-return-type"
)
target_compile_options(partition-alloc
target_compile_options(partition-alloc_unpadded
PUBLIC
"-fno-exceptions"
)
target_link_options(partition-alloc
target_link_options(partition-alloc_unpadded
PRIVATE
"-Wl,-z,now"
)

# Wrapping options from upstream BUILD.gn
target_link_options(partition-alloc
target_link_options(partition-alloc_unpadded
PUBLIC
"-Wl,-wrap,calloc"
"-Wl,-wrap,free"
Expand Down

0 comments on commit c479da0

Please sign in to comment.