|
| 1 | +// Copyright 2026 Code Intelligence GmbH |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "synthetic_symbolizer.h" |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <cstring> |
| 19 | +#include <string> |
| 20 | + |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +// Stubs for libFuzzer symbols pulled in transitively via counters_tracker. |
| 24 | +extern "C" { |
| 25 | +void __sanitizer_cov_8bit_counters_init(uint8_t *, uint8_t *) {} |
| 26 | +void __sanitizer_cov_pcs_init(const uintptr_t *, const uintptr_t *) {} |
| 27 | +size_t __sanitizer_cov_get_observed_pcs(uintptr_t **) { return 0; } |
| 28 | +} |
| 29 | + |
| 30 | +// Helper: call SymbolizePC with an unregistered PC and return the result. |
| 31 | +static std::string Symbolize(uintptr_t pc, const char *fmt, |
| 32 | + size_t buf_size = 1024) { |
| 33 | + std::string buf(buf_size, '\0'); |
| 34 | + jazzer::SymbolizePC(pc, fmt, buf.data(), buf_size); |
| 35 | + return {buf.c_str()}; |
| 36 | +} |
| 37 | + |
| 38 | +// The default libFuzzer format for DescribePC is "%p %F %L". |
| 39 | +// With no registered locations, we should get clean <unknown> fallback. |
| 40 | +TEST(SyntheticSymbolizerTest, UnregisteredPCProducesUnknownFallback) { |
| 41 | + auto result = Symbolize(42, "%p %F %L"); |
| 42 | + // %p should be eaten (virtual PCs are meaningless), leaving "%F %L". |
| 43 | + EXPECT_NE(std::string::npos, result.find("in <unknown>")); |
| 44 | + EXPECT_NE(std::string::npos, result.find("<unknown>:0")); |
| 45 | + // No hex address should appear (the %p was consumed). |
| 46 | + EXPECT_EQ(std::string::npos, result.find("0x")); |
| 47 | +} |
| 48 | + |
| 49 | +// A small buffer should truncate without crashing. |
| 50 | +TEST(SyntheticSymbolizerTest, SmallBufferTruncatesSafely) { |
| 51 | + char tiny[8] = {}; |
| 52 | + jazzer::SymbolizePC(42, "%F %L", tiny, sizeof(tiny)); |
| 53 | + // Must be null-terminated and not overflow. |
| 54 | + EXPECT_LT(strlen(tiny), sizeof(tiny)); |
| 55 | + |
| 56 | + // Zero-size buffer is a no-op. |
| 57 | + char zero = 'X'; |
| 58 | + jazzer::SymbolizePC(42, "%F", &zero, 0); |
| 59 | + EXPECT_EQ('X', zero); |
| 60 | +} |
| 61 | + |
| 62 | +// Verify individual format specifiers produce the right fallback shape. |
| 63 | +TEST(SyntheticSymbolizerTest, FormatSpecifiers) { |
| 64 | + EXPECT_EQ("in <unknown>", Symbolize(42, "%F")); |
| 65 | + EXPECT_EQ("<unknown>:0", Symbolize(42, "%L")); |
| 66 | + EXPECT_EQ("<unknown>", Symbolize(42, "%s")); |
| 67 | + EXPECT_EQ("0", Symbolize(42, "%l")); |
| 68 | + EXPECT_EQ("0", Symbolize(42, "%c")); |
| 69 | + // Literal text passes through. |
| 70 | + EXPECT_EQ("hello", Symbolize(42, "hello")); |
| 71 | +} |
0 commit comments