flat assembler
Message board for the users of flat assembler.

Index > Main > Align section .bbs or import ?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 08:35
32bits program.
Today in fasmw 1.73 i get bug.
If i write in my big code (compiled exe file 94208 bytes) and in any places code.
rept 16 { nop } ; I think this unaligned in some place, some data using directx 11 or SSE.
I get bug in render Directx11.

If I write in section '.bss' readable writeable
align 16 I get tha same bug.

My question how write start org address for section .bbs or section import ?

And how in exe file hierarchy section ?
0) Section import
1) Section data
3) Section code


Last edited by Roman on 05 Mar 2024, 09:12; edited 1 time in total
Post 05 Mar 2024, 08:35
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 08:56
wrote:
My question how write start org address for section .bbs or section import ?
Use rva.
Code:
rb rva and 1
rw ( rva $ and 2 ) shr 1
rd ( rva $ and 4 ) shr 2
rq ( rva $ and 8 ) shr 3

; or

rb ( 0 - rva $ and 15 )    
Since the sections in memory are aligned by 4096 bytes (page), then rva can align data up to 4096 (alignment for the in-memory and in-file sections is specified in the PE header)
Roman wrote:
And how in exe file hierarchy section ?
0) Section import
1) Section data
3) Section code
Any. The PE file has only a limit on the number of sections (no more than 16), but their order is arbitrary. Then everything depends on the index in the DataDirectory. That is, the import section in the DataDirectory array always has the same index. This is how the loader understands what kind of data is in the section and finds the corresponding section headers.

Code:
PE32_NUMOF_DIR_ENTRIES  equ 16

struc PE32_DATA_DIRECTORY
{
 .:
   .RVA                 dd ? ; memory address
   .Size                dd ? ; size
 .Length                = $ - .
}

struc PE64_OPTIONAL_HEADER
{
 .:
   .Magic               dw ?
   .majLinkerVer        db ?
   .minLinkerVer        db ?
   .CodeSize            dd ?
   .IdatSize            dd ?
   .UdatSize            dd ?
   .EntryPoint          dd ?
   .BaseOfCode          dd ?
   .ImageBase           dq ?
   .AlignSections       dd ?
   .AlignFile           dd ?
   .majOSVer            dw ?
   .minOSVer            dw ?
   .majPE32Ver          dw ?
   .minPE32Ver          dw ?
   .majSubsysVer        dw ?
   .minSubsysVer        dw ?
   .Win32VerValue       dd ?
   .PE32Size            dd ?
   .HeaderSize          dd ?
   .Checksum            dd ?
   .SubSystem           dw ?
   .DLLCharacters       dw ?
   .StackReserved       dq ?
   .StackCommit         dq ?
   .HeapReserved        dq ?
   .HeapCommit          dq ?
   .LoaderFlags         dd ?
   .DataDirSize         dd ?
   .DataDirectory       PE32_DATA_DIRECTORY ; <- sections
   repeat PE32_NUMOF_DIR_ENTRIES - 1
                        db .DataDirectory.Length dup ?
   end repeat
 .Length                = $ - .
}

struc PE32_SECTION_HEADER
{
 .:
   .Name                db PE32_SIZEOF_SHORT_NAME dup ?
   .VirtualSize         dd ?
   .VirtualAddress      dd ? ; Later, there is a comparison by virtual address
   .RAWSize             dd ?
   .RAWPointer          dd ? ; or raw pointer
   .pRelocations        dd ?
   .pLinenumbers        dd ?
   .cRelocations        dw ?
   .cLinenumbers        dw ?
   .Characteristic      dd ?
 .Length                = $ - .
}    


