Skip to content

Commit 97e3448

Browse files
authored
Set ErlNifEntry.min_erts to a placeholder value (#703)
This value was introduced with NIF version 2.14 (so, OTP-21, erts-10.0). Us leaving it unassigned and defaulting to NIF version 2.15 means the OTP is currently always putting a random pointer into the respective field here: https://github.com/erlang/otp/blob/ae81b2f6ff2d541c01242f12cdbd5238aa4b26bd/erts/emulator/beam/erl_nif.c#L4581-L4585 It is used if one tries to load a NIF library that was compiled for a newer NIF version to display a debug message, at which point it would perform an out-of-bounds read. As we currently default to NIF v2.15 (OTP-22) and only have features up to 2.16 (OTP-24), there are probably very few cases of this actually occurring in the wild.
1 parent 5f34069 commit 97e3448

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

rustler/src/codegen_runtime.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,15 @@ where
140140
}
141141
}
142142
}
143+
144+
pub const fn min_erts() -> &'static [u8] {
145+
if cfg!(feature = "nif_version_2_17") {
146+
b"OTP-26.0\0"
147+
} else if cfg!(feature = "nif_version_2_16") {
148+
b"OTP-24.0\0"
149+
} else if cfg!(feature = "nif_version_2_15") {
150+
b"OTP-22.0\0"
151+
} else {
152+
b"OTP-21.0\0"
153+
}
154+
}

rustler/src/sys/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub struct ErlNifEntry {
8585
pub vm_variant: *const c_char,
8686
pub options: c_uint, // added in 2.7
8787
pub sizeof_ErlNifResourceTypeInit: usize, // added in 2.12
88+
pub min_erts: *const c_char, // added in 2.14
8889
}
8990

9091
pub const ERL_NIF_DIRTY_NIF_OPTION: c_uint = 1;

rustler_codegen/src/init.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl From<InitMacroInput> for proc_macro2::TokenStream {
125125
vm_variant: b"beam.vanilla\0".as_ptr() as *const rustler::codegen_runtime::c_char,
126126
options: 0,
127127
sizeof_ErlNifResourceTypeInit: rustler::codegen_runtime::get_nif_resource_type_init_size(),
128+
min_erts: rustler::codegen_runtime::min_erts().as_ptr() as *const rustler::codegen_runtime::c_char,
128129
};
129130

130131
unsafe {

0 commit comments

Comments
 (0)