1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 #include "cachetable-test.h"
41 
42 CACHETABLE ct;
43 
44 //
45 // This test exposed a bug (#3970) caught only by Valgrind.
46 // freed memory was being accessed by toku_test_cachetable_unpin_and_remove
47 //
run_end_chkpt(void * arg)48 static void *run_end_chkpt(void *arg) {
49     assert(arg == NULL);
50     CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
51     toku_cachetable_end_checkpoint(
52         cp,
53         NULL,
54         NULL,
55         NULL
56         );
57     return arg;
58 }
59 
60 static void
run_test(void)61 run_test (void) {
62     const int test_limit = 12;
63     int r;
64     ct = NULL;
65     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
66     const char *fname1 = TOKU_TEST_FILENAME;
67     unlink(fname1);
68     CACHEFILE f1;
69     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
70     create_dummy_functions(f1);
71 
72     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
73     void* v1;
74     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), toku_cachetable_hash(f1, make_blocknum(1)), &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
75     toku_test_cachetable_unpin(
76         f1,
77         make_blocknum(1),
78         toku_cachetable_hash(f1, make_blocknum(1)),
79         CACHETABLE_DIRTY,
80         make_pair_attr(8)
81         );
82 
83     // now this should mark the pair for checkpoint
84     CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
85     toku_cachetable_begin_checkpoint(cp, NULL);
86     r = toku_cachetable_get_and_pin(f1,
87                                     make_blocknum(1),
88                                     toku_cachetable_hash(f1, make_blocknum(1)),
89                                     &v1,
90                                     wc,
91                                     def_fetch,
92                                     def_pf_req_callback,
93                                     def_pf_callback,
94                                     true,
95                                     NULL);
96 
97     toku_pthread_t mytid;
98     r = toku_pthread_create(
99         toku_uninstrumented, &mytid, nullptr, run_end_chkpt, nullptr);
100     assert(r == 0);
101 
102     // give checkpoint thread a chance to start waiting on lock
103     sleep(1);
104     r = toku_test_cachetable_unpin_and_remove(f1, make_blocknum(1), NULL, NULL);
105     assert(r==0);
106 
107     void* ret;
108     r = toku_pthread_join(mytid, &ret);
109     assert(r==0);
110 
111     toku_cachetable_verify(ct);
112     toku_cachefile_close(&f1, false, ZERO_LSN);
113     toku_cachetable_close(&ct);
114 
115 
116 }
117 
118 int
test_main(int argc,const char * argv[])119 test_main(int argc, const char *argv[]) {
120   default_parse_args(argc, argv);
121   run_test();
122   return 0;
123 }
124