Thanks to visit codestin.com
Credit goes to github.com

Skip to content

arturo32/HowPointersWork-server

Repository files navigation

How Pointers Work - Server

This server receives HTTP requests with C/C++ code and returns the stackstrace of the first 300 lines that are run. The stacktrace contains the state of each line, both in the stack and heap: names, types and addresses of variables, the current function name and signature, etc.

It was inspired by this article on Tork engine and this fork of Philip Guo's Python Tutor.

This system also has a frontend repository.

How it works

There are 2 kinds of containers in this backend server (actually 3, but let's not focus on that for now): one that holds the Go application that uses Tork and other that is the sandbox where the C/C++ code is compiled and run with Valgrind.

Diagram with three containers: the first is an application container that hosts the “Go” image. This image has an arrow pointing to the next container, the sandbox that contains the Ubuntu image. These two containers are in the backend region. The first container exchanges HTTP communication arrows with an external container, the frontend with the Node image.

See the big arrow in the image above? That is Tork managing and creating a container as a task when it receives a request. Inside this new container the code is compiled and run through a modified version of Valgrind (made by Philip Guo). It then returns the stacktrace of the code to the client.

Sequence diagram: The user accesses the frontend, which sends a request to the backend. The backend creates an Ubuntu instance with bash, which compiles the code and executes a Python script. This, in turn, executes valgrind, which returns the stack trace. The stack trace is parsed in the Python script, which returns the stack trace to bash. The Ubuntu instance is then terminated and returns the stack trace to the backend, which responds to the frontend.

The extra container mentioned at the beginning is the database used by Tork to manage tasks: PostgreSQL.

One caveat of this system is that the Valgrind version is very old: from 2013/2014. The original code run in a Ubuntu 14 container, with gcc 4.8.4. I managed to update to a Debian 9 container, with gcc 6.3.0 pulled from archived repositories of apt. Above that, some weird errors happens that I don't remember. With containers with gcc 10 it changes the error to "lack of debug symbols" (or something like that) from newer versions of libc6-dbg.

Running

This project is composed of two images: One that will run the Go program and other that will be run by Tork.

So we need to first build the image that Tork will use (the one that will compile and run the custom valgrind):

sudo docker build -f Dockerfile -t gcc-compiler .

Then, we build the main image:

sudo docker build -f Dockerfile.main -t hpw-server .

With docker compose

apt install haveged # docker compose use of random things like.. name creation?
sudo docker compose up -d

To see logs, run:

sudo docker compose logs hpw-server

Without docker compose

We build and run postgres:

sudo docker run -d --name tork-postgres -p 5432:5432 -e POSTGRES_PASSWORD=tork -e POSTGRES_USER=tork  -e PGDATA=/var/lib/postgresql/data/pgdata   -e POSTGRES_DB=tork postgres:15.3

As "Docker in docker" is unadvised, we run the main container using the -v flag that will bind the most internal container image docker socket to the external docker. Ps.: --network=host/--net=host don't work on Windows.

sudo docker run -v /var/run/docker.sock:/var/run/docker.sock --network=host -d hpw-server

Execute a code snippet. Example

curl \
  -s \
  -X POST \
  -H "content-type:application/json" \
  -d '{"language":"c","code":"#include <stdio.h>\n\nint main(){\nint i = 23;\nint *k = &i;\nreturn 0;\n}"}' \
  http://localhost:8000/execute

You can try changing the language to c++.

How to update Tork in the future

go list -m -u github.com/runabol/tork #find last version
go get github.com/runabol/[email protected] #get last version
go mod tidy #syncronize dependencies

to-do

  • Make it don't jump commands inside for loops.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published