Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 348233c

Browse files
committed
Fix incorrect accessing of pfree'd memory in Memoize
For pass-by-reference types, the code added in 0b053e7, which aimed to resolve a memory leak, was overly aggressive in resetting the per-tuple memory context which could result in pfree'd memory being accessed resulting in failing to find previously cached results in the hash table. What was happening was prepare_probe_slot() was switching to the per-tuple memory context and calling ExecEvalExpr(). ExecEvalExpr() may have required a memory allocation. Both MemoizeHash_hash() and MemoizeHash_equal() were aggressively resetting the per-tuple context and after determining the hash value, the context would have gotten reset before MemoizeHash_equal() was called. This could have resulted in MemoizeHash_equal() looking at pfree'd memory. This is less likely to have caused issues on a production build as some other allocation would have had to have reused the pfree'd memory to overwrite it. Otherwise, the original contents would have been intact. However, this clearly caused issues on MEMORY_CONTEXT_CHECKING builds. Author: Tender Wang, Andrei Lepikhov Reported-by: Tender Wang (using SQLancer) Reviewed-by: Andrei Lepikhov, Richard Guo, David Rowley Discussion: https://postgr.es/m/CAHewXNnT6N6UJkya0z-jLFzVxcwGfeRQSfhiwA+NyLg-x8iGew@mail.gmail.com Backpatch-through: 14, where Memoize was added
1 parent 7607671 commit 348233c

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/backend/executor/nodeMemoize.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Memoize nodes are intended to sit above parameterized nodes in the plan
1414
* tree in order to cache results from them. The intention here is that a
1515
* repeat scan with a parameter value that has already been seen by the node
16-
* can fetch tuples from the cache rather than having to re-scan the outer
16+
* can fetch tuples from the cache rather than having to re-scan the inner
1717
* node all over again. The query planner may choose to make use of one of
1818
* these when it thinks rescans for previously seen values are likely enough
1919
* to warrant adding the additional node.
@@ -207,7 +207,6 @@ MemoizeHash_hash(struct memoize_hash *tb, const MemoizeKey *key)
207207
}
208208
}
209209

210-
ResetExprContext(econtext);
211210
MemoryContextSwitchTo(oldcontext);
212211
return murmurhash32(hashkey);
213212
}
@@ -265,15 +264,14 @@ MemoizeHash_equal(struct memoize_hash *tb, const MemoizeKey *key1,
265264
}
266265
}
267266

268-
ResetExprContext(econtext);
269267
MemoryContextSwitchTo(oldcontext);
270268
return match;
271269
}
272270
else
273271
{
274272
econtext->ecxt_innertuple = tslot;
275273
econtext->ecxt_outertuple = pslot;
276-
return ExecQualAndReset(mstate->cache_eq_expr, econtext);
274+
return ExecQual(mstate->cache_eq_expr, econtext);
277275
}
278276
}
279277

@@ -694,9 +692,18 @@ static TupleTableSlot *
694692
ExecMemoize(PlanState *pstate)
695693
{
696694
MemoizeState *node = castNode(MemoizeState, pstate);
695+
ExprContext *econtext = node->ss.ps.ps_ExprContext;
697696
PlanState *outerNode;
698697
TupleTableSlot *slot;
699698

699+
CHECK_FOR_INTERRUPTS();
700+
701+
/*
702+
* Reset per-tuple memory context to free any expression evaluation
703+
* storage allocated in the previous tuple cycle.
704+
*/
705+
ResetExprContext(econtext);
706+
700707
switch (node->mstatus)
701708
{
702709
case MEMO_CACHE_LOOKUP:

src/test/regress/expected/memoize.out

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,39 @@ WHERE t1.unique1 < 1000;
9292
1000 | 9.5000000000000000
9393
(1 row)
9494

95+
SET enable_mergejoin TO off;
96+
-- Test for varlena datatype with expr evaluation
97+
CREATE TABLE expr_key (x numeric, t text);
98+
INSERT INTO expr_key (x, t)
99+
SELECT d1::numeric, d1::text FROM (
100+
SELECT round((d / pi())::numeric, 7) AS d1 FROM generate_series(1, 20) AS d
101+
) t;
102+
-- duplicate rows so we get some cache hits
103+
INSERT INTO expr_key SELECT * FROM expr_key;
104+
CREATE INDEX expr_key_idx_x_t ON expr_key (x, t);
105+
VACUUM ANALYZE expr_key;
106+
-- Ensure we get we get a cache miss and hit for each of the 20 distinct values
107+
SELECT explain_memoize('
108+
SELECT * FROM expr_key t1 INNER JOIN expr_key t2
109+
ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false);
110+
explain_memoize
111+
-------------------------------------------------------------------------------------------
112+
Nested Loop (actual rows=80 loops=N)
113+
-> Seq Scan on expr_key t1 (actual rows=40 loops=N)
114+
-> Memoize (actual rows=2 loops=N)
115+
Cache Key: t1.x, (t1.t)::numeric
116+
Cache Mode: logical
117+
Hits: 20 Misses: 20 Evictions: Zero Overflows: 0 Memory Usage: NkB
118+
-> Index Only Scan using expr_key_idx_x_t on expr_key t2 (actual rows=2 loops=N)
119+
Index Cond: (x = (t1.t)::numeric)
120+
Filter: (t1.x = (t)::numeric)
121+
Heap Fetches: N
122+
(10 rows)
123+
124+
DROP TABLE expr_key;
95125
-- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions
96126
SET work_mem TO '64kB';
97127
SET hash_mem_multiplier TO 1.0;
98-
SET enable_mergejoin TO off;
99128
-- Ensure we get some evictions. We're unable to validate the hits and misses
100129
-- here as the number of entries that fit in the cache at once will vary
101130
-- between different machines.

src/test/regress/sql/memoize.sql

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,31 @@ LATERAL (SELECT t2.unique1 FROM tenk1 t2
5757
WHERE t1.twenty = t2.unique1 OFFSET 0) t2
5858
WHERE t1.unique1 < 1000;
5959

60+
SET enable_mergejoin TO off;
61+
62+
-- Test for varlena datatype with expr evaluation
63+
CREATE TABLE expr_key (x numeric, t text);
64+
INSERT INTO expr_key (x, t)
65+
SELECT d1::numeric, d1::text FROM (
66+
SELECT round((d / pi())::numeric, 7) AS d1 FROM generate_series(1, 20) AS d
67+
) t;
68+
69+
-- duplicate rows so we get some cache hits
70+
INSERT INTO expr_key SELECT * FROM expr_key;
71+
72+
CREATE INDEX expr_key_idx_x_t ON expr_key (x, t);
73+
VACUUM ANALYZE expr_key;
74+
75+
-- Ensure we get we get a cache miss and hit for each of the 20 distinct values
76+
SELECT explain_memoize('
77+
SELECT * FROM expr_key t1 INNER JOIN expr_key t2
78+
ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false);
79+
80+
DROP TABLE expr_key;
81+
6082
-- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions
6183
SET work_mem TO '64kB';
6284
SET hash_mem_multiplier TO 1.0;
63-
SET enable_mergejoin TO off;
6485
-- Ensure we get some evictions. We're unable to validate the hits and misses
6586
-- here as the number of entries that fit in the cache at once will vary
6687
-- between different machines.

0 commit comments

Comments
 (0)