nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.29k stars 29.45k forks source link

require addon reports `ELF SyntaxError: Invalid or unexpected token` #46856

Closed Zvicii closed 1 year ago

Zvicii commented 1 year ago

Version

v16.19.1 & v18.14.2

Platform

Linux e0b3b5af329d 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

I wrote a node addon using node-api, it works fine on Windows and macOS, but on Linux, it reports SyntaxError. After browsing though the community and the internet, I found people who are getting the same error.

https://github.com/vercel/pkg/discussions/1643 https://github.com/Unitech/pm2/issues/4775

How often does it reproduce? Is there a required condition?

Stable recurrence

I'm testing using ubuntu 1604 with node 16.19.1 and ubuntu 2204 with nodejs 18.14.2. Getting the same error.

What is the expected behavior?

nodejs loads node-nim.node and treat it as a node addon

What do you see instead?

conan@e0b3b5af329d:~/nim-cross-platform-sdk$ node test.js 
/home/conan/nim-cross-platform-sdk/build/lib/node-nim.node.9.9.2:1
ELF
^

SyntaxError: Invalid or unexpected token
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1119:15)
    at Module._compile (node:internal/modules/cjs/loader:1155:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1245:10)
    at Module.load (node:internal/modules/cjs/loader:1069:32)
    at Function.Module._load (node:internal/modules/cjs/loader:904:12)
    at Module.require (node:internal/modules/cjs/loader:1093:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/home/conan/nim-cross-platform-sdk/test.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1191:14)

Additional information

conan@e0b3b5af329d:~/nim-cross-platform-sdk$ readelf -h /home/conan/nim-cross-platform-sdk/build/lib/node-nim.node
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - GNU
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1
  Entry point address:               0x25a960
  Start of program headers:          64 (bytes into file)
  Start of section headers:          194051024 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)
  Number of program headers:         9
  Size of section headers:           64 (bytes)
  Number of section headers:         37
  Section header string table index: 36
