{"record":{"id":"9b20f63f86110821","repo":"wasmerio/wasmer","slug":"duplicate-function","errorCode":null,"errorMessage":"duplicate function","messagePattern":"duplicate function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/compiler-llvm/src/translator/intrinsics.rs","lineNumber":1869,"sourceCode":"                        value_type: type_to_llvm(intrinsics, global_value_type)?,\n                    },\n                });\n\n                Ok(ret)\n            }\n        }\n    }\n\n    pub fn add_func(\n        &mut self,\n        function_index: FunctionIndex,\n        func: PointerValue<'ctx>,\n        llvm_func_type: FunctionType<'ctx>,\n        vmctx: BasicValueEnum<'ctx>,\n        attrs: &[(Attribute, AttributeLoc)],\n    ) {\n        match self.cached_functions.entry(function_index) {\n            Entry::Occupied(_) => unreachable!(\"duplicate function\"),\n            Entry::Vacant(entry) => {\n                entry.insert(FunctionCache {\n                    func,\n                    llvm_func_type,\n                    vmctx,\n                    imported_include_m0_param: None,\n                    attrs: attrs.to_vec(),\n                });\n            }\n        }\n    }\n\n    #[allow(clippy::too_many_arguments)]\n    pub fn local_func(\n        &mut self,\n        _local_function_index: LocalFunctionIndex,\n        function_index: FunctionIndex,\n        intrinsics: &Intrinsics<'ctx>,","sourceCodeStart":1851,"sourceCodeEnd":1887,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/compiler-llvm/src/translator/intrinsics.rs#L1851-L1887","documentation":"This panic comes from `unreachable!(\"duplicate function\")` in wasmtime's LLVM intrinsics translator. The function cache (`cached_functions` map keyed by `function_index`) is being inserted into for an index that already has a cached `FunctionCache`, which the code assumes can never happen. It means the translation pipeline tried to define/declare the same function twice, violating the one-entry-per-function invariant.","triggerScenarios":"The same `function_index` is passed twice to the cache-filling helper during LLVM translation — e.g. a module whose function index space maps two entries to one intrinsic definition, or a bug/regression in the translation driver that re-invokes function translation for an already-cached index.","commonSituations":"Corrupted or hand-crafted wasm binaries with inconsistent function index spaces; wasmtime version mismatches where multi-memory or multi-value features shift index mapping; forks of wasmtime with custom intrinsic handling.","solutions":["Validate the wasm module first (`wasm-tools validate`) to rule out a malformed binary","Upgrade wasmtime — this invariant violation is typically fixed in newer releases","If you control the module pipeline, check for double-compilation of the same module (e.g. caching compiled artifacts instead of recompiling)","If running a fork, diff your intrinsics.rs against upstream to find the duplicate registration","File a wasmtime issue with the reproducing module"],"exampleFix":"// before: recompiling the same module per instantiation\nfor _ in 0..2 { engine.compile(&module_bytes)?; }\n// after\nlet module = Module::new(&engine, &module_bytes)?; // compile once, reuse the Module","handlingStrategy":"try-catch","validationCode":"// catch process-level panics from the compiler thread so a bad module cannot kill the host\nstd::panic::set_hook(Box::new(|info| log::error!(\"wasmtime compiler panic: {info}\")));","typeGuard":null,"tryCatchPattern":"let result = std::panic::catch_unwind(|| {\n    Module::new(&engine, &bytes).map_err(|e| anyhow::anyhow!(e))\n});\nmatch result {\n    Ok(Ok(module)) => module,\n    Ok(Err(e)) | Err(_) => { validate_and_report_module(&bytes); bail!(\"compile failed (possible double registration)\"); }\n}","preventionTips":["Always run wasmparser::Validator::validate_all on untrusted modules before compilation","Compile each module once and reuse the Module/Instance instead of recompiling","Keep wasmtime and its object/wasmtime-env dependencies on versions from the same lockfile","Compile on a worker thread so a compiler panic does not abort the host process"],"tags":["wasmtime","llvm-backend","intrinsics","panic","duplicate-function"],"backgroundTag":"duplicate-function-cache-insert","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}