Skip to content

Truly independent web browser

Meta: Generate the HSTS preload table without pointers

Andreas Klingcf743a460fb7
diff --git a/Meta/Generators/generate_hsts_preload_data.py b/Meta/Generators/generate_hsts_preload_data.py
index 74714112f3f..0b070c20650 100644
--- a/Meta/Generators/generate_hsts_preload_data.py
+++ b/Meta/Generators/generate_hsts_preload_data.py
@@ -13,6 +13,8 @@ from pathlib import Path
NAME_RE = re.compile(r"[a-z0-9.-]+")
+INCLUDE_SUBDOMAINS_BIT = 1 << 31
+
def parse_entries(input_path: Path) -> list[tuple[str, bool]]:
text = input_path.read_text(encoding="utf-8")
@@ -91,20 +93,48 @@ private:
def generate_implementation_file(entries: list[tuple[str, bool]], output_path: Path) -> None:
+ # NB: The names are one string and each entry is where its name starts in it, so the table holds no pointers. A table
+ # of pointers would need a relocation per entry, which dirties every page of it in every process.
+ offsets: list[int] = []
+ offset = 0
+ for name, include_subdomains in entries:
+ offsets.append(offset | (INCLUDE_SUBDOMAINS_BIT if include_subdomains else 0))
+ offset += len(name)
+ offsets.append(offset)
+ if offset >= INCLUDE_SUBDOMAINS_BIT:
+ raise ValueError("HSTS preload entry names do not fit in the offsets of the generated table")
+
lines: list[str] = []
lines.append("#include <AK/Array.h>")
- lines.append("#include <AK/BinarySearch.h>")
lines.append("#include <AK/StringView.h>")
lines.append("#include <LibHTTP/HSTSPreloadData.h>")
lines.append("")
lines.append("namespace HTTP {")
lines.append("")
- lines.append(f"static constexpr Array<HSTSPreloadEntry, {len(entries)}> s_hsts_preload_entries {{ {{")
- for name, include_subdomains in entries:
- flag = "true" if include_subdomains else "false"
- lines.append(f' HSTSPreloadEntry {{ "{name}"sv, {flag} }},')
+ lines.append("static constexpr char s_hsts_preload_names[] =")
+ for name, _ in entries:
+ lines.append(f' "{name}"')
+ lines.append(" ;")
+ lines.append("")
+ lines.append(f"static constexpr u32 include_subdomains_bit = {INCLUDE_SUBDOMAINS_BIT:#x};")
+ lines.append("")
+ lines.append(
+ "// Where each entry's name starts in the names, with include_subdomains_bit set where the entry includes its"
+ )
+ lines.append("// subdomains, and a last offset where the names end.")
+ lines.append(f"static constexpr Array<u32, {len(offsets)}> s_hsts_preload_offsets {{ {{")
+ for chunk_start in range(0, len(offsets), 8):
+ chunk = offsets[chunk_start : chunk_start + 8]
+ lines.append(" " + " ".join(f"{value:#x}," for value in chunk))
lines.append("} };")
lines.append("")
+ lines.append("static StringView name_of_entry(size_t index)")
+ lines.append("{")
+ lines.append(" auto start = s_hsts_preload_offsets[index] & ~include_subdomains_bit;")
+ lines.append(" auto end = s_hsts_preload_offsets[index + 1] & ~include_subdomains_bit;")
+ lines.append(" return { s_hsts_preload_names + start, end - start };")
+ lines.append("}")
+ lines.append("")
lines.append("HSTSPreloadData const& HSTSPreloadData::the()")
lines.append("{")
lines.append(" static HSTSPreloadData s_the;")
@@ -113,16 +143,22 @@ def generate_implementation_file(entries: list[tuple[str, bool]], output_path: P
lines.append("")
lines.append("Optional<HSTSPreloadEntry> HSTSPreloadData::find_exact(StringView needle) const")
lines.append("{")
- lines.append(" auto* hit = binary_search(")
- lines.append(" s_hsts_preload_entries,")
- lines.append(" needle,")
- lines.append(" nullptr,")
- lines.append(" [](StringView lhs, HSTSPreloadEntry const& rhs) {")
- lines.append(" return lhs.compare(rhs.name);")
- lines.append(" });")
- lines.append(" if (!hit)")
- lines.append(" return {};")
- lines.append(" return *hit;")
+ lines.append(" size_t low = 0;")
+ lines.append(" size_t high = s_hsts_preload_offsets.size() - 1;")
+ lines.append(" while (low < high) {")
+ lines.append(" auto middle = low + (high - low) / 2;")
+ lines.append(" auto name = name_of_entry(middle);")
+ lines.append(" auto comparison = needle.compare(name);")
+ lines.append(" if (comparison == 0)")
+ lines.append(
+ " return HSTSPreloadEntry { name, (s_hsts_preload_offsets[middle] & include_subdomains_bit) != 0 };"
+ )
+ lines.append(" if (comparison < 0)")
+ lines.append(" high = middle;")
+ lines.append(" else")
+ lines.append(" low = middle + 1;")
+ lines.append(" }")
+ lines.append(" return {};")
lines.append("}")
lines.append("")
lines.append("// https://www.rfc-editor.org/rfc/rfc6797#section-8.2")