flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > Maximal allocation of memory in Linux fasm

Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20520
Location: In your JS exploiting you and your system
revolution 14 Feb 2019, 09:41
This is a follow up to another topic for WIndows.

The default memory allocation in Linux is 16384kB. It's hardcoded in the source. The user can always override with the -m setting. But there are two main problems with this
  • The default limit is quite small and only suitable for small tasks
  • There is no error checking in the code, so if you set the -m value too large fasm crashes
Both problems can be fixed with a small code change to SYSTEM.INC.
Code:
;..
init_memory:
        mov     eax,esp
        and     eax,not 0FFFh
        add     eax,1000h-10000h
        mov     [stack_limit],eax
        xor     ebx,ebx
        mov     eax,45
        int     0x80
        mov     [additional_memory],eax
        mov     ecx,[memory_setting]
        shl     ecx,10
        jz      allocate_max_memory
        add     ecx,eax
        jnc     allocate_memory
      allocate_max_memory:
        mov     ecx,-1 shl 10
      allocate_memory:
        mov     ebx,ecx
      allocate_memory_loop:
        mov     eax,45
        int     0x80
        cmp     eax,ecx
        jae     allocate_memory_okay
        cmp     eax,ebx
        jae     allocate_memory_end_okay
        mov     ecx,ebx         ;adjust the end point
      allocate_memory_end_okay:
        mov     edx,ecx
        sub     edx,eax
        dec     edx
        bsr     edx,edx
        xor     ebx,ebx
        bts     ebx,edx
        add     ebx,eax
        cmp     dl,12
        jae     allocate_memory_loop
      allocate_memory_okay:
        mov     [memory_end],eax
        sub     eax,[additional_memory]
        shr     eax,2
        add     eax,[additional_memory]
        mov     [additional_memory_end],eax
        mov     [memory_start],eax
        ret
;...    
If the invocation uses the -m setting then that amount of memory is requested. If no -m setting if given then the maximum amount of memory is requested. If the request fails then the code tries successively smaller requests and searches for the most amount of memory it can use up to the requested limit.

I got this result with the x64 version of fasm I posted in another thread
Code:
flat assembler  version 1.73.08  (4182188 kilobytes memory)    
And with the 32-bit version I get this
Code:
flat assembler  version 1.73.08  (3917052 kilobytes memory)    
To check that the memory is only reserved and not committed I started 16 copies of x64 fasm with ~4GB allocation each, a total of ~64GB of memory would be used. I only have 4GB in the test machine. All 16 started okay and allocated the memory okay. Output from ps
Code:
0  1000 29281 10566  20   0 4167544    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29300 10566  20   0 4178820    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29307 10566  20   0 4170244    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29318 10566  20   0 4174980    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29327 10566  20   0 4189256    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29334 10566  20   0 4180716    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29341 10566  20   0 4173552    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29348 10566  20   0 4163380    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29355 10566  20   0 4165800    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29362 10566  20   0 4176976    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29369 10566  20   0 4181436    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29376 10566  20   0 4182996    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29383 10566  20   0 4178572    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29390 10566  20   0 4175316    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29397 10566  20   0 4171436    4 signal t    pts/8      0:00 ./fasm fasm.asm
0  1000 29404 10566  20   0 4192836    4 signal t    pts/8      0:00 ./fasm fasm.asm    
And when I release the signal they all complete okay.

The difference in the allocation size is due to the randomisation of memory allocation addresses in the kernel.
Post 14 Feb 2019, 09:41
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.