Fix: NULL dereference on sampling and restoration of graph's 'can_consume'
[babeltrace.git] / lib / graph / graph.c
index 0c18ef62bf7dbbfe42a9b5164845473bb0353265..7b43a1017df88539c913166504e45fe18ff8529b 100644 (file)
@@ -188,6 +188,7 @@ struct bt_graph *bt_graph_create(void)
                goto error;
        }
 
+       graph->can_consume = BT_TRUE;
        ret = init_listeners_array(&graph->listeners.port_added);
        if (ret) {
                BT_LOGE_STR("Cannot create the \"port added\" listener array.");
@@ -232,12 +233,14 @@ enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
        struct bt_component *upstream_component = NULL;
        struct bt_component *downstream_component = NULL;
        enum bt_component_status component_status;
+        bt_bool init_can_consume;
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
                status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
+       init_can_consume = graph->can_consume;
 
        if (!upstream_port) {
                BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
@@ -264,6 +267,8 @@ enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
                goto end;
        }
 
+       graph->can_consume = BT_FALSE;
+
        /* Ensure appropriate types for upstream and downstream ports. */
        if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
                BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
@@ -406,38 +411,24 @@ end:
        bt_put(upstream_component);
        bt_put(downstream_component);
        bt_put(connection);
+       if (graph) {
+               graph->can_consume = init_can_consume;
+       }
        return status;
 }
 
 static
-enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
+enum bt_graph_status consume_graph_sink(struct bt_component *sink)
 {
-       struct bt_component *sink;
        enum bt_graph_status status = BT_GRAPH_STATUS_OK;
        enum bt_component_status comp_status;
-       GList *current_node;
-
-       BT_LOGV("Making next sink consume: addr=%p", graph);
-
-       if (!graph->has_sink) {
-               BT_LOGW_STR("Graph has no sink component.");
-               status = BT_GRAPH_STATUS_NO_SINK;
-               goto end;
-       }
-
-       if (g_queue_is_empty(graph->sinks_to_consume)) {
-               BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
-               status = BT_GRAPH_STATUS_END;
-               goto end;
-       }
 
-       current_node = g_queue_pop_head_link(graph->sinks_to_consume);
-       sink = current_node->data;
-       BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
-               sink, bt_component_get_name(sink));
+       assert(sink);
        comp_status = bt_component_sink_consume(sink);
-       BT_LOGV("Consumed from sink: status=%s",
+       BT_LOGV("Consumed from sink: addr=%p, name=\"%s\", status=%s",
+               sink, bt_component_get_name(sink),
                bt_component_status_string(comp_status));
+
        switch (comp_status) {
        case BT_COMPONENT_STATUS_OK:
                break;
@@ -455,21 +446,105 @@ enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
                break;
        }
 
+       return status;
+}
+
+/*
+ * `node` is removed from the queue of sinks to consume when passed to
+ * this function. This function adds it back to the queue if there's
+ * still something to consume afterwards.
+ */
+static
+enum bt_graph_status consume_sink_node(struct bt_graph *graph,
+               GList *node)
+{
+       enum bt_graph_status status;
+       struct bt_component *sink;
+
+       sink = node->data;
+       status = consume_graph_sink(sink);
        if (status != BT_GRAPH_STATUS_END) {
-               g_queue_push_tail_link(graph->sinks_to_consume, current_node);
+               g_queue_push_tail_link(graph->sinks_to_consume, node);
                goto end;
        }
 
        /* End reached, the node is not added back to the queue and free'd. */
-       g_queue_delete_link(graph->sinks_to_consume, current_node);
+       g_queue_delete_link(graph->sinks_to_consume, node);
 
        /* Don't forward an END status if there are sinks left to consume. */
        if (!g_queue_is_empty(graph->sinks_to_consume)) {
                status = BT_GRAPH_STATUS_OK;
                goto end;
        }
+
+end:
+       BT_LOGV("Consumed sink node: status=%s", bt_graph_status_string(status));
+       return status;
+}
+
+BT_HIDDEN
+enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
+               struct bt_component *sink)
+{
+       enum bt_graph_status status;
+       GList *sink_node;
+       int index;
+
+       BT_LOGV("Making specific sink consume: addr=%p, "
+               "comp-addr=%p, comp-name=\"%s\"",
+               graph, sink, bt_component_get_name(sink));
+
+       assert(bt_component_borrow_graph(sink) == graph);
+
+       if (g_queue_is_empty(graph->sinks_to_consume)) {
+               BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
+               status = BT_GRAPH_STATUS_END;
+               goto end;
+       }
+
+       index = g_queue_index(graph->sinks_to_consume, sink);
+       if (index < 0) {
+               BT_LOGV_STR("Sink is not marked as consumable: sink is ended.");
+               status = BT_GRAPH_STATUS_END;
+               goto end;
+       }
+
+       sink_node = g_queue_pop_nth_link(graph->sinks_to_consume, index);
+       assert(sink_node);
+       status = consume_sink_node(graph, sink_node);
+
+end:
+       return status;
+}
+
+BT_HIDDEN
+enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
+{
+       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+       struct bt_component *sink;
+       GList *current_node;
+
+       BT_LOGV("Making next sink consume: addr=%p", graph);
+
+       if (!graph->has_sink) {
+               BT_LOGW_STR("Graph has no sink component.");
+               status = BT_GRAPH_STATUS_NO_SINK;
+               goto end;
+       }
+
+       if (g_queue_is_empty(graph->sinks_to_consume)) {
+               BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
+               status = BT_GRAPH_STATUS_END;
+               goto end;
+       }
+
+       current_node = g_queue_pop_head_link(graph->sinks_to_consume);
+       sink = current_node->data;
+       BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
+               sink, bt_component_get_name(sink));
+       status = consume_sink_node(graph, current_node);
+
 end:
-       BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status));
        return status;
 }
 
@@ -490,7 +565,15 @@ enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
                goto end;
        }
 
+       if (!graph->can_consume) {
+               BT_LOGW_STR("Cannot consume graph in its current state.");
+               status = BT_GRAPH_STATUS_CANNOT_CONSUME;
+               goto end;
+       }
+
+       graph->can_consume = BT_FALSE;
        status = bt_graph_consume_no_check(graph);
+       graph->can_consume = BT_TRUE;
 
 end:
        return status;
@@ -513,6 +596,13 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                goto end;
        }
 
+       if (!graph->can_consume) {
+               BT_LOGW_STR("Cannot run graph in its current state.");
+               status = BT_GRAPH_STATUS_CANNOT_CONSUME;
+               goto end;
+       }
+
+       graph->can_consume = BT_FALSE;
        BT_LOGV("Running graph: addr=%p", graph);
 
        do {
@@ -529,7 +619,7 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                        goto end;
                }
 
-               status = bt_graph_consume(graph);
+               status = bt_graph_consume_no_check(graph);
                if (status == BT_GRAPH_STATUS_AGAIN) {
                        /*
                         * If AGAIN is received and there are multiple
@@ -556,6 +646,9 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
 
 end:
        BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
+       if (graph) {
+               graph->can_consume = BT_TRUE;
+       }
        return status;
 }
 
@@ -863,6 +956,7 @@ enum bt_graph_status bt_graph_add_component_with_init_method_data(
        struct bt_component *component = NULL;
        enum bt_component_class_type type;
        size_t i;
+       bt_bool init_can_consume;
 
        bt_get(params);
 
@@ -871,6 +965,7 @@ enum bt_graph_status bt_graph_add_component_with_init_method_data(
                graph_status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
+       init_can_consume = graph->can_consume;
 
        if (!component_class) {
                BT_LOGW_STR("Invalid parameter: component class is NULL.");
@@ -878,6 +973,7 @@ enum bt_graph_status bt_graph_add_component_with_init_method_data(
                goto end;
        }
 
+       graph->can_consume = BT_FALSE;
        type = bt_component_class_get_type(component_class);
        BT_LOGD("Adding component to graph: "
                "graph-addr=%p, comp-cls-addr=%p, "
@@ -1011,6 +1107,9 @@ enum bt_graph_status bt_graph_add_component_with_init_method_data(
 end:
        bt_put(component);
        bt_put(params);
+       if (graph) {
+               graph->can_consume = init_can_consume;
+       }
        return graph_status;
 }
 
@@ -1023,3 +1122,87 @@ enum bt_graph_status bt_graph_add_component(
        return bt_graph_add_component_with_init_method_data(graph,
                component_class, name, params, NULL, component);
 }
+
+BT_HIDDEN
+int bt_graph_remove_unconnected_component(struct bt_graph *graph,
+               struct bt_component *component)
+{
+       bt_bool init_can_consume;
+       int64_t count;
+       uint64_t i;
+       int ret = 0;
+
+       assert(graph);
+       assert(component);
+       assert(component->base.ref_count.count == 0);
+       assert(bt_component_borrow_graph(component) == graph);
+
+       init_can_consume = graph->can_consume;
+       count = bt_component_get_input_port_count(component);
+
+       for (i = 0; i < count; i++) {
+               struct bt_port *port =
+                       bt_component_get_input_port_by_index(component, i);
+
+               assert(port);
+               bt_put(port);
+
+               if (bt_port_is_connected(port)) {
+                       BT_LOGW("Cannot remove component from graph: "
+                               "an input port is connected: "
+                               "graph-addr=%p, comp-addr=%p, "
+                               "comp-name=\"%s\", connected-port-addr=%p, "
+                               "connected-port-name=\"%s\"",
+                               graph, component,
+                               bt_component_get_name(component),
+                               port, bt_port_get_name(port));
+                       goto error;
+               }
+       }
+
+       count = bt_component_get_output_port_count(component);
+
+       for (i = 0; i < count; i++) {
+               struct bt_port *port =
+                       bt_component_get_output_port_by_index(component, i);
+
+               assert(port);
+               bt_put(port);
+
+               if (bt_port_is_connected(port)) {
+                       BT_LOGW("Cannot remove component from graph: "
+                               "an output port is connected: "
+                               "graph-addr=%p, comp-addr=%p, "
+                               "comp-name=\"%s\", connected-port-addr=%p, "
+                               "connected-port-name=\"%s\"",
+                               graph, component,
+                               bt_component_get_name(component),
+                               port, bt_port_get_name(port));
+                       goto error;
+               }
+       }
+
+       graph->can_consume = BT_FALSE;
+
+       /* Possibly remove from sinks to consume */
+       (void) g_queue_remove(graph->sinks_to_consume, component);
+
+       if (graph->sinks_to_consume->length == 0) {
+               graph->has_sink = BT_FALSE;
+       }
+
+       /*
+        * This calls bt_object_release() on the component, and since
+        * its reference count is 0, its destructor is called. Its
+        * destructor calls the user's finalization method (if set).
+        */
+       g_ptr_array_remove(graph->components, component);
+       goto end;
+
+error:
+       ret = -1;
+
+end:
+       graph->can_consume = init_can_consume;
+       return ret;
+}
This page took 0.03011 seconds and 4 git commands to generate.