Skip to content

Commit

Permalink
Merge pull request #12 from javierhonduco/cpp-improve-test-binary
Browse files Browse the repository at this point in the history
cpp: Add more comprehensive example
  • Loading branch information
kakkoyun committed Jun 30, 2022
2 parents c582933 + 7a5dc59 commit 3661452
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
14 changes: 12 additions & 2 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
.PHONY: build
build: pie no-pie with-clang
build: pie pie-with-fp no-pie no-pie-with-fp clang clang-with-fp

pie:
docker build -t parca-demo:cpp-pie --build-arg GCC_FLAGS='-g' .

pie-with-fp:
docker build -t parca-demo:cpp-pie-with-fp --build-arg GCC_FLAGS='-g -fno-omit-frame-pointer' .


no-pie:
docker build -t parca-demo:cpp --build-arg GCC_FLAGS='-g -no-pie' .

with-clang:
no-pie-with-fp:
docker build -t parca-demo:cpp-with-fp --build-arg GCC_FLAGS='-g -no-pie -fno-omit-frame-pointer' .

clang:
docker build -f Dockerfile.clang -t parca-demo:cpp-clang --build-arg CLANG_FLAGS='-g' .

clang-with-fp:
docker build -f Dockerfile.clang -t parca-demo:cpp-clang-with-fp --build-arg CLANG_FLAGS='-g -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer' .
50 changes: 30 additions & 20 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
#include <fcntl.h>
#include <iostream>
#include <unistd.h>

unsigned int fibonacci(unsigned int n)
{
if (n == 0)
return 0;

unsigned int a = 1, b = 1;
for (unsigned int i = 3; i <= n; ++i)
{
unsigned int fib = a + b;
a = b;
b = fib;
}
int __attribute__((noinline)) top() {
for (int i = 0; i < 100000; i++) {
int fd = open("/", O_DIRECTORY);
close(fd);
}

return b;
return 0;
}

int main()
{
int limit = 1000000;
for (int i = 0; i < limit; i++)
{
std::cout << "F(" << i << ") = " << fibonacci(i) << std::endl;
}
return 0;
// ones
int __attribute__((noinline)) c1() { return top(); }

int __attribute__((noinline)) b1() { return c1(); }

int __attribute__((noinline)) a1() { return b1(); }

// twos
int __attribute__((noinline)) c2() { return top(); }

int __attribute__((noinline)) b2() { return c2(); }

int __attribute__((noinline)) a2() { return b2(); }

int main() {
while (true) {
std::cout << "Calling a1" << std::endl;
a1();
std::cout << "Calling a2" << std::endl;
a2();
}
return 0;
}

0 comments on commit 3661452

Please sign in to comment.