1- use super :: translate:: ConstExprKind ;
21use super :: Mutator ;
3- use crate :: mutators:: { DefaultTranslator , Translator } ;
42use crate :: { Result , WasmMutate } ;
53use rand:: Rng ;
6- use wasm_encoder:: { DataSection , DataSegment , DataSegmentMode , Module } ;
4+ use wasm_encoder:: reencode:: { Reencode , RoundtripReencoder } ;
5+ use wasm_encoder:: { DataSection , Module } ;
76use wasmparser:: { DataKind , DataSectionReader } ;
87
98/// Mutator that modifies a data segment, either adding or removing bytes.
@@ -29,32 +28,29 @@ impl Mutator for ModifyDataMutator {
2928 // them to the `new_section` one-by-one.
3029 for ( i, data) in reader. into_iter ( ) . enumerate ( ) {
3130 let data = data?;
32- let offset;
33- // Preserve the mode of the data segment
34- let mode = match & data. kind {
31+ // If this is the correct data segment apply the mutation,
32+ // otherwise preserve the data.
33+ let mut contents = data. data . to_vec ( ) ;
34+ if i as u32 == data_to_modify {
35+ config. raw_mutate ( & mut contents, self . max_data_size ) ?;
36+ }
37+
38+ // Add the data segment to the section that we're building
39+ match data. kind {
3540 DataKind :: Active {
3641 memory_index,
3742 offset_expr,
3843 } => {
39- offset = DefaultTranslator . translate_const_expr (
40- offset_expr,
41- & wasmparser:: ValType :: I32 ,
42- ConstExprKind :: DataOffset ,
43- ) ?;
44- DataSegmentMode :: Active {
45- memory_index : * memory_index,
46- offset : & offset,
47- }
44+ new_section. active (
45+ memory_index,
46+ & RoundtripReencoder . const_expr ( offset_expr) ?,
47+ contents,
48+ ) ;
49+ }
50+ DataKind :: Passive => {
51+ new_section. passive ( contents) ;
4852 }
49- DataKind :: Passive => DataSegmentMode :: Passive ,
5053 } ;
51- // If this is the correct data segment apply the mutation,
52- // otherwise preserve the data.
53- let mut data = data. data . to_vec ( ) ;
54- if i as u32 == data_to_modify {
55- config. raw_mutate ( & mut data, self . max_data_size ) ?;
56- }
57- new_section. segment ( DataSegment { mode, data } ) ;
5854 }
5955
6056 Ok ( Box :: new ( std:: iter:: once ( Ok ( config
0 commit comments