fixed security and non-security issues of Profiler Python code andtags/v1.0.0
| @@ -19,6 +19,7 @@ import os | |||||
| from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | ||||
| from mindinsight.profiler.common.log import logger | from mindinsight.profiler.common.log import logger | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class AicoreTypeAnalyser(BaseAnalyser): | class AicoreTypeAnalyser(BaseAnalyser): | ||||
| @@ -42,6 +43,11 @@ class AicoreTypeAnalyser(BaseAnalyser): | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_aicore_type_time.format(self._device_id) | self._file_name_aicore_type_time.format(self._device_id) | ||||
| ) | ) | ||||
| op_type_file_path = validate_and_normalize_path( | |||||
| op_type_file_path, raise_key='Invalid aicore_type file path.' | |||||
| ) | |||||
| if not os.path.isfile(op_type_file_path): | if not os.path.isfile(op_type_file_path): | ||||
| logger.warning('The file <%s> does not exist.', op_type_file_path) | logger.warning('The file <%s> does not exist.', op_type_file_path) | ||||
| return | return | ||||
| @@ -161,6 +167,13 @@ class AicoreDetailAnalyser(BaseAnalyser): | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_framework_info.format(self._device_id) | self._file_name_framework_info.format(self._device_id) | ||||
| ) | ) | ||||
| op_detail_file_path = validate_and_normalize_path( | |||||
| op_detail_file_path, raise_key='Invalid aicore_detail file path.' | |||||
| ) | |||||
| framework_file_path = validate_and_normalize_path( | |||||
| framework_file_path, raise_key='Invalid framework file path.' | |||||
| ) | |||||
| if not os.path.isfile(op_detail_file_path): | if not os.path.isfile(op_detail_file_path): | ||||
| logger.warning('The file <%s> does not exist.', op_detail_file_path) | logger.warning('The file <%s> does not exist.', op_detail_file_path) | ||||
| return | return | ||||
| @@ -283,6 +296,9 @@ class AicpuAnalyser(BaseAnalyser): | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_aicpu_time.format(self._device_id) | self._file_name_aicpu_time.format(self._device_id) | ||||
| ) | ) | ||||
| aicpu_file_path = validate_and_normalize_path( | |||||
| aicpu_file_path, raise_key='Invalid aicpu file path.' | |||||
| ) | |||||
| if not os.path.isfile(aicpu_file_path): | if not os.path.isfile(aicpu_file_path): | ||||
| logger.warning('The file <%s> does not exist.', aicpu_file_path) | logger.warning('The file <%s> does not exist.', aicpu_file_path) | ||||
| return | return | ||||
| @@ -18,6 +18,7 @@ import os | |||||
| from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | ||||
| from mindinsight.profiler.common.log import logger | from mindinsight.profiler.common.log import logger | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class GpuAnalyser(BaseAnalyser): | class GpuAnalyser(BaseAnalyser): | ||||
| @@ -30,6 +31,8 @@ class GpuAnalyser(BaseAnalyser): | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._csv_file_to_analyse.format(self._device_id) | self._csv_file_to_analyse.format(self._device_id) | ||||
| ) | ) | ||||
| op_type_file_path = validate_and_normalize_path( | |||||
| op_type_file_path, raise_key="Invalid op_type_file_path") | |||||
| if not os.path.isfile(op_type_file_path): | if not os.path.isfile(op_type_file_path): | ||||
| logger.warning('The file <%s> does not exist.', op_type_file_path) | logger.warning('The file <%s> does not exist.', op_type_file_path) | ||||
| return | return | ||||
| @@ -16,6 +16,7 @@ | |||||
| import csv | import csv | ||||
| import os | import os | ||||
| from decimal import Decimal | from decimal import Decimal | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class Integrator: | class Integrator: | ||||
| @@ -54,6 +55,8 @@ class Integrator: | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_framework.format(self._device_id) | self._file_name_framework.format(self._device_id) | ||||
| ) | ) | ||||
| framework_file = validate_and_normalize_path( | |||||
| framework_file, raise_key="Invaild framework file path.") | |||||
| if not os.path.isfile(framework_file): | if not os.path.isfile(framework_file): | ||||
| return | return | ||||
| @@ -93,6 +96,8 @@ class Integrator: | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_aicore_detail_time.format(self._device_id) | self._file_name_aicore_detail_time.format(self._device_id) | ||||
| ) | ) | ||||
| aicore_detail_file = validate_and_normalize_path( | |||||
| aicore_detail_file, raise_key="Invaild aicore_detail file path.") | |||||
| if not os.path.isfile(aicore_detail_file): | if not os.path.isfile(aicore_detail_file): | ||||
| return | return | ||||
| @@ -132,6 +137,8 @@ class Integrator: | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_aicpu_time.format(self._device_id) | self._file_name_aicpu_time.format(self._device_id) | ||||
| ) | ) | ||||
| aicpu_file = validate_and_normalize_path( | |||||
| aicpu_file, raise_key="Invaild aicpu file path.") | |||||
| if not os.path.isfile(aicpu_file): | if not os.path.isfile(aicpu_file): | ||||
| return | return | ||||
| @@ -16,7 +16,7 @@ | |||||
| import os | import os | ||||
| from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class MinddataAnalyser(BaseAnalyser): | class MinddataAnalyser(BaseAnalyser): | ||||
| """The Minddata profiling analyser.""" | """The Minddata profiling analyser.""" | ||||
| @@ -49,6 +49,8 @@ class MinddataAnalyser(BaseAnalyser): | |||||
| file_path = MinddataAnalyser.find_target_file(self._profiling_dir, file_name) | file_path = MinddataAnalyser.find_target_file(self._profiling_dir, file_name) | ||||
| if file_path: | if file_path: | ||||
| file_path = validate_and_normalize_path( | |||||
| file_path, raise_key="Invaild minddata_aicpu file path.") | |||||
| with open(file_path) as data_file: | with open(file_path) as data_file: | ||||
| for line in data_file.readlines(): | for line in data_file.readlines(): | ||||
| node_info = line.split() | node_info = line.split() | ||||
| @@ -107,6 +109,8 @@ class MinddataAnalyser(BaseAnalyser): | |||||
| file_path = self.get_device_queue_file_path() | file_path = self.get_device_queue_file_path() | ||||
| if file_path: | if file_path: | ||||
| file_path = validate_and_normalize_path( | |||||
| file_path, raise_key="Invaild device_queue file path.") | |||||
| with open(file_path) as data_file: | with open(file_path) as data_file: | ||||
| for line in data_file.readlines(): | for line in data_file.readlines(): | ||||
| op_info = line.split() | op_info = line.split() | ||||
| @@ -22,6 +22,7 @@ from mindinsight.profiler.analyser.base_analyser import BaseAnalyser | |||||
| from mindinsight.profiler.common.exceptions.exceptions import \ | from mindinsight.profiler.common.exceptions.exceptions import \ | ||||
| ProfilerPipelineOpNotExistException | ProfilerPipelineOpNotExistException | ||||
| from mindinsight.profiler.common.log import logger | from mindinsight.profiler.common.log import logger | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class MinddataPipelineAnalyser(BaseAnalyser): | class MinddataPipelineAnalyser(BaseAnalyser): | ||||
| @@ -115,6 +116,8 @@ class MinddataPipelineAnalyser(BaseAnalyser): | |||||
| self._profiling_dir, | self._profiling_dir, | ||||
| self._file_name_pipeline.format(self._device_id) | self._file_name_pipeline.format(self._device_id) | ||||
| ) | ) | ||||
| pipeline_file_path = validate_and_normalize_path( | |||||
| pipeline_file_path, raise_key="Invaild pipeline file path.") | |||||
| if not os.path.isfile(pipeline_file_path): | if not os.path.isfile(pipeline_file_path): | ||||
| logger.warning('The file <%s> does not exist.', pipeline_file_path) | logger.warning('The file <%s> does not exist.', pipeline_file_path) | ||||
| return | return | ||||
| @@ -24,6 +24,7 @@ from mindinsight.profiler.common.exceptions.exceptions import ProfilerParamValue | |||||
| from mindinsight.profiler.common.log import logger as log | from mindinsight.profiler.common.log import logger as log | ||||
| from mindinsight.profiler.common.util import query_latest_trace_time_file, get_field_value, \ | from mindinsight.profiler.common.util import query_latest_trace_time_file, get_field_value, \ | ||||
| get_summary_for_step_trace, to_millisecond | get_summary_for_step_trace, to_millisecond | ||||
| from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path | |||||
| class StepTraceAnalyser(BaseAnalyser): | class StepTraceAnalyser(BaseAnalyser): | ||||
| @@ -93,6 +94,8 @@ class StepTraceAnalyser(BaseAnalyser): | |||||
| if not file_path: | if not file_path: | ||||
| log.error("Failed to find parsed trace time file.") | log.error("Failed to find parsed trace time file.") | ||||
| raise ProfilerFileNotFoundException('parsed step trace time file') | raise ProfilerFileNotFoundException('parsed step trace time file') | ||||
| file_path = validate_and_normalize_path( | |||||
| file_path, raise_key="Invaild latest_trace_trace_time file path.") | |||||
| with open(file_path, 'r') as handle: | with open(file_path, 'r') as handle: | ||||
| csv_reader = csv.reader(handle) | csv_reader = csv.reader(handle) | ||||
| self.__column__ = next(csv_reader) | self.__column__ = next(csv_reader) | ||||
| @@ -104,6 +107,8 @@ class StepTraceAnalyser(BaseAnalyser): | |||||
| def _load_point_info(self): | def _load_point_info(self): | ||||
| """Load point info.""" | """Load point info.""" | ||||
| file_path = os.path.join(self._profiling_dir, 'step_trace_point_info.json') | file_path = os.path.join(self._profiling_dir, 'step_trace_point_info.json') | ||||
| file_path = validate_and_normalize_path( | |||||
| file_path, raise_key="Invaild step_trace_point_info file path.") | |||||
| if os.path.isfile(file_path): | if os.path.isfile(file_path): | ||||
| with open(file_path, 'r', encoding='utf-8') as file: | with open(file_path, 'r', encoding='utf-8') as file: | ||||
| try: | try: | ||||