Skip to content

Commit 8bbf186

Browse files
committed
remain in compartment 1 for iconv test
1 parent 1d9ce04 commit 8bbf186

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ include(cmake/qemu-command.cmake)
88

99
find_package(PkgConfig REQUIRED)
1010

11+
option(IA2_TRACE_EXIT "Enable IA2 exit-path tracing instrumentation" OFF)
12+
1113
set(EXTERNAL_DIR ${PROJECT_SOURCE_DIR}/external)
1214

1315
enable_testing()
@@ -38,4 +40,5 @@ ExternalProject_Add(tools
3840
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
3941
-DClang_DIR=${Clang_DIR}
4042
-DLLVM_DIR=${LLVM_DIR}
43+
-DIA2_TRACE_EXIT=${IA2_TRACE_EXIT}
4144
INSTALL_COMMAND "")

cmake/ia2.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ function(add_ia2_compartment NAME TYPE)
4949
IA2_ENABLE=1
5050
PKEY=${ARG_PKEY}
5151
)
52+
if(IA2_TRACE_EXIT)
53+
target_compile_definitions(${NAME} PRIVATE IA2_TRACE_EXIT=1)
54+
endif()
5255
set_target_properties(${NAME} PROPERTIES PKEY ${ARG_PKEY})
5356
target_compile_options(${NAME} PRIVATE
5457
"-Werror=incompatible-pointer-types"
@@ -147,6 +150,9 @@ function(create_compile_commands NAME TYPE)
147150
IA2_ENABLE=0
148151
PKEY=${ARG_PKEY}
149152
)
153+
if(IA2_TRACE_EXIT)
154+
target_compile_definitions(${COMPILE_COMMAND_TARGET} PRIVATE IA2_TRACE_EXIT=1)
155+
endif()
150156
# Copy target properties from the real target. We might need to add more properties.
151157
target_link_libraries(${COMPILE_COMMAND_TARGET} PRIVATE $<TARGET_PROPERTY:${NAME},LINK_LIBRARIES>)
152158
target_include_directories(${COMPILE_COMMAND_TARGET} PRIVATE ${INCLUDE_DIRECTORIES})

runtime/libia2/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ if(IA2_DEBUG_MEMORY)
2121
target_compile_definitions(libia2 PRIVATE IA2_DEBUG_MEMORY=1)
2222
endif()
2323

24+
if(IA2_TRACE_EXIT)
25+
target_compile_definitions(libia2 PUBLIC IA2_TRACE_EXIT=1)
26+
endif()
27+
2428
target_link_options(libia2
2529
INTERFACE
2630
"-pthread"

runtime/libia2/main.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ __asm__(
4545
"mov main_sp(%rip), %rsp\n"
4646
// Save return value
4747
"mov %rax,%r10\n"
48-
// Switch pkey to untrusted compartment
49-
"xor %ecx,%ecx\n"
50-
"xor %edx,%edx\n"
51-
"mov_pkru_eax 0\n"
52-
"wrpkru\n"
48+
// NOTE: Removed switch to compartment 0 to allow exit handlers to run
49+
// in compartment 1 (where libc lives). This prevents SEGV_PKUERR when
50+
// exit() tries to acquire __exit_funcs_lock in libc's .bss section.
51+
// See: tests/dl_debug_test/*_ANALYSIS.md for details
52+
// "xor %ecx,%ecx\n"
53+
// "xor %edx,%edx\n"
54+
// "mov_pkru_eax 0\n"
55+
// "wrpkru\n"
5356
// Restore return value
5457
"mov %r10,%rax\n"
5558
"popq %rbp\n"

tests/dl_debug_test/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,28 @@ INIT_RUNTIME(2);
1414
#define IA2_COMPARTMENT 1
1515
#include <ia2_compartment_init.inc>
1616

17+
// on_exit handler to set PKRU=0 before destructors run
18+
// on_exit handlers run BEFORE atexit handlers
19+
static void set_pkru_zero_for_exit(int status, void *arg) {
20+
(void)status;
21+
(void)arg;
22+
__asm__ volatile(
23+
"xor %%eax, %%eax\n"
24+
"xor %%ecx, %%ecx\n"
25+
"xor %%edx, %%edx\n"
26+
"wrpkru\n"
27+
::: "eax", "ecx", "edx"
28+
);
29+
}
30+
1731
void ia2_main(void) {
1832
ia2_register_compartment("main", 1, NULL);
1933
ia2_register_compartment("libdl_debug_test_lib.so", 2, NULL);
34+
35+
// Register handler to run FIRST during exit (before atexit handlers)
36+
// on_exit handlers run BEFORE atexit, which runs BEFORE destructors
37+
// Use IA2_IGNORE to prevent rewriter from wrapping this function pointer
38+
on_exit(IA2_IGNORE(&set_pkru_zero_for_exit), NULL);
2039
}
2140

2241
// Test that iconv (libc) runs in compartment 1 and _dl_debug_state inherits it

tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ cmake_minimum_required(VERSION 4.0)
22
project(tools)
33
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
44

5+
option(IA2_TRACE_EXIT "Enable IA2 exit-path tracing instrumentation" OFF)
6+
57
add_subdirectory(rewriter)
68
add_subdirectory(pad-tls)

tools/rewriter/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ target_link_libraries(ia2-rewriter PRIVATE
3535
clang-cpp
3636
LLVM
3737
)
38+
39+
if(IA2_TRACE_EXIT)
40+
target_compile_definitions(ia2-rewriter PRIVATE IA2_TRACE_EXIT=1)
41+
endif()

0 commit comments

Comments
 (0)