xref: /aosp_15_r20/external/zstd/tests/fuzz/seq_prod_fuzz_example/example_seq_prod.c (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1 /*
2  * Copyright (c) Yann Collet, Meta Platforms, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 #include "fuzz_third_party_seq_prod.h"
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
17 _Thread_local size_t threadLocalState;
18 
FUZZ_seqProdSetup(void)19 size_t FUZZ_seqProdSetup(void) {
20     threadLocalState = 0;
21     return 0;
22 }
23 
FUZZ_seqProdTearDown(void)24 size_t FUZZ_seqProdTearDown(void) {
25     return 0;
26 }
27 
FUZZ_createSeqProdState(void)28 void* FUZZ_createSeqProdState(void) {
29     return calloc(1, sizeof(size_t));
30 }
31 
FUZZ_freeSeqProdState(void * state)32 size_t FUZZ_freeSeqProdState(void* state) {
33     free(state);
34     return 0;
35 }
36 
FUZZ_thirdPartySeqProd(void * sequenceProducerState,ZSTD_Sequence * outSeqs,size_t outSeqsCapacity,const void * src,size_t srcSize,const void * dict,size_t dictSize,int compressionLevel,size_t windowSize)37 size_t FUZZ_thirdPartySeqProd(
38     void* sequenceProducerState,
39     ZSTD_Sequence* outSeqs, size_t outSeqsCapacity,
40     const void* src, size_t srcSize,
41     const void* dict, size_t dictSize,
42     int compressionLevel,
43     size_t windowSize
44 ) {
45     /* Try to catch unsafe use of the shared state */
46     size_t* const sharedStatePtr = (size_t*)sequenceProducerState;
47     assert(*sharedStatePtr == threadLocalState);
48     (*sharedStatePtr)++; threadLocalState++;
49 
50     /* Check that fallback is enabled when FUZZ_THIRD_PARTY_SEQ_PROD is defined */
51     return ZSTD_SEQUENCE_PRODUCER_ERROR;
52 }
53