-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: Update handling of large array sizes #8174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
5371e2a
limit total array size
mattwittwer 087ad05
Add customizable http input size limit
mattwittwer a96f0e1
remove debug statements
mattwittwer efb4e94
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer d5a44c8
update error message and include size test
mattwittwer 33d4a63
remove max_input_size check from generate handler
mattwittwer a271aed
update sagemaker and vertex_ai endpoints
mattwittwer 35e1cb6
created new tests for max http input size
mattwittwer 97a32fb
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer 7fdbbba
remove unused tests
mattwittwer 45d4d36
add parameter tests
mattwittwer e825e2c
revert changes to vertex ai and sagemaker endpoints
mattwittwer a595139
fix constructor
mattwittwer 403654a
spelling fix
mattwittwer f2882a7
whitespace fix
mattwittwer 27ead96
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer 37c1c73
improve test case readability
mattwittwer 64008c4
update test for int values
mattwittwer eafc2e6
update input to take values as MB
mattwittwer cdee641
update comment
mattwittwer 730f963
restore byte size input values
mattwittwer 66407f4
Merge remote-tracking branch 'origin/main' into mwittwer/shape_array_…
mattwittwer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,365 @@ | ||
| #!/usr/bin/python | ||
| # Copyright 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # * Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # * Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # * Neither the name of NVIDIA CORPORATION nor the names of its | ||
| # contributors may be used to endorse or promote products derived | ||
| # from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | ||
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
| # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| import sys | ||
|
|
||
| sys.path.append("../common") | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
| import requests | ||
| import test_util as tu | ||
|
|
||
| # Constants for size calculations | ||
| # Each FP32 value is 4 bytes, so we need to divide target byte sizes by 4 to get element counts | ||
| BYTES_PER_FP32 = 4 | ||
| MB = 2**20 # 1 MB = 1,048,576 bytes | ||
| DEFAULT_LIMIT_BYTES = 64 * MB # 64MB default limit | ||
| INCREASED_LIMIT_BYTES = 128 * MB # 128MB increased limit | ||
|
|
||
| # Calculate element counts for size limits | ||
| DEFAULT_LIMIT_ELEMENTS = DEFAULT_LIMIT_BYTES // BYTES_PER_FP32 # 16,777,216 elements | ||
| INCREASED_LIMIT_ELEMENTS = ( | ||
| INCREASED_LIMIT_BYTES // BYTES_PER_FP32 | ||
| ) # 33,554,432 elements | ||
|
|
||
| # Small offsets to go just over/under the limits | ||
| OFFSET_ELEMENTS = 32 | ||
|
|
||
|
|
||
| class InferSizeLimitTest(tu.TestResultCollector): | ||
| def _get_infer_url(self, model_name): | ||
| return "http://localhost:8000/v2/models/{}/infer".format(model_name) | ||
|
|
||
| def test_default_limit_rejection_raw_binary(self): | ||
| """Test raw binary inputs with default limit""" | ||
| model = "onnx_zero_1_float32" | ||
|
|
||
| # Test case 1: Input just over the 64MB limit (should fail) | ||
| # (2^24 + 32) elements * 4 bytes = 64MB + 128 bytes = 67,108,992 bytes | ||
| large_input = np.ones( | ||
| DEFAULT_LIMIT_ELEMENTS + OFFSET_ELEMENTS, dtype=np.float32 | ||
| ) | ||
| input_bytes = large_input.tobytes() | ||
| assert len(input_bytes) > 64 * MB # Verify we're actually over the 64MB limit | ||
|
|
||
| headers = {"Inference-Header-Content-Length": "0"} | ||
| response = requests.post( | ||
| self._get_infer_url(model), data=input_bytes, headers=headers | ||
| ) | ||
|
|
||
| # Should fail with 400 bad request with default limit | ||
| self.assertEqual( | ||
| 400, | ||
| response.status_code, | ||
| "Expected error code for oversized request, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify error message contains size limit info | ||
| error_msg = response.content.decode() | ||
| self.assertIn( | ||
| "exceeds the maximum allowed value", | ||
| error_msg, | ||
| "Expected error message about exceeding max input size", | ||
| ) | ||
|
|
||
| # Test case 2: Input just under the 64MB limit (should succeed) | ||
| # (2^24 - 32) elements * 4 bytes = 64MB - 128 bytes = 67,108,736 bytes | ||
| small_input = np.ones( | ||
| DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS, dtype=np.float32 | ||
| ) | ||
| input_bytes = small_input.tobytes() | ||
| assert len(input_bytes) < 64 * MB # Verify we're actually under the 64MB limit | ||
|
|
||
| response = requests.post( | ||
| self._get_infer_url(model), data=input_bytes, headers=headers | ||
| ) | ||
|
|
||
| # Should succeed with 200 OK | ||
| self.assertEqual( | ||
| 200, | ||
| response.status_code, | ||
| "Expected success code for request within size limit, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify output matches our input (identity model) | ||
| header_size = int(response.headers["Inference-Header-Content-Length"]) | ||
| output_data = response.content[header_size:] | ||
|
|
||
| # Convert output bytes back to numpy array for comparison | ||
| output_array = np.frombuffer(output_data, dtype=np.float32) | ||
| self.assertTrue( | ||
| np.array_equal(output_array, small_input), | ||
| "Response data does not match input data", | ||
| ) | ||
|
|
||
| def test_default_limit_rejection_json(self): | ||
| """Test JSON inputs with default limit""" | ||
| model = "onnx_zero_1_float32" | ||
|
|
||
| # Test case 1: Input just over the 64MB limit (should fail) | ||
| # (2^24 + 32) elements * 4 bytes = 64MB + 128 bytes = 67,108,992 bytes | ||
| shape_size = DEFAULT_LIMIT_ELEMENTS + OFFSET_ELEMENTS | ||
|
|
||
| payload = { | ||
| "inputs": [ | ||
| { | ||
| "name": "INPUT0", | ||
| "datatype": "FP32", | ||
| "shape": [1, shape_size], | ||
| "data": [1.0] * shape_size, | ||
| } | ||
| ] | ||
| } | ||
| assert ( | ||
| shape_size * BYTES_PER_FP32 > 64 * MB | ||
| ) # Verify we're actually over the 64MB limit | ||
|
|
||
| headers = {"Content-Type": "application/json"} | ||
| response = requests.post( | ||
| self._get_infer_url(model), headers=headers, json=payload | ||
| ) | ||
|
|
||
| # Should fail with 400 bad request with default limit | ||
| self.assertEqual( | ||
| 400, | ||
| response.status_code, | ||
| "Expected error code for oversized JSON request, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify error message contains size limit info | ||
| error_msg = response.content.decode() | ||
| self.assertIn( | ||
| "exceeds the maximum allowed value", | ||
| error_msg, | ||
| "Expected error message about exceeding max input size", | ||
| ) | ||
|
|
||
| # Test case 2: Input just under the 64MB limit (should succeed) | ||
| # (2^24 - 32) elements * 4 bytes = 64MB - 128 bytes = 67,108,736 bytes | ||
| shape_size = DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS | ||
|
|
||
| payload = { | ||
| "inputs": [ | ||
| { | ||
| "name": "INPUT0", | ||
| "datatype": "FP32", | ||
| "shape": [1, shape_size], | ||
| "data": [1.0] * shape_size, | ||
| } | ||
| ] | ||
| } | ||
| assert ( | ||
| shape_size * BYTES_PER_FP32 < 64 * MB | ||
| ) # Verify we're actually under the 64MB limit | ||
|
|
||
| response = requests.post( | ||
| self._get_infer_url(model), headers=headers, json=payload | ||
| ) | ||
|
|
||
| # Should succeed with 200 OK | ||
| self.assertEqual( | ||
| 200, | ||
| response.status_code, | ||
| "Expected success code for JSON request within size limit, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify we got a valid response | ||
| result = response.json() | ||
| self.assertIn("outputs", result, "Response missing outputs field") | ||
| self.assertEqual(1, len(result["outputs"]), "Expected 1 output") | ||
| self.assertEqual( | ||
| shape_size, | ||
| result["outputs"][0]["shape"][1], | ||
| f"Expected shape {[1, shape_size]}, got {result['outputs'][0]['shape']}", | ||
| ) | ||
|
|
||
| def test_large_input_raw_binary(self): | ||
| """Test raw binary input larger with custom limit set""" | ||
| model = "onnx_zero_1_float32" | ||
|
|
||
| # Test case 1: Input just over the 128MB configured limit (should fail) | ||
| # (2^25 + 32) elements * 4 bytes = 128MB + 128 bytes = 134,217,856 bytes | ||
| large_input = np.ones( | ||
| INCREASED_LIMIT_ELEMENTS + OFFSET_ELEMENTS, dtype=np.float32 | ||
| ) | ||
| input_bytes = large_input.tobytes() | ||
| assert len(input_bytes) > 128 * MB # Verify we're actually over the 128MB limit | ||
|
|
||
| headers = {"Inference-Header-Content-Length": "0"} | ||
| response = requests.post( | ||
| self._get_infer_url(model), data=input_bytes, headers=headers | ||
| ) | ||
|
|
||
| # Should fail with 400 bad request with our increased limit | ||
| self.assertEqual( | ||
| 400, | ||
| response.status_code, | ||
| "Expected error code for oversized request, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify error message contains size limit info | ||
| error_msg = response.content.decode() | ||
| self.assertIn( | ||
| "exceeds the maximum allowed value", | ||
| error_msg, | ||
| "Expected error message about exceeding max input size", | ||
| ) | ||
|
|
||
| # Test case 2: Input just under the 128MB configured limit (should succeed) | ||
| # (2^25 - 32) elements * 4 bytes = 128MB - 128 bytes = 134,217,600 bytes | ||
| small_input = np.ones( | ||
| INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS, dtype=np.float32 | ||
| ) | ||
| input_bytes = small_input.tobytes() | ||
| assert ( | ||
| len(input_bytes) < 128 * MB | ||
| ) # Verify we're actually under the 128MB limit | ||
|
|
||
| response = requests.post( | ||
| self._get_infer_url(model), data=input_bytes, headers=headers | ||
| ) | ||
|
|
||
| # Should succeed with 200 OK | ||
| self.assertEqual( | ||
| 200, | ||
| response.status_code, | ||
| "Expected success code for request within increased limit, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify output matches our input (identity model) | ||
| header_size = int(response.headers["Inference-Header-Content-Length"]) | ||
| output_data = response.content[header_size:] | ||
|
|
||
| # Convert output bytes back to numpy array for comparison | ||
| output_array = np.frombuffer(output_data, dtype=np.float32) | ||
| self.assertTrue( | ||
| np.array_equal(output_array, small_input), | ||
| "Response data does not match input data", | ||
| ) | ||
|
|
||
| def test_large_input_json(self): | ||
| """Test JSON input larger with custom limit set""" | ||
| model = "onnx_zero_1_float32" | ||
|
|
||
| # Test case 1: Input just over the 128MB configured limit (should fail) | ||
| # (2^25 + 32) elements * 4 bytes = 128MB + 128 bytes = 134,217,856 bytes | ||
| shape_size = INCREASED_LIMIT_ELEMENTS + OFFSET_ELEMENTS | ||
|
|
||
| payload = { | ||
| "inputs": [ | ||
| { | ||
| "name": "INPUT0", | ||
| "datatype": "FP32", | ||
| "shape": [1, shape_size], | ||
| "data": [1.0] * shape_size, | ||
| } | ||
| ] | ||
| } | ||
| assert ( | ||
| shape_size * BYTES_PER_FP32 > 128 * MB | ||
| ) # Verify we're actually over the 128MB limit | ||
|
|
||
| headers = {"Content-Type": "application/json"} | ||
| response = requests.post( | ||
| self._get_infer_url(model), headers=headers, json=payload | ||
| ) | ||
|
|
||
| # Should fail with 400 bad request with our increased limit | ||
| self.assertEqual( | ||
| 400, | ||
| response.status_code, | ||
| "Expected error code for oversized JSON request, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify error message contains size limit info | ||
| error_msg = response.content.decode() | ||
| self.assertIn( | ||
| "exceeds the maximum allowed value", | ||
| error_msg, | ||
| "Expected error message about exceeding max input size", | ||
| ) | ||
|
|
||
| # Test case 2: Input just under the 128MB configured limit (should succeed) | ||
| # (2^25 - 32) elements * 4 bytes = 128MB - 128 bytes = 134,217,600 bytes | ||
| shape_size = INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS | ||
|
|
||
| payload = { | ||
| "inputs": [ | ||
| { | ||
| "name": "INPUT0", | ||
| "datatype": "FP32", | ||
| "shape": [1, shape_size], | ||
| "data": [1.0] * shape_size, | ||
| } | ||
| ] | ||
| } | ||
| assert ( | ||
| shape_size * BYTES_PER_FP32 < 128 * MB | ||
| ) # Verify we're actually under the 128MB limit | ||
|
|
||
| response = requests.post( | ||
| self._get_infer_url(model), headers=headers, json=payload | ||
| ) | ||
|
|
||
| # Should succeed with 200 OK | ||
| self.assertEqual( | ||
| 200, | ||
| response.status_code, | ||
| "Expected success code for request within increased limit, got: {}".format( | ||
| response.status_code | ||
| ), | ||
| ) | ||
|
|
||
| # Verify we got a valid response | ||
| result = response.json() | ||
| self.assertIn("outputs", result, "Response missing outputs field") | ||
| self.assertEqual(1, len(result["outputs"]), "Expected 1 output") | ||
| self.assertEqual( | ||
| shape_size, | ||
| result["outputs"][0]["shape"][1], | ||
| f"Expected shape {[1, shape_size]}, got {result['outputs'][0]['shape']}", | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests might be a bit easier to maintain if you added something like this to these test cases:
Right now, it's a bit hard to read something like 224 (* 4 bytes per element) + 32 > 226, etc. because of the implicit
4 bytes per elementadding2to the exponent for comparisonThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Added in a test case for each input to validate the input size is larger/smaller than the limit as expected for each test:
https://github.com/triton-inference-server/server/blob/mwittwer/shape_array_vulnerability/qa/L0_http/http_input_size_limit_test.py#L67
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added a section for constants to be defined in one location. This reduces the number of separately defined values used in the tests:
https://github.com/triton-inference-server/server/blob/mwittwer/shape_array_vulnerability/qa/L0_http/http_input_size_limit_test.py#L38-L50