Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of sbrk #113

Closed
darleybarreto opened this issue Aug 30, 2020 · 9 comments
Closed

Implementation of sbrk #113

darleybarreto opened this issue Aug 30, 2020 · 9 comments

Comments

@darleybarreto
Copy link
Contributor

In order to implement dynamic allocated str (#91) and more, today I worked on bringing malloc support. I am almost there (I altered the necessary python code for that), the only thing remaining is sbrk function, following this implementation I would need to link the information about the end of the free memory on the host. What would be the best way of doing that?

@windelbouwman
Copy link
Owner

I'm not sure how to do that, but you could use a linker script to do so? From the memory map you could define a symbol and use that in an assembly level implementation of sbrk.

I'm not sure what you want to achieve, do you want to implement your own sbrk function?

@darleybarreto
Copy link
Contributor Author

I want to implement malloc, so we could allocate arbitrary objects inside ppci (for unix based systems with brk similar syscalls). The "easy" way would be to implement brk syscall and sbrk wrapper, then malloc using sbrk.

@windelbouwman
Copy link
Owner

If you want to implement syscalls, I would checkout this sample: https://github.com/windelbouwman/ppci/blob/master/examples/linux64/hello-make/hello.c

@darleybarreto
Copy link
Contributor Author

Oh yeah, I have seen this one, and I altered the implementation of do_inline_asm, so one can get the return of a syscall in a variable like this:

long syscall(long nr, long a, long b, long c)
{   
    long ret;

    asm(
        "mov rax, %0 \n"
        "mov rdi, %1 \n"
        "mov rsi, %2 \n"
        "mov rdx, %3 \n"
        "syscall \n"
        : "=r" (ret)
        : "r" (nr), "r" (a), "r" (b), "r" (c)
        : "rax", "rdi", "rsi", "rdx"
    );

    return ret;
}

In ret we would have the break point given by the system, so sbrk would work with this value to give the start memory address when called inside a malloc implementation.

@darleybarreto
Copy link
Contributor Author

I worked out the syscalls and made a port of a simple malloc. So now we can do something like this

#include <stdio.h>
#include <stdlib.h>

int main(){
   int *int_vec = (int*)malloc(10*sizeof(int));
   for (int i =0; i < 10; i++){
      int_vec[i] = i*i + 2;
   }
   
   for (int i =0; i < 10; i++){
      printf("%d",int_vec[i]);
   }

   free(int_vec);

   exit(0);
}

@windelbouwman
Copy link
Owner

Cool! Nicely done!!

@darleybarreto
Copy link
Contributor Author

I also took the liberty to change some file structures inside librt/libc, I guess you will check if it is ok when reviewing the PR. I've never submitted a PR before, should I do something w.r.t CI/CD?

@windelbouwman
Copy link
Owner

Sure, feel free to create a PR, lets see what happens :). No need to do any CI/CD. If you want, you can run tests locally with tox / pytest as described in the docs, but I guess this is done as well with CI/CD on github.

@darleybarreto
Copy link
Contributor Author

Closed by #114

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants