Re:Initialization in C++ is Seriously Bonkers
by waynecochran on Saturday January 05, 2019 @02:55PM
(#18833824)
It’s the C runtime that zeros it, not the OS. That’s my main point. The OS may initialize too, but that is outside the standard.
Re:Initialization in C++ is Seriously Bonkers
by ajross on Saturday January 05, 2019 @03:04PM
(#18833888)
On oddball platforms it might be (though I think there's some quibbling as to whether the C runtime in such an environment is really distinct form "the OS"). It's not on Linux. The .bss segment becomes an anonymous mmap which is zeroed by the kernel when first accessed. Glibc never touches it.
(Edit to correct: obviously glibc "touches" .bss because it has its own static variables. But there's no "zero .bss" step in crt0.o)
Re:Initialization in C++ is Seriously Bonkers
by monocasa on Saturday January 05, 2019 @03:42PM
(#18834114)
I mean, there's probably more shipped units of "oddball systems" than there are of Unix-like multi process systems that have to wipe segments like .bss vracuse of their security model. So it's not like it's some pedantic corner case like you're implying.
And there are other parts of the C standard that would just declare 'implementation defined', so the standard doesn't have to say static duration is initialized just because that's what multi process systems do.
Re:Initialization in C++ is Seriously Bonkers
by waynecochran on Saturday January 05, 2019 @07:00PM
(#18835259)
./libgcc/config/nds32/crtzero.S
.L_bss_init:
! clear BSS, this process can be 4 time faster if data is 4 byte aligned
! if so, use swi.p instead of sbi.p
! the related stuff are defined in linker script
la $r0, _edata ! get the starting addr of bss
la $r2, _end ! get ending addr of bss
beq $r0, $r2, .L_call_main ! if no bss just do nothing
movi $r1, 0 ! should be cleared to 0
.L_clear_bss:
sbi.p $r1, [$r0], 1 ! Set 0 to bss
bne $r0, $r2, .L_clear_bss ! Still bytes left to set
Re:Initialization in C++ is Seriously Bonkers
by ajross on Saturday January 05, 2019 @08:08PM
(#18835602)
How does finding some uncompiled sample code someone added to gcc for an architecture no one has heard of refute the point that glibc on Linux implements bss with an anonymous mmap?
Re:Initialization in C++ is Seriously Bonkers
by waynecochran on Sunday January 06, 2019 @12:26AM
(#18836720)
Not meant to refute, just sample code on the C runtime zeroing the bss section. You'll find this for a variety of systems, this was just one of them...
Re:Initialization in C++ is Seriously Bonkers
by monocasa on Saturday January 05, 2019 @10:59PM
(#18836487)
Rather than pointing to Linux over and over, let's look at Win95. It kind of comes from a 'fuck local security' standpoint (everything mmaped is in one global space mapped into all processes), and still clears bss because that's what the spec expects, and like a lot of embedded systems is fully spec compliant.
Re:Initialization in C++ is Seriously Bonkers
by gamma-male on Saturday January 05, 2019 @09:40PM
(#18836140)
The OS does initialize it, so if you’re writing code for linux in any languages, or even in assembly, you won’t be able to spy on that retrieved memory.
Now C might want to do it too, but bare metal stuff is not necessarily compliant C so that doesn’t matter much.
Re:Initialization in C++ is Seriously Bonkers
by monocasa on Saturday January 05, 2019 @10:54PM
(#18836473)
(Having been the lead for an RTOS) it matters less what the spec says, and more what the compiler expects. So like, you can ignore kill(3) by just not calling it. Patching the compiler to not depend on zero initialized .bss is way harder, and it's just easier to clear it yourself pre main.
Yeah, I know. I live in that world too. I don't know that it's particularly relevant. The C standard is written to a norm of a Unix userspace environment, and that's clearly where the linked article is working.