Skip to contentSkip to conversation

use qemu_malloc and friends consistently

PATCH [email protected] 3 participants 5 messages May 29, 2009 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 8 lines · ----- "Kevin Wolf
Quoted 7 lines · [email protected] schrieb:

Kevin,

I certainly understand your goal ...

Anyway, I will update my patche and resubmit.

BTW it has to be noted that most of the time the return value of

Quoted 8 lines

malloc/qemu_malloc is not checked which can also prove problematic whatever the qemu_malloc/malloc behavior is.

It doesn't need to be checked, qemu_malloc never returns if it fails. If
malloc(0) returns NULL (on success!) checking it is even wrong in places
where this can happen.

I don't necessarily speak about the malloc(0) case.

Up to now the qemu_io code (for example) was using malloc() without checking for the returned value. If allocation fails, I believe it would be quite wrong to pass the returned (NULL) pointer to memcpy/memset/memcmp whatever platform you are considering ...

So it seems to me you definitively need a way to dicriminate between the value returned on succesfull malloc(0) and the value returned on a failed malloc(1000).

So now, back to the malloc(0) case.

Granted, it would be important to have a standard behavior of malloc() on all platforms to be able to check for failure. And malloc(0) returning NULL on success (on some plateform) is not very compatible with other malloc() failure schemes.

qemu_malloc is a nice place to unify all the malloc() behavior. Now it just has to be decided for the "qemu platform" if we allow the malloc(0) usage.
- If yes, we have to allow it for all platforms in qemu_alloc() [using malloc(1) under the cover for example] returning a valid/non NULL pointer for all underlying platforms (even the ones that were returning NULL on success).
- If not, we can just abort() to catch these case early as it is proposed.

It seems to be just a fact that all malloc implementation are not compatible in their behavior. None of these implementations are necessarily wrong, but we just need to decide what is the allowed behavior for the qemu "platform" and then make sure that the qemu_malloc() implemetation is consistent on all underlying platforms.

Quoted 2 lines

Kevin

#2 lore raw
Quoted 19 lines · [email protected] schrieb:
Quoted 8 lines · ----- "Kevin Wolf
Quoted 7 lines · [email protected] schrieb:

Kevin,

I certainly understand your goal ...

Anyway, I will update my patche and resubmit.

BTW it has to be noted that most of the time the return value of

Quoted 8 lines

malloc/qemu_malloc is not checked which can also prove problematic whatever the qemu_malloc/malloc behavior is.

It doesn't need to be checked, qemu_malloc never returns if it fails. If
malloc(0) returns NULL (on success!) checking it is even wrong in places
where this can happen.

Quoted 5 lines

I don't necessarily speak about the malloc(0) case.

Up to now the qemu_io code (for example) was using malloc() without checking for the returned value. If allocation fails, I believe it would be quite wrong to pass the returned (NULL) pointer to memcpy/memset/memcmp whatever platform you are considering ...

So it seems to me you definitively need a way to dicriminate between the value returned on succesfull malloc(0) and the value returned on a failed malloc(1000).

I should not have mentioned the malloc(0) in this mail, it only adds to the confusion.

What I wanted to say is that a failed qemu_malloc doesn't return anything because it aborts (this code is in oom_check). So the caller needs not and even cannot check for failure.

Kevin

#3 lore raw
Quoted 11 lines · ----- "Kevin Wolf
Quoted 1 line

So it seems to me you definitively need a way to dicriminate between

the value returned on succesfull malloc(0) and the value returned on a failed malloc(1000).

I should not have mentioned the malloc(0) in this mail, it only adds to
the confusion.

What I wanted to say is that a failed qemu_malloc doesn't return anything because it aborts (this code is in oom_check). So the caller needs not and even cannot check for failure.

OK, Sory, no failure case then ... My bad ...

Then it is quite important to make sure qemu_malloc is used everywhere to catch/abort any allocation error with oom_check if we don't want/expect to let the user code deal with failure.

JC

Quoted 2 lines

Kevin

#4 lore raw
In reply to [email protected]

[email protected] writes:

[...]

Quoted 5 lines

It seems to be just a fact that all malloc implementation are not compatible in their behavior. None of these implementations are necessarily wrong, but we just need to decide what is the allowed behavior for the qemu "platform" and then make sure that the qemu_malloc() implemetation is consistent on all underlying platforms.

No. malloc() implements the same, well-known spec everywhere. Some people don't understand or don't like the spec, but that doesn't make implementations of the spec incompatible. Here's what you can expect from malloc():

* After p = malloc(SIZE), you can access p[i] for 0 <= i < SIZE. It follows that you must not dereference p at all if SIZE == 0.

* If malloc(SIZE) returns a null pointer, and SIZE != 0, then malloc() failed.

* Any result of malloc() can safely be passed to free().

A wrapper like qemu_malloc() can implement a different spec, of course. But only if the spec is compatible, the wrapper can be used as a drop-in replacement. Making it gratuitously incompatible is foolish, because it *breaks* code that works just fine with malloc().

We already agreed on one compatible difference: qemu_malloc() shall not fail, but just terminate the program.

We're debating another difference, namely what qemu_malloc(0) shall do. Two fine compatible options have been proposed here:

1. qemu_malloc(0) shall return a null pointer.

2. qemu_malloc(0) shall return a non-null pointer that can be passed to qemu_free()

The one that got committed is incompatible.

#5 lore raw
Quoted 31 lines · ----- "Markus Armbruster

No. malloc() implements the same, well-known spec everywhere. Some people don't understand or don't like the spec, but that doesn't make implementations of the spec incompatible. Here's what you can expect from malloc():

* After p = malloc(SIZE), you can access p[i] for 0 <= i < SIZE. It follows that you must not dereference p at all if SIZE == 0.

* If malloc(SIZE) returns a null pointer, and SIZE != 0, then malloc()
failed.

* Any result of malloc() can safely be passed to free().

A wrapper like qemu_malloc() can implement a different spec, of course.
But only if the spec is compatible, the wrapper can be used as a drop-in
replacement. Making it gratuitously incompatible is foolish, because it
*breaks* code that works just fine with malloc().

We already agreed on one compatible difference: qemu_malloc() shall not
fail, but just terminate the program.

We're debating another difference, namely what qemu_malloc(0) shall do.
Two fine compatible options have been proposed here:

Quoted 7 lines

1. qemu_malloc(0) shall return a null pointer.

2. qemu_malloc(0) shall return a non-null pointer that can be passed to
qemu_free()

The one that got committed is incompatible.

Thanks for the clarification Markus.

JC