Last edited by macomics on 05 Mar 2024, 09:09; edited 1 time in total
Post 05 Mar 2024, 08:56
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 09:06
Exist code section limit to 94 kb ?
Or limit section .bss ?
My .bss section size 610 390 176 bytes (610Mb)
Section .bss last in my program.
Code:
IZonesImgX = 1920
IZonesImgY = 1080
GreenTraviNum = 2000
gltf_names = 60000
section '.bss' readable writeable 
                VarTb         rd      100  ;simple vars s types
                InitVars_Tabl rd      100
                Intrfc_Tabl   rd      200  ;push glukanul iz-za rq v 32 bit !
                MshDir        rb      256
                Status        rb      200*80
                Status22      rb      200
                zbuf          rd      90000000   ;uzat s zbuf+4000000 dla scriptov\etc data
                BigTangents   rd      100000
                BigGenDim     rd      4*5000
                GenRocksBig   rd      4*200
                GenBushSuh    rd      4*200
                              rd      1
                GenTravaGreen1 rd     4*GreenTraviNum
                GenTravaSuh1   rd     4*GreenTraviNum

                align 16              ;dla movaps v DX11MyTxt
                CBTextParam     rd      16*4096
                AllMeshes       rd      8*3000            ;ogf
                NxtGenProcPush  rd 1
                GenProcs_List   rd 400
                TestMshsNxtOfst rd 1
                TestMshs        rd 4*400
                Map2DHg         rb IZonesImgX*IZonesImgY
                Map2DInterZoni  rb IZonesImgX*IZonesImgY
                Map2DVragZoni   rb IZonesImgX*IZonesImgY
                ;HltSoundRect   rd 1
                               rd 1     ;count sounds rects
                SoundsRectsLst rd 1*400
                AnimationKeyTab          rd      9600000
                TmpNumKosti              rd      1
                TmpKosti                 rd      3000
                zbuf2                    rd      40000000
                Countr_MshsDraw          rd  1
                CheckedCBDraw            rb  14*14 ;0-not draw this CB, 1-draw
                DatHousesOfst            rd 1
                DatHouses                rd  2*100  ;pointr DX11Mesh
                Penok1ConstBuffrs        rd 2*(11*11) ;na odin cb_adrs,inst draw. 11x11 razmer map 9x9
                irpv v,rbObjcb           { v }
                RlsTabAllCBOfst          rd 1
                RlsTabAllCB              rd 4000
                RlsTabMshsOfst           rd 1
                RlsTabMshs               rd 200000 ;pointer to msh
                PosGrassNum              rb 1
                PosGrassGroundParts      rd 2*100 ;ground parts XZ
                ThreadProcsTabOfst       rd 1
                ThreadProcsTab           rd 1000
                pregradaWallsEnd         rd 1
                pregradaWalls            rd 8*1000
                gltfMapKeyNamesOfst      rd 1
                gltfMapKeyNames          rb 64*gltf_names  ;idet 60 byts name i ptr gltflvlData
                gltflvlData              rd 8000000
                gltflvlDataCountrOfst    rd 1
                gltflvlDataCountr        rd 1*gltf_names
                gltflvlDataEnd:
                GLTFSlotsNum             rd 1
                GLTFSlote                rd 32*gltf_names ;kazdii 128 bytes
                CBZone1                  rd 1*gltf_names
                DSMBarriersZdanii        rd 7*200
                align 16
                DSMWalkPlanesZdanie      rd 8*100               
                DSMPlankiWalls           rd 8*100
      
    


Last edited by Roman on 05 Mar 2024, 09:25; edited 1 time in total
Post 05 Mar 2024, 09:06
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 09:18
Personally, I prefer to create the following order of sections: import,data,bss,code,export,resource,fixups
Roman wrote:
Exist code section limit to 94 kb ?
No
Roman wrote:
Or limit section .bss ?
No
Roman wrote:
My .bss section size 610 390 176 bytes (610Mb)
Blocks longer than 64 kb should be allocated via VirtualAlloc/VirtualFree calls. It is better to allocate a buffer up to 64 кб via GetProcessHeap + HeapAlloc/HeapFree calls. And in the section .bss is better to leave only buffers for an emergency (dump when processing a critical error).

ADD: I understand that you are writing a game. Creating 610 MB of static buffers for the game is not too good. It is better to use dynamic memory allocation for such volumes. But it is better to arrange the order of data definition in the bss section. First everything is rq, then rd, rw and at the end rb


Last edited by macomics on 05 Mar 2024, 09:29; edited 1 time in total
Post 05 Mar 2024, 09:18
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 09:29
Post 05 Mar 2024, 09:29
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 09:43
It's all right. rb 2*15 specifies 30 bytes. Of course, then all the dd's are not aligned.
Code:
                VarTb         rd      100  ;simple vars s types ; <<< aligned
                InitVars_Tabl rd      100
                Intrfc_Tabl   rd      200  ;push glukanul iz-za rq v 32 bit !    

