qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v4 2/3] module: add Error arguments to module_load_one and mo


From: Claudio Fontana
Subject: Re: [PATCH v4 2/3] module: add Error arguments to module_load_one and module_load_qom_one
Date: Fri, 23 Sep 2022 11:40:04 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0

On 9/23/22 07:31, Markus Armbruster wrote:
> Claudio Fontana <cfontana@suse.de> writes:
> 
>> On 9/22/22 16:36, Markus Armbruster wrote:
>>> Claudio Fontana <cfontana@suse.de> writes:
>>>
>>>> On 9/22/22 15:20, Markus Armbruster wrote:
>>>>> Claudio Fontana <cfontana@suse.de> writes:
>>>>>
>>>>> [...]
>>>>>
>>>>>> I think it would be better to completely make the return value separate 
>>>>>> from the Error,
>>>>>> and really treat Error as an exception and not mix it up with the 
>>>>>> regular execution,
>>>>>>
>>>>>> but if it is the general consensus that I am the only one seeing this 
>>>>>> conflation problem we can model it this way too.
>>>>>
>>>>> It's a matter of language pragmatics.  In Java, you throw an exception
>>>>> on error.  In C, you return an error value.
>>>>>
>>>>> Trying to emulate exceptions in C might be even more unadvisable than
>>>>> trying to avoid them in Java.  Best to work with the language, not
>>>>> against it.
>>>>>
>>>>> Trouble is the error values we can conveniently return in C can't convey
>>>>> enough information.  So we use Error for that.  Just like GLib uses
>>>>
>>>> Right, we use Error for that and that's fine, but we should use it _only 
>>>> Error_ for that.
>>>>
>>>> Ie map the Exceptions directly to Error.
>>>>
>>>> So:
>>>>
>>>> try {
>>>>
>>>>   rv = function_call(...);
>>>>
>>>>   use_return_value(rv);
>>>>
>>>> } catch (Exception e) {
>>>>
>>>>   /* handle exceptional case */
>>>>
>>>> }
>>>>
>>>> becomes
>>>>
>>>> rv = function_call(..., Error **errp);
>>>>
>>>> if (errp) {
>>>>   /* handle exceptional case */
>>>> }
>>>>
>>>> use_return_value(rv);
>>>
>>> This is simply not the intended use of Error.
>>>
>>> Also, "trying to emulate exceptions in C might be even more unadvisable
>>> than trying to avoid them in Java."
>>>
>>>> Instead we mix up the Exception code path and the regular code path, by 
>>>> having rv carry a mix of the Exception and regular return value,
>>>> and this creates problems and confusion.
>>>
>>> "In C, you return an error value."
>>>
>>>> If we put a hard line between the two, I think more clarity emerges.
>>>
>>> Maybe, but consistency matters.  Clarity doesn't emerge in isolation.  
>>> Deviating from prevailing usage tends to confuse.
>>>
>>>>> GError.
>>>>>
>>>>> More modern languages do "return error value" much better than C can.  C
>>>>> is what it is.
>>>>>
>>>>> We could certainly argue how to do better than we do now in QEMU's C
>>>>> code.  However, the Error API is used all over the place, which makes
>>>>> changing it expensive.  "Rethinking the whole Error API" (your words)
>>>>> would have to generate benefits worth this expense.  Which seems
>>>>> unlikely.
>>>>>
>>>>> [...]
>>>>>
>>>>
>>>> This is all fine, the problem is how we remodel this in C.
>>>>
>>>> This is how I see the next steps to clarify my position:
>>>>
>>>> short term:
>>>>
>>>> - keep the existing API with the existing assumptions, use a separate 
>>>> argument to carry the pointer to the actual return value (where the 
>>>> function return value as provided by the language is used to return if an 
>>>> exception was generated or not, as we assume today).
>>>
>>> We don't actually need separate values for "actual return value" and
>>> "success vs. failure" here.  We can easily encode them in a single
>>
>> Yes, we do, it would avoid the confusion that we see as soon as people look 
>> at the module_load_one code the first time.
>>
>>> return value.  This is *common* in C, for better or worse.
>>
>> We make our own life difficult, by wasting the very precious space of the 
>> return value that should be used to provide the meaning of the function,
>>
>> and using it to provide a completely useless and redundant bool return 
>> value, that by your own definition,
>> is just "errp != NULL".
> 
> *errp != NULL, except that doesn't work when the caller NULL to errp.


