diff mbox series

[1/1] sandbox: handling out of memory

Message ID 20200604172822.13112-1-xypron.glpk@gmx.de
State Accepted
Commit c7e49ddc613534f2eb4286ae100fbc90938f160f
Headers show
Series [1/1] sandbox: handling out of memory | expand

Commit Message

Heinrich Schuchardt June 4, 2020, 5:28 p.m. UTC
assert() only works in debug mode. So checking a successful memory
allocation should not use assert().

Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
---
 arch/sandbox/cpu/state.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

--
2.26.2

Comments

Simon Glass June 7, 2020, 1:45 p.m. UTC | #1
Hi Heinrich,

On Thu, 4 Jun 2020 at 11:28, Heinrich Schuchardt <xypron.glpk at gmx.de> wrote:
>
> assert() only works in debug mode. So checking a successful memory
> allocation should not use assert().
>

Reviewed-by: Simon Glass <sjg at chromium.org>

What sort of environment are you using that returns NULL in this case?

Regards,
Simon


> Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
> ---
>  arch/sandbox/cpu/state.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
> index 1f794123b3..34b6fff7e7 100644
> --- a/arch/sandbox/cpu/state.c
> +++ b/arch/sandbox/cpu/state.c
> @@ -378,7 +378,10 @@ int state_init(void)
>
>         state->ram_size = CONFIG_SYS_SDRAM_SIZE;
>         state->ram_buf = os_malloc(state->ram_size);
> -       assert(state->ram_buf);
> +       if (!state->ram_buf) {
> +               printf("Out of memory\n");
> +               os_exit(1);
> +       }
>
>         state_reset_for_test(state);
>         /*
> --
> 2.26.2
>
Heinrich Schuchardt June 7, 2020, 2:02 p.m. UTC | #2
Am June 7, 2020 1:45:53 PM UTC schrieb Simon Glass <sjg at chromium.org>:
>Hi Heinrich,
>
>On Thu, 4 Jun 2020 at 11:28, Heinrich Schuchardt <xypron.glpk at gmx.de>
>wrote:
>>
>> assert() only works in debug mode. So checking a successful memory
>> allocation should not use assert().
>>
>
>Reviewed-by: Simon Glass <sjg at chromium.org>
>
>What sort of environment are you using that returns NULL in this case?

You will get NULL here if mmap() fails. This should happen if your machine has less then 128 MiB left over or you increase the RAM size of the sandbox.

For testing I suggest you increase the sandbox memory size beyond the RAM and swap size of your computer.

Best regards

Heinrich

>
>Regards,
>Simon
>
>
>> Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
>> ---
>>  arch/sandbox/cpu/state.c | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
>> index 1f794123b3..34b6fff7e7 100644
>> --- a/arch/sandbox/cpu/state.c
>> +++ b/arch/sandbox/cpu/state.c
>> @@ -378,7 +378,10 @@ int state_init(void)
>>
>>         state->ram_size = CONFIG_SYS_SDRAM_SIZE;
>>         state->ram_buf = os_malloc(state->ram_size);
>> -       assert(state->ram_buf);
>> +       if (!state->ram_buf) {
>> +               printf("Out of memory\n");
>> +               os_exit(1);
>> +       }
>>
>>         state_reset_for_test(state);
>>         /*
>> --
>> 2.26.2
>>
Heinrich Schuchardt June 7, 2020, 4:54 p.m. UTC | #3
On 6/7/20 4:02 PM, Heinrich Schuchardt wrote:
> Am June 7, 2020 1:45:53 PM UTC schrieb Simon Glass <sjg at chromium.org>:
>> Hi Heinrich,
>>
>> On Thu, 4 Jun 2020 at 11:28, Heinrich Schuchardt <xypron.glpk at gmx.de>
>> wrote:
>>>
>>> assert() only works in debug mode. So checking a successful memory
>>> allocation should not use assert().
>>>
>>
>> Reviewed-by: Simon Glass <sjg at chromium.org>
>>
>> What sort of environment are you using that returns NULL in this case?
>
> You will get NULL here if mmap() fails. This should happen if your machine has less then 128 MiB left over or you increase the RAM size of the sandbox.
>
> For testing I suggest you increase the sandbox memory size beyond the RAM and swap size of your computer.
>
> Best regards
>
> Heinrich

An excessive RAM sandbox w/o the patch:

$ ./u-boot
Segmentation fault

with the patch

$ ./u-boot
Out of memory

Best regards

Heinrich

>
>>
>> Regards,
>> Simon
>>
>>
>>> Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
>>> ---
>>>  arch/sandbox/cpu/state.c | 5 ++++-
>>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
>>> index 1f794123b3..34b6fff7e7 100644
>>> --- a/arch/sandbox/cpu/state.c
>>> +++ b/arch/sandbox/cpu/state.c
>>> @@ -378,7 +378,10 @@ int state_init(void)
>>>
>>>         state->ram_size = CONFIG_SYS_SDRAM_SIZE;
>>>         state->ram_buf = os_malloc(state->ram_size);
>>> -       assert(state->ram_buf);
>>> +       if (!state->ram_buf) {
>>> +               printf("Out of memory\n");
>>> +               os_exit(1);
>>> +       }
>>>
>>>         state_reset_for_test(state);
>>>         /*
>>> --
>>> 2.26.2
>>>
>
diff mbox series

Patch

diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
index 1f794123b3..34b6fff7e7 100644
--- a/arch/sandbox/cpu/state.c
+++ b/arch/sandbox/cpu/state.c
@@ -378,7 +378,10 @@  int state_init(void)

 	state->ram_size = CONFIG_SYS_SDRAM_SIZE;
 	state->ram_buf = os_malloc(state->ram_size);
-	assert(state->ram_buf);
+	if (!state->ram_buf) {
+		printf("Out of memory\n");
+		os_exit(1);
+	}

 	state_reset_for_test(state);
 	/*