Skip to contentSkip to conversation

runner: Ensure generated json is properly UTF8-encoded

PATCH Superseded Petri Latvala 3 participants 5 messages Dec 5, 2019 – Jan 10, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 runner: Ensure generated json is properly UTF8-encoded

Sometimes tests output garbage (e.g. due to extreme occurrences of
https://gitlab.freedesktop.org/drm/igt-gpu-tools/issues/55) but we need to present the garbage as results.

We already ignore any test output after the first \0, and for the rest of the bytes that are not directly UTF-8 as-is, we can quite easily represent them with two-byte UTF-8 encoding.

libjson-c already expects the string you feed it through json_object_new_string* functions to be UTF-8.

Cc Arkadiusz Hiler <[email protected]>
---
 runner/resultgen.c | 53 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 40 insertions(+), 13 deletions(-)
runner/resultgen.c +40 -13
index aa438000..92030431 100644
@@ -365,6 +365,33 @@ static void free_matches(struct matches *matches)
 	free(matches->items);
 }
 
+static struct json_object *new_escaped_json_string(const char *buf, size_t len)
+{
+	struct json_object *obj;
+	char *str = NULL;
+	size_t strsize = 0;
+	size_t i;
+
+	for (i = 0; i < len; i++) {
+		if (buf[i] > 0 && buf[i] < 128) {
+			str = realloc(str, strsize + 1);
+			str[strsize] = buf[i];
+			++strsize;
+		} else {
+			/* Encode > 128 character to UTF-8. */
+			str = realloc(str, strsize + 2);
+			str[strsize] = ((unsigned char)buf[i] >> 6) | 0xC0;
+			str[strsize + 1] = ((unsigned char)buf[i] & 0x3F) | 0x80;
+			strsize += 2;
+		}
+	}
+
+	obj = json_object_new_string_len(str, strsize);
+	free(str);
+
+	return obj;
+}
+
 static bool fill_from_output(int fd, const char *binary, const char *key,
 			     struct subtest_list *subtests,
 			     struct json_object *tests)
@@ -419,11 +446,11 @@ static bool fill_from_output(int fd, const char *binary, const char *key,
 		current_test = get_or_create_json_object(tests, piglit_name);
 
 		json_object_object_add(current_test, key,
-				       json_object_new_string_len(buf, statbuf.st_size));
+				       new_escaped_json_string(buf, statbuf.st_size));
 		if (igt_version)
 			json_object_object_add(current_test, "igt-version",
-					       json_object_new_string_len(igt_version,
-									  igt_version_len));
+					       new_escaped_json_string(igt_version,
+								       igt_version_len));
 
 		return true;
 	}
@@ -520,12 +547,12 @@ static bool fill_from_output(int fd, const char *binary, const char *key,
 		}
 
 		json_object_object_add(current_test, key,
-				       json_object_new_string_len(beg, end - beg));
+				       new_escaped_json_string(beg, end - beg));
 
 		if (igt_version) {
 			json_object_object_add(current_test, "igt-version",
-					       json_object_new_string_len(igt_version,
-									  igt_version_len));
+					       new_escaped_json_string(igt_version,
+								       igt_version_len));
 		}
 
 		if (!json_object_object_get_ex(current_test, "result", NULL)) {
@@ -607,11 +634,11 @@ static bool fill_from_output(int fd, const char *binary, const char *key,
 				current_dynamic_test = get_or_create_json_object(tests, dynamic_piglit_name);
 
 				json_object_object_add(current_dynamic_test, key,
-						       json_object_new_string_len(dynbeg, dynend - dynbeg));
+						       new_escaped_json_string(dynbeg, dynend - dynbeg));
 				if (igt_version)
 					json_object_object_add(current_dynamic_test, "igt-version",
-							       json_object_new_string_len(igt_version,
-											  igt_version_len));
+							       new_escaped_json_string(igt_version,
+										       igt_version_len));
 
 				if (!json_object_object_get_ex(current_dynamic_test, "result", NULL)) {
 					const char *dynresulttext;
@@ -764,11 +791,11 @@ static void add_dmesg(struct json_object *obj,
 		      const char *warnings, size_t warningslen)
 {
 	json_object_object_add(obj, "dmesg",
-			       json_object_new_string_len(dmesg, dmesglen));
+			       new_escaped_json_string(dmesg, dmesglen));
 
 	if (warnings) {
 		json_object_object_add(obj, "dmesg-warnings",
-				       json_object_new_string_len(warnings, warningslen));
+				       new_escaped_json_string(warnings, warningslen));
 	}
 }
 
