Skip to content

Fix a few issues which can lead to undefined-behaviour #1730

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

Merged
merged 1 commit into from
Apr 13, 2017

Conversation

bzsolt
Copy link
Member

@bzsolt bzsolt commented Apr 12, 2017

JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély [email protected]

JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély [email protected]
@@ -126,7 +126,8 @@ re_bytecode_list_insert (re_bytecode_ctx_t *bc_ctx_p, /**< RegExp bytecode conte
inline ecma_char_t __attr_always_inline___
re_get_char (uint8_t **bc_p) /**< pointer to bytecode start */
{
ecma_char_t chr = *((ecma_char_t *) *bc_p);
ecma_char_t chr;
memcpy (&chr, *bc_p, sizeof (ecma_char_t));
Copy link
Member

Choose a reason for hiding this comment

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

What exactly was undefined behaviour in the original code? Is memcpy the only way to get around it? As it is a call to a libc function, it is much more expensive than a simple cast.

Copy link
Member

Choose a reason for hiding this comment

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

Reading uint16 from byte aligned address is undefined in C.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that. But cannot we have a macro that does the uint16 read from a byte pointer "in place" without the function call overhead (and the loop overhead of memcpy)?

Copy link
Member

Choose a reason for hiding this comment

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

Any answer to this, even though it got merged already?...

Copy link
Member

Choose a reason for hiding this comment

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

As far as I know most compilers are proud to optimize this.

Copy link
Member

Choose a reason for hiding this comment

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

If we don't trust them we could create a macro. Problem is, if the architecture supports unaligned read, we are expecting the compiler to deoptimize our macro.

Copy link
Member

Choose a reason for hiding this comment

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

Btw GCC has a flag to not optimize this: -fno-builtin-memcpy

Copy link
Member

Choose a reason for hiding this comment

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

The thing is that we have our own memcpy implementation in jerry-libc and I'm not sure that gcc can optimize it in that case. Especially as we are compiling jerry with -fno-builtin, which has effect on memcpy as well.

Copy link
Member

@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
Contributor

@LaszloLango LaszloLango left a comment

Choose a reason for hiding this comment

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

LGTM

@yichoi yichoi merged commit 950a0f1 into jerryscript-project:master Apr 13, 2017
@bzsolt bzsolt deleted the undefined-fixes branch April 18, 2017 11:13
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.

5 participants