├── Compiler
├── Makefile
├── RWInst.c
└── ReadWriteCalls.c
├── LICENSE
├── README
├── README.orig
├── Runtime
├── LWT.h
├── Makefile
├── Plugins
│ ├── Makefile
│ ├── README
│ ├── cganom_plugin.cpp
│ ├── cgtest_plugin.cpp
│ ├── null_plugin.c
│ └── sfi_plugin.c
├── RWCalls.cpp
├── comm_graph.cpp
├── comm_graph.h
├── ct_plugin.h
├── ct_plugin_manager.h
├── thd_ctr.c
└── thd_ctr.h
├── Test
├── Makefile
└── test.c
└── paper.pdf
/Compiler/Makefile:
--------------------------------------------------------------------------------
1 | GCC=gcc-4.8
2 | GXX=g++-4.8
3 | LIBIBERTYDIR=$(shell brew --prefix)/lib/x86_64
4 | GMPDIR=$(shell brew --prefix)/Cellar/gmp4/4.3.2
5 |
6 | UNAME:= $(shell uname -s)
7 |
8 | RWCALLS=ReadWriteCalls.c
9 | RWCALLSBIN=ReadWriteCalls.c.o
10 |
11 |
12 | ifneq "$(RDTYPE)" ""
13 | RDOPTS=-D$(RDTYPE)
14 | endif
15 |
16 | FOO:= $(shell echo $(UNAME))
17 | GCCPLUGINS_DIR:= $(shell $(GCC) -print-file-name=plugin)
18 | CFLAGS+= -I${GMPDIR}/include/ -I${GCCSRCDIR}/include -I$(GCCPLUGINS_DIR)/include -fPIC -O3 -g $(RDOPTS)
19 |
20 | LDFLAGS+= -shared
21 | all: RWInst.so
22 |
23 | %.o: %.cpp
24 | $(GXX) $(CFLAGS) -c $^ -o $@
25 |
26 | %.o: %.c
27 | $(GXX) $(CFLAGS) -c $^ -o $@
28 |
29 | ReadWriteCalls.c.o: ReadWriteCalls.c
30 | $(GXX) $(CFLAGS) -c $^ -o $@
31 |
32 | RWInst.o: RWInst.c
33 | $(GXX) $(CFLAGS) -c $^ -o $@
34 |
35 | RWInst.so: RWInst.o $(RWCALLSBIN)
36 | $(GXX) -undefined dynamic_lookup $(CFLAGS) $(LDFLAGS) $^ -o $@
37 |
38 | clean:
39 | -rm ReadWriteCalls.c.o RWInst.so RWInst.o
40 |
--------------------------------------------------------------------------------
/Compiler/RWInst.c:
--------------------------------------------------------------------------------
1 | /******************************************************************************
2 | * RWInst.c
3 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
4 |
5 | Copyright (C) 2012 Brandon Lucia
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | *
20 | * RWInst - RWInst instrumentation plugin
21 | *****************************************************************************/
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 |
33 | int plugin_is_GPL_compatible = 1;
34 | extern void init_new_func();
35 | /* Help info about the plugin if one were to use gcc's --version --help */
36 | static struct plugin_info RWInst_info =
37 | {
38 | .version = "1",
39 | .help = "CTraps: A Shared Memory Access Instrumentation Plugin (http://github.com/blucia0a/CTraps-gcc -- email blucia@gmail.com)",
40 | };
41 |
42 |
43 | static struct plugin_gcc_version RWInst_ver =
44 | {
45 |
46 | .basever = "4.8",
47 |
48 | };
49 |
50 | /* We don't need to run any tests before we execute our plugin pass */
51 | static bool RWInst_gate(void)
52 | {
53 | return true;
54 | }
55 |
56 | extern unsigned number_dommed_out;
57 | extern unsigned number_escaped_out;
58 | extern unsigned number_total;
59 | void my_insert_rd_wr(basic_block bb, gimple_stmt_iterator *gsi);
60 | static unsigned RWInst_exec(void)
61 | {
62 |
63 | unsigned i;
64 | const_tree str, op;
65 | basic_block bb;
66 | gimple stmt;
67 | gimple_stmt_iterator gsi;
68 |
69 | number_dommed_out = 0;
70 | number_escaped_out = 0;
71 | number_total = 0;
72 | init_new_func();
73 |
74 | //struct loops loo;
75 | //struct loops *outloo;
76 | //outloo = flow_loops_find(&loo);
77 |
78 | calculate_dominance_info(CDI_DOMINATORS);
79 | calculate_dominance_info(CDI_POST_DOMINATORS);
80 |
81 | FOR_EACH_BB(bb)
82 | for (gsi=gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi)) {
83 |
84 | my_insert_rd_wr(bb, &gsi);
85 |
86 | }
87 | return 0;
88 |
89 | }
90 |
91 |
92 | /* See tree-pass.h for a list and desctiptions for the fields of this struct */
93 | static struct gimple_opt_pass RWInst_pass;// =
94 | /*{
95 | .pass.type = GIMPLE_PASS,
96 | .pass.name = "RWInst", // For use in the dump file
97 | .pass.gate = RWInst_gate,
98 | .pass.execute = RWInst_exec, // Pass handler/callback
99 | };*/
100 |
101 |
102 |
103 | /* Return 0 on success or error code on failure */
104 | int plugin_init(struct plugin_name_args *info, /* Argument infor */
105 | struct plugin_gcc_version *ver) /* Version of GCC */
106 | {
107 |
108 |
109 | RWInst_pass.pass.type = GIMPLE_PASS;
110 | RWInst_pass.pass.name = "RWInst"; // For use in the dump file
111 | RWInst_pass.pass.gate = RWInst_gate;
112 | RWInst_pass.pass.execute = RWInst_exec; // Pass handler/callback
113 |
114 | struct register_pass_info pass;
115 |
116 | if (strncmp(ver->basever, RWInst_ver.basever, strlen("4.8")))
117 | return -1; /* Incorrect version of gcc */
118 |
119 | pass.pass = &RWInst_pass.pass;
120 | //pass.reference_pass_name = "alias";
121 | pass.reference_pass_name = "dom";
122 | pass.ref_pass_instance_number = 1;
123 | pass.pos_op = PASS_POS_INSERT_AFTER;
124 |
125 | /* Tell gcc we want to be called after the first SSA pass */
126 | register_callback("RWInst", PLUGIN_PASS_MANAGER_SETUP, NULL, &pass);
127 | register_callback("RWInst", PLUGIN_INFO, NULL, &RWInst_info);
128 |
129 | return 0;
130 | }
131 |
--------------------------------------------------------------------------------
/Compiler/ReadWriteCalls.c:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | ReadWriteCalls.c
5 |
6 | Copyright (C) 2012 Brandon Lucia
7 |
8 | This program is free software: you can redistribute it and/or modify
9 | it under the terms of the GNU General Public License as published by
10 | the Free Software Foundation, either version 3 of the License, or
11 | (at your option) any later version.
12 |
13 | This program is distributed in the hope that it will be useful,
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 | GNU General Public License for more details.
17 |
18 | You should have received a copy of the GNU General Public License
19 | along with this program. If not, see .
20 | */
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include
33 |
34 | #undef SLCOPTEXC
35 |
36 | tree Wr_type;
37 | tree Wr_decl;
38 | gimple Wr_call;
39 |
40 | tree Rd_type;
41 | tree Rd_decl;
42 | gimple Rd_call;
43 |
44 | #if defined(SLCOPTEXC)
45 | tree Rd_Exc_type;
46 | tree Rd_Exc_decl;
47 | gimple Rd_Exc_call;
48 | #endif
49 |
50 | unsigned number_dommed_out;
51 | unsigned number_escaped_out;
52 | unsigned number_total;
53 |
54 | typedef struct _ipoint{
55 |
56 | bool valid;
57 | basic_block b;
58 | gimple s;
59 | tree e;
60 |
61 | } ipoint;
62 |
63 | /*MAX 10000 per function --hack!*/
64 | ipoint ipoints[10000];
65 |
66 | bool inited = false;
67 |
68 | void init_new_func(void){
69 |
70 | tree wr_param_type_list = tree_cons(NULL_TREE, ptr_type_node, NULL_TREE);
71 | Wr_type = build_function_type(void_type_node, wr_param_type_list);
72 | Wr_decl = build_fn_decl ("MemWrite", Wr_type);
73 |
74 | tree rd_param_type_list = tree_cons(NULL_TREE, ptr_type_node, NULL_TREE);
75 | Rd_type = build_function_type(void_type_node, rd_param_type_list);
76 | Rd_decl = build_fn_decl ("MemRead", Rd_type);
77 |
78 | #if defined(SLCOPTEXC)
79 | tree rd_exc_param_type_list = tree_cons(NULL_TREE, ptr_type_node, NULL_TREE);
80 | Rd_Exc_type = build_function_type(void_type_node, rd_exc_param_type_list);
81 | Rd_Exc_decl = build_fn_decl ("MemReadExc", Rd_Exc_type);
82 | #endif
83 |
84 | int i;
85 | for( i = 0; i < 10000; i++ ){
86 |
87 | ipoints[i].b = NULL;
88 | ipoints[i].s = NULL;
89 | ipoints[i].e = NULL;
90 | ipoints[i].valid = false;
91 |
92 |
93 | }
94 |
95 | }
96 |
97 | bool points_to_escaped( tree ssaname ){
98 |
99 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO( ssaname );
100 |
101 | if( pi ){
102 |
103 | struct pt_solution *pt = &pi->pt;
104 |
105 | if( pt->anything || pt->nonlocal || pt->escaped || (pt->vars && pt->vars_contains_global) ){
106 | return true;
107 | }
108 |
109 | }else{
110 |
111 | return true;
112 |
113 | }
114 | return false;
115 | }
116 |
117 | bool can_escape (tree var){
118 |
119 | bool escapes = false;
120 | if (TREE_CODE (var) == SSA_NAME) {
121 |
122 | if (POINTER_TYPE_P (TREE_TYPE (var))){
123 |
124 | escapes = points_to_escaped( var );
125 |
126 | }
127 |
128 | var = SSA_NAME_VAR (var);
129 |
130 | }
131 |
132 | bool glob = false;
133 | if (var != NULL_TREE){
134 |
135 | glob = is_global_var(var);
136 |
137 | }
138 |
139 |
140 | if( !escapes && !glob ){
141 | number_total++;
142 | number_escaped_out++;
143 | }
144 | return escapes || glob;
145 |
146 | }
147 |
148 | static inline void
149 | recompute_all_dominators (void)
150 | {
151 | free_dominance_info (CDI_DOMINATORS);
152 | free_dominance_info (CDI_POST_DOMINATORS);
153 | calculate_dominance_info (CDI_DOMINATORS);
154 | calculate_dominance_info (CDI_POST_DOMINATORS);
155 | }
156 |
157 | bool stmt_postdominates_stmt_p (gimple s2, gimple s1)
158 | {
159 |
160 | basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
161 |
162 | if (!bb1 || s1 == s2){
163 | //fprintf(stderr,"pd case 1\n");
164 | return true;
165 | }
166 |
167 | if (bb1 == bb2){
168 |
169 | gimple_stmt_iterator bsi;
170 |
171 | if (gimple_code (s2) == GIMPLE_PHI){
172 |
173 | //fprintf(stderr,"pd case 2\n");
174 | return true;
175 |
176 | }
177 |
178 | if (gimple_code (s1) == GIMPLE_PHI){
179 |
180 | //fprintf(stderr,"pd case 3\n");
181 | return false;
182 |
183 | }
184 |
185 | for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi)){
186 |
187 | if (gsi_stmt (bsi) == s1){
188 |
189 | //fprintf(stderr,"pd case 4\n");
190 | return true;
191 |
192 | }
193 |
194 | }
195 |
196 | //fprintf(stderr,"pd case 5\n");
197 | return false;
198 | }
199 |
200 | //fprintf(stderr,"pd case 5\n");
201 | return dominated_by_p (CDI_POST_DOMINATORS, bb1, bb2);
202 |
203 | }
204 |
205 | bool in_DOM_with_other( basic_block b, gimple stmt, tree expr ){
206 | int i;
207 | for( i = 0; i < 10000; i++ ){
208 |
209 | if( ipoints[i].valid ){
210 |
211 | if( ipoints[i].e == expr ){
212 |
213 | if( stmt_dominates_stmt_p(ipoints[i].s,stmt) ){
214 | return true;
215 | }
216 |
217 | if( stmt_dominates_stmt_p(stmt,ipoints[i].s) ){
218 | return true;
219 | }
220 |
221 | }
222 |
223 | }
224 |
225 | }
226 |
227 | return false;
228 |
229 | }
230 |
231 | bool in_SLC_with_other( basic_block b, gimple stmt, tree expr ){
232 |
233 | int i;
234 | for( i = 0; i < 10000; i++ ){
235 |
236 | if( ipoints[i].valid ){
237 |
238 | if( ipoints[i].e == expr ){
239 |
240 | if( stmt_dominates_stmt_p(ipoints[i].s,stmt) &&
241 | stmt_postdominates_stmt_p(stmt,ipoints[i].s) ){
242 | return true;
243 | }
244 |
245 | if( stmt_postdominates_stmt_p(ipoints[i].s,stmt) &&
246 | stmt_dominates_stmt_p(stmt,ipoints[i].s) ){
247 | return true;
248 | }
249 |
250 | }
251 |
252 | }
253 |
254 | }
255 |
256 | return false;
257 | }
258 |
259 |
260 | void update_ipoints_list( basic_block bb, gimple stmt, tree expr ){
261 |
262 | int i;
263 | for( i = 0; i < 10000; i++ ){
264 |
265 | if( !ipoints[i].valid ){
266 |
267 | ipoints[i].b = bb;
268 | ipoints[i].e = expr;
269 | ipoints[i].s = stmt;
270 | ipoints[i].valid = true;
271 | break;
272 |
273 | }
274 |
275 | }
276 |
277 | }
278 |
279 | bool try_insert_rd_off_loop( tree expr, basic_block bb, gimple_stmt_iterator *gsi, bool *hoisted );
280 |
281 | void insert_rd( tree expr, basic_block bb, gimple_stmt_iterator *gsi, bool *hoisted ){
282 |
283 | number_total++;
284 |
285 |
286 |
287 | #ifndef WRITEONLY
288 |
289 | #if defined(DOMOPT) || defined(SLCOPT)
290 | #ifdef DOMOPT
291 | if( in_DOM_with_other( bb, gsi_stmt(*gsi), expr ) ){
292 | #endif
293 |
294 | #if defined(SLCOPT)
295 | if( in_SLC_with_other( bb, gsi_stmt(*gsi), expr ) ){
296 | #endif
297 |
298 |
299 | #if defined(SLCOPTEXC)
300 |
301 | gimple Rd_Exc_call = gimple_build_call(Rd_Exc_decl, 1, expr );
302 |
303 | gsi_insert_before(gsi, Rd_Exc_call, GSI_SAME_STMT);
304 |
305 | recompute_all_dominators();
306 |
307 | struct cgraph_node *current_fun_decl_node = cgraph_get_create_node(current_function_decl);
308 |
309 | struct cgraph_node *Rd_decl_node = cgraph_get_create_node(Rd_Exc_decl);
310 |
311 | struct cgraph_edge *e;
312 | if( !(e = cgraph_edge(current_fun_decl_node,Rd_call)) ){
313 |
314 | cgraph_create_edge(current_fun_decl_node,
315 | Rd_decl_node,
316 | Rd_call,
317 | compute_call_stmt_bb_frequency(current_function_decl, bb),
318 | bb_loop_depth(bb));
319 | }
320 |
321 | #endif
322 |
323 | number_dommed_out++;
324 | return;
325 |
326 |
327 | }
328 | #endif
329 |
330 | update_ipoints_list( bb, gsi_stmt(*gsi), expr );
331 |
332 | gimple Rd_call = gimple_build_call(Rd_decl, 1, expr );
333 |
334 | gsi_insert_before(gsi, Rd_call, GSI_SAME_STMT);
335 |
336 | recompute_all_dominators();
337 |
338 | struct cgraph_node *current_fun_decl_node = cgraph_get_create_node(current_function_decl);
339 |
340 | struct cgraph_node *Rd_decl_node = cgraph_get_create_node(Rd_decl);
341 |
342 | struct cgraph_edge *e;
343 | if( !(e = cgraph_edge(current_fun_decl_node,Rd_call)) ){
344 |
345 | cgraph_create_edge(current_fun_decl_node,
346 | Rd_decl_node,
347 | Rd_call,
348 | compute_call_stmt_bb_frequency(current_function_decl, bb),
349 | bb_loop_depth(bb));
350 | }
351 |
352 | #endif
353 |
354 | }
355 |
356 | /*TODO: FIx this mess of trash*/
357 | bool try_insert_rd_off_loop( tree expr, basic_block bb, gimple_stmt_iterator *gsi, bool *hoisted ){
358 |
359 | *hoisted = true;
360 | struct loop *lo = bb->loop_father;
361 |
362 | print_node(stderr,"\n\nTrying to make it work with a loop doodly\n\n",expr,0);
363 | gimple stmt = gsi_stmt(*gsi);
364 | fprintf(stderr,"The statement is: \n");
365 | //print_gimple_stmt(stderr,stmt,0,0);
366 |
367 | fprintf(stderr,"Getting loop...\n");
368 | if( lo != NULL ){
369 |
370 | fprintf(stderr,"Got it. Getting preheader edge\n");
371 | edge entry_e = loop_preheader_edge (lo);
372 |
373 | fprintf(stderr,"Got it. Null testing preheader edge\n");
374 | if( entry_e ){
375 |
376 | fprintf(stderr,"Got it. Not Null.\n");
377 |
378 | basic_block hdr = entry_e->src;
379 |
380 | fprintf(stderr,"Got header block.\n");
381 | if( hdr != NULL ){
382 |
383 | fprintf(stderr,"header block was not null. Getting def stmt\n");
384 | //gimple defstmt = SSA_NAME_DEF_STMT(expr);
385 |
386 | //fprintf(stderr,"null testing def stmt\n");
387 | //if( defstmt != NULL ){
388 |
389 | fprintf(stderr,"def stmt was not null. getting start bb gsi\n");
390 | gimple_stmt_iterator hdrsi = gsi_start_bb(hdr);
391 |
392 | fprintf(stderr,"end-checking start bb gsi\n");
393 | if( !gsi_end_p(hdrsi) ){
394 |
395 | fprintf(stderr,"getting loop preheader start stmt.\n");
396 | gimple hdrstmt = gsi_stmt(hdrsi);
397 |
398 | /*This last condition checks to be sure the symbol we refer to
399 | * in the call is defined by the time we make the call, i.e., after the loop header*/
400 | //fprintf(stderr,"checking domination.\n");
401 |
402 | //print_gimple_stmt(stderr,hdrstmt,0,0);
403 | // print_gimple_stmt(stderr,defstmt,0,0);
404 |
405 | // if( stmt_dominates_stmt_p( hdrstmt, defstmt ) ){
406 |
407 | fprintf(stderr,"Insertion.\n");
408 | insert_rd( expr, hdr, &hdrsi, hoisted );
409 |
410 | return true;
411 |
412 | // }
413 |
414 | }
415 |
416 | //}
417 |
418 | }
419 |
420 | }
421 |
422 | }
423 | return false;
424 |
425 | }
426 |
427 | void insert_wr( tree expr, basic_block bb, gimple_stmt_iterator *gsi ){
428 |
429 | gimple Wr_call = gimple_build_call(Wr_decl, 1, expr );
430 |
431 | gsi_insert_before(gsi, Wr_call, GSI_SAME_STMT);
432 |
433 | recompute_all_dominators();
434 |
435 | struct cgraph_node *current_fun_decl_node = cgraph_get_create_node(current_function_decl);
436 |
437 | struct cgraph_node *Wr_decl_node = cgraph_get_create_node(Wr_decl);
438 |
439 | struct cgraph_edge *e;
440 | if( !(e = cgraph_edge(current_fun_decl_node,Wr_call)) ){
441 |
442 | cgraph_create_edge(current_fun_decl_node,
443 | Wr_decl_node,
444 | Wr_call,
445 | compute_call_stmt_bb_frequency(current_function_decl, bb),
446 | bb_loop_depth(bb));
447 | }
448 |
449 | }
450 |
451 | int compute_component_ref_offset(tree cref){
452 |
453 | /*Compute the offset from properties of the field declaration*/
454 | tree fielddecl = TREE_OPERAND(cref,1);
455 | tree bitoffset = DECL_FIELD_BIT_OFFSET(fielddecl);
456 | tree fieldoffset = DECL_FIELD_OFFSET(fielddecl);
457 | unsigned long byteoffset = ((TREE_INT_CST_HIGH (fieldoffset) << HOST_BITS_PER_WIDE_INT) + TREE_INT_CST_LOW (fieldoffset));
458 | unsigned long ibitoffset = ((TREE_INT_CST_HIGH (bitoffset) << HOST_BITS_PER_WIDE_INT) + TREE_INT_CST_LOW (bitoffset));
459 |
460 | /*Build a new offset constant node to pass to our instrumentation*/
461 | return (byteoffset + (ibitoffset/8));
462 |
463 | }
464 |
465 | tree component_ref_offset_tree(int offset){
466 |
467 | return build_int_cst( integer_type_node, offset);
468 |
469 | }
470 |
471 |
472 | tree get_base(tree refbase){
473 |
474 | if( TREE_CODE(refbase) == MEM_REF ){
475 |
476 | /*BASE CASE #1*/
477 | /*If it is a mem_ref, the first argument is the address of the base of the component ref*/
478 | return TREE_OPERAND(refbase,0);
479 |
480 | }else if( DECL_P(refbase) ){
481 |
482 | /*BASE CASE #2*/
483 | /*If it is a decl, then it's a global or static or something, so we have to take the address*/
484 | return build_addr (refbase, current_function_decl);
485 |
486 | }else{
487 | return NULL;
488 | }
489 |
490 | }
491 |
492 |
493 | /*Recursively descend into component_ref and array_refs that base off of one another.
494 | *
495 | * The code accumulates an offset expression (as a tree) and when it hits the bottom
496 | * it fills in the base tree*/
497 | void get_offset_and_base(tree expr, tree *offset, tree *base){
498 |
499 | tree ref_base = TREE_OPERAND(expr,0);
500 |
501 |
502 | if( TREE_CODE( ref_base ) == ARRAY_REF ||
503 | TREE_CODE( ref_base ) == COMPONENT_REF ){
504 |
505 | get_offset_and_base(ref_base, offset, base);
506 |
507 | }
508 | else if( TREE_CODE(ref_base) == MEM_REF ){
509 | /*Base Case! We have a base value here!*/
510 | *base = get_base(ref_base);
511 | *offset = TREE_OPERAND(ref_base, 1);
512 | }else{
513 |
514 | /*Base Case! We have a base value here!*/
515 | *base = get_base(ref_base);
516 |
517 | }
518 |
519 | /*On the way back up the return stack, update the offset expr*/
520 | /*Only ARRAY_REFs and COMPONENT_REFs get here*/
521 | if( TREE_CODE( expr ) == ARRAY_REF ){
522 |
523 | tree elem_size = array_ref_element_size(expr);
524 |
525 | tree arr_idx = TREE_OPERAND(expr,1);
526 |
527 | tree aoffset = fold_build2 (MULT_EXPR, TREE_TYPE (arr_idx), arr_idx, elem_size);
528 |
529 | if( *offset == NULL ){
530 |
531 | *offset = aoffset;
532 |
533 | }else{
534 |
535 | *offset = fold_build2(PLUS_EXPR, integer_type_node, *offset, aoffset );
536 |
537 | }
538 |
539 |
540 | }else if( TREE_CODE(expr) == COMPONENT_REF ){
541 |
542 |
543 | int coffset = compute_component_ref_offset(expr);
544 |
545 | if( *offset == NULL ){
546 |
547 | tree constoff = build_int_cst(integer_type_node, coffset);
548 |
549 | *offset = constoff;
550 |
551 |
552 | }else{
553 |
554 | *offset = fold_build2(PLUS_EXPR, integer_type_node, *offset, build_int_cst( integer_type_node, coffset) );
555 |
556 | }
557 |
558 | }else{
559 |
560 | //print_node(stderr,"\n\n--------------------OFFSET TYPE ERROR ---------------\n\n",expr,0);
561 |
562 | }
563 |
564 | return;
565 |
566 | }
567 |
568 | /*Handle a read in a COMPONENT_REF tree*/
569 | void insert_rd_comp_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
570 |
571 | tree offset = NULL;
572 | tree base = NULL;
573 |
574 | get_offset_and_base(expr, &offset, &base);
575 |
576 | tree final_addr = fold_build_pointer_plus(base,offset);
577 |
578 | if( can_escape( base ) ){
579 |
580 | bool hoisted = false;
581 | insert_rd(final_addr, bb,gsi, &hoisted);
582 |
583 | }
584 |
585 | }
586 |
587 | /*Handle a read in an ARRAY_REF tree*/
588 | void insert_rd_arr_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
589 |
590 | tree offset = NULL;
591 | tree base = NULL;
592 |
593 | tree ref_base = TREE_OPERAND(expr,0);
594 | if( TREE_CODE(ref_base) == STRING_CST ){ return; }
595 |
596 | get_offset_and_base(expr, &offset, &base);
597 |
598 | tree final_addr = fold_build_pointer_plus(base,offset);
599 |
600 | if( can_escape( base) ){
601 |
602 | bool hoisted = false;
603 | insert_rd(final_addr, bb,gsi, &hoisted);
604 |
605 | }
606 |
607 | }
608 |
609 | /*Handle a read in an MEM_REF tree*/
610 | void insert_rd_mem_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
611 |
612 | tree base = TREE_OPERAND(expr, 0);
613 | tree offset = TREE_OPERAND(expr, 1);
614 |
615 | tree final_addr = fold_build_pointer_plus(base, offset);
616 |
617 | if( can_escape( base ) ){
618 |
619 | bool hoisted = false;
620 | insert_rd(final_addr,bb,gsi,&hoisted);
621 |
622 | }
623 |
624 | }
625 |
626 | /*Handle a write in an COMPONENT_REF tree*/
627 | void insert_wr_comp_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
628 |
629 | tree offset = NULL;
630 | tree base = NULL;
631 |
632 | get_offset_and_base(expr, &offset, &base);
633 |
634 | tree final_addr = fold_build_pointer_plus(base,offset);
635 |
636 | if( can_escape( base) ){
637 |
638 | insert_wr(final_addr, bb,gsi);
639 |
640 | }
641 |
642 | }
643 |
644 | /*Handle a write in an ARRAY_REF tree*/
645 | void insert_wr_arr_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
646 |
647 | tree offset = NULL;
648 | tree base = NULL;
649 |
650 | tree ref_base = TREE_OPERAND(expr,0);
651 | if( TREE_CODE(ref_base) == STRING_CST ){ return; }
652 |
653 | get_offset_and_base(expr, &offset, &base);
654 |
655 | tree final_addr = fold_build_pointer_plus(base,offset);
656 |
657 | if( can_escape( base) ){
658 |
659 | insert_wr(final_addr, bb, gsi);
660 |
661 | }
662 |
663 | }
664 |
665 | /*Handle a write in an MEM_REF tree*/
666 | void insert_wr_mem_ref(tree expr, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
667 |
668 | tree base = TREE_OPERAND(expr, 0);
669 | tree offset = TREE_OPERAND(expr, 1);
670 |
671 | tree final_addr = fold_build_pointer_plus(base, offset);
672 |
673 | if( can_escape( base ) ){
674 |
675 | insert_wr(final_addr,bb,gsi);
676 |
677 | }
678 |
679 | }
680 |
681 | void handle_read_ref(tree ref, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
682 |
683 |
684 | if( TREE_CODE(ref) == MEM_REF ){
685 |
686 | insert_rd_mem_ref(ref,stmt,bb,gsi);
687 |
688 | }else if( TREE_CODE(ref) == COMPONENT_REF ){
689 |
690 | insert_rd_comp_ref(ref,stmt,bb,gsi);
691 |
692 | }else if( TREE_CODE(ref) == ARRAY_REF ){
693 |
694 | insert_rd_arr_ref(ref,stmt,bb,gsi);
695 |
696 | }else if( DECL_P(ref) ){
697 |
698 | if( can_escape(ref) ){
699 |
700 | bool hoisted = false;
701 | insert_rd( build_addr(ref,current_function_decl),bb,gsi,&hoisted );
702 |
703 | }
704 |
705 | }else if( TREE_CODE(ref) == BIT_FIELD_REF ){
706 | /*Would like to handle these, but the IR is new and undocumented, and the args aren't what they seemm...*/
707 | /*
708 | tree memref = TREE_OPERAND(ref,0);
709 | insert_rd_mem_ref(memref,stmt,bb,gsi);
710 | */
711 | }else{
712 |
713 | /*I know about these three, and they are not important. Ctrs, Locals (SSA), Csts and Addrs are all constant*/
714 | if( TREE_CODE(ref) != CONSTRUCTOR &&
715 | TREE_CODE(ref) != SSA_NAME &&
716 | !TREE_CONSTANT(ref) &&
717 | TREE_CODE(ref) != ADDR_EXPR ){
718 |
719 | //print_node(stderr,"\n\n--------------------UNHANDLED ASSIGN RHS---------------\n\n",ref,0);
720 |
721 | }
722 |
723 | }
724 |
725 | }
726 |
727 | void handle_write_ref(tree ref, gimple stmt, basic_block bb, gimple_stmt_iterator *gsi){
728 |
729 | if( TREE_CODE(ref) == MEM_REF ){
730 |
731 | insert_wr_mem_ref(ref,stmt,bb,gsi);
732 |
733 | }else if( TREE_CODE(ref) == COMPONENT_REF ){
734 |
735 | insert_wr_comp_ref(ref,stmt,bb,gsi);
736 |
737 | }else if( TREE_CODE(ref) == ARRAY_REF ){
738 |
739 | insert_wr_arr_ref(ref,stmt,bb,gsi);
740 |
741 | }else if( DECL_P(ref) ){
742 |
743 | if( can_escape(ref) ){
744 |
745 | insert_wr( build_addr(ref,current_function_decl), bb, gsi );
746 |
747 | }
748 |
749 | }else if( TREE_CODE(ref) == BIT_FIELD_REF ){
750 | /*
751 | tree memref = TREE_OPERAND(ref,0);
752 | insert_wr_mem_ref(memref,stmt,bb,gsi);
753 | */
754 | }else{
755 |
756 | /*I know about these three, and they are not important. Ctrs, Csts and Addrs are all constant*/
757 | if( TREE_CODE(ref) != CONSTRUCTOR &&
758 | !TREE_CONSTANT(ref) &&
759 | TREE_CODE(ref) != SSA_NAME &&
760 | TREE_CODE(ref) != ADDR_EXPR ){
761 |
762 | //print_node(stderr,"\n\n--------------------UNHANDLED ASSIGN LHS---------------\n\n",ref,0);
763 |
764 | }
765 |
766 | }
767 |
768 | }
769 |
770 | void insert_for_assign(basic_block bb, gimple_stmt_iterator *gsi){
771 |
772 | gimple stmt = gsi_stmt(*gsi);
773 |
774 | if( gimple_assign_rhs_class(stmt) == GIMPLE_SINGLE_RHS){
775 |
776 | tree rhs_full = gimple_assign_rhs1 (stmt);
777 |
778 | handle_read_ref(rhs_full,stmt,bb,gsi);
779 |
780 | tree lhs_full = gimple_assign_lhs (stmt);
781 |
782 | handle_write_ref(lhs_full,stmt,bb,gsi);
783 |
784 | }
785 |
786 | }
787 |
788 |
789 | void insert_for_call(basic_block bb, gimple_stmt_iterator *gsi){
790 |
791 | gimple stmt = gsi_stmt(*gsi);
792 |
793 | tree function = gimple_call_fndecl(stmt);
794 |
795 | //if( !function || !DECL_EXTERNAL(function) ){ return; }
796 | if( !function ){ return; }
797 |
798 | unsigned num_args = gimple_call_num_args(stmt);
799 |
800 | int i;
801 | for(i = 0; i < num_args; i++){
802 |
803 | stmt = gsi_stmt(*gsi);
804 | bb = gimple_bb(stmt);
805 |
806 | tree arg;
807 |
808 | arg = gimple_call_arg(stmt,i);
809 |
810 | handle_read_ref(arg,stmt,bb,gsi);
811 |
812 | tree name_id = DECL_NAME(function);
813 | if( name_id != NULL ){
814 | const char *name_str = IDENTIFIER_POINTER(name_id);
815 | if( !strncmp( name_str,"free",strlen(name_str)) ||
816 | !strncmp( name_str,"operator delete",strlen(name_str)) ){
817 |
818 | if( TREE_CODE(arg) == SSA_NAME ){
819 | insert_wr( arg, bb, gsi );
820 | }else{
821 | //print_node(stderr,"\n\n--------------------UNHANDLED DEALLOCATION ARGUMENT---------------\n\n",arg,0);
822 | }
823 |
824 | }
825 |
826 | }
827 |
828 | }
829 |
830 | }
831 |
832 | void insert_for_return(basic_block bb, gimple_stmt_iterator *gsi){
833 |
834 | gimple stmt;
835 |
836 | stmt = gsi_stmt(*gsi);
837 |
838 | tree ret = gimple_return_retval(stmt);
839 |
840 | if( ret ){
841 |
842 | handle_read_ref( ret, stmt, bb, gsi );
843 |
844 | }
845 |
846 | }
847 |
848 | void my_insert_rd_wr(basic_block bb, gimple_stmt_iterator *gsi){
849 |
850 | gimple stmt;
851 |
852 | stmt = gsi_stmt(*gsi);
853 | if( gimple_has_mem_ops( stmt ) &&
854 | gimple_code(stmt) != GIMPLE_ASSIGN &&
855 | gimple_code(stmt) != GIMPLE_RETURN &&
856 | gimple_code(stmt) != GIMPLE_CALL &&
857 | gimple_code(stmt) != GIMPLE_ASM){
858 |
859 | //fprintf(stderr,"Has MemOps we won't be touching...\n");
860 | //fprintf(stderr,"Code=%s\n",gimple_code_name[ gimple_code(stmt) ]);
861 | //print_gimple_stmt(stderr,stmt,0,0);
862 |
863 |
864 | }
865 |
866 | if( gimple_code(stmt) == GIMPLE_ASSIGN ){
867 |
868 | insert_for_assign(bb,gsi);
869 |
870 | }else if( gimple_code(stmt) == GIMPLE_CALL ){
871 |
872 | insert_for_call(bb, gsi);
873 |
874 | }else if( gimple_code(stmt) == GIMPLE_RETURN){
875 |
876 | insert_for_return(bb, gsi);
877 |
878 | } else if( gimple_code(stmt) == GIMPLE_ASM){
879 | /* Not much to do for ASM memory clobber statements. */
880 | (void)0;
881 | }
882 |
883 | }
884 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | CTraps for GCC-4.8, as published at CGO 2015.
2 |
3 | Copyright (C) 2012-2014 Brandon Lucia blucia@cmu.edu
4 |
5 | This program is free software: you can redistribute it and/or modify
6 | it under the terms of the GNU General Public License as published by
7 | the Free Software Foundation, either version 3 of the License, or
8 | (at your option) any later version.
9 |
10 | This program is distributed in the hope that it will be useful,
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | GNU General Public License for more details.
14 |
15 | You should have received a copy of the GNU General Public License
16 | along with this program. If not, see .
17 |
18 | The accepted version of this paper is in this repository as ``paper.pdf''.
19 | This is not the camera-ready version, but rather the accepted version with
20 | author names added.
21 |
22 | Build instructions for CGO Artifact Evaluation
23 | -----------------------------------------------
24 |
25 | This README will help you build the Last Writer Slices compiler pass and
26 | runtime, and test them both on an included example program. The output of the
27 | test program shows the contents of the last writer table, which is the main
28 | contribution of the paper. README.orig describes the advanced features of this
29 | system, such as how to build plugins (i.e., CTraps), and a few other features.
30 | For a basic artifact evaluation, the most important component is the LWS
31 | runtime described here.
32 |
33 | This package was built and tested on OS X Mavericks, with homebrew installed. First, clone the project from github:
34 |
35 | 0)$>git clone git@github.com:blucia0a/CTraps-gcc.git
36 |
37 | To build the cloned project, there are a few prerequisites.
38 |
39 | 1)Install homebrew (http://brew.sh/)
40 | 2)$>brew install gcc48
41 |
42 | That will install g++-4.8, which is used to build the plugin.
43 |
44 | Homebrew changed the name of the libiberty.h header that is required by the plugin infrastructure. That means we need to symlink the renamed homebrew one to the original name using this command:
45 |
46 | $>ln -s `brew --prefix`/Cellar/gcc48/4.8.3/lib/gcc/x86_64-apple-darwin13.3.0/4.8.3/plugin/include/libiberty-4.8.h `brew --prefix`/Cellar/gcc48/4.8.3/lib/gcc/x86_64-apple-darwin13.3.0/4.8.3/plugin/include/libiberty.h
47 |
48 | After that, things should build.
49 |
50 | $>cd Compiler; make
51 | $>cd ../Runtime; make
52 |
53 | On my machine, this process produces a few bit-shift width warnings that are safe to ignore, but no other errors.
54 |
55 | The next thing to do is build and run the test program in Test/test.c
56 |
57 | $>cd ../Test; make; make run
58 |
59 | This runs the test driver. By default, the Last Writer Slices runtime is
60 | configured with #define ARTIFACTEVAL. The effect of this is that each time the
61 | Last Writer Table is updated, the runtime prints the variable's address, thread
62 | ID, and program counter of the update. Normally, this information would be
63 | accessible via gdb (or lldb), but for the sake of making evaluation easier,
64 | I've opted to display the output for the artifact evaluator.
65 |
--------------------------------------------------------------------------------
/README.orig:
--------------------------------------------------------------------------------
1 | /*
2 | ==Last Writer Slices & CTraps -- A GCC plugin and runtime system to track data provenance and interpose on shared data accesses==
3 |
4 | Copyright (C) 2012-2014 Brandon Lucia blucia@cmu.edu
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | Last writer slices and CTraps (collectively "CTraps", for short) is a gcc
21 | plugin and runtime library that inserts calls to runtime library functions just
22 | before shared memory accesses in parallel/concurrent code.
23 |
24 | The purpose of this plugin is to expose information about when and how threads
25 | communicate with one another to programmers for debugging and program
26 | analysis. The overhead of the instrumentation and runtime code is
27 | very low -- often low enough for always-on use in production code. In a series
28 | of initial experiments the overhead was 0-10% in several widely used server programs.
29 |
30 | The provided runtime implementation has several modes:
31 |
32 | 1)Collect "last writer slices". A Last writer slice is a table that shows,
33 | for each shared memory location in the program that was written, what thread
34 | and instruction last wrote it. These are useful when debugging crashes.
35 | Last writer slices are stored in the program's memory map, so on a crash,
36 | the last writer slices are preserved with the core dump. Last writer slices
37 | can be collected very efficiently, so they can be collected during production
38 | runs with extremely low overhead (0-10% in many useful cases).
39 |
40 | 2)Expose "communication traps". When exposing communication traps,
41 | CTraps collects last writer slices. When an instrumented shared memory
42 | operation executes, its instrumentation looks at the last writer slice
43 | for the memory location being accessed. If the last write was performed
44 | by a different thread, a special function called a "communication trap handler"
45 | is called. Communication trap handlers are registered with the runtime
46 | at program startup. A communication trap handler can execute arbitrary code.
47 | These handlers are only called when threads communicate with one another.
48 | Communication is infrequent, so the overhead of these traps
49 | is often low enough for use in production code. The benefit of these traps
50 | is that a developer can: 1)dynamically profile thread sharing; 2)dynamically
51 | tune how threads interact to improve performance; 3)identify likely bugs
52 | manifesting as yet-unseen communication between threads. There are likely
53 | many other uses for these traps as well.
54 |
55 | =BUILDING=
56 | To build, you should configure the paths in the Compiler/Makefile and
57 | Runtime/Makefile.
58 |
59 | There are some configurable build parameters for the Compiler and for the
60 | Runtime.
61 |
62 | For the compiler:
63 | There are three build configurations. Each build configuration inserts
64 | instrumentation at different points in the program. Which configuration gets
65 | built is determined by setting "RDTYPE" in the environment when you run make.
66 | (i.e., "RDTYPE= make" in CTraps-gcc/Compiler). RDTYPE can take four
67 | different values:
68 |
69 | if RDTYPE is undefined:
70 | Instrumentation calls are inserted at all read and write operations.
71 |
72 | if RDTYPE=WRITEONLY:
73 | Instrumentation calls are inserted at write operations only. This mode
74 | collects last last writer slices, but does not support instrumentation on
75 | reads.
76 |
77 | if RDTYPE=DOMOPT:
78 | Instrumentation calls are inserted at all write operations and all read
79 | operations except those that are dominated in the control flow graph by other
80 | points that are instrumented.
81 |
82 | if RDTYPE=SLCOPT:
83 | Instrumentation calls are inserted at all write operations and all read
84 | operations except those that are in straight line code sequences with
85 | other points that are instrumented.
86 |
87 | There are also several build configurations for the Runtime. These are
88 | controlled by a series of #defines at the top of CTraps-gcc/Runtime/RWCalls.cpp.
89 | Each different configuration enables different runtime features.
90 |
91 | -PLUGIN-
92 | This #define enables use of the plugin framework in the runtime. That means you
93 | can build a plugin (see the README in CTraps-gcc/Runtime/Plugins), load it, and
94 | it will be called when instrumented memory operations communicate.
95 |
96 | -SAMPLING-
97 | Sampling should not be used.
98 |
99 | -COMMGRAPH-
100 | Comm graph collects a communication graph that records, for each read and write
101 | to a shared location, what the accessing instruction, and the last writer are.
102 | The results are stored in a file when the program ends. The file to store the
103 | graph in is specified via the CG_GRAPH environment variable. The communication
104 | graph is useful for debugging (as with an automated debugging methodology like
105 | http://recon.cs.washington.edu). The communication graph may also be useful
106 | for understanding program structure and performance tuning.
107 |
108 | -RRRW-
109 | RRRW traces, for each read and write to shared memory, the previous access to that location. If an operation's previous access was in a different thread, the current operation is recorded. The trace of such accesses reveals which parts of the program access data shared by multiple threads. This is useful for performance tuning and debugging.
110 |
111 | -SLCOPTEXC-
112 | This flag is for debugging CTraps and is not important.
113 |
114 |
115 | =INSTRUMENTING YOUR PROGRAM=
116 |
117 | To enable instrumentation of shared memory accesses, add the following flag to
118 | your compilation command line:
119 |
120 | -fplugin=/RWInst.so
121 |
122 | where is the path where you built the CTraps compiler support
123 | (e.g., ${HOME}/CTraps-gcc/Compiler).
124 |
125 | Doing that will insert a call to MemRead(void *addr) before instrumented reads
126 | and will insert a call to MemWrite(void *addr) before instrumented writes. Note
127 | that if you've used RDTYPE=WRITEONLY, the compiler will only insert MemWrite
128 | calls.
129 |
130 | =RUNNING YOUR INSTRUMENTED PROGRAM=
131 | Be sure the runtime is built with the options you wanted. Also be sure that
132 | CTraps-gcc/Runtime/libRWCalls.so is in your LD_LIBRARY_PATH. If those two
133 | things are true, you should be able to run your program like normal and CTraps
134 | should make its instrumentation calls.
135 |
136 | -Using Plugins-
137 | A plugin to CTraps to be loaded is specified via the CTRAP_PLUGIN environment
138 | variable. Plugins must expose the interface specified in
139 | CTraps-gcc/Runtime/ct_plugin.h. See the readme in the plugins section for more
140 | information on how to write your own CTraps plugin.
141 |
--------------------------------------------------------------------------------
/Runtime/LWT.h:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | LWT.h
5 | Copyright (C) 2012 Brandon Lucia
6 |
7 | This program is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | This program is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with this program. If not, see .
19 | */
20 |
21 | #ifndef _LWT_H_
22 | #define _LWT_H_
23 |
24 | #ifndef _GNU_SOURCE
25 | #define _GNU_SOURCE
26 | #endif
27 |
28 | #include
29 | #include
30 | #include
31 |
32 | #undef USE_ATOMICS
33 | #define LWT_SIZE 0xffffff
34 | #define LWT_ENTRIES 0x1000000
35 |
36 |
37 | #ifdef USE_ATOMICS
38 | typedef std::atomic LWT_Entry;
39 | #else
40 | typedef unsigned long LWT_Entry;
41 | #endif
42 |
43 | #undef EVAL_TRACKMEMSIZE
44 | #ifdef EVAL_TRACKMEMSIZE
45 | std::set MSet;
46 | pthread_mutex_t mset_lock;
47 | #endif
48 |
49 | extern "C"{
50 | static LWT_Entry *LWT_table;
51 | }
52 |
53 | pthread_mutex_t last_writer_lock;
54 |
55 | void init_LWT();
56 |
57 | void insert_into_LWT( pthread_t thd, void *pc, void *addr );
58 |
59 | inline unsigned long find_LWT_bin_for_addr( void *addr );
60 |
61 | LWT_Entry get_LWT_entry_for_addr( void *addr );
62 |
63 | LWT_Entry create_LWT_entry( pthread_t thd, void *pc );
64 |
65 | bool LWT_entry_thd_equals( pthread_t thd, LWT_Entry e );
66 |
67 | bool LWT_entry_pc_equals( void *pc, LWT_Entry e);
68 |
69 | bool LWT_entry_equals( pthread_t thd, void *pc, LWT_Entry e);
70 |
71 | void *LWT_entry_get_pc( LWT_Entry e);
72 |
73 | #endif
74 |
--------------------------------------------------------------------------------
/Runtime/Makefile:
--------------------------------------------------------------------------------
1 | GXX=g++-4.8
2 | GCC=gcc-4.8
3 |
4 | CFLAGS+= -g -fPIC -O3 -pthread -Wno-deprecated
5 | CXXFLAGS+= -g -fPIC -O3 -pthread -std=c++11 -Wno-deprecated
6 |
7 | LDFLAGS+= -shared -std=c++11 -lpthread -ldl
8 |
9 | all: libRWCalls.so
10 |
11 | %.o: %.cpp
12 | $(GXX) $(CXXFLAGS) -c $^ -o $@
13 |
14 | %.o: %.c
15 | $(GCC) $(CFLAGS) -c $^ -o $@
16 |
17 | RWCalls.s: RWCalls.cpp
18 | $(GXX) $(CXXFLAGS) -S $^ -o $@
19 |
20 | libRWCalls.so: RWCalls.o thd_ctr.o comm_graph.o
21 | $(GXX) -I. $(CFLAGS) $(LDFLAGS) $^ -o $@ -ldl -Wno-deprecated
22 |
23 | clean:
24 | -rm libRWCalls.so
25 | -rm *.o
26 |
--------------------------------------------------------------------------------
/Runtime/Plugins/Makefile:
--------------------------------------------------------------------------------
1 | GXX=${HOME}/cvsandbox/GCC-experimental/inst/bin/g++
2 | GCC=${HOME}/cvsandbox/GCC-experimental/inst/bin/gcc
3 |
4 | CTRAPSHOME=${HOME}/cvsandbox/CommGraph/Runtime
5 |
6 | CFLAGS+= -fPIC -O3 -g -pthread -I${HOME}/cvsandbox/GCC-experimental/inst/include -I${CTRAPSHOME} -Wno-deprecated
7 | CXXFLAGS+= -fPIC -O3 -g -pthread -std=c++11 -I${HOME}/cvsandbox/GCC-experimental/inst/include -I${CTRAPSHOME} -Wno-deprecated
8 |
9 | LDFLAGS+= -shared -std=c++11 -lpthread -L${HOME}/cvsandbox/GCC-experimental/inst/lib -L${HOME}/cvsandbox/GCC-experimental/inst/lib64 -L${HOME}/cvsandbox/GCC-experimental/inst/libexec -ldl
10 |
11 | all: null_plugin.so sfi_plugin.so cgtest_plugin.so cganom_plugin.so
12 |
13 | %.o: %.cpp
14 | $(GXX) $(CXXFLAGS) -c $^ -o $@
15 |
16 | %.o: %.c
17 | $(GCC) $(CFLAGS) -c $^ -o $@
18 |
19 | %.so: %.o
20 | $(GXX) -I. $(CFLAGS) $(LDFLAGS) $^ -o $@ -Wno-deprecated
21 |
22 | clean:
23 | -rm *.so
24 | -rm *.o
25 |
--------------------------------------------------------------------------------
/Runtime/Plugins/README:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | This document describes how to build a plugin that can be loaded by CTraps
21 | instrumentation. Your plugin can specify actions that should be taken on
22 | program start and end, thread start and end, and on instrumented communicating
23 | read and write operations. For information on how to load a plugin into
24 | CTraps, see ../../README.
25 |
26 | To build a plugin, copy null_plugin.c, and change the method stubs to implement
27 | what you want your plugin to do.
28 |
29 | TODO: Add plugin support for calling plugin function on non-communicating
30 | instrumented memory accesses.
31 |
--------------------------------------------------------------------------------
/Runtime/Plugins/cganom_plugin.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include "ct_plugin.h"
26 |
27 | using namespace std;
28 | __thread __gnu_cxx::hash_map > *graph;
29 |
30 | int is_gone = 0;
31 |
32 | extern "C" void *queryLWT_PC_plugin(void *addr);
33 |
34 | void load_graph(){
35 |
36 | graph = new __gnu_cxx::hash_map >();
37 |
38 | char *p = getenv("CGTEST_GRAPH");
39 | if( p != NULL ){
40 |
41 | unsigned long src;
42 | unsigned long sink;
43 | FILE *gf = fopen(p,"r");
44 | while( fscanf(gf,"%lx %lx\n",&src,&sink) != EOF ){
45 |
46 | auto ct_iter = graph->find( (unsigned long)src);
47 | if( ct_iter == graph->end() ){
48 |
49 |
50 | ((*graph)[ (unsigned long)src ]) = set();
51 | (*graph)[ (unsigned long)src ].insert( (unsigned long) sink);
52 |
53 | }else{
54 |
55 | if( (ct_iter->second ).find( (unsigned long)sink ) == (ct_iter->second).end() ){
56 |
57 | (ct_iter->second ).insert( (unsigned long)sink );
58 |
59 | }
60 |
61 | }
62 |
63 | }
64 |
65 | }else{
66 | fprintf(stderr,"Couldn't load the graph from %s\n",p);
67 | abort();
68 | }
69 |
70 | }
71 |
72 | extern "C"{
73 | void global_init(){
74 |
75 | fprintf(stderr,"CTRAPS: CGAnom Plugin Startup\n");
76 | is_gone = 0;
77 |
78 | }
79 | }
80 |
81 | extern "C" {
82 | void thread_init(){
83 |
84 | load_graph();
85 |
86 | }
87 | }
88 |
89 | extern "C" {
90 | void global_deinit(){
91 |
92 | fprintf(stderr,"CTRAPS: CGAnom Plugin Shutdown\n");
93 |
94 | }
95 | }
96 |
97 | extern "C" {
98 | void thread_deinit(){
99 |
100 | fprintf(stderr,"CTRAPS: CGAnom Plugin Thread Deinit (T=%lu)\n",(unsigned long)pthread_self());
101 | delete graph;
102 |
103 | }
104 | }
105 |
106 | #define MAX_DELAY 100000
107 | #define DELAY_STEP 1
108 |
109 | extern "C" {
110 | void read_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
111 |
112 | auto srcIter = graph->find( (unsigned long)oldPC );
113 | if( srcIter == graph->end() ){
114 |
115 | fprintf(stderr,"{Communication Anomaly - New SRC} M[%p]: T%lu,W@%p -> T%lu,R@%p\n",addr,oldTid,oldPC,newTid,newPC);
116 |
117 | //((*graph)[ (unsigned long)oldPC])();
118 | ((*graph)[ (unsigned long)oldPC]) = set();
119 | (*graph)[ (unsigned long)oldPC ].insert( (unsigned long)newPC );
120 | return;
121 |
122 | } else {
123 |
124 | auto sinkIter = srcIter->second.find( (unsigned long)newPC );
125 | if( sinkIter == srcIter->second.end() ){
126 |
127 | fprintf(stderr,"{Communication Anomaly} M[%p]: T%lu,W@%p -> T%lu,R@%p\n",addr,oldTid,oldPC,newTid,newPC);
128 | srcIter->second.insert( (unsigned long)newPC );
129 | return;
130 |
131 | } else {
132 |
133 | return;
134 |
135 | }
136 |
137 | }
138 |
139 | }
140 | }
141 |
142 | extern "C" {
143 | void write_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
144 |
145 | auto srcIter = graph->find( (unsigned long)oldPC );
146 | if( srcIter == graph->end() ){
147 |
148 | fprintf(stderr,"{Communication Anomaly - NEW SRC} M[%p]: T%lu,W@%p -> T%lu,W@%p\n",addr,oldTid,oldPC,newTid,newPC);
149 | ((*graph)[ (unsigned long)oldPC]) = set();
150 | (*graph)[ (unsigned long)oldPC ].insert( (unsigned long)newPC );
151 | return;
152 |
153 | } else {
154 |
155 | auto sinkIter = srcIter->second.find( (unsigned long)newPC );
156 | if( sinkIter == srcIter->second.end() ){
157 |
158 | fprintf(stderr,"{Communication Anomaly} M[%p]: T%lu,W@%p -> T%lu,W@%p\n",addr,oldTid,oldPC,newTid,newPC);
159 | srcIter->second.insert( (unsigned long)newPC );
160 | return;
161 |
162 | } else {
163 |
164 | return;
165 |
166 | }
167 |
168 | }
169 |
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/Runtime/Plugins/cgtest_plugin.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include "ct_plugin.h"
25 |
26 | using namespace std;
27 | __thread __gnu_cxx::hash_map > *graph;
28 | __thread __gnu_cxx::hash_map *graph_max;
29 |
30 | int is_gone = 0;
31 |
32 | extern "C" void *queryLWT_PC_plugin(void *addr);
33 |
34 | void load_graph(){
35 |
36 | graph = new __gnu_cxx::hash_map >();
37 | graph_max = new __gnu_cxx::hash_map();
38 |
39 | char *p = getenv("CGTEST_GRAPH");
40 | if( p != NULL ){
41 |
42 | unsigned long src;
43 | unsigned long sink;
44 | unsigned long num;
45 | FILE *gf = fopen(p,"r");
46 | while( fscanf(gf,"%lx-%lx:%lu\n",&src,&sink,&num) != EOF ){
47 |
48 | auto ct_iter = graph->find( (unsigned long)src);
49 | if( ct_iter == graph->end() ){
50 |
51 | (*graph)[ (unsigned long)src ] = __gnu_cxx::hash_map();
52 | (*graph)[ (unsigned long)src ][ (unsigned long)sink ] = num;
53 |
54 |
55 | }else{
56 |
57 | if( (ct_iter->second ).find( (unsigned long)sink ) == (ct_iter->second).end() ){
58 |
59 | (ct_iter->second )[ (unsigned long)sink ] = num;
60 |
61 | }else{
62 |
63 | (ct_iter->second )[ (unsigned long)sink ] += num;
64 |
65 | }
66 |
67 | }
68 | (*graph_max)[ src ] += num;
69 |
70 | }
71 |
72 | }else{
73 | fprintf(stderr,"Couldn't load the graph from %s\n",p);
74 | abort();
75 | }
76 |
77 | }
78 |
79 | extern "C"{
80 | void global_init(){
81 |
82 | fprintf(stderr,"CTRAPS: CGTest Plugin Startup\n");
83 | is_gone = 0;
84 |
85 | }
86 | }
87 |
88 | extern "C" {
89 | void thread_init(){
90 |
91 | load_graph();
92 |
93 | }
94 | }
95 |
96 | extern "C" {
97 | void global_deinit(){
98 |
99 | fprintf(stderr,"CTRAPS: CGTest Plugin Shutdown\n");
100 |
101 | }
102 | }
103 |
104 | extern "C" {
105 | void thread_deinit(){
106 |
107 | fprintf(stderr,"CTRAPS: CGTest Plugin Thread Deinit (T=%lu)\n",(unsigned long)pthread_self());
108 |
109 | }
110 | }
111 |
112 | #define MAX_DELAY 100000
113 | #define DELAY_STEP 1
114 |
115 | extern "C" {
116 | void read_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
117 |
118 | unsigned long delay = 0;
119 | unsigned long amt = 0;
120 | unsigned long max = 0;
121 | auto srcIter = graph->find( (unsigned long)oldPC );
122 | if( srcIter == graph->end() ){
123 |
124 | /*For testing, we want to delay the least likely operations the least.
125 | * We've never seen this, so let it go immediately*/
126 | return;
127 |
128 | } else {
129 |
130 | auto sinkIter = srcIter->second.find( (unsigned long)newPC );
131 | if( sinkIter == srcIter->second.end() ){
132 |
133 | return;
134 |
135 | } else {
136 |
137 | amt = (*graph)[(unsigned long)oldPC][(unsigned long)newPC];
138 | max = (*graph_max)[(unsigned long)oldPC];
139 | float pct = ((float)amt)/((float)max);
140 | delay = (unsigned long)(((float)pct) * ((float)MAX_DELAY));
141 | //fprintf(stderr,"%f * %lu = %lu\n",pct,MAX_DELAY,delay);
142 |
143 | }
144 |
145 | }
146 |
147 | unsigned long e = 0;
148 | void *origPC = oldPC;
149 | //fprintf(stderr,"T%lu: Delaying@%p because %p->%p is %lu/%lu frequent (%f)\n",(unsigned long)newTid,newPC,oldPC,newPC,amt,max,delay);
150 | while( true ){
151 |
152 |
153 | if( e >= delay ){ /*Max delay time for this edge*/
154 | break;
155 | }
156 |
157 | void *newc = NULL;
158 | if( (newc = queryLWT_PC_plugin(addr)) != origPC ){ /*communication changed*/
159 | fprintf(stderr,"PCBREAK! %p became %p\n",origPC,newc);
160 | break;
161 | }else{
162 | fprintf(stderr,"newc == %p and orig == %p\n",newc,origPC);
163 | }
164 | usleep(DELAY_STEP);
165 | e += DELAY_STEP;
166 |
167 | }
168 |
169 |
170 | }
171 | }
172 |
173 | extern "C" {
174 | void write_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
175 |
176 |
177 | }
178 | }
179 |
--------------------------------------------------------------------------------
/Runtime/Plugins/null_plugin.c:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 |
21 | #include
22 | #include
23 | #include "ct_plugin.h"
24 |
25 | void global_init(){
26 |
27 | fprintf(stderr,"Running Global Init\n");
28 |
29 | }
30 |
31 | void thread_init(){
32 |
33 | fprintf(stderr,"Running Thread Init (T=%lu)\n",(unsigned long)pthread_self());
34 |
35 | }
36 |
37 | void global_deinit(){
38 |
39 | fprintf(stderr,"Running Global Init\n");
40 |
41 | }
42 |
43 | void thread_deinit(){
44 |
45 | fprintf(stderr,"Running Thread Deinit (T=%lu)\n",(unsigned long)pthread_self());
46 |
47 | }
48 |
49 | void read_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
50 |
51 | fprintf(stderr,"M[%p]: T%lu,W@%p -> T%lu,R@%p\n",addr,oldTid,oldPC,newTid,newPC);
52 |
53 | }
54 |
55 | void write_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
56 |
57 | fprintf(stderr,"M[%p]: T%lu,W@%p -> T%lu,W@%p\n",addr,oldTid,oldPC,newTid,newPC);
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/Runtime/Plugins/sfi_plugin.c:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 |
21 | #include
22 | #include
23 | #include "ct_plugin.h"
24 |
25 | int is_gone = 0;
26 | void global_init(){
27 |
28 | fprintf(stderr,"CTRAPS: SFI Plugin Startup\n");
29 | is_gone = 0;
30 |
31 | }
32 |
33 | void thread_init(){
34 |
35 | fprintf(stderr,"CTRAPS: SFI Plugin Thread Init (T=%lu)\n",(unsigned long)pthread_self());
36 |
37 | }
38 |
39 | void global_deinit(){
40 |
41 | fprintf(stderr,"CTRAPS: SFI Plugin Shutdown\n");
42 |
43 | }
44 |
45 | void thread_deinit(){
46 |
47 | fprintf(stderr,"CTRAPS: SFI Plugin Thread Deinit (T=%lu)\n",(unsigned long)pthread_self());
48 |
49 | }
50 |
51 | void read_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
52 |
53 | if( newPC == (void*)0x400c53 &&
54 | *( (unsigned long *)addr) == 0 ){
55 |
56 | fprintf(stderr,"M[%p]: T%lu,W@%p -> T%lu,R@%p\n",addr,oldTid,oldPC,newTid,newPC);
57 | fprintf(stderr,"NULL POINTER!!!\n",addr,oldTid,oldPC,newTid,newPC);
58 | abort();
59 |
60 | }
61 |
62 | }
63 |
64 | void write_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid){
65 |
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/Runtime/RWCalls.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 | #include
22 | #include
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include "LWT.h"
30 | #include "thd_ctr.h"
31 | #include "comm_graph.h"
32 | #include "ct_plugin_manager.h"
33 |
34 | #undef SAMPLING
35 | #undef COMMGRAPH
36 | #undef PLUGIN
37 | #undef RRRW
38 | #undef SLCOPTEXC
39 | #define ARTIFACTEVAL
40 |
41 | using namespace std;
42 |
43 | pthread_mutex_t tidLock;
44 | unsigned long nexttid;
45 | __thread unsigned long myTid;
46 |
47 | pthread_t samplingThread;
48 |
49 | unsigned long num_reads;
50 | unsigned long num_gone_reads;
51 |
52 | #ifdef RRRW
53 | pthread_mutex_t cciprevlock;
54 | std::set *cciprev_glob;
55 | __thread std::set *cciprev;
56 | #endif
57 |
58 | extern "C"{
59 | void thdDestructor(void *v){
60 |
61 | #if defined(PLUGIN)
62 | /*This is where pluggable per-thread shutdown gets called*/
63 | plugin_thd_deinit();
64 | #endif
65 |
66 | #if defined(COMMGRAPH)
67 | dumpCommunicationGraph();
68 | #endif
69 |
70 | #if defined(RRRW)
71 | pthread_mutex_lock(&cciprevlock);
72 | for(auto it = cciprev->begin(), et = cciprev->end(); it != et; it++){
73 | cciprev_glob->insert(*it);
74 | }
75 | pthread_mutex_unlock(&cciprevlock);
76 | #endif
77 |
78 | }
79 | }
80 |
81 | extern "C"{
82 | void *threadStartFunc(void *arg){
83 |
84 | threadInitData *tid = (threadInitData *)arg;
85 |
86 | tlsKey = (pthread_key_t *)malloc( sizeof( pthread_key_t ) );
87 |
88 | pthread_key_create( tlsKey, thdDestructor );
89 |
90 | pthread_setspecific(*tlsKey,(void*)0x1);
91 |
92 | pthread_mutex_lock(&tidLock);
93 | myTid = (nexttid << 48) & 0xffff000000000000;
94 | nexttid++;
95 | if( nexttid > 0xffff ){
96 | nexttid = 1;
97 | }
98 | pthread_mutex_unlock(&tidLock);
99 |
100 | #if defined(PLUGIN)
101 | /*This is where pluggable startup gets called*/
102 | plugin_thd_init();
103 | #endif
104 |
105 | #if defined(COMMGRAPH)
106 | createCommunicationGraph();
107 | #endif
108 |
109 | #if defined(RRRW)
110 | cciprev = new std::set();
111 | #endif
112 |
113 | return (tid->start_routine(tid->arg));
114 |
115 | }
116 | }
117 |
118 | void null_init(){
119 |
120 | }
121 |
122 | void null_trap(void*a,void*b,unsigned long c,void*d,unsigned long e){
123 |
124 | }
125 |
126 | void loadPlugins(){
127 |
128 | char *p = getenv("CTRAP_PLUGIN");
129 | if( p ){
130 |
131 | void *p_handle = dlopen(p, RTLD_LAZY | RTLD_GLOBAL);
132 | if( p_handle != NULL ){
133 |
134 | plugin_init = (void(*)(void))dlsym(p_handle,"global_init");
135 | plugin_thd_init = (void(*)(void))dlsym(p_handle,"thread_init");
136 | plugin_deinit = (void(*)(void))dlsym(p_handle,"global_deinit");
137 | plugin_thd_deinit = (void(*)(void))dlsym(p_handle,"thread_deinit");
138 |
139 | plugin_read_trap = (void(*)(void*,void*,unsigned long,void*,unsigned long))dlsym(p_handle,"read_trap");
140 | plugin_write_trap = (void(*)(void*,void*,unsigned long,void*,unsigned long))dlsym(p_handle,"write_trap");
141 |
142 | }
143 |
144 | }else{
145 |
146 | plugin_init = &null_init;
147 | plugin_thd_init = &null_init;
148 | plugin_deinit = &null_init;
149 | plugin_thd_deinit = &null_init;
150 |
151 | plugin_read_trap = &null_trap;
152 | plugin_write_trap = &null_trap;
153 |
154 |
155 | }
156 |
157 | }
158 |
159 | /*1 Quantum is 100 us*/
160 | #define SAMPLE_QUANTUM 1000
161 | #define NON_SAMPLE_PERIOD 100000
162 |
163 | atomic sampleGeneration;
164 | atomic samplingOn;
165 | void *sampleTimer(void*v){
166 |
167 | srand( time(NULL) );
168 | samplingOn = false;
169 | while(true){
170 |
171 | /*Non-Sampling Period*/
172 | usleep(NON_SAMPLE_PERIOD);
173 |
174 | /*Sample Generation is ordered by samplingOn's fence*/
175 | unsigned long t = sampleGeneration.load(memory_order_acquire);
176 | sampleGeneration.store(t + 1, memory_order_release);
177 | samplingOn.store(true, memory_order_release);
178 |
179 | /*Non-Sampling Period*/
180 | while( samplingOn.load(memory_order_acquire) ){
181 |
182 | usleep(SAMPLE_QUANTUM);
183 |
184 | }
185 |
186 | }
187 |
188 | }
189 |
190 | extern "C" void setup_thd_ctr();
191 | extern "C" int __call_real_pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);
192 | static void __attribute__ ((constructor)) init();
193 | static void init(){
194 |
195 | #if defined(SLCOPTEXC)
196 | num_reads = 0;
197 | num_gone_reads = 0;
198 | #endif
199 |
200 | #if defined(PLUGIN)
201 | /*This is where plugins get loaded*/
202 | loadPlugins();
203 |
204 | /*This is where pluggable global startup gets called*/
205 | plugin_init();
206 |
207 | /*This is where pluggable per-thread startup gets called.*/
208 | plugin_thd_init();
209 | #endif
210 |
211 | #if defined(COMMGRAPH)
212 | createCommunicationGraph();
213 | #endif
214 |
215 | #if defined(RRRW)
216 | cciprev_glob = new std::set();
217 | cciprev = new std::set();
218 | #endif
219 |
220 | nexttid = 1;
221 | myTid = (nexttid << 48) & 0xffff000000000000;
222 | nexttid++;
223 |
224 | pthread_mutex_init(&tidLock,NULL);
225 |
226 | #ifdef EVAL_TRACKMEMSIZE
227 | pthread_mutex_init(&mset_lock,NULL);
228 | #endif
229 |
230 | #ifdef USE_ATOMICS
231 | LWT_table = new LWT_Entry[LWT_ENTRIES];
232 | #else
233 | LWT_table = (LWT_Entry *)calloc(LWT_ENTRIES,sizeof(LWT_Entry));
234 | #endif
235 |
236 |
237 | setup_thd_ctr();
238 |
239 | sigset_t olds;
240 | sigset_t sigs;
241 | sigfillset(&sigs);
242 | pthread_sigmask(SIG_BLOCK,&sigs,&olds);
243 | #if defined(SAMPLING)
244 | __call_real_pthread_create(&samplingThread,NULL,sampleTimer,NULL);
245 | #endif
246 | pthread_sigmask(SIG_SETMASK,&olds,NULL);
247 |
248 | #if defined(RRRW)
249 | pthread_mutex_init(&cciprevlock,NULL);
250 | #endif
251 |
252 | }
253 |
254 |
255 | static void __attribute__ ((destructor)) deinit();
256 | static void deinit(){
257 |
258 | thdDestructor(NULL);
259 |
260 | #if defined(PLUGIN)
261 | /*This is where pluggable global shutdown gets called*/
262 | plugin_deinit();
263 | #endif
264 |
265 | #if defined(RRRW)
266 | pthread_mutex_lock(&cciprevlock);
267 | fprintf(stderr,"CCI-Prev: Dumping\n");
268 | for(auto it = cciprev_glob->begin(), et = cciprev_glob->end(); it != et; it++){
269 | fprintf(stderr,"%p\n",*it);
270 | }
271 | pthread_mutex_unlock(&cciprevlock);
272 | #endif
273 |
274 | #if defined(SLCOPTEXC)
275 | fprintf(stderr,"Dom %lu %lu\n",num_reads,num_gone_reads);
276 | #endif
277 |
278 | #ifdef EVAL_TRACKMEMSIZE
279 | fprintf(stderr,"memsize %lu\n",MSet.size());
280 | #endif
281 |
282 | }
283 |
284 | #if defined(SLCOPTEXC)
285 | extern "C"{
286 | void MemReadExc(void *addr){
287 | num_reads++;
288 | num_gone_reads++;
289 | return;
290 | }
291 | }
292 | #endif
293 |
294 | __thread unsigned long myGeneration;
295 | __thread unsigned long sampleTicks;
296 | #define LOCAL_SAMPLE_TICKS 100
297 | extern "C"{
298 | void MemRead(void *addr){
299 |
300 | #if defined(SLCOPTEXC)
301 | num_reads++;
302 | #endif
303 |
304 | #if defined(SAMPLING)
305 | #if defined(COMMGRAPH) || defined(PLUGIN)
306 | if( !samplingOn.load(memory_order_acquire) ){
307 | /*Not in a sampling period*/
308 | return;
309 |
310 | }
311 |
312 | unsigned long gGen = sampleGeneration.load( memory_order_acquire );
313 | if( myGeneration != gGen ){
314 |
315 | myGeneration = gGen;
316 | sampleTicks = 0;
317 |
318 | }
319 |
320 | /*In a sampling period*/
321 | if( ++sampleTicks > LOCAL_SAMPLE_TICKS ){
322 |
323 | samplingOn.store(false,memory_order_release);
324 |
325 | }
326 | #endif
327 | #endif
328 |
329 | unsigned long index = ((unsigned long)addr) & ((unsigned long)LWT_SIZE);
330 |
331 | #ifdef USE_ATOMICS
332 | unsigned long e = LWT_table[index].load(memory_order_consume);
333 | #else
334 | LWT_Entry e = LWT_table[index];
335 | #endif
336 |
337 | unsigned long you = (e & ((unsigned long)0xffff000000000000));
338 |
339 | #if defined(RRRW)
340 | #if defined(STACKS)
341 | void *pc0 = __builtin_return_address( 0 );
342 | void *pc1 = __builtin_return_address( 1 );
343 | void *pc = (void*)((((unsigned long)pc1) << 24) | ((unsigned long)pc0));
344 | #else
345 | void *pc = __builtin_return_address( 0 );
346 | #endif
347 | #endif
348 |
349 |
350 | if( myTid != you){
351 |
352 | void *oldPC = (void*)(0x0000ffffffffffff & e);
353 |
354 | #if defined(RRRW)
355 | cciprev->insert((unsigned long)oldPC);
356 | #endif
357 |
358 | #if !defined(RRRW)
359 | #if defined(STACKS)
360 | void *pc0 = __builtin_return_address( 0 );
361 | void *pc1 = __builtin_return_address( 1 );
362 | void *pc = (void*)((((unsigned long)pc1) << 24) | ((unsigned long)pc0));
363 | #else
364 | void *pc = __builtin_return_address( 0 );
365 | #endif
366 | #endif
367 |
368 | #if defined(PLUGIN)
369 | plugin_read_trap(addr,oldPC,you,pc,myTid);
370 | #endif
371 |
372 | #if defined(COMMGRAPH)
373 | addToCommunicationTable( oldPC, pc );
374 | #endif
375 |
376 |
377 | }
378 |
379 | #ifdef RRRW
380 | unsigned long who = myTid;
381 | unsigned long where = (0x0000ffffffffffff & ((unsigned long)pc));
382 | unsigned long newe = ( who | where );
383 |
384 | #ifdef USE_ATOMICS
385 | LWT_table[index].store(newe, memory_order_relaxed);
386 | #else
387 | LWT_table[ index ] = newe;
388 | #endif
389 |
390 | #endif
391 |
392 | }
393 | }
394 |
395 |
396 | extern "C"{
397 | void MemWrite(void *addr){
398 |
399 | #if defined(SAMPLING)
400 | if( !samplingOn.load(memory_order_acquire) ){
401 | /*Not in a sampling period*/
402 | return;
403 |
404 | }
405 |
406 | unsigned long gGen = sampleGeneration.load( memory_order_acquire );
407 | if( myGeneration != gGen ){
408 |
409 | myGeneration = gGen;
410 | sampleTicks = 0;
411 | }
412 |
413 | /*In a sampling period*/
414 | if( ++sampleTicks > LOCAL_SAMPLE_TICKS ){
415 |
416 | samplingOn.store(false,memory_order_release);
417 |
418 | }
419 | #endif
420 |
421 |
422 |
423 | //unsigned long selfThd = (unsigned long)pthread_self();
424 |
425 |
426 | unsigned long index = ((unsigned long)addr) & ((unsigned long)LWT_SIZE);
427 |
428 | #ifdef USE_ATOMICS
429 | unsigned long e = LWT_table[index].load(memory_order_consume );
430 | #else
431 | LWT_Entry e = LWT_table[index];
432 | #endif
433 |
434 | #if defined(STACKS)
435 | void *pc0 = __builtin_return_address( 0 );
436 | void *pc1 = __builtin_return_address( 1 );
437 | /*0x8... means "write"*/
438 | void *pc = (void*)((((unsigned long)pc1) << 24) | ((unsigned long)pc0));
439 | #else
440 | void *pc = __builtin_return_address( 0 );
441 | #endif
442 |
443 | unsigned long who = myTid;
444 | unsigned long where = (0x0000ffffffffffff & ((unsigned long)pc));
445 | unsigned long newe = ( who | where );
446 |
447 | #if defined(PLUGIN) || defined(RRRW) || defined(COMMGRAPH)
448 | unsigned long you = (e & ((unsigned long)0xffff000000000000));
449 | if( myTid != you ){
450 |
451 |
452 | void *oldPC = (void*)(0x0000ffffffffffff & e);
453 |
454 | #if defined(RRRW)
455 | cciprev->insert((unsigned long)oldPC);
456 | #endif
457 |
458 | #if defined(PLUGIN)
459 | plugin_write_trap(addr,oldPC,you,pc,myTid);
460 | #endif
461 |
462 | #if defined(COMMGRAPH)
463 | addToCommunicationTable( oldPC, pc );
464 | #endif
465 |
466 | }
467 | #endif
468 |
469 | #ifdef ARTIFACTEVAL
470 | fprintf(stderr,"LWT[%x]= (Thd 0x%x, PC 0x%x)\n",addr,pthread_self(),pc);
471 | #endif
472 |
473 | #ifdef USE_ATOMICS
474 | LWT_table[index].store(newe, memory_order_relaxed );
475 | #else
476 | LWT_table[ index ] = newe;
477 | #endif
478 |
479 | #ifdef EVAL_TRACKMEMSIZE
480 | pthread_mutex_lock(&mset_lock);
481 | MSet.insert( (unsigned long)addr );
482 | pthread_mutex_unlock(&mset_lock);
483 | #endif
484 |
485 | }
486 | }
487 |
488 | extern "C"{
489 | void *queryLWT_PC_plugin(void *addr){
490 |
491 | unsigned long index = ((unsigned long)addr) & ((unsigned long)LWT_SIZE);
492 |
493 | unsigned long e = LWT_table[index];
494 |
495 | return (void*)(0x0000ffffffffffff & e);
496 |
497 | }
498 | }
499 |
--------------------------------------------------------------------------------
/Runtime/comm_graph.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 |
21 |
22 | #ifndef _GNU_SOURCE
23 | #define _GNU_SOURCE
24 | #endif
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 | #include "comm_graph.h"
33 |
34 | using namespace std;
35 |
36 | pthread_mutex_t communication_table_lock;
37 |
38 | __thread __gnu_cxx::hash_map > *communication_table;
39 |
40 | void dumpCommunicationGraph(){
41 |
42 | char *p = getenv("CG_GRAPH");
43 |
44 | if(p){
45 |
46 | char *s = (char *)malloc(sizeof(char) * strlen(p) + strlen("executable") + 18);
47 | sprintf(s,"%s%s%lx",p,"executable",(unsigned long)pthread_self());
48 | ofstream fs(s);
49 | //__gnu_cxx::hash_map >::iterator ct_iter, ct_end;
50 | for( auto ct_iter = communication_table->begin(), ct_end = communication_table->end(); ct_iter != ct_end; ct_iter++ ){
51 |
52 | //__gnu_cxx::hash_map< unsigned long, unsigned long>::iterator sink_iter, sink_end;
53 | for( auto sink_iter = ct_iter->second.begin(), sink_end = ct_iter->second.end(); sink_iter != sink_end; sink_iter++ ){
54 |
55 | //string w = ct_iter->first & 0x8000000000000000 ? string("w") : string("r");
56 | #if defined(STACKS)
57 | unsigned long src0 = (ct_iter->first & 0x0000FFFFFF000000) >> 24;
58 | unsigned long src1 = (ct_iter->first & 0xFFFFFF);
59 | unsigned long sink0 = (sink_iter->first & 0x0000FFFFFF000000) >> 24;
60 | unsigned long sink1 = (sink_iter->first & 0xFFFFFF);
61 | fs << hex << src0 << ":" << src1 << " " << sink0 << ":" << sink1 << " " << dec << sink_iter->second << endl;
62 | #else
63 | unsigned long src = (ct_iter->first & 0xFFFFFF);
64 | unsigned long sink = (sink_iter->first & 0xFFFFFF);
65 | fs << hex << src << " " << sink << " " << dec << sink_iter->second << endl;
66 | #endif
67 |
68 | }
69 |
70 | }
71 |
72 | }else{
73 |
74 | //__gnu_cxx::hash_map >::iterator ct_iter, ct_end;
75 | for( auto ct_iter = communication_table->begin(), ct_end = communication_table->end(); ct_iter != ct_end; ct_iter++ ){
76 |
77 | //__gnu_cxx::hash_map< unsigned long, unsigned long>::iterator sink_iter, sink_end;
78 | for( auto sink_iter = ct_iter->second.begin(), sink_end = ct_iter->second.end(); sink_iter != sink_end; sink_iter++ ){
79 |
80 | cerr << hex << ct_iter->first << "-" << sink_iter->first << ":" << dec << sink_iter->second << endl;
81 |
82 | }
83 |
84 | }
85 |
86 | }
87 |
88 | }
89 |
90 | void createCommunicationGraph(){
91 |
92 | communication_table = new __gnu_cxx::hash_map >();
93 |
94 | }
95 |
96 | void addToCommunicationTable( void *src, void *sink ){
97 |
98 |
99 | auto ct_iter = communication_table->find( (unsigned long)src);
100 | if( ct_iter == communication_table->end() ){
101 |
102 | (*communication_table)[ (unsigned long)src ] = __gnu_cxx::hash_map();
103 | (*communication_table)[ (unsigned long)src ][ (unsigned long)sink ] = 1;
104 |
105 |
106 | }else{
107 |
108 | if( (ct_iter->second ).find( (unsigned long)sink ) == (ct_iter->second).end() ){
109 |
110 | (ct_iter->second )[ (unsigned long)sink ] = 1;
111 |
112 | }else{
113 |
114 | (ct_iter->second )[ (unsigned long)sink ]++;
115 |
116 | }
117 |
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/Runtime/comm_graph.h:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #include
21 |
22 | using namespace std;
23 | #undef STACKS
24 | void dumpCommunicationGraph();
25 | void createCommunicationGraph();
26 | void addToCommunicationTable( void *src, void *sink );
27 |
--------------------------------------------------------------------------------
/Runtime/ct_plugin.h:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #ifdef __cplusplus
21 | extern "C"{
22 | #endif
23 | void global_init();
24 | void thread_init();
25 | void global_deinit();
26 | void thread_deinit();
27 | void read_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid);
28 | void write_trap(void *addr, void *oldPC, unsigned long oldTid, void *newPC, unsigned long newTid);
29 | #ifdef __cplusplus
30 | }
31 | #endif
32 |
--------------------------------------------------------------------------------
/Runtime/ct_plugin_manager.h:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 | void (*plugin_thd_init)(void);
20 | void (*plugin_thd_deinit)(void);
21 | void (*plugin_init)(void);
22 | void (*plugin_deinit)(void);
23 |
24 | void (*plugin_read_trap)(void *, void *, unsigned long, void *,unsigned long);
25 | void (*plugin_write_trap)(void *, void *, unsigned long, void *,unsigned long);
26 |
--------------------------------------------------------------------------------
/Runtime/thd_ctr.c:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | #define _GNU_SOURCE
21 | #include
22 | #include
23 | #include
24 | #include
25 |
26 | typedef struct _threadInitData{
27 |
28 | void *(*start_routine)(void*);
29 | void *arg;
30 |
31 | } threadInitData;
32 |
33 | /*Thread Constructor Stuff*/
34 | void (*thd_ctr)(void*,void*(*)(void*));
35 |
36 | extern void *threadStartFunc(void *arg);
37 |
38 | static int (* __real_pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void*), void *);
39 | int pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg){
40 |
41 |
42 | threadInitData *tid = (threadInitData*)malloc(sizeof(*tid));
43 |
44 | tid->start_routine = start_routine;
45 |
46 | tid->arg = arg;
47 |
48 | int ret = __real_pthread_create(thread,attr,threadStartFunc,(void*)tid);
49 |
50 | return ret;
51 |
52 | }
53 |
54 | int __call_real_pthread_create( pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg){
55 |
56 | __real_pthread_create(thread,attr,start_routine,arg);
57 |
58 | }
59 |
60 | void setup_thd_ctr(){
61 |
62 | dlerror();
63 |
64 | __real_pthread_create = (int (*)(pthread_t *, const pthread_attr_t *, void *(*)(void*), void *)) dlsym(RTLD_NEXT, "pthread_create");
65 |
66 | if( __real_pthread_create == NULL ){
67 |
68 | fprintf(stderr,"Couldn't load pthread_create %s\n",dlerror());
69 | exit(-1);
70 |
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/Runtime/thd_ctr.h:
--------------------------------------------------------------------------------
1 | /*
2 | ==CTraps -- A GCC Plugin to instrument shared memory accesses==
3 |
4 | Copyright (C) 2012 Brandon Lucia
5 |
6 | This program is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | This program is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with this program. If not, see .
18 | */
19 |
20 | __thread pthread_key_t *tlsKey;
21 |
22 | typedef struct _threadInitData{
23 |
24 | void *(*start_routine)(void*);
25 | void *arg;
26 |
27 | } threadInitData;
28 |
29 |
--------------------------------------------------------------------------------
/Test/Makefile:
--------------------------------------------------------------------------------
1 | build:
2 | gcc-4.8 -fplugin=$(PWD)/../Compiler/RWInst.so ../Test/test.c -O1 -g -o ../Test/test -L $(PWD)/../Runtime/ -lRWCalls
3 |
4 | run: build
5 | -DYLD_LIBRARY_PATH=$(PWD)/../Runtime ./test
6 |
7 | clean:
8 | -rm -rf ./test ./test.dSYM
9 |
--------------------------------------------------------------------------------
/Test/test.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | int *x;
6 |
7 | void *worker(void *v){
8 |
9 | int i;
10 | for(i = 0; i < 10000; i++){
11 | (*x)++;
12 | }
13 |
14 | }
15 |
16 | int main(int argc, char *argv){
17 |
18 | x = malloc(sizeof(int));
19 | pthread_t t1,t2;
20 | pthread_create(&t1,NULL,worker,NULL);
21 | pthread_create(&t2,NULL,worker,NULL);
22 |
23 | pthread_join(t1,NULL);
24 | pthread_join(t2,NULL);
25 | fprintf(stderr,"FINAL VALUE: (%x)=%d\n",x,*x);
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/paper.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/blucia0a/CTraps-gcc/c70b58a59572f5fad39a7b18106148b35f2e4910/paper.pdf
--------------------------------------------------------------------------------