njs: bind() segfault.
From https://github.com/nginx/njs/issues/106#issuecomment-480480675
function foo() {
var t = 2;
function baz() {
t = 3;
}
baz.bind()()
}
foo();
./build/njs bind_bug.js
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2195==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004c6c12 bp 0x7ffec1e80c70 sp 0x7ffec1e80418 T0
)
==2195==The signal is caused by a READ memory access.
==2195==Hint: address points to the zero page.
#0 0x4c6c11 in __asan::QuickCheckForUnpoisonedRegion(unsigned long, unsigned long) (/home/xeioex/workspace/nginx/nginScript/n
js/build/njs+0x4c6c11)
#1 0x4c6b71 in __asan_memcpy (/home/xeioex/workspace/nginx/nginScript/njs/build/njs+0x4c6b71)
#2 0x515973 in njs_vmcode_interpreter /home/xeioex/workspace/nginx/nginScript/njs/njs/njs_vm.c:176:27
#3 0x513db4 in njs_vm_start /home/xeioex/workspace/nginx/nginScript/njs/njs/njs.c:594:11
...
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 50 (50 by maintainers)
Commits related to this issue
- Fixed function declaration with the same name as a variable. This closes #126 issue on Github. — committed to nginx/njs by hongzhidao 5 years ago
@drsm @xeioex
Good gcc option, we can add into njs. And I think it’s also needed in UNIT 😃
@hongzhidao
the problem arise when we assign a value to the variable generated by the function definition:
the code generated is somewhat different (but should be identical):
test:
vm->active_frame->closures is array of parent functions scopes that are referenced in nested functions. function->closure means that the function has closures (function->closures) captured on function creation.
When parent function calls nested function, the nested function should use current active frame closures. When parent function creates closure from nested function (by returning it or by assigning it to variable or property) the nested function should capture current active frame closures. And when the nested closure function will later be called after parent function will complete, the nested closure function should use the captured closures.
We plan to process it later, it’s not clear now.
@drsm
Can you try it again? It seems work well.
@xeioex
Fixed segfault.
BTW, it seems the code related to shim and function declaration can be simplified, it is a separate topic. I think this ticket can be closed. Finally, the answer to the question is helpful, thanks.
Why not need to call njs_generate_children_indexes_release in njs_generate_object_dest_index?@hongzhidao
BTW, this patch breaks the following test: https://gist.github.com/xeioex/10d915edfbf7b5395dcf22fcb7fc1aa2 (The test itself is at the bottom, everything else is test harness).
It fixes the following test: https://gist.github.com/64099ab9ef7f8f5f63a8e0771766c98f Everything else is not changed.
@xeioex
Making njs_parser_node_t more generic.
Making parser hoist more generic. https://gist.github.com/hongzhidao/1733a63d20b4accc2490ba35ea51bc92
Simplified shim variable and scope. https://gist.github.com/hongzhidao/c79d454849f93cd9d7ea4adeef482802 Check whether this looks clear, please.
@hongzhidao
It’s OK, but I want to re-use it with
node->namefor function name without introducing newly field.@xeioex take a look.
Simplified function creation. https://gist.github.com/hongzhidao/397fdb1611101fe088535f5d97ed7f09
Now.
shim variable typeandshim scope type.NJS_TOKEN_FUNCTION_EXPRESSION.Left question.
@hongzhidao
no. it is a named function expression. but, as for now, Function.prototype.name is not supported. and the name
fis not bound to any variable in the module scope oftest.js.OK, This can belongs to
moduleimprovement.@hongzhidao
no, it’s just a statement that generates a function object. the name
fin the example above is bound to the scope of the function object created:Thanks. So it seems we needn’t consider variable hoist, since all variables belongs to
scope->variablesin njs.gis declared before function declaration. It has no concern withbind.No, it should depend on function->closure flag. If it is set, then use function->closures, otherwise vm->active->frame->closures.