1mod callgraph;
39mod debug;
40mod errors;
41mod library;
42mod module;
43pub mod namespaces;
44mod resolver;
45mod rewrites;
46mod symbols;
47
48use alloc::{boxed::Box, collections::BTreeMap, string::ToString, sync::Arc, vec::Vec};
49use core::{
50 cell::RefCell,
51 ops::{ControlFlow, Index},
52};
53
54use miden_assembly_syntax::{
55 Report,
56 ast::{
57 self, AttributeSet, GlobalItemIndex, InvocationTarget, ItemIndex, Module, ModuleIndex,
58 Path, SymbolResolution, Visibility, types,
59 },
60 debuginfo::{SourceManager, SourceSpan, Span, Spanned},
61 module::{ItemInfo, ModuleInfo},
62};
63use miden_core::{Word, advice::AdviceMap, program::Kernel};
64use miden_mast_package::Package as MastPackage;
65use smallvec::{SmallVec, smallvec};
66
67pub use self::{
68 callgraph::{CallGraph, CycleError},
69 errors::LinkerError,
70 library::{LinkLibrary, Linkage},
71 namespaces::NamespaceGraph,
72 resolver::{ResolverCache, SymbolResolutionContext, SymbolResolver},
73 symbols::{Import, Symbol, SymbolItem},
74};
75use self::{
76 module::{LinkModule, ModuleSource},
77 namespaces::ResolvedImports,
78 resolver::*,
79};
80
81#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
83pub enum LinkStatus {
84 #[default]
86 Unlinked,
87 PartiallyLinked,
90 Linked,
92}
93
94#[derive(Clone)]
127pub struct Linker {
128 libraries: BTreeMap<Word, LinkLibrary>,
130 static_libraries: BTreeMap<Word, LinkLibrary>,
135 modules: Vec<LinkModule>,
137 callgraph: CallGraph,
140 procedures_by_mast_root: BTreeMap<Word, SmallVec<[GlobalItemIndex; 1]>>,
143 kernel_index: Option<ModuleIndex>,
145 kernel: Kernel,
149 kernel_package: Option<Arc<MastPackage>>,
150 source_manager: Arc<dyn SourceManager>,
152}
153
154impl Linker {
157 pub fn new(source_manager: Arc<dyn SourceManager>) -> Self {
159 Self {
160 libraries: Default::default(),
161 static_libraries: Default::default(),
162 modules: Default::default(),
163 callgraph: Default::default(),
164 procedures_by_mast_root: Default::default(),
165 kernel_index: None,
166 kernel: Default::default(),
167 kernel_package: None,
168 source_manager,
169 }
170 }
171
172 pub fn link_library(&mut self, library: LinkLibrary) -> Result<(), LinkerError> {
174 use alloc::collections::btree_map::Entry;
175
176 let module_infos =
177 library.module_infos().map_err(|err| LinkerError::InvalidPackageModuleSurface {
178 package: library.package.name.to_string(),
179 reason: err.to_string(),
180 })?;
181 let library_interface_digest = library.package.interface_digest().map_err(|err| {
182 LinkerError::InvalidPackageModuleSurface {
183 package: library.package.name.to_string(),
184 reason: err.to_string(),
185 }
186 })?;
187
188 let static_library = matches!(library.linkage, Linkage::Static).then(|| library.clone());
189 let result = match self.libraries.entry(library_interface_digest) {
190 Entry::Vacant(entry) => {
191 entry.insert(library);
192 self.link_assembled_modules(module_infos)
193 },
194 Entry::Occupied(mut entry) => {
195 let prev = entry.get_mut();
196
197 if matches!(prev.linkage, Linkage::Dynamic) {
200 prev.linkage = library.linkage;
201 }
202
203 Ok(())
204 },
205 };
206
207 if result.is_ok()
208 && let Some(static_library) = static_library
209 {
210 self.static_libraries
211 .entry(static_library.commitment())
212 .or_insert(static_library);
213 }
214
215 result
216 }
217
218 pub fn link_assembled_modules(
223 &mut self,
224 modules: impl IntoIterator<Item = ModuleInfo>,
225 ) -> Result<(), LinkerError> {
226 for module in modules {
227 self.link_assembled_module(module)?;
228 }
229
230 Ok(())
231 }
232
233 pub fn link_assembled_module(
238 &mut self,
239 module: ModuleInfo,
240 ) -> Result<ModuleIndex, LinkerError> {
241 log::debug!(target: "linker", "adding pre-assembled module {} to module graph", module.path());
242
243 let module_path = module.path();
244 let is_duplicate = self.find_module_index(module_path).is_some();
245 if is_duplicate {
246 return Err(LinkerError::DuplicateModule {
247 path: module_path.to_path_buf().into_boxed_path().into(),
248 });
249 }
250
251 let module_index = self.next_module_id();
252 let submodules = module.submodules().to_vec();
253 let items = module.items();
254 let mut symbols = Vec::with_capacity(items.len());
255 for (idx, item) in items {
256 let gid = module_index + idx;
257 self.callgraph.get_or_insert_node(gid);
258 match &item {
259 ItemInfo::Procedure(item) => {
260 self.register_procedure_root(gid, item.digest);
261 },
262 ItemInfo::Constant(_) | ItemInfo::Type(_) => (),
263 }
264 symbols.push(Symbol::new(
265 item.name().clone(),
266 Visibility::Public,
267 LinkStatus::Linked,
268 SymbolItem::Compiled(item.clone()),
269 ));
270 }
271
272 let link_module = LinkModule::new(
273 module_index,
274 ast::ModuleKind::Library,
275 LinkStatus::Linked,
276 ModuleSource::Mast,
277 module_path.into(),
278 )
279 .with_submodules(submodules)
280 .with_symbols(symbols);
281
282 self.modules.push(link_module);
283 Ok(module_index)
284 }
285
286 pub fn link_modules(
290 &mut self,
291 modules: impl IntoIterator<Item = Box<Module>>,
292 ) -> Result<Vec<ModuleIndex>, LinkerError> {
293 modules.into_iter().map(|mut m| self.link_module(&mut m)).collect()
294 }
295
296 pub fn link_module(&mut self, module: &mut Module) -> Result<ModuleIndex, LinkerError> {
315 log::debug!(target: "linker", "adding unprocessed module {}", module.path());
316
317 let is_duplicate = self.find_module_index(module.path()).is_some();
318 if is_duplicate {
319 return Err(LinkerError::DuplicateModule { path: module.path().into() });
320 }
321
322 let module_index = self.next_module_id();
323 let submodules = module.submodules().to_vec();
324 let mut symbols = Vec::new();
325 let imports = module.take_imports().into_iter().map(Import::new).collect::<Vec<_>>();
326 for item in module.take_items() {
327 match item {
328 ast::Item::Type(item) => {
329 let gid = module_index + ItemIndex::new(symbols.len());
330 self.callgraph.get_or_insert_node(gid);
331 symbols.push(Symbol::new(
332 item.name().clone(),
333 item.visibility(),
334 LinkStatus::Unlinked,
335 SymbolItem::Type(item),
336 ));
337 },
338 ast::Item::Constant(item) => {
339 let gid = module_index + ItemIndex::new(symbols.len());
340 self.callgraph.get_or_insert_node(gid);
341 symbols.push(Symbol::new(
342 item.name().clone(),
343 item.visibility,
344 LinkStatus::Unlinked,
345 SymbolItem::Constant(item),
346 ));
347 },
348 ast::Item::Procedure(item) => {
349 let gid = module_index + ItemIndex::new(symbols.len());
350 self.callgraph.get_or_insert_node(gid);
351 symbols.push(Symbol::new(
352 item.name().clone().into(),
353 item.visibility(),
354 LinkStatus::Unlinked,
355 SymbolItem::Procedure(RefCell::new(Box::new(item))),
356 ));
357 },
358 }
359 }
360 let link_module = LinkModule::new(
361 module_index,
362 module.kind(),
363 LinkStatus::Unlinked,
364 ModuleSource::Ast,
365 module.path().into(),
366 )
367 .with_advice_map(module.advice_map().clone())
368 .with_submodules(submodules)
369 .with_imports(imports)
370 .with_symbols(symbols);
371
372 self.modules.push(link_module);
373 Ok(module_index)
374 }
375
376 #[inline]
377 fn next_module_id(&self) -> ModuleIndex {
378 ModuleIndex::new(self.modules.len())
379 }
380}
381
382impl Linker {
385 pub fn with_kernel(
389 source_manager: Arc<dyn SourceManager>,
390 kernel_package: Arc<MastPackage>,
391 ) -> Result<Self, Report> {
392 log::debug!(target: "linker", "instantiating linker with kernel package {}@{}", kernel_package.name, kernel_package.version);
393
394 let mut linker = Self::new(source_manager);
395 linker.link_with_kernel(kernel_package)?;
396
397 Ok(linker)
398 }
399
400 pub fn link_with_kernel(&mut self, kernel_package: Arc<MastPackage>) -> Result<(), Report> {
408 if !kernel_package.is_kernel() {
409 return Err(Report::msg("invalid kernel package: not a kernel"));
410 }
411 let kernel = kernel_package.to_kernel()?;
412 if kernel.is_empty() {
413 return Err(Report::msg("invalid kernel package: kernel cannot be empty"));
414 }
415 assert!(self.kernel.is_empty());
416 assert!(self.kernel_package.is_none());
417
418 log::debug!(target: "linker", "modifying linker with kernel package {}@{}", kernel_package.name, kernel_package.version);
419
420 let mut kernel_index = None;
421 let module_infos = kernel_package.try_module_infos().map_err(|err| {
422 LinkerError::InvalidPackageModuleSurface {
423 package: kernel_package.name.to_string(),
424 reason: err.to_string(),
425 }
426 })?;
427 for module_info in module_infos {
428 let is_kernel_module = module_info.path().is_kernel_path();
429 let module_index = self.link_assembled_module(module_info)?;
430 if is_kernel_module {
431 kernel_index = Some(module_index);
432 }
433 }
434 assert!(kernel_index.is_some());
435
436 self.kernel_index = kernel_index;
437 self.kernel = kernel;
438 self.kernel_package = Some(kernel_package);
439
440 Ok(())
441 }
442
443 pub fn kernel(&self) -> &Kernel {
444 &self.kernel
445 }
446
447 pub fn kernel_package(&self) -> Option<Arc<MastPackage>> {
448 self.kernel_package.clone()
449 }
450
451 pub fn has_nonempty_kernel(&self) -> bool {
452 self.kernel_index.is_some() || !self.kernel.is_empty()
453 }
454}
455
456impl Linker {
459 fn cycle_error(&self, cycle: CycleError) -> LinkerError {
460 let iter = cycle.into_node_ids();
461 let mut nodes = Vec::with_capacity(iter.len());
462 for node in iter {
463 let module = self[node.module].path();
464 let item = self[node].name();
465 nodes.push(module.join(item).to_string());
466 }
467 LinkerError::Cycle { nodes: nodes.into() }
468 }
469
470 pub fn link(
477 &mut self,
478 roots: impl IntoIterator<Item = Box<Module>>,
479 support: impl IntoIterator<Item = Box<Module>>,
480 ) -> Result<Vec<ModuleIndex>, LinkerError> {
481 use alloc::collections::BTreeSet;
482
483 let root_indices = self.link_modules(roots)?;
484 let _support_indices = self.link_modules(support)?;
485 let namespaces = NamespaceGraph::build(self)?;
486 let imports = namespaces.resolve_imports(self)?;
487
488 self.link_and_rewrite(&namespaces, &imports)?;
489
490 let mut reachable = BTreeSet::new();
491
492 for root in root_indices {
493 reachable.extend(namespaces.reachable_from_root(root));
494 }
495
496 Ok(reachable.into_iter().collect())
497 }
498
499 pub fn link_kernel(
507 &mut self,
508 mut kernel: Box<Module>,
509 support: impl IntoIterator<Item = Box<Module>>,
510 ) -> Result<Vec<ModuleIndex>, LinkerError> {
511 self.link_modules(support)?;
512 let original_module_len = self.modules.len();
513 let original_callgraph = self.callgraph.clone();
514 let module_index = self.link_module(&mut kernel)?;
515 let original_kernel_index = self.kernel_index;
516 let original_module_kinds = self
517 .modules
518 .iter()
519 .enumerate()
520 .take(module_index.as_usize())
521 .filter(|(_, module)| matches!(module.source(), ModuleSource::Ast))
522 .map(|(module_index, module)| (module_index, module.kind()))
523 .collect::<Vec<_>>();
524
525 for module in self.modules.iter_mut().take(module_index.as_usize()) {
527 if matches!(module.source(), ModuleSource::Ast) {
528 module.set_kind(ast::ModuleKind::Kernel);
529 }
530 }
531
532 self.kernel_index = Some(module_index);
533
534 let result = (|| {
535 let namespaces = NamespaceGraph::build(self)?;
536 let imports = namespaces.resolve_imports(self)?;
537 self.link_and_rewrite(&namespaces, &imports)?;
538
539 Ok(namespaces.reachable_from_root(module_index))
540 })();
541
542 match result {
543 ok @ Ok(_) => ok,
544 err => {
545 self.kernel_index = original_kernel_index;
546 self.callgraph = original_callgraph;
547 self.modules.truncate(original_module_len);
548 for (module_index, module_kind) in original_module_kinds {
549 self.modules[module_index].set_kind(module_kind);
550 }
551
552 err
553 },
554 }
555 }
556
557 fn link_and_rewrite(
596 &mut self,
597 namespaces: &NamespaceGraph,
598 imports: &ResolvedImports,
599 ) -> Result<(), LinkerError> {
600 log::debug!(
601 target: "linker",
602 "processing {} unlinked/partially-linked modules, and recomputing module graph",
603 self.modules.iter().filter(|m| !m.is_linked()).count()
604 );
605
606 if self.modules.is_empty() {
609 return Err(LinkerError::Empty);
610 }
611
612 if self.modules.iter().all(LinkModule::is_linked) {
614 return Ok(());
615 }
616
617 let pending_modules = self
620 .modules
621 .iter()
622 .enumerate()
623 .filter(|(_, module)| module.is_unlinked())
624 .map(|(module_index, module)| (module_index, module.clone()))
625 .collect::<Vec<_>>();
626 let original_callgraph = self.callgraph.clone();
627
628 let result = {
629 let resolver = SymbolResolver::with_namespaces(self, namespaces, imports);
630 let mut edges = Vec::new();
631 let mut cache = ResolverCache::default();
632 let mut linked_modules = Vec::new();
633
634 for (module_index, module) in self.modules.iter().enumerate() {
635 if !module.is_unlinked() {
636 continue;
637 }
638
639 let module_index = ModuleIndex::new(module_index);
640
641 for import in module.imports() {
642 if let Some(namespaces::ResolvedUse::Item(gid)) =
643 imports.get(module_index, import.local_name().as_str())
644 {
645 import.set_resolved(gid);
646 }
647 }
648
649 for (symbol_idx, symbol) in module.symbols().enumerate() {
650 let gid = module_index + ItemIndex::new(symbol_idx);
651
652 rewrites::rewrite_symbol(gid, symbol, &resolver, &mut cache)?;
654
655 match symbol.item() {
657 SymbolItem::Compiled(_) | SymbolItem::Type(_) | SymbolItem::Constant(_) => {
658 },
659 SymbolItem::Procedure(proc) => {
660 let proc = proc.borrow();
663 for invoke in proc.invoked() {
664 log::debug!(target: "linker", " | recording {} dependency on {}", invoke.kind, invoke.target);
665
666 let context = SymbolResolutionContext {
667 span: invoke.span(),
668 module: module_index,
669 kind: Some(invoke.kind),
670 };
671 if let Some(callee) = resolver
672 .resolve_invoke_target(&context, &invoke.target)?
673 .into_global_id()
674 {
675 log::debug!(
676 target: "linker",
677 " | resolved dependency to gid {}:{}",
678 callee.module.as_usize(),
679 callee.index.as_usize()
680 );
681 edges.push((gid, callee));
682 }
683 }
684 },
685 }
686 }
687
688 linked_modules.push(module_index);
689 }
690
691 let mut callgraph = self.callgraph.clone();
692 for (caller, callee) in edges {
693 callgraph.add_edge(caller, callee).map_err(|cycle| self.cycle_error(cycle))?;
694 }
695
696 callgraph.toposort().map_err(|cycle| self.cycle_error(cycle))?;
698
699 Ok::<_, LinkerError>((linked_modules, callgraph))
700 };
701
702 match result {
703 Ok((linked_modules, callgraph)) => {
704 self.callgraph = callgraph;
705 for module_index in linked_modules {
706 self.modules[module_index.as_usize()].set_status(LinkStatus::Linked);
707 }
708 },
709 Err(err) => {
710 self.callgraph = original_callgraph;
711 for (module_index, module) in pending_modules {
712 self.modules[module_index] = module;
713 }
714 return Err(err);
715 },
716 }
717
718 Ok(())
719 }
720}
721
722impl Linker {
725 pub fn modules(&self) -> &[LinkModule] {
727 self.modules.as_slice()
728 }
729
730 pub fn libraries(&self) -> impl Iterator<Item = &LinkLibrary> {
732 self.libraries.values()
733 }
734
735 pub fn static_libraries(&self) -> impl Iterator<Item = &LinkLibrary> {
737 self.static_libraries.values()
738 }
739
740 pub fn topological_sort_from_root(
742 &self,
743 caller: GlobalItemIndex,
744 ) -> Result<Vec<GlobalItemIndex>, CycleError> {
745 self.callgraph.toposort_caller(caller)
746 }
747
748 pub fn get_procedure_index_by_digest(
753 &self,
754 procedure_digest: &Word,
755 ) -> Option<GlobalItemIndex> {
756 self.procedures_by_mast_root.get(procedure_digest).map(|indices| indices[0])
757 }
758
759 pub fn resolve_invoke_target(
761 &self,
762 caller: &SymbolResolutionContext,
763 target: &InvocationTarget,
764 ) -> Result<SymbolResolution, LinkerError> {
765 let namespaces = NamespaceGraph::build(self)?;
766 let imports = namespaces.resolve_imports(self)?;
767 let resolver = SymbolResolver::with_namespaces(self, &namespaces, &imports);
768 resolver.resolve_invoke_target(caller, target)
769 }
770
771 pub fn resolve_path(
773 &self,
774 caller: &SymbolResolutionContext,
775 path: &Path,
776 ) -> Result<SymbolResolution, LinkerError> {
777 let namespaces = NamespaceGraph::build(self)?;
778 let imports = namespaces.resolve_imports(self)?;
779 let resolver = SymbolResolver::with_namespaces(self, &namespaces, &imports);
780 resolver.resolve_path(caller, Span::new(caller.span, path))
781 }
782
783 pub fn resolve_signature(
785 &self,
786 gid: GlobalItemIndex,
787 ) -> Result<Option<Arc<types::FunctionType>>, LinkerError> {
788 match self[gid].item() {
789 SymbolItem::Compiled(ItemInfo::Procedure(proc)) => Ok(proc.signature.clone()),
790 SymbolItem::Procedure(proc) => {
791 let proc = proc.borrow();
792 match proc.signature() {
793 Some(ty) => self.translate_function_type(gid.module, ty).map(Some),
794 None => Ok(None),
795 }
796 },
797 SymbolItem::Compiled(_) | SymbolItem::Constant(_) | SymbolItem::Type(_) => {
798 panic!("procedure index unexpectedly refers to non-procedure item")
799 },
800 }
801 }
802
803 fn translate_function_type(
804 &self,
805 module_index: ModuleIndex,
806 ty: &ast::FunctionType,
807 ) -> Result<Arc<types::FunctionType>, LinkerError> {
808 use miden_assembly_syntax::ast::TypeResolver;
809
810 let cc = ty.cc;
811 let mut args = Vec::with_capacity(ty.args.len());
812
813 let symbol_resolver = SymbolResolver::new(self);
814 let mut cache = ResolverCache::default();
815 let mut resolver = Resolver {
816 resolver: &symbol_resolver,
817 cache: &mut cache,
818 current_module: module_index,
819 };
820 for arg in ty.args.iter() {
821 if let Some(arg) = resolver.resolve(arg)? {
822 args.push(arg);
823 } else {
824 let span = arg.span();
825 return Err(LinkerError::UndefinedType {
826 span,
827 source_file: self.source_manager.get(span.source_id()).ok(),
828 });
829 }
830 }
831 let mut results = Vec::with_capacity(ty.results.len());
832 for result in ty.results.iter() {
833 if let Some(result) = resolver.resolve(result)? {
834 results.push(result);
835 } else {
836 let span = result.span();
837 return Err(LinkerError::UndefinedType {
838 span,
839 source_file: self.source_manager.get(span.source_id()).ok(),
840 });
841 }
842 }
843 Ok(Arc::new(types::FunctionType::new(cc, args, results)))
844 }
845
846 pub fn resolve_attributes(&self, gid: GlobalItemIndex) -> AttributeSet {
848 match self[gid].item() {
849 SymbolItem::Compiled(ItemInfo::Procedure(proc)) => proc.attributes.clone(),
850 SymbolItem::Procedure(proc) => {
851 let proc = proc.borrow();
852 proc.attributes().clone()
853 },
854 SymbolItem::Compiled(_) | SymbolItem::Constant(_) | SymbolItem::Type(_) => {
855 panic!("procedure index unexpectedly refers to non-procedure item")
856 },
857 }
858 }
859
860 pub fn resolve_type(
862 &self,
863 span: SourceSpan,
864 gid: GlobalItemIndex,
865 ) -> Result<types::Type, LinkerError> {
866 use miden_assembly_syntax::ast::TypeResolver;
867
868 let symbol_resolver = SymbolResolver::new(self);
869 let mut cache = ResolverCache::default();
870 let mut resolver = Resolver {
871 cache: &mut cache,
872 resolver: &symbol_resolver,
873 current_module: gid.module,
874 };
875
876 resolver.get_type(span, gid)
877 }
878
879 pub(crate) fn register_procedure_root(
888 &mut self,
889 id: GlobalItemIndex,
890 procedure_mast_root: Word,
891 ) {
892 use alloc::collections::btree_map::Entry;
893 match self.procedures_by_mast_root.entry(procedure_mast_root) {
894 Entry::Occupied(ref mut entry) => {
895 let prev_id = entry.get()[0];
896 if prev_id != id {
897 entry.get_mut().push(id);
899 }
900 },
901 Entry::Vacant(entry) => {
902 entry.insert(smallvec![id]);
903 },
904 }
905 }
906
907 pub fn find_module_index(&self, path: &Path) -> Option<ModuleIndex> {
909 self.modules.iter().position(|m| path == m.path()).map(ModuleIndex::new)
910 }
911
912 pub fn find_module(&self, path: &Path) -> Option<&LinkModule> {
914 self.modules.iter().find(|m| path == m.path())
915 }
916}
917
918impl Linker {
920 pub fn const_eval(
922 &self,
923 gid: GlobalItemIndex,
924 expr: &ast::ConstantExpr,
925 cache: &mut ResolverCache,
926 ) -> Result<ast::ConstantValue, LinkerError> {
927 let symbol_resolver = SymbolResolver::new(self);
928 let mut resolver = Resolver {
929 resolver: &symbol_resolver,
930 cache,
931 current_module: gid.module,
932 };
933
934 ast::constants::eval::expr(expr, &mut resolver).map(|expr| expr.expect_value())
935 }
936}
937
938impl Index<ModuleIndex> for Linker {
939 type Output = LinkModule;
940
941 fn index(&self, index: ModuleIndex) -> &Self::Output {
942 &self.modules[index.as_usize()]
943 }
944}
945
946impl Index<GlobalItemIndex> for Linker {
947 type Output = Symbol;
948
949 fn index(&self, index: GlobalItemIndex) -> &Self::Output {
950 &self.modules[index.module.as_usize()][index.index]
951 }
952}
953
954#[cfg(test)]
955mod tests {
956 use std::{
957 panic::{AssertUnwindSafe, catch_unwind},
958 string::String,
959 sync::Arc,
960 };
961
962 use miden_assembly_syntax::{
963 ast::{
964 Ident, InvocationTarget, InvokeKind, ItemIndex, Path, SymbolResolutionError,
965 Visibility, types,
966 },
967 debuginfo::{SourceSpan, Span},
968 module::{ItemInfo, TypeInfo},
969 };
970 use miden_core::Felt;
971
972 use super::*;
973 use crate::{
974 Assembler,
975 ast::Module,
976 testing::{TestContext, source_file},
977 };
978
979 #[test]
980 fn failed_kernel_link_restores_kernel_state() {
981 let context = TestContext::default();
982 let source_manager = context.source_manager();
983 let kernel_source = r#"
984 pub proc a
985 call.b
986 end
987
988 proc b
989 call.a
990 end
991 "#;
992
993 let userspace = context
994 .parse_module(source_file!(
995 &context,
996 r#"
997 namespace userspace
998
999 pub proc helper
1000 push.1
1001 end
1002 "#
1003 ))
1004 .expect("userspace module parsing must succeed");
1005
1006 let mut linker = Linker::new(source_manager);
1007 let userspace_index = linker
1008 .link([userspace], None)
1009 .expect("userspace module must link successfully")
1010 .into_iter()
1011 .next()
1012 .expect("linked module index must be returned");
1013
1014 let first_err = linker
1015 .link_kernel(
1016 context
1017 .parse_kernel(source_file!(&context, kernel_source))
1018 .expect("kernel parsing must succeed"),
1019 None,
1020 )
1021 .expect_err("expected cyclic kernel to be rejected");
1022
1023 assert!(first_err.to_string().contains("found a cycle in the call graph"));
1024 assert!(!linker.has_nonempty_kernel(), "failed kernel link must not leave a kernel set");
1025 assert_eq!(linker[userspace_index].kind(), ast::ModuleKind::Library);
1026
1027 let second_err = linker
1028 .link_kernel(
1029 context
1030 .parse_kernel(source_file!(&context, kernel_source))
1031 .expect("kernel parsing must succeed"),
1032 None,
1033 )
1034 .expect_err("expected cyclic kernel retry to be rejected");
1035 assert!(second_err.to_string().contains("found a cycle in the call graph"));
1036 assert!(!second_err.to_string().contains("duplicate module"));
1037
1038 let syscall_context = SymbolResolutionContext {
1039 span: SourceSpan::UNKNOWN,
1040 module: userspace_index,
1041 kind: Some(InvokeKind::SysCall),
1042 };
1043 let err = linker
1044 .resolve_invoke_target(
1045 &syscall_context,
1046 &InvocationTarget::Symbol(Ident::new("a").expect("valid identifier")),
1047 )
1048 .expect_err("expected syscall without a linked kernel to be rejected");
1049 assert!(matches!(err, LinkerError::InvalidSysCallTarget { .. }));
1050 }
1051
1052 #[test]
1053 fn link_library_keeps_same_interface_libraries_with_distinct_forest_commitments() {
1054 let context = TestContext::default();
1055 let module = context
1056 .parse_module(source_file!(
1057 &context,
1058 r#"
1059 namespace lib
1060
1061 pub proc foo
1062 push.1
1063 end
1064 "#
1065 ))
1066 .expect("library module should parse");
1067 let package: Arc<MastPackage> = Assembler::new(context.source_manager())
1068 .assemble_library("lib", module, None::<Box<Module>>)
1069 .expect("library should assemble")
1070 .into();
1071 let with_advice = Arc::new(package.as_ref().clone().with_advice_map(AdviceMap::from_iter(
1072 [(Word::from([1_u32, 2, 3, 4]), vec![Felt::from_u32(5)])],
1073 )));
1074
1075 assert_ne!(package.digest(), with_advice.digest());
1076 assert_eq!(package.interface_digest().unwrap(), with_advice.interface_digest().unwrap());
1077 assert_ne!(package.mast_forest().commitment(), with_advice.mast_forest().commitment());
1078
1079 let mut linker = Linker::new(context.source_manager());
1080 linker
1081 .link_library(LinkLibrary::from_package(package).with_linkage(Linkage::Static))
1082 .expect("first library should link");
1083 linker
1084 .link_library(LinkLibrary::from_package(with_advice).with_linkage(Linkage::Static))
1085 .expect("same public interface with distinct forest commitment should link");
1086
1087 assert_eq!(linker.libraries().count(), 1);
1088 assert_eq!(linker.static_libraries().count(), 2);
1089 }
1090
1091 #[test]
1092 fn oversized_link_module_resolution_returns_structured_error() {
1093 let context = TestContext::default();
1094 let mut linker = Linker::new(context.source_manager());
1095 let module_id = ModuleIndex::new(0);
1096 let path = Arc::<Path>::from(Path::new("::m::huge"));
1097 let mut symbols = Vec::with_capacity(ItemIndex::MAX_ITEMS + 1);
1098
1099 for i in 0..=ItemIndex::MAX_ITEMS {
1100 let name = Ident::new(format!("a{i}")).expect("valid identifier");
1101 symbols.push(Symbol::new(
1102 name.clone(),
1103 Visibility::Private,
1104 LinkStatus::Unlinked,
1105 SymbolItem::Compiled(ItemInfo::Type(TypeInfo { name, ty: types::Type::Felt })),
1106 ));
1107 }
1108
1109 linker.modules.push(
1110 LinkModule::new(
1111 module_id,
1112 ast::ModuleKind::Library,
1113 LinkStatus::Unlinked,
1114 ModuleSource::Mast,
1115 path,
1116 )
1117 .with_symbols(symbols),
1118 );
1119
1120 let result = catch_unwind(AssertUnwindSafe(|| {
1121 linker[module_id].resolve(Span::unknown("a0"), &SymbolResolver::new(&linker))
1122 }));
1123
1124 let result = match result {
1125 Ok(result) => result,
1126 Err(panic) => {
1127 let message = panic
1128 .downcast_ref::<&str>()
1129 .copied()
1130 .or_else(|| panic.downcast_ref::<String>().map(String::as_str))
1131 .expect("panic payload should be a string");
1132 panic!("expected graceful error, got panic: {message}");
1133 },
1134 };
1135
1136 assert!(matches!(
1137 result,
1138 Err(err) if matches!(*err, SymbolResolutionError::TooManyItemsInModule { .. })
1139 ));
1140 }
1141}