argotorg / argotorg/solidity

Crash on invalid UTF-8 bytes in Natspec

Open
#16,519 1 comment 0 reactions 0 assignees View on GitHub
bug :bug: low effort low impact must have eventually
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
2d 19h
Merged PRs (30d)
29

Description

## Description

```
./solc/solc --optimize --ir minimized-from-ab4ee1498fff0c71961d81fc308f1dee0be16f33
Uncaught exception:
Dynamic exception type: nlohmann::json_abi_v3_12_0::detail::type_error
std::exception::what: [json.exception.type_error.316] invalid UTF-8 byte at index 0: 0xF7
```

Input:
```
$ xxd minimized-from-ab4ee1498fff0c71961d81fc308f1dee0be16f33
00000000: 2f2f 2ff7 0a63 6f6e 7472 6163 7420 677b ///..contract g{
00000010: 7d }
```

GDB backtrace:
```
(gdb) r
Starting program: /home/matesoos/development/solidity/develop/build/solc/solc --optimize --ir minimized-from-ab4ee1498fff0c71961d81fc308f1dee0be16f33
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Catchpoint 1 (exception thrown), 0x00007ffff7cb213a in __cxxabiv1::__cxa_throw (obj=0x55555649b860, tinfo=0x55555643f578 ,
dest=0x555555791a30 ) at /usr/src/debug/gcc/gcc/libstdc++-v3/libsupc++/eh_throw.cc:81
81 PROBE2 (throw, obj, tinfo);
(gdb) bt
#0 0x00007ffff7cb213a in __cxxabiv1::__cxa_throw (obj=0x55555649b860, tinfo=0x55555643f578 ,
dest=0x555555791a30 ) at /usr/src/debug/gcc/gcc/libstdc++-v3/libsupc++/eh_throw.cc:81
#1 0x000055555613ca04 in nlohmann::json_abi_v3_12_0::detail::serializer, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void> >::dump_escaped (this=this@entry=0x7fffffffb270, s=, ensure_ascii=)
at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/detail/output/serializer.hpp:513
#2 0x000055555613fd6c in nlohmann::json_abi_v3_12_0::detail::serializer, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void> >::dump (this=this@entry=0x7fffffffb270, val=..., pretty_print=pretty_print@entry=false, ensure_ascii=ensure_ascii@entry=true, indent_step=indent_step@entry=0,
current_indent=current_indent@entry=0) at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/detail/output/serializer.hpp:250
#3 0x0000555556140230 in nlohmann::json_abi_v3_12_0::detail::serializer, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void> >::dump (this=this@entry=0x7fffffffb270, val=..., pretty_print=pretty_print@entry=false, ensure_ascii=ensure_ascii@entry=true, indent_step=,
current_indent=current_indent@entry=0) at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/detail/output/serializer.hpp:170
#4 0x000055555614033d in nlohmann::json_abi_v3_12_0::detail::serializer, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void> >::dump (this=this@entry=0x7fffffffb270, val=..., pretty_print=pretty_print@entry=false, ensure_ascii=ensure_ascii@entry=true, indent_step=indent_step@entry=0,
current_indent=current_indent@entry=0) at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/detail/output/serializer.hpp:180
#5 0x0000555556140230 in nlohmann::json_abi_v3_12_0::detail::serializer, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void> >::dump (this=0x7fffffffb270, val=..., pretty_print=pretty_print@entry=false, ensure_ascii=, indent_step=indent_step@entry=0, current_indent=0)
at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/detail/output/serializer.hpp:170
#6 0x0000555556140b41 in nlohmann::json_abi_v3_12_0::basic_json, std::allocator >, bool, long, unsigned long, double, std::allocator, nlohmann::json_abi_v3_12_0::adl_serializer, std::vector >, void>::dump (this=this@entry=0x7fffffffb620,
indent=indent@entry=-1, indent_char=indent_char@entry=32 ' ', ensure_ascii=ensure_ascii@entry=true,
error_handler=error_handler@entry=nlohmann::json_abi_v3_12_0::detail::error_handler_t::strict)
at /home/matesoos/development/solidity/develop/deps/nlohmann-json/include/nlohmann/json.hpp:1313
#7 0x0000555556138441 in solidity::util::jsonPrint (_input=..., _format=...) at /home/matesoos/development/solidity/develop/libsolutil/JSON.cpp:132
#8 solidity::util::jsonCompactPrint (_input=...) at /home/matesoos/development/solidity/develop/libsolutil/JSON.cpp:127
#9 0x0000555555873a72 in solidity::frontend::CompilerStack::createMetadata[abi:cxx11](solidity::frontend::CompilerStack::Contract const&, bool) const (this=this@entry=0x55555649a6c0,
_contract=..., _forIR=_forIR@entry=true) at /home/matesoos/development/solidity/develop/libsolidity/interface/CompilerStack.cpp:1863
#10 0x0000555555874bcc in solidity::frontend::CompilerStack::createCBORMetadata (this=this@entry=0x55555649a6c0, _contract=..., _forIR=_forIR@entry=true)
at /home/matesoos/development/solidity/develop/libsolidity/interface/CompilerStack.cpp:1962
#11 0x00005555558774d3 in solidity::frontend::CompilerStack::generateIR (this=this@entry=0x55555649a6c0, _contract=..., _unoptimizedOnly=)
at /home/matesoos/development/solidity/develop/libsolidity/interface/CompilerStack.cpp:1651
#12 0x000055555587d1e1 in solidity::frontend::CompilerStack::compile (this=0x55555649a6c0, _stopAfter=)
at /home/matesoos/development/solidity/develop/libsolidity/interface/CompilerStack.cpp:806
#13 0x0000555555762a31 in solidity::frontend::CommandLineInterface::compile (this=this@entry=0x7fffffffc7b0) at /usr/include/c++/15.2.1/bits/unique_ptr.h:192
#14 0x000055555576f320 in solidity::frontend::CommandLineInterface::processInput (this=0x7fffffffc7b0) at /home/matesoos/development/solidity/develop/solc/CommandLineInterface.cpp:859
#15 0x000055555576f720 in solidity::frontend::CommandLineInterface::run (this=this@entry=0x7fffffffc7b0, _argc=_argc@entry=4, _argv=_argv@entry=0x7fffffffccd8)
at /home/matesoos/development/solidity/develop/solc/CommandLineInterface.cpp:766
#16 0x0000555555725341 in main (argc=4, argv=0x7fffffffccd8) at /home/matesoos/development/solidity/develop/solc/main.cpp:40
```

## Environment

- Compiler version: latest `develop`, i.e. 63131344df6e0038729fa9da4f04c1e88a2ab087
- Compilation pipeline (legacy, IR, EOF): default `--optimize --ir `
- Target EVM version (as per compiler settings): default latest
- Framework/IDE (e.g. Foundry, Hardhat, Remix): N/A
- EVM execution environment / backend / blockchain client: N/A
- Operating system: Linux

## Steps to Reproduce

Run as above.

Contributor guide

Open the contributing guide

Research direction

Start by running the reported solc command with the minimized input containing the invalid 0xF7 byte. Read libsolutil/JSON.cpp around jsonPrint and libsolidity/interface/CompilerStack.cpp around createMetadata and createCBORMetadata, following the supplied backtrace. Done means this input no longer terminates with the nlohmann::json invalid-UTF-8 exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.