This is a solvable detail. If the caller passes NULL it is unlikely to make 
this check after the call.
Checking for the exceptions could even be a macro that solves this.

> 
>> The very precious and scarce return value of the C function is completely 
>> wasted.
> 
> I think you're tilting at windmills :)


I have no idea what you mean.


> 
> error.h again:
> 
>  * - Whenever practical, also return a value that indicates success /
>  *   failure.  This can make the error checking more concise, and can
>  *   avoid useless error object creation and destruction.  Note that
>  *   we still have many functions returning void.  We recommend
>  *   • bool-valued functions return true on success / false on failure,
>  *   • pointer-valued functions return non-null / null pointer, and
>  *   • integer-valued functions return non-negative / negative.
> 
> This does *not* ask you to waste the return value on a bool indicating
> success.  It asks you to use error values whenever practical, and
> recommends common ones, namely:


This is good text, I might have misunderstood you before then.

> 
>      • When a function returns a non-negative integer on success, use
>        negative integers to signify failure.
> 
>      • When a function returns a non-null pointer on success, use a null
>        pointer to signify failure.
> 
>      • When a function has nothing to return, make it return true on
>        success, and false on failure.
> 
> Such use of return values is idiomatic C.
> 
> When a function can return any value of its return type on success,
> there are no error values left.  Unless we can tweak the return type to
> accomodate error values, say widen it from unsigned char to int, we're
> outside "when practical" territory.
> 
> [...]
> 

Interesting, this text contradicts what was implied before (quoting you):

>>>> Functions that set an error on some failures only tend to be awkward: in
>>>> addition to checking the return value for failure, you have to check
>>>> @errp for special failures.  This is particularly cumbersome when it
>>>> requires a @local_err and an error_propagate() just for that.  I
>>>> generally prefer to return an error code and always set an error.


And this may even be inferred from error.h around line 60, just before the 
snipped you quoted above.

Quoting error.h around line 60:

 * - On success, the function should not touch *errp.  On failure, it           
                                                            
 *   should set a new error, e.g. with error_setg(errp, ...), or                
                                                            
 *   propagate an existing one, e.g. with error_propagate(errp, ...).    

The problem is, there is no definition of what "success" and "failure" mean in 
error.h.
And in this absence of definition, it is easy to assume this is connected with 
the return value of the function,
and mix up the return value of what the function is trying to do (in this case 
load a module),
with whether an error/exception was generated when trying to do so.

The issue I think happened here specifically is that seeing a "bool" in the 
return value triggers a pattern in most readers,
and even reading the snippet above in error.h would quickly lead one to believe 
that the bool return value seen is connected with the errp.

In the original patch, the meaning and definition attached to the bool return 
value (as I documented in the code comments),
was "returns true if module found and loaded, and false otherwise".

When seeing

bool function(..., Error **errp);

a short-circuit seems to happen, making assumptions about the meaning of the 
boolean return value, as if connected to the errp.

That is why when changing this to an int, suddenly people seem more comfortable 
with it, and I think this is independent of the extended range of the return 
values.

In any case, returning -1, 0 or 1 is fine, it solves the problem at hand, and I 
think by virtue of the fact that we are using an int instead of the bool,
this will force more thinking from the reader and avoid the short-circuit 
presented here.

Thanks,

Claudio

















reply via email to

[Prev in Thread] Current Thread [Next in Thread]