From 6f936ea2e3f76dbf9b1a995aa3a526c0607e99ce Mon Sep 17 00:00:00 2001 From: Sven Klemm Date: Fri, 21 Jun 2019 15:37:44 -0400 Subject: [PATCH] Fix use-after-free in ChunkAppend Fixes use-after-free in chunk_append/exec.c. Also, strip out memory usage from EXPLAIN ANALYZE output of Sort nodes because it is not stable across platforms --- src/chunk_append/exec.c | 2 +- test/expected/append-10.out | 14 +++++++------- test/expected/append-11.out | 14 +++++++------- test/expected/plan_ordered_append-10.out | 10 +++++----- test/expected/plan_ordered_append-11.out | 10 +++++----- test/runner.sh | 4 +++- 6 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/chunk_append/exec.c b/src/chunk_append/exec.c index 316f3a2f8..1756bfe71 100644 --- a/src/chunk_append/exec.c +++ b/src/chunk_append/exec.c @@ -187,7 +187,7 @@ chunk_append_begin(CustomScanState *node, EState *estate, int eflags) /* * make sure all params are initialized for runtime exclusion */ - node->ss.ps.chgParam = state->subplanstates[0]->plan->allParam; + node->ss.ps.chgParam = bms_copy(state->subplanstates[0]->plan->allParam); } } diff --git a/test/expected/append-10.out b/test/expected/append-10.out index 026452a55..fc70b6d38 100644 --- a/test/expected/append-10.out +++ b/test/expected/append-10.out @@ -265,7 +265,7 @@ psql:include/append_query.sql:58: NOTICE: Stable function now_s() called! ---------------------------------------------------------------------------------------------------------------------- Sort (actual rows=1 loops=1) Sort Key: (date_trunc('year'::text, append_test."time")) DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> HashAggregate (actual rows=1 loops=1) Group Key: (date_trunc('year'::text, append_test."time")) -> Custom Scan (ChunkAppend) on append_test (actual rows=3 loops=1) @@ -292,7 +292,7 @@ ORDER BY t DESC; Group Key: (date_trunc('year'::text, "time")) -> Sort (actual rows=0 loops=1) Sort Key: (date_trunc('year'::text, "time")) DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Result (actual rows=0 loops=1) One-Time Filter: false (7 rows) @@ -363,11 +363,11 @@ psql:include/append_query.sql:98: NOTICE: Stable function now_s() called! -> Function Scan on generate_series "time" (actual rows=6 loops=1) -> Sort (actual rows=6 loops=1) Sort Key: period.btime - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> CTE Scan on period (actual rows=6 loops=1) -> Sort (actual rows=3 loops=1) Sort Key: data.btime - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> CTE Scan on data (actual rows=3 loops=1) (26 rows) @@ -968,7 +968,7 @@ ORDER BY time DESC; ---------------------------------------------------------------------------------- Sort (actual rows=17 loops=1) Sort Key: _hyper_3_11_chunk."time" DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Append (actual rows=17 loops=1) -> Sample Scan on _hyper_3_11_chunk (actual rows=4 loops=1) Sampling: bernoulli ('5'::real) REPEATABLE ('0'::double precision) @@ -991,7 +991,7 @@ ORDER BY time DESC; ---------------------------------------------------------------------------------- Sort (actual rows=17 loops=1) Sort Key: metrics_date."time" DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Custom Scan (ChunkAppend) on metrics_date (actual rows=17 loops=1) Chunks excluded during startup: 2 -> Sample Scan on _hyper_3_11_chunk (actual rows=4 loops=1) @@ -1089,7 +1089,7 @@ ORDER BY time DESC; Heap Fetches: 129 -> Sort (actual rows=32 loops=1) Sort Key: g."time" - Sort Method: quicksort Memory: 26kB + Sort Method: quicksort -> Function Scan on generate_series g (actual rows=32 loops=1) (18 rows) diff --git a/test/expected/append-11.out b/test/expected/append-11.out index 4229b1235..443159460 100644 --- a/test/expected/append-11.out +++ b/test/expected/append-11.out @@ -265,7 +265,7 @@ psql:include/append_query.sql:58: NOTICE: Stable function now_s() called! ---------------------------------------------------------------------------------------------------------------------- Sort (actual rows=1 loops=1) Sort Key: (date_trunc('year'::text, append_test."time")) DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> HashAggregate (actual rows=1 loops=1) Group Key: date_trunc('year'::text, append_test."time") -> Custom Scan (ChunkAppend) on append_test (actual rows=3 loops=1) @@ -292,7 +292,7 @@ ORDER BY t DESC; Group Key: (date_trunc('year'::text, "time")) -> Sort (actual rows=0 loops=1) Sort Key: (date_trunc('year'::text, "time")) DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Result (actual rows=0 loops=1) One-Time Filter: false (7 rows) @@ -363,11 +363,11 @@ psql:include/append_query.sql:98: NOTICE: Stable function now_s() called! -> Function Scan on generate_series "time" (actual rows=6 loops=1) -> Sort (actual rows=6 loops=1) Sort Key: period.btime - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> CTE Scan on period (actual rows=6 loops=1) -> Sort (actual rows=3 loops=1) Sort Key: data.btime - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> CTE Scan on data (actual rows=3 loops=1) (26 rows) @@ -968,7 +968,7 @@ ORDER BY time DESC; ---------------------------------------------------------------------------------- Sort (actual rows=17 loops=1) Sort Key: _hyper_3_11_chunk."time" DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Append (actual rows=17 loops=1) -> Sample Scan on _hyper_3_11_chunk (actual rows=4 loops=1) Sampling: bernoulli ('5'::real) REPEATABLE ('0'::double precision) @@ -991,7 +991,7 @@ ORDER BY time DESC; ---------------------------------------------------------------------------------- Sort (actual rows=17 loops=1) Sort Key: metrics_date."time" DESC - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Custom Scan (ChunkAppend) on metrics_date (actual rows=17 loops=1) Chunks excluded during startup: 2 -> Sample Scan on _hyper_3_11_chunk (actual rows=4 loops=1) @@ -1089,7 +1089,7 @@ ORDER BY time DESC; Heap Fetches: 129 -> Sort (actual rows=32 loops=1) Sort Key: g."time" - Sort Method: quicksort Memory: 26kB + Sort Method: quicksort -> Function Scan on generate_series g (actual rows=32 loops=1) (18 rows) diff --git a/test/expected/plan_ordered_append-10.out b/test/expected/plan_ordered_append-10.out index 4d6ed7ac4..cdcb93d8b 100644 --- a/test/expected/plan_ordered_append-10.out +++ b/test/expected/plan_ordered_append-10.out @@ -432,7 +432,7 @@ ORDER BY device_id, time LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_1_1_chunk.device_id, _hyper_1_1_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=73443 loops=1) -> Seq Scan on _hyper_1_1_chunk (actual rows=20160 loops=1) -> Seq Scan on _hyper_1_2_chunk (actual rows=30240 loops=1) @@ -932,7 +932,7 @@ ORDER BY time LIMIT 10; Order: ht_missing_indexes."time" -> Sort (actual rows=10 loops=1) Sort Key: _hyper_5_16_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Seq Scan on _hyper_5_16_chunk (actual rows=24477 loops=1) Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) Rows Removed by Filter: 5763 @@ -1036,7 +1036,7 @@ ORDER BY time, device_id LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_7_24_chunk."time", _hyper_7_24_chunk.device_id - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=77766 loops=1) -> Seq Scan on _hyper_7_24_chunk (actual rows=6720 loops=1) -> Seq Scan on _hyper_7_26_chunk (actual rows=13440 loops=1) @@ -1059,7 +1059,7 @@ ORDER BY device_id, time LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_7_23_chunk.device_id, _hyper_7_23_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=77766 loops=1) -> Seq Scan on _hyper_7_23_chunk (actual rows=6241 loops=1) -> Seq Scan on _hyper_7_24_chunk (actual rows=6720 loops=1) @@ -1353,7 +1353,7 @@ FROM ordered_append o1 INNER JOIN (SELECT max(time) AS max_time FROM ordered_app -------------------------------------------------------------------------------------------------------------------------------------------- Sort (actual rows=3 loops=1) Sort Key: o1."time" - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Nested Loop (actual rows=3 loops=1) -> Result (actual rows=1 loops=1) InitPlan 1 (returns $0) diff --git a/test/expected/plan_ordered_append-11.out b/test/expected/plan_ordered_append-11.out index 5bc0184f9..e7ddfca85 100644 --- a/test/expected/plan_ordered_append-11.out +++ b/test/expected/plan_ordered_append-11.out @@ -432,7 +432,7 @@ ORDER BY device_id, time LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_1_1_chunk.device_id, _hyper_1_1_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=73443 loops=1) -> Seq Scan on _hyper_1_1_chunk (actual rows=20160 loops=1) -> Seq Scan on _hyper_1_2_chunk (actual rows=30240 loops=1) @@ -932,7 +932,7 @@ ORDER BY time LIMIT 10; Order: ht_missing_indexes."time" -> Sort (actual rows=10 loops=1) Sort Key: _hyper_5_16_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Seq Scan on _hyper_5_16_chunk (actual rows=24477 loops=1) Filter: ("time" > 'Fri Jan 07 00:00:00 2000 PST'::timestamp with time zone) Rows Removed by Filter: 5763 @@ -1036,7 +1036,7 @@ ORDER BY time, device_id LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_7_24_chunk."time", _hyper_7_24_chunk.device_id - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=77766 loops=1) -> Seq Scan on _hyper_7_24_chunk (actual rows=6720 loops=1) -> Seq Scan on _hyper_7_26_chunk (actual rows=13440 loops=1) @@ -1059,7 +1059,7 @@ ORDER BY device_id, time LIMIT 1; Limit (actual rows=1 loops=1) -> Sort (actual rows=1 loops=1) Sort Key: _hyper_7_23_chunk.device_id, _hyper_7_23_chunk."time" - Sort Method: top-N heapsort Memory: 25kB + Sort Method: top-N heapsort -> Append (actual rows=77766 loops=1) -> Seq Scan on _hyper_7_23_chunk (actual rows=6241 loops=1) -> Seq Scan on _hyper_7_24_chunk (actual rows=6720 loops=1) @@ -1353,7 +1353,7 @@ FROM ordered_append o1 INNER JOIN (SELECT max(time) AS max_time FROM ordered_app -------------------------------------------------------------------------------------------------------------------------------------------- Sort (actual rows=3 loops=1) Sort Key: o1."time" - Sort Method: quicksort Memory: 25kB + Sort Method: quicksort -> Nested Loop (actual rows=3 loops=1) -> Result (actual rows=1 loops=1) InitPlan 1 (returns $0) diff --git a/test/runner.sh b/test/runner.sh index b8c326045..4fe3609e2 100755 --- a/test/runner.sh +++ b/test/runner.sh @@ -58,6 +58,8 @@ ${PSQL} $@ -U $TEST_ROLE_SUPERUSER -d ${TEST_DBNAME} -v ECHO=none -v MODULE_PATH export TEST_DBNAME +# we strip out any output between +# and the part about memory usage in EXPLAIN ANALYZE output of Sort nodes ${PSQL} -U ${TEST_PGUSER} \ -v ON_ERROR_STOP=1 \ -v VERBOSITY=terse \ @@ -74,4 +76,4 @@ ${PSQL} -U ${TEST_PGUSER} \ -v ROLE_DEFAULT_PERM_USER_2=${TEST_ROLE_DEFAULT_PERM_USER_2} \ -v MODULE_PATHNAME="'timescaledb-${EXT_VERSION}'" \ -v TSL_MODULE_PATHNAME="'timescaledb-tsl-${EXT_VERSION}'" \ - $@ -d ${TEST_DBNAME} 2>&1 | sed '//,/<\/exclude_from_test>/d' + $@ -d ${TEST_DBNAME} 2>&1 | sed -e '//,/<\/exclude_from_test>/d' -e 's! Memory: [0-9]\+kB!!'