@@ -1361,7 +1388,7 @@ struct json_object *generate_results_json(int dirfd)
 			r--;
 
 		json_object_object_add(obj, "uname",
-				       json_object_new_string_len(buf, r));
+				       new_escaped_json_string(buf, r));
 		close(fd);
 	}
 
@@ -1424,7 +1451,7 @@ struct json_object *generate_results_json(int dirfd)
 		s = read(fd, buf, sizeof(buf));
 
 		json_object_object_add(aborttest, "out",
-				       json_object_new_string_len(buf, s));
+				       new_escaped_json_string(buf, s));
 		json_object_object_add(aborttest, "err",
 				       json_object_new_string(""));
 		json_object_object_add(aborttest, "dmesg",
Signature · 6 lines
2.19.1

_______________________________________________
igt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/igt-dev
#1 lore raw

PATCH 2/2 runner/json_tests: Test handling of unprintable output from tests

A simple test output with numbers from 1 to 255, both in plain text form and as a single byte with that particular value.

Note that the json spec doesn't require \u-encoding for characters other than '"', '\' and the range U+0000 to U+001F, the raw non-\u-encoded UTF-8 in the reference.json file for bytes 128 and up is what libjson-c outputs for those codepoints and is valid.

The validity of the json file can be verified with iconv, i.e.

 $ iconv -f UTF-8 reference.json -o /dev/null && echo it is utf-8
Cc Arkadiusz Hiler <[email protected]>
---
 .../unprintable-characters/0/dmesg.txt        |   5 +
 .../unprintable-characters/0/err.txt          | 258 +++++++++++++++++
 .../unprintable-characters/0/journal.txt      |   2 +
 .../unprintable-characters/0/out.txt          | 259 ++++++++++++++++++
 .../unprintable-characters/README.txt         |   2 +
 .../unprintable-characters/endtime.txt        |   1 +
 .../unprintable-characters/joblist.txt        |   1 +
 .../unprintable-characters/metadata.txt       |  12 +
 .../unprintable-characters/reference.json     |  72 +++++
 .../unprintable-characters/starttime.txt      |   1 +
 .../unprintable-characters/uname.txt          |   1 +
 runner/runner_json_tests.c                    |   1 +
 12 files changed, 615 insertions(+)
 create mode 100644 runner/json_tests_data/unprintable-characters/0/dmesg.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/0/err.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/0/journal.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/0/out.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/README.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/endtime.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/joblist.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/metadata.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/reference.json
 create mode 100644 runner/json_tests_data/unprintable-characters/starttime.txt
 create mode 100644 runner/json_tests_data/unprintable-characters/uname.txt
runner/json_tests_data/unprintable-characters/0/dmesg.txt new file +5 -0
new file mode 100644
index 00000000..6aac2735
@@ -0,0 +1,5 @@
+6,951,3216186095083,-;Console: switching to colour dummy device 80x25
+14,952,3216186095097,-;[IGT] successtest: executing
+14,953,3216186101115,-;[IGT] successtest: starting subtest first-subtest
+14,955,3216186101160,-;[IGT] successtest: exiting, ret=0
+6,956,3216186101299,-;Console: switching to colour frame buffer device 240x75
runner/json_tests_data/unprintable-characters/0/err.txt new file +258 -0

258 lines not on this page · raw message

runner/json_tests_data/unprintable-characters/0/journal.txt new file +2 -0
new file mode 100644
index 00000000..86a30e07
@@ -0,0 +1,2 @@
+first-subtest
+exit:0 (0.014s)
runner/json_tests_data/unprintable-characters/0/out.txt new file +259 -0

259 lines not on this page · raw message

runner/json_tests_data/unprintable-characters/README.txt new file +2 -0
new file mode 100644
index 00000000..046605ea
@@ -0,0 +1,2 @@
+Any nonprintable output in stdout and stderr should yield a json file
+that is still valid UTF-8.
runner/json_tests_data/unprintable-characters/endtime.txt new file +1 -0
new file mode 100644
index 00000000..635f6ae9
@@ -0,0 +1 @@
+1539953735.172373
runner/json_tests_data/unprintable-characters/joblist.txt new file +1 -0
new file mode 100644
index 00000000..81f914a7
@@ -0,0 +1 @@
+successtest first-subtest
runner/json_tests_data/unprintable-characters/metadata.txt new file +12 -0
new file mode 100644
index 00000000..c501ae0e
@@ -0,0 +1,12 @@
+abort_mask : 0
+name : normal-run
+dry_run : 0
+sync : 0
+log_level : 0
+overwrite : 0
+multiple_mode : 0
+inactivity_timeout : 0
+use_watchdog : 0
+piglit_style_dmesg : 0
+test_root : /path/does/not/exist
+results_path : /path/does/not/exist
runner/json_tests_data/unprintable-characters/reference.json new file +72 -0

73 lines not on this page · raw message

runner/json_tests_data/unprintable-characters/starttime.txt new file +1 -0
new file mode 100644
index 00000000..ae038f18
@@ -0,0 +1 @@
+1539953735.111039
runner/json_tests_data/unprintable-characters/uname.txt new file +1 -0
new file mode 100644
index 00000000..a7aef6f7
@@ -0,0 +1 @@
+Linux hostname 4.18.0-1-amd64 #1 SMP Debian 4.18.6-1 (2018-09-06) x86_64
runner/runner_json_tests.c +1 -0
index 3443bd2b..1d2fc3eb 100644
@@ -163,6 +163,7 @@ static const char *dirnames[] = {
 	"dmesg-warn-level-piglit-style",
 	"dmesg-warn-level-one-piglit-style",
 	"dynamic-subtests",
+	"unprintable-characters",
 };
 
 igt_main
Signature · 1 line
2.19.1

_______________________________________________
igt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/igt-dev

#2 lore raw

[igt-dev] ✗ GitLab.Pipeline: warning for series starting with [i-g-t,1/2] runner: Ensure generated json is properly UTF8-encoded

In reply to Petri Latvala
== Series Details ==

== Series Details ==

Series: series starting with [i-g-t,1/2] runner: Ensure generated json is properly UTF8-encoded
URL : https://patchwork.freedesktop.org/series/70492/
State : warning

== Summary ==

Did not get list of undocumented tests for this run, something is wrong!

Other than that, pipeline status: FAILED.

see https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/pipelines/87140 for the overview.

test:ninja-test has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/1105688):

  (runner_json_test:2389) igt_core-INFO:   #3 ../runner/runner_json_tests.c:40 compare()
  (runner_json_test:2389) igt_core-INFO:   #4 ../runner/runner_json_tests.c:40 compare()
  (runner_json_test:2389) igt_core-INFO:   #5 ../runner/runner_json_tests.c:146 __real_main169()
  (runner_json_test:2389) igt_core-INFO:   #6 ../runner/runner_json_tests.c:169 main()
  (runner_json_test:2389) igt_core-INFO:   #7 [__libc_start_main+0xf3]
  (runner_json_test:2389) igt_core-INFO:   #8 [_start+0x2e]

