Skip to content

[lldb][test] Disable PIE for some API tests #93808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024

Conversation

dzhidzhoev
Copy link
Member

@dzhidzhoev dzhidzhoev commented May 30, 2024

When PIE is enabled on a platform by default, these tests fail since the target variable command can't read a global string variable value before running an inferior process.

It fixes the following tests when built with clang on Ubuntu aarch64:

commands/target/basic/TestTargetCommand.py
lang/c/global_variables/TestGlobalVariables.py
lang/cpp/char8_t/TestCxxChar8_t.py

@dzhidzhoev dzhidzhoev requested review from rupprecht and dcci May 30, 2024 11:29
@dzhidzhoev dzhidzhoev self-assigned this May 30, 2024
@dzhidzhoev dzhidzhoev requested a review from JDevlieghere as a code owner May 30, 2024 11:29
@llvmbot llvmbot added the lldb label May 30, 2024
@llvmbot
Copy link
Member

llvmbot commented May 30, 2024

@llvm/pr-subscribers-lldb

Author: Vladislav Dzhidzhoev (dzhidzhoev)

Changes

When PIE is enabled on a platform by default, these tests fail since the target variable command can't read a global string variable value before running an inferior process.


Full diff: https://github.com/llvm/llvm-project/pull/93808.diff

3 Files Affected:

  • (modified) lldb/test/API/commands/target/basic/Makefile (+4)
  • (modified) lldb/test/API/lang/c/global_variables/Makefile (+3)
  • (modified) lldb/test/API/lang/cpp/char8_t/Makefile (+3)
diff --git a/lldb/test/API/commands/target/basic/Makefile b/lldb/test/API/commands/target/basic/Makefile
index b31e594019f6f..e66971834b689 100644
--- a/lldb/test/API/commands/target/basic/Makefile
+++ b/lldb/test/API/commands/target/basic/Makefile
@@ -3,4 +3,8 @@
 # C_SOURCES := b.c
 # EXE := b.out
 
+ifndef PIE
+	LDFLAGS := -no-pie
+endif
+
 include Makefile.rules
diff --git a/lldb/test/API/lang/c/global_variables/Makefile b/lldb/test/API/lang/c/global_variables/Makefile
index 7b94b6556f254..00c2557033d81 100644
--- a/lldb/test/API/lang/c/global_variables/Makefile
+++ b/lldb/test/API/lang/c/global_variables/Makefile
@@ -2,5 +2,8 @@ C_SOURCES := main.c
 
 DYLIB_NAME := a
 DYLIB_C_SOURCES := a.c
+ifndef PIE
+	LDFLAGS := -no-pie
+endif
 
 include Makefile.rules
diff --git a/lldb/test/API/lang/cpp/char8_t/Makefile b/lldb/test/API/lang/cpp/char8_t/Makefile
index e7c9938b5a85e..28f982a0078d8 100644
--- a/lldb/test/API/lang/cpp/char8_t/Makefile
+++ b/lldb/test/API/lang/cpp/char8_t/Makefile
@@ -1,4 +1,7 @@
 CXX_SOURCES := main.cpp
 CXXFLAGS_EXTRAS := -std=c++2a -fchar8_t
+ifndef PIE
+	LDFLAGS := -no-pie
+endif
 
 include Makefile.rules

@dzhidzhoev dzhidzhoev requested a review from labath May 30, 2024 13:31
@labath
Copy link
Collaborator

labath commented May 30, 2024

What do the failures look like? I tried to force -pie on some of the tests that you modified, and I couldn't get them to fail. Is this somehow specific to remote execution?

@dzhidzhoev
Copy link
Member Author

dzhidzhoev commented May 30, 2024

What do the failures look like? I tried to force -pie on some of the tests that you modified, and I couldn't get them to fail. Is this somehow specific to remote execution?

No, it seems not to be related to the remote test execution.
These tests fail on my setup when they're compiled with mainline clang on Aarch64 Ubuntu:

commands/target/basic/TestTargetCommand.py
lang/c/global_variables/TestGlobalVariables.py
lang/cpp/char8_t/TestCxxChar8_t.py

cmake command line looks like this:

cmake -G Ninja -DLLVM_CCACHE_BUILD=On \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_TARGETS_TO_BUILD=AArch64 \
  -DLLVM_ENABLE_PROJECTS="llvm;clang;clang-tools-extra;lld;lldb" \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" \
  -DCMAKE_INSTALL_PREFIX=/home/user/llvm-install \
  -DLLVM_ENABLE_ASSERTIONS=On \
  -DLLDB_TEST_ARCH=aarch64 \
  /home/user/llvm-project/llvm

For example, TestTargetCommand.py fails with the error:

======================================================================
FAIL: test_target_variable_command_dwarf (TestTargetCommand.targetCommandTestCase.test_target_variable_command_dwarf)
    Test 'target variable' command before and after starting the inferior.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/user/llvm-project/lldb/packages/Python/lldbsuite/test/lldbtest.py", line 1760, in test_method
    return attrvalue(self)
           ^^^^^^^^^^^^^^^
  File "/home/user/llvm-project/lldb/test/API/commands/target/basic/TestTargetCommand.py", line 51, in test_target_variable_command
    self.do_target_variable_command("globals")
  File "/home/user/llvm-project/lldb/test/API/commands/target/basic/TestTargetCommand.py", line 169, in do_target_variable_command
    self.expect(
  File "/home/user/llvm-project/lldb/packages/Python/lldbsuite/test/lldbtest.py", line 2466, in expect
    self.fail(log_msg)
AssertionError: Ran command:
"target variable my_global_str"

Got output:
(const char *) my_global_str = 0x0000000000000000

Expecting sub string: "my_global_str" (was found)
Expecting sub string: ""abc"" (was not found)
Variable(s) displayed correctly
Config=aarch64-/home/user/llvm-build-local-release/bin/clang

This test fails when it tries to evaluate the global variable my_global_str of char* type before running the executable. LLDB can't decode its value before dynamic relocations are resolved in the binary. These relocations show up when PIE is enabled. And, for clang, it seems to be enabled by default on linux aarch64.
The other 2 tests fail for the same reason. Thus the compilation flag is proposed for turning PIE off for these tests unless it is enforced externally by the PIE make flag.

@labath
Copy link
Collaborator

labath commented May 31, 2024

Ok, I see now. It's an ld.bfd vs ld.lld thing. You probably have your clang configured to use lld. LLD does not put relocation addends into the data section (on both arm and intel). ld.bfd does, which is why this sort of happens to work there. Was your intention to test with LLD?

@dzhidzhoev
Copy link
Member Author

Ok, I see now. It's an ld.bfd vs ld.lld thing. You probably have your clang configured to use lld. LLD does not put relocation addends into the data section (on both arm and intel). ld.bfd does, which is why this sort of happens to work there. Was your intention to test with LLD?

Yep, I run it with lld built together with clang and lldb. Should the condition be narrowed to affect only builds with lld?

@labath
Copy link
Collaborator

labath commented Jun 3, 2024

Ok, I see now. It's an ld.bfd vs ld.lld thing. You probably have your clang configured to use lld. LLD does not put relocation addends into the data section (on both arm and intel). ld.bfd does, which is why this sort of happens to work there. Was your intention to test with LLD?

Yep, I run it with lld built together with clang and lldb. Should the condition be narrowed to affect only builds with lld?

No, I think this is fine. I don't believe we have the ability to detect the linker used at the moment, and I'd like to avoid adding new dimensions to the test suite. Plus, the PIE flag captures the problem (that lldb depends on linker-specific relocation behavior, at least for ELF) these test expose fairly well. To fix this, we'd need to change ObjectFileELF::RelocateSection to relocate non-debug info sections as well. It may be worth linking these comments to a bug that provides more context.

It's possible this could break some targets (like android) that do not support building/running non-PIE executables, but if that happens, we should just get those targets to set the PIE flag.

@@ -3,4 +3,8 @@
# C_SOURCES := b.c
# EXE := b.out

ifndef PIE
LDFLAGS := -no-pie
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting LD_EXTRAS is the typical way to pass linker flags from here (the same for other files)

These tests fail when PIE is enabled by default on a platform since
`target variable` can't read global string variable value before
running inferior.
@dzhidzhoev dzhidzhoev force-pushed the lldb/no-pie-per-test branch from d305048 to eaf50dc Compare June 12, 2024 15:09
@dzhidzhoev dzhidzhoev merged commit f6a2ca4 into llvm:main Jun 12, 2024
4 of 5 checks passed
@felipepiovezan
Copy link
Contributor

hi @dzhidzhoev, I think this commit is causing the arm lldb incremental bots to fail.
Could you have a look or revert if you think the fix is not obvious? This is our most important bot, so it would be nice to get it green again
https://green.lab.llvm.org/job/llvm.org/view/LLDB/job/as-lldb-cmake/5651/console

@felipepiovezan
Copy link
Contributor

felipepiovezan commented Jun 12, 2024

@dzhidzhoev I think @labath 's suggestion might fix this (LD_EXTRAS)
(trying to test locally)

felipepiovezan added a commit that referenced this pull request Jun 12, 2024
This is a fixup to #93808,
which used LDFLAGS instead of the correct LD_EXTRAS
@felipepiovezan
Copy link
Contributor

Pushed a fix

@labath
Copy link
Collaborator

labath commented Jun 13, 2024

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants