citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 3c6dbd5a61f1d5f68b963551a4d5dd4b4f3bf01a
parent 9afb38d0780aabf2d0c3faf756d31871fa46626e
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Tue, 22 Mar 2016 22:57:33 -0600

instrument: try to privatize injected identifiers

Diffstat:
Minstrument/instrumenter.cc | 31++++++++++++++-----------------
Ainstrument/runtime_h.h | 10++++++++++
Mlib/runtime.c | 10+++++-----
Alib/runtime.h | 8++++++++
Dlib/scv_runtime.h | 11-----------
Mt/fibonacci.t | 42+++++++++++++++++++++++++-----------------
Mt/for.t | 24++++++++++++++++--------
Mt/hello_world.t | 24++++++++++++++++--------
Mt/if.t | 36++++++++++++++++++++++--------------
Mt/return.t | 28++++++++++++++++++----------
Mt/switch.t | 24++++++++++++++++--------
Mt/while.t | 24++++++++++++++++--------
12 files changed, 166 insertions(+), 106 deletions(-)

diff --git a/instrument/instrumenter.cc b/instrument/instrumenter.cc @@ -14,6 +14,7 @@ #include <clang/Frontend/CompilerInstance.h> #include "instrumenter.h" +#include "runtime_h.h" bool instrumenter::VisitVarDecl(VarDecl *d) @@ -29,24 +30,19 @@ instrumenter::VisitStmt(Stmt *s) Stmt *stmt_to_inst = NULL; if (isa<IfStmt>(s)) { - IfStmt *IfStatement = cast<IfStmt>(s); - stmt_to_inst = IfStatement->getCond(); + stmt_to_inst = cast<IfStmt>(s)->getCond(); } else if (isa<ForStmt>(s)) { - ForStmt *ForStatement = cast<ForStmt>(s); - stmt_to_inst = ForStatement->getCond(); + stmt_to_inst = cast<ForStmt>(s)->getCond(); } else if (isa<WhileStmt>(s)) { - WhileStmt *WhileStatement = cast<WhileStmt>(s); - stmt_to_inst = WhileStatement->getCond(); + stmt_to_inst = cast<WhileStmt>(s)->getCond(); } else if (isa<SwitchStmt>(s)) { - SwitchStmt *SwitchStatement = cast<SwitchStmt>(s); - stmt_to_inst = SwitchStatement->getCond(); + stmt_to_inst = cast<SwitchStmt>(s)->getCond(); } else if (isa<ReturnStmt>(s)) { - ReturnStmt *ReturnStatement = cast<ReturnStmt>(s); - stmt_to_inst = ReturnStatement->getRetValue(); + stmt_to_inst = cast<ReturnStmt>(s)->getRetValue(); } /* else if (isa<BreakStmt>(s) || isa<ContinueStmt>(s) || @@ -62,7 +58,7 @@ instrumenter::VisitStmt(Stmt *s) if (stmt_to_inst == NULL) return true; - ss << "(++lines[" << line << "], "; + ss << "(++_scv_lines[" << line << "], "; if (TheRewriter.InsertTextBefore(stmt_to_inst->getLocStart(), ss.str())) // writing failed, don't attempt to add ")" return true; @@ -170,21 +166,22 @@ MyFrontendAction::EndSourceFileAction() unsigned int tu_number = get_src_number(); std::stringstream ss; + // Embed the header directly in the primary source file. + ss << runtime_h << std::endl; - ss << "#include <scv_runtime.h>" << std::endl; // Define storage for coverage data - ss << "static uint64_t lines[" << num_lines << "];" << std::endl; + ss << "static uint64_t _scv_lines[" << num_lines << "];" << std::endl; // Always declare this. The next TU will overwrite this or there won't // be a next TU. - ss << "struct scv_node node" << tu_number + 1 << ";" << std::endl; + ss << "struct _scv_node _scv_node" << tu_number + 1 << ";" << std::endl; // Define this translation units main book keeping data structure - ss << "struct scv_node node" << tu_number << " = {" << std::endl - << " .lines_ptr = lines," << std::endl + ss << "struct _scv_node _scv_node" << tu_number << " = {" << std::endl + << " .lines_ptr = _scv_lines," << std::endl << " .size = " << num_lines << "," << std::endl << " .file_name = \"" << file_name << "\"," << std::endl; - ss << " .next = &node" << tu_number + 1 << "," << std::endl; + ss << " .next = &_scv_node" << tu_number + 1 << "," << std::endl; ss << "};" << std::endl; TheRewriter.InsertTextAfter(start, ss.str()); diff --git a/instrument/runtime_h.h b/instrument/runtime_h.h @@ -0,0 +1,10 @@ +static const char *runtime_h = +"#include <stdint.h>\n" +"struct _scv_node {\n" +" uint64_t *lines_ptr;\n" +" uint64_t size;\n" +" const char *file_name;\n" +" struct _scv_node *next;\n" +"};\n" +"void libscv_init();\n" +; diff --git a/lib/runtime.c b/lib/runtime.c @@ -12,10 +12,10 @@ #endif #include <unistd.h> // read, getpid, getppid, getpgrp -#include "scv_runtime.h" +#include "runtime.h" /* Entry point into instrumented application */ -extern struct scv_node node0; +extern struct _scv_node _scv_node0; void send_metadata(int); void send_execution_data(int); @@ -68,7 +68,7 @@ send_metadata(int fd) { size_t file_name_sz; uint64_t num_tus = 0; - struct scv_node walk = node0; + struct _scv_node walk = _scv_node0; /* Send the total number of translation unit records we'll send later */ while (walk.size != 0) { @@ -87,7 +87,7 @@ send_metadata(int fd) xwrite(fd, &parent_process_id, sizeof(pid_t)); xwrite(fd, &process_group, sizeof(pid_t)); - walk = node0; + walk = _scv_node0; /* Send translation unit records */ while (walk.size != 0) { /* Send file name size and then the file name itself. */ @@ -105,7 +105,7 @@ send_metadata(int fd) void send_execution_data(int fd) { - struct scv_node walk = node0; + struct _scv_node walk = _scv_node0; while (walk.size != 0) { /* Write execution buffer, one 8 byte counter per source line */ diff --git a/lib/runtime.h b/lib/runtime.h @@ -0,0 +1,8 @@ +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); diff --git a/lib/scv_runtime.h b/lib/scv_runtime.h @@ -1,11 +0,0 @@ -#include <stdint.h> - -struct scv_node { - /* long long in C99 is also guaranteed to be 64 bits */ - uint64_t *lines_ptr; - uint64_t size; - const char *file_name; - struct scv_node *next; -}; - -void libscv_init(); diff --git a/t/fibonacci.t b/t/fibonacci.t @@ -48,14 +48,22 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[31]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[31]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 31, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; #include <stdio.h> #include <stdlib.h> @@ -63,12 +71,12 @@ struct scv_node node0 = { long long fibonacci(long long n) { - if ((++lines[7], n == 0)) - return (++lines[8], 0); - else if ((++lines[9], n == 1)) - return (++lines[10], 1); + if ((++_scv_lines[7], n == 0)) + return (++_scv_lines[8], 0); + else if ((++_scv_lines[9], n == 1)) + return (++_scv_lines[10], 1); - return (++lines[12], (++lines[12], fibonacci(n - 1)) + (++lines[12], fibonacci(n - 2))); + return (++_scv_lines[12], (++_scv_lines[12], fibonacci(n - 1)) + (++_scv_lines[12], fibonacci(n - 2))); } int @@ -76,16 +84,16 @@ main(int argc, char *argv[]) {libscv_init(); long long n; - if ((++lines[20], argc != 2)) { - (++lines[21], fprintf(stderr, "usage: %s <N>", argv[0])); - return (++lines[22], 1); + if ((++_scv_lines[20], argc != 2)) { + (++_scv_lines[21], fprintf(stderr, "usage: %s <N>", argv[0])); + return (++_scv_lines[22], 1); } - n = (++lines[25], atoi(argv[1])); + n = (++_scv_lines[25], atoi(argv[1])); - (++lines[27], fprintf(stderr, "result: %lli", (++lines[27], fibonacci(n)))); + (++_scv_lines[27], fprintf(stderr, "result: %lli", (++_scv_lines[27], fibonacci(n)))); - return (++lines[29], 0); + return (++_scv_lines[29], 0); } EOF diff --git a/t/for.t b/t/for.t @@ -29,25 +29,33 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[12]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[12]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 12, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; int main(void) {libscv_init(); int i; - for (i = 0; (++lines[6], i < 19); i++) { + for (i = 0; (++_scv_lines[6], i < 19); i++) { i++; } - return (++lines[10], i); + return (++_scv_lines[10], i); } EOF diff --git a/t/hello_world.t b/t/hello_world.t @@ -26,22 +26,30 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[9]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[9]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 9, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; #include <stdio.h> int main(void) {libscv_init(); - (++lines[6], fprintf(stderr, "hello, world!")); - return (++lines[7], 0); + (++_scv_lines[6], fprintf(stderr, "hello, world!")); + return (++_scv_lines[7], 0); } EOF diff --git a/t/if.t b/t/if.t @@ -36,33 +36,41 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[21]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[21]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 21, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; #include <stdlib.h> int main(int argc, char *argv[]) {libscv_init(); - if ((++lines[6], argc == 1)) - return (++lines[7], 1); + if ((++_scv_lines[6], argc == 1)) + return (++_scv_lines[7], 1); else - (++lines[9], exit(14)); + (++_scv_lines[9], exit(14)); - if ((++lines[11], argc == 2)) { - return (++lines[12], 5); + if ((++_scv_lines[11], argc == 2)) { + return (++_scv_lines[12], 5); } - else if ((++lines[14], argc == 3)) { - return (++lines[15], 0); + else if ((++_scv_lines[14], argc == 3)) { + return (++_scv_lines[15], 0); } else { - (++lines[18], exit(0)); + (++_scv_lines[18], exit(0)); } } EOF diff --git a/t/return.t b/t/return.t @@ -29,25 +29,33 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[12]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[12]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 12, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; int foo() { - return (++lines[2], 0); + return (++_scv_lines[2], 0); } int main(void) {libscv_init(); - return (++lines[6], 10); + return (++_scv_lines[6], 10); - return (++lines[8], 10 + 10); + return (++_scv_lines[8], 10 + 10); - return (++lines[10], (++lines[10], foo())); + return (++_scv_lines[10], (++_scv_lines[10], foo())); } EOF diff --git a/t/switch.t b/t/switch.t @@ -32,28 +32,36 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[15]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[15]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 15, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; int main(void) {libscv_init(); int i; - switch ((++lines[6], i)) { + switch ((++_scv_lines[6], i)) { case 0: break; case 1: break; } - return (++lines[13], 0); + return (++_scv_lines[13], 0); } EOF diff --git a/t/while.t b/t/while.t @@ -30,14 +30,22 @@ $project->compile(); my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; -#include <scv_runtime.h> -static uint64_t lines[13]; -struct scv_node node1; -struct scv_node node0 = { - .lines_ptr = lines, +#include <stdint.h> +struct _scv_node { + uint64_t *lines_ptr; + uint64_t size; + const char *file_name; + struct _scv_node *next; +}; +void libscv_init(); + +static uint64_t _scv_lines[13]; +struct _scv_node _scv_node1; +struct _scv_node _scv_node0 = { + .lines_ptr = _scv_lines, .size = 13, .file_name = "$tmp_dir/source_0.c", - .next = &node1, + .next = &_scv_node1, }; int main(void) @@ -45,11 +53,11 @@ main(void) int i; i = 0; - while ((++lines[7], i < 17)) { + while ((++_scv_lines[7], i < 17)) { i++; } - return (++lines[11], i); + return (++_scv_lines[11], i); } EOF