99use crate :: VMBuiltinFunctionIndex ;
1010use more_asserts:: assert_lt;
1111use std:: convert:: TryFrom ;
12+ use std:: mem:: align_of;
1213use wasmer_types:: {
1314 FunctionIndex , GlobalIndex , LocalGlobalIndex , LocalMemoryIndex , LocalTableIndex , MemoryIndex ,
1415 ModuleInfo , SignatureIndex , TableIndex ,
@@ -377,6 +378,15 @@ impl VMOffsets {
377378 }
378379}
379380
381+ /// Offset base by num_items items of size item_size, panicking on overflow
382+ fn offset_by ( base : u32 , num_items : u32 , prev_item_size : u32 , next_item_align : usize ) -> u32 {
383+ align (
384+ base. checked_add ( num_items. checked_mul ( prev_item_size) . unwrap ( ) )
385+ . unwrap ( ) ,
386+ next_item_align as u32 ,
387+ )
388+ }
389+
380390/// Offsets for [`VMContext`].
381391///
382392/// [`VMContext`]: crate::vmcontext::VMContext
@@ -389,122 +399,113 @@ impl VMOffsets {
389399 /// The offset of the `tables` array.
390400 #[ allow( clippy:: erasing_op) ]
391401 pub fn vmctx_imported_functions_begin ( & self ) -> u32 {
392- self . vmctx_signature_ids_begin ( )
393- . checked_add (
394- self . num_signature_ids
395- . checked_mul ( u32:: from ( self . size_of_vmshared_signature_index ( ) ) )
396- . unwrap ( ) ,
397- )
398- . unwrap ( )
402+ offset_by (
403+ self . vmctx_signature_ids_begin ( ) ,
404+ self . num_signature_ids ,
405+ u32:: from ( self . size_of_vmshared_signature_index ( ) ) ,
406+ align_of :: < crate :: VMFunctionImport > ( ) ,
407+ )
399408 }
400409
401410 /// The offset of the `tables` array.
402411 #[ allow( clippy:: identity_op) ]
403412 pub fn vmctx_imported_tables_begin ( & self ) -> u32 {
404- self . vmctx_imported_functions_begin ( )
405- . checked_add (
406- self . num_imported_functions
407- . checked_mul ( u32:: from ( self . size_of_vmfunction_import ( ) ) )
408- . unwrap ( ) ,
409- )
410- . unwrap ( )
413+ offset_by (
414+ self . vmctx_imported_functions_begin ( ) ,
415+ self . num_imported_functions ,
416+ u32:: from ( self . size_of_vmfunction_import ( ) ) ,
417+ align_of :: < crate :: VMTableImport > ( ) ,
418+ )
411419 }
412420
413421 /// The offset of the `memories` array.
414422 pub fn vmctx_imported_memories_begin ( & self ) -> u32 {
415- self . vmctx_imported_tables_begin ( )
416- . checked_add (
417- self . num_imported_tables
418- . checked_mul ( u32:: from ( self . size_of_vmtable_import ( ) ) )
419- . unwrap ( ) ,
420- )
421- . unwrap ( )
423+ offset_by (
424+ self . vmctx_imported_tables_begin ( ) ,
425+ self . num_imported_tables ,
426+ u32:: from ( self . size_of_vmtable_import ( ) ) ,
427+ align_of :: < crate :: VMMemoryImport > ( ) ,
428+ )
422429 }
423430
424431 /// The offset of the `globals` array.
425432 pub fn vmctx_imported_globals_begin ( & self ) -> u32 {
426- self . vmctx_imported_memories_begin ( )
427- . checked_add (
428- self . num_imported_memories
429- . checked_mul ( u32:: from ( self . size_of_vmmemory_import ( ) ) )
430- . unwrap ( ) ,
431- )
432- . unwrap ( )
433+ offset_by (
434+ self . vmctx_imported_memories_begin ( ) ,
435+ self . num_imported_memories ,
436+ u32:: from ( self . size_of_vmmemory_import ( ) ) ,
437+ align_of :: < crate :: VMGlobalImport > ( ) ,
438+ )
433439 }
434440
435441 /// The offset of the `tables` array.
436442 pub fn vmctx_tables_begin ( & self ) -> u32 {
437- self . vmctx_imported_globals_begin ( )
438- . checked_add (
439- self . num_imported_globals
440- . checked_mul ( u32:: from ( self . size_of_vmglobal_import ( ) ) )
441- . unwrap ( ) ,
442- )
443- . unwrap ( )
443+ offset_by (
444+ self . vmctx_imported_globals_begin ( ) ,
445+ self . num_imported_globals ,
446+ u32:: from ( self . size_of_vmglobal_import ( ) ) ,
447+ align_of :: < crate :: VMTableImport > ( ) ,
448+ )
444449 }
445450
446451 /// The offset of the `memories` array.
447452 pub fn vmctx_memories_begin ( & self ) -> u32 {
448- self . vmctx_tables_begin ( )
449- . checked_add (
450- self . num_local_tables
451- . checked_mul ( u32:: from ( self . size_of_vmtable_definition ( ) ) )
452- . unwrap ( ) ,
453- )
454- . unwrap ( )
453+ offset_by (
454+ self . vmctx_tables_begin ( ) ,
455+ self . num_local_tables ,
456+ u32:: from ( self . size_of_vmtable_definition ( ) ) ,
457+ align_of :: < crate :: VMMemoryDefinition > ( ) ,
458+ )
455459 }
456460
457461 /// The offset of the `globals` array.
458462 pub fn vmctx_globals_begin ( & self ) -> u32 {
459- let offset = self
460- . vmctx_memories_begin ( )
461- . checked_add (
462- self . num_local_memories
463- . checked_mul ( u32:: from ( self . size_of_vmmemory_definition ( ) ) )
464- . unwrap ( ) ,
465- )
466- . unwrap ( ) ;
467- align ( offset, 16 )
463+ offset_by (
464+ self . vmctx_memories_begin ( ) ,
465+ self . num_local_memories ,
466+ u32:: from ( self . size_of_vmmemory_definition ( ) ) ,
467+ align_of :: < crate :: VMGlobalDefinition > ( ) ,
468+ )
468469 }
469470
470471 /// The offset of the builtin functions array.
471472 pub fn vmctx_builtin_functions_begin ( & self ) -> u32 {
472- self . vmctx_globals_begin ( )
473- . checked_add (
474- self . num_local_globals
475- . checked_mul ( u32:: from ( self . size_of_vmglobal_local ( ) ) )
476- . unwrap ( ) ,
477- )
478- . unwrap ( )
473+ offset_by (
474+ self . vmctx_globals_begin ( ) ,
475+ self . num_local_globals ,
476+ u32:: from ( self . size_of_vmglobal_local ( ) ) ,
477+ align_of :: < crate :: vmcontext:: VMBuiltinFunctionsArray > ( ) ,
478+ )
479479 }
480480
481481 /// The offset of the trap handler.
482482 pub fn vmctx_trap_handler_begin ( & self ) -> u32 {
483- self . vmctx_builtin_functions_begin ( )
484- . checked_add (
485- VMBuiltinFunctionIndex :: builtin_functions_total_number ( )
486- . checked_mul ( u32:: from ( self . pointer_size ) )
487- . unwrap ( ) ,
488- )
489- . unwrap ( )
483+ offset_by (
484+ self . vmctx_builtin_functions_begin ( ) ,
485+ VMBuiltinFunctionIndex :: builtin_functions_total_number ( ) ,
486+ u32:: from ( self . pointer_size ) ,
487+ align_of :: < fn ( ) > ( ) ,
488+ )
490489 }
491490
492491 /// The offset of the gas limiter pointer.
493492 pub fn vmctx_gas_limiter_pointer ( & self ) -> u32 {
494- self . vmctx_trap_handler_begin ( )
495- . checked_add ( if self . has_trap_handlers {
496- u32:: from ( self . pointer_size )
497- } else {
498- 0u32
499- } )
500- . unwrap ( )
493+ offset_by (
494+ self . vmctx_trap_handler_begin ( ) ,
495+ if self . has_trap_handlers { 1 } else { 0 } ,
496+ u32:: from ( self . pointer_size ) ,
497+ align_of :: < * mut wasmer_types:: FastGasCounter > ( ) ,
498+ )
501499 }
502500
503501 /// The offset of the current stack limit.
504502 pub fn vmctx_stack_limit_begin ( & self ) -> u32 {
505- self . vmctx_gas_limiter_pointer ( )
506- . checked_add ( u32:: from ( self . pointer_size ) )
507- . unwrap ( )
503+ offset_by (
504+ self . vmctx_gas_limiter_pointer ( ) ,
505+ 1 ,
506+ u32:: from ( self . pointer_size ) ,
507+ align_of :: < u32 > ( ) ,
508+ )
508509 }
509510
510511 /// The offset of the initial stack limit.
0 commit comments