codeql/swift
Mathias Vorreiter Pedersen d8f53e5524
Merge pull request #14925 from geoffw0/flows
Swift: Imprecise Taint Flows
2023-12-11 10:06:01 +00:00
..
actions Swift: skip print_unextracted 2023-06-14 16:17:44 +02:00
codegen Misc: add bazel buildifer pre-commit hook 2023-06-30 13:07:04 +02:00
downgrades Swift: upgrade/downgrade for pattern types 2023-11-29 21:04:05 +00:00
extractor Merge pull request #14570 from rdmarsh2/rdmarsh2/swift/extract-pattern-types 2023-12-06 14:11:53 -05:00
integration-tests Swift: fix autobuilder bug when Xcode failure breaks the whole autobuild process 2023-12-05 09:58:33 +01:00
logging Swift: Build incompatible OS diagnostic on all platforms. 2023-06-16 17:22:43 +01:00
ql Merge pull request #14925 from geoffw0/flows 2023-12-11 10:06:01 +00:00
swift-autobuilder Swift: fix autobuilder bug when Xcode failure breaks the whole autobuild process 2023-12-05 09:58:33 +01:00
third_party Swift: upgrade to 5.9.1 2023-11-07 13:40:28 +01:00
tools Swift: fix Linux compatibility problem and fetch resource dir from toolchain 2023-10-31 16:20:58 +01:00
.clang-format Build: Bump build mode to C++20. 2023-06-08 13:10:43 +01:00
.gitignore Swift: generate raw helpers in synthesized stubs 2023-02-16 10:49:21 +01:00
.pep8 Swift: implement python schema 2022-09-21 15:53:09 +02:00
.python-version Swift: bump actions/setup-python from 3 to 4 2022-10-19 18:10:42 +02:00
BUILD.bazel Swift: rename autobuilder. NFC 2023-08-31 08:12:52 +02:00
CMakeLists.txt Swift: bump C++ version in CMake 2023-06-22 09:26:29 +02:00
README.md Update README.md 2023-10-24 15:46:32 +01:00
codegen.conf Move `swift/codegen` to `misc/codegen` 2023-02-27 09:46:48 +01:00
codeql-extractor.yml Swift: bump all versions to 0.1.0 2023-05-17 11:28:28 +02:00
create_extractor_pack.py Swift: add `qltest.sh` tests 2022-10-21 12:54:09 +02:00
prefix.dbscheme Swift: implement python schema 2022-09-21 15:53:09 +02:00
rules.bzl Swift: Build incompatible OS diagnostic on all platforms. 2023-06-16 17:22:43 +01:00
schema.py Swift: extract types for patterns 2023-11-29 20:54:50 +00:00
schema_documentation.md Swift: tweaks to the schema doc documentation 2023-02-10 08:58:07 +01:00

README.md

Swift on CodeQL

[!NOTE] CodeQL analysis for Swift is currently in beta. During the beta, analysis of Swift code, and the accompanying documentation, will not be as comprehensive as for other languages.

Development

Building the Swift extractor

First ensure you have Bazel installed, for example with

brew install bazelisk

then from the ql directory run

bazel run //swift:create-extractor-pack    # --cpu=darwin_x86_64 # Uncomment on Arm-based Macs

which will install swift/extractor-pack.

Notice you can run bazel run :create-extractor-pack if you already are in the swift directory.

Using codeql ... --search-path=swift/extractor-pack will then pick up the Swift extractor. You can also use --search-path=., as the extractor pack is mentioned in the root codeql-workspace.yml. Alternatively, you can set up the search path in the per-user CodeQL configuration file .

Code generation

Run

bazel run //swift/codegen

to update generated files. This can be shortened to bazel run codegen if you are in the swift directory.

You can also run ../misc/codegen/codegen.py, as long as you are beneath the swift directory.

Logging configuration

A log file is produced for each run under CODEQL_EXTRACTOR_SWIFT_LOG_DIR (the usual DB log directory).

You can use the environment variable CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS to configure levels for loggers and outputs. This must have the form of a comma separated spec:min_level list, where spec is either a glob pattern (made up of alphanumeric, /, * and . characters) for matching logger names or one of out:binary, out:text, out:console or out:diagnostics, and min_level is one of trace, debug, info, warning, error, critical or no_logs to turn logs completely off.

Current output default levels are no binary logs, info logs or higher in the text file and warning logs or higher on standard error. By default, all loggers are configured with the lowest logging level of all outputs (info by default). Logger names are visible in the textual logs between [...]. Examples are extractor/dispatcher or extractor/<source filename>.trap. An example of CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS usage is the following:

export CODEQL_EXTRACTOR_SWIFT_LOG_LEVELS=out:console:trace,out:text:no_logs,*:warning,*.trap:trace

This will turn off generation of a text log file, redirecting all logs to standard error, but will make all loggers only write warnings or above, except for trap emission logs which will output all logs.

CLion and the native bazel plugin

You can use CLion with the official IntelliJ Bazel plugin, creating the project from scratch with default options. This is known to have issues on non-Linux platforms.

CMake project

The CMakeLists.txt file allows to load the Swift extractor as a CMake project, which allows integration into a wider variety of IDEs. Building with CMake also creates a compile_commands.json compilation database that can be picked up by even more IDEs. In particular, opening the swift directory in VSCode should work.

Debugging codeql database creation

If you want to debug a specific run of the extractor within an integration test or a complex codeql database create invocation, you can do so using gdbserver or lldb-server.

For example with gdbserver, you can

export CODEQL_EXTRACTOR_SWIFT_RUN_UNDER="gdbserver :1234"
export CODEQL_EXTRACTOR_SWIFT_RUN_UNDER_FILTER="SomeSwiftSource\.swift"  # can be any regex matching extractor args

before starting the database extraction, and when that source is encountered the extractor will be run under a gdbserver instance listening on port 1234. You can then attach to the running debugging server from gdb or your IDE. Please refer to your IDE's instructions for how to set up remote debugging.

In particular for breakpoints to work you might need to setup the following remote path mapping:

Remote Local
swift /absolute/path/to/codeql/swift
bazel-out /absolute/path/to/codeql/bazel-out

Thread safety

The extractor is single-threaded, and there was no effort to make anything in it thread-safe.