Jump to content

cci_value::from_json() error handling


Recommended Posts

Hi,

I'm using cci_value::from_json(std::string const & json) to allow users to set model parameters. If the supplied json string is invalid JSON, ie. cannot be deserialized correctly, the function fails rather harshly in the CCI 1.0.0 PoC implementation -- it simply sc_assert()'s that the conversion was ok:

inline cci_value
cci_value::from_json( std::string const & json )
{
  cci_value v;
  bool ok = v.json_deserialize( json );
  sc_assert( ok );
  return v;
} 

In SystemC 2.3.2, sc_assert() calls sc_assertion_failed() in case of a failure, which in turn, reports an SC_FATAL, the default action of which includes to abort() the program.

In case of an invalid user input, it obviously makes more sense to actually handle the error. However, the best I could come up with to deal with the SC_FATAL is this:

auto const old_actions = sc_core::sc_report_handler::set_actions(sc_core::SC_FATAL, sc_core::SC_THROW);
cci::cci_value value;
try {
    value = cci::cci_value::from_json(user_supplied_string);
} catch (sc_core::sc_report &) {
    SC_REPORT_WARNING(...);
    return;
}
sc_core::sc_report_handler::set_actions(sc_core::SC_FATAL, old_actions);

This seems kind of clunky for what I assume is a rather common operation. Is the use of sc_assert() an oversight or am I missing something here?

 

As a side node, while I have to admit that I haven't read the CCI LRM in depth, it gives this description, which doesn't seem to agree with the implementation (emphasis mine):

Quote

Given a JSON description of the value, returns a new cci_value initialized with the value. Reports a value error if the JSON description is invalid.

and

Quote

In the case of an error, the implementation shall call report with a severity of SC_ERROR.

 

Thanks,
Rainer.

Link to comment
Share on other sites

Hi Rainer,

thanks for your detailed report.  The issue seems indeed an incomplete implementation of the CCI 1.0 LRM.  I'll forward your finding to the Accellera CCI working group.

To achieve the expected behavior, you can locally change the from_json implementation to something like:

inline cci_value
cci_value::from_json( std::string const & json )
{
  cci_value v;
  bool ok = v.json_deserialize( json );
  if( !ok ) {
    // report CCI_VALUE_ERROR instead of using sc_assert
    v.report_error( "JSON conversion failed", __FILE__, __LINE__ );
  }
  return v;
}

Local error handling on the calling side then looks like:

cci::cci_value v;
try {
  v = cci::cci_value::from_json(string);
} catch (... ) {
  // catch CCI value errors only, others are re-thrown
  cci::cci_handle_exception( cci::CCI_VALUE_FAILURE );
  // customize error behavior, e.g. via SC_REPORT_WARNING( ... );   
}

Thanks and Greetings from Duisburg,
  Philipp

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...