Fix: barectf_packet_set_buf(): keep full packet state
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 6 Oct 2020 18:20:12 +0000 (14:20 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 6 Oct 2020 18:20:12 +0000 (14:20 -0400)
The condition of barectf_packet_is_full() is:

    ctx->at == ctx->packet_size

So we're reusing `ctx->at` to know whether or not a packet is full.

A full packet is always (already) closed.

The problem is that barectf_packet_set_buf() changes `ctx->packet_size`
without touching `ctx->at`. Therefore if the value of `ctx->packet_size`
changes, and if the packet was considered to be full, it's not the case
anymore, but it must be.

In barectf_packet_set_buf(), set `ctx->at` to `ctx->packet_size` if it
was already the case to keep any full packet state.

Adding a test with a custom platform calling barectf_packet_set_buf()
with a `NULL` buffer after closing a completely full packet. Without
this patch (in `barectf.c.j2`), this test leads to a segmentation fault.

Fixes: https://github.com/efficios/barectf/issues/18
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
barectf/templates/c/barectf.c.j2
tests/tracing/configs/packet-set-buf/packet-set-buf/packet-set-buf.yaml [new file with mode: 0644]
tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.data.expect [new file with mode: 0644]
tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.metadata.expect [new file with mode: 0644]
tests/tracing/src/packet-set-buf/packet-set-buf/packet-set-buf.c [new file with mode: 0644]
tests/tracing/support/packet-set-buf/Makefile [new file with mode: 0644]
tests/tracing/support/packet-set-buf/test-platform.c [new file with mode: 0644]
tests/tracing/support/packet-set-buf/test-platform.h [new file with mode: 0644]

index d4b4ee0ed40971d89aa43195845d9a5781a6c2f6..6086a74de5fff73981b5dfe8c0d84c78389d3b22 100644 (file)
@@ -118,6 +118,12 @@ void {{ prefix }}packet_set_buf(void * const vctx, uint8_t * const buf,
        struct {{ ctx_struct_name }} * const ctx = _FROM_VOID_PTR(struct {{ ctx_struct_name }}, vctx);
 
        ctx->buf = buf;
+
+       if (ctx->at == ctx->packet_size) {
+               /* Keep full packet state */
+               ctx->at = _BYTES_TO_BITS(buf_size);
+       }
+
        ctx->packet_size = _BYTES_TO_BITS(buf_size);
 }
 
diff --git a/tests/tracing/configs/packet-set-buf/packet-set-buf/packet-set-buf.yaml b/tests/tracing/configs/packet-set-buf/packet-set-buf/packet-set-buf.yaml
new file mode 100644 (file)
index 0000000..a766fed
--- /dev/null
@@ -0,0 +1,51 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+%YAML 1.2
+--- !<tag:barectf.org,2020/3/config>
+trace:
+  type:
+    $include:
+      - stdint.yaml
+      - stdmisc.yaml
+    native-byte-order: le
+    $features:
+      magic-field-type: false
+      uuid-field-type: false
+      data-stream-type-id-field-type: uint8
+    data-stream-types:
+      default:
+        $is-default: true
+        $features:
+          packet:
+            total-size-field-type: uint8
+            content-size-field-type: uint8
+            discarded-event-records-counter-snapshot-field-type: false
+          event-record:
+            type-id-field-type: uint8
+        event-record-types:
+          ev:
+            payload-field-type:
+              class: struct
+              members:
+                - s: str
diff --git a/tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.data.expect b/tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.data.expect
new file mode 100644 (file)
index 0000000..6cce0a2
Binary files /dev/null and b/tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.data.expect differ
diff --git a/tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.metadata.expect b/tests/tracing/expect/packet-set-buf/packet-set-buf/packet-set-buf.metadata.expect
new file mode 100644 (file)
index 0000000..3ba0e70
--- /dev/null
@@ -0,0 +1,92 @@
+/* CTF 1.8 */
+
+/*
+ * The MIT License (MIT)
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ *
+ *
+ * For more details, see <https://barectf.org/>.
+ */
+
+trace {
+       major = 1;
+       minor = 8;
+       byte_order = le;
+       packet.header := struct {
+               integer {
+                       signed = false;
+                       size = 8;
+                       align = 8;
+                       byte_order = native;
+                       base = 10;
+               } stream_id;
+       } align(8);
+};
+
+env {
+       domain = "bare";
+       tracer_name = "barectf";
+};
+
+/* Data stream type `default` */
+stream {
+       id = 0;
+       packet.context := struct {
+               integer {
+                       signed = false;
+                       size = 8;
+                       align = 8;
+                       byte_order = native;
+                       base = 10;
+               } packet_size;
+               integer {
+                       signed = false;
+                       size = 8;
+                       align = 8;
+                       byte_order = native;
+                       base = 10;
+               } content_size;
+       } align(8);
+       event.header := struct {
+               integer {
+                       signed = false;
+                       size = 8;
+                       align = 8;
+                       byte_order = native;
+                       base = 10;
+               } id;
+       } align(8);
+};
+
+event {
+       stream_id = 0;
+       id = 0;
+       name = "ev";
+       fields := struct {
+               string {
+                       encoding = UTF8;
+               } s;
+       } align(1);
+};
diff --git a/tests/tracing/src/packet-set-buf/packet-set-buf/packet-set-buf.c b/tests/tracing/src/packet-set-buf/packet-set-buf/packet-set-buf.c
new file mode 100644 (file)
index 0000000..6f5c4a9
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <assert.h>
+#include <stdint.h>
+
+#include "test-platform.h"
+#include "barectf.h"
+
+int main(void)
+{
+       struct test_platform_ctx * const platform_ctx = test_platform_init(16);
+
+       assert(platform_ctx);
+       barectf_trace_ev(test_platform_barectf_ctx(platform_ctx),
+               "abcdefghijk");
+       barectf_trace_ev(test_platform_barectf_ctx(platform_ctx),
+               "01234567890");
+       test_platform_fini(platform_ctx);
+       return 0;
+}
diff --git a/tests/tracing/support/packet-set-buf/Makefile b/tests/tracing/support/packet-set-buf/Makefile
new file mode 100644 (file)
index 0000000..a572962
--- /dev/null
@@ -0,0 +1,31 @@
+# The MIT License (MIT)
+#
+# Copyright (c) 2016-2020 Philippe Proulx <pproulx@efficios.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+CFLAGS += -O0 -g -Wall -pedantic -Wno-unused-function
+TARGET = test
+OBJS = $(TARGET).o barectf.o test-platform.o
+
+$(TARGET): $(OBJS)
+       $(CC) -o $@ $(LDFLAGS) $^
+
+barectf.o: barectf.c
+       $(CC) $(CFLAGS) -ansi -c $<
diff --git a/tests/tracing/support/packet-set-buf/test-platform.c b/tests/tracing/support/packet-set-buf/test-platform.c
new file mode 100644 (file)
index 0000000..d65e266
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+
+#include "barectf.h"
+#include "test-platform.h"
+
+struct test_platform_ctx {
+       struct barectf_default_ctx ctx;
+       uint8_t *buf;
+       size_t buf_size;
+       FILE *fh;
+};
+
+static void write_packet(struct test_platform_ctx * const platform_ctx)
+{
+       const size_t nmemb = fwrite(barectf_packet_buf(&platform_ctx->ctx),
+               barectf_packet_buf_size(&platform_ctx->ctx), 1,
+                       platform_ctx->fh);
+
+       assert(nmemb == 1);
+}
+
+static int is_backend_full(void * const data)
+{
+       struct test_platform_ctx * const platform_ctx = (void *) data;
+
+       barectf_packet_set_buf(&platform_ctx->ctx, platform_ctx->buf,
+               platform_ctx->buf_size);
+       return 0;
+}
+
+static void open_packet(void * const data)
+{
+       struct test_platform_ctx * const platform_ctx = (void *) data;
+
+       memset(barectf_packet_buf(&platform_ctx->ctx), 0,
+               barectf_packet_buf_size(&platform_ctx->ctx));
+       barectf_default_open_packet(&platform_ctx->ctx);
+}
+
+static void close_packet(void * const data)
+{
+       struct test_platform_ctx * const platform_ctx = (void *) data;
+
+       barectf_default_close_packet(&platform_ctx->ctx);
+       write_packet(platform_ctx);
+       barectf_packet_set_buf(&platform_ctx->ctx, NULL, 512);
+}
+
+struct test_platform_ctx *test_platform_init(const size_t buf_size)
+{
+       struct test_platform_ctx *platform_ctx;
+       struct barectf_platform_callbacks cbs;
+
+       cbs.is_backend_full = is_backend_full;
+       cbs.open_packet = open_packet;
+       cbs.close_packet = close_packet;
+       platform_ctx = malloc(sizeof(*platform_ctx));
+       assert(platform_ctx);
+       platform_ctx->buf_size = buf_size;
+       platform_ctx->buf = malloc(platform_ctx->buf_size);
+       assert(platform_ctx->buf);
+       platform_ctx->fh = fopen("stream", "wb");
+       assert(platform_ctx->fh);
+       barectf_init(&platform_ctx->ctx, platform_ctx->buf,
+               platform_ctx->buf_size, cbs, platform_ctx);
+       open_packet(platform_ctx);
+       return platform_ctx;
+}
+
+void test_platform_fini(struct test_platform_ctx * const platform_ctx)
+{
+       if (barectf_packet_is_open(&platform_ctx->ctx) &&
+                       !barectf_packet_is_empty(&platform_ctx->ctx)) {
+               close_packet(platform_ctx);
+       }
+
+       fclose(platform_ctx->fh);
+       free(platform_ctx->buf);
+       free(platform_ctx);
+}
+
+struct barectf_default_ctx *test_platform_barectf_ctx(
+       struct test_platform_ctx * const platform_ctx)
+{
+       return &platform_ctx->ctx;
+}
diff --git a/tests/tracing/support/packet-set-buf/test-platform.h b/tests/tracing/support/packet-set-buf/test-platform.h
new file mode 100644 (file)
index 0000000..53e033d
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef _BARECTF_TEST_PLATFORM_H
+#define _BARECTF_TEST_PLATFORM_H
+
+/*
+ * Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdlib.h>
+
+struct test_platform_ctx;
+struct barectf_default_ctx;
+
+struct test_platform_ctx *test_platform_init(size_t buf_size);
+void test_platform_fini(struct test_platform_ctx *platform_ctx);
+struct barectf_default_ctx *test_platform_barectf_ctx(
+       struct test_platform_ctx *platform_ctx);
+
+#endif /* _BARECTF_TEST_PLATFORM_H */
This page took 0.028682 seconds and 4 git commands to generate.