Skip to content

Commit

Permalink
Attempt to further improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Apr 7, 2024
1 parent f973dd8 commit 1011727
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 10 additions & 5 deletions nix/derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
WerrorSupport ? checkSupport,
valgrindSupport ? true,
}:
buildPecl rec {
(buildPecl rec {
pname = "perfidious";
name = "perfidious-${version}";
version = "v0.1.0";
Expand All @@ -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;
})
13 changes: 3 additions & 10 deletions src/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();

Expand All @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -172,6 +173,7 @@ static PHP_FUNCTION(perfidious_open)
}
}
}
#endif

// Check cpu for overflow
long int n_proc_onln = sysconf(_SC_NPROCESSORS_ONLN);
Expand Down
20 changes: 20 additions & 0 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/handle/debug-corrupt-metric-ids.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Perfidious\Handle::read()
--EXTENSIONS--
perfidious
--SKIPIF--
<?php if (!Perfidious\DEBUG) die("skip: must be compiled in debug mode"); ?>
--FILE--
<?php
$rv = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
]);
$rv->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
17 changes: 17 additions & 0 deletions tests/handle/dirty-4.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Perfidious\Handle::rawStream() - do dirty things part 4
--EXTENSIONS--
perfidious
--FILE--
<?php
$handle = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
]);
$stream = $handle->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
15 changes: 15 additions & 0 deletions tests/handle/open-fails-missing-cap-debug.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Perfidious\Handle (open fails with missing cap, debug)
--EXTENSIONS--
perfidious
--SKIPIF--
<?php if (get_current_user() === 'root' || str_contains(get_current_user(), 'nixbld')) die("skip: would fail as root"); ?>
<?php if (!Perfidious\DEBUG) die("skip: must be compiled in debug mode"); ?>
--FILE--
<?php
// Note: this test will fail if run with CAP_PERFMON (e.g. as root)
$rv = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
], pid: 1);
--EXPECTF--
%A Uncaught Perfidious\IOException: perf_event_open() failed for perf::PERF_COUNT_SW_DUMMY: Permission denied %A
1 change: 1 addition & 0 deletions tests/handle/open-fails-missing-cap.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Perfidious\Handle (open fails with missing cap)
perfidious
--SKIPIF--
<?php if (get_current_user() === 'root') die("skip: would fail as root"); ?>
<?php if (Perfidious\DEBUG) die("skip: must not be compiled in debug mode"); ?>
--FILE--
<?php
// Note: this test will fail if run with CAP_PERFMON (e.g. as root)
Expand Down
14 changes: 14 additions & 0 deletions tests/handle/raw-stream-invalid-idx.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Perfidious\Handle::rawStream() - invalid idx
--EXTENSIONS--
perfidious
--FILE--
<?php
$handle = Perfidious\open([
"perf::PERF_COUNT_SW_CPU_CLOCK:u",
]);
$handle->enable();
$stream = $handle->rawStream(PHP_INT_MAX);
var_dump($stream);
--EXPECT--
NULL
2 changes: 1 addition & 1 deletion tests/handle/raw-stream.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Perfidious\Handle
Perfidious\Handle::rawStream()
--EXTENSIONS--
perfidious
--FILE--
Expand Down

0 comments on commit 1011727

Please sign in to comment.