Code:
rb 15*2
                VarTb         rd      100  ;simple vars s types ; <<< unaligned
                InitVars_Tabl rd      100; <<< unaligned
                Intrfc_Tabl   rd      200  ;push glukanul iz-za rq v 32 bit !; <<< unaligned    
In Windows, data should be located at addresses aligned by their length: that is, for word, the address should always be aligned by 2, for dword by 4, etc.

Code:
                RlsTabMshs               rd 200000 ;pointer to msh
                PosGrassNum              rb 1                                                ; <<< Here too
                PosGrassGroundParts      rd 2*100 ;ground parts XZ    
Post 05 Mar 2024, 09:43
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 10:14
if i set
PosGrassNum rb 1

to
PosGrassNum rb 4 i get empty animation, no characters in 3d world
Post 05 Mar 2024, 10:14
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 10:39
Code:
section '.bss' readable writeable
; aligned by 4096=0
                VarTb         rd      100  ;simple vars s types
; 0 + 4*100 = 400 aligned by 16
                InitVars_Tabl rd      100
; 400 + 4*100 = 800 aligned by 32
                Intrfc_Tabl   rd      200  ;push glukanul iz-za rq v 32 bit !
; 800+4*200=1600 aligned by 64
                MshDir        rb      256
; 1600+256=1856 aligned by 64
                Status        rb      200*80
; 1856+200*80=3456 aligned by 128
                Status22      rb      200
; 3456+200=3656 aligned by 8
                zbuf          rd      90000000   ;uzat s zbuf+4000000 dla scriptov\etc data
; 3656+4*90000000=360003656 aligned by 8
                BigTangents   rd      100000
; 360003656+4*100000=360403656 aligned by 8
                BigGenDim     rd      4*5000
; 360403656+4*4*5000=360483656 aligned by 8
                GenRocksBig   rd      4*200
; 360483656+4*4*200 = 360486856 aligned by 8
                GenBushSuh    rd      4*200
; 360486856+4*4*200=360490056 aligned by 8
                              rd      1
; 360490056+4=360490060 aligned by 4
                GenTravaGreen1 rd     4*GreenTraviNum
; 360490060+4*4*2000=360522060 aligned by 4
                GenTravaSuh1   rd     4*GreenTraviNum
; 360522060+4*4*2000=360554060 aligned by 4

                align 16              ;dla movaps v DX11MyTxt
; 360554060+4=360554064 aligned by 16
                CBTextParam     rd      16*4096
; 360554064+4*16*4096=360816208 aligned by 16
                AllMeshes       rd      8*3000            ;ogf
; 360816208+4*8*3000=360912208 aligned by 16
                NxtGenProcPush  rd 1
; 360912208+4=360912212 aligned by 4
                GenProcs_List   rd 400
; 360912212+4*400=360913812 aligned by 8
                TestMshsNxtOfst rd 1
; 360913812+4=360913816 aligned by 4
                TestMshs        rd 4*400
; 360913816+4*400=360916416 aligned by 64
                Map2DHg         rb IZonesImgX*IZonesImgY
; 360913816+1920*1080=362990016 aligned by 64
                Map2DInterZoni  rb IZonesImgX*IZonesImgY
; 362990016+1920*1080=365063616 aligned by 64
                Map2DVragZoni   rb IZonesImgX*IZonesImgY
; 365063616+1920*1080=367137216 aligned by 64
                ;HltSoundRect   rd 1
                               rd 1     ;count sounds rects
; 367137216+4=367137220 aligned by 4
                SoundsRectsLst rd 1*400
; 367137220+4*1*400=367138820 aligned by 4
                AnimationKeyTab          rd      9600000
; 367138820+4*9600000=405538820 aligned by 4
                TmpNumKosti              rd      1
; 405538820+4=405538824 aligned by 8
                TmpKosti                 rd      3000
; 405538824+4*3000=405550824 aligned by 8
                zbuf2                    rd      40000000
; 405550824+4*40000000=565550824 aligned by 8
                Countr_MshsDraw          rd  1
;...    
That's the kind of chaos you have with alignments right now
Post 05 Mar 2024, 10:39
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 10:44
macomics
If problem only was in align, i get this bug 12 months ago !
I writing my game 16 months, and not get any bugs !
And only today i get this bug.

Could be directx 11 memory limit to 1024 mb ?
I reserved 610 mb
1024 - 610 = 414 mb for directx11

I load many models and textures.
Maybe all them more than 414 mb

And my fasm data enters to Directx11 data zone.
That corrupted Directx11 characters meshes.
Post 05 Mar 2024, 10:44
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 11:54
I try this ok:
section '.bss' readable writeable
rb 1

Than this ok:
section '.bss' readable writeable
rb 2

Than this ok:
section '.bss' readable writeable
rb 3

Than this ok:
section '.bss' readable writeable
rb 4

This not show character hero.
section '.bss' readable writeable
rb 5

This not show character hero.
section '.bss' readable writeable
rb 6

This not show character hero.
section '.bss' readable writeable
rb 7

This not show character hero.
section '.bss' readable writeable
rb 8
...

This not show character hero.
section '.bss' readable writeable
rb 160


Last edited by Roman on 05 Mar 2024, 12:03; edited 1 time in total
Post 05 Mar 2024, 11:54
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 12:02
Roman wrote:
Could be directx 11 memory limit to 1024 mb ?
No, but it depends where you get it. If there is a 32-bit address space, then there may not be enough addresses for such a block of memory. And Dx11 may incorrectly perceive 64-bit address values. But this is just a guess.

However, I advise you to arrange the buffers by size from larger to smaller, if they do not require a fixed position relative to each other.
Post 05 Mar 2024, 12:02
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 12:06
Quote:

GetProcessHeap

If I created thread(I do 2 threads), I must do GetProcessHeap or allocated memory for this thread ?
Or thread can simple using .bss section data ?
Post 05 Mar 2024, 12:06
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 12:31
Roman wrote:
If I created thread(I do 2 threads), I must do GetProcessHeap or allocated memory for this thread ?
Or thread can simple using .bss section data ?
It doesn't matter.
But in order not to mix up the affiliation of data to threads, it is worth using dynamic memory allocation and storing pointers in local variables of the function of the corresponding thread.
Post 05 Mar 2024, 12:31
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 13:11
Could it be bug fasm compiler ?
Big .bss section.

And Fasm loosing some data in code section.

In code I do:
Code:
Proc a1
rbObjcb equ Dom1CBufrs            rd 2*(11*11)
mov eax,Dom1CBufrs
ret
endp

proc Gen_zaborwood
     rbObjcb equ zaborwoodCBuf     rd 2*(11*11)
mov eax,zaborwoodCBuf 
ret
endp

section .bss
irpv v,rbObjcb           { v }
    
Post 05 Mar 2024, 13:11
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 14:17
Code:
format PE64 GUI 5.0
section '.text' code readable executable
if used a1
a1:
        rbObjcb equ  Dom1CBufrs            rd 2*(11*11)
        mov     eax, Dom1CBufrs
        ret
end if

if used Gen_zaborwood
Gen_zaborwood:
        rbObjcb equ  zaborwoodCBuf     rd 2*(11*11)
        mov     eax, zaborwoodCBuf
        ret
end if
entry   $
        call    a1
        call    Gen_zaborwood
        mov     eax, bss_length
        retn

section '.bss' data readable writeable
bss_start = $
irpv v,rbObjcb           { v }
bss_length = $ - bss_start    

Code:
$ fasm -m 102400 bss_test.asm
flat assembler  version 1.73.32  (102400 kilobytes memory, x64)
3 passes, 1024 bytes.

$ objdump -D bss_test.exe 

bss_test.exe:     формат файла pei-x86-64


Дизассемблирование раздела .text:

0000000000401000 <.text>:
  401000:       b8 00 20 40 00          mov    $0x402000,%eax
  401005:       c3                      ret
  401006:       b8 c8 23 40 00          mov    $0x4023c8,%eax
  40100b:       c3                      ret
  40100c:       e8 ef ff ff ff          call   0x401000
  401011:       e8 f0 ff ff ff          call   0x401006
  401016:       b8 90 07 00 00          mov    $0x790,%eax ; <<< bss_length = $790 = 1938 = 4 * 2 * 2 * 11 * 11
  40101b:       c3                      ret

$ hexdump -C bss_test.exe
00000000  4d 5a 80 00 01 00 00 00  04 00 10 00 ff ff 00 00  |MZ..............|
00000010  40 01 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |@.......@.......|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 80 00 00 00  |................|
00000040  0e 1f ba 0e 00 b4 09 cd  21 b8 01 4c cd 21 54 68  |........!..L.!Th|
00000050  69 73 20 70 72 6f 67 72  61 6d 20 63 61 6e 6e 6f  |is program canno|
00000060  74 20 62 65 20 72 75 6e  20 69 6e 20 44 4f 53 20  |t be run in DOS |
00000070  6d 6f 64 65 2e 0d 0a 24  00 00 00 00 00 00 00 00  |mode...$........|
00000080  50 45 00 00 64 86 02 00  ca 27 e7 65 00 00 00 00  |PE..d....'.e....|
00000090  00 00 00 00 f0 00 2f 00  0b 02 01 49 00 02 00 00  |....../....I....|
000000a0  00 00 00 00 00 00 00 00  0c 10 00 00 00 10 00 00  |................|
000000b0  00 00 40 00 00 00 00 00  00 10 00 00 00 02 00 00  |..@.............|
000000c0  01 00 00 00 00 00 00 00  05 00 00 00 00 00 00 00  |................|
000000d0  00 30 00 00 00 02 00 00  cb c2 00 00 02 00 00 00  |.0..............|
000000e0  00 10 00 00 00 00 00 00  00 10 00 00 00 00 00 00  |................|
000000f0  00 00 01 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 10 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000180  00 00 00 00 00 00 00 00  2e 74 65 78 74 00 00 00  |.........text...|
00000190  1c 00 00 00 00 10 00 00  00 02 00 00 00 02 00 00  |................|
000001a0  00 00 00 00 00 00 00 00  00 00 00 00 20 00 00 60  |............ ..`|
000001b0  2e 62 73 73 00 00 00 00  90 07 00 00 00 20 00 00  |.bss......... ..|
                                   ^^ ^^ ^^ ^^ - VirtualSize = 790h
000001c0  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
          ^^ ^^ ^^ ^^ - RAWSize = 0
000001d0  00 00 00 00 c0 00 00 c0  00 00 00 00 00 00 00 00  |................|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200  b8 00 20 40 00 c3 b8 c8  23 40 00 c3 e8 ef ff ff  |.. @....#@......|
00000210  ff e8 f0 ff ff ff b8 90  07 00 00 c3 00 00 00 00  |................|
00000220  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400    
As you can see, the data has the correct size

The debugger also shows that one memory page has been allocated for the bss section (1000h = 4096 bytes), which is enough to accommodate 1936 bytes of buffers

In this case, the size of the section in the file is 0, but the virtual size of the section is just 790h


Description:
Filesize: 78.4 KB
Viewed: 1465 Time(s)

Screenshot_20240305_171522.png


Description:
Filesize: 28.26 KB
Viewed: 1465 Time(s)

Screenshot_20240305_171453.png


Post 05 Mar 2024, 14:17
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1701
Roman 05 Mar 2024, 14:29
Quote:

790h

How expected.
2*(11*11)*2*4=1936

Another proc filled values in .bss data
Post 05 Mar 2024, 14:29
View user's profile Send private message Reply with quote
MatQuasar



Joined: 25 Oct 2023
Posts: 101
MatQuasar 05 Mar 2024, 14:55
macomics wrote:
It is better to allocate a buffer up to 64 кб via GetProcessHeap + HeapAlloc/HeapFree calls.


That day we have a discussion in fasm Discord, which pointed out that GetProcessHeap + HeapAlloc can allocate buffer larger than the Heap value set in PE header (default 64KB).

I ran an experiment code and it returns a memory address (not null value), means 640KB successfully allocated, larger than the 5KB set by Heap directive.

Code:
format PE GUI
include 'win32a.inc'
heap 5*1024

section ".code" code executable readable

entry $

  invoke   GetProcessHeap
  invoke   HeapAlloc,eax,HEAP_ZERO_MEMORY,640*1024
  mov      [_mem],eax
  cinvoke  wsprintf,_hex,_type,eax
  invoke   MessageBox,0,_hex,_title,0x40
  invoke   ExitProcess,0

section ".data" data readable writeable

  _mem   dd  ?
  _type  db  "%#x",0
  _hex   rb  1024
         db  0
  _title db  "TestHeap",0

section ".idata" import readable

  library user32,'user32.dll',\
          kernel32,'kernel32.dll'

  import user32,\
         MessageBox,'MessageBoxA',\
         wsprintf,'wsprintfA'

  import kernel32, \
         ExitProcess,'ExitProcess',\
         GetProcessHeap,'GetProcessHeap',\
         HeapAlloc,'HeapAlloc'    


But maybe there is flaw in my finding / experiment code above?
Post 05 Mar 2024, 14:55
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 14:59
Even if you dramatically increase the size of the bss section
Code:
format PE64 GUI 5.0
section '.text' code readable executable
if used a1
a1:
        rbObjcb equ  Dom1CBufrs            rd 2*(11*11)
        mov     eax, Dom1CBufrs
        ret
end if

if used Gen_zaborwood
Gen_zaborwood:
        rbObjcb equ  zaborwoodCBuf     rd 2*(11*11)
        mov     eax, zaborwoodCBuf
        ret
end if
entry   $
        call    a1
        call    Gen_zaborwood
        mov     eax, bss_length
        retn

section '.bss' data readable writeable
bss_start = $
buffer rb 1024*1024*768 ; 768 Mb
irpv v,rbObjcb           { v }
bss_length = $ - bss_start      

Code:
$ fasm -m 1224000 bss_test.asm
flat assembler  version 1.73.32  (1224000 kilobytes memory, x64)
3 passes, 1024 bytes.
$ hexdump -C bss_test.exe
00000000  4d 5a 80 00 01 00 00 00  04 00 10 00 ff ff 00 00  |MZ..............|
00000010  40 01 00 00 00 00 00 00  40 00 00 00 00 00 00 00  |@.......@.......|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 80 00 00 00  |................|
00000040  0e 1f ba 0e 00 b4 09 cd  21 b8 01 4c cd 21 54 68  |........!..L.!Th|
00000050  69 73 20 70 72 6f 67 72  61 6d 20 63 61 6e 6e 6f  |is program canno|
00000060  74 20 62 65 20 72 75 6e  20 69 6e 20 44 4f 53 20  |t be run in DOS |
00000070  6d 6f 64 65 2e 0d 0a 24  00 00 00 00 00 00 00 00  |mode...$........|
00000080  50 45 00 00 64 86 02 00  07 32 e7 65 00 00 00 00  |PE..d....2.e....|
00000090  00 00 00 00 f0 00 2f 00  0b 02 01 49 00 02 00 00  |....../....I....|
000000a0  00 00 00 00 00 00 00 00  0c 10 00 00 00 10 00 00  |................|
000000b0  00 00 40 00 00 00 00 00  00 10 00 00 00 02 00 00  |..@.............|
000000c0  01 00 00 00 00 00 00 00  05 00 00 00 00 00 00 00  |................|
000000d0  00 30 00 30 00 02 00 00  99 2d 00 00 02 00 00 00  |.0.0.....-......|
000000e0  00 10 00 00 00 00 00 00  00 10 00 00 00 00 00 00  |................|
000000f0  00 00 01 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 10 00 00 00  00 00 00 00 00 00 00 00  |................|
00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000180  00 00 00 00 00 00 00 00  2e 74 65 78 74 00 00 00  |.........text...|
00000190  1c 00 00 00 00 10 00 00  00 02 00 00 00 02 00 00  |................|
000001a0  00 00 00 00 00 00 00 00  00 00 00 00 20 00 00 60  |............ ..`|
000001b0  2e 62 73 73 00 00 00 00  90 07 00 30 00 20 00 00  |.bss.......0. ..|
                                   ^^ ^^ ^^ ^^ - VirtualSize
000001c0  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
          ^^ ^^ ^^ ^^-RAWSize
000001d0  00 00 00 00 c0 00 00 c0  00 00 00 00 00 00 00 00  |................|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200  b8 00 20 40 30 c3 b8 c8  23 40 30 c3 e8 ef ff ff  |.. @0...#@0.....|
00000210  ff e8 f0 ff ff ff b8 90  07 00 30 c3 00 00 00 00  |..........0.....|
00000220  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400    

.bss = 0x30001000 = 768 Mb + 4 kb


Description:
Filesize: 72.25 KB
Viewed: 1434 Time(s)

Screenshot_20240305_175842.png


Post 05 Mar 2024, 14:59
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 16:48
MatQuasar wrote:
That day we have a discussion in fasm Discord, which pointed out that GetProcessHeap + HeapAlloc can allocate buffer larger than the Heap value set in PE header (default 64KB).

I ran an experiment code and it returns a memory address (not null value), means 640KB successfully allocated, larger than the 5KB set by Heap directive.
Before the first access to the process heap, it simply does not exist. If, on the first call, the allocated amount of memory exceeds the parameters specified in the headers, then the heap will be created to accommodate the required buffer volume and there will be no errors.

Here's my example. There, I first allocated a small buffer that fits into the size of the heap, and only then tried to exceed the size of the heap and got an error.

And more. A heap is the same object module as the rest of the PE memory blocks. Specifying a heap size less than 64 kb does not make sense. It is also better to specify the heap size in multiples of 64kb.
Code:
format PE GUI
include 'WIN32A.INC'
heap 5*1024*1024; 5Mb

section ".code" code executable readable

entry $

  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info0,8,_info
  invoke   HeapAlloc,[_heap],HEAP_ZERO_MEMORY,640*1024 ; 64kb
  mov      [_mem0],eax
  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info1,8,_info
  invoke   HeapAlloc,eax,HEAP_ZERO_MEMORY,64*1024*1024 ; 64 Mb
  mov      [_mem1],eax
  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info2,8,_info
  cinvoke  wsprintf,_hex,_type,[_mem0],[_mem1],[_info0],[_info1],[_info2]
  invoke   MessageBox,0,_hex,_title,0x40
  invoke   ExitProcess,0

section ".data" data readable writeable

  _heap  dd  ?
  _info  dd  ?
  _info0 rd  4
  _info1 rd  4
  _info2 rd  4
  _mem0  dd  ?
  _mem1  dd  ?
  _type  db  "%#x; %#x; %#x; %#x; %#x",0
  _hex   rb  1024
         db  0
  _title db  "TestHeap",0

section ".idata" import readable

  library user32,'user32.dll',\
          kernel32,'kernel32.dll'

  import user32,\
         MessageBox,'MessageBoxA',\
         wsprintf,'wsprintfA'

  import kernel32, \
         ExitProcess,'ExitProcess',\
         GetProcessHeap,'GetProcessHeap',\
         HeapAlloc,'HeapAlloc',\
         HeapQueryInformation,'HeapQueryInformation'    
Post 05 Mar 2024, 16:48
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 05 Mar 2024, 16:56
At the same time, after such memory allocation, the heap mechanism will no longer work. I.e., it will not be possible to allocate more memory by receiving a handle through GetProcessHeap.
Code:
format PE GUI
include 'WIN32A.INC'
heap 5*1024*1024

section ".code" code executable readable

entry $

  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info0,8,_info
  invoke   HeapAlloc,[_heap],HEAP_ZERO_MEMORY,64*1024*1024
  mov      [_mem0],eax
  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info1,8,_info
  invoke   HeapFree,[_heap],HEAP_ZERO_MEMORY,[_mem0]
  invoke   HeapAlloc,eax,HEAP_ZERO_MEMORY,640*1024
  mov      [_mem1],eax
  invoke   GetProcessHeap
  mov      [_heap],eax
  mov      [_info],0
  invoke   HeapQueryInformation,[_heap],0,_info2,8,_info
  cinvoke  wsprintf,_hex,_type,[_mem0],[_mem1],[_info0],[_info1],[_info2]
  invoke   MessageBox,0,_hex,_title,0x40
  invoke   ExitProcess,0

section ".data" data readable writeable

  _heap  dd  ?
  _info  dd  ?
  _info0 rd  4
  _info1 rd  4
  _info2 rd  4
  _mem0  dd  ?
  _mem1  dd  ?
  _type  db  "%#x; %#x; %#x; %#x; %#x",0
  _hex   rb  1024
         db  0
  _title db  "TestHeap",0

section ".idata" import readable

  library user32,'user32.dll',\
          kernel32,'kernel32.dll'

  import user32,\
         MessageBox,'MessageBoxA',\
         wsprintf,'wsprintfA'

  import kernel32, \
         ExitProcess,'ExitProcess',\
         GetProcessHeap,'GetProcessHeap',\
         HeapAlloc,'HeapAlloc',\
         HeapFree,'HeapFree',\
         HeapQueryInformation,'HeapQueryInformation'    
Post 05 Mar 2024, 16:56
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.