From 101172785cc30056b32cdd83e205f7717459c35e Mon Sep 17 00:00:00 2001 From: John Boehr Date: Sat, 6 Apr 2024 22:47:49 -0700 Subject: [PATCH] Attempt to further improve code coverage --- examples/watch.php | 2 +- nix/derivation.nix | 15 +++++++++----- src/extension.c | 13 +++--------- src/functions.c | 2 ++ src/handle.c | 20 +++++++++++++++++++ tests/handle/debug-corrupt-metric-ids.phpt | 20 +++++++++++++++++++ tests/handle/dirty-4.phpt | 17 ++++++++++++++++ .../handle/open-fails-missing-cap-debug.phpt | 15 ++++++++++++++ tests/handle/open-fails-missing-cap.phpt | 1 + tests/handle/raw-stream-invalid-idx.phpt | 14 +++++++++++++ tests/handle/raw-stream.phpt | 2 +- 11 files changed, 104 insertions(+), 17 deletions(-) create mode 100644 tests/handle/debug-corrupt-metric-ids.phpt create mode 100644 tests/handle/dirty-4.phpt create mode 100644 tests/handle/open-fails-missing-cap-debug.phpt create mode 100644 tests/handle/raw-stream-invalid-idx.phpt diff --git a/examples/watch.php b/examples/watch.php index d36e2b3..2ffff60 100755 --- a/examples/watch.php +++ b/examples/watch.php @@ -23,7 +23,7 @@ } $pid = $opts['pid'] ?? 0; -$pid = is_numeric($pid) ? (int) $opts['pid'] : 0; +$pid = is_numeric($pid) ? (int) $pid : 0; $cpu = $opts['cpu'] ?? -1; $cpu = is_numeric($cpu) ? (int) $cpu : -1; $interval = $opts['interval'] ?? 2; diff --git a/nix/derivation.nix b/nix/derivation.nix index c163e75..61bf534 100644 --- a/nix/derivation.nix +++ b/nix/derivation.nix @@ -14,7 +14,7 @@ WerrorSupport ? checkSupport, valgrindSupport ? true, }: -buildPecl rec { +(buildPecl rec { pname = "perfidious"; name = "perfidious-${version}"; version = "v0.1.0"; @@ -40,13 +40,18 @@ buildPecl rec { outputs = ["out" "dev"]; doCheck = checkSupport; - checkPhase = + theRealFuckingCheckPhase = '' - NO_INTERACTON=yes REPORT_EXIT_STATUS=yes make test + REPORT_EXIT_STATUS=1 NO_INTERACTION=1 make test TEST_PHP_ARGS="-n" || (find tests -name '*.log' | xargs cat ; exit 1) '' + (lib.optionalString valgrindSupport '' - USE_ZEND_ALLOC=0 NO_INTERACTON=yes REPORT_EXIT_STATUS=yes make test TEST_PHP_ARGS=-m + USE_ZEND_ALLOC=0 REPORT_EXIT_STATUS=1 NO_INTERACTION=1 make test TEST_PHP_ARGS="-n -m" || (find tests -name '*.mem' | xargs cat ; exit 1) ''); #TEST_PHP_DETAILED = 1; -} +}) +.overrideAttrs (o: + o + // { + checkPhase = o.theRealFuckingCheckPhase; + }) diff --git a/src/extension.c b/src/extension.c index f21e203..cdab12e 100644 --- a/src/extension.c +++ b/src/extension.c @@ -106,9 +106,7 @@ static struct perfidious_handle *split_and_open(zend_string *restrict metrics, b zend_string_release(delim); } while (false); - if (UNEXPECTED(Z_TYPE(z_metrics) != IS_ARRAY)) { - goto done; - } + ZEND_ASSERT(Z_TYPE(z_metrics) == IS_ARRAY); do { zend_string **arr = alloca(sizeof(zend_string *) * (zend_array_count(Z_ARRVAL(z_metrics)) + 1)); @@ -117,12 +115,8 @@ static struct perfidious_handle *split_and_open(zend_string *restrict metrics, b ZEND_HASH_FOREACH_VAL(Z_ARRVAL(z_metrics), z) { - if (EXPECTED(Z_TYPE_P(z) == IS_STRING)) { - arr[arr_len++] = Z_STR_P(z); - } else { - zend_type_error("All event names must be strings"); - goto done; - } + ZEND_ASSERT(Z_TYPE_P(z) == IS_STRING); + arr[arr_len++] = Z_STR_P(z); } ZEND_HASH_FOREACH_END(); @@ -134,7 +128,6 @@ static struct perfidious_handle *split_and_open(zend_string *restrict metrics, b perfidious_handle_enable(handle); } -done: zval_dtor(&z_metrics); return handle; } diff --git a/src/functions.c b/src/functions.c index 6d87327..10b62b2 100644 --- a/src/functions.c +++ b/src/functions.c @@ -161,6 +161,7 @@ static PHP_FUNCTION(perfidious_open) } // Check capability if pid > 0 +#ifndef PERFIDIOUS_DEBUG if (pid > 0) { cap_t cap = cap_get_proc(); if (cap != NULL) { @@ -172,6 +173,7 @@ static PHP_FUNCTION(perfidious_open) } } } +#endif // Check cpu for overflow long int n_proc_onln = sysconf(_SC_NPROCESSORS_ONLN); diff --git a/src/handle.c b/src/handle.c index 3d3ff78..6093104 100644 --- a/src/handle.c +++ b/src/handle.c @@ -550,6 +550,23 @@ static PHP_METHOD(PerfidousHandle, reset) RETURN_ZVAL(ZEND_THIS, 1, 0); } +#ifdef PERFIDIOUS_DEBUG +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO(perfidious_handle_debug_corrupt_metric_ids_arginfo, IS_NULL, true) +ZEND_END_ARG_INFO() + +ZEND_COLD +static PHP_METHOD(PerfidousHandle, debugCorruptMetricIds) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + struct perfidious_handle_obj *obj = perfidious_fetch_handle_object(Z_OBJ_P(ZEND_THIS)); + + for (size_t i = 0; i < obj->handle->metrics_count; i++) { + obj->handle->metrics[i].id = UINT64_MAX; + } +} +#endif + // clang-format off static zend_function_entry perfidious_handle_methods[] = { PHP_ME(PerfidousHandle, disable, perfidious_handle_disable_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) @@ -558,6 +575,9 @@ static zend_function_entry perfidious_handle_methods[] = { PHP_ME(PerfidousHandle, read, perfidious_handle_read_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(PerfidousHandle, readArray, perfidious_handle_read_array_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(PerfidousHandle, reset, perfidious_handle_reset_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) +#ifdef PERFIDIOUS_DEBUG + PHP_ME(PerfidousHandle, debugCorruptMetricIds, perfidious_handle_debug_corrupt_metric_ids_arginfo, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) +#endif PHP_FE_END }; // clang-format on diff --git a/tests/handle/debug-corrupt-metric-ids.phpt b/tests/handle/debug-corrupt-metric-ids.phpt new file mode 100644 index 0000000..1c308a2 --- /dev/null +++ b/tests/handle/debug-corrupt-metric-ids.phpt @@ -0,0 +1,20 @@ +--TEST-- +Perfidious\Handle::read() +--EXTENSIONS-- +perfidious +--SKIPIF-- + +--FILE-- +enable(); +for ($i = 0; $i < 100; $i++) { + usleep(1); +} +$rv->debugCorruptMetricIds(); +$value = $rv->read(); +var_dump($value); +--EXPECTF-- +%A Uncaught DomainException: ID mismatch: %d != %d %A \ No newline at end of file diff --git a/tests/handle/dirty-4.phpt b/tests/handle/dirty-4.phpt new file mode 100644 index 0000000..a14155e --- /dev/null +++ b/tests/handle/dirty-4.phpt @@ -0,0 +1,17 @@ +--TEST-- +Perfidious\Handle::rawStream() - do dirty things part 4 +--EXTENSIONS-- +perfidious +--FILE-- +rawStream(); +var_dump(strlen(fread($stream, 32))); +fclose($stream); +var_dump($handle->readArray()); +--EXPECTF-- +int(32) +%A Uncaught Perfidious\IOException: failed to read: Bad file descriptor %A +%A Uncaught Perfidious\IOException: close failed: Bad file descriptor %A diff --git a/tests/handle/open-fails-missing-cap-debug.phpt b/tests/handle/open-fails-missing-cap-debug.phpt new file mode 100644 index 0000000..582749d --- /dev/null +++ b/tests/handle/open-fails-missing-cap-debug.phpt @@ -0,0 +1,15 @@ +--TEST-- +Perfidious\Handle (open fails with missing cap, debug) +--EXTENSIONS-- +perfidious +--SKIPIF-- + + +--FILE-- + + --FILE-- enable(); +$stream = $handle->rawStream(PHP_INT_MAX); +var_dump($stream); +--EXPECT-- +NULL \ No newline at end of file diff --git a/tests/handle/raw-stream.phpt b/tests/handle/raw-stream.phpt index 9dbaaa7..92fc6e2 100644 --- a/tests/handle/raw-stream.phpt +++ b/tests/handle/raw-stream.phpt @@ -1,5 +1,5 @@ --TEST-- -Perfidious\Handle +Perfidious\Handle::rawStream() --EXTENSIONS-- perfidious --FILE--