boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

007-malloc-roundtrip.c (573B)


      1 /* Allocator smoke: malloc -> brk syscall (via boot2-syscall.c::brk).
      2  * Writes a sentinel through the returned pointer and reads it back so
      3  * a successful malloc that returned a bad address is still caught. */
      4 typedef unsigned long size_t;
      5 
      6 extern void *malloc (size_t size);
      7 extern long sys_write (long fd, long buf, long len);
      8 
      9 int
     10 main (void)
     11 {
     12   int *p = (int *) malloc (4);
     13   if (!p)
     14     {
     15       sys_write (1, (long) "malloc-null\n", 12);
     16       return 1;
     17     }
     18   *p = 0x000a4b4f;              /* "OK\n\0" little-endian */
     19   sys_write (1, (long) p, 3);
     20   return 0;
     21 }