Elijah Mirecki
I’m currently working on implementing system calls in BlarbVM (see my previous post on it), so I am trying to get comfortable using only syscalls.
In Linux, you can change the heap size through brk (syscall #12). To write a string to standard output, you must provide a pointer to a location on the heap.
The following code:
-
Get’s the end of the heap address
-
Increments the end of the heap address by 6 bytes
-
Set’s the value of those 6 bytes to a null terminated “hello\n” string
-
Runs a syscall on the desired heap position to print “hello\n”
#include <syscall.h>
// http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/
int main(int argc, char **argv) {
long brkPoint;
// Get the brk size
if ((brkPoint = syscall(12)) == -1) {
perror("syscall");
return 1;
}
// Increase heap size
if ((brkPoint = syscall(12, brkPoint + 6)) == -1) {
perror("syscall");
return 1;
}
// Add the string "hello\n" to the heap
*((char*)(brkPoint - 6)) = 'h';
*((char*)(brkPoint - 5)) = 'e';
*((char*)(brkPoint - 4)) = 'l';
*((char*)(brkPoint - 3)) = 'l';
*((char*)(brkPoint - 2)) = 'o';
*((char*)(brkPoint - 1)) = '\n';
*((char*)(brkPoint)) = '\0';
// Write the address 4 bytes before the end of the brk point (end of heap)
if (syscall(1, 1, (char*)(brkPoint - 6), 6) == -1) {
perror("syscall");
return 1;
}
return 0;
}