From cb87ff891c9968115d151a54e93695da0ac4df02 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Tue, 31 Oct 2017 11:48:43 -0400 Subject: [PATCH] Checksum the project.py file to validate that the pickle we get from the project cache is still valid. This will rebuild all project if project.py changes. Not the best solution but is good enough to prevent corruption. Signed-off-by: Jonathan Rajotte --- lttng_ivc/settings.py | 3 +++ lttng_ivc/utils/ProjectFactory.py | 12 ++++++++++-- lttng_ivc/utils/project.py | 5 +++++ lttng_ivc/utils/utils.py | 9 +++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lttng_ivc/settings.py b/lttng_ivc/settings.py index 7c9e3e4..bad61cf 100644 --- a/lttng_ivc/settings.py +++ b/lttng_ivc/settings.py @@ -15,6 +15,9 @@ apps_folder = os.path.join(base_dir, "apps") apps_gen_events_folder = os.path.join(apps_folder, "gen_ust_events") apps_preload_provider_folder = os.path.join(apps_folder, "preload_provider") +# Used for checksum validation +project_py_file_location = os.path.join(base_dir, "utils/project.py") + tmp_object_prefix = "lttng-ivc-" default_babeltrace = "babeltrace-1.5" diff --git a/lttng_ivc/utils/ProjectFactory.py b/lttng_ivc/utils/ProjectFactory.py index 5daba55..f45b786 100644 --- a/lttng_ivc/utils/ProjectFactory.py +++ b/lttng_ivc/utils/ProjectFactory.py @@ -6,6 +6,7 @@ import pickle import lttng_ivc.utils.project as Project import lttng_ivc.settings as Settings +from lttng_ivc.utils.utils import sha256_checksum _logger = logging.getLogger('project.factory') _project_constructor = { @@ -17,6 +18,8 @@ _project_constructor = { __projects_cache = {} +_project_py_checksum = sha256_checksum(Settings.project_py_file_location) + _markers = None with open(Settings.run_configuration_file, 'r') as stream: # This is voluntary static across calls, no need to perform this @@ -38,10 +41,15 @@ def get_fresh(label, tmpdir): def _validate_pickle(pickle, label): _logger.debug("Checking validate for {} {}".format(pickle, - label)) + label)) + if pickle._py_file_checksum != _project_py_checksum: + _logger.warn("Project py file changed".format(pickle.label, + label)) + return False + if pickle.label != label: _logger.warn("Label {} and {} are not the same".format(pickle.label, - label)) + label)) return False if pickle.sha1 != _markers[label]['sha1']: _logger.warn("Sha1 {} and {} are not the same".format(pickle.sha1, diff --git a/lttng_ivc/utils/project.py b/lttng_ivc/utils/project.py index 7e99571..f508c31 100644 --- a/lttng_ivc/utils/project.py +++ b/lttng_ivc/utils/project.py @@ -3,6 +3,9 @@ import shutil import git import subprocess import logging +import lttng_ivc.settings as Settings + +from lttng_ivc.utils.utils import sha256_checksum _logger = logging.getLogger('project') @@ -23,7 +26,9 @@ class Project(object): """ A collection of Project dependencies """ self.dependencies = {} + # used for project cache and pickle validation self._immutable = False + self._py_file_checksum = sha256_checksum(Settings.project_py_file_location) # State self.isBuilt = False diff --git a/lttng_ivc/utils/utils.py b/lttng_ivc/utils/utils.py index aedb1fd..23ce4bc 100644 --- a/lttng_ivc/utils/utils.py +++ b/lttng_ivc/utils/utils.py @@ -1,4 +1,5 @@ import signal +import hashlib def line_count(file_path): line_count = 0 @@ -8,6 +9,14 @@ def line_count(file_path): return line_count +def sha256_checksum(filename, block_size=65536): + sha256 = hashlib.sha256() + with open(filename, 'rb') as f: + for block in iter(lambda: f.read(block_size), b''): + sha256.update(block) + return sha256.hexdigest() + + def __dummy_sigusr1_handler(): pass -- 2.34.1