aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/utils.py
diff options
context:
space:
mode:
authorGravatar Leo Neat <leosneat@gmail.com>2020-01-29 11:03:43 -0800
committerGravatar GitHub <noreply@github.com>2020-01-29 11:03:43 -0800
commit8ffc6db00c83e5f75e92b3c4c63c1924597711a1 (patch)
tree7cca59f92af682000c57be2e0b8ad5e926e8701f /infra/utils.py
parent4dc4c0240f96105f2330a0fc1f5f321a6e796ddb (diff)
[Infra] CIFuzz pipeline complete. (#3281)
* Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Testing action build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working build * Working fuzzers with out error surface * Working fuzzers with out error surface * Working fuzzers with out error surface * Printing std err * Adding fuzzer timeout * Adding fuzzer timeout * Changing fuzzer timeout to fuzz time * Formatting and refactoring * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Spelling in fuzz_target.py * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Upload artifact fix * Refactoring error codes. * reverting helper.py * reverting helper.py * reverting helper.py * chaning method to static * moving cifuzz file * Jonathan changes * Oliver and Jonathan comments * Oliver and Jonathan comments * Oliver and Jonathan comments * Utils unit tests * Test formatting and documentation * Build fuzzer test added * Changed repo manager errors * Unit and integration tests complete * Jonathan comments pt.1 * Jonathan comments pt.1 * Jonathan comments pt.1 * adding cifuzz_test * Build fuzzer test completed * Run fuzzers test finished. * Removed SRC dependency * Jonathan comments pt.2 * Max comments pt.1 * Max comments pt.2 * removing log specified out stream * Max comments pt.3 * Adding OSS_FUZZ_HOME env var * Jonathan comments pt.3 * Formatting * Olivers comments * Jonathan comments
Diffstat (limited to 'infra/utils.py')
-rw-r--r--infra/utils.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/infra/utils.py b/infra/utils.py
new file mode 100644
index 00000000..c2820c38
--- /dev/null
+++ b/infra/utils.py
@@ -0,0 +1,94 @@
+# Copyright 2020 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Utilities for OSS-Fuzz infrastructure."""
+
+import os
+import re
+import stat
+
+import helper
+
+ALLOWED_FUZZ_TARGET_EXTENSIONS = ['', '.exe']
+FUZZ_TARGET_SEARCH_STRING = 'LLVMFuzzerTestOneInput'
+VALID_TARGET_NAME = re.compile(r'^[a-zA-Z0-9_-]+$')
+
+
+def chdir_to_root():
+ """Changes cwd to OSS-Fuzz root directory."""
+ # Change to oss-fuzz main directory so helper.py runs correctly.
+ if os.getcwd() != helper.OSSFUZZ_DIR:
+ os.chdir(helper.OSSFUZZ_DIR)
+
+
+def is_fuzz_target_local(file_path):
+ """Returns whether |file_path| is a fuzz target binary (local path).
+ Copied from clusterfuzz src/python/bot/fuzzers/utils.py
+ with slight modifications.
+ """
+ filename, file_extension = os.path.splitext(os.path.basename(file_path))
+ if not VALID_TARGET_NAME.match(filename):
+ # Check fuzz target has a valid name (without any special chars).
+ return False
+
+ if file_extension not in ALLOWED_FUZZ_TARGET_EXTENSIONS:
+ # Ignore files with disallowed extensions (to prevent opening e.g. .zips).
+ return False
+
+ if not os.path.exists(file_path) or not os.access(file_path, os.X_OK):
+ return False
+
+ if filename.endswith('_fuzzer'):
+ return True
+
+ if os.path.exists(file_path) and not stat.S_ISREG(os.stat(file_path).st_mode):
+ return False
+
+ with open(file_path, 'rb') as file_handle:
+ return file_handle.read().find(FUZZ_TARGET_SEARCH_STRING.encode()) != -1
+
+
+def get_fuzz_targets(path):
+ """Get list of fuzz targets in a directory.
+
+ Args:
+ path: A path to search for fuzz targets in.
+
+ Returns:
+ A list of paths to fuzzers or an empty list if None.
+ """
+ if not os.path.exists(path):
+ return []
+ fuzz_target_paths = []
+ for root, _, _ in os.walk(path):
+ for filename in os.listdir(path):
+ file_path = os.path.join(root, filename)
+ if is_fuzz_target_local(file_path):
+ fuzz_target_paths.append(file_path)
+
+ return fuzz_target_paths
+
+
+def get_container_name():
+ """Gets the name of the current docker container you are in.
+ /proc/self/cgroup can be used to check control groups e.g. Docker.
+ See: https://docs.docker.com/config/containers/runmetrics/ for more info.
+
+ Returns:
+ Container name or None if not in a container.
+ """
+ with open('/proc/self/cgroup') as file_handle:
+ if 'docker' not in file_handle.read():
+ return None
+ with open('/etc/hostname') as file_handle:
+ return file_handle.read().strip()