rainer Posted August 30, 2019 Report Posted August 30, 2019 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. Quote
Philipp A Hartmann Posted September 1, 2019 Report Posted September 1, 2019 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 Quote
rainer Posted September 2, 2019 Author Report Posted September 2, 2019 Hi Philipp, thank you for the quick answer and for taking care of this! BR, Rainer. Quote
Recommended Posts
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.