**** END ****
-------

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt
FAILED: meson-test
/usr/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed. section_end:1575988880:build_script section_start:1575988880:after_script section_end:1575988881:after_script section_start:1575988881:upload_artifacts_on_failure section_end:1575988883:upload_artifacts_on_failure ERROR: Job failed: exit code 1


test:ninja-test-clang has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/1105689):

  (runner_json_test:2390) igt_core-INFO:   #2 [compare+0x249]
  (runner_json_test:2390) igt_core-INFO:   #3 [compare+0x249]
  (runner_json_test:2390) igt_core-INFO:   #4 [__real_main169+0x154]
  (runner_json_test:2390) igt_core-INFO:   #5 [main+0x29]
  (runner_json_test:2390) igt_core-INFO:   #6 [__libc_start_main+0xf3]
  (runner_json_test:2390) igt_core-INFO:   #7 [_start+0x2e]

**** END ****
-------

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt
FAILED: meson-test
/usr/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed. section_end:1575988880:build_script section_start:1575988880:after_script section_end:1575988881:after_script section_start:1575988881:upload_artifacts_on_failure section_end:1575988883:upload_artifacts_on_failure ERROR: Job failed: exit code 1


test:ninja-test-arm64 has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/1105691):

  (runner_json_test:5093) igt_core-INFO:   #6 ../runner/runner_json_tests.c:169 main()
  (runner_json_test:5093) igt_core-INFO:   #7 [__libc_start_main+0xe4]
  (runner_json_test:5093) igt_core-INFO:   #8 [_start+0x34]

**** END ****
-------

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt
FAILED: meson-test
/usr/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed. section_end:1575988868:build_script section_start:1575988868:after_script section_end:1575988869:after_script section_start:1575988869:upload_artifacts_on_failure Uploading artifacts...
build: found 1367 matching files 
Uploading artifacts to coordinator... ok  id=1105691 responseStatus=201 Created token=6daYJK9Q
section_end:1575988881:upload_artifacts_on_failure ERROR: Job failed: exit code 1


test:ninja-test-armhf has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/1105692):

  (runner_json_test:4467) igt_core-INFO:   #6 ../runner/runner_json_tests.c:169 main()
  (runner_json_test:4467) igt_core-INFO:   #7 [__libc_start_main+0x98]
  (runner_json_test:4467) igt_core-INFO:   #8 [_start+0x34]

**** END ****
-------

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt
FAILED: meson-test
/usr/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed. section_end:1575988876:build_script section_start:1575988876:after_script section_end:1575988878:after_script section_start:1575988878:upload_artifacts_on_failure Uploading artifacts...
build: found 1366 matching files 
Uploading artifacts to coordinator... ok  id=1105692 responseStatus=201 Created token=Ry_5mmhn
section_end:1575988888:upload_artifacts_on_failure ERROR: Job failed: exit code 1


test:ninja-test-mips has failed (https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/-/jobs/1105693):
(runner_json_test:4924) CRITICAL: Test assertion failure function compare, file ../runner/runner_json_tests.c:109:
(runner_json_test:4924) CRITICAL: Failed assertion: !strcmp(json_object_get_string(one), json_object_get_string(two)) (runner_json_test:4924) igt_core-INFO: Stack trace:
**** END ****
-------

Full log written to /builds/gfx-ci/igt-ci-tags/build/meson-logs/testlog.txt
FAILED: meson-test
/usr/bin/meson test --no-rebuild --print-errorlogs
ninja: build stopped: subcommand failed. section_end:1575988880:build_script section_start:1575988880:after_script section_end:1575988881:after_script section_start:1575988881:upload_artifacts_on_failure Uploading artifacts...
build: found 1366 matching files 
Uploading artifacts to coordinator... ok  id=1105693 responseStatus=201 Created token=sgwGgmK5
section_end:1575988892:upload_artifacts_on_failure ERROR: Job failed: exit code 1


== Logs ==

For more details see: https://gitlab.freedesktop.org/gfx-ci/igt-ci-tags/pipelines/87140
_______________________________________________
igt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/igt-dev

#3 lore raw

Re: [igt-dev] ✗ GitLab.Pipeline: warning for series starting with [i-g-t,1/2] runner: Ensure generated json is properly UTF8-encoded

Quoted 11 lines · Patchwork

== Series Details ==

Series: series starting with [i-g-t,1/2] runner: Ensure generated json is properly UTF8-encoded
URL : https://patchwork.freedesktop.org/series/70492/
State : warning

== Summary ==

Did not get list of undocumented tests for this run, something is wrong!

Other than that, pipeline status: FAILED.

Not completely surprisingly the fancy-characters patch doesn't survive being sent by email. The series can be seen in
https://gitlab.freedesktop.org/adrinael/igt-gpu-tools/commits/unprintable
with the build-time tests passing.

Signature · 5 lines
Petri Latvala
_______________________________________________
igt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/igt-dev
Reviewed-by on v1 1/2
#4 lore raw
In reply to Petri Latvala
Quoted 8 lines · Petri Latvala

Sometimes tests output garbage (e.g. due to extreme occurrences of
https://gitlab.freedesktop.org/drm/igt-gpu-tools/issues/55) but we need to present the garbage as results.

We already ignore any test output after the first \0, and for the rest of the bytes that are not directly UTF-8 as-is, we can quite easily represent them with two-byte UTF-8 encoding.

Quoted 5 lines

libjson-c already expects the string you feed it through json_object_new_string* functions to be UTF-8.

Cc Arkadiusz Hiler <[email protected]>

the series is:

Added this series as an example here:
https://gitlab.freedesktop.org/patchwork-fdo/patchwork-fdo/issues/34
_______________________________________________
igt-dev mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/igt-dev