@@ -524,6 +524,64 @@ def _handle_boolean_op(
524524 logger .error (f"Unsupported boolean operator: { type (expr .op ).__name__ } " )
525525 return None
526526
527+ # ============================================================================
528+ # VMLinux casting
529+ # ============================================================================
530+
531+ def _handle_vmlinux_cast (
532+ func ,
533+ module ,
534+ builder ,
535+ expr ,
536+ local_sym_tab ,
537+ map_sym_tab ,
538+ structs_sym_tab = None ,
539+ ):
540+ # handle expressions such as struct_request(ctx.di) where struct_request is a vmlinux
541+ # struct and ctx.di is a pointer to a struct but is actually represented as a c_uint64
542+ # which needs to be cast to a pointer. This is also a field of another vmlinux struct
543+ """Handle vmlinux struct cast expressions like struct_request(ctx.di)."""
544+ if len (expr .args ) != 1 :
545+ logger .info ("vmlinux struct cast takes exactly one argument" )
546+ return None
547+
548+ # Get the struct name
549+ struct_name = expr .func .id
550+
551+ # Evaluate the argument (e.g., ctx.di which is a c_uint64)
552+ arg_result = eval_expr (
553+ func ,
554+ module ,
555+ builder ,
556+ expr .args [0 ],
557+ local_sym_tab ,
558+ map_sym_tab ,
559+ structs_sym_tab ,
560+ )
561+
562+ if arg_result is None :
563+ logger .info ("Failed to evaluate argument to vmlinux struct cast" )
564+ return None
565+
566+ arg_val , arg_type = arg_result
567+ # Get the vmlinux struct type
568+ vmlinux_struct_type = VmlinuxHandlerRegistry .get_struct_type (struct_name )
569+ if vmlinux_struct_type is None :
570+ logger .error (f"Failed to get vmlinux struct type for { struct_name } " )
571+ return None
572+ # Cast the integer/value to a pointer to the struct
573+ # If arg_val is an integer type, we need to inttoptr it
574+ ptr_type = ir .PointerType ()
575+ #TODO: add a integer check here later
576+ if ctypes_to_ir (arg_type .type .__name__ ):
577+ # Cast integer to pointer
578+ casted_ptr = builder .inttoptr (arg_val , ptr_type )
579+ else :
580+ logger .error (f"Unsupported type for vmlinux cast: { arg_type } " )
581+ return None
582+
583+ return casted_ptr , vmlinux_struct_type
584+
527585
528586# ============================================================================
529587# Expression Dispatcher
@@ -545,6 +603,16 @@ def eval_expr(
545603 elif isinstance (expr , ast .Constant ):
546604 return _handle_constant_expr (module , builder , expr )
547605 elif isinstance (expr , ast .Call ):
606+ if isinstance (expr .func , ast .Name ) and VmlinuxHandlerRegistry .is_vmlinux_struct (expr .func .id ):
607+ return _handle_vmlinux_cast (
608+ func ,
609+ module ,
610+ builder ,
611+ expr ,
612+ local_sym_tab ,
613+ map_sym_tab ,
614+ structs_sym_tab ,
615+ )
548616 if isinstance (expr .func , ast .Name ) and expr .func .id == "deref" :
549617 return _handle_deref_call (expr , local_sym_tab , builder )
550618
0 commit comments