@@ -12,13 +12,13 @@ use rustc_codegen_ssa::traits::{
1212} ;
1313use rustc_middle:: bug;
1414use rustc_middle:: ty:: Instance ;
15- use rustc_span:: Span ;
15+ use rustc_span:: { DUMMY_SP , Span } ;
1616use rustc_target:: asm:: * ;
1717
1818use crate :: builder:: Builder ;
1919use crate :: callee:: get_fn;
2020use crate :: context:: CodegenCx ;
21- use crate :: errors:: UnwindingInlineAsm ;
21+ use crate :: errors:: { NulBytesInAsm , UnwindingInlineAsm } ;
2222use crate :: type_of:: LayoutGccExt ;
2323
2424// Rust asm! and GCC Extended Asm semantics differ substantially.
@@ -530,8 +530,15 @@ impl<'a, 'gcc, 'tcx> AsmBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
530530 template_str. push_str ( INTEL_SYNTAX_INS ) ;
531531 }
532532
533- // 4. Generate Extended Asm block
533+ // NOTE: GCC's extended asm uses CString which cannot contain nul bytes.
534+ // Emit an error if there are any nul bytes in the template string.
535+ if template_str. contains ( '\0' ) {
536+ let err_sp = span. first ( ) . copied ( ) . unwrap_or ( DUMMY_SP ) ;
537+ self . sess ( ) . dcx ( ) . emit_err ( NulBytesInAsm { span : err_sp } ) ;
538+ return ;
539+ }
534540
541+ // 4. Generate Extended Asm block
535542 let block = self . llbb ( ) ;
536543 let extended_asm = if let Some ( dest) = dest {
537544 assert ! ( !labels. is_empty( ) ) ;
@@ -859,7 +866,7 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
859866 template : & [ InlineAsmTemplatePiece ] ,
860867 operands : & [ GlobalAsmOperandRef < ' tcx > ] ,
861868 options : InlineAsmOptions ,
862- _line_spans : & [ Span ] ,
869+ line_spans : & [ Span ] ,
863870 ) {
864871 let asm_arch = self . tcx . sess . asm_arch . unwrap ( ) ;
865872
@@ -926,6 +933,13 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
926933 }
927934 // NOTE: seems like gcc will put the asm in the wrong section, so set it to .text manually.
928935 template_str. push_str ( "\n .popsection" ) ;
936+ // NOTE: GCC's add_top_level_asm uses CString which cannot contain nul bytes.
937+ // Emit an error if there are any nul bytes in the template string.
938+ if template_str. contains ( '\0' ) {
939+ let span = line_spans. first ( ) . copied ( ) . unwrap_or ( DUMMY_SP ) ;
940+ self . tcx . dcx ( ) . emit_err ( NulBytesInAsm { span } ) ;
941+ return ;
942+ }
929943 self . context . add_top_level_asm ( None , & template_str) ;
930944 }
931945
0 commit comments