Commit Graph

5085 Commits

Author SHA1 Message Date
Vincent Rabaud
61791c774a Expand the 64-bit platforms for VP8LBitWriter
The defines are the same as the ones in bit_reader_utils.h

Change-Id: I3782425baf7dfb861dcc39d7683fba5c15e33f84
2025-10-10 09:22:25 +02:00
mxms
e40787da71 Add WEBP_UNSAFE_MEMCMP helper
Memcmp is used in other parts of libwebp which we didn't see yet in
webputils. The places that use memcp use it as an expression, (i.e. if
(memcmp(...)), so explicitly do not wrap in do { } while (0) guard.

Change-Id: Ifa972640bae052717a1af02112c62dc197dfe0f1
2025-10-09 16:38:41 +00:00
Vincent Rabaud
9636d8e04f Restrict MSAN fixes to old clang versions
Bug: 448420960
Change-Id: Ia9cb762bc9f5d9415a28ddebaf39855c46a51768
2025-10-09 10:19:04 +02:00
James Zern
64dce5d826 api.md: mention 'exact' default in WebPEncodeLossless*
Note that config.exact defaults to 0 and point users to WebPEncode() if
the default isn't acceptable. This duplicates the comment from encode.h
for visibility. Follow up to:
6d2e11ec encode.h: mention 'exact' default in WebPEncodeLossless*

Bug: 449296530
Change-Id: Iba44dbcbb179bd80ea0a6b9e129b2c7e0a406916
2025-10-06 15:23:36 -07:00
Vincent Rabaud
1f0a494e80 Fix 32 bit writing in VP8LPutBits
>>32 is undefined. VP8LPutBits is never called for 32 bits in the
pipeline though.

Change-Id: I11f0a4c15380ab94213adab25f06b2ab73e73519
2025-10-01 09:15:34 +02:00
Vincent Rabaud
0e5f4ee3de Fix endianness with CMake.
Original patch from C. Neidahl.

Change-Id: I734d7cb33c3da5abb8d4faf074277dba53b37147
2025-09-29 22:26:10 +02:00
Vincent Rabaud
158b533d3e Fix potential integer overflow
Bug: 447862936

Change-Id: I58129333520facf213b7961a284e07fe04a0e55e
2025-09-29 21:11:03 +02:00
Vincent Rabaud
13f42ea2d2 Remove dead code
rg==NULL is dealt with above. Reported by Coverity.

Change-Id: I703698efaf9d5dd8cfd16309d3d83040a76c0749
2025-09-29 15:29:50 +02:00
Vincent Rabaud
74f6afd3e6 Merge "Have lossless use ImportYUVAFromRGB" into main 2025-09-27 04:56:31 -07:00
Vincent Rabaud
0d14d84bdb Have lossless use ImportYUVAFromRGB
There was a duplicated functionality with a lower quality which
could lead to decoded lossless WebP to YUV being different from
lossless WebP to PNG to YUV.

The rescaler is not using it yet.

Bug: 432241412
Change-Id: Id794880957935b69729d4b34ae453551d13364dc
2025-09-24 16:43:02 +02:00
James Zern
c00d83f664 {xcframework,ios}build.sh: remove 32-bit targets
The armv7, armv7s and i386 iOS targets were deprecated in Xcode 14 [1]
and failed to build with Xcode 16.4.

[1]: https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes
Building iOS projects with deployment targets for the armv7, armv7s, and
i386 architectures is no longer supported. (92831716)

Change-Id: I08d376fea64638d056258798bd7e586ca6880454
2025-09-22 15:35:12 -07:00
Philippe Antoine
d5b3883812 fuzz: fix typo in nalloc env variable
Change-Id: Icc0d48c8699b0fc8e820e89c44bfc55ddfe4d675
2025-09-21 20:51:59 +02:00
James Zern
aae8a3da33 Merge "Add fbounds-safety annotations in quant_levels_dec_utils.c/.h." into main 2025-09-18 12:46:00 -07:00
Arman Hasanzadeh
b4dbec562f Add fbounds-safety annotations in quant_levels_dec_utils.c/.h.
Reasoning:

Image Data Buffers:
The `data` parameter of `WebPDequantizeLevels` (in both .c and .h)
and `InitParams` (src/utils/quant_levels_dec_utils.c:232) is annotated
with `WEBP_SIZED_BY((long)stride * height)`, as it points to the start
of the image buffer.

The `src` and `dst` fields in `SmoothParams`
(src/utils/quant_levels_dec_utils.c:54) are annotated as
`WEBP_INDEXABLE`. They are initialized from `data` in `InitParams`
(L266) and are advanced row by row using pointer arithmetic (e.g.,
`p->src += p->stride` in `VFilter` L111, `p->dst += p->stride` in
`ApplyFilter` L165). `WEBP_INDEXABLE` is used because the pointers
iterate within the buffer and are only accessed with positive indices.

Scratch Buffers (`SmoothParams`):
Scratch buffers are allocated in `InitParams` via `WebPSafeMalloc`.
The local variable `mem` holding this allocation (L245) is explicitly
annotated as `WEBP_BIDI_INDEXABLE` to ensure safety when compiling with
error suppression.

- `start`, `cur`, `top`: These pointers are used for iteration and
  pointer arithmetic within the circular scratch buffer. They are
  annotated as `WEBP_INDEXABLE`.
- `end`: This pointer is annotated as `WEBP_BIDI_INDEXABLE` because it
  is used in subtraction (`p->end - width`) in `InitParams` (L257) to
  calculate `p->top`.
- `average`: This buffer is accessed sequentially up to `width`. It is
  annotated as `WEBP_COUNTED_BY(width)`. Initialization in `InitParams`
  is reordered (L261) to ensure `p->width` is set before `p->average`.
- `correction`: This lookup table requires negative indexing. To avoid
  using `WEBP_BIDI_INDEXABLE` in the struct, it is annotated as
  `WEBP_COUNTED_BY_OR_NULL(CORRECTION_LUT_SIZE)` (L75), pointing to the
  start of the buffer. `CORRECTION_LUT_SIZE` is defined (L33).
  `InitCorrectionLUT` (L188) and `ApplyFilter` (L147) calculate a local
  middle pointer which is explicitly annotated as `WEBP_BIDI_INDEXABLE`
  to allow safe negative indexing.

Local Pointers:
To ensure safety when compiling with error suppression (where locals
default to unsafe), explicit annotations are added to local pointers
derived from safe struct members:
- `VFilter` (L87): `src`, `cur`, `top`, `out` are `WEBP_INDEXABLE`.
- `HFilter` (L121): `in`, `out` are `WEBP_INDEXABLE`.
- `ApplyFilter` (L145): `average`, `dst` are `WEBP_INDEXABLE`.
- `CountLevels` (L214): `data` is `WEBP_INDEXABLE`.

Bug: 432511821
Change-Id: I6bdf86f80c94a5b182c5aef7e4092fe4ea24afb8
2025-09-17 15:31:57 -07:00
James Zern
d16489f66c Merge "fuzz: add allocations failures injections" into main 2025-09-17 15:18:26 -07:00
skal
0fd008f832 sharpyuv: remove unnecessary rgb_bit_depth -> bit_depth
Change-Id: I1c57dd83720ee286636762f0dd5ac586930d7838
2025-09-16 08:48:08 +02:00
James Zern
3779daa97f Merge "Add fbounds-safety annotations in huffman_utils.c/.h." into main 2025-08-27 12:34:36 -07:00
Arman Hasanzadeh
f2372fba3b Add fbounds-safety annotations in huffman_utils.c/.h.
Reasoning:

In `HuffmanTablesSegment` (`src/utils/huffman_utils.h`), `start`
was annotated `WEBP_COUNTED_BY_OR_NULL(size)` as it points to an
allocation of `size` elements. `curr_table` was annotated
`WEBP_UNSAFE_INDEXABLE` because it iterates within `[start, start
+ size)`, a bound that cannot be expressed statically in the struct
without ABI changes. The code manually checks bounds for
`curr_table` (e.g., `src/utils/huffman_utils.c:240-241`). To
support the annotation on `start`, allocation sites in
`VP8LBuildHuffmanTable` and `VP8LHuffmanTablesAllocate`
(`src/utils/huffman_utils.c`) were refactored to assign `start` and
`size` side-by-side, using `WEBP_BIDI_INDEXABLE` local variables to
hold the safe pointer returned by `WebPSafeMalloc`.
`VP8LHuffmanTablesDeallocate` was updated to set `size` to 0 when
`start` is freed.

The `root_table` parameter of `BuildHuffmanTable`
(`src/utils/huffman_utils.c:86`) was annotated `WEBP_BIDI_INDEXABLE` to
accommodate accesses to secondary tables beyond the `root table` size
since with explicitly annotating the local variable `table`
as `WEBP_BIDI_INDEXABLE`, `table` inherits its bounds from `root_table`.
Call sites in `VP8LBuildHuffmanTable`
required `WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` to convert the unsafe
`curr_table` to the safe `root_table`.

The `table` parameter of `ReplicateValue`
(`src/utils/huffman_utils.c:59`) was annotated
`WEBP_COUNTED_BY(end - step + 1)` and the function was refactored to
avoid modifying `end`. Call sites in `BuildHuffmanTable` required
`WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` because the strided access
patterns used for Huffman table construction cannot be statically
verified by the compiler.

Bug: 432511821
Change-Id: I77c5c82ac36bc9bb79cd5119a4113ac5d62af762
2025-08-27 10:31:58 -07:00
Yannis Guyon
fd2c2cc05b Merge "Add canvas carryover to libwebp anim_encode" into main 2025-08-26 13:40:53 -07:00
Philippe Antoine
fdc81cebda fuzz: add allocations failures injections
nallocfuzz comes from https://github.com/catenacyber/nallocfuzz

Change-Id: Ia5be1ffd91cccca135927f0f43da04abc8194d27
2025-08-26 21:20:12 +02:00
James Zern
ed8b34cf16 Merge "WebPEstimateBestFilter: remove unneeded stride param" into main 2025-08-26 10:49:51 -07:00
Yannis Guyon
94bfff3ffe Add canvas carryover to libwebp anim_encode
Memory footprint is increased by twice the canvas pixel count in bytes
at encoding. There should be little impact on encoding speed because
only buffer allocs/reads/writes are introduced, with little to no
added logic. Animation encoding may be 2% slower.

Bug: 42340478
Change-Id: I8f0048107a2bfbee7a8124c100f78eac93447d80
2025-08-22 12:00:22 +00:00
James Zern
1ba05593d0 Merge "Add missing {}s to conditionals & loops" into main 2025-08-21 12:37:16 -07:00
James Zern
b3f8ce7015 WebPEstimateBestFilter: remove unneeded stride param
The calling function, `GetFilterMap()` only takes `width` and `height`;
the alpha data is assumed to have a stride equal to its width. The
`WebPEstimateBestFilter()` was inconsistently using the parameters,
setting up the current row with `stride`, but accessing the previous one
with `width`.

Change-Id: I9dd90222b6923eea3626e426a61bdef3985546ff
2025-08-21 10:43:24 -07:00
James Zern
2074cb4ba1 Merge "Add fbounds-safety annotations for WebPRescaler." into main 2025-08-20 19:06:52 -07:00
Arman Hasanzadeh
1fdd4ef501 Add fbounds-safety annotations for WebPRescaler.
Reasoning:

The `irow` and `frow` pointers in `WebPRescaler`
(src/utils/rescaler_utils.h:49) were annotated with
`WEBP_COUNTED_BY(dst_width * num_channels)`. This is based on their
initialization in `WebPRescalerInit` (src/utils/rescaler_utils.c:82-83)
where they are assigned parts of the `work` buffer, whose total size
is `2 * dst_width * num_channels`. The `work` parameter in
`WebPRescalerInit` (src/utils/rescaler_utils.h:58,
src/utils/rescaler_utils.c:33) was also annotated accordingly.

To satisfy the side-by-side assignment requirement for external bounds,
assignments to `rescaler->irow` and `rescaler->frow` in
`WebPRescalerInit` were moved closer to the assignments of
`dst_width` and `num_channels` (src/utils/rescaler_utils.c:50-53).
Since `work` have bound information, `WEBP_UNSAFE_MEMSET` has
been changed to `memset`.

In `WebPRescalerImport` (src/utils/rescaler_utils.c:140-150), where
`irow` and `frow` are swapped, self-assignments for `dst_width` and
`num_channels` were added side-by-side with the pointer assignments.
Additionally, `WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` was used for the
pointer assignments to handle the `WEBP_ASSUME_UNSAFE_INDEXABLE_ABI`
setting used during testing.

Bug: 432511821
Change-Id: If716fb79a06dee9e807eff060806daf038810523
2025-08-20 17:56:26 -07:00
James Zern
235286fd78 Add missing {}s to conditionals & loops
This was necessary after:
44257cb8 apply clang-format

Which made some single-line statements into multi-line. Using braces on
multi-line statements better conforms with the Google style guide.

Bug: 433996651
Change-Id: I615c0ecf3b94571f67fceadfe8c15914aea45ccb
2025-08-20 16:22:29 -07:00
James Zern
dab2cf21fa Merge "Add fbounds-safety annotations in palette.c/.h." into main 2025-08-20 15:10:00 -07:00
James Zern
b7d30cfd94 Merge "Add fbounds-safety annotations for sorted." into main 2025-08-20 15:04:27 -07:00
James Zern
17ba97c149 Merge "Add fbounds-safety annotations for data." into main 2025-08-20 14:07:31 -07:00
Arman Hasanzadeh
4cdb42070f Add fbounds-safety annotations for sorted.
Reasoning:

The `sorted` parameter in `BuildHuffmanTable`
(`src/utils/huffman_utils.c:87`) is annotated with
`WEBP_COUNTED_BY_OR_NULL(code_lengths_size)`. Analysis of the
access patterns (lines 137, 177, 207) shows that the indices used
are bounded by `code_lengths_size`. Since `sorted` can be NULL,
`_OR_NULL` is used. When compiling, calls
to `BuildHuffmanTable` in `VP8LBuildHuffmanTable` (line 272) required forging bounds because the `sorted` buffer, allocated
via `WebPSafeMalloc` or on the stack, was treated as unsafe.
`WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` is used at the call sites to
provide the necessary bounds information.

Bug: 432511821
Change-Id: I6fea3ac5d77cb56139f9748ba0277a4f0ad21737
2025-08-20 13:22:41 -07:00
Arman Hasanzadeh
baf42a5876 Add fbounds-safety annotations for data.
Reasoning:

The `data` pointer in `WebPEstimateBestFilter` is annotated with
`WEBP_COUNTED_BY((size_t)height * width)`. This reflects the
allocation size found by tracing the call graph back to
`EncodeAlpha` (src/enc/alpha_enc.c:332), where the buffer is
allocated using `WebPSafeMalloc` with a size of `width * height`.
Although the function signature includes a `stride` parameter used
for pointer arithmetic (`data + j * stride` at
src/utils/filters_utils.c:44), the implementation implicitly assumes
`stride == width` for vertical and gradient filtering logic (e.g.,
accesses like `p[i - width]` at line 49). The only caller
(src/enc/alpha_enc.c:220) respects this assumption by passing `width`
as the `stride`. Therefore, `height * width` accurately represents
the required buffer size.

Bug: 432511821
Change-Id: I7d308cf8dcccd7d6128a17dbc0f7b177ee9282a1
2025-08-20 13:04:51 -07:00
mxms
7ee251d3fd Add -fbounds-safety to webpdecodeutils
webpdecodeutils is a subset of webputils, so we add -fbounds-safety to
it to ensure that the files are being compiled the same way between
modules.

Bug: webp:432511821
Change-Id: I5c01a87601a13c331b628c605238e645d1efa77f
2025-08-20 12:47:15 -07:00
James Zern
69c8056c7a Merge "Add fbounds-safety annotations for data." into main 2025-08-20 12:28:49 -07:00
James Zern
5d3a9fc55b Merge "Add fbounds-safety annotations for count." into main 2025-08-20 12:24:13 -07:00
Arman Hasanzadeh
52135b8e00 Add fbounds-safety annotations in palette.c/.h.
Reasoning:

The `palette` parameter in `PaletteSortModifiedZeng` (src/utils/palette.c)
was accessed using `palette[i]` in a loop from 0 to `num_colors` (line
386), indicating it's an array of size `num_colors`. The `palette_in`
parameter was also deduced to be of size `num_colors` based on its
usage and how the indices accessing it are generated. Therefore, both
`palette` and `palette_in` were annotated with
`WEBP_COUNTED_BY(num_colors)`. This change required updating the caller
function `PaletteSort` (lines 393-395 in src/utils/palette.c and lines
61-63 in src/utils/palette.h) to match the new signature by adding the
same annotations to its `palette_sorted` and `palette` parameters.

The `palette` parameter in
`PaletteHasNonMonotonousDeltas` was being indexed like an array but was
typed as a `WEBP_SINGLE` pointer. Since `palette` is indexed up to
`num_colors`, it was annotated with `WEBP_COUNTED_BY(num_colors)`. This
introduced a warning at the call site in `PaletteSortMinimizeDeltas`
(src/utils/palette.c:197), as it passed a `WEBP_SINGLE` pointer
(`palette_sorted`) where `WEBP_COUNTED_BY` was expected. Analysis showed
both `palette_sorted` and `palette` parameters in
`PaletteSortMinimizeDeltas` are accessed up to `num_colors`, so they were
annotated with `WEBP_COUNTED_BY(num_colors)`.

The `sorted` parameter in `SearchColorNoIdx`
was annotated with `__counted_by(num_colors)` in both the definition
(src/utils/palette.c:68) and declaration (src/utils/palette.h:40).
This change led to cascading errors during testing.
These errors occurred because callers
passed pointers that were considered `__unsafe_indexable` under this
setting. To resolve this, the following functions were also
annotated:
- `PrepareMapToPalette` (src/utils/palette.c:85, src/utils/palette.h:46):
  `palette`, `sorted`, and `idx_map` were annotated with
  `__counted_by(num_colors)`.

The pointer `cooccurrence` in `src/utils/palette.c` is used as a
flattened 2D array of size `num_colors * num_colors` in functions
`CoOccurrenceFindMax` (lines 226-252) and `CoOccurrenceBuild` (lines
255-300). The parameters in these functions were annotated with
`WEBP_COUNTED_BY(num_colors * num_colors)` to reflect this usage and
fix the original bounds safety errors during indexing.

In the caller function `PaletteSortModifiedZeng` (lines 307-391),
`cooccurrence` is a local variable allocated using `WebPSafeCalloc`.
To ensure compatibility with the annotated parameters, especially
during test builds where local variables appeared to be treated as
unsafe pointers, `WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` was used when
passing `cooccurrence` to `CoOccurrenceBuild` (line 327) and
`CoOccurrenceFindMax` (line 333). This ensures a pointer with the
correct bounds is passed to the callees in all build configurations.

Bug: 432511821
Change-Id: I7540968ecca67645c5ca57e542433971b235e582
2025-08-20 08:27:00 -07:00
Yannis Guyon
ddcdfa6a42 Refactor libwebp anim_encode curr prev rectangles
Previously, WebPAnimEncoder::prev_rect was set in
SetFrame()>PickBestCandidate(), and SetFrame() may be called twice in
a row in CacheFrame(), making prev_rect value wrong in the second
SetFrame() call. Fortunately, the second call is always for a
keyframe, and in that case SetFrame() does not use the value of
prev_rect.

Since commit b1feaa4 that may have been a bigger issue as
PickBestCandidate() was moved from the end of SetFrame() to
SetFrame()>GenerateCandidates()>PickBestCandidate(), effectively
changing the value of prev_rect for non-keyframe candidates within the
same SetFrame() call. Fortunately, prev_rect is only used at the
beginning of SetFrame(), before any call to GenerateCandidates(),
hence the no-op change.

Change-Id: I693dbf9d264b8768d25a29d75b511e616a3db56b
2025-08-20 07:52:53 +00:00
Arman Hasanzadeh
ddabb66f23 Add fbounds-safety annotations for data.
Reasoning:

The `data` parameter in `QuantizeLevels` (defined in
`src/utils/quant_levels_utils.c` line 33) was causing bounds safety
errors because it was accessed using array subscripts (e.g., `data[n]`
at lines 60-63 and 137) but was typed as a single pointer. The size of
the buffer pointed to by `data` is determined by the `width` and
`height` parameters, calculated as `data_size = height * width` at
line 39. The loops accessing `data` iterate up to `data_size`. To fix
this, the `data` parameter in both the function definition and its
declaration (`src/utils/quant_levels_utils.h` line 30) was annotated
with `WEBP_COUNTED_BY((size_t)width * height)`.

Bug: 432511821
Change-Id: Idfe8810eaaf1239d86e38eb661a1a987d817127c
2025-08-19 22:33:39 -07:00
Arman Hasanzadeh
f66f1ee95c Add fbounds-safety annotations for count.
Reasoning:

The function `NextTableBitSize` is only called by `BuildHuffmanTable`
(src/utils/huffman_utils.c:196). In `BuildHuffmanTable`, `count` is
defined as a local array `int count[MAX_ALLOWED_CODE_LENGTH + 1]`
(line 93). This array decays to a pointer when passed to
`NextTableBitSize`. The maximum index accessed within `NextTableBitSize`
is `MAX_ALLOWED_CODE_LENGTH - 1` (line 75, inside a loop where `len`
starts <= `MAX_ALLOWED_CODE_LENGTH` and increments up to
`MAX_ALLOWED_CODE_LENGTH`).

Since the caller provides an array of size `MAX_ALLOWED_CODE_LENGTH + 1`
and the accesses are within bounds, the `count` parameter in
`NextTableBitSize` is annotated with
`WEBP_COUNTED_BY(MAX_ALLOWED_CODE_LENGTH + 1)`. This resolves the
compiler error.

Bug: 432511821
Change-Id: I29a55dd7f09c04aa3a2394996cf0e123d985fcd0
2025-08-19 22:15:25 -07:00
James Zern
f2061209d0 Merge "Add fbounds-safety annotations for histogram." into main 2025-08-19 13:58:27 -07:00
Arman Hasanzadeh
fde90a49e4 Add fbounds-safety annotations for histogram.
Reasoning:

The `histogram` parameter in `GenerateOptimalTree`
(src/utils/huffman_encode_utils.c:170) is used as an array indexed up
to `histogram_size`. It was annotated with
`__counted_by(histogram_size)` to fix bounds-safety errors. This
required annotating the caller `VP8LCreateHuffmanTree`
(src/utils/huffman_encode_utils.c:411). However, the size of
`histogram` in `VP8LCreateHuffmanTree` depends on another parameter
(`huff_code->num_symbols`), preventing direct annotation in the
signature. Instead, a local `__bidi_indexable` pointer
(`bounded_histogram`) was forged inside `VP8LCreateHuffmanTree` using
`__unsafe_forge_bidi_indexable` with the correct size and passed to
`GenerateOptimalTree`. This also required annotating parameters
`good_for_rle` and `counts` in `OptimizeHuffmanForRle`
(src/utils/huffman_encode_utils.c:37) and forging a bounded pointer
for `buf_rle` (`bounded_buf_rle`) in `VP8LCreateHuffmanTree` to pass
to `OptimizeHuffmanForRle`.

Bug: 432511821
Change-Id: I5905ff93f4cc0a9ff03c5786b5ac20c6bfa965ff
2025-08-19 12:34:27 -07:00
James Zern
2be405c472 Merge "Add fbounds-safety annotations for palette." into main 2025-08-19 12:24:38 -07:00
James Zern
ed02bfa963 Merge "Add fbounds-safety annotations for data." into main 2025-08-19 11:58:43 -07:00
James Zern
9d690dbf06 Merge "Add fbounds-safety annotations for VP8LBitWriter." into main 2025-08-19 11:55:47 -07:00
James Zern
603d4055bc Merge "Add fbounds-safety annotations for pool." into main 2025-08-19 11:54:47 -07:00
Arman Hasanzadeh
69b9b8525e Add fbounds-safety annotations for palette.
Reasoning:

The `palette` parameter in `GetColorPalette` (src/utils/palette.c:100)
was annotated with `WEBP_COUNTED_BY_OR_NULL(MAX_PALETTE_SIZE)` to fix
an array subscript error at src/utils/palette.c:146. This annotation
reflects the function's contract, documented in src/utils/palette.h,
which states that if `palette` is not NULL, it must point to memory
allocated for at least `MAX_PALETTE_SIZE` elements. To make
`MAX_PALETTE_SIZE` visible, `src/webp/format_constants.h` was included
in `src/utils/palette.h`. Consequently, the wrapper function
`WebPGetColorPalette` (src/utils/utils.c:273) and its declaration in
`src/utils/utils.h` were also annotated similarly, requiring the
inclusion of `src/webp/format_constants.h` in `src/utils/utils.h` as
well. This resolved a compilation error caused by the type mismatch
when calling the annotated `GetColorPalette` from `WebPGetColorPalette`.

Bug: 432511821
Change-Id: Iceebf2facf9558dd49889f056f028d9a3fb22d41
2025-08-19 11:52:40 -07:00
James Zern
9b3fc5f5e8 Merge "Add fbounds-safety annotations for tokens." into main 2025-08-19 11:36:44 -07:00
Arman Hasanzadeh
30b2c593c9 Add fbounds-safety annotations for VP8LBitWriter.
Reasoning:

The struct `VP8LBitWriter` in `src/utils/bit_writer_utils.h` uses
`buf`, `cur`, and `end` pointers to manage a dynamic buffer. Pointer
arithmetic on `buf` and `cur` caused bounds safety errors.

Initial attempts using `WEBP_ENDED_BY(end)` for both `buf` and `cur`
failed due to compiler limitations (one field cannot be the upper
bound for multiple other fields). Using `WEBP_INDEXABLE` for `buf` and
`cur` resolved the arithmetic errors but caused ABI incompatibility
issues, as `VP8LBitWriter` is used in multiple compilation units with
different settings.

The final approach annotates `buf` with `WEBP_ENDED_BY(end)` and `cur`
with `WEBP_UNSAFE_INDEXABLE`. This resolves the arithmetic errors for
both pointers without changing the ABI. `WEBP_UNSAFE_INDEXABLE` is
used for `cur` as a workaround for the `WEBP_ENDED_BY` limitation and
ABI constraints.

Additionally, the `VP8LBitWriterResize` function in
`src/utils/bit_writer_utils.c` was modified:
- The assignments to `bw->buf`, `bw->end`, and `bw->cur` (lines
  231-233) were reordered and updated to use the local
  `allocated_buf` variable on the right-hand side. This satisfies the
  consecutive assignment requirement imposed by `WEBP_ENDED_BY(end)`
  on `bw->buf`.
- The local variable `allocated_buf` (line 207) was annotated with
  `WEBP_BIDI_INDEXABLE`.
- The allocation for `allocated_buf` (line 222) now uses
  `WEBP_UNSAFE_FORGE_BIDI_INDEXABLE` to create a safe, bounded
  pointer from the result of `WebPSafeMalloc`, fixing potential type
  mismatches when `suppress_fbounds_errors=yes`.

Bug: 432511821
Change-Id: I603a6a7d3ff3bf2a8edf9749a9898ea227c32982
2025-08-19 07:38:06 -07:00
Arman Hasanzadeh
46d65e4a19 Add fbounds-safety annotations for data.
Reasoning:

Analysis of `GetLE32` and its called function `GetLE16`
(src/utils/utils.h:95) showed that `GetLE32` requires access to 4
bytes starting from `data`. `GetLE16` accesses 2 bytes.

Therefore, `WEBP_COUNTED_BY(4)` was added to the `data` parameter of
`GetLE32`. Similarly, `WEBP_COUNTED_BY(2)` was added to `GetLE16`.

During this analysis, related functions `GetLE24`, `PutLE16`,
`PutLE24`, and `PutLE32` (src/utils/utils.h lines 99-123) were also
identified as needing similar annotations. They were annotated with
`WEBP_COUNTED_BY(3)`, `WEBP_COUNTED_BY(2)`, `WEBP_COUNTED_BY(3)`, and
`WEBP_COUNTED_BY(4)` respectively, based on the number of bytes they
access or modify.

Bug: 432511821
Change-Id: I5783392bc8dcaa2f346a81928ef496fc52da3a30
2025-08-18 23:41:03 -07:00
Arman Hasanzadeh
10d81e1ef0 Add fbounds-safety annotations for pool.
Reasoning:

The `pool` parameter in the static function `SetBitDepths`
(`src/utils/huffman_encode_utils.c:140`) is indexed as an array. Since
the function is static and the exact size is not available in the
signature, `pool` was annotated as `__bidi_indexable`. This required
updating the caller, `GenerateOptimalTree` (also static), where the
corresponding argument `tree_pool` is derived from the `tree` parameter.
Consequently, both the `tree` parameter and the local variable
`tree_pool` in `GenerateOptimalTree`
(`src/utils/huffman_encode_utils.c:170`) were also annotated as
`__bidi_indexable`. Finally, the call to `GenerateOptimalTree` from the
ABI-visible function `VP8LCreateHuffmanTree`
(`src/utils/huffman_encode_utils.c:417`) required adapting the
`huff_tree` pointer. Tracing the allocation in `src/enc/vp8l_enc.c:473`
revealed that `huff_tree` is allocated with enough space for
`3 * max_num_symbols`. Therefore, `__unsafe_forge_bidi_indexable` was
used to cast `huff_tree` to `__bidi_indexable` with the calculated
bounds (`3 * num_symbols`).

Bug: 432511821
Change-Id: I53c6965c3fd708256241a225ec9223085a1ec2d4
2025-08-18 23:31:14 -07:00