Skip to content
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

Revert "[analyzer] Use absolute path to logger.so in LD_PRELOAD" #4314

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions analyzer/codechecker_analyzer/analyzer_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
from argparse import ArgumentTypeError

import os
import platform
import sys

from pathlib import Path

from codechecker_analyzer.arg import analyzer_binary
from codechecker_common import logger
from codechecker_common.checker_labels import CheckerLabels
Expand Down Expand Up @@ -67,18 +64,12 @@ def __init__(self):
self.__analyzers = {}
self.__analyzer_env = None

machine = platform.uname().machine

self.logger_lib_dir_path = os.path.join(
self._data_files_dir_path, 'ld_logger', 'lib', machine)
self._data_files_dir_path, 'ld_logger', 'lib')

if not os.path.exists(self.logger_lib_dir_path):
self.logger_lib_dir_path = os.path.join(
self._lib_dir_path,
'codechecker_analyzer',
'ld_logger',
'lib',
machine)
self._lib_dir_path, 'codechecker_analyzer', 'ld_logger', 'lib')

self.logger_bin = None
self.logger_file = None
Expand Down Expand Up @@ -281,12 +272,8 @@ def path_logger_bin(self):
return os.path.join(self._bin_dir_path, 'ld_logger')

@property
def logger_lib_path(self):
"""
Returns the absolute path to the logger library.
"""
return str(Path(self.logger_lib_dir_path,
self.logger_lib_name).absolute())
def path_logger_lib(self):
return self.logger_lib_dir_path

@property
def logger_lib_name(self):
Expand Down
13 changes: 12 additions & 1 deletion analyzer/codechecker_analyzer/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ def get_log_env(logfile, original_env):

new_env[context.env_var_cc_logger_bin] = context.path_logger_bin

new_env['LD_PRELOAD'] = context.logger_lib_path
if 'LD_PRELOAD' in new_env:
new_env['LD_PRELOAD'] = new_env['LD_PRELOAD'] \
+ " " + context.logger_lib_name
else:
new_env['LD_PRELOAD'] = context.logger_lib_name

try:
original_ld_library_path = new_env['LD_LIBRARY_PATH']
new_env['LD_LIBRARY_PATH'] = context.path_logger_lib + ':' + \
original_ld_library_path
except KeyError:
new_env['LD_LIBRARY_PATH'] = context.path_logger_lib

# Set ld logger logfile.
new_env[context.env_var_cc_logger_file] = logfile
Expand Down
23 changes: 0 additions & 23 deletions analyzer/tests/unit/test_log_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

import json
import os
import platform
import shutil
import tempfile
import unittest

from codechecker_analyzer.buildlog import log_parser
from codechecker_analyzer.env import get_log_env
from codechecker_common.skiplist_handler import SkipListHandler, \
SkipListHandlers
from codechecker_common.util import load_json
Expand Down Expand Up @@ -664,24 +662,3 @@ def test_symlink(self):

self.assertEqual(len(build_actions), 3)
self.assertEqual(build_action.source, file_c_symdir)

def test_get_log_env(self):
"""
Test if get_log_env returns the correct environment
with LD_PRELOAD set to pointing to the correct directory
of the ldlogger.so lib.
"""
log_file = os.path.join(self.tmp_dir, "compile_commands.json")
original_env = os.environ.copy()
# If this asset fails, make sure that you don't have LD_PRELOAD set
# in your environment.
self.assertNotIn("LD_PRELOAD", original_env)
env = get_log_env(log_file, original_env)

# The new environment should contain the LD_PRELOAD variable.
self.assertIn("LD_PRELOAD", env)

# Make sure that the test running machine architecture is in the
# LD_PRELOAD path.
machine = platform.uname().machine
self.assertTrue(env["LD_PRELOAD"].endswith(machine + "/ldlogger.so"))
3 changes: 2 additions & 1 deletion analyzer/tools/build-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ make all test

Set the following environment variables:
~~~~~~~
export LD_PRELOAD=`pwd`/build/lib/`uname -m`/ldlogger.so
export LD_PRELOAD=ldlogger.so
export LD_LIBRARY_PATH=`pwd`/build/lib:$LD_LIBRARY_PATH
export CC_LOGGER_GCC_LIKE="gcc:g++:clang:clang++:/cc:c++"
# The output compilation JSON file.
export CC_LOGGER_FILE=`pwd`/compilation.json
Expand Down
7 changes: 2 additions & 5 deletions analyzer/tools/build-logger/tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ def read_actual_json(self) -> str:
return fd.read()

def get_envvars(self) -> Mapping[str, str]:
machine = platform.uname().machine
return {
"PATH": os.getenv("PATH"),
"LD_PRELOAD": os.path.join(LOGGER_DIR,
"lib",
machine,
"ldlogger.so"),
"LD_PRELOAD": "ldlogger.so",
"LD_LIBRARY_PATH": os.path.join(LOGGER_DIR, "lib"),
"CC_LOGGER_GCC_LIKE": "gcc:g++:clang:clang++:/cc:c++",
"CC_LOGGER_FILE": self.logger_file,
"CC_LOGGER_DEBUG_FILE": self.logger_debug_file,
Expand Down
10 changes: 10 additions & 0 deletions docs/analyzer/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ object files as input) should be captured. For further details see
[this documentation](/analyzer/tools/build-logger/README.md).


If your build tool overrides `LD_LIBRARY_PATH` during the build process, then
`ldlogger.so` will not be found. The best solution is to making sure
that the LD_LIBRARY_PATH is not overridden, only extended.
If this is not possible, you can work around the situation by
specifying the absolute path of the `ldlogger.so` in the `LD_PRELOAD`:

```sh
LD_PRELOAD=<CODECHECKER_DIR>/ld_logger/lib/x86_64/ldlogger.so CodeChecker log -o compile_commands.json -b "make -j2"
```

#### Change user inside the build command
If we change user inside the build command of the CodeChecker log command
before the actual compiler invocation, the compilation database will be empty:
Expand Down
Loading