diff --git a/Changelog.md b/Changelog.md index d3172cb06..86787337e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ Language Features: Compiler Features: + * C API (``libsolc`` / raw ``soljson.js``): Introduce ``solidity_free`` method which releases all internal buffers to save memory. Bugfixes: diff --git a/libsolc/libsolc.cpp b/libsolc/libsolc.cpp index 766e1c0cd..f20b50d46 100644 --- a/libsolc/libsolc.cpp +++ b/libsolc/libsolc.cpp @@ -98,4 +98,8 @@ extern char const* solidity_compile(char const* _input, CStyleReadFileCallback _ s_outputBuffer = compile(_input, _readCallback); return s_outputBuffer.c_str(); } +extern void solidity_free() noexcept +{ + s_outputBuffer.clear(); +} } diff --git a/libsolc/libsolc.h b/libsolc/libsolc.h index baa39fd44..2c55c2342 100644 --- a/libsolc/libsolc.h +++ b/libsolc/libsolc.h @@ -55,6 +55,11 @@ char const* solidity_version() SOLC_NOEXCEPT; /// The pointer returned must not be freed by the caller. char const* solidity_compile(char const* _input, CStyleReadFileCallback _readCallback) SOLC_NOEXCEPT; +/// Frees up any allocated memory. +/// +/// NOTE: the pointer returned by solidity_compile is invalid after calling this! +void solidity_free() SOLC_NOEXCEPT; + #ifdef __cplusplus } #endif diff --git a/test/libsolidity/LibSolc.cpp b/test/libsolidity/LibSolc.cpp index ec97f22f0..62ec32f76 100644 --- a/test/libsolidity/LibSolc.cpp +++ b/test/libsolidity/LibSolc.cpp @@ -45,6 +45,7 @@ Json::Value compile(string const& _input) string output(solidity_compile(_input.c_str(), nullptr)); Json::Value ret; BOOST_REQUIRE(jsonParseStrict(output, ret)); + solidity_free(); return ret; } @@ -56,12 +57,14 @@ BOOST_AUTO_TEST_CASE(read_version) { string output(solidity_version()); BOOST_CHECK(output.find(VersionString) == 0); + solidity_free(); } BOOST_AUTO_TEST_CASE(read_license) { string output(solidity_license()); BOOST_CHECK(output.find("GNU GENERAL PUBLIC LICENSE") != string::npos); + solidity_free(); } BOOST_AUTO_TEST_CASE(standard_compilation) diff --git a/test/tools/fuzzer_common.cpp b/test/tools/fuzzer_common.cpp index b661cb54f..0ad86e6ea 100644 --- a/test/tools/fuzzer_common.cpp +++ b/test/tools/fuzzer_common.cpp @@ -35,6 +35,10 @@ void FuzzerUtil::runCompiler(string const& _input, bool _quiet) string outputString(solidity_compile(_input.c_str(), nullptr)); if (!_quiet) cout << "Output JSON: " << outputString << endl; + + // This should be safe given the above copies the output. + solidity_free(); + Json::Value output; if (!jsonParseStrict(outputString, output)) {