conan@e0b3b5af329d:~/nim-cross-platform-sdk$ nm -D /home/conan/nim-cross-platform-sdk/build/lib/node-nim.node | awk '{if($1=="U"){print $2}}'
abort@GLIBC_2.2.5
__assert_fail@GLIBC_2.2.5
atoi@GLIBC_2.2.5
__cxa_allocate_exception@CXXABI_1.3
__cxa_atexit@GLIBC_2.2.5
__cxa_begin_catch@CXXABI_1.3
__cxa_end_catch@CXXABI_1.3
__cxa_free_exception@CXXABI_1.3
__cxa_guard_abort@CXXABI_1.3
__cxa_guard_acquire@CXXABI_1.3
__cxa_guard_release@CXXABI_1.3
__cxa_init_primary_exception@CXXABI_1.3.11
__cxa_pure_virtual@CXXABI_1.3
__cxa_rethrow@CXXABI_1.3
__cxa_thread_atexit@CXXABI_1.3.7
__cxa_throw@CXXABI_1.3
__cxa_throw_bad_array_new_length@CXXABI_1.3.8
dlclose@GLIBC_2.2.5
dlerror@GLIBC_2.2.5
dlopen@GLIBC_2.2.5
dlsym@GLIBC_2.2.5
__dynamic_cast@CXXABI_1.3
__errno_location@GLIBC_2.2.5
fclose@GLIBC_2.2.5
ferror@GLIBC_2.2.5
fopen@GLIBC_2.2.5
fread@GLIBC_2.2.5
free@GLIBC_2.2.5
fseek@GLIBC_2.2.5
ftell@GLIBC_2.2.5
fwrite@GLIBC_2.2.5
__gxx_personality_v0@CXXABI_1.3
malloc@GLIBC_2.2.5
memcmp@GLIBC_2.2.5
memcpy@GLIBC_2.14
memmove@GLIBC_2.2.5
memset@GLIBC_2.2.5
modf@GLIBC_2.2.5
napi_async_destroy
napi_call_function
napi_call_threadsafe_function
napi_close_callback_scope
napi_close_escapable_handle_scope
napi_close_handle_scope
napi_create_double
napi_create_error
napi_create_external
napi_create_function
napi_create_object
napi_create_reference
napi_create_string_utf8
napi_create_symbol
napi_create_threadsafe_function
napi_create_type_error
napi_define_class
napi_define_properties
napi_delete_reference
napi_escape_handle
napi_fatal_error
napi_get_and_clear_last_exception
napi_get_array_length
napi_get_boolean
napi_get_cb_info
napi_get_element
napi_get_global
napi_get_last_error_info
napi_get_named_property
napi_get_new_target
napi_get_null
napi_get_property
napi_get_property_names
napi_get_reference_value
napi_get_undefined
napi_get_value_bool
napi_get_value_double
napi_get_value_int32
napi_get_value_int64
napi_get_value_string_utf8
napi_get_value_uint32
napi_has_named_property
napi_has_property
napi_is_exception_pending
napi_module_register
napi_open_escapable_handle_scope
napi_open_handle_scope
napi_remove_wrap
napi_set_named_property
napi_throw
napi_throw_type_error
napi_typeof
napi_unwrap
napi_wrap
__once_proxy@GLIBCXX_3.4.11
printf@GLIBC_2.2.5
realloc@GLIBC_2.2.5
snprintf@GLIBC_2.2.5
strchr@GLIBC_2.2.5
strcmp@GLIBC_2.2.5
strlen@GLIBC_2.2.5
strncmp@GLIBC_2.2.5
strncpy@GLIBC_2.2.5
strtod@GLIBC_2.2.5
strtof@GLIBC_2.2.5
strtol@GLIBC_2.2.5
strtoll@GLIBC_2.2.5
strtoul@GLIBC_2.2.5
strtoull@GLIBC_2.2.5
__tls_get_addr@GLIBC_2.3
_Unwind_Resume@GCC_3.0
vsnprintf@GLIBC_2.2.5
_ZdaPv@GLIBCXX_3.4
_ZdlPv@GLIBCXX_3.4
_ZdlPvm@CXXABI_1.3.9
_Znam@GLIBCXX_3.4
_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE3endEv@GLIBCXX_3.4.21
_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4copyEPcmm@GLIBCXX_3.4.21
_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4findEcm@GLIBCXX_3.4.21
_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5beginEv@GLIBCXX_3.4.21
_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc@GLIBCXX_3.4.21
_ZNKSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEE3strEv@GLIBCXX_3.4.21
_ZNKSt9basic_iosIcSt11char_traitsIcEE5rdbufEv@GLIBCXX_3.4
_ZNKSt9basic_iosIcSt11char_traitsIcEEntEv@GLIBCXX_3.4
_ZNSaIcEC1ERKS_@GLIBCXX_3.4
_ZNSaIcEC1Ev@GLIBCXX_3.4
_ZNSaIcEC2ERKS_@GLIBCXX_3.4
_ZNSaIcED1Ev@GLIBCXX_3.4
_ZNSaIcED2Ev@GLIBCXX_3.4
_ZNSirsERd@GLIBCXX_3.4
_ZNSolsEPFRSoS_E@GLIBCXX_3.4
_ZNSolsEPSt15basic_streambufIcSt11char_traitsIcEE@GLIBCXX_3.4
_ZNSt11logic_errorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE@GLIBCXX_3.4.21
_ZNSt11logic_errorC2ERKS_@GLIBCXX_3.4.21
_ZNSt12future_errorD1Ev@GLIBCXX_3.4.14
_ZNSt13__future_base12_Result_baseC2Ev@GLIBCXX_3.4.15
_ZNSt13__future_base12_Result_baseD2Ev@GLIBCXX_3.4.15
_ZNSt13runtime_errorC1EPKc@GLIBCXX_3.4.21
_ZNSt13runtime_errorC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE@GLIBCXX_3.4.21
_ZNSt13runtime_errorD1Ev@GLIBCXX_3.4
_ZNSt14basic_ifstreamIcSt11char_traitsIcEEC1EPKcSt13_Ios_Openmode@GLIBCXX_3.4
_ZNSt14basic_ifstreamIcSt11char_traitsIcEED1Ev@GLIBCXX_3.4
_ZNSt15basic_streambufIcSt11char_traitsIcEE5sgetcEv@GLIBCXX_3.4
_ZNSt15basic_streambufIcSt11char_traitsIcEE6sbumpcEv@GLIBCXX_3.4
_ZNSt15__exception_ptr13exception_ptr4swapERS0_@CXXABI_1.3.3
_ZNSt15__exception_ptr13exception_ptrC1EPv@CXXABI_1.3.11
_ZNSt15__exception_ptr13exception_ptrC1ERKS0_@CXXABI_1.3.3
_ZNSt15__exception_ptr13exception_ptrD1Ev@CXXABI_1.3.3
_ZNSt15__exception_ptreqERKNS_13exception_ptrES2_@CXXABI_1.3.3
_ZNSt28__atomic_futex_unsigned_base19_M_futex_notify_allEPj@GLIBCXX_3.4.21
_ZNSt28__atomic_futex_unsigned_base19_M_futex_wait_untilEPjjbNSt6chrono8durationIlSt5ratioILl1ELl1EEEENS2_IlS3_ILl1ELl1000000000EEEE@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE3endEv@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4backEv@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5beginEv@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE5eraseEN9__gnu_cxx17__normal_iteratorIPKcS4_EES9_@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7replaceEN9__gnu_cxx17__normal_iteratorIPKcS4_EES9_S9_S9_@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE8pop_backEv@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC1EmcRKS3_@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEc@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLEPKc@GLIBCXX_3.4.21
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEpLERKS4_@GLIBCXX_3.4.21
_ZNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEEC1ERKNS_12basic_stringIcS2_S3_EESt13_Ios_Openmode@GLIBCXX_3.4.21
_ZNSt7__cxx1119basic_istringstreamIcSt11char_traitsIcESaIcEED1Ev@GLIBCXX_3.4.21
_ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1ESt13_Ios_Openmode@GLIBCXX_3.4.21
_ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEED1Ev@GLIBCXX_3.4.21
_ZNSt8__detail15_List_node_base11_M_transferEPS0_S1_@GLIBCXX_3.4.15
_ZNSt8__detail15_List_node_base7_M_hookEPS0_@GLIBCXX_3.4.15
_ZNSt8__detail15_List_node_base9_M_unhookEv@GLIBCXX_3.4.15
_ZNSt8ios_base4InitC1Ev@GLIBCXX_3.4
_ZNSt8ios_base4InitD1Ev@GLIBCXX_3.4
_ZNSt9exceptionD2Ev@GLIBCXX_3.4
_Znwm@GLIBCXX_3.4
_ZSt11__once_call@GLIBCXX_3.4.11
_ZSt15future_categoryv@GLIBCXX_3.4.15
_ZSt15__once_callable@GLIBCXX_3.4.11
_ZSt17rethrow_exceptionNSt15__exception_ptr13exception_ptrE@CXXABI_1.3.3
_ZSt17__throw_bad_allocv@GLIBCXX_3.4
_ZSt18_Rb_tree_decrementPKSt18_Rb_tree_node_base@GLIBCXX_3.4
_ZSt18_Rb_tree_decrementPSt18_Rb_tree_node_base@GLIBCXX_3.4
_ZSt18_Rb_tree_incrementPKSt18_Rb_tree_node_base@GLIBCXX_3.4
_ZSt18_Rb_tree_incrementPSt18_Rb_tree_node_base@GLIBCXX_3.4
_ZSt19__throw_logic_errorPKc@GLIBCXX_3.4
_ZSt20__throw_future_errori@GLIBCXX_3.4.14
_ZSt20__throw_length_errorPKc@GLIBCXX_3.4
_ZSt20__throw_out_of_rangePKc@GLIBCXX_3.4
_ZSt20__throw_system_errori@GLIBCXX_3.4.11
_ZSt24__throw_invalid_argumentPKc@GLIBCXX_3.4
_ZSt24__throw_out_of_range_fmtPKcz@GLIBCXX_3.4.20
_ZSt25__throw_bad_function_callv@GLIBCXX_3.4.14
_ZSt28_Rb_tree_rebalance_for_erasePSt18_Rb_tree_node_baseRS_@GLIBCXX_3.4
_ZSt29_Rb_tree_insert_and_rebalancebPSt18_Rb_tree_node_baseS0_RS_@GLIBCXX_3.4
_ZSt4cerr@GLIBCXX_3.4
_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GLIBCXX_3.4
_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RNSt7__cxx1112basic_stringIS4_S5_T1_EES4_@GLIBCXX_3.4.21
_ZStlsIcSt11char_traitsIcESaIcEERSt13basic_ostreamIT_T0_ES7_RKNSt7__cxx1112basic_stringIS4_S5_T1_EE@GLIBCXX_3.4.21
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@GLIBCXX_3.4
_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@GLIBCXX_3.4
_ZTINSt13__future_base12_Result_baseE@GLIBCXX_3.4.15
_ZTISt12future_error@GLIBCXX_3.4.14
_ZTISt13runtime_error@GLIBCXX_3.4
_ZTISt9exception@GLIBCXX_3.4
_ZTVN10__cxxabiv117__class_type_infoE@CXXABI_1.3
_ZTVN10__cxxabiv119__pointer_type_infoE@CXXABI_1.3
_ZTVN10__cxxabiv120__function_type_infoE@CXXABI_1.3
_ZTVN10__cxxabiv120__si_class_type_infoE@CXXABI_1.3
_ZTVN10__cxxabiv121__vmi_class_type_infoE@CXXABI_1.3
_ZTVSt12future_error@GLIBCXX_3.4.14
_ZTVSt9exception@GLIBCXX_3.4
targos commented 1 year ago

Can you show the contents of test.js?

Zvicii commented 1 year ago
const test = require('/home/conan/nim-cross-platform-sdk/build/lib/node-nim.node')
console.log('test')
mscdex commented 1 year ago

Your error output shows this filename being loaded:

/home/conan/nim-cross-platform-sdk/build/lib/node-nim.node.9.9.2

instead of

/home/conan/nim-cross-platform-sdk/build/lib/node-nim.node

Perhaps that is the cause? Is the latter a symlink to the former or is your code being transformed somehow to require the former?

Zvicii commented 1 year ago

Sorry I didn't make myself clear,node-nim.node is just a symlink to node-nim.node.9.9.2. change test.js to

const test = require('/home/conan/nim-cross-platform-sdk/build/lib/node-nim.node.9.9.2')
console.log('test')

has the same output

mscdex commented 1 year ago

I believe you need a .node at the end of the filename for node to treat it as an addon.

Zvicii commented 1 year ago

I believe you need a .node at the end of the filename for node to treat it as an addon.

yes! thats it, so Im actually requiring node-nim.node.9.9.2 which is not ended by .node. After changing it to node-nim.node and remove the symlink, its working now, Many thanks!