std.process: memory allocation with malloc in execv_

kdevel kdevel at vogtner.de
Tue Jan 31 18:45:11 UTC 2023


On Tuesday, 31 January 2023 at 15:29:35 UTC, Steven Schveighoffer 
wrote:
> On 1/30/23 12:56 PM, kdevel wrote:
>> On Monday, 30 January 2023 at 17:19:13 UTC, Steven 
>> Schveighoffer wrote:
>>>> [...]
>>>> Freeing the memory is — in the "happy path" — neither 
>>>> required nor possible. When unhappy the GC is ready to clean 
>>>> up the mess. I uploaded a patch to the issue.
>>>
>>> It's actually fine to use GC, you are right. But use 
>>> `GC.disable` before using it (with a `scope(exit)` to 
>>> re-enable), because running a GC just before exec is also 
>>> pointless.
>> 
>> There is no indication that the GC kicks in after patching (v0)
>> 
>>     https://issues.dlang.org/attachment.cgi?id=1868&action=diff
>> 
>> I have a patch v1 in preparation which removes the wrappers 
>> entirely. BTW: There is a non-POSIX function execvpe in the 
>> process.d which is actually a GNU extension.
>
> Using `GC.disable` ensures the GC will not run when you 
> allocate memory.

That leads directly to an avoidable allocation failure if there 
is no free memory but enough memory which could be reclaimed in 
order to allocate `argv_` (the array of pointers to C strings).

> Whether it runs or not is up to the memory allocator.

That is the way how systems with GC appear to work since the 
sixties? Is there a "guideline" that Phobos functions shall 
**not** be implemented in plain vanilla D? I mean: There is 
little point in using a GC managed allocation when you have to 
switch the GC off every now and then.

> There is no guarantee it will run, so checking whether it did 
> run is not conclusive.

Noone declared the intent to implement a check if the GC ran.

> Running a collection just before replacing the entire image 
> with another program isn't productive work.

Can you quantify the likelihood of such incidents and the impact 
(performance, electrical power, money loss) of a GC not switched 
off before `execv*`?

> Just add:
>
> ```d
> GC.disable;
> scope(exit) GC.enable;
> ```
>
> to the part where you are about to set up the call to `exec`

To me there is no benefit of doing so. However, it makes the code 
more complicated and hence less readable.


More information about the Digitalmars-d mailing list