Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print error message #110

Merged
merged 1 commit into from
Sep 21, 2023
Merged

Print error message #110

merged 1 commit into from
Sep 21, 2023

Conversation

GorogPeter
Copy link
Contributor

  • print lexer initialisation error
  • print syntax error
  • print semantic error
  • print assert fail
  • print unequal call paramterer size in assert error
  • print not-defined function call in asser error

fix 2 typo (replace uint16_t with Walrus::ByteCodeStackOffset).

fix another typo. Because of it 3 tests
failed (binary.wast, binary-leb128.wast, elem.wast), but it didn't throw assert failed and the ExecuteWASM() returned with an error that wasn't caught.

For example in elem.wast:174-177 where it tries to write nothing to a 0 sized table:

(module
  (table 0 funcref)
  (elem (i32.const 0))
)

Copy link
Collaborator

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This improves the usage of shell a lot. Some comments though.

fprintf(stderr, "Syntax error");
if (errors.size() > 1)
fprintf(stderr, "s");
fprintf(stderr, ":\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fprintf(stderr, "Syntax error(s)\n"); is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

fprintf(stderr, ":\n");
int i = 1;
for (auto e : errors)
fprintf(stderr, "%15d.) %s\n", i++, e.message.c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is rare to have many errors, and their index is not improtant, so I think just print the error messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

auto trapResult = executeWASM(store, filename, buf->data, functionTypes, &registeredInstanceMap);
if (trapResult.exception) {
std::string& errorMessage = trapResult.exception->message();
fprintf(stderr, "Semantic error: %s\n", errorMessage.c_str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Error: " is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

printf(") expect value(");
printConstVector(data->expectedResult);
printf(") (line: %d) : FAILED\n", data->action->loc.line);
abort();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think printing i, result[i], and data->expectedResult[i] are the key thing here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, printf is used to print messages but other case, fprintf is used instead.
For the code consistency, please use just one function for printing messages.
One more, we prefer RELEASE_ASSERT or ASSERT to abort function.
Please change abort function too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.
Thank you!

@clover2123
Copy link
Collaborator

In this patch, you replaced some RELEASE_ASSERT statements by abort along with printing error messages.
But IMO RELEASE_ASSERT is used in place where we assume that the condition of RELEASE_ASSERT should be met.
Are there any test cases that violate these RELEASE_ASSERT?

@@ -282,7 +282,7 @@ Instance* Module::instantiate(ExecutionState& state, const ExternVector& imports
&data);
}

if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index >= instance->m_tables[elem->tableIndex()]->size() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
if (UNLIKELY(elem->tableIndex() >= numberOfTableTypes() || index > instance->m_tables[elem->tableIndex()]->size() || index + elem->functionIndex().size() > instance->m_tables[elem->tableIndex()]->size())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that index >= instance->m_tables[elem->tableIndex()]->size() can be totally removed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding an (uint64_t) casting could be used to avoid overflow situations.

Btw, isn't the wabt validator should be able to catch these issues? We could check it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked this?
It seems that wabt validator cannot check this case in advance.. but what do you think about it?

@zherczeg
Copy link
Collaborator

https://github.com/Samsung/walrus/blob/interp/src/Walrus.h#L266

RELEASE_ASSERT is basically an abort(), so we thought printing the error and aborting mimics its operation. How shall we change it? I am not sure I understand the question about the tests, the code called abort before, and it is still doing it.

@clover2123
Copy link
Collaborator

I'm wondering that additionally printing error messages are really necessary for each RELEASE_ASSERT.
We have used RELEASE_ASSERT or just ASSERT so far to check the certain conditions that need to be true, not like exceptions.
RELEASE_ASSERT is quite simple and clear but this patch adds messages with abort which makes the code more complex.

@zherczeg
Copy link
Collaborator

It is a developer friendly patch, since it is hard to tell (for me) the actual error from the error message provided by shell. So this is a QoL change not a functionality change. It only affects the shell, not the engine.

@zherczeg
Copy link
Collaborator

We could use a macro to make the code simpler if that would help.

Comment on lines 575 to 579
RELEASE_ASSERT(data->fn->functionType()->result().size() == data->expectedResult.size());
// compare result
for (size_t i = 0; i < result.size(); i++) {
RELEASE_ASSERT(equals(result[i], data->expectedResult[i]));
if (!equals(result[i], data->expectedResult[i])) {
printf("Assert FAILED (line: %d) ", data->action->loc.line);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you going to replace all RELEASE_ASSERT statements by this approach?
If so, please consider all RELEASE_ASSERT cases in this patch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

@GorogPeter GorogPeter force-pushed the error_msg branch 3 times, most recently from d537e06 to 41207f9 Compare July 26, 2023 11:29
Copy link
Collaborator

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Close to done

if (!wabt::Succeeded(result)) {
RELEASE_ASSERT_NOT_REACHED();
return;
if (wabt::Succeeded(result) == false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!wabt::Succeeded(result) is ok

if (assertReturn->action->type() == wabt::ActionType::Invoke) {
auto action = static_cast<wabt::InvokeAction*>(assertReturn->action.get());
auto fn = fetchInstance(action->module_var, instanceMap, registeredInstanceMap)->resolveExportFunction(action->name);
executeInvokeAction(action, fn, assertReturn->expected, nullptr);
} else if (assertReturn->action->type() == wabt::ActionType::Get) {
auto action = static_cast<wabt::GetAction*>(assertReturn->action.get());
auto v = fetchInstance(action->module_var, instanceMap, registeredInstanceMap)->resolveExportGlobal(action->name)->value();
RELEASE_ASSERT(equals(v, assertReturn->expected[0]))
if (equals(v, assertReturn->expected[0]) == false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!equals

std::vector<uint8_t> buf;
if (tsm) {
buf = readModuleData(&tsm->module)->data;
} else {
buf = dsm->data;
}
auto trapResult = executeWASM(store, filename, buf, functionTypes);
RELEASE_ASSERT(trapResult.exception);
if (trapResult.exception == nullptr) {
printf("Execute WASM returned nullptr (in wabt::CommandType::AssertInvalid case)\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected exception: assertModuleInvalid->text.data()

std::vector<uint8_t> buf;
if (tsm) {
buf = readModuleData(&tsm->module)->data;
} else {
buf = dsm->data;
}
auto trapResult = executeWASM(store, filename, buf, functionTypes);
RELEASE_ASSERT(trapResult.exception);
if (trapResult.exception == nullptr) {
printf("Execute WASM returned nullptr (in wabt::CommandType::AssertUnlinkable case)\n");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected exception

@@ -427,7 +438,9 @@ static Walrus::Value toWalrusValue(wabt::Const& c)
return Walrus::Value(Walrus::Value::ExternRef, c.ref_bits() + 1, Walrus::Value::Force);
}
default:
RELEASE_ASSERT_NOT_REACHED();
printf("Error: unknown value type during converting wabt::Const to wabt::Value\n");
ASSERT_NOT_REACHED();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep RELEASE_ASSERT_NOT_REACHED

}
default: {
printf("Error: unkown wabt::Const type\n");
ASSERT_NOT_REACHED();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep RELEASE_ASSERT_NOT_REACHED

RELEASE_ASSERT(fn->functionType()->param().size() == action->args.size());
if (fn->functionType()->param().size() != action->args.size()) {
printf("Error: expected %zu parameter(s) but got %zu.\n", fn->functionType()->param().size(), action->args.size());
ASSERT_NOT_REACHED();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assert is weaker than RELEASE_ASSERT_NOT_REACHED, keep the latter everywhere

Copy link
Collaborator

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@clover2123 clover2123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

- fix 2 typos (uint16_t to Walrus::ByteCodeStackOffset)

- fix 1 typo (because of it 3 tests failed, altough it was
  unnoticable hence the error wasn't caught)

- refactor nested if...else to switch...case

- print error message before exit

- make string() operator for Value class
@zherczeg
Copy link
Collaborator

Since this patch is updated now, could we land it?

@GorogPeter
Copy link
Contributor Author

May I alter something or is it good?

@clover2123
Copy link
Collaborator

Sorry for late 😢

@clover2123 clover2123 merged commit 708e231 into Samsung:interp Sep 21, 2023
12 checks passed
@GorogPeter GorogPeter deleted the error_msg branch November 2, 2023 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants