Skip to content

Commit 170ecc6

Browse files
michielp1807folkertdev
authored andcommitted
Add test case for error found by fuzzer for ZDICT_trainFromBuffer_legacy
1 parent 2609639 commit 170ecc6

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed

test-libzstd-rs-sys/src/dict_builder.rs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,200 @@ fn test_train_from_buffer_legacy() {
278278
});
279279
}
280280

281+
#[test]
282+
#[cfg(not(target_family = "wasm"))]
283+
#[cfg_attr(miri, ignore = "slow")]
284+
fn test_train_from_buffer_legacy_try_merge_1() {
285+
let _ = assert_eq_rs_c!({
286+
let sample_data: Vec<u8> = vec![
287+
255, 255, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255, 255, 61, 0, 135, 255, 255,
288+
255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135, 135, 255, 255,
289+
135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 255,
290+
255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255, 255, 135, 167,
291+
135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255,
292+
135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 167, 135, 135, 61, 0, 135, 255,
293+
255, 255, 255, 61, 0, 135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 135, 167,
294+
135, 126, 135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135, 143, 135,
295+
135, 135, 255, 255, 135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255, 135, 135,
296+
135, 255, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135,
297+
135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255,
298+
135, 167, 135, 135, 61, 0, 135, 255, 255, 255, 255, 61, 0, 135, 255, 255, 255, 255,
299+
255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135, 135, 255, 255, 135, 135,
300+
255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 255, 255, 255,
301+
255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255, 255, 135, 167, 135, 143,
302+
135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135,
303+
135, 135, 135, 135, 135, 135, 255, 255, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255,
304+
255, 61, 0, 135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126,
305+
135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135,
306+
255, 255, 135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255,
307+
255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135,
308+
135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135,
309+
135, 255, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135,
310+
135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255,
311+
255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135, 135, 255, 255,
312+
135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 255,
313+
255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255, 255, 135, 167,
314+
135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255,
315+
135, 135, 135, 135, 135, 135, 135, 135, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255,
316+
255, 61, 0, 135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126,
317+
135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135,
318+
255, 255, 135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255,
319+
255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135,
320+
135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 167,
321+
135, 135, 61, 0, 135, 255, 255, 255, 255, 61, 0, 135, 255, 255, 255, 255, 255, 255,
322+
255, 255, 255, 255, 135, 167, 135, 126, 135, 135, 135, 255, 255, 135, 135, 255, 255,
323+
255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 255, 255,
324+
135, 135, 255, 255, 135, 135, 135, 255, 255, 255, 255, 135, 167, 135, 143, 135, 135,
325+
135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135,
326+
135, 135, 135, 135, 255, 255, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255, 255, 61,
327+
0, 135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135,
328+
135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255,
329+
135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255,
330+
255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135,
331+
135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 255, 135, 167, 135,
332+
143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135,
333+
135, 135, 135, 135, 135, 135, 135, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135,
334+
135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135,
335+
135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135,
336+
255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 167, 135, 135, 61, 0,
337+
135, 255, 255, 255, 255, 61, 0, 135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
338+
135, 167, 135, 126, 135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135,
339+
143, 135, 135, 135, 255, 255, 135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255,
340+
135, 135, 135, 255, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135,
341+
135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135,
342+
255, 255, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255, 255, 61, 0, 135, 255, 255,
343+
255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135, 135, 255, 255,
344+
135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 255,
345+
255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255, 255, 135, 167,
346+
135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255,
347+
135, 135, 135, 135, 135, 135, 135, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135,
348+
135, 135, 135, 255, 255, 135, 167, 135, 135, 61, 0, 135, 255, 255, 255, 255, 61, 0,
349+
135, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 135, 167, 135, 126, 135, 135,
350+
135, 255, 255, 135, 135, 255, 255, 255, 135, 167, 135, 143, 135, 135, 135, 255, 255,
351+
135, 135, 255, 255, 255, 255, 255, 135, 135, 255, 255, 135, 135, 135, 255, 255, 255,
352+
255, 135, 167, 135, 143, 135, 135, 135, 255, 255, 135, 135, 135, 135, 135, 135, 135,
353+
135, 255, 255, 135, 135, 135, 135, 135, 135, 135, 135,
354+
];
355+
356+
let sample_sizes = vec![
357+
94, 94, 94, 94, 36, 78, 92, 94, 94, 30, 27, 27, 94, 94, 12, 94,
358+
];
359+
360+
let params = ZDICT_legacy_params_t {
361+
zParams: ZDICT_params_t {
362+
compressionLevel: 0,
363+
notificationLevel: 0,
364+
dictID: 0,
365+
},
366+
selectivityLevel: 0,
367+
};
368+
369+
let dict_capacity = 1024;
370+
let mut dict_buffer = vec![0u8; dict_capacity];
371+
372+
let dict_size = ZDICT_trainFromBuffer_legacy(
373+
dict_buffer.as_mut_ptr() as *mut c_void,
374+
dict_buffer.len(),
375+
sample_data.as_ptr() as *const c_void,
376+
sample_sizes.as_ptr(),
377+
sample_sizes.len() as u32,
378+
params,
379+
);
380+
381+
if ZDICT_isError(dict_size) != 0 {
382+
Err(CStr::from_ptr(ZDICT_getErrorName(dict_size)).to_str())
383+
} else {
384+
dict_buffer.truncate(dict_size);
385+
386+
println!("Dictionary size: {}", dict_size);
387+
388+
Ok(dict_buffer)
389+
}
390+
});
391+
}
392+
393+
#[test]
394+
#[cfg(not(target_family = "wasm"))]
395+
#[cfg_attr(miri, ignore = "slow")]
396+
fn test_train_from_buffer_legacy_try_merge_2() {
397+
let _ = assert_eq_rs_c!({
398+
let mut sample_data: Vec<u8> = vec![
399+
31, 31, 255, 31, 31, 31, 19, 19, 19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255,
400+
255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31, 31,
401+
19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3,
402+
19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 31, 255, 31, 31, 31, 19, 19,
403+
19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 31, 31, 31, 19, 19, 19, 19,
404+
19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31, 31, 19, 255, 255, 255, 19, 19, 19,
405+
19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255, 255, 255, 255,
406+
19, 19, 19, 19, 19, 31, 31, 255, 31, 31, 31, 19, 19, 19, 19, 255, 255, 255, 19, 19, 19,
407+
19, 255, 19, 255, 255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255, 255, 255,
408+
255, 255, 31, 31, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19,
409+
19, 69, 19, 3, 19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 255, 31, 31,
410+
31, 19, 19, 19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 31, 31, 31, 19,
411+
19, 19, 19, 19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31, 31, 19, 255, 255, 255,
412+
19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255,
413+
255, 255, 255, 19, 19, 19, 19, 19, 31, 31, 255, 31, 31, 31, 19, 19, 19, 19, 255, 255,
414+
255, 19, 19, 19, 19, 255, 19, 255, 255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19,
415+
255, 255, 255, 255, 255, 255, 31, 31, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255,
416+
255, 37, 31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19,
417+
19, 31, 31, 255, 31, 31, 31, 19, 19, 19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19,
418+
255, 255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31,
419+
31, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19,
420+
3, 19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 31, 255, 31, 31, 31, 19,
421+
19, 19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 31, 31, 31, 19, 19, 19,
422+
19, 19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31, 31, 19, 255, 255, 255, 19, 19,
423+
19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255, 255, 255,
424+
255, 19, 19, 19, 19, 19, 255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255,
425+
255, 255, 255, 255, 31, 31, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37,
426+
31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 31,
427+
255, 31, 31, 31, 19, 19, 19, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 31,
428+
31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255, 255, 255, 255, 255, 31, 31, 19, 255,
429+
255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3, 19, 19,
430+
255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 31, 19, 19, 19, 19, 255, 255, 255, 19,
431+
19, 19, 19, 255, 19, 255, 255, 31, 31, 31, 19, 19, 19, 19, 19, 19, 19, 19, 255, 255,
432+
255, 255, 255, 255, 31, 31, 19, 255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37,
433+
31, 19, 19, 19, 69, 19, 3, 19, 19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19, 31, 19,
434+
255, 255, 255, 19, 19, 19, 19, 255, 19, 255, 255, 37, 31, 19, 19, 19, 69, 19, 3, 19,
435+
19, 255, 255, 255, 255, 255, 19, 19, 19, 19, 19,
436+
];
437+
438+
let sample_sizes = vec![72, 72, 72, 71, 72, 72, 72, 52, 72, 68, 33];
439+
440+
sample_data.resize(sample_sizes.iter().sum::<usize>(), 0u8);
441+
442+
let params = ZDICT_legacy_params_t {
443+
zParams: ZDICT_params_t {
444+
compressionLevel: 0,
445+
notificationLevel: 0,
446+
dictID: 0,
447+
},
448+
selectivityLevel: 0,
449+
};
450+
451+
let dict_capacity = 1024;
452+
let mut dict_buffer = vec![0u8; dict_capacity];
453+
454+
let dict_size = ZDICT_trainFromBuffer_legacy(
455+
dict_buffer.as_mut_ptr() as *mut c_void,
456+
dict_buffer.len(),
457+
sample_data.as_ptr() as *const c_void,
458+
sample_sizes.as_ptr(),
459+
sample_sizes.len() as u32,
460+
params,
461+
);
462+
463+
if ZDICT_isError(dict_size) != 0 {
464+
Err(CStr::from_ptr(ZDICT_getErrorName(dict_size)).to_str())
465+
} else {
466+
dict_buffer.truncate(dict_size);
467+
468+
println!("Dictionary size: {}", dict_size);
469+
470+
Ok(dict_buffer)
471+
}
472+
});
473+
}
474+
281475
#[test]
282476
#[cfg(not(target_family = "wasm"))]
283477
fn test_optimize_train_from_buffer_cover_single_threaded() {

0 commit comments

Comments
 (0)