Skip to content
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
5 changes: 3 additions & 2 deletions contrib/codeql/import.ql
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import rust
/** Materialises the file-to-cfg-line mapping. */
pragma[nomagic]
private predicate fileCfgLines(File f, int cfgLine) {
exists(string relPath |
exists(string relPath, string content |
relPath = f.getAbsolutePath().regexpCapture(".*/pkgs/(.*)", 1) and
hasCfgLine(relPath, cfgLine)
sourceLineContent(relPath, cfgLine, content) and
content.matches("#[cfg%")
)
}

Expand Down
23 changes: 17 additions & 6 deletions contrib/codeql/lib/policy.qll
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,30 @@ private predicate fileRelPath(File f, string relPath) {

/**
* Holds if `t` implements a serde trait via crate-qualified impl
* or a cfg_attr/cfg gate that mentions serde.
* or source-scanned match.
*/
bindingset[traitName]
predicate implementsSerdeTrait(TypeItem t, string traitName) {
implementsTraitInCrate(t, traitName, "serde")
or
exists(Attr a, int serdeLine, string relPath |
// Derive mention inside an attribute range (cfg_attr, cfg, or derive).
exists(Attr a, int srcLine, string relPath, string content |
a = t.getAnAttr() and
a.getMeta().getPath().getSegment().getIdentifier().getText() = ["cfg_attr", "cfg"] and
fileRelPath(fileOf(t), relPath) and
hasSerdeMention(relPath, serdeLine, traitName) and
serdeLine >= a.getLocation().getStartLine() and
serdeLine <= a.getLocation().getEndLine()
sourceLineContent(relPath, srcLine, content) and
content.regexpMatch(".*\\b" + traitName + "\\b.*") and
srcLine >= a.getLocation().getStartLine() and
srcLine <= a.getLocation().getEndLine()
)
or
// Manual impl behind #[cfg(feature = "serde")] that the
// extractor cannot see.
exists(string relPath, string content |
fileRelPath(fileOf(t), relPath) and
sourceLineContent(relPath, _, content) and
content
.regexpMatch("impl\\b.*\\bserde::" + traitName + "\\b.*\\bfor\\s+" + t.getName().getText() +
"\\b.*")
)
}

Expand Down
72 changes: 71 additions & 1 deletion contrib/lint/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path

RETCODE_ERR = 1
RETCODE_PASS = 0
Expand Down Expand Up @@ -55,3 +57,71 @@ def require_bin(name: str, path: str | None = None) -> str:
where = "in expected path" if path else "in PATH"
raise FileNotFoundError(f"error: {name} binary not found {where}")
return result


def root_dir() -> Path:
"""Return the workspace root (directory containing Cargo.toml)."""
return find_up(
Path(__file__).resolve().parent,
is_workspace_root,
"workspace Cargo.toml",
)


def usable_threads() -> int:
"""Return a conservative thread count (total CPUs minus one)."""
return max(1, (os.cpu_count() or 2) - 1)


def usable_mem() -> int:
"""Return half the physical RAM in MiB.

Raises RuntimeError when physical RAM cannot be determined.
"""
total = _physical_ram_bytes()
return total // (2 * 1024 * 1024)


def _physical_ram_bytes() -> int:
"""Return total physical RAM in bytes."""
if sys.platform.startswith("linux"):
for line in Path("/proc/meminfo").read_text(encoding="utf-8").splitlines():
if line.startswith("MemTotal:"):
return int(line.split()[1]) * 1024
raise RuntimeError("MemTotal not found in /proc/meminfo")
if sys.platform == "darwin":
try:
out = subprocess.check_output(
["sysctl", "-n", "hw.memsize"], # noqa: S607
)
return int(out.strip())
except (
FileNotFoundError, subprocess.CalledProcessError, ValueError,
) as exc:
raise RuntimeError(
"could not determine physical RAM on macOS",
) from exc
if sys.platform == "win32":
try:
out = subprocess.check_output(
["powershell", "-NoProfile", "-Command", # noqa: S607
"(Get-CimInstance Win32_ComputerSystem)"
".TotalPhysicalMemory"],
).decode()
value = out.strip()
if value.isdigit():
return int(value)
except (FileNotFoundError, subprocess.CalledProcessError):
pass
try:
out = subprocess.check_output(
["wmic", "computersystem", "get", # noqa: S607
"TotalPhysicalMemory", "/value"],
).decode()
for line in out.splitlines():
if line.startswith("TotalPhysicalMemory="):
return int(line.split("=", 1)[1].strip())
except (FileNotFoundError, subprocess.CalledProcessError, ValueError):
pass
raise RuntimeError("could not determine physical RAM on Windows")
raise RuntimeError(f"unsupported platform: {sys.platform}")
Loading
Loading