Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(cpp): harden the test-count checker and flag test

Adeshina Lasisie5cd8bc80355
diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml
index 91b97312..5b637757 100644
--- a/.github/workflows/cpp.yml
+++ b/.github/workflows/cpp.yml
@@ -16,11 +16,11 @@ name: C++
# * The flags are tested: the `compile_flags` ctest fails if -ffp-contract=off or
# -fno-fast-math is missing from a laya-cpp translation unit.
#
-# The security job is NOT advisory, as in go.yml.
-#
-# TODO(security): CodeQL for C++ is not added here. go.yml has no CodeQL step; CodeQL lives in the
-# `codeql` matrix of security.yml, and a `cpp` cell there needs a manual build step and is a
-# follow-up for the maintainer to agree to, not a pin invented in this change.
+# The security job is NOT advisory, as in go.yml, and `build` needs it: the fetched GoogleTest
+# commit is verified against the upstream tag before anything is built. Be clear about what it is:
+# the pin check and the workflow-folding check only. There is NO static analyzer for C++ in this
+# change; the sanitizer presets (asan-ubsan) run in `build`. CodeQL for C++ is tracked as a TODO
+# row in security.yml's `codeql` matrix (it needs a manual build step and the maintainer's yes).
on:
pull_request:
@@ -43,6 +43,7 @@ jobs:
build:
name: build (${{ matrix.compiler }}, ${{ matrix.preset }})
runs-on: ubuntu-latest
+ needs: security # the GoogleTest pin is verified before it is fetched and built
continue-on-error: true # advisory: see the header
timeout-minutes: 20
strategy:
diff --git a/laya-cpp/CMakeLists.txt b/laya-cpp/CMakeLists.txt
index 82f0c393..f0e871be 100644
--- a/laya-cpp/CMakeLists.txt
+++ b/laya-cpp/CMakeLists.txt
@@ -9,7 +9,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
-# Floating point must match the Python reference bit for bit: no fused multiply-add, no fast math.
+# Floating point should match the Python reference: no fused multiply-add, no fast math.
+# GCC and Clang get -ffp-contract=off. MSVC gets /fp:precise, which only disables fast-math
+# reassociation and, before VS2022, does not by itself forbid contraction; the MSVC branch is
+# untested and is not exercised by CI until the Windows lane (C11).
if(MSVC)
set(LAYA_FP_FLAGS /fp:precise)
set(LAYA_WARN_FLAGS /W4 /WX)
diff --git a/laya-cpp/cmake/check_compile_flags.cmake b/laya-cpp/cmake/check_compile_flags.cmake
index d880155e..8ad91583 100644
--- a/laya-cpp/cmake/check_compile_flags.cmake
+++ b/laya-cpp/cmake/check_compile_flags.cmake
@@ -24,6 +24,13 @@ foreach(i RANGE ${last})
message(FATAL_ERROR "${file} was compiled without ${flag}:\n${command}")
endif()
endforeach()
+ # A later flag overrides an earlier one, so a conflicting flag anywhere fails, whoever added it
+ # (target options, CMAKE_CXX_FLAGS or the environment).
+ foreach(arg IN LISTS args)
+ if(arg MATCHES "^(-ffast-math|-Ofast|-ffp-contract=(on|fast)|-funsafe-math-optimizations|-fassociative-math|-ffinite-math-only|-freciprocal-math|-fno-signed-zeros)$")
+ message(FATAL_ERROR "${file} was compiled with ${arg}, which overrides the required flags:\n${command}")
+ endif()
+ endforeach()
math(EXPR checked "${checked} + 1")
endforeach()
if(checked LESS 2)
diff --git a/laya-cpp/scripts/check_test_xml.py b/laya-cpp/scripts/check_test_xml.py
index 4dba26aa..3d3d5743 100644
--- a/laya-cpp/scripts/check_test_xml.py
+++ b/laya-cpp/scripts/check_test_xml.py
@@ -8,26 +8,47 @@ and counts instead of trusting the exit status.
ctest --test-dir build/debug --output-junit junit.xml
python laya-cpp/scripts/check_test_xml.py junit.xml --min-tests 2
-Exit 0 only when no test skipped, none failed or errored, and at least --min-tests ran. A disabled
-test (`status="notrun"`) counts as skipped. Several report files may be given; counts add up.
+Exit 0 only when every test case has status `run` (or none), none failed or errored, and at least
+--min-tests ran. Any other status fails: ctest writes `disabled` (a DISABLED_ test or a DISABLED
+property, with no <skipped> child) and `notrun` (a skip, with a <skipped> child); GoogleTest's own
+XML writes `notrun` for a disabled test. Several report files may be given; counts add up. A report
+over 16 MiB or with a DTD (any encoding) is refused.
"""
import argparse
import os
import sys
-# XXE and entity bombs need a DTD, and summarise() refuses any report that has one, so the stdlib
-# parser is safe here and the checker keeps to the standard library.
import xml.etree.ElementTree as ET # nosemgrep: use-defused-xml
+import xml.parsers.expat as expat # nosemgrep: use-defused-xml
+
+# The stdlib parser is used with DTDs refused (see _refuse_dtd) so the checker stays standard library.
+
+MAX_BYTES = 16 * 1024 * 1024
+
+
+def _refuse(*_args):
+ raise ValueError("a DTD or entity declaration is not allowed in a test report")
+
+
+def _refuse_dtd(data):
+ """Parse once with expat, which decodes the document first, so a UTF-16 report cannot hide a
+ DOCTYPE from it the way it hides one from a byte search. XXE and entity bombs need a DTD."""
+ guard = expat.ParserCreate()
+ guard.StartDoctypeDeclHandler = _refuse
+ guard.EntityDeclHandler = _refuse
+ guard.UnparsedEntityDeclHandler = _refuse
+ guard.Parse(data, True)
def summarise(path):
"""(tests, problems) for one report file. Raises ValueError on a malformed report."""
try:
+ if os.path.getsize(path) > MAX_BYTES:
+ raise ValueError("%s is over %d bytes" % (path, MAX_BYTES))
with open(path, "rb") as handle:
data = handle.read()
- if b"<!DOCTYPE" in data or b"<!ENTITY" in data:
- raise ValueError("%s: a DTD is not allowed in a test report" % path)
+ _refuse_dtd(data)
root = ET.fromstring(data)
- except (ET.ParseError, OSError) as err:
+ except (ET.ParseError, expat.ExpatError, OSError) as err:
raise ValueError("%s is not a readable XML report: %s" % (path, err))
if root.tag not in ("testsuites", "testsuite"):
raise ValueError("%s: unexpected root element <%s>" % (path, root.tag))
@@ -36,9 +57,14 @@ def summarise(path):
tests += 1
name = "%s.%s" % (case.get("classname") or "", case.get("name") or "?")
children = {child.tag for child in case}
- if "skipped" in children or case.get("status") == "notrun":
+ status = case.get("status")
+ if "skipped" in children:
problems.append("skipped: " + name)
- if "failure" in children or "error" in children or case.get("status") == "fail":
+ elif status == "fail":
+ problems.append("failed: " + name)
+ elif status not in (None, "run"):
+ problems.append("not run (status=%s): %s" % (status, name))
+ if "failure" in children or "error" in children:
problems.append("failed: " + name)
return tests, problems
@@ -49,6 +75,13 @@ def main(argv=None):
parser.add_argument("--min-tests", type=int, default=1,
help="fail when fewer tests than this ran")
args = parser.parse_args(argv)
+ if args.min_tests < 0:
+ print("check_test_xml: --min-tests must not be negative", file=sys.stderr)
+ return 1
+ if len(set(os.path.realpath(r) for r in args.reports)) != len(args.reports):
+ print("check_test_xml: the same report is listed twice; its tests would count twice",
+ file=sys.stderr)
+ return 1
total, problems = 0, []
for path in args.reports:
if not os.path.isfile(path):
diff --git a/laya-cpp/scripts/fixtures/ctest-disabled-and-skipped.xml b/laya-cpp/scripts/fixtures/ctest-disabled-and-skipped.xml
new file mode 100644
index 00000000..298afe8c
--- /dev/null
+++ b/laya-cpp/scripts/fixtures/ctest-disabled-and-skipped.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<testsuite name="(empty)"
+ tests="4"
+ failures="0"
+ disabled="1"
+ skipped="1"
+ hostname=""
+ time="0"
+ timestamp=""
+ >
+ <testcase name="Version.IsSemver" classname="Version.IsSemver" time="0.0194895" status="run">
+ <properties/>
+ <system-out/>
+ </testcase>
+ <testcase name="Extra.Off" classname="Extra.Off" time="0" status="disabled">
+ <properties/>
+ <system-out/>
+ </testcase>
+ <testcase name="Extra.Skipped" classname="Extra.Skipped" time="0.0104455" status="notrun">
+ <skipped message="SKIP_REGULAR_EXPRESSION_MATCHED"/>
+ <properties/>
+ <system-out/>
+ </testcase>
+ <testcase name="compile_flags" classname="compile_flags" time="0.0162012" status="run">
+ <properties/>
+ <system-out/>
+ </testcase>
+</testsuite>
diff --git a/laya-cpp/scripts/test_check_test_xml.py b/laya-cpp/scripts/test_check_test_xml.py
index a3da107b..a19e23a3 100644
--- a/laya-cpp/scripts/test_check_test_xml.py
+++ b/laya-cpp/scripts/test_check_test_xml.py
@@ -65,6 +65,67 @@ class CheckerTest(unittest.TestCase):
self.assertEqual(code, 1)
self.assertIn("DTD", out)
+ def test_dtd_in_utf16_is_refused(self):
+ text = '<?xml version="1.0" encoding="UTF-16"?><!DOCTYPE x [<!ENTITY a "b">]>' + report(PASS % "a")
+ with tempfile.NamedTemporaryFile("wb", suffix=".xml", delete=False) as handle:
+ handle.write(text.encode("utf-16"))
+ try:
+ done = subprocess.run([sys.executable, CHECKER, handle.name], capture_output=True, text=True)
+ finally:
+ os.unlink(handle.name)
+ self.assertEqual(done.returncode, 1)
+ self.assertIn("DTD", done.stdout + done.stderr)
+
+ def test_status_fail_without_child_fails(self):
+ code, out = run(report('<testcase name="a" classname="S" status="fail"/>'))
+ self.assertEqual(code, 1)
+ self.assertIn("failed: S.a", out)
+
+ def test_error_child_fails(self):
+ code, out = run(report('<testcase name="a" classname="S" status="run"><error/></testcase>'))
+ self.assertEqual(code, 1)
+ self.assertIn("failed: S.a", out)
+
+ def test_root_must_be_testsuite(self):
+ code, out = run('<other><testcase name="a" classname="S" status="run"/></other>')
+ self.assertEqual(code, 1)
+ self.assertIn("unexpected root", out)
+
+ def test_real_ctest_disabled_and_skipped_fail(self):
+ fixture = os.path.join(HERE, "fixtures", "ctest-disabled-and-skipped.xml")
+ done = subprocess.run([sys.executable, CHECKER, fixture], capture_output=True, text=True)
+ out = done.stdout + done.stderr
+ self.assertEqual(done.returncode, 1)
+ self.assertIn("status=disabled", out)
+ self.assertIn("skipped: Extra.Skipped", out)
+
+ def test_disabled_status_alone_fails(self):
+ code, out = run(report(PASS % "a", '<testcase name="b" classname="S" status="disabled"/>'))
+ self.assertEqual(code, 1)
+ self.assertIn("status=disabled", out)
+
+ def test_oversize_report_fails(self):
+ big = report(PASS % "a") + "<!--" + "x" * (16 * 1024 * 1024) + "-->"
+ code, out = run(big)
+ self.assertEqual(code, 1)
+ self.assertIn("over", out)
+
+ def test_duplicate_report_path_fails(self):
+ with tempfile.NamedTemporaryFile("w", suffix=".xml", delete=False) as handle:
+ handle.write(report(PASS % "a"))
+ try:
+ done = subprocess.run([sys.executable, CHECKER, handle.name, handle.name],
+ capture_output=True, text=True)
+ finally:
+ os.unlink(handle.name)
+ self.assertEqual(done.returncode, 1)
+ self.assertIn("twice", done.stderr)
+
+ def test_negative_floor_fails(self):
+ code, out = run(report(PASS % "a"), "--min-tests", "-1")
+ self.assertEqual(code, 1)
+ self.assertIn("negative", out)
+
def test_wrong_root_fails(self):
code, _ = run("<html/>")
self.assertEqual